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