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