sunscpal.c revision 1.5 1 /* $NetBSD: sunscpal.c,v 1.5 2001/07/08 18:06:46 wiz Exp $ */
2
3 /*
4 * Copyright (c) 2001 Matthew Fredette
5 * Copyright (c) 1995 David Jones, Gordon W. Ross
6 * Copyright (c) 1994 Jarle Greipsland
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the authors may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 * 4. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by
22 * David Jones and Gordon Ross
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * This is a machine-independent driver for the Sun "sc"
38 * SCSI Bus Controller (SBC).
39 *
40 * This code should work with any memory-mapped card,
41 * and can be shared by multiple adapters that address
42 * the card with different register offset spacings.
43 * (This can happen on the atari, for example.)
44 *
45 * Credits, history:
46 *
47 * Matthew Fredette completely copied revision 1.38 of
48 * ncr5380sbc.c, and then heavily modified it to match
49 * the Sun sc PAL. The remaining credits are for
50 * ncr5380sbc.c:
51 *
52 * David Jones is the author of most of the code that now
53 * appears in this file, and was the architect of the
54 * current overall structure (MI/MD code separation, etc.)
55 *
56 * Gordon Ross integrated the message phase code, added lots of
57 * comments about what happens when and why (re. SCSI spec.),
58 * debugged some reentrance problems, and added several new
59 * "hooks" needed for the Sun3 "si" adapters.
60 *
61 * The message in/out code was taken nearly verbatim from
62 * the aic6360 driver by Jarle Greipsland.
63 *
64 * Several other NCR5380 drivers were used for reference
65 * while developing this driver, including work by:
66 * The Alice Group (mac68k port) namely:
67 * Allen K. Briggs, Chris P. Caputo, Michael L. Finch,
68 * Bradley A. Grantham, and Lawrence A. Kesteloot
69 * Michael L. Hitch (amiga drivers: sci.c)
70 * Leo Weppelman (atari driver: ncr5380.c)
71 * There are others too. Thanks, everyone.
72 *
73 * Transliteration to bus_space() performed 9/17/98 by
74 * John Ruschmeyer (jruschme (at) exit109.com) for i386 'nca' driver.
75 * Thank you all.
76 */
77
78 #include "opt_ddb.h"
79
80 #include <sys/types.h>
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/errno.h>
85 #include <sys/malloc.h>
86 #include <sys/device.h>
87 #include <sys/buf.h>
88 #include <sys/proc.h>
89 #include <sys/user.h>
90
91 #include <dev/scsipi/scsi_all.h>
92 #include <dev/scsipi/scsipi_all.h>
93 #include <dev/scsipi/scsipi_debug.h>
94 #include <dev/scsipi/scsi_message.h>
95 #include <dev/scsipi/scsiconf.h>
96
97 #ifdef DDB
98 #include <ddb/db_output.h>
99 #endif
100
101 #include <dev/ic/sunscpalreg.h>
102 #include <dev/ic/sunscpalvar.h>
103
104 static void sunscpal_reset_scsibus __P((struct sunscpal_softc *));
105 static void sunscpal_sched __P((struct sunscpal_softc *));
106 static void sunscpal_done __P((struct sunscpal_softc *));
107
108 static int sunscpal_select
109 __P((struct sunscpal_softc *, struct sunscpal_req *));
110 static void sunscpal_reselect __P((struct sunscpal_softc *));
111
112 static int sunscpal_msg_in __P((struct sunscpal_softc *));
113 static int sunscpal_msg_out __P((struct sunscpal_softc *));
114 static int sunscpal_data_xfer __P((struct sunscpal_softc *, int));
115 static int sunscpal_command __P((struct sunscpal_softc *));
116 static int sunscpal_status __P((struct sunscpal_softc *));
117 static void sunscpal_machine __P((struct sunscpal_softc *));
118
119 void sunscpal_abort __P((struct sunscpal_softc *));
120 void sunscpal_cmd_timeout __P((void *));
121 /*
122 * Action flags returned by the info_transfer functions:
123 * (These determine what happens next.)
124 */
125 #define ACT_CONTINUE 0x00 /* No flags: expect another phase */
126 #define ACT_DISCONNECT 0x01 /* Target is disconnecting */
127 #define ACT_CMD_DONE 0x02 /* Need to call scsipi_done() */
128 #define ACT_RESET_BUS 0x04 /* Need bus reset (cmd timeout) */
129 #define ACT_WAIT_DMA 0x10 /* Wait for DMA to complete */
130
131 /*****************************************************************
132 * Debugging stuff
133 *****************************************************************/
134
135 #ifndef DDB
136 /* This is used only in recoverable places. */
137 #ifndef Debugger
138 #define Debugger() printf("Debug: sunscpal.c:%d\n", __LINE__)
139 #endif
140 #endif
141
142 #ifdef SUNSCPAL_DEBUG
143
144 #define SUNSCPAL_DBG_BREAK 1
145 #define SUNSCPAL_DBG_CMDS 2
146 #define SUNSCPAL_DBG_DMA 4
147 int sunscpal_debug = 0;
148 #define SUNSCPAL_BREAK() \
149 do { if (sunscpal_debug & SUNSCPAL_DBG_BREAK) Debugger(); } while (0)
150 static void sunscpal_show_scsi_cmd __P((struct scsipi_xfer *));
151 #ifdef DDB
152 void sunscpal_clear_trace __P((void));
153 void sunscpal_show_trace __P((void));
154 void sunscpal_show_req __P((struct sunscpal_req *));
155 void sunscpal_show_state __P((void));
156 #endif /* DDB */
157 #else /* SUNSCPAL_DEBUG */
158
159 #define SUNSCPAL_BREAK() /* nada */
160 #define sunscpal_show_scsi_cmd(xs) /* nada */
161
162 #endif /* SUNSCPAL_DEBUG */
163
164 static char *
165 phase_names[8] = {
166 "DATA_OUT",
167 "DATA_IN",
168 "COMMAND",
169 "STATUS",
170 "UNSPEC1",
171 "UNSPEC2",
172 "MSG_OUT",
173 "MSG_IN",
174 };
175
176 #ifdef SUNSCPAL_USE_BUS_DMA
177 static void sunscpal_dma_alloc __P((struct sunscpal_softc *));
178 static void sunscpal_dma_free __P((struct sunscpal_softc *));
179 static void sunscpal_dma_setup __P((struct sunscpal_softc *));
180 #else
181 #define sunscpal_dma_alloc(sc) (*sc->sc_dma_alloc)(sc)
182 #define sunscpal_dma_free(sc) (*sc->sc_dma_free)(sc)
183 #define sunscpal_dma_setup(sc) (*sc->sc_dma_setup)(sc)
184 #endif
185 static void sunscpal_minphys __P((struct buf *));
186
187 /*****************************************************************
188 * Actual chip control
189 *****************************************************************/
190
191 /*
192 * XXX: These timeouts might need to be tuned...
193 */
194
195 /* This one is used when waiting for a phase change. (X100uS.) */
196 int sunscpal_wait_phase_timo = 1000 * 10 * 300; /* 5 min. */
197
198 /* These are used in the following inline functions. */
199 int sunscpal_wait_req_timo = 1000 * 50; /* X2 = 100 mS. */
200 int sunscpal_wait_nrq_timo = 1000 * 25; /* X2 = 50 mS. */
201
202 static __inline int sunscpal_wait_req __P((struct sunscpal_softc *));
203 static __inline int sunscpal_wait_not_req __P((struct sunscpal_softc *));
204 static __inline void sunscpal_sched_msgout __P((struct sunscpal_softc *, int));
205
206 /* Return zero on success. */
207 static __inline int sunscpal_wait_req(sc)
208 struct sunscpal_softc *sc;
209 {
210 int timo = sunscpal_wait_req_timo;
211 for (;;) {
212 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST) {
213 timo = 0; /* return 0 */
214 break;
215 }
216 if (--timo < 0)
217 break; /* return -1 */
218 delay(2);
219 }
220 return (timo);
221 }
222
223 /* Return zero on success. */
224 static __inline int sunscpal_wait_not_req(sc)
225 struct sunscpal_softc *sc;
226 {
227 int timo = sunscpal_wait_nrq_timo;
228 for (;;) {
229 if ((SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST) == 0) {
230 timo = 0; /* return 0 */
231 break;
232 }
233 if (--timo < 0)
234 break; /* return -1 */
235 delay(2);
236 }
237 return (timo);
238 }
239
240 /*
241 * These functions control DMA functions in the chipset independent of
242 * the host DMA implementation.
243 */
244 static void sunscpal_dma_start __P((struct sunscpal_softc *));
245 static void sunscpal_dma_poll __P((struct sunscpal_softc *));
246 static void sunscpal_dma_stop __P((struct sunscpal_softc *));
247
248 static void
249 sunscpal_dma_start(sc)
250 struct sunscpal_softc *sc;
251 {
252 struct sunscpal_req *sr = sc->sc_current;
253 int xlen;
254 u_int16_t icr;
255
256 xlen = sc->sc_reqlen;
257
258 /* Let'er rip! */
259 icr = SUNSCPAL_READ_2(sc, sunscpal_icr);
260 icr |= SUNSCPAL_ICR_DMA_ENABLE |
261 ((xlen & 1) ? 0 : SUNSCPAL_ICR_WORD_MODE) |
262 ((sr->sr_flags & SR_IMMED) ? 0 : SUNSCPAL_ICR_INTERRUPT_ENABLE);
263 SUNSCPAL_WRITE_2(sc, sunscpal_icr, icr);
264
265 sc->sc_state |= SUNSCPAL_DOINGDMA;
266
267 #ifdef SUNSCPAL_DEBUG
268 if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
269 printf("sunscpal_dma_start: started, flags=0x%x\n",
270 sc->sc_state);
271 }
272 #endif
273 }
274
275 #define ICR_MASK (SUNSCPAL_ICR_PARITY_ERROR | SUNSCPAL_ICR_BUS_ERROR | SUNSCPAL_ICR_INTERRUPT_REQUEST)
276 #define POLL_TIMO 50000 /* X100 = 5 sec. */
277
278 /*
279 * Poll (spin-wait) for DMA completion.
280 * Called right after xx_dma_start(), and
281 * xx_dma_stop() will be called next.
282 */
283 static void
284 sunscpal_dma_poll(sc)
285 struct sunscpal_softc *sc;
286 {
287 struct sunscpal_req *sr = sc->sc_current;
288 int tmo;
289
290 /* Make sure DMA started successfully. */
291 if (sc->sc_state & SUNSCPAL_ABORTING)
292 return;
293
294 /* Wait for any "dma complete" or error bits. */
295 tmo = POLL_TIMO;
296 for (;;) {
297 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & ICR_MASK)
298 break;
299 if (--tmo <= 0) {
300 printf("sc: DMA timeout (while polling)\n");
301 /* Indicate timeout as MI code would. */
302 sr->sr_flags |= SR_OVERDUE;
303 break;
304 }
305 delay(100);
306 }
307 SUNSCPAL_TRACE("sunscpal_dma_poll: waited %d\n",
308 POLL_TIMO - tmo);
309
310 #ifdef SUNSCPAL_DEBUG
311 if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
312 printf("sunscpal_dma_poll: done, icr=0x%x\n", SUNSCPAL_READ_2(sc, sunscpal_icr));
313 }
314 #endif
315 }
316
317 static void
318 sunscpal_dma_stop(sc)
319 struct sunscpal_softc *sc;
320 {
321 struct sunscpal_req *sr = sc->sc_current;
322 struct scsipi_xfer *xs = sr->sr_xs;
323 int resid, ntrans;
324 u_int16_t icr;
325
326 if ((sc->sc_state & SUNSCPAL_DOINGDMA) == 0) {
327 #ifdef DEBUG
328 printf("sunscpal_dma_stop: dma not running\n");
329 #endif
330 return;
331 }
332 sc->sc_state &= ~SUNSCPAL_DOINGDMA;
333
334 /* First, halt the DMA engine. */
335 icr = SUNSCPAL_READ_2(sc, sunscpal_icr);
336 icr &= ~(SUNSCPAL_ICR_DMA_ENABLE | SUNSCPAL_ICR_WORD_MODE | SUNSCPAL_ICR_INTERRUPT_ENABLE);
337 SUNSCPAL_WRITE_2(sc, sunscpal_icr, icr);
338
339 #ifdef SUNSCPAL_USE_BUS_DMA
340 /*
341 * XXX - this function is supposed to be independent of
342 * the host's DMA implementation.
343 */
344 {
345 sunscpal_dma_handle_t dh = sr->sr_dma_hand;
346
347 /* sync the DMA map: */
348 bus_dmamap_sync(sc->sunscpal_dmat, dh->dh_dmamap, 0, dh->dh_maplen,
349 ((xs->xs_control & XS_CTL_DATA_OUT) == 0 ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
350 }
351 #endif /* SUNSCPAL_USE_BUS_DMA */
352
353
354 if (icr & (SUNSCPAL_ICR_BUS_ERROR)) {
355 printf("sc: DMA error, icr=0x%x, reset\n", icr);
356 sr->sr_xs->error = XS_DRIVER_STUFFUP;
357 sc->sc_state |= SUNSCPAL_ABORTING;
358 goto out;
359 }
360
361 /* Note that timeout may have set the error flag. */
362 if (sc->sc_state & SUNSCPAL_ABORTING)
363 goto out;
364
365 /* XXX: Wait for DMA to actually finish? */
366
367 /*
368 * Now try to figure out how much actually transferred
369 */
370
371 resid = SUNSCPAL_DMA_COUNT_FLIP(SUNSCPAL_READ_2(sc, sunscpal_dma_count));
372 ntrans = sc->sc_reqlen - resid;
373
374 #ifdef SUNSCPAL_DEBUG
375 if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
376 printf("sunscpal_dma_stop: resid=0x%x ntrans=0x%x\n",
377 resid, ntrans);
378 }
379 #endif
380
381 if (ntrans < sc->sc_min_dma_len) {
382 printf("sc: DMA count: 0x%x\n", resid);
383 sc->sc_state |= SUNSCPAL_ABORTING;
384 goto out;
385 }
386 if (ntrans > sc->sc_datalen)
387 panic("sunscpal_dma_stop: excess transfer");
388
389 /* Adjust data pointer */
390 sc->sc_dataptr += ntrans;
391 sc->sc_datalen -= ntrans;
392
393 /*
394 * After a read, we may need to clean-up
395 * "Left-over bytes" (yuck!)
396 */
397 if (((xs->xs_control & XS_CTL_DATA_OUT) == 0) &&
398 ((icr & SUNSCPAL_ICR_ODD_LENGTH) != 0))
399 {
400 #ifdef DEBUG
401 printf("sc: Got Left-over bytes!\n");
402 #endif
403 *(sc->sc_dataptr++) = SUNSCPAL_READ_1(sc, sunscpal_data);
404 sc->sc_datalen--;
405 }
406
407 out:
408 SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(0));
409
410 }
411
412 /* Ask the target for a MSG_OUT phase. */
413 static __inline void
414 sunscpal_sched_msgout(sc, msg_code)
415 struct sunscpal_softc *sc;
416 int msg_code;
417 {
418 /*
419 * This controller does not allow you to assert ATN, which
420 * will eventually leave us with no option other than to reset
421 * the bus. We keep this function as a placeholder, though,
422 * and this printf will eventually go away or get #ifdef'ed:
423 */
424 printf("sunscpal_sched_msgout: trying to schedule 0x%0x\n", msg_code);
425 sc->sc_msgpriq |= msg_code;
426 }
427
428 int
429 sunscpal_pio_out(sc, phase, count, data)
430 struct sunscpal_softc *sc;
431 int phase, count;
432 unsigned char *data;
433 {
434 int resid;
435
436 resid = count;
437 while (resid > 0) {
438 if (!SUNSCPAL_BUSY(sc)) {
439 SUNSCPAL_TRACE("pio_out: lost BSY, resid=%d\n", resid);
440 break;
441 }
442 if (sunscpal_wait_req(sc)) {
443 SUNSCPAL_TRACE("pio_out: no REQ, resid=%d\n", resid);
444 break;
445 }
446 if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) != phase)
447 break;
448
449 /* Put the data on the bus. */
450 if (data) {
451 SUNSCPAL_BYTE_WRITE(sc, phase, *data++);
452 } else {
453 SUNSCPAL_BYTE_WRITE(sc, phase, 0);
454 }
455
456 --resid;
457 }
458
459 return (count - resid);
460 }
461
462
463 int
464 sunscpal_pio_in(sc, phase, count, data)
465 struct sunscpal_softc *sc;
466 int phase, count;
467 unsigned char *data;
468 {
469 int resid;
470
471 resid = count;
472 while (resid > 0) {
473 if (!SUNSCPAL_BUSY(sc)) {
474 SUNSCPAL_TRACE("pio_in: lost BSY, resid=%d\n", resid);
475 break;
476 }
477 if (sunscpal_wait_req(sc)) {
478 SUNSCPAL_TRACE("pio_in: no REQ, resid=%d\n", resid);
479 break;
480 }
481 /* A phase change is not valid until AFTER REQ rises! */
482 if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) != phase)
483 break;
484
485 /* Read the data bus. */
486 if (data)
487 *data++ = SUNSCPAL_BYTE_READ(sc, phase);
488 else
489 (void) SUNSCPAL_BYTE_READ(sc, phase);
490
491 --resid;
492 }
493
494 return (count - resid);
495 }
496
497
498 void
499 sunscpal_init(sc)
500 struct sunscpal_softc *sc;
501 {
502 int i, j;
503
504 #ifdef SUNSCPAL_DEBUG
505 sunscpal_debug_sc = sc;
506 #endif
507
508 for (i = 0; i < SUNSCPAL_OPENINGS; i++)
509 sc->sc_ring[i].sr_xs = NULL;
510 for (i = 0; i < 8; i++)
511 for (j = 0; j < 8; j++)
512 sc->sc_matrix[i][j] = NULL;
513
514 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
515 sc->sc_state = SUNSCPAL_IDLE;
516
517 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
518 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_h, 0);
519 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_l, 0);
520 SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(0));
521
522 SUNSCPAL_CLR_INTR(sc);
523
524 /* Another hack (Er.. hook!) for anything that needs it: */
525 if (sc->sc_intr_on) {
526 SUNSCPAL_TRACE("init: intr ON\n", 0);
527 sc->sc_intr_on(sc);
528 }
529 }
530
531
532 static void
533 sunscpal_reset_scsibus(sc)
534 struct sunscpal_softc *sc;
535 {
536
537 SUNSCPAL_TRACE("reset_scsibus, cur=0x%x\n",
538 (long) sc->sc_current);
539
540 SUNSCPAL_WRITE_2(sc, sunscpal_icr, SUNSCPAL_ICR_RESET);
541 delay(500);
542 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
543
544 SUNSCPAL_CLR_INTR(sc);
545 /* XXX - Need long delay here! */
546 delay(100000);
547
548 /* XXX - Need to cancel disconnected requests. */
549 }
550
551
552 /*
553 * Interrupt handler for the SCSI Bus Controller (SBC)
554 * This may also called for a DMA timeout (at splbio).
555 */
556 int
557 sunscpal_intr(arg)
558 void *arg;
559 {
560 struct sunscpal_softc *sc = arg;
561 int claimed = 0;
562
563 /*
564 * Do not touch SBC regs here unless sc_current == NULL
565 * or it will complain about "register conflict" errors.
566 * Instead, just let sunscpal_machine() deal with it.
567 */
568 SUNSCPAL_TRACE("intr: top, state=%d\n", sc->sc_state);
569
570 if (sc->sc_state == SUNSCPAL_IDLE) {
571 /*
572 * Might be reselect. sunscpal_reselect() will check,
573 * and set up the connection if so. This will verify
574 * that sc_current == NULL at the beginning...
575 */
576
577 /* Another hack (Er.. hook!) for anything that needs it: */
578 if (sc->sc_intr_off) {
579 SUNSCPAL_TRACE("intr: for reselect, intr off\n", 0);
580 sc->sc_intr_off(sc);
581 }
582
583 sunscpal_reselect(sc);
584 }
585
586 /*
587 * The remaining documented interrupt causes are a DMA complete
588 * condition.
589 *
590 * The procedure is to let sunscpal_machine() figure out what
591 * to do next.
592 */
593 if (sc->sc_state & SUNSCPAL_WORKING) {
594 SUNSCPAL_TRACE("intr: call machine, cur=0x%x\n",
595 (long) sc->sc_current);
596 /* This will usually free-up the nexus. */
597 sunscpal_machine(sc);
598 SUNSCPAL_TRACE("intr: machine done, cur=0x%x\n",
599 (long) sc->sc_current);
600 claimed = 1;
601 }
602
603 /* Maybe we can run some commands now... */
604 if (sc->sc_state == SUNSCPAL_IDLE) {
605 SUNSCPAL_TRACE("intr: call sched, cur=0x%x\n",
606 (long) sc->sc_current);
607 sunscpal_sched(sc);
608 SUNSCPAL_TRACE("intr: sched done, cur=0x%x\n",
609 (long) sc->sc_current);
610 }
611
612 return claimed;
613 }
614
615
616 /*
617 * Abort the current command (i.e. due to timeout)
618 */
619 void
620 sunscpal_abort(sc)
621 struct sunscpal_softc *sc;
622 {
623
624 /*
625 * Finish it now. If DMA is in progress, we
626 * can not call sunscpal_sched_msgout() because
627 * that hits the SBC (avoid DMA conflict).
628 */
629
630 /* Another hack (Er.. hook!) for anything that needs it: */
631 if (sc->sc_intr_off) {
632 SUNSCPAL_TRACE("abort: intr off\n", 0);
633 sc->sc_intr_off(sc);
634 }
635
636 sc->sc_state |= SUNSCPAL_ABORTING;
637 if ((sc->sc_state & SUNSCPAL_DOINGDMA) == 0) {
638 sunscpal_sched_msgout(sc, SEND_ABORT);
639 }
640 SUNSCPAL_TRACE("abort: call machine, cur=0x%x\n",
641 (long) sc->sc_current);
642 sunscpal_machine(sc);
643 SUNSCPAL_TRACE("abort: machine done, cur=0x%x\n",
644 (long) sc->sc_current);
645
646 /* Another hack (Er.. hook!) for anything that needs it: */
647 if (sc->sc_intr_on) {
648 SUNSCPAL_TRACE("abort: intr ON\n", 0);
649 sc->sc_intr_on(sc);
650 }
651 }
652
653 /*
654 * Timeout handler, scheduled for each SCSI command.
655 */
656 void
657 sunscpal_cmd_timeout(arg)
658 void *arg;
659 {
660 struct sunscpal_req *sr = arg;
661 struct scsipi_xfer *xs;
662 struct scsipi_periph *periph;
663 struct sunscpal_softc *sc;
664 int s;
665
666 s = splbio();
667
668 /* Get all our variables... */
669 xs = sr->sr_xs;
670 if (xs == NULL) {
671 printf("sunscpal_cmd_timeout: no scsipi_xfer\n");
672 goto out;
673 }
674 periph = xs->xs_periph;
675 sc = (void *)periph->periph_channel->chan_adapter->adapt_dev;
676
677 printf("%s: cmd timeout, targ=%d, lun=%d\n",
678 sc->sc_dev.dv_xname,
679 sr->sr_target, sr->sr_lun);
680
681 /*
682 * Mark the overdue job as failed, and arrange for
683 * sunscpal_machine to terminate it. If the victim
684 * is the current job, call sunscpal_machine() now.
685 * Otherwise arrange for sunscpal_sched() to do it.
686 */
687 sr->sr_flags |= SR_OVERDUE;
688 if (sc->sc_current == sr) {
689 SUNSCPAL_TRACE("cmd_tmo: call abort, sr=0x%x\n", (long) sr);
690 sunscpal_abort(sc);
691 } else {
692 /*
693 * The driver may be idle, or busy with another job.
694 * Arrange for sunscpal_sched() to do the deed.
695 */
696 SUNSCPAL_TRACE("cmd_tmo: clear matrix, t/l=0x%02x\n",
697 (sr->sr_target << 4) | sr->sr_lun);
698 sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL;
699 }
700
701 /*
702 * We may have aborted the current job, or may have
703 * already been idle. In either case, we should now
704 * be idle, so try to start another job.
705 */
706 if (sc->sc_state == SUNSCPAL_IDLE) {
707 SUNSCPAL_TRACE("cmd_tmo: call sched, cur=0x%x\n",
708 (long) sc->sc_current);
709 sunscpal_sched(sc);
710 SUNSCPAL_TRACE("cmd_tmo: sched done, cur=0x%x\n",
711 (long) sc->sc_current);
712 }
713
714 out:
715 splx(s);
716 }
717
718
719 /*****************************************************************
720 * Interface to higher level
721 *****************************************************************/
722
723
724 /*
725 * Enter a new SCSI command into the "issue" queue, and
726 * if there is work to do, start it going.
727 *
728 * WARNING: This can be called recursively!
729 * (see comment in sunscpal_done)
730 */
731 void
732 sunscpal_scsipi_request(chan, req, arg)
733 struct scsipi_channel *chan;
734 scsipi_adapter_req_t req;
735 void *arg;
736 {
737 struct scsipi_xfer *xs;
738 struct scsipi_periph *periph;
739 struct sunscpal_softc *sc = (void *)chan->chan_adapter->adapt_dev;
740 struct sunscpal_req *sr;
741 int s, i, flags;
742
743 switch (req) {
744 case ADAPTER_REQ_RUN_XFER:
745 xs = arg;
746 periph = xs->xs_periph;
747 flags = xs->xs_control;
748
749 if (sc->sc_flags & SUNSCPAL_FORCE_POLLING)
750 flags |= XS_CTL_POLL;
751
752 if (flags & XS_CTL_DATA_UIO)
753 panic("sunscpal: scsi data uio requested");
754
755 s = splbio();
756
757 if (flags & XS_CTL_POLL) {
758 /* Terminate any current command. */
759 sr = sc->sc_current;
760 if (sr) {
761 printf("%s: polled request aborting %d/%d\n",
762 sc->sc_dev.dv_xname,
763 sr->sr_target, sr->sr_lun);
764 sunscpal_abort(sc);
765 }
766 if (sc->sc_state != SUNSCPAL_IDLE) {
767 panic("sunscpal_scsi_cmd: polled request, abort failed");
768 }
769 }
770
771 /*
772 * Find lowest empty slot in ring buffer.
773 * XXX: What about "fairness" and cmd order?
774 */
775 for (i = 0; i < SUNSCPAL_OPENINGS; i++)
776 if (sc->sc_ring[i].sr_xs == NULL)
777 goto new;
778
779 xs->error = XS_RESOURCE_SHORTAGE;
780 SUNSCPAL_TRACE("scsipi_cmd: no openings, rv=%d\n", rv);
781 goto out;
782
783 new:
784 /* Create queue entry */
785 sr = &sc->sc_ring[i];
786 sr->sr_xs = xs;
787 sr->sr_target = xs->xs_periph->periph_target;
788 sr->sr_lun = xs->xs_periph->periph_lun;
789 sr->sr_dma_hand = NULL;
790 sr->sr_dataptr = xs->data;
791 sr->sr_datalen = xs->datalen;
792 sr->sr_flags = (flags & XS_CTL_POLL) ? SR_IMMED : 0;
793 sr->sr_status = -1; /* no value */
794 sc->sc_ncmds++;
795
796 SUNSCPAL_TRACE("scsipi_cmd: new sr=0x%x\n", (long)sr);
797
798 if (flags & XS_CTL_POLL) {
799 /* Force this new command to be next. */
800 sc->sc_rr = i;
801 }
802
803 /*
804 * If we were idle, run some commands...
805 */
806 if (sc->sc_state == SUNSCPAL_IDLE) {
807 SUNSCPAL_TRACE("scsipi_cmd: call sched, cur=0x%x\n",
808 (long) sc->sc_current);
809 sunscpal_sched(sc);
810 SUNSCPAL_TRACE("scsipi_cmd: sched done, cur=0x%x\n",
811 (long) sc->sc_current);
812 }
813
814 if (flags & XS_CTL_POLL) {
815 /* Make sure sunscpal_sched() finished it. */
816 if ((xs->xs_status & XS_STS_DONE) == 0)
817 panic("sunscpal_scsi_cmd: poll didn't finish");
818 }
819
820 out:
821 splx(s);
822 return;
823 case ADAPTER_REQ_GROW_RESOURCES:
824 case ADAPTER_REQ_SET_XFER_MODE:
825 /* not supported */
826 return;
827 }
828 }
829
830
831 /*
832 * POST PROCESSING OF SCSI_CMD (usually current)
833 * Called by sunscpal_sched(), sunscpal_machine()
834 */
835 static void
836 sunscpal_done(sc)
837 struct sunscpal_softc *sc;
838 {
839 struct sunscpal_req *sr;
840 struct scsipi_xfer *xs;
841
842 #ifdef DIAGNOSTIC
843 if (sc->sc_state == SUNSCPAL_IDLE)
844 panic("sunscpal_done: state=idle");
845 if (sc->sc_current == NULL)
846 panic("sunscpal_done: current=0");
847 #endif
848
849 sr = sc->sc_current;
850 xs = sr->sr_xs;
851
852 SUNSCPAL_TRACE("done: top, cur=0x%x\n", (long) sc->sc_current);
853
854 /*
855 * Clean up DMA resources for this command.
856 */
857 if (sr->sr_dma_hand) {
858 SUNSCPAL_TRACE("done: dma_free, dh=0x%x\n",
859 (long) sr->sr_dma_hand);
860 sunscpal_dma_free(sc);
861 }
862 #ifdef DIAGNOSTIC
863 if (sr->sr_dma_hand)
864 panic("sunscpal_done: dma free did not");
865 #endif
866
867 if (sc->sc_state & SUNSCPAL_ABORTING) {
868 SUNSCPAL_TRACE("done: aborting, error=%d\n", xs->error);
869 if (xs->error == XS_NOERROR)
870 xs->error = XS_TIMEOUT;
871 }
872
873 SUNSCPAL_TRACE("done: check error=%d\n", (long) xs->error);
874
875 /* If error is already set, ignore sr_status value. */
876 if (xs->error != XS_NOERROR)
877 goto finish;
878
879 SUNSCPAL_TRACE("done: check status=%d\n", sr->sr_status);
880
881 xs->status = sr->sr_status;
882 switch (sr->sr_status) {
883 case SCSI_OK: /* 0 */
884 break;
885
886 case SCSI_CHECK:
887 case SCSI_BUSY:
888 xs->error = XS_BUSY;
889 break;
890
891 case -1:
892 /* This is our "impossible" initial value. */
893 /* fallthrough */
894 default:
895 printf("%s: target %d, bad status=%d\n",
896 sc->sc_dev.dv_xname, sr->sr_target, sr->sr_status);
897 xs->error = XS_DRIVER_STUFFUP;
898 break;
899 }
900
901 finish:
902
903 SUNSCPAL_TRACE("done: finish, error=%d\n", xs->error);
904
905 /*
906 * Dequeue the finished command, but don't clear sc_state until
907 * after the call to scsipi_done(), because that may call back to
908 * sunscpal_scsi_cmd() - unwanted recursion!
909 *
910 * Keeping sc->sc_state != idle terminates the recursion.
911 */
912 #ifdef DIAGNOSTIC
913 if ((sc->sc_state & SUNSCPAL_WORKING) == 0)
914 panic("sunscpal_done: bad state");
915 #endif
916
917 /* Clear our pointers to the request. */
918 sc->sc_current = NULL;
919 sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL;
920 callout_stop(&sr->sr_xs->xs_callout);
921
922 /* Make the request free. */
923 sr->sr_xs = NULL;
924 sc->sc_ncmds--;
925
926 /* Tell common SCSI code it is done. */
927 scsipi_done(xs);
928
929 sc->sc_state = SUNSCPAL_IDLE;
930 /* Now sunscpal_sched() may be called again. */
931 }
932
933
934 /*
935 * Schedule a SCSI operation. This routine should return
936 * only after it achieves one of the following conditions:
937 * Busy (sc->sc_state != SUNSCPAL_IDLE)
938 * No more work can be started.
939 */
940 static void
941 sunscpal_sched(sc)
942 struct sunscpal_softc *sc;
943 {
944 struct sunscpal_req *sr;
945 struct scsipi_xfer *xs;
946 int target = 0, lun = 0;
947 int error, i;
948
949 /* Another hack (Er.. hook!) for anything that needs it: */
950 if (sc->sc_intr_off) {
951 SUNSCPAL_TRACE("sched: top, intr off\n", 0);
952 sc->sc_intr_off(sc);
953 }
954
955 next_job:
956 /*
957 * Grab the next job from queue. Must be idle.
958 */
959 #ifdef DIAGNOSTIC
960 if (sc->sc_state != SUNSCPAL_IDLE)
961 panic("sunscpal_sched: not idle");
962 if (sc->sc_current)
963 panic("sunscpal_sched: current set");
964 #endif
965
966 /*
967 * Always start the search where we last looked.
968 */
969 i = sc->sc_rr;
970 sr = NULL;
971 do {
972 if (sc->sc_ring[i].sr_xs) {
973 target = sc->sc_ring[i].sr_target;
974 lun = sc->sc_ring[i].sr_lun;
975 if (sc->sc_matrix[target][lun] == NULL) {
976 /*
977 * Do not mark the target/LUN busy yet,
978 * because reselect may cause some other
979 * job to become the current one, so we
980 * might not actually start this job.
981 * Instead, set sc_matrix later on.
982 */
983 sc->sc_rr = i;
984 sr = &sc->sc_ring[i];
985 break;
986 }
987 }
988 i++;
989 if (i == SUNSCPAL_OPENINGS)
990 i = 0;
991 } while (i != sc->sc_rr);
992
993 if (sr == NULL) {
994 SUNSCPAL_TRACE("sched: no work, cur=0x%x\n",
995 (long) sc->sc_current);
996
997 /* Another hack (Er.. hook!) for anything that needs it: */
998 if (sc->sc_intr_on) {
999 SUNSCPAL_TRACE("sched: ret, intr ON\n", 0);
1000 sc->sc_intr_on(sc);
1001 }
1002
1003 return; /* No more work to do. */
1004 }
1005
1006 SUNSCPAL_TRACE("sched: select for t/l=0x%02x\n",
1007 (sr->sr_target << 4) | sr->sr_lun);
1008
1009 sc->sc_state = SUNSCPAL_WORKING;
1010 error = sunscpal_select(sc, sr);
1011 if (sc->sc_current) {
1012 /* Lost the race! reselected out from under us! */
1013 /* Work with the reselected job. */
1014 if (sr->sr_flags & SR_IMMED) {
1015 printf("%s: reselected while polling (abort)\n",
1016 sc->sc_dev.dv_xname);
1017 /* Abort the reselected job. */
1018 sc->sc_state |= SUNSCPAL_ABORTING;
1019 sc->sc_msgpriq |= SEND_ABORT;
1020 }
1021 sr = sc->sc_current;
1022 xs = sr->sr_xs;
1023 SUNSCPAL_TRACE("sched: reselect, new sr=0x%x\n", (long)sr);
1024 goto have_nexus;
1025 }
1026
1027 /* Normal selection result. Target/LUN is now busy. */
1028 sc->sc_matrix[target][lun] = sr;
1029 sc->sc_current = sr; /* connected */
1030 xs = sr->sr_xs;
1031
1032 /*
1033 * Initialize pointers, etc. for this job
1034 */
1035 sc->sc_dataptr = sr->sr_dataptr;
1036 sc->sc_datalen = sr->sr_datalen;
1037 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
1038 sc->sc_msgpriq = SEND_IDENTIFY;
1039 sc->sc_msgoutq = 0;
1040 sc->sc_msgout = 0;
1041
1042 SUNSCPAL_TRACE("sched: select rv=%d\n", error);
1043
1044 switch (error) {
1045 case XS_NOERROR:
1046 break;
1047
1048 case XS_BUSY:
1049 /* XXX - Reset and try again. */
1050 printf("%s: select found SCSI bus busy, resetting...\n",
1051 sc->sc_dev.dv_xname);
1052 sunscpal_reset_scsibus(sc);
1053 /* fallthrough */
1054 case XS_SELTIMEOUT:
1055 default:
1056 xs->error = error; /* from select */
1057 SUNSCPAL_TRACE("sched: call done, sr=0x%x\n", (long)sr);
1058 sunscpal_done(sc);
1059
1060 /* Paranoia: clear everything. */
1061 sc->sc_dataptr = NULL;
1062 sc->sc_datalen = 0;
1063 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
1064 sc->sc_msgpriq = 0;
1065 sc->sc_msgoutq = 0;
1066 sc->sc_msgout = 0;
1067
1068 goto next_job;
1069 }
1070
1071 /*
1072 * Selection was successful. Normally, this means
1073 * we are starting a new command. However, this
1074 * might be the termination of an overdue job.
1075 */
1076 if (sr->sr_flags & SR_OVERDUE) {
1077 SUNSCPAL_TRACE("sched: overdue, sr=0x%x\n", (long)sr);
1078 sc->sc_state |= SUNSCPAL_ABORTING;
1079 sc->sc_msgpriq |= SEND_ABORT;
1080 goto have_nexus;
1081 }
1082
1083 /*
1084 * OK, we are starting a new command.
1085 * Initialize and allocate resources for the new command.
1086 * Device reset is special (only uses MSG_OUT phase).
1087 * Normal commands start in MSG_OUT phase where we will
1088 * send and IDENDIFY message, and then expect CMD phase.
1089 */
1090 #ifdef SUNSCPAL_DEBUG
1091 if (sunscpal_debug & SUNSCPAL_DBG_CMDS) {
1092 printf("sunscpal_sched: begin, target=%d, LUN=%d\n",
1093 xs->xs_periph->periph_target, xs->xs_periph->periph_lun);
1094 sunscpal_show_scsi_cmd(xs);
1095 }
1096 #endif
1097 if (xs->xs_control & XS_CTL_RESET) {
1098 SUNSCPAL_TRACE("sched: cmd=reset, sr=0x%x\n", (long)sr);
1099 /* Not an error, so do not set SUNSCPAL_ABORTING */
1100 sc->sc_msgpriq |= SEND_DEV_RESET;
1101 goto have_nexus;
1102 }
1103
1104 #ifdef DIAGNOSTIC
1105 if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) == 0) {
1106 if (sc->sc_dataptr) {
1107 printf("%s: ptr but no data in/out flags?\n",
1108 sc->sc_dev.dv_xname);
1109 SUNSCPAL_BREAK();
1110 sc->sc_dataptr = NULL;
1111 }
1112 }
1113 #endif
1114
1115 /* Allocate DMA space (maybe) */
1116 if (sc->sc_dataptr && (sc->sc_flags & SUNSCPAL_DISABLE_DMA) == 0 &&
1117 (sc->sc_datalen >= sc->sc_min_dma_len))
1118 {
1119 SUNSCPAL_TRACE("sched: dma_alloc, len=%d\n", sc->sc_datalen);
1120 sunscpal_dma_alloc(sc);
1121 }
1122
1123 /*
1124 * Initialization hook called just after select,
1125 * at the beginning of COMMAND phase.
1126 * (but AFTER the DMA allocation is done)
1127 *
1128 * We need to set up the DMA engine BEFORE the target puts
1129 * the SCSI bus into any DATA phase.
1130 */
1131 if (sr->sr_dma_hand) {
1132 SUNSCPAL_TRACE("sched: dma_setup, dh=0x%x\n",
1133 (long) sr->sr_dma_hand);
1134 sunscpal_dma_setup(sc);
1135 }
1136
1137 /*
1138 * Schedule a timeout for the job we are starting.
1139 */
1140 if ((sr->sr_flags & SR_IMMED) == 0) {
1141 i = (xs->timeout * hz) / 1000;
1142 SUNSCPAL_TRACE("sched: set timeout=%d\n", i);
1143 callout_reset(&sr->sr_xs->xs_callout, i,
1144 sunscpal_cmd_timeout, sr);
1145 }
1146
1147 have_nexus:
1148
1149 SUNSCPAL_TRACE("sched: call machine, cur=0x%x\n",
1150 (long) sc->sc_current);
1151 sunscpal_machine(sc);
1152 SUNSCPAL_TRACE("sched: machine done, cur=0x%x\n",
1153 (long) sc->sc_current);
1154
1155 /*
1156 * What state did sunscpal_machine() leave us in?
1157 * Hopefully it sometimes completes a job...
1158 */
1159 if (sc->sc_state == SUNSCPAL_IDLE)
1160 goto next_job;
1161
1162 return; /* Have work in progress. */
1163 }
1164
1165
1166 /*
1167 * Reselect handler: checks for reselection, and if we are being
1168 * reselected, it sets up sc->sc_current.
1169 *
1170 * We are reselected when:
1171 * SEL is TRUE
1172 * IO is TRUE
1173 * BSY is FALSE
1174 */
1175 void
1176 sunscpal_reselect(sc)
1177 struct sunscpal_softc *sc;
1178 {
1179 /*
1180 * This controller does not implement disconnect/reselect, so
1181 * we really don't have anything to do here. We keep this
1182 * function as a placeholder, though.
1183 */
1184 }
1185
1186 /*
1187 * Select target: xs is the transfer that we are selecting for.
1188 * sc->sc_current should be NULL.
1189 *
1190 * Returns:
1191 * sc->sc_current != NULL ==> we were reselected (race!)
1192 * XS_NOERROR ==> selection worked
1193 * XS_BUSY ==> lost arbitration
1194 * XS_SELTIMEOUT ==> no response to selection
1195 */
1196 static int
1197 sunscpal_select(sc, sr)
1198 struct sunscpal_softc *sc;
1199 struct sunscpal_req *sr;
1200 {
1201 int timo, target_mask;
1202 u_short mode;
1203
1204 /* Check for reselect */
1205 sunscpal_reselect(sc);
1206 if (sc->sc_current) {
1207 SUNSCPAL_TRACE("select: reselect, cur=0x%x\n",
1208 (long) sc->sc_current);
1209 return XS_BUSY; /* reselected */
1210 }
1211
1212 /*
1213 * Select the target.
1214 */
1215 target_mask = (1 << sr->sr_target);
1216 SUNSCPAL_WRITE_1(sc, sunscpal_data, target_mask);
1217 SUNSCPAL_WRITE_2(sc, sunscpal_icr, SUNSCPAL_ICR_SELECT);
1218
1219 /*
1220 * Wait for the target to assert BSY.
1221 * SCSI spec. says wait for 250 mS.
1222 */
1223 for (timo = 25000;;) {
1224 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_BUSY)
1225 goto success;
1226 if (--timo <= 0)
1227 break;
1228 delay(10);
1229 }
1230
1231 SUNSCPAL_WRITE_1(sc, sunscpal_data, 0);
1232 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
1233
1234 SUNSCPAL_TRACE("select: device down, rc=%d\n", XS_SELTIMEOUT);
1235 return XS_SELTIMEOUT;
1236
1237 success:
1238
1239 /*
1240 * The target is now driving BSY, so we can stop
1241 * driving SEL and the data bus. We do set up
1242 * whether or not this target needs parity.
1243 */
1244 mode = 0;
1245 if ((sc->sc_parity_disable & target_mask) == 0)
1246 mode |= SUNSCPAL_ICR_PARITY_ENABLE;
1247 SUNSCPAL_WRITE_2(sc, sunscpal_icr, mode);
1248
1249 return XS_NOERROR;
1250 }
1251
1252 /*****************************************************************
1253 * Functions to handle each info. transfer phase:
1254 *****************************************************************/
1255
1256 /*
1257 * The message system:
1258 *
1259 * This is a revamped message system that now should easier accomodate
1260 * new messages, if necessary.
1261 *
1262 * Currently we accept these messages:
1263 * IDENTIFY (when reselecting)
1264 * COMMAND COMPLETE # (expect bus free after messages marked #)
1265 * NOOP
1266 * MESSAGE REJECT
1267 * SYNCHRONOUS DATA TRANSFER REQUEST
1268 * SAVE DATA POINTER
1269 * RESTORE POINTERS
1270 * DISCONNECT #
1271 *
1272 * We may send these messages in prioritized order:
1273 * BUS DEVICE RESET # if XS_CTL_RESET & xs->xs_control (or in
1274 * weird sits.)
1275 * MESSAGE PARITY ERROR par. err. during MSGI
1276 * MESSAGE REJECT If we get a message we don't know how to handle
1277 * ABORT # send on errors
1278 * INITIATOR DETECTED ERROR also on errors (SCSI2) (during info xfer)
1279 * IDENTIFY At the start of each transfer
1280 * SYNCHRONOUS DATA TRANSFER REQUEST if appropriate
1281 * NOOP if nothing else fits the bill ...
1282 */
1283
1284 #define IS1BYTEMSG(m) (((m) != 0x01 && (m) < 0x20) || (m) >= 0x80)
1285 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
1286 #define ISEXTMSG(m) ((m) == 0x01)
1287
1288 /*
1289 * Precondition:
1290 * The SCSI bus is already in the MSGI phase and there is a message byte
1291 * on the bus, along with an asserted REQ signal.
1292 *
1293 * Our return value determines whether our caller, sunscpal_machine()
1294 * will expect to see another REQ (and possibly phase change).
1295 */
1296 static int
1297 sunscpal_msg_in(sc)
1298 struct sunscpal_softc *sc;
1299 {
1300 struct sunscpal_req *sr = sc->sc_current;
1301 struct scsipi_xfer *xs = sr->sr_xs;
1302 int n, phase;
1303 int act_flags;
1304
1305 act_flags = ACT_CONTINUE;
1306
1307 if (sc->sc_prevphase == SUNSCPAL_PHASE_MSG_IN) {
1308 /* This is a continuation of the previous message. */
1309 n = sc->sc_imp - sc->sc_imess;
1310 SUNSCPAL_TRACE("msg_in: continuation, n=%d\n", n);
1311 goto nextbyte;
1312 }
1313
1314 /* This is a new MESSAGE IN phase. Clean up our state. */
1315 sc->sc_state &= ~SUNSCPAL_DROP_MSGIN;
1316
1317 nextmsg:
1318 n = 0;
1319 sc->sc_imp = &sc->sc_imess[n];
1320
1321 nextbyte:
1322 /*
1323 * Read a whole message, but don't ack the last byte. If we reject the
1324 * message, we have to assert ATN during the message transfer phase
1325 * itself.
1326 */
1327 for (;;) {
1328 /*
1329 * Read a message byte.
1330 * First, check BSY, REQ, phase...
1331 */
1332 if (!SUNSCPAL_BUSY(sc)) {
1333 SUNSCPAL_TRACE("msg_in: lost BSY, n=%d\n", n);
1334 /* XXX - Assume the command completed? */
1335 act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
1336 return (act_flags);
1337 }
1338 if (sunscpal_wait_req(sc)) {
1339 SUNSCPAL_TRACE("msg_in: BSY but no REQ, n=%d\n", n);
1340 /* Just let sunscpal_machine() handle it... */
1341 return (act_flags);
1342 }
1343 phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr));
1344 if (phase != SUNSCPAL_PHASE_MSG_IN) {
1345 /*
1346 * Target left MESSAGE IN, probably because it
1347 * a) noticed our ATN signal, or
1348 * b) ran out of messages.
1349 */
1350 return (act_flags);
1351 }
1352 /* Still in MESSAGE IN phase, and REQ is asserted. */
1353 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_PARITY_ERROR) {
1354 sunscpal_sched_msgout(sc, SEND_PARITY_ERROR);
1355 sc->sc_state |= SUNSCPAL_DROP_MSGIN;
1356 }
1357
1358 /* Gather incoming message bytes if needed. */
1359 if ((sc->sc_state & SUNSCPAL_DROP_MSGIN) == 0) {
1360 if (n >= SUNSCPAL_MAX_MSG_LEN) {
1361 sunscpal_sched_msgout(sc, SEND_REJECT);
1362 sc->sc_state |= SUNSCPAL_DROP_MSGIN;
1363 } else {
1364 *sc->sc_imp++ = SUNSCPAL_READ_1(sc, sunscpal_cmd_stat);
1365 n++;
1366 /*
1367 * This testing is suboptimal, but most
1368 * messages will be of the one byte variety, so
1369 * it should not affect performance
1370 * significantly.
1371 */
1372 if (n == 1 && IS1BYTEMSG(sc->sc_imess[0]))
1373 goto have_msg;
1374 if (n == 2 && IS2BYTEMSG(sc->sc_imess[0]))
1375 goto have_msg;
1376 if (n >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
1377 n == sc->sc_imess[1] + 2)
1378 goto have_msg;
1379 }
1380 }
1381
1382 /*
1383 * If we reach this spot we're either:
1384 * a) in the middle of a multi-byte message, or
1385 * b) dropping bytes.
1386 */
1387
1388 if (act_flags != ACT_CONTINUE)
1389 return (act_flags);
1390
1391 /* back to nextbyte */
1392 }
1393
1394 have_msg:
1395 /* We now have a complete message. Parse it. */
1396
1397 switch (sc->sc_imess[0]) {
1398 case MSG_CMDCOMPLETE:
1399 SUNSCPAL_TRACE("msg_in: CMDCOMPLETE\n", 0);
1400 /* Target is about to disconnect. */
1401 act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
1402 break;
1403
1404 case MSG_PARITY_ERROR:
1405 SUNSCPAL_TRACE("msg_in: PARITY_ERROR\n", 0);
1406 /* Resend the last message. */
1407 sunscpal_sched_msgout(sc, sc->sc_msgout);
1408 break;
1409
1410 case MSG_MESSAGE_REJECT:
1411 /* The target rejects the last message we sent. */
1412 SUNSCPAL_TRACE("msg_in: got reject for 0x%x\n", sc->sc_msgout);
1413 switch (sc->sc_msgout) {
1414 case SEND_IDENTIFY:
1415 /* Really old target controller? */
1416 /* XXX ... */
1417 break;
1418 case SEND_INIT_DET_ERR:
1419 goto abort;
1420 }
1421 break;
1422
1423 case MSG_NOOP:
1424 SUNSCPAL_TRACE("msg_in: NOOP\n", 0);
1425 break;
1426
1427 case MSG_DISCONNECT:
1428 SUNSCPAL_TRACE("msg_in: DISCONNECT\n", 0);
1429 /* Target is about to disconnect. */
1430 act_flags |= ACT_DISCONNECT;
1431 if ((xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
1432 break;
1433 /*FALLTHROUGH*/
1434
1435 case MSG_SAVEDATAPOINTER:
1436 SUNSCPAL_TRACE("msg_in: SAVE_PTRS\n", 0);
1437 sr->sr_dataptr = sc->sc_dataptr;
1438 sr->sr_datalen = sc->sc_datalen;
1439 break;
1440
1441 case MSG_RESTOREPOINTERS:
1442 SUNSCPAL_TRACE("msg_in: RESTORE_PTRS\n", 0);
1443 sc->sc_dataptr = sr->sr_dataptr;
1444 sc->sc_datalen = sr->sr_datalen;
1445 break;
1446
1447 case MSG_EXTENDED:
1448 switch (sc->sc_imess[2]) {
1449 case MSG_EXT_SDTR:
1450 case MSG_EXT_WDTR:
1451 /* The ncr5380 can not do synchronous mode. */
1452 goto reject;
1453 default:
1454 printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
1455 sc->sc_dev.dv_xname);
1456 SUNSCPAL_BREAK();
1457 goto reject;
1458 }
1459 break;
1460
1461 default:
1462 SUNSCPAL_TRACE("msg_in: eh? imsg=0x%x\n", sc->sc_imess[0]);
1463 printf("%s: unrecognized MESSAGE; sending REJECT\n",
1464 sc->sc_dev.dv_xname);
1465 SUNSCPAL_BREAK();
1466 /* fallthrough */
1467 reject:
1468 sunscpal_sched_msgout(sc, SEND_REJECT);
1469 break;
1470
1471 abort:
1472 sc->sc_state |= SUNSCPAL_ABORTING;
1473 sunscpal_sched_msgout(sc, SEND_ABORT);
1474 break;
1475 }
1476
1477 /* Go get the next message, if any. */
1478 if (act_flags == ACT_CONTINUE)
1479 goto nextmsg;
1480
1481 return (act_flags);
1482 }
1483
1484
1485 /*
1486 * The message out (and in) stuff is a bit complicated:
1487 * If the target requests another message (sequence) without
1488 * having changed phase in between it really asks for a
1489 * retransmit, probably due to parity error(s).
1490 * The following messages can be sent:
1491 * IDENTIFY @ These 4 stem from SCSI command activity
1492 * SDTR @
1493 * WDTR @
1494 * DEV_RESET @
1495 * REJECT if MSGI doesn't make sense
1496 * PARITY_ERROR if parity error while in MSGI
1497 * INIT_DET_ERR if parity error while not in MSGI
1498 * ABORT if INIT_DET_ERR rejected
1499 * NOOP if asked for a message and there's nothing to send
1500 *
1501 * Note that we call this one with (sc_current == NULL)
1502 * when sending ABORT for unwanted reselections.
1503 */
1504 static int
1505 sunscpal_msg_out(sc)
1506 struct sunscpal_softc *sc;
1507 {
1508 /*
1509 * This controller does not allow you to assert ATN, which
1510 * means we will never get the opportunity to send messages to
1511 * the target (the bus will never enter this MSG_OUT phase).
1512 * This will eventually leave us with no option other than to
1513 * reset the bus. We keep this function as a placeholder,
1514 * though, and this printf will eventually go away or get
1515 * #ifdef'ed:
1516 */
1517 printf("sunscpal_msg_out: bus is in MSG_OUT phase?\n");
1518 return (ACT_CONTINUE | ACT_RESET_BUS);
1519 }
1520
1521 /*
1522 * Handle command phase.
1523 */
1524 static int
1525 sunscpal_command(sc)
1526 struct sunscpal_softc *sc;
1527 {
1528 struct sunscpal_req *sr = sc->sc_current;
1529 struct scsipi_xfer *xs = sr->sr_xs;
1530 int len;
1531
1532 /* Assume command can be sent in one go. */
1533 /* XXX: Do this using DMA, and get a phase change intr? */
1534 len = sunscpal_pio_out(sc, SUNSCPAL_PHASE_COMMAND, xs->cmdlen,
1535 (u_char *)xs->cmd);
1536
1537 if (len != xs->cmdlen) {
1538 #ifdef SUNSCPAL_DEBUG
1539 printf("sunscpal_command: short transfer: wanted %d got %d.\n",
1540 xs->cmdlen, len);
1541 sunscpal_show_scsi_cmd(xs);
1542 SUNSCPAL_BREAK();
1543 #endif
1544 if (len < 6) {
1545 xs->error = XS_DRIVER_STUFFUP;
1546 sc->sc_state |= SUNSCPAL_ABORTING;
1547 sunscpal_sched_msgout(sc, SEND_ABORT);
1548 }
1549
1550 }
1551
1552 return ACT_CONTINUE;
1553 }
1554
1555
1556 /*
1557 * Handle either data_in or data_out
1558 */
1559 static int
1560 sunscpal_data_xfer(sc, phase)
1561 struct sunscpal_softc *sc;
1562 int phase;
1563 {
1564 struct sunscpal_req *sr = sc->sc_current;
1565 struct scsipi_xfer *xs = sr->sr_xs;
1566 int expected_phase;
1567 int len;
1568
1569 /*
1570 * When aborting a command, disallow any data phase.
1571 */
1572 if (sc->sc_state & SUNSCPAL_ABORTING) {
1573 printf("%s: aborting, bus phase=%s (reset)\n",
1574 sc->sc_dev.dv_xname, phase_names[(phase >> 8) & 7]);
1575 return ACT_RESET_BUS; /* XXX */
1576 }
1577
1578 /* Validate expected phase (data_in or data_out) */
1579 expected_phase = (xs->xs_control & XS_CTL_DATA_OUT) ?
1580 SUNSCPAL_PHASE_DATA_OUT : SUNSCPAL_PHASE_DATA_IN;
1581 if (phase != expected_phase) {
1582 printf("%s: data phase error\n", sc->sc_dev.dv_xname);
1583 goto abort;
1584 }
1585
1586 /* Make sure we have some data to move. */
1587 if (sc->sc_datalen <= 0) {
1588 /* Device needs padding. */
1589 if (phase == SUNSCPAL_PHASE_DATA_IN)
1590 sunscpal_pio_in(sc, phase, 4096, NULL);
1591 else
1592 sunscpal_pio_out(sc, phase, 4096, NULL);
1593 /* Make sure that caused a phase change. */
1594 if (SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr)) == phase) {
1595 /* More than 4k is just too much! */
1596 printf("%s: too much data padding\n",
1597 sc->sc_dev.dv_xname);
1598 goto abort;
1599 }
1600 return ACT_CONTINUE;
1601 }
1602
1603 /*
1604 * Attempt DMA only if dma_alloc gave us a DMA handle AND
1605 * there is enough left to transfer so DMA is worth while.
1606 */
1607 if (sr->sr_dma_hand &&
1608 (sc->sc_datalen >= sc->sc_min_dma_len))
1609 {
1610 /*
1611 * OK, really start DMA. Note, the MD start function
1612 * is responsible for setting the TCMD register, etc.
1613 * (Acknowledge the phase change there, not here.)
1614 */
1615 SUNSCPAL_TRACE("data_xfer: dma_start, dh=0x%x\n",
1616 (long) sr->sr_dma_hand);
1617 sunscpal_dma_start(sc);
1618 return ACT_WAIT_DMA;
1619 }
1620
1621 /*
1622 * Doing PIO for data transfer. (Possibly "Pseudo DMA")
1623 * XXX: Do PDMA functions need to set tcmd later?
1624 */
1625 SUNSCPAL_TRACE("data_xfer: doing PIO, len=%d\n", sc->sc_datalen);
1626 if (phase == SUNSCPAL_PHASE_DATA_OUT) {
1627 len = sunscpal_pio_out(sc, phase, sc->sc_datalen, sc->sc_dataptr);
1628 } else {
1629 len = sunscpal_pio_in(sc, phase, sc->sc_datalen, sc->sc_dataptr);
1630 }
1631 sc->sc_dataptr += len;
1632 sc->sc_datalen -= len;
1633
1634 SUNSCPAL_TRACE("data_xfer: did PIO, resid=%d\n", sc->sc_datalen);
1635 return (ACT_CONTINUE);
1636
1637 abort:
1638 sc->sc_state |= SUNSCPAL_ABORTING;
1639 sunscpal_sched_msgout(sc, SEND_ABORT);
1640 return (ACT_CONTINUE);
1641 }
1642
1643
1644 static int
1645 sunscpal_status(sc)
1646 struct sunscpal_softc *sc;
1647 {
1648 int len;
1649 u_char status;
1650 struct sunscpal_req *sr = sc->sc_current;
1651
1652 len = sunscpal_pio_in(sc, SUNSCPAL_PHASE_STATUS, 1, &status);
1653 if (len) {
1654 sr->sr_status = status;
1655 } else {
1656 printf("sunscpal_status: none?\n");
1657 }
1658
1659 return ACT_CONTINUE;
1660 }
1661
1662
1663 /*
1664 * This is the big state machine that follows SCSI phase changes.
1665 * This is somewhat like a co-routine. It will do a SCSI command,
1666 * and exit if the command is complete, or if it must wait, i.e.
1667 * for DMA to complete or for reselect to resume the job.
1668 *
1669 * The bus must be selected, and we need to know which command is
1670 * being undertaken.
1671 */
1672 static void
1673 sunscpal_machine(sc)
1674 struct sunscpal_softc *sc;
1675 {
1676 struct sunscpal_req *sr;
1677 struct scsipi_xfer *xs;
1678 int act_flags, phase, timo;
1679
1680 #ifdef DIAGNOSTIC
1681 if (sc->sc_state == SUNSCPAL_IDLE)
1682 panic("sunscpal_machine: state=idle");
1683 if (sc->sc_current == NULL)
1684 panic("sunscpal_machine: no current cmd");
1685 #endif
1686
1687 sr = sc->sc_current;
1688 xs = sr->sr_xs;
1689 act_flags = ACT_CONTINUE;
1690
1691 /*
1692 * This will be called by sunscpal_intr() when DMA is
1693 * complete. Must stop DMA before touching the PAL or
1694 * there will be "register conflict" errors.
1695 */
1696 if (sc->sc_state & SUNSCPAL_DOINGDMA) {
1697 /* Pick-up where where we left off... */
1698 goto dma_done;
1699 }
1700
1701 next_phase:
1702
1703 if (!SUNSCPAL_BUSY(sc)) {
1704 /* Unexpected disconnect */
1705 printf("sunscpal_machine: unexpected disconnect.\n");
1706 xs->error = XS_DRIVER_STUFFUP;
1707 act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
1708 goto do_actions;
1709 }
1710
1711 /*
1712 * Wait for REQ before reading the phase.
1713 * Need to wait longer than usual here, because
1714 * some devices are just plain slow...
1715 */
1716 timo = sunscpal_wait_phase_timo;
1717 for (;;) {
1718 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_REQUEST)
1719 break;
1720 if (--timo <= 0) {
1721 if (sc->sc_state & SUNSCPAL_ABORTING) {
1722 printf("%s: no REQ while aborting, reset\n",
1723 sc->sc_dev.dv_xname);
1724 act_flags |= ACT_RESET_BUS;
1725 goto do_actions;
1726 }
1727 printf("%s: no REQ for next phase, abort\n",
1728 sc->sc_dev.dv_xname);
1729 sc->sc_state |= SUNSCPAL_ABORTING;
1730 sunscpal_sched_msgout(sc, SEND_ABORT);
1731 goto next_phase;
1732 }
1733 delay(100);
1734 }
1735
1736 phase = SUNSCPAL_BUS_PHASE(SUNSCPAL_READ_2(sc, sunscpal_icr));
1737 SUNSCPAL_TRACE("machine: phase=%s\n",
1738 (long) phase_names[(phase >> 8) & 7]);
1739
1740 /*
1741 * We assume that the device knows what it's doing,
1742 * so any phase is good.
1743 */
1744
1745 switch (phase) {
1746
1747 case SUNSCPAL_PHASE_DATA_OUT:
1748 case SUNSCPAL_PHASE_DATA_IN:
1749 act_flags = sunscpal_data_xfer(sc, phase);
1750 break;
1751
1752 case SUNSCPAL_PHASE_COMMAND:
1753 act_flags = sunscpal_command(sc);
1754 break;
1755
1756 case SUNSCPAL_PHASE_STATUS:
1757 act_flags = sunscpal_status(sc);
1758 break;
1759
1760 case SUNSCPAL_PHASE_MSG_OUT:
1761 act_flags = sunscpal_msg_out(sc);
1762 break;
1763
1764 case SUNSCPAL_PHASE_MSG_IN:
1765 act_flags = sunscpal_msg_in(sc);
1766 break;
1767
1768 default:
1769 printf("sunscpal_machine: Unexpected phase 0x%x\n", phase);
1770 sc->sc_state |= SUNSCPAL_ABORTING;
1771 sunscpal_sched_msgout(sc, SEND_ABORT);
1772 goto next_phase;
1773
1774 } /* switch */
1775 sc->sc_prevphase = phase;
1776
1777 do_actions:
1778
1779 if (act_flags & ACT_WAIT_DMA) {
1780 act_flags &= ~ACT_WAIT_DMA;
1781 /* Wait for DMA to complete (polling, or interrupt). */
1782 if ((sr->sr_flags & SR_IMMED) == 0) {
1783 SUNSCPAL_TRACE("machine: wait for DMA intr.\n", 0);
1784 return; /* will resume at dma_done */
1785 }
1786 /* Busy-wait for it to finish. */
1787 SUNSCPAL_TRACE("machine: dma_poll, dh=0x%x\n",
1788 (long) sr->sr_dma_hand);
1789 sunscpal_dma_poll(sc);
1790 dma_done:
1791 /* Return here after interrupt. */
1792 if (sr->sr_flags & SR_OVERDUE)
1793 sc->sc_state |= SUNSCPAL_ABORTING;
1794 SUNSCPAL_TRACE("machine: dma_stop, dh=0x%x\n",
1795 (long) sr->sr_dma_hand);
1796 sunscpal_dma_stop(sc);
1797 SUNSCPAL_CLR_INTR(sc); /* XXX */
1798 /*
1799 * While DMA is running we can not touch the SBC,
1800 * so various places just set SUNSCPAL_ABORTING and
1801 * expect us the "kick it" when DMA is done.
1802 */
1803 if (sc->sc_state & SUNSCPAL_ABORTING) {
1804 sunscpal_sched_msgout(sc, SEND_ABORT);
1805 }
1806 }
1807
1808 /*
1809 * Check for parity error.
1810 * XXX - better place to check?
1811 */
1812 if (SUNSCPAL_READ_2(sc, sunscpal_icr) & SUNSCPAL_ICR_PARITY_ERROR) {
1813 printf("%s: parity error!\n", sc->sc_dev.dv_xname);
1814 /* XXX: sc->sc_state |= SUNSCPAL_ABORTING; */
1815 sunscpal_sched_msgout(sc, SEND_PARITY_ERROR);
1816 }
1817
1818 if (act_flags == ACT_CONTINUE)
1819 goto next_phase;
1820 /* All other actions "break" from the loop. */
1821
1822 SUNSCPAL_TRACE("machine: act_flags=0x%x\n", act_flags);
1823
1824 if (act_flags & ACT_RESET_BUS) {
1825 act_flags |= ACT_CMD_DONE;
1826 /*
1827 * Reset the SCSI bus, usually due to a timeout.
1828 * The error code XS_TIMEOUT allows retries.
1829 */
1830 sc->sc_state |= SUNSCPAL_ABORTING;
1831 printf("%s: reset SCSI bus for TID=%d LUN=%d\n",
1832 sc->sc_dev.dv_xname, sr->sr_target, sr->sr_lun);
1833 sunscpal_reset_scsibus(sc);
1834 }
1835
1836 if (act_flags & ACT_CMD_DONE) {
1837 act_flags |= ACT_DISCONNECT;
1838 /* Need to call scsipi_done() */
1839 /* XXX: from the aic6360 driver, but why? */
1840 if (sc->sc_datalen < 0) {
1841 printf("%s: %d extra bytes from %d:%d\n",
1842 sc->sc_dev.dv_xname, -sc->sc_datalen,
1843 sr->sr_target, sr->sr_lun);
1844 sc->sc_datalen = 0;
1845 }
1846 xs->resid = sc->sc_datalen;
1847 /* Note: this will clear sc_current */
1848 SUNSCPAL_TRACE("machine: call done, cur=0x%x\n", (long)sr);
1849 sunscpal_done(sc);
1850 }
1851
1852 if (act_flags & ACT_DISCONNECT) {
1853 /*
1854 * The device has dropped BSY (or will soon).
1855 * We have to wait here for BSY to drop, otherwise
1856 * the next command may decide we need a bus reset.
1857 */
1858 timo = sunscpal_wait_req_timo; /* XXX */
1859 for (;;) {
1860 if (!SUNSCPAL_BUSY(sc))
1861 goto busfree;
1862 if (--timo <= 0)
1863 break;
1864 delay(2);
1865 }
1866 /* Device is sitting on the bus! */
1867 printf("%s: Target %d LUN %d stuck busy, resetting...\n",
1868 sc->sc_dev.dv_xname, sr->sr_target, sr->sr_lun);
1869 sunscpal_reset_scsibus(sc);
1870 busfree:
1871 SUNSCPAL_TRACE("machine: discon, waited %d\n",
1872 sunscpal_wait_req_timo - timo);
1873
1874 SUNSCPAL_WRITE_2(sc, sunscpal_icr, 0);
1875
1876 if ((act_flags & ACT_CMD_DONE) == 0) {
1877 SUNSCPAL_TRACE("machine: discon, cur=0x%x\n", (long)sr);
1878 }
1879
1880 /*
1881 * We may be here due to a disconnect message,
1882 * in which case we did NOT call sunscpal_done,
1883 * and we need to clear sc_current.
1884 */
1885 sc->sc_state = SUNSCPAL_IDLE;
1886 sc->sc_current = NULL;
1887
1888 /* Paranoia: clear everything. */
1889 sc->sc_dataptr = NULL;
1890 sc->sc_datalen = 0;
1891 sc->sc_prevphase = SUNSCPAL_PHASE_INVALID;
1892 sc->sc_msgpriq = 0;
1893 sc->sc_msgoutq = 0;
1894 sc->sc_msgout = 0;
1895
1896 /* Our caller will re-enable interrupts. */
1897 }
1898 }
1899
1900
1901 #ifdef SUNSCPAL_DEBUG
1902
1903 static void
1904 sunscpal_show_scsi_cmd(xs)
1905 struct scsipi_xfer *xs;
1906 {
1907 u_char *b = (u_char *) xs->cmd;
1908 int i = 0;
1909
1910 scsipi_printaddr(xs->xs_periph);
1911 if ( ! ( xs->xs_control & XS_CTL_RESET ) ) {
1912 printf("-");
1913 while (i < xs->cmdlen) {
1914 if (i) printf(",");
1915 printf("%x",b[i++]);
1916 }
1917 printf("-\n");
1918 } else {
1919
1920 printf("-RESET-\n");
1921 }
1922 }
1923
1924
1925 int sunscpal_traceidx = 0;
1926
1927 #define TRACE_MAX 1024
1928 struct trace_ent {
1929 char *msg;
1930 long val;
1931 } sunscpal_tracebuf[TRACE_MAX];
1932
1933 void
1934 sunscpal_trace(msg, val)
1935 char *msg;
1936 long val;
1937 {
1938 struct trace_ent *tr;
1939 int s;
1940
1941 s = splbio();
1942
1943 tr = &sunscpal_tracebuf[sunscpal_traceidx];
1944
1945 sunscpal_traceidx++;
1946 if (sunscpal_traceidx >= TRACE_MAX)
1947 sunscpal_traceidx = 0;
1948
1949 tr->msg = msg;
1950 tr->val = val;
1951
1952 splx(s);
1953 }
1954
1955 #ifdef DDB
1956 void
1957 sunscpal_clear_trace()
1958 {
1959 sunscpal_traceidx = 0;
1960 memset((char*) sunscpal_tracebuf, 0, sizeof(sunscpal_tracebuf));
1961 }
1962
1963 void
1964 sunscpal_show_trace()
1965 {
1966 struct trace_ent *tr;
1967 int idx;
1968
1969 idx = sunscpal_traceidx;
1970 do {
1971 tr = &sunscpal_tracebuf[idx];
1972 idx++;
1973 if (idx >= TRACE_MAX)
1974 idx = 0;
1975 if (tr->msg)
1976 db_printf(tr->msg, tr->val);
1977 } while (idx != sunscpal_traceidx);
1978 }
1979
1980 void
1981 sunscpal_show_req(sr)
1982 struct sunscpal_req *sr;
1983 {
1984 struct scsipi_xfer *xs = sr->sr_xs;
1985
1986 db_printf("TID=%d ", sr->sr_target);
1987 db_printf("LUN=%d ", sr->sr_lun);
1988 db_printf("dh=%p ", sr->sr_dma_hand);
1989 db_printf("dptr=%p ", sr->sr_dataptr);
1990 db_printf("dlen=0x%x ", sr->sr_datalen);
1991 db_printf("flags=%d ", sr->sr_flags);
1992 db_printf("stat=%d ", sr->sr_status);
1993
1994 if (xs == NULL) {
1995 db_printf("(xs=NULL)\n");
1996 return;
1997 }
1998 db_printf("\n");
1999 #ifdef SCSIDEBUG
2000 show_scsipi_xs(xs);
2001 #else
2002 db_printf("xs=%p\n", xs);
2003 #endif
2004 }
2005
2006 void
2007 sunscpal_show_state()
2008 {
2009 struct sunscpal_softc *sc;
2010 struct sunscpal_req *sr;
2011 int i, j, k;
2012
2013 sc = sunscpal_debug_sc;
2014
2015 if (sc == NULL) {
2016 db_printf("sunscpal_debug_sc == NULL\n");
2017 return;
2018 }
2019
2020 db_printf("sc_ncmds=%d\n", sc->sc_ncmds);
2021 k = -1; /* which is current? */
2022 for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
2023 sr = &sc->sc_ring[i];
2024 if (sr->sr_xs) {
2025 if (sr == sc->sc_current)
2026 k = i;
2027 db_printf("req %d: (sr=%p)", i, sr);
2028 sunscpal_show_req(sr);
2029 }
2030 }
2031 db_printf("sc_rr=%d, current=%d\n", sc->sc_rr, k);
2032
2033 db_printf("Active request matrix:\n");
2034 for(i = 0; i < 8; i++) { /* targets */
2035 for (j = 0; j < 8; j++) { /* LUN */
2036 sr = sc->sc_matrix[i][j];
2037 if (sr) {
2038 db_printf("TID=%d LUN=%d sr=%p\n", i, j, sr);
2039 }
2040 }
2041 }
2042
2043 db_printf("sc_state=0x%x\n", sc->sc_state);
2044 db_printf("sc_current=%p\n", sc->sc_current);
2045 db_printf("sc_dataptr=%p\n", sc->sc_dataptr);
2046 db_printf("sc_datalen=0x%x\n", sc->sc_datalen);
2047
2048 db_printf("sc_prevphase=%d\n", sc->sc_prevphase);
2049 db_printf("sc_msgpriq=0x%x\n", sc->sc_msgpriq);
2050 }
2051 #endif /* DDB */
2052 #endif /* SUNSCPAL_DEBUG */
2053
2054 void
2055 sunscpal_attach(sc, options)
2056 struct sunscpal_softc *sc;
2057 int options;
2058 {
2059
2060 /*
2061 * Handle our options.
2062 */
2063 printf(": options=0x%x\n", options);
2064 sc->sc_parity_disable = (options & SUNSCPAL_OPT_NO_PARITY_CHK);
2065 if (options & SUNSCPAL_OPT_FORCE_POLLING)
2066 sc->sc_flags |= SUNSCPAL_FORCE_POLLING;
2067 if (options & SUNSCPAL_OPT_DISABLE_DMA)
2068 sc->sc_flags |= SUNSCPAL_DISABLE_DMA;
2069
2070 /*
2071 * Fill in the adapter.
2072 */
2073 memset(&sc->sc_adapter, 0, sizeof(sc->sc_adapter));
2074 sc->sc_adapter.adapt_dev = &sc->sc_dev;
2075 sc->sc_adapter.adapt_nchannels = 1;
2076 sc->sc_adapter.adapt_openings = SUNSCPAL_OPENINGS;
2077 sc->sc_adapter.adapt_max_periph = 1;
2078 sc->sc_adapter.adapt_request = sunscpal_scsipi_request;
2079 sc->sc_adapter.adapt_minphys = sunscpal_minphys;
2080
2081 sc->sc_channel.chan_adapter = &sc->sc_adapter;
2082 sc->sc_channel.chan_bustype = &scsi_bustype;
2083 sc->sc_channel.chan_channel = 0;
2084 sc->sc_channel.chan_ntargets = 8;
2085 sc->sc_channel.chan_nluns = 8;
2086 sc->sc_channel.chan_id = 7;
2087
2088 /*
2089 * Add reference to adapter so that we drop the reference after
2090 * config_found() to make sure the adatper is disabled.
2091 */
2092 if (scsipi_adapter_addref(&sc->sc_adapter) != 0) {
2093 printf("%s: unable to enable controller\n",
2094 sc->sc_dev.dv_xname);
2095 return;
2096 }
2097
2098 sunscpal_init(sc); /* Init chip and driver */
2099 sunscpal_reset_scsibus(sc);
2100
2101 /*
2102 * Ask the adapter what subunits are present
2103 */
2104 (void) config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
2105 scsipi_adapter_delref(&sc->sc_adapter);
2106 }
2107
2108 int
2109 sunscpal_detach(sc, flags)
2110 struct sunscpal_softc *sc;
2111 int flags;
2112 {
2113
2114 return (EOPNOTSUPP);
2115 }
2116
2117 static void
2118 sunscpal_minphys(struct buf *bp)
2119 {
2120 if (bp->b_bcount > SUNSCPAL_MAX_DMA_LEN) {
2121 #ifdef SUNSCPAL_DEBUG
2122 if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
2123 printf("sunscpal_minphys len = 0x%lx.\n", bp->b_bcount);
2124 Debugger();
2125 }
2126 #endif
2127 bp->b_bcount = SUNSCPAL_MAX_DMA_LEN;
2128 }
2129 return (minphys(bp));
2130 }
2131
2132 #ifdef SUNSCPAL_USE_BUS_DMA
2133
2134 /*
2135 * Allocate a DMA handle and put it in sr->sr_dma_hand. Prepare
2136 * for DMA transfer.
2137 */
2138 static void
2139 sunscpal_dma_alloc(sc)
2140 struct sunscpal_softc *sc;
2141 {
2142 struct sunscpal_req *sr = sc->sc_current;
2143 sunscpal_dma_handle_t dh;
2144 int i, xlen;
2145 u_long addr;
2146
2147 #ifdef DIAGNOSTIC
2148 if (sr->sr_dma_hand != NULL)
2149 panic("sunscpal_dma_alloc: already have DMA handle");
2150 #endif
2151
2152 addr = (u_long) sc->sc_dataptr;
2153 xlen = sc->sc_datalen;
2154
2155 /* If the DMA start addr is misaligned then do PIO */
2156 if ((addr & 1) || (xlen & 1)) {
2157 printf("sunscpal_dma_alloc: misaligned.\n");
2158 return;
2159 }
2160
2161 /* Make sure our caller checked sc_min_dma_len. */
2162 if (xlen < sc->sc_min_dma_len)
2163 panic("sunscpal_dma_alloc: xlen=0x%x\n", xlen);
2164
2165 /*
2166 * Never attempt single transfers of more than 63k, because
2167 * our count register is only 16 bits.
2168 * This should never happen since already bounded by minphys().
2169 * XXX - Should just segment these...
2170 */
2171 if (xlen > SUNSCPAL_MAX_DMA_LEN) {
2172 printf("sunscpal_dma_alloc: excessive xlen=0x%x\n", xlen);
2173 Debugger();
2174 sc->sc_datalen = xlen = SUNSCPAL_MAX_DMA_LEN;
2175 }
2176
2177 /* Find free DMA handle. Guaranteed to find one since we have
2178 as many DMA handles as the driver has processes. */
2179 for (i = 0; i < SUNSCPAL_OPENINGS; i++) {
2180 if ((sc->sc_dma_handles[i].dh_flags & SUNSCDH_BUSY) == 0)
2181 goto found;
2182 }
2183 panic("sc: no free DMA handles.");
2184 found:
2185
2186 dh = &sc->sc_dma_handles[i];
2187 dh->dh_flags = SUNSCDH_BUSY;
2188 dh->dh_mapaddr = (u_char*) addr;
2189 dh->dh_maplen = xlen;
2190 dh->dh_dvma = 0;
2191
2192 /* Load the DMA map. */
2193 if (bus_dmamap_load(sc->sunscpal_dmat, dh->dh_dmamap, dh->dh_mapaddr, dh->dh_maplen, NULL, BUS_DMA_NOWAIT) != 0) {
2194 /* Can't load map */
2195 printf("sunscpal_dma_alloc: can't DMA %p/0x%x\n",
2196 dh->dh_mapaddr, dh->dh_maplen);
2197 dh->dh_flags = 0;
2198 return;
2199 }
2200
2201 /* success */
2202 sr->sr_dma_hand = dh;
2203
2204 return;
2205 }
2206
2207 static void
2208 sunscpal_dma_free(sc)
2209 struct sunscpal_softc *sc;
2210 {
2211 struct sunscpal_req *sr = sc->sc_current;
2212 sunscpal_dma_handle_t dh = sr->sr_dma_hand;
2213
2214 #ifdef DIAGNOSTIC
2215 if (dh == NULL)
2216 panic("sunscpal_dma_free: no DMA handle");
2217 #endif
2218
2219 if (sc->sc_state & SUNSCPAL_DOINGDMA)
2220 panic("sunscpal_dma_free: free while in progress");
2221
2222 if (dh->dh_flags & SUNSCDH_BUSY) {
2223 /* XXX - Should separate allocation and mapping. */
2224 /* Give back the DVMA space. */
2225 bus_dmamap_unload(sc->sunscpal_dmat, dh->dh_dmamap);
2226 dh->dh_flags = 0;
2227 }
2228 sr->sr_dma_hand = NULL;
2229 }
2230
2231 /*
2232 * This function is called during the SELECT phase that
2233 * precedes a COMMAND phase, in case we need to setup the
2234 * DMA engine before the bus enters a DATA phase.
2235 *
2236 * On the sc version, setup the start address and the count.
2237 */
2238 static void
2239 sunscpal_dma_setup(sc)
2240 struct sunscpal_softc *sc;
2241 {
2242 struct sunscpal_req *sr = sc->sc_current;
2243 struct scsipi_xfer *xs = sr->sr_xs;
2244 sunscpal_dma_handle_t dh = sr->sr_dma_hand;
2245 long data_pa;
2246 int xlen;
2247
2248 /*
2249 * Get the DVMA mapping for this segment.
2250 * XXX - Should separate allocation and mapin.
2251 */
2252 data_pa = dh->dh_dvma;
2253 data_pa += (sc->sc_dataptr - dh->dh_mapaddr);
2254 if (data_pa & 1)
2255 panic("sunscpal_dma_setup: bad pa=0x%lx", data_pa);
2256 xlen = sc->sc_datalen;
2257 if (xlen & 1)
2258 panic("sunscpal_dma_setup: bad xlen=0x%x", xlen);
2259 sc->sc_reqlen = xlen; /* XXX: or less? */
2260
2261 #ifdef SUNSCPAL_DEBUG
2262 if (sunscpal_debug & SUNSCPAL_DBG_DMA) {
2263 printf("sunscpal_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
2264 dh, data_pa, xlen);
2265 }
2266 #endif
2267
2268 /* sync the DMA map: */
2269 bus_dmamap_sync(sc->sunscpal_dmat, dh->dh_dmamap, 0, dh->dh_maplen,
2270 ((xs->xs_control & XS_CTL_DATA_OUT) == 0 ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
2271
2272 /* Load the start address and the count. */
2273 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_h, (data_pa >> 16) & 0xFFFF);
2274 SUNSCPAL_WRITE_2(sc, sunscpal_dma_addr_l, (data_pa >> 0) & 0xFFFF);
2275 SUNSCPAL_WRITE_2(sc, sunscpal_dma_count, SUNSCPAL_DMA_COUNT_FLIP(xlen));
2276 }
2277
2278 #endif /* SUNSCPAL_USE_BUS_DMA */
2279