xref: /netbsd/sys/dev/isa/wds.c (revision bf9ec67e)
1 /*	$NetBSD: wds.c,v 1.49 2002/04/05 18:27:55 bouyer Exp $	*/
2 
3 /*
4  * XXX
5  * aborts
6  * resets
7  */
8 
9 /*-
10  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
11  * All rights reserved.
12  *
13  * This code is derived from software contributed to The NetBSD Foundation
14  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
15  * NASA Ames Research Center.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. All advertising materials mentioning features or use of this software
26  *    must display the following acknowledgement:
27  *	This product includes software developed by the NetBSD
28  *	Foundation, Inc. and its contributors.
29  * 4. Neither the name of The NetBSD Foundation nor the names of its
30  *    contributors may be used to endorse or promote products derived
31  *    from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
34  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
35  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
37  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  * POSSIBILITY OF SUCH DAMAGE.
44  */
45 
46 /*
47  * Copyright (c) 1994, 1995 Julian Highfield.  All rights reserved.
48  * Portions copyright (c) 1994, 1996, 1997
49  *	Charles M. Hannum.  All rights reserved.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  *    notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  *    notice, this list of conditions and the following disclaimer in the
58  *    documentation and/or other materials provided with the distribution.
59  * 3. All advertising materials mentioning features or use of this software
60  *    must display the following acknowledgement:
61  *	This product includes software developed by Julian Highfield.
62  * 4. The name of the author may not be used to endorse or promote products
63  *    derived from this software without specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
66  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
67  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
68  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
69  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
70  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
71  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
72  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
73  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
74  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75  */
76 
77 /*
78  * This driver is for the WD7000 family of SCSI controllers:
79  *   the WD7000-ASC, a bus-mastering DMA controller,
80  *   the WD7000-FASST2, an -ASC with new firmware and scatter-gather,
81  *   and the WD7000-ASE, which was custom manufactured for Apollo
82  *      workstations and seems to include an -ASC as well as floppy
83  *      and ESDI interfaces.
84  *
85  * Loosely based on Theo Deraadt's unfinished attempt.
86  */
87 
88 #include <sys/cdefs.h>
89 __KERNEL_RCSID(0, "$NetBSD: wds.c,v 1.49 2002/04/05 18:27:55 bouyer Exp $");
90 
91 #include "opt_ddb.h"
92 
93 #undef WDSDIAG
94 #ifdef DDB
95 #define	integrate
96 #else
97 #define	integrate	static inline
98 #endif
99 
100 #include <sys/param.h>
101 #include <sys/systm.h>
102 #include <sys/kernel.h>
103 #include <sys/errno.h>
104 #include <sys/ioctl.h>
105 #include <sys/device.h>
106 #include <sys/malloc.h>
107 #include <sys/buf.h>
108 #include <sys/proc.h>
109 #include <sys/user.h>
110 
111 #include <uvm/uvm_extern.h>
112 
113 #include <machine/bus.h>
114 #include <machine/intr.h>
115 
116 #include <dev/scsipi/scsi_all.h>
117 #include <dev/scsipi/scsipi_all.h>
118 #include <dev/scsipi/scsiconf.h>
119 
120 #include <dev/isa/isavar.h>
121 #include <dev/isa/isadmavar.h>
122 
123 #include <dev/isa/wdsreg.h>
124 
125 #define	WDS_ISA_IOSIZE	8
126 
127 #ifndef DDB
128 #define Debugger() panic("should call debugger here (wds.c)")
129 #endif /* ! DDB */
130 
131 #define	WDS_MAXXFER	((WDS_NSEG - 1) << PGSHIFT)
132 
133 #define WDS_MBX_SIZE	16
134 
135 #define WDS_SCB_MAX	32
136 #define	SCB_HASH_SIZE	32	/* hash table size for phystokv */
137 #define	SCB_HASH_SHIFT	9
138 #define	SCB_HASH(x)	((((long)(x))>>SCB_HASH_SHIFT) & (SCB_HASH_SIZE - 1))
139 
140 #define	wds_nextmbx(wmb, mbx, mbio) \
141 	if ((wmb) == &(mbx)->mbio[WDS_MBX_SIZE - 1])	\
142 		(wmb) = &(mbx)->mbio[0];		\
143 	else						\
144 		(wmb)++;
145 
146 struct wds_mbx {
147 	struct wds_mbx_out mbo[WDS_MBX_SIZE];
148 	struct wds_mbx_in mbi[WDS_MBX_SIZE];
149 	struct wds_mbx_out *cmbo;	/* Collection Mail Box out */
150 	struct wds_mbx_out *tmbo;	/* Target Mail Box out */
151 	struct wds_mbx_in *tmbi;	/* Target Mail Box in */
152 };
153 
154 struct wds_softc {
155 	struct device sc_dev;
156 
157 	bus_space_tag_t sc_iot;
158 	bus_space_handle_t sc_ioh;
159 	bus_dma_tag_t sc_dmat;
160 	bus_dmamap_t sc_dmamap_mbox;	/* maps the mailbox */
161 	void *sc_ih;
162 
163 	struct wds_mbx *sc_mbx;
164 #define	wmbx	(sc->sc_mbx)
165 	struct wds_scb *sc_scbhash[SCB_HASH_SIZE];
166 	TAILQ_HEAD(, wds_scb) sc_free_scb, sc_waiting_scb;
167 	int sc_numscbs, sc_mbofull;
168 
169 	struct scsipi_adapter sc_adapter;
170 	struct scsipi_channel sc_channel;
171 
172 	int sc_revision;
173 	int sc_maxsegs;
174 };
175 
176 struct wds_probe_data {
177 #ifdef notyet
178 	int sc_irq, sc_drq;
179 #endif
180 	int sc_scsi_dev;
181 };
182 
183 integrate void
184 	wds_wait __P((bus_space_tag_t, bus_space_handle_t, int, int, int));
185 int     wds_cmd __P((bus_space_tag_t, bus_space_handle_t, u_char *, int));
186 integrate void wds_finish_scbs __P((struct wds_softc *));
187 int     wdsintr __P((void *));
188 integrate void wds_reset_scb __P((struct wds_softc *, struct wds_scb *));
189 void    wds_free_scb __P((struct wds_softc *, struct wds_scb *));
190 integrate int wds_init_scb __P((struct wds_softc *, struct wds_scb *));
191 struct	wds_scb *wds_get_scb __P((struct wds_softc *));
192 struct	wds_scb *wds_scb_phys_kv __P((struct wds_softc *, u_long));
193 void	wds_queue_scb __P((struct wds_softc *, struct wds_scb *));
194 void	wds_collect_mbo __P((struct wds_softc *));
195 void	wds_start_scbs __P((struct wds_softc *));
196 void    wds_done __P((struct wds_softc *, struct wds_scb *, u_char));
197 int	wds_find __P((bus_space_tag_t, bus_space_handle_t, struct wds_probe_data *));
198 void	wds_attach __P((struct wds_softc *, struct wds_probe_data *));
199 void	wds_init __P((struct wds_softc *, int));
200 void	wds_inquire_setup_information __P((struct wds_softc *));
201 void    wdsminphys __P((struct buf *));
202 void	wds_scsipi_request __P((struct scsipi_channel *,
203 	    scsipi_adapter_req_t, void *));
204 int	wds_poll __P((struct wds_softc *, struct scsipi_xfer *, int));
205 int	wds_ipoll __P((struct wds_softc *, struct wds_scb *, int));
206 void	wds_timeout __P((void *));
207 int	wds_create_scbs __P((struct wds_softc *, void *, size_t));
208 
209 int	wdsprobe __P((struct device *, struct cfdata *, void *));
210 void	wdsattach __P((struct device *, struct device *, void *));
211 
212 struct cfattach wds_ca = {
213 	sizeof(struct wds_softc), wdsprobe, wdsattach
214 };
215 
216 #define	WDS_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
217 
218 integrate void
219 wds_wait(iot, ioh, port, mask, val)
220 	bus_space_tag_t iot;
221 	bus_space_handle_t ioh;
222 	int port;
223 	int mask, val;
224 {
225 
226 	while ((bus_space_read_1(iot, ioh, port) & mask) != val)
227 		;
228 }
229 
230 /*
231  * Write a command to the board's I/O ports.
232  */
233 int
234 wds_cmd(iot, ioh, ibuf, icnt)
235 	bus_space_tag_t iot;
236 	bus_space_handle_t ioh;
237 	u_char *ibuf;
238 	int icnt;
239 {
240 	u_char c;
241 
242 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
243 
244 	while (icnt--) {
245 		bus_space_write_1(iot, ioh, WDS_CMD, *ibuf++);
246 		wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
247 		c = bus_space_read_1(iot, ioh, WDS_STAT);
248 		if (c & WDSS_REJ)
249 			return 1;
250 	}
251 
252 	return 0;
253 }
254 
255 /*
256  * Check for the presence of a WD7000 SCSI controller.
257  */
258 int
259 wdsprobe(parent, match, aux)
260 	struct device *parent;
261 	struct cfdata *match;
262 	void *aux;
263 {
264 	struct isa_attach_args *ia = aux;
265 	bus_space_tag_t iot = ia->ia_iot;
266 	bus_space_handle_t ioh;
267 	struct wds_probe_data wpd;
268 	int rv;
269 
270 	if (ia->ia_nio < 1)
271 		return (0);
272 	if (ia->ia_nirq < 1)
273 		return (0);
274 	if (ia->ia_ndrq < 1)
275 		return (0);
276 
277 	if (ISA_DIRECT_CONFIG(ia))
278 		return (0);
279 
280 	/* Disallow wildcarded i/o address. */
281 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
282 		return (0);
283 
284 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WDS_ISA_IOSIZE, 0, &ioh))
285 		return (0);
286 
287 	rv = wds_find(iot, ioh, &wpd);
288 
289 	bus_space_unmap(iot, ioh, WDS_ISA_IOSIZE);
290 
291 	if (rv) {
292 #ifdef notyet
293 		if (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT &&
294 		    ia->ia_irq[0].ir_irq != wpd.sc_irq)
295 			return (0);
296 		if (ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT &&
297 		    ia->ia_drq[0].ir_drq != wpd.sc_drq)
298 			return (0);
299 
300 		ia->ia_nirq = 1;
301 		ia->ia_irq[0].ir_irq = wpd.sc_irq;
302 
303 		ia->ia_ndrq = 1;
304 		ia->ia_drq[0].ir_drq = wpd.sc_drq;
305 #else
306 		if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
307 			return (0);
308 		if (ia->ia_drq[0].ir_drq == ISACF_DRQ_DEFAULT)
309 			return (0);
310 
311 		ia->ia_nirq = 1;
312 		ia->ia_ndrq = 1;
313 #endif
314 		ia->ia_nio = 1;
315 		ia->ia_io[0].ir_size = WDS_ISA_IOSIZE;
316 
317 		ia->ia_niomem = 0;
318 	}
319 	return (rv);
320 }
321 
322 /*
323  * Attach all available units.
324  */
325 void
326 wdsattach(parent, self, aux)
327 	struct device *parent, *self;
328 	void *aux;
329 {
330 	struct isa_attach_args *ia = aux;
331 	struct wds_softc *sc = (void *)self;
332 	bus_space_tag_t iot = ia->ia_iot;
333 	bus_space_handle_t ioh;
334 	struct wds_probe_data wpd;
335 	isa_chipset_tag_t ic = ia->ia_ic;
336 	int error;
337 
338 	printf("\n");
339 
340 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WDS_ISA_IOSIZE, 0, &ioh)) {
341 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
342 		return;
343 	}
344 
345 	sc->sc_iot = iot;
346 	sc->sc_ioh = ioh;
347 	sc->sc_dmat = ia->ia_dmat;
348 	if (!wds_find(iot, ioh, &wpd)) {
349 		printf("%s: wds_find failed\n", sc->sc_dev.dv_xname);
350 		return;
351 	}
352 
353 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
354 #ifdef notyet
355 	if (wpd.sc_drq != -1) {
356 		if ((error = isa_dmacascade(ic, wpd.sc_drq)) != 0) {
357 			printf("%s: unable to cascade DRQ, error = %d\n",
358 			    sc->sc_dev.dv_xname, error);
359 			return;
360 		}
361 	}
362 
363 	sc->sc_ih = isa_intr_establish(ic, wpd.sc_irq, IST_EDGE, IPL_BIO,
364 	    wdsintr, sc);
365 #else
366 	if ((error = isa_dmacascade(ic, ia->ia_drq[0].ir_drq)) != 0) {
367 		printf("%s: unable to cascade DRQ, error = %d\n",
368 		    sc->sc_dev.dv_xname, error);
369 		return;
370 	}
371 
372 	sc->sc_ih = isa_intr_establish(ic, ia->ia_irq[0].ir_irq, IST_EDGE,
373 	    IPL_BIO, wdsintr, sc);
374 #endif
375 	if (sc->sc_ih == NULL) {
376 		printf("%s: couldn't establish interrupt\n",
377 		    sc->sc_dev.dv_xname);
378 		return;
379 	}
380 
381 	wds_attach(sc, &wpd);
382 }
383 
384 void
385 wds_attach(sc, wpd)
386 	struct wds_softc *sc;
387 	struct wds_probe_data *wpd;
388 {
389 	struct scsipi_adapter *adapt = &sc->sc_adapter;
390 	struct scsipi_channel *chan = &sc->sc_channel;
391 
392 	TAILQ_INIT(&sc->sc_free_scb);
393 	TAILQ_INIT(&sc->sc_waiting_scb);
394 
395 	/*
396 	 * Fill in the scsipi_adapter.
397 	 */
398 	memset(adapt, 0, sizeof(*adapt));
399 	adapt->adapt_dev = &sc->sc_dev;
400 	adapt->adapt_nchannels = 1;
401 	/* adapt_openings initialized below */
402 	adapt->adapt_max_periph = 1;
403 	adapt->adapt_request = wds_scsipi_request;
404 	adapt->adapt_minphys = minphys;
405 
406 	/*
407 	 * Fill in the scsipi_channel.
408 	 */
409 	memset(chan, 0, sizeof(*chan));
410 	chan->chan_adapter = adapt;
411 	chan->chan_bustype = &scsi_bustype;
412 	chan->chan_channel = 0;
413 	chan->chan_ntargets = 8;
414 	chan->chan_nluns = 8;
415 	chan->chan_id = wpd->sc_scsi_dev;
416 
417 	wds_init(sc, 0);
418 	wds_inquire_setup_information(sc);
419 
420 	/* XXX add support for GROW */
421 	adapt->adapt_openings = sc->sc_numscbs;
422 
423 	/*
424 	 * ask the adapter what subunits are present
425 	 */
426 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
427 }
428 
429 integrate void
430 wds_finish_scbs(sc)
431 	struct wds_softc *sc;
432 {
433 	struct wds_mbx_in *wmbi;
434 	struct wds_scb *scb;
435 	int i;
436 
437 	wmbi = wmbx->tmbi;
438 
439 	if (wmbi->stat == WDS_MBI_FREE) {
440 		for (i = 0; i < WDS_MBX_SIZE; i++) {
441 			if (wmbi->stat != WDS_MBI_FREE) {
442 				printf("%s: mbi not in round-robin order\n",
443 				    sc->sc_dev.dv_xname);
444 				goto AGAIN;
445 			}
446 			wds_nextmbx(wmbi, wmbx, mbi);
447 		}
448 #ifdef WDSDIAGnot
449 		printf("%s: mbi interrupt with no full mailboxes\n",
450 		    sc->sc_dev.dv_xname);
451 #endif
452 		return;
453 	}
454 
455 AGAIN:
456 	do {
457 		scb = wds_scb_phys_kv(sc, phystol(wmbi->scb_addr));
458 		if (!scb) {
459 			printf("%s: bad mbi scb pointer; skipping\n",
460 			    sc->sc_dev.dv_xname);
461 			goto next;
462 		}
463 
464 #ifdef WDSDEBUG
465 		if (wds_debug) {
466 			u_char *cp = &scb->scsipi_cmd;
467 			printf("op=%x %x %x %x %x %x\n",
468 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
469 			printf("stat %x for mbi addr = 0x%08x, ",
470 			    wmbi->stat, wmbi);
471 			printf("scb addr = 0x%x\n", scb);
472 		}
473 #endif /* WDSDEBUG */
474 
475 		callout_stop(&scb->xs->xs_callout);
476 		wds_done(sc, scb, wmbi->stat);
477 
478 	next:
479 		wmbi->stat = WDS_MBI_FREE;
480 		wds_nextmbx(wmbi, wmbx, mbi);
481 	} while (wmbi->stat != WDS_MBI_FREE);
482 
483 	wmbx->tmbi = wmbi;
484 }
485 
486 /*
487  * Process an interrupt.
488  */
489 int
490 wdsintr(arg)
491 	void *arg;
492 {
493 	struct wds_softc *sc = arg;
494 	bus_space_tag_t iot = sc->sc_iot;
495 	bus_space_handle_t ioh = sc->sc_ioh;
496 	u_char c;
497 
498 	/* Was it really an interrupt from the board? */
499 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ) == 0)
500 		return 0;
501 
502 	/* Get the interrupt status byte. */
503 	c = bus_space_read_1(iot, ioh, WDS_IRQSTAT) & WDSI_MASK;
504 
505 	/* Acknowledge (which resets) the interrupt. */
506 	bus_space_write_1(iot, ioh, WDS_IRQACK, 0x00);
507 
508 	switch (c) {
509 	case WDSI_MSVC:
510 		wds_finish_scbs(sc);
511 		break;
512 
513 	case WDSI_MFREE:
514 		wds_start_scbs(sc);
515 		break;
516 
517 	default:
518 		printf("%s: unrecognized interrupt type %02x",
519 		    sc->sc_dev.dv_xname, c);
520 		break;
521 	}
522 
523 	return 1;
524 }
525 
526 integrate void
527 wds_reset_scb(sc, scb)
528 	struct wds_softc *sc;
529 	struct wds_scb *scb;
530 {
531 
532 	scb->flags = 0;
533 }
534 
535 /*
536  * Free the command structure, the outgoing mailbox and the data buffer.
537  */
538 void
539 wds_free_scb(sc, scb)
540 	struct wds_softc *sc;
541 	struct wds_scb *scb;
542 {
543 	int s;
544 
545 	s = splbio();
546 	wds_reset_scb(sc, scb);
547 	TAILQ_INSERT_HEAD(&sc->sc_free_scb, scb, chain);
548 	splx(s);
549 }
550 
551 integrate int
552 wds_init_scb(sc, scb)
553 	struct wds_softc *sc;
554 	struct wds_scb *scb;
555 {
556 	bus_dma_tag_t dmat = sc->sc_dmat;
557 	int hashnum, error;
558 
559 	/*
560 	 * XXX Should we put a DIAGNOSTIC check for multiple
561 	 * XXX SCB inits here?
562 	 */
563 
564 	memset(scb, 0, sizeof(struct wds_scb));
565 
566 	/*
567 	 * Create DMA maps for this SCB.
568 	 */
569 	error = bus_dmamap_create(dmat, sizeof(struct wds_scb), 1,
570 	    sizeof(struct wds_scb), 0, BUS_DMA_NOWAIT, &scb->dmamap_self);
571 	if (error) {
572 		printf("%s: can't create scb dmamap_self\n",
573 		    sc->sc_dev.dv_xname);
574 		return (error);
575 	}
576 
577 	error = bus_dmamap_create(dmat, WDS_MAXXFER, WDS_NSEG, WDS_MAXXFER,
578 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &scb->dmamap_xfer);
579 	if (error) {
580 		printf("%s: can't create scb dmamap_xfer\n",
581 		    sc->sc_dev.dv_xname);
582 		bus_dmamap_destroy(dmat, scb->dmamap_self);
583 		return (error);
584 	}
585 
586 	/*
587 	 * Load the permanent DMA maps.
588 	 */
589 	error = bus_dmamap_load(dmat, scb->dmamap_self, scb,
590 	    sizeof(struct wds_scb), NULL, BUS_DMA_NOWAIT);
591 	if (error) {
592 		printf("%s: can't load scb dmamap_self\n",
593 		    sc->sc_dev.dv_xname);
594 		bus_dmamap_destroy(dmat, scb->dmamap_self);
595 		bus_dmamap_destroy(dmat, scb->dmamap_xfer);
596 		return (error);
597 	}
598 
599 	/*
600 	 * put in the phystokv hash table
601 	 * Never gets taken out.
602 	 */
603 	scb->hashkey = scb->dmamap_self->dm_segs[0].ds_addr;
604 	hashnum = SCB_HASH(scb->hashkey);
605 	scb->nexthash = sc->sc_scbhash[hashnum];
606 	sc->sc_scbhash[hashnum] = scb;
607 	wds_reset_scb(sc, scb);
608 	return (0);
609 }
610 
611 /*
612  * Create a set of scbs and add them to the free list.
613  */
614 int
615 wds_create_scbs(sc, mem, size)
616 	struct wds_softc *sc;
617 	void *mem;
618 	size_t size;
619 {
620 	bus_dma_segment_t seg;
621 	struct wds_scb *scb;
622 	int rseg, error;
623 
624 	if (sc->sc_numscbs >= WDS_SCB_MAX)
625 		return (0);
626 
627 	if ((scb = mem) != NULL)
628 		goto have_mem;
629 
630 	size = PAGE_SIZE;
631 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg,
632 	    1, &rseg, BUS_DMA_NOWAIT);
633 	if (error) {
634 		printf("%s: can't allocate memory for scbs\n",
635 		    sc->sc_dev.dv_xname);
636 		return (error);
637 	}
638 
639 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
640 	    (caddr_t *)&scb, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
641 	if (error) {
642 		printf("%s: can't map memory for scbs\n",
643 		    sc->sc_dev.dv_xname);
644 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
645 		return (error);
646 	}
647 
648  have_mem:
649 	memset(scb, 0, size);
650 	while (size > sizeof(struct wds_scb) && sc->sc_numscbs < WDS_SCB_MAX) {
651 		error = wds_init_scb(sc, scb);
652 		if (error) {
653 			printf("%s: can't initialize scb\n",
654 			    sc->sc_dev.dv_xname);
655 			return (error);
656 		}
657 		TAILQ_INSERT_TAIL(&sc->sc_free_scb, scb, chain);
658 		(caddr_t)scb += ALIGN(sizeof(struct wds_scb));
659 		size -= ALIGN(sizeof(struct wds_scb));
660 		sc->sc_numscbs++;
661 	}
662 
663 	return (0);
664 }
665 
666 /*
667  * Get a free scb
668  *
669  * If there are none, see if we can allocate a new one.  If so, put it in
670  * the hash table too otherwise either return an error or sleep.
671  */
672 struct wds_scb *
673 wds_get_scb(sc)
674 	struct wds_softc *sc;
675 {
676 	struct wds_scb *scb;
677 	int s;
678 
679 	s = splbio();
680 	scb = TAILQ_FIRST(&sc->sc_free_scb);
681 	if (scb != NULL) {
682 		TAILQ_REMOVE(&sc->sc_free_scb, scb, chain);
683 		scb->flags |= SCB_ALLOC;
684 	}
685 	splx(s);
686 	return (scb);
687 }
688 
689 struct wds_scb *
690 wds_scb_phys_kv(sc, scb_phys)
691 	struct wds_softc *sc;
692 	u_long scb_phys;
693 {
694 	int hashnum = SCB_HASH(scb_phys);
695 	struct wds_scb *scb = sc->sc_scbhash[hashnum];
696 
697 	while (scb) {
698 		if (scb->hashkey == scb_phys)
699 			break;
700 		/* XXX Check to see if it matches the sense command block. */
701 		if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
702 			break;
703 		scb = scb->nexthash;
704 	}
705 	return (scb);
706 }
707 
708 /*
709  * Queue a SCB to be sent to the controller, and send it if possible.
710  */
711 void
712 wds_queue_scb(sc, scb)
713 	struct wds_softc *sc;
714 	struct wds_scb *scb;
715 {
716 
717 	TAILQ_INSERT_TAIL(&sc->sc_waiting_scb, scb, chain);
718 	wds_start_scbs(sc);
719 }
720 
721 /*
722  * Garbage collect mailboxes that are no longer in use.
723  */
724 void
725 wds_collect_mbo(sc)
726 	struct wds_softc *sc;
727 {
728 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
729 #ifdef WDSDIAG
730 	struct wds_scb *scb;
731 #endif
732 
733 	wmbo = wmbx->cmbo;
734 
735 	while (sc->sc_mbofull > 0) {
736 		if (wmbo->cmd != WDS_MBO_FREE)
737 			break;
738 
739 #ifdef WDSDIAG
740 		scb = wds_scb_phys_kv(sc, phystol(wmbo->scb_addr));
741 		scb->flags &= ~SCB_SENDING;
742 #endif
743 
744 		--sc->sc_mbofull;
745 		wds_nextmbx(wmbo, wmbx, mbo);
746 	}
747 
748 	wmbx->cmbo = wmbo;
749 }
750 
751 /*
752  * Send as many SCBs as we have empty mailboxes for.
753  */
754 void
755 wds_start_scbs(sc)
756 	struct wds_softc *sc;
757 {
758 	bus_space_tag_t iot = sc->sc_iot;
759 	bus_space_handle_t ioh = sc->sc_ioh;
760 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
761 	struct wds_scb *scb;
762 	u_char c;
763 
764 	wmbo = wmbx->tmbo;
765 
766 	while ((scb = sc->sc_waiting_scb.tqh_first) != NULL) {
767 		if (sc->sc_mbofull >= WDS_MBX_SIZE) {
768 			wds_collect_mbo(sc);
769 			if (sc->sc_mbofull >= WDS_MBX_SIZE) {
770 				c = WDSC_IRQMFREE;
771 				wds_cmd(iot, ioh, &c, sizeof c);
772 				break;
773 			}
774 		}
775 
776 		TAILQ_REMOVE(&sc->sc_waiting_scb, scb, chain);
777 #ifdef WDSDIAG
778 		scb->flags |= SCB_SENDING;
779 #endif
780 
781 		/* Link scb to mbo. */
782 		ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
783 		    offsetof(struct wds_scb, cmd), wmbo->scb_addr);
784 		/* XXX What about aborts? */
785 		wmbo->cmd = WDS_MBO_START;
786 
787 		/* Tell the card to poll immediately. */
788 		c = WDSC_MSTART(wmbo - wmbx->mbo);
789 		wds_cmd(sc->sc_iot, sc->sc_ioh, &c, sizeof c);
790 
791 		if ((scb->flags & SCB_POLLED) == 0)
792 			callout_reset(&scb->xs->xs_callout,
793 			    mstohz(scb->timeout), wds_timeout, scb);
794 
795 		++sc->sc_mbofull;
796 		wds_nextmbx(wmbo, wmbx, mbo);
797 	}
798 
799 	wmbx->tmbo = wmbo;
800 }
801 
802 /*
803  * Process the result of a SCSI command.
804  */
805 void
806 wds_done(sc, scb, stat)
807 	struct wds_softc *sc;
808 	struct wds_scb *scb;
809 	u_char stat;
810 {
811 	bus_dma_tag_t dmat = sc->sc_dmat;
812 	struct scsipi_xfer *xs = scb->xs;
813 
814 	/* XXXXX */
815 
816 	/* Don't release the SCB if it was an internal command. */
817 	if (xs == 0) {
818 		scb->flags |= SCB_DONE;
819 		return;
820 	}
821 
822 	/*
823 	 * If we were a data transfer, unload the map that described
824 	 * the data buffer.
825 	 */
826 	if (xs->datalen) {
827 		bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
828 		    scb->dmamap_xfer->dm_mapsize,
829 		    (xs->xs_control & XS_CTL_DATA_IN) ?
830 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
831 		bus_dmamap_unload(dmat, scb->dmamap_xfer);
832 	}
833 	if (xs->error == XS_NOERROR) {
834 		/* If all went well, or an error is acceptable. */
835 		if (stat == WDS_MBI_OK) {
836 			/* OK, set the result */
837 			xs->resid = 0;
838 		} else {
839 			/* Check the mailbox status. */
840 			switch (stat) {
841 			case WDS_MBI_OKERR:
842 				/*
843 				 * SCSI error recorded in scb,
844 				 * counts as WDS_MBI_OK
845 				 */
846 				switch (scb->cmd.venderr) {
847 				case 0x00:
848 					printf("%s: Is this "
849 					    "an error?\n",
850 					    sc->sc_dev.dv_xname);
851 					/* Experiment. */
852 					xs->error = XS_DRIVER_STUFFUP;
853 					break;
854 				case 0x01:
855 #if 0
856 					printf("%s: OK, see SCSI "
857 					    "error field.\n",
858 					    sc->sc_dev.dv_xname);
859 #endif
860 					if (scb->cmd.stat == SCSI_CHECK ||
861 					    scb->cmd.stat == SCSI_BUSY) {
862 						xs->status = scb->cmd.stat;
863 						xs->error = XS_BUSY;
864 					}
865 					break;
866 				case 0x40:
867 #if 0
868 					printf("%s: DMA underrun!\n",
869 					    sc->sc_dev.dv_xname);
870 #endif
871 					/*
872 					 * Hits this if the target
873 					 * returns fewer that datalen
874 					 * bytes (eg my CD-ROM, which
875 					 * returns a short version
876 					 * string, or if DMA is
877 					 * turned off etc.
878 					 */
879 					xs->resid = 0;
880 					break;
881 				default:
882 					printf("%s: VENDOR ERROR "
883 					    "%02x, scsi %02x\n",
884 					    sc->sc_dev.dv_xname,
885 					    scb->cmd.venderr,
886 					    scb->cmd.stat);
887 					/* Experiment. */
888 					xs->error = XS_DRIVER_STUFFUP;
889 					break;
890 				}
891 					break;
892 			case WDS_MBI_ETIME:
893 				/*
894 				 * The documentation isn't clear on
895 				 * what conditions might generate this,
896 				 * but selection timeouts are the only
897 				 * one I can think of.
898 				 */
899 				xs->error = XS_SELTIMEOUT;
900 				break;
901 			case WDS_MBI_ERESET:
902 			case WDS_MBI_ETARCMD:
903 			case WDS_MBI_ERESEL:
904 			case WDS_MBI_ESEL:
905 			case WDS_MBI_EABORT:
906 			case WDS_MBI_ESRESET:
907 			case WDS_MBI_EHRESET:
908 				xs->error = XS_DRIVER_STUFFUP;
909 				break;
910 			}
911 		}
912 	} /* XS_NOERROR */
913 
914 	wds_free_scb(sc, scb);
915 	scsipi_done(xs);
916 }
917 
918 int
919 wds_find(iot, ioh, sc)
920 	bus_space_tag_t iot;
921 	bus_space_handle_t ioh;
922 	struct wds_probe_data *sc;
923 {
924 	int i;
925 
926 	/* XXXXX */
927 
928 	/*
929 	 * Sending a command causes the CMDRDY bit to clear.
930  	 */
931 	for (i = 5; i; i--) {
932 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
933 			break;
934 		delay(100);
935 	}
936 	if (!i)
937 		return 0;
938 
939 	bus_space_write_1(iot, ioh, WDS_CMD, WDSC_NOOP);
940 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
941 		return 0;
942 
943 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
944 	delay(10000);
945 	bus_space_write_1(iot, ioh, WDS_HCR, 0x00);
946 	delay(500000);
947 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
948 	if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 1)
949 		if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 7)
950 			return 0;
951 
952 	for (i = 2000; i; i--) {
953 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
954 			break;
955 		delay(100);
956 	}
957 	if (!i)
958 		return 0;
959 
960 	if (sc) {
961 #ifdef notyet
962 		sc->sc_irq = ...;
963 		sc->sc_drq = ...;
964 #endif
965 		/* XXX Can we do this better? */
966 		sc->sc_scsi_dev = 7;
967 	}
968 
969 	return 1;
970 }
971 
972 /*
973  * Initialise the board and driver.
974  */
975 void
976 wds_init(sc, isreset)
977 	struct wds_softc *sc;
978 	int isreset;
979 {
980 	bus_space_tag_t iot = sc->sc_iot;
981 	bus_space_handle_t ioh = sc->sc_ioh;
982 	bus_dma_segment_t seg;
983 	struct wds_setup init;
984 	u_char c;
985 	int i, rseg;
986 
987 	if (isreset)
988 		goto doinit;
989 
990 	/*
991 	 * Allocate the mailbox.
992 	 */
993 	if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg, 1,
994 	    &rseg, BUS_DMA_NOWAIT) ||
995 	    bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
996 	    (caddr_t *)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
997 		panic("wds_init: can't create or map mailbox");
998 
999 	/*
1000 	 * Since DMA memory allocation is always rounded up to a
1001 	 * page size, create some scbs from the leftovers.
1002 	 */
1003 	if (wds_create_scbs(sc, ((caddr_t)wmbx) +
1004 	    ALIGN(sizeof(struct wds_mbx)),
1005 	    PAGE_SIZE - ALIGN(sizeof(struct wds_mbx))))
1006 		panic("wds_init: can't create scbs");
1007 
1008 	/*
1009 	 * Create and load the mailbox DMA map.
1010 	 */
1011 	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct wds_mbx), 1,
1012 	    sizeof(struct wds_mbx), 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_mbox) ||
1013 	    bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
1014 	    sizeof(struct wds_mbx), NULL, BUS_DMA_NOWAIT))
1015 		panic("wds_ionit: can't craete or load mailbox dma map");
1016 
1017  doinit:
1018 	/*
1019 	 * Set up initial mail box for round-robin operation.
1020 	 */
1021 	for (i = 0; i < WDS_MBX_SIZE; i++) {
1022 		wmbx->mbo[i].cmd = WDS_MBO_FREE;
1023 		wmbx->mbi[i].stat = WDS_MBI_FREE;
1024 	}
1025 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1026 	wmbx->tmbi = &wmbx->mbi[0];
1027 	sc->sc_mbofull = 0;
1028 
1029 	init.opcode = WDSC_INIT;
1030 	init.scsi_id = sc->sc_channel.chan_id;
1031 	init.buson_t = 48;
1032 	init.busoff_t = 24;
1033 	init.xx = 0;
1034 	ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, init.mbaddr);
1035 	init.nomb = init.nimb = WDS_MBX_SIZE;
1036 	wds_cmd(iot, ioh, (u_char *)&init, sizeof init);
1037 
1038 	wds_wait(iot, ioh, WDS_STAT, WDSS_INIT, WDSS_INIT);
1039 
1040 	c = WDSC_DISUNSOL;
1041 	wds_cmd(iot, ioh, &c, sizeof c);
1042 }
1043 
1044 /*
1045  * Read the board's firmware revision information.
1046  */
1047 void
1048 wds_inquire_setup_information(sc)
1049 	struct wds_softc *sc;
1050 {
1051 	bus_space_tag_t iot = sc->sc_iot;
1052 	bus_space_handle_t ioh = sc->sc_ioh;
1053 	struct wds_scb *scb;
1054 	u_char *j;
1055 	int s;
1056 
1057 	sc->sc_maxsegs = 1;
1058 
1059 	scb = wds_get_scb(sc);
1060 	if (scb == 0)
1061 		panic("wds_inquire_setup_information: no scb available");
1062 
1063 	scb->xs = NULL;
1064 	scb->timeout = 40;
1065 
1066 	memset(&scb->cmd, 0, sizeof scb->cmd);
1067 	scb->cmd.write = 0x80;
1068 	scb->cmd.opcode = WDSX_GETFIRMREV;
1069 
1070 	/* Will poll card, await result. */
1071 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
1072 	scb->flags |= SCB_POLLED;
1073 
1074 	s = splbio();
1075 	wds_queue_scb(sc, scb);
1076 	splx(s);
1077 
1078 	if (wds_ipoll(sc, scb, scb->timeout))
1079 		goto out;
1080 
1081 	/* Print the version number. */
1082 	printf("%s: version %x.%02x ", sc->sc_dev.dv_xname,
1083 	    scb->cmd.targ, scb->cmd.scb.opcode);
1084 	sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb.opcode;
1085 	/* Print out the version string. */
1086 	j = 2 + &(scb->cmd.targ);
1087 	while ((*j >= 32) && (*j < 128)) {
1088 		printf("%c", *j);
1089 		j++;
1090 	}
1091 
1092 	/*
1093 	 * Determine if we can use scatter/gather.
1094 	 */
1095 	if (sc->sc_revision >= 0x800)
1096 		sc->sc_maxsegs = WDS_NSEG;
1097 
1098 out:
1099 	printf("\n");
1100 
1101 	/*
1102 	 * Free up the resources used by this scb.
1103 	 */
1104 	wds_free_scb(sc, scb);
1105 }
1106 
1107 void
1108 wdsminphys(bp)
1109 	struct buf *bp;
1110 {
1111 
1112 	if (bp->b_bcount > WDS_MAXXFER)
1113 		bp->b_bcount = WDS_MAXXFER;
1114 	minphys(bp);
1115 }
1116 
1117 /*
1118  * Send a SCSI command.
1119  */
1120 void
1121 wds_scsipi_request(chan, req, arg)
1122 	struct scsipi_channel *chan;
1123 	scsipi_adapter_req_t req;
1124 	void *arg;
1125 {
1126 	struct scsipi_xfer *xs;
1127 	struct scsipi_periph *periph;
1128 	struct wds_softc *sc = (void *)chan->chan_adapter->adapt_dev;
1129 	bus_dma_tag_t dmat = sc->sc_dmat;
1130 	struct wds_scb *scb;
1131 	struct wds_scat_gath *sg;
1132 	int error, seg, flags, s;
1133 
1134 	switch (req) {
1135 	case ADAPTER_REQ_RUN_XFER:
1136 		xs = arg;
1137 		periph = xs->xs_periph;
1138 
1139 		if (xs->xs_control & XS_CTL_RESET) {
1140 			/* XXX Fix me! */
1141 			printf("%s: reset!\n", sc->sc_dev.dv_xname);
1142 			wds_init(sc, 1);
1143 			scsipi_done(xs);
1144 			return;
1145 		}
1146 
1147 		if (xs->xs_control & XS_CTL_DATA_UIO) {
1148 			/* XXX Fix me! */
1149 			/*
1150 			 * Let's not worry about UIO. There isn't any code
1151 			 * for the non-SG boards anyway!
1152 			 */
1153 			printf("%s: UIO is untested and disabled!\n",
1154 			    sc->sc_dev.dv_xname);
1155 			xs->error = XS_DRIVER_STUFFUP;
1156 			scsipi_done(xs);
1157 			return;
1158 		}
1159 
1160 		flags = xs->xs_control;
1161 
1162 		/* Get an SCB to use. */
1163 		scb = wds_get_scb(sc);
1164 #ifdef DIAGNOSTIC
1165 		/*
1166 		 * This should never happen as we track the resources
1167 		 * in the mid-layer.
1168 		 */
1169 		if (scb == NULL) {
1170 			scsipi_printaddr(periph);
1171 			printf("unable to allocate scb\n");
1172 			panic("wds_scsipi_request");
1173 		}
1174 #endif
1175 
1176 		scb->xs = xs;
1177 		scb->timeout = xs->timeout;
1178 
1179 		/* Zero out the command structure. */
1180 		memset(&scb->cmd, 0, sizeof scb->cmd);
1181 		memcpy(&scb->cmd.scb, xs->cmd,
1182 		    xs->cmdlen < 12 ? xs->cmdlen : 12);
1183 
1184 		/* Set up some of the command fields. */
1185 		scb->cmd.targ = (periph->periph_target << 5) |
1186 		    periph->periph_lun;
1187 
1188 		/*
1189 		 * NOTE: cmd.write may be OK as 0x40 (disable direction
1190 		 * checking) on boards other than the WD-7000V-ASE. Need
1191 		 * this for the ASE:
1192  		 */
1193 		scb->cmd.write = (xs->xs_control & XS_CTL_DATA_IN) ?
1194 		    0x80 : 0x00;
1195 
1196 		if (xs->datalen) {
1197 			sg = scb->scat_gath;
1198 			seg = 0;
1199 #ifdef TFS
1200 			if (flags & XS_CTL_DATA_UIO) {
1201 				error = bus_dmamap_load_uio(dmat,
1202 				    scb->dmamap_xfer, (struct uio *)xs->data,
1203 				    BUS_DMA_NOWAIT |
1204 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
1205 				     BUS_DMA_WRITE));
1206 			} else
1207 #endif /* TFS */
1208 			{
1209 				error = bus_dmamap_load(dmat,
1210 				    scb->dmamap_xfer, xs->data, xs->datalen,
1211 				    NULL, BUS_DMA_NOWAIT |
1212 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
1213 				     BUS_DMA_WRITE));
1214 			}
1215 
1216 			switch (error) {
1217 			case 0:
1218 				break;
1219 
1220 			case ENOMEM:
1221 			case EAGAIN:
1222 				xs->error = XS_RESOURCE_SHORTAGE;
1223 				goto out_bad;
1224 
1225 			default:
1226 				xs->error = XS_DRIVER_STUFFUP;
1227 				printf("%s: error %d loading DMA map\n",
1228 				    sc->sc_dev.dv_xname, error);
1229  out_bad:
1230 				wds_free_scb(sc, scb);
1231 				scsipi_done(xs);
1232 				return;
1233 			}
1234 
1235 			bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
1236 			    scb->dmamap_xfer->dm_mapsize,
1237 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
1238 			    BUS_DMASYNC_PREWRITE);
1239 
1240 			if (sc->sc_maxsegs > 1) {
1241 				/*
1242 				 * Load the hardware scatter/gather map with the
1243 				 * contents of the DMA map.
1244 				 */
1245 				for (seg = 0;
1246 				     seg < scb->dmamap_xfer->dm_nsegs; seg++) {
1247 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_addr,
1248 					    scb->scat_gath[seg].seg_addr);
1249 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_len,
1250 					    scb->scat_gath[seg].seg_len);
1251 				}
1252 
1253 				/*
1254 				 * Set up for scatter/gather transfer.
1255 				 */
1256 				scb->cmd.opcode = WDSX_SCSISG;
1257 				ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
1258 				    offsetof(struct wds_scb, scat_gath),
1259 				    scb->cmd.data);
1260 				ltophys(scb->dmamap_self->dm_nsegs *
1261 				    sizeof(struct wds_scat_gath), scb->cmd.len);
1262 			} else {
1263 				/*
1264 				 * This board is an ASC or an ASE, and the
1265 				 * transfer has been mapped contig for us.
1266 				 */
1267 				scb->cmd.opcode = WDSX_SCSICMD;
1268 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_addr,
1269 				    scb->cmd.data);
1270 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_len,
1271 				    scb->cmd.len);
1272 			}
1273 		} else {
1274 			scb->cmd.opcode = WDSX_SCSICMD;
1275 			ltophys(0, scb->cmd.data);
1276 			ltophys(0, scb->cmd.len);
1277 		}
1278 
1279 		scb->cmd.stat = 0x00;
1280 		scb->cmd.venderr = 0x00;
1281 		ltophys(0, scb->cmd.link);
1282 
1283 		/* XXX Do we really want to do this? */
1284 		if (flags & XS_CTL_POLL) {
1285 			/* Will poll card, await result. */
1286 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
1287 			    WDS_HCR, WDSH_DRQEN);
1288 			scb->flags |= SCB_POLLED;
1289 		} else {
1290 			/*
1291 			 * Will send command, let interrupt routine
1292 			 * handle result.
1293 			 */
1294 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
1295 			    WDSH_IRQEN | WDSH_DRQEN);
1296 		}
1297 
1298 		s = splbio();
1299 		wds_queue_scb(sc, scb);
1300 		splx(s);
1301 
1302 		if ((flags & XS_CTL_POLL) == 0)
1303 			return;
1304 
1305 		if (wds_poll(sc, xs, scb->timeout)) {
1306 			wds_timeout(scb);
1307 			if (wds_poll(sc, xs, scb->timeout))
1308 				wds_timeout(scb);
1309 		}
1310 		return;
1311 
1312 	case ADAPTER_REQ_GROW_RESOURCES:
1313 		/* XXX Not supported. */
1314 		return;
1315 
1316 	case ADAPTER_REQ_SET_XFER_MODE:
1317 		/* XXX How do we do this? */
1318 		return;
1319 	}
1320 }
1321 
1322 /*
1323  * Poll a particular unit, looking for a particular scb
1324  */
1325 int
1326 wds_poll(sc, xs, count)
1327 	struct wds_softc *sc;
1328 	struct scsipi_xfer *xs;
1329 	int count;
1330 {
1331 	bus_space_tag_t iot = sc->sc_iot;
1332 	bus_space_handle_t ioh = sc->sc_ioh;
1333 
1334 	/* timeouts are in msec, so we loop in 1000 usec cycles */
1335 	while (count) {
1336 		/*
1337 		 * If we had interrupts enabled, would we
1338 		 * have got an interrupt?
1339 		 */
1340 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1341 			wdsintr(sc);
1342 		if (xs->xs_status & XS_STS_DONE)
1343 			return 0;
1344 		delay(1000);	/* only happens in boot so ok */
1345 		count--;
1346 	}
1347 	return 1;
1348 }
1349 
1350 /*
1351  * Poll a particular unit, looking for a particular scb
1352  */
1353 int
1354 wds_ipoll(sc, scb, count)
1355 	struct wds_softc *sc;
1356 	struct wds_scb *scb;
1357 	int count;
1358 {
1359 	bus_space_tag_t iot = sc->sc_iot;
1360 	bus_space_handle_t ioh = sc->sc_ioh;
1361 
1362 	/* timeouts are in msec, so we loop in 1000 usec cycles */
1363 	while (count) {
1364 		/*
1365 		 * If we had interrupts enabled, would we
1366 		 * have got an interrupt?
1367 		 */
1368 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1369 			wdsintr(sc);
1370 		if (scb->flags & SCB_DONE)
1371 			return 0;
1372 		delay(1000);	/* only happens in boot so ok */
1373 		count--;
1374 	}
1375 	return 1;
1376 }
1377 
1378 void
1379 wds_timeout(arg)
1380 	void *arg;
1381 {
1382 	struct wds_scb *scb = arg;
1383 	struct scsipi_xfer *xs = scb->xs;
1384 	struct scsipi_periph *periph = xs->xs_periph;
1385 	struct wds_softc *sc =
1386 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
1387 	int s;
1388 
1389 	scsipi_printaddr(periph);
1390 	printf("timed out");
1391 
1392 	s = splbio();
1393 
1394 #ifdef WDSDIAG
1395 	/*
1396 	 * If The scb's mbx is not free, then the board has gone south?
1397 	 */
1398 	wds_collect_mbo(sc);
1399 	if (scb->flags & SCB_SENDING) {
1400 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1401 		Debugger();
1402 	}
1403 #endif
1404 
1405 	/*
1406 	 * If it has been through before, then
1407 	 * a previous abort has failed, don't
1408 	 * try abort again
1409 	 */
1410 	if (scb->flags & SCB_ABORT) {
1411 		/* abort timed out */
1412 		printf(" AGAIN\n");
1413 		/* XXX Must reset! */
1414 	} else {
1415 		/* abort the operation that has timed out */
1416 		printf("\n");
1417 		scb->xs->error = XS_TIMEOUT;
1418 		scb->timeout = WDS_ABORT_TIMEOUT;
1419 		scb->flags |= SCB_ABORT;
1420 		wds_queue_scb(sc, scb);
1421 	}
1422 
1423 	splx(s);
1424 }
1425