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