xref: /netbsd-src/sys/dev/isa/wds.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: wds.c,v 1.47 2001/11/15 09:48:10 lukem 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.47 2001/11/15 09:48:10 lukem 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 	/* Disallow wildcarded i/o address. */
271 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
272 		return (0);
273 
274 	if (bus_space_map(iot, ia->ia_iobase, WDS_ISA_IOSIZE, 0, &ioh))
275 		return (0);
276 
277 	rv = wds_find(iot, ioh, &wpd);
278 
279 	bus_space_unmap(iot, ioh, WDS_ISA_IOSIZE);
280 
281 	if (rv) {
282 #ifdef notyet
283 		if (ia->ia_irq != -1 && ia->ia_irq != wpd.sc_irq)
284 			return (0);
285 		if (ia->ia_drq != -1 && ia->ia_drq != wpd.sc_drq)
286 			return (0);
287 		ia->ia_irq = wpd.sc_irq;
288 		ia->ia_drq = wpd.sc_drq;
289 #else
290 		if (ia->ia_irq == -1)
291 			return (0);
292 		if (ia->ia_drq == -1)
293 			return (0);
294 #endif
295 		ia->ia_msize = 0;
296 		ia->ia_iosize = WDS_ISA_IOSIZE;
297 	}
298 	return (rv);
299 }
300 
301 /*
302  * Attach all available units.
303  */
304 void
305 wdsattach(parent, self, aux)
306 	struct device *parent, *self;
307 	void *aux;
308 {
309 	struct isa_attach_args *ia = aux;
310 	struct wds_softc *sc = (void *)self;
311 	bus_space_tag_t iot = ia->ia_iot;
312 	bus_space_handle_t ioh;
313 	struct wds_probe_data wpd;
314 	isa_chipset_tag_t ic = ia->ia_ic;
315 	int error;
316 
317 	printf("\n");
318 
319 	if (bus_space_map(iot, ia->ia_iobase, WDS_ISA_IOSIZE, 0, &ioh)) {
320 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
321 		return;
322 	}
323 
324 	sc->sc_iot = iot;
325 	sc->sc_ioh = ioh;
326 	sc->sc_dmat = ia->ia_dmat;
327 	if (!wds_find(iot, ioh, &wpd)) {
328 		printf("%s: wds_find failed\n", sc->sc_dev.dv_xname);
329 		return;
330 	}
331 
332 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
333 #ifdef notyet
334 	if (wpd.sc_drq != -1) {
335 		if ((error = isa_dmacascade(ic, wpd.sc_drq)) != 0) {
336 			printf("%s: unable to cascade DRQ, error = %d\n",
337 			    sc->sc_dev.dv_xname, error);
338 			return;
339 		}
340 	}
341 
342 	sc->sc_ih = isa_intr_establish(ic, wpd.sc_irq, IST_EDGE, IPL_BIO,
343 	    wdsintr, sc);
344 #else
345 	if (ia->ia_drq != -1) {
346 		if ((error = isa_dmacascade(ic, ia->ia_drq)) != 0) {
347 			printf("%s: unable to cascade DRQ, error = %d\n",
348 			    sc->sc_dev.dv_xname, error);
349 			return;
350 		}
351 	}
352 
353 	sc->sc_ih = isa_intr_establish(ic, ia->ia_irq, IST_EDGE, IPL_BIO,
354 	    wdsintr, sc);
355 #endif
356 	if (sc->sc_ih == NULL) {
357 		printf("%s: couldn't establish interrupt\n",
358 		    sc->sc_dev.dv_xname);
359 		return;
360 	}
361 
362 	wds_attach(sc, &wpd);
363 }
364 
365 void
366 wds_attach(sc, wpd)
367 	struct wds_softc *sc;
368 	struct wds_probe_data *wpd;
369 {
370 	struct scsipi_adapter *adapt = &sc->sc_adapter;
371 	struct scsipi_channel *chan = &sc->sc_channel;
372 
373 	TAILQ_INIT(&sc->sc_free_scb);
374 	TAILQ_INIT(&sc->sc_waiting_scb);
375 
376 	/*
377 	 * Fill in the scsipi_adapter.
378 	 */
379 	memset(adapt, 0, sizeof(*adapt));
380 	adapt->adapt_dev = &sc->sc_dev;
381 	adapt->adapt_nchannels = 1;
382 	/* adapt_openings initialized below */
383 	adapt->adapt_max_periph = 1;
384 	adapt->adapt_request = wds_scsipi_request;
385 	adapt->adapt_minphys = minphys;
386 
387 	/*
388 	 * Fill in the scsipi_channel.
389 	 */
390 	memset(chan, 0, sizeof(*chan));
391 	chan->chan_adapter = adapt;
392 	chan->chan_bustype = &scsi_bustype;
393 	chan->chan_channel = 0;
394 	chan->chan_ntargets = 8;
395 	chan->chan_nluns = 8;
396 	chan->chan_id = wpd->sc_scsi_dev;
397 
398 	wds_init(sc, 0);
399 	wds_inquire_setup_information(sc);
400 
401 	/* XXX add support for GROW */
402 	adapt->adapt_openings = sc->sc_numscbs;
403 
404 	/*
405 	 * ask the adapter what subunits are present
406 	 */
407 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
408 }
409 
410 integrate void
411 wds_finish_scbs(sc)
412 	struct wds_softc *sc;
413 {
414 	struct wds_mbx_in *wmbi;
415 	struct wds_scb *scb;
416 	int i;
417 
418 	wmbi = wmbx->tmbi;
419 
420 	if (wmbi->stat == WDS_MBI_FREE) {
421 		for (i = 0; i < WDS_MBX_SIZE; i++) {
422 			if (wmbi->stat != WDS_MBI_FREE) {
423 				printf("%s: mbi not in round-robin order\n",
424 				    sc->sc_dev.dv_xname);
425 				goto AGAIN;
426 			}
427 			wds_nextmbx(wmbi, wmbx, mbi);
428 		}
429 #ifdef WDSDIAGnot
430 		printf("%s: mbi interrupt with no full mailboxes\n",
431 		    sc->sc_dev.dv_xname);
432 #endif
433 		return;
434 	}
435 
436 AGAIN:
437 	do {
438 		scb = wds_scb_phys_kv(sc, phystol(wmbi->scb_addr));
439 		if (!scb) {
440 			printf("%s: bad mbi scb pointer; skipping\n",
441 			    sc->sc_dev.dv_xname);
442 			goto next;
443 		}
444 
445 #ifdef WDSDEBUG
446 		if (wds_debug) {
447 			u_char *cp = &scb->scsipi_cmd;
448 			printf("op=%x %x %x %x %x %x\n",
449 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
450 			printf("stat %x for mbi addr = 0x%08x, ",
451 			    wmbi->stat, wmbi);
452 			printf("scb addr = 0x%x\n", scb);
453 		}
454 #endif /* WDSDEBUG */
455 
456 		callout_stop(&scb->xs->xs_callout);
457 		wds_done(sc, scb, wmbi->stat);
458 
459 	next:
460 		wmbi->stat = WDS_MBI_FREE;
461 		wds_nextmbx(wmbi, wmbx, mbi);
462 	} while (wmbi->stat != WDS_MBI_FREE);
463 
464 	wmbx->tmbi = wmbi;
465 }
466 
467 /*
468  * Process an interrupt.
469  */
470 int
471 wdsintr(arg)
472 	void *arg;
473 {
474 	struct wds_softc *sc = arg;
475 	bus_space_tag_t iot = sc->sc_iot;
476 	bus_space_handle_t ioh = sc->sc_ioh;
477 	u_char c;
478 
479 	/* Was it really an interrupt from the board? */
480 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ) == 0)
481 		return 0;
482 
483 	/* Get the interrupt status byte. */
484 	c = bus_space_read_1(iot, ioh, WDS_IRQSTAT) & WDSI_MASK;
485 
486 	/* Acknowledge (which resets) the interrupt. */
487 	bus_space_write_1(iot, ioh, WDS_IRQACK, 0x00);
488 
489 	switch (c) {
490 	case WDSI_MSVC:
491 		wds_finish_scbs(sc);
492 		break;
493 
494 	case WDSI_MFREE:
495 		wds_start_scbs(sc);
496 		break;
497 
498 	default:
499 		printf("%s: unrecognized interrupt type %02x",
500 		    sc->sc_dev.dv_xname, c);
501 		break;
502 	}
503 
504 	return 1;
505 }
506 
507 integrate void
508 wds_reset_scb(sc, scb)
509 	struct wds_softc *sc;
510 	struct wds_scb *scb;
511 {
512 
513 	scb->flags = 0;
514 }
515 
516 /*
517  * Free the command structure, the outgoing mailbox and the data buffer.
518  */
519 void
520 wds_free_scb(sc, scb)
521 	struct wds_softc *sc;
522 	struct wds_scb *scb;
523 {
524 	int s;
525 
526 	s = splbio();
527 	wds_reset_scb(sc, scb);
528 	TAILQ_INSERT_HEAD(&sc->sc_free_scb, scb, chain);
529 	splx(s);
530 }
531 
532 integrate int
533 wds_init_scb(sc, scb)
534 	struct wds_softc *sc;
535 	struct wds_scb *scb;
536 {
537 	bus_dma_tag_t dmat = sc->sc_dmat;
538 	int hashnum, error;
539 
540 	/*
541 	 * XXX Should we put a DIAGNOSTIC check for multiple
542 	 * XXX SCB inits here?
543 	 */
544 
545 	memset(scb, 0, sizeof(struct wds_scb));
546 
547 	/*
548 	 * Create DMA maps for this SCB.
549 	 */
550 	error = bus_dmamap_create(dmat, sizeof(struct wds_scb), 1,
551 	    sizeof(struct wds_scb), 0, BUS_DMA_NOWAIT, &scb->dmamap_self);
552 	if (error) {
553 		printf("%s: can't create scb dmamap_self\n",
554 		    sc->sc_dev.dv_xname);
555 		return (error);
556 	}
557 
558 	error = bus_dmamap_create(dmat, WDS_MAXXFER, WDS_NSEG, WDS_MAXXFER,
559 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &scb->dmamap_xfer);
560 	if (error) {
561 		printf("%s: can't create scb dmamap_xfer\n",
562 		    sc->sc_dev.dv_xname);
563 		bus_dmamap_destroy(dmat, scb->dmamap_self);
564 		return (error);
565 	}
566 
567 	/*
568 	 * Load the permanent DMA maps.
569 	 */
570 	error = bus_dmamap_load(dmat, scb->dmamap_self, scb,
571 	    sizeof(struct wds_scb), NULL, BUS_DMA_NOWAIT);
572 	if (error) {
573 		printf("%s: can't load scb dmamap_self\n",
574 		    sc->sc_dev.dv_xname);
575 		bus_dmamap_destroy(dmat, scb->dmamap_self);
576 		bus_dmamap_destroy(dmat, scb->dmamap_xfer);
577 		return (error);
578 	}
579 
580 	/*
581 	 * put in the phystokv hash table
582 	 * Never gets taken out.
583 	 */
584 	scb->hashkey = scb->dmamap_self->dm_segs[0].ds_addr;
585 	hashnum = SCB_HASH(scb->hashkey);
586 	scb->nexthash = sc->sc_scbhash[hashnum];
587 	sc->sc_scbhash[hashnum] = scb;
588 	wds_reset_scb(sc, scb);
589 	return (0);
590 }
591 
592 /*
593  * Create a set of scbs and add them to the free list.
594  */
595 int
596 wds_create_scbs(sc, mem, size)
597 	struct wds_softc *sc;
598 	void *mem;
599 	size_t size;
600 {
601 	bus_dma_segment_t seg;
602 	struct wds_scb *scb;
603 	int rseg, error;
604 
605 	if (sc->sc_numscbs >= WDS_SCB_MAX)
606 		return (0);
607 
608 	if ((scb = mem) != NULL)
609 		goto have_mem;
610 
611 	size = PAGE_SIZE;
612 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg,
613 	    1, &rseg, BUS_DMA_NOWAIT);
614 	if (error) {
615 		printf("%s: can't allocate memory for scbs\n",
616 		    sc->sc_dev.dv_xname);
617 		return (error);
618 	}
619 
620 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
621 	    (caddr_t *)&scb, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
622 	if (error) {
623 		printf("%s: can't map memory for scbs\n",
624 		    sc->sc_dev.dv_xname);
625 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
626 		return (error);
627 	}
628 
629  have_mem:
630 	memset(scb, 0, size);
631 	while (size > sizeof(struct wds_scb) && sc->sc_numscbs < WDS_SCB_MAX) {
632 		error = wds_init_scb(sc, scb);
633 		if (error) {
634 			printf("%s: can't initialize scb\n",
635 			    sc->sc_dev.dv_xname);
636 			return (error);
637 		}
638 		TAILQ_INSERT_TAIL(&sc->sc_free_scb, scb, chain);
639 		(caddr_t)scb += ALIGN(sizeof(struct wds_scb));
640 		size -= ALIGN(sizeof(struct wds_scb));
641 		sc->sc_numscbs++;
642 	}
643 
644 	return (0);
645 }
646 
647 /*
648  * Get a free scb
649  *
650  * If there are none, see if we can allocate a new one.  If so, put it in
651  * the hash table too otherwise either return an error or sleep.
652  */
653 struct wds_scb *
654 wds_get_scb(sc)
655 	struct wds_softc *sc;
656 {
657 	struct wds_scb *scb;
658 	int s;
659 
660 	s = splbio();
661 	scb = TAILQ_FIRST(&sc->sc_free_scb);
662 	if (scb != NULL) {
663 		TAILQ_REMOVE(&sc->sc_free_scb, scb, chain);
664 		scb->flags |= SCB_ALLOC;
665 	}
666 	splx(s);
667 	return (scb);
668 }
669 
670 struct wds_scb *
671 wds_scb_phys_kv(sc, scb_phys)
672 	struct wds_softc *sc;
673 	u_long scb_phys;
674 {
675 	int hashnum = SCB_HASH(scb_phys);
676 	struct wds_scb *scb = sc->sc_scbhash[hashnum];
677 
678 	while (scb) {
679 		if (scb->hashkey == scb_phys)
680 			break;
681 		/* XXX Check to see if it matches the sense command block. */
682 		if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
683 			break;
684 		scb = scb->nexthash;
685 	}
686 	return (scb);
687 }
688 
689 /*
690  * Queue a SCB to be sent to the controller, and send it if possible.
691  */
692 void
693 wds_queue_scb(sc, scb)
694 	struct wds_softc *sc;
695 	struct wds_scb *scb;
696 {
697 
698 	TAILQ_INSERT_TAIL(&sc->sc_waiting_scb, scb, chain);
699 	wds_start_scbs(sc);
700 }
701 
702 /*
703  * Garbage collect mailboxes that are no longer in use.
704  */
705 void
706 wds_collect_mbo(sc)
707 	struct wds_softc *sc;
708 {
709 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
710 #ifdef WDSDIAG
711 	struct wds_scb *scb;
712 #endif
713 
714 	wmbo = wmbx->cmbo;
715 
716 	while (sc->sc_mbofull > 0) {
717 		if (wmbo->cmd != WDS_MBO_FREE)
718 			break;
719 
720 #ifdef WDSDIAG
721 		scb = wds_scb_phys_kv(sc, phystol(wmbo->scb_addr));
722 		scb->flags &= ~SCB_SENDING;
723 #endif
724 
725 		--sc->sc_mbofull;
726 		wds_nextmbx(wmbo, wmbx, mbo);
727 	}
728 
729 	wmbx->cmbo = wmbo;
730 }
731 
732 /*
733  * Send as many SCBs as we have empty mailboxes for.
734  */
735 void
736 wds_start_scbs(sc)
737 	struct wds_softc *sc;
738 {
739 	bus_space_tag_t iot = sc->sc_iot;
740 	bus_space_handle_t ioh = sc->sc_ioh;
741 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
742 	struct wds_scb *scb;
743 	u_char c;
744 
745 	wmbo = wmbx->tmbo;
746 
747 	while ((scb = sc->sc_waiting_scb.tqh_first) != NULL) {
748 		if (sc->sc_mbofull >= WDS_MBX_SIZE) {
749 			wds_collect_mbo(sc);
750 			if (sc->sc_mbofull >= WDS_MBX_SIZE) {
751 				c = WDSC_IRQMFREE;
752 				wds_cmd(iot, ioh, &c, sizeof c);
753 				break;
754 			}
755 		}
756 
757 		TAILQ_REMOVE(&sc->sc_waiting_scb, scb, chain);
758 #ifdef WDSDIAG
759 		scb->flags |= SCB_SENDING;
760 #endif
761 
762 		/* Link scb to mbo. */
763 		ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
764 		    offsetof(struct wds_scb, cmd), wmbo->scb_addr);
765 		/* XXX What about aborts? */
766 		wmbo->cmd = WDS_MBO_START;
767 
768 		/* Tell the card to poll immediately. */
769 		c = WDSC_MSTART(wmbo - wmbx->mbo);
770 		wds_cmd(sc->sc_iot, sc->sc_ioh, &c, sizeof c);
771 
772 		if ((scb->flags & SCB_POLLED) == 0)
773 			callout_reset(&scb->xs->xs_callout,
774 			    (scb->timeout * hz) / 1000, wds_timeout, scb);
775 
776 		++sc->sc_mbofull;
777 		wds_nextmbx(wmbo, wmbx, mbo);
778 	}
779 
780 	wmbx->tmbo = wmbo;
781 }
782 
783 /*
784  * Process the result of a SCSI command.
785  */
786 void
787 wds_done(sc, scb, stat)
788 	struct wds_softc *sc;
789 	struct wds_scb *scb;
790 	u_char stat;
791 {
792 	bus_dma_tag_t dmat = sc->sc_dmat;
793 	struct scsipi_xfer *xs = scb->xs;
794 
795 	/* XXXXX */
796 
797 	/* Don't release the SCB if it was an internal command. */
798 	if (xs == 0) {
799 		scb->flags |= SCB_DONE;
800 		return;
801 	}
802 
803 	/*
804 	 * If we were a data transfer, unload the map that described
805 	 * the data buffer.
806 	 */
807 	if (xs->datalen) {
808 		bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
809 		    scb->dmamap_xfer->dm_mapsize,
810 		    (xs->xs_control & XS_CTL_DATA_IN) ?
811 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
812 		bus_dmamap_unload(dmat, scb->dmamap_xfer);
813 	}
814 	if (xs->error == XS_NOERROR) {
815 		/* If all went well, or an error is acceptable. */
816 		if (stat == WDS_MBI_OK) {
817 			/* OK, set the result */
818 			xs->resid = 0;
819 		} else {
820 			/* Check the mailbox status. */
821 			switch (stat) {
822 			case WDS_MBI_OKERR:
823 				/*
824 				 * SCSI error recorded in scb,
825 				 * counts as WDS_MBI_OK
826 				 */
827 				switch (scb->cmd.venderr) {
828 				case 0x00:
829 					printf("%s: Is this "
830 					    "an error?\n",
831 					    sc->sc_dev.dv_xname);
832 					/* Experiment. */
833 					xs->error = XS_DRIVER_STUFFUP;
834 					break;
835 				case 0x01:
836 #if 0
837 					printf("%s: OK, see SCSI "
838 					    "error field.\n",
839 					    sc->sc_dev.dv_xname);
840 #endif
841 					if (scb->cmd.stat == SCSI_CHECK ||
842 					    scb->cmd.stat == SCSI_BUSY) {
843 						xs->status = scb->cmd.stat;
844 						xs->error = XS_BUSY;
845 					}
846 					break;
847 				case 0x40:
848 #if 0
849 					printf("%s: DMA underrun!\n",
850 					    sc->sc_dev.dv_xname);
851 #endif
852 					/*
853 					 * Hits this if the target
854 					 * returns fewer that datalen
855 					 * bytes (eg my CD-ROM, which
856 					 * returns a short version
857 					 * string, or if DMA is
858 					 * turned off etc.
859 					 */
860 					xs->resid = 0;
861 					break;
862 				default:
863 					printf("%s: VENDOR ERROR "
864 					    "%02x, scsi %02x\n",
865 					    sc->sc_dev.dv_xname,
866 					    scb->cmd.venderr,
867 					    scb->cmd.stat);
868 					/* Experiment. */
869 					xs->error = XS_DRIVER_STUFFUP;
870 					break;
871 				}
872 					break;
873 			case WDS_MBI_ETIME:
874 				/*
875 				 * The documentation isn't clear on
876 				 * what conditions might generate this,
877 				 * but selection timeouts are the only
878 				 * one I can think of.
879 				 */
880 				xs->error = XS_SELTIMEOUT;
881 				break;
882 			case WDS_MBI_ERESET:
883 			case WDS_MBI_ETARCMD:
884 			case WDS_MBI_ERESEL:
885 			case WDS_MBI_ESEL:
886 			case WDS_MBI_EABORT:
887 			case WDS_MBI_ESRESET:
888 			case WDS_MBI_EHRESET:
889 				xs->error = XS_DRIVER_STUFFUP;
890 				break;
891 			}
892 		}
893 	} /* XS_NOERROR */
894 
895 	wds_free_scb(sc, scb);
896 	scsipi_done(xs);
897 }
898 
899 int
900 wds_find(iot, ioh, sc)
901 	bus_space_tag_t iot;
902 	bus_space_handle_t ioh;
903 	struct wds_probe_data *sc;
904 {
905 	int i;
906 
907 	/* XXXXX */
908 
909 	/*
910 	 * Sending a command causes the CMDRDY bit to clear.
911  	 */
912 	for (i = 5; i; i--) {
913 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
914 			break;
915 		delay(100);
916 	}
917 	if (!i)
918 		return 0;
919 
920 	bus_space_write_1(iot, ioh, WDS_CMD, WDSC_NOOP);
921 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
922 		return 0;
923 
924 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
925 	delay(10000);
926 	bus_space_write_1(iot, ioh, WDS_HCR, 0x00);
927 	delay(500000);
928 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
929 	if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 1)
930 		if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 7)
931 			return 0;
932 
933 	for (i = 2000; i; i--) {
934 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
935 			break;
936 		delay(100);
937 	}
938 	if (!i)
939 		return 0;
940 
941 	if (sc) {
942 #ifdef notyet
943 		sc->sc_irq = ...;
944 		sc->sc_drq = ...;
945 #endif
946 		/* XXX Can we do this better? */
947 		sc->sc_scsi_dev = 7;
948 	}
949 
950 	return 1;
951 }
952 
953 /*
954  * Initialise the board and driver.
955  */
956 void
957 wds_init(sc, isreset)
958 	struct wds_softc *sc;
959 	int isreset;
960 {
961 	bus_space_tag_t iot = sc->sc_iot;
962 	bus_space_handle_t ioh = sc->sc_ioh;
963 	bus_dma_segment_t seg;
964 	struct wds_setup init;
965 	u_char c;
966 	int i, rseg;
967 
968 	if (isreset)
969 		goto doinit;
970 
971 	/*
972 	 * Allocate the mailbox.
973 	 */
974 	if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg, 1,
975 	    &rseg, BUS_DMA_NOWAIT) ||
976 	    bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
977 	    (caddr_t *)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
978 		panic("wds_init: can't create or map mailbox");
979 
980 	/*
981 	 * Since DMA memory allocation is always rounded up to a
982 	 * page size, create some scbs from the leftovers.
983 	 */
984 	if (wds_create_scbs(sc, ((caddr_t)wmbx) +
985 	    ALIGN(sizeof(struct wds_mbx)),
986 	    PAGE_SIZE - ALIGN(sizeof(struct wds_mbx))))
987 		panic("wds_init: can't create scbs");
988 
989 	/*
990 	 * Create and load the mailbox DMA map.
991 	 */
992 	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct wds_mbx), 1,
993 	    sizeof(struct wds_mbx), 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_mbox) ||
994 	    bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
995 	    sizeof(struct wds_mbx), NULL, BUS_DMA_NOWAIT))
996 		panic("wds_ionit: can't craete or load mailbox dma map");
997 
998  doinit:
999 	/*
1000 	 * Set up initial mail box for round-robin operation.
1001 	 */
1002 	for (i = 0; i < WDS_MBX_SIZE; i++) {
1003 		wmbx->mbo[i].cmd = WDS_MBO_FREE;
1004 		wmbx->mbi[i].stat = WDS_MBI_FREE;
1005 	}
1006 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1007 	wmbx->tmbi = &wmbx->mbi[0];
1008 	sc->sc_mbofull = 0;
1009 
1010 	init.opcode = WDSC_INIT;
1011 	init.scsi_id = sc->sc_channel.chan_id;
1012 	init.buson_t = 48;
1013 	init.busoff_t = 24;
1014 	init.xx = 0;
1015 	ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, init.mbaddr);
1016 	init.nomb = init.nimb = WDS_MBX_SIZE;
1017 	wds_cmd(iot, ioh, (u_char *)&init, sizeof init);
1018 
1019 	wds_wait(iot, ioh, WDS_STAT, WDSS_INIT, WDSS_INIT);
1020 
1021 	c = WDSC_DISUNSOL;
1022 	wds_cmd(iot, ioh, &c, sizeof c);
1023 }
1024 
1025 /*
1026  * Read the board's firmware revision information.
1027  */
1028 void
1029 wds_inquire_setup_information(sc)
1030 	struct wds_softc *sc;
1031 {
1032 	bus_space_tag_t iot = sc->sc_iot;
1033 	bus_space_handle_t ioh = sc->sc_ioh;
1034 	struct wds_scb *scb;
1035 	u_char *j;
1036 	int s;
1037 
1038 	sc->sc_maxsegs = 1;
1039 
1040 	scb = wds_get_scb(sc);
1041 	if (scb == 0)
1042 		panic("wds_inquire_setup_information: no scb available");
1043 
1044 	scb->xs = NULL;
1045 	scb->timeout = 40;
1046 
1047 	memset(&scb->cmd, 0, sizeof scb->cmd);
1048 	scb->cmd.write = 0x80;
1049 	scb->cmd.opcode = WDSX_GETFIRMREV;
1050 
1051 	/* Will poll card, await result. */
1052 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
1053 	scb->flags |= SCB_POLLED;
1054 
1055 	s = splbio();
1056 	wds_queue_scb(sc, scb);
1057 	splx(s);
1058 
1059 	if (wds_ipoll(sc, scb, scb->timeout))
1060 		goto out;
1061 
1062 	/* Print the version number. */
1063 	printf("%s: version %x.%02x ", sc->sc_dev.dv_xname,
1064 	    scb->cmd.targ, scb->cmd.scb.opcode);
1065 	sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb.opcode;
1066 	/* Print out the version string. */
1067 	j = 2 + &(scb->cmd.targ);
1068 	while ((*j >= 32) && (*j < 128)) {
1069 		printf("%c", *j);
1070 		j++;
1071 	}
1072 
1073 	/*
1074 	 * Determine if we can use scatter/gather.
1075 	 */
1076 	if (sc->sc_revision >= 0x800)
1077 		sc->sc_maxsegs = WDS_NSEG;
1078 
1079 out:
1080 	printf("\n");
1081 
1082 	/*
1083 	 * Free up the resources used by this scb.
1084 	 */
1085 	wds_free_scb(sc, scb);
1086 }
1087 
1088 void
1089 wdsminphys(bp)
1090 	struct buf *bp;
1091 {
1092 
1093 	if (bp->b_bcount > WDS_MAXXFER)
1094 		bp->b_bcount = WDS_MAXXFER;
1095 	minphys(bp);
1096 }
1097 
1098 /*
1099  * Send a SCSI command.
1100  */
1101 void
1102 wds_scsipi_request(chan, req, arg)
1103 	struct scsipi_channel *chan;
1104 	scsipi_adapter_req_t req;
1105 	void *arg;
1106 {
1107 	struct scsipi_xfer *xs;
1108 	struct scsipi_periph *periph;
1109 	struct wds_softc *sc = (void *)chan->chan_adapter->adapt_dev;
1110 	bus_dma_tag_t dmat = sc->sc_dmat;
1111 	struct wds_scb *scb;
1112 	struct wds_scat_gath *sg;
1113 	int error, seg, flags, s;
1114 
1115 	switch (req) {
1116 	case ADAPTER_REQ_RUN_XFER:
1117 		xs = arg;
1118 		periph = xs->xs_periph;
1119 
1120 		if (xs->xs_control & XS_CTL_RESET) {
1121 			/* XXX Fix me! */
1122 			printf("%s: reset!\n", sc->sc_dev.dv_xname);
1123 			wds_init(sc, 1);
1124 			scsipi_done(xs);
1125 			return;
1126 		}
1127 
1128 		if (xs->xs_control & XS_CTL_DATA_UIO) {
1129 			/* XXX Fix me! */
1130 			/*
1131 			 * Let's not worry about UIO. There isn't any code
1132 			 * for the non-SG boards anyway!
1133 			 */
1134 			printf("%s: UIO is untested and disabled!\n",
1135 			    sc->sc_dev.dv_xname);
1136 			xs->error = XS_DRIVER_STUFFUP;
1137 			scsipi_done(xs);
1138 			return;
1139 		}
1140 
1141 		flags = xs->xs_control;
1142 
1143 		/* Get an SCB to use. */
1144 		scb = wds_get_scb(sc);
1145 #ifdef DIAGNOSTIC
1146 		/*
1147 		 * This should never happen as we track the resources
1148 		 * in the mid-layer.
1149 		 */
1150 		if (scb == NULL) {
1151 			scsipi_printaddr(periph);
1152 			printf("unable to allocate scb\n");
1153 			panic("wds_scsipi_request");
1154 		}
1155 #endif
1156 
1157 		scb->xs = xs;
1158 		scb->timeout = xs->timeout;
1159 
1160 		/* Zero out the command structure. */
1161 		memset(&scb->cmd, 0, sizeof scb->cmd);
1162 		memcpy(&scb->cmd.scb, xs->cmd,
1163 		    xs->cmdlen < 12 ? xs->cmdlen : 12);
1164 
1165 		/* Set up some of the command fields. */
1166 		scb->cmd.targ = (periph->periph_target << 5) |
1167 		    periph->periph_lun;
1168 
1169 		/*
1170 		 * NOTE: cmd.write may be OK as 0x40 (disable direction
1171 		 * checking) on boards other than the WD-7000V-ASE. Need
1172 		 * this for the ASE:
1173  		 */
1174 		scb->cmd.write = (xs->xs_control & XS_CTL_DATA_IN) ?
1175 		    0x80 : 0x00;
1176 
1177 		if (xs->datalen) {
1178 			sg = scb->scat_gath;
1179 			seg = 0;
1180 #ifdef TFS
1181 			if (flags & XS_CTL_DATA_UIO) {
1182 				error = bus_dmamap_load_uio(dmat,
1183 				    scb->dmamap_xfer, (struct uio *)xs->data,
1184 				    BUS_DMA_NOWAIT |
1185 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
1186 				     BUS_DMA_WRITE));
1187 			} else
1188 #endif /* TFS */
1189 			{
1190 				error = bus_dmamap_load(dmat,
1191 				    scb->dmamap_xfer, xs->data, xs->datalen,
1192 				    NULL, BUS_DMA_NOWAIT |
1193 				    ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
1194 				     BUS_DMA_WRITE));
1195 			}
1196 
1197 			switch (error) {
1198 			case 0:
1199 				break;
1200 
1201 			case ENOMEM:
1202 			case EAGAIN:
1203 				xs->error = XS_RESOURCE_SHORTAGE;
1204 				goto out_bad;
1205 
1206 			default:
1207 				xs->error = XS_DRIVER_STUFFUP;
1208 				printf("%s: error %d loading DMA map\n",
1209 				    sc->sc_dev.dv_xname, error);
1210  out_bad:
1211 				wds_free_scb(sc, scb);
1212 				scsipi_done(xs);
1213 				return;
1214 			}
1215 
1216 			bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
1217 			    scb->dmamap_xfer->dm_mapsize,
1218 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
1219 			    BUS_DMASYNC_PREWRITE);
1220 
1221 			if (sc->sc_maxsegs > 1) {
1222 				/*
1223 				 * Load the hardware scatter/gather map with the
1224 				 * contents of the DMA map.
1225 				 */
1226 				for (seg = 0;
1227 				     seg < scb->dmamap_xfer->dm_nsegs; seg++) {
1228 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_addr,
1229 					    scb->scat_gath[seg].seg_addr);
1230 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_len,
1231 					    scb->scat_gath[seg].seg_len);
1232 				}
1233 
1234 				/*
1235 				 * Set up for scatter/gather transfer.
1236 				 */
1237 				scb->cmd.opcode = WDSX_SCSISG;
1238 				ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
1239 				    offsetof(struct wds_scb, scat_gath),
1240 				    scb->cmd.data);
1241 				ltophys(scb->dmamap_self->dm_nsegs *
1242 				    sizeof(struct wds_scat_gath), scb->cmd.len);
1243 			} else {
1244 				/*
1245 				 * This board is an ASC or an ASE, and the
1246 				 * transfer has been mapped contig for us.
1247 				 */
1248 				scb->cmd.opcode = WDSX_SCSICMD;
1249 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_addr,
1250 				    scb->cmd.data);
1251 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_len,
1252 				    scb->cmd.len);
1253 			}
1254 		} else {
1255 			scb->cmd.opcode = WDSX_SCSICMD;
1256 			ltophys(0, scb->cmd.data);
1257 			ltophys(0, scb->cmd.len);
1258 		}
1259 
1260 		scb->cmd.stat = 0x00;
1261 		scb->cmd.venderr = 0x00;
1262 		ltophys(0, scb->cmd.link);
1263 
1264 		/* XXX Do we really want to do this? */
1265 		if (flags & XS_CTL_POLL) {
1266 			/* Will poll card, await result. */
1267 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
1268 			    WDS_HCR, WDSH_DRQEN);
1269 			scb->flags |= SCB_POLLED;
1270 		} else {
1271 			/*
1272 			 * Will send command, let interrupt routine
1273 			 * handle result.
1274 			 */
1275 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
1276 			    WDSH_IRQEN | WDSH_DRQEN);
1277 		}
1278 
1279 		s = splbio();
1280 		wds_queue_scb(sc, scb);
1281 		splx(s);
1282 
1283 		if ((flags & XS_CTL_POLL) == 0)
1284 			return;
1285 
1286 		if (wds_poll(sc, xs, scb->timeout)) {
1287 			wds_timeout(scb);
1288 			if (wds_poll(sc, xs, scb->timeout))
1289 				wds_timeout(scb);
1290 		}
1291 		return;
1292 
1293 	case ADAPTER_REQ_GROW_RESOURCES:
1294 		/* XXX Not supported. */
1295 		return;
1296 
1297 	case ADAPTER_REQ_SET_XFER_MODE:
1298 		/* XXX How do we do this? */
1299 		return;
1300 	}
1301 }
1302 
1303 /*
1304  * Poll a particular unit, looking for a particular scb
1305  */
1306 int
1307 wds_poll(sc, xs, count)
1308 	struct wds_softc *sc;
1309 	struct scsipi_xfer *xs;
1310 	int count;
1311 {
1312 	bus_space_tag_t iot = sc->sc_iot;
1313 	bus_space_handle_t ioh = sc->sc_ioh;
1314 
1315 	/* timeouts are in msec, so we loop in 1000 usec cycles */
1316 	while (count) {
1317 		/*
1318 		 * If we had interrupts enabled, would we
1319 		 * have got an interrupt?
1320 		 */
1321 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1322 			wdsintr(sc);
1323 		if (xs->xs_status & XS_STS_DONE)
1324 			return 0;
1325 		delay(1000);	/* only happens in boot so ok */
1326 		count--;
1327 	}
1328 	return 1;
1329 }
1330 
1331 /*
1332  * Poll a particular unit, looking for a particular scb
1333  */
1334 int
1335 wds_ipoll(sc, scb, count)
1336 	struct wds_softc *sc;
1337 	struct wds_scb *scb;
1338 	int count;
1339 {
1340 	bus_space_tag_t iot = sc->sc_iot;
1341 	bus_space_handle_t ioh = sc->sc_ioh;
1342 
1343 	/* timeouts are in msec, so we loop in 1000 usec cycles */
1344 	while (count) {
1345 		/*
1346 		 * If we had interrupts enabled, would we
1347 		 * have got an interrupt?
1348 		 */
1349 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1350 			wdsintr(sc);
1351 		if (scb->flags & SCB_DONE)
1352 			return 0;
1353 		delay(1000);	/* only happens in boot so ok */
1354 		count--;
1355 	}
1356 	return 1;
1357 }
1358 
1359 void
1360 wds_timeout(arg)
1361 	void *arg;
1362 {
1363 	struct wds_scb *scb = arg;
1364 	struct scsipi_xfer *xs = scb->xs;
1365 	struct scsipi_periph *periph = xs->xs_periph;
1366 	struct wds_softc *sc =
1367 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
1368 	int s;
1369 
1370 	scsipi_printaddr(periph);
1371 	printf("timed out");
1372 
1373 	s = splbio();
1374 
1375 #ifdef WDSDIAG
1376 	/*
1377 	 * If The scb's mbx is not free, then the board has gone south?
1378 	 */
1379 	wds_collect_mbo(sc);
1380 	if (scb->flags & SCB_SENDING) {
1381 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1382 		Debugger();
1383 	}
1384 #endif
1385 
1386 	/*
1387 	 * If it has been through before, then
1388 	 * a previous abort has failed, don't
1389 	 * try abort again
1390 	 */
1391 	if (scb->flags & SCB_ABORT) {
1392 		/* abort timed out */
1393 		printf(" AGAIN\n");
1394 		/* XXX Must reset! */
1395 	} else {
1396 		/* abort the operation that has timed out */
1397 		printf("\n");
1398 		scb->xs->error = XS_TIMEOUT;
1399 		scb->timeout = WDS_ABORT_TIMEOUT;
1400 		scb->flags |= SCB_ABORT;
1401 		wds_queue_scb(sc, scb);
1402 	}
1403 
1404 	splx(s);
1405 }
1406