xref: /netbsd-src/sys/dev/isa/wds.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: wds.c,v 1.69 2008/04/28 20:23:52 martin 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.69 2008/04/28 20:23:52 martin 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(iot, ioh, port, mask, val)
216 	bus_space_tag_t iot;
217 	bus_space_handle_t ioh;
218 	int port;
219 	int mask, val;
220 {
221 
222 	while ((bus_space_read_1(iot, ioh, port) & mask) != val)
223 		;
224 }
225 
226 /*
227  * Write a command to the board's I/O ports.
228  */
229 int
230 wds_cmd(iot, ioh, ibuf, icnt)
231 	bus_space_tag_t iot;
232 	bus_space_handle_t ioh;
233 	u_char *ibuf;
234 	int icnt;
235 {
236 	u_char c;
237 
238 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
239 
240 	while (icnt--) {
241 		bus_space_write_1(iot, ioh, WDS_CMD, *ibuf++);
242 		wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
243 		c = bus_space_read_1(iot, ioh, WDS_STAT);
244 		if (c & WDSS_REJ)
245 			return 1;
246 	}
247 
248 	return 0;
249 }
250 
251 /*
252  * Check for the presence of a WD7000 SCSI controller.
253  */
254 int
255 wdsprobe(struct device *parent, struct cfdata *match,
256     void *aux)
257 {
258 	struct isa_attach_args *ia = aux;
259 	bus_space_tag_t iot = ia->ia_iot;
260 	bus_space_handle_t ioh;
261 	struct wds_probe_data wpd;
262 	int rv;
263 
264 	if (ia->ia_nio < 1)
265 		return (0);
266 	if (ia->ia_nirq < 1)
267 		return (0);
268 	if (ia->ia_ndrq < 1)
269 		return (0);
270 
271 	if (ISA_DIRECT_CONFIG(ia))
272 		return (0);
273 
274 	/* Disallow wildcarded i/o address. */
275 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
276 		return (0);
277 
278 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WDS_ISA_IOSIZE, 0, &ioh))
279 		return (0);
280 
281 	rv = wds_find(iot, ioh, &wpd);
282 
283 	bus_space_unmap(iot, ioh, WDS_ISA_IOSIZE);
284 
285 	if (rv) {
286 #ifdef notyet
287 		if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
288 		    ia->ia_irq[0].ir_irq != wpd.sc_irq)
289 			return (0);
290 		if (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ &&
291 		    ia->ia_drq[0].ir_drq != wpd.sc_drq)
292 			return (0);
293 
294 		ia->ia_nirq = 1;
295 		ia->ia_irq[0].ir_irq = wpd.sc_irq;
296 
297 		ia->ia_ndrq = 1;
298 		ia->ia_drq[0].ir_drq = wpd.sc_drq;
299 #else
300 		if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
301 			return (0);
302 		if (ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ)
303 			return (0);
304 
305 		ia->ia_nirq = 1;
306 		ia->ia_ndrq = 1;
307 #endif
308 		ia->ia_nio = 1;
309 		ia->ia_io[0].ir_size = WDS_ISA_IOSIZE;
310 
311 		ia->ia_niomem = 0;
312 	}
313 	return (rv);
314 }
315 
316 /*
317  * Attach all available units.
318  */
319 void
320 wdsattach(struct device *parent, struct device *self, void *aux)
321 {
322 	struct isa_attach_args *ia = aux;
323 	struct wds_softc *sc = (void *)self;
324 	bus_space_tag_t iot = ia->ia_iot;
325 	bus_space_handle_t ioh;
326 	struct wds_probe_data wpd;
327 	isa_chipset_tag_t ic = ia->ia_ic;
328 	int error;
329 
330 	printf("\n");
331 
332 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, WDS_ISA_IOSIZE, 0, &ioh)) {
333 		aprint_error_dev(&sc->sc_dev, "can't map i/o space\n");
334 		return;
335 	}
336 
337 	sc->sc_iot = iot;
338 	sc->sc_ioh = ioh;
339 	sc->sc_dmat = ia->ia_dmat;
340 	if (!wds_find(iot, ioh, &wpd)) {
341 		aprint_error_dev(&sc->sc_dev, "wds_find failed\n");
342 		return;
343 	}
344 
345 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
346 #ifdef notyet
347 	if (wpd.sc_drq != -1) {
348 		if ((error = isa_dmacascade(ic, wpd.sc_drq)) != 0) {
349 			aprint_error_dev(&sc->sc_dev, "unable to cascade DRQ, error = %d\n", error);
350 			return;
351 		}
352 	}
353 
354 	sc->sc_ih = isa_intr_establish(ic, wpd.sc_irq, IST_EDGE, IPL_BIO,
355 	    wdsintr, sc);
356 #else
357 	if ((error = isa_dmacascade(ic, ia->ia_drq[0].ir_drq)) != 0) {
358 		aprint_error_dev(&sc->sc_dev, "unable to cascade DRQ, error = %d\n", error);
359 		return;
360 	}
361 
362 	sc->sc_ih = isa_intr_establish(ic, ia->ia_irq[0].ir_irq, IST_EDGE,
363 	    IPL_BIO, wdsintr, sc);
364 #endif
365 	if (sc->sc_ih == NULL) {
366 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt\n");
367 		return;
368 	}
369 
370 	wds_attach(sc, &wpd);
371 }
372 
373 void
374 wds_attach(sc, wpd)
375 	struct wds_softc *sc;
376 	struct wds_probe_data *wpd;
377 {
378 	struct scsipi_adapter *adapt = &sc->sc_adapter;
379 	struct scsipi_channel *chan = &sc->sc_channel;
380 
381 	TAILQ_INIT(&sc->sc_free_scb);
382 	TAILQ_INIT(&sc->sc_waiting_scb);
383 
384 	/*
385 	 * Fill in the scsipi_adapter.
386 	 */
387 	memset(adapt, 0, sizeof(*adapt));
388 	adapt->adapt_dev = &sc->sc_dev;
389 	adapt->adapt_nchannels = 1;
390 	/* adapt_openings initialized below */
391 	adapt->adapt_max_periph = 1;
392 	adapt->adapt_request = wds_scsipi_request;
393 	adapt->adapt_minphys = minphys;
394 
395 	/*
396 	 * Fill in the scsipi_channel.
397 	 */
398 	memset(chan, 0, sizeof(*chan));
399 	chan->chan_adapter = adapt;
400 	chan->chan_bustype = &scsi_bustype;
401 	chan->chan_channel = 0;
402 	chan->chan_ntargets = 8;
403 	chan->chan_nluns = 8;
404 	chan->chan_id = wpd->sc_scsi_dev;
405 
406 	wds_init(sc, 0);
407 	wds_inquire_setup_information(sc);
408 
409 	/* XXX add support for GROW */
410 	adapt->adapt_openings = sc->sc_numscbs;
411 
412 	/*
413 	 * ask the adapter what subunits are present
414 	 */
415 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
416 }
417 
418 integrate void
419 wds_finish_scbs(sc)
420 	struct wds_softc *sc;
421 {
422 	struct wds_mbx_in *wmbi;
423 	struct wds_scb *scb;
424 	int i;
425 
426 	wmbi = wmbx->tmbi;
427 
428 	if (wmbi->stat == WDS_MBI_FREE) {
429 		for (i = 0; i < WDS_MBX_SIZE; i++) {
430 			if (wmbi->stat != WDS_MBI_FREE) {
431 				printf("%s: mbi not in round-robin order\n",
432 				    device_xname(&sc->sc_dev));
433 				goto AGAIN;
434 			}
435 			wds_nextmbx(wmbi, wmbx, mbi);
436 		}
437 #ifdef WDSDIAGnot
438 		printf("%s: mbi interrupt with no full mailboxes\n",
439 		    device_xname(&sc->sc_dev));
440 #endif
441 		return;
442 	}
443 
444 AGAIN:
445 	do {
446 		scb = wds_scb_phys_kv(sc, phystol(wmbi->scb_addr));
447 		if (!scb) {
448 			printf("%s: bad mbi scb pointer; skipping\n",
449 			    device_xname(&sc->sc_dev));
450 			goto next;
451 		}
452 
453 #ifdef WDSDEBUG
454 		if (wds_debug) {
455 			u_char *cp = scb->cmd.xx;
456 			printf("op=%x %x %x %x %x %x\n",
457 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
458 			printf("stat %x for mbi addr = %p, ",
459 			    wmbi->stat, wmbi);
460 			printf("scb addr = %p\n", scb);
461 		}
462 #endif /* WDSDEBUG */
463 
464 		callout_stop(&scb->xs->xs_callout);
465 		wds_done(sc, scb, wmbi->stat);
466 
467 	next:
468 		wmbi->stat = WDS_MBI_FREE;
469 		wds_nextmbx(wmbi, wmbx, mbi);
470 	} while (wmbi->stat != WDS_MBI_FREE);
471 
472 	wmbx->tmbi = wmbi;
473 }
474 
475 /*
476  * Process an interrupt.
477  */
478 int
479 wdsintr(arg)
480 	void *arg;
481 {
482 	struct wds_softc *sc = arg;
483 	bus_space_tag_t iot = sc->sc_iot;
484 	bus_space_handle_t ioh = sc->sc_ioh;
485 	u_char c;
486 
487 	/* Was it really an interrupt from the board? */
488 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ) == 0)
489 		return 0;
490 
491 	/* Get the interrupt status byte. */
492 	c = bus_space_read_1(iot, ioh, WDS_IRQSTAT) & WDSI_MASK;
493 
494 	/* Acknowledge (which resets) the interrupt. */
495 	bus_space_write_1(iot, ioh, WDS_IRQACK, 0x00);
496 
497 	switch (c) {
498 	case WDSI_MSVC:
499 		wds_finish_scbs(sc);
500 		break;
501 
502 	case WDSI_MFREE:
503 		wds_start_scbs(sc);
504 		break;
505 
506 	default:
507 		aprint_error_dev(&sc->sc_dev, "unrecognized interrupt type %02x", c);
508 		break;
509 	}
510 
511 	return 1;
512 }
513 
514 integrate void
515 wds_reset_scb(struct wds_softc *sc, struct wds_scb *scb)
516 {
517 
518 	scb->flags = 0;
519 }
520 
521 /*
522  * Free the command structure, the outgoing mailbox and the data buffer.
523  */
524 void
525 wds_free_scb(sc, scb)
526 	struct wds_softc *sc;
527 	struct wds_scb *scb;
528 {
529 	int s;
530 
531 	s = splbio();
532 	wds_reset_scb(sc, scb);
533 	TAILQ_INSERT_HEAD(&sc->sc_free_scb, scb, chain);
534 	splx(s);
535 }
536 
537 integrate int
538 wds_init_scb(sc, scb)
539 	struct wds_softc *sc;
540 	struct wds_scb *scb;
541 {
542 	bus_dma_tag_t dmat = sc->sc_dmat;
543 	int hashnum, error;
544 
545 	/*
546 	 * XXX Should we put a DIAGNOSTIC check for multiple
547 	 * XXX SCB inits here?
548 	 */
549 
550 	memset(scb, 0, sizeof(struct wds_scb));
551 
552 	/*
553 	 * Create DMA maps for this SCB.
554 	 */
555 	error = bus_dmamap_create(dmat, sizeof(struct wds_scb), 1,
556 	    sizeof(struct wds_scb), 0, BUS_DMA_NOWAIT, &scb->dmamap_self);
557 	if (error) {
558 		aprint_error_dev(&sc->sc_dev, "can't create scb dmamap_self\n");
559 		return (error);
560 	}
561 
562 	error = bus_dmamap_create(dmat, WDS_MAXXFER, WDS_NSEG, WDS_MAXXFER,
563 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &scb->dmamap_xfer);
564 	if (error) {
565 		aprint_error_dev(&sc->sc_dev, "can't create scb dmamap_xfer\n");
566 		bus_dmamap_destroy(dmat, scb->dmamap_self);
567 		return (error);
568 	}
569 
570 	/*
571 	 * Load the permanent DMA maps.
572 	 */
573 	error = bus_dmamap_load(dmat, scb->dmamap_self, scb,
574 	    sizeof(struct wds_scb), NULL, BUS_DMA_NOWAIT);
575 	if (error) {
576 		aprint_error_dev(&sc->sc_dev, "can't load scb dmamap_self\n");
577 		bus_dmamap_destroy(dmat, scb->dmamap_self);
578 		bus_dmamap_destroy(dmat, scb->dmamap_xfer);
579 		return (error);
580 	}
581 
582 	/*
583 	 * put in the phystokv hash table
584 	 * Never gets taken out.
585 	 */
586 	scb->hashkey = scb->dmamap_self->dm_segs[0].ds_addr;
587 	hashnum = SCB_HASH(scb->hashkey);
588 	scb->nexthash = sc->sc_scbhash[hashnum];
589 	sc->sc_scbhash[hashnum] = scb;
590 	wds_reset_scb(sc, scb);
591 	return (0);
592 }
593 
594 /*
595  * Create a set of scbs and add them to the free list.
596  */
597 int
598 wds_create_scbs(sc, mem, size)
599 	struct wds_softc *sc;
600 	void *mem;
601 	size_t size;
602 {
603 	bus_dma_segment_t seg;
604 	struct wds_scb *scb;
605 	int rseg, error;
606 
607 	if (sc->sc_numscbs >= WDS_SCB_MAX)
608 		return (0);
609 
610 	if ((scb = mem) != NULL)
611 		goto have_mem;
612 
613 	size = PAGE_SIZE;
614 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg,
615 	    1, &rseg, BUS_DMA_NOWAIT);
616 	if (error) {
617 		aprint_error_dev(&sc->sc_dev, "can't allocate memory for scbs\n");
618 		return (error);
619 	}
620 
621 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
622 	    (void *)&scb, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
623 	if (error) {
624 		aprint_error_dev(&sc->sc_dev, "can't map memory for scbs\n");
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 			aprint_error_dev(&sc->sc_dev, "can't initialize scb\n");
635 			return (error);
636 		}
637 		TAILQ_INSERT_TAIL(&sc->sc_free_scb, scb, chain);
638 		scb = (struct wds_scb *)((char *)scb +
639 			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 			    mstohz(scb->timeout), 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 					aprint_error_dev(&sc->sc_dev, "Is this "
830 					    "an error?\n");
831 					/* Experiment. */
832 					xs->error = XS_DRIVER_STUFFUP;
833 					break;
834 				case 0x01:
835 #if 0
836 					aprint_error_dev(&sc->sc_dev, "OK, see SCSI "
837 					    "error field.\n");
838 #endif
839 					if (scb->cmd.stat == SCSI_CHECK ||
840 					    scb->cmd.stat == SCSI_BUSY) {
841 						xs->status = scb->cmd.stat;
842 						xs->error = XS_BUSY;
843 					}
844 					break;
845 				case 0x40:
846 #if 0
847 					printf("%s: DMA underrun!\n",
848 					    device_xname(&sc->sc_dev));
849 #endif
850 					/*
851 					 * Hits this if the target
852 					 * returns fewer that datalen
853 					 * bytes (eg my CD-ROM, which
854 					 * returns a short version
855 					 * string, or if DMA is
856 					 * turned off etc.
857 					 */
858 					xs->resid = 0;
859 					break;
860 				default:
861 					printf("%s: VENDOR ERROR "
862 					    "%02x, scsi %02x\n",
863 					    device_xname(&sc->sc_dev),
864 					    scb->cmd.venderr,
865 					    scb->cmd.stat);
866 					/* Experiment. */
867 					xs->error = XS_DRIVER_STUFFUP;
868 					break;
869 				}
870 					break;
871 			case WDS_MBI_ETIME:
872 				/*
873 				 * The documentation isn't clear on
874 				 * what conditions might generate this,
875 				 * but selection timeouts are the only
876 				 * one I can think of.
877 				 */
878 				xs->error = XS_SELTIMEOUT;
879 				break;
880 			case WDS_MBI_ERESET:
881 			case WDS_MBI_ETARCMD:
882 			case WDS_MBI_ERESEL:
883 			case WDS_MBI_ESEL:
884 			case WDS_MBI_EABORT:
885 			case WDS_MBI_ESRESET:
886 			case WDS_MBI_EHRESET:
887 				xs->error = XS_DRIVER_STUFFUP;
888 				break;
889 			}
890 		}
891 	} /* XS_NOERROR */
892 
893 	wds_free_scb(sc, scb);
894 	scsipi_done(xs);
895 }
896 
897 int
898 wds_find(iot, ioh, sc)
899 	bus_space_tag_t iot;
900 	bus_space_handle_t ioh;
901 	struct wds_probe_data *sc;
902 {
903 	int i;
904 
905 	/* XXXXX */
906 
907 	/*
908 	 * Sending a command causes the CMDRDY bit to clear.
909  	 */
910 	for (i = 5; i; i--) {
911 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
912 			break;
913 		delay(100);
914 	}
915 	if (!i)
916 		return 0;
917 
918 	bus_space_write_1(iot, ioh, WDS_CMD, WDSC_NOOP);
919 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
920 		return 0;
921 
922 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
923 	delay(10000);
924 	bus_space_write_1(iot, ioh, WDS_HCR, 0x00);
925 	delay(500000);
926 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
927 	if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 1)
928 		if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 7)
929 			return 0;
930 
931 	for (i = 2000; i; i--) {
932 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
933 			break;
934 		delay(100);
935 	}
936 	if (!i)
937 		return 0;
938 
939 	if (sc) {
940 #ifdef notyet
941 		sc->sc_irq = ...;
942 		sc->sc_drq = ...;
943 #endif
944 		/* XXX Can we do this better? */
945 		sc->sc_scsi_dev = 7;
946 	}
947 
948 	return 1;
949 }
950 
951 /*
952  * Initialise the board and driver.
953  */
954 void
955 wds_init(sc, isreset)
956 	struct wds_softc *sc;
957 	int isreset;
958 {
959 	bus_space_tag_t iot = sc->sc_iot;
960 	bus_space_handle_t ioh = sc->sc_ioh;
961 	bus_dma_segment_t seg;
962 	struct wds_setup init;
963 	u_char c;
964 	int i, rseg;
965 
966 	if (isreset)
967 		goto doinit;
968 
969 	/*
970 	 * Allocate the mailbox.
971 	 */
972 	if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg, 1,
973 	    &rseg, BUS_DMA_NOWAIT) ||
974 	    bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
975 	    (void **)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
976 		panic("wds_init: can't create or map mailbox");
977 
978 	/*
979 	 * Since DMA memory allocation is always rounded up to a
980 	 * page size, create some scbs from the leftovers.
981 	 */
982 	if (wds_create_scbs(sc, ((char *)wmbx) +
983 	    ALIGN(sizeof(struct wds_mbx)),
984 	    PAGE_SIZE - ALIGN(sizeof(struct wds_mbx))))
985 		panic("wds_init: can't create scbs");
986 
987 	/*
988 	 * Create and load the mailbox DMA map.
989 	 */
990 	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct wds_mbx), 1,
991 	    sizeof(struct wds_mbx), 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_mbox) ||
992 	    bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
993 	    sizeof(struct wds_mbx), NULL, BUS_DMA_NOWAIT))
994 		panic("wds_ionit: can't create or load mailbox DMA map");
995 
996  doinit:
997 	/*
998 	 * Set up initial mail box for round-robin operation.
999 	 */
1000 	for (i = 0; i < WDS_MBX_SIZE; i++) {
1001 		wmbx->mbo[i].cmd = WDS_MBO_FREE;
1002 		wmbx->mbi[i].stat = WDS_MBI_FREE;
1003 	}
1004 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1005 	wmbx->tmbi = &wmbx->mbi[0];
1006 	sc->sc_mbofull = 0;
1007 
1008 	init.opcode = WDSC_INIT;
1009 	init.scsi_id = sc->sc_channel.chan_id;
1010 	init.buson_t = 48;
1011 	init.busoff_t = 24;
1012 	init.xx = 0;
1013 	ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, init.mbaddr);
1014 	init.nomb = init.nimb = WDS_MBX_SIZE;
1015 	wds_cmd(iot, ioh, (u_char *)&init, sizeof init);
1016 
1017 	wds_wait(iot, ioh, WDS_STAT, WDSS_INIT, WDSS_INIT);
1018 
1019 	c = WDSC_DISUNSOL;
1020 	wds_cmd(iot, ioh, &c, sizeof c);
1021 }
1022 
1023 /*
1024  * Read the board's firmware revision information.
1025  */
1026 void
1027 wds_inquire_setup_information(sc)
1028 	struct wds_softc *sc;
1029 {
1030 	bus_space_tag_t iot = sc->sc_iot;
1031 	bus_space_handle_t ioh = sc->sc_ioh;
1032 	struct wds_scb *scb;
1033 	u_char *j;
1034 	int s;
1035 
1036 	sc->sc_maxsegs = 1;
1037 
1038 	scb = wds_get_scb(sc);
1039 	if (scb == 0)
1040 		panic("wds_inquire_setup_information: no scb available");
1041 
1042 	scb->xs = NULL;
1043 	scb->timeout = 40;
1044 
1045 	memset(&scb->cmd, 0, sizeof scb->cmd);
1046 	scb->cmd.write = 0x80;
1047 	scb->cmd.opcode = WDSX_GETFIRMREV;
1048 
1049 	/* Will poll card, await result. */
1050 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
1051 	scb->flags |= SCB_POLLED;
1052 
1053 	s = splbio();
1054 	wds_queue_scb(sc, scb);
1055 	splx(s);
1056 
1057 	if (wds_ipoll(sc, scb, scb->timeout))
1058 		goto out;
1059 
1060 	/* Print the version number. */
1061 	printf("%s: version %x.%02x ", device_xname(&sc->sc_dev),
1062 	    scb->cmd.targ, scb->cmd.scb[0]);
1063 	sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb[0];
1064 	/* Print out the version string. */
1065 	j = 2 + &(scb->cmd.targ);
1066 	while ((*j >= 32) && (*j < 128)) {
1067 		printf("%c", *j);
1068 		j++;
1069 	}
1070 
1071 	/*
1072 	 * Determine if we can use scatter/gather.
1073 	 */
1074 	if (sc->sc_revision >= 0x800)
1075 		sc->sc_maxsegs = WDS_NSEG;
1076 
1077 out:
1078 	printf("\n");
1079 
1080 	/*
1081 	 * Free up the resources used by this scb.
1082 	 */
1083 	wds_free_scb(sc, scb);
1084 }
1085 
1086 void
1087 wdsminphys(bp)
1088 	struct buf *bp;
1089 {
1090 
1091 	if (bp->b_bcount > WDS_MAXXFER)
1092 		bp->b_bcount = WDS_MAXXFER;
1093 	minphys(bp);
1094 }
1095 
1096 /*
1097  * Send a SCSI command.
1098  */
1099 void
1100 wds_scsipi_request(chan, req, arg)
1101 	struct scsipi_channel *chan;
1102 	scsipi_adapter_req_t req;
1103 	void *arg;
1104 {
1105 	struct scsipi_xfer *xs;
1106 	struct scsipi_periph *periph;
1107 	struct wds_softc *sc = (void *)chan->chan_adapter->adapt_dev;
1108 	bus_dma_tag_t dmat = sc->sc_dmat;
1109 	struct wds_scb *scb;
1110 	int error, seg, flags, s;
1111 
1112 	switch (req) {
1113 	case ADAPTER_REQ_RUN_XFER:
1114 		xs = arg;
1115 		periph = xs->xs_periph;
1116 
1117 		if (xs->xs_control & XS_CTL_RESET) {
1118 			/* XXX Fix me! */
1119 			printf("%s: reset!\n", device_xname(&sc->sc_dev));
1120 			wds_init(sc, 1);
1121 			scsipi_done(xs);
1122 			return;
1123 		}
1124 
1125 		if (xs->xs_control & XS_CTL_DATA_UIO) {
1126 			/* XXX Fix me! */
1127 			/*
1128 			 * Let's not worry about UIO. There isn't any code
1129 			 * for the non-SG boards anyway!
1130 			 */
1131 			aprint_error_dev(&sc->sc_dev, "UIO is untested and disabled!\n");
1132 			xs->error = XS_DRIVER_STUFFUP;
1133 			scsipi_done(xs);
1134 			return;
1135 		}
1136 
1137 		flags = xs->xs_control;
1138 
1139 		/* Get an SCB to use. */
1140 		scb = wds_get_scb(sc);
1141 #ifdef DIAGNOSTIC
1142 		/*
1143 		 * This should never happen as we track the resources
1144 		 * in the mid-layer.
1145 		 */
1146 		if (scb == NULL) {
1147 			scsipi_printaddr(periph);
1148 			printf("unable to allocate scb\n");
1149 			panic("wds_scsipi_request");
1150 		}
1151 #endif
1152 
1153 		scb->xs = xs;
1154 		scb->timeout = xs->timeout;
1155 
1156 		/* Zero out the command structure. */
1157 		if (xs->cmdlen > sizeof(scb->cmd.scb)) {
1158 			aprint_error_dev(&sc->sc_dev, "cmdlen %d too large for SCB\n",
1159 			    xs->cmdlen);
1160 			xs->error = XS_DRIVER_STUFFUP;
1161 			goto out_bad;
1162 		}
1163 		memset(&scb->cmd, 0, sizeof scb->cmd);
1164 		memcpy(&scb->cmd.scb, xs->cmd, xs->cmdlen);
1165 
1166 		/* Set up some of the command fields. */
1167 		scb->cmd.targ = (periph->periph_target << 5) |
1168 		    periph->periph_lun;
1169 
1170 		/*
1171 		 * NOTE: cmd.write may be OK as 0x40 (disable direction
1172 		 * checking) on boards other than the WD-7000V-ASE. Need
1173 		 * this for the ASE:
1174  		 */
1175 		scb->cmd.write = (xs->xs_control & XS_CTL_DATA_IN) ?
1176 		    0x80 : 0x00;
1177 
1178 		if (xs->datalen) {
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 				aprint_error_dev(&sc->sc_dev, "error %d loading DMA map\n", error);
1209  out_bad:
1210 				wds_free_scb(sc, scb);
1211 				scsipi_done(xs);
1212 				return;
1213 			}
1214 
1215 			bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
1216 			    scb->dmamap_xfer->dm_mapsize,
1217 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
1218 			    BUS_DMASYNC_PREWRITE);
1219 
1220 			if (sc->sc_maxsegs > 1) {
1221 				/*
1222 				 * Load the hardware scatter/gather map with the
1223 				 * contents of the DMA map.
1224 				 */
1225 				for (seg = 0;
1226 				     seg < scb->dmamap_xfer->dm_nsegs; seg++) {
1227 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_addr,
1228 					    scb->scat_gath[seg].seg_addr);
1229 				ltophys(scb->dmamap_xfer->dm_segs[seg].ds_len,
1230 					    scb->scat_gath[seg].seg_len);
1231 				}
1232 
1233 				/*
1234 				 * Set up for scatter/gather transfer.
1235 				 */
1236 				scb->cmd.opcode = WDSX_SCSISG;
1237 				ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
1238 				    offsetof(struct wds_scb, scat_gath),
1239 				    scb->cmd.data);
1240 				ltophys(scb->dmamap_self->dm_nsegs *
1241 				    sizeof(struct wds_scat_gath), scb->cmd.len);
1242 			} else {
1243 				/*
1244 				 * This board is an ASC or an ASE, and the
1245 				 * transfer has been mapped contig for us.
1246 				 */
1247 				scb->cmd.opcode = WDSX_SCSICMD;
1248 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_addr,
1249 				    scb->cmd.data);
1250 				ltophys(scb->dmamap_xfer->dm_segs[0].ds_len,
1251 				    scb->cmd.len);
1252 			}
1253 		} else {
1254 			scb->cmd.opcode = WDSX_SCSICMD;
1255 			ltophys(0, scb->cmd.data);
1256 			ltophys(0, scb->cmd.len);
1257 		}
1258 
1259 		scb->cmd.stat = 0x00;
1260 		scb->cmd.venderr = 0x00;
1261 		ltophys(0, scb->cmd.link);
1262 
1263 		/* XXX Do we really want to do this? */
1264 		if (flags & XS_CTL_POLL) {
1265 			/* Will poll card, await result. */
1266 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
1267 			    WDS_HCR, WDSH_DRQEN);
1268 			scb->flags |= SCB_POLLED;
1269 		} else {
1270 			/*
1271 			 * Will send command, let interrupt routine
1272 			 * handle result.
1273 			 */
1274 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
1275 			    WDSH_IRQEN | WDSH_DRQEN);
1276 		}
1277 
1278 		s = splbio();
1279 		wds_queue_scb(sc, scb);
1280 		splx(s);
1281 
1282 		if ((flags & XS_CTL_POLL) == 0)
1283 			return;
1284 
1285 		if (wds_poll(sc, xs, scb->timeout)) {
1286 			wds_timeout(scb);
1287 			if (wds_poll(sc, xs, scb->timeout))
1288 				wds_timeout(scb);
1289 		}
1290 		return;
1291 
1292 	case ADAPTER_REQ_GROW_RESOURCES:
1293 		/* XXX Not supported. */
1294 		return;
1295 
1296 	case ADAPTER_REQ_SET_XFER_MODE:
1297 		/* XXX How do we do this? */
1298 		return;
1299 	}
1300 }
1301 
1302 /*
1303  * Poll a particular unit, looking for a particular scb
1304  */
1305 int
1306 wds_poll(sc, xs, count)
1307 	struct wds_softc *sc;
1308 	struct scsipi_xfer *xs;
1309 	int count;
1310 {
1311 	bus_space_tag_t iot = sc->sc_iot;
1312 	bus_space_handle_t ioh = sc->sc_ioh;
1313 
1314 	/* timeouts are in msec, so we loop in 1000 usec cycles */
1315 	while (count) {
1316 		/*
1317 		 * If we had interrupts enabled, would we
1318 		 * have got an interrupt?
1319 		 */
1320 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1321 			wdsintr(sc);
1322 		if (xs->xs_status & XS_STS_DONE)
1323 			return 0;
1324 		delay(1000);	/* only happens in boot so ok */
1325 		count--;
1326 	}
1327 	return 1;
1328 }
1329 
1330 /*
1331  * Poll a particular unit, looking for a particular scb
1332  */
1333 int
1334 wds_ipoll(sc, scb, count)
1335 	struct wds_softc *sc;
1336 	struct wds_scb *scb;
1337 	int count;
1338 {
1339 	bus_space_tag_t iot = sc->sc_iot;
1340 	bus_space_handle_t ioh = sc->sc_ioh;
1341 
1342 	/* timeouts are in msec, so we loop in 1000 usec cycles */
1343 	while (count) {
1344 		/*
1345 		 * If we had interrupts enabled, would we
1346 		 * have got an interrupt?
1347 		 */
1348 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1349 			wdsintr(sc);
1350 		if (scb->flags & SCB_DONE)
1351 			return 0;
1352 		delay(1000);	/* only happens in boot so ok */
1353 		count--;
1354 	}
1355 	return 1;
1356 }
1357 
1358 void
1359 wds_timeout(arg)
1360 	void *arg;
1361 {
1362 	struct wds_scb *scb = arg;
1363 	struct scsipi_xfer *xs = scb->xs;
1364 	struct scsipi_periph *periph = xs->xs_periph;
1365 	struct wds_softc *sc =
1366 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
1367 	int s;
1368 
1369 	scsipi_printaddr(periph);
1370 	printf("timed out");
1371 
1372 	s = splbio();
1373 
1374 #ifdef WDSDIAG
1375 	/*
1376 	 * If The scb's mbx is not free, then the board has gone south?
1377 	 */
1378 	wds_collect_mbo(sc);
1379 	if (scb->flags & SCB_SENDING) {
1380 		aprint_error_dev(&sc->sc_dev, "not taking commands!\n");
1381 		Debugger();
1382 	}
1383 #endif
1384 
1385 	/*
1386 	 * If it has been through before, then
1387 	 * a previous abort has failed, don't
1388 	 * try abort again
1389 	 */
1390 	if (scb->flags & SCB_ABORT) {
1391 		/* abort timed out */
1392 		printf(" AGAIN\n");
1393 		/* XXX Must reset! */
1394 	} else {
1395 		/* abort the operation that has timed out */
1396 		printf("\n");
1397 		scb->xs->error = XS_TIMEOUT;
1398 		scb->timeout = WDS_ABORT_TIMEOUT;
1399 		scb->flags |= SCB_ABORT;
1400 		wds_queue_scb(sc, scb);
1401 	}
1402 
1403 	splx(s);
1404 }
1405