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