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