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