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