Sun Jul 31 18:39:00 2011 UTC ()
simple_lock to mutex conversion.


(jakllsch)
diff -r1.142 -r1.143 src/sys/dev/ic/ncr53c9x.c
diff -r1.54 -r1.55 src/sys/dev/ic/ncr53c9xvar.h

cvs diff -r1.142 -r1.143 src/sys/dev/ic/ncr53c9x.c (switch to unified diff)

--- src/sys/dev/ic/ncr53c9x.c 2011/07/04 16:06:17 1.142
+++ src/sys/dev/ic/ncr53c9x.c 2011/07/31 18:39:00 1.143
@@ -1,2986 +1,2978 @@ @@ -1,2986 +1,2978 @@
1/* $NetBSD: ncr53c9x.c,v 1.142 2011/07/04 16:06:17 joerg Exp $ */ 1/* $NetBSD: ncr53c9x.c,v 1.143 2011/07/31 18:39:00 jakllsch Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum. 8 * by Charles M. Hannum.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32/* 32/*
33 * Copyright (c) 1994 Peter Galbavy 33 * Copyright (c) 1994 Peter Galbavy
34 * Copyright (c) 1995 Paul Kranenburg 34 * Copyright (c) 1995 Paul Kranenburg
35 * All rights reserved. 35 * All rights reserved.
36 * 36 *
37 * Redistribution and use in source and binary forms, with or without 37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions 38 * modification, are permitted provided that the following conditions
39 * are met: 39 * are met:
40 * 1. Redistributions of source code must retain the above copyright 40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer. 41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright 42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the 43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution. 44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software 45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement: 46 * must display the following acknowledgement:
47 * This product includes software developed by Peter Galbavy 47 * This product includes software developed by Peter Galbavy
48 * 4. The name of the author may not be used to endorse or promote products 48 * 4. The name of the author may not be used to endorse or promote products
49 * derived from this software without specific prior written permission. 49 * derived from this software without specific prior written permission.
50 * 50 *
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 54 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 55 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
60 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 60 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE. 61 * POSSIBILITY OF SUCH DAMAGE.
62 */ 62 */
63 63
64/* 64/*
65 * Based on aic6360 by Jarle Greipsland 65 * Based on aic6360 by Jarle Greipsland
66 * 66 *
67 * Acknowledgements: Many of the algorithms used in this driver are 67 * Acknowledgements: Many of the algorithms used in this driver are
68 * inspired by the work of Julian Elischer (julian@tfs.com) and 68 * inspired by the work of Julian Elischer (julian@tfs.com) and
69 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million! 69 * Charles Hannum (mycroft@duality.gnu.ai.mit.edu). Thanks a million!
70 */ 70 */
71 71
72#include <sys/cdefs.h> 72#include <sys/cdefs.h>
73__KERNEL_RCSID(0, "$NetBSD: ncr53c9x.c,v 1.142 2011/07/04 16:06:17 joerg Exp $"); 73__KERNEL_RCSID(0, "$NetBSD: ncr53c9x.c,v 1.143 2011/07/31 18:39:00 jakllsch Exp $");
74 74
75#include <sys/param.h> 75#include <sys/param.h>
76#include <sys/systm.h> 76#include <sys/systm.h>
77#include <sys/callout.h> 77#include <sys/callout.h>
78#include <sys/kernel.h> 78#include <sys/kernel.h>
79#include <sys/errno.h> 79#include <sys/errno.h>
80#include <sys/ioctl.h> 80#include <sys/ioctl.h>
81#include <sys/device.h> 81#include <sys/device.h>
82#include <sys/buf.h> 82#include <sys/buf.h>
83#include <sys/malloc.h> 83#include <sys/malloc.h>
84#include <sys/proc.h> 84#include <sys/proc.h>
85#include <sys/queue.h> 85#include <sys/queue.h>
86#include <sys/pool.h> 86#include <sys/pool.h>
87#include <sys/scsiio.h> 87#include <sys/scsiio.h>
88 88
89#include <dev/scsipi/scsi_spc.h> 89#include <dev/scsipi/scsi_spc.h>
90#include <dev/scsipi/scsi_all.h> 90#include <dev/scsipi/scsi_all.h>
91#include <dev/scsipi/scsipi_all.h> 91#include <dev/scsipi/scsipi_all.h>
92#include <dev/scsipi/scsiconf.h> 92#include <dev/scsipi/scsiconf.h>
93#include <dev/scsipi/scsi_message.h> 93#include <dev/scsipi/scsi_message.h>
94 94
95#include <dev/ic/ncr53c9xreg.h> 95#include <dev/ic/ncr53c9xreg.h>
96#include <dev/ic/ncr53c9xvar.h> 96#include <dev/ic/ncr53c9xvar.h>
97 97
98int ncr53c9x_debug = NCR_SHOWMISC; /*NCR_SHOWPHASE|NCR_SHOWMISC|NCR_SHOWTRAC|NCR_SHOWCMDS;*/ 98int ncr53c9x_debug = NCR_SHOWMISC; /*NCR_SHOWPHASE|NCR_SHOWMISC|NCR_SHOWTRAC|NCR_SHOWCMDS;*/
99#ifdef DEBUG 99#ifdef DEBUG
100int ncr53c9x_notag = 0; 100int ncr53c9x_notag = 0;
101#endif 101#endif
102 102
103static void ncr53c9x_readregs(struct ncr53c9x_softc *); 103static void ncr53c9x_readregs(struct ncr53c9x_softc *);
104static void ncr53c9x_select(struct ncr53c9x_softc *, struct ncr53c9x_ecb *); 104static void ncr53c9x_select(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
105static int ncr53c9x_reselect(struct ncr53c9x_softc *, int, int, int); 105static int ncr53c9x_reselect(struct ncr53c9x_softc *, int, int, int);
106#if 0 106#if 0
107static void ncr53c9x_scsi_reset(struct ncr53c9x_softc *); 107static void ncr53c9x_scsi_reset(struct ncr53c9x_softc *);
108#endif 108#endif
109static void ncr53c9x_clear(struct ncr53c9x_softc *, scsipi_xfer_result_t); 109static void ncr53c9x_clear(struct ncr53c9x_softc *, scsipi_xfer_result_t);
110static int ncr53c9x_poll(struct ncr53c9x_softc *, 110static int ncr53c9x_poll(struct ncr53c9x_softc *,
111 struct scsipi_xfer *, int); 111 struct scsipi_xfer *, int);
112static void ncr53c9x_sched(struct ncr53c9x_softc *); 112static void ncr53c9x_sched(struct ncr53c9x_softc *);
113static void ncr53c9x_done(struct ncr53c9x_softc *, struct ncr53c9x_ecb *); 113static void ncr53c9x_done(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
114static void ncr53c9x_msgin(struct ncr53c9x_softc *); 114static void ncr53c9x_msgin(struct ncr53c9x_softc *);
115static void ncr53c9x_msgout(struct ncr53c9x_softc *); 115static void ncr53c9x_msgout(struct ncr53c9x_softc *);
116static void ncr53c9x_timeout(void *arg); 116static void ncr53c9x_timeout(void *arg);
117static void ncr53c9x_watch(void *arg); 117static void ncr53c9x_watch(void *arg);
118static void ncr53c9x_dequeue(struct ncr53c9x_softc *, 118static void ncr53c9x_dequeue(struct ncr53c9x_softc *,
119 struct ncr53c9x_ecb *); 119 struct ncr53c9x_ecb *);
120static int ncr53c9x_ioctl(struct scsipi_channel *, u_long, 120static int ncr53c9x_ioctl(struct scsipi_channel *, u_long,
121 void *, int, struct proc *); 121 void *, int, struct proc *);
122 122
123void ncr53c9x_sense(struct ncr53c9x_softc *, struct ncr53c9x_ecb *); 123void ncr53c9x_sense(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
124void ncr53c9x_free_ecb(struct ncr53c9x_softc *, struct ncr53c9x_ecb *); 124void ncr53c9x_free_ecb(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
125struct ncr53c9x_ecb *ncr53c9x_get_ecb(struct ncr53c9x_softc *, int); 125struct ncr53c9x_ecb *ncr53c9x_get_ecb(struct ncr53c9x_softc *, int);
126 126
127static inline int ncr53c9x_stp2cpb(struct ncr53c9x_softc *, int); 127static inline int ncr53c9x_stp2cpb(struct ncr53c9x_softc *, int);
128static inline void ncr53c9x_setsync(struct ncr53c9x_softc *, 128static inline void ncr53c9x_setsync(struct ncr53c9x_softc *,
129 struct ncr53c9x_tinfo *); 129 struct ncr53c9x_tinfo *);
130void ncr53c9x_update_xfer_mode (struct ncr53c9x_softc *, int); 130void ncr53c9x_update_xfer_mode (struct ncr53c9x_softc *, int);
131static struct ncr53c9x_linfo *ncr53c9x_lunsearch(struct ncr53c9x_tinfo *, 131static struct ncr53c9x_linfo *ncr53c9x_lunsearch(struct ncr53c9x_tinfo *,
132 int64_t lun); 132 int64_t lun);
133 133
134static void ncr53c9x_wrfifo(struct ncr53c9x_softc *, uint8_t *, int); 134static void ncr53c9x_wrfifo(struct ncr53c9x_softc *, uint8_t *, int);
135 135
136static int ncr53c9x_rdfifo(struct ncr53c9x_softc *, int); 136static int ncr53c9x_rdfifo(struct ncr53c9x_softc *, int);
137#define NCR_RDFIFO_START 0 137#define NCR_RDFIFO_START 0
138#define NCR_RDFIFO_CONTINUE 1 138#define NCR_RDFIFO_CONTINUE 1
139 139
140 140
141#define NCR_SET_COUNT(sc, size) do { \ 141#define NCR_SET_COUNT(sc, size) do { \
142 NCR_WRITE_REG((sc), NCR_TCL, (size)); \ 142 NCR_WRITE_REG((sc), NCR_TCL, (size)); \
143 NCR_WRITE_REG((sc), NCR_TCM, (size) >> 8); \ 143 NCR_WRITE_REG((sc), NCR_TCM, (size) >> 8); \
144 if ((sc->sc_cfg2 & NCRCFG2_FE) || \ 144 if ((sc->sc_cfg2 & NCRCFG2_FE) || \
145 (sc->sc_rev == NCR_VARIANT_FAS366)) { \ 145 (sc->sc_rev == NCR_VARIANT_FAS366)) { \
146 NCR_WRITE_REG((sc), NCR_TCH, (size) >> 16); \ 146 NCR_WRITE_REG((sc), NCR_TCH, (size) >> 16); \
147 } \ 147 } \
148 if (sc->sc_rev == NCR_VARIANT_FAS366) { \ 148 if (sc->sc_rev == NCR_VARIANT_FAS366) { \
149 NCR_WRITE_REG(sc, NCR_RCH, 0); \ 149 NCR_WRITE_REG(sc, NCR_RCH, 0); \
150 } \ 150 } \
151} while (/* CONSTCOND */0) 151} while (/* CONSTCOND */0)
152 152
153static int ecb_pool_initialized = 0; 153static int ecb_pool_initialized = 0;
154static struct pool ecb_pool; 154static struct pool ecb_pool;
155 155
156/* 156/*
157 * Names for the NCR53c9x variants, corresponding to the variant tags 157 * Names for the NCR53c9x variants, corresponding to the variant tags
158 * in ncr53c9xvar.h. 158 * in ncr53c9xvar.h.
159 */ 159 */
160static const char *ncr53c9x_variant_names[] = { 160static const char *ncr53c9x_variant_names[] = {
161 "ESP100", 161 "ESP100",
162 "ESP100A", 162 "ESP100A",
163 "ESP200", 163 "ESP200",
164 "NCR53C94", 164 "NCR53C94",
165 "NCR53C96", 165 "NCR53C96",
166 "ESP406", 166 "ESP406",
167 "FAS408", 167 "FAS408",
168 "FAS216", 168 "FAS216",
169 "AM53C974", 169 "AM53C974",
170 "FAS366/HME", 170 "FAS366/HME",
171 "NCR53C90 (86C01)", 171 "NCR53C90 (86C01)",
172}; 172};
173 173
174/* 174/*
175 * Search linked list for LUN info by LUN id. 175 * Search linked list for LUN info by LUN id.
176 */ 176 */
177static struct ncr53c9x_linfo * 177static struct ncr53c9x_linfo *
178ncr53c9x_lunsearch(struct ncr53c9x_tinfo *ti, int64_t lun) 178ncr53c9x_lunsearch(struct ncr53c9x_tinfo *ti, int64_t lun)
179{ 179{
180 struct ncr53c9x_linfo *li; 180 struct ncr53c9x_linfo *li;
181 181
182 LIST_FOREACH(li, &ti->luns, link) 182 LIST_FOREACH(li, &ti->luns, link)
183 if (li->lun == lun) 183 if (li->lun == lun)
184 return li; 184 return li;
185 return NULL; 185 return NULL;
186} 186}
187 187
188/* 188/*
189 * Attach this instance, and then all the sub-devices 189 * Attach this instance, and then all the sub-devices
190 */ 190 */
191void 191void
192ncr53c9x_attach(struct ncr53c9x_softc *sc) 192ncr53c9x_attach(struct ncr53c9x_softc *sc)
193{ 193{
194 struct scsipi_adapter *adapt = &sc->sc_adapter; 194 struct scsipi_adapter *adapt = &sc->sc_adapter;
195 struct scsipi_channel *chan = &sc->sc_channel; 195 struct scsipi_channel *chan = &sc->sc_channel;
196 196
197 simple_lock_init(&sc->sc_lock); 197 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_BIO);
198 198
199 callout_init(&sc->sc_watchdog, 0); 199 callout_init(&sc->sc_watchdog, 0);
200 200
201 /* 201 /*
202 * Note, the front-end has set us up to print the chip variation. 202 * Note, the front-end has set us up to print the chip variation.
203 */ 203 */
204 if (sc->sc_rev >= NCR_VARIANT_MAX) { 204 if (sc->sc_rev >= NCR_VARIANT_MAX) {
205 aprint_error(": unknown variant %d, devices not attached\n", 205 aprint_error(": unknown variant %d, devices not attached\n",
206 sc->sc_rev); 206 sc->sc_rev);
207 return; 207 return;
208 } 208 }
209 209
210 aprint_normal(": %s, %dMHz, SCSI ID %d\n", 210 aprint_normal(": %s, %dMHz, SCSI ID %d\n",
211 ncr53c9x_variant_names[sc->sc_rev], sc->sc_freq, sc->sc_id); 211 ncr53c9x_variant_names[sc->sc_rev], sc->sc_freq, sc->sc_id);
212 212
213 sc->sc_ntarg = (sc->sc_rev == NCR_VARIANT_FAS366) ? 16 : 8; 213 sc->sc_ntarg = (sc->sc_rev == NCR_VARIANT_FAS366) ? 16 : 8;
214 214
215 /* 215 /*
216 * Allocate SCSI message buffers. 216 * Allocate SCSI message buffers.
217 * Front-ends can override allocation to avoid alignment 217 * Front-ends can override allocation to avoid alignment
218 * handling in the DMA engines. Note that that ncr53c9x_msgout() 218 * handling in the DMA engines. Note that that ncr53c9x_msgout()
219 * can request a 1 byte DMA transfer. 219 * can request a 1 byte DMA transfer.
220 */ 220 */
221 if (sc->sc_omess == NULL) 221 if (sc->sc_omess == NULL)
222 sc->sc_omess = malloc(NCR_MAX_MSG_LEN, M_DEVBUF, M_NOWAIT); 222 sc->sc_omess = malloc(NCR_MAX_MSG_LEN, M_DEVBUF, M_NOWAIT);
223 223
224 if (sc->sc_imess == NULL) 224 if (sc->sc_imess == NULL)
225 sc->sc_imess = malloc(NCR_MAX_MSG_LEN + 1, M_DEVBUF, M_NOWAIT); 225 sc->sc_imess = malloc(NCR_MAX_MSG_LEN + 1, M_DEVBUF, M_NOWAIT);
226 226
227 sc->sc_tinfo = malloc(sc->sc_ntarg * sizeof(sc->sc_tinfo[0]), 227 sc->sc_tinfo = malloc(sc->sc_ntarg * sizeof(sc->sc_tinfo[0]),
228 M_DEVBUF, M_NOWAIT | M_ZERO); 228 M_DEVBUF, M_NOWAIT | M_ZERO);
229 229
230 if (sc->sc_omess == NULL || sc->sc_imess == NULL || 230 if (sc->sc_omess == NULL || sc->sc_imess == NULL ||
231 sc->sc_tinfo == NULL) { 231 sc->sc_tinfo == NULL) {
232 aprint_error_dev(sc->sc_dev, "out of memory\n"); 232 aprint_error_dev(sc->sc_dev, "out of memory\n");
233 return; 233 return;
234 } 234 }
235 235
236 /* 236 /*
237 * Treat NCR53C90 with the 86C01 DMA chip exactly as ESP100 237 * Treat NCR53C90 with the 86C01 DMA chip exactly as ESP100
238 * from now on. 238 * from now on.
239 */ 239 */
240 if (sc->sc_rev == NCR_VARIANT_NCR53C90_86C01) 240 if (sc->sc_rev == NCR_VARIANT_NCR53C90_86C01)
241 sc->sc_rev = NCR_VARIANT_ESP100; 241 sc->sc_rev = NCR_VARIANT_ESP100;
242 242
243 sc->sc_ccf = FREQTOCCF(sc->sc_freq); 243 sc->sc_ccf = FREQTOCCF(sc->sc_freq);
244 244
245 /* The value *must not* be == 1. Make it 2 */ 245 /* The value *must not* be == 1. Make it 2 */
246 if (sc->sc_ccf == 1) 246 if (sc->sc_ccf == 1)
247 sc->sc_ccf = 2; 247 sc->sc_ccf = 2;
248 248
249 /* 249 /*
250 * The recommended timeout is 250ms. This register is loaded 250 * The recommended timeout is 250ms. This register is loaded
251 * with a value calculated as follows, from the docs: 251 * with a value calculated as follows, from the docs:
252 * 252 *
253 * (timout period) x (CLK frequency) 253 * (timout period) x (CLK frequency)
254 * reg = ------------------------------------- 254 * reg = -------------------------------------
255 * 8192 x (Clock Conversion Factor) 255 * 8192 x (Clock Conversion Factor)
256 * 256 *
257 * Since CCF has a linear relation to CLK, this generally computes 257 * Since CCF has a linear relation to CLK, this generally computes
258 * to the constant of 153. 258 * to the constant of 153.
259 */ 259 */
260 sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf); 260 sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf);
261 261
262 /* CCF register only has 3 bits; 0 is actually 8 */ 262 /* CCF register only has 3 bits; 0 is actually 8 */
263 sc->sc_ccf &= 7; 263 sc->sc_ccf &= 7;
264 264
265 /* 265 /*
266 * Fill in the scsipi_adapter. 266 * Fill in the scsipi_adapter.
267 */ 267 */
268 adapt->adapt_dev = sc->sc_dev; 268 adapt->adapt_dev = sc->sc_dev;
269 adapt->adapt_nchannels = 1; 269 adapt->adapt_nchannels = 1;
270 adapt->adapt_openings = 256; 270 adapt->adapt_openings = 256;
271 adapt->adapt_max_periph = 256; 271 adapt->adapt_max_periph = 256;
272 adapt->adapt_ioctl = ncr53c9x_ioctl; 272 adapt->adapt_ioctl = ncr53c9x_ioctl;
273 /* adapt_request initialized by front-end */ 273 /* adapt_request initialized by front-end */
274 /* adapt_minphys initialized by front-end */ 274 /* adapt_minphys initialized by front-end */
275 275
276 /* 276 /*
277 * Fill in the scsipi_channel. 277 * Fill in the scsipi_channel.
278 */ 278 */
279 memset(chan, 0, sizeof(*chan)); 279 memset(chan, 0, sizeof(*chan));
280 chan->chan_adapter = adapt; 280 chan->chan_adapter = adapt;
281 chan->chan_bustype = &scsi_bustype; 281 chan->chan_bustype = &scsi_bustype;
282 chan->chan_channel = 0; 282 chan->chan_channel = 0;
283 chan->chan_ntargets = sc->sc_ntarg; 283 chan->chan_ntargets = sc->sc_ntarg;
284 chan->chan_nluns = 8; 284 chan->chan_nluns = 8;
285 chan->chan_id = sc->sc_id; 285 chan->chan_id = sc->sc_id;
286 286
287 /* 287 /*
288 * Add reference to adapter so that we drop the reference after 288 * Add reference to adapter so that we drop the reference after
289 * config_found() to make sure the adatper is disabled. 289 * config_found() to make sure the adatper is disabled.
290 */ 290 */
291 if (scsipi_adapter_addref(adapt) != 0) { 291 if (scsipi_adapter_addref(adapt) != 0) {
292 aprint_error_dev(sc->sc_dev, "unable to enable controller\n"); 292 aprint_error_dev(sc->sc_dev, "unable to enable controller\n");
293 return; 293 return;
294 } 294 }
295 295
296 /* Reset state & bus */ 296 /* Reset state & bus */
297 sc->sc_cfflags = device_cfdata(sc->sc_dev)->cf_flags; 297 sc->sc_cfflags = device_cfdata(sc->sc_dev)->cf_flags;
298 sc->sc_state = 0; 298 sc->sc_state = 0;
299 ncr53c9x_init(sc, 1); 299 ncr53c9x_init(sc, 1);
300 300
301 /* 301 /*
302 * Now try to attach all the sub-devices 302 * Now try to attach all the sub-devices
303 */ 303 */
304 sc->sc_child = config_found(sc->sc_dev, &sc->sc_channel, scsiprint); 304 sc->sc_child = config_found(sc->sc_dev, &sc->sc_channel, scsiprint);
305 305
306 scsipi_adapter_delref(adapt); 306 scsipi_adapter_delref(adapt);
307 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc); 307 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc);
308} 308}
309 309
310int 310int
311ncr53c9x_detach(struct ncr53c9x_softc *sc, int flags) 311ncr53c9x_detach(struct ncr53c9x_softc *sc, int flags)
312{ 312{
313 struct ncr53c9x_linfo *li, *nextli; 313 struct ncr53c9x_linfo *li, *nextli;
314 int t; 314 int t;
315 int error; 315 int error;
316 316
317 callout_stop(&sc->sc_watchdog); 317 callout_stop(&sc->sc_watchdog);
318 318
319 if (sc->sc_tinfo) { 319 if (sc->sc_tinfo) {
320 /* Cancel all commands. */ 320 /* Cancel all commands. */
321 ncr53c9x_clear(sc, XS_DRIVER_STUFFUP); 321 ncr53c9x_clear(sc, XS_DRIVER_STUFFUP);
322 322
323 /* Free logical units. */ 323 /* Free logical units. */
324 for (t = 0; t < sc->sc_ntarg; t++) { 324 for (t = 0; t < sc->sc_ntarg; t++) {
325 for (li = LIST_FIRST(&sc->sc_tinfo[t].luns); li; 325 for (li = LIST_FIRST(&sc->sc_tinfo[t].luns); li;
326 li = nextli) { 326 li = nextli) {
327 nextli = LIST_NEXT(li, link); 327 nextli = LIST_NEXT(li, link);
328 free(li, M_DEVBUF); 328 free(li, M_DEVBUF);
329 } 329 }
330 } 330 }
331 } 331 }
332 332
333 if (sc->sc_child) { 333 if (sc->sc_child) {
334 error = config_detach(sc->sc_child, flags); 334 error = config_detach(sc->sc_child, flags);
335 if (error) 335 if (error)
336 return error; 336 return error;
337 } 337 }
338 338
339 if (sc->sc_imess) 339 if (sc->sc_imess)
340 free(sc->sc_imess, M_DEVBUF); 340 free(sc->sc_imess, M_DEVBUF);
341 if (sc->sc_omess) 341 if (sc->sc_omess)
342 free(sc->sc_omess, M_DEVBUF); 342 free(sc->sc_omess, M_DEVBUF);
343 343
 344 mutex_destroy(&sc->sc_lock);
 345
344 return 0; 346 return 0;
345} 347}
346 348
347/* 349/*
348 * This is the generic ncr53c9x reset function. It does not reset the SCSI bus, 350 * This is the generic ncr53c9x reset function. It does not reset the SCSI bus,
349 * only this controller, but kills any on-going commands, and also stops 351 * only this controller, but kills any on-going commands, and also stops
350 * and resets the DMA. 352 * and resets the DMA.
351 * 353 *
352 * After reset, registers are loaded with the defaults from the attach 354 * After reset, registers are loaded with the defaults from the attach
353 * routine above. 355 * routine above.
354 */ 356 */
355void 357void
356ncr53c9x_reset(struct ncr53c9x_softc *sc) 358ncr53c9x_reset(struct ncr53c9x_softc *sc)
357{ 359{
358 360
359 /* reset DMA first */ 361 /* reset DMA first */
360 NCRDMA_RESET(sc); 362 NCRDMA_RESET(sc);
361 363
362 /* reset SCSI chip */ 364 /* reset SCSI chip */
363 NCRCMD(sc, NCRCMD_RSTCHIP); 365 NCRCMD(sc, NCRCMD_RSTCHIP);
364 NCRCMD(sc, NCRCMD_NOP); 366 NCRCMD(sc, NCRCMD_NOP);
365 DELAY(500); 367 DELAY(500);
366 368
367 /* do these backwards, and fall through */ 369 /* do these backwards, and fall through */
368 switch (sc->sc_rev) { 370 switch (sc->sc_rev) {
369 case NCR_VARIANT_ESP406: 371 case NCR_VARIANT_ESP406:
370 case NCR_VARIANT_FAS408: 372 case NCR_VARIANT_FAS408:
371 NCR_WRITE_REG(sc, NCR_CFG5, sc->sc_cfg5 | NCRCFG5_SINT); 373 NCR_WRITE_REG(sc, NCR_CFG5, sc->sc_cfg5 | NCRCFG5_SINT);
372 NCR_WRITE_REG(sc, NCR_CFG4, sc->sc_cfg4); 374 NCR_WRITE_REG(sc, NCR_CFG4, sc->sc_cfg4);
373 case NCR_VARIANT_AM53C974: 375 case NCR_VARIANT_AM53C974:
374 case NCR_VARIANT_FAS216: 376 case NCR_VARIANT_FAS216:
375 case NCR_VARIANT_NCR53C94: 377 case NCR_VARIANT_NCR53C94:
376 case NCR_VARIANT_NCR53C96: 378 case NCR_VARIANT_NCR53C96:
377 case NCR_VARIANT_ESP200: 379 case NCR_VARIANT_ESP200:
378 sc->sc_features |= NCR_F_HASCFG3; 380 sc->sc_features |= NCR_F_HASCFG3;
379 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3); 381 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
380 case NCR_VARIANT_ESP100A: 382 case NCR_VARIANT_ESP100A:
381 sc->sc_features |= NCR_F_SELATN3; 383 sc->sc_features |= NCR_F_SELATN3;
382 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2); 384 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
383 case NCR_VARIANT_ESP100: 385 case NCR_VARIANT_ESP100:
384 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1); 386 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
385 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf); 387 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
386 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0); 388 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
387 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout); 389 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
388 break; 390 break;
389 391
390 case NCR_VARIANT_FAS366: 392 case NCR_VARIANT_FAS366:
391 sc->sc_features |= 393 sc->sc_features |=
392 NCR_F_HASCFG3 | NCR_F_FASTSCSI | NCR_F_SELATN3; 394 NCR_F_HASCFG3 | NCR_F_FASTSCSI | NCR_F_SELATN3;
393 sc->sc_cfg3 = NCRFASCFG3_FASTCLK | NCRFASCFG3_OBAUTO; 395 sc->sc_cfg3 = NCRFASCFG3_FASTCLK | NCRFASCFG3_OBAUTO;
394 sc->sc_cfg3_fscsi = NCRFASCFG3_FASTSCSI; 396 sc->sc_cfg3_fscsi = NCRFASCFG3_FASTSCSI;
395 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3); 397 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
396 sc->sc_cfg2 = 0; /* NCRCFG2_HMEFE| NCRCFG2_HME32 */ 398 sc->sc_cfg2 = 0; /* NCRCFG2_HMEFE| NCRCFG2_HME32 */
397 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2); 399 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
398 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1); 400 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
399 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf); 401 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
400 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0); 402 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
401 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout); 403 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
402 break; 404 break;
403 405
404 default: 406 default:
405 printf("%s: unknown revision code, assuming ESP100\n", 407 printf("%s: unknown revision code, assuming ESP100\n",
406 device_xname(sc->sc_dev)); 408 device_xname(sc->sc_dev));
407 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1); 409 NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
408 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf); 410 NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
409 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0); 411 NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
410 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout); 412 NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
411 } 413 }
412 414
413 if (sc->sc_rev == NCR_VARIANT_AM53C974) 415 if (sc->sc_rev == NCR_VARIANT_AM53C974)
414 NCR_WRITE_REG(sc, NCR_AMDCFG4, sc->sc_cfg4); 416 NCR_WRITE_REG(sc, NCR_AMDCFG4, sc->sc_cfg4);
415 417
416#if 0 418#if 0
417 printf("%s: ncr53c9x_reset: revision %d\n", 419 printf("%s: ncr53c9x_reset: revision %d\n",
418 device_xname(sc->sc_dev), sc->sc_rev); 420 device_xname(sc->sc_dev), sc->sc_rev);
419 printf("%s: ncr53c9x_reset: cfg1 0x%x, cfg2 0x%x, cfg3 0x%x, " 421 printf("%s: ncr53c9x_reset: cfg1 0x%x, cfg2 0x%x, cfg3 0x%x, "
420 "ccf 0x%x, timeout 0x%x\n", 422 "ccf 0x%x, timeout 0x%x\n",
421 device_xname(sc->sc_dev), sc->sc_cfg1, sc->sc_cfg2, sc->sc_cfg3, 423 device_xname(sc->sc_dev), sc->sc_cfg1, sc->sc_cfg2, sc->sc_cfg3,
422 sc->sc_ccf, sc->sc_timeout); 424 sc->sc_ccf, sc->sc_timeout);
423#endif 425#endif
424} 426}
425 427
426#if 0 428#if 0
427/* 429/*
428 * Reset the SCSI bus, but not the chip 430 * Reset the SCSI bus, but not the chip
429 */ 431 */
430void 432void
431ncr53c9x_scsi_reset(struct ncr53c9x_softc *sc) 433ncr53c9x_scsi_reset(struct ncr53c9x_softc *sc)
432{ 434{
433 435
434 (*sc->sc_glue->gl_dma_stop)(sc); 436 (*sc->sc_glue->gl_dma_stop)(sc);
435 437
436 printf("%s: resetting SCSI bus\n", device_xname(sc->sc_dev)); 438 printf("%s: resetting SCSI bus\n", device_xname(sc->sc_dev));
437 NCRCMD(sc, NCRCMD_RSTSCSI); 439 NCRCMD(sc, NCRCMD_RSTSCSI);
438} 440}
439#endif 441#endif
440 442
441/* 443/*
442 * Clear all commands 444 * Clear all commands
443 */ 445 */
444void 446void
445ncr53c9x_clear(struct ncr53c9x_softc *sc, scsipi_xfer_result_t result) 447ncr53c9x_clear(struct ncr53c9x_softc *sc, scsipi_xfer_result_t result)
446{ 448{
447 struct ncr53c9x_ecb *ecb; 449 struct ncr53c9x_ecb *ecb;
448 struct ncr53c9x_linfo *li; 450 struct ncr53c9x_linfo *li;
449 int i, r; 451 int i, r;
450 452
451 /* Cancel any active commands. */ 453 /* Cancel any active commands. */
452 sc->sc_state = NCR_CLEANING; 454 sc->sc_state = NCR_CLEANING;
453 sc->sc_msgify = 0; 455 sc->sc_msgify = 0;
454 ecb = sc->sc_nexus; 456 ecb = sc->sc_nexus;
455 if (ecb != NULL) { 457 if (ecb != NULL) {
456 ecb->xs->error = result; 458 ecb->xs->error = result;
457 ncr53c9x_done(sc, ecb); 459 ncr53c9x_done(sc, ecb);
458 } 460 }
459 /* Cancel outstanding disconnected commands on each LUN */ 461 /* Cancel outstanding disconnected commands on each LUN */
460 for (r = 0; r < sc->sc_ntarg; r++) { 462 for (r = 0; r < sc->sc_ntarg; r++) {
461 LIST_FOREACH(li, &sc->sc_tinfo[r].luns, link) { 463 LIST_FOREACH(li, &sc->sc_tinfo[r].luns, link) {
462 ecb = li->untagged; 464 ecb = li->untagged;
463 if (ecb != NULL) { 465 if (ecb != NULL) {
464 li->untagged = NULL; 466 li->untagged = NULL;
465 /* 467 /*
466 * XXXXXXX 468 * XXXXXXX
467 * 469 *
468 * Should we terminate a command 470 * Should we terminate a command
469 * that never reached the disk? 471 * that never reached the disk?
470 */ 472 */
471 li->busy = 0; 473 li->busy = 0;
472 ecb->xs->error = result; 474 ecb->xs->error = result;
473 ncr53c9x_done(sc, ecb); 475 ncr53c9x_done(sc, ecb);
474 } 476 }
475 for (i = 0; i < 256; i++) { 477 for (i = 0; i < 256; i++) {
476 ecb = li->queued[i]; 478 ecb = li->queued[i];
477 if (ecb != NULL) { 479 if (ecb != NULL) {
478 li->queued[i] = NULL; 480 li->queued[i] = NULL;
479 ecb->xs->error = result; 481 ecb->xs->error = result;
480 ncr53c9x_done(sc, ecb); 482 ncr53c9x_done(sc, ecb);
481 } 483 }
482 } 484 }
483 li->used = 0; 485 li->used = 0;
484 } 486 }
485 } 487 }
486} 488}
487 489
488/* 490/*
489 * Initialize ncr53c9x state machine 491 * Initialize ncr53c9x state machine
490 */ 492 */
491void 493void
492ncr53c9x_init(struct ncr53c9x_softc *sc, int doreset) 494ncr53c9x_init(struct ncr53c9x_softc *sc, int doreset)
493{ 495{
494 int r; 496 int r;
495 497
496 NCR_MISC(("[NCR_INIT(%d) %d] ", doreset, sc->sc_state)); 498 NCR_MISC(("[NCR_INIT(%d) %d] ", doreset, sc->sc_state));
497 499
498 if (!ecb_pool_initialized) { 500 if (!ecb_pool_initialized) {
499 /* All instances share this pool */ 501 /* All instances share this pool */
500 pool_init(&ecb_pool, sizeof(struct ncr53c9x_ecb), 0, 0, 0, 502 pool_init(&ecb_pool, sizeof(struct ncr53c9x_ecb), 0, 0, 0,
501 "ncr53c9x_ecb", NULL, IPL_BIO); 503 "ncr53c9x_ecb", NULL, IPL_BIO);
502 /* make sure to always have some items to play with */ 504 /* make sure to always have some items to play with */
503 if (pool_prime(&ecb_pool, 1) == ENOMEM) { 505 if (pool_prime(&ecb_pool, 1) == ENOMEM) {
504 printf("WARNING: not enough memory for ncr53c9x_ecb\n"); 506 printf("WARNING: not enough memory for ncr53c9x_ecb\n");
505 } 507 }
506 ecb_pool_initialized = 1; 508 ecb_pool_initialized = 1;
507 } 509 }
508 510
509 if (sc->sc_state == 0) { 511 if (sc->sc_state == 0) {
510 /* First time through; initialize. */ 512 /* First time through; initialize. */
511 513
512 TAILQ_INIT(&sc->ready_list); 514 TAILQ_INIT(&sc->ready_list);
513 sc->sc_nexus = NULL; 515 sc->sc_nexus = NULL;
514 memset(sc->sc_tinfo, 0, sizeof(*sc->sc_tinfo)); 516 memset(sc->sc_tinfo, 0, sizeof(*sc->sc_tinfo));
515 for (r = 0; r < sc->sc_ntarg; r++) { 517 for (r = 0; r < sc->sc_ntarg; r++) {
516 LIST_INIT(&sc->sc_tinfo[r].luns); 518 LIST_INIT(&sc->sc_tinfo[r].luns);
517 } 519 }
518 } else { 520 } else {
519 ncr53c9x_clear(sc, XS_TIMEOUT); 521 ncr53c9x_clear(sc, XS_TIMEOUT);
520 } 522 }
521 523
522 /* 524 /*
523 * reset the chip to a known state 525 * reset the chip to a known state
524 */ 526 */
525 ncr53c9x_reset(sc); 527 ncr53c9x_reset(sc);
526 528
527 sc->sc_flags = 0; 529 sc->sc_flags = 0;
528 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0; 530 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
529 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE; 531 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
530 532
531 for (r = 0; r < sc->sc_ntarg; r++) { 533 for (r = 0; r < sc->sc_ntarg; r++) {
532 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[r]; 534 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[r];
533/* XXX - config flags per target: low bits: no reselect; high bits: no synch */ 535/* XXX - config flags per target: low bits: no reselect; high bits: no synch */
534 536
535 ti->flags = ((sc->sc_minsync && 537 ti->flags = ((sc->sc_minsync &&
536 !(sc->sc_cfflags & (1 << ((r & 7) + 8)))) ? 538 !(sc->sc_cfflags & (1 << ((r & 7) + 8)))) ?
537 0 : T_SYNCHOFF) | 539 0 : T_SYNCHOFF) |
538 ((sc->sc_cfflags & (1 << (r & 7))) ? T_RSELECTOFF : 0); 540 ((sc->sc_cfflags & (1 << (r & 7))) ? T_RSELECTOFF : 0);
539#ifdef DEBUG 541#ifdef DEBUG
540 if (ncr53c9x_notag) 542 if (ncr53c9x_notag)
541 ti->flags &= ~T_TAG; 543 ti->flags &= ~T_TAG;
542#endif 544#endif
543 ti->period = sc->sc_minsync; 545 ti->period = sc->sc_minsync;
544 ti->offset = 0; 546 ti->offset = 0;
545 ti->cfg3 = 0; 547 ti->cfg3 = 0;
546 548
547 ncr53c9x_update_xfer_mode(sc, r); 549 ncr53c9x_update_xfer_mode(sc, r);
548 } 550 }
549 551
550 if (doreset) { 552 if (doreset) {
551 sc->sc_state = NCR_SBR; 553 sc->sc_state = NCR_SBR;
552 NCRCMD(sc, NCRCMD_RSTSCSI); 554 NCRCMD(sc, NCRCMD_RSTSCSI);
553 } else { 555 } else {
554 sc->sc_state = NCR_IDLE; 556 sc->sc_state = NCR_IDLE;
555 ncr53c9x_sched(sc); 557 ncr53c9x_sched(sc);
556 } 558 }
557 559
558 /* Notify upper layer */ 560 /* Notify upper layer */
559 scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_RESET, NULL); 561 scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_RESET, NULL);
560} 562}
561 563
562/* 564/*
563 * Read the NCR registers, and save their contents for later use. 565 * Read the NCR registers, and save their contents for later use.
564 * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading 566 * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
565 * NCR_INTR - so make sure it is the last read. 567 * NCR_INTR - so make sure it is the last read.
566 * 568 *
567 * I think that (from reading the docs) most bits in these registers 569 * I think that (from reading the docs) most bits in these registers
568 * only make sense when he DMA CSR has an interrupt showing. Call only 570 * only make sense when he DMA CSR has an interrupt showing. Call only
569 * if an interrupt is pending. 571 * if an interrupt is pending.
570 */ 572 */
571inline void 573inline void
572ncr53c9x_readregs(struct ncr53c9x_softc *sc) 574ncr53c9x_readregs(struct ncr53c9x_softc *sc)
573{ 575{
574 576
575 sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT); 577 sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
576 /* Only the stepo bits are of interest */ 578 /* Only the stepo bits are of interest */
577 sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK; 579 sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
578 580
579 if (sc->sc_rev == NCR_VARIANT_FAS366) 581 if (sc->sc_rev == NCR_VARIANT_FAS366)
580 sc->sc_espstat2 = NCR_READ_REG(sc, NCR_STAT2); 582 sc->sc_espstat2 = NCR_READ_REG(sc, NCR_STAT2);
581 583
582 sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR); 584 sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
583 585
584 if (sc->sc_glue->gl_clear_latched_intr != NULL) 586 if (sc->sc_glue->gl_clear_latched_intr != NULL)
585 (*sc->sc_glue->gl_clear_latched_intr)(sc); 587 (*sc->sc_glue->gl_clear_latched_intr)(sc);
586 588
587 /* 589 /*
588 * Determine the SCSI bus phase, return either a real SCSI bus phase 590 * Determine the SCSI bus phase, return either a real SCSI bus phase
589 * or some pseudo phase we use to detect certain exceptions. 591 * or some pseudo phase we use to detect certain exceptions.
590 */ 592 */
591 593
592 sc->sc_phase = (sc->sc_espintr & NCRINTR_DIS) ? 594 sc->sc_phase = (sc->sc_espintr & NCRINTR_DIS) ?
593 /* Disconnected */ BUSFREE_PHASE : sc->sc_espstat & NCRSTAT_PHASE; 595 /* Disconnected */ BUSFREE_PHASE : sc->sc_espstat & NCRSTAT_PHASE;
594 596
595 NCR_INTS(("regs[intr=%02x,stat=%02x,step=%02x,stat2=%02x] ", 597 NCR_INTS(("regs[intr=%02x,stat=%02x,step=%02x,stat2=%02x] ",
596 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep, sc->sc_espstat2)); 598 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep, sc->sc_espstat2));
597} 599}
598 600
599/* 601/*
600 * Convert Synchronous Transfer Period to chip register Clock Per Byte value. 602 * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
601 */ 603 */
602static inline int 604static inline int
603ncr53c9x_stp2cpb(struct ncr53c9x_softc *sc, int period) 605ncr53c9x_stp2cpb(struct ncr53c9x_softc *sc, int period)
604{ 606{
605 int v; 607 int v;
606 608
607 v = (sc->sc_freq * period) / 250; 609 v = (sc->sc_freq * period) / 250;
608 if (ncr53c9x_cpb2stp(sc, v) < period) 610 if (ncr53c9x_cpb2stp(sc, v) < period)
609 /* Correct round-down error */ 611 /* Correct round-down error */
610 v++; 612 v++;
611 return v; 613 return v;
612} 614}
613 615
614static inline void 616static inline void
615ncr53c9x_setsync(struct ncr53c9x_softc *sc, struct ncr53c9x_tinfo *ti) 617ncr53c9x_setsync(struct ncr53c9x_softc *sc, struct ncr53c9x_tinfo *ti)
616{ 618{
617 uint8_t syncoff, synctp; 619 uint8_t syncoff, synctp;
618 uint8_t cfg3 = sc->sc_cfg3 | ti->cfg3; 620 uint8_t cfg3 = sc->sc_cfg3 | ti->cfg3;
619 621
620 if (ti->flags & T_SYNCMODE) { 622 if (ti->flags & T_SYNCMODE) {
621 syncoff = ti->offset; 623 syncoff = ti->offset;
622 synctp = ncr53c9x_stp2cpb(sc, ti->period); 624 synctp = ncr53c9x_stp2cpb(sc, ti->period);
623 if (sc->sc_features & NCR_F_FASTSCSI) { 625 if (sc->sc_features & NCR_F_FASTSCSI) {
624 /* 626 /*
625 * If the period is 200ns or less (ti->period <= 50), 627 * If the period is 200ns or less (ti->period <= 50),
626 * put the chip in Fast SCSI mode. 628 * put the chip in Fast SCSI mode.
627 */ 629 */
628 if (ti->period <= 50) 630 if (ti->period <= 50)
629 /* 631 /*
630 * There are (at least) 4 variations of the 632 * There are (at least) 4 variations of the
631 * configuration 3 register. The drive attach 633 * configuration 3 register. The drive attach
632 * routine sets the appropriate bit to put the 634 * routine sets the appropriate bit to put the
633 * chip into Fast SCSI mode so that it doesn't 635 * chip into Fast SCSI mode so that it doesn't
634 * have to be figured out here each time. 636 * have to be figured out here each time.
635 */ 637 */
636 cfg3 |= sc->sc_cfg3_fscsi; 638 cfg3 |= sc->sc_cfg3_fscsi;
637 } 639 }
638 640
639 /* 641 /*
640 * Am53c974 requires different SYNCTP values when the 642 * Am53c974 requires different SYNCTP values when the
641 * FSCSI bit is off. 643 * FSCSI bit is off.
642 */ 644 */
643 if (sc->sc_rev == NCR_VARIANT_AM53C974 && 645 if (sc->sc_rev == NCR_VARIANT_AM53C974 &&
644 (cfg3 & NCRAMDCFG3_FSCSI) == 0) 646 (cfg3 & NCRAMDCFG3_FSCSI) == 0)
645 synctp--; 647 synctp--;
646 } else { 648 } else {
647 syncoff = 0; 649 syncoff = 0;
648 synctp = 0; 650 synctp = 0;
649 } 651 }
650 652
651 if (sc->sc_features & NCR_F_HASCFG3) 653 if (sc->sc_features & NCR_F_HASCFG3)
652 NCR_WRITE_REG(sc, NCR_CFG3, cfg3); 654 NCR_WRITE_REG(sc, NCR_CFG3, cfg3);
653 655
654 NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff); 656 NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff);
655 NCR_WRITE_REG(sc, NCR_SYNCTP, synctp); 657 NCR_WRITE_REG(sc, NCR_SYNCTP, synctp);
656} 658}
657 659
658/* 660/*
659 * Send a command to a target, set the driver state to NCR_SELECTING 661 * Send a command to a target, set the driver state to NCR_SELECTING
660 * and let the caller take care of the rest. 662 * and let the caller take care of the rest.
661 * 663 *
662 * Keeping this as a function allows me to say that this may be done 664 * Keeping this as a function allows me to say that this may be done
663 * by DMA instead of programmed I/O soon. 665 * by DMA instead of programmed I/O soon.
664 */ 666 */
665void 667void
666ncr53c9x_select(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb) 668ncr53c9x_select(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
667{ 669{
668 struct scsipi_periph *periph = ecb->xs->xs_periph; 670 struct scsipi_periph *periph = ecb->xs->xs_periph;
669 int target = periph->periph_target; 671 int target = periph->periph_target;
670 int lun = periph->periph_lun; 672 int lun = periph->periph_lun;
671 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target]; 673 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
672 int tiflags = ti->flags; 674 int tiflags = ti->flags;
673 uint8_t *cmd; 675 uint8_t *cmd;
674 int clen; 676 int clen;
675 bool selatn3, selatns; 677 bool selatn3, selatns;
676 size_t dmasize; 678 size_t dmasize;
677 679
678 NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x,tag:%x,%x)] ", 680 NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x,tag:%x,%x)] ",
679 target, lun, ecb->cmd.cmd.opcode, ecb->tag[0], ecb->tag[1])); 681 target, lun, ecb->cmd.cmd.opcode, ecb->tag[0], ecb->tag[1]));
680 682
681 sc->sc_state = NCR_SELECTING; 683 sc->sc_state = NCR_SELECTING;
682 /* 684 /*
683 * Schedule the timeout now, the first time we will go away 685 * Schedule the timeout now, the first time we will go away
684 * expecting to come back due to an interrupt, because it is 686 * expecting to come back due to an interrupt, because it is
685 * always possible that the interrupt may never happen. 687 * always possible that the interrupt may never happen.
686 */ 688 */
687 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0) { 689 if ((ecb->xs->xs_control & XS_CTL_POLL) == 0) {
688 callout_reset(&ecb->xs->xs_callout, mstohz(ecb->timeout), 690 callout_reset(&ecb->xs->xs_callout, mstohz(ecb->timeout),
689 ncr53c9x_timeout, ecb); 691 ncr53c9x_timeout, ecb);
690 } 692 }
691 693
692 /* 694 /*
693 * The docs say the target register is never reset, and I 695 * The docs say the target register is never reset, and I
694 * can't think of a better place to set it 696 * can't think of a better place to set it
695 */ 697 */
696 if (sc->sc_rev == NCR_VARIANT_FAS366) { 698 if (sc->sc_rev == NCR_VARIANT_FAS366) {
697 NCRCMD(sc, NCRCMD_FLUSH); 699 NCRCMD(sc, NCRCMD_FLUSH);
698 NCR_WRITE_REG(sc, NCR_SELID, target | NCR_BUSID_HME); 700 NCR_WRITE_REG(sc, NCR_SELID, target | NCR_BUSID_HME);
699 } else { 701 } else {
700 NCR_WRITE_REG(sc, NCR_SELID, target); 702 NCR_WRITE_REG(sc, NCR_SELID, target);
701 } 703 }
702 ncr53c9x_setsync(sc, ti); 704 ncr53c9x_setsync(sc, ti);
703 705
704 if ((ecb->flags & ECB_SENSE) != 0) { 706 if ((ecb->flags & ECB_SENSE) != 0) {
705 /* 707 /*
706 * For REQUEST SENSE, we should not send an IDENTIFY or 708 * For REQUEST SENSE, we should not send an IDENTIFY or
707 * otherwise mangle the target. There should be no MESSAGE IN 709 * otherwise mangle the target. There should be no MESSAGE IN
708 * phase. 710 * phase.
709 */ 711 */
710 if (sc->sc_features & NCR_F_DMASELECT) { 712 if (sc->sc_features & NCR_F_DMASELECT) {
711 /* setup DMA transfer for command */ 713 /* setup DMA transfer for command */
712 dmasize = clen = ecb->clen; 714 dmasize = clen = ecb->clen;
713 sc->sc_cmdlen = clen; 715 sc->sc_cmdlen = clen;
714 sc->sc_cmdp = (void *)&ecb->cmd.cmd; 716 sc->sc_cmdp = (void *)&ecb->cmd.cmd;
715 717
716 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, 718 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0,
717 &dmasize); 719 &dmasize);
718 /* Program the SCSI counter */ 720 /* Program the SCSI counter */
719 NCR_SET_COUNT(sc, dmasize); 721 NCR_SET_COUNT(sc, dmasize);
720 722
721 if (sc->sc_rev != NCR_VARIANT_FAS366) 723 if (sc->sc_rev != NCR_VARIANT_FAS366)
722 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA); 724 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
723 725
724 /* And get the targets attention */ 726 /* And get the targets attention */
725 NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA); 727 NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA);
726 NCRDMA_GO(sc); 728 NCRDMA_GO(sc);
727 } else { 729 } else {
728 ncr53c9x_wrfifo(sc, (uint8_t *)&ecb->cmd.cmd, 730 ncr53c9x_wrfifo(sc, (uint8_t *)&ecb->cmd.cmd,
729 ecb->clen); 731 ecb->clen);
730 NCRCMD(sc, NCRCMD_SELNATN); 732 NCRCMD(sc, NCRCMD_SELNATN);
731 } 733 }
732 return; 734 return;
733 } 735 }
734 736
735 selatn3 = selatns = false; 737 selatn3 = selatns = false;
736 if (ecb->tag[0] != 0) { 738 if (ecb->tag[0] != 0) {
737 if (sc->sc_features & NCR_F_SELATN3) 739 if (sc->sc_features & NCR_F_SELATN3)
738 /* use SELATN3 to send tag messages */ 740 /* use SELATN3 to send tag messages */
739 selatn3 = true; 741 selatn3 = true;
740 else 742 else
741 /* We don't have SELATN3; use SELATNS to send tags */ 743 /* We don't have SELATN3; use SELATNS to send tags */
742 selatns = true; 744 selatns = true;
743 } 745 }
744 746
745 if (ti->flags & T_NEGOTIATE) { 747 if (ti->flags & T_NEGOTIATE) {
746 /* We have to use SELATNS to send sync/wide messages */ 748 /* We have to use SELATNS to send sync/wide messages */
747 selatn3 = false; 749 selatn3 = false;
748 selatns = true; 750 selatns = true;
749 } 751 }
750 752
751 cmd = (uint8_t *)&ecb->cmd.cmd; 753 cmd = (uint8_t *)&ecb->cmd.cmd;
752 754
753 if (selatn3) { 755 if (selatn3) {
754 /* We'll use tags with SELATN3 */ 756 /* We'll use tags with SELATN3 */
755 clen = ecb->clen + 3; 757 clen = ecb->clen + 3;
756 cmd -= 3; 758 cmd -= 3;
757 cmd[0] = MSG_IDENTIFY(lun, 1); /* msg[0] */ 759 cmd[0] = MSG_IDENTIFY(lun, 1); /* msg[0] */
758 cmd[1] = ecb->tag[0]; /* msg[1] */ 760 cmd[1] = ecb->tag[0]; /* msg[1] */
759 cmd[2] = ecb->tag[1]; /* msg[2] */ 761 cmd[2] = ecb->tag[1]; /* msg[2] */
760 } else { 762 } else {
761 /* We don't have tags, or will send messages with SELATNS */ 763 /* We don't have tags, or will send messages with SELATNS */
762 clen = ecb->clen + 1; 764 clen = ecb->clen + 1;
763 cmd -= 1; 765 cmd -= 1;
764 cmd[0] = MSG_IDENTIFY(lun, (tiflags & T_RSELECTOFF) == 0); 766 cmd[0] = MSG_IDENTIFY(lun, (tiflags & T_RSELECTOFF) == 0);
765 } 767 }
766 768
767 if ((sc->sc_features & NCR_F_DMASELECT) && !selatns) { 769 if ((sc->sc_features & NCR_F_DMASELECT) && !selatns) {
768 770
769 /* setup DMA transfer for command */ 771 /* setup DMA transfer for command */
770 dmasize = clen; 772 dmasize = clen;
771 sc->sc_cmdlen = clen; 773 sc->sc_cmdlen = clen;
772 sc->sc_cmdp = cmd; 774 sc->sc_cmdp = cmd;
773 775
774 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize); 776 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
775 /* Program the SCSI counter */ 777 /* Program the SCSI counter */
776 NCR_SET_COUNT(sc, dmasize); 778 NCR_SET_COUNT(sc, dmasize);
777 779
778 /* load the count in */ 780 /* load the count in */
779 /* if (sc->sc_rev != NCR_VARIANT_FAS366) */ 781 /* if (sc->sc_rev != NCR_VARIANT_FAS366) */
780 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA); 782 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
781 783
782 /* And get the targets attention */ 784 /* And get the targets attention */
783 if (selatn3) { 785 if (selatn3) {
784 sc->sc_msgout = SEND_TAG; 786 sc->sc_msgout = SEND_TAG;
785 sc->sc_flags |= NCR_ATN; 787 sc->sc_flags |= NCR_ATN;
786 NCRCMD(sc, NCRCMD_SELATN3 | NCRCMD_DMA); 788 NCRCMD(sc, NCRCMD_SELATN3 | NCRCMD_DMA);
787 } else 789 } else
788 NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA); 790 NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
789 NCRDMA_GO(sc); 791 NCRDMA_GO(sc);
790 return; 792 return;
791 } 793 }
792 794
793 /* 795 /*
794 * Who am I. This is where we tell the target that we are 796 * Who am I. This is where we tell the target that we are
795 * happy for it to disconnect etc. 797 * happy for it to disconnect etc.
796 */ 798 */
797 799
798 /* Now get the command into the FIFO */ 800 /* Now get the command into the FIFO */
799 ncr53c9x_wrfifo(sc, cmd, clen); 801 ncr53c9x_wrfifo(sc, cmd, clen);
800 802
801 /* And get the targets attention */ 803 /* And get the targets attention */
802 if (selatns) { 804 if (selatns) {
803 NCR_MSGS(("SELATNS \n")); 805 NCR_MSGS(("SELATNS \n"));
804 /* Arbitrate, select and stop after IDENTIFY message */ 806 /* Arbitrate, select and stop after IDENTIFY message */
805 NCRCMD(sc, NCRCMD_SELATNS); 807 NCRCMD(sc, NCRCMD_SELATNS);
806 } else if (selatn3) { 808 } else if (selatn3) {
807 sc->sc_msgout = SEND_TAG; 809 sc->sc_msgout = SEND_TAG;
808 sc->sc_flags |= NCR_ATN; 810 sc->sc_flags |= NCR_ATN;
809 NCRCMD(sc, NCRCMD_SELATN3); 811 NCRCMD(sc, NCRCMD_SELATN3);
810 } else 812 } else
811 NCRCMD(sc, NCRCMD_SELATN); 813 NCRCMD(sc, NCRCMD_SELATN);
812} 814}
813 815
814void 816void
815ncr53c9x_free_ecb(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb) 817ncr53c9x_free_ecb(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
816{ 818{
817 int s; 819 int s;
818 820
819 s = splbio(); 821 s = splbio();
820 ecb->flags = 0; 822 ecb->flags = 0;
821 pool_put(&ecb_pool, (void *)ecb); 823 pool_put(&ecb_pool, (void *)ecb);
822 splx(s); 824 splx(s);
823 return; 825 return;
824} 826}
825 827
826struct ncr53c9x_ecb * 828struct ncr53c9x_ecb *
827ncr53c9x_get_ecb(struct ncr53c9x_softc *sc, int flags) 829ncr53c9x_get_ecb(struct ncr53c9x_softc *sc, int flags)
828{ 830{
829 struct ncr53c9x_ecb *ecb; 831 struct ncr53c9x_ecb *ecb;
830 int s; 832 int s;
831 833
832 s = splbio(); 834 s = splbio();
833 ecb = pool_get(&ecb_pool, PR_NOWAIT); 835 ecb = pool_get(&ecb_pool, PR_NOWAIT);
834 splx(s); 836 splx(s);
835 if (ecb) { 837 if (ecb) {
836 memset(ecb, 0, sizeof(*ecb)); 838 memset(ecb, 0, sizeof(*ecb));
837 ecb->flags |= ECB_ALLOC; 839 ecb->flags |= ECB_ALLOC;
838 } 840 }
839 return ecb; 841 return ecb;
840} 842}
841 843
842/* 844/*
843 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS 845 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
844 */ 846 */
845 847
846/* 848/*
847 * Start a SCSI-command 849 * Start a SCSI-command
848 * This function is called by the higher level SCSI-driver to queue/run 850 * This function is called by the higher level SCSI-driver to queue/run
849 * SCSI-commands. 851 * SCSI-commands.
850 */ 852 */
851 853
852void 854void
853ncr53c9x_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, 855ncr53c9x_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
854 void *arg) 856 void *arg)
855{ 857{
856 struct scsipi_xfer *xs; 858 struct scsipi_xfer *xs;
857 struct scsipi_periph *periph; 859 struct scsipi_periph *periph;
858 struct ncr53c9x_softc *sc; 860 struct ncr53c9x_softc *sc;
859 struct ncr53c9x_ecb *ecb; 861 struct ncr53c9x_ecb *ecb;
860 int s, flags; 862 int flags;
861 863
862 NCR_TRACE(("[ncr53c9x_scsipi_request] ")); 864 NCR_TRACE(("[ncr53c9x_scsipi_request] "));
863 865
864 sc = device_private(chan->chan_adapter->adapt_dev); 866 sc = device_private(chan->chan_adapter->adapt_dev);
865 s = splbio(); 867 mutex_enter(&sc->sc_lock);
866 simple_lock(&sc->sc_lock); 
867 868
868 switch (req) { 869 switch (req) {
869 case ADAPTER_REQ_RUN_XFER: 870 case ADAPTER_REQ_RUN_XFER:
870 xs = arg; 871 xs = arg;
871 periph = xs->xs_periph; 872 periph = xs->xs_periph;
872 flags = xs->xs_control; 873 flags = xs->xs_control;
873 874
874 NCR_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen, 875 NCR_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
875 periph->periph_target)); 876 periph->periph_target));
876 877
877 /* Get an ECB to use. */ 878 /* Get an ECB to use. */
878 ecb = ncr53c9x_get_ecb(sc, xs->xs_control); 879 ecb = ncr53c9x_get_ecb(sc, xs->xs_control);
879 /* 880 /*
880 * This should never happen as we track resources 881 * This should never happen as we track resources
881 * in the mid-layer, but for now it can as pool_get() 882 * in the mid-layer, but for now it can as pool_get()
882 * can fail. 883 * can fail.
883 */ 884 */
884 if (ecb == NULL) { 885 if (ecb == NULL) {
885 scsipi_printaddr(periph); 886 scsipi_printaddr(periph);
886 printf("%s: unable to allocate ecb\n", 887 printf("%s: unable to allocate ecb\n",
887 device_xname(sc->sc_dev)); 888 device_xname(sc->sc_dev));
888 xs->error = XS_RESOURCE_SHORTAGE; 889 xs->error = XS_RESOURCE_SHORTAGE;
889 simple_unlock(&sc->sc_lock); 890 mutex_exit(&sc->sc_lock);
890 splx(s); 
891 scsipi_done(xs); 891 scsipi_done(xs);
892 return; 892 return;
893 } 893 }
894 894
895 /* Initialize ecb */ 895 /* Initialize ecb */
896 ecb->xs = xs; 896 ecb->xs = xs;
897 ecb->timeout = xs->timeout; 897 ecb->timeout = xs->timeout;
898 898
899 if (flags & XS_CTL_RESET) { 899 if (flags & XS_CTL_RESET) {
900 ecb->flags |= ECB_RESET; 900 ecb->flags |= ECB_RESET;
901 ecb->clen = 0; 901 ecb->clen = 0;
902 ecb->dleft = 0; 902 ecb->dleft = 0;
903 } else { 903 } else {
904 memcpy(&ecb->cmd.cmd, xs->cmd, xs->cmdlen); 904 memcpy(&ecb->cmd.cmd, xs->cmd, xs->cmdlen);
905 ecb->clen = xs->cmdlen; 905 ecb->clen = xs->cmdlen;
906 ecb->daddr = xs->data; 906 ecb->daddr = xs->data;
907 ecb->dleft = xs->datalen; 907 ecb->dleft = xs->datalen;
908 } 908 }
909 ecb->stat = 0; 909 ecb->stat = 0;
910 910
911 TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain); 911 TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
912 ecb->flags |= ECB_READY; 912 ecb->flags |= ECB_READY;
913 if (sc->sc_state == NCR_IDLE) 913 if (sc->sc_state == NCR_IDLE)
914 ncr53c9x_sched(sc); 914 ncr53c9x_sched(sc);
915 915
916 if ((flags & XS_CTL_POLL) == 0) 916 if ((flags & XS_CTL_POLL) == 0)
917 break; 917 break;
918 918
919 /* Not allowed to use interrupts, use polling instead */ 919 /* Not allowed to use interrupts, use polling instead */
920 if (ncr53c9x_poll(sc, xs, ecb->timeout)) { 920 if (ncr53c9x_poll(sc, xs, ecb->timeout)) {
921 ncr53c9x_timeout(ecb); 921 ncr53c9x_timeout(ecb);
922 if (ncr53c9x_poll(sc, xs, ecb->timeout)) 922 if (ncr53c9x_poll(sc, xs, ecb->timeout))
923 ncr53c9x_timeout(ecb); 923 ncr53c9x_timeout(ecb);
924 } 924 }
925 break; 925 break;
926 926
927 case ADAPTER_REQ_GROW_RESOURCES: 927 case ADAPTER_REQ_GROW_RESOURCES:
928 /* XXX Not supported. */ 928 /* XXX Not supported. */
929 break; 929 break;
930 930
931 case ADAPTER_REQ_SET_XFER_MODE: 931 case ADAPTER_REQ_SET_XFER_MODE:
932 { 932 {
933 struct ncr53c9x_tinfo *ti; 933 struct ncr53c9x_tinfo *ti;
934 struct scsipi_xfer_mode *xm = arg; 934 struct scsipi_xfer_mode *xm = arg;
935 935
936 ti = &sc->sc_tinfo[xm->xm_target]; 936 ti = &sc->sc_tinfo[xm->xm_target];
937 ti->flags &= ~(T_NEGOTIATE|T_SYNCMODE); 937 ti->flags &= ~(T_NEGOTIATE|T_SYNCMODE);
938 ti->period = 0; 938 ti->period = 0;
939 ti->offset = 0; 939 ti->offset = 0;
940 940
941 if ((sc->sc_cfflags & (1 << ((xm->xm_target & 7) + 16))) == 0 && 941 if ((sc->sc_cfflags & (1 << ((xm->xm_target & 7) + 16))) == 0 &&
942 (xm->xm_mode & PERIPH_CAP_TQING)) { 942 (xm->xm_mode & PERIPH_CAP_TQING)) {
943 NCR_MISC(("%s: target %d: tagged queuing\n", 943 NCR_MISC(("%s: target %d: tagged queuing\n",
944 device_xname(sc->sc_dev), xm->xm_target)); 944 device_xname(sc->sc_dev), xm->xm_target));
945 ti->flags |= T_TAG; 945 ti->flags |= T_TAG;
946 } else 946 } else
947 ti->flags &= ~T_TAG; 947 ti->flags &= ~T_TAG;
948 948
949 if ((xm->xm_mode & PERIPH_CAP_WIDE16) != 0) { 949 if ((xm->xm_mode & PERIPH_CAP_WIDE16) != 0) {
950 NCR_MISC(("%s: target %d: wide scsi negotiation\n", 950 NCR_MISC(("%s: target %d: wide scsi negotiation\n",
951 device_xname(sc->sc_dev), xm->xm_target)); 951 device_xname(sc->sc_dev), xm->xm_target));
952 if (sc->sc_rev == NCR_VARIANT_FAS366) { 952 if (sc->sc_rev == NCR_VARIANT_FAS366) {
953 ti->flags |= T_WIDE; 953 ti->flags |= T_WIDE;
954 ti->width = 1; 954 ti->width = 1;
955 } 955 }
956 } 956 }
957 957
958 if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 && 958 if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
959 (ti->flags & T_SYNCHOFF) == 0 && sc->sc_minsync != 0) { 959 (ti->flags & T_SYNCHOFF) == 0 && sc->sc_minsync != 0) {
960 NCR_MISC(("%s: target %d: sync negotiation\n", 960 NCR_MISC(("%s: target %d: sync negotiation\n",
961 device_xname(sc->sc_dev), xm->xm_target)); 961 device_xname(sc->sc_dev), xm->xm_target));
962 ti->flags |= T_NEGOTIATE; 962 ti->flags |= T_NEGOTIATE;
963 ti->period = sc->sc_minsync; 963 ti->period = sc->sc_minsync;
964 } 964 }
965 /* 965 /*
966 * If we're not going to negotiate, send the notification 966 * If we're not going to negotiate, send the notification
967 * now, since it won't happen later. 967 * now, since it won't happen later.
968 */ 968 */
969 if ((ti->flags & T_NEGOTIATE) == 0) 969 if ((ti->flags & T_NEGOTIATE) == 0)
970 ncr53c9x_update_xfer_mode(sc, xm->xm_target); 970 ncr53c9x_update_xfer_mode(sc, xm->xm_target);
971 } 971 }
972 break; 972 break;
973 } 973 }
974 974
975 simple_unlock(&sc->sc_lock); 975 mutex_exit(&sc->sc_lock);
976 splx(s); 
977} 976}
978 977
979void 978void
980ncr53c9x_update_xfer_mode(struct ncr53c9x_softc *sc, int target) 979ncr53c9x_update_xfer_mode(struct ncr53c9x_softc *sc, int target)
981{ 980{
982 struct scsipi_xfer_mode xm; 981 struct scsipi_xfer_mode xm;
983 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target]; 982 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
984 983
985 xm.xm_target = target; 984 xm.xm_target = target;
986 xm.xm_mode = 0; 985 xm.xm_mode = 0;
987 xm.xm_period = 0; 986 xm.xm_period = 0;
988 xm.xm_offset = 0; 987 xm.xm_offset = 0;
989 988
990 if (ti->flags & T_SYNCMODE) { 989 if (ti->flags & T_SYNCMODE) {
991 xm.xm_mode |= PERIPH_CAP_SYNC; 990 xm.xm_mode |= PERIPH_CAP_SYNC;
992 xm.xm_period = ti->period; 991 xm.xm_period = ti->period;
993 xm.xm_offset = ti->offset; 992 xm.xm_offset = ti->offset;
994 } 993 }
995 if (ti->width) 994 if (ti->width)
996 xm.xm_mode |= PERIPH_CAP_WIDE16; 995 xm.xm_mode |= PERIPH_CAP_WIDE16;
997 996
998 if ((ti->flags & (T_RSELECTOFF|T_TAG)) == T_TAG) 997 if ((ti->flags & (T_RSELECTOFF|T_TAG)) == T_TAG)
999 xm.xm_mode |= PERIPH_CAP_TQING; 998 xm.xm_mode |= PERIPH_CAP_TQING;
1000 999
1001 scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm); 1000 scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
1002} 1001}
1003 1002
1004/* 1003/*
1005 * Used when interrupt driven I/O isn't allowed, e.g. during boot. 1004 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
1006 */ 1005 */
1007int 1006int
1008ncr53c9x_poll(struct ncr53c9x_softc *sc, struct scsipi_xfer *xs, int count) 1007ncr53c9x_poll(struct ncr53c9x_softc *sc, struct scsipi_xfer *xs, int count)
1009{ 1008{
1010 1009
1011 NCR_TRACE(("[ncr53c9x_poll] ")); 1010 NCR_TRACE(("[ncr53c9x_poll] "));
1012 while (count) { 1011 while (count) {
1013 if (NCRDMA_ISINTR(sc)) { 1012 if (NCRDMA_ISINTR(sc)) {
1014 simple_unlock(&sc->sc_lock); 1013 mutex_exit(&sc->sc_lock);
1015 ncr53c9x_intr(sc); 1014 ncr53c9x_intr(sc);
1016 simple_lock(&sc->sc_lock); 1015 mutex_enter(&sc->sc_lock);
1017 } 1016 }
1018#if alternatively 1017#if alternatively
1019 if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT) 1018 if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT)
1020 ncr53c9x_intr(sc); 1019 ncr53c9x_intr(sc);
1021#endif 1020#endif
1022 if ((xs->xs_status & XS_STS_DONE) != 0) 1021 if ((xs->xs_status & XS_STS_DONE) != 0)
1023 return 0; 1022 return 0;
1024 if (sc->sc_state == NCR_IDLE) { 1023 if (sc->sc_state == NCR_IDLE) {
1025 NCR_TRACE(("[ncr53c9x_poll: rescheduling] ")); 1024 NCR_TRACE(("[ncr53c9x_poll: rescheduling] "));
1026 ncr53c9x_sched(sc); 1025 ncr53c9x_sched(sc);
1027 } 1026 }
1028 DELAY(1000); 1027 DELAY(1000);
1029 count--; 1028 count--;
1030 } 1029 }
1031 return 1; 1030 return 1;
1032} 1031}
1033 1032
1034int 1033int
1035ncr53c9x_ioctl(struct scsipi_channel *chan, u_long cmd, void *arg, 1034ncr53c9x_ioctl(struct scsipi_channel *chan, u_long cmd, void *arg,
1036 int flag, struct proc *p) 1035 int flag, struct proc *p)
1037{ 1036{
1038 struct ncr53c9x_softc *sc; 1037 struct ncr53c9x_softc *sc;
1039 int s, error = 0; 1038 int error = 0;
1040 1039
1041 sc = device_private(chan->chan_adapter->adapt_dev); 1040 sc = device_private(chan->chan_adapter->adapt_dev);
1042 switch (cmd) { 1041 switch (cmd) {
1043 case SCBUSIORESET: 1042 case SCBUSIORESET:
1044 s = splbio(); 1043 mutex_enter(&sc->sc_lock);
1045 simple_lock(&sc->sc_lock); 
1046 ncr53c9x_init(sc, 1); 1044 ncr53c9x_init(sc, 1);
1047 simple_unlock(&sc->sc_lock); 1045 mutex_exit(&sc->sc_lock);
1048 splx(s); 
1049 break; 1046 break;
1050 default: 1047 default:
1051 error = ENOTTY; 1048 error = ENOTTY;
1052 break; 1049 break;
1053 } 1050 }
1054 return error; 1051 return error;
1055} 1052}
1056 1053
1057 1054
1058/* 1055/*
1059 * LOW LEVEL SCSI UTILITIES 1056 * LOW LEVEL SCSI UTILITIES
1060 */ 1057 */
1061 1058
1062/* 1059/*
1063 * Schedule a scsi operation. This has now been pulled out of the interrupt 1060 * Schedule a scsi operation. This has now been pulled out of the interrupt
1064 * handler so that we may call it from ncr53c9x_scsipi_request and 1061 * handler so that we may call it from ncr53c9x_scsipi_request and
1065 * ncr53c9x_done. This may save us an unnecessary interrupt just to get 1062 * ncr53c9x_done. This may save us an unnecessary interrupt just to get
1066 * things going. Should only be called when state == NCR_IDLE and at bio pl. 1063 * things going. Should only be called when state == NCR_IDLE and at bio pl.
1067 */ 1064 */
1068void 1065void
1069ncr53c9x_sched(struct ncr53c9x_softc *sc) 1066ncr53c9x_sched(struct ncr53c9x_softc *sc)
1070{ 1067{
1071 struct ncr53c9x_ecb *ecb; 1068 struct ncr53c9x_ecb *ecb;
1072 struct scsipi_periph *periph; 1069 struct scsipi_periph *periph;
1073 struct ncr53c9x_tinfo *ti; 1070 struct ncr53c9x_tinfo *ti;
1074 struct ncr53c9x_linfo *li; 1071 struct ncr53c9x_linfo *li;
1075 int lun; 1072 int lun;
1076 int tag; 1073 int tag;
1077 1074
1078 NCR_TRACE(("[ncr53c9x_sched] ")); 1075 NCR_TRACE(("[ncr53c9x_sched] "));
1079 if (sc->sc_state != NCR_IDLE) 1076 if (sc->sc_state != NCR_IDLE)
1080 panic("%s: not IDLE (state=%d)", __func__, sc->sc_state); 1077 panic("%s: not IDLE (state=%d)", __func__, sc->sc_state);
1081 1078
1082 /* 1079 /*
1083 * Find first ecb in ready queue that is for a target/lunit 1080 * Find first ecb in ready queue that is for a target/lunit
1084 * combinations that is not busy. 1081 * combinations that is not busy.
1085 */ 1082 */
1086 for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL; 1083 for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL;
1087 ecb = TAILQ_NEXT(ecb, chain)) { 1084 ecb = TAILQ_NEXT(ecb, chain)) {
1088 periph = ecb->xs->xs_periph; 1085 periph = ecb->xs->xs_periph;
1089 ti = &sc->sc_tinfo[periph->periph_target]; 1086 ti = &sc->sc_tinfo[periph->periph_target];
1090 lun = periph->periph_lun; 1087 lun = periph->periph_lun;
1091 1088
1092 /* Select type of tag for this command */ 1089 /* Select type of tag for this command */
1093 if ((ti->flags & T_RSELECTOFF) != 0) 1090 if ((ti->flags & T_RSELECTOFF) != 0)
1094 tag = 0; 1091 tag = 0;
1095 else if ((ti->flags & T_TAG) == 0) 1092 else if ((ti->flags & T_TAG) == 0)
1096 tag = 0; 1093 tag = 0;
1097 else if ((ecb->flags & ECB_SENSE) != 0) 1094 else if ((ecb->flags & ECB_SENSE) != 0)
1098 tag = 0; 1095 tag = 0;
1099 else 1096 else
1100 tag = ecb->xs->xs_tag_type; 1097 tag = ecb->xs->xs_tag_type;
1101#if 0 1098#if 0
1102 /* XXXX Use tags for polled commands? */ 1099 /* XXXX Use tags for polled commands? */
1103 if (ecb->xs->xs_control & XS_CTL_POLL) 1100 if (ecb->xs->xs_control & XS_CTL_POLL)
1104 tag = 0; 1101 tag = 0;
1105#endif 1102#endif
1106 1103
1107 li = TINFO_LUN(ti, lun); 1104 li = TINFO_LUN(ti, lun);
1108 if (li == NULL) { 1105 if (li == NULL) {
1109 /* Initialize LUN info and add to list. */ 1106 /* Initialize LUN info and add to list. */
1110 li = malloc(sizeof(*li), M_DEVBUF, M_NOWAIT|M_ZERO); 1107 li = malloc(sizeof(*li), M_DEVBUF, M_NOWAIT|M_ZERO);
1111 if (li == NULL) { 1108 if (li == NULL) {
1112 continue; 1109 continue;
1113 } 1110 }
1114 li->lun = lun; 1111 li->lun = lun;
1115 1112
1116 LIST_INSERT_HEAD(&ti->luns, li, link); 1113 LIST_INSERT_HEAD(&ti->luns, li, link);
1117 if (lun < NCR_NLUN) 1114 if (lun < NCR_NLUN)
1118 ti->lun[lun] = li; 1115 ti->lun[lun] = li;
1119 } 1116 }
1120 li->last_used = time_second; 1117 li->last_used = time_second;
1121 if (tag == 0) { 1118 if (tag == 0) {
1122 /* Try to issue this as an un-tagged command */ 1119 /* Try to issue this as an un-tagged command */
1123 if (li->untagged == NULL) 1120 if (li->untagged == NULL)
1124 li->untagged = ecb; 1121 li->untagged = ecb;
1125 } 1122 }
1126 if (li->untagged != NULL) { 1123 if (li->untagged != NULL) {
1127 tag = 0; 1124 tag = 0;
1128 if ((li->busy != 1) && li->used == 0) { 1125 if ((li->busy != 1) && li->used == 0) {
1129 /* We need to issue this untagged command now */ 1126 /* We need to issue this untagged command now */
1130 ecb = li->untagged; 1127 ecb = li->untagged;
1131 periph = ecb->xs->xs_periph; 1128 periph = ecb->xs->xs_periph;
1132 } else { 1129 } else {
1133 /* Not ready yet */ 1130 /* Not ready yet */
1134 continue; 1131 continue;
1135 } 1132 }
1136 } 1133 }
1137 ecb->tag[0] = tag; 1134 ecb->tag[0] = tag;
1138 if (tag != 0) { 1135 if (tag != 0) {
1139 li->queued[ecb->xs->xs_tag_id] = ecb; 1136 li->queued[ecb->xs->xs_tag_id] = ecb;
1140 ecb->tag[1] = ecb->xs->xs_tag_id; 1137 ecb->tag[1] = ecb->xs->xs_tag_id;
1141 li->used++; 1138 li->used++;
1142 } 1139 }
1143 if (li->untagged != NULL && (li->busy != 1)) { 1140 if (li->untagged != NULL && (li->busy != 1)) {
1144 li->busy = 1; 1141 li->busy = 1;
1145 TAILQ_REMOVE(&sc->ready_list, ecb, chain); 1142 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1146 ecb->flags &= ~ECB_READY; 1143 ecb->flags &= ~ECB_READY;
1147 sc->sc_nexus = ecb; 1144 sc->sc_nexus = ecb;
1148 ncr53c9x_select(sc, ecb); 1145 ncr53c9x_select(sc, ecb);
1149 break; 1146 break;
1150 } 1147 }
1151 if (li->untagged == NULL && tag != 0) { 1148 if (li->untagged == NULL && tag != 0) {
1152 TAILQ_REMOVE(&sc->ready_list, ecb, chain); 1149 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1153 ecb->flags &= ~ECB_READY; 1150 ecb->flags &= ~ECB_READY;
1154 sc->sc_nexus = ecb; 1151 sc->sc_nexus = ecb;
1155 ncr53c9x_select(sc, ecb); 1152 ncr53c9x_select(sc, ecb);
1156 break; 1153 break;
1157 } else { 1154 } else {
1158 NCR_TRACE(("%d:%d busy\n", 1155 NCR_TRACE(("%d:%d busy\n",
1159 periph->periph_target, 1156 periph->periph_target,
1160 periph->periph_lun)); 1157 periph->periph_lun));
1161 } 1158 }
1162 } 1159 }
1163} 1160}
1164 1161
1165void 1162void
1166ncr53c9x_sense(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb) 1163ncr53c9x_sense(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1167{ 1164{
1168 struct scsipi_xfer *xs = ecb->xs; 1165 struct scsipi_xfer *xs = ecb->xs;
1169 struct scsipi_periph *periph = xs->xs_periph; 1166 struct scsipi_periph *periph = xs->xs_periph;
1170 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[periph->periph_target]; 1167 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
1171 struct scsi_request_sense *ss = (void *)&ecb->cmd.cmd; 1168 struct scsi_request_sense *ss = (void *)&ecb->cmd.cmd;
1172 struct ncr53c9x_linfo *li; 1169 struct ncr53c9x_linfo *li;
1173 int lun = periph->periph_lun; 1170 int lun = periph->periph_lun;
1174 1171
1175 NCR_TRACE(("requesting sense ")); 1172 NCR_TRACE(("requesting sense "));
1176 /* Next, setup a request sense command block */ 1173 /* Next, setup a request sense command block */
1177 memset(ss, 0, sizeof(*ss)); 1174 memset(ss, 0, sizeof(*ss));
1178 ss->opcode = SCSI_REQUEST_SENSE; 1175 ss->opcode = SCSI_REQUEST_SENSE;
1179 ss->byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT; 1176 ss->byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT;
1180 ss->length = sizeof(struct scsi_sense_data); 1177 ss->length = sizeof(struct scsi_sense_data);
1181 ecb->clen = sizeof(*ss); 1178 ecb->clen = sizeof(*ss);
1182 ecb->daddr = (uint8_t *)&xs->sense.scsi_sense; 1179 ecb->daddr = (uint8_t *)&xs->sense.scsi_sense;
1183 ecb->dleft = sizeof(struct scsi_sense_data); 1180 ecb->dleft = sizeof(struct scsi_sense_data);
1184 ecb->flags |= ECB_SENSE; 1181 ecb->flags |= ECB_SENSE;
1185 ecb->timeout = NCR_SENSE_TIMEOUT; 1182 ecb->timeout = NCR_SENSE_TIMEOUT;
1186 ti->senses++; 1183 ti->senses++;
1187 li = TINFO_LUN(ti, lun); 1184 li = TINFO_LUN(ti, lun);
1188 if (li->busy) 1185 if (li->busy)
1189 li->busy = 0; 1186 li->busy = 0;
1190 ncr53c9x_dequeue(sc, ecb); 1187 ncr53c9x_dequeue(sc, ecb);
1191 li->untagged = ecb; /* must be executed first to fix C/A */ 1188 li->untagged = ecb; /* must be executed first to fix C/A */
1192 li->busy = 2; 1189 li->busy = 2;
1193 if (ecb == sc->sc_nexus) { 1190 if (ecb == sc->sc_nexus) {
1194 ncr53c9x_select(sc, ecb); 1191 ncr53c9x_select(sc, ecb);
1195 } else { 1192 } else {
1196 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain); 1193 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
1197 ecb->flags |= ECB_READY; 1194 ecb->flags |= ECB_READY;
1198 if (sc->sc_state == NCR_IDLE) 1195 if (sc->sc_state == NCR_IDLE)
1199 ncr53c9x_sched(sc); 1196 ncr53c9x_sched(sc);
1200 } 1197 }
1201} 1198}
1202 1199
1203/* 1200/*
1204 * POST PROCESSING OF SCSI_CMD (usually current) 1201 * POST PROCESSING OF SCSI_CMD (usually current)
1205 */ 1202 */
1206void 1203void
1207ncr53c9x_done(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb) 1204ncr53c9x_done(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1208{ 1205{
1209 struct scsipi_xfer *xs = ecb->xs; 1206 struct scsipi_xfer *xs = ecb->xs;
1210 struct scsipi_periph *periph = xs->xs_periph; 1207 struct scsipi_periph *periph = xs->xs_periph;
1211 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[periph->periph_target]; 1208 struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
1212 int lun = periph->periph_lun; 1209 int lun = periph->periph_lun;
1213 struct ncr53c9x_linfo *li = TINFO_LUN(ti, lun); 1210 struct ncr53c9x_linfo *li = TINFO_LUN(ti, lun);
1214 1211
1215 NCR_TRACE(("[ncr53c9x_done(error:%x)] ", xs->error)); 1212 NCR_TRACE(("[ncr53c9x_done(error:%x)] ", xs->error));
1216 1213
1217 if ((xs->xs_control & XS_CTL_POLL) == 0) 1214 if ((xs->xs_control & XS_CTL_POLL) == 0)
1218 callout_stop(&xs->xs_callout); 1215 callout_stop(&xs->xs_callout);
1219 1216
1220 /* 1217 /*
1221 * Now, if we've come here with no error code, i.e. we've kept the 1218 * Now, if we've come here with no error code, i.e. we've kept the
1222 * initial XS_NOERROR, and the status code signals that we should 1219 * initial XS_NOERROR, and the status code signals that we should
1223 * check sense, we'll need to set up a request sense cmd block and 1220 * check sense, we'll need to set up a request sense cmd block and
1224 * push the command back into the ready queue *before* any other 1221 * push the command back into the ready queue *before* any other
1225 * commands for this target/lunit, else we lose the sense info. 1222 * commands for this target/lunit, else we lose the sense info.
1226 * We don't support chk sense conditions for the request sense cmd. 1223 * We don't support chk sense conditions for the request sense cmd.
1227 */ 1224 */
1228 if (xs->error == XS_NOERROR) { 1225 if (xs->error == XS_NOERROR) {
1229 xs->status = ecb->stat; 1226 xs->status = ecb->stat;
1230 if ((ecb->flags & ECB_ABORT) != 0) { 1227 if ((ecb->flags & ECB_ABORT) != 0) {
1231 xs->error = XS_TIMEOUT; 1228 xs->error = XS_TIMEOUT;
1232 } else if ((ecb->flags & ECB_SENSE) != 0) { 1229 } else if ((ecb->flags & ECB_SENSE) != 0) {
1233 xs->error = XS_SENSE; 1230 xs->error = XS_SENSE;
1234 } else if ((ecb->stat & ST_MASK) == SCSI_CHECK) { 1231 } else if ((ecb->stat & ST_MASK) == SCSI_CHECK) {
1235 /* First, save the return values */ 1232 /* First, save the return values */
1236 xs->resid = ecb->dleft; 1233 xs->resid = ecb->dleft;
1237 ncr53c9x_sense(sc, ecb); 1234 ncr53c9x_sense(sc, ecb);
1238 return; 1235 return;
1239 } else { 1236 } else {
1240 xs->resid = ecb->dleft; 1237 xs->resid = ecb->dleft;
1241 } 1238 }
1242 if (xs->status == SCSI_QUEUE_FULL || xs->status == XS_BUSY) 1239 if (xs->status == SCSI_QUEUE_FULL || xs->status == XS_BUSY)
1243 xs->error = XS_BUSY; 1240 xs->error = XS_BUSY;
1244 } 1241 }
1245 1242
1246#ifdef NCR53C9X_DEBUG 1243#ifdef NCR53C9X_DEBUG
1247 if (ncr53c9x_debug & NCR_SHOWTRAC) { 1244 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1248 if (xs->resid != 0) 1245 if (xs->resid != 0)
1249 printf("resid=%d ", xs->resid); 1246 printf("resid=%d ", xs->resid);
1250 if (xs->error == XS_SENSE) 1247 if (xs->error == XS_SENSE)
1251 printf("sense=0x%02x\n", 1248 printf("sense=0x%02x\n",
1252 xs->sense.scsi_sense.response_code); 1249 xs->sense.scsi_sense.response_code);
1253 else 1250 else
1254 printf("error=%d\n", xs->error); 1251 printf("error=%d\n", xs->error);
1255 } 1252 }
1256#endif 1253#endif
1257 1254
1258 /* 1255 /*
1259 * Remove the ECB from whatever queue it's on. 1256 * Remove the ECB from whatever queue it's on.
1260 */ 1257 */
1261 ncr53c9x_dequeue(sc, ecb); 1258 ncr53c9x_dequeue(sc, ecb);
1262 if (ecb == sc->sc_nexus) { 1259 if (ecb == sc->sc_nexus) {
1263 sc->sc_nexus = NULL; 1260 sc->sc_nexus = NULL;
1264 if (sc->sc_state != NCR_CLEANING) { 1261 if (sc->sc_state != NCR_CLEANING) {
1265 sc->sc_state = NCR_IDLE; 1262 sc->sc_state = NCR_IDLE;
1266 ncr53c9x_sched(sc); 1263 ncr53c9x_sched(sc);
1267 } 1264 }
1268 } 1265 }
1269 1266
1270 if (xs->error == XS_SELTIMEOUT) { 1267 if (xs->error == XS_SELTIMEOUT) {
1271 /* Selection timeout -- discard this LUN if empty */ 1268 /* Selection timeout -- discard this LUN if empty */
1272 if (li->untagged == NULL && li->used == 0) { 1269 if (li->untagged == NULL && li->used == 0) {
1273 if (lun < NCR_NLUN) 1270 if (lun < NCR_NLUN)
1274 ti->lun[lun] = NULL; 1271 ti->lun[lun] = NULL;
1275 LIST_REMOVE(li, link); 1272 LIST_REMOVE(li, link);
1276 free(li, M_DEVBUF); 1273 free(li, M_DEVBUF);
1277 } 1274 }
1278 } 1275 }
1279 1276
1280 ncr53c9x_free_ecb(sc, ecb); 1277 ncr53c9x_free_ecb(sc, ecb);
1281 ti->cmds++; 1278 ti->cmds++;
1282 simple_unlock(&sc->sc_lock); 1279 mutex_exit(&sc->sc_lock);
1283 scsipi_done(xs); 1280 scsipi_done(xs);
1284 simple_lock(&sc->sc_lock); 1281 mutex_enter(&sc->sc_lock);
1285} 1282}
1286 1283
1287void 1284void
1288ncr53c9x_dequeue(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb) 1285ncr53c9x_dequeue(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
1289{ 1286{
1290 struct ncr53c9x_tinfo *ti = 1287 struct ncr53c9x_tinfo *ti =
1291 &sc->sc_tinfo[ecb->xs->xs_periph->periph_target]; 1288 &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
1292 struct ncr53c9x_linfo *li; 1289 struct ncr53c9x_linfo *li;
1293 int64_t lun = ecb->xs->xs_periph->periph_lun; 1290 int64_t lun = ecb->xs->xs_periph->periph_lun;
1294 1291
1295 li = TINFO_LUN(ti, lun); 1292 li = TINFO_LUN(ti, lun);
1296#ifdef DIAGNOSTIC 1293#ifdef DIAGNOSTIC
1297 if (li == NULL || li->lun != lun) 1294 if (li == NULL || li->lun != lun)
1298 panic("%s: lun %" PRIx64 " for ecb %p does not exist", 1295 panic("%s: lun %" PRIx64 " for ecb %p does not exist",
1299 __func__, lun, ecb); 1296 __func__, lun, ecb);
1300#endif 1297#endif
1301 if (li->untagged == ecb) { 1298 if (li->untagged == ecb) {
1302 li->busy = 0; 1299 li->busy = 0;
1303 li->untagged = NULL; 1300 li->untagged = NULL;
1304 } 1301 }
1305 if (ecb->tag[0] && li->queued[ecb->tag[1]] != NULL) { 1302 if (ecb->tag[0] && li->queued[ecb->tag[1]] != NULL) {
1306#ifdef DIAGNOSTIC 1303#ifdef DIAGNOSTIC
1307 if (li->queued[ecb->tag[1]] != NULL && 1304 if (li->queued[ecb->tag[1]] != NULL &&
1308 (li->queued[ecb->tag[1]] != ecb)) 1305 (li->queued[ecb->tag[1]] != ecb))
1309 panic("%s: slot %d for lun %" PRIx64 " has %p " 1306 panic("%s: slot %d for lun %" PRIx64 " has %p "
1310 "instead of ecb %p\n", __func__, ecb->tag[1], 1307 "instead of ecb %p\n", __func__, ecb->tag[1],
1311 lun, 1308 lun,
1312 li->queued[ecb->tag[1]], ecb); 1309 li->queued[ecb->tag[1]], ecb);
1313#endif 1310#endif
1314 li->queued[ecb->tag[1]] = NULL; 1311 li->queued[ecb->tag[1]] = NULL;
1315 li->used--; 1312 li->used--;
1316 } 1313 }
1317 1314
1318 if ((ecb->flags & ECB_READY) != 0) { 1315 if ((ecb->flags & ECB_READY) != 0) {
1319 ecb->flags &= ~ECB_READY; 1316 ecb->flags &= ~ECB_READY;
1320 TAILQ_REMOVE(&sc->ready_list, ecb, chain); 1317 TAILQ_REMOVE(&sc->ready_list, ecb, chain);
1321 } 1318 }
1322} 1319}
1323 1320
1324/* 1321/*
1325 * INTERRUPT/PROTOCOL ENGINE 1322 * INTERRUPT/PROTOCOL ENGINE
1326 */ 1323 */
1327 1324
1328/* 1325/*
1329 * Schedule an outgoing message by prioritizing it, and asserting 1326 * Schedule an outgoing message by prioritizing it, and asserting
1330 * attention on the bus. We can only do this when we are the initiator 1327 * attention on the bus. We can only do this when we are the initiator
1331 * else there will be an illegal command interrupt. 1328 * else there will be an illegal command interrupt.
1332 */ 1329 */
1333#define ncr53c9x_sched_msgout(m) \ 1330#define ncr53c9x_sched_msgout(m) \
1334 do { \ 1331 do { \
1335 NCR_MSGS(("ncr53c9x_sched_msgout %x %d", m, __LINE__)); \ 1332 NCR_MSGS(("ncr53c9x_sched_msgout %x %d", m, __LINE__)); \
1336 NCRCMD(sc, NCRCMD_SETATN); \ 1333 NCRCMD(sc, NCRCMD_SETATN); \
1337 sc->sc_flags |= NCR_ATN; \ 1334 sc->sc_flags |= NCR_ATN; \
1338 sc->sc_msgpriq |= (m); \ 1335 sc->sc_msgpriq |= (m); \
1339 } while (/* CONSTCOND */0) 1336 } while (/* CONSTCOND */0)
1340 1337
1341static void 1338static void
1342ncr53c9x_flushfifo(struct ncr53c9x_softc *sc) 1339ncr53c9x_flushfifo(struct ncr53c9x_softc *sc)
1343{ 1340{
1344 1341
1345 NCR_TRACE(("[flushfifo] ")); 1342 NCR_TRACE(("[flushfifo] "));
1346 1343
1347 NCRCMD(sc, NCRCMD_FLUSH); 1344 NCRCMD(sc, NCRCMD_FLUSH);
1348 1345
1349 if (sc->sc_phase == COMMAND_PHASE || 1346 if (sc->sc_phase == COMMAND_PHASE ||
1350 sc->sc_phase == MESSAGE_OUT_PHASE) 1347 sc->sc_phase == MESSAGE_OUT_PHASE)
1351 DELAY(2); 1348 DELAY(2);
1352} 1349}
1353 1350
1354static int 1351static int
1355ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how) 1352ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how)
1356{ 1353{
1357 int i, n; 1354 int i, n;
1358 uint8_t *ibuf; 1355 uint8_t *ibuf;
1359 1356
1360 switch (how) { 1357 switch (how) {
1361 case NCR_RDFIFO_START: 1358 case NCR_RDFIFO_START:
1362 ibuf = sc->sc_imess; 1359 ibuf = sc->sc_imess;
1363 sc->sc_imlen = 0; 1360 sc->sc_imlen = 0;
1364 break; 1361 break;
1365 case NCR_RDFIFO_CONTINUE: 1362 case NCR_RDFIFO_CONTINUE:
1366 ibuf = sc->sc_imess + sc->sc_imlen; 1363 ibuf = sc->sc_imess + sc->sc_imlen;
1367 break; 1364 break;
1368 default: 1365 default:
1369 panic("%s: bad flag", __func__); 1366 panic("%s: bad flag", __func__);
1370 break; 1367 break;
1371 } 1368 }
1372 1369
1373 /* 1370 /*
1374 * XXX buffer (sc_imess) size for message 1371 * XXX buffer (sc_imess) size for message
1375 */ 1372 */
1376 1373
1377 n = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF; 1374 n = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF;
1378 1375
1379 if (sc->sc_rev == NCR_VARIANT_FAS366) { 1376 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1380 n *= 2; 1377 n *= 2;
1381 1378
1382 for (i = 0; i < n; i++) 1379 for (i = 0; i < n; i++)
1383 ibuf[i] = NCR_READ_REG(sc, NCR_FIFO); 1380 ibuf[i] = NCR_READ_REG(sc, NCR_FIFO);
1384 1381
1385 if (sc->sc_espstat2 & NCRFAS_STAT2_ISHUTTLE) { 1382 if (sc->sc_espstat2 & NCRFAS_STAT2_ISHUTTLE) {
1386 1383
1387 NCR_WRITE_REG(sc, NCR_FIFO, 0); 1384 NCR_WRITE_REG(sc, NCR_FIFO, 0);
1388 ibuf[i++] = NCR_READ_REG(sc, NCR_FIFO); 1385 ibuf[i++] = NCR_READ_REG(sc, NCR_FIFO);
1389 1386
1390 NCR_READ_REG(sc, NCR_FIFO); 1387 NCR_READ_REG(sc, NCR_FIFO);
1391 1388
1392 ncr53c9x_flushfifo(sc); 1389 ncr53c9x_flushfifo(sc);
1393 } 1390 }
1394 } else { 1391 } else {
1395 for (i = 0; i < n; i++) 1392 for (i = 0; i < n; i++)
1396 ibuf[i] = NCR_READ_REG(sc, NCR_FIFO); 1393 ibuf[i] = NCR_READ_REG(sc, NCR_FIFO);
1397 } 1394 }
1398 1395
1399 sc->sc_imlen += i; 1396 sc->sc_imlen += i;
1400 1397
1401#if 0 1398#if 0
1402#ifdef NCR53C9X_DEBUG 1399#ifdef NCR53C9X_DEBUG
1403 { 1400 {
1404 int j; 1401 int j;
1405 1402
1406 NCR_TRACE(("\n[rdfifo %s (%d):", 1403 NCR_TRACE(("\n[rdfifo %s (%d):",
1407 (how == NCR_RDFIFO_START) ? "start" : "cont", 1404 (how == NCR_RDFIFO_START) ? "start" : "cont",
1408 (int)sc->sc_imlen)); 1405 (int)sc->sc_imlen));
1409 if (ncr53c9x_debug & NCR_SHOWTRAC) { 1406 if (ncr53c9x_debug & NCR_SHOWTRAC) {
1410 for (j = 0; j < sc->sc_imlen; j++) 1407 for (j = 0; j < sc->sc_imlen; j++)
1411 printf(" %02x", sc->sc_imess[j]); 1408 printf(" %02x", sc->sc_imess[j]);
1412 printf("]\n"); 1409 printf("]\n");
1413 } 1410 }
1414 } 1411 }
1415#endif 1412#endif
1416#endif 1413#endif
1417 return sc->sc_imlen; 1414 return sc->sc_imlen;
1418} 1415}
1419 1416
1420static void 1417static void
1421ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, uint8_t *p, int len) 1418ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, uint8_t *p, int len)
1422{ 1419{
1423 int i; 1420 int i;
1424 1421
1425#ifdef NCR53C9X_DEBUG 1422#ifdef NCR53C9X_DEBUG
1426 NCR_MSGS(("[wrfifo(%d):", len)); 1423 NCR_MSGS(("[wrfifo(%d):", len));
1427 if (ncr53c9x_debug & NCR_SHOWMSGS) { 1424 if (ncr53c9x_debug & NCR_SHOWMSGS) {
1428 for (i = 0; i < len; i++) 1425 for (i = 0; i < len; i++)
1429 printf(" %02x", p[i]); 1426 printf(" %02x", p[i]);
1430 printf("]\n"); 1427 printf("]\n");
1431 } 1428 }
1432#endif 1429#endif
1433 1430
1434 for (i = 0; i < len; i++) { 1431 for (i = 0; i < len; i++) {
1435 NCR_WRITE_REG(sc, NCR_FIFO, p[i]); 1432 NCR_WRITE_REG(sc, NCR_FIFO, p[i]);
1436 1433
1437 if (sc->sc_rev == NCR_VARIANT_FAS366) 1434 if (sc->sc_rev == NCR_VARIANT_FAS366)
1438 NCR_WRITE_REG(sc, NCR_FIFO, 0); 1435 NCR_WRITE_REG(sc, NCR_FIFO, 0);
1439 } 1436 }
1440} 1437}
1441 1438
1442int 1439int
1443ncr53c9x_reselect(struct ncr53c9x_softc *sc, int message, int tagtype, 1440ncr53c9x_reselect(struct ncr53c9x_softc *sc, int message, int tagtype,
1444 int tagid) 1441 int tagid)
1445{ 1442{
1446 uint8_t selid, target, lun; 1443 uint8_t selid, target, lun;
1447 struct ncr53c9x_ecb *ecb = NULL; 1444 struct ncr53c9x_ecb *ecb = NULL;
1448 struct ncr53c9x_tinfo *ti; 1445 struct ncr53c9x_tinfo *ti;
1449 struct ncr53c9x_linfo *li; 1446 struct ncr53c9x_linfo *li;
1450 1447
1451 if (sc->sc_rev == NCR_VARIANT_FAS366) { 1448 if (sc->sc_rev == NCR_VARIANT_FAS366) {
1452 target = sc->sc_selid; 1449 target = sc->sc_selid;
1453 } else { 1450 } else {
1454 /* 1451 /*
1455 * The SCSI chip made a snapshot of the data bus 1452 * The SCSI chip made a snapshot of the data bus
1456 * while the reselection was being negotiated. 1453 * while the reselection was being negotiated.
1457 * This enables us to determine which target did 1454 * This enables us to determine which target did
1458 * the reselect. 1455 * the reselect.
1459 */ 1456 */
1460 selid = sc->sc_selid & ~(1 << sc->sc_id); 1457 selid = sc->sc_selid & ~(1 << sc->sc_id);
1461 if (selid & (selid - 1)) { 1458 if (selid & (selid - 1)) {
1462 printf("%s: reselect with invalid selid %02x;" 1459 printf("%s: reselect with invalid selid %02x;"
1463 " sending DEVICE RESET\n", 1460 " sending DEVICE RESET\n",
1464 device_xname(sc->sc_dev), selid); 1461 device_xname(sc->sc_dev), selid);
1465 goto reset; 1462 goto reset;
1466 } 1463 }
1467 1464
1468 target = ffs(selid) - 1; 1465 target = ffs(selid) - 1;
1469 } 1466 }
1470 lun = message & 0x07; 1467 lun = message & 0x07;
1471 1468
1472 /* 1469 /*
1473 * Search wait queue for disconnected cmd 1470 * Search wait queue for disconnected cmd
1474 * The list should be short, so I haven't bothered with 1471 * The list should be short, so I haven't bothered with
1475 * any more sophisticated structures than a simple 1472 * any more sophisticated structures than a simple
1476 * singly linked list. 1473 * singly linked list.
1477 */ 1474 */
1478 ti = &sc->sc_tinfo[target]; 1475 ti = &sc->sc_tinfo[target];
1479 li = TINFO_LUN(ti, lun); 1476 li = TINFO_LUN(ti, lun);
1480 1477
1481 /* 1478 /*
1482 * We can get as far as the LUN with the IDENTIFY 1479 * We can get as far as the LUN with the IDENTIFY
1483 * message. Check to see if we're running an 1480 * message. Check to see if we're running an
1484 * un-tagged command. Otherwise ack the IDENTIFY 1481 * un-tagged command. Otherwise ack the IDENTIFY
1485 * and wait for a tag message. 1482 * and wait for a tag message.
1486 */ 1483 */
1487 if (li != NULL) { 1484 if (li != NULL) {
1488 if (li->untagged != NULL && li->busy) 1485 if (li->untagged != NULL && li->busy)
1489 ecb = li->untagged; 1486 ecb = li->untagged;
1490 else if (tagtype != MSG_SIMPLE_Q_TAG) { 1487 else if (tagtype != MSG_SIMPLE_Q_TAG) {
1491 /* Wait for tag to come by */ 1488 /* Wait for tag to come by */
1492 sc->sc_state = NCR_IDENTIFIED; 1489 sc->sc_state = NCR_IDENTIFIED;
1493 return 0; 1490 return 0;
1494 } else if (tagtype) 1491 } else if (tagtype)
1495 ecb = li->queued[tagid]; 1492 ecb = li->queued[tagid];
1496 } 1493 }
1497 if (ecb == NULL) { 1494 if (ecb == NULL) {
1498 printf("%s: reselect from target %d lun %d tag %x:%x " 1495 printf("%s: reselect from target %d lun %d tag %x:%x "
1499 "with no nexus; sending ABORT\n", 1496 "with no nexus; sending ABORT\n",
1500 device_xname(sc->sc_dev), target, lun, tagtype, tagid); 1497 device_xname(sc->sc_dev), target, lun, tagtype, tagid);
1501 goto abort; 1498 goto abort;
1502 } 1499 }
1503 1500
1504 /* Make this nexus active again. */ 1501 /* Make this nexus active again. */
1505 sc->sc_state = NCR_CONNECTED; 1502 sc->sc_state = NCR_CONNECTED;
1506 sc->sc_nexus = ecb; 1503 sc->sc_nexus = ecb;
1507 ncr53c9x_setsync(sc, ti); 1504 ncr53c9x_setsync(sc, ti);
1508 1505
1509 if (ecb->flags & ECB_RESET) 1506 if (ecb->flags & ECB_RESET)
1510 ncr53c9x_sched_msgout(SEND_DEV_RESET); 1507 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1511 else if (ecb->flags & ECB_ABORT) 1508 else if (ecb->flags & ECB_ABORT)
1512 ncr53c9x_sched_msgout(SEND_ABORT); 1509 ncr53c9x_sched_msgout(SEND_ABORT);
1513 1510
1514 /* Do an implicit RESTORE POINTERS. */ 1511 /* Do an implicit RESTORE POINTERS. */
1515 sc->sc_dp = ecb->daddr; 1512 sc->sc_dp = ecb->daddr;
1516 sc->sc_dleft = ecb->dleft; 1513 sc->sc_dleft = ecb->dleft;
1517 1514
1518 return 0; 1515 return 0;
1519 1516
1520reset: 1517reset:
1521 ncr53c9x_sched_msgout(SEND_DEV_RESET); 1518 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1522 return 1; 1519 return 1;
1523 1520
1524abort: 1521abort:
1525 ncr53c9x_sched_msgout(SEND_ABORT); 1522 ncr53c9x_sched_msgout(SEND_ABORT);
1526 return 1; 1523 return 1;
1527} 1524}
1528 1525
1529static inline int 1526static inline int
1530__verify_msg_format(uint8_t *p, int len) 1527__verify_msg_format(uint8_t *p, int len)
1531{ 1528{
1532 1529
1533 if (len == 1 && MSG_IS1BYTE(p[0])) 1530 if (len == 1 && MSG_IS1BYTE(p[0]))
1534 return 1; 1531 return 1;
1535 if (len == 2 && MSG_IS2BYTE(p[0])) 1532 if (len == 2 && MSG_IS2BYTE(p[0]))
1536 return 1; 1533 return 1;
1537 if (len >= 3 && MSG_ISEXTENDED(p[0]) && 1534 if (len >= 3 && MSG_ISEXTENDED(p[0]) &&
1538 len == p[1] + 2) 1535 len == p[1] + 2)
1539 return 1; 1536 return 1;
1540 1537
1541 return 0; 1538 return 0;
1542} 1539}
1543 1540
1544/* 1541/*
1545 * Get an incoming message as initiator. 1542 * Get an incoming message as initiator.
1546 * 1543 *
1547 * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a 1544 * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
1548 * byte in the FIFO 1545 * byte in the FIFO
1549 */ 1546 */
1550void 1547void
1551ncr53c9x_msgin(struct ncr53c9x_softc *sc) 1548ncr53c9x_msgin(struct ncr53c9x_softc *sc)
1552{ 1549{
1553 1550
1554 NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen)); 1551 NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
1555 1552
1556 if (sc->sc_imlen == 0) { 1553 if (sc->sc_imlen == 0) {
1557 printf("%s: msgin: no msg byte available\n", 1554 printf("%s: msgin: no msg byte available\n",
1558 device_xname(sc->sc_dev)); 1555 device_xname(sc->sc_dev));
1559 return; 1556 return;
1560 } 1557 }
1561 1558
1562 /* 1559 /*
1563 * Prepare for a new message. A message should (according 1560 * Prepare for a new message. A message should (according
1564 * to the SCSI standard) be transmitted in one single 1561 * to the SCSI standard) be transmitted in one single
1565 * MESSAGE_IN_PHASE. If we have been in some other phase, 1562 * MESSAGE_IN_PHASE. If we have been in some other phase,
1566 * then this is a new message. 1563 * then this is a new message.
1567 */ 1564 */
1568 if (sc->sc_prevphase != MESSAGE_IN_PHASE && 1565 if (sc->sc_prevphase != MESSAGE_IN_PHASE &&
1569 sc->sc_state != NCR_RESELECTED) { 1566 sc->sc_state != NCR_RESELECTED) {
1570 printf("%s: phase change, dropping message, " 1567 printf("%s: phase change, dropping message, "
1571 "prev %d, state %d\n", 1568 "prev %d, state %d\n",
1572 device_xname(sc->sc_dev), sc->sc_prevphase, sc->sc_state); 1569 device_xname(sc->sc_dev), sc->sc_prevphase, sc->sc_state);
1573 sc->sc_flags &= ~NCR_DROP_MSGI; 1570 sc->sc_flags &= ~NCR_DROP_MSGI;
1574 sc->sc_imlen = 0; 1571 sc->sc_imlen = 0;
1575 } 1572 }
1576 1573
1577 /* 1574 /*
1578 * If we're going to reject the message, don't bother storing 1575 * If we're going to reject the message, don't bother storing
1579 * the incoming bytes. But still, we need to ACK them. 1576 * the incoming bytes. But still, we need to ACK them.
1580 */ 1577 */
1581 if ((sc->sc_flags & NCR_DROP_MSGI) != 0) { 1578 if ((sc->sc_flags & NCR_DROP_MSGI) != 0) {
1582 NCRCMD(sc, NCRCMD_MSGOK); 1579 NCRCMD(sc, NCRCMD_MSGOK);
1583 printf("<dropping msg byte %x>", sc->sc_imess[sc->sc_imlen]); 1580 printf("<dropping msg byte %x>", sc->sc_imess[sc->sc_imlen]);
1584 return; 1581 return;
1585 } 1582 }
1586 1583
1587 if (sc->sc_imlen >= NCR_MAX_MSG_LEN) { 1584 if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
1588 ncr53c9x_sched_msgout(SEND_REJECT); 1585 ncr53c9x_sched_msgout(SEND_REJECT);
1589 sc->sc_flags |= NCR_DROP_MSGI; 1586 sc->sc_flags |= NCR_DROP_MSGI;
1590 } else { 1587 } else {
1591 uint8_t *pb; 1588 uint8_t *pb;
1592 int plen; 1589 int plen;
1593 1590
1594 switch (sc->sc_state) { 1591 switch (sc->sc_state) {
1595 /* 1592 /*
1596 * if received message is the first of reselection 1593 * if received message is the first of reselection
1597 * then first byte is selid, and then message 1594 * then first byte is selid, and then message
1598 */ 1595 */
1599 case NCR_RESELECTED: 1596 case NCR_RESELECTED:
1600 pb = sc->sc_imess + 1; 1597 pb = sc->sc_imess + 1;
1601 plen = sc->sc_imlen - 1; 1598 plen = sc->sc_imlen - 1;
1602 break; 1599 break;
1603 default: 1600 default:
1604 pb = sc->sc_imess; 1601 pb = sc->sc_imess;
1605 plen = sc->sc_imlen; 1602 plen = sc->sc_imlen;
1606 break; 1603 break;
1607 } 1604 }
1608 1605
1609 if (__verify_msg_format(pb, plen)) 1606 if (__verify_msg_format(pb, plen))
1610 goto gotit; 1607 goto gotit;
1611 } 1608 }
1612 1609
1613 /* Ack what we have so far */ 1610 /* Ack what we have so far */
1614 NCRCMD(sc, NCRCMD_MSGOK); 1611 NCRCMD(sc, NCRCMD_MSGOK);
1615 return; 1612 return;
1616 1613
1617gotit: 1614gotit:
1618 NCR_MSGS(("gotmsg(%x) state %d", sc->sc_imess[0], sc->sc_state)); 1615 NCR_MSGS(("gotmsg(%x) state %d", sc->sc_imess[0], sc->sc_state));
1619 /* we got complete message, flush the imess, */ 1616 /* we got complete message, flush the imess, */
1620 /* XXX nobody uses imlen below */ 1617 /* XXX nobody uses imlen below */
1621 sc->sc_imlen = 0; 1618 sc->sc_imlen = 0;
1622 /* 1619 /*
1623 * Now we should have a complete message (1 byte, 2 byte 1620 * Now we should have a complete message (1 byte, 2 byte
1624 * and moderately long extended messages). We only handle 1621 * and moderately long extended messages). We only handle
1625 * extended messages which total length is shorter than 1622 * extended messages which total length is shorter than
1626 * NCR_MAX_MSG_LEN. Longer messages will be amputated. 1623 * NCR_MAX_MSG_LEN. Longer messages will be amputated.
1627 */ 1624 */
1628 switch (sc->sc_state) { 1625 switch (sc->sc_state) {
1629 struct ncr53c9x_ecb *ecb; 1626 struct ncr53c9x_ecb *ecb;
1630 struct ncr53c9x_tinfo *ti; 1627 struct ncr53c9x_tinfo *ti;
1631 struct ncr53c9x_linfo *li; 1628 struct ncr53c9x_linfo *li;
1632 int lun; 1629 int lun;
1633 1630
1634 case NCR_CONNECTED: 1631 case NCR_CONNECTED:
1635 ecb = sc->sc_nexus; 1632 ecb = sc->sc_nexus;
1636 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target]; 1633 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
1637 1634
1638 switch (sc->sc_imess[0]) { 1635 switch (sc->sc_imess[0]) {
1639 case MSG_CMDCOMPLETE: 1636 case MSG_CMDCOMPLETE:
1640 NCR_MSGS(("cmdcomplete ")); 1637 NCR_MSGS(("cmdcomplete "));
1641 if (sc->sc_dleft < 0) { 1638 if (sc->sc_dleft < 0) {
1642 scsipi_printaddr(ecb->xs->xs_periph); 1639 scsipi_printaddr(ecb->xs->xs_periph);
1643 printf("%s: got %ld extra bytes\n", 1640 printf("%s: got %ld extra bytes\n",
1644 device_xname(sc->sc_dev), 1641 device_xname(sc->sc_dev),
1645 -(long)sc->sc_dleft); 1642 -(long)sc->sc_dleft);
1646 sc->sc_dleft = 0; 1643 sc->sc_dleft = 0;
1647 } 1644 }
1648 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE) ? 1645 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE) ?
1649 0 : sc->sc_dleft; 1646 0 : sc->sc_dleft;
1650 if ((ecb->flags & ECB_SENSE) == 0) 1647 if ((ecb->flags & ECB_SENSE) == 0)
1651 ecb->xs->resid = ecb->dleft; 1648 ecb->xs->resid = ecb->dleft;
1652 sc->sc_state = NCR_CMDCOMPLETE; 1649 sc->sc_state = NCR_CMDCOMPLETE;
1653 break; 1650 break;
1654 1651
1655 case MSG_MESSAGE_REJECT: 1652 case MSG_MESSAGE_REJECT:
1656 NCR_MSGS(("msg reject (msgout=%x) ", sc->sc_msgout)); 1653 NCR_MSGS(("msg reject (msgout=%x) ", sc->sc_msgout));
1657 switch (sc->sc_msgout) { 1654 switch (sc->sc_msgout) {
1658 case SEND_TAG: 1655 case SEND_TAG:
1659 /* 1656 /*
1660 * Target does not like tagged queuing. 1657 * Target does not like tagged queuing.
1661 * - Flush the command queue 1658 * - Flush the command queue
1662 * - Disable tagged queuing for the target 1659 * - Disable tagged queuing for the target
1663 * - Dequeue ecb from the queued array. 1660 * - Dequeue ecb from the queued array.
1664 */ 1661 */
1665 printf("%s: tagged queuing rejected: " 1662 printf("%s: tagged queuing rejected: "
1666 "target %d\n", 1663 "target %d\n",
1667 device_xname(sc->sc_dev), 1664 device_xname(sc->sc_dev),
1668 ecb->xs->xs_periph->periph_target); 1665 ecb->xs->xs_periph->periph_target);
1669 1666
1670 NCR_MSGS(("(rejected sent tag)")); 1667 NCR_MSGS(("(rejected sent tag)"));
1671 NCRCMD(sc, NCRCMD_FLUSH); 1668 NCRCMD(sc, NCRCMD_FLUSH);
1672 DELAY(1); 1669 DELAY(1);
1673 ti->flags &= ~T_TAG; 1670 ti->flags &= ~T_TAG;
1674 lun = ecb->xs->xs_periph->periph_lun; 1671 lun = ecb->xs->xs_periph->periph_lun;
1675 li = TINFO_LUN(ti, lun); 1672 li = TINFO_LUN(ti, lun);
1676 if (ecb->tag[0] && 1673 if (ecb->tag[0] &&
1677 li->queued[ecb->tag[1]] != NULL) { 1674 li->queued[ecb->tag[1]] != NULL) {
1678 li->queued[ecb->tag[1]] = NULL; 1675 li->queued[ecb->tag[1]] = NULL;
1679 li->used--; 1676 li->used--;
1680 } 1677 }
1681 ecb->tag[0] = ecb->tag[1] = 0; 1678 ecb->tag[0] = ecb->tag[1] = 0;
1682 li->untagged = ecb; 1679 li->untagged = ecb;
1683 li->busy = 1; 1680 li->busy = 1;
1684 break; 1681 break;
1685 1682
1686 case SEND_SDTR: 1683 case SEND_SDTR:
1687 printf("%s: sync transfer rejected: " 1684 printf("%s: sync transfer rejected: "
1688 "target %d\n", 1685 "target %d\n",
1689 device_xname(sc->sc_dev), 1686 device_xname(sc->sc_dev),
1690 ecb->xs->xs_periph->periph_target); 1687 ecb->xs->xs_periph->periph_target);
1691 1688
1692 sc->sc_flags &= ~NCR_SYNCHNEGO; 1689 sc->sc_flags &= ~NCR_SYNCHNEGO;
1693 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE); 1690 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
1694 ncr53c9x_setsync(sc, ti); 1691 ncr53c9x_setsync(sc, ti);
1695 ncr53c9x_update_xfer_mode(sc, 1692 ncr53c9x_update_xfer_mode(sc,
1696 ecb->xs->xs_periph->periph_target); 1693 ecb->xs->xs_periph->periph_target);
1697 break; 1694 break;
1698 1695
1699 case SEND_WDTR: 1696 case SEND_WDTR:
1700 printf("%s: wide transfer rejected: " 1697 printf("%s: wide transfer rejected: "
1701 "target %d\n", 1698 "target %d\n",
1702 device_xname(sc->sc_dev), 1699 device_xname(sc->sc_dev),
1703 ecb->xs->xs_periph->periph_target); 1700 ecb->xs->xs_periph->periph_target);
1704 ti->flags &= ~(T_WIDE | T_WDTRSENT); 1701 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1705 ti->width = 0; 1702 ti->width = 0;
1706 break; 1703 break;
1707 1704
1708 case SEND_INIT_DET_ERR: 1705 case SEND_INIT_DET_ERR:
1709 goto abort; 1706 goto abort;
1710 } 1707 }
1711 break; 1708 break;
1712 1709
1713 case MSG_NOOP: 1710 case MSG_NOOP:
1714 NCR_MSGS(("noop ")); 1711 NCR_MSGS(("noop "));
1715 break; 1712 break;
1716 1713
1717 case MSG_HEAD_OF_Q_TAG: 1714 case MSG_HEAD_OF_Q_TAG:
1718 case MSG_SIMPLE_Q_TAG: 1715 case MSG_SIMPLE_Q_TAG:
1719 case MSG_ORDERED_Q_TAG: 1716 case MSG_ORDERED_Q_TAG:
1720 NCR_MSGS(("TAG %x:%x", 1717 NCR_MSGS(("TAG %x:%x",
1721 sc->sc_imess[0], sc->sc_imess[1])); 1718 sc->sc_imess[0], sc->sc_imess[1]));
1722 break; 1719 break;
1723 1720
1724 case MSG_DISCONNECT: 1721 case MSG_DISCONNECT:
1725 NCR_MSGS(("disconnect ")); 1722 NCR_MSGS(("disconnect "));
1726 ti->dconns++; 1723 ti->dconns++;
1727 sc->sc_state = NCR_DISCONNECT; 1724 sc->sc_state = NCR_DISCONNECT;
1728 1725
1729 /* 1726 /*
1730 * Mark the fact that all bytes have moved. The 1727 * Mark the fact that all bytes have moved. The
1731 * target may not bother to do a SAVE POINTERS 1728 * target may not bother to do a SAVE POINTERS
1732 * at this stage. This flag will set the residual 1729 * at this stage. This flag will set the residual
1733 * count to zero on MSG COMPLETE. 1730 * count to zero on MSG COMPLETE.
1734 */ 1731 */
1735 if (sc->sc_dleft == 0) 1732 if (sc->sc_dleft == 0)
1736 ecb->flags |= ECB_TENTATIVE_DONE; 1733 ecb->flags |= ECB_TENTATIVE_DONE;
1737 1734
1738 break; 1735 break;
1739 1736
1740 case MSG_SAVEDATAPOINTER: 1737 case MSG_SAVEDATAPOINTER:
1741 NCR_MSGS(("save datapointer ")); 1738 NCR_MSGS(("save datapointer "));
1742 ecb->daddr = sc->sc_dp; 1739 ecb->daddr = sc->sc_dp;
1743 ecb->dleft = sc->sc_dleft; 1740 ecb->dleft = sc->sc_dleft;
1744 break; 1741 break;
1745 1742
1746 case MSG_RESTOREPOINTERS: 1743 case MSG_RESTOREPOINTERS:
1747 NCR_MSGS(("restore datapointer ")); 1744 NCR_MSGS(("restore datapointer "));
1748 sc->sc_dp = ecb->daddr; 1745 sc->sc_dp = ecb->daddr;
1749 sc->sc_dleft = ecb->dleft; 1746 sc->sc_dleft = ecb->dleft;
1750 break; 1747 break;
1751 1748
1752 case MSG_EXTENDED: 1749 case MSG_EXTENDED:
1753 NCR_MSGS(("extended(%x) ", sc->sc_imess[2])); 1750 NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
1754 switch (sc->sc_imess[2]) { 1751 switch (sc->sc_imess[2]) {
1755 case MSG_EXT_SDTR: 1752 case MSG_EXT_SDTR:
1756 NCR_MSGS(("SDTR period %d, offset %d ", 1753 NCR_MSGS(("SDTR period %d, offset %d ",
1757 sc->sc_imess[3], sc->sc_imess[4])); 1754 sc->sc_imess[3], sc->sc_imess[4]));
1758 if (sc->sc_imess[1] != 3) 1755 if (sc->sc_imess[1] != 3)
1759 goto reject; 1756 goto reject;
1760 ti->period = sc->sc_imess[3]; 1757 ti->period = sc->sc_imess[3];
1761 ti->offset = sc->sc_imess[4]; 1758 ti->offset = sc->sc_imess[4];
1762 ti->flags &= ~T_NEGOTIATE; 1759 ti->flags &= ~T_NEGOTIATE;
1763 if (sc->sc_minsync == 0 || 1760 if (sc->sc_minsync == 0 ||
1764 ti->offset == 0 || 1761 ti->offset == 0 ||
1765 ti->period > 124) { 1762 ti->period > 124) {
1766#if 0 1763#if 0
1767#ifdef NCR53C9X_DEBUG 1764#ifdef NCR53C9X_DEBUG
1768 scsipi_printaddr(ecb->xs->xs_periph); 1765 scsipi_printaddr(ecb->xs->xs_periph);
1769 printf("async mode\n"); 1766 printf("async mode\n");
1770#endif 1767#endif
1771#endif 1768#endif
1772 ti->flags &= ~T_SYNCMODE; 1769 ti->flags &= ~T_SYNCMODE;
1773 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) { 1770 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
1774 /* 1771 /*
1775 * target initiated negotiation 1772 * target initiated negotiation
1776 */ 1773 */
1777 ti->offset = 0; 1774 ti->offset = 0;
1778 ncr53c9x_sched_msgout( 1775 ncr53c9x_sched_msgout(
1779 SEND_SDTR); 1776 SEND_SDTR);
1780 } 1777 }
1781 } else { 1778 } else {
1782 int p; 1779 int p;
1783 1780
1784 p = ncr53c9x_stp2cpb(sc, ti->period); 1781 p = ncr53c9x_stp2cpb(sc, ti->period);
1785 ti->period = ncr53c9x_cpb2stp(sc, p); 1782 ti->period = ncr53c9x_cpb2stp(sc, p);
1786 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) { 1783 if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
1787 /* 1784 /*
1788 * target initiated negotiation 1785 * target initiated negotiation
1789 */ 1786 */
1790 if (ti->period < 1787 if (ti->period <
1791 sc->sc_minsync) 1788 sc->sc_minsync)
1792 ti->period = 1789 ti->period =
1793 sc->sc_minsync; 1790 sc->sc_minsync;
1794 if (ti->offset > 15) 1791 if (ti->offset > 15)
1795 ti->offset = 15; 1792 ti->offset = 15;
1796 ti->flags &= ~T_SYNCMODE; 1793 ti->flags &= ~T_SYNCMODE;
1797 ncr53c9x_sched_msgout( 1794 ncr53c9x_sched_msgout(
1798 SEND_SDTR); 1795 SEND_SDTR);
1799 } else { 1796 } else {
1800 /* we are sync */ 1797 /* we are sync */
1801 ti->flags |= T_SYNCMODE; 1798 ti->flags |= T_SYNCMODE;
1802 } 1799 }
1803 } 1800 }
1804 ncr53c9x_update_xfer_mode(sc, 1801 ncr53c9x_update_xfer_mode(sc,
1805 ecb->xs->xs_periph->periph_target); 1802 ecb->xs->xs_periph->periph_target);
1806 sc->sc_flags &= ~NCR_SYNCHNEGO; 1803 sc->sc_flags &= ~NCR_SYNCHNEGO;
1807 ncr53c9x_setsync(sc, ti); 1804 ncr53c9x_setsync(sc, ti);
1808 break; 1805 break;
1809 1806
1810 case MSG_EXT_WDTR: 1807 case MSG_EXT_WDTR:
1811#ifdef NCR53C9X_DEBUG 1808#ifdef NCR53C9X_DEBUG
1812 printf("%s: wide mode %d\n", 1809 printf("%s: wide mode %d\n",
1813 device_xname(sc->sc_dev), sc->sc_imess[3]); 1810 device_xname(sc->sc_dev), sc->sc_imess[3]);
1814#endif 1811#endif
1815 if (sc->sc_imess[3] == 1) { 1812 if (sc->sc_imess[3] == 1) {
1816 ti->cfg3 |= NCRFASCFG3_EWIDE; 1813 ti->cfg3 |= NCRFASCFG3_EWIDE;
1817 ncr53c9x_setsync(sc, ti); 1814 ncr53c9x_setsync(sc, ti);
1818 } else 1815 } else
1819 ti->width = 0; 1816 ti->width = 0;
1820 /* 1817 /*
1821 * Device started width negotiation. 1818 * Device started width negotiation.
1822 */ 1819 */
1823 if ((ti->flags & T_WDTRSENT) == 0) 1820 if ((ti->flags & T_WDTRSENT) == 0)
1824 ncr53c9x_sched_msgout(SEND_WDTR); 1821 ncr53c9x_sched_msgout(SEND_WDTR);
1825 ti->flags &= ~(T_WIDE | T_WDTRSENT); 1822 ti->flags &= ~(T_WIDE | T_WDTRSENT);
1826 break; 1823 break;
1827 default: 1824 default:
1828 scsipi_printaddr(ecb->xs->xs_periph); 1825 scsipi_printaddr(ecb->xs->xs_periph);
1829 printf("%s: unrecognized MESSAGE EXTENDED;" 1826 printf("%s: unrecognized MESSAGE EXTENDED;"
1830 " sending REJECT\n", 1827 " sending REJECT\n",
1831 device_xname(sc->sc_dev)); 1828 device_xname(sc->sc_dev));
1832 goto reject; 1829 goto reject;
1833 } 1830 }
1834 break; 1831 break;
1835 1832
1836 default: 1833 default:
1837 NCR_MSGS(("ident ")); 1834 NCR_MSGS(("ident "));
1838 scsipi_printaddr(ecb->xs->xs_periph); 1835 scsipi_printaddr(ecb->xs->xs_periph);
1839 printf("%s: unrecognized MESSAGE; sending REJECT\n", 1836 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1840 device_xname(sc->sc_dev)); 1837 device_xname(sc->sc_dev));
1841 reject: 1838 reject:
1842 ncr53c9x_sched_msgout(SEND_REJECT); 1839 ncr53c9x_sched_msgout(SEND_REJECT);
1843 break; 1840 break;
1844 } 1841 }
1845 break; 1842 break;
1846 1843
1847 case NCR_IDENTIFIED: 1844 case NCR_IDENTIFIED:
1848 /* 1845 /*
1849 * IDENTIFY message was received and queue tag is expected now 1846 * IDENTIFY message was received and queue tag is expected now
1850 */ 1847 */
1851 if ((sc->sc_imess[0] != MSG_SIMPLE_Q_TAG) || 1848 if ((sc->sc_imess[0] != MSG_SIMPLE_Q_TAG) ||
1852 (sc->sc_msgify == 0)) { 1849 (sc->sc_msgify == 0)) {
1853 printf("%s: TAG reselect without IDENTIFY;" 1850 printf("%s: TAG reselect without IDENTIFY;"
1854 " MSG %x;" 1851 " MSG %x;"
1855 " sending DEVICE RESET\n", 1852 " sending DEVICE RESET\n",
1856 device_xname(sc->sc_dev), 1853 device_xname(sc->sc_dev),
1857 sc->sc_imess[0]); 1854 sc->sc_imess[0]);
1858 goto reset; 1855 goto reset;
1859 } 1856 }
1860 (void)ncr53c9x_reselect(sc, sc->sc_msgify, 1857 (void)ncr53c9x_reselect(sc, sc->sc_msgify,
1861 sc->sc_imess[0], sc->sc_imess[1]); 1858 sc->sc_imess[0], sc->sc_imess[1]);
1862 break; 1859 break;
1863 1860
1864 case NCR_RESELECTED: 1861 case NCR_RESELECTED:
1865 if (MSG_ISIDENTIFY(sc->sc_imess[1])) { 1862 if (MSG_ISIDENTIFY(sc->sc_imess[1])) {
1866 sc->sc_msgify = sc->sc_imess[1]; 1863 sc->sc_msgify = sc->sc_imess[1];
1867 } else { 1864 } else {
1868 printf("%s: reselect without IDENTIFY;" 1865 printf("%s: reselect without IDENTIFY;"
1869 " MSG %x;" 1866 " MSG %x;"
1870 " sending DEVICE RESET\n", 1867 " sending DEVICE RESET\n",
1871 device_xname(sc->sc_dev), 1868 device_xname(sc->sc_dev),
1872 sc->sc_imess[1]); 1869 sc->sc_imess[1]);
1873 goto reset; 1870 goto reset;
1874 } 1871 }
1875 (void)ncr53c9x_reselect(sc, sc->sc_msgify, 0, 0); 1872 (void)ncr53c9x_reselect(sc, sc->sc_msgify, 0, 0);
1876 break; 1873 break;
1877 1874
1878 default: 1875 default:
1879 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n", 1876 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1880 device_xname(sc->sc_dev)); 1877 device_xname(sc->sc_dev));
1881 reset: 1878 reset:
1882 ncr53c9x_sched_msgout(SEND_DEV_RESET); 1879 ncr53c9x_sched_msgout(SEND_DEV_RESET);
1883 break; 1880 break;
1884 1881
1885 abort: 1882 abort:
1886 ncr53c9x_sched_msgout(SEND_ABORT); 1883 ncr53c9x_sched_msgout(SEND_ABORT);
1887 break; 1884 break;
1888 } 1885 }
1889 1886
1890 /* if we have more messages to send set ATN */ 1887 /* if we have more messages to send set ATN */
1891 if (sc->sc_msgpriq) 1888 if (sc->sc_msgpriq)
1892 NCRCMD(sc, NCRCMD_SETATN); 1889 NCRCMD(sc, NCRCMD_SETATN);
1893 1890
1894 /* Ack last message byte */ 1891 /* Ack last message byte */
1895 NCRCMD(sc, NCRCMD_MSGOK); 1892 NCRCMD(sc, NCRCMD_MSGOK);
1896 1893
1897 /* Done, reset message pointer. */ 1894 /* Done, reset message pointer. */
1898 sc->sc_flags &= ~NCR_DROP_MSGI; 1895 sc->sc_flags &= ~NCR_DROP_MSGI;
1899 sc->sc_imlen = 0; 1896 sc->sc_imlen = 0;
1900} 1897}
1901 1898
1902 1899
1903/* 1900/*
1904 * Send the highest priority, scheduled message 1901 * Send the highest priority, scheduled message
1905 */ 1902 */
1906void 1903void
1907ncr53c9x_msgout(struct ncr53c9x_softc *sc) 1904ncr53c9x_msgout(struct ncr53c9x_softc *sc)
1908{ 1905{
1909 struct ncr53c9x_tinfo *ti; 1906 struct ncr53c9x_tinfo *ti;
1910 struct ncr53c9x_ecb *ecb; 1907 struct ncr53c9x_ecb *ecb;
1911 size_t size; 1908 size_t size;
1912 1909
1913 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]", 1910 NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
1914 sc->sc_msgpriq, sc->sc_prevphase)); 1911 sc->sc_msgpriq, sc->sc_prevphase));
1915 1912
1916 /* 1913 /*
1917 * XXX - the NCR_ATN flag is not in sync with the actual ATN 1914 * XXX - the NCR_ATN flag is not in sync with the actual ATN
1918 * condition on the SCSI bus. The 53c9x chip 1915 * condition on the SCSI bus. The 53c9x chip
1919 * automatically turns off ATN before sending the 1916 * automatically turns off ATN before sending the
1920 * message byte. (see also the comment below in the 1917 * message byte. (see also the comment below in the
1921 * default case when picking out a message to send) 1918 * default case when picking out a message to send)
1922 */ 1919 */
1923 if (sc->sc_flags & NCR_ATN) { 1920 if (sc->sc_flags & NCR_ATN) {
1924 if (sc->sc_prevphase != MESSAGE_OUT_PHASE) { 1921 if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
1925 new: 1922 new:
1926 NCRCMD(sc, NCRCMD_FLUSH); 1923 NCRCMD(sc, NCRCMD_FLUSH);
1927#if 0 1924#if 0
1928 DELAY(1); 1925 DELAY(1);
1929#endif 1926#endif
1930 sc->sc_msgoutq = 0; 1927 sc->sc_msgoutq = 0;
1931 sc->sc_omlen = 0; 1928 sc->sc_omlen = 0;
1932 } 1929 }
1933 } else { 1930 } else {
1934 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) { 1931 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1935 ncr53c9x_sched_msgout(sc->sc_msgoutq); 1932 ncr53c9x_sched_msgout(sc->sc_msgoutq);
1936 goto new; 1933 goto new;
1937 } else { 1934 } else {
1938 printf("%s at line %d: unexpected MESSAGE OUT phase\n", 1935 printf("%s at line %d: unexpected MESSAGE OUT phase\n",
1939 device_xname(sc->sc_dev), __LINE__); 1936 device_xname(sc->sc_dev), __LINE__);
1940 } 1937 }
1941 } 1938 }
1942 1939
1943 if (sc->sc_omlen == 0) { 1940 if (sc->sc_omlen == 0) {
1944 /* Pick up highest priority message */ 1941 /* Pick up highest priority message */
1945 sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq; 1942 sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
1946 sc->sc_msgoutq |= sc->sc_msgout; 1943 sc->sc_msgoutq |= sc->sc_msgout;
1947 sc->sc_msgpriq &= ~sc->sc_msgout; 1944 sc->sc_msgpriq &= ~sc->sc_msgout;
1948 sc->sc_omlen = 1; /* "Default" message len */ 1945 sc->sc_omlen = 1; /* "Default" message len */
1949 switch (sc->sc_msgout) { 1946 switch (sc->sc_msgout) {
1950 case SEND_SDTR: 1947 case SEND_SDTR:
1951 ecb = sc->sc_nexus; 1948 ecb = sc->sc_nexus;
1952 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target]; 1949 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
1953 sc->sc_omess[0] = MSG_EXTENDED; 1950 sc->sc_omess[0] = MSG_EXTENDED;
1954 sc->sc_omess[1] = MSG_EXT_SDTR_LEN; 1951 sc->sc_omess[1] = MSG_EXT_SDTR_LEN;
1955 sc->sc_omess[2] = MSG_EXT_SDTR; 1952 sc->sc_omess[2] = MSG_EXT_SDTR;
1956 sc->sc_omess[3] = ti->period; 1953 sc->sc_omess[3] = ti->period;
1957 sc->sc_omess[4] = ti->offset; 1954 sc->sc_omess[4] = ti->offset;
1958 sc->sc_omlen = 5; 1955 sc->sc_omlen = 5;
1959 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) { 1956 if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
1960 ti->flags |= T_SYNCMODE; 1957 ti->flags |= T_SYNCMODE;
1961 ncr53c9x_setsync(sc, ti); 1958 ncr53c9x_setsync(sc, ti);
1962 } 1959 }
1963 break; 1960 break;
1964 case SEND_WDTR: 1961 case SEND_WDTR:
1965 ecb = sc->sc_nexus; 1962 ecb = sc->sc_nexus;
1966 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target]; 1963 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
1967 sc->sc_omess[0] = MSG_EXTENDED; 1964 sc->sc_omess[0] = MSG_EXTENDED;
1968 sc->sc_omess[1] = MSG_EXT_WDTR_LEN; 1965 sc->sc_omess[1] = MSG_EXT_WDTR_LEN;
1969 sc->sc_omess[2] = MSG_EXT_WDTR; 1966 sc->sc_omess[2] = MSG_EXT_WDTR;
1970 sc->sc_omess[3] = ti->width; 1967 sc->sc_omess[3] = ti->width;
1971 sc->sc_omlen = 4; 1968 sc->sc_omlen = 4;
1972 break; 1969 break;
1973 case SEND_IDENTIFY: 1970 case SEND_IDENTIFY:
1974 if (sc->sc_state != NCR_CONNECTED) { 1971 if (sc->sc_state != NCR_CONNECTED) {
1975 printf("%s at line %d: no nexus\n", 1972 printf("%s at line %d: no nexus\n",
1976 device_xname(sc->sc_dev), __LINE__); 1973 device_xname(sc->sc_dev), __LINE__);
1977 } 1974 }
1978 ecb = sc->sc_nexus; 1975 ecb = sc->sc_nexus;
1979 sc->sc_omess[0] = 1976 sc->sc_omess[0] =
1980 MSG_IDENTIFY(ecb->xs->xs_periph->periph_lun, 0); 1977 MSG_IDENTIFY(ecb->xs->xs_periph->periph_lun, 0);
1981 break; 1978 break;
1982 case SEND_TAG: 1979 case SEND_TAG:
1983 if (sc->sc_state != NCR_CONNECTED) { 1980 if (sc->sc_state != NCR_CONNECTED) {
1984 printf("%s at line %d: no nexus\n", 1981 printf("%s at line %d: no nexus\n",
1985 device_xname(sc->sc_dev), __LINE__); 1982 device_xname(sc->sc_dev), __LINE__);
1986 } 1983 }
1987 ecb = sc->sc_nexus; 1984 ecb = sc->sc_nexus;
1988 sc->sc_omess[0] = ecb->tag[0]; 1985 sc->sc_omess[0] = ecb->tag[0];
1989 sc->sc_omess[1] = ecb->tag[1]; 1986 sc->sc_omess[1] = ecb->tag[1];
1990 sc->sc_omlen = 2; 1987 sc->sc_omlen = 2;
1991 break; 1988 break;
1992 case SEND_DEV_RESET: 1989 case SEND_DEV_RESET:
1993 sc->sc_flags |= NCR_ABORTING; 1990 sc->sc_flags |= NCR_ABORTING;
1994 sc->sc_omess[0] = MSG_BUS_DEV_RESET; 1991 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1995 ecb = sc->sc_nexus; 1992 ecb = sc->sc_nexus;
1996 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target]; 1993 ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
1997 ti->flags &= ~T_SYNCMODE; 1994 ti->flags &= ~T_SYNCMODE;
1998 ncr53c9x_update_xfer_mode(sc, 1995 ncr53c9x_update_xfer_mode(sc,
1999 ecb->xs->xs_periph->periph_target); 1996 ecb->xs->xs_periph->periph_target);
2000 if ((ti->flags & T_SYNCHOFF) == 0) 1997 if ((ti->flags & T_SYNCHOFF) == 0)
2001 /* We can re-start sync negotiation */ 1998 /* We can re-start sync negotiation */
2002 ti->flags |= T_NEGOTIATE; 1999 ti->flags |= T_NEGOTIATE;
2003 break; 2000 break;
2004 case SEND_PARITY_ERROR: 2001 case SEND_PARITY_ERROR:
2005 sc->sc_omess[0] = MSG_PARITY_ERROR; 2002 sc->sc_omess[0] = MSG_PARITY_ERROR;
2006 break; 2003 break;
2007 case SEND_ABORT: 2004 case SEND_ABORT:
2008 sc->sc_flags |= NCR_ABORTING; 2005 sc->sc_flags |= NCR_ABORTING;
2009 sc->sc_omess[0] = MSG_ABORT; 2006 sc->sc_omess[0] = MSG_ABORT;
2010 break; 2007 break;
2011 case SEND_INIT_DET_ERR: 2008 case SEND_INIT_DET_ERR:
2012 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR; 2009 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
2013 break; 2010 break;
2014 case SEND_REJECT: 2011 case SEND_REJECT:
2015 sc->sc_omess[0] = MSG_MESSAGE_REJECT; 2012 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
2016 break; 2013 break;
2017 default: 2014 default:
2018 /* 2015 /*
2019 * We normally do not get here, since the chip 2016 * We normally do not get here, since the chip
2020 * automatically turns off ATN before the last 2017 * automatically turns off ATN before the last
2021 * byte of a message is sent to the target. 2018 * byte of a message is sent to the target.
2022 * However, if the target rejects our (multi-byte) 2019 * However, if the target rejects our (multi-byte)
2023 * message early by switching to MSG IN phase 2020 * message early by switching to MSG IN phase
2024 * ATN remains on, so the target may return to 2021 * ATN remains on, so the target may return to
2025 * MSG OUT phase. If there are no scheduled messages 2022 * MSG OUT phase. If there are no scheduled messages
2026 * left we send a NO-OP. 2023 * left we send a NO-OP.
2027 * 2024 *
2028 * XXX - Note that this leaves no useful purpose for 2025 * XXX - Note that this leaves no useful purpose for
2029 * the NCR_ATN flag. 2026 * the NCR_ATN flag.
2030 */ 2027 */
2031 sc->sc_flags &= ~NCR_ATN; 2028 sc->sc_flags &= ~NCR_ATN;
2032 sc->sc_omess[0] = MSG_NOOP; 2029 sc->sc_omess[0] = MSG_NOOP;
2033 break; 2030 break;
2034 } 2031 }
2035 sc->sc_omp = sc->sc_omess; 2032 sc->sc_omp = sc->sc_omess;
2036 } 2033 }
2037 2034
2038#ifdef DEBUG 2035#ifdef DEBUG
2039 if (ncr53c9x_debug & NCR_SHOWMSGS) { 2036 if (ncr53c9x_debug & NCR_SHOWMSGS) {
2040 int i; 2037 int i;
2041 2038
2042 NCR_MSGS(("<msgout:")); 2039 NCR_MSGS(("<msgout:"));
2043 for (i = 0; i < sc->sc_omlen; i++) 2040 for (i = 0; i < sc->sc_omlen; i++)
2044 NCR_MSGS((" %02x", sc->sc_omess[i])); 2041 NCR_MSGS((" %02x", sc->sc_omess[i]));
2045 NCR_MSGS(("> ")); 2042 NCR_MSGS(("> "));
2046 } 2043 }
2047#endif 2044#endif
2048 if (sc->sc_rev == NCR_VARIANT_FAS366) { 2045 if (sc->sc_rev == NCR_VARIANT_FAS366) {
2049 /* 2046 /*
2050 * XXX fifo size 2047 * XXX fifo size
2051 */ 2048 */
2052 ncr53c9x_flushfifo(sc); 2049 ncr53c9x_flushfifo(sc);
2053 ncr53c9x_wrfifo(sc, sc->sc_omp, sc->sc_omlen); 2050 ncr53c9x_wrfifo(sc, sc->sc_omp, sc->sc_omlen);
2054 NCRCMD(sc, NCRCMD_TRANS); 2051 NCRCMD(sc, NCRCMD_TRANS);
2055 } else { 2052 } else {
2056 /* (re)send the message */ 2053 /* (re)send the message */
2057 size = min(sc->sc_omlen, sc->sc_maxxfer); 2054 size = min(sc->sc_omlen, sc->sc_maxxfer);
2058 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size); 2055 NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
2059 /* Program the SCSI counter */ 2056 /* Program the SCSI counter */
2060 NCR_SET_COUNT(sc, size); 2057 NCR_SET_COUNT(sc, size);
2061 2058
2062 /* Load the count in and start the message-out transfer */ 2059 /* Load the count in and start the message-out transfer */
2063 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA); 2060 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
2064 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA); 2061 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
2065 NCRDMA_GO(sc); 2062 NCRDMA_GO(sc);
2066 } 2063 }
2067} 2064}
2068 2065
2069/* 2066/*
2070 * This is the most critical part of the driver, and has to know 2067 * This is the most critical part of the driver, and has to know
2071 * how to deal with *all* error conditions and phases from the SCSI 2068 * how to deal with *all* error conditions and phases from the SCSI
2072 * bus. If there are no errors and the DMA was active, then call the 2069 * bus. If there are no errors and the DMA was active, then call the
2073 * DMA pseudo-interrupt handler. If this returns 1, then that was it 2070 * DMA pseudo-interrupt handler. If this returns 1, then that was it
2074 * and we can return from here without further processing. 2071 * and we can return from here without further processing.
2075 * 2072 *
2076 * Most of this needs verifying. 2073 * Most of this needs verifying.
2077 */ 2074 */
2078int 2075int
2079ncr53c9x_intr(void *arg) 2076ncr53c9x_intr(void *arg)
2080{ 2077{
2081 struct ncr53c9x_softc *sc = arg; 2078 struct ncr53c9x_softc *sc = arg;
2082 struct ncr53c9x_ecb *ecb; 2079 struct ncr53c9x_ecb *ecb;
2083 struct scsipi_periph *periph; 2080 struct scsipi_periph *periph;
2084 struct ncr53c9x_tinfo *ti; 2081 struct ncr53c9x_tinfo *ti;
2085 size_t size; 2082 size_t size;
2086 int nfifo; 2083 int nfifo;
2087 2084
2088 NCR_INTS(("[ncr53c9x_intr: state %d]", sc->sc_state)); 2085 NCR_INTS(("[ncr53c9x_intr: state %d]", sc->sc_state));
2089 2086
2090 if (!NCRDMA_ISINTR(sc)) 2087 if (!NCRDMA_ISINTR(sc))
2091 return 0; 2088 return 0;
2092 2089
2093 simple_lock(&sc->sc_lock); 2090 mutex_enter(&sc->sc_lock);
2094again: 2091again:
2095 /* and what do the registers say... */ 2092 /* and what do the registers say... */
2096 ncr53c9x_readregs(sc); 2093 ncr53c9x_readregs(sc);
2097 2094
2098 sc->sc_intrcnt.ev_count++; 2095 sc->sc_intrcnt.ev_count++;
2099 2096
2100 /* 2097 /*
2101 * At the moment, only a SCSI Bus Reset or Illegal 2098 * At the moment, only a SCSI Bus Reset or Illegal
2102 * Command are classed as errors. A disconnect is a 2099 * Command are classed as errors. A disconnect is a
2103 * valid condition, and we let the code check is the 2100 * valid condition, and we let the code check is the
2104 * "NCR_BUSFREE_OK" flag was set before declaring it 2101 * "NCR_BUSFREE_OK" flag was set before declaring it
2105 * and error. 2102 * and error.
2106 * 2103 *
2107 * Also, the status register tells us about "Gross 2104 * Also, the status register tells us about "Gross
2108 * Errors" and "Parity errors". Only the Gross Error 2105 * Errors" and "Parity errors". Only the Gross Error
2109 * is really bad, and the parity errors are dealt 2106 * is really bad, and the parity errors are dealt
2110 * with later 2107 * with later
2111 * 2108 *
2112 * TODO 2109 * TODO
2113 * If there are too many parity error, go to slow 2110 * If there are too many parity error, go to slow
2114 * cable mode ? 2111 * cable mode ?
2115 */ 2112 */
2116 2113
2117 /* SCSI Reset */ 2114 /* SCSI Reset */
2118 if ((sc->sc_espintr & NCRINTR_SBR) != 0) { 2115 if ((sc->sc_espintr & NCRINTR_SBR) != 0) {
2119 if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 0) { 2116 if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 0) {
2120 NCRCMD(sc, NCRCMD_FLUSH); 2117 NCRCMD(sc, NCRCMD_FLUSH);
2121 DELAY(1); 2118 DELAY(1);
2122 } 2119 }
2123 if (sc->sc_state != NCR_SBR) { 2120 if (sc->sc_state != NCR_SBR) {
2124 printf("%s: SCSI bus reset\n", 2121 printf("%s: SCSI bus reset\n",
2125 device_xname(sc->sc_dev)); 2122 device_xname(sc->sc_dev));
2126 ncr53c9x_init(sc, 0); /* Restart everything */ 2123 ncr53c9x_init(sc, 0); /* Restart everything */
2127 goto out; 2124 goto out;
2128 } 2125 }
2129#if 0 2126#if 0
2130/*XXX*/ printf("<expected bus reset: " 2127/*XXX*/ printf("<expected bus reset: "
2131 "[intr %x, stat %x, step %d]>\n", 2128 "[intr %x, stat %x, step %d]>\n",
2132 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep); 2129 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2133#endif 2130#endif
2134 if (sc->sc_nexus != NULL) 2131 if (sc->sc_nexus != NULL)
2135 panic("%s: nexus in reset state", 2132 panic("%s: nexus in reset state",
2136 device_xname(sc->sc_dev)); 2133 device_xname(sc->sc_dev));
2137 goto sched; 2134 goto sched;
2138 } 2135 }
2139 2136
2140 ecb = sc->sc_nexus; 2137 ecb = sc->sc_nexus;
2141 2138
2142#define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL) 2139#define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL)
2143 if (sc->sc_espintr & NCRINTR_ERR || 2140 if (sc->sc_espintr & NCRINTR_ERR ||
2144 sc->sc_espstat & NCRSTAT_GE) { 2141 sc->sc_espstat & NCRSTAT_GE) {
2145 2142
2146 if ((sc->sc_espstat & NCRSTAT_GE) != 0) { 2143 if ((sc->sc_espstat & NCRSTAT_GE) != 0) {
2147 /* Gross Error; no target ? */ 2144 /* Gross Error; no target ? */
2148 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) { 2145 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2149 NCRCMD(sc, NCRCMD_FLUSH); 2146 NCRCMD(sc, NCRCMD_FLUSH);
2150 DELAY(1); 2147 DELAY(1);
2151 } 2148 }
2152 if (sc->sc_state == NCR_CONNECTED || 2149 if (sc->sc_state == NCR_CONNECTED ||
2153 sc->sc_state == NCR_SELECTING) { 2150 sc->sc_state == NCR_SELECTING) {
2154 ecb->xs->error = XS_TIMEOUT; 2151 ecb->xs->error = XS_TIMEOUT;
2155 ncr53c9x_done(sc, ecb); 2152 ncr53c9x_done(sc, ecb);
2156 } 2153 }
2157 goto out; 2154 goto out;
2158 } 2155 }
2159 2156
2160 if ((sc->sc_espintr & NCRINTR_ILL) != 0) { 2157 if ((sc->sc_espintr & NCRINTR_ILL) != 0) {
2161 if ((sc->sc_flags & NCR_EXPECT_ILLCMD) != 0) { 2158 if ((sc->sc_flags & NCR_EXPECT_ILLCMD) != 0) {
2162 /* 2159 /*
2163 * Eat away "Illegal command" interrupt 2160 * Eat away "Illegal command" interrupt
2164 * on a ESP100 caused by a re-selection 2161 * on a ESP100 caused by a re-selection
2165 * while we were trying to select 2162 * while we were trying to select
2166 * another target. 2163 * another target.
2167 */ 2164 */
2168#ifdef NCR53C9X_DEBUG 2165#ifdef NCR53C9X_DEBUG
2169 printf("%s: ESP100 work-around activated\n", 2166 printf("%s: ESP100 work-around activated\n",
2170 device_xname(sc->sc_dev)); 2167 device_xname(sc->sc_dev));
2171#endif 2168#endif
2172 sc->sc_flags &= ~NCR_EXPECT_ILLCMD; 2169 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2173 goto out; 2170 goto out;
2174 } 2171 }
2175 /* illegal command, out of sync ? */ 2172 /* illegal command, out of sync ? */
2176 printf("%s: illegal command: 0x%x " 2173 printf("%s: illegal command: 0x%x "
2177 "(state %d, phase %x, prevphase %x)\n", 2174 "(state %d, phase %x, prevphase %x)\n",
2178 device_xname(sc->sc_dev), sc->sc_lastcmd, 2175 device_xname(sc->sc_dev), sc->sc_lastcmd,
2179 sc->sc_state, sc->sc_phase, sc->sc_prevphase); 2176 sc->sc_state, sc->sc_phase, sc->sc_prevphase);
2180 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) { 2177 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2181 NCRCMD(sc, NCRCMD_FLUSH); 2178 NCRCMD(sc, NCRCMD_FLUSH);
2182 DELAY(1); 2179 DELAY(1);
2183 } 2180 }
2184 ncr53c9x_init(sc, 1); /* Restart everything */ 2181 ncr53c9x_init(sc, 1); /* Restart everything */
2185 goto out; 2182 goto out;
2186 } 2183 }
2187 } 2184 }
2188 sc->sc_flags &= ~NCR_EXPECT_ILLCMD; 2185 sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
2189 2186
2190 /* 2187 /*
2191 * Call if DMA is active. 2188 * Call if DMA is active.
2192 * 2189 *
2193 * If DMA_INTR returns true, then maybe go 'round the loop 2190 * If DMA_INTR returns true, then maybe go 'round the loop
2194 * again in case there is no more DMA queued, but a phase 2191 * again in case there is no more DMA queued, but a phase
2195 * change is expected. 2192 * change is expected.
2196 */ 2193 */
2197 if (NCRDMA_ISACTIVE(sc)) { 2194 if (NCRDMA_ISACTIVE(sc)) {
2198 int r = NCRDMA_INTR(sc); 2195 int r = NCRDMA_INTR(sc);
2199 if (r == -1) { 2196 if (r == -1) {
2200 printf("%s: DMA error; resetting\n", 2197 printf("%s: DMA error; resetting\n",
2201 device_xname(sc->sc_dev)); 2198 device_xname(sc->sc_dev));
2202 ncr53c9x_init(sc, 1); 2199 ncr53c9x_init(sc, 1);
2203 goto out; 2200 goto out;
2204 } 2201 }
2205 /* If DMA active here, then go back to work... */ 2202 /* If DMA active here, then go back to work... */
2206 if (NCRDMA_ISACTIVE(sc)) 2203 if (NCRDMA_ISACTIVE(sc))
2207 goto out; 2204 goto out;
2208 2205
2209 if ((sc->sc_espstat & NCRSTAT_TC) == 0) { 2206 if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
2210 /* 2207 /*
2211 * DMA not completed. If we can not find a 2208 * DMA not completed. If we can not find a
2212 * acceptable explanation, print a diagnostic. 2209 * acceptable explanation, print a diagnostic.
2213 */ 2210 */
2214 if (sc->sc_state == NCR_SELECTING) 2211 if (sc->sc_state == NCR_SELECTING)
2215 /* 2212 /*
2216 * This can happen if we are reselected 2213 * This can happen if we are reselected
2217 * while using DMA to select a target. 2214 * while using DMA to select a target.
2218 */ 2215 */
2219 /*void*/; 2216 /*void*/;
2220 else if (sc->sc_prevphase == MESSAGE_OUT_PHASE) { 2217 else if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
2221 /* 2218 /*
2222 * Our (multi-byte) message (eg SDTR) was 2219 * Our (multi-byte) message (eg SDTR) was
2223 * interrupted by the target to send 2220 * interrupted by the target to send
2224 * a MSG REJECT. 2221 * a MSG REJECT.
2225 * Print diagnostic if current phase 2222 * Print diagnostic if current phase
2226 * is not MESSAGE IN. 2223 * is not MESSAGE IN.
2227 */ 2224 */
2228 if (sc->sc_phase != MESSAGE_IN_PHASE) 2225 if (sc->sc_phase != MESSAGE_IN_PHASE)
2229 printf("%s: !TC on MSG OUT" 2226 printf("%s: !TC on MSG OUT"
2230 " [intr %x, stat %x, step %d]" 2227 " [intr %x, stat %x, step %d]"
2231 " prevphase %x, resid %lx\n", 2228 " prevphase %x, resid %lx\n",
2232 device_xname(sc->sc_dev), 2229 device_xname(sc->sc_dev),
2233 sc->sc_espintr, 2230 sc->sc_espintr,
2234 sc->sc_espstat, 2231 sc->sc_espstat,
2235 sc->sc_espstep, 2232 sc->sc_espstep,
2236 sc->sc_prevphase, 2233 sc->sc_prevphase,
2237 (u_long)sc->sc_omlen); 2234 (u_long)sc->sc_omlen);
2238 } else if (sc->sc_dleft == 0) { 2235 } else if (sc->sc_dleft == 0) {
2239 /* 2236 /*
2240 * The DMA operation was started for 2237 * The DMA operation was started for
2241 * a DATA transfer. Print a diagnostic 2238 * a DATA transfer. Print a diagnostic
2242 * if the DMA counter and TC bit 2239 * if the DMA counter and TC bit
2243 * appear to be out of sync. 2240 * appear to be out of sync.
2244 */ 2241 */
2245 printf("%s: !TC on DATA XFER" 2242 printf("%s: !TC on DATA XFER"
2246 " [intr %x, stat %x, step %d]" 2243 " [intr %x, stat %x, step %d]"
2247 " prevphase %x, resid %x\n", 2244 " prevphase %x, resid %x\n",
2248 device_xname(sc->sc_dev), 2245 device_xname(sc->sc_dev),
2249 sc->sc_espintr, 2246 sc->sc_espintr,
2250 sc->sc_espstat, 2247 sc->sc_espstat,
2251 sc->sc_espstep, 2248 sc->sc_espstep,
2252 sc->sc_prevphase, 2249 sc->sc_prevphase,
2253 ecb ? ecb->dleft : -1); 2250 ecb ? ecb->dleft : -1);
2254 } 2251 }
2255 } 2252 }
2256 } 2253 }
2257 2254
2258 /* 2255 /*
2259 * Check for less serious errors. 2256 * Check for less serious errors.
2260 */ 2257 */
2261 if ((sc->sc_espstat & NCRSTAT_PE) != 0) { 2258 if ((sc->sc_espstat & NCRSTAT_PE) != 0) {
2262 printf("%s: SCSI bus parity error\n", device_xname(sc->sc_dev)); 2259 printf("%s: SCSI bus parity error\n", device_xname(sc->sc_dev));
2263 if (sc->sc_prevphase == MESSAGE_IN_PHASE) 2260 if (sc->sc_prevphase == MESSAGE_IN_PHASE)
2264 ncr53c9x_sched_msgout(SEND_PARITY_ERROR); 2261 ncr53c9x_sched_msgout(SEND_PARITY_ERROR);
2265 else 2262 else
2266 ncr53c9x_sched_msgout(SEND_INIT_DET_ERR); 2263 ncr53c9x_sched_msgout(SEND_INIT_DET_ERR);
2267 } 2264 }
2268 2265
2269 if ((sc->sc_espintr & NCRINTR_DIS) != 0) { 2266 if ((sc->sc_espintr & NCRINTR_DIS) != 0) {
2270 sc->sc_msgify = 0; 2267 sc->sc_msgify = 0;
2271 NCR_INTS(("<DISC [intr %x, stat %x, step %d]>", 2268 NCR_INTS(("<DISC [intr %x, stat %x, step %d]>",
2272 sc->sc_espintr,sc->sc_espstat,sc->sc_espstep)); 2269 sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
2273 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) { 2270 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2274 NCRCMD(sc, NCRCMD_FLUSH); 2271 NCRCMD(sc, NCRCMD_FLUSH);
2275#if 0 2272#if 0
2276 DELAY(1); 2273 DELAY(1);
2277#endif 2274#endif
2278 } 2275 }
2279 /* 2276 /*
2280 * This command must (apparently) be issued within 2277 * This command must (apparently) be issued within
2281 * 250mS of a disconnect. So here you are... 2278 * 250mS of a disconnect. So here you are...
2282 */ 2279 */
2283 NCRCMD(sc, NCRCMD_ENSEL); 2280 NCRCMD(sc, NCRCMD_ENSEL);
2284 2281
2285 switch (sc->sc_state) { 2282 switch (sc->sc_state) {
2286 case NCR_RESELECTED: 2283 case NCR_RESELECTED:
2287 goto sched; 2284 goto sched;
2288 2285
2289 case NCR_SELECTING: 2286 case NCR_SELECTING:
2290 { 2287 {
2291 struct ncr53c9x_linfo *li; 2288 struct ncr53c9x_linfo *li;
2292 2289
2293 ecb->xs->error = XS_SELTIMEOUT; 2290 ecb->xs->error = XS_SELTIMEOUT;
2294 2291
2295 /* Selection timeout -- discard all LUNs if empty */ 2292 /* Selection timeout -- discard all LUNs if empty */
2296 periph = ecb->xs->xs_periph; 2293 periph = ecb->xs->xs_periph;
2297 ti = &sc->sc_tinfo[periph->periph_target]; 2294 ti = &sc->sc_tinfo[periph->periph_target];
2298 li = LIST_FIRST(&ti->luns); 2295 li = LIST_FIRST(&ti->luns);
2299 while (li != NULL) { 2296 while (li != NULL) {
2300 if (li->untagged == NULL && li->used == 0) { 2297 if (li->untagged == NULL && li->used == 0) {
2301 if (li->lun < NCR_NLUN) 2298 if (li->lun < NCR_NLUN)
2302 ti->lun[li->lun] = NULL; 2299 ti->lun[li->lun] = NULL;
2303 LIST_REMOVE(li, link); 2300 LIST_REMOVE(li, link);
2304 free(li, M_DEVBUF); 2301 free(li, M_DEVBUF);
2305 /* 2302 /*
2306 * Restart the search at the beginning 2303 * Restart the search at the beginning
2307 */ 2304 */
2308 li = LIST_FIRST(&ti->luns); 2305 li = LIST_FIRST(&ti->luns);
2309 continue; 2306 continue;
2310 } 2307 }
2311 li = LIST_NEXT(li, link); 2308 li = LIST_NEXT(li, link);
2312 } 2309 }
2313 goto finish; 2310 goto finish;
2314 } 2311 }
2315 case NCR_CONNECTED: 2312 case NCR_CONNECTED:
2316 if ((sc->sc_flags & NCR_SYNCHNEGO) != 0) { 2313 if ((sc->sc_flags & NCR_SYNCHNEGO) != 0) {
2317#ifdef NCR53C9X_DEBUG 2314#ifdef NCR53C9X_DEBUG
2318 if (ecb != NULL) 2315 if (ecb != NULL)
2319 scsipi_printaddr(ecb->xs->xs_periph); 2316 scsipi_printaddr(ecb->xs->xs_periph);
2320 printf("sync nego not completed!\n"); 2317 printf("sync nego not completed!\n");
2321#endif 2318#endif
2322 ti = &sc->sc_tinfo[ 2319 ti = &sc->sc_tinfo[
2323 ecb->xs->xs_periph->periph_target]; 2320 ecb->xs->xs_periph->periph_target];
2324 sc->sc_flags &= ~NCR_SYNCHNEGO; 2321 sc->sc_flags &= ~NCR_SYNCHNEGO;
2325 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE); 2322 ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
2326 } 2323 }
2327 2324
2328 /* it may be OK to disconnect */ 2325 /* it may be OK to disconnect */
2329 if ((sc->sc_flags & NCR_ABORTING) == 0) { 2326 if ((sc->sc_flags & NCR_ABORTING) == 0) {
2330 /* 2327 /*
2331 * Section 5.1.1 of the SCSI 2 spec 2328 * Section 5.1.1 of the SCSI 2 spec
2332 * suggests issuing a REQUEST SENSE 2329 * suggests issuing a REQUEST SENSE
2333 * following an unexpected disconnect. 2330 * following an unexpected disconnect.
2334 * Some devices go into a contingent 2331 * Some devices go into a contingent
2335 * allegiance condition when 2332 * allegiance condition when
2336 * disconnecting, and this is necessary 2333 * disconnecting, and this is necessary
2337 * to clean up their state. 2334 * to clean up their state.
2338 */ 2335 */
2339 printf("%s: unexpected disconnect " 2336 printf("%s: unexpected disconnect "
2340 "[state %d, intr %x, stat %x, phase(c %x, p %x)]; ", 2337 "[state %d, intr %x, stat %x, phase(c %x, p %x)]; ",
2341 device_xname(sc->sc_dev), sc->sc_state, 2338 device_xname(sc->sc_dev), sc->sc_state,
2342 sc->sc_espintr, sc->sc_espstat, 2339 sc->sc_espintr, sc->sc_espstat,
2343 sc->sc_phase, sc->sc_prevphase); 2340 sc->sc_phase, sc->sc_prevphase);
2344 2341
2345 if ((ecb->flags & ECB_SENSE) != 0) { 2342 if ((ecb->flags & ECB_SENSE) != 0) {
2346 printf("resetting\n"); 2343 printf("resetting\n");
2347 goto reset; 2344 goto reset;
2348 } 2345 }
2349 printf("sending REQUEST SENSE\n"); 2346 printf("sending REQUEST SENSE\n");
2350 callout_stop(&ecb->xs->xs_callout); 2347 callout_stop(&ecb->xs->xs_callout);
2351 ncr53c9x_sense(sc, ecb); 2348 ncr53c9x_sense(sc, ecb);
2352 goto out; 2349 goto out;
2353 } 2350 }
2354 2351
2355 ecb->xs->error = XS_TIMEOUT; 2352 ecb->xs->error = XS_TIMEOUT;
2356 goto finish; 2353 goto finish;
2357 2354
2358 case NCR_DISCONNECT: 2355 case NCR_DISCONNECT:
2359 sc->sc_nexus = NULL; 2356 sc->sc_nexus = NULL;
2360 goto sched; 2357 goto sched;
2361 2358
2362 case NCR_CMDCOMPLETE: 2359 case NCR_CMDCOMPLETE:
2363 goto finish; 2360 goto finish;
2364 } 2361 }
2365 } 2362 }
2366 2363
2367 switch (sc->sc_state) { 2364 switch (sc->sc_state) {
2368 2365
2369 case NCR_SBR: 2366 case NCR_SBR:
2370 printf("%s: waiting for SCSI Bus Reset to happen\n", 2367 printf("%s: waiting for SCSI Bus Reset to happen\n",
2371 device_xname(sc->sc_dev)); 2368 device_xname(sc->sc_dev));
2372 goto out; 2369 goto out;
2373 2370
2374 case NCR_RESELECTED: 2371 case NCR_RESELECTED:
2375 /* 2372 /*
2376 * we must be continuing a message ? 2373 * we must be continuing a message ?
2377 */ 2374 */
2378 printf("%s: unhandled reselect continuation, " 2375 printf("%s: unhandled reselect continuation, "
2379 "state %d, intr %02x\n", 2376 "state %d, intr %02x\n",
2380 device_xname(sc->sc_dev), sc->sc_state, sc->sc_espintr); 2377 device_xname(sc->sc_dev), sc->sc_state, sc->sc_espintr);
2381 ncr53c9x_init(sc, 1); 2378 ncr53c9x_init(sc, 1);
2382 goto out; 2379 goto out;
2383 2380
2384 case NCR_IDENTIFIED: 2381 case NCR_IDENTIFIED:
2385 ecb = sc->sc_nexus; 2382 ecb = sc->sc_nexus;
2386 if (sc->sc_phase != MESSAGE_IN_PHASE) { 2383 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2387 int i = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF); 2384 int i = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
2388 /* 2385 /*
2389 * Things are seriously screwed up. 2386 * Things are seriously screwed up.
2390 * Pull the brakes, i.e. reset 2387 * Pull the brakes, i.e. reset
2391 */ 2388 */
2392 printf("%s: target didn't send tag: %d bytes in fifo\n", 2389 printf("%s: target didn't send tag: %d bytes in fifo\n",
2393 device_xname(sc->sc_dev), i); 2390 device_xname(sc->sc_dev), i);
2394 /* Drain and display fifo */ 2391 /* Drain and display fifo */
2395 while (i-- > 0) 2392 while (i-- > 0)
2396 printf("[%d] ", NCR_READ_REG(sc, NCR_FIFO)); 2393 printf("[%d] ", NCR_READ_REG(sc, NCR_FIFO));
2397 2394
2398 ncr53c9x_init(sc, 1); 2395 ncr53c9x_init(sc, 1);
2399 goto out; 2396 goto out;
2400 } else 2397 } else
2401 goto msgin; 2398 goto msgin;
2402 2399
2403 case NCR_IDLE: 2400 case NCR_IDLE:
2404 case NCR_SELECTING: 2401 case NCR_SELECTING:
2405 ecb = sc->sc_nexus; 2402 ecb = sc->sc_nexus;
2406 if (sc->sc_espintr & NCRINTR_RESEL) { 2403 if (sc->sc_espintr & NCRINTR_RESEL) {
2407 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0; 2404 sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
2408 sc->sc_flags = 0; 2405 sc->sc_flags = 0;
2409 /* 2406 /*
2410 * If we're trying to select a 2407 * If we're trying to select a
2411 * target ourselves, push our command 2408 * target ourselves, push our command
2412 * back into the ready list. 2409 * back into the ready list.
2413 */ 2410 */
2414 if (sc->sc_state == NCR_SELECTING) { 2411 if (sc->sc_state == NCR_SELECTING) {
2415 NCR_INTS(("backoff selector ")); 2412 NCR_INTS(("backoff selector "));
2416 callout_stop(&ecb->xs->xs_callout); 2413 callout_stop(&ecb->xs->xs_callout);
2417 ncr53c9x_dequeue(sc, ecb); 2414 ncr53c9x_dequeue(sc, ecb);
2418 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain); 2415 TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
2419 ecb->flags |= ECB_READY; 2416 ecb->flags |= ECB_READY;
2420 ecb = sc->sc_nexus = NULL; 2417 ecb = sc->sc_nexus = NULL;
2421 } 2418 }
2422 sc->sc_state = NCR_RESELECTED; 2419 sc->sc_state = NCR_RESELECTED;
2423 if (sc->sc_phase != MESSAGE_IN_PHASE) { 2420 if (sc->sc_phase != MESSAGE_IN_PHASE) {
2424 /* 2421 /*
2425 * Things are seriously screwed up. 2422 * Things are seriously screwed up.
2426 * Pull the brakes, i.e. reset 2423 * Pull the brakes, i.e. reset
2427 */ 2424 */
2428 printf("%s: target didn't identify\n", 2425 printf("%s: target didn't identify\n",
2429 device_xname(sc->sc_dev)); 2426 device_xname(sc->sc_dev));
2430 ncr53c9x_init(sc, 1); 2427 ncr53c9x_init(sc, 1);
2431 goto out; 2428 goto out;
2432 } 2429 }
2433 /* 2430 /*
2434 * The C90 only inhibits FIFO writes until reselection 2431 * The C90 only inhibits FIFO writes until reselection
2435 * is complete, instead of waiting until the interrupt 2432 * is complete, instead of waiting until the interrupt
2436 * status register has been read. So, if the reselect 2433 * status register has been read. So, if the reselect
2437 * happens while we were entering command bytes (for 2434 * happens while we were entering command bytes (for
2438 * another target) some of those bytes can appear in 2435 * another target) some of those bytes can appear in
2439 * the FIFO here, after the interrupt is taken. 2436 * the FIFO here, after the interrupt is taken.
2440 * 2437 *
2441 * To remedy this situation, pull the Selection ID 2438 * To remedy this situation, pull the Selection ID
2442 * and Identify message from the FIFO directly, and 2439 * and Identify message from the FIFO directly, and
2443 * ignore any extraneous fifo contents. Also, set 2440 * ignore any extraneous fifo contents. Also, set
2444 * a flag that allows one Illegal Command Interrupt 2441 * a flag that allows one Illegal Command Interrupt
2445 * to occur which the chip also generates as a result 2442 * to occur which the chip also generates as a result
2446 * of writing to the FIFO during a reselect. 2443 * of writing to the FIFO during a reselect.
2447 */ 2444 */
2448 if (sc->sc_rev == NCR_VARIANT_ESP100) { 2445 if (sc->sc_rev == NCR_VARIANT_ESP100) {
2449 nfifo = NCR_READ_REG(sc, NCR_FFLAG) & 2446 nfifo = NCR_READ_REG(sc, NCR_FFLAG) &
2450 NCRFIFO_FF; 2447 NCRFIFO_FF;
2451 sc->sc_imess[0] = NCR_READ_REG(sc, NCR_FIFO); 2448 sc->sc_imess[0] = NCR_READ_REG(sc, NCR_FIFO);
2452 sc->sc_imess[1] = NCR_READ_REG(sc, NCR_FIFO); 2449 sc->sc_imess[1] = NCR_READ_REG(sc, NCR_FIFO);
2453 sc->sc_imlen = 2; 2450 sc->sc_imlen = 2;
2454 if (nfifo != 2) { 2451 if (nfifo != 2) {
2455 /* Flush the rest */ 2452 /* Flush the rest */
2456 NCRCMD(sc, NCRCMD_FLUSH); 2453 NCRCMD(sc, NCRCMD_FLUSH);
2457 } 2454 }
2458 sc->sc_flags |= NCR_EXPECT_ILLCMD; 2455 sc->sc_flags |= NCR_EXPECT_ILLCMD;
2459 if (nfifo > 2) 2456 if (nfifo > 2)
2460 nfifo = 2; /* We fixed it.. */ 2457 nfifo = 2; /* We fixed it.. */
2461 } else 2458 } else
2462 nfifo = ncr53c9x_rdfifo(sc, NCR_RDFIFO_START); 2459 nfifo = ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
2463 2460
2464 if (nfifo != 2) { 2461 if (nfifo != 2) {
2465 printf("%s: RESELECT: %d bytes in FIFO! " 2462 printf("%s: RESELECT: %d bytes in FIFO! "
2466 "[intr %x, stat %x, step %d, " 2463 "[intr %x, stat %x, step %d, "
2467 "prevphase %x]\n", 2464 "prevphase %x]\n",
2468 device_xname(sc->sc_dev), 2465 device_xname(sc->sc_dev),
2469 nfifo, 2466 nfifo,
2470 sc->sc_espintr, 2467 sc->sc_espintr,
2471 sc->sc_espstat, 2468 sc->sc_espstat,
2472 sc->sc_espstep, 2469 sc->sc_espstep,
2473 sc->sc_prevphase); 2470 sc->sc_prevphase);
2474 ncr53c9x_init(sc, 1); 2471 ncr53c9x_init(sc, 1);
2475 goto out; 2472 goto out;
2476 } 2473 }
2477 sc->sc_selid = sc->sc_imess[0]; 2474 sc->sc_selid = sc->sc_imess[0];
2478 NCR_INTS(("selid=%02x ", sc->sc_selid)); 2475 NCR_INTS(("selid=%02x ", sc->sc_selid));
2479 2476
2480 /* Handle identify message */ 2477 /* Handle identify message */
2481 ncr53c9x_msgin(sc); 2478 ncr53c9x_msgin(sc);
2482 2479
2483 if (sc->sc_state != NCR_CONNECTED && 2480 if (sc->sc_state != NCR_CONNECTED &&
2484 sc->sc_state != NCR_IDENTIFIED) { 2481 sc->sc_state != NCR_IDENTIFIED) {
2485 /* IDENTIFY fail?! */ 2482 /* IDENTIFY fail?! */
2486 printf("%s: identify failed, " 2483 printf("%s: identify failed, "
2487 "state %d, intr %02x\n", 2484 "state %d, intr %02x\n",
2488 device_xname(sc->sc_dev), 2485 device_xname(sc->sc_dev),
2489 sc->sc_state, sc->sc_espintr); 2486 sc->sc_state, sc->sc_espintr);
2490 ncr53c9x_init(sc, 1); 2487 ncr53c9x_init(sc, 1);
2491 goto out; 2488 goto out;
2492 } 2489 }
2493 goto shortcut; /* ie. next phase expected soon */ 2490 goto shortcut; /* ie. next phase expected soon */
2494 } 2491 }
2495 2492
2496#define NCRINTR_DONE (NCRINTR_FC | NCRINTR_BS) 2493#define NCRINTR_DONE (NCRINTR_FC | NCRINTR_BS)
2497 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) { 2494 if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
2498 /* 2495 /*
2499 * Arbitration won; examine the `step' register 2496 * Arbitration won; examine the `step' register
2500 * to determine how far the selection could progress. 2497 * to determine how far the selection could progress.
2501 */ 2498 */
2502 ecb = sc->sc_nexus; 2499 ecb = sc->sc_nexus;
2503 if (ecb == NULL) 2500 if (ecb == NULL)
2504 panic("%s: no nexus", __func__); 2501 panic("%s: no nexus", __func__);
2505 2502
2506 periph = ecb->xs->xs_periph; 2503 periph = ecb->xs->xs_periph;
2507 ti = &sc->sc_tinfo[periph->periph_target]; 2504 ti = &sc->sc_tinfo[periph->periph_target];
2508 2505
2509 switch (sc->sc_espstep) { 2506 switch (sc->sc_espstep) {
2510 case 0: 2507 case 0:
2511 /* 2508 /*
2512 * The target did not respond with a 2509 * The target did not respond with a
2513 * message out phase - probably an old 2510 * message out phase - probably an old
2514 * device that doesn't recognize ATN. 2511 * device that doesn't recognize ATN.
2515 * Clear ATN and just continue, the 2512 * Clear ATN and just continue, the
2516 * target should be in the command 2513 * target should be in the command
2517 * phase. 2514 * phase.
2518 * XXXX check for command phase? 2515 * XXXX check for command phase?
2519 */ 2516 */
2520 NCRCMD(sc, NCRCMD_RSTATN); 2517 NCRCMD(sc, NCRCMD_RSTATN);
2521 break; 2518 break;
2522 case 1: 2519 case 1:
2523 if ((ti->flags & T_NEGOTIATE) == 0 && 2520 if ((ti->flags & T_NEGOTIATE) == 0 &&
2524 ecb->tag[0] == 0) { 2521 ecb->tag[0] == 0) {
2525 printf("%s: step 1 & !NEG\n", 2522 printf("%s: step 1 & !NEG\n",
2526 device_xname(sc->sc_dev)); 2523 device_xname(sc->sc_dev));
2527 goto reset; 2524 goto reset;
2528 } 2525 }
2529 if (sc->sc_phase != MESSAGE_OUT_PHASE) { 2526 if (sc->sc_phase != MESSAGE_OUT_PHASE) {
2530 printf("%s: !MSGOUT\n", 2527 printf("%s: !MSGOUT\n",
2531 device_xname(sc->sc_dev)); 2528 device_xname(sc->sc_dev));
2532 goto reset; 2529 goto reset;
2533 } 2530 }
2534 if (ti->flags & T_WIDE) { 2531 if (ti->flags & T_WIDE) {
2535 ti->flags |= T_WDTRSENT; 2532 ti->flags |= T_WDTRSENT;
2536 ncr53c9x_sched_msgout(SEND_WDTR); 2533 ncr53c9x_sched_msgout(SEND_WDTR);
2537 } 2534 }
2538 if (ti->flags & T_NEGOTIATE) { 2535 if (ti->flags & T_NEGOTIATE) {
2539 /* Start negotiating */ 2536 /* Start negotiating */
2540 ti->period = sc->sc_minsync; 2537 ti->period = sc->sc_minsync;
2541 ti->offset = 15; 2538 ti->offset = 15;
2542 sc->sc_flags |= NCR_SYNCHNEGO; 2539 sc->sc_flags |= NCR_SYNCHNEGO;
2543 if (ecb->tag[0]) 2540 if (ecb->tag[0])
2544 ncr53c9x_sched_msgout( 2541 ncr53c9x_sched_msgout(
2545 SEND_TAG | SEND_SDTR); 2542 SEND_TAG | SEND_SDTR);
2546 else 2543 else
2547 ncr53c9x_sched_msgout( 2544 ncr53c9x_sched_msgout(
2548 SEND_SDTR); 2545 SEND_SDTR);
2549 } else { 2546 } else {
2550 /* Could not do ATN3 so send TAG */ 2547 /* Could not do ATN3 so send TAG */
2551 ncr53c9x_sched_msgout(SEND_TAG); 2548 ncr53c9x_sched_msgout(SEND_TAG);
2552 } 2549 }
2553 sc->sc_prevphase = MESSAGE_OUT_PHASE; /* XXXX */ 2550 sc->sc_prevphase = MESSAGE_OUT_PHASE; /* XXXX */
2554 break; 2551 break;
2555 case 3: 2552 case 3:
2556 /* 2553 /*
2557 * Grr, this is supposed to mean 2554 * Grr, this is supposed to mean
2558 * "target left command phase prematurely". 2555 * "target left command phase prematurely".
2559 * It seems to happen regularly when 2556 * It seems to happen regularly when
2560 * sync mode is on. 2557 * sync mode is on.
2561 * Look at FIFO to see if command went out. 2558 * Look at FIFO to see if command went out.
2562 * (Timing problems?) 2559 * (Timing problems?)
2563 */ 2560 */
2564 if (sc->sc_features & NCR_F_DMASELECT) { 2561 if (sc->sc_features & NCR_F_DMASELECT) {
2565 if (sc->sc_cmdlen == 0) 2562 if (sc->sc_cmdlen == 0)
2566 /* Hope for the best.. */ 2563 /* Hope for the best.. */
2567 break; 2564 break;
2568 } else if ((NCR_READ_REG(sc, NCR_FFLAG) 2565 } else if ((NCR_READ_REG(sc, NCR_FFLAG)
2569 & NCRFIFO_FF) == 0) { 2566 & NCRFIFO_FF) == 0) {
2570 /* Hope for the best.. */ 2567 /* Hope for the best.. */
2571 break; 2568 break;
2572 } 2569 }
2573 printf("(%s:%d:%d): selection failed;" 2570 printf("(%s:%d:%d): selection failed;"
2574 " %d left in FIFO " 2571 " %d left in FIFO "
2575 "[intr %x, stat %x, step %d]\n", 2572 "[intr %x, stat %x, step %d]\n",
2576 device_xname(sc->sc_dev), 2573 device_xname(sc->sc_dev),
2577 periph->periph_target, 2574 periph->periph_target,
2578 periph->periph_lun, 2575 periph->periph_lun,
2579 NCR_READ_REG(sc, NCR_FFLAG) 2576 NCR_READ_REG(sc, NCR_FFLAG)
2580 & NCRFIFO_FF, 2577 & NCRFIFO_FF,
2581 sc->sc_espintr, sc->sc_espstat, 2578 sc->sc_espintr, sc->sc_espstat,
2582 sc->sc_espstep); 2579 sc->sc_espstep);
2583 NCRCMD(sc, NCRCMD_FLUSH); 2580 NCRCMD(sc, NCRCMD_FLUSH);
2584 ncr53c9x_sched_msgout(SEND_ABORT); 2581 ncr53c9x_sched_msgout(SEND_ABORT);
2585 goto out; 2582 goto out;
2586 case 2: 2583 case 2:
2587 /* Select stuck at Command Phase */ 2584 /* Select stuck at Command Phase */
2588 NCRCMD(sc, NCRCMD_FLUSH); 2585 NCRCMD(sc, NCRCMD_FLUSH);
2589 break; 2586 break;
2590 case 4: 2587 case 4:
2591 if (sc->sc_features & NCR_F_DMASELECT && 2588 if (sc->sc_features & NCR_F_DMASELECT &&
2592 sc->sc_cmdlen != 0) 2589 sc->sc_cmdlen != 0)
2593 printf("(%s:%d:%d): select; " 2590 printf("(%s:%d:%d): select; "
2594 "%lu left in DMA buffer " 2591 "%lu left in DMA buffer "
2595 "[intr %x, stat %x, step %d]\n", 2592 "[intr %x, stat %x, step %d]\n",
2596 device_xname(sc->sc_dev), 2593 device_xname(sc->sc_dev),
2597 periph->periph_target, 2594 periph->periph_target,
2598 periph->periph_lun, 2595 periph->periph_lun,
2599 (u_long)sc->sc_cmdlen, 2596 (u_long)sc->sc_cmdlen,
2600 sc->sc_espintr, 2597 sc->sc_espintr,
2601 sc->sc_espstat, 2598 sc->sc_espstat,
2602 sc->sc_espstep); 2599 sc->sc_espstep);
2603 /* So far, everything went fine */ 2600 /* So far, everything went fine */
2604 break; 2601 break;
2605 } 2602 }
2606 2603
2607 sc->sc_prevphase = INVALID_PHASE; /* ?? */ 2604 sc->sc_prevphase = INVALID_PHASE; /* ?? */
2608 /* Do an implicit RESTORE POINTERS. */ 2605 /* Do an implicit RESTORE POINTERS. */
2609 sc->sc_dp = ecb->daddr; 2606 sc->sc_dp = ecb->daddr;
2610 sc->sc_dleft = ecb->dleft; 2607 sc->sc_dleft = ecb->dleft;
2611 sc->sc_state = NCR_CONNECTED; 2608 sc->sc_state = NCR_CONNECTED;
2612 break; 2609 break;
2613 2610
2614 } else { 2611 } else {
2615 2612
2616 printf("%s: unexpected status after select" 2613 printf("%s: unexpected status after select"
2617 ": [intr %x, stat %x, step %x]\n", 2614 ": [intr %x, stat %x, step %x]\n",
2618 device_xname(sc->sc_dev), 2615 device_xname(sc->sc_dev),
2619 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep); 2616 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2620 NCRCMD(sc, NCRCMD_FLUSH); 2617 NCRCMD(sc, NCRCMD_FLUSH);
2621 DELAY(1); 2618 DELAY(1);
2622 goto reset; 2619 goto reset;
2623 } 2620 }
2624 if (sc->sc_state == NCR_IDLE) { 2621 if (sc->sc_state == NCR_IDLE) {
2625 printf("%s: stray interrupt\n", 2622 printf("%s: stray interrupt\n",
2626 device_xname(sc->sc_dev)); 2623 device_xname(sc->sc_dev));
2627 simple_unlock(&sc->sc_lock); 2624 mutex_exit(&sc->sc_lock);
2628 return 0; 2625 return 0;
2629 } 2626 }
2630 break; 2627 break;
2631 2628
2632 case NCR_CONNECTED: 2629 case NCR_CONNECTED:
2633 if ((sc->sc_flags & NCR_ICCS) != 0) { 2630 if ((sc->sc_flags & NCR_ICCS) != 0) {
2634 /* "Initiate Command Complete Steps" in progress */ 2631 /* "Initiate Command Complete Steps" in progress */
2635 uint8_t msg; 2632 uint8_t msg;
2636 2633
2637 sc->sc_flags &= ~NCR_ICCS; 2634 sc->sc_flags &= ~NCR_ICCS;
2638 2635
2639 if ((sc->sc_espintr & NCRINTR_DONE) == 0) { 2636 if ((sc->sc_espintr & NCRINTR_DONE) == 0) {
2640 printf("%s: ICCS: " 2637 printf("%s: ICCS: "
2641 ": [intr %x, stat %x, step %x]\n", 2638 ": [intr %x, stat %x, step %x]\n",
2642 device_xname(sc->sc_dev), 2639 device_xname(sc->sc_dev),
2643 sc->sc_espintr, sc->sc_espstat, 2640 sc->sc_espintr, sc->sc_espstat,
2644 sc->sc_espstep); 2641 sc->sc_espstep);
2645 } 2642 }
2646 ncr53c9x_rdfifo(sc, NCR_RDFIFO_START); 2643 ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
2647 if (sc->sc_imlen < 2) 2644 if (sc->sc_imlen < 2)
2648 printf("%s: can't get status, only %d bytes\n", 2645 printf("%s: can't get status, only %d bytes\n",
2649 device_xname(sc->sc_dev), 2646 device_xname(sc->sc_dev),
2650 (int)sc->sc_imlen); 2647 (int)sc->sc_imlen);
2651 ecb->stat = sc->sc_imess[sc->sc_imlen - 2]; 2648 ecb->stat = sc->sc_imess[sc->sc_imlen - 2];
2652 msg = sc->sc_imess[sc->sc_imlen - 1]; 2649 msg = sc->sc_imess[sc->sc_imlen - 1];
2653 NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg)); 2650 NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
2654 if (msg == MSG_CMDCOMPLETE) { 2651 if (msg == MSG_CMDCOMPLETE) {
2655 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE) 2652 ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
2656 ? 0 : sc->sc_dleft; 2653 ? 0 : sc->sc_dleft;
2657 if ((ecb->flags & ECB_SENSE) == 0) 2654 if ((ecb->flags & ECB_SENSE) == 0)
2658 ecb->xs->resid = ecb->dleft; 2655 ecb->xs->resid = ecb->dleft;
2659 sc->sc_state = NCR_CMDCOMPLETE; 2656 sc->sc_state = NCR_CMDCOMPLETE;
2660 } else 2657 } else
2661 printf("%s: STATUS_PHASE: msg %d\n", 2658 printf("%s: STATUS_PHASE: msg %d\n",
2662 device_xname(sc->sc_dev), msg); 2659 device_xname(sc->sc_dev), msg);
2663 sc->sc_imlen = 0; 2660 sc->sc_imlen = 0;
2664 NCRCMD(sc, NCRCMD_MSGOK); 2661 NCRCMD(sc, NCRCMD_MSGOK);
2665 goto shortcut; /* ie. wait for disconnect */ 2662 goto shortcut; /* ie. wait for disconnect */
2666 } 2663 }
2667 break; 2664 break;
2668 2665
2669 default: 2666 default:
2670 printf("%s: invalid state: %d [intr %x, phase(c %x, p %x)]\n", 2667 printf("%s: invalid state: %d [intr %x, phase(c %x, p %x)]\n",
2671 device_xname(sc->sc_dev), sc->sc_state, 2668 device_xname(sc->sc_dev), sc->sc_state,
2672 sc->sc_espintr, sc->sc_phase, sc->sc_prevphase); 2669 sc->sc_espintr, sc->sc_phase, sc->sc_prevphase);
2673 goto reset; 2670 goto reset;
2674 } 2671 }
2675 2672
2676 /* 2673 /*
2677 * Driver is now in state NCR_CONNECTED, i.e. we 2674 * Driver is now in state NCR_CONNECTED, i.e. we
2678 * have a current command working the SCSI bus. 2675 * have a current command working the SCSI bus.
2679 */ 2676 */
2680 if (sc->sc_state != NCR_CONNECTED || ecb == NULL) { 2677 if (sc->sc_state != NCR_CONNECTED || ecb == NULL) {
2681 panic("%s: no nexus", __func__); 2678 panic("%s: no nexus", __func__);
2682 } 2679 }
2683 2680
2684 switch (sc->sc_phase) { 2681 switch (sc->sc_phase) {
2685 case MESSAGE_OUT_PHASE: 2682 case MESSAGE_OUT_PHASE:
2686 NCR_PHASE(("MESSAGE_OUT_PHASE ")); 2683 NCR_PHASE(("MESSAGE_OUT_PHASE "));
2687 ncr53c9x_msgout(sc); 2684 ncr53c9x_msgout(sc);
2688 sc->sc_prevphase = MESSAGE_OUT_PHASE; 2685 sc->sc_prevphase = MESSAGE_OUT_PHASE;
2689 break; 2686 break;
2690 2687
2691 case MESSAGE_IN_PHASE: 2688 case MESSAGE_IN_PHASE:
2692msgin: 2689msgin:
2693 NCR_PHASE(("MESSAGE_IN_PHASE ")); 2690 NCR_PHASE(("MESSAGE_IN_PHASE "));
2694 if ((sc->sc_espintr & NCRINTR_BS) != 0) { 2691 if ((sc->sc_espintr & NCRINTR_BS) != 0) {
2695 if ((sc->sc_rev != NCR_VARIANT_FAS366) || 2692 if ((sc->sc_rev != NCR_VARIANT_FAS366) ||
2696 (sc->sc_espstat2 & NCRFAS_STAT2_EMPTY) == 0) { 2693 (sc->sc_espstat2 & NCRFAS_STAT2_EMPTY) == 0) {
2697 NCRCMD(sc, NCRCMD_FLUSH); 2694 NCRCMD(sc, NCRCMD_FLUSH);
2698 } 2695 }
2699 sc->sc_flags |= NCR_WAITI; 2696 sc->sc_flags |= NCR_WAITI;
2700 NCRCMD(sc, NCRCMD_TRANS); 2697 NCRCMD(sc, NCRCMD_TRANS);
2701 } else if ((sc->sc_espintr & NCRINTR_FC) != 0) { 2698 } else if ((sc->sc_espintr & NCRINTR_FC) != 0) {
2702 if ((sc->sc_flags & NCR_WAITI) == 0) { 2699 if ((sc->sc_flags & NCR_WAITI) == 0) {
2703 printf("%s: MSGIN: unexpected FC bit: " 2700 printf("%s: MSGIN: unexpected FC bit: "
2704 "[intr %x, stat %x, step %x]\n", 2701 "[intr %x, stat %x, step %x]\n",
2705 device_xname(sc->sc_dev), 2702 device_xname(sc->sc_dev),
2706 sc->sc_espintr, sc->sc_espstat, 2703 sc->sc_espintr, sc->sc_espstat,
2707 sc->sc_espstep); 2704 sc->sc_espstep);
2708 } 2705 }
2709 sc->sc_flags &= ~NCR_WAITI; 2706 sc->sc_flags &= ~NCR_WAITI;
2710 ncr53c9x_rdfifo(sc, 2707 ncr53c9x_rdfifo(sc,
2711 (sc->sc_prevphase == sc->sc_phase) ? 2708 (sc->sc_prevphase == sc->sc_phase) ?
2712 NCR_RDFIFO_CONTINUE : NCR_RDFIFO_START); 2709 NCR_RDFIFO_CONTINUE : NCR_RDFIFO_START);
2713 ncr53c9x_msgin(sc); 2710 ncr53c9x_msgin(sc);
2714 } else { 2711 } else {
2715 printf("%s: MSGIN: weird bits: " 2712 printf("%s: MSGIN: weird bits: "
2716 "[intr %x, stat %x, step %x]\n", 2713 "[intr %x, stat %x, step %x]\n",
2717 device_xname(sc->sc_dev), 2714 device_xname(sc->sc_dev),
2718 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep); 2715 sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
2719 } 2716 }
2720 sc->sc_prevphase = MESSAGE_IN_PHASE; 2717 sc->sc_prevphase = MESSAGE_IN_PHASE;
2721 goto shortcut; /* i.e. expect data to be ready */ 2718 goto shortcut; /* i.e. expect data to be ready */
2722 2719
2723 case COMMAND_PHASE: 2720 case COMMAND_PHASE:
2724 /* 2721 /*
2725 * Send the command block. Normally we don't see this 2722 * Send the command block. Normally we don't see this
2726 * phase because the SEL_ATN command takes care of 2723 * phase because the SEL_ATN command takes care of
2727 * all this. However, we end up here if either the 2724 * all this. However, we end up here if either the
2728 * target or we wanted to exchange some more messages 2725 * target or we wanted to exchange some more messages
2729 * first (e.g. to start negotiations). 2726 * first (e.g. to start negotiations).
2730 */ 2727 */
2731 2728
2732 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ", 2729 NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
2733 ecb->cmd.cmd.opcode, ecb->clen)); 2730 ecb->cmd.cmd.opcode, ecb->clen));
2734 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) { 2731 if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
2735 NCRCMD(sc, NCRCMD_FLUSH); 2732 NCRCMD(sc, NCRCMD_FLUSH);
2736#if 0 2733#if 0
2737 DELAY(1); 2734 DELAY(1);
2738#endif 2735#endif
2739 } 2736 }
2740 if (sc->sc_features & NCR_F_DMASELECT) { 2737 if (sc->sc_features & NCR_F_DMASELECT) {
2741 /* setup DMA transfer for command */ 2738 /* setup DMA transfer for command */
2742 size = ecb->clen; 2739 size = ecb->clen;
2743 sc->sc_cmdlen = size; 2740 sc->sc_cmdlen = size;
2744 sc->sc_cmdp = (void *)&ecb->cmd.cmd; 2741 sc->sc_cmdp = (void *)&ecb->cmd.cmd;
2745 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 2742 NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
2746 0, &size); 2743 0, &size);
2747 /* Program the SCSI counter */ 2744 /* Program the SCSI counter */
2748 NCR_SET_COUNT(sc, size); 2745 NCR_SET_COUNT(sc, size);
2749 2746
2750 /* load the count in */ 2747 /* load the count in */
2751 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA); 2748 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
2752 2749
2753 /* start the command transfer */ 2750 /* start the command transfer */
2754 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA); 2751 NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
2755 NCRDMA_GO(sc); 2752 NCRDMA_GO(sc);
2756 } else { 2753 } else {
2757 ncr53c9x_wrfifo(sc, (uint8_t *)&ecb->cmd.cmd, 2754 ncr53c9x_wrfifo(sc, (uint8_t *)&ecb->cmd.cmd,
2758 ecb->clen); 2755 ecb->clen);
2759 NCRCMD(sc, NCRCMD_TRANS); 2756 NCRCMD(sc, NCRCMD_TRANS);
2760 } 2757 }
2761 sc->sc_prevphase = COMMAND_PHASE; 2758 sc->sc_prevphase = COMMAND_PHASE;
2762 break; 2759 break;
2763 2760
2764 case DATA_OUT_PHASE: 2761 case DATA_OUT_PHASE:
2765 NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft)); 2762 NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft));
2766 NCRCMD(sc, NCRCMD_FLUSH); 2763 NCRCMD(sc, NCRCMD_FLUSH);
2767 size = min(sc->sc_dleft, sc->sc_maxxfer); 2764 size = min(sc->sc_dleft, sc->sc_maxxfer);
2768 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 0, &size); 2765 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 0, &size);
2769 sc->sc_prevphase = DATA_OUT_PHASE; 2766 sc->sc_prevphase = DATA_OUT_PHASE;
2770 goto setup_xfer; 2767 goto setup_xfer;
2771 2768
2772 case DATA_IN_PHASE: 2769 case DATA_IN_PHASE:
2773 NCR_PHASE(("DATA_IN_PHASE ")); 2770 NCR_PHASE(("DATA_IN_PHASE "));
2774 if (sc->sc_rev == NCR_VARIANT_ESP100) 2771 if (sc->sc_rev == NCR_VARIANT_ESP100)
2775 NCRCMD(sc, NCRCMD_FLUSH); 2772 NCRCMD(sc, NCRCMD_FLUSH);
2776 size = min(sc->sc_dleft, sc->sc_maxxfer); 2773 size = min(sc->sc_dleft, sc->sc_maxxfer);
2777 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 1, &size); 2774 NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 1, &size);
2778 sc->sc_prevphase = DATA_IN_PHASE; 2775 sc->sc_prevphase = DATA_IN_PHASE;
2779 setup_xfer: 2776 setup_xfer:
2780 /* Target returned to data phase: wipe "done" memory */ 2777 /* Target returned to data phase: wipe "done" memory */
2781 ecb->flags &= ~ECB_TENTATIVE_DONE; 2778 ecb->flags &= ~ECB_TENTATIVE_DONE;
2782 2779
2783 /* Program the SCSI counter */ 2780 /* Program the SCSI counter */
2784 NCR_SET_COUNT(sc, size); 2781 NCR_SET_COUNT(sc, size);
2785 2782
2786 /* load the count in */ 2783 /* load the count in */
2787 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA); 2784 NCRCMD(sc, NCRCMD_NOP | NCRCMD_DMA);
2788 2785
2789 /* 2786 /*
2790 * Note that if `size' is 0, we've already transceived 2787 * Note that if `size' is 0, we've already transceived
2791 * all the bytes we want but we're still in DATA PHASE. 2788 * all the bytes we want but we're still in DATA PHASE.
2792 * Apparently, the device needs padding. Also, a 2789 * Apparently, the device needs padding. Also, a
2793 * transfer size of 0 means "maximum" to the chip 2790 * transfer size of 0 means "maximum" to the chip
2794 * DMA logic. 2791 * DMA logic.
2795 */ 2792 */
2796 NCRCMD(sc, 2793 NCRCMD(sc,
2797 (size == 0 ? NCRCMD_TRPAD : NCRCMD_TRANS) | NCRCMD_DMA); 2794 (size == 0 ? NCRCMD_TRPAD : NCRCMD_TRANS) | NCRCMD_DMA);
2798 NCRDMA_GO(sc); 2795 NCRDMA_GO(sc);
2799 goto out; 2796 goto out;
2800 2797
2801 case STATUS_PHASE: 2798 case STATUS_PHASE:
2802 NCR_PHASE(("STATUS_PHASE ")); 2799 NCR_PHASE(("STATUS_PHASE "));
2803 sc->sc_flags |= NCR_ICCS; 2800 sc->sc_flags |= NCR_ICCS;
2804 NCRCMD(sc, NCRCMD_ICCS); 2801 NCRCMD(sc, NCRCMD_ICCS);
2805 sc->sc_prevphase = STATUS_PHASE; 2802 sc->sc_prevphase = STATUS_PHASE;
2806 goto shortcut; /* i.e. expect status results soon */ 2803 goto shortcut; /* i.e. expect status results soon */
2807 2804
2808 case INVALID_PHASE: 2805 case INVALID_PHASE:
2809 break; 2806 break;
2810 2807
2811 default: 2808 default:
2812 printf("%s: unexpected bus phase; resetting\n", 2809 printf("%s: unexpected bus phase; resetting\n",
2813 device_xname(sc->sc_dev)); 2810 device_xname(sc->sc_dev));
2814 goto reset; 2811 goto reset;
2815 } 2812 }
2816 2813
2817out: 2814out:
2818 simple_unlock(&sc->sc_lock); 2815 mutex_exit(&sc->sc_lock);
2819 return 1; 2816 return 1;
2820 2817
2821reset: 2818reset:
2822 ncr53c9x_init(sc, 1); 2819 ncr53c9x_init(sc, 1);
2823 goto out; 2820 goto out;
2824 2821
2825finish: 2822finish:
2826 ncr53c9x_done(sc, ecb); 2823 ncr53c9x_done(sc, ecb);
2827 goto out; 2824 goto out;
2828 2825
2829sched: 2826sched:
2830 sc->sc_state = NCR_IDLE; 2827 sc->sc_state = NCR_IDLE;
2831 ncr53c9x_sched(sc); 2828 ncr53c9x_sched(sc);
2832 goto out; 2829 goto out;
2833 2830
2834shortcut: 2831shortcut:
2835 /* 2832 /*
2836 * The idea is that many of the SCSI operations take very little 2833 * The idea is that many of the SCSI operations take very little
2837 * time, and going away and getting interrupted is too high an 2834 * time, and going away and getting interrupted is too high an
2838 * overhead to pay. For example, selecting, sending a message 2835 * overhead to pay. For example, selecting, sending a message
2839 * and command and then doing some work can be done in one "pass". 2836 * and command and then doing some work can be done in one "pass".
2840 * 2837 *
2841 * The delay is a heuristic. It is 2 when at 20MHz, 2 at 25MHz and 1 2838 * The delay is a heuristic. It is 2 when at 20MHz, 2 at 25MHz and 1
2842 * at 40MHz. This needs testing. 2839 * at 40MHz. This needs testing.
2843 */ 2840 */
2844 { 2841 {
2845 struct timeval wait, cur; 2842 struct timeval wait, cur;
2846 2843
2847 microtime(&wait); 2844 microtime(&wait);
2848 wait.tv_usec += 50 / sc->sc_freq; 2845 wait.tv_usec += 50 / sc->sc_freq;
2849 if (wait.tv_usec > 1000000) { 2846 if (wait.tv_usec > 1000000) {
2850 wait.tv_sec++; 2847 wait.tv_sec++;
2851 wait.tv_usec -= 1000000; 2848 wait.tv_usec -= 1000000;
2852 } 2849 }
2853 do { 2850 do {
2854 if (NCRDMA_ISINTR(sc)) 2851 if (NCRDMA_ISINTR(sc))
2855 goto again; 2852 goto again;
2856 microtime(&cur); 2853 microtime(&cur);
2857 } while (timercmp(&cur, &wait, <=)); 2854 } while (timercmp(&cur, &wait, <=));
2858 } 2855 }
2859 goto out; 2856 goto out;
2860} 2857}
2861 2858
2862void 2859void
2863ncr53c9x_abort(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb) 2860ncr53c9x_abort(struct ncr53c9x_softc *sc, struct ncr53c9x_ecb *ecb)
2864{ 2861{
2865 2862
2866 /* 2 secs for the abort */ 2863 /* 2 secs for the abort */
2867 ecb->timeout = NCR_ABORT_TIMEOUT; 2864 ecb->timeout = NCR_ABORT_TIMEOUT;
2868 ecb->flags |= ECB_ABORT; 2865 ecb->flags |= ECB_ABORT;
2869 2866
2870 if (ecb == sc->sc_nexus) { 2867 if (ecb == sc->sc_nexus) {
2871 /* 2868 /*
2872 * If we're still selecting, the message will be scheduled 2869 * If we're still selecting, the message will be scheduled
2873 * after selection is complete. 2870 * after selection is complete.
2874 */ 2871 */
2875 if (sc->sc_state == NCR_CONNECTED) 2872 if (sc->sc_state == NCR_CONNECTED)
2876 ncr53c9x_sched_msgout(SEND_ABORT); 2873 ncr53c9x_sched_msgout(SEND_ABORT);
2877 2874
2878 /* 2875 /*
2879 * Reschedule timeout. 2876 * Reschedule timeout.
2880 */ 2877 */
2881 callout_reset(&ecb->xs->xs_callout, mstohz(ecb->timeout), 2878 callout_reset(&ecb->xs->xs_callout, mstohz(ecb->timeout),
2882 ncr53c9x_timeout, ecb); 2879 ncr53c9x_timeout, ecb);
2883 } else { 2880 } else {
2884 /* 2881 /*
2885 * Just leave the command where it is. 2882 * Just leave the command where it is.
2886 * XXX - what choice do we have but to reset the SCSI 2883 * XXX - what choice do we have but to reset the SCSI
2887 * eventually? 2884 * eventually?
2888 */ 2885 */
2889 if (sc->sc_state == NCR_IDLE) 2886 if (sc->sc_state == NCR_IDLE)
2890 ncr53c9x_sched(sc); 2887 ncr53c9x_sched(sc);
2891 } 2888 }
2892} 2889}
2893 2890
2894void 2891void
2895ncr53c9x_timeout(void *arg) 2892ncr53c9x_timeout(void *arg)
2896{ 2893{
2897 struct ncr53c9x_ecb *ecb = arg; 2894 struct ncr53c9x_ecb *ecb = arg;
2898 struct scsipi_xfer *xs = ecb->xs; 2895 struct scsipi_xfer *xs = ecb->xs;
2899 struct scsipi_periph *periph = xs->xs_periph; 2896 struct scsipi_periph *periph = xs->xs_periph;
2900 struct ncr53c9x_softc *sc; 2897 struct ncr53c9x_softc *sc;
2901 struct ncr53c9x_tinfo *ti; 2898 struct ncr53c9x_tinfo *ti;
2902 int s; 
2903 2899
2904 sc = device_private(periph->periph_channel->chan_adapter->adapt_dev); 2900 sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
2905 ti = &sc->sc_tinfo[periph->periph_target]; 2901 ti = &sc->sc_tinfo[periph->periph_target];
2906 2902
2907 scsipi_printaddr(periph); 2903 scsipi_printaddr(periph);
2908 printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], " 2904 printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], "
2909 "<state %d, nexus %p, phase(l %x, c %x, p %x), resid %lx, " 2905 "<state %d, nexus %p, phase(l %x, c %x, p %x), resid %lx, "
2910 "msg(q %x,o %x) %s>", 2906 "msg(q %x,o %x) %s>",
2911 device_xname(sc->sc_dev), 2907 device_xname(sc->sc_dev),
2912 ecb, ecb->flags, ecb->dleft, ecb->stat, 2908 ecb, ecb->flags, ecb->dleft, ecb->stat,
2913 sc->sc_state, sc->sc_nexus, 2909 sc->sc_state, sc->sc_nexus,
2914 NCR_READ_REG(sc, NCR_STAT), 2910 NCR_READ_REG(sc, NCR_STAT),
2915 sc->sc_phase, sc->sc_prevphase, 2911 sc->sc_phase, sc->sc_prevphase,
2916 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout, 2912 (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
2917 NCRDMA_ISACTIVE(sc) ? "DMA active" : ""); 2913 NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
2918#if NCR53C9X_DEBUG > 1 2914#if NCR53C9X_DEBUG > 1
2919 printf("TRACE: %s.", ecb->trace); 2915 printf("TRACE: %s.", ecb->trace);
2920#endif 2916#endif
2921 2917
2922 s = splbio(); 2918 mutex_enter(&sc->sc_lock);
2923 simple_lock(&sc->sc_lock); 
2924 2919
2925 if (ecb->flags & ECB_ABORT) { 2920 if (ecb->flags & ECB_ABORT) {
2926 /* abort timed out */ 2921 /* abort timed out */
2927 printf(" AGAIN\n"); 2922 printf(" AGAIN\n");
2928 2923
2929 ncr53c9x_init(sc, 1); 2924 ncr53c9x_init(sc, 1);
2930 } else { 2925 } else {
2931 /* abort the operation that has timed out */ 2926 /* abort the operation that has timed out */
2932 printf("\n"); 2927 printf("\n");
2933 xs->error = XS_TIMEOUT; 2928 xs->error = XS_TIMEOUT;
2934 ncr53c9x_abort(sc, ecb); 2929 ncr53c9x_abort(sc, ecb);
2935 2930
2936 /* Disable sync mode if stuck in a data phase */ 2931 /* Disable sync mode if stuck in a data phase */
2937 if (ecb == sc->sc_nexus && 2932 if (ecb == sc->sc_nexus &&
2938 (ti->flags & T_SYNCMODE) != 0 && 2933 (ti->flags & T_SYNCMODE) != 0 &&
2939 (sc->sc_phase & (MSGI | CDI)) == 0) { 2934 (sc->sc_phase & (MSGI | CDI)) == 0) {
2940 /* XXX ASYNC CALLBACK! */ 2935 /* XXX ASYNC CALLBACK! */
2941 scsipi_printaddr(periph); 2936 scsipi_printaddr(periph);
2942 printf("sync negotiation disabled\n"); 2937 printf("sync negotiation disabled\n");
2943 sc->sc_cfflags |= 2938 sc->sc_cfflags |=
2944 (1 << ((periph->periph_target & 7) + 8)); 2939 (1 << ((periph->periph_target & 7) + 8));
2945 ncr53c9x_update_xfer_mode(sc, periph->periph_target); 2940 ncr53c9x_update_xfer_mode(sc, periph->periph_target);
2946 } 2941 }
2947 } 2942 }
2948 2943
2949 simple_unlock(&sc->sc_lock); 2944 mutex_exit(&sc->sc_lock);
2950 splx(s); 
2951} 2945}
2952 2946
2953void 2947void
2954ncr53c9x_watch(void *arg) 2948ncr53c9x_watch(void *arg)
2955{ 2949{
2956 struct ncr53c9x_softc *sc = arg; 2950 struct ncr53c9x_softc *sc = arg;
2957 struct ncr53c9x_tinfo *ti; 2951 struct ncr53c9x_tinfo *ti;
2958 struct ncr53c9x_linfo *li; 2952 struct ncr53c9x_linfo *li;
2959 int t, s; 2953 int t;
2960 /* Delete any structures that have not been used in 10min. */ 2954 /* Delete any structures that have not been used in 10min. */
2961 time_t old = time_second - (10 * 60); 2955 time_t old = time_second - (10 * 60);
2962 2956
2963 s = splbio(); 2957 mutex_enter(&sc->sc_lock);
2964 simple_lock(&sc->sc_lock); 
2965 for (t = 0; t < sc->sc_ntarg; t++) { 2958 for (t = 0; t < sc->sc_ntarg; t++) {
2966 ti = &sc->sc_tinfo[t]; 2959 ti = &sc->sc_tinfo[t];
2967 li = LIST_FIRST(&ti->luns); 2960 li = LIST_FIRST(&ti->luns);
2968 while (li) { 2961 while (li) {
2969 if (li->last_used < old && 2962 if (li->last_used < old &&
2970 li->untagged == NULL && 2963 li->untagged == NULL &&
2971 li->used == 0) { 2964 li->used == 0) {
2972 if (li->lun < NCR_NLUN) 2965 if (li->lun < NCR_NLUN)
2973 ti->lun[li->lun] = NULL; 2966 ti->lun[li->lun] = NULL;
2974 LIST_REMOVE(li, link); 2967 LIST_REMOVE(li, link);
2975 free(li, M_DEVBUF); 2968 free(li, M_DEVBUF);
2976 /* Restart the search at the beginning */ 2969 /* Restart the search at the beginning */
2977 li = LIST_FIRST(&ti->luns); 2970 li = LIST_FIRST(&ti->luns);
2978 continue; 2971 continue;
2979 } 2972 }
2980 li = LIST_NEXT(li, link); 2973 li = LIST_NEXT(li, link);
2981 } 2974 }
2982 } 2975 }
2983 simple_unlock(&sc->sc_lock); 2976 mutex_exit(&sc->sc_lock);
2984 splx(s); 
2985 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc); 2977 callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc);
2986} 2978}

cvs diff -r1.54 -r1.55 src/sys/dev/ic/ncr53c9xvar.h (switch to unified diff)

--- src/sys/dev/ic/ncr53c9xvar.h 2009/09/07 13:31:44 1.54
+++ src/sys/dev/ic/ncr53c9xvar.h 2011/07/31 18:39:00 1.55
@@ -1,448 +1,448 @@ @@ -1,448 +1,448 @@
1/* $NetBSD: ncr53c9xvar.h,v 1.54 2009/09/07 13:31:44 tsutsui Exp $ */ 1/* $NetBSD: ncr53c9xvar.h,v 1.55 2011/07/31 18:39:00 jakllsch Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc. 4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center. 9 * NASA Ames Research Center.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * Copyright (c) 1994 Peter Galbavy. All rights reserved. 34 * Copyright (c) 1994 Peter Galbavy. All rights reserved.
35 * 35 *
36 * Redistribution and use in source and binary forms, with or without 36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions 37 * modification, are permitted provided that the following conditions
38 * are met: 38 * are met:
39 * 1. Redistributions of source code must retain the above copyright 39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer. 40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright 41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the 42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution. 43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software 44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement: 45 * must display the following acknowledgement:
46 * This product includes software developed by Peter Galbavy. 46 * This product includes software developed by Peter Galbavy.
47 * 4. The name of the author may not be used to endorse or promote products 47 * 4. The name of the author may not be used to endorse or promote products
48 * derived from this software without specific prior written permission. 48 * derived from this software without specific prior written permission.
49 * 49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 */ 60 */
61 61
62#ifndef _DEV_IC_NCR53C9XVAR_H_ 62#ifndef _DEV_IC_NCR53C9XVAR_H_
63#define _DEV_IC_NCR53C9XVAR_H_ 63#define _DEV_IC_NCR53C9XVAR_H_
64 64
65#include <sys/simplelock.h> 65#include <sys/mutex.h>
66 66
67/* Set this to 1 for normal debug, or 2 for per-target tracing. */ 67/* Set this to 1 for normal debug, or 2 for per-target tracing. */
68/* #define NCR53C9X_DEBUG 1 */ 68/* #define NCR53C9X_DEBUG 1 */
69 69
70/* Wide or differential can have 16 targets */ 70/* Wide or differential can have 16 targets */
71#define NCR_NLUN 8 71#define NCR_NLUN 8
72 72
73#define NCR_ABORT_TIMEOUT 2000 /* time to wait for abort */ 73#define NCR_ABORT_TIMEOUT 2000 /* time to wait for abort */
74#define NCR_SENSE_TIMEOUT 1000 /* time to wait for sense */ 74#define NCR_SENSE_TIMEOUT 1000 /* time to wait for sense */
75 75
76#define FREQTOCCF(freq) (((freq + 4) / 5)) 76#define FREQTOCCF(freq) (((freq + 4) / 5))
77 77
78/* 78/*
79 * NCR 53c9x variants. Note, these values are used as indexes into 79 * NCR 53c9x variants. Note, these values are used as indexes into
80 * a table; don't modify them unless you know what you're doing. 80 * a table; don't modify them unless you know what you're doing.
81 */ 81 */
82#define NCR_VARIANT_ESP100 0 82#define NCR_VARIANT_ESP100 0
83#define NCR_VARIANT_ESP100A 1 83#define NCR_VARIANT_ESP100A 1
84#define NCR_VARIANT_ESP200 2 84#define NCR_VARIANT_ESP200 2
85#define NCR_VARIANT_NCR53C94 3 85#define NCR_VARIANT_NCR53C94 3
86#define NCR_VARIANT_NCR53C96 4 86#define NCR_VARIANT_NCR53C96 4
87#define NCR_VARIANT_ESP406 5 87#define NCR_VARIANT_ESP406 5
88#define NCR_VARIANT_FAS408 6 88#define NCR_VARIANT_FAS408 6
89#define NCR_VARIANT_FAS216 7 89#define NCR_VARIANT_FAS216 7
90#define NCR_VARIANT_AM53C974 8 90#define NCR_VARIANT_AM53C974 8
91#define NCR_VARIANT_FAS366 9 91#define NCR_VARIANT_FAS366 9
92#define NCR_VARIANT_NCR53C90_86C01 10 92#define NCR_VARIANT_NCR53C90_86C01 10
93#define NCR_VARIANT_MAX 11 93#define NCR_VARIANT_MAX 11
94 94
95/* 95/*
96 * ECB. Holds additional information for each SCSI command Comments: We 96 * ECB. Holds additional information for each SCSI command Comments: We
97 * need a separate scsi command block because we may need to overwrite it 97 * need a separate scsi command block because we may need to overwrite it
98 * with a request sense command. Basicly, we refrain from fiddling with 98 * with a request sense command. Basicly, we refrain from fiddling with
99 * the scsipi_xfer struct (except do the expected updating of return values). 99 * the scsipi_xfer struct (except do the expected updating of return values).
100 * We'll generally update: xs->{flags,resid,error,sense,status} and 100 * We'll generally update: xs->{flags,resid,error,sense,status} and
101 * occasionally xs->retries. 101 * occasionally xs->retries.
102 */ 102 */
103struct ncr53c9x_ecb { 103struct ncr53c9x_ecb {
104 TAILQ_ENTRY(ncr53c9x_ecb) chain; 104 TAILQ_ENTRY(ncr53c9x_ecb) chain;
105 struct scsipi_xfer *xs; /* SCSI xfer ctrl block from above */ 105 struct scsipi_xfer *xs; /* SCSI xfer ctrl block from above */
106 int flags; 106 int flags;
107#define ECB_ALLOC 0x01 107#define ECB_ALLOC 0x01
108#define ECB_READY 0x02 108#define ECB_READY 0x02
109#define ECB_SENSE 0x04 109#define ECB_SENSE 0x04
110#define ECB_ABORT 0x40 110#define ECB_ABORT 0x40
111#define ECB_RESET 0x80 111#define ECB_RESET 0x80
112#define ECB_TENTATIVE_DONE 0x100 112#define ECB_TENTATIVE_DONE 0x100
113 int timeout; 113 int timeout;
114 114
115 struct { 115 struct {
116 u_char msg[3]; /* Selection Id msg and tags */ 116 u_char msg[3]; /* Selection Id msg and tags */
117 struct scsipi_generic cmd; /* SCSI command block */ 117 struct scsipi_generic cmd; /* SCSI command block */
118 } cmd; 118 } cmd;
119 uint8_t *daddr; /* Saved data pointer */ 119 uint8_t *daddr; /* Saved data pointer */
120 int clen; /* Size of command in cmd.cmd */ 120 int clen; /* Size of command in cmd.cmd */
121 int dleft; /* Residue */ 121 int dleft; /* Residue */
122 u_char stat; /* SCSI status byte */ 122 u_char stat; /* SCSI status byte */
123 u_char tag[2]; /* TAG bytes */ 123 u_char tag[2]; /* TAG bytes */
124 u_char pad[1]; 124 u_char pad[1];
125 125
126#if NCR53C9X_DEBUG > 1 126#if NCR53C9X_DEBUG > 1
127 char trace[1000]; 127 char trace[1000];
128#endif 128#endif
129}; 129};
130#if NCR53C9X_DEBUG > 1 130#if NCR53C9X_DEBUG > 1
131#define ECB_TRACE(ecb, msg, a, b) do { \ 131#define ECB_TRACE(ecb, msg, a, b) do { \
132 const char *f = "[" msg "]"; \ 132 const char *f = "[" msg "]"; \
133 int n = strlen((ecb)->trace); \ 133 int n = strlen((ecb)->trace); \
134 if (n < (sizeof((ecb)->trace)-100)) \ 134 if (n < (sizeof((ecb)->trace)-100)) \
135 sprintf((ecb)->trace + n, f, a, b); \ 135 sprintf((ecb)->trace + n, f, a, b); \
136} while(0) 136} while(0)
137#else 137#else
138#define ECB_TRACE(ecb, msg, a, b) 138#define ECB_TRACE(ecb, msg, a, b)
139#endif 139#endif
140 140
141/* 141/*
142 * Some info about each (possible) target and LUN on the SCSI bus. 142 * Some info about each (possible) target and LUN on the SCSI bus.
143 * 143 *
144 * SCSI I and II devices can have up to 8 LUNs, each with up to 256 144 * SCSI I and II devices can have up to 8 LUNs, each with up to 256
145 * outstanding tags. SCSI III devices have 64-bit LUN identifiers 145 * outstanding tags. SCSI III devices have 64-bit LUN identifiers
146 * that can be sparsely allocated. 146 * that can be sparsely allocated.
147 * 147 *
148 * Since SCSI II devices can have up to 8 LUNs, we use an array 148 * Since SCSI II devices can have up to 8 LUNs, we use an array
149 * of 8 pointers to ncr53c9x_linfo structures for fast lookup. 149 * of 8 pointers to ncr53c9x_linfo structures for fast lookup.
150 * Longer LUNs need to traverse the linked list. 150 * Longer LUNs need to traverse the linked list.
151 */ 151 */
152 152
153struct ncr53c9x_linfo { 153struct ncr53c9x_linfo {
154 int64_t lun; 154 int64_t lun;
155 LIST_ENTRY(ncr53c9x_linfo) link; 155 LIST_ENTRY(ncr53c9x_linfo) link;
156 time_t last_used; 156 time_t last_used;
157 uint8_t used; /* # slots in use */ 157 uint8_t used; /* # slots in use */
158 uint8_t avail; /* where to start scanning */ 158 uint8_t avail; /* where to start scanning */
159 uint8_t busy; 159 uint8_t busy;
160 struct ncr53c9x_ecb *untagged; 160 struct ncr53c9x_ecb *untagged;
161 struct ncr53c9x_ecb *queued[256]; 161 struct ncr53c9x_ecb *queued[256];
162}; 162};
163 163
164struct ncr53c9x_tinfo { 164struct ncr53c9x_tinfo {
165 int cmds; /* # of commands processed */ 165 int cmds; /* # of commands processed */
166 int dconns; /* # of disconnects */ 166 int dconns; /* # of disconnects */
167 int touts; /* # of timeouts */ 167 int touts; /* # of timeouts */
168 int perrs; /* # of parity errors */ 168 int perrs; /* # of parity errors */
169 int senses; /* # of request sense commands sent */ 169 int senses; /* # of request sense commands sent */
170 uint8_t flags; 170 uint8_t flags;
171#define T_NEGOTIATE 0x02 /* (Re)Negotiate synchronous options */ 171#define T_NEGOTIATE 0x02 /* (Re)Negotiate synchronous options */
172#define T_SYNCMODE 0x08 /* SYNC mode has been negotiated */ 172#define T_SYNCMODE 0x08 /* SYNC mode has been negotiated */
173#define T_SYNCHOFF 0x10 /* SYNC mode for is permanently off */ 173#define T_SYNCHOFF 0x10 /* SYNC mode for is permanently off */
174#define T_RSELECTOFF 0x20 /* RE-SELECT mode is off */ 174#define T_RSELECTOFF 0x20 /* RE-SELECT mode is off */
175#define T_TAG 0x40 /* Turn on TAG QUEUEs */ 175#define T_TAG 0x40 /* Turn on TAG QUEUEs */
176#define T_WIDE 0x80 /* Negotiate wide options */ 176#define T_WIDE 0x80 /* Negotiate wide options */
177#define T_WDTRSENT 0x04 /* WDTR message has been sent to */ 177#define T_WDTRSENT 0x04 /* WDTR message has been sent to */
178 uint8_t period; /* Period suggestion */ 178 uint8_t period; /* Period suggestion */
179 uint8_t offset; /* Offset suggestion */ 179 uint8_t offset; /* Offset suggestion */
180 uint8_t cfg3; /* per target config 3 */ 180 uint8_t cfg3; /* per target config 3 */
181 uint8_t nextag; /* Next available tag */ 181 uint8_t nextag; /* Next available tag */
182 uint8_t width; /* width suggesion */ 182 uint8_t width; /* width suggesion */
183 LIST_HEAD(lun_list, ncr53c9x_linfo) luns; 183 LIST_HEAD(lun_list, ncr53c9x_linfo) luns;
184 struct ncr53c9x_linfo *lun[NCR_NLUN]; /* For speedy lookups */ 184 struct ncr53c9x_linfo *lun[NCR_NLUN]; /* For speedy lookups */
185}; 185};
186 186
187/* Look up a lun in a tinfo */ 187/* Look up a lun in a tinfo */
188#define TINFO_LUN(t, l) ( \ 188#define TINFO_LUN(t, l) ( \
189 (((l) < NCR_NLUN) && (((t)->lun[(l)]) != NULL)) \ 189 (((l) < NCR_NLUN) && (((t)->lun[(l)]) != NULL)) \
190 ? ((t)->lun[(l)]) \ 190 ? ((t)->lun[(l)]) \
191 : ncr53c9x_lunsearch((t), (int64_t)(l)) \ 191 : ncr53c9x_lunsearch((t), (int64_t)(l)) \
192) 192)
193 193
194/* Register a linenumber (for debugging) */ 194/* Register a linenumber (for debugging) */
195#define LOGLINE(p) 195#define LOGLINE(p)
196 196
197#define NCR_SHOWECBS 0x01 197#define NCR_SHOWECBS 0x01
198#define NCR_SHOWINTS 0x02 198#define NCR_SHOWINTS 0x02
199#define NCR_SHOWCMDS 0x04 199#define NCR_SHOWCMDS 0x04
200#define NCR_SHOWMISC 0x08 200#define NCR_SHOWMISC 0x08
201#define NCR_SHOWTRAC 0x10 201#define NCR_SHOWTRAC 0x10
202#define NCR_SHOWSTART 0x20 202#define NCR_SHOWSTART 0x20
203#define NCR_SHOWPHASE 0x40 203#define NCR_SHOWPHASE 0x40
204#define NCR_SHOWDMA 0x80 204#define NCR_SHOWDMA 0x80
205#define NCR_SHOWCCMDS 0x100 205#define NCR_SHOWCCMDS 0x100
206#define NCR_SHOWMSGS 0x200 206#define NCR_SHOWMSGS 0x200
207 207
208#ifdef NCR53C9X_DEBUG 208#ifdef NCR53C9X_DEBUG
209extern int ncr53c9x_debug; 209extern int ncr53c9x_debug;
210#define NCR_ECBS(str) \ 210#define NCR_ECBS(str) \
211 do {if (ncr53c9x_debug & NCR_SHOWECBS) printf str;} while (0) 211 do {if (ncr53c9x_debug & NCR_SHOWECBS) printf str;} while (0)
212#define NCR_MISC(str) \ 212#define NCR_MISC(str) \
213 do {if (ncr53c9x_debug & NCR_SHOWMISC) printf str;} while (0) 213 do {if (ncr53c9x_debug & NCR_SHOWMISC) printf str;} while (0)
214#define NCR_INTS(str) \ 214#define NCR_INTS(str) \
215 do {if (ncr53c9x_debug & NCR_SHOWINTS) printf str;} while (0) 215 do {if (ncr53c9x_debug & NCR_SHOWINTS) printf str;} while (0)
216#define NCR_TRACE(str) \ 216#define NCR_TRACE(str) \
217 do {if (ncr53c9x_debug & NCR_SHOWTRAC) printf str;} while (0) 217 do {if (ncr53c9x_debug & NCR_SHOWTRAC) printf str;} while (0)
218#define NCR_CMDS(str) \ 218#define NCR_CMDS(str) \
219 do {if (ncr53c9x_debug & NCR_SHOWCMDS) printf str;} while (0) 219 do {if (ncr53c9x_debug & NCR_SHOWCMDS) printf str;} while (0)
220#define NCR_START(str) \ 220#define NCR_START(str) \
221 do {if (ncr53c9x_debug & NCR_SHOWSTART) printf str;}while (0) 221 do {if (ncr53c9x_debug & NCR_SHOWSTART) printf str;}while (0)
222#define NCR_PHASE(str) \ 222#define NCR_PHASE(str) \
223 do {if (ncr53c9x_debug & NCR_SHOWPHASE) printf str;}while (0) 223 do {if (ncr53c9x_debug & NCR_SHOWPHASE) printf str;}while (0)
224#define NCR_DMA(str) \ 224#define NCR_DMA(str) \
225 do {if (ncr53c9x_debug & NCR_SHOWDMA) printf str;}while (0) 225 do {if (ncr53c9x_debug & NCR_SHOWDMA) printf str;}while (0)
226#define NCR_MSGS(str) \ 226#define NCR_MSGS(str) \
227 do {if (ncr53c9x_debug & NCR_SHOWMSGS) printf str;}while (0) 227 do {if (ncr53c9x_debug & NCR_SHOWMSGS) printf str;}while (0)
228#else 228#else
229#define NCR_ECBS(str) 229#define NCR_ECBS(str)
230#define NCR_MISC(str) 230#define NCR_MISC(str)
231#define NCR_INTS(str) 231#define NCR_INTS(str)
232#define NCR_TRACE(str) 232#define NCR_TRACE(str)
233#define NCR_CMDS(str) 233#define NCR_CMDS(str)
234#define NCR_START(str) 234#define NCR_START(str)
235#define NCR_PHASE(str) 235#define NCR_PHASE(str)
236#define NCR_DMA(str) 236#define NCR_DMA(str)
237#define NCR_MSGS(str) 237#define NCR_MSGS(str)
238#endif 238#endif
239 239
240#define NCR_MAX_MSG_LEN 8 240#define NCR_MAX_MSG_LEN 8
241 241
242struct ncr53c9x_softc; 242struct ncr53c9x_softc;
243 243
244/* 244/*
245 * Function switch used as glue to MD code. 245 * Function switch used as glue to MD code.
246 */ 246 */
247struct ncr53c9x_glue { 247struct ncr53c9x_glue {
248 /* Mandatory entry points. */ 248 /* Mandatory entry points. */
249 uint8_t (*gl_read_reg)(struct ncr53c9x_softc *, int); 249 uint8_t (*gl_read_reg)(struct ncr53c9x_softc *, int);
250 void (*gl_write_reg)(struct ncr53c9x_softc *, int, uint8_t); 250 void (*gl_write_reg)(struct ncr53c9x_softc *, int, uint8_t);
251 int (*gl_dma_isintr)(struct ncr53c9x_softc *); 251 int (*gl_dma_isintr)(struct ncr53c9x_softc *);
252 void (*gl_dma_reset)(struct ncr53c9x_softc *); 252 void (*gl_dma_reset)(struct ncr53c9x_softc *);
253 int (*gl_dma_intr)(struct ncr53c9x_softc *); 253 int (*gl_dma_intr)(struct ncr53c9x_softc *);
254 int (*gl_dma_setup)(struct ncr53c9x_softc *, 254 int (*gl_dma_setup)(struct ncr53c9x_softc *,
255 uint8_t **, size_t *, int, size_t *); 255 uint8_t **, size_t *, int, size_t *);
256 void (*gl_dma_go)(struct ncr53c9x_softc *); 256 void (*gl_dma_go)(struct ncr53c9x_softc *);
257 void (*gl_dma_stop)(struct ncr53c9x_softc *); 257 void (*gl_dma_stop)(struct ncr53c9x_softc *);
258 int (*gl_dma_isactive)(struct ncr53c9x_softc *); 258 int (*gl_dma_isactive)(struct ncr53c9x_softc *);
259 259
260 /* Optional entry points. */ 260 /* Optional entry points. */
261 void (*gl_clear_latched_intr)(struct ncr53c9x_softc *); 261 void (*gl_clear_latched_intr)(struct ncr53c9x_softc *);
262}; 262};
263 263
264struct ncr53c9x_softc { 264struct ncr53c9x_softc {
265 device_t sc_dev; /* us as a device */ 265 device_t sc_dev; /* us as a device */
266 266
267 struct evcnt sc_intrcnt; /* intr count */ 267 struct evcnt sc_intrcnt; /* intr count */
268 struct scsipi_adapter sc_adapter; /* out scsipi adapter */ 268 struct scsipi_adapter sc_adapter; /* out scsipi adapter */
269 struct scsipi_channel sc_channel; /* our scsipi channel */ 269 struct scsipi_channel sc_channel; /* our scsipi channel */
270 device_t sc_child; /* attached scsibus, if any */ 270 device_t sc_child; /* attached scsibus, if any */
271 struct callout sc_watchdog; /* periodic timer */ 271 struct callout sc_watchdog; /* periodic timer */
272 272
273 const struct ncr53c9x_glue *sc_glue; /* glue to MD code */ 273 const struct ncr53c9x_glue *sc_glue; /* glue to MD code */
274 274
275 int sc_cfflags; /* Copy of config flags */ 275 int sc_cfflags; /* Copy of config flags */
276 276
277 /* register defaults */ 277 /* register defaults */
278 uint8_t sc_cfg1; /* Config 1 */ 278 uint8_t sc_cfg1; /* Config 1 */
279 uint8_t sc_cfg2; /* Config 2, not ESP100 */ 279 uint8_t sc_cfg2; /* Config 2, not ESP100 */
280 uint8_t sc_cfg3; /* Config 3, ESP200,FAS */ 280 uint8_t sc_cfg3; /* Config 3, ESP200,FAS */
281 uint8_t sc_cfg3_fscsi; /* Chip-specific FSCSI bit */ 281 uint8_t sc_cfg3_fscsi; /* Chip-specific FSCSI bit */
282 uint8_t sc_cfg4; /* Config 4, only ESP200 */ 282 uint8_t sc_cfg4; /* Config 4, only ESP200 */
283 uint8_t sc_cfg5; /* Config 5, only ESP200 */ 283 uint8_t sc_cfg5; /* Config 5, only ESP200 */
284 uint8_t sc_ccf; /* Clock Conversion */ 284 uint8_t sc_ccf; /* Clock Conversion */
285 uint8_t sc_timeout; 285 uint8_t sc_timeout;
286 286
287 /* register copies, see espreadregs() */ 287 /* register copies, see espreadregs() */
288 uint8_t sc_espintr; 288 uint8_t sc_espintr;
289 uint8_t sc_espstat; 289 uint8_t sc_espstat;
290 uint8_t sc_espstep; 290 uint8_t sc_espstep;
291 uint8_t sc_espstat2; 291 uint8_t sc_espstat2;
292 uint8_t sc_espfflags; 292 uint8_t sc_espfflags;
293 293
294 /* Lists of command blocks */ 294 /* Lists of command blocks */
295 TAILQ_HEAD(ecb_list, ncr53c9x_ecb) 295 TAILQ_HEAD(ecb_list, ncr53c9x_ecb)
296 ready_list; 296 ready_list;
297 297
298 struct ncr53c9x_ecb *sc_nexus; /* Current command */ 298 struct ncr53c9x_ecb *sc_nexus; /* Current command */
299 int sc_ntarg; 299 int sc_ntarg;
300 struct ncr53c9x_tinfo *sc_tinfo; 300 struct ncr53c9x_tinfo *sc_tinfo;
301 301
302 /* Data about the current nexus (updated for every cmd switch) */ 302 /* Data about the current nexus (updated for every cmd switch) */
303 uint8_t *sc_dp; /* Current data pointer */ 303 uint8_t *sc_dp; /* Current data pointer */
304 ssize_t sc_dleft; /* Data left to transfer */ 304 ssize_t sc_dleft; /* Data left to transfer */
305 305
306 /* Adapter state */ 306 /* Adapter state */
307 int sc_phase; /* Copy of what bus phase we are in */ 307 int sc_phase; /* Copy of what bus phase we are in */
308 int sc_prevphase; /* Copy of what bus phase we were in */ 308 int sc_prevphase; /* Copy of what bus phase we were in */
309 uint8_t sc_state; /* State applicable to the adapter */ 309 uint8_t sc_state; /* State applicable to the adapter */
310 uint8_t sc_flags; /* See below */ 310 uint8_t sc_flags; /* See below */
311 uint8_t sc_selid; 311 uint8_t sc_selid;
312 uint8_t sc_lastcmd; 312 uint8_t sc_lastcmd;
313 313
314 /* Message stuff */ 314 /* Message stuff */
315 uint16_t sc_msgify; /* IDENTIFY msg associated with this nexus */ 315 uint16_t sc_msgify; /* IDENTIFY msg associated with this nexus */
316 uint16_t sc_msgout; /* What message is on its way out? */ 316 uint16_t sc_msgout; /* What message is on its way out? */
317 uint16_t sc_msgpriq; /* One or more messages to send (encoded) */ 317 uint16_t sc_msgpriq; /* One or more messages to send (encoded) */
318 uint16_t sc_msgoutq; /* What messages have been sent so far? */ 318 uint16_t sc_msgoutq; /* What messages have been sent so far? */
319 319
320 uint8_t *sc_omess; /* MSGOUT buffer */ 320 uint8_t *sc_omess; /* MSGOUT buffer */
321 uint8_t *sc_omp; /* Message pointer (for multibyte messages) */ 321 uint8_t *sc_omp; /* Message pointer (for multibyte messages) */
322 size_t sc_omlen; 322 size_t sc_omlen;
323 uint8_t *sc_imess; /* MSGIN buffer */ 323 uint8_t *sc_imess; /* MSGIN buffer */
324 uint8_t *sc_imp; /* Message pointer (for multibyte messages) */ 324 uint8_t *sc_imp; /* Message pointer (for multibyte messages) */
325 size_t sc_imlen; 325 size_t sc_imlen;
326 326
327 uint8_t *sc_cmdp; /* Command pointer (for DMAed commands) */ 327 uint8_t *sc_cmdp; /* Command pointer (for DMAed commands) */
328 size_t sc_cmdlen; /* Size of command in transit */ 328 size_t sc_cmdlen; /* Size of command in transit */
329 329
330 /* Hardware attributes */ 330 /* Hardware attributes */
331 int sc_freq; /* SCSI bus frequency in MHz */ 331 int sc_freq; /* SCSI bus frequency in MHz */
332 int sc_id; /* Our SCSI id */ 332 int sc_id; /* Our SCSI id */
333 int sc_rev; /* Chip revision */ 333 int sc_rev; /* Chip revision */
334 int sc_features; /* Chip features */ 334 int sc_features; /* Chip features */
335 int sc_minsync; /* Minimum sync period / 4 */ 335 int sc_minsync; /* Minimum sync period / 4 */
336 int sc_maxxfer; /* Maximum transfer size */ 336 int sc_maxxfer; /* Maximum transfer size */
337 337
338 struct simplelock sc_lock;/* driver mutex */ 338 kmutex_t sc_lock; /* driver mutex */
339}; 339};
340 340
341/* values for sc_state */ 341/* values for sc_state */
342#define NCR_IDLE 1 /* waiting for something to do */ 342#define NCR_IDLE 1 /* waiting for something to do */
343#define NCR_SELECTING 2 /* SCSI command is arbiting */ 343#define NCR_SELECTING 2 /* SCSI command is arbiting */
344#define NCR_RESELECTED 3 /* Has been reselected */ 344#define NCR_RESELECTED 3 /* Has been reselected */
345#define NCR_IDENTIFIED 4 /* Has gotten IFY but not TAG */ 345#define NCR_IDENTIFIED 4 /* Has gotten IFY but not TAG */
346#define NCR_CONNECTED 5 /* Actively using the SCSI bus */ 346#define NCR_CONNECTED 5 /* Actively using the SCSI bus */
347#define NCR_DISCONNECT 6 /* MSG_DISCONNECT received */ 347#define NCR_DISCONNECT 6 /* MSG_DISCONNECT received */
348#define NCR_CMDCOMPLETE 7 /* MSG_CMDCOMPLETE received */ 348#define NCR_CMDCOMPLETE 7 /* MSG_CMDCOMPLETE received */
349#define NCR_CLEANING 8 349#define NCR_CLEANING 8
350#define NCR_SBR 9 /* Expect a SCSI RST because we commanded it */ 350#define NCR_SBR 9 /* Expect a SCSI RST because we commanded it */
351 351
352/* values for sc_flags */ 352/* values for sc_flags */
353#define NCR_DROP_MSGI 0x01 /* Discard all msgs (parity err detected) */ 353#define NCR_DROP_MSGI 0x01 /* Discard all msgs (parity err detected) */
354#define NCR_ABORTING 0x02 /* Bailing out */ 354#define NCR_ABORTING 0x02 /* Bailing out */
355#define NCR_DOINGDMA 0x04 /* The FIFO data path is active! */ 355#define NCR_DOINGDMA 0x04 /* The FIFO data path is active! */
356#define NCR_SYNCHNEGO 0x08 /* Synch negotiation in progress. */ 356#define NCR_SYNCHNEGO 0x08 /* Synch negotiation in progress. */
357#define NCR_ICCS 0x10 /* Expect status phase results */ 357#define NCR_ICCS 0x10 /* Expect status phase results */
358#define NCR_WAITI 0x20 /* Waiting for non-DMA data to arrive */ 358#define NCR_WAITI 0x20 /* Waiting for non-DMA data to arrive */
359#define NCR_ATN 0x40 /* ATN asserted */ 359#define NCR_ATN 0x40 /* ATN asserted */
360#define NCR_EXPECT_ILLCMD 0x80 /* Expect Illegal Command Interrupt */ 360#define NCR_EXPECT_ILLCMD 0x80 /* Expect Illegal Command Interrupt */
361 361
362/* values for sc_features */ 362/* values for sc_features */
363#define NCR_F_HASCFG3 0x01 /* chip has CFG3 register */ 363#define NCR_F_HASCFG3 0x01 /* chip has CFG3 register */
364#define NCR_F_FASTSCSI 0x02 /* chip supports Fast mode */ 364#define NCR_F_FASTSCSI 0x02 /* chip supports Fast mode */
365#define NCR_F_DMASELECT 0x04 /* can do dmaselect */ 365#define NCR_F_DMASELECT 0x04 /* can do dmaselect */
366#define NCR_F_SELATN3 0x08 /* chip supports SELATN3 command */ 366#define NCR_F_SELATN3 0x08 /* chip supports SELATN3 command */
367 367
368/* values for sc_msgout */ 368/* values for sc_msgout */
369#define SEND_DEV_RESET 0x0001 369#define SEND_DEV_RESET 0x0001
370#define SEND_PARITY_ERROR 0x0002 370#define SEND_PARITY_ERROR 0x0002
371#define SEND_INIT_DET_ERR 0x0004 371#define SEND_INIT_DET_ERR 0x0004
372#define SEND_REJECT 0x0008 372#define SEND_REJECT 0x0008
373#define SEND_IDENTIFY 0x0010 373#define SEND_IDENTIFY 0x0010
374#define SEND_ABORT 0x0020 374#define SEND_ABORT 0x0020
375#define SEND_WDTR 0x0040 375#define SEND_WDTR 0x0040
376#define SEND_SDTR 0x0080 376#define SEND_SDTR 0x0080
377#define SEND_TAG 0x0100 377#define SEND_TAG 0x0100
378 378
379/* SCSI Status codes */ 379/* SCSI Status codes */
380#define ST_MASK 0x3e /* bit 0,6,7 is reserved */ 380#define ST_MASK 0x3e /* bit 0,6,7 is reserved */
381 381
382/* phase bits */ 382/* phase bits */
383#define IOI 0x01 383#define IOI 0x01
384#define CDI 0x02 384#define CDI 0x02
385#define MSGI 0x04 385#define MSGI 0x04
386 386
387/* Information transfer phases */ 387/* Information transfer phases */
388#define DATA_OUT_PHASE (0) 388#define DATA_OUT_PHASE (0)
389#define DATA_IN_PHASE (IOI) 389#define DATA_IN_PHASE (IOI)
390#define COMMAND_PHASE (CDI) 390#define COMMAND_PHASE (CDI)
391#define STATUS_PHASE (CDI|IOI) 391#define STATUS_PHASE (CDI|IOI)
392#define MESSAGE_OUT_PHASE (MSGI|CDI) 392#define MESSAGE_OUT_PHASE (MSGI|CDI)
393#define MESSAGE_IN_PHASE (MSGI|CDI|IOI) 393#define MESSAGE_IN_PHASE (MSGI|CDI|IOI)
394 394
395#define PHASE_MASK (MSGI|CDI|IOI) 395#define PHASE_MASK (MSGI|CDI|IOI)
396 396
397/* Some pseudo phases for getphase()*/ 397/* Some pseudo phases for getphase()*/
398#define BUSFREE_PHASE 0x100 /* Re/Selection no longer valid */ 398#define BUSFREE_PHASE 0x100 /* Re/Selection no longer valid */
399#define INVALID_PHASE 0x101 /* Re/Selection valid, but no REQ yet */ 399#define INVALID_PHASE 0x101 /* Re/Selection valid, but no REQ yet */
400#define PSEUDO_PHASE 0x100 /* "pseudo" bit */ 400#define PSEUDO_PHASE 0x100 /* "pseudo" bit */
401 401
402/* 402/*
403 * Macros to read and write the chip's registers. 403 * Macros to read and write the chip's registers.
404 */ 404 */
405#define NCR_READ_REG(sc, reg) \ 405#define NCR_READ_REG(sc, reg) \
406 (*(sc)->sc_glue->gl_read_reg)((sc), (reg)) 406 (*(sc)->sc_glue->gl_read_reg)((sc), (reg))
407#define NCR_WRITE_REG(sc, reg, val) \ 407#define NCR_WRITE_REG(sc, reg, val) \
408 (*(sc)->sc_glue->gl_write_reg)((sc), (reg), (val)) 408 (*(sc)->sc_glue->gl_write_reg)((sc), (reg), (val))
409 409
410#ifdef NCR53C9X_DEBUG 410#ifdef NCR53C9X_DEBUG
411#define NCRCMD(sc, cmd) do { \ 411#define NCRCMD(sc, cmd) do { \
412 if ((ncr53c9x_debug & NCR_SHOWCCMDS) != 0) \ 412 if ((ncr53c9x_debug & NCR_SHOWCCMDS) != 0) \
413 printf("<CMD:0x%x %d>", (unsigned int)cmd, __LINE__); \ 413 printf("<CMD:0x%x %d>", (unsigned int)cmd, __LINE__); \
414 sc->sc_lastcmd = cmd; \ 414 sc->sc_lastcmd = cmd; \
415 NCR_WRITE_REG(sc, NCR_CMD, cmd); \ 415 NCR_WRITE_REG(sc, NCR_CMD, cmd); \
416} while (/* CONSTCOND */ 0) 416} while (/* CONSTCOND */ 0)
417#else 417#else
418#define NCRCMD(sc, cmd) NCR_WRITE_REG(sc, NCR_CMD, cmd) 418#define NCRCMD(sc, cmd) NCR_WRITE_REG(sc, NCR_CMD, cmd)
419#endif 419#endif
420 420
421/* 421/*
422 * DMA macros for NCR53c9x 422 * DMA macros for NCR53c9x
423 */ 423 */
424#define NCRDMA_ISINTR(sc) (*(sc)->sc_glue->gl_dma_isintr)((sc)) 424#define NCRDMA_ISINTR(sc) (*(sc)->sc_glue->gl_dma_isintr)((sc))
425#define NCRDMA_RESET(sc) (*(sc)->sc_glue->gl_dma_reset)((sc)) 425#define NCRDMA_RESET(sc) (*(sc)->sc_glue->gl_dma_reset)((sc))
426#define NCRDMA_INTR(sc) (*(sc)->sc_glue->gl_dma_intr)((sc)) 426#define NCRDMA_INTR(sc) (*(sc)->sc_glue->gl_dma_intr)((sc))
427#define NCRDMA_SETUP(sc, addr, len, datain, dmasize) \ 427#define NCRDMA_SETUP(sc, addr, len, datain, dmasize) \
428 (*(sc)->sc_glue->gl_dma_setup)((sc), (addr), (len), (datain), (dmasize)) 428 (*(sc)->sc_glue->gl_dma_setup)((sc), (addr), (len), (datain), (dmasize))
429#define NCRDMA_GO(sc) (*(sc)->sc_glue->gl_dma_go)((sc)) 429#define NCRDMA_GO(sc) (*(sc)->sc_glue->gl_dma_go)((sc))
430#define NCRDMA_ISACTIVE(sc) (*(sc)->sc_glue->gl_dma_isactive)((sc)) 430#define NCRDMA_ISACTIVE(sc) (*(sc)->sc_glue->gl_dma_isactive)((sc))
431 431
432/* 432/*
433 * Macro to convert the chip register Clock Per Byte value to 433 * Macro to convert the chip register Clock Per Byte value to
434 * Synchronous Transfer Period. 434 * Synchronous Transfer Period.
435 */ 435 */
436#define ncr53c9x_cpb2stp(sc, cpb) \ 436#define ncr53c9x_cpb2stp(sc, cpb) \
437 ((250 * (cpb)) / (sc)->sc_freq) 437 ((250 * (cpb)) / (sc)->sc_freq)
438 438
439void ncr53c9x_attach(struct ncr53c9x_softc *); 439void ncr53c9x_attach(struct ncr53c9x_softc *);
440int ncr53c9x_detach(struct ncr53c9x_softc *, int); 440int ncr53c9x_detach(struct ncr53c9x_softc *, int);
441void ncr53c9x_scsipi_request(struct scsipi_channel *chan, 441void ncr53c9x_scsipi_request(struct scsipi_channel *chan,
442 scsipi_adapter_req_t req, void *); 442 scsipi_adapter_req_t req, void *);
443void ncr53c9x_reset(struct ncr53c9x_softc *); 443void ncr53c9x_reset(struct ncr53c9x_softc *);
444int ncr53c9x_intr(void *); 444int ncr53c9x_intr(void *);
445void ncr53c9x_init(struct ncr53c9x_softc *, int); 445void ncr53c9x_init(struct ncr53c9x_softc *, int);
446void ncr53c9x_abort(struct ncr53c9x_softc *, struct ncr53c9x_ecb *); 446void ncr53c9x_abort(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
447 447
448#endif /* _DEV_IC_NCR53C9XVAR_H_ */ 448#endif /* _DEV_IC_NCR53C9XVAR_H_ */