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