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