spc.c revision 1.5 1 /* $NetBSD: spc.c,v 1.5 1996/08/28 19:00:50 cgd Exp $ */
2
3 #define integrate static inline
4
5 /*
6 * Copyright (c) 1996 Masaru Oki. All rights reserved.
7 * Copyright (c) 1994, 1995, 1996 Charles M. Hannum. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Charles M. Hannum.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * Copyright (c) 1994 Jarle Greipsland
24 * All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. The name of the author may not be used to endorse or promote products
35 * derived from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
38 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
39 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
41 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
46 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 * POSSIBILITY OF SUCH DAMAGE.
48 */
49
50 /*
51 * Acknowledgements: Many of the algorithms used in this driver are
52 * inspired by the work of Julian Elischer (julian (at) tfs.com) and
53 * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu). Thanks a million!
54 */
55
56 /* TODO list:
57 * 1) Get the DMA stuff working.
58 * 2) Get the iov/uio stuff working. Is this a good thing ???
59 * 3) Get the synch stuff working.
60 * 4) Rewrite it to use malloc for the acb structs instead of static alloc.?
61 */
62
63 /*
64 * A few customizable items:
65 */
66
67 /* Use doubleword transfers to/from SCSI chip. Note: This requires
68 * motherboard support. Basicly, some motherboard chipsets are able to
69 * split a 32 bit I/O operation into two 16 bit I/O operations,
70 * transparently to the processor. This speeds up some things, notably long
71 * data transfers.
72 */
73 #define SPC_USE_DWORDS 0
74
75 /* Synchronous data transfers? */
76 #define SPC_USE_SYNCHRONOUS 0
77 #define SPC_SYNC_REQ_ACK_OFS 8
78
79 /* Wide data transfers? */
80 #define SPC_USE_WIDE 0
81 #define SPC_MAX_WIDTH 0
82
83 /* Max attempts made to transmit a message */
84 #define SPC_MSG_MAX_ATTEMPT 3 /* Not used now XXX */
85
86 /* Some spin loop parameters (essentially how long to wait some places)
87 * The problem(?) is that sometimes we expect either to be able to transmit a
88 * byte or to get a new one from the SCSI bus pretty soon. In order to avoid
89 * returning from the interrupt just to get yanked back for the next byte we
90 * may spin in the interrupt routine waiting for this byte to come. How long?
91 * This is really (SCSI) device and processor dependent. Tuneable, I guess.
92 */
93 #define SPC_MSGIN_SPIN 1 /* Will spinwait upto ?ms for a new msg byte */
94 #define SPC_MSGOUT_SPIN 1
95
96 /* Include debug functions? At the end of this file there are a bunch of
97 * functions that will print out various information regarding queued SCSI
98 * commands, driver state and chip contents. You can call them from the
99 * kernel debugger. If you set SPC_DEBUG to 0 they are not included (the
100 * kernel uses less memory) but you lose the debugging facilities.
101 */
102 #define SPC_DEBUG 1
103
104 #define SPC_ABORT_TIMEOUT 2000 /* time to wait for abort */
105
106 /* End of customizable parameters */
107
108 /*
109 * MB89352 SCSI Protocol Controller (SPC) routines.
110 */
111
112 #include <sys/types.h>
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/kernel.h>
116 #include <sys/errno.h>
117 #include <sys/ioctl.h>
118 #include <sys/device.h>
119 #include <sys/buf.h>
120 #include <sys/proc.h>
121 #include <sys/user.h>
122 #include <sys/queue.h>
123
124 #include <scsi/scsi_all.h>
125 #include <scsi/scsi_message.h>
126 #include <scsi/scsiconf.h>
127
128 #include <x68k/x68k/iodevice.h>
129 #include <x68k/dev/mb89352reg.h>
130
131 /*
132 * Definitions, most of them has turned out to be unneccesary, but here they
133 * are anyway.
134 */
135
136 #define IOBASE sc->sc_iobase
137 #define BDID (IOBASE->scsi_bdid)
138 #define SCTL (IOBASE->scsi_sctl)
139 #define SCMD (IOBASE->scsi_scmd)
140 #define TMOD (IOBASE->scsi_tmod)
141 #define INTS (IOBASE->scsi_ints)
142 #define PSNS (IOBASE->scsi_psns)
143 #define SSTS (IOBASE->scsi_ssts)
144 #define SERR (IOBASE->scsi_serr)
145 #define PCTL (IOBASE->scsi_pctl)
146 #define MBC (IOBASE->scsi_mbc)
147 #define DREG (IOBASE->scsi_dreg)
148 #define TEMP (IOBASE->scsi_temp)
149 #define TCH (IOBASE->scsi_tch)
150 #define TCM (IOBASE->scsi_tcm)
151 #define TCL (IOBASE->scsi_tcl)
152 #define EXBF (IOBASE->scsi_exbf)
153
154 /* PSNS */
155 #define REQI 0x80
156 #define ACKI 0x40
157 #define ATNI 0x20
158 #define SELI 0x10
159 #define BSYI 0x08
160 #define MSGI 0x04
161 #define CDI 0x02
162 #define IOI 0x01
163
164 /* Important! The 3 most significant bits of this register, in initiator mode,
165 * represents the "expected" SCSI bus phase and can be used to trigger phase
166 * mismatch and phase change interrupts. But more important: If there is a
167 * phase mismatch the chip will not transfer any data! This is actually a nice
168 * feature as it gives us a bit more control over what is happening when we are
169 * bursting data (in) through the FIFOs and the phase suddenly changes from
170 * DATA IN to STATUS or MESSAGE IN. The transfer will stop and wait for the
171 * proper phase to be set in this register instead of dumping the bits into the
172 * FIFOs.
173 */
174 #if 0
175 #define REQO 0x80
176 #define ACKO 0x40
177 #define ATNO 0x20
178 #define SELO 0x10
179 #define BSYO 0x08
180 #endif
181 /* PCTL */
182 #define MSGO 0x04
183 #define CDO 0x02
184 #define IOO 0x01
185
186 /* Information transfer phases */
187 #define PH_DATAOUT (0)
188 #define PH_DATAIN (IOI)
189 #define PH_CMD (CDI)
190 #define PH_STAT (CDI | IOI)
191 #define PH_MSGOUT (MSGI | CDI)
192 #define PH_MSGIN (MSGI | CDI | IOI)
193
194 #define PH_MASK (MSGI | CDI | IOI)
195
196 #define PH_INVALID 0xff
197
198 /* SCSI selection/reselection ID (both target *and* initiator) */
199 #define SELID7 0x80
200 #define SELID6 0x40
201 #define SELID5 0x20
202 #define SELID4 0x10
203 #define SELID3 0x08
204 #define SELID2 0x04
205 #define SELID1 0x02
206 #define SELID0 0x01
207
208 #ifndef DDB
210 #define Debugger() panic("should call debugger here (spc.c)")
211 #endif /* ! DDB */
212
213 /*
214 * ACB. Holds additional information for each SCSI command Comments: We
215 * need a separate scsi command block because we may need to overwrite it
216 * with a request sense command. Basicly, we refrain from fiddling with
217 * the scsi_xfer struct (except do the expected updating of return values).
218 * We'll generally update: xs->{flags,resid,error,sense,status} and
219 * occasionally xs->retries.
220 */
221 struct spc_acb {
222 struct scsi_generic scsi_cmd;
223 int scsi_cmd_length;
224 u_char *data_addr; /* Saved data pointer */
225 int data_length; /* Residue */
226
227 u_char target_stat; /* SCSI status byte */
228
229 /* struct spc_dma_seg dma[SPC_NSEG];*/ /* Physical addresses+len */
230
231 TAILQ_ENTRY(spc_acb) chain;
232 struct scsi_xfer *xs; /* SCSI xfer ctrl block from above */
233 int flags;
234 #define ACB_ALLOC 0x01
235 #define ACB_NEXUS 0x02
236 #define ACB_SENSE 0x04
237 #define ACB_ABORT 0x40
238 #define ACB_RESET 0x80
239 int timeout;
240 };
241
242 /*
243 * Some info about each (possible) target on the SCSI bus. This should
244 * probably have been a "per target+lunit" structure, but we'll leave it at
245 * this for now.
246 */
247 struct spc_tinfo {
248 int cmds; /* #commands processed */
249 int dconns; /* #disconnects */
250 int touts; /* #timeouts */
251 int perrs; /* #parity errors */
252 int senses; /* #request sense commands sent */
253 ushort lubusy; /* What local units/subr. are busy? */
254 u_char flags;
255 #define DO_SYNC 0x01 /* (Re)Negotiate synchronous options */
256 #define DO_WIDE 0x02 /* (Re)Negotiate wide options */
257 u_char period; /* Period suggestion */
258 u_char offset; /* Offset suggestion */
259 u_char width; /* Width suggestion */
260 } tinfo_t;
261
262 struct spc_softc {
263 struct device sc_dev;
264 volatile struct mb89352 *sc_iobase;
265
266 struct scsi_link sc_link; /* prototype for subdevs */
267
268 TAILQ_HEAD(, spc_acb) free_list, ready_list, nexus_list;
269 struct spc_acb *sc_nexus; /* current command */
270 struct spc_acb sc_acb[8];
271 struct spc_tinfo sc_tinfo[8];
272
273 /* Data about the current nexus (updated for every cmd switch) */
274 u_char *sc_dp; /* Current data pointer */
275 size_t sc_dleft; /* Data bytes left to transfer */
276 u_char *sc_cp; /* Current command pointer */
277 size_t sc_cleft; /* Command bytes left to transfer */
278
279 /* Adapter state */
280 u_char sc_phase; /* Current bus phase */
281 u_char sc_prevphase; /* Previous bus phase */
282 u_char sc_state; /* State applicable to the adapter */
283 #define SPC_INIT 0
284 #define SPC_IDLE 1
285 #define SPC_SELECTING 2 /* SCSI command is arbiting */
286 #define SPC_RESELECTED 3 /* Has been reselected */
287 #define SPC_CONNECTED 4 /* Actively using the SCSI bus */
288 #define SPC_DISCONNECT 5 /* MSG_DISCONNECT received */
289 #define SPC_CMDCOMPLETE 6 /* MSG_CMDCOMPLETE received */
290 #define SPC_CLEANING 7
291 u_char sc_flags;
292 #define SPC_DROP_MSGIN 0x01 /* Discard all msgs (parity err detected) */
293 #define SPC_ABORTING 0x02 /* Bailing out */
294 #define SPC_DOINGDMA 0x04 /* The FIFO data path is active! */
295 u_char sc_selid; /* Reselection ID */
296
297 /* Message stuff */
298 u_char sc_msgpriq; /* Messages we want to send */
299 u_char sc_msgoutq; /* Messages sent during last MESSAGE OUT */
300 u_char sc_lastmsg; /* Message last transmitted */
301 u_char sc_currmsg; /* Message currently ready to transmit */
302 #define SEND_DEV_RESET 0x01
303 #define SEND_PARITY_ERROR 0x02
304 #define SEND_INIT_DET_ERR 0x04
305 #define SEND_REJECT 0x08
306 #define SEND_IDENTIFY 0x10
307 #define SEND_ABORT 0x20
308 #define SEND_SDTR 0x40
309 #define SEND_WDTR 0x80
310 #define SPC_MAX_MSG_LEN 8
311 u_char sc_omess[SPC_MAX_MSG_LEN];
312 u_char *sc_omp; /* Outgoing message pointer */
313 u_char sc_imess[SPC_MAX_MSG_LEN];
314 u_char *sc_imp; /* Incoming message pointer */
315
316 /* Hardware stuff */
317 int sc_initiator; /* Our scsi id */
318 int sc_freq; /* Clock frequency in MHz */
319 int sc_minsync; /* Minimum sync period / 4 */
320 int sc_maxsync; /* Maximum sync period / 4 */
321 };
322
323 #if SPC_DEBUG
324 #define SPC_SHOWACBS 0x01
325 #define SPC_SHOWINTS 0x02
326 #define SPC_SHOWCMDS 0x04
327 #define SPC_SHOWMISC 0x08
328 #define SPC_SHOWTRACE 0x10
329 #define SPC_SHOWSTART 0x20
330 #define SPC_DOBREAK 0x40
331 int spc_debug = 0x00; /* SPC_SHOWSTART|SPC_SHOWMISC|SPC_SHOWTRACE; */
332 #define SPC_PRINT(b, s) do {if ((spc_debug & (b)) != 0) printf s;} while (0)
333 #define SPC_BREAK() do {if ((spc_debug & SPC_DOBREAK) != 0) Debugger();} while (0)
334 #define SPC_ASSERT(x) do {if (x) {} else {printf("%s at line %d: assertion failed\n", sc->sc_dev.dv_xname, __LINE__); Debugger();}} while (0)
335 #else
336 #define SPC_PRINT(b, s)
337 #define SPC_BREAK()
338 #define SPC_ASSERT(x)
339 #endif
340
341 #define SPC_ACBS(s) SPC_PRINT(SPC_SHOWACBS, s)
342 #define SPC_INTS(s) SPC_PRINT(SPC_SHOWINTS, s)
343 #define SPC_CMDS(s) SPC_PRINT(SPC_SHOWCMDS, s)
344 #define SPC_MISC(s) SPC_PRINT(SPC_SHOWMISC, s)
345 #define SPC_TRACE(s) SPC_PRINT(SPC_SHOWTRACE, s)
346 #define SPC_START(s) SPC_PRINT(SPC_SHOWSTART, s)
347
348 int spcmatch __P((struct device *, void *, void *));
349 void spcattach __P((struct device *, struct device *, void *));
350 int spcprint __P((void *, const char *));
351 void spc_minphys __P((struct buf *));
352 int spcintr __P((int));
353 void spc_init __P((struct spc_softc *));
354 void spc_done __P((struct spc_softc *, struct spc_acb *));
355 void spc_dequeue __P((struct spc_softc *, struct spc_acb *));
356 int spc_scsi_cmd __P((struct scsi_xfer *));
357 int spc_poll __P((struct spc_softc *, struct scsi_xfer *, int));
358 integrate void spc_sched_msgout __P((struct spc_softc *, u_char));
359 integrate void spc_setsync __P((struct spc_softc *, struct spc_tinfo *));
360 void spc_select __P((struct spc_softc *, struct spc_acb *));
361 void spc_timeout __P((void *));
362 void spc_sched __P((struct spc_softc *));
363 void spc_scsi_reset __P((struct spc_softc *));
364 void spc_reset __P((struct spc_softc *));
365 #if SPC_DEBUG
366 void spc_print_active_acb();
367 void spc_dump_driver();
368 #endif
369 volatile void * spc_find __P((int));
370
371 struct cfattach spc_ca = {
372 sizeof(struct spc_softc), spcmatch, spcattach
373 };
374
375 struct cfdriver spc_cd = {
376 NULL, "spc", DV_DULL
377 };
378
379 struct scsi_adapter spc_switch = {
380 spc_scsi_cmd,
381 spc_minphys,
382 0,
383 0,
384 };
385
386 struct scsi_device spc_dev = {
387 NULL, /* Use default error handler */
388 NULL, /* have a queue, served by this */
389 NULL, /* have no async handler */
390 NULL, /* Use default 'done' routine */
391 };
392
393 /*
395 * INITIALIZATION ROUTINES (probe, attach ++)
396 */
397
398 /*
399 * returns non-zero value if a controller is found.
400 */
401 int
402 spcmatch(parent, match, aux)
403 struct device *parent;
404 void *match, *aux;
405 {
406 struct cfdata *cf = match;
407
408 if (strcmp(aux, "spc") || spc_find(cf->cf_unit) == 0)
409 return 0;
410 return 1;
411 }
412
413 /*
414 * Find the board
415 */
416 volatile void *
417 spc_find(unit)
418 int unit;
419 {
420 volatile void *addr;
421
422 if (unit > 1)
423 return 0;
424 switch(unit) {
425 case 0: /* builtin */
426 if (badaddr(IODEVbase->inscsirom) ||
427 badbaddr(&IODEVbase->io_inspc.bdid) ||
428 bcmp((void *)&IODEVbase->inscsirom[0x24], "SCSIIN", 6))
429 return 0;
430 addr = &IODEVbase->io_inspc;
431 break;
432 case 1: /* external */
433 if (badaddr(IODEVbase->exscsirom) ||
434 badbaddr(&IODEVbase->io_exspc.bdid) ||
435 bcmp((void *)&IODEVbase->exscsirom[0x24], "SCSIEX", 6))
436 return 0;
437 addr = &IODEVbase->io_exspc;
438 break;
439 }
440
441 if (badaddr(addr))
442 return 0;
443
444 return addr;
445 }
446
447 /*
448 */
449 void
450 spcattach(parent, self, aux)
451 struct device *parent, *self;
452 void *aux;
453 {
454 struct spc_softc *sc = (void *)self;
455
456 SPC_TRACE(("spcattach "));
457 sc->sc_state = SPC_INIT;
458 sc->sc_iobase = spc_find(sc->sc_dev.dv_unit); /* XXX */
459 spc_init(sc); /* Init chip and driver */
460
461 /*
462 * Fill in the prototype scsi_link
463 */
464 sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
465 sc->sc_link.adapter_softc = sc;
466 sc->sc_link.adapter_target = sc->sc_initiator;
467 sc->sc_link.adapter = &spc_switch;
468 sc->sc_link.device = &spc_dev;
469 sc->sc_link.openings = 2;
470
471 printf("\n");
472
473 config_found(self, &sc->sc_link, scsiprint);
474 }
475
476 void
477 spc_reset(sc)
478 struct spc_softc *sc;
479 {
480 sc->sc_initiator = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
481 /*
482 * Disable interrupts then reset the FUJITSU chip.
483 */
484 SCTL = SCTL_DISABLE | SCTL_CTRLRST;
485 SCMD = 0;
486 PCTL = 0;
487 TEMP = 0;
488 TCH = 0;
489 TCM = 0;
490 TCL = 0;
491 INTS = 0;
492 SCTL = SCTL_DISABLE | SCTL_ABRT_ENAB | SCTL_PARITY_ENAB | SCTL_RESEL_ENAB;
493 BDID = sc->sc_initiator;
494 delay(400);
495 SCTL &= ~SCTL_DISABLE;
496 }
497
498 /*
499 * Pull the SCSI RST line for 500us.
500 */
501 void
502 spc_scsi_reset(sc)
503 struct spc_softc *sc;
504 {
505
506 SCMD |= SCMD_RST;
507 delay(500);
508 SCMD &= ~SCMD_RST;
509 delay(50);
510 }
511
512 /*
513 * Initialize spc SCSI driver.
514 */
515 void
516 spc_init(sc)
517 struct spc_softc *sc;
518 {
519 struct spc_acb *acb;
520 int r;
521
522 spc_reset(sc);
523 spc_scsi_reset(sc);
524 spc_reset(sc);
525
526 if (sc->sc_state == SPC_INIT) {
527 /* First time through; initialize. */
528 TAILQ_INIT(&sc->ready_list);
529 TAILQ_INIT(&sc->nexus_list);
530 TAILQ_INIT(&sc->free_list);
531 sc->sc_nexus = NULL;
532 acb = sc->sc_acb;
533 bzero(acb, sizeof(sc->sc_acb));
534 for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
535 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
536 acb++;
537 }
538 bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
539 } else {
540 /* Cancel any active commands. */
541 sc->sc_state = SPC_CLEANING;
542 if ((acb = sc->sc_nexus) != NULL) {
543 acb->xs->error = XS_DRIVER_STUFFUP;
544 untimeout(spc_timeout, acb);
545 spc_done(sc, acb);
546 }
547 while ((acb = sc->nexus_list.tqh_first) != NULL) {
548 acb->xs->error = XS_DRIVER_STUFFUP;
549 untimeout(spc_timeout, acb);
550 spc_done(sc, acb);
551 }
552 }
553
554 sc->sc_prevphase = PH_INVALID;
555 for (r = 0; r < 8; r++) {
556 struct spc_tinfo *ti = &sc->sc_tinfo[r];
557
558 ti->flags = 0;
559 #if SPC_USE_SYNCHRONOUS
560 ti->flags |= DO_SYNC;
561 ti->period = sc->sc_minsync;
562 ti->offset = SPC_SYNC_REQ_ACK_OFS;
563 #else
564 ti->period = ti->offset = 0;
565 #endif
566 #if SPC_USE_WIDE
567 ti->flags |= DO_WIDE;
568 ti->width = SPC_MAX_WIDTH;
569 #else
570 ti->width = 0;
571 #endif
572 }
573
574 sc->sc_state = SPC_IDLE;
575 SCTL |= SCTL_INTR_ENAB;
576 }
577
578 void
579 spc_free_acb(sc, acb, flags)
580 struct spc_softc *sc;
581 struct spc_acb *acb;
582 int flags;
583 {
584 int s;
585
586 s = splbio();
587
588 acb->flags = 0;
589 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
590
591 /*
592 * If there were none, wake anybody waiting for one to come free,
593 * starting with queued entries.
594 */
595 if (acb->chain.tqe_next == 0)
596 wakeup(&sc->free_list);
597
598 splx(s);
599 }
600
601 struct spc_acb *
602 spc_get_acb(sc, flags)
603 struct spc_softc *sc;
604 int flags;
605 {
606 struct spc_acb *acb;
607 int s;
608
609 s = splbio();
610
611 while ((acb = sc->free_list.tqh_first) == NULL &&
612 (flags & SCSI_NOSLEEP) == 0)
613 tsleep(&sc->free_list, PRIBIO, "spcacb", 0);
614 if (acb) {
615 TAILQ_REMOVE(&sc->free_list, acb, chain);
616 acb->flags |= ACB_ALLOC;
617 }
618
619 splx(s);
620 return acb;
621 }
622
623 /*
625 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
626 */
627
628 /*
629 * Expected sequence:
630 * 1) Command inserted into ready list
631 * 2) Command selected for execution
632 * 3) Command won arbitration and has selected target device
633 * 4) Send message out (identify message, eventually also sync.negotiations)
634 * 5) Send command
635 * 5a) Receive disconnect message, disconnect.
636 * 5b) Reselected by target
637 * 5c) Receive identify message from target.
638 * 6) Send or receive data
639 * 7) Receive status
640 * 8) Receive message (command complete etc.)
641 * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
642 * Repeat 2-8 (no disconnects please...)
643 */
644
645 /*
646 * Start a SCSI-command
647 * This function is called by the higher level SCSI-driver to queue/run
648 * SCSI-commands.
649 */
650 int
651 spc_scsi_cmd(xs)
652 struct scsi_xfer *xs;
653 {
654 struct scsi_link *sc_link = xs->sc_link;
655 struct spc_softc *sc = sc_link->adapter_softc;
656 struct spc_acb *acb;
657 int s, flags;
658
659 SPC_TRACE(("spc_scsi_cmd "));
660 SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
661 sc_link->target));
662
663 flags = xs->flags;
664 if ((acb = spc_get_acb(sc, flags)) == NULL) {
665 xs->error = XS_DRIVER_STUFFUP;
666 return TRY_AGAIN_LATER;
667 }
668
669 /* Initialize acb */
670 acb->xs = xs;
671 acb->timeout = xs->timeout;
672
673 if (xs->flags & SCSI_RESET) {
674 acb->flags |= ACB_RESET;
675 acb->scsi_cmd_length = 0;
676 acb->data_length = 0;
677 } else {
678 bcopy(xs->cmd, &acb->scsi_cmd, xs->cmdlen);
679 #if 1
680 acb->scsi_cmd.bytes[0] |= sc_link->lun << 5; /* XXX? */
681 #endif
682 acb->scsi_cmd_length = xs->cmdlen;
683 acb->data_addr = xs->data;
684 acb->data_length = xs->datalen;
685 }
686 acb->target_stat = 0;
687
688 s = splbio();
689
690 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
691 /*
692 * $B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k(B
693 */
694 if (sc->sc_state == SPC_IDLE)
695 spc_sched(sc);
696 /*
697 * $BAw?.$K@.8y$7$?$i!"$9$0$K%j%?!<%s$9$k$+D4$Y$k(B
698 * $B$9$0%j%?!<%s$9$k$J$i(B SUCCESSFULLY_QUEUED $B$rJV$9(B
699 */
700
701 splx(s);
702
703 if ((flags & SCSI_POLL) == 0)
704 return SUCCESSFULLY_QUEUED;
705
706 /* Not allowed to use interrupts, use polling instead */
707 s = splbio();
708 if (spc_poll(sc, xs, acb->timeout)) {
709 spc_timeout(acb);
710 if (spc_poll(sc, xs, acb->timeout))
711 spc_timeout(acb);
712 }
713 splx(s);
714 return COMPLETE;
715 }
716
717 /*
718 * Adjust transfer size in buffer structure
719 */
720 void
721 spc_minphys(bp)
722 struct buf *bp;
723 {
724
725 SPC_TRACE(("spc_minphys "));
726 minphys(bp);
727 }
728
729 /*
730 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
731 */
732 int
733 spc_poll(sc, xs, count)
734 struct spc_softc *sc;
735 struct scsi_xfer *xs;
736 int count;
737 {
738
739 SPC_TRACE(("spc_poll "));
740 while (count) {
741 /*
742 * If we had interrupts enabled, would we
743 * have got an interrupt?
744 */
745 if (INTS != 0)
746 spcintr(sc->sc_dev.dv_unit);
747 if ((xs->flags & ITSDONE) != 0)
748 return 0;
749 delay(1000);
750 count--;
751 }
752 return 1;
753 }
754
755 /*
757 * LOW LEVEL SCSI UTILITIES
758 */
759
760 integrate void
761 spc_sched_msgout(sc, m)
762 struct spc_softc *sc;
763 u_char m;
764 {
765 if (sc->sc_msgpriq == 0)
766 SCMD = SCMD_SET_ATN;
767 sc->sc_msgpriq |= m;
768 }
769
770 /*
771 * Set synchronous transfer offset and period.
772 */
773 integrate void
774 spc_setsync(sc, ti)
775 struct spc_softc *sc;
776 struct spc_tinfo *ti;
777 {
778 #if SPC_USE_SYNCHRONOUS
779
780 if (ti->offset != 0)
781 TMOD =
782 ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
783 else
784 TMOD = 0;
785 #endif
786 }
787
788 /*
789 * Start a selection. This is used by spc_sched() to select an idle target,
790 * and by spc_done() to immediately reselect a target to get sense information.
791 */
792 void
793 spc_select(sc, acb)
794 struct spc_softc *sc;
795 struct spc_acb *acb;
796 {
797 struct scsi_link *sc_link = acb->xs->sc_link;
798 int target = sc_link->target;
799 struct spc_tinfo *ti = &sc->sc_tinfo[target];
800
801 spc_setsync(sc, ti);
802
803 #if 0
804 SCMD = SCMD_SET_ATN;
805 #endif
806 PCTL = 0;
807 TEMP = (1 << sc->sc_initiator) | (1 << target);
808 /*
809 * BSY $B$K$h$k1~EzBT$A;~4V@_Dj(B ($B@_Dj;~4V$r2a$.$k$H(B selection timeout)
810 * 0 $B$K$9$k$HL58BBT$A(B (x68k $B$G$O(B Tclf == 200ns)
811 * T = (X * 256 + 15) * Tclf * 2 $B$J$N$G(B... 256ms $BBT$D$H$9$k$H(B
812 * 128000ns/200ns = X * 256 + 15
813 * 640 - 15 = X * 256
814 * X = 625 / 256
815 * X = 2 + 113 / 256
816 * $B$J$N$G(B tch $B$K(B 2, tcm $B$K(B 113 $B$rBeF~!#(B($B$$$$$N$+(B?)
817 */
818 TCH = 2;
819 TCM = 113;
820 /* BSY $B$H(B SEL $B$,(B 0 $B$K$J$C$F$+$i%U%'!<%:3+;O$^$G$N;~4V(B */
821 TCL = 3;
822 SCMD = SCMD_SELECT;
823
824 sc->sc_state = SPC_SELECTING;
825 }
826
827 int
828 spc_reselect(sc, message)
829 struct spc_softc *sc;
830 u_char message;
831 {
832 u_char selid, target, lun;
833 struct spc_acb *acb;
834 struct scsi_link *sc_link;
835 struct spc_tinfo *ti;
836
837 /*
838 * The SCSI chip made a snapshot of the data bus while the reselection
839 * was being negotiated. This enables us to determine which target did
840 * the reselect.
841 */
842 selid = sc->sc_selid & ~(1 << sc->sc_initiator);
843 if (selid & (selid - 1)) {
844 printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
845 sc->sc_dev.dv_xname, selid);
846 SPC_BREAK();
847 goto reset;
848 }
849
850 /*
851 * Search wait queue for disconnected cmd
852 * The list should be short, so I haven't bothered with
853 * any more sophisticated structures than a simple
854 * singly linked list.
855 */
856 target = ffs(selid) - 1;
857 lun = message & 0x07;
858 for (acb = sc->nexus_list.tqh_first; acb != NULL;
859 acb = acb->chain.tqe_next) {
860 sc_link = acb->xs->sc_link;
861 if (sc_link->target == target && sc_link->lun == lun)
862 break;
863 }
864 if (acb == NULL) {
865 printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
866 sc->sc_dev.dv_xname, target, lun);
867 SPC_BREAK();
868 goto abort;
869 }
870
871 /* Make this nexus active again. */
872 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
873 sc->sc_state = SPC_CONNECTED;
874 sc->sc_nexus = acb;
875 ti = &sc->sc_tinfo[target];
876 ti->lubusy |= (1 << lun);
877 spc_setsync(sc, ti);
878
879 if (acb->flags & ACB_RESET)
880 spc_sched_msgout(sc, SEND_DEV_RESET);
881 else if (acb->flags & ACB_ABORT)
882 spc_sched_msgout(sc, SEND_ABORT);
883
884 /* Do an implicit RESTORE POINTERS. */
885 sc->sc_dp = acb->data_addr;
886 sc->sc_dleft = acb->data_length;
887 sc->sc_cp = (u_char *)&acb->scsi_cmd;
888 sc->sc_cleft = acb->scsi_cmd_length;
889
890 return (0);
891
892 reset:
893 spc_sched_msgout(sc, SEND_DEV_RESET);
894 return (1);
895
896 abort:
897 spc_sched_msgout(sc, SEND_ABORT);
898 return (1);
899 }
900
901 /*
903 * Schedule a SCSI operation. This has now been pulled out of the interrupt
904 * handler so that we may call it from spc_scsi_cmd and spc_done. This may
905 * save us an unecessary interrupt just to get things going. Should only be
906 * called when state == SPC_IDLE and at bio pl.
907 */
908 void
909 spc_sched(sc)
910 register struct spc_softc *sc;
911 {
912 struct spc_acb *acb;
913 struct scsi_link *sc_link;
914 struct spc_tinfo *ti;
915
916 /*
917 * Find first acb in ready queue that is for a target/lunit pair that
918 * is not busy.
919 */
920 for (acb = sc->ready_list.tqh_first; acb != NULL;
921 acb = acb->chain.tqe_next) {
922 sc_link = acb->xs->sc_link;
923 ti = &sc->sc_tinfo[sc_link->target];
924 if ((ti->lubusy & (1 << sc_link->lun)) == 0) {
925 SPC_MISC(("selecting %d:%d ",
926 sc_link->target, sc_link->lun));
927 TAILQ_REMOVE(&sc->ready_list, acb, chain);
928 sc->sc_nexus = acb;
929 spc_select(sc, acb);
930 return;
931 } else
932 SPC_MISC(("%d:%d busy\n",
933 sc_link->target, sc_link->lun));
934 }
935 SPC_MISC(("idle "));
936 /* Nothing to start; just enable reselections and wait. */
937 }
938
939 void
941 spc_sense(sc, acb)
942 struct spc_softc *sc;
943 struct spc_acb *acb;
944 {
945 struct scsi_xfer *xs = acb->xs;
946 struct scsi_link *sc_link = xs->sc_link;
947 struct spc_tinfo *ti = &sc->sc_tinfo[sc_link->target];
948 struct scsi_sense *ss = (void *)&acb->scsi_cmd;
949
950 SPC_MISC(("requesting sense "));
951 /* Next, setup a request sense command block */
952 bzero(ss, sizeof(*ss));
953 ss->opcode = REQUEST_SENSE;
954 ss->byte2 = sc_link->lun << 5;
955 ss->length = sizeof(struct scsi_sense_data);
956 acb->scsi_cmd_length = sizeof(*ss);
957 acb->data_addr = (char *)&xs->sense;
958 acb->data_length = sizeof(struct scsi_sense_data);
959 acb->flags |= ACB_SENSE;
960 ti->senses++;
961 if (acb->flags & ACB_NEXUS)
962 ti->lubusy &= ~(1 << sc_link->lun);
963 if (acb == sc->sc_nexus) {
964 spc_select(sc, acb);
965 } else {
966 spc_dequeue(sc, acb);
967 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
968 if (sc->sc_state == SPC_IDLE)
969 spc_sched(sc);
970 }
971 }
972
973 /*
974 * POST PROCESSING OF SCSI_CMD (usually current)
975 */
976 void
977 spc_done(sc, acb)
978 struct spc_softc *sc;
979 struct spc_acb *acb;
980 {
981 struct scsi_xfer *xs = acb->xs;
982 struct scsi_link *sc_link = xs->sc_link;
983 struct spc_tinfo *ti = &sc->sc_tinfo[sc_link->target];
984
985 SPC_TRACE(("spc_done "));
986
987 /*
988 * Now, if we've come here with no error code, i.e. we've kept the
989 * initial XS_NOERROR, and the status code signals that we should
990 * check sense, we'll need to set up a request sense cmd block and
991 * push the command back into the ready queue *before* any other
992 * commands for this target/lunit, else we lose the sense info.
993 * We don't support chk sense conditions for the request sense cmd.
994 */
995 if (xs->error == XS_NOERROR) {
996 if (acb->flags & ACB_ABORT) {
997 xs->error = XS_DRIVER_STUFFUP;
998 } else if (acb->flags & ACB_SENSE) {
999 xs->error = XS_SENSE;
1000 } else if (acb->target_stat == SCSI_CHECK) {
1001 /* First, save the return values */
1002 xs->resid = acb->data_length;
1003 xs->status = acb->target_stat;
1004 spc_sense(sc, acb);
1005 return;
1006 } else {
1007 xs->resid = acb->data_length;
1008 }
1009 }
1010
1011 xs->flags |= ITSDONE;
1012
1013 #if SPC_DEBUG
1014 if ((spc_debug & SPC_SHOWMISC) != 0) {
1015 if (xs->resid != 0)
1016 printf("resid=%d ", xs->resid);
1017 if (xs->error == XS_SENSE)
1018 printf("sense=0x%02x\n", xs->sense.error_code);
1019 else
1020 printf("error=%d\n", xs->error);
1021 }
1022 #endif
1023
1024 /*
1025 * Remove the ACB from whatever queue it happens to be on.
1026 */
1027 if (acb->flags & ACB_NEXUS)
1028 ti->lubusy &= ~(1 << sc_link->lun);
1029 if (acb == sc->sc_nexus) {
1030 sc->sc_nexus = NULL;
1031 sc->sc_state = SPC_IDLE;
1032 spc_sched(sc);
1033 } else
1034 spc_dequeue(sc, acb);
1035
1036 spc_free_acb(sc, acb, xs->flags);
1037 ti->cmds++;
1038 scsi_done(xs);
1039 }
1040
1041 void
1042 spc_dequeue(sc, acb)
1043 struct spc_softc *sc;
1044 struct spc_acb *acb;
1045 {
1046
1047 if (acb->flags & ACB_NEXUS) {
1048 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1049 } else {
1050 TAILQ_REMOVE(&sc->ready_list, acb, chain);
1051 }
1052 }
1053
1054 /*
1056 * INTERRUPT/PROTOCOL ENGINE
1057 */
1058
1059 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
1060 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
1061 #define ISEXTMSG(m) ((m) == 0x01)
1062
1063 /*
1064 * Precondition:
1065 * The SCSI bus is already in the MSGI phase and there is a message byte
1066 * on the bus, along with an asserted REQ signal.
1067 */
1068 void
1069 spc_msgin(sc)
1070 register struct spc_softc *sc;
1071 {
1072 int n;
1073
1074 SPC_TRACE(("spc_msgin "));
1075
1076 if (sc->sc_prevphase == PH_MSGIN) {
1077 /* This is a continuation of the previous message. */
1078 n = sc->sc_imp - sc->sc_imess;
1079 goto nextbyte;
1080 }
1081
1082 /* This is a new MESSAGE IN phase. Clean up our state. */
1083 sc->sc_flags &= ~SPC_DROP_MSGIN;
1084
1085 nextmsg:
1086 n = 0;
1087 sc->sc_imp = &sc->sc_imess[n];
1088
1089 nextbyte:
1090 /*
1091 * Read a whole message, but don't ack the last byte. If we reject the
1092 * message, we have to assert ATN during the message transfer phase
1093 * itself.
1094 */
1095 for (;;) {
1096 #if 0
1097 for (;;) {
1098 if ((PSNS & PSNS_REQ) != 0)
1099 break;
1100 /* Wait for REQINIT. XXX Need timeout. */
1101 }
1102 #endif
1103 if (INTS != 0) {
1104 /*
1105 * Target left MESSAGE IN, probably because it
1106 * a) noticed our ATN signal, or
1107 * b) ran out of messages.
1108 */
1109 goto out;
1110 }
1111
1112 /* If parity error, just dump everything on the floor. */
1113 if ((SERR & (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) {
1114 sc->sc_flags |= SPC_DROP_MSGIN;
1115 spc_sched_msgout(sc, SEND_PARITY_ERROR);
1116 }
1117
1118 /* send TRANSFER command. */
1119 TCH = 0;
1120 TCM = 0;
1121 TCL = 1;
1122 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1123 SCMD = SCMD_XFR; /* | SCMD_PROG_XFR */
1124 for (;;) {
1125 /*if ((SSTS & SSTS_BUSY) != 0 && (SSTS & SSTS_DREG_EMPTY) != 0)*/
1126 if ((SSTS & SSTS_DREG_EMPTY) == 0)
1127 break;
1128 if (INTS != 0)
1129 goto out;
1130 }
1131
1132 /* Gather incoming message bytes if needed. */
1133 if ((sc->sc_flags & SPC_DROP_MSGIN) == 0) {
1134 if (n >= SPC_MAX_MSG_LEN) {
1135 (void) DREG;
1136 sc->sc_flags |= SPC_DROP_MSGIN;
1137 spc_sched_msgout(sc, SEND_REJECT);
1138 } else {
1139 *sc->sc_imp++ = DREG;
1140 n++;
1141 /*
1142 * This testing is suboptimal, but most
1143 * messages will be of the one byte variety, so
1144 * it should not affect performance
1145 * significantly.
1146 */
1147 if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
1148 break;
1149 if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
1150 break;
1151 if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
1152 n == sc->sc_imess[1] + 2)
1153 break;
1154 }
1155 } else
1156 (void) DREG;
1157
1158 /*
1159 * If we reach this spot we're either:
1160 * a) in the middle of a multi-byte message, or
1161 * b) dropping bytes.
1162 */
1163
1164 #if 0
1165 /* Ack the last byte read. */
1166 /*(void) DREG;*/
1167 while ((PSNS & ACKI) != 0)
1168 ;
1169 #endif
1170 }
1171
1172 SPC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1173
1174 /* We now have a complete message. Parse it. */
1175 switch (sc->sc_state) {
1176 struct spc_acb *acb;
1177 struct scsi_link *sc_link;
1178 struct spc_tinfo *ti;
1179
1180 case SPC_CONNECTED:
1181 SPC_ASSERT(sc->sc_nexus != NULL);
1182 acb = sc->sc_nexus;
1183 ti = &sc->sc_tinfo[acb->xs->sc_link->target];
1184
1185 switch (sc->sc_imess[0]) {
1186 case MSG_CMDCOMPLETE:
1187 if (sc->sc_dleft < 0) {
1188 sc_link = acb->xs->sc_link;
1189 printf("%s: %d extra bytes from %d:%d\n",
1190 sc->sc_dev.dv_xname, -sc->sc_dleft,
1191 sc_link->target, sc_link->lun);
1192 acb->data_length = 0;
1193 }
1194 acb->xs->resid = acb->data_length = sc->sc_dleft;
1195 sc->sc_state = SPC_CMDCOMPLETE;
1196 break;
1197
1198 case MSG_PARITY_ERROR:
1199 /* Resend the last message. */
1200 spc_sched_msgout(sc, sc->sc_lastmsg);
1201 break;
1202
1203 case MSG_MESSAGE_REJECT:
1204 SPC_MISC(("message rejected %02x ", sc->sc_lastmsg));
1205 switch (sc->sc_lastmsg) {
1206 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
1207 case SEND_IDENTIFY:
1208 ti->flags &= ~(DO_SYNC | DO_WIDE);
1209 ti->period = ti->offset = 0;
1210 spc_setsync(sc, ti);
1211 ti->width = 0;
1212 break;
1213 #endif
1214 #if SPC_USE_SYNCHRONOUS
1215 case SEND_SDTR:
1216 ti->flags &= ~DO_SYNC;
1217 ti->period = ti->offset = 0;
1218 spc_setsync(sc, ti);
1219 break;
1220 #endif
1221 #if SPC_USE_WIDE
1222 case SEND_WDTR:
1223 ti->flags &= ~DO_WIDE;
1224 ti->width = 0;
1225 break;
1226 #endif
1227 case SEND_INIT_DET_ERR:
1228 spc_sched_msgout(sc, SEND_ABORT);
1229 break;
1230 }
1231 break;
1232
1233 case MSG_NOOP:
1234 break;
1235
1236 case MSG_DISCONNECT:
1237 ti->dconns++;
1238 sc->sc_state = SPC_DISCONNECT;
1239 break;
1240
1241 case MSG_SAVEDATAPOINTER:
1242 acb->data_addr = sc->sc_dp;
1243 acb->data_length = sc->sc_dleft;
1244 break;
1245
1246 case MSG_RESTOREPOINTERS:
1247 sc->sc_dp = acb->data_addr;
1248 sc->sc_dleft = acb->data_length;
1249 sc->sc_cp = (u_char *)&acb->scsi_cmd;
1250 sc->sc_cleft = acb->scsi_cmd_length;
1251 break;
1252
1253 case MSG_EXTENDED:
1254 switch (sc->sc_imess[2]) {
1255 #if SPC_USE_SYNCHRONOUS
1256 case MSG_EXT_SDTR:
1257 if (sc->sc_imess[1] != 3)
1258 goto reject;
1259 ti->period = sc->sc_imess[3];
1260 ti->offset = sc->sc_imess[4];
1261 ti->flags &= ~DO_SYNC;
1262 if (ti->offset == 0) {
1263 } else if (ti->period < sc->sc_minsync ||
1264 ti->period > sc->sc_maxsync ||
1265 ti->offset > 8) {
1266 ti->period = ti->offset = 0;
1267 spc_sched_msgout(sc, SEND_SDTR);
1268 } else {
1269 sc_print_addr(acb->xs->sc_link);
1270 printf("sync, offset %d, period %dnsec\n",
1271 ti->offset, ti->period * 4);
1272 }
1273 spc_setsync(sc, ti);
1274 break;
1275 #endif
1276
1277 #if SPC_USE_WIDE
1278 case MSG_EXT_WDTR:
1279 if (sc->sc_imess[1] != 2)
1280 goto reject;
1281 ti->width = sc->sc_imess[3];
1282 ti->flags &= ~DO_WIDE;
1283 if (ti->width == 0) {
1284 } else if (ti->width > SPC_MAX_WIDTH) {
1285 ti->width = 0;
1286 spc_sched_msgout(sc, SEND_WDTR);
1287 } else {
1288 sc_print_addr(acb->xs->sc_link);
1289 printf("wide, width %d\n",
1290 1 << (3 + ti->width));
1291 }
1292 break;
1293 #endif
1294
1295 default:
1296 printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
1297 sc->sc_dev.dv_xname);
1298 SPC_BREAK();
1299 goto reject;
1300 }
1301 break;
1302
1303 default:
1304 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1305 sc->sc_dev.dv_xname);
1306 SPC_BREAK();
1307 reject:
1308 spc_sched_msgout(sc, SEND_REJECT);
1309 break;
1310 }
1311 break;
1312
1313 case SPC_RESELECTED:
1314 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1315 printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
1316 sc->sc_dev.dv_xname);
1317 SPC_BREAK();
1318 goto reset;
1319 }
1320
1321 (void) spc_reselect(sc, sc->sc_imess[0]);
1322 break;
1323
1324 default:
1325 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1326 sc->sc_dev.dv_xname);
1327 SPC_BREAK();
1328 reset:
1329 spc_sched_msgout(sc, SEND_DEV_RESET);
1330 break;
1331
1332 abort:
1333 spc_sched_msgout(sc, SEND_ABORT);
1334 break;
1335 }
1336
1337 /* Ack the last message byte. */
1338 #if 0 /* XXX? */
1339 (void) DREG;
1340 while ((PSNS & ACKI) != 0)
1341 ;
1342 #endif
1343
1344 /* Go get the next message, if any. */
1345 goto nextmsg;
1346
1347 out:
1348 SCMD = SCMD_RST_ACK;
1349 SPC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1350 }
1351
1352 /*
1353 * Send the highest priority, scheduled message.
1354 */
1355 void
1356 spc_msgout(sc)
1357 register struct spc_softc *sc;
1358 {
1359 struct spc_tinfo *ti;
1360 int n;
1361
1362 SPC_TRACE(("spc_msgout "));
1363
1364 if (sc->sc_prevphase == PH_MSGOUT) {
1365 if (sc->sc_omp == sc->sc_omess) {
1366 /*
1367 * This is a retransmission.
1368 *
1369 * We get here if the target stayed in MESSAGE OUT
1370 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1371 * that all of the previously transmitted messages must
1372 * be sent again, in the same order. Therefore, we
1373 * requeue all the previously transmitted messages, and
1374 * start again from the top. Our simple priority
1375 * scheme keeps the messages in the right order.
1376 */
1377 SPC_MISC(("retransmitting "));
1378 sc->sc_msgpriq |= sc->sc_msgoutq;
1379 /*
1380 * Set ATN. If we're just sending a trivial 1-byte
1381 * message, we'll clear ATN later on anyway.
1382 */
1383 SCMD = SCMD_SET_ATN; /* XXX? */
1384 } else {
1385 /* This is a continuation of the previous message. */
1386 n = sc->sc_omp - sc->sc_omess;
1387 goto nextbyte;
1388 }
1389 }
1390
1391 /* No messages transmitted so far. */
1392 sc->sc_msgoutq = 0;
1393 sc->sc_lastmsg = 0;
1394
1395 nextmsg:
1396 /* Pick up highest priority message. */
1397 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1398 sc->sc_msgpriq &= ~sc->sc_currmsg;
1399 sc->sc_msgoutq |= sc->sc_currmsg;
1400
1401 /* Build the outgoing message data. */
1402 switch (sc->sc_currmsg) {
1403 case SEND_IDENTIFY:
1404 SPC_ASSERT(sc->sc_nexus != NULL);
1405 sc->sc_omess[0] =
1406 MSG_IDENTIFY(sc->sc_nexus->xs->sc_link->lun, 1);
1407 n = 1;
1408 break;
1409
1410 #if SPC_USE_SYNCHRONOUS
1411 case SEND_SDTR:
1412 SPC_ASSERT(sc->sc_nexus != NULL);
1413 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
1414 sc->sc_omess[4] = MSG_EXTENDED;
1415 sc->sc_omess[3] = 3;
1416 sc->sc_omess[2] = MSG_EXT_SDTR;
1417 sc->sc_omess[1] = ti->period >> 2;
1418 sc->sc_omess[0] = ti->offset;
1419 n = 5;
1420 break;
1421 #endif
1422
1423 #if SPC_USE_WIDE
1424 case SEND_WDTR:
1425 SPC_ASSERT(sc->sc_nexus != NULL);
1426 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
1427 sc->sc_omess[3] = MSG_EXTENDED;
1428 sc->sc_omess[2] = 2;
1429 sc->sc_omess[1] = MSG_EXT_WDTR;
1430 sc->sc_omess[0] = ti->width;
1431 n = 4;
1432 break;
1433 #endif
1434
1435 case SEND_DEV_RESET:
1436 sc->sc_flags |= SPC_ABORTING;
1437 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1438 n = 1;
1439 break;
1440
1441 case SEND_REJECT:
1442 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1443 n = 1;
1444 break;
1445
1446 case SEND_PARITY_ERROR:
1447 sc->sc_omess[0] = MSG_PARITY_ERROR;
1448 n = 1;
1449 break;
1450
1451 case SEND_INIT_DET_ERR:
1452 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1453 n = 1;
1454 break;
1455
1456 case SEND_ABORT:
1457 sc->sc_flags |= SPC_ABORTING;
1458 sc->sc_omess[0] = MSG_ABORT;
1459 n = 1;
1460 break;
1461
1462 default:
1463 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1464 sc->sc_dev.dv_xname);
1465 SPC_BREAK();
1466 sc->sc_omess[0] = MSG_NOOP;
1467 n = 1;
1468 break;
1469 }
1470 sc->sc_omp = &sc->sc_omess[n];
1471
1472 nextbyte:
1473 /* Send message bytes. */
1474 /* send TRANSFER command. */
1475 TCH = n >> 16;
1476 TCM = n >> 8;
1477 TCL = n;
1478 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1479 SCMD = SCMD_XFR; /* | SCMD_PROG_XFR */
1480 for (;;) {
1481 if ((SSTS & SSTS_BUSY) != 0)
1482 break;
1483 if (INTS != 0)
1484 goto out;
1485 }
1486 for (;;) {
1487 #if 0
1488 for (;;) {
1489 if ((PSNS & PSNS_REQ) != 0)
1490 break;
1491 /* Wait for REQINIT. XXX Need timeout. */
1492 }
1493 #endif
1494 if (INTS != 0) {
1495 /*
1496 * Target left MESSAGE OUT, possibly to reject
1497 * our message.
1498 *
1499 * If this is the last message being sent, then we
1500 * deassert ATN, since either the target is going to
1501 * ignore this message, or it's going to ask for a
1502 * retransmission via MESSAGE PARITY ERROR (in which
1503 * case we reassert ATN anyway).
1504 */
1505 #if 0
1506 if (sc->sc_msgpriq == 0)
1507 SCMD = SCMD_RST_ATN;
1508 #endif
1509 goto out;
1510 }
1511
1512 #if 0
1513 /* Clear ATN before last byte if this is the last message. */
1514 if (n == 1 && sc->sc_msgpriq == 0)
1515 SCMD = SCMD_RST_ATN;
1516 #endif
1517
1518 while ((SSTS & SSTS_DREG_FULL) != 0)
1519 ;
1520 /* Send message byte. */
1521 DREG = *--sc->sc_omp;
1522 --n;
1523 /* Keep track of the last message we've sent any bytes of. */
1524 sc->sc_lastmsg = sc->sc_currmsg;
1525 #if 0
1526 /* Wait for ACK to be negated. XXX Need timeout. */
1527 while ((PSNS & ACKI) != 0)
1528 ;
1529 #endif
1530
1531 if (n == 0)
1532 break;
1533 }
1534
1535 /* We get here only if the entire message has been transmitted. */
1536 if (sc->sc_msgpriq != 0) {
1537 /* There are more outgoing messages. */
1538 goto nextmsg;
1539 }
1540
1541 /*
1542 * The last message has been transmitted. We need to remember the last
1543 * message transmitted (in case the target switches to MESSAGE IN phase
1544 * and sends a MESSAGE REJECT), and the list of messages transmitted
1545 * this time around (in case the target stays in MESSAGE OUT phase to
1546 * request a retransmit).
1547 */
1548
1549 out:
1550 /* Disable REQ/ACK protocol. */
1551 }
1552
1553 /*
1555 * This new revision has been optimized (I tried) to make the common case fast,
1556 * and the rarer cases (as a result) somewhat more comlex
1557 */
1558 int
1559 spc_dataout_pio(sc, p, n)
1560 register struct spc_softc *sc;
1561 u_char *p;
1562 int n;
1563 {
1564 register u_char intstat = 0;
1565 int out = 0;
1566 #define DOUTAMOUNT 8 /* Full FIFO */
1567
1568 /* send TRANSFER command. */
1569 TCH = n >> 16;
1570 TCM = n >> 8;
1571 TCL = n;
1572 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1573 SCMD = SCMD_XFR;
1574 for (;;) {
1575 if ((SSTS & SSTS_BUSY) != 0)
1576 break;
1577 if (INTS != 0)
1578 break;
1579 }
1580
1581 /*
1582 * I have tried to make the main loop as tight as possible. This
1583 * means that some of the code following the loop is a bit more
1584 * complex than otherwise.
1585 */
1586 while (n > 0) {
1587 int xfer;
1588
1589 for (;;) {
1590 intstat = INTS;
1591 /* $B%P%C%U%!$,6u$K$J$k$^$GBT$D(B */
1592 if ((SSTS & SSTS_DREG_EMPTY) != 0)
1593 break;
1594 /* $B$?$@$73d$j9~$_$,F~$C$F$-$?$iH4$1$k(B */
1595 if (intstat != 0)
1596 goto phasechange;
1597 }
1598
1599 xfer = min(DOUTAMOUNT, n);
1600
1601 SPC_MISC(("%d> ", xfer));
1602
1603 n -= xfer;
1604 out += xfer;
1605
1606 while (xfer-- > 0) {
1607 DREG = *p++;
1608 }
1609 }
1610
1611 if (out == 0) {
1612 for (;;) {
1613 if (INTS != 0)
1614 break;
1615 }
1616 SPC_MISC(("extra data "));
1617 } else {
1618 /* See the bytes off chip */
1619 for (;;) {
1620 /* $B%P%C%U%!$,6u$K$J$k$^$GBT$D(B */
1621 if ((SSTS & SSTS_DREG_EMPTY) != 0)
1622 break;
1623 intstat = INTS;
1624 /* $B$?$@$73d$j9~$_$,F~$C$F$-$?$iH4$1$k(B */
1625 if (intstat != 0)
1626 goto phasechange;
1627 }
1628 }
1629
1630 phasechange:
1631 /* Stop the FIFO data path. */
1632
1633 if (intstat != 0) {
1634 /* Some sort of phase change. */
1635 int amount;
1636
1637 amount = (TCH << 16) | (TCM << 8) | TCL;
1638 if (amount > 0) {
1639 out -= amount;
1640 SPC_MISC(("+%d ", amount));
1641 }
1642 }
1643 /* Turn on ENREQINIT again. */
1644
1645 return out;
1646 }
1647
1648 /*
1650 * For now, uses a pretty dumb algorithm, hangs around until all data has been
1651 * transferred. This, is OK for fast targets, but not so smart for slow
1652 * targets which don't disconnect or for huge transfers.
1653 */
1654 int
1655 spc_datain_pio(sc, p, n)
1656 register struct spc_softc *sc;
1657 u_char *p;
1658 int n;
1659 {
1660 register u_short intstat;
1661 int in = 0;
1662 #define DINAMOUNT 8 /* Full FIFO */
1663
1664 /* send TRANSFER command. */
1665 TCH = n >> 16;
1666 TCM = n >> 8;
1667 TCL = n;
1668 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1669 SCMD = SCMD_XFR;
1670 for (;;) {
1671 if ((SSTS & SSTS_BUSY) != 0)
1672 break;
1673 if (INTS != 0)
1674 goto phasechange;
1675 }
1676
1677 /*
1678 * We leave this loop if one or more of the following is true:
1679 * a) phase != PH_DATAIN && FIFOs are empty
1680 * b) reset has occurred or busfree is detected.
1681 */
1682 while (n > 0) {
1683 int xfer;
1684
1685 #define INTSMASK 0xff
1686 /* Wait for fifo half full or phase mismatch */
1687 for (;;) {
1688 intstat = (SSTS << 8) | INTS;
1689 if ((intstat & (INTSMASK | (SSTS_DREG_FULL << 8))) != 0)
1690 break;
1691 if ((intstat & (SSTS_DREG_EMPTY << 8)) == 0)
1692 break;
1693 }
1694
1695 #if 1
1696 if ((intstat & INTSMASK) != 0)
1697 goto phasechange;
1698 #else
1699 if ((intstat & INTSMASK) != 0 &&
1700 (intstat & (SSTS_DREG_EMPTY << 8)))
1701 goto phasechange;
1702 #endif
1703 if ((intstat & (SSTS_DREG_FULL << 8)) != 0)
1704 xfer = min(DINAMOUNT, n);
1705 else
1706 xfer = min(1, n);
1707
1708 SPC_MISC((">%d ", xfer));
1709
1710 n -= xfer;
1711 in += xfer;
1712
1713 while (xfer-- > 0) {
1714 *p++ = DREG;
1715 }
1716
1717 if ((intstat & INTSMASK) != 0)
1718 goto phasechange;
1719 }
1720
1721 /*
1722 * Some SCSI-devices are rude enough to transfer more data than what
1723 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
1724 * requested 512. Test for progress, i.e. real transfers. If no real
1725 * transfers have been performed (n is probably already zero) and the
1726 * FIFO is not empty, waste some bytes....
1727 */
1728 if (in == 0) {
1729 for (;;) {
1730 if (INTS != 0)
1731 break;
1732 }
1733 SPC_MISC(("extra data "));
1734 }
1735
1736 phasechange:
1737 /* Stop the FIFO data path. */
1738
1739 /* Turn on ENREQINIT again. */
1740
1741 return in;
1742 }
1743
1744 /*
1746 * Catch an interrupt from the adaptor
1747 */
1748 /*
1749 * This is the workhorse routine of the driver.
1750 * Deficiencies (for now):
1751 * 1) always uses programmed I/O
1752 */
1753 int
1754 spcintr(unit)
1755 int unit;
1756 {
1757 register struct spc_softc *sc = spc_cd.cd_devs[unit]; /* XXX */
1758 u_char ints;
1759 register struct spc_acb *acb;
1760 register struct scsi_link *sc_link;
1761 struct spc_tinfo *ti;
1762 int n;
1763
1764 /*
1765 * $B3d$j9~$_6X;_$K$9$k(B
1766 */
1767 SCTL &= ~SCTL_INTR_ENAB;
1768
1769 SPC_TRACE(("spcintr "));
1770
1771 loop:
1772 /*
1773 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
1774 */
1775 /*
1776 * First check for abnormal conditions, such as reset.
1777 */
1778 #if 1 /* XXX? */
1779 while ((ints = INTS) == 0)
1780 delay(1);
1781 SPC_MISC(("ints = 0x%x ", ints));
1782 #else /* usually? */
1783 ints = INTS;
1784 #endif
1785 if ((ints & INTS_RST) != 0) {
1786 printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
1787 goto reset;
1788 }
1789
1790 /*
1791 * Check for less serious errors.
1792 */
1793 if ((SERR & (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) {
1794 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
1795 if (sc->sc_prevphase == PH_MSGIN) {
1796 sc->sc_flags |= SPC_DROP_MSGIN;
1797 spc_sched_msgout(sc, SEND_PARITY_ERROR);
1798 } else
1799 spc_sched_msgout(sc, SEND_INIT_DET_ERR);
1800 }
1801
1802 /*
1803 * If we're not already busy doing something test for the following
1804 * conditions:
1805 * 1) We have been reselected by something
1806 * 2) We have selected something successfully
1807 * 3) Our selection process has timed out
1808 * 4) This is really a bus free interrupt just to get a new command
1809 * going?
1810 * 5) Spurious interrupt?
1811 */
1812 switch (sc->sc_state) {
1813 case SPC_IDLE:
1814 case SPC_SELECTING:
1815
1816 if ((ints & INTS_SEL) != 0) {
1817 /*
1818 * We don't currently support target mode.
1819 */
1820 printf("%s: target mode selected; going to BUS FREE\n",
1821 sc->sc_dev.dv_xname);
1822
1823 goto sched;
1824 } else if ((ints & INTS_RESEL) != 0) {
1825 SPC_MISC(("reselected "));
1826
1827 /*
1828 * If we're trying to select a target ourselves,
1829 * push our command back into the ready list.
1830 */
1831 if (sc->sc_state == SPC_SELECTING) {
1832 SPC_MISC(("backoff selector "));
1833 SPC_ASSERT(sc->sc_nexus != NULL);
1834 acb = sc->sc_nexus;
1835 sc->sc_nexus = NULL;
1836 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
1837 }
1838
1839 /* Save reselection ID. */
1840 sc->sc_selid = TEMP;
1841
1842 sc->sc_state = SPC_RESELECTED;
1843 } else if ((ints & INTS_CMD_DONE) != 0) {
1844 SPC_MISC(("selected "));
1845
1846 /*
1847 * We have selected a target. Things to do:
1848 * a) Determine what message(s) to send.
1849 * b) Verify that we're still selecting the target.
1850 * c) Mark device as busy.
1851 */
1852 if (sc->sc_state != SPC_SELECTING) {
1853 printf("%s: selection out while idle; resetting\n",
1854 sc->sc_dev.dv_xname);
1855 SPC_BREAK();
1856 goto reset;
1857 }
1858 SPC_ASSERT(sc->sc_nexus != NULL);
1859 acb = sc->sc_nexus;
1860 sc_link = acb->xs->sc_link;
1861 ti = &sc->sc_tinfo[sc_link->target];
1862
1863 sc->sc_msgpriq = SEND_IDENTIFY;
1864 if (acb->flags & ACB_RESET)
1865 sc->sc_msgpriq |= SEND_DEV_RESET;
1866 else if (acb->flags & ACB_ABORT)
1867 sc->sc_msgpriq |= SEND_ABORT;
1868 else {
1869 #if SPC_USE_SYNCHRONOUS
1870 if ((ti->flags & DO_SYNC) != 0)
1871 sc->sc_msgpriq |= SEND_SDTR;
1872 #endif
1873 #if SPC_USE_WIDE
1874 if ((ti->flags & DO_WIDE) != 0)
1875 sc->sc_msgpriq |= SEND_WDTR;
1876 #endif
1877 }
1878
1879 acb->flags |= ACB_NEXUS;
1880 ti->lubusy |= (1 << sc_link->lun);
1881
1882 /* Do an implicit RESTORE POINTERS. */
1883 sc->sc_dp = acb->data_addr;
1884 sc->sc_dleft = acb->data_length;
1885 sc->sc_cp = (u_char *)&acb->scsi_cmd;
1886 sc->sc_cleft = acb->scsi_cmd_length;
1887
1888 /* On our first connection, schedule a timeout. */
1889 if ((acb->xs->flags & SCSI_POLL) == 0)
1890 timeout(spc_timeout, acb, (acb->timeout * hz) / 1000);
1891
1892 sc->sc_state = SPC_CONNECTED;
1893 } else if ((ints & INTS_TIMEOUT) != 0) {
1894 SPC_MISC(("selection timeout "));
1895
1896 if (sc->sc_state != SPC_SELECTING) {
1897 printf("%s: selection timeout while idle; resetting\n",
1898 sc->sc_dev.dv_xname);
1899 SPC_BREAK();
1900 goto reset;
1901 }
1902 SPC_ASSERT(sc->sc_nexus != NULL);
1903 acb = sc->sc_nexus;
1904
1905 delay(250);
1906
1907 acb->xs->error = XS_SELTIMEOUT;
1908 goto finish;
1909 } else {
1910 if (sc->sc_state != SPC_IDLE) {
1911 printf("%s: BUS FREE while not idle; state=%d\n",
1912 sc->sc_dev.dv_xname, sc->sc_state);
1913 SPC_BREAK();
1914 goto out;
1915 }
1916
1917 goto sched;
1918 }
1919
1920 /*
1921 * Turn off selection stuff, and prepare to catch bus free
1922 * interrupts, parity errors, and phase changes.
1923 */
1924
1925 sc->sc_flags = 0;
1926 sc->sc_prevphase = PH_INVALID;
1927 goto dophase;
1928 }
1929
1930 if ((ints & INTS_DISCON) != 0) {
1931 /* We've gone to BUS FREE phase. */
1932 PCTL &= ~PCTL_BFINT_ENAB; /* disable disconnect interrupt */
1933 INTS = ints; /* XXX reset interrput */
1934
1935 switch (sc->sc_state) {
1936 case SPC_RESELECTED:
1937 goto sched;
1938
1939 case SPC_CONNECTED:
1940 SPC_ASSERT(sc->sc_nexus != NULL);
1941 acb = sc->sc_nexus;
1942
1943 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
1944 if (sc->sc_prevphase == PH_MSGOUT) {
1945 /*
1946 * If the target went to BUS FREE phase during
1947 * or immediately after sending a SDTR or WDTR
1948 * message, disable negotiation.
1949 */
1950 sc_link = acb->xs->sc_link;
1951 ti = &sc->sc_tinfo[sc_link->target];
1952 switch (sc->sc_lastmsg) {
1953 #if SPC_USE_SYNCHRONOUS
1954 case SEND_SDTR:
1955 ti->flags &= ~DO_SYNC;
1956 ti->period = ti->offset = 0;
1957 break;
1958 #endif
1959 #if SPC_USE_WIDE
1960 case SEND_WDTR:
1961 ti->flags &= ~DO_WIDE;
1962 ti->width = 0;
1963 break;
1964 #endif
1965 }
1966 }
1967 #endif
1968
1969 if ((sc->sc_flags & SPC_ABORTING) == 0) {
1970 /*
1971 * Section 5.1.1 of the SCSI 2 spec suggests
1972 * issuing a REQUEST SENSE following an
1973 * unexpected disconnect. Some devices go into
1974 * a contingent allegiance condition when
1975 * disconnecting, and this is necessary to
1976 * clean up their state.
1977 */
1978 printf("%s: unexpected disconnect; sending REQUEST SENSE\n",
1979 sc->sc_dev.dv_xname);
1980 SPC_BREAK();
1981 spc_sense(sc, acb);
1982 goto out;
1983 }
1984
1985 acb->xs->error = XS_DRIVER_STUFFUP;
1986 goto finish;
1987
1988 case SPC_DISCONNECT:
1989 SPC_ASSERT(sc->sc_nexus != NULL);
1990 acb = sc->sc_nexus;
1991 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1992 sc->sc_nexus = NULL;
1993 goto sched;
1994
1995 case SPC_CMDCOMPLETE:
1996 SPC_ASSERT(sc->sc_nexus != NULL);
1997 acb = sc->sc_nexus;
1998 goto finish;
1999 }
2000 }
2001 else if ((ints & INTS_CMD_DONE) != 0 &&
2002 sc->sc_prevphase == PH_MSGIN && sc->sc_state != SPC_CONNECTED)
2003 goto out;
2004
2005 dophase:
2006 #if 0
2007 if ((PSNS & PSNS_REQ) == 0) {
2008 /* Wait for REQINIT. */
2009 goto out;
2010 }
2011 #else
2012 INTS = ints;
2013 ints = 0;
2014 while ((PSNS & PSNS_REQ) == 0)
2015 delay(1); /* need timeout XXX */
2016 #endif
2017
2018 /*
2019 * $B%U%'!<%:$K$h$C$F>uBVA+0\$9$k(B
2020 */
2021 sc->sc_phase = PSNS & PH_MASK;
2022 /* PCTL = sc->sc_phase;*/
2023
2024 switch (sc->sc_phase) {
2025 case PH_MSGOUT:
2026 if (sc->sc_state != SPC_CONNECTED &&
2027 sc->sc_state != SPC_RESELECTED)
2028 break;
2029 spc_msgout(sc);
2030 sc->sc_prevphase = PH_MSGOUT;
2031 goto loop;
2032
2033 case PH_MSGIN:
2034 if (sc->sc_state != SPC_CONNECTED &&
2035 sc->sc_state != SPC_RESELECTED)
2036 break;
2037 spc_msgin(sc);
2038 sc->sc_prevphase = PH_MSGIN;
2039 goto loop;
2040
2041 case PH_CMD:
2042 if (sc->sc_state != SPC_CONNECTED)
2043 break;
2044 #if SPC_DEBUG
2045 if ((spc_debug & SPC_SHOWMISC) != 0) {
2046 SPC_ASSERT(sc->sc_nexus != NULL);
2047 acb = sc->sc_nexus;
2048 printf("cmd=0x%02x+%d ",
2049 acb->scsi_cmd.opcode, acb->scsi_cmd_length-1);
2050 }
2051 #endif
2052 n = spc_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
2053 sc->sc_cp += n;
2054 sc->sc_cleft -= n;
2055 sc->sc_prevphase = PH_CMD;
2056 goto loop;
2057
2058 case PH_DATAOUT:
2059 if (sc->sc_state != SPC_CONNECTED)
2060 break;
2061 SPC_MISC(("dataout dleft=%d ", sc->sc_dleft));
2062 n = spc_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
2063 sc->sc_dp += n;
2064 sc->sc_dleft -= n;
2065 sc->sc_prevphase = PH_DATAOUT;
2066 goto loop;
2067
2068 case PH_DATAIN:
2069 if (sc->sc_state != SPC_CONNECTED)
2070 break;
2071 SPC_MISC(("datain "));
2072 n = spc_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
2073 sc->sc_dp += n;
2074 sc->sc_dleft -= n;
2075 sc->sc_prevphase = PH_DATAIN;
2076 goto loop;
2077
2078 case PH_STAT:
2079 if (sc->sc_state != SPC_CONNECTED)
2080 break;
2081 SPC_ASSERT(sc->sc_nexus != NULL);
2082 acb = sc->sc_nexus;
2083 /*acb->target_stat = DREG;*/
2084 spc_datain_pio(sc, &acb->target_stat, 1);
2085 SPC_MISC(("target_stat=0x%02x ", acb->target_stat));
2086 sc->sc_prevphase = PH_STAT;
2087 goto loop;
2088 }
2089
2090 printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
2091 SPC_BREAK();
2092 reset:
2093 spc_init(sc);
2094 return 1;
2095
2096 finish:
2097 untimeout(spc_timeout, acb);
2098 INTS = ints;
2099 ints = 0;
2100 spc_done(sc, acb);
2101 goto out;
2102
2103 sched:
2104 sc->sc_state = SPC_IDLE;
2105 spc_sched(sc);
2106 goto out;
2107
2108 out:
2109 if (ints)
2110 INTS = ints;
2111 SCTL |= SCTL_INTR_ENAB;
2112 return 1;
2113 }
2114
2115 void
2116 spc_abort(sc, acb)
2117 struct spc_softc *sc;
2118 struct spc_acb *acb;
2119 {
2120
2121 /* 2 secs for the abort */
2122 acb->timeout = SPC_ABORT_TIMEOUT;
2123 acb->flags |= ACB_ABORT;
2124
2125 if (acb == sc->sc_nexus) {
2126 /*
2127 * If we're still selecting, the message will be scheduled
2128 * after selection is complete.
2129 */
2130 if (sc->sc_state == SPC_CONNECTED)
2131 spc_sched_msgout(sc, SEND_ABORT);
2132 } else {
2133 spc_dequeue(sc, acb);
2134 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
2135 if (sc->sc_state == SPC_IDLE)
2136 spc_sched(sc);
2137 }
2138 }
2139
2140 void
2141 spc_timeout(arg)
2142 void *arg;
2143 {
2144 struct spc_acb *acb = arg;
2145 struct scsi_xfer *xs = acb->xs;
2146 struct scsi_link *sc_link = xs->sc_link;
2147 struct spc_softc *sc = sc_link->adapter_softc;
2148 int s;
2149
2150 sc_print_addr(sc_link);
2151 printf("timed out");
2152
2153 s = splbio();
2154
2155 if (acb->flags & ACB_ABORT) {
2156 /* abort timed out */
2157 printf(" AGAIN\n");
2158 /* XXX Must reset! */
2159 } else {
2160 /* abort the operation that has timed out */
2161 printf("\n");
2162 acb->xs->error = XS_TIMEOUT;
2163 spc_abort(sc, acb);
2164 }
2165
2166 splx(s);
2167 }
2168
2169 #ifdef SPC_DEBUG
2171 /*
2172 * The following functions are mostly used for debugging purposes, either
2173 * directly called from the driver or from the kernel debugger.
2174 */
2175
2176 void
2177 spc_show_scsi_cmd(acb)
2178 struct spc_acb *acb;
2179 {
2180 u_char *b = (u_char *)&acb->scsi_cmd;
2181 struct scsi_link *sc_link = acb->xs->sc_link;
2182 int i;
2183
2184 sc_print_addr(sc_link);
2185 if ((acb->xs->flags & SCSI_RESET) == 0) {
2186 for (i = 0; i < acb->scsi_cmd_length; i++) {
2187 if (i)
2188 printf(",");
2189 printf("%x", b[i]);
2190 }
2191 printf("\n");
2192 } else
2193 printf("RESET\n");
2194 }
2195
2196 void
2197 spc_print_acb(acb)
2198 struct spc_acb *acb;
2199 {
2200
2201 printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
2202 printf(" dp=%x dleft=%d target_stat=%x\n",
2203 (long)acb->data_addr, acb->data_length, acb->target_stat);
2204 spc_show_scsi_cmd(acb);
2205 }
2206
2207 void
2208 spc_print_active_acb()
2209 {
2210 struct spc_acb *acb;
2211 struct spc_softc *sc = spc_cd.cd_devs[0]; /* XXX */
2212
2213 printf("ready list:\n");
2214 for (acb = sc->ready_list.tqh_first; acb != NULL;
2215 acb = acb->chain.tqe_next)
2216 spc_print_acb(acb);
2217 printf("nexus:\n");
2218 if (sc->sc_nexus != NULL)
2219 spc_print_acb(sc->sc_nexus);
2220 printf("nexus list:\n");
2221 for (acb = sc->nexus_list.tqh_first; acb != NULL;
2222 acb = acb->chain.tqe_next)
2223 spc_print_acb(acb);
2224 }
2225
2226 void
2227 spc_dump_driver(sc)
2228 struct spc_softc *sc;
2229 {
2230 struct spc_tinfo *ti;
2231 int i;
2232
2233 printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2234 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2235 sc->sc_state, sc->sc_imess[0],
2236 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2237 for (i = 0; i < 7; i++) {
2238 ti = &sc->sc_tinfo[i];
2239 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2240 i, ti->cmds, ti->dconns, ti->touts);
2241 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2242 }
2243 }
2244 #endif
2245