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