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