spc.c revision 1.1 1 /* $NetBSD: spc.c,v 1.1 1996/05/05 12:17:06 oki 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 *, 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 int
448 spcprint(aux, name)
449 void *aux;
450 char *name;
451 {
452 if (name != NULL)
453 printf("%s: scsibus ", name);
454 return UNCONF;
455 }
456
457 /*
458 */
459 void
460 spcattach(parent, self, aux)
461 struct device *parent, *self;
462 void *aux;
463 {
464 struct spc_softc *sc = (void *)self;
465
466 SPC_TRACE(("spcattach "));
467 sc->sc_state = SPC_INIT;
468 sc->sc_iobase = spc_find(sc->sc_dev.dv_unit); /* XXX */
469 spc_init(sc); /* Init chip and driver */
470
471 /*
472 * Fill in the prototype scsi_link
473 */
474 sc->sc_link.adapter_softc = sc;
475 sc->sc_link.adapter_target = sc->sc_initiator;
476 sc->sc_link.adapter = &spc_switch;
477 sc->sc_link.device = &spc_dev;
478 sc->sc_link.openings = 2;
479
480 printf("\n");
481
482 config_found(self, &sc->sc_link, spcprint);
483 }
484
485 void
486 spc_reset(sc)
487 struct spc_softc *sc;
488 {
489 sc->sc_initiator = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
490 /*
491 * Disable interrupts then reset the FUJITSU chip.
492 */
493 SCTL = SCTL_DISABLE | SCTL_CTRLRST;
494 SCMD = 0;
495 PCTL = 0;
496 TEMP = 0;
497 TCH = 0;
498 TCM = 0;
499 TCL = 0;
500 INTS = 0;
501 SCTL = SCTL_DISABLE | SCTL_ABRT_ENAB | SCTL_PARITY_ENAB | SCTL_RESEL_ENAB;
502 BDID = sc->sc_initiator;
503 delay(400);
504 SCTL &= ~SCTL_DISABLE;
505 }
506
507 /*
508 * Pull the SCSI RST line for 500us.
509 */
510 void
511 spc_scsi_reset(sc)
512 struct spc_softc *sc;
513 {
514
515 SCMD |= SCMD_RST;
516 delay(500);
517 SCMD &= ~SCMD_RST;
518 delay(50);
519 }
520
521 /*
522 * Initialize spc SCSI driver.
523 */
524 void
525 spc_init(sc)
526 struct spc_softc *sc;
527 {
528 struct spc_acb *acb;
529 int r;
530
531 spc_reset(sc);
532 spc_scsi_reset(sc);
533 spc_reset(sc);
534
535 if (sc->sc_state == SPC_INIT) {
536 /* First time through; initialize. */
537 TAILQ_INIT(&sc->ready_list);
538 TAILQ_INIT(&sc->nexus_list);
539 TAILQ_INIT(&sc->free_list);
540 sc->sc_nexus = NULL;
541 acb = sc->sc_acb;
542 bzero(acb, sizeof(sc->sc_acb));
543 for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
544 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
545 acb++;
546 }
547 bzero(&sc->sc_tinfo, sizeof(sc->sc_tinfo));
548 } else {
549 /* Cancel any active commands. */
550 sc->sc_state = SPC_CLEANING;
551 if ((acb = sc->sc_nexus) != NULL) {
552 acb->xs->error = XS_DRIVER_STUFFUP;
553 untimeout(spc_timeout, acb);
554 spc_done(sc, acb);
555 }
556 while (acb = sc->nexus_list.tqh_first) {
557 acb->xs->error = XS_DRIVER_STUFFUP;
558 untimeout(spc_timeout, acb);
559 spc_done(sc, acb);
560 }
561 }
562
563 sc->sc_prevphase = PH_INVALID;
564 for (r = 0; r < 8; r++) {
565 struct spc_tinfo *ti = &sc->sc_tinfo[r];
566
567 ti->flags = 0;
568 #if SPC_USE_SYNCHRONOUS
569 ti->flags |= DO_SYNC;
570 ti->period = sc->sc_minsync;
571 ti->offset = SPC_SYNC_REQ_ACK_OFS;
572 #else
573 ti->period = ti->offset = 0;
574 #endif
575 #if SPC_USE_WIDE
576 ti->flags |= DO_WIDE;
577 ti->width = SPC_MAX_WIDTH;
578 #else
579 ti->width = 0;
580 #endif
581 }
582
583 sc->sc_state = SPC_IDLE;
584 SCTL |= SCTL_INTR_ENAB;
585 }
586
587 void
588 spc_free_acb(sc, acb, flags)
589 struct spc_softc *sc;
590 struct spc_acb *acb;
591 int flags;
592 {
593 int s;
594
595 s = splbio();
596
597 acb->flags = 0;
598 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
599
600 /*
601 * If there were none, wake anybody waiting for one to come free,
602 * starting with queued entries.
603 */
604 if (acb->chain.tqe_next == 0)
605 wakeup(&sc->free_list);
606
607 splx(s);
608 }
609
610 struct spc_acb *
611 spc_get_acb(sc, flags)
612 struct spc_softc *sc;
613 int flags;
614 {
615 struct spc_acb *acb;
616 int s;
617
618 s = splbio();
619
620 while ((acb = sc->free_list.tqh_first) == NULL &&
621 (flags & SCSI_NOSLEEP) == 0)
622 tsleep(&sc->free_list, PRIBIO, "spcacb", 0);
623 if (acb) {
624 TAILQ_REMOVE(&sc->free_list, acb, chain);
625 acb->flags |= ACB_ALLOC;
626 }
627
628 splx(s);
629 return acb;
630 }
631
632 /*
634 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
635 */
636
637 /*
638 * Expected sequence:
639 * 1) Command inserted into ready list
640 * 2) Command selected for execution
641 * 3) Command won arbitration and has selected target device
642 * 4) Send message out (identify message, eventually also sync.negotiations)
643 * 5) Send command
644 * 5a) Receive disconnect message, disconnect.
645 * 5b) Reselected by target
646 * 5c) Receive identify message from target.
647 * 6) Send or receive data
648 * 7) Receive status
649 * 8) Receive message (command complete etc.)
650 * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
651 * Repeat 2-8 (no disconnects please...)
652 */
653
654 /*
655 * Start a SCSI-command
656 * This function is called by the higher level SCSI-driver to queue/run
657 * SCSI-commands.
658 */
659 int
660 spc_scsi_cmd(xs)
661 struct scsi_xfer *xs;
662 {
663 struct scsi_link *sc_link = xs->sc_link;
664 struct spc_softc *sc = sc_link->adapter_softc;
665 struct spc_acb *acb;
666 int s, flags;
667
668 SPC_TRACE(("spc_scsi_cmd "));
669 SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
670 sc_link->target));
671
672 flags = xs->flags;
673 if ((acb = spc_get_acb(sc, flags)) == NULL) {
674 xs->error = XS_DRIVER_STUFFUP;
675 return TRY_AGAIN_LATER;
676 }
677
678 /* Initialize acb */
679 acb->xs = xs;
680 acb->timeout = xs->timeout;
681
682 if (xs->flags & SCSI_RESET) {
683 acb->flags |= ACB_RESET;
684 acb->scsi_cmd_length = 0;
685 acb->data_length = 0;
686 } else {
687 bcopy(xs->cmd, &acb->scsi_cmd, xs->cmdlen);
688 #if 1
689 acb->scsi_cmd.bytes[0] |= sc_link->lun << 5; /* XXX? */
690 #endif
691 acb->scsi_cmd_length = xs->cmdlen;
692 acb->data_addr = xs->data;
693 acb->data_length = xs->datalen;
694 }
695 acb->target_stat = 0;
696
697 s = splbio();
698
699 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
700 /*
701 * $B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k(B
702 */
703 if (sc->sc_state == SPC_IDLE)
704 spc_sched(sc);
705 /*
706 * $BAw?.$K@.8y$7$?$i!"$9$0$K%j%?!<%s$9$k$+D4$Y$k(B
707 * $B$9$0%j%?!<%s$9$k$J$i(B SUCCESSFULLY_QUEUED $B$rJV$9(B
708 */
709
710 splx(s);
711
712 if ((flags & SCSI_POLL) == 0)
713 return SUCCESSFULLY_QUEUED;
714
715 /* Not allowed to use interrupts, use polling instead */
716 if (spc_poll(sc, xs, acb->timeout)) {
717 spc_timeout(acb);
718 if (spc_poll(sc, xs, acb->timeout))
719 spc_timeout(acb);
720 }
721 return COMPLETE;
722 }
723
724 /*
725 * Adjust transfer size in buffer structure
726 */
727 void
728 spc_minphys(bp)
729 struct buf *bp;
730 {
731
732 SPC_TRACE(("spc_minphys "));
733 minphys(bp);
734 }
735
736 /*
737 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
738 */
739 int
740 spc_poll(sc, xs, count)
741 struct spc_softc *sc;
742 struct scsi_xfer *xs;
743 int count;
744 {
745
746 SPC_TRACE(("spc_poll "));
747 while (count) {
748 /*
749 * If we had interrupts enabled, would we
750 * have got an interrupt?
751 */
752 if (INTS != 0)
753 spcintr(sc->sc_dev.dv_unit);
754 if ((xs->flags & ITSDONE) != 0)
755 return 0;
756 delay(1000);
757 count--;
758 }
759 return 1;
760 }
761
762 /*
764 * LOW LEVEL SCSI UTILITIES
765 */
766
767 integrate void
768 spc_sched_msgout(sc, m)
769 struct spc_softc *sc;
770 u_char m;
771 {
772 if (sc->sc_msgpriq == 0)
773 SCMD = SCMD_SET_ATN;
774 sc->sc_msgpriq |= m;
775 }
776
777 /*
778 * Set synchronous transfer offset and period.
779 */
780 integrate void
781 spc_setsync(sc, ti)
782 struct spc_softc *sc;
783 struct spc_tinfo *ti;
784 {
785 #if SPC_USE_SYNCHRONOUS
786
787 if (ti->offset != 0)
788 TMOD =
789 ((ti->period * sc->sc_freq) / 250 - 2) << 4 | ti->offset);
790 else
791 TMOD = 0;
792 #endif
793 }
794
795 /*
796 * Start a selection. This is used by spc_sched() to select an idle target,
797 * and by spc_done() to immediately reselect a target to get sense information.
798 */
799 void
800 spc_select(sc, acb)
801 struct spc_softc *sc;
802 struct spc_acb *acb;
803 {
804 struct scsi_link *sc_link = acb->xs->sc_link;
805 int target = sc_link->target;
806 struct spc_tinfo *ti = &sc->sc_tinfo[target];
807
808 spc_setsync(sc, ti);
809
810 #if 0
811 SCMD = SCMD_SET_ATN;
812 #endif
813 PCTL = 0;
814 TEMP = (1 << sc->sc_initiator) | (1 << target);
815 /*
816 * BSY $B$K$h$k1~EzBT$A;~4V@_Dj(B ($B@_Dj;~4V$r2a$.$k$H(B selection timeout)
817 * 0 $B$K$9$k$HL58BBT$A(B (x68k $B$G$O(B Tclf == 200ns)
818 * T = (X * 256 + 15) * Tclf * 2 $B$J$N$G(B... 256ms $BBT$D$H$9$k$H(B
819 * 128000ns/200ns = X * 256 + 15
820 * 640 - 15 = X * 256
821 * X = 625 / 256
822 * X = 2 + 113 / 256
823 * $B$J$N$G(B tch $B$K(B 2, tcm $B$K(B 113 $B$rBeF~!#(B($B$$$$$N$+(B?)
824 */
825 TCH = 2;
826 TCM = 113;
827 /* BSY $B$H(B SEL $B$,(B 0 $B$K$J$C$F$+$i%U%'!<%:3+;O$^$G$N;~4V(B */
828 TCL = 3;
829 SCMD = SCMD_SELECT;
830
831 sc->sc_state = SPC_SELECTING;
832 }
833
834 int
835 spc_reselect(sc, message)
836 struct spc_softc *sc;
837 u_char message;
838 {
839 u_char selid, target, lun;
840 struct spc_acb *acb;
841 struct scsi_link *sc_link;
842 struct spc_tinfo *ti;
843
844 /*
845 * The SCSI chip made a snapshot of the data bus while the reselection
846 * was being negotiated. This enables us to determine which target did
847 * the reselect.
848 */
849 selid = sc->sc_selid & ~(1 << sc->sc_initiator);
850 if (selid & (selid - 1)) {
851 printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
852 sc->sc_dev.dv_xname, selid);
853 SPC_BREAK();
854 goto reset;
855 }
856
857 /*
858 * Search wait queue for disconnected cmd
859 * The list should be short, so I haven't bothered with
860 * any more sophisticated structures than a simple
861 * singly linked list.
862 */
863 target = ffs(selid) - 1;
864 lun = message & 0x07;
865 for (acb = sc->nexus_list.tqh_first; acb != NULL;
866 acb = acb->chain.tqe_next) {
867 sc_link = acb->xs->sc_link;
868 if (sc_link->target == target && sc_link->lun == lun)
869 break;
870 }
871 if (acb == NULL) {
872 printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
873 sc->sc_dev.dv_xname, target, lun);
874 SPC_BREAK();
875 goto abort;
876 }
877
878 /* Make this nexus active again. */
879 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
880 sc->sc_state = SPC_CONNECTED;
881 sc->sc_nexus = acb;
882 ti = &sc->sc_tinfo[target];
883 ti->lubusy |= (1 << lun);
884 spc_setsync(sc, ti);
885
886 if (acb->flags & ACB_RESET)
887 spc_sched_msgout(sc, SEND_DEV_RESET);
888 else if (acb->flags & ACB_ABORT)
889 spc_sched_msgout(sc, SEND_ABORT);
890
891 /* Do an implicit RESTORE POINTERS. */
892 sc->sc_dp = acb->data_addr;
893 sc->sc_dleft = acb->data_length;
894 sc->sc_cp = (u_char *)&acb->scsi_cmd;
895 sc->sc_cleft = acb->scsi_cmd_length;
896
897 return (0);
898
899 reset:
900 spc_sched_msgout(sc, SEND_DEV_RESET);
901 return (1);
902
903 abort:
904 spc_sched_msgout(sc, SEND_ABORT);
905 return (1);
906 }
907
908 /*
910 * Schedule a SCSI operation. This has now been pulled out of the interrupt
911 * handler so that we may call it from spc_scsi_cmd and spc_done. This may
912 * save us an unecessary interrupt just to get things going. Should only be
913 * called when state == SPC_IDLE and at bio pl.
914 */
915 void
916 spc_sched(sc)
917 register struct spc_softc *sc;
918 {
919 struct spc_acb *acb;
920 struct scsi_link *sc_link;
921 struct spc_tinfo *ti;
922
923 /*
924 * Find first acb in ready queue that is for a target/lunit pair that
925 * is not busy.
926 */
927 for (acb = sc->ready_list.tqh_first; acb != NULL;
928 acb = acb->chain.tqe_next) {
929 sc_link = acb->xs->sc_link;
930 ti = &sc->sc_tinfo[sc_link->target];
931 if ((ti->lubusy & (1 << sc_link->lun)) == 0) {
932 SPC_MISC(("selecting %d:%d ",
933 sc_link->target, sc_link->lun));
934 TAILQ_REMOVE(&sc->ready_list, acb, chain);
935 sc->sc_nexus = acb;
936 spc_select(sc, acb);
937 return;
938 } else
939 SPC_MISC(("%d:%d busy\n",
940 sc_link->target, sc_link->lun));
941 }
942 SPC_MISC(("idle "));
943 /* Nothing to start; just enable reselections and wait. */
944 }
945
946 void
948 spc_sense(sc, acb)
949 struct spc_softc *sc;
950 struct spc_acb *acb;
951 {
952 struct scsi_xfer *xs = acb->xs;
953 struct scsi_link *sc_link = xs->sc_link;
954 struct spc_tinfo *ti = &sc->sc_tinfo[sc_link->target];
955 struct scsi_sense *ss = (void *)&acb->scsi_cmd;
956
957 SPC_MISC(("requesting sense "));
958 /* Next, setup a request sense command block */
959 bzero(ss, sizeof(*ss));
960 ss->opcode = REQUEST_SENSE;
961 ss->byte2 = sc_link->lun << 5;
962 ss->length = sizeof(struct scsi_sense_data);
963 acb->scsi_cmd_length = sizeof(*ss);
964 acb->data_addr = (char *)&xs->sense;
965 acb->data_length = sizeof(struct scsi_sense_data);
966 acb->flags |= ACB_SENSE;
967 ti->senses++;
968 if (acb->flags & ACB_NEXUS)
969 ti->lubusy &= ~(1 << sc_link->lun);
970 if (acb == sc->sc_nexus) {
971 spc_select(sc, acb);
972 } else {
973 spc_dequeue(sc, acb);
974 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
975 if (sc->sc_state == SPC_IDLE)
976 spc_sched(sc);
977 }
978 }
979
980 /*
981 * POST PROCESSING OF SCSI_CMD (usually current)
982 */
983 void
984 spc_done(sc, acb)
985 struct spc_softc *sc;
986 struct spc_acb *acb;
987 {
988 struct scsi_xfer *xs = acb->xs;
989 struct scsi_link *sc_link = xs->sc_link;
990 struct spc_tinfo *ti = &sc->sc_tinfo[sc_link->target];
991
992 SPC_TRACE(("spc_done "));
993
994 /*
995 * Now, if we've come here with no error code, i.e. we've kept the
996 * initial XS_NOERROR, and the status code signals that we should
997 * check sense, we'll need to set up a request sense cmd block and
998 * push the command back into the ready queue *before* any other
999 * commands for this target/lunit, else we lose the sense info.
1000 * We don't support chk sense conditions for the request sense cmd.
1001 */
1002 if (xs->error == XS_NOERROR) {
1003 if (acb->flags & ACB_ABORT) {
1004 xs->error = XS_DRIVER_STUFFUP;
1005 } else if (acb->flags & ACB_SENSE) {
1006 xs->error = XS_SENSE;
1007 } else if (acb->target_stat == SCSI_CHECK) {
1008 /* First, save the return values */
1009 xs->resid = acb->data_length;
1010 xs->status = acb->target_stat;
1011 spc_sense(sc, acb);
1012 return;
1013 } else {
1014 xs->resid = acb->data_length;
1015 }
1016 }
1017
1018 xs->flags |= ITSDONE;
1019
1020 #if SPC_DEBUG
1021 if ((spc_debug & SPC_SHOWMISC) != 0) {
1022 if (xs->resid != 0)
1023 printf("resid=%d ", xs->resid);
1024 if (xs->error == XS_SENSE)
1025 printf("sense=0x%02x\n", xs->sense.error_code);
1026 else
1027 printf("error=%d\n", xs->error);
1028 }
1029 #endif
1030
1031 /*
1032 * Remove the ACB from whatever queue it happens to be on.
1033 */
1034 if (acb->flags & ACB_NEXUS)
1035 ti->lubusy &= ~(1 << sc_link->lun);
1036 if (acb == sc->sc_nexus) {
1037 sc->sc_nexus = NULL;
1038 sc->sc_state = SPC_IDLE;
1039 spc_sched(sc);
1040 } else
1041 spc_dequeue(sc, acb);
1042
1043 spc_free_acb(sc, acb, xs->flags);
1044 ti->cmds++;
1045 scsi_done(xs);
1046 }
1047
1048 void
1049 spc_dequeue(sc, acb)
1050 struct spc_softc *sc;
1051 struct spc_acb *acb;
1052 {
1053
1054 if (acb->flags & ACB_NEXUS) {
1055 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1056 } else {
1057 TAILQ_REMOVE(&sc->ready_list, acb, chain);
1058 }
1059 }
1060
1061 /*
1063 * INTERRUPT/PROTOCOL ENGINE
1064 */
1065
1066 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
1067 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
1068 #define ISEXTMSG(m) ((m) == 0x01)
1069
1070 /*
1071 * Precondition:
1072 * The SCSI bus is already in the MSGI phase and there is a message byte
1073 * on the bus, along with an asserted REQ signal.
1074 */
1075 void
1076 spc_msgin(sc)
1077 register struct spc_softc *sc;
1078 {
1079 int n;
1080
1081 SPC_TRACE(("spc_msgin "));
1082
1083 if (sc->sc_prevphase == PH_MSGIN) {
1084 /* This is a continuation of the previous message. */
1085 n = sc->sc_imp - sc->sc_imess;
1086 goto nextbyte;
1087 }
1088
1089 /* This is a new MESSAGE IN phase. Clean up our state. */
1090 sc->sc_flags &= ~SPC_DROP_MSGIN;
1091
1092 nextmsg:
1093 n = 0;
1094 sc->sc_imp = &sc->sc_imess[n];
1095
1096 nextbyte:
1097 /*
1098 * Read a whole message, but don't ack the last byte. If we reject the
1099 * message, we have to assert ATN during the message transfer phase
1100 * itself.
1101 */
1102 for (;;) {
1103 #if 0
1104 for (;;) {
1105 if ((PSNS & PSNS_REQ) != 0)
1106 break;
1107 /* Wait for REQINIT. XXX Need timeout. */
1108 }
1109 #endif
1110 if (INTS != 0) {
1111 /*
1112 * Target left MESSAGE IN, probably because it
1113 * a) noticed our ATN signal, or
1114 * b) ran out of messages.
1115 */
1116 goto out;
1117 }
1118
1119 /* If parity error, just dump everything on the floor. */
1120 if ((SERR & (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) {
1121 sc->sc_flags |= SPC_DROP_MSGIN;
1122 spc_sched_msgout(sc, SEND_PARITY_ERROR);
1123 }
1124
1125 /* send TRANSFER command. */
1126 TCH = 0;
1127 TCM = 0;
1128 TCL = 1;
1129 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1130 SCMD = SCMD_XFR; /* | SCMD_PROG_XFR */
1131 for (;;) {
1132 /*if ((SSTS & SSTS_BUSY) != 0 && (SSTS & SSTS_DREG_EMPTY) != 0)*/
1133 if ((SSTS & SSTS_DREG_EMPTY) == 0)
1134 break;
1135 if (INTS != 0)
1136 goto out;
1137 }
1138
1139 /* Gather incoming message bytes if needed. */
1140 if ((sc->sc_flags & SPC_DROP_MSGIN) == 0) {
1141 if (n >= SPC_MAX_MSG_LEN) {
1142 (void) DREG;
1143 sc->sc_flags |= SPC_DROP_MSGIN;
1144 spc_sched_msgout(sc, SEND_REJECT);
1145 } else {
1146 *sc->sc_imp++ = DREG;
1147 n++;
1148 /*
1149 * This testing is suboptimal, but most
1150 * messages will be of the one byte variety, so
1151 * it should not affect performance
1152 * significantly.
1153 */
1154 if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
1155 break;
1156 if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
1157 break;
1158 if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
1159 n == sc->sc_imess[1] + 2)
1160 break;
1161 }
1162 } else
1163 (void) DREG;
1164
1165 /*
1166 * If we reach this spot we're either:
1167 * a) in the middle of a multi-byte message, or
1168 * b) dropping bytes.
1169 */
1170
1171 #if 0
1172 /* Ack the last byte read. */
1173 /*(void) DREG;*/
1174 while ((PSNS & ACKI) != 0)
1175 ;
1176 #endif
1177 }
1178
1179 SPC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1180
1181 /* We now have a complete message. Parse it. */
1182 switch (sc->sc_state) {
1183 struct spc_acb *acb;
1184 struct scsi_link *sc_link;
1185 struct spc_tinfo *ti;
1186
1187 case SPC_CONNECTED:
1188 SPC_ASSERT(sc->sc_nexus != NULL);
1189 acb = sc->sc_nexus;
1190 ti = &sc->sc_tinfo[acb->xs->sc_link->target];
1191
1192 switch (sc->sc_imess[0]) {
1193 case MSG_CMDCOMPLETE:
1194 if (sc->sc_dleft < 0) {
1195 sc_link = acb->xs->sc_link;
1196 printf("%s: %d extra bytes from %d:%d\n",
1197 sc->sc_dev.dv_xname, -sc->sc_dleft,
1198 sc_link->target, sc_link->lun);
1199 acb->data_length = 0;
1200 }
1201 acb->xs->resid = acb->data_length = sc->sc_dleft;
1202 sc->sc_state = SPC_CMDCOMPLETE;
1203 break;
1204
1205 case MSG_PARITY_ERROR:
1206 /* Resend the last message. */
1207 spc_sched_msgout(sc, sc->sc_lastmsg);
1208 break;
1209
1210 case MSG_MESSAGE_REJECT:
1211 SPC_MISC(("message rejected %02x ", sc->sc_lastmsg));
1212 switch (sc->sc_lastmsg) {
1213 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
1214 case SEND_IDENTIFY:
1215 ti->flags &= ~(DO_SYNC | DO_WIDE);
1216 ti->period = ti->offset = 0;
1217 spc_setsync(sc, ti);
1218 ti->width = 0;
1219 break;
1220 #endif
1221 #if SPC_USE_SYNCHRONOUS
1222 case SEND_SDTR:
1223 ti->flags &= ~DO_SYNC;
1224 ti->period = ti->offset = 0;
1225 spc_setsync(sc, ti);
1226 break;
1227 #endif
1228 #if SPC_USE_WIDE
1229 case SEND_WDTR:
1230 ti->flags &= ~DO_WIDE;
1231 ti->width = 0;
1232 break;
1233 #endif
1234 case SEND_INIT_DET_ERR:
1235 spc_sched_msgout(sc, SEND_ABORT);
1236 break;
1237 }
1238 break;
1239
1240 case MSG_NOOP:
1241 break;
1242
1243 case MSG_DISCONNECT:
1244 ti->dconns++;
1245 sc->sc_state = SPC_DISCONNECT;
1246 break;
1247
1248 case MSG_SAVEDATAPOINTER:
1249 acb->data_addr = sc->sc_dp;
1250 acb->data_length = sc->sc_dleft;
1251 break;
1252
1253 case MSG_RESTOREPOINTERS:
1254 sc->sc_dp = acb->data_addr;
1255 sc->sc_dleft = acb->data_length;
1256 sc->sc_cp = (u_char *)&acb->scsi_cmd;
1257 sc->sc_cleft = acb->scsi_cmd_length;
1258 break;
1259
1260 case MSG_EXTENDED:
1261 switch (sc->sc_imess[2]) {
1262 #if SPC_USE_SYNCHRONOUS
1263 case MSG_EXT_SDTR:
1264 if (sc->sc_imess[1] != 3)
1265 goto reject;
1266 ti->period = sc->sc_imess[3];
1267 ti->offset = sc->sc_imess[4];
1268 ti->flags &= ~DO_SYNC;
1269 if (ti->offset == 0) {
1270 } else if (ti->period < sc->sc_minsync ||
1271 ti->period > sc->sc_maxsync ||
1272 ti->offset > 8) {
1273 ti->period = ti->offset = 0;
1274 spc_sched_msgout(sc, SEND_SDTR);
1275 } else {
1276 sc_print_addr(acb->xs->sc_link);
1277 printf("sync, offset %d, period %dnsec\n",
1278 ti->offset, ti->period * 4);
1279 }
1280 spc_setsync(sc, ti);
1281 break;
1282 #endif
1283
1284 #if SPC_USE_WIDE
1285 case MSG_EXT_WDTR:
1286 if (sc->sc_imess[1] != 2)
1287 goto reject;
1288 ti->width = sc->sc_imess[3];
1289 ti->flags &= ~DO_WIDE;
1290 if (ti->width == 0) {
1291 } else if (ti->width > SPC_MAX_WIDTH) {
1292 ti->width = 0;
1293 spc_sched_msgout(sc, SEND_WDTR);
1294 } else {
1295 sc_print_addr(acb->xs->sc_link);
1296 printf("wide, width %d\n",
1297 1 << (3 + ti->width));
1298 }
1299 break;
1300 #endif
1301
1302 default:
1303 printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
1304 sc->sc_dev.dv_xname);
1305 SPC_BREAK();
1306 goto reject;
1307 }
1308 break;
1309
1310 default:
1311 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1312 sc->sc_dev.dv_xname);
1313 SPC_BREAK();
1314 reject:
1315 spc_sched_msgout(sc, SEND_REJECT);
1316 break;
1317 }
1318 break;
1319
1320 case SPC_RESELECTED:
1321 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1322 printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
1323 sc->sc_dev.dv_xname);
1324 SPC_BREAK();
1325 goto reset;
1326 }
1327
1328 (void) spc_reselect(sc, sc->sc_imess[0]);
1329 break;
1330
1331 default:
1332 printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
1333 sc->sc_dev.dv_xname);
1334 SPC_BREAK();
1335 reset:
1336 spc_sched_msgout(sc, SEND_DEV_RESET);
1337 break;
1338
1339 abort:
1340 spc_sched_msgout(sc, SEND_ABORT);
1341 break;
1342 }
1343
1344 /* Ack the last message byte. */
1345 #if 0 /* XXX? */
1346 (void) DREG;
1347 while ((PSNS & ACKI) != 0)
1348 ;
1349 #endif
1350
1351 /* Go get the next message, if any. */
1352 goto nextmsg;
1353
1354 out:
1355 SCMD = SCMD_RST_ACK;
1356 SPC_MISC(("n=%d imess=0x%02x ", n, sc->sc_imess[0]));
1357 }
1358
1359 /*
1360 * Send the highest priority, scheduled message.
1361 */
1362 void
1363 spc_msgout(sc)
1364 register struct spc_softc *sc;
1365 {
1366 struct spc_tinfo *ti;
1367 int n;
1368
1369 SPC_TRACE(("spc_msgout "));
1370
1371 if (sc->sc_prevphase == PH_MSGOUT) {
1372 if (sc->sc_omp == sc->sc_omess) {
1373 /*
1374 * This is a retransmission.
1375 *
1376 * We get here if the target stayed in MESSAGE OUT
1377 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1378 * that all of the previously transmitted messages must
1379 * be sent again, in the same order. Therefore, we
1380 * requeue all the previously transmitted messages, and
1381 * start again from the top. Our simple priority
1382 * scheme keeps the messages in the right order.
1383 */
1384 SPC_MISC(("retransmitting "));
1385 sc->sc_msgpriq |= sc->sc_msgoutq;
1386 /*
1387 * Set ATN. If we're just sending a trivial 1-byte
1388 * message, we'll clear ATN later on anyway.
1389 */
1390 SCMD = SCMD_SET_ATN; /* XXX? */
1391 } else {
1392 /* This is a continuation of the previous message. */
1393 n = sc->sc_omp - sc->sc_omess;
1394 goto nextbyte;
1395 }
1396 }
1397
1398 /* No messages transmitted so far. */
1399 sc->sc_msgoutq = 0;
1400 sc->sc_lastmsg = 0;
1401
1402 nextmsg:
1403 /* Pick up highest priority message. */
1404 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1405 sc->sc_msgpriq &= ~sc->sc_currmsg;
1406 sc->sc_msgoutq |= sc->sc_currmsg;
1407
1408 /* Build the outgoing message data. */
1409 switch (sc->sc_currmsg) {
1410 case SEND_IDENTIFY:
1411 SPC_ASSERT(sc->sc_nexus != NULL);
1412 sc->sc_omess[0] =
1413 MSG_IDENTIFY(sc->sc_nexus->xs->sc_link->lun, 1);
1414 n = 1;
1415 break;
1416
1417 #if SPC_USE_SYNCHRONOUS
1418 case SEND_SDTR:
1419 SPC_ASSERT(sc->sc_nexus != NULL);
1420 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
1421 sc->sc_omess[4] = MSG_EXTENDED;
1422 sc->sc_omess[3] = 3;
1423 sc->sc_omess[2] = MSG_EXT_SDTR;
1424 sc->sc_omess[1] = ti->period >> 2;
1425 sc->sc_omess[0] = ti->offset;
1426 n = 5;
1427 break;
1428 #endif
1429
1430 #if SPC_USE_WIDE
1431 case SEND_WDTR:
1432 SPC_ASSERT(sc->sc_nexus != NULL);
1433 ti = &sc->sc_tinfo[sc->sc_nexus->xs->sc_link->target];
1434 sc->sc_omess[3] = MSG_EXTENDED;
1435 sc->sc_omess[2] = 2;
1436 sc->sc_omess[1] = MSG_EXT_WDTR;
1437 sc->sc_omess[0] = ti->width;
1438 n = 4;
1439 break;
1440 #endif
1441
1442 case SEND_DEV_RESET:
1443 sc->sc_flags |= SPC_ABORTING;
1444 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1445 n = 1;
1446 break;
1447
1448 case SEND_REJECT:
1449 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1450 n = 1;
1451 break;
1452
1453 case SEND_PARITY_ERROR:
1454 sc->sc_omess[0] = MSG_PARITY_ERROR;
1455 n = 1;
1456 break;
1457
1458 case SEND_INIT_DET_ERR:
1459 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1460 n = 1;
1461 break;
1462
1463 case SEND_ABORT:
1464 sc->sc_flags |= SPC_ABORTING;
1465 sc->sc_omess[0] = MSG_ABORT;
1466 n = 1;
1467 break;
1468
1469 default:
1470 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1471 sc->sc_dev.dv_xname);
1472 SPC_BREAK();
1473 sc->sc_omess[0] = MSG_NOOP;
1474 n = 1;
1475 break;
1476 }
1477 sc->sc_omp = &sc->sc_omess[n];
1478
1479 nextbyte:
1480 /* Send message bytes. */
1481 /* send TRANSFER command. */
1482 TCH = n >> 16;
1483 TCM = n >> 8;
1484 TCL = n;
1485 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1486 SCMD = SCMD_XFR; /* | SCMD_PROG_XFR */
1487 for (;;) {
1488 if ((SSTS & SSTS_BUSY) != 0)
1489 break;
1490 if (INTS != 0)
1491 goto out;
1492 }
1493 for (;;) {
1494 #if 0
1495 for (;;) {
1496 if ((PSNS & PSNS_REQ) != 0)
1497 break;
1498 /* Wait for REQINIT. XXX Need timeout. */
1499 }
1500 #endif
1501 if (INTS != 0) {
1502 /*
1503 * Target left MESSAGE OUT, possibly to reject
1504 * our message.
1505 *
1506 * If this is the last message being sent, then we
1507 * deassert ATN, since either the target is going to
1508 * ignore this message, or it's going to ask for a
1509 * retransmission via MESSAGE PARITY ERROR (in which
1510 * case we reassert ATN anyway).
1511 */
1512 #if 0
1513 if (sc->sc_msgpriq == 0)
1514 SCMD = SCMD_RST_ATN;
1515 #endif
1516 goto out;
1517 }
1518
1519 #if 0
1520 /* Clear ATN before last byte if this is the last message. */
1521 if (n == 1 && sc->sc_msgpriq == 0)
1522 SCMD = SCMD_RST_ATN;
1523 #endif
1524
1525 while ((SSTS & SSTS_DREG_FULL) != 0)
1526 ;
1527 /* Send message byte. */
1528 DREG = *--sc->sc_omp;
1529 --n;
1530 /* Keep track of the last message we've sent any bytes of. */
1531 sc->sc_lastmsg = sc->sc_currmsg;
1532 #if 0
1533 /* Wait for ACK to be negated. XXX Need timeout. */
1534 while ((PSNS & ACKI) != 0)
1535 ;
1536 #endif
1537
1538 if (n == 0)
1539 break;
1540 }
1541
1542 /* We get here only if the entire message has been transmitted. */
1543 if (sc->sc_msgpriq != 0) {
1544 /* There are more outgoing messages. */
1545 goto nextmsg;
1546 }
1547
1548 /*
1549 * The last message has been transmitted. We need to remember the last
1550 * message transmitted (in case the target switches to MESSAGE IN phase
1551 * and sends a MESSAGE REJECT), and the list of messages transmitted
1552 * this time around (in case the target stays in MESSAGE OUT phase to
1553 * request a retransmit).
1554 */
1555
1556 out:
1557 /* Disable REQ/ACK protocol. */
1558 }
1559
1560 /*
1562 * This new revision has been optimized (I tried) to make the common case fast,
1563 * and the rarer cases (as a result) somewhat more comlex
1564 */
1565 int
1566 spc_dataout_pio(sc, p, n)
1567 register struct spc_softc *sc;
1568 u_char *p;
1569 int n;
1570 {
1571 register u_char intstat = 0;
1572 int out = 0;
1573 #define DOUTAMOUNT 8 /* Full FIFO */
1574
1575 /* send TRANSFER command. */
1576 TCH = n >> 16;
1577 TCM = n >> 8;
1578 TCL = n;
1579 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1580 SCMD = SCMD_XFR;
1581 for (;;) {
1582 if ((SSTS & SSTS_BUSY) != 0)
1583 break;
1584 if (INTS != 0)
1585 break;
1586 }
1587
1588 /*
1589 * I have tried to make the main loop as tight as possible. This
1590 * means that some of the code following the loop is a bit more
1591 * complex than otherwise.
1592 */
1593 while (n > 0) {
1594 int xfer;
1595
1596 for (;;) {
1597 intstat = INTS;
1598 /* $B%P%C%U%!$,6u$K$J$k$^$GBT$D(B */
1599 if ((SSTS & SSTS_DREG_EMPTY) != 0)
1600 break;
1601 /* $B$?$@$73d$j9~$_$,F~$C$F$-$?$iH4$1$k(B */
1602 if (intstat != 0)
1603 goto phasechange;
1604 }
1605
1606 xfer = min(DOUTAMOUNT, n);
1607
1608 SPC_MISC(("%d> ", xfer));
1609
1610 n -= xfer;
1611 out += xfer;
1612
1613 while (xfer-- > 0) {
1614 DREG = *p++;
1615 }
1616 }
1617
1618 if (out == 0) {
1619 for (;;) {
1620 if (INTS != 0)
1621 break;
1622 }
1623 SPC_MISC(("extra data "));
1624 } else {
1625 /* See the bytes off chip */
1626 for (;;) {
1627 /* $B%P%C%U%!$,6u$K$J$k$^$GBT$D(B */
1628 if ((SSTS & SSTS_DREG_EMPTY) != 0)
1629 break;
1630 intstat = INTS;
1631 /* $B$?$@$73d$j9~$_$,F~$C$F$-$?$iH4$1$k(B */
1632 if (intstat != 0)
1633 goto phasechange;
1634 }
1635 }
1636
1637 phasechange:
1638 /* Stop the FIFO data path. */
1639
1640 if (intstat != 0) {
1641 /* Some sort of phase change. */
1642 int amount;
1643
1644 amount = (TCH << 16) | (TCM << 8) | TCL;
1645 if (amount > 0) {
1646 out -= amount;
1647 SPC_MISC(("+%d ", amount));
1648 }
1649 }
1650 /* Turn on ENREQINIT again. */
1651
1652 return out;
1653 }
1654
1655 /*
1657 * For now, uses a pretty dumb algorithm, hangs around until all data has been
1658 * transferred. This, is OK for fast targets, but not so smart for slow
1659 * targets which don't disconnect or for huge transfers.
1660 */
1661 int
1662 spc_datain_pio(sc, p, n)
1663 register struct spc_softc *sc;
1664 u_char *p;
1665 int n;
1666 {
1667 register u_short intstat;
1668 int in = 0;
1669 #define DINAMOUNT 8 /* Full FIFO */
1670
1671 /* send TRANSFER command. */
1672 TCH = n >> 16;
1673 TCM = n >> 8;
1674 TCL = n;
1675 PCTL = sc->sc_phase | PCTL_BFINT_ENAB;
1676 SCMD = SCMD_XFR;
1677 for (;;) {
1678 if ((SSTS & SSTS_BUSY) != 0)
1679 break;
1680 if (INTS != 0)
1681 goto phasechange;
1682 }
1683
1684 /*
1685 * We leave this loop if one or more of the following is true:
1686 * a) phase != PH_DATAIN && FIFOs are empty
1687 * b) reset has occurred or busfree is detected.
1688 */
1689 while (n > 0) {
1690 int xfer;
1691
1692 #define INTSMASK 0xff
1693 /* Wait for fifo half full or phase mismatch */
1694 for (;;) {
1695 intstat = (SSTS << 8) | INTS;
1696 if ((intstat & (INTSMASK | (SSTS_DREG_FULL << 8))) != 0)
1697 break;
1698 if ((intstat & (SSTS_DREG_EMPTY << 8)) == 0)
1699 break;
1700 }
1701
1702 #if 1
1703 if ((intstat & INTSMASK) != 0)
1704 goto phasechange;
1705 #else
1706 if ((intstat & INTSMASK) != 0 &&
1707 (intstat & (SSTS_DREG_EMPTY << 8)))
1708 goto phasechange;
1709 #endif
1710 if ((intstat & (SSTS_DREG_FULL << 8)) != 0)
1711 xfer = min(DINAMOUNT, n);
1712 else
1713 xfer = min(1, n);
1714
1715 SPC_MISC((">%d ", xfer));
1716
1717 n -= xfer;
1718 in += xfer;
1719
1720 while (xfer-- > 0) {
1721 *p++ = DREG;
1722 }
1723
1724 if ((intstat & INTSMASK) != 0)
1725 goto phasechange;
1726 }
1727
1728 /*
1729 * Some SCSI-devices are rude enough to transfer more data than what
1730 * was requested, e.g. 2048 bytes from a CD-ROM instead of the
1731 * requested 512. Test for progress, i.e. real transfers. If no real
1732 * transfers have been performed (n is probably already zero) and the
1733 * FIFO is not empty, waste some bytes....
1734 */
1735 if (in == 0) {
1736 for (;;) {
1737 if (INTS != 0)
1738 break;
1739 }
1740 SPC_MISC(("extra data "));
1741 }
1742
1743 phasechange:
1744 /* Stop the FIFO data path. */
1745
1746 /* Turn on ENREQINIT again. */
1747
1748 return in;
1749 }
1750
1751 /*
1753 * Catch an interrupt from the adaptor
1754 */
1755 /*
1756 * This is the workhorse routine of the driver.
1757 * Deficiencies (for now):
1758 * 1) always uses programmed I/O
1759 */
1760 int
1761 spcintr(unit)
1762 int unit;
1763 {
1764 register struct spc_softc *sc = spc_cd.cd_devs[unit]; /* XXX */
1765 u_char ints;
1766 register struct spc_acb *acb;
1767 register struct scsi_link *sc_link;
1768 struct spc_tinfo *ti;
1769 int n;
1770
1771 /*
1772 * $B3d$j9~$_6X;_$K$9$k(B
1773 */
1774 SCTL &= ~SCTL_INTR_ENAB;
1775
1776 SPC_TRACE(("spcintr "));
1777
1778 loop:
1779 /*
1780 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
1781 */
1782 /*
1783 * First check for abnormal conditions, such as reset.
1784 */
1785 #if 1 /* XXX? */
1786 while ((ints = INTS) == 0)
1787 delay(1);
1788 SPC_MISC(("ints = 0x%x ", ints));
1789 #else /* usually? */
1790 ints = INTS;
1791 #endif
1792 if ((ints & INTS_RST) != 0) {
1793 printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
1794 goto reset;
1795 }
1796
1797 /*
1798 * Check for less serious errors.
1799 */
1800 if ((SERR & (SERR_SCSI_PAR|SERR_SPC_PAR)) != 0) {
1801 printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
1802 if (sc->sc_prevphase == PH_MSGIN) {
1803 sc->sc_flags |= SPC_DROP_MSGIN;
1804 spc_sched_msgout(sc, SEND_PARITY_ERROR);
1805 } else
1806 spc_sched_msgout(sc, SEND_INIT_DET_ERR);
1807 }
1808
1809 /*
1810 * If we're not already busy doing something test for the following
1811 * conditions:
1812 * 1) We have been reselected by something
1813 * 2) We have selected something successfully
1814 * 3) Our selection process has timed out
1815 * 4) This is really a bus free interrupt just to get a new command
1816 * going?
1817 * 5) Spurious interrupt?
1818 */
1819 switch (sc->sc_state) {
1820 case SPC_IDLE:
1821 case SPC_SELECTING:
1822
1823 if ((ints & INTS_SEL) != 0) {
1824 /*
1825 * We don't currently support target mode.
1826 */
1827 printf("%s: target mode selected; going to BUS FREE\n",
1828 sc->sc_dev.dv_xname);
1829
1830 goto sched;
1831 } else if ((ints & INTS_RESEL) != 0) {
1832 SPC_MISC(("reselected "));
1833
1834 /*
1835 * If we're trying to select a target ourselves,
1836 * push our command back into the ready list.
1837 */
1838 if (sc->sc_state == SPC_SELECTING) {
1839 SPC_MISC(("backoff selector "));
1840 SPC_ASSERT(sc->sc_nexus != NULL);
1841 acb = sc->sc_nexus;
1842 sc->sc_nexus = NULL;
1843 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
1844 }
1845
1846 /* Save reselection ID. */
1847 sc->sc_selid = TEMP;
1848
1849 sc->sc_state = SPC_RESELECTED;
1850 } else if ((ints & INTS_CMD_DONE) != 0) {
1851 SPC_MISC(("selected "));
1852
1853 /*
1854 * We have selected a target. Things to do:
1855 * a) Determine what message(s) to send.
1856 * b) Verify that we're still selecting the target.
1857 * c) Mark device as busy.
1858 */
1859 if (sc->sc_state != SPC_SELECTING) {
1860 printf("%s: selection out while idle; resetting\n",
1861 sc->sc_dev.dv_xname);
1862 SPC_BREAK();
1863 goto reset;
1864 }
1865 SPC_ASSERT(sc->sc_nexus != NULL);
1866 acb = sc->sc_nexus;
1867 sc_link = acb->xs->sc_link;
1868 ti = &sc->sc_tinfo[sc_link->target];
1869
1870 sc->sc_msgpriq = SEND_IDENTIFY;
1871 if (acb->flags & ACB_RESET)
1872 sc->sc_msgpriq |= SEND_DEV_RESET;
1873 else if (acb->flags & ACB_ABORT)
1874 sc->sc_msgpriq |= SEND_ABORT;
1875 else {
1876 #if SPC_USE_SYNCHRONOUS
1877 if ((ti->flags & DO_SYNC) != 0)
1878 sc->sc_msgpriq |= SEND_SDTR;
1879 #endif
1880 #if SPC_USE_WIDE
1881 if ((ti->flags & DO_WIDE) != 0)
1882 sc->sc_msgpriq |= SEND_WDTR;
1883 #endif
1884 }
1885
1886 acb->flags |= ACB_NEXUS;
1887 ti->lubusy |= (1 << sc_link->lun);
1888
1889 /* Do an implicit RESTORE POINTERS. */
1890 sc->sc_dp = acb->data_addr;
1891 sc->sc_dleft = acb->data_length;
1892 sc->sc_cp = (u_char *)&acb->scsi_cmd;
1893 sc->sc_cleft = acb->scsi_cmd_length;
1894
1895 /* On our first connection, schedule a timeout. */
1896 if ((acb->xs->flags & SCSI_POLL) == 0)
1897 timeout(spc_timeout, acb, (acb->timeout * hz) / 1000);
1898
1899 sc->sc_state = SPC_CONNECTED;
1900 } else if ((ints & INTS_TIMEOUT) != 0) {
1901 SPC_MISC(("selection timeout "));
1902
1903 if (sc->sc_state != SPC_SELECTING) {
1904 printf("%s: selection timeout while idle; resetting\n",
1905 sc->sc_dev.dv_xname);
1906 SPC_BREAK();
1907 goto reset;
1908 }
1909 SPC_ASSERT(sc->sc_nexus != NULL);
1910 acb = sc->sc_nexus;
1911
1912 delay(250);
1913
1914 acb->xs->error = XS_SELTIMEOUT;
1915 goto finish;
1916 } else {
1917 if (sc->sc_state != SPC_IDLE) {
1918 printf("%s: BUS FREE while not idle; state=%d\n",
1919 sc->sc_dev.dv_xname, sc->sc_state);
1920 SPC_BREAK();
1921 goto out;
1922 }
1923
1924 goto sched;
1925 }
1926
1927 /*
1928 * Turn off selection stuff, and prepare to catch bus free
1929 * interrupts, parity errors, and phase changes.
1930 */
1931
1932 sc->sc_flags = 0;
1933 sc->sc_prevphase = PH_INVALID;
1934 goto dophase;
1935 }
1936
1937 if ((ints & INTS_DISCON) != 0) {
1938 /* We've gone to BUS FREE phase. */
1939 PCTL &= ~PCTL_BFINT_ENAB; /* disable disconnect interrupt */
1940 INTS = ints; /* XXX reset interrput */
1941
1942 switch (sc->sc_state) {
1943 case SPC_RESELECTED:
1944 goto sched;
1945
1946 case SPC_CONNECTED:
1947 SPC_ASSERT(sc->sc_nexus != NULL);
1948 acb = sc->sc_nexus;
1949
1950 #if SPC_USE_SYNCHRONOUS + SPC_USE_WIDE
1951 if (sc->sc_prevphase == PH_MSGOUT) {
1952 /*
1953 * If the target went to BUS FREE phase during
1954 * or immediately after sending a SDTR or WDTR
1955 * message, disable negotiation.
1956 */
1957 sc_link = acb->xs->sc_link;
1958 ti = &sc->sc_tinfo[sc_link->target];
1959 switch (sc->sc_lastmsg) {
1960 #if SPC_USE_SYNCHRONOUS
1961 case SEND_SDTR:
1962 ti->flags &= ~DO_SYNC;
1963 ti->period = ti->offset = 0;
1964 break;
1965 #endif
1966 #if SPC_USE_WIDE
1967 case SEND_WDTR:
1968 ti->flags &= ~DO_WIDE;
1969 ti->width = 0;
1970 break;
1971 #endif
1972 }
1973 }
1974 #endif
1975
1976 if ((sc->sc_flags & SPC_ABORTING) == 0) {
1977 /*
1978 * Section 5.1.1 of the SCSI 2 spec suggests
1979 * issuing a REQUEST SENSE following an
1980 * unexpected disconnect. Some devices go into
1981 * a contingent allegiance condition when
1982 * disconnecting, and this is necessary to
1983 * clean up their state.
1984 */
1985 printf("%s: unexpected disconnect; sending REQUEST SENSE\n",
1986 sc->sc_dev.dv_xname);
1987 SPC_BREAK();
1988 spc_sense(sc, acb);
1989 goto out;
1990 }
1991
1992 acb->xs->error = XS_DRIVER_STUFFUP;
1993 goto finish;
1994
1995 case SPC_DISCONNECT:
1996 SPC_ASSERT(sc->sc_nexus != NULL);
1997 acb = sc->sc_nexus;
1998 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1999 sc->sc_nexus = NULL;
2000 goto sched;
2001
2002 case SPC_CMDCOMPLETE:
2003 SPC_ASSERT(sc->sc_nexus != NULL);
2004 acb = sc->sc_nexus;
2005 goto finish;
2006 }
2007 }
2008 else if ((ints & INTS_CMD_DONE) != 0 &&
2009 sc->sc_prevphase == PH_MSGIN && sc->sc_state != SPC_CONNECTED)
2010 goto out;
2011
2012 dophase:
2013 #if 0
2014 if ((PSNS & PSNS_REQ) == 0) {
2015 /* Wait for REQINIT. */
2016 goto out;
2017 }
2018 #else
2019 INTS = ints;
2020 ints = 0;
2021 while ((PSNS & PSNS_REQ) == 0)
2022 delay(1); /* need timeout XXX */
2023 #endif
2024
2025 /*
2026 * $B%U%'!<%:$K$h$C$F>uBVA+0\$9$k(B
2027 */
2028 sc->sc_phase = PSNS & PH_MASK;
2029 /* PCTL = sc->sc_phase;*/
2030
2031 switch (sc->sc_phase) {
2032 case PH_MSGOUT:
2033 if (sc->sc_state != SPC_CONNECTED &&
2034 sc->sc_state != SPC_RESELECTED)
2035 break;
2036 spc_msgout(sc);
2037 sc->sc_prevphase = PH_MSGOUT;
2038 goto loop;
2039
2040 case PH_MSGIN:
2041 if (sc->sc_state != SPC_CONNECTED &&
2042 sc->sc_state != SPC_RESELECTED)
2043 break;
2044 spc_msgin(sc);
2045 sc->sc_prevphase = PH_MSGIN;
2046 goto loop;
2047
2048 case PH_CMD:
2049 if (sc->sc_state != SPC_CONNECTED)
2050 break;
2051 #if SPC_DEBUG
2052 if ((spc_debug & SPC_SHOWMISC) != 0) {
2053 SPC_ASSERT(sc->sc_nexus != NULL);
2054 acb = sc->sc_nexus;
2055 printf("cmd=0x%02x+%d ",
2056 acb->scsi_cmd.opcode, acb->scsi_cmd_length-1);
2057 }
2058 #endif
2059 n = spc_dataout_pio(sc, sc->sc_cp, sc->sc_cleft);
2060 sc->sc_cp += n;
2061 sc->sc_cleft -= n;
2062 sc->sc_prevphase = PH_CMD;
2063 goto loop;
2064
2065 case PH_DATAOUT:
2066 if (sc->sc_state != SPC_CONNECTED)
2067 break;
2068 SPC_MISC(("dataout dleft=%d ", sc->sc_dleft));
2069 n = spc_dataout_pio(sc, sc->sc_dp, sc->sc_dleft);
2070 sc->sc_dp += n;
2071 sc->sc_dleft -= n;
2072 sc->sc_prevphase = PH_DATAOUT;
2073 goto loop;
2074
2075 case PH_DATAIN:
2076 if (sc->sc_state != SPC_CONNECTED)
2077 break;
2078 SPC_MISC(("datain "));
2079 n = spc_datain_pio(sc, sc->sc_dp, sc->sc_dleft);
2080 sc->sc_dp += n;
2081 sc->sc_dleft -= n;
2082 sc->sc_prevphase = PH_DATAIN;
2083 goto loop;
2084
2085 case PH_STAT:
2086 if (sc->sc_state != SPC_CONNECTED)
2087 break;
2088 SPC_ASSERT(sc->sc_nexus != NULL);
2089 acb = sc->sc_nexus;
2090 /*acb->target_stat = DREG;*/
2091 spc_datain_pio(sc, &acb->target_stat, 1);
2092 SPC_MISC(("target_stat=0x%02x ", acb->target_stat));
2093 sc->sc_prevphase = PH_STAT;
2094 goto loop;
2095 }
2096
2097 printf("%s: unexpected bus phase; resetting\n", sc->sc_dev.dv_xname);
2098 SPC_BREAK();
2099 reset:
2100 spc_init(sc);
2101 return 1;
2102
2103 finish:
2104 untimeout(spc_timeout, acb);
2105 INTS = ints;
2106 ints = 0;
2107 spc_done(sc, acb);
2108 goto out;
2109
2110 sched:
2111 sc->sc_state = SPC_IDLE;
2112 spc_sched(sc);
2113 goto out;
2114
2115 out:
2116 if (ints)
2117 INTS = ints;
2118 SCTL |= SCTL_INTR_ENAB;
2119 return 1;
2120 }
2121
2122 void
2123 spc_abort(sc, acb)
2124 struct spc_softc *sc;
2125 struct spc_acb *acb;
2126 {
2127
2128 /* 2 secs for the abort */
2129 acb->timeout = SPC_ABORT_TIMEOUT;
2130 acb->flags |= ACB_ABORT;
2131
2132 if (acb == sc->sc_nexus) {
2133 /*
2134 * If we're still selecting, the message will be scheduled
2135 * after selection is complete.
2136 */
2137 if (sc->sc_state == SPC_CONNECTED)
2138 spc_sched_msgout(sc, SEND_ABORT);
2139 } else {
2140 spc_dequeue(sc, acb);
2141 TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
2142 if (sc->sc_state == SPC_IDLE)
2143 spc_sched(sc);
2144 }
2145 }
2146
2147 void
2148 spc_timeout(arg)
2149 void *arg;
2150 {
2151 struct spc_acb *acb = arg;
2152 struct scsi_xfer *xs = acb->xs;
2153 struct scsi_link *sc_link = xs->sc_link;
2154 struct spc_softc *sc = sc_link->adapter_softc;
2155 int s;
2156
2157 sc_print_addr(sc_link);
2158 printf("timed out");
2159
2160 s = splbio();
2161
2162 if (acb->flags & ACB_ABORT) {
2163 /* abort timed out */
2164 printf(" AGAIN\n");
2165 /* XXX Must reset! */
2166 } else {
2167 /* abort the operation that has timed out */
2168 printf("\n");
2169 acb->xs->error = XS_TIMEOUT;
2170 spc_abort(sc, acb);
2171 }
2172
2173 splx(s);
2174 }
2175
2176 #ifdef SPC_DEBUG
2178 /*
2179 * The following functions are mostly used for debugging purposes, either
2180 * directly called from the driver or from the kernel debugger.
2181 */
2182
2183 void
2184 spc_show_scsi_cmd(acb)
2185 struct spc_acb *acb;
2186 {
2187 u_char *b = (u_char *)&acb->scsi_cmd;
2188 struct scsi_link *sc_link = acb->xs->sc_link;
2189 int i;
2190
2191 sc_print_addr(sc_link);
2192 if ((acb->xs->flags & SCSI_RESET) == 0) {
2193 for (i = 0; i < acb->scsi_cmd_length; i++) {
2194 if (i)
2195 printf(",");
2196 printf("%x", b[i]);
2197 }
2198 printf("\n");
2199 } else
2200 printf("RESET\n");
2201 }
2202
2203 void
2204 spc_print_acb(acb)
2205 struct spc_acb *acb;
2206 {
2207
2208 printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
2209 printf(" dp=%x dleft=%d target_stat=%x\n",
2210 (long)acb->data_addr, acb->data_length, acb->target_stat);
2211 spc_show_scsi_cmd(acb);
2212 }
2213
2214 void
2215 spc_print_active_acb()
2216 {
2217 struct spc_acb *acb;
2218 struct spc_softc *sc = spc_cd.cd_devs[0]; /* XXX */
2219
2220 printf("ready list:\n");
2221 for (acb = sc->ready_list.tqh_first; acb != NULL;
2222 acb = acb->chain.tqe_next)
2223 spc_print_acb(acb);
2224 printf("nexus:\n");
2225 if (sc->sc_nexus != NULL)
2226 spc_print_acb(sc->sc_nexus);
2227 printf("nexus list:\n");
2228 for (acb = sc->nexus_list.tqh_first; acb != NULL;
2229 acb = acb->chain.tqe_next)
2230 spc_print_acb(acb);
2231 }
2232
2233 void
2234 spc_dump_driver(sc)
2235 struct spc_softc *sc;
2236 {
2237 struct spc_tinfo *ti;
2238 int i;
2239
2240 printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2241 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2242 sc->sc_state, sc->sc_imess[0],
2243 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2244 for (i = 0; i < 7; i++) {
2245 ti = &sc->sc_tinfo[i];
2246 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2247 i, ti->cmds, ti->dconns, ti->touts);
2248 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2249 }
2250 }
2251 #endif
2252