mha.c revision 1.23.2.2 1 /* $NetBSD: mha.c,v 1.23.2.2 2002/01/08 00:28:40 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1996-1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum, Masaru Oki, Takumi Nakamura, Masanobu Saitoh and
9 * Minoura Makoto.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*-
41 * Copyright (c) 1994 Jarle Greipsland
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. The name of the author may not be used to endorse or promote products
53 * derived from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
56 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
57 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
58 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
59 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
60 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
61 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
63 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
64 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
66 */
67
68 #include "opt_ddb.h"
69
70 /* Synchronous data transfers? */
71 #define SPC_USE_SYNCHRONOUS 0
72 #define SPC_SYNC_REQ_ACK_OFS 8
73
74 /* Default DMA mode? */
75 #define MHA_DMA_LIMIT_XFER 1
76 #define MHA_DMA_BURST_XFER 1
77 #define MHA_DMA_SHORT_BUS_CYCLE 1
78
79 #define MHA_DMA_DATAIN (0 | (MHA_DMA_LIMIT_XFER << 1) \
80 | (MHA_DMA_BURST_XFER << 2) \
81 | (MHA_DMA_SHORT_BUS_CYCLE << 3))
82 #define MHA_DMA_DATAOUT (1 | (MHA_DMA_LIMIT_XFER << 1) \
83 | (MHA_DMA_BURST_XFER << 2) \
84 | (MHA_DMA_SHORT_BUS_CYCLE << 3))
85
86 /* Include debug functions? At the end of this file there are a bunch of
87 * functions that will print out various information regarding queued SCSI
88 * commands, driver state and chip contents. You can call them from the
89 * kernel debugger. If you set SPC_DEBUG to 0 they are not included (the
90 * kernel uses less memory) but you lose the debugging facilities.
91 */
92 #define SPC_DEBUG 0
93
94 /* End of customizable parameters */
95
96 /*
97 * MB86601A SCSI Protocol Controller (SPC) routines for MANKAI Mach-2
98 */
99
100 #include <sys/types.h>
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/kernel.h>
104 #include <sys/errno.h>
105 #include <sys/ioctl.h>
106 #include <sys/device.h>
107 #include <sys/buf.h>
108 #include <sys/proc.h>
109 #include <sys/user.h>
110 #include <sys/queue.h>
111
112 #include <machine/bus.h>
113
114 #include <dev/scsipi/scsi_all.h>
115 #include <dev/scsipi/scsipi_all.h>
116 #include <dev/scsipi/scsi_message.h>
117 #include <dev/scsipi/scsiconf.h>
118
119 #include <x68k/x68k/iodevice.h>
120 #include <x68k/dev/mb86601reg.h>
121 #include <x68k/dev/mhavar.h>
122 #include <x68k/dev/intiovar.h>
123 #include <x68k/dev/scsiromvar.h>
124
125 #if 0
126 #define WAIT {if (sc->sc_pc[2]) {printf("[W_%d", __LINE__); while (sc->sc_pc[2] & 0x40);printf("]");}}
127 #else
128 #define WAIT {while (sc->sc_pc[2] & 0x40);}
129 #endif
130
131 #define SSR (sc->sc_pc[2])
132 #define SS_IREQUEST 0x80
133 #define SS_BUSY 0x40
134 #define SS_DREG_FULL 0x02
135
136 #define NSR (sc->sc_pc[3])
137
138 #define SIR (sc->sc_pc[4])
139
140 #define CMR (sc->sc_pc[5])
141 #define CMD_SEL_AND_CMD 0x00
142 #define CMD_SELECT 0x09
143 #define CMD_SET_ATN 0x0a
144 #define CMD_RESET_ATN 0x0b
145 #define CMD_RESET_ACK 0x0d
146 #define CMD_SEND_FROM_MPU 0x10
147 #define CMD_SEND_FROM_DMA 0x11
148 #define CMD_RECEIVE_TO_MPU 0x12
149 #define CMD_RECEIVE_TO_DMA 0x13
150 #define CMD_RECEIVE_MSG 0x1a
151 #define CMD_RECEIVE_STS 0x1c
152 #define CMD_SOFT_RESET 0x40
153 #define CMD_SCSI_RESET 0x42
154 #define CMD_SET_UP_REG 0x43
155
156 #define SCR (sc->sc_pc[11])
157
158 #define TMR (sc->sc_pc[12])
159 #define TM_SYNC 0x80
160 #define TM_ASYNC 0x00
161
162 #define WAR (sc->sc_pc[15])
163 #define WA_MCSBUFWIN 0x00
164 #define WA_UPMWIN 0x80
165 #define WA_INITWIN 0xc0
166
167 #define MBR (sc->sc_pc[15])
168
169 #define ISCSR (sc->sc_ps[2])
170
171 #define CCR (sc->sc_pcx[0])
172 #define OIR (sc->sc_pcx[1])
173 #define AMR (sc->sc_pcx[2])
174 #define SMR (sc->sc_pcx[3])
175 #define SRR (sc->sc_pcx[4])
176 #define STR (sc->sc_pcx[5])
177 #define RTR (sc->sc_pcx[6])
178 #define ATR (sc->sc_pcx[7])
179 #define PER (sc->sc_pcx[8])
180 #define IER (sc->sc_pcx[9])
181 #define IE_ALL 0xBF
182
183 #define GLR (sc->sc_pcx[10])
184 #define DMR (sc->sc_pcx[11])
185 #define IMR (sc->sc_pcx[12])
186
187
188 #ifndef DDB
190 #define Debugger() panic("should call debugger here (mha.c)")
191 #endif /* ! DDB */
192
193
194 #if SPC_DEBUG
195 #define SPC_SHOWACBS 0x01
196 #define SPC_SHOWINTS 0x02
197 #define SPC_SHOWCMDS 0x04
198 #define SPC_SHOWMISC 0x08
199 #define SPC_SHOWTRAC 0x10
200 #define SPC_SHOWSTART 0x20
201 #define SPC_SHOWPHASE 0x40
202 #define SPC_SHOWDMA 0x80
203 #define SPC_SHOWCCMDS 0x100
204 #define SPC_SHOWMSGS 0x200
205 #define SPC_DOBREAK 0x400
206
207 int mha_debug =
208 #if 0
209 0x7FF;
210 #else
211 SPC_SHOWSTART|SPC_SHOWTRAC;
212 #endif
213
214
215 #define SPC_ACBS(str) do {if (mha_debug & SPC_SHOWACBS) printf str;} while (0)
216 #define SPC_MISC(str) do {if (mha_debug & SPC_SHOWMISC) printf str;} while (0)
217 #define SPC_INTS(str) do {if (mha_debug & SPC_SHOWINTS) printf str;} while (0)
218 #define SPC_TRACE(str) do {if (mha_debug & SPC_SHOWTRAC) printf str;} while (0)
219 #define SPC_CMDS(str) do {if (mha_debug & SPC_SHOWCMDS) printf str;} while (0)
220 #define SPC_START(str) do {if (mha_debug & SPC_SHOWSTART) printf str;}while (0)
221 #define SPC_PHASE(str) do {if (mha_debug & SPC_SHOWPHASE) printf str;}while (0)
222 #define SPC_DMA(str) do {if (mha_debug & SPC_SHOWDMA) printf str;}while (0)
223 #define SPC_MSGS(str) do {if (mha_debug & SPC_SHOWMSGS) printf str;}while (0)
224 #define SPC_BREAK() do {if ((mha_debug & SPC_DOBREAK) != 0) Debugger();} while (0)
225 #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)
226 #else
227 #define SPC_ACBS(str)
228 #define SPC_MISC(str)
229 #define SPC_INTS(str)
230 #define SPC_TRACE(str)
231 #define SPC_CMDS(str)
232 #define SPC_START(str)
233 #define SPC_PHASE(str)
234 #define SPC_DMA(str)
235 #define SPC_MSGS(str)
236 #define SPC_BREAK()
237 #define SPC_ASSERT(x)
238 #endif
239
240 int mhamatch __P((struct device *, struct cfdata *, void *));
241 void mhaattach __P((struct device *, struct device *, void *));
242 void mhaselect __P((struct mha_softc *,
243 u_char, u_char, u_char *, u_char));
244 void mha_scsi_reset __P((struct mha_softc *));
245 void mha_reset __P((struct mha_softc *));
246 void mha_free_acb __P((struct mha_softc *, struct acb *, int));
247 void mha_sense __P((struct mha_softc *, struct acb *));
248 void mha_msgin __P((struct mha_softc *));
249 void mha_msgout __P((struct mha_softc *));
250 int mha_dataout_pio __P((struct mha_softc *, u_char *, int));
251 int mha_datain_pio __P((struct mha_softc *, u_char *, int));
252 int mha_dataout __P((struct mha_softc *, u_char *, int));
253 int mha_datain __P((struct mha_softc *, u_char *, int));
254 void mha_abort __P((struct mha_softc *, struct acb *));
255 void mha_init __P((struct mha_softc *));
256 void mha_scsi_request __P((struct scsipi_channel *,
257 scsipi_adapter_req_t, void *));
258 void mha_poll __P((struct mha_softc *, struct acb *));
259 void mha_sched __P((struct mha_softc *));
260 void mha_done __P((struct mha_softc *, struct acb *));
261 int mhaintr __P((void*));
262 void mha_timeout __P((void *));
263 void mha_minphys __P((struct buf *));
264 void mha_dequeue __P((struct mha_softc *, struct acb *));
265 inline void mha_setsync __P((struct mha_softc *, struct spc_tinfo *));
266 #if SPC_DEBUG
267 void mha_print_acb __P((struct acb *));
268 void mha_show_scsi_cmd __P((struct acb *));
269 void mha_print_active_acb __P((void));
270 void mha_dump_driver __P((struct mha_softc *));
271 #endif
272
273 static int mha_dataio_dma __P((int, int, struct mha_softc *, u_char *, int));
274
275 struct cfattach mha_ca = {
276 sizeof(struct mha_softc), mhamatch, mhaattach
277 };
278
279 extern struct cfdriver mha_cd;
280
281
282 /*
284 * returns non-zero value if a controller is found.
285 */
286 int
287 mhamatch(parent, cf, aux)
288 struct device *parent;
289 struct cfdata *cf;
290 void *aux;
291 {
292 struct intio_attach_args *ia = aux;
293 bus_space_tag_t iot = ia->ia_bst;
294 bus_space_handle_t ioh;
295
296 ia->ia_size=0x20;
297 if (ia->ia_addr != 0xea0000)
298 return 0;
299
300 if (intio_map_allocate_region(parent->dv_parent, ia,
301 INTIO_MAP_TESTONLY) < 0) /* FAKE */
302 return 0;
303
304 if (bus_space_map(iot, ia->ia_addr, 0x20, BUS_SPACE_MAP_SHIFTED,
305 &ioh) < 0)
306 return 0;
307 if (!badaddr ((caddr_t)INTIO_ADDR(ia->ia_addr + 0)))
308 return 0;
309 bus_space_unmap(iot, ioh, 0x20);
310
311 return 1;
312 }
313
314 /*
315 */
316
317 struct mha_softc *tmpsc;
318
319 void
320 mhaattach(parent, self, aux)
321 struct device *parent, *self;
322 void *aux;
323 {
324 struct mha_softc *sc = (void *)self;
325 struct intio_attach_args *ia = aux;
326
327 tmpsc = sc; /* XXX */
328
329 printf (": Mankai Mach-2 Fast SCSI Host Adaptor\n");
330
331 SPC_TRACE(("mhaattach "));
332 sc->sc_state = SPC_INIT;
333 sc->sc_iobase = INTIO_ADDR(ia->ia_addr + 0x80); /* XXX */
334 intio_map_allocate_region (parent->dv_parent, ia, INTIO_MAP_ALLOCATE);
335 /* XXX: FAKE */
336 sc->sc_dmat = ia->ia_dmat;
337
338 sc->sc_pc = (volatile u_char *)sc->sc_iobase;
339 sc->sc_ps = (volatile u_short *)sc->sc_iobase;
340 sc->sc_pcx = &sc->sc_pc[0x10];
341
342 sc->sc_id = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
343
344 intio_intr_establish (ia->ia_intr, "mha", mhaintr, sc);
345
346 mha_init(sc); /* Init chip and driver */
347
348 mha_scsi_reset(sc); /* XXX: some devices need this. */
349
350 sc->sc_phase = BUSFREE_PHASE;
351
352 /*
353 * Fill in the adapter.
354 */
355 sc->sc_adapter.adapt_dev = &sc->sc_dev;
356 sc->sc_adapter.adapt_nchannels = 1;
357 sc->sc_adapter.adapt_openings = 7;
358 sc->sc_adapter.adapt_max_periph = 1;
359 sc->sc_adapter.adapt_ioctl = NULL;
360 sc->sc_adapter.adapt_minphys = mha_minphys;
361 sc->sc_adapter.adapt_request = mha_scsi_request;
362
363 sc->sc_channel.chan_adapter = &sc->sc_adapter;
364 sc->sc_channel.chan_bustype = &scsi_bustype;
365 sc->sc_channel.chan_channel = 0;
366 sc->sc_channel.chan_ntargets = 8;
367 sc->sc_channel.chan_nluns = 8;
368 sc->sc_channel.chan_id = sc->sc_id;
369
370 sc->sc_spcinitialized = 0;
371 WAR = WA_INITWIN;
372 #if 1
373 CCR = 0x14;
374 OIR = sc->sc_id;
375 AMR = 0x00;
376 SMR = 0x00;
377 SRR = 0x00;
378 STR = 0x20;
379 RTR = 0x40;
380 ATR = 0x01;
381 PER = 0xc9;
382 #endif
383 IER = IE_ALL; /* $B$9$Y$F$N3d$j9~$_$r5v2D(B */
384 #if 1
385 GLR = 0x00;
386 DMR = 0x30;
387 IMR = 0x00;
388 #endif
389 WAR = WA_MCSBUFWIN;
390
391 /* drop off */
392 while (SSR & SS_IREQUEST)
393 {
394 (void) ISCSR;
395 }
396
397 CMR = CMD_SET_UP_REG; /* setup reg cmd. */
398
399 SPC_TRACE(("waiting for intr..."));
400 while (!(SSR & SS_IREQUEST))
401 delay(10);
402 mhaintr (sc);
403
404 tmpsc = NULL;
405
406 config_found(self, &sc->sc_channel, scsiprint);
407 }
408
409 #if 0
410 void
411 mha_reset(sc)
412 struct mha_softc *sc;
413 {
414 u_short dummy;
415 printf("reset...");
416 CMR = CMD_SOFT_RESET;
417 asm volatile ("nop"); /* XXX wait (4clk in 20mhz) ??? */
418 dummy = sc->sc_ps[-1];
419 dummy = sc->sc_ps[-1];
420 dummy = sc->sc_ps[-1];
421 dummy = sc->sc_ps[-1];
422 asm volatile ("nop");
423 CMR = CMD_SOFT_RESET;
424 sc->sc_spcinitialized = 0;
425 CMR = CMD_SET_UP_REG; /* setup reg cmd. */
426 while(!sc->sc_spcinitialized);
427
428 sc->sc_id = IODEVbase->io_sram[0x70] & 0x7; /* XXX */
429 printf("done.\n");
430 }
431 #endif
432
433 /*
434 * Pull the SCSI RST line for 500us.
435 */
436 void
437 mha_scsi_reset(sc) /* FINISH? */
438 struct mha_softc *sc;
439 {
440
441 CMR = CMD_SCSI_RESET; /* SCSI RESET */
442 while (!(SSR&SS_IREQUEST))
443 delay(10);
444 }
445
446 /*
447 * Initialize mha SCSI driver.
448 */
449 void
450 mha_init(sc)
451 struct mha_softc *sc;
452 {
453 struct acb *acb;
454 int r;
455
456 if (sc->sc_state == SPC_INIT) {
457 /* First time through; initialize. */
458 TAILQ_INIT(&sc->ready_list);
459 TAILQ_INIT(&sc->nexus_list);
460 TAILQ_INIT(&sc->free_list);
461 sc->sc_nexus = NULL;
462 acb = sc->sc_acb;
463 memset(acb, 0, sizeof(sc->sc_acb));
464 for (r = 0; r < sizeof(sc->sc_acb) / sizeof(*acb); r++) {
465 TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
466 acb++;
467 }
468 memset(&sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
469
470 r = bus_dmamem_alloc(sc->sc_dmat, MAXBSIZE, 0, 0,
471 sc->sc_dmaseg, 1, &sc->sc_ndmasegs,
472 BUS_DMA_NOWAIT);
473 if (r)
474 panic("mha_init: cannot allocate dma memory");
475 if (sc->sc_ndmasegs != 1)
476 panic("mha_init: number of segment > 1??");
477 r = bus_dmamem_map(sc->sc_dmat, sc->sc_dmaseg, sc->sc_ndmasegs,
478 MAXBSIZE, &sc->sc_dmabuf, BUS_DMA_NOWAIT);
479 if (r)
480 panic("mha_init: cannot map dma memory");
481 r = bus_dmamap_create(sc->sc_dmat, MAXBSIZE, 1,
482 MAXBSIZE, 0, BUS_DMA_NOWAIT,
483 &sc->sc_dmamap);
484 if (r)
485 panic("mha_init: cannot create dmamap structure");
486 r = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
487 sc->sc_dmabuf, MAXBSIZE, NULL,
488 BUS_DMA_NOWAIT);
489 if (r)
490 panic("mha_init: cannot load dma buffer into dmamap");
491 sc->sc_p = 0;
492 } else {
493 /* Cancel any active commands. */
494 sc->sc_flags |= SPC_ABORTING;
495 sc->sc_state = SPC_IDLE;
496 if ((acb = sc->sc_nexus) != NULL) {
497 acb->xs->error = XS_DRIVER_STUFFUP;
498 mha_done(sc, acb);
499 }
500 while ((acb = sc->nexus_list.tqh_first) != NULL) {
501 acb->xs->error = XS_DRIVER_STUFFUP;
502 mha_done(sc, acb);
503 }
504 }
505
506 sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
507 for (r = 0; r < 8; r++) {
508 struct spc_tinfo *ti = &sc->sc_tinfo[r];
509
510 ti->flags = 0;
511 #if SPC_USE_SYNCHRONOUS
512 ti->flags |= T_SYNCMODE;
513 ti->period = sc->sc_minsync;
514 ti->offset = SPC_SYNC_REQ_ACK_OFS;
515 #else
516 ti->period = ti->offset = 0;
517 #endif
518 ti->width = 0;
519 }
520
521 sc->sc_state = SPC_IDLE;
522 }
523
524 void
525 mha_free_acb(sc, acb, flags)
526 struct mha_softc *sc;
527 struct acb *acb;
528 int flags;
529 {
530 int s;
531
532 s = splbio();
533
534 acb->flags = 0;
535 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
536
537 /*
538 * If there were none, wake anybody waiting for one to come free,
539 * starting with queued entries.
540 */
541 if (acb->chain.tqe_next == 0)
542 wakeup(&sc->free_list);
543
544 splx(s);
545 }
546
547
548 /*
550 * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
551 */
552
553 /*
554 * Expected sequence:
555 * 1) Command inserted into ready list
556 * 2) Command selected for execution
557 * 3) Command won arbitration and has selected target device
558 * 4) Send message out (identify message, eventually also sync.negotiations)
559 * 5) Send command
560 * 5a) Receive disconnect message, disconnect.
561 * 5b) Reselected by target
562 * 5c) Receive identify message from target.
563 * 6) Send or receive data
564 * 7) Receive status
565 * 8) Receive message (command complete etc.)
566 * 9) If status == SCSI_CHECK construct a synthetic request sense SCSI cmd.
567 * Repeat 2-8 (no disconnects please...)
568 */
569
570 /*
571 * Start a selection. This is used by mha_sched() to select an idle target,
572 * and by mha_done() to immediately reselect a target to get sense information.
573 */
574 void
575 mhaselect(sc, target, lun, cmd, clen)
576 struct mha_softc *sc;
577 u_char target, lun;
578 u_char *cmd;
579 u_char clen;
580 {
581 int i;
582 int s;
583
584 s = splbio(); /* XXX */
585
586 SPC_TRACE(("[mhaselect(t%d,l%d,cmd:%x)] ", target, lun, *(u_char *)cmd));
587
588 /* CDB $B$r(B SPC $B$N(B MCS REG $B$K%;%C%H$9$k(B */
589 /* Now the command into the FIFO */
590 WAIT;
591 #if 1
592 SPC_MISC(("[cmd:"));
593 for (i = 0; i < clen; i++)
594 {
595 unsigned c = cmd[i];
596 if (i == 1)
597 c |= lun << 5;
598 SPC_MISC((" %02x", c));
599 sc->sc_pcx[i] = c;
600 }
601 SPC_MISC(("], target=%d\n", target));
602 #else
603 memcpy(sc->sc_pcx, cmd, clen);
604 #endif
605 if (NSR & 0x80)
606 panic("scsistart: already selected...");
607 sc->sc_phase = COMMAND_PHASE;
608
609 /* new state ASP_SELECTING */
610 sc->sc_state = SPC_SELECTING;
611
612 SIR = target;
613 #if 0
614 CMR = CMD_SELECT;
615 #else
616 CMR = CMD_SEL_AND_CMD; /* select & cmd */
617 #endif
618 splx(s);
619 }
620
621 #if 0
622 int
623 mha_reselect(sc, message)
624 struct mha_softc *sc;
625 u_char message;
626 {
627 u_char selid, target, lun;
628 struct acb *acb;
629 struct scsipi_periph *periph;
630 struct spc_tinfo *ti;
631
632 /*
633 * The SCSI chip made a snapshot of the data bus while the reselection
634 * was being negotiated. This enables us to determine which target did
635 * the reselect.
636 */
637 selid = sc->sc_selid & ~(1 << sc->sc_id);
638 if (selid & (selid - 1)) {
639 printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
640 sc->sc_dev.dv_xname, selid);
641 SPC_BREAK();
642 goto reset;
643 }
644
645 /*
646 * Search wait queue for disconnected cmd
647 * The list should be short, so I haven't bothered with
648 * any more sophisticated structures than a simple
649 * singly linked list.
650 */
651 target = ffs(selid) - 1;
652 lun = message & 0x07;
653 for (acb = sc->nexus_list.tqh_first; acb != NULL;
654 acb = acb->chain.tqe_next) {
655 periph = acb->xs->xs_periph;
656 if (periph->periph_target == target &&
657 periph->periph_lun == lun)
658 break;
659 }
660 if (acb == NULL) {
661 printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
662 sc->sc_dev.dv_xname, target, lun);
663 SPC_BREAK();
664 goto abort;
665 }
666
667 /* Make this nexus active again. */
668 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
669 sc->sc_state = SPC_HASNEXUS;
670 sc->sc_nexus = acb;
671 ti = &sc->sc_tinfo[target];
672 ti->lubusy |= (1 << lun);
673 mha_setsync(sc, ti);
674
675 if (acb->flags & ACB_RESET)
676 mha_sched_msgout(sc, SEND_DEV_RESET);
677 else if (acb->flags & ACB_ABORTED)
678 mha_sched_msgout(sc, SEND_ABORT);
679
680 /* Do an implicit RESTORE POINTERS. */
681 sc->sc_dp = acb->daddr;
682 sc->sc_dleft = acb->dleft;
683 sc->sc_cp = (u_char *)&acb->cmd;
684 sc->sc_cleft = acb->clen;
685
686 return (0);
687
688 reset:
689 mha_sched_msgout(sc, SEND_DEV_RESET);
690 return (1);
691
692 abort:
693 mha_sched_msgout(sc, SEND_ABORT);
694 return (1);
695 }
696 #endif
697 /*
698 * Start a SCSI-command
699 * This function is called by the higher level SCSI-driver to queue/run
700 * SCSI-commands.
701 */
702 void
703 mha_scsi_request(chan, req, arg)
704 struct scsipi_channel *chan;
705 scsipi_adapter_req_t req;
706 void *arg;
707 {
708 struct scsipi_xfer *xs;
709 struct scsipi_periph *periph;
710 struct mha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
711 struct acb *acb;
712 int s, flags;
713
714 switch (req) {
715 case ADAPTER_REQ_RUN_XFER:
716 xs = arg;
717 periph = xs->xs_periph;
718
719 SPC_TRACE(("[mha_scsi_cmd] "));
720 SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
721 periph->periph_target));
722
723 flags = xs->xs_control;
724
725 /* Get a mha command block */
726 s = splbio();
727 acb = sc->free_list.tqh_first;
728 if (acb) {
729 TAILQ_REMOVE(&sc->free_list, acb, chain);
730 ACB_SETQ(acb, ACB_QNONE);
731 }
732
733 if (acb == NULL) {
734 xs->error = XS_RESOURCE_SHORTAGE;
735 scsipi_done(xs);
736 splx(s);
737 return;
738 }
739 splx(s);
740
741 /* Initialize acb */
742 acb->xs = xs;
743 memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
744 acb->clen = xs->cmdlen;
745 acb->daddr = xs->data;
746 acb->dleft = xs->datalen;
747 acb->stat = 0;
748
749 s = splbio();
750 ACB_SETQ(acb, ACB_QREADY);
751 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
752 #if 1
753 callout_reset(&acb->xs->xs_callout,
754 ((u_int64_t)xs->timeout * (u_int64_t)hz) / 1000,
755 mha_timeout, acb);
756 #endif
757
758 /*
759 * $B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k(B
760 */
761 if (sc->sc_state == SPC_IDLE)
762 mha_sched(sc);
763
764 splx(s);
765
766 if (flags & XS_CTL_POLL) {
767 /* Not allowed to use interrupts, use polling instead */
768 mha_poll(sc, acb);
769 }
770
771 SPC_MISC(("SUCCESSFULLY_QUEUED"));
772 return;
773
774 case ADAPTER_REQ_GROW_RESOURCES:
775 /* XXX Not supported. */
776 return;
777
778 case ADAPTER_REQ_SET_XFER_MODE:
779 /* XXX Not supported. */
780 return;
781 }
782 }
783
784 /*
785 * Adjust transfer size in buffer structure
786 */
787 void
788 mha_minphys(bp)
789 struct buf *bp;
790 {
791
792 SPC_TRACE(("mha_minphys "));
793 minphys(bp);
794 }
795
796 /*
797 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
798 */
799 void
800 mha_poll(sc, acb)
801 struct mha_softc *sc;
802 struct acb *acb;
803 {
804 struct scsipi_xfer *xs = acb->xs;
805 int count = xs->timeout * 100;
806 int s = splbio();
807
808 SPC_TRACE(("[mha_poll] "));
809
810 while (count) {
811 /*
812 * If we had interrupts enabled, would we
813 * have got an interrupt?
814 */
815 if (SSR & SS_IREQUEST)
816 mhaintr(sc);
817 if ((xs->xs_status & XS_STS_DONE) != 0)
818 break;
819 DELAY(10);
820 #if 1
821 if (sc->sc_state == SPC_IDLE) {
822 SPC_TRACE(("[mha_poll: rescheduling] "));
823 mha_sched(sc);
824 }
825 #endif
826 count--;
827 }
828
829 if (count == 0) {
830 SPC_MISC(("mha_poll: timeout"));
831 mha_timeout((caddr_t)acb);
832 }
833 splx(s);
834 scsipi_done(xs);
835 }
836
837 /*
839 * LOW LEVEL SCSI UTILITIES
840 */
841
842 /*
843 * Set synchronous transfer offset and period.
844 */
845 inline void
846 mha_setsync(sc, ti)
847 struct mha_softc *sc;
848 struct spc_tinfo *ti;
849 {
850 }
851
852
853 /*
855 * Schedule a SCSI operation. This has now been pulled out of the interrupt
856 * handler so that we may call it from mha_scsi_cmd and mha_done. This may
857 * save us an unecessary interrupt just to get things going. Should only be
858 * called when state == SPC_IDLE and at bio pl.
859 */
860 void
861 mha_sched(sc)
862 register struct mha_softc *sc;
863 {
864 struct scsipi_periph *periph;
865 struct acb *acb;
866 int t;
867
868 SPC_TRACE(("[mha_sched] "));
869 if (sc->sc_state != SPC_IDLE)
870 panic("mha_sched: not IDLE (state=%d)", sc->sc_state);
871
872 if (sc->sc_flags & SPC_ABORTING)
873 return;
874
875 /*
876 * Find first acb in ready queue that is for a target/lunit
877 * combinations that is not busy.
878 */
879 for (acb = sc->ready_list.tqh_first; acb ; acb = acb->chain.tqe_next) {
880 struct spc_tinfo *ti;
881 periph = acb->xs->xs_periph;
882 t = periph->periph_target;
883 ti = &sc->sc_tinfo[t];
884 if (!(ti->lubusy & (1 << periph->periph_lun))) {
885 if ((acb->flags & ACB_QBITS) != ACB_QREADY)
886 panic("mha: busy entry on ready list");
887 TAILQ_REMOVE(&sc->ready_list, acb, chain);
888 ACB_SETQ(acb, ACB_QNONE);
889 sc->sc_nexus = acb;
890 sc->sc_flags = 0;
891 sc->sc_prevphase = INVALID_PHASE;
892 sc->sc_dp = acb->daddr;
893 sc->sc_dleft = acb->dleft;
894 ti->lubusy |= (1<<periph->periph_lun);
895 mhaselect(sc, t, periph->periph_lun,
896 (u_char *)&acb->cmd, acb->clen);
897 break;
898 } else {
899 SPC_MISC(("%d:%d busy\n",
900 periph->periph_target,
901 periph->periph_lun));
902 }
903 }
904 }
905
906 /*
908 * POST PROCESSING OF SCSI_CMD (usually current)
909 */
910 void
911 mha_done(sc, acb)
912 struct mha_softc *sc;
913 struct acb *acb;
914 {
915 struct scsipi_xfer *xs = acb->xs;
916 struct scsipi_periph *periph = xs->xs_periph;
917 struct spc_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
918
919 SPC_TRACE(("[mha_done(error:%x)] ", xs->error));
920
921 #if 1
922 callout_stop(&acb->xs->xs_callout);
923 #endif
924
925 /*
926 * Now, if we've come here with no error code, i.e. we've kept the
927 * initial XS_NOERROR, and the status code signals that we should
928 * check sense, we'll need to set up a request sense cmd block and
929 * push the command back into the ready queue *before* any other
930 * commands for this target/lunit, else we lose the sense info.
931 * We don't support chk sense conditions for the request sense cmd.
932 */
933 if (xs->error == XS_NOERROR) {
934 if ((acb->flags & ACB_ABORTED) != 0) {
935 xs->error = XS_TIMEOUT;
936 } else if (acb->flags & ACB_CHKSENSE) {
937 xs->error = XS_SENSE;
938 } else {
939 xs->status = acb->stat & ST_MASK;
940 switch (xs->status) {
941 case SCSI_CHECK:
942 xs->resid = acb->dleft;
943 /* FALLTHOUGH */
944 case SCSI_BUSY:
945 xs->error = XS_BUSY;
946 break;
947 case SCSI_OK:
948 xs->resid = acb->dleft;
949 break;
950 default:
951 xs->error = XS_DRIVER_STUFFUP;
952 #if SPC_DEBUG
953 printf("%s: mha_done: bad stat 0x%x\n",
954 sc->sc_dev.dv_xname, acb->stat);
955 #endif
956 break;
957 }
958 }
959 }
960
961 #if SPC_DEBUG
962 if ((mha_debug & SPC_SHOWMISC) != 0) {
963 if (xs->resid != 0)
964 printf("resid=%d ", xs->resid);
965 if (xs->error == XS_SENSE)
966 printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
967 else
968 printf("error=%d\n", xs->error);
969 }
970 #endif
971
972 /*
973 * Remove the ACB from whatever queue it's on.
974 */
975 switch (acb->flags & ACB_QBITS) {
976 case ACB_QNONE:
977 if (acb != sc->sc_nexus) {
978 panic("%s: floating acb", sc->sc_dev.dv_xname);
979 }
980 sc->sc_nexus = NULL;
981 sc->sc_state = SPC_IDLE;
982 ti->lubusy &= ~(1<<periph->periph_lun);
983 mha_sched(sc);
984 break;
985 case ACB_QREADY:
986 TAILQ_REMOVE(&sc->ready_list, acb, chain);
987 break;
988 case ACB_QNEXUS:
989 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
990 ti->lubusy &= ~(1<<periph->periph_lun);
991 break;
992 case ACB_QFREE:
993 panic("%s: dequeue: busy acb on free list",
994 sc->sc_dev.dv_xname);
995 break;
996 default:
997 panic("%s: dequeue: unknown queue %d",
998 sc->sc_dev.dv_xname, acb->flags & ACB_QBITS);
999 }
1000
1001 /* Put it on the free list, and clear flags. */
1002 #if 0
1003 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
1004 acb->flags = ACB_QFREE;
1005 #else
1006 mha_free_acb(sc, acb, xs->xs_control);
1007 #endif
1008
1009 ti->cmds++;
1010 scsipi_done(xs);
1011 }
1012
1013 void
1014 mha_dequeue(sc, acb)
1015 struct mha_softc *sc;
1016 struct acb *acb;
1017 {
1018
1019 if (acb->flags & ACB_QNEXUS) {
1020 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1021 } else {
1022 TAILQ_REMOVE(&sc->ready_list, acb, chain);
1023 }
1024 }
1025
1026 /*
1028 * INTERRUPT/PROTOCOL ENGINE
1029 */
1030
1031 /*
1032 * Schedule an outgoing message by prioritizing it, and asserting
1033 * attention on the bus. We can only do this when we are the initiator
1034 * else there will be an illegal command interrupt.
1035 */
1036 #define mha_sched_msgout(m) \
1037 do { \
1038 SPC_MISC(("mha_sched_msgout %d ", m)); \
1039 CMR = CMD_SET_ATN; \
1040 sc->sc_msgpriq |= (m); \
1041 } while (0)
1042
1043 /*
1044 * Precondition:
1045 * The SCSI bus is already in the MSGI phase and there is a message byte
1046 * on the bus, along with an asserted REQ signal.
1047 */
1048 void
1049 mha_msgin(sc)
1050 register struct mha_softc *sc;
1051 {
1052 register int v;
1053
1054 SPC_TRACE(("[mha_msgin(curmsglen:%d)] ", sc->sc_imlen));
1055
1056 /*
1057 * Prepare for a new message. A message should (according
1058 * to the SCSI standard) be transmitted in one single
1059 * MESSAGE_IN_PHASE. If we have been in some other phase,
1060 * then this is a new message.
1061 */
1062 if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
1063 sc->sc_flags &= ~SPC_DROP_MSGI;
1064 sc->sc_imlen = 0;
1065 }
1066
1067 WAIT;
1068
1069 v = MBR; /* modified byte */
1070 v = sc->sc_pcx[0];
1071
1072 sc->sc_imess[sc->sc_imlen] = v;
1073
1074 /*
1075 * If we're going to reject the message, don't bother storing
1076 * the incoming bytes. But still, we need to ACK them.
1077 */
1078
1079 if ((sc->sc_flags & SPC_DROP_MSGI)) {
1080 CMR = CMD_SET_ATN;
1081 /* ESPCMD(sc, ESPCMD_MSGOK);*/
1082 printf("<dropping msg byte %x>",
1083 sc->sc_imess[sc->sc_imlen]);
1084 return;
1085 }
1086
1087 if (sc->sc_imlen >= SPC_MAX_MSG_LEN) {
1088 mha_sched_msgout(SEND_REJECT);
1089 sc->sc_flags |= SPC_DROP_MSGI;
1090 } else {
1091 sc->sc_imlen++;
1092 /*
1093 * This testing is suboptimal, but most
1094 * messages will be of the one byte variety, so
1095 * it should not effect performance
1096 * significantly.
1097 */
1098 if (sc->sc_imlen == 1 && MSG_IS1BYTE(sc->sc_imess[0]))
1099 goto gotit;
1100 if (sc->sc_imlen == 2 && MSG_IS2BYTE(sc->sc_imess[0]))
1101 goto gotit;
1102 if (sc->sc_imlen >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) &&
1103 sc->sc_imlen == sc->sc_imess[1] + 2)
1104 goto gotit;
1105 }
1106 #if 0
1107 /* Ack what we have so far */
1108 ESPCMD(sc, ESPCMD_MSGOK);
1109 #endif
1110 return;
1111
1112 gotit:
1113 SPC_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
1114 /*
1115 * Now we should have a complete message (1 byte, 2 byte
1116 * and moderately long extended messages). We only handle
1117 * extended messages which total length is shorter than
1118 * SPC_MAX_MSG_LEN. Longer messages will be amputated.
1119 */
1120 if (sc->sc_state == SPC_HASNEXUS) {
1121 struct acb *acb = sc->sc_nexus;
1122 struct spc_tinfo *ti =
1123 &sc->sc_tinfo[acb->xs->xs_periph->periph_target];
1124
1125 switch (sc->sc_imess[0]) {
1126 case MSG_CMDCOMPLETE:
1127 SPC_MSGS(("cmdcomplete "));
1128 if (sc->sc_dleft < 0) {
1129 struct scsipi_periph *periph = acb->xs->xs_periph;
1130 printf("mha: %d extra bytes from %d:%d\n",
1131 -sc->sc_dleft,
1132 periph->periph_target,
1133 periph->periph_lun);
1134 sc->sc_dleft = 0;
1135 }
1136 acb->xs->resid = acb->dleft = sc->sc_dleft;
1137 sc->sc_flags |= SPC_BUSFREE_OK;
1138 break;
1139
1140 case MSG_MESSAGE_REJECT:
1141 #if SPC_DEBUG
1142 if (mha_debug & SPC_SHOWMSGS)
1143 printf("%s: our msg rejected by target\n",
1144 sc->sc_dev.dv_xname);
1145 #endif
1146 #if 1 /* XXX - must remember last message */
1147 scsipi_printaddr(acb->xs->xs_periph);
1148 printf("MSG_MESSAGE_REJECT>>");
1149 #endif
1150 if (sc->sc_flags & SPC_SYNCHNEGO) {
1151 ti->period = ti->offset = 0;
1152 sc->sc_flags &= ~SPC_SYNCHNEGO;
1153 ti->flags &= ~T_NEGOTIATE;
1154 }
1155 /* Not all targets understand INITIATOR_DETECTED_ERR */
1156 if (sc->sc_msgout == SEND_INIT_DET_ERR)
1157 mha_sched_msgout(SEND_ABORT);
1158 break;
1159 case MSG_NOOP:
1160 SPC_MSGS(("noop "));
1161 break;
1162 case MSG_DISCONNECT:
1163 SPC_MSGS(("disconnect "));
1164 ti->dconns++;
1165 sc->sc_flags |= SPC_DISCON;
1166 sc->sc_flags |= SPC_BUSFREE_OK;
1167 if ((acb->xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
1168 break;
1169 /*FALLTHROUGH*/
1170 case MSG_SAVEDATAPOINTER:
1171 SPC_MSGS(("save datapointer "));
1172 acb->dleft = sc->sc_dleft;
1173 acb->daddr = sc->sc_dp;
1174 break;
1175 case MSG_RESTOREPOINTERS:
1176 SPC_MSGS(("restore datapointer "));
1177 if (!acb) {
1178 mha_sched_msgout(SEND_ABORT);
1179 printf("%s: no DATAPOINTERs to restore\n",
1180 sc->sc_dev.dv_xname);
1181 break;
1182 }
1183 sc->sc_dp = acb->daddr;
1184 sc->sc_dleft = acb->dleft;
1185 break;
1186 case MSG_PARITY_ERROR:
1187 printf("%s:target%d: MSG_PARITY_ERROR\n",
1188 sc->sc_dev.dv_xname,
1189 acb->xs->xs_periph->periph_target);
1190 break;
1191 case MSG_EXTENDED:
1192 SPC_MSGS(("extended(%x) ", sc->sc_imess[2]));
1193 switch (sc->sc_imess[2]) {
1194 case MSG_EXT_SDTR:
1195 SPC_MSGS(("SDTR period %d, offset %d ",
1196 sc->sc_imess[3], sc->sc_imess[4]));
1197 ti->period = sc->sc_imess[3];
1198 ti->offset = sc->sc_imess[4];
1199 if (sc->sc_minsync == 0) {
1200 /* We won't do synch */
1201 ti->offset = 0;
1202 mha_sched_msgout(SEND_SDTR);
1203 } else if (ti->offset == 0) {
1204 printf("%s:%d: async\n", "mha",
1205 acb->xs->xs_periph->periph_target);
1206 ti->offset = 0;
1207 sc->sc_flags &= ~SPC_SYNCHNEGO;
1208 } else if (ti->period > 124) {
1209 printf("%s:%d: async\n", "mha",
1210 acb->xs->xs_periph->periph_target);
1211 ti->offset = 0;
1212 mha_sched_msgout(SEND_SDTR);
1213 } else {
1214 #if 0
1215 int p;
1216 p = mha_stp2cpb(sc, ti->period);
1217 ti->period = mha_cpb2stp(sc, p);
1218 #endif
1219
1220 #if SPC_DEBUG
1221 scsipi_printaddr(acb->xs->xs_periph);
1222 #endif
1223 if ((sc->sc_flags&SPC_SYNCHNEGO) == 0) {
1224 /* Target initiated negotiation */
1225 if (ti->flags & T_SYNCMODE) {
1226 ti->flags &= ~T_SYNCMODE;
1227 #if SPC_DEBUG
1228 printf("renegotiated ");
1229 #endif
1230 }
1231 TMR=TM_ASYNC;
1232 /* Clamp to our maxima */
1233 if (ti->period < sc->sc_minsync)
1234 ti->period = sc->sc_minsync;
1235 if (ti->offset > 15)
1236 ti->offset = 15;
1237 mha_sched_msgout(SEND_SDTR);
1238 } else {
1239 /* we are sync */
1240 sc->sc_flags &= ~SPC_SYNCHNEGO;
1241 TMR = TM_SYNC;
1242 ti->flags |= T_SYNCMODE;
1243 }
1244 }
1245 ti->flags &= ~T_NEGOTIATE;
1246 break;
1247 default: /* Extended messages we don't handle */
1248 CMR = CMD_SET_ATN; /* XXX? */
1249 break;
1250 }
1251 break;
1252 default:
1253 SPC_MSGS(("ident "));
1254 /* thanks for that ident... */
1255 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1256 SPC_MISC(("unknown "));
1257 printf("%s: unimplemented message: %d\n", sc->sc_dev.dv_xname, sc->sc_imess[0]);
1258 CMR = CMD_SET_ATN; /* XXX? */
1259 }
1260 break;
1261 }
1262 } else if (sc->sc_state == SPC_RESELECTED) {
1263 struct scsipi_periph *periph = NULL;
1264 struct acb *acb;
1265 struct spc_tinfo *ti;
1266 u_char lunit;
1267
1268 if (MSG_ISIDENTIFY(sc->sc_imess[0])) { /* Identify? */
1269 SPC_MISC(("searching "));
1270 /*
1271 * Search wait queue for disconnected cmd
1272 * The list should be short, so I haven't bothered with
1273 * any more sophisticated structures than a simple
1274 * singly linked list.
1275 */
1276 lunit = sc->sc_imess[0] & 0x07;
1277 for (acb = sc->nexus_list.tqh_first; acb;
1278 acb = acb->chain.tqe_next) {
1279 periph = acb->xs->xs_periph;
1280 if (periph->periph_lun == lunit &&
1281 sc->sc_selid == (1<<periph->periph_target)) {
1282 TAILQ_REMOVE(&sc->nexus_list, acb,
1283 chain);
1284 ACB_SETQ(acb, ACB_QNONE);
1285 break;
1286 }
1287 }
1288
1289 if (!acb) { /* Invalid reselection! */
1290 mha_sched_msgout(SEND_ABORT);
1291 printf("mha: invalid reselect (idbit=0x%2x)\n",
1292 sc->sc_selid);
1293 } else { /* Reestablish nexus */
1294 /*
1295 * Setup driver data structures and
1296 * do an implicit RESTORE POINTERS
1297 */
1298 ti = &sc->sc_tinfo[periph->periph_target];
1299 sc->sc_nexus = acb;
1300 sc->sc_dp = acb->daddr;
1301 sc->sc_dleft = acb->dleft;
1302 sc->sc_tinfo[periph->periph_target].lubusy
1303 |= (1<<periph->periph_lun);
1304 if (ti->flags & T_SYNCMODE) {
1305 TMR = TM_SYNC; /* XXX */
1306 } else {
1307 TMR = TM_ASYNC;
1308 }
1309 SPC_MISC(("... found acb"));
1310 sc->sc_state = SPC_HASNEXUS;
1311 }
1312 } else {
1313 printf("%s: bogus reselect (no IDENTIFY) %0x2x\n",
1314 sc->sc_dev.dv_xname, sc->sc_selid);
1315 mha_sched_msgout(SEND_DEV_RESET);
1316 }
1317 } else { /* Neither SPC_HASNEXUS nor SPC_RESELECTED! */
1318 printf("%s: unexpected message in; will send DEV_RESET\n",
1319 sc->sc_dev.dv_xname);
1320 mha_sched_msgout(SEND_DEV_RESET);
1321 }
1322
1323 /* Ack last message byte */
1324 #if 0
1325 ESPCMD(sc, ESPCMD_MSGOK);
1326 #endif
1327
1328 /* Done, reset message pointer. */
1329 sc->sc_flags &= ~SPC_DROP_MSGI;
1330 sc->sc_imlen = 0;
1331 }
1332
1333 /*
1334 * Send the highest priority, scheduled message.
1335 */
1336 void
1337 mha_msgout(sc)
1338 register struct mha_softc *sc;
1339 {
1340 #if (SPC_USE_SYNCHRONOUS || SPC_USE_WIDE)
1341 struct spc_tinfo *ti;
1342 #endif
1343 int n;
1344
1345 SPC_TRACE(("mha_msgout "));
1346
1347 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1348 if (sc->sc_omp == sc->sc_omess) {
1349 /*
1350 * This is a retransmission.
1351 *
1352 * We get here if the target stayed in MESSAGE OUT
1353 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1354 * that all of the previously transmitted messages must
1355 * be sent again, in the same order. Therefore, we
1356 * requeue all the previously transmitted messages, and
1357 * start again from the top. Our simple priority
1358 * scheme keeps the messages in the right order.
1359 */
1360 SPC_MISC(("retransmitting "));
1361 sc->sc_msgpriq |= sc->sc_msgoutq;
1362 /*
1363 * Set ATN. If we're just sending a trivial 1-byte
1364 * message, we'll clear ATN later on anyway.
1365 */
1366 CMR = CMD_SET_ATN; /* XXX? */
1367 } else {
1368 /* This is a continuation of the previous message. */
1369 n = sc->sc_omp - sc->sc_omess;
1370 goto nextbyte;
1371 }
1372 }
1373
1374 /* No messages transmitted so far. */
1375 sc->sc_msgoutq = 0;
1376 sc->sc_lastmsg = 0;
1377
1378 nextmsg:
1379 /* Pick up highest priority message. */
1380 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1381 sc->sc_msgpriq &= ~sc->sc_currmsg;
1382 sc->sc_msgoutq |= sc->sc_currmsg;
1383
1384 /* Build the outgoing message data. */
1385 switch (sc->sc_currmsg) {
1386 case SEND_IDENTIFY:
1387 SPC_ASSERT(sc->sc_nexus != NULL);
1388 sc->sc_omess[0] =
1389 MSG_IDENTIFY(sc->sc_nexus->xs->xs_periph->periph_lun, 1);
1390 n = 1;
1391 break;
1392
1393 #if SPC_USE_SYNCHRONOUS
1394 case SEND_SDTR:
1395 SPC_ASSERT(sc->sc_nexus != NULL);
1396 ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
1397 sc->sc_omess[4] = MSG_EXTENDED;
1398 sc->sc_omess[3] = 3;
1399 sc->sc_omess[2] = MSG_EXT_SDTR;
1400 sc->sc_omess[1] = ti->period >> 2;
1401 sc->sc_omess[0] = ti->offset;
1402 n = 5;
1403 break;
1404 #endif
1405
1406 #if SPC_USE_WIDE
1407 case SEND_WDTR:
1408 SPC_ASSERT(sc->sc_nexus != NULL);
1409 ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
1410 sc->sc_omess[3] = MSG_EXTENDED;
1411 sc->sc_omess[2] = 2;
1412 sc->sc_omess[1] = MSG_EXT_WDTR;
1413 sc->sc_omess[0] = ti->width;
1414 n = 4;
1415 break;
1416 #endif
1417
1418 case SEND_DEV_RESET:
1419 sc->sc_flags |= SPC_ABORTING;
1420 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1421 n = 1;
1422 break;
1423
1424 case SEND_REJECT:
1425 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1426 n = 1;
1427 break;
1428
1429 case SEND_PARITY_ERROR:
1430 sc->sc_omess[0] = MSG_PARITY_ERROR;
1431 n = 1;
1432 break;
1433
1434 case SEND_INIT_DET_ERR:
1435 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1436 n = 1;
1437 break;
1438
1439 case SEND_ABORT:
1440 sc->sc_flags |= SPC_ABORTING;
1441 sc->sc_omess[0] = MSG_ABORT;
1442 n = 1;
1443 break;
1444
1445 default:
1446 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1447 sc->sc_dev.dv_xname);
1448 SPC_BREAK();
1449 sc->sc_omess[0] = MSG_NOOP;
1450 n = 1;
1451 break;
1452 }
1453 sc->sc_omp = &sc->sc_omess[n];
1454
1455 nextbyte:
1456 /* Send message bytes. */
1457 /* send TRANSFER command. */
1458 sc->sc_ps[3] = 1;
1459 sc->sc_ps[4] = n >> 8;
1460 sc->sc_pc[10] = n;
1461 sc->sc_ps[-1] = 0x000F; /* burst */
1462 asm volatile ("nop");
1463 CMR = CMD_SEND_FROM_DMA; /* send from DMA */
1464 for (;;) {
1465 if ((SSR & SS_BUSY) != 0)
1466 break;
1467 if (SSR & SS_IREQUEST)
1468 goto out;
1469 }
1470 for (;;) {
1471 #if 0
1472 for (;;) {
1473 if ((PSNS & PSNS_REQ) != 0)
1474 break;
1475 /* Wait for REQINIT. XXX Need timeout. */
1476 }
1477 #endif
1478 if (SSR & SS_IREQUEST) {
1479 /*
1480 * Target left MESSAGE OUT, possibly to reject
1481 * our message.
1482 *
1483 * If this is the last message being sent, then we
1484 * deassert ATN, since either the target is going to
1485 * ignore this message, or it's going to ask for a
1486 * retransmission via MESSAGE PARITY ERROR (in which
1487 * case we reassert ATN anyway).
1488 */
1489 #if 0
1490 if (sc->sc_msgpriq == 0)
1491 CMR = CMD_RESET_ATN;
1492 #endif
1493 goto out;
1494 }
1495
1496 #if 0
1497 /* Clear ATN before last byte if this is the last message. */
1498 if (n == 1 && sc->sc_msgpriq == 0)
1499 CMR = CMD_RESET_ATN;
1500 #endif
1501
1502 while ((SSR & SS_DREG_FULL) != 0)
1503 ;
1504 /* Send message byte. */
1505 sc->sc_pc[0] = *--sc->sc_omp;
1506 --n;
1507 /* Keep track of the last message we've sent any bytes of. */
1508 sc->sc_lastmsg = sc->sc_currmsg;
1509
1510 if (n == 0)
1511 break;
1512 }
1513
1514 /* We get here only if the entire message has been transmitted. */
1515 if (sc->sc_msgpriq != 0) {
1516 /* There are more outgoing messages. */
1517 goto nextmsg;
1518 }
1519
1520 /*
1521 * The last message has been transmitted. We need to remember the last
1522 * message transmitted (in case the target switches to MESSAGE IN phase
1523 * and sends a MESSAGE REJECT), and the list of messages transmitted
1524 * this time around (in case the target stays in MESSAGE OUT phase to
1525 * request a retransmit).
1526 */
1527
1528 out:
1529 /* Disable REQ/ACK protocol. */
1530 }
1531
1532
1533 /***************************************************************
1535 *
1536 * datain/dataout
1537 *
1538 */
1539
1540 int
1541 mha_datain_pio(sc, p, n)
1542 register struct mha_softc *sc;
1543 u_char *p;
1544 int n;
1545 {
1546 u_short d;
1547 int a;
1548 int total_n = n;
1549
1550 SPC_TRACE(("[mha_datain_pio(%p,%d)", p, n));
1551
1552 WAIT;
1553 sc->sc_ps[3] = 1;
1554 sc->sc_ps[4] = n >> 8;
1555 sc->sc_pc[10] = n;
1556 /* $BHa$7$-%=%U%HE>Aw(B */
1557 CMR = CMD_RECEIVE_TO_MPU;
1558 for (;;) {
1559 a = SSR;
1560 if (a & 0x04) {
1561 d = sc->sc_ps[0];
1562 *p++ = d >> 8;
1563 if (--n > 0) {
1564 *p++ = d;
1565 --n;
1566 }
1567 a = SSR;
1568 }
1569 if (a & 0x40)
1570 continue;
1571 if (a & 0x80)
1572 break;
1573 }
1574 SPC_TRACE(("...%d resd]", n));
1575 return total_n - n;
1576 }
1577
1578 int
1579 mha_dataout_pio(sc, p, n)
1580 register struct mha_softc *sc;
1581 u_char *p;
1582 int n;
1583 {
1584 u_short d;
1585 int a;
1586 int total_n = n;
1587
1588 SPC_TRACE(("[mha_dataout_pio(%p,%d)", p, n));
1589
1590 WAIT;
1591 sc->sc_ps[3] = 1;
1592 sc->sc_ps[4] = n >> 8;
1593 sc->sc_pc[10] = n;
1594 /* $BHa$7$-%=%U%HE>Aw(B */
1595 CMR = CMD_SEND_FROM_MPU;
1596 for (;;) {
1597 a = SSR;
1598 if (a & 0x04) {
1599 d = *p++ << 8;
1600 if (--n > 0) {
1601 d |= *p++;
1602 --n;
1603 }
1604 sc->sc_ps[0] = d;
1605 a = SSR;
1606 }
1607 if (a & 0x40)
1608 continue;
1609 if (a & 0x80)
1610 break;
1611 }
1612 SPC_TRACE(("...%d resd]", n));
1613 return total_n - n;
1614 }
1615
1616 static int
1617 mha_dataio_dma(dw, cw, sc, p, n)
1618 int dw; /* DMA word */
1619 int cw; /* CMR word */
1620 register struct mha_softc *sc;
1621 u_char *p;
1622 int n;
1623 {
1624 char *paddr;
1625
1626 if (n > MAXBSIZE)
1627 panic("transfer size exceeds MAXBSIZE");
1628 if (sc->sc_dmasize > 0)
1629 panic("DMA request while another DMA transfer is in pregress");
1630
1631 if (cw == CMD_SEND_FROM_DMA) {
1632 memcpy(sc->sc_dmabuf, p, n);
1633 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREWRITE);
1634 } else {
1635 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREREAD);
1636 }
1637 sc->sc_p = p;
1638 sc->sc_dmasize = n;
1639
1640 paddr = (char *)sc->sc_dmaseg[0].ds_addr;
1641 #if MHA_DMA_SHORT_BUS_CYCLE == 1
1642 if ((*(int *)&IODEVbase->io_sram[0xac]) & (1 << ((paddr_t)paddr >> 19)))
1643 dw &= ~(1 << 3);
1644 #endif
1645 sc->sc_pc[0x80 + (((long)paddr >> 16) & 0xFF)] = 0;
1646 sc->sc_pc[0x180 + (((long)paddr >> 8) & 0xFF)] = 0;
1647 sc->sc_pc[0x280 + (((long)paddr >> 0) & 0xFF)] = 0;
1648 WAIT;
1649 sc->sc_ps[3] = 1;
1650 sc->sc_ps[4] = n >> 8;
1651 sc->sc_pc[10] = n;
1652 /* DMA $BE>Aw@)8f$O0J2<$NDL$j!#(B
1653 3 ... short bus cycle
1654 2 ... MAXIMUM XFER.
1655 1 ... BURST XFER.
1656 0 ... R/W */
1657 sc->sc_ps[-1] = dw; /* burst */
1658 asm volatile ("nop");
1659 CMR = cw; /* receive to DMA */
1660 return n;
1661 }
1662 int
1663 mha_dataout(sc, p, n)
1664 register struct mha_softc *sc;
1665 u_char *p;
1666 int n;
1667 {
1668 if (n == 0)
1669 return n;
1670
1671 if (n & 1)
1672 return mha_dataout_pio(sc, p, n);
1673 return mha_dataio_dma(MHA_DMA_DATAOUT, CMD_SEND_FROM_DMA, sc, p, n);
1674 }
1675
1676 int
1678 mha_datain(sc, p, n)
1679 register struct mha_softc *sc;
1680 u_char *p;
1681 int n;
1682 {
1683 register struct acb *acb = sc->sc_nexus;
1684
1685 if (n == 0)
1686 return n;
1687 if (acb->cmd.opcode == REQUEST_SENSE || (n & 1))
1688 return mha_datain_pio(sc, p, n);
1689 return mha_dataio_dma(MHA_DMA_DATAIN, CMD_RECEIVE_TO_DMA, sc, p, n);
1690 }
1691
1692
1694 /*
1695 * Catch an interrupt from the adaptor
1696 */
1697 /*
1698 * This is the workhorse routine of the driver.
1699 * Deficiencies (for now):
1700 * 1) always uses programmed I/O
1701 */
1702 int
1703 mhaintr(arg)
1704 void *arg;
1705 {
1706 struct mha_softc *sc = arg;
1707 #if 0
1708 u_char ints;
1709 #endif
1710 struct acb *acb;
1711 u_char ph;
1712 u_short r;
1713 int n;
1714
1715 #if 1 /* XXX called during attach? */
1716 if (tmpsc != NULL) {
1717 SPC_MISC(("[%p %p]\n", mha_cd.cd_devs, sc));
1718 sc = tmpsc;
1719 } else {
1720 #endif
1721
1722 #if 1 /* XXX */
1723 }
1724 #endif
1725
1726 #if 0
1727 /*
1728 * $B3d$j9~$_6X;_$K$9$k(B
1729 */
1730 SCTL &= ~SCTL_INTR_ENAB;
1731 #endif
1732
1733 SPC_TRACE(("[mhaintr]"));
1734
1735 /*
1736 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
1737 */
1738 /*
1739 * First check for abnormal conditions, such as reset.
1740 */
1741 #if 0
1742 #if 1 /* XXX? */
1743 while (((ints = SSR) & SS_IREQUEST) == 0)
1744 delay(1);
1745 SPC_MISC(("ints = 0x%x ", ints));
1746 #else /* usually? */
1747 ints = SSR;
1748 #endif
1749 #endif
1750 while (SSR & SS_IREQUEST) {
1751 acb = sc->sc_nexus;
1752 r = ISCSR;
1753 SPC_MISC(("[r=0x%x]", r));
1754 switch (r >> 8) {
1755 default:
1756 printf("[addr=%p\n"
1757 "result=0x%x\n"
1758 "cmd=0x%x\n"
1759 "ph=0x%x(ought to be %d)]\n",
1760 &ISCSR,
1761 r,
1762 acb->xs->cmd->opcode,
1763 SCR, sc->sc_phase);
1764 panic("unexpected result.");
1765 case 0x82: /* selection timeout */
1766 SPC_MISC(("selection timeout "));
1767 sc->sc_phase = BUSFREE_PHASE;
1768 SPC_ASSERT(sc->sc_nexus != NULL);
1769 acb = sc->sc_nexus;
1770 delay(250);
1771 acb->xs->error = XS_SELTIMEOUT;
1772 mha_done(sc, acb);
1773 continue; /* XXX ??? msaitoh */
1774 case 0x60: /* command completed */
1775 sc->sc_spcinitialized++;
1776 if (sc->sc_phase == BUSFREE_PHASE)
1777 continue;
1778 ph = SCR;
1779 if (ph & PSNS_ACK) {
1780 int s;
1781 /* $B$U$D!<$N%3%^%s%I$,=*N;$7$?$i$7$$(B */
1782 SPC_MISC(("0x60)phase = %x(ought to be %x)\n",
1783 ph & PHASE_MASK, sc->sc_phase));
1784 #if 0
1785 /* switch (sc->sc_phase) {*/
1786 #else
1787 switch (ph & PHASE_MASK) {
1788 #endif
1789 case STATUS_PHASE:
1790 if (sc->sc_state != SPC_HASNEXUS)
1791 printf("stsin: !SPC_HASNEXUS->(%d)\n",
1792 sc->sc_state);
1793 SPC_ASSERT(sc->sc_nexus != NULL);
1794 acb = sc->sc_nexus;
1795 WAIT;
1796 s = MBR;
1797 SPC_ASSERT(s == 1);
1798 acb->stat = sc->sc_pcx[0]; /* XXX */
1799 SPC_MISC(("stat=0x%02x ", acb->stat));
1800 sc->sc_prevphase = STATUS_PHASE;
1801 break;
1802 case MESSAGE_IN_PHASE:
1803 mha_msgin(sc);
1804 sc->sc_prevphase = MESSAGE_IN_PHASE;
1805 /* thru */
1806 case DATA_IN_PHASE:
1807 if (sc->sc_dmasize == 0)
1808 break;
1809 bus_dmamap_sync(sc->sc_dmat,
1810 sc->sc_dmamap,
1811 0, sc->sc_dmasize,
1812 BUS_DMASYNC_POSTREAD);
1813 memcpy(sc->sc_p, sc->sc_dmabuf,
1814 sc->sc_dmasize);
1815 sc->sc_dmasize = 0;
1816 break;
1817 case DATA_OUT_PHASE:
1818 if (sc->sc_dmasize == 0)
1819 break;
1820 bus_dmamap_sync(sc->sc_dmat,
1821 sc->sc_dmamap,
1822 0, sc->sc_dmasize,
1823 BUS_DMASYNC_POSTWRITE);
1824 sc->sc_dmasize = 0;
1825 break;
1826 }
1827 WAIT;
1828 CMR = CMD_RESET_ACK; /* reset ack */
1829 /*mha_done(sc, acb); XXX */
1830 continue;
1831 } else if (NSR & 0x80) { /* nexus */
1832 #if 1
1833 if (sc->sc_state == SPC_SELECTING) /* XXX msaitoh */
1834 sc->sc_state = SPC_HASNEXUS;
1835 /* $B%U%'!<%:$N7h$aBG$A$r$9$k(B
1836 $B30$l$?$i!"(Binitial-phase error(0x54) $B$,(B
1837 $BJV$C$F$/$k$s$GCm0U$7$?$^$(!#(B
1838 $B$G$b$J$<$+(B 0x65 $B$,JV$C$F$-$?$j$7$F$M!<$+(B? */
1839 WAIT;
1840 if (SSR & SS_IREQUEST)
1841 continue;
1842 switch (sc->sc_phase) {
1843 default:
1844 panic("$B8+CN$i$L(B phase $B$,Mh$A$^$C$?$@$h(B");
1845 case MESSAGE_IN_PHASE:
1846 /* $B2?$b$7$J$$(B */
1847 continue;
1848 case STATUS_PHASE:
1849 sc->sc_phase = MESSAGE_IN_PHASE;
1850 CMR = CMD_RECEIVE_MSG; /* receive msg */
1851 continue;
1852 case DATA_IN_PHASE:
1853 sc->sc_prevphase = DATA_IN_PHASE;
1854 if (sc->sc_dleft == 0) {
1855 /* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
1856 $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
1857 sc->sc_phase = STATUS_PHASE;
1858 CMR = CMD_RECEIVE_STS; /* receive sts */
1859 continue;
1860 }
1861 n = mha_datain(sc, sc->sc_dp,
1862 sc->sc_dleft);
1863 sc->sc_dp += n;
1864 sc->sc_dleft -= n;
1865 continue;
1866 case DATA_OUT_PHASE:
1867 sc->sc_prevphase = DATA_OUT_PHASE;
1868 if (sc->sc_dleft == 0) {
1869 /* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
1870 $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
1871 sc->sc_phase = STATUS_PHASE;
1872 CMR = CMD_RECEIVE_STS; /* receive sts */
1873 continue;
1874 }
1875 /* data phase $B$NB3$-$r$d$m$&(B */
1876 n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1877 sc->sc_dp += n;
1878 sc->sc_dleft -= n;
1879 continue;
1880 case COMMAND_PHASE:
1881 /* $B:G=i$O(B CMD PHASE $B$H$$$&$3$H$i$7$$(B */
1882 if (acb->dleft) {
1883 /* $B%G!<%?E>Aw$,$"$j$&$k>l9g(B */
1884 if (acb->xs->xs_control & XS_CTL_DATA_IN) {
1885 sc->sc_phase = DATA_IN_PHASE;
1886 n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
1887 sc->sc_dp += n;
1888 sc->sc_dleft -= n;
1889 }
1890 else if (acb->xs->xs_control & XS_CTL_DATA_OUT) {
1891 sc->sc_phase = DATA_OUT_PHASE;
1892 n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1893 sc->sc_dp += n;
1894 sc->sc_dleft -= n;
1895 }
1896 continue;
1897 }
1898 else {
1899 /* $B%G!<%?E>Aw$O$J$$$i$7$$(B?! */
1900 WAIT;
1901 sc->sc_phase = STATUS_PHASE;
1902 CMR = CMD_RECEIVE_STS; /* receive sts */
1903 continue;
1904 }
1905 }
1906 #endif
1907 }
1908 continue;
1909 case 0x31: /* disconnected in xfer progress. */
1910 SPC_MISC(("[0x31]"));
1911 case 0x70: /* disconnected. */
1912 SPC_ASSERT(sc->sc_flags & SPC_BUSFREE_OK);
1913 sc->sc_phase = BUSFREE_PHASE;
1914 sc->sc_state = SPC_IDLE;
1915 #if 1
1916 acb = sc->sc_nexus;
1917 SPC_ASSERT(sc->sc_nexus != NULL);
1918 acb->xs->error = XS_NOERROR;
1919 mha_done(sc, acb);
1920 #else
1921 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1922 mha_sched(sc);
1923 #endif
1924 continue;
1925 case 0x32: /* phase error in xfer progress. */
1926 SPC_MISC(("[0x32]"));
1927 #if 0
1928 case 0x65: /* invalid command.
1929 $B$J$<$3$s$J$b$N$,=P$k$N$+(B
1930 $B26$K$OA4$/M}2r$G$-$J$$(B */
1931 #if 1
1932 SPC_MISC(("[0x%04x]", r));
1933 #endif
1934 #endif
1935 case 0x54: /* initial-phase error. */
1936 SPC_MISC(("[0x54, ns=%x, ph=%x(ought to be %x)]",
1937 NSR,
1938 SCR, sc->sc_phase));
1939 /* thru */
1940 case 0x71: /* assert req */
1941 WAIT;
1942 if (SSR & 0x40) {
1943 printf("SPC sts=%2x, r=%04x, ns=%x, ph=%x\n",
1944 SSR, r, NSR, SCR);
1945 WAIT;
1946 }
1947 ph = SCR;
1948 if (sc->sc_state == SPC_SELECTING) { /* XXX msaitoh */
1949 sc->sc_state = SPC_HASNEXUS;
1950 }
1951 if (ph & 0x80) {
1952 switch (ph & PHASE_MASK) {
1953 default:
1954 printf("phase = %x\n", ph);
1955 panic("assert req: the phase I don't know!");
1956 case DATA_IN_PHASE:
1957 sc->sc_prevphase = DATA_IN_PHASE;
1958 SPC_MISC(("DATAIN(%d)...", sc->sc_dleft));
1959 n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
1960 sc->sc_dp += n;
1961 sc->sc_dleft -= n;
1962 SPC_MISC(("done\n"));
1963 continue;
1964 case DATA_OUT_PHASE:
1965 sc->sc_prevphase = DATA_OUT_PHASE;
1966 SPC_MISC(("DATAOUT\n"));
1967 n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1968 sc->sc_dp += n;
1969 sc->sc_dleft -= n;
1970 continue;
1971 case STATUS_PHASE:
1972 sc->sc_phase = STATUS_PHASE;
1973 SPC_MISC(("[RECV_STS]"));
1974 WAIT;
1975 CMR = CMD_RECEIVE_STS; /* receive sts */
1976 continue;
1977 case MESSAGE_IN_PHASE:
1978 sc->sc_phase = MESSAGE_IN_PHASE;
1979 WAIT;
1980 CMR = CMD_RECEIVE_MSG;
1981 continue;
1982 }
1983 }
1984 continue;
1985 }
1986 }
1987
1988 return 1;
1989 }
1990
1991 void
1992 mha_abort(sc, acb)
1993 struct mha_softc *sc;
1994 struct acb *acb;
1995 {
1996 acb->flags |= ACB_ABORTED;
1997
1998 if (acb == sc->sc_nexus) {
1999 /*
2000 * If we're still selecting, the message will be scheduled
2001 * after selection is complete.
2002 */
2003 if (sc->sc_state == SPC_HASNEXUS) {
2004 sc->sc_flags |= SPC_ABORTING;
2005 mha_sched_msgout(SEND_ABORT);
2006 }
2007 } else {
2008 if (sc->sc_state == SPC_IDLE)
2009 mha_sched(sc);
2010 }
2011 }
2012
2013 void
2014 mha_timeout(arg)
2015 void *arg;
2016 {
2017 int s = splbio();
2018 struct acb *acb = (struct acb *)arg;
2019 struct scsipi_xfer *xs = acb->xs;
2020 struct scsipi_periph *periph = xs->xs_periph;
2021 struct mha_softc *sc =
2022 (void*)periph->periph_channel->chan_adapter->adapt_dev;
2023
2024 scsipi_printaddr(periph);
2025 printf("%s: timed out [acb %p (flags 0x%x, dleft %x, stat %x)], "
2026 "<state %d, nexus %p, phase(c %x, p %x), resid %x, msg(q %x,o %x) >",
2027 sc->sc_dev.dv_xname,
2028 acb, acb->flags, acb->dleft, acb->stat,
2029 sc->sc_state, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
2030 sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout
2031 );
2032 printf("[%04x %02x]\n", sc->sc_ps[1], SCR);
2033 panic("timeout, ouch!");
2034
2035 if (acb->flags & ACB_ABORTED) {
2036 /* abort timed out */
2037 printf(" AGAIN\n");
2038 #if 0
2039 mha_init(sc, 1); /* XXX 1?*/
2040 #endif
2041 } else {
2042 /* abort the operation that has timed out */
2043 printf("\n");
2044 xs->error = XS_TIMEOUT;
2045 mha_abort(sc, acb);
2046 }
2047
2048 splx(s);
2049 }
2050
2051 #if SPC_DEBUG
2053 /*
2054 * The following functions are mostly used for debugging purposes, either
2055 * directly called from the driver or from the kernel debugger.
2056 */
2057
2058 void
2059 mha_show_scsi_cmd(acb)
2060 struct acb *acb;
2061 {
2062 u_char *b = (u_char *)&acb->cmd;
2063 struct scsipi_periph *periph = acb->xs->xs_periph;
2064 int i;
2065
2066 scsipi_printaddr(periph);
2067 if ((acb->xs->xs_control & XS_CTL_RESET) == 0) {
2068 for (i = 0; i < acb->clen; i++) {
2069 if (i)
2070 printf(",");
2071 printf("%x", b[i]);
2072 }
2073 printf("\n");
2074 } else
2075 printf("RESET\n");
2076 }
2077
2078 void
2079 mha_print_acb(acb)
2080 struct acb *acb;
2081 {
2082
2083 printf("acb@%p xs=%p flags=%x", acb, acb->xs, acb->flags);
2084 printf(" dp=%p dleft=%d stat=%x\n",
2085 acb->daddr, acb->dleft, acb->stat);
2086 mha_show_scsi_cmd(acb);
2087 }
2088
2089 void
2090 mha_print_active_acb()
2091 {
2092 struct acb *acb;
2093 struct mha_softc *sc = mha_cd.cd_devs[0]; /* XXX */
2094
2095 printf("ready list:\n");
2096 for (acb = sc->ready_list.tqh_first; acb != NULL;
2097 acb = acb->chain.tqe_next)
2098 mha_print_acb(acb);
2099 printf("nexus:\n");
2100 if (sc->sc_nexus != NULL)
2101 mha_print_acb(sc->sc_nexus);
2102 printf("nexus list:\n");
2103 for (acb = sc->nexus_list.tqh_first; acb != NULL;
2104 acb = acb->chain.tqe_next)
2105 mha_print_acb(acb);
2106 }
2107
2108 void
2109 mha_dump_driver(sc)
2110 struct mha_softc *sc;
2111 {
2112 struct spc_tinfo *ti;
2113 int i;
2114
2115 printf("nexus=%p prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2116 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2117 sc->sc_state, sc->sc_imess[0],
2118 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2119 for (i = 0; i < 7; i++) {
2120 ti = &sc->sc_tinfo[i];
2121 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2122 i, ti->cmds, ti->dconns, ti->touts);
2123 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2124 }
2125 }
2126 #endif
2127