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