mha.c revision 1.22 1 /* $NetBSD: mha.c,v 1.22 2001/04/25 17:53:26 bouyer 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 unsigned a = 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 bzero(acb, 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 bzero(&sc->sc_tinfo, 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 struct spc_tinfo *ti = &sc->sc_tinfo[target];
582 int i;
583 int s;
584
585 s = splbio(); /* XXX */
586
587 SPC_TRACE(("[mhaselect(t%d,l%d,cmd:%x)] ", target, lun, *(u_char *)cmd));
588
589 /* CDB $B$r(B SPC $B$N(B MCS REG $B$K%;%C%H$9$k(B */
590 /* Now the command into the FIFO */
591 WAIT;
592 #if 1
593 SPC_MISC(("[cmd:"));
594 for (i = 0; i < clen; i++)
595 {
596 unsigned c = cmd[i];
597 if (i == 1)
598 c |= lun << 5;
599 SPC_MISC((" %02x", c));
600 sc->sc_pcx[i] = c;
601 }
602 SPC_MISC(("], target=%d\n", target));
603 #else
604 bcopy(cmd, sc->sc_pcx, clen);
605 #endif
606 if (NSR & 0x80)
607 panic("scsistart: already selected...");
608 sc->sc_phase = COMMAND_PHASE;
609
610 /* new state ASP_SELECTING */
611 sc->sc_state = SPC_SELECTING;
612
613 SIR = target;
614 #if 0
615 CMR = CMD_SELECT;
616 #else
617 CMR = CMD_SEL_AND_CMD; /* select & cmd */
618 #endif
619 splx(s);
620 }
621
622 #if 0
623 int
624 mha_reselect(sc, message)
625 struct mha_softc *sc;
626 u_char message;
627 {
628 u_char selid, target, lun;
629 struct acb *acb;
630 struct scsipi_periph *periph;
631 struct spc_tinfo *ti;
632
633 /*
634 * The SCSI chip made a snapshot of the data bus while the reselection
635 * was being negotiated. This enables us to determine which target did
636 * the reselect.
637 */
638 selid = sc->sc_selid & ~(1 << sc->sc_id);
639 if (selid & (selid - 1)) {
640 printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
641 sc->sc_dev.dv_xname, selid);
642 SPC_BREAK();
643 goto reset;
644 }
645
646 /*
647 * Search wait queue for disconnected cmd
648 * The list should be short, so I haven't bothered with
649 * any more sophisticated structures than a simple
650 * singly linked list.
651 */
652 target = ffs(selid) - 1;
653 lun = message & 0x07;
654 for (acb = sc->nexus_list.tqh_first; acb != NULL;
655 acb = acb->chain.tqe_next) {
656 periph = acb->xs->xs_periph;
657 if (periph->periph_target == target &&
658 periph->periph_lun == lun)
659 break;
660 }
661 if (acb == NULL) {
662 printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
663 sc->sc_dev.dv_xname, target, lun);
664 SPC_BREAK();
665 goto abort;
666 }
667
668 /* Make this nexus active again. */
669 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
670 sc->sc_state = SPC_HASNEXUS;
671 sc->sc_nexus = acb;
672 ti = &sc->sc_tinfo[target];
673 ti->lubusy |= (1 << lun);
674 mha_setsync(sc, ti);
675
676 if (acb->flags & ACB_RESET)
677 mha_sched_msgout(sc, SEND_DEV_RESET);
678 else if (acb->flags & ACB_ABORTED)
679 mha_sched_msgout(sc, SEND_ABORT);
680
681 /* Do an implicit RESTORE POINTERS. */
682 sc->sc_dp = acb->daddr;
683 sc->sc_dleft = acb->dleft;
684 sc->sc_cp = (u_char *)&acb->cmd;
685 sc->sc_cleft = acb->clen;
686
687 return (0);
688
689 reset:
690 mha_sched_msgout(sc, SEND_DEV_RESET);
691 return (1);
692
693 abort:
694 mha_sched_msgout(sc, SEND_ABORT);
695 return (1);
696 }
697 #endif
698 /*
699 * Start a SCSI-command
700 * This function is called by the higher level SCSI-driver to queue/run
701 * SCSI-commands.
702 */
703 void
704 mha_scsi_request(chan, req, arg)
705 struct scsipi_channel *chan;
706 scsipi_adapter_req_t req;
707 void *arg;
708 {
709 struct scsipi_xfer *xs;
710 struct scsipi_periph *periph;
711 struct mha_softc *sc = (void *)chan->chan_adapter->adapt_dev;
712 struct acb *acb;
713 int s, flags;
714
715 switch (req) {
716 case ADAPTER_REQ_RUN_XFER:
717 xs = arg;
718 periph = xs->xs_periph;
719
720 SPC_TRACE(("[mha_scsi_cmd] "));
721 SPC_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
722 periph->periph_target));
723
724 flags = xs->xs_control;
725
726 /* Get a mha command block */
727 s = splbio();
728 acb = sc->free_list.tqh_first;
729 if (acb) {
730 TAILQ_REMOVE(&sc->free_list, acb, chain);
731 ACB_SETQ(acb, ACB_QNONE);
732 }
733
734 if (acb == NULL) {
735 xs->error = XS_RESOURCE_SHORTAGE;
736 scsipi_done(xs);
737 splx(s);
738 return;
739 }
740 splx(s);
741
742 /* Initialize acb */
743 acb->xs = xs;
744 bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
745 acb->clen = xs->cmdlen;
746 acb->daddr = xs->data;
747 acb->dleft = xs->datalen;
748 acb->stat = 0;
749
750 s = splbio();
751 ACB_SETQ(acb, ACB_QREADY);
752 TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
753 #if 1
754 callout_reset(&acb->xs->xs_callout,
755 ((u_int64_t)xs->timeout * (u_int64_t)hz) / 1000,
756 mha_timeout, acb);
757 #endif
758
759 /*
760 * $B%-%e!<$N=hM}Cf$G$J$1$l$P!"%9%1%8%e!<%j%s%03+;O$9$k(B
761 */
762 if (sc->sc_state == SPC_IDLE)
763 mha_sched(sc);
764
765 splx(s);
766
767 if (flags & XS_CTL_POLL) {
768 /* Not allowed to use interrupts, use polling instead */
769 mha_poll(sc, acb);
770 }
771
772 SPC_MISC(("SUCCESSFULLY_QUEUED"));
773 return;
774
775 case ADAPTER_REQ_GROW_RESOURCES:
776 /* XXX Not supported. */
777 return;
778
779 case ADAPTER_REQ_SET_XFER_MODE:
780 /* XXX Not supported. */
781 return;
782 }
783 }
784
785 /*
786 * Adjust transfer size in buffer structure
787 */
788 void
789 mha_minphys(bp)
790 struct buf *bp;
791 {
792
793 SPC_TRACE(("mha_minphys "));
794 minphys(bp);
795 }
796
797 /*
798 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
799 */
800 void
801 mha_poll(sc, acb)
802 struct mha_softc *sc;
803 struct acb *acb;
804 {
805 struct scsipi_xfer *xs = acb->xs;
806 int count = xs->timeout * 100;
807 int s = splbio();
808
809 SPC_TRACE(("[mha_poll] "));
810
811 while (count) {
812 /*
813 * If we had interrupts enabled, would we
814 * have got an interrupt?
815 */
816 if (SSR & SS_IREQUEST)
817 mhaintr(sc);
818 if ((xs->xs_status & XS_STS_DONE) != 0)
819 break;
820 DELAY(10);
821 #if 1
822 if (sc->sc_state == SPC_IDLE) {
823 SPC_TRACE(("[mha_poll: rescheduling] "));
824 mha_sched(sc);
825 }
826 #endif
827 count--;
828 }
829
830 if (count == 0) {
831 SPC_MISC(("mha_poll: timeout"));
832 mha_timeout((caddr_t)acb);
833 }
834 splx(s);
835 scsipi_done(xs);
836 }
837
838 /*
840 * LOW LEVEL SCSI UTILITIES
841 */
842
843 /*
844 * Set synchronous transfer offset and period.
845 */
846 inline void
847 mha_setsync(sc, ti)
848 struct mha_softc *sc;
849 struct spc_tinfo *ti;
850 {
851 }
852
853
854 /*
856 * Schedule a SCSI operation. This has now been pulled out of the interrupt
857 * handler so that we may call it from mha_scsi_cmd and mha_done. This may
858 * save us an unecessary interrupt just to get things going. Should only be
859 * called when state == SPC_IDLE and at bio pl.
860 */
861 void
862 mha_sched(sc)
863 register struct mha_softc *sc;
864 {
865 struct scsipi_periph *periph;
866 struct acb *acb;
867 int t;
868
869 SPC_TRACE(("[mha_sched] "));
870 if (sc->sc_state != SPC_IDLE)
871 panic("mha_sched: not IDLE (state=%d)", sc->sc_state);
872
873 if (sc->sc_flags & SPC_ABORTING)
874 return;
875
876 /*
877 * Find first acb in ready queue that is for a target/lunit
878 * combinations that is not busy.
879 */
880 for (acb = sc->ready_list.tqh_first; acb ; acb = acb->chain.tqe_next) {
881 struct spc_tinfo *ti;
882 periph = acb->xs->xs_periph;
883 t = periph->periph_target;
884 ti = &sc->sc_tinfo[t];
885 if (!(ti->lubusy & (1 << periph->periph_lun))) {
886 if ((acb->flags & ACB_QBITS) != ACB_QREADY)
887 panic("mha: busy entry on ready list");
888 TAILQ_REMOVE(&sc->ready_list, acb, chain);
889 ACB_SETQ(acb, ACB_QNONE);
890 sc->sc_nexus = acb;
891 sc->sc_flags = 0;
892 sc->sc_prevphase = INVALID_PHASE;
893 sc->sc_dp = acb->daddr;
894 sc->sc_dleft = acb->dleft;
895 ti->lubusy |= (1<<periph->periph_lun);
896 mhaselect(sc, t, periph->periph_lun,
897 (u_char *)&acb->cmd, acb->clen);
898 break;
899 } else {
900 SPC_MISC(("%d:%d busy\n",
901 periph->periph_target,
902 periph->periph_lun));
903 }
904 }
905 }
906
907 /*
909 * POST PROCESSING OF SCSI_CMD (usually current)
910 */
911 void
912 mha_done(sc, acb)
913 struct mha_softc *sc;
914 struct acb *acb;
915 {
916 struct scsipi_xfer *xs = acb->xs;
917 struct scsipi_periph *periph = xs->xs_periph;
918 struct spc_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
919
920 SPC_TRACE(("[mha_done(error:%x)] ", xs->error));
921
922 #if 1
923 callout_stop(&acb->xs->xs_callout);
924 #endif
925
926 /*
927 * Now, if we've come here with no error code, i.e. we've kept the
928 * initial XS_NOERROR, and the status code signals that we should
929 * check sense, we'll need to set up a request sense cmd block and
930 * push the command back into the ready queue *before* any other
931 * commands for this target/lunit, else we lose the sense info.
932 * We don't support chk sense conditions for the request sense cmd.
933 */
934 if (xs->error == XS_NOERROR) {
935 if ((acb->flags & ACB_ABORTED) != 0) {
936 xs->error = XS_TIMEOUT;
937 } else if (acb->flags & ACB_CHKSENSE) {
938 xs->error = XS_SENSE;
939 } else {
940 xs->status = acb->stat & ST_MASK;
941 switch (xs->status) {
942 case SCSI_CHECK:
943 xs->resid = acb->dleft;
944 /* FALLTHOUGH */
945 case SCSI_BUSY:
946 xs->error = XS_BUSY;
947 break;
948 case SCSI_OK:
949 xs->resid = acb->dleft;
950 break;
951 default:
952 xs->error = XS_DRIVER_STUFFUP;
953 #if SPC_DEBUG
954 printf("%s: mha_done: bad stat 0x%x\n",
955 sc->sc_dev.dv_xname, acb->stat);
956 #endif
957 break;
958 }
959 }
960 }
961
962 #if SPC_DEBUG
963 if ((mha_debug & SPC_SHOWMISC) != 0) {
964 if (xs->resid != 0)
965 printf("resid=%d ", xs->resid);
966 if (xs->error == XS_SENSE)
967 printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
968 else
969 printf("error=%d\n", xs->error);
970 }
971 #endif
972
973 /*
974 * Remove the ACB from whatever queue it's on.
975 */
976 switch (acb->flags & ACB_QBITS) {
977 case ACB_QNONE:
978 if (acb != sc->sc_nexus) {
979 panic("%s: floating acb", sc->sc_dev.dv_xname);
980 }
981 sc->sc_nexus = NULL;
982 sc->sc_state = SPC_IDLE;
983 ti->lubusy &= ~(1<<periph->periph_lun);
984 mha_sched(sc);
985 break;
986 case ACB_QREADY:
987 TAILQ_REMOVE(&sc->ready_list, acb, chain);
988 break;
989 case ACB_QNEXUS:
990 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
991 ti->lubusy &= ~(1<<periph->periph_lun);
992 break;
993 case ACB_QFREE:
994 panic("%s: dequeue: busy acb on free list",
995 sc->sc_dev.dv_xname);
996 break;
997 default:
998 panic("%s: dequeue: unknown queue %d",
999 sc->sc_dev.dv_xname, acb->flags & ACB_QBITS);
1000 }
1001
1002 /* Put it on the free list, and clear flags. */
1003 #if 0
1004 TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
1005 acb->flags = ACB_QFREE;
1006 #else
1007 mha_free_acb(sc, acb, xs->xs_control);
1008 #endif
1009
1010 ti->cmds++;
1011 scsipi_done(xs);
1012 }
1013
1014 void
1015 mha_dequeue(sc, acb)
1016 struct mha_softc *sc;
1017 struct acb *acb;
1018 {
1019
1020 if (acb->flags & ACB_QNEXUS) {
1021 TAILQ_REMOVE(&sc->nexus_list, acb, chain);
1022 } else {
1023 TAILQ_REMOVE(&sc->ready_list, acb, chain);
1024 }
1025 }
1026
1027 /*
1029 * INTERRUPT/PROTOCOL ENGINE
1030 */
1031
1032 /*
1033 * Schedule an outgoing message by prioritizing it, and asserting
1034 * attention on the bus. We can only do this when we are the initiator
1035 * else there will be an illegal command interrupt.
1036 */
1037 #define mha_sched_msgout(m) \
1038 do { \
1039 SPC_MISC(("mha_sched_msgout %d ", m)); \
1040 CMR = CMD_SET_ATN; \
1041 sc->sc_msgpriq |= (m); \
1042 } while (0)
1043
1044 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
1045 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
1046 #define ISEXTMSG(m) ((m) == 0x01)
1047
1048 /*
1049 * Precondition:
1050 * The SCSI bus is already in the MSGI phase and there is a message byte
1051 * on the bus, along with an asserted REQ signal.
1052 */
1053 void
1054 mha_msgin(sc)
1055 register struct mha_softc *sc;
1056 {
1057 register int v;
1058 int n;
1059
1060 SPC_TRACE(("[mha_msgin(curmsglen:%d)] ", sc->sc_imlen));
1061
1062 /*
1063 * Prepare for a new message. A message should (according
1064 * to the SCSI standard) be transmitted in one single
1065 * MESSAGE_IN_PHASE. If we have been in some other phase,
1066 * then this is a new message.
1067 */
1068 if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
1069 sc->sc_flags &= ~SPC_DROP_MSGI;
1070 sc->sc_imlen = 0;
1071 }
1072
1073 WAIT;
1074
1075 v = MBR; /* modified byte */
1076 v = sc->sc_pcx[0];
1077
1078 sc->sc_imess[sc->sc_imlen] = v;
1079
1080 /*
1081 * If we're going to reject the message, don't bother storing
1082 * the incoming bytes. But still, we need to ACK them.
1083 */
1084
1085 if ((sc->sc_flags & SPC_DROP_MSGI)) {
1086 CMR = CMD_SET_ATN;
1087 /* ESPCMD(sc, ESPCMD_MSGOK);*/
1088 printf("<dropping msg byte %x>",
1089 sc->sc_imess[sc->sc_imlen]);
1090 return;
1091 }
1092
1093 if (sc->sc_imlen >= SPC_MAX_MSG_LEN) {
1094 mha_sched_msgout(SEND_REJECT);
1095 sc->sc_flags |= SPC_DROP_MSGI;
1096 } else {
1097 sc->sc_imlen++;
1098 /*
1099 * This testing is suboptimal, but most
1100 * messages will be of the one byte variety, so
1101 * it should not effect performance
1102 * significantly.
1103 */
1104 if (sc->sc_imlen == 1 && IS1BYTEMSG(sc->sc_imess[0]))
1105 goto gotit;
1106 if (sc->sc_imlen == 2 && IS2BYTEMSG(sc->sc_imess[0]))
1107 goto gotit;
1108 if (sc->sc_imlen >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
1109 sc->sc_imlen == sc->sc_imess[1] + 2)
1110 goto gotit;
1111 }
1112 #if 0
1113 /* Ack what we have so far */
1114 ESPCMD(sc, ESPCMD_MSGOK);
1115 #endif
1116 return;
1117
1118 gotit:
1119 SPC_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
1120 /*
1121 * Now we should have a complete message (1 byte, 2 byte
1122 * and moderately long extended messages). We only handle
1123 * extended messages which total length is shorter than
1124 * SPC_MAX_MSG_LEN. Longer messages will be amputated.
1125 */
1126 if (sc->sc_state == SPC_HASNEXUS) {
1127 struct acb *acb = sc->sc_nexus;
1128 struct spc_tinfo *ti =
1129 &sc->sc_tinfo[acb->xs->xs_periph->periph_target];
1130
1131 switch (sc->sc_imess[0]) {
1132 case MSG_CMDCOMPLETE:
1133 SPC_MSGS(("cmdcomplete "));
1134 if (sc->sc_dleft < 0) {
1135 struct scsipi_periph *periph = acb->xs->xs_periph;
1136 printf("mha: %d extra bytes from %d:%d\n",
1137 -sc->sc_dleft,
1138 periph->periph_target,
1139 periph->periph_lun);
1140 sc->sc_dleft = 0;
1141 }
1142 acb->xs->resid = acb->dleft = sc->sc_dleft;
1143 sc->sc_flags |= SPC_BUSFREE_OK;
1144 break;
1145
1146 case MSG_MESSAGE_REJECT:
1147 #if SPC_DEBUG
1148 if (mha_debug & SPC_SHOWMSGS)
1149 printf("%s: our msg rejected by target\n",
1150 sc->sc_dev.dv_xname);
1151 #endif
1152 #if 1 /* XXX - must remember last message */
1153 scsipi_printaddr(acb->xs->xs_periph);
1154 printf("MSG_MESSAGE_REJECT>>");
1155 #endif
1156 if (sc->sc_flags & SPC_SYNCHNEGO) {
1157 ti->period = ti->offset = 0;
1158 sc->sc_flags &= ~SPC_SYNCHNEGO;
1159 ti->flags &= ~T_NEGOTIATE;
1160 }
1161 /* Not all targets understand INITIATOR_DETECTED_ERR */
1162 if (sc->sc_msgout == SEND_INIT_DET_ERR)
1163 mha_sched_msgout(SEND_ABORT);
1164 break;
1165 case MSG_NOOP:
1166 SPC_MSGS(("noop "));
1167 break;
1168 case MSG_DISCONNECT:
1169 SPC_MSGS(("disconnect "));
1170 ti->dconns++;
1171 sc->sc_flags |= SPC_DISCON;
1172 sc->sc_flags |= SPC_BUSFREE_OK;
1173 if ((acb->xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
1174 break;
1175 /*FALLTHROUGH*/
1176 case MSG_SAVEDATAPOINTER:
1177 SPC_MSGS(("save datapointer "));
1178 acb->dleft = sc->sc_dleft;
1179 acb->daddr = sc->sc_dp;
1180 break;
1181 case MSG_RESTOREPOINTERS:
1182 SPC_MSGS(("restore datapointer "));
1183 if (!acb) {
1184 mha_sched_msgout(SEND_ABORT);
1185 printf("%s: no DATAPOINTERs to restore\n",
1186 sc->sc_dev.dv_xname);
1187 break;
1188 }
1189 sc->sc_dp = acb->daddr;
1190 sc->sc_dleft = acb->dleft;
1191 break;
1192 case MSG_PARITY_ERROR:
1193 printf("%s:target%d: MSG_PARITY_ERROR\n",
1194 sc->sc_dev.dv_xname,
1195 acb->xs->xs_periph->periph_target);
1196 break;
1197 case MSG_EXTENDED:
1198 SPC_MSGS(("extended(%x) ", sc->sc_imess[2]));
1199 switch (sc->sc_imess[2]) {
1200 case MSG_EXT_SDTR:
1201 SPC_MSGS(("SDTR period %d, offset %d ",
1202 sc->sc_imess[3], sc->sc_imess[4]));
1203 ti->period = sc->sc_imess[3];
1204 ti->offset = sc->sc_imess[4];
1205 if (sc->sc_minsync == 0) {
1206 /* We won't do synch */
1207 ti->offset = 0;
1208 mha_sched_msgout(SEND_SDTR);
1209 } else if (ti->offset == 0) {
1210 printf("%s:%d: async\n", "mha",
1211 acb->xs->xs_periph->periph_target);
1212 ti->offset = 0;
1213 sc->sc_flags &= ~SPC_SYNCHNEGO;
1214 } else if (ti->period > 124) {
1215 printf("%s:%d: async\n", "mha",
1216 acb->xs->xs_periph->periph_target);
1217 ti->offset = 0;
1218 mha_sched_msgout(SEND_SDTR);
1219 } else {
1220 int r = 250/ti->period;
1221 int s = (100*250)/ti->period - 100*r;
1222 int p;
1223 #if 0
1224 p = mha_stp2cpb(sc, ti->period);
1225 ti->period = mha_cpb2stp(sc, p);
1226 #endif
1227
1228 #if SPC_DEBUG
1229 scsipi_printaddr(acb->xs->xs_periph);
1230 #endif
1231 if ((sc->sc_flags&SPC_SYNCHNEGO) == 0) {
1232 /* Target initiated negotiation */
1233 if (ti->flags & T_SYNCMODE) {
1234 ti->flags &= ~T_SYNCMODE;
1235 #if SPC_DEBUG
1236 printf("renegotiated ");
1237 #endif
1238 }
1239 TMR=TM_ASYNC;
1240 /* Clamp to our maxima */
1241 if (ti->period < sc->sc_minsync)
1242 ti->period = sc->sc_minsync;
1243 if (ti->offset > 15)
1244 ti->offset = 15;
1245 mha_sched_msgout(SEND_SDTR);
1246 } else {
1247 /* we are sync */
1248 sc->sc_flags &= ~SPC_SYNCHNEGO;
1249 TMR = TM_SYNC;
1250 ti->flags |= T_SYNCMODE;
1251 }
1252 #if SPC_DEBUG
1253 printf("max sync rate %d.%02dMb/s\n",
1254 r, s);
1255 #endif
1256 }
1257 ti->flags &= ~T_NEGOTIATE;
1258 break;
1259 default: /* Extended messages we don't handle */
1260 CMR = CMD_SET_ATN; /* XXX? */
1261 break;
1262 }
1263 break;
1264 default:
1265 SPC_MSGS(("ident "));
1266 /* thanks for that ident... */
1267 if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
1268 SPC_MISC(("unknown "));
1269 printf("%s: unimplemented message: %d\n", sc->sc_dev.dv_xname, sc->sc_imess[0]);
1270 CMR = CMD_SET_ATN; /* XXX? */
1271 }
1272 break;
1273 }
1274 } else if (sc->sc_state == SPC_RESELECTED) {
1275 struct scsipi_periph *periph = NULL;
1276 struct acb *acb;
1277 struct spc_tinfo *ti;
1278 u_char lunit;
1279
1280 if (MSG_ISIDENTIFY(sc->sc_imess[0])) { /* Identify? */
1281 SPC_MISC(("searching "));
1282 /*
1283 * Search wait queue for disconnected cmd
1284 * The list should be short, so I haven't bothered with
1285 * any more sophisticated structures than a simple
1286 * singly linked list.
1287 */
1288 lunit = sc->sc_imess[0] & 0x07;
1289 for (acb = sc->nexus_list.tqh_first; acb;
1290 acb = acb->chain.tqe_next) {
1291 periph = acb->xs->xs_periph;
1292 if (periph->periph_lun == lunit &&
1293 sc->sc_selid == (1<<periph->periph_target)) {
1294 TAILQ_REMOVE(&sc->nexus_list, acb,
1295 chain);
1296 ACB_SETQ(acb, ACB_QNONE);
1297 break;
1298 }
1299 }
1300
1301 if (!acb) { /* Invalid reselection! */
1302 mha_sched_msgout(SEND_ABORT);
1303 printf("mha: invalid reselect (idbit=0x%2x)\n",
1304 sc->sc_selid);
1305 } else { /* Reestablish nexus */
1306 /*
1307 * Setup driver data structures and
1308 * do an implicit RESTORE POINTERS
1309 */
1310 ti = &sc->sc_tinfo[periph->periph_target];
1311 sc->sc_nexus = acb;
1312 sc->sc_dp = acb->daddr;
1313 sc->sc_dleft = acb->dleft;
1314 sc->sc_tinfo[periph->periph_target].lubusy
1315 |= (1<<periph->periph_lun);
1316 if (ti->flags & T_SYNCMODE) {
1317 TMR = TM_SYNC; /* XXX */
1318 } else {
1319 TMR = TM_ASYNC;
1320 }
1321 SPC_MISC(("... found acb"));
1322 sc->sc_state = SPC_HASNEXUS;
1323 }
1324 } else {
1325 printf("%s: bogus reselect (no IDENTIFY) %0x2x\n",
1326 sc->sc_dev.dv_xname, sc->sc_selid);
1327 mha_sched_msgout(SEND_DEV_RESET);
1328 }
1329 } else { /* Neither SPC_HASNEXUS nor SPC_RESELECTED! */
1330 printf("%s: unexpected message in; will send DEV_RESET\n",
1331 sc->sc_dev.dv_xname);
1332 mha_sched_msgout(SEND_DEV_RESET);
1333 }
1334
1335 /* Ack last message byte */
1336 #if 0
1337 ESPCMD(sc, ESPCMD_MSGOK);
1338 #endif
1339
1340 /* Done, reset message pointer. */
1341 sc->sc_flags &= ~SPC_DROP_MSGI;
1342 sc->sc_imlen = 0;
1343 }
1344
1345 /*
1346 * Send the highest priority, scheduled message.
1347 */
1348 void
1349 mha_msgout(sc)
1350 register struct mha_softc *sc;
1351 {
1352 struct spc_tinfo *ti;
1353 int n;
1354
1355 SPC_TRACE(("mha_msgout "));
1356
1357 if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
1358 if (sc->sc_omp == sc->sc_omess) {
1359 /*
1360 * This is a retransmission.
1361 *
1362 * We get here if the target stayed in MESSAGE OUT
1363 * phase. Section 5.1.9.2 of the SCSI 2 spec indicates
1364 * that all of the previously transmitted messages must
1365 * be sent again, in the same order. Therefore, we
1366 * requeue all the previously transmitted messages, and
1367 * start again from the top. Our simple priority
1368 * scheme keeps the messages in the right order.
1369 */
1370 SPC_MISC(("retransmitting "));
1371 sc->sc_msgpriq |= sc->sc_msgoutq;
1372 /*
1373 * Set ATN. If we're just sending a trivial 1-byte
1374 * message, we'll clear ATN later on anyway.
1375 */
1376 CMR = CMD_SET_ATN; /* XXX? */
1377 } else {
1378 /* This is a continuation of the previous message. */
1379 n = sc->sc_omp - sc->sc_omess;
1380 goto nextbyte;
1381 }
1382 }
1383
1384 /* No messages transmitted so far. */
1385 sc->sc_msgoutq = 0;
1386 sc->sc_lastmsg = 0;
1387
1388 nextmsg:
1389 /* Pick up highest priority message. */
1390 sc->sc_currmsg = sc->sc_msgpriq & -sc->sc_msgpriq;
1391 sc->sc_msgpriq &= ~sc->sc_currmsg;
1392 sc->sc_msgoutq |= sc->sc_currmsg;
1393
1394 /* Build the outgoing message data. */
1395 switch (sc->sc_currmsg) {
1396 case SEND_IDENTIFY:
1397 SPC_ASSERT(sc->sc_nexus != NULL);
1398 sc->sc_omess[0] =
1399 MSG_IDENTIFY(sc->sc_nexus->xs->xs_periph->periph_lun, 1);
1400 n = 1;
1401 break;
1402
1403 #if SPC_USE_SYNCHRONOUS
1404 case SEND_SDTR:
1405 SPC_ASSERT(sc->sc_nexus != NULL);
1406 ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
1407 sc->sc_omess[4] = MSG_EXTENDED;
1408 sc->sc_omess[3] = 3;
1409 sc->sc_omess[2] = MSG_EXT_SDTR;
1410 sc->sc_omess[1] = ti->period >> 2;
1411 sc->sc_omess[0] = ti->offset;
1412 n = 5;
1413 break;
1414 #endif
1415
1416 #if SPC_USE_WIDE
1417 case SEND_WDTR:
1418 SPC_ASSERT(sc->sc_nexus != NULL);
1419 ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
1420 sc->sc_omess[3] = MSG_EXTENDED;
1421 sc->sc_omess[2] = 2;
1422 sc->sc_omess[1] = MSG_EXT_WDTR;
1423 sc->sc_omess[0] = ti->width;
1424 n = 4;
1425 break;
1426 #endif
1427
1428 case SEND_DEV_RESET:
1429 sc->sc_flags |= SPC_ABORTING;
1430 sc->sc_omess[0] = MSG_BUS_DEV_RESET;
1431 n = 1;
1432 break;
1433
1434 case SEND_REJECT:
1435 sc->sc_omess[0] = MSG_MESSAGE_REJECT;
1436 n = 1;
1437 break;
1438
1439 case SEND_PARITY_ERROR:
1440 sc->sc_omess[0] = MSG_PARITY_ERROR;
1441 n = 1;
1442 break;
1443
1444 case SEND_INIT_DET_ERR:
1445 sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
1446 n = 1;
1447 break;
1448
1449 case SEND_ABORT:
1450 sc->sc_flags |= SPC_ABORTING;
1451 sc->sc_omess[0] = MSG_ABORT;
1452 n = 1;
1453 break;
1454
1455 default:
1456 printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
1457 sc->sc_dev.dv_xname);
1458 SPC_BREAK();
1459 sc->sc_omess[0] = MSG_NOOP;
1460 n = 1;
1461 break;
1462 }
1463 sc->sc_omp = &sc->sc_omess[n];
1464
1465 nextbyte:
1466 /* Send message bytes. */
1467 /* send TRANSFER command. */
1468 sc->sc_ps[3] = 1;
1469 sc->sc_ps[4] = n >> 8;
1470 sc->sc_pc[10] = n;
1471 sc->sc_ps[-1] = 0x000F; /* burst */
1472 asm volatile ("nop");
1473 CMR = CMD_SEND_FROM_DMA; /* send from DMA */
1474 for (;;) {
1475 if ((SSR & SS_BUSY) != 0)
1476 break;
1477 if (SSR & SS_IREQUEST)
1478 goto out;
1479 }
1480 for (;;) {
1481 #if 0
1482 for (;;) {
1483 if ((PSNS & PSNS_REQ) != 0)
1484 break;
1485 /* Wait for REQINIT. XXX Need timeout. */
1486 }
1487 #endif
1488 if (SSR & SS_IREQUEST) {
1489 /*
1490 * Target left MESSAGE OUT, possibly to reject
1491 * our message.
1492 *
1493 * If this is the last message being sent, then we
1494 * deassert ATN, since either the target is going to
1495 * ignore this message, or it's going to ask for a
1496 * retransmission via MESSAGE PARITY ERROR (in which
1497 * case we reassert ATN anyway).
1498 */
1499 #if 0
1500 if (sc->sc_msgpriq == 0)
1501 CMR = CMD_RESET_ATN;
1502 #endif
1503 goto out;
1504 }
1505
1506 #if 0
1507 /* Clear ATN before last byte if this is the last message. */
1508 if (n == 1 && sc->sc_msgpriq == 0)
1509 CMR = CMD_RESET_ATN;
1510 #endif
1511
1512 while ((SSR & SS_DREG_FULL) != 0)
1513 ;
1514 /* Send message byte. */
1515 sc->sc_pc[0] = *--sc->sc_omp;
1516 --n;
1517 /* Keep track of the last message we've sent any bytes of. */
1518 sc->sc_lastmsg = sc->sc_currmsg;
1519
1520 if (n == 0)
1521 break;
1522 }
1523
1524 /* We get here only if the entire message has been transmitted. */
1525 if (sc->sc_msgpriq != 0) {
1526 /* There are more outgoing messages. */
1527 goto nextmsg;
1528 }
1529
1530 /*
1531 * The last message has been transmitted. We need to remember the last
1532 * message transmitted (in case the target switches to MESSAGE IN phase
1533 * and sends a MESSAGE REJECT), and the list of messages transmitted
1534 * this time around (in case the target stays in MESSAGE OUT phase to
1535 * request a retransmit).
1536 */
1537
1538 out:
1539 /* Disable REQ/ACK protocol. */
1540 }
1541
1542
1543 /***************************************************************
1545 *
1546 * datain/dataout
1547 *
1548 */
1549
1550 int
1551 mha_datain_pio(sc, p, n)
1552 register struct mha_softc *sc;
1553 u_char *p;
1554 int n;
1555 {
1556 u_short d;
1557 int a;
1558 int total_n = n;
1559
1560 SPC_TRACE(("[mha_datain_pio(%x,%d)", p, n));
1561
1562 WAIT;
1563 sc->sc_ps[3] = 1;
1564 sc->sc_ps[4] = n >> 8;
1565 sc->sc_pc[10] = n;
1566 /* $BHa$7$-%=%U%HE>Aw(B */
1567 CMR = CMD_RECEIVE_TO_MPU;
1568 for (;;) {
1569 a = SSR;
1570 if (a & 0x04) {
1571 d = sc->sc_ps[0];
1572 *p++ = d >> 8;
1573 if (--n > 0) {
1574 *p++ = d;
1575 --n;
1576 }
1577 a = SSR;
1578 }
1579 if (a & 0x40)
1580 continue;
1581 if (a & 0x80)
1582 break;
1583 }
1584 SPC_TRACE(("...%d resd]", n));
1585 return total_n - n;
1586 }
1587
1588 int
1589 mha_dataout_pio(sc, p, n)
1590 register struct mha_softc *sc;
1591 u_char *p;
1592 int n;
1593 {
1594 u_short d;
1595 int a;
1596 int total_n = n;
1597
1598 SPC_TRACE(("[mha_dataout_pio(%x,%d)", p, n));
1599
1600 WAIT;
1601 sc->sc_ps[3] = 1;
1602 sc->sc_ps[4] = n >> 8;
1603 sc->sc_pc[10] = n;
1604 /* $BHa$7$-%=%U%HE>Aw(B */
1605 CMR = CMD_SEND_FROM_MPU;
1606 for (;;) {
1607 a = SSR;
1608 if (a & 0x04) {
1609 d = *p++ << 8;
1610 if (--n > 0) {
1611 d |= *p++;
1612 --n;
1613 }
1614 sc->sc_ps[0] = d;
1615 a = SSR;
1616 }
1617 if (a & 0x40)
1618 continue;
1619 if (a & 0x80)
1620 break;
1621 }
1622 SPC_TRACE(("...%d resd]", n));
1623 return total_n - n;
1624 }
1625
1626 static int
1627 mha_dataio_dma(dw, cw, sc, p, n)
1628 int dw; /* DMA word */
1629 int cw; /* CMR word */
1630 register struct mha_softc *sc;
1631 u_char *p;
1632 int n;
1633 {
1634 char *paddr, *vaddr;
1635
1636 if (n > MAXBSIZE)
1637 panic("transfer size exceeds MAXBSIZE");
1638 if (sc->sc_dmasize > 0)
1639 panic("DMA request while another DMA transfer is in pregress");
1640
1641 if (cw == CMD_SEND_FROM_DMA) {
1642 memcpy(sc->sc_dmabuf, p, n);
1643 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREWRITE);
1644 } else {
1645 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0, n, BUS_DMASYNC_PREREAD);
1646 }
1647 sc->sc_p = p;
1648 sc->sc_dmasize = n;
1649
1650 paddr = (char *)sc->sc_dmaseg[0].ds_addr;
1651 #if MHA_DMA_SHORT_BUS_CYCLE == 1
1652 if ((*(int *)&IODEVbase->io_sram[0xac]) & (1 << ((paddr_t)paddr >> 19)))
1653 dw &= ~(1 << 3);
1654 #endif
1655 dma_cachectl((caddr_t) sc->sc_dmabuf, n);
1656 #if 0
1657 printf("(%x,%x)->(%x,%x)\n", p, n, paddr, n);
1658 PCIA(); /* XXX */
1659 #endif
1660 sc->sc_pc[0x80 + (((long)paddr >> 16) & 0xFF)] = 0;
1661 sc->sc_pc[0x180 + (((long)paddr >> 8) & 0xFF)] = 0;
1662 sc->sc_pc[0x280 + (((long)paddr >> 0) & 0xFF)] = 0;
1663 WAIT;
1664 sc->sc_ps[3] = 1;
1665 sc->sc_ps[4] = n >> 8;
1666 sc->sc_pc[10] = n;
1667 /* DMA $BE>Aw@)8f$O0J2<$NDL$j!#(B
1668 3 ... short bus cycle
1669 2 ... MAXIMUM XFER.
1670 1 ... BURST XFER.
1671 0 ... R/W */
1672 sc->sc_ps[-1] = dw; /* burst */
1673 asm volatile ("nop");
1674 CMR = cw; /* receive to DMA */
1675 return n;
1676 }
1677 int
1678 mha_dataout(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
1688 if (n & 1)
1689 return mha_dataout_pio(sc, p, n);
1690 return mha_dataio_dma(MHA_DMA_DATAOUT, CMD_SEND_FROM_DMA, sc, p, n);
1691 }
1692
1693 int
1695 mha_datain(sc, p, n)
1696 register struct mha_softc *sc;
1697 u_char *p;
1698 int n;
1699 {
1700 int ts;
1701 register struct acb *acb = sc->sc_nexus;
1702 char *paddr, *vaddr;
1703
1704 if (n == 0)
1705 return n;
1706 if (acb->cmd.opcode == REQUEST_SENSE || (n & 1))
1707 return mha_datain_pio(sc, p, n);
1708 return mha_dataio_dma(MHA_DMA_DATAIN, CMD_RECEIVE_TO_DMA, sc, p, n);
1709 }
1710
1711
1713 /*
1714 * Catch an interrupt from the adaptor
1715 */
1716 /*
1717 * This is the workhorse routine of the driver.
1718 * Deficiencies (for now):
1719 * 1) always uses programmed I/O
1720 */
1721 int
1722 mhaintr(arg)
1723 void *arg;
1724 {
1725 struct mha_softc *sc = arg;
1726 #if 0
1727 u_char ints;
1728 #endif
1729 struct acb *acb;
1730 struct scsipi_periph *periph;
1731 struct spc_tinfo *ti;
1732 u_char ph;
1733 u_short r;
1734 int n;
1735
1736 #if 1 /* XXX called during attach? */
1737 if (tmpsc != NULL) {
1738 SPC_MISC(("[%x %x]\n", mha_cd.cd_devs, sc));
1739 sc = tmpsc;
1740 } else {
1741 #endif
1742
1743 #if 1 /* XXX */
1744 }
1745 #endif
1746
1747 #if 0
1748 /*
1749 * $B3d$j9~$_6X;_$K$9$k(B
1750 */
1751 SCTL &= ~SCTL_INTR_ENAB;
1752 #endif
1753
1754 SPC_TRACE(("[mhaintr]"));
1755
1756 loop:
1757 /*
1758 * $BA4E>Aw$,40A4$K=*N;$9$k$^$G%k!<%W$9$k(B
1759 */
1760 /*
1761 * First check for abnormal conditions, such as reset.
1762 */
1763 #if 0
1764 #if 1 /* XXX? */
1765 while (((ints = SSR) & SS_IREQUEST) == 0)
1766 delay(1);
1767 SPC_MISC(("ints = 0x%x ", ints));
1768 #else /* usually? */
1769 ints = SSR;
1770 #endif
1771 #endif
1772 while (SSR & SS_IREQUEST) {
1773 acb = sc->sc_nexus;
1774 r = ISCSR;
1775 SPC_MISC(("[r=0x%x]", r));
1776 switch (r >> 8) {
1777 default:
1778 printf("[addr=%x\n"
1779 "result=0x%x\n"
1780 "cmd=0x%x\n"
1781 "ph=0x%x(ought to be %d)]\n",
1782 &ISCSR,
1783 r,
1784 acb->xs->cmd->opcode,
1785 SCR, sc->sc_phase);
1786 panic("unexpected result.");
1787 case 0x82: /* selection timeout */
1788 SPC_MISC(("selection timeout "));
1789 sc->sc_phase = BUSFREE_PHASE;
1790 SPC_ASSERT(sc->sc_nexus != NULL);
1791 acb = sc->sc_nexus;
1792 delay(250);
1793 acb->xs->error = XS_SELTIMEOUT;
1794 mha_done(sc, acb);
1795 continue; /* XXX ??? msaitoh */
1796 case 0x60: /* command completed */
1797 sc->sc_spcinitialized++;
1798 if (sc->sc_phase == BUSFREE_PHASE)
1799 continue;
1800 ph = SCR;
1801 if (ph & PSNS_ACK) {
1802 int s;
1803 /* $B$U$D!<$N%3%^%s%I$,=*N;$7$?$i$7$$(B */
1804 SPC_MISC(("0x60)phase = %x(ought to be %x)\n",
1805 ph & PHASE_MASK, sc->sc_phase));
1806 #if 0
1807 /* switch (sc->sc_phase) {*/
1808 #else
1809 switch (ph & PHASE_MASK) {
1810 #endif
1811 case STATUS_PHASE:
1812 if (sc->sc_state != SPC_HASNEXUS)
1813 printf("stsin: !SPC_HASNEXUS->(%d)\n",
1814 sc->sc_state);
1815 SPC_ASSERT(sc->sc_nexus != NULL);
1816 acb = sc->sc_nexus;
1817 WAIT;
1818 s = MBR;
1819 SPC_ASSERT(s == 1);
1820 acb->stat = sc->sc_pcx[0]; /* XXX */
1821 SPC_MISC(("stat=0x%02x ", acb->stat));
1822 sc->sc_prevphase = STATUS_PHASE;
1823 break;
1824 case MESSAGE_IN_PHASE:
1825 mha_msgin(sc);
1826 sc->sc_prevphase = MESSAGE_IN_PHASE;
1827 /* thru */
1828 case DATA_IN_PHASE:
1829 if (sc->sc_dmasize == 0)
1830 break;
1831 bus_dmamap_sync(sc->sc_dmat,
1832 sc->sc_dmamap,
1833 0, sc->sc_dmasize,
1834 BUS_DMASYNC_POSTREAD);
1835 memcpy(sc->sc_p, sc->sc_dmabuf,
1836 sc->sc_dmasize);
1837 sc->sc_dmasize = 0;
1838 break;
1839 case DATA_OUT_PHASE:
1840 if (sc->sc_dmasize == 0)
1841 break;
1842 bus_dmamap_sync(sc->sc_dmat,
1843 sc->sc_dmamap,
1844 0, sc->sc_dmasize,
1845 BUS_DMASYNC_POSTWRITE);
1846 sc->sc_dmasize = 0;
1847 break;
1848 }
1849 WAIT;
1850 CMR = CMD_RESET_ACK; /* reset ack */
1851 /*mha_done(sc, acb); XXX */
1852 continue;
1853 } else if (NSR & 0x80) { /* nexus */
1854 #if 1
1855 if (sc->sc_state == SPC_SELECTING) /* XXX msaitoh */
1856 sc->sc_state = SPC_HASNEXUS;
1857 /* $B%U%'!<%:$N7h$aBG$A$r$9$k(B
1858 $B30$l$?$i!"(Binitial-phase error(0x54) $B$,(B
1859 $BJV$C$F$/$k$s$GCm0U$7$?$^$(!#(B
1860 $B$G$b$J$<$+(B 0x65 $B$,JV$C$F$-$?$j$7$F$M!<$+(B? */
1861 WAIT;
1862 if (SSR & SS_IREQUEST)
1863 continue;
1864 switch (sc->sc_phase) {
1865 default:
1866 panic("$B8+CN$i$L(B phase $B$,Mh$A$^$C$?$@$h(B");
1867 case MESSAGE_IN_PHASE:
1868 /* $B2?$b$7$J$$(B */
1869 continue;
1870 case STATUS_PHASE:
1871 sc->sc_phase = MESSAGE_IN_PHASE;
1872 CMR = CMD_RECEIVE_MSG; /* receive msg */
1873 continue;
1874 case DATA_IN_PHASE:
1875 sc->sc_prevphase = DATA_IN_PHASE;
1876 if (sc->sc_dleft == 0) {
1877 /* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
1878 $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
1879 sc->sc_phase = STATUS_PHASE;
1880 CMR = CMD_RECEIVE_STS; /* receive sts */
1881 continue;
1882 }
1883 n = mha_datain(sc, sc->sc_dp,
1884 sc->sc_dleft);
1885 sc->sc_dp += n;
1886 sc->sc_dleft -= n;
1887 continue;
1888 case DATA_OUT_PHASE:
1889 sc->sc_prevphase = DATA_OUT_PHASE;
1890 if (sc->sc_dleft == 0) {
1891 /* $BE>Aw%G!<%?$O$b$&$J$$$N$G(B
1892 $B%9%F!<%?%9%U%'!<%:$r4|BT$7$h$&(B */
1893 sc->sc_phase = STATUS_PHASE;
1894 CMR = CMD_RECEIVE_STS; /* receive sts */
1895 continue;
1896 }
1897 /* data phase $B$NB3$-$r$d$m$&(B */
1898 n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1899 sc->sc_dp += n;
1900 sc->sc_dleft -= n;
1901 continue;
1902 case COMMAND_PHASE:
1903 /* $B:G=i$O(B CMD PHASE $B$H$$$&$3$H$i$7$$(B */
1904 if (acb->dleft) {
1905 /* $B%G!<%?E>Aw$,$"$j$&$k>l9g(B */
1906 if (acb->xs->xs_control & XS_CTL_DATA_IN) {
1907 sc->sc_phase = DATA_IN_PHASE;
1908 n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
1909 sc->sc_dp += n;
1910 sc->sc_dleft -= n;
1911 }
1912 else if (acb->xs->xs_control & XS_CTL_DATA_OUT) {
1913 sc->sc_phase = DATA_OUT_PHASE;
1914 n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1915 sc->sc_dp += n;
1916 sc->sc_dleft -= n;
1917 }
1918 continue;
1919 }
1920 else {
1921 /* $B%G!<%?E>Aw$O$J$$$i$7$$(B?! */
1922 WAIT;
1923 sc->sc_phase = STATUS_PHASE;
1924 CMR = CMD_RECEIVE_STS; /* receive sts */
1925 continue;
1926 }
1927 }
1928 #endif
1929 }
1930 continue;
1931 case 0x31: /* disconnected in xfer progress. */
1932 SPC_MISC(("[0x31]"));
1933 case 0x70: /* disconnected. */
1934 SPC_ASSERT(sc->sc_flags & SPC_BUSFREE_OK);
1935 sc->sc_phase = BUSFREE_PHASE;
1936 sc->sc_state = SPC_IDLE;
1937 #if 1
1938 acb = sc->sc_nexus;
1939 SPC_ASSERT(sc->sc_nexus != NULL);
1940 acb->xs->error = XS_NOERROR;
1941 mha_done(sc, acb);
1942 #else
1943 TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
1944 mha_sched(sc);
1945 #endif
1946 continue;
1947 case 0x32: /* phase error in xfer progress. */
1948 SPC_MISC(("[0x32]"));
1949 #if 0
1950 case 0x65: /* invalid command.
1951 $B$J$<$3$s$J$b$N$,=P$k$N$+(B
1952 $B26$K$OA4$/M}2r$G$-$J$$(B */
1953 #if 1
1954 SPC_MISC(("[0x%04x]", r));
1955 #endif
1956 #endif
1957 case 0x54: /* initial-phase error. */
1958 SPC_MISC(("[0x54, ns=%x, ph=%x(ought to be %x)]",
1959 NSR,
1960 SCR, sc->sc_phase));
1961 /* thru */
1962 case 0x71: /* assert req */
1963 WAIT;
1964 if (SSR & 0x40) {
1965 printf("SPC sts=%2x, r=%04x, ns=%x, ph=%x\n",
1966 SSR, r, NSR, SCR);
1967 WAIT;
1968 }
1969 ph = SCR;
1970 if (sc->sc_state == SPC_SELECTING) { /* XXX msaitoh */
1971 sc->sc_state = SPC_HASNEXUS;
1972 }
1973 if (ph & 0x80) {
1974 switch (ph & PHASE_MASK) {
1975 default:
1976 printf("phase = %x\n", ph);
1977 panic("assert req: the phase I don't know!");
1978 case DATA_IN_PHASE:
1979 sc->sc_prevphase = DATA_IN_PHASE;
1980 SPC_MISC(("DATAIN(%d)...", sc->sc_dleft));
1981 n = mha_datain(sc, sc->sc_dp, sc->sc_dleft);
1982 sc->sc_dp += n;
1983 sc->sc_dleft -= n;
1984 SPC_MISC(("done\n"));
1985 continue;
1986 case DATA_OUT_PHASE:
1987 sc->sc_prevphase = DATA_OUT_PHASE;
1988 SPC_MISC(("DATAOUT\n"));
1989 n = mha_dataout(sc, sc->sc_dp, sc->sc_dleft);
1990 sc->sc_dp += n;
1991 sc->sc_dleft -= n;
1992 continue;
1993 case STATUS_PHASE:
1994 sc->sc_phase = STATUS_PHASE;
1995 SPC_MISC(("[RECV_STS]"));
1996 WAIT;
1997 CMR = CMD_RECEIVE_STS; /* receive sts */
1998 continue;
1999 case MESSAGE_IN_PHASE:
2000 sc->sc_phase = MESSAGE_IN_PHASE;
2001 WAIT;
2002 CMR = CMD_RECEIVE_MSG;
2003 continue;
2004 }
2005 }
2006 continue;
2007 }
2008 }
2009 }
2010
2011 void
2012 mha_abort(sc, acb)
2013 struct mha_softc *sc;
2014 struct acb *acb;
2015 {
2016 acb->flags |= ACB_ABORTED;
2017
2018 if (acb == sc->sc_nexus) {
2019 /*
2020 * If we're still selecting, the message will be scheduled
2021 * after selection is complete.
2022 */
2023 if (sc->sc_state == SPC_HASNEXUS) {
2024 sc->sc_flags |= SPC_ABORTING;
2025 mha_sched_msgout(SEND_ABORT);
2026 }
2027 } else {
2028 if (sc->sc_state == SPC_IDLE)
2029 mha_sched(sc);
2030 }
2031 }
2032
2033 void
2034 mha_timeout(arg)
2035 void *arg;
2036 {
2037 int s = splbio();
2038 struct acb *acb = (struct acb *)arg;
2039 struct scsipi_xfer *xs = acb->xs;
2040 struct scsipi_periph *periph = xs->xs_periph;
2041 struct mha_softc *sc =
2042 (void*)periph->periph_channel->chan_adapter->adapt_dev;
2043
2044 scsipi_printaddr(periph);
2045 again:
2046 printf("%s: timed out [acb %p (flags 0x%x, dleft %x, stat %x)], "
2047 "<state %d, nexus %p, phase(c %x, p %x), resid %x, msg(q %x,o %x) >",
2048 sc->sc_dev.dv_xname,
2049 acb, acb->flags, acb->dleft, acb->stat,
2050 sc->sc_state, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
2051 sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout
2052 );
2053 printf("[%04x %02x]\n", sc->sc_ps[1], SCR);
2054 panic("timeout, ouch!");
2055
2056 if (acb->flags & ACB_ABORTED) {
2057 /* abort timed out */
2058 printf(" AGAIN\n");
2059 #if 0
2060 mha_init(sc, 1); /* XXX 1?*/
2061 #endif
2062 } else {
2063 /* abort the operation that has timed out */
2064 printf("\n");
2065 xs->error = XS_TIMEOUT;
2066 mha_abort(sc, acb);
2067 }
2068
2069 splx(s);
2070 }
2071
2072 #if SPC_DEBUG
2074 /*
2075 * The following functions are mostly used for debugging purposes, either
2076 * directly called from the driver or from the kernel debugger.
2077 */
2078
2079 void
2080 mha_show_scsi_cmd(acb)
2081 struct acb *acb;
2082 {
2083 u_char *b = (u_char *)&acb->cmd;
2084 struct scsipi_periph *periph = acb->xs->xs_periph;
2085 int i;
2086
2087 scsipi_printaddr(periph);
2088 if ((acb->xs->xs_control & XS_CTL_RESET) == 0) {
2089 for (i = 0; i < acb->clen; i++) {
2090 if (i)
2091 printf(",");
2092 printf("%x", b[i]);
2093 }
2094 printf("\n");
2095 } else
2096 printf("RESET\n");
2097 }
2098
2099 void
2100 mha_print_acb(acb)
2101 struct acb *acb;
2102 {
2103
2104 printf("acb@%x xs=%x flags=%x", acb, acb->xs, acb->flags);
2105 printf(" dp=%x dleft=%d stat=%x\n",
2106 (long)acb->daddr, acb->dleft, acb->stat);
2107 mha_show_scsi_cmd(acb);
2108 }
2109
2110 void
2111 mha_print_active_acb()
2112 {
2113 struct acb *acb;
2114 struct mha_softc *sc = mha_cd.cd_devs[0]; /* XXX */
2115
2116 printf("ready list:\n");
2117 for (acb = sc->ready_list.tqh_first; acb != NULL;
2118 acb = acb->chain.tqe_next)
2119 mha_print_acb(acb);
2120 printf("nexus:\n");
2121 if (sc->sc_nexus != NULL)
2122 mha_print_acb(sc->sc_nexus);
2123 printf("nexus list:\n");
2124 for (acb = sc->nexus_list.tqh_first; acb != NULL;
2125 acb = acb->chain.tqe_next)
2126 mha_print_acb(acb);
2127 }
2128
2129 void
2130 mha_dump_driver(sc)
2131 struct mha_softc *sc;
2132 {
2133 struct spc_tinfo *ti;
2134 int i;
2135
2136 printf("nexus=%x prevphase=%x\n", sc->sc_nexus, sc->sc_prevphase);
2137 printf("state=%x msgin=%x msgpriq=%x msgoutq=%x lastmsg=%x currmsg=%x\n",
2138 sc->sc_state, sc->sc_imess[0],
2139 sc->sc_msgpriq, sc->sc_msgoutq, sc->sc_lastmsg, sc->sc_currmsg);
2140 for (i = 0; i < 7; i++) {
2141 ti = &sc->sc_tinfo[i];
2142 printf("tinfo%d: %d cmds %d disconnects %d timeouts",
2143 i, ti->cmds, ti->dconns, ti->touts);
2144 printf(" %d senses flags=%x\n", ti->senses, ti->flags);
2145 }
2146 }
2147 #endif
2148