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