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