wds.c revision 1.30 1 /* $NetBSD: wds.c,v 1.30 1998/02/04 05:14:36 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 #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, 0,
895 scb->dmamap_xfer->dm_mapsize,
896 (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
897 BUS_DMASYNC_POSTWRITE);
898 bus_dmamap_unload(dmat, scb->dmamap_xfer);
899 }
900 if (xs->error == XS_NOERROR) {
901 /* If all went well, or an error is acceptable. */
902 if (stat == WDS_MBI_OK) {
903 /* OK, set the result */
904 xs->resid = 0;
905 } else {
906 /* Check the mailbox status. */
907 switch (stat) {
908 case WDS_MBI_OKERR:
909 /*
910 * SCSI error recorded in scb,
911 * counts as WDS_MBI_OK
912 */
913 switch (scb->cmd.venderr) {
914 case 0x00:
915 printf("%s: Is this "
916 "an error?\n",
917 sc->sc_dev.dv_xname);
918 /* Experiment. */
919 xs->error = XS_DRIVER_STUFFUP;
920 break;
921 case 0x01:
922 #if 0
923 printf("%s: OK, see SCSI "
924 "error field.\n",
925 sc->sc_dev.dv_xname);
926 #endif
927 if (scb->cmd.stat ==
928 SCSI_CHECK) {
929 /* Do sense. */
930 wds_sense(sc, scb);
931 return;
932 } else if (scb->cmd.stat ==
933 SCSI_BUSY) {
934 xs->error = XS_BUSY;
935 }
936 break;
937 case 0x40:
938 #if 0
939 printf("%s: DMA underrun!\n",
940 sc->sc_dev.dv_xname);
941 #endif
942 /*
943 * Hits this if the target
944 * returns fewer that datalen
945 * bytes (eg my CD-ROM, which
946 * returns a short version
947 * string, or if DMA is
948 * turned off etc.
949 */
950 xs->resid = 0;
951 break;
952 default:
953 printf("%s: VENDOR ERROR "
954 "%02x, scsi %02x\n",
955 sc->sc_dev.dv_xname,
956 scb->cmd.venderr,
957 scb->cmd.stat);
958 /* Experiment. */
959 xs->error = XS_DRIVER_STUFFUP;
960 break;
961 }
962 break;
963 case WDS_MBI_ETIME:
964 /*
965 * The documentation isn't clear on
966 * what conditions might generate this,
967 * but selection timeouts are the only
968 * one I can think of.
969 */
970 xs->error = XS_SELTIMEOUT;
971 break;
972 case WDS_MBI_ERESET:
973 case WDS_MBI_ETARCMD:
974 case WDS_MBI_ERESEL:
975 case WDS_MBI_ESEL:
976 case WDS_MBI_EABORT:
977 case WDS_MBI_ESRESET:
978 case WDS_MBI_EHRESET:
979 xs->error = XS_DRIVER_STUFFUP;
980 break;
981 }
982 }
983 } /* else sense */
984 } /* XS_NOERROR */
985
986 wds_free_scb(sc, scb);
987 xs->flags |= ITSDONE;
988 scsipi_done(xs);
989
990 /*
991 * If there are queue entries in the software queue, try to
992 * run the first one. We should be more or less guaranteed
993 * to succeed, since we just freed a CCB.
994 *
995 * NOTE: wds_scsi_cmd() relies on our calling it with
996 * the first entry in the queue.
997 */
998 if ((xs = sc->sc_queue.lh_first) != NULL)
999 (void) wds_scsi_cmd(xs);
1000 }
1001
1002 int
1003 wds_find(iot, ioh, sc)
1004 bus_space_tag_t iot;
1005 bus_space_handle_t ioh;
1006 struct wds_probe_data *sc;
1007 {
1008 int i;
1009
1010 /* XXXXX */
1011
1012 /*
1013 * Sending a command causes the CMDRDY bit to clear.
1014 */
1015 for (i = 5; i; i--) {
1016 if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
1017 break;
1018 delay(100);
1019 }
1020 if (!i)
1021 return 0;
1022
1023 bus_space_write_1(iot, ioh, WDS_CMD, WDSC_NOOP);
1024 if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
1025 return 0;
1026
1027 bus_space_write_1(iot, ioh, WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
1028 delay(10000);
1029 bus_space_write_1(iot, ioh, WDS_HCR, 0x00);
1030 delay(500000);
1031 wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
1032 if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 1)
1033 if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 7)
1034 return 0;
1035
1036 for (i = 2000; i; i--) {
1037 if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
1038 break;
1039 delay(100);
1040 }
1041 if (!i)
1042 return 0;
1043
1044 if (sc) {
1045 #ifdef notyet
1046 sc->sc_irq = ...;
1047 sc->sc_drq = ...;
1048 #endif
1049 /* XXX Can we do this better? */
1050 sc->sc_scsi_dev = 7;
1051 }
1052
1053 return 1;
1054 }
1055
1056 /*
1057 * Initialise the board and driver.
1058 */
1059 void
1060 wds_init(sc, isreset)
1061 struct wds_softc *sc;
1062 int isreset;
1063 {
1064 bus_space_tag_t iot = sc->sc_iot;
1065 bus_space_handle_t ioh = sc->sc_ioh;
1066 bus_dma_segment_t seg;
1067 struct wds_setup init;
1068 u_char c;
1069 int i, rseg;
1070
1071 if (isreset)
1072 goto doinit;
1073
1074 /*
1075 * Allocate the mailbox.
1076 */
1077 if (bus_dmamem_alloc(sc->sc_dmat, NBPG, NBPG, 0, &seg, 1,
1078 &rseg, BUS_DMA_NOWAIT) ||
1079 bus_dmamem_map(sc->sc_dmat, &seg, rseg, NBPG,
1080 (caddr_t *)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
1081 panic("wds_init: can't create or map mailbox");
1082
1083 /*
1084 * Since DMA memory allocation is always rounded up to a
1085 * page size, create some scbs from the leftovers.
1086 */
1087 if (wds_create_scbs(sc, ((caddr_t)wmbx) +
1088 ALIGN(sizeof(struct wds_mbx)),
1089 NBPG - ALIGN(sizeof(struct wds_mbx))))
1090 panic("wds_init: can't create scbs");
1091
1092 /*
1093 * Create and load the mailbox DMA map.
1094 */
1095 if (bus_dmamap_create(sc->sc_dmat, sizeof(struct wds_mbx), 1,
1096 sizeof(struct wds_mbx), 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_mbox) ||
1097 bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
1098 sizeof(struct wds_mbx), NULL, BUS_DMA_NOWAIT))
1099 panic("wds_ionit: can't craete or load mailbox dma map");
1100
1101 doinit:
1102 /*
1103 * Set up initial mail box for round-robin operation.
1104 */
1105 for (i = 0; i < WDS_MBX_SIZE; i++) {
1106 wmbx->mbo[i].cmd = WDS_MBO_FREE;
1107 wmbx->mbi[i].stat = WDS_MBI_FREE;
1108 }
1109 wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1110 wmbx->tmbi = &wmbx->mbi[0];
1111 sc->sc_mbofull = 0;
1112
1113 init.opcode = WDSC_INIT;
1114 init.scsi_id = sc->sc_link.scsipi_scsi.adapter_target;
1115 init.buson_t = 48;
1116 init.busoff_t = 24;
1117 init.xx = 0;
1118 ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, init.mbaddr);
1119 init.nomb = init.nimb = WDS_MBX_SIZE;
1120 wds_cmd(iot, ioh, (u_char *)&init, sizeof init);
1121
1122 wds_wait(iot, ioh, WDS_STAT, WDSS_INIT, WDSS_INIT);
1123
1124 c = WDSC_DISUNSOL;
1125 wds_cmd(iot, ioh, &c, sizeof c);
1126 }
1127
1128 /*
1129 * Read the board's firmware revision information.
1130 */
1131 void
1132 wds_inquire_setup_information(sc)
1133 struct wds_softc *sc;
1134 {
1135 bus_space_tag_t iot = sc->sc_iot;
1136 bus_space_handle_t ioh = sc->sc_ioh;
1137 struct wds_scb *scb;
1138 u_char *j;
1139 int s;
1140
1141 sc->sc_maxsegs = 1;
1142
1143 scb = wds_get_scb(sc, SCSI_NOSLEEP);
1144 if (scb == 0)
1145 panic("wds_inquire_setup_information: no scb available");
1146
1147 scb->xs = NULL;
1148 scb->timeout = 40;
1149
1150 bzero(&scb->cmd, sizeof scb->cmd);
1151 scb->cmd.write = 0x80;
1152 scb->cmd.opcode = WDSX_GETFIRMREV;
1153
1154 /* Will poll card, await result. */
1155 bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
1156 scb->flags |= SCB_POLLED;
1157
1158 s = splbio();
1159 wds_queue_scb(sc, scb);
1160 splx(s);
1161
1162 if (wds_ipoll(sc, scb, scb->timeout))
1163 goto out;
1164
1165 /* Print the version number. */
1166 printf("%s: version %x.%02x ", sc->sc_dev.dv_xname,
1167 scb->cmd.targ, scb->cmd.scb.opcode);
1168 sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb.opcode;
1169 /* Print out the version string. */
1170 j = 2 + &(scb->cmd.targ);
1171 while ((*j >= 32) && (*j < 128)) {
1172 printf("%c", *j);
1173 j++;
1174 }
1175
1176 /*
1177 * Determine if we can use scatter/gather.
1178 */
1179 if (sc->sc_revision >= 0x800)
1180 sc->sc_maxsegs = WDS_NSEG;
1181
1182 out:
1183 printf("\n");
1184
1185 /*
1186 * Free up the resources used by this scb.
1187 */
1188 wds_free_scb(sc, scb);
1189 }
1190
1191 void
1192 wdsminphys(bp)
1193 struct buf *bp;
1194 {
1195
1196 if (bp->b_bcount > WDS_MAXXFER)
1197 bp->b_bcount = WDS_MAXXFER;
1198 minphys(bp);
1199 }
1200
1201 /*
1202 * Send a SCSI command.
1203 */
1204 int
1205 wds_scsi_cmd(xs)
1206 struct scsipi_xfer *xs;
1207 {
1208 struct scsipi_link *sc_link = xs->sc_link;
1209 struct wds_softc *sc = sc_link->adapter_softc;
1210 bus_dma_tag_t dmat = sc->sc_dmat;
1211 struct wds_scb *scb;
1212 struct wds_scat_gath *sg;
1213 int error, seg, flags, s;
1214 int fromqueue = 0, dontqueue = 0;
1215 #ifdef TFS
1216 struct iovec *iovp;
1217 #endif
1218
1219 if (xs->flags & SCSI_RESET) {
1220 /* XXX Fix me! */
1221 printf("%s: reset!\n", sc->sc_dev.dv_xname);
1222 wds_init(sc, 1);
1223 return COMPLETE;
1224 }
1225
1226 s = splbio(); /* protect the queue */
1227
1228 /*
1229 * If we're running the queue from wds_done(), we've been
1230 * called with the first queue entry as our argument.
1231 */
1232 if (xs == sc->sc_queue.lh_first) {
1233 xs = wds_dequeue(sc);
1234 fromqueue = 1;
1235 goto get_scb;
1236 }
1237
1238 /* Polled requests can't be queued for later. */
1239 dontqueue = xs->flags & SCSI_POLL;
1240
1241 /*
1242 * If there are jobs in the queue, run them first.
1243 */
1244 if (sc->sc_queue.lh_first != NULL) {
1245 /*
1246 * If we can't queue, we have to abort, since
1247 * we have to preserve order.
1248 */
1249 if (dontqueue) {
1250 splx(s);
1251 xs->error = XS_DRIVER_STUFFUP;
1252 return (TRY_AGAIN_LATER);
1253 }
1254
1255 /*
1256 * Swap with the first queue entry.
1257 */
1258 wds_enqueue(sc, xs, 0);
1259 xs = wds_dequeue(sc);
1260 fromqueue = 1;
1261 }
1262
1263 get_scb:
1264 flags = xs->flags;
1265 if ((scb = wds_get_scb(sc, flags)) == NULL) {
1266 /*
1267 * If we can't queue, we lose.
1268 */
1269 if (dontqueue) {
1270 splx(s);
1271 xs->error = XS_DRIVER_STUFFUP;
1272 return (TRY_AGAIN_LATER);
1273 }
1274
1275 /*
1276 * Stuff ourselves into the queue, in front
1277 * if we came off in the first place.
1278 */
1279 wds_enqueue(sc, xs, fromqueue);
1280 splx(s);
1281 return (SUCCESSFULLY_QUEUED);
1282 }
1283
1284 splx(s); /* done playing with the queue */
1285
1286 scb->xs = xs;
1287 scb->timeout = xs->timeout;
1288
1289 if (xs->flags & SCSI_DATA_UIO) {
1290 /* XXX Fix me! */
1291 /* Let's not worry about UIO. There isn't any code for the *
1292 * non-SG boards anyway! */
1293 printf("%s: UIO is untested and disabled!\n",
1294 sc->sc_dev.dv_xname);
1295 goto bad;
1296 }
1297
1298 /* Zero out the command structure. */
1299 bzero(&scb->cmd, sizeof scb->cmd);
1300 bcopy(xs->cmd, &scb->cmd.scb, xs->cmdlen < 12 ? xs->cmdlen : 12);
1301
1302 /* Set up some of the command fields. */
1303 scb->cmd.targ = (xs->sc_link->scsipi_scsi.target << 5) |
1304 xs->sc_link->scsipi_scsi.lun;
1305
1306 /* NOTE: cmd.write may be OK as 0x40 (disable direction checking)
1307 * on boards other than the WD-7000V-ASE. Need this for the ASE:
1308 */
1309 scb->cmd.write = (xs->flags & SCSI_DATA_IN) ? 0x80 : 0x00;
1310
1311 if (xs->datalen) {
1312 sg = scb->scat_gath;
1313 seg = 0;
1314 #ifdef TFS
1315 if (flags & SCSI_DATA_UIO) {
1316 error = bus_Dmamap_load_uio(dmat,
1317 scb->dmamap_xfer, (struct uio *)xs->data,
1318 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1319 BUS_DMA_WAITOK);
1320 } else
1321 #endif /* TFS */
1322 {
1323 error = bus_dmamap_load(dmat,
1324 scb->dmamap_xfer, xs->data, xs->datalen, NULL,
1325 (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1326 BUS_DMA_WAITOK);
1327 }
1328
1329 if (error) {
1330 if (error == EFBIG) {
1331 printf("%s: wds_scsi_cmd, more than %d"
1332 " dma segments\n",
1333 sc->sc_dev.dv_xname, sc->sc_maxsegs);
1334 } else {
1335 printf("%s: wds_scsi_cmd, error %d loading"
1336 " dma map\n",
1337 sc->sc_dev.dv_xname, error);
1338 }
1339 goto bad;
1340 }
1341
1342 bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
1343 scb->dmamap_xfer->dm_mapsize,
1344 (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
1345 BUS_DMASYNC_PREWRITE);
1346
1347 if (sc->sc_maxsegs > 1) {
1348 /*
1349 * Load the hardware scatter/gather map with the
1350 * contents of the DMA map.
1351 */
1352 for (seg = 0; seg < scb->dmamap_xfer->dm_nsegs;
1353 seg++) {
1354 ltophys(scb->dmamap_xfer->dm_segs[seg].ds_addr,
1355 scb->scat_gath[seg].seg_addr);
1356 ltophys(scb->dmamap_xfer->dm_segs[seg].ds_len,
1357 scb->scat_gath[seg].seg_len);
1358 }
1359
1360 /*
1361 * Set up for scatter/gather transfer.
1362 */
1363 scb->cmd.opcode = WDSX_SCSISG;
1364 ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
1365 offsetof(struct wds_scb, scat_gath),
1366 scb->cmd.data);
1367 ltophys(scb->dmamap_self->dm_nsegs *
1368 sizeof(struct wds_scat_gath), scb->cmd.len);
1369 } else {
1370 /*
1371 * This board is an ASC or an ASE, and the
1372 * transfer has been mapped contig for us.
1373 */
1374 scb->cmd.opcode = WDSX_SCSICMD;
1375 ltophys(scb->dmamap_xfer->dm_segs[0].ds_addr,
1376 scb->cmd.data);
1377 ltophys(scb->dmamap_xfer->dm_segs[0].ds_len,
1378 scb->cmd.len);
1379 }
1380 } else {
1381 scb->cmd.opcode = WDSX_SCSICMD;
1382 ltophys(0, scb->cmd.data);
1383 ltophys(0, scb->cmd.len);
1384 }
1385
1386 scb->cmd.stat = 0x00;
1387 scb->cmd.venderr = 0x00;
1388 ltophys(0, scb->cmd.link);
1389
1390 /* XXX Do we really want to do this? */
1391 if (flags & SCSI_POLL) {
1392 /* Will poll card, await result. */
1393 bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR, WDSH_DRQEN);
1394 scb->flags |= SCB_POLLED;
1395 } else {
1396 /* Will send command, let interrupt routine handle result. */
1397 bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
1398 WDSH_IRQEN | WDSH_DRQEN);
1399 }
1400
1401 s = splbio();
1402 wds_queue_scb(sc, scb);
1403 splx(s);
1404
1405 if ((flags & SCSI_POLL) == 0)
1406 return SUCCESSFULLY_QUEUED;
1407
1408 if (wds_poll(sc, xs, scb->timeout)) {
1409 wds_timeout(scb);
1410 if (wds_poll(sc, xs, scb->timeout))
1411 wds_timeout(scb);
1412 }
1413 return COMPLETE;
1414
1415 bad:
1416 xs->error = XS_DRIVER_STUFFUP;
1417 wds_free_scb(sc, scb);
1418 return COMPLETE;
1419 }
1420
1421 /*
1422 * Send a sense request.
1423 */
1424 void
1425 wds_sense(sc, scb)
1426 struct wds_softc *sc;
1427 struct wds_scb *scb;
1428 {
1429 struct scsipi_xfer *xs = scb->xs;
1430 struct scsipi_sense *ss = (void *)&scb->sense.scb;
1431 int s;
1432
1433 /* XXXXX */
1434
1435 /* Send sense request SCSI command. */
1436 xs->error = XS_SENSE;
1437 scb->flags |= SCB_SENSE;
1438
1439 /* Next, setup a request sense command block */
1440 bzero(ss, sizeof(*ss));
1441 ss->opcode = REQUEST_SENSE;
1442 ss->byte2 = xs->sc_link->scsipi_scsi.lun << 5;
1443 ss->length = sizeof(struct scsipi_sense_data);
1444
1445 /* Set up some of the command fields. */
1446 scb->sense.targ = scb->cmd.targ;
1447 scb->sense.write = 0x80;
1448 scb->sense.opcode = WDSX_SCSICMD;
1449 ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
1450 offsetof(struct wds_scb, sense_data), scb->sense.data);
1451 ltophys(sizeof(struct scsipi_sense_data), scb->sense.len);
1452
1453 s = splbio();
1454 wds_queue_scb(sc, scb);
1455 splx(s);
1456
1457 /*
1458 * There's no reason for us to poll here. There are two cases:
1459 * 1) If it's a polling operation, then we're called from the interrupt
1460 * handler, and we return and continue polling.
1461 * 2) If it's an interrupt-driven operation, then it gets completed
1462 * later on when the REQUEST SENSE finishes.
1463 */
1464 }
1465
1466 /*
1467 * Poll a particular unit, looking for a particular scb
1468 */
1469 int
1470 wds_poll(sc, xs, count)
1471 struct wds_softc *sc;
1472 struct scsipi_xfer *xs;
1473 int count;
1474 {
1475 bus_space_tag_t iot = sc->sc_iot;
1476 bus_space_handle_t ioh = sc->sc_ioh;
1477
1478 /* timeouts are in msec, so we loop in 1000 usec cycles */
1479 while (count) {
1480 /*
1481 * If we had interrupts enabled, would we
1482 * have got an interrupt?
1483 */
1484 if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1485 wdsintr(sc);
1486 if (xs->flags & ITSDONE)
1487 return 0;
1488 delay(1000); /* only happens in boot so ok */
1489 count--;
1490 }
1491 return 1;
1492 }
1493
1494 /*
1495 * Poll a particular unit, looking for a particular scb
1496 */
1497 int
1498 wds_ipoll(sc, scb, count)
1499 struct wds_softc *sc;
1500 struct wds_scb *scb;
1501 int count;
1502 {
1503 bus_space_tag_t iot = sc->sc_iot;
1504 bus_space_handle_t ioh = sc->sc_ioh;
1505
1506 /* timeouts are in msec, so we loop in 1000 usec cycles */
1507 while (count) {
1508 /*
1509 * If we had interrupts enabled, would we
1510 * have got an interrupt?
1511 */
1512 if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
1513 wdsintr(sc);
1514 if (scb->flags & SCB_DONE)
1515 return 0;
1516 delay(1000); /* only happens in boot so ok */
1517 count--;
1518 }
1519 return 1;
1520 }
1521
1522 void
1523 wds_timeout(arg)
1524 void *arg;
1525 {
1526 struct wds_scb *scb = arg;
1527 struct scsipi_xfer *xs = scb->xs;
1528 struct scsipi_link *sc_link = xs->sc_link;
1529 struct wds_softc *sc = sc_link->adapter_softc;
1530 int s;
1531
1532 scsi_print_addr(sc_link);
1533 printf("timed out");
1534
1535 s = splbio();
1536
1537 #ifdef WDSDIAG
1538 /*
1539 * If The scb's mbx is not free, then the board has gone south?
1540 */
1541 wds_collect_mbo(sc);
1542 if (scb->flags & SCB_SENDING) {
1543 printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1544 Debugger();
1545 }
1546 #endif
1547
1548 /*
1549 * If it has been through before, then
1550 * a previous abort has failed, don't
1551 * try abort again
1552 */
1553 if (scb->flags & SCB_ABORT) {
1554 /* abort timed out */
1555 printf(" AGAIN\n");
1556 /* XXX Must reset! */
1557 } else {
1558 /* abort the operation that has timed out */
1559 printf("\n");
1560 scb->xs->error = XS_TIMEOUT;
1561 scb->timeout = WDS_ABORT_TIMEOUT;
1562 scb->flags |= SCB_ABORT;
1563 wds_queue_scb(sc, scb);
1564 }
1565
1566 splx(s);
1567 }
1568