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