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