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