xref: /netbsd-src/sys/arch/sun3/dev/si_sebuf.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: si_sebuf.c,v 1.9 1999/11/03 14:16:33 jdolecek Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Sun3/E SCSI driver (machine-dependent portion).
41  * The machine-independent parts are in ncr5380sbc.c
42  *
43  * XXX - Mostly from the si driver.  Merge?
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/device.h>
52 #include <sys/buf.h>
53 #include <sys/proc.h>
54 #include <sys/user.h>
55 
56 #include <dev/scsipi/scsi_all.h>
57 #include <dev/scsipi/scsipi_all.h>
58 #include <dev/scsipi/scsipi_debug.h>
59 #include <dev/scsipi/scsiconf.h>
60 
61 #include <machine/autoconf.h>
62 
63 /* #define DEBUG XXX */
64 
65 #include <dev/ic/ncr5380reg.h>
66 #include <dev/ic/ncr5380var.h>
67 
68 #include "sereg.h"
69 #include "sevar.h"
70 
71 /*
72  * Transfers smaller than this are done using PIO
73  * (on assumption they're not worth DMA overhead)
74  */
75 #define	MIN_DMA_LEN 128
76 
77 /*
78  * Transfers lager than 65535 bytes need to be split-up.
79  * (Some of the FIFO logic has only 16 bits counters.)
80  * Make the size an integer multiple of the page size
81  * to avoid buf/cluster remap problems.  (paranoid?)
82  */
83 #define	MAX_DMA_LEN 0xE000
84 
85 /*
86  * This structure is used to keep track of mapped DMA requests.
87  */
88 struct se_dma_handle {
89 	int 		dh_flags;
90 #define	SIDH_BUSY	1		/* This DH is in use */
91 #define	SIDH_OUT	2		/* DMA does data out (write) */
92 	u_char *	dh_addr;	/* KVA of start of buffer */
93 	int 		dh_maplen;	/* Length of KVA mapping. */
94 	long		dh_dma; 	/* Offset in DMA buffer. */
95 };
96 
97 /*
98  * The first structure member has to be the ncr5380_softc
99  * so we can just cast to go back and fourth between them.
100  */
101 struct se_softc {
102 	struct ncr5380_softc	ncr_sc;
103 	volatile struct se_regs	*sc_regs;
104 	int		sc_adapter_type;
105 	int		sc_adapter_iv;		/* int. vec */
106 	int 	sc_options;			/* options for this instance */
107 	int 	sc_reqlen;  		/* requested transfer length */
108 	struct se_dma_handle *sc_dma;
109 	/* DMA command block for the OBIO controller. */
110 	void *sc_dmacmd;
111 };
112 
113 /* Options for disconnect/reselect, DMA, and interrupts. */
114 #define SE_NO_DISCONNECT    0xff
115 #define SE_NO_PARITY_CHK  0xff00
116 #define SE_FORCE_POLLING 0x10000
117 #define SE_DISABLE_DMA   0x20000
118 
119 void se_dma_alloc __P((struct ncr5380_softc *));
120 void se_dma_free __P((struct ncr5380_softc *));
121 void se_dma_poll __P((struct ncr5380_softc *));
122 
123 void se_dma_setup __P((struct ncr5380_softc *));
124 void se_dma_start __P((struct ncr5380_softc *));
125 void se_dma_eop __P((struct ncr5380_softc *));
126 void se_dma_stop __P((struct ncr5380_softc *));
127 
128 void se_intr_on  __P((struct ncr5380_softc *));
129 void se_intr_off __P((struct ncr5380_softc *));
130 
131 static int  se_intr __P((void *));
132 static void se_reset __P((struct ncr5380_softc *));
133 
134 /*
135  * New-style autoconfig attachment
136  */
137 
138 static int	se_match __P((struct device *, struct cfdata *, void *));
139 static void	se_attach __P((struct device *, struct device *, void *));
140 
141 struct cfattach si_sebuf_ca = {
142 	sizeof(struct se_softc), se_match, se_attach
143 };
144 
145 static void	se_minphys __P((struct buf *));
146 
147 /* This is copied from julian's bt driver */
148 /* "so we have a default dev struct for our link struct." */
149 static struct scsipi_device se_dev = {
150 	NULL,		/* Use default error handler.	    */
151 	NULL,		/* Use default start handler.		*/
152 	NULL,		/* Use default async handler.	    */
153 	NULL,		/* Use default "done" routine.	    */
154 };
155 
156 /* Options for disconnect/reselect, DMA, and interrupts. */
157 int se_options = SE_DISABLE_DMA | SE_FORCE_POLLING | 0xff;
158 
159 /* How long to wait for DMA before declaring an error. */
160 int se_dma_intr_timo = 500;	/* ticks (sec. X 100) */
161 
162 int se_debug = 0;
163 #ifdef	DEBUG
164 static int se_link_flags = 0 /* | SDEV_DB2 */ ;
165 #endif
166 
167 
168 static int
169 se_match(parent, cf, args)
170 	struct device	*parent;
171 	struct cfdata *cf;
172 	void *args;
173 {
174 	struct sebuf_attach_args *aa = args;
175 
176 	/* Match by name. */
177 	if (strcmp(aa->name, "se"))
178 		return (0);
179 
180 	/* Anyting else to check? */
181 
182 	return (1);
183 }
184 
185 static void
186 se_attach(parent, self, args)
187 	struct device	*parent, *self;
188 	void		*args;
189 {
190 	struct se_softc *sc = (struct se_softc *) self;
191 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
192 	struct cfdata *cf = self->dv_cfdata;
193 	struct sebuf_attach_args *aa = args;
194 	volatile struct se_regs *regs;
195 	int i;
196 
197 	/* Get options from config flags if specified. */
198 	if (cf->cf_flags)
199 		sc->sc_options = cf->cf_flags;
200 	else
201 		sc->sc_options = se_options;
202 
203 	printf(": options=0x%x\n", sc->sc_options);
204 
205 	sc->sc_adapter_type = aa->ca.ca_bustype;
206 	sc->sc_adapter_iv = aa->ca.ca_intvec;
207 	sc->sc_regs = regs = aa->regs;
208 
209 	/*
210 	 * MD function pointers used by the MI code.
211 	 */
212 	ncr_sc->sc_pio_out = ncr5380_pio_out;
213 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
214 
215 #if 0	/* XXX - not yet... */
216 	ncr_sc->sc_dma_alloc = se_dma_alloc;
217 	ncr_sc->sc_dma_free  = se_dma_free;
218 	ncr_sc->sc_dma_setup = se_dma_setup;
219 	ncr_sc->sc_dma_start = se_dma_start;
220 	ncr_sc->sc_dma_poll  = se_dma_poll;
221 	ncr_sc->sc_dma_eop   = se_dma_eop;
222 	ncr_sc->sc_dma_stop  = se_dma_stop;
223 	ncr_sc->sc_intr_on   = se_intr_on;
224 	ncr_sc->sc_intr_off  = se_intr_off;
225 #endif	/* XXX */
226 
227 	/* Attach interrupt handler. */
228 	isr_add_vectored(se_intr, (void *)sc,
229 		aa->ca.ca_intpri, aa->ca.ca_intvec);
230 
231 	/* Reset the hardware. */
232 	se_reset(ncr_sc);
233 
234 	/* Do the common attach stuff. */
235 
236 	/*
237 	 * Support the "options" (config file flags).
238 	 * Disconnect/reselect is a per-target mask.
239 	 * Interrupts and DMA are per-controller.
240 	 */
241 	ncr_sc->sc_no_disconnect =
242 		(sc->sc_options & SE_NO_DISCONNECT);
243 	ncr_sc->sc_parity_disable =
244 		(sc->sc_options & SE_NO_PARITY_CHK) >> 8;
245 	if (sc->sc_options & SE_FORCE_POLLING)
246 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
247 
248 #if 1	/* XXX - Temporary */
249 	/* XXX - In case we think DMA is completely broken... */
250 	if (sc->sc_options & SE_DISABLE_DMA) {
251 		/* Override this function pointer. */
252 		ncr_sc->sc_dma_alloc = NULL;
253 	}
254 #endif
255 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
256 
257 	/*
258 	 * Fill in the adapter.
259 	 */
260 	ncr_sc->sc_adapter.scsipi_cmd = ncr5380_scsi_cmd;
261 	ncr_sc->sc_adapter.scsipi_minphys = se_minphys;
262 
263 	/*
264 	 * Fill in the prototype scsi_link.
265 	 */
266 	ncr_sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
267 	ncr_sc->sc_link.adapter_softc = sc;
268 	ncr_sc->sc_link.scsipi_scsi.adapter_target = 7;
269 	ncr_sc->sc_link.adapter = &ncr_sc->sc_adapter;
270 	ncr_sc->sc_link.device = &se_dev;
271 	ncr_sc->sc_link.type = BUS_SCSI;
272 
273 #ifdef	DEBUG
274 	if (se_debug)
275 		printf("se: Set TheSoftC=%p TheRegs=%p\n", sc, regs);
276 	ncr_sc->sc_link.flags |= se_link_flags;
277 #endif
278 
279 	/*
280 	 * Initialize fields used by the MI code
281 	 */
282 	ncr_sc->sci_r0 = &regs->ncrregs[0];
283 	ncr_sc->sci_r1 = &regs->ncrregs[1];
284 	ncr_sc->sci_r2 = &regs->ncrregs[2];
285 	ncr_sc->sci_r3 = &regs->ncrregs[3];
286 	ncr_sc->sci_r4 = &regs->ncrregs[4];
287 	ncr_sc->sci_r5 = &regs->ncrregs[5];
288 	ncr_sc->sci_r6 = &regs->ncrregs[6];
289 	ncr_sc->sci_r7 = &regs->ncrregs[7];
290 
291 	/*
292 	 * Allocate DMA handles.
293 	 */
294 	i = SCI_OPENINGS * sizeof(struct se_dma_handle);
295 	sc->sc_dma = (struct se_dma_handle *)
296 		malloc(i, M_DEVBUF, M_WAITOK);
297 	if (sc->sc_dma == NULL)
298 		panic("se: dma_malloc failed\n");
299 	for (i = 0; i < SCI_OPENINGS; i++)
300 		sc->sc_dma[i].dh_flags = 0;
301 
302 	/*
303 	 *  Initialize se board itself.
304 	 */
305 	ncr5380_init(ncr_sc);
306 	ncr5380_reset_scsibus(ncr_sc);
307 	config_found(&(ncr_sc->sc_dev), &(ncr_sc->sc_link), scsiprint);
308 }
309 
310 static void
311 se_reset(struct ncr5380_softc *ncr_sc)
312 {
313 	struct se_softc *sc = (struct se_softc *)ncr_sc;
314 	volatile struct se_regs *se = sc->sc_regs;
315 
316 #ifdef	DEBUG
317 	if (se_debug) {
318 		printf("se_reset\n");
319 	}
320 #endif
321 
322 	/* The reset bits in the CSR are active low. */
323 	se->se_csr = 0;
324 	delay(10);
325 	se->se_csr = SE_CSR_SCSI_RES /* | SE_CSR_INTR_EN */ ;
326 	delay(10);
327 
328 	/* Make sure the DMA engine is stopped. */
329 	se->dma_addr = 0;
330 	se->dma_cntr = 0;
331 	se->se_ivec = sc->sc_adapter_iv;
332 }
333 
334 /*
335  * This is called when the bus is going idle,
336  * so we want to enable the SBC interrupts.
337  * That is controlled by the DMA enable!
338  * Who would have guessed!
339  * What a NASTY trick!
340  */
341 void
342 se_intr_on(ncr_sc)
343 	struct ncr5380_softc *ncr_sc;
344 {
345 	struct se_softc *sc = (struct se_softc *)ncr_sc;
346 	volatile struct se_regs *se = sc->sc_regs;
347 
348 	/* receive mode should be safer */
349 	se->se_csr &= ~SE_CSR_SEND;
350 
351 	/* Clear the count so nothing happens. */
352 	se->dma_cntr = 0;
353 
354 	/* Clear the start address too. (paranoid?) */
355 	se->dma_addr = 0;
356 
357 	/* Finally, enable the DMA engine. */
358 	se->se_csr |= SE_CSR_INTR_EN;
359 }
360 
361 /*
362  * This is called when the bus is idle and we are
363  * about to start playing with the SBC chip.
364  */
365 void
366 se_intr_off(ncr_sc)
367 	struct ncr5380_softc *ncr_sc;
368 {
369 	struct se_softc *sc = (struct se_softc *)ncr_sc;
370 	volatile struct se_regs *se = sc->sc_regs;
371 
372 	se->se_csr &= ~SE_CSR_INTR_EN;
373 }
374 
375 /*
376  * This function is called during the COMMAND or MSG_IN phase
377  * that preceeds a DATA_IN or DATA_OUT phase, in case we need
378  * to setup the DMA engine before the bus enters a DATA phase.
379  *
380  * On the VME version, setup the start addres, but clear the
381  * count (to make sure it stays idle) and set that later.
382  * XXX: The VME adapter appears to suppress SBC interrupts
383  * when the FIFO is not empty or the FIFO count is non-zero!
384  * XXX: Need to copy data into the DMA buffer...
385  */
386 void
387 se_dma_setup(ncr_sc)
388 	struct ncr5380_softc *ncr_sc;
389 {
390 	struct se_softc *sc = (struct se_softc *)ncr_sc;
391 	struct sci_req *sr = ncr_sc->sc_current;
392 	struct se_dma_handle *dh = sr->sr_dma_hand;
393 	volatile struct se_regs *se = sc->sc_regs;
394 	long data_pa;
395 	int xlen;
396 
397 	/*
398 	 * Get the DMA mapping for this segment.
399 	 * XXX - Should separate allocation and mapin.
400 	 */
401 	data_pa = 0; /* XXX se_dma_kvtopa(dh->dh_dma); */
402 	data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
403 	if (data_pa & 1)
404 		panic("se_dma_start: bad pa=0x%lx", data_pa);
405 	xlen = ncr_sc->sc_datalen;
406 	xlen &= ~1;				/* XXX: necessary? */
407 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
408 
409 #ifdef	DEBUG
410 	if (se_debug & 2) {
411 		printf("se_dma_setup: dh=%p, pa=0x%lx, xlen=0x%x\n",
412 			   dh, data_pa, xlen);
413 	}
414 #endif
415 
416 	/* Set direction (send/recv) */
417 	if (dh->dh_flags & SIDH_OUT) {
418 		se->se_csr |= SE_CSR_SEND;
419 	} else {
420 		se->se_csr &= ~SE_CSR_SEND;
421 	}
422 
423 	/* Load the start address. */
424 	se->dma_addr = (ushort)(data_pa & 0xFFFF);
425 
426 	/*
427 	 * Keep the count zero or it may start early!
428 	 */
429 	se->dma_cntr = 0;
430 }
431 
432 
433 void
434 se_dma_start(ncr_sc)
435 	struct ncr5380_softc *ncr_sc;
436 {
437 	struct se_softc *sc = (struct se_softc *)ncr_sc;
438 	struct sci_req *sr = ncr_sc->sc_current;
439 	struct se_dma_handle *dh = sr->sr_dma_hand;
440 	volatile struct se_regs *se = sc->sc_regs;
441 	int s, xlen;
442 
443 	xlen = sc->sc_reqlen;
444 
445 	/* This MAY be time critical (not sure). */
446 	s = splhigh();
447 
448 	se->dma_cntr = (ushort)(xlen & 0xFFFF);
449 
450 	/*
451 	 * Acknowledge the phase change.  (After DMA setup!)
452 	 * Put the SBIC into DMA mode, and start the transfer.
453 	 */
454 	if (dh->dh_flags & SIDH_OUT) {
455 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
456 		SCI_CLR_INTR(ncr_sc);
457 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
458 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
459 		*ncr_sc->sci_dma_send = 0;	/* start it */
460 	} else {
461 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
462 		SCI_CLR_INTR(ncr_sc);
463 		*ncr_sc->sci_icmd = 0;
464 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
465 		*ncr_sc->sci_irecv = 0;	/* start it */
466 	}
467 
468 	/* Let'er rip! */
469 	se->se_csr |= SE_CSR_INTR_EN;
470 
471 	splx(s);
472 	ncr_sc->sc_state |= NCR_DOINGDMA;
473 
474 #ifdef	DEBUG
475 	if (se_debug & 2) {
476 		printf("se_dma_start: started, flags=0x%x\n",
477 			   ncr_sc->sc_state);
478 	}
479 #endif
480 }
481 
482 
483 void
484 se_dma_eop(ncr_sc)
485 	struct ncr5380_softc *ncr_sc;
486 {
487 
488 	/* Not needed - DMA was stopped prior to examining sci_csr */
489 }
490 
491 
492 void
493 se_dma_stop(ncr_sc)
494 	struct ncr5380_softc *ncr_sc;
495 {
496 	struct se_softc *sc = (struct se_softc *)ncr_sc;
497 	struct sci_req *sr = ncr_sc->sc_current;
498 	struct se_dma_handle *dh = sr->sr_dma_hand;
499 	volatile struct se_regs *se = sc->sc_regs;
500 	int resid, ntrans;
501 
502 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
503 #ifdef	DEBUG
504 		printf("se_dma_stop: dma not running\n");
505 #endif
506 		return;
507 	}
508 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
509 
510 	/* First, halt the DMA engine. */
511 	se->se_csr &= ~SE_CSR_INTR_EN;	/* VME only */
512 
513 	/* Set an impossible phase to prevent data movement? */
514 	*ncr_sc->sci_tcmd = PHASE_INVALID;
515 
516 	/* Note that timeout may have set the error flag. */
517 	if (ncr_sc->sc_state & NCR_ABORTING)
518 		goto out;
519 
520 	/* XXX: Wait for DMA to actually finish? */
521 
522 	/*
523 	 * Now try to figure out how much actually transferred
524 	 */
525 	resid = se->dma_cntr & 0xFFFF;
526 	if (dh->dh_flags & SIDH_OUT)
527 		if ((resid > 0) && (resid < sc->sc_reqlen))
528 			resid++;
529 	ntrans = sc->sc_reqlen - resid;
530 
531 #ifdef	DEBUG
532 	if (se_debug & 2) {
533 		printf("se_dma_stop: resid=0x%x ntrans=0x%x\n",
534 		       resid, ntrans);
535 	}
536 #endif
537 
538 	if (ntrans < MIN_DMA_LEN) {
539 		printf("se: fifo count: 0x%x\n", resid);
540 		ncr_sc->sc_state |= NCR_ABORTING;
541 		goto out;
542 	}
543 	if (ntrans > ncr_sc->sc_datalen)
544 		panic("se_dma_stop: excess transfer");
545 
546 	/* Adjust data pointer */
547 	ncr_sc->sc_dataptr += ntrans;
548 	ncr_sc->sc_datalen -= ntrans;
549 
550 out:
551 	se->dma_addr = 0;
552 	se->dma_cntr = 0;
553 
554 	/* Put SBIC back in PIO mode. */
555 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
556 	*ncr_sc->sci_icmd = 0;
557 }
558 
559 /*****************************************************************/
560 
561 static void
562 se_minphys(struct buf *bp)
563 {
564 
565 	if (bp->b_bcount > MAX_DMA_LEN)
566 		bp->b_bcount = MAX_DMA_LEN;
567 
568 	return (minphys(bp));
569 }
570 
571 
572 int
573 se_intr(void *arg)
574 {
575 	struct se_softc *sc = arg;
576 	volatile struct se_regs *se = sc->sc_regs;
577 	int dma_error, claimed;
578 	u_short csr;
579 
580 	claimed = 0;
581 	dma_error = 0;
582 
583 	/* SBC interrupt? DMA interrupt? */
584 	csr = se->se_csr;
585 	NCR_TRACE("se_intr: csr=0x%x\n", csr);
586 
587 	if (csr & SE_CSR_SBC_IP) {
588 		claimed = ncr5380_intr(&sc->ncr_sc);
589 #ifdef	DEBUG
590 		if (!claimed) {
591 			printf("se_intr: spurious from SBC\n");
592 		}
593 #endif
594 		/* Yes, we DID cause this interrupt. */
595 		claimed = 1;
596 	}
597 
598 	return (claimed);
599 }
600 
601 
602 /*****************************************************************
603  * Common functions for DMA
604  ****************************************************************/
605 
606 /*
607  * Allocate a DMA handle and put it in sc->sc_dma.  Prepare
608  * for DMA transfer.  On the Sun3/E, this means we have to
609  * allocate space in the DMA buffer for this transfer.
610  */
611 void
612 se_dma_alloc(ncr_sc)
613 	struct ncr5380_softc *ncr_sc;
614 {
615 	struct se_softc *sc = (struct se_softc *)ncr_sc;
616 	struct sci_req *sr = ncr_sc->sc_current;
617 	struct scsipi_xfer *xs = sr->sr_xs;
618 	struct se_dma_handle *dh;
619 	int i, xlen;
620 	u_long addr;
621 
622 #ifdef	DIAGNOSTIC
623 	if (sr->sr_dma_hand != NULL)
624 		panic("se_dma_alloc: already have DMA handle");
625 #endif
626 
627 	addr = (u_long) ncr_sc->sc_dataptr;
628 	xlen = ncr_sc->sc_datalen;
629 
630 	/* If the DMA start addr is misaligned then do PIO */
631 	if ((addr & 1) || (xlen & 1)) {
632 		printf("se_dma_alloc: misaligned.\n");
633 		return;
634 	}
635 
636 	/* Make sure our caller checked sc_min_dma_len. */
637 	if (xlen < MIN_DMA_LEN)
638 		panic("se_dma_alloc: xlen=0x%x\n", xlen);
639 
640 	/*
641 	 * Never attempt single transfers of more than 63k, because
642 	 * our count register may be only 16 bits (an OBIO adapter).
643 	 * This should never happen since already bounded by minphys().
644 	 * XXX - Should just segment these...
645 	 */
646 	if (xlen > MAX_DMA_LEN) {
647 		printf("se_dma_alloc: excessive xlen=0x%x\n", xlen);
648 		ncr_sc->sc_datalen = xlen = MAX_DMA_LEN;
649 	}
650 
651 	/* Find free DMA handle.  Guaranteed to find one since we have
652 	   as many DMA handles as the driver has processes. */
653 	for (i = 0; i < SCI_OPENINGS; i++) {
654 		if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
655 			goto found;
656 	}
657 	panic("se: no free DMA handles.");
658 found:
659 
660 	dh = &sc->sc_dma[i];
661 	dh->dh_flags = SIDH_BUSY;
662 
663 	/* Copy the "write" flag for convenience. */
664 	if (xs->xs_control & XS_CTL_DATA_OUT)
665 		dh->dh_flags |= SIDH_OUT;
666 
667 	dh->dh_addr = (u_char*) addr;
668 	dh->dh_maplen  = xlen;
669 	dh->dh_dma = 0;	/* XXX - Allocate space in DMA buffer. */
670 	/* XXX: dh->dh_dma = alloc(xlen) */
671 	if (!dh->dh_dma) {
672 		/* Can't remap segment */
673 		printf("se_dma_alloc: can't remap %p/0x%x\n",
674 			dh->dh_addr, dh->dh_maplen);
675 		dh->dh_flags = 0;
676 		return;
677 	}
678 
679 	/* success */
680 	sr->sr_dma_hand = dh;
681 
682 	return;
683 }
684 
685 
686 void
687 se_dma_free(ncr_sc)
688 	struct ncr5380_softc *ncr_sc;
689 {
690 	struct sci_req *sr = ncr_sc->sc_current;
691 	struct se_dma_handle *dh = sr->sr_dma_hand;
692 
693 #ifdef	DIAGNOSTIC
694 	if (dh == NULL)
695 		panic("se_dma_free: no DMA handle");
696 #endif
697 
698 	if (ncr_sc->sc_state & NCR_DOINGDMA)
699 		panic("se_dma_free: free while in progress");
700 
701 	if (dh->dh_flags & SIDH_BUSY) {
702 		/* XXX: Should separate allocation and mapping. */
703 		/* XXX: Give back the DMA space. */
704 		/* XXX: free((caddr_t)dh->dh_dma, dh->dh_maplen); */
705 		dh->dh_dma = 0;
706 		dh->dh_flags = 0;
707 	}
708 	sr->sr_dma_hand = NULL;
709 }
710 
711 
712 #define	CSR_MASK SE_CSR_SBC_IP
713 #define	POLL_TIMO	50000	/* X100 = 5 sec. */
714 
715 /*
716  * Poll (spin-wait) for DMA completion.
717  * Called right after xx_dma_start(), and
718  * xx_dma_stop() will be called next.
719  * Same for either VME or OBIO.
720  */
721 void
722 se_dma_poll(ncr_sc)
723 	struct ncr5380_softc *ncr_sc;
724 {
725 	struct se_softc *sc = (struct se_softc *)ncr_sc;
726 	struct sci_req *sr = ncr_sc->sc_current;
727 	volatile struct se_regs *se = sc->sc_regs;
728 	int tmo;
729 
730 	/* Make sure DMA started successfully. */
731 	if (ncr_sc->sc_state & NCR_ABORTING)
732 		return;
733 
734 	/*
735 	 * XXX: The Sun driver waits for ~SE_CSR_DMA_ACTIVE here
736 	 * XXX: (on obio) or even worse (on vme) a 10mS. delay!
737 	 * XXX: I really doubt that is necessary...
738 	 */
739 
740 	/* Wait for any "dma complete" or error bits. */
741 	tmo = POLL_TIMO;
742 	for (;;) {
743 		if (se->se_csr & CSR_MASK)
744 			break;
745 		if (--tmo <= 0) {
746 			printf("se: DMA timeout (while polling)\n");
747 			/* Indicate timeout as MI code would. */
748 			sr->sr_flags |= SR_OVERDUE;
749 			break;
750 		}
751 		delay(100);
752 	}
753 	NCR_TRACE("se_dma_poll: waited %d\n",
754 			  POLL_TIMO - tmo);
755 
756 #ifdef	DEBUG
757 	if (se_debug & 2) {
758 		printf("se_dma_poll: done, csr=0x%x\n", se->se_csr);
759 	}
760 #endif
761 }
762 
763