si.c revision 1.56 1 1.56 chs /* $NetBSD: si.c,v 1.56 2005/01/22 15:36:10 chs Exp $ */
2 1.8 cgd
3 1.31 gwr /*-
4 1.31 gwr * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.1 glass * All rights reserved.
6 1.1 glass *
7 1.31 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.31 gwr * by Adam Glass, David Jones, and Gordon W. Ross.
9 1.31 gwr *
10 1.1 glass * Redistribution and use in source and binary forms, with or without
11 1.1 glass * modification, are permitted provided that the following conditions
12 1.1 glass * are met:
13 1.1 glass * 1. Redistributions of source code must retain the above copyright
14 1.1 glass * notice, this list of conditions and the following disclaimer.
15 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 glass * notice, this list of conditions and the following disclaimer in the
17 1.1 glass * documentation and/or other materials provided with the distribution.
18 1.31 gwr * 3. All advertising materials mentioning features or use of this software
19 1.1 glass * must display the following acknowledgement:
20 1.31 gwr * This product includes software developed by the NetBSD
21 1.31 gwr * Foundation, Inc. and its contributors.
22 1.31 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.31 gwr * contributors may be used to endorse or promote products derived
24 1.31 gwr * from this software without specific prior written permission.
25 1.1 glass *
26 1.31 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.31 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.31 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.33 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.33 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.31 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.31 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.31 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.31 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.31 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.31 gwr * POSSIBILITY OF SUCH DAMAGE.
37 1.1 glass */
38 1.1 glass
39 1.24 gwr /*
40 1.24 gwr * This file contains only the machine-dependent parts of the
41 1.24 gwr * Sun3 SCSI driver. (Autoconfig stuff and DMA functions.)
42 1.24 gwr * The machine-independent parts are in ncr5380sbc.c
43 1.24 gwr *
44 1.24 gwr * Supported hardware includes:
45 1.24 gwr * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
46 1.24 gwr * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
47 1.24 gwr *
48 1.24 gwr * Could be made to support the Sun3/E if someone wanted to.
49 1.24 gwr *
50 1.24 gwr * Note: Both supported variants of the Sun SCSI-3 adapter have
51 1.24 gwr * some really unusual "features" for this driver to deal with,
52 1.24 gwr * generally related to the DMA engine. The OBIO variant will
53 1.24 gwr * ignore any attempt to write the FIFO count register while the
54 1.24 gwr * SCSI bus is in DATA_IN or DATA_OUT phase. This is dealt with
55 1.24 gwr * by setting the FIFO count early in COMMAND or MSG_IN phase.
56 1.24 gwr *
57 1.24 gwr * The VME variant has a bit to enable or disable the DMA engine,
58 1.24 gwr * but that bit also gates the interrupt line from the NCR5380!
59 1.24 gwr * Therefore, in order to get any interrupt from the 5380, (i.e.
60 1.24 gwr * for reselect) one must clear the DMA engine transfer count and
61 1.24 gwr * then enable DMA. This has the further complication that you
62 1.24 gwr * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
63 1.24 gwr * we have to turn DMA back off before we even look at the 5380.
64 1.24 gwr *
65 1.24 gwr * What wonderfully whacky hardware this is!
66 1.24 gwr *
67 1.24 gwr * Credits, history:
68 1.24 gwr *
69 1.24 gwr * David Jones wrote the initial version of this module, which
70 1.24 gwr * included support for the VME adapter only. (no reselection).
71 1.24 gwr *
72 1.24 gwr * Gordon Ross added support for the OBIO adapter, and re-worked
73 1.24 gwr * both the VME and OBIO code to support disconnect/reselect.
74 1.24 gwr * (Required figuring out the hardware "features" noted above.)
75 1.24 gwr *
76 1.24 gwr * The autoconfiguration boilerplate came from Adam Glass.
77 1.24 gwr */
78 1.54 lukem
79 1.54 lukem #include <sys/cdefs.h>
80 1.56 chs __KERNEL_RCSID(0, "$NetBSD: si.c,v 1.56 2005/01/22 15:36:10 chs Exp $");
81 1.1 glass
82 1.1 glass #include <sys/param.h>
83 1.1 glass #include <sys/systm.h>
84 1.1 glass #include <sys/errno.h>
85 1.24 gwr #include <sys/kernel.h>
86 1.24 gwr #include <sys/malloc.h>
87 1.24 gwr #include <sys/device.h>
88 1.1 glass #include <sys/buf.h>
89 1.1 glass #include <sys/proc.h>
90 1.1 glass #include <sys/user.h>
91 1.24 gwr
92 1.36 bouyer #include <dev/scsipi/scsi_all.h>
93 1.36 bouyer #include <dev/scsipi/scsipi_all.h>
94 1.36 bouyer #include <dev/scsipi/scsipi_debug.h>
95 1.36 bouyer #include <dev/scsipi/scsiconf.h>
96 1.1 glass
97 1.1 glass #include <machine/autoconf.h>
98 1.24 gwr #include <machine/dvma.h>
99 1.1 glass
100 1.38 gwr /* #define DEBUG XXX */
101 1.24 gwr
102 1.24 gwr #include <dev/ic/ncr5380reg.h>
103 1.24 gwr #include <dev/ic/ncr5380var.h>
104 1.2 gwr
105 1.24 gwr #include "sireg.h"
106 1.24 gwr #include "sivar.h"
107 1.14 gwr
108 1.30 gwr /*
109 1.30 gwr * Transfers smaller than this are done using PIO
110 1.30 gwr * (on assumption they're not worth DMA overhead)
111 1.30 gwr */
112 1.30 gwr #define MIN_DMA_LEN 128
113 1.30 gwr
114 1.24 gwr int si_debug = 0;
115 1.14 gwr #ifdef DEBUG
116 1.14 gwr #endif
117 1.1 glass
118 1.24 gwr /* How long to wait for DMA before declaring an error. */
119 1.24 gwr int si_dma_intr_timo = 500; /* ticks (sec. X 100) */
120 1.1 glass
121 1.56 chs static void si_minphys(struct buf *);
122 1.1 glass
123 1.24 gwr /*
124 1.24 gwr * New-style autoconfig attachment. The cfattach
125 1.24 gwr * structures are in si_obio.c and si_vme.c
126 1.24 gwr */
127 1.1 glass
128 1.56 chs void
129 1.56 chs si_attach(struct si_softc *sc)
130 1.24 gwr {
131 1.24 gwr struct ncr5380_softc *ncr_sc = (void *)sc;
132 1.24 gwr volatile struct si_regs *regs = sc->sc_regs;
133 1.24 gwr int i;
134 1.30 gwr
135 1.30 gwr /*
136 1.30 gwr * Support the "options" (config file flags).
137 1.34 gwr * Disconnect/reselect is a per-target mask.
138 1.34 gwr * Interrupts and DMA are per-controller.
139 1.30 gwr */
140 1.34 gwr ncr_sc->sc_no_disconnect =
141 1.34 gwr (sc->sc_options & SI_NO_DISCONNECT);
142 1.34 gwr ncr_sc->sc_parity_disable =
143 1.34 gwr (sc->sc_options & SI_NO_PARITY_CHK) >> 8;
144 1.34 gwr if (sc->sc_options & SI_FORCE_POLLING)
145 1.30 gwr ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
146 1.34 gwr
147 1.30 gwr #if 1 /* XXX - Temporary */
148 1.30 gwr /* XXX - In case we think DMA is completely broken... */
149 1.34 gwr if (sc->sc_options & SI_DISABLE_DMA) {
150 1.30 gwr /* Override this function pointer. */
151 1.30 gwr ncr_sc->sc_dma_alloc = NULL;
152 1.30 gwr }
153 1.30 gwr #endif
154 1.30 gwr ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
155 1.16 gwr
156 1.24 gwr /*
157 1.24 gwr * Initialize fields used by the MI code
158 1.24 gwr */
159 1.24 gwr ncr_sc->sci_r0 = ®s->sci.sci_r0;
160 1.24 gwr ncr_sc->sci_r1 = ®s->sci.sci_r1;
161 1.24 gwr ncr_sc->sci_r2 = ®s->sci.sci_r2;
162 1.24 gwr ncr_sc->sci_r3 = ®s->sci.sci_r3;
163 1.24 gwr ncr_sc->sci_r4 = ®s->sci.sci_r4;
164 1.24 gwr ncr_sc->sci_r5 = ®s->sci.sci_r5;
165 1.24 gwr ncr_sc->sci_r6 = ®s->sci.sci_r6;
166 1.24 gwr ncr_sc->sci_r7 = ®s->sci.sci_r7;
167 1.48 tsutsui
168 1.48 tsutsui ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
169 1.12 gwr
170 1.24 gwr /*
171 1.24 gwr * Allocate DMA handles.
172 1.24 gwr */
173 1.24 gwr i = SCI_OPENINGS * sizeof(struct si_dma_handle);
174 1.24 gwr sc->sc_dma = (struct si_dma_handle *)
175 1.24 gwr malloc(i, M_DEVBUF, M_WAITOK);
176 1.24 gwr if (sc->sc_dma == NULL)
177 1.51 provos panic("si: dvma_malloc failed");
178 1.24 gwr for (i = 0; i < SCI_OPENINGS; i++)
179 1.24 gwr sc->sc_dma[i].dh_flags = 0;
180 1.12 gwr
181 1.50 bouyer ncr_sc->sc_channel.chan_id = 7;
182 1.50 bouyer ncr_sc->sc_adapter.adapt_minphys = si_minphys;
183 1.47 mycroft
184 1.12 gwr /*
185 1.24 gwr * Initialize si board itself.
186 1.12 gwr */
187 1.47 mycroft ncr5380_attach(ncr_sc);
188 1.47 mycroft
189 1.1 glass }
190 1.1 glass
191 1.24 gwr static void
192 1.24 gwr si_minphys(struct buf *bp)
193 1.1 glass {
194 1.24 gwr if (bp->b_bcount > MAX_DMA_LEN) {
195 1.14 gwr #ifdef DEBUG
196 1.24 gwr if (si_debug) {
197 1.55 chs printf("si_minphys len = 0x%x.\n", bp->b_bcount);
198 1.24 gwr Debugger();
199 1.1 glass }
200 1.24 gwr #endif
201 1.24 gwr bp->b_bcount = MAX_DMA_LEN;
202 1.1 glass }
203 1.52 kristerw minphys(bp);
204 1.1 glass }
205 1.1 glass
206 1.1 glass
207 1.24 gwr #define CSR_WANT (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
208 1.24 gwr SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR )
209 1.24 gwr
210 1.24 gwr int
211 1.24 gwr si_intr(void *arg)
212 1.1 glass {
213 1.24 gwr struct si_softc *sc = arg;
214 1.24 gwr volatile struct si_regs *si = sc->sc_regs;
215 1.24 gwr int dma_error, claimed;
216 1.24 gwr u_short csr;
217 1.1 glass
218 1.24 gwr claimed = 0;
219 1.24 gwr dma_error = 0;
220 1.24 gwr
221 1.24 gwr /* SBC interrupt? DMA interrupt? */
222 1.24 gwr csr = si->si_csr;
223 1.24 gwr NCR_TRACE("si_intr: csr=0x%x\n", csr);
224 1.24 gwr
225 1.24 gwr if (csr & SI_CSR_DMA_CONFLICT) {
226 1.24 gwr dma_error |= SI_CSR_DMA_CONFLICT;
227 1.29 christos printf("si_intr: DMA conflict\n");
228 1.24 gwr }
229 1.24 gwr if (csr & SI_CSR_DMA_BUS_ERR) {
230 1.24 gwr dma_error |= SI_CSR_DMA_BUS_ERR;
231 1.29 christos printf("si_intr: DMA bus error\n");
232 1.24 gwr }
233 1.24 gwr if (dma_error) {
234 1.24 gwr if (sc->ncr_sc.sc_state & NCR_DOINGDMA)
235 1.24 gwr sc->ncr_sc.sc_state |= NCR_ABORTING;
236 1.24 gwr /* Make sure we will call the main isr. */
237 1.24 gwr csr |= SI_CSR_DMA_IP;
238 1.24 gwr }
239 1.24 gwr
240 1.24 gwr if (csr & (SI_CSR_SBC_IP | SI_CSR_DMA_IP)) {
241 1.24 gwr claimed = ncr5380_intr(&sc->ncr_sc);
242 1.24 gwr #ifdef DEBUG
243 1.24 gwr if (!claimed) {
244 1.29 christos printf("si_intr: spurious from SBC\n");
245 1.44 jdolecek if (si_debug & 4)
246 1.24 gwr Debugger(); /* XXX */
247 1.24 gwr }
248 1.2 gwr #endif
249 1.35 gwr /* Yes, we DID cause this interrupt. */
250 1.35 gwr claimed = 1;
251 1.10 gwr }
252 1.14 gwr
253 1.24 gwr return (claimed);
254 1.1 glass }
255 1.1 glass
256 1.14 gwr
257 1.24 gwr /*****************************************************************
258 1.24 gwr * Common functions for DMA
259 1.24 gwr ****************************************************************/
260 1.1 glass
261 1.24 gwr /*
262 1.24 gwr * Allocate a DMA handle and put it in sc->sc_dma. Prepare
263 1.24 gwr * for DMA transfer. On the Sun3, this means mapping the buffer
264 1.24 gwr * into DVMA space. dvma_mapin() flushes the cache for us.
265 1.24 gwr */
266 1.56 chs void
267 1.56 chs si_dma_alloc(struct ncr5380_softc *ncr_sc)
268 1.24 gwr {
269 1.24 gwr struct si_softc *sc = (struct si_softc *)ncr_sc;
270 1.24 gwr struct sci_req *sr = ncr_sc->sc_current;
271 1.36 bouyer struct scsipi_xfer *xs = sr->sr_xs;
272 1.24 gwr struct si_dma_handle *dh;
273 1.24 gwr int i, xlen;
274 1.24 gwr u_long addr;
275 1.1 glass
276 1.14 gwr #ifdef DIAGNOSTIC
277 1.24 gwr if (sr->sr_dma_hand != NULL)
278 1.24 gwr panic("si_dma_alloc: already have DMA handle");
279 1.14 gwr #endif
280 1.14 gwr
281 1.24 gwr addr = (u_long) ncr_sc->sc_dataptr;
282 1.24 gwr xlen = ncr_sc->sc_datalen;
283 1.13 gwr
284 1.24 gwr /* If the DMA start addr is misaligned then do PIO */
285 1.24 gwr if ((addr & 1) || (xlen & 1)) {
286 1.29 christos printf("si_dma_alloc: misaligned.\n");
287 1.24 gwr return;
288 1.1 glass }
289 1.1 glass
290 1.24 gwr /* Make sure our caller checked sc_min_dma_len. */
291 1.24 gwr if (xlen < MIN_DMA_LEN)
292 1.51 provos panic("si_dma_alloc: xlen=0x%x", xlen);
293 1.14 gwr
294 1.24 gwr /*
295 1.24 gwr * Never attempt single transfers of more than 63k, because
296 1.24 gwr * our count register may be only 16 bits (an OBIO adapter).
297 1.24 gwr * This should never happen since already bounded by minphys().
298 1.24 gwr * XXX - Should just segment these...
299 1.24 gwr */
300 1.24 gwr if (xlen > MAX_DMA_LEN) {
301 1.29 christos printf("si_dma_alloc: excessive xlen=0x%x\n", xlen);
302 1.24 gwr Debugger();
303 1.24 gwr ncr_sc->sc_datalen = xlen = MAX_DMA_LEN;
304 1.24 gwr }
305 1.24 gwr
306 1.24 gwr /* Find free DMA handle. Guaranteed to find one since we have
307 1.24 gwr as many DMA handles as the driver has processes. */
308 1.24 gwr for (i = 0; i < SCI_OPENINGS; i++) {
309 1.24 gwr if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
310 1.24 gwr goto found;
311 1.24 gwr }
312 1.24 gwr panic("si: no free DMA handles.");
313 1.24 gwr found:
314 1.24 gwr
315 1.24 gwr dh = &sc->sc_dma[i];
316 1.24 gwr dh->dh_flags = SIDH_BUSY;
317 1.24 gwr dh->dh_addr = (u_char*) addr;
318 1.24 gwr dh->dh_maplen = xlen;
319 1.24 gwr dh->dh_dvma = 0;
320 1.24 gwr
321 1.24 gwr /* Copy the "write" flag for convenience. */
322 1.45 jdolecek if (xs->xs_control & XS_CTL_DATA_OUT)
323 1.24 gwr dh->dh_flags |= SIDH_OUT;
324 1.17 gwr
325 1.24 gwr #if 0
326 1.24 gwr /*
327 1.24 gwr * Some machines might not need to remap B_PHYS buffers.
328 1.24 gwr * The sun3 does not map B_PHYS buffers into DVMA space,
329 1.24 gwr * (they are mapped into normal KV space) so on the sun3
330 1.24 gwr * we must always remap to a DVMA address here. Re-map is
331 1.24 gwr * cheap anyway, because it's done by segments, not pages.
332 1.24 gwr */
333 1.24 gwr if (xs->bp && (xs->bp->b_flags & B_PHYS))
334 1.24 gwr dh->dh_flags |= SIDH_PHYS;
335 1.14 gwr #endif
336 1.17 gwr
337 1.37 gwr dh->dh_dvma = dvma_mapin((char *)addr, xlen, 0);
338 1.24 gwr if (!dh->dh_dvma) {
339 1.24 gwr /* Can't remap segment */
340 1.32 gwr printf("si_dma_alloc: can't remap %p/0x%x\n",
341 1.24 gwr dh->dh_addr, dh->dh_maplen);
342 1.24 gwr dh->dh_flags = 0;
343 1.24 gwr return;
344 1.14 gwr }
345 1.1 glass
346 1.24 gwr /* success */
347 1.24 gwr sr->sr_dma_hand = dh;
348 1.1 glass
349 1.24 gwr return;
350 1.1 glass }
351 1.1 glass
352 1.1 glass
353 1.56 chs void
354 1.56 chs si_dma_free(struct ncr5380_softc *ncr_sc)
355 1.1 glass {
356 1.24 gwr struct sci_req *sr = ncr_sc->sc_current;
357 1.24 gwr struct si_dma_handle *dh = sr->sr_dma_hand;
358 1.1 glass
359 1.24 gwr #ifdef DIAGNOSTIC
360 1.24 gwr if (dh == NULL)
361 1.24 gwr panic("si_dma_free: no DMA handle");
362 1.24 gwr #endif
363 1.1 glass
364 1.24 gwr if (ncr_sc->sc_state & NCR_DOINGDMA)
365 1.24 gwr panic("si_dma_free: free while in progress");
366 1.1 glass
367 1.24 gwr if (dh->dh_flags & SIDH_BUSY) {
368 1.24 gwr /* XXX - Should separate allocation and mapping. */
369 1.24 gwr /* Give back the DVMA space. */
370 1.37 gwr dvma_mapout(dh->dh_dvma, dh->dh_maplen);
371 1.24 gwr dh->dh_dvma = 0;
372 1.24 gwr dh->dh_flags = 0;
373 1.1 glass }
374 1.24 gwr sr->sr_dma_hand = NULL;
375 1.1 glass }
376 1.1 glass
377 1.1 glass
378 1.25 gwr #define CSR_MASK (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
379 1.25 gwr SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)
380 1.25 gwr #define POLL_TIMO 50000 /* X100 = 5 sec. */
381 1.25 gwr
382 1.24 gwr /*
383 1.24 gwr * Poll (spin-wait) for DMA completion.
384 1.24 gwr * Called right after xx_dma_start(), and
385 1.24 gwr * xx_dma_stop() will be called next.
386 1.24 gwr * Same for either VME or OBIO.
387 1.24 gwr */
388 1.56 chs void
389 1.56 chs si_dma_poll(struct ncr5380_softc *ncr_sc)
390 1.24 gwr {
391 1.24 gwr struct si_softc *sc = (struct si_softc *)ncr_sc;
392 1.24 gwr struct sci_req *sr = ncr_sc->sc_current;
393 1.24 gwr volatile struct si_regs *si = sc->sc_regs;
394 1.25 gwr int tmo;
395 1.1 glass
396 1.24 gwr /* Make sure DMA started successfully. */
397 1.24 gwr if (ncr_sc->sc_state & NCR_ABORTING)
398 1.24 gwr return;
399 1.1 glass
400 1.25 gwr /*
401 1.25 gwr * XXX: The Sun driver waits for ~SI_CSR_DMA_ACTIVE here
402 1.25 gwr * XXX: (on obio) or even worse (on vme) a 10mS. delay!
403 1.25 gwr * XXX: I really doubt that is necessary...
404 1.25 gwr */
405 1.1 glass
406 1.53 wiz /* Wait for any "DMA complete" or error bits. */
407 1.25 gwr tmo = POLL_TIMO;
408 1.24 gwr for (;;) {
409 1.25 gwr if (si->si_csr & CSR_MASK)
410 1.24 gwr break;
411 1.24 gwr if (--tmo <= 0) {
412 1.29 christos printf("si: DMA timeout (while polling)\n");
413 1.24 gwr /* Indicate timeout as MI code would. */
414 1.24 gwr sr->sr_flags |= SR_OVERDUE;
415 1.24 gwr break;
416 1.1 glass }
417 1.24 gwr delay(100);
418 1.1 glass }
419 1.25 gwr NCR_TRACE("si_dma_poll: waited %d\n",
420 1.25 gwr POLL_TIMO - tmo);
421 1.1 glass
422 1.17 gwr #ifdef DEBUG
423 1.25 gwr if (si_debug & 2) {
424 1.29 christos printf("si_dma_poll: done, csr=0x%x\n", si->si_csr);
425 1.17 gwr }
426 1.17 gwr #endif
427 1.1 glass }
428