wds.c revision 1.6 1 /* $NetBSD: wds.c,v 1.6 1996/05/05 00:40:03 mycroft 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(ia->ia_ic, sc->sc_irq, IST_EDGE,
299 IPL_BIO, wdsintr, 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 c;
374
375 /* Was it really an interrupt from the board? */
376 if ((inb(iobase + WDS_STAT) & WDSS_IRQ) == 0)
377 return 0;
378
379 /* Get the interrupt status byte. */
380 c = inb(iobase + WDS_IRQSTAT) & WDSI_MASK;
381
382 /* Acknowledge (which resets) the interrupt. */
383 outb(iobase + WDS_IRQACK, 0x00);
384
385 switch (c) {
386 case WDSI_MSVC:
387 wds_finish_scbs(sc);
388 break;
389
390 case WDSI_MFREE:
391 wds_start_scbs(sc);
392 break;
393
394 default:
395 printf("%s: unrecognized interrupt type %02x",
396 sc->sc_dev.dv_xname, c);
397 break;
398 }
399
400 return 1;
401 }
402
403 integrate void
404 wds_reset_scb(sc, scb)
405 struct wds_softc *sc;
406 struct wds_scb *scb;
407 {
408
409 scb->flags = 0;
410 }
411
412 /*
413 * Free the command structure, the outgoing mailbox and the data buffer.
414 */
415 void
416 wds_free_scb(sc, scb)
417 struct wds_softc *sc;
418 struct wds_scb *scb;
419 {
420 int s;
421
422 if (scb->buf != 0) {
423 wds_free_buf(sc, scb->buf);
424 scb->buf = 0;
425 }
426
427 s = splbio();
428
429 wds_reset_scb(sc, scb);
430 TAILQ_INSERT_HEAD(&sc->sc_free_scb, scb, chain);
431
432 /*
433 * If there were none, wake anybody waiting for one to come free,
434 * starting with queued entries.
435 */
436 if (scb->chain.tqe_next == 0)
437 wakeup(&sc->sc_free_scb);
438
439 splx(s);
440 }
441
442 void
443 wds_free_buf(sc, buf)
444 struct wds_softc *sc;
445 struct wds_buf *buf;
446 {
447 int s;
448
449 s = splbio();
450
451 buf->busy = 0;
452 TAILQ_INSERT_HEAD(&wds_free_buffer, buf, chain);
453
454 /*
455 * If there were none, wake anybody waiting for one to come free,
456 * starting with queued entries.
457 */
458 if (buf->chain.tqe_next == 0)
459 wakeup(&wds_free_buffer);
460
461 splx(s);
462 }
463
464 integrate void
465 wds_init_scb(sc, scb)
466 struct wds_softc *sc;
467 struct wds_scb *scb;
468 {
469 int hashnum;
470
471 bzero(scb, sizeof(struct wds_scb));
472 /*
473 * put in the phystokv hash table
474 * Never gets taken out.
475 */
476 scb->hashkey = KVTOPHYS(scb);
477 hashnum = SCB_HASH(scb->hashkey);
478 scb->nexthash = sc->sc_scbhash[hashnum];
479 sc->sc_scbhash[hashnum] = scb;
480 wds_reset_scb(sc, scb);
481 }
482
483 /*
484 * Get a free scb
485 *
486 * If there are none, see if we can allocate a new one. If so, put it in
487 * the hash table too otherwise either return an error or sleep.
488 */
489 struct wds_scb *
490 wds_get_scb(sc, flags, needbuffer)
491 struct wds_softc *sc;
492 int flags;
493 int needbuffer;
494 {
495 struct wds_scb *scb;
496 int s;
497
498 s = splbio();
499
500 /*
501 * If we can and have to, sleep waiting for one to come free
502 * but only if we can't allocate a new one.
503 */
504 for (;;) {
505 scb = sc->sc_free_scb.tqh_first;
506 if (scb) {
507 TAILQ_REMOVE(&sc->sc_free_scb, scb, chain);
508 break;
509 }
510 if (sc->sc_numscbs < WDS_SCB_MAX) {
511 scb = (struct wds_scb *) malloc(sizeof(struct wds_scb),
512 M_TEMP, M_NOWAIT);
513 if (!scb) {
514 printf("%s: can't malloc scb\n",
515 sc->sc_dev.dv_xname);
516 goto out;
517 }
518 wds_init_scb(sc, scb);
519 sc->sc_numscbs++;
520 break;
521 }
522 if ((flags & SCSI_NOSLEEP) != 0)
523 goto out;
524 tsleep(&sc->sc_free_scb, PRIBIO, "wdsscb", 0);
525 }
526
527 scb->flags |= SCB_ALLOC;
528
529 if (needbuffer) {
530 scb->buf = wds_get_buf(sc, flags);
531 if (scb->buf == 0)
532 wds_free_scb(sc, scb);
533 scb = 0;
534 }
535
536 out:
537 splx(s);
538 return (scb);
539 }
540
541 struct wds_buf *
542 wds_get_buf(sc, flags)
543 struct wds_softc *sc;
544 int flags;
545 {
546 struct wds_buf *buf;
547 int s;
548
549 s = splbio();
550
551 for (;;) {
552 buf = wds_free_buffer.tqh_first;
553 if (buf) {
554 TAILQ_REMOVE(&wds_free_buffer, buf, chain);
555 break;
556 }
557 if ((flags & SCSI_NOSLEEP) != 0)
558 goto out;
559 tsleep(&wds_free_buffer, PRIBIO, "wdsbuf", 0);
560 }
561
562 buf->busy = 1;
563
564 out:
565 splx(s);
566 return (buf);
567 }
568
569 struct wds_scb *
570 wds_scb_phys_kv(sc, scb_phys)
571 struct wds_softc *sc;
572 u_long scb_phys;
573 {
574 int hashnum = SCB_HASH(scb_phys);
575 struct wds_scb *scb = sc->sc_scbhash[hashnum];
576
577 while (scb) {
578 if (scb->hashkey == scb_phys)
579 break;
580 /* XXX Check to see if it matches the sense command block. */
581 if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
582 break;
583 scb = scb->nexthash;
584 }
585 return scb;
586 }
587
588 /*
589 * Queue a SCB to be sent to the controller, and send it if possible.
590 */
591 void
592 wds_queue_scb(sc, scb)
593 struct wds_softc *sc;
594 struct wds_scb *scb;
595 {
596
597 TAILQ_INSERT_TAIL(&sc->sc_waiting_scb, scb, chain);
598 wds_start_scbs(sc);
599 }
600
601 /*
602 * Garbage collect mailboxes that are no longer in use.
603 */
604 void
605 wds_collect_mbo(sc)
606 struct wds_softc *sc;
607 {
608 struct wds_mbx_out *wmbo; /* Mail Box Out pointer */
609 struct wds_scb *scb;
610
611 wmbo = wmbx->cmbo;
612
613 while (sc->sc_mbofull > 0) {
614 if (wmbo->cmd != WDS_MBO_FREE)
615 break;
616
617 #ifdef WDSDIAG
618 scb = wds_scb_phys_kv(sc, phystol(wmbo->scb_addr));
619 scb->flags &= ~SCB_SENDING;
620 #endif
621
622 --sc->sc_mbofull;
623 wds_nextmbx(wmbo, wmbx, mbo);
624 }
625
626 wmbx->cmbo = wmbo;
627 }
628
629 /*
630 * Send as many SCBs as we have empty mailboxes for.
631 */
632 void
633 wds_start_scbs(sc)
634 struct wds_softc *sc;
635 {
636 int iobase = sc->sc_iobase;
637 struct wds_mbx_out *wmbo; /* Mail Box Out pointer */
638 struct wds_scb *scb;
639 u_char c;
640
641 wmbo = wmbx->tmbo;
642
643 while ((scb = sc->sc_waiting_scb.tqh_first) != NULL) {
644 if (sc->sc_mbofull >= WDS_MBX_SIZE) {
645 wds_collect_mbo(sc);
646 if (sc->sc_mbofull >= WDS_MBX_SIZE) {
647 c = WDSC_IRQMFREE;
648 wds_cmd(iobase, &c, sizeof c);
649 break;
650 }
651 }
652
653 TAILQ_REMOVE(&sc->sc_waiting_scb, scb, chain);
654 #ifdef WDSDIAG
655 scb->flags |= SCB_SENDING;
656 #endif
657
658 /* Link scb to mbo. */
659 if (scb->flags & SCB_SENSE)
660 ltophys(KVTOPHYS(&scb->sense), wmbo->scb_addr);
661 else
662 ltophys(KVTOPHYS(&scb->cmd), wmbo->scb_addr);
663 /* XXX What about aborts? */
664 wmbo->cmd = WDS_MBO_START;
665
666 /* Tell the card to poll immediately. */
667 c = WDSC_MSTART(wmbo - wmbx->mbo);
668 wds_cmd(sc->sc_iobase, &c, sizeof c);
669
670 if ((scb->flags & SCB_POLLED) == 0)
671 timeout(wds_timeout, scb, (scb->timeout * hz) / 1000);
672
673 ++sc->sc_mbofull;
674 wds_nextmbx(wmbo, wmbx, mbo);
675 }
676
677 wmbx->tmbo = wmbo;
678 }
679
680 /*
681 * Process the result of a SCSI command.
682 */
683 void
684 wds_done(sc, scb, stat)
685 struct wds_softc *sc;
686 struct wds_scb *scb;
687 u_char stat;
688 {
689 struct scsi_xfer *xs = scb->xs;
690
691 /* XXXXX */
692
693 /* Don't release the SCB if it was an internal command. */
694 if (xs == 0) {
695 scb->flags |= SCB_DONE;
696 return;
697 }
698
699 /* Sense handling. */
700 if (xs->error == XS_SENSE) {
701 bcopy(&scb->sense_data, &xs->sense, sizeof (struct scsi_sense_data));
702 } else {
703 if (xs->error == XS_NOERROR) {
704 /* If all went well, or an error is acceptable. */
705 if (stat == WDS_MBI_OK) {
706 /* OK, set the result */
707 xs->resid = 0;
708 } else {
709 /* Check the mailbox status. */
710 switch (stat) {
711 case WDS_MBI_OKERR:
712 /* SCSI error recorded in scb, counts as WDS_MBI_OK */
713 switch (scb->cmd.venderr) {
714 case 0x00:
715 printf("%s: Is this an error?\n", sc->sc_dev.dv_xname);
716 xs->error = XS_DRIVER_STUFFUP; /* Experiment */
717 break;
718 case 0x01:
719 /*printf("%s: OK, see SCSI error field.\n", sc->sc_dev.dv_xname);*/
720 if (scb->cmd.stat == SCSI_CHECK) {
721 /* Do sense. */
722 wds_sense (sc, scb);
723 return;
724 } else if (scb->cmd.stat == SCSI_BUSY) {
725 xs->error = XS_BUSY;
726 }
727 break;
728 case 0x40:
729 /*printf("%s: DMA underrun!\n", sc->sc_dev.dv_xname);*/
730 /* Hits this if the target returns fewer that datalen bytes (eg my CD-ROM,
731 which returns a short version string, or if DMA is turned off etc. */
732 xs->resid = 0;
733 break;
734 default:
735 printf("%s: VENDOR ERROR %02x, scsi %02x\n", sc->sc_dev.dv_xname, scb->cmd.venderr, scb->cmd.stat);
736 xs->error = XS_DRIVER_STUFFUP; /* Experiment */
737 break;
738 }
739 break;
740 case WDS_MBI_ETIME:
741 /*
742 * The documentation isn't clear on
743 * what conditions might generate this,
744 * but selection timeouts are the only
745 * one I can think of.
746 */
747 xs->error = XS_SELTIMEOUT;
748 break;
749 case WDS_MBI_ERESET:
750 case WDS_MBI_ETARCMD:
751 case WDS_MBI_ERESEL:
752 case WDS_MBI_ESEL:
753 case WDS_MBI_EABORT:
754 case WDS_MBI_ESRESET:
755 case WDS_MBI_EHRESET:
756 xs->error = XS_DRIVER_STUFFUP;
757 break;
758 }
759 }
760 } /* else sense */
761
762 if (NEEDBUFFER(sc) && xs->datalen) {
763 if (xs->flags & SCSI_DATA_IN)
764 bcopy(scb->buf->data, xs->data, xs->datalen);
765 }
766 } /* XS_NOERROR */
767
768 wds_free_scb(sc, scb);
769 xs->flags |= ITSDONE;
770 scsi_done(xs);
771 }
772
773 int
774 wds_find(ia, sc)
775 struct isa_attach_args *ia;
776 struct wds_softc *sc;
777 {
778 int iobase = ia->ia_iobase;
779 u_char c;
780 int i;
781
782 /* XXXXX */
783
784 /*
785 * Sending a command causes the CMDRDY bit to clear.
786 */
787 c = inb(iobase + WDS_STAT);
788 for (i = 0; i < 4; i++)
789 if ((inb(iobase+WDS_STAT) & WDSS_RDY) != 0) {
790 goto ready;
791 delay(10);
792 }
793 return 1;
794
795 ready:
796 outb(iobase + WDS_CMD, WDSC_NOOP);
797 if (inb(iobase + WDS_STAT) & WDSS_RDY)
798 return 1;
799
800 outb(iobase + WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
801 delay(10000);
802 outb(iobase + WDS_HCR, 0x00);
803 delay(500000);
804 wds_wait(iobase + WDS_STAT, WDSS_RDY, WDSS_RDY);
805 if (inb(iobase + WDS_IRQSTAT) != 1)
806 if (inb(iobase + WDS_IRQSTAT) != 7)
807 printf("%s: failed reset!!! %2x\n", sc->sc_dev.dv_xname, inb(iobase + WDS_IRQSTAT));
808
809 if ((inb(iobase + WDS_STAT) & (WDSS_RDY)) != WDSS_RDY) {
810 printf("%s: waiting for controller to become ready.", sc->sc_dev.dv_xname);
811 for (i = 0; i < 20; i++) {
812 if ((inb(iobase + WDS_STAT) & (WDSS_RDY)) == WDSS_RDY)
813 break;
814 printf(".");
815 delay(10000);
816 }
817 if ((inb(iobase + WDS_STAT) & (WDSS_RDY)) != WDSS_RDY) {
818 printf(" failed\n");
819 return 1;
820 }
821 printf("\n");
822 }
823
824 if (sc != NULL) {
825 /* XXX Can we do this better? */
826 /* who are we on the scsi bus? */
827 sc->sc_scsi_dev = 7;
828
829 sc->sc_iobase = iobase;
830 sc->sc_irq = ia->ia_irq;
831 sc->sc_drq = ia->ia_drq;
832 }
833
834 return 0;
835 }
836
837 /*
838 * Initialise the board and driver.
839 */
840 void
841 wds_init(sc)
842 struct wds_softc *sc;
843 {
844 int iobase = sc->sc_iobase;
845 struct wds_setup init;
846 u_char c;
847 int i;
848
849 /*
850 * Set up initial mail box for round-robin operation.
851 */
852 for (i = 0; i < WDS_MBX_SIZE; i++) {
853 wmbx->mbo[i].cmd = WDS_MBO_FREE;
854 wmbx->mbi[i].stat = WDS_MBI_FREE;
855 }
856 wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
857 wmbx->tmbi = &wmbx->mbi[0];
858 sc->sc_mbofull = 0;
859
860 /* Clear the buffers. */
861 TAILQ_INIT(&wds_free_buffer);
862 for (i = 0; i < BUFCNT; i++) {
863 wds_buffer[i].busy = 0;
864 TAILQ_INSERT_HEAD(&wds_free_buffer, &wds_buffer[i], chain);
865 }
866
867 init.opcode = WDSC_INIT;
868 init.scsi_id = sc->sc_scsi_dev;
869 /* Record scsi id of controller for use in scsi_attach */
870 sc->sc_scsi_dev = init.scsi_id;
871 init.buson_t = 48;
872 init.busoff_t = 24;
873 init.xx = 0;
874 ltophys(KVTOPHYS(wmbx), init.mbaddr);
875 init.nomb = init.nimb = WDS_MBX_SIZE;
876 wds_cmd(iobase, (u_char *)&init, sizeof init);
877
878 wds_wait(iobase + WDS_STAT, WDSS_INIT, WDSS_INIT);
879
880 c = WDSC_DISUNSOL;
881 wds_cmd(iobase, &c, sizeof c);
882
883 outb(iobase + WDS_HCR, WDSH_DRQEN);
884 }
885
886 /*
887 * Read the board's firmware revision information.
888 */
889 void
890 wds_inquire_setup_information(sc)
891 struct wds_softc *sc;
892 {
893 struct wds_scb *scb;
894 int iobase;
895 u_char *j;
896 int s;
897
898 iobase = sc->sc_iobase;
899
900 if ((scb = wds_get_scb(sc, SCSI_NOSLEEP, 0)) == NULL) {
901 printf("%s: no request slot available in getvers()!\n", sc->sc_dev.dv_xname);
902 return;
903 }
904 scb->xs = NULL;
905 scb->timeout = 40;
906
907 bzero(&scb->cmd, sizeof scb->cmd);
908 scb->cmd.write = 0x80;
909 scb->cmd.opcode = WDSX_GETFIRMREV;
910
911 /* Will poll card, await result. */
912 outb(iobase + WDS_HCR, WDSH_DRQEN);
913 scb->flags |= SCB_POLLED;
914
915 s = splbio();
916 wds_queue_scb(sc, scb);
917 splx(s);
918
919 if (wds_ipoll(sc, scb, scb->timeout))
920 goto out;
921
922 /* Print the version number. */
923 printf(": version %x.%02x ", scb->cmd.targ, scb->cmd.scb.opcode);
924 sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb.opcode;
925 /* Print out the version string. */
926 j = 2 + &(scb->cmd.targ);
927 while ((*j >= 32) && (*j < 128)) {
928 printf("%c", *j);
929 j++;
930 }
931
932 out:
933 printf("\n");
934 wds_free_scb(sc, scb);
935 }
936
937 void
938 wdsminphys(bp)
939 struct buf *bp;
940 {
941
942 if (bp->b_bcount > ((WDS_NSEG - 1) << PGSHIFT))
943 bp->b_bcount = ((WDS_NSEG - 1) << PGSHIFT);
944 minphys(bp);
945 }
946
947 /*
948 * Send a SCSI command.
949 */
950 int
951 wds_scsi_cmd(xs)
952 struct scsi_xfer *xs;
953 {
954 struct scsi_link *sc_link = xs->sc_link;
955 struct wds_softc *sc = sc_link->adapter_softc;
956 struct wds_scb *scb;
957 struct wds_scat_gath *sg;
958 int seg;
959 u_long thiskv, thisphys, nextphys;
960 int bytes_this_seg, bytes_this_page, datalen, flags;
961 #ifdef TFS
962 struct iovec *iovp;
963 #endif
964 int s;
965
966 int iobase;
967
968 iobase = sc->sc_iobase;
969
970 if (xs->flags & SCSI_RESET) {
971 /* XXX Fix me! */
972 printf("%s: reset!\n", sc->sc_dev.dv_xname);
973 wds_init(sc);
974 return COMPLETE;
975 }
976
977 flags = xs->flags;
978 if ((scb = wds_get_scb(sc, flags, NEEDBUFFER(sc))) == NULL) {
979 xs->error = XS_DRIVER_STUFFUP;
980 return TRY_AGAIN_LATER;
981 }
982 scb->xs = xs;
983 scb->timeout = xs->timeout;
984
985 if (xs->flags & SCSI_DATA_UIO) {
986 /* XXX Fix me! */
987 /* Let's not worry about UIO. There isn't any code for the *
988 * non-SG boards anyway! */
989 printf("%s: UIO is untested and disabled!\n", sc->sc_dev.dv_xname);
990 goto bad;
991 }
992
993 /* Zero out the command structure. */
994 bzero(&scb->cmd, sizeof scb->cmd);
995 bcopy(xs->cmd, &scb->cmd.scb, xs->cmdlen < 12 ? xs->cmdlen : 12);
996
997 /* Set up some of the command fields. */
998 scb->cmd.targ = (xs->sc_link->target << 5) | xs->sc_link->lun;
999
1000 /* NOTE: cmd.write may be OK as 0x40 (disable direction checking)
1001 * on boards other than the WD-7000V-ASE. Need this for the ASE:
1002 */
1003 scb->cmd.write = (xs->flags & SCSI_DATA_IN) ? 0x80 : 0x00;
1004
1005 if (!NEEDBUFFER(sc) && xs->datalen) {
1006 sg = scb->scat_gath;
1007 seg = 0;
1008 #ifdef TFS
1009 if (flags & SCSI_DATA_UIO) {
1010 iovp = ((struct uio *)xs->data)->uio_iov;
1011 datalen = ((struct uio *)xs->data)->uio_iovcnt;
1012 xs->datalen = 0;
1013 while (datalen && seg < WDS_NSEG) {
1014 ltophys(iovp->iov_base, sg->seg_addr);
1015 ltophys(iovp->iov_len, sg->seg_len);
1016 xs->datalen += iovp->iov_len;
1017 SC_DEBUGN(sc_link, SDEV_DB4, ("UIO(0x%x@0x%x)",
1018 iovp->iov_len, iovp->iov_base));
1019 sg++;
1020 iovp++;
1021 seg++;
1022 datalen--;
1023 }
1024 } else
1025 #endif /* TFS */
1026 {
1027 /*
1028 * Set up the scatter-gather block.
1029 */
1030 SC_DEBUG(sc_link, SDEV_DB4,
1031 ("%d @0x%x:- ", xs->datalen, xs->data));
1032
1033 datalen = xs->datalen;
1034 thiskv = (int)xs->data;
1035 thisphys = KVTOPHYS(xs->data);
1036
1037 while (datalen && seg < WDS_NSEG) {
1038 bytes_this_seg = 0;
1039
1040 /* put in the base address */
1041 ltophys(thisphys, sg->seg_addr);
1042
1043 SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
1044
1045 /* do it at least once */
1046 nextphys = thisphys;
1047 while (datalen && thisphys == nextphys) {
1048 /*
1049 * This page is contiguous (physically)
1050 * with the the last, just extend the
1051 * length
1052 */
1053 /* check it fits on the ISA bus */
1054 if (thisphys > 0xFFFFFF) {
1055 printf("%s: DMA beyond"
1056 " end of ISA\n",
1057 sc->sc_dev.dv_xname);
1058 goto bad;
1059 }
1060 /* how far to the end of the page */
1061 nextphys = (thisphys & ~PGOFSET) + NBPG;
1062 bytes_this_page = nextphys - thisphys;
1063 /**** or the data ****/
1064 bytes_this_page = min(bytes_this_page,
1065 datalen);
1066 bytes_this_seg += bytes_this_page;
1067 datalen -= bytes_this_page;
1068
1069 /* get more ready for the next page */
1070 thiskv = (thiskv & ~PGOFSET) + NBPG;
1071 if (datalen)
1072 thisphys = KVTOPHYS(thiskv);
1073 }
1074 /*
1075 * next page isn't contiguous, finish the seg
1076 */
1077 SC_DEBUGN(sc_link, SDEV_DB4,
1078 ("(0x%x)", bytes_this_seg));
1079 ltophys(bytes_this_seg, sg->seg_len);
1080 sg++;
1081 seg++;
1082 }
1083 }
1084 /* end of iov/kv decision */
1085 SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
1086 if (datalen) {
1087 /*
1088 * there's still data, must have run out of segs!
1089 */
1090 printf("%s: wds_scsi_cmd, more than %d dma segs\n",
1091 sc->sc_dev.dv_xname, WDS_NSEG);
1092 goto bad;
1093 }
1094 scb->cmd.opcode = WDSX_SCSISG;
1095 ltophys(KVTOPHYS(scb->scat_gath), scb->cmd.data);
1096 ltophys(seg * sizeof(struct wds_scat_gath), scb->cmd.len);
1097 } else if (xs->datalen > 0) {
1098 /* The board is an ASC or ASE. Do not use scatter/gather. */
1099 if (xs->datalen > BUFLEN) {
1100 printf("%s: wds_scsi_cmd, I/O too large for bounce buffer\n",
1101 sc->sc_dev.dv_xname);
1102 goto bad;
1103 }
1104 if (xs->flags & SCSI_DATA_OUT)
1105 bcopy(xs->data, scb->buf->data, xs->datalen);
1106 else
1107 bzero(scb->buf->data, xs->datalen);
1108 scb->cmd.opcode = WDSX_SCSICMD;
1109 ltophys(KVTOPHYS(scb->buf->data), scb->cmd.data);
1110 ltophys(xs->datalen, scb->cmd.len);
1111 } else {
1112 scb->cmd.opcode = WDSX_SCSICMD;
1113 ltophys(0, scb->cmd.data);
1114 ltophys(0, scb->cmd.len);
1115 }
1116
1117 scb->cmd.stat = 0x00;
1118 scb->cmd.venderr = 0x00;
1119 ltophys(0, scb->cmd.link);
1120
1121 /* XXX Do we really want to do this? */
1122 if (flags & SCSI_POLL) {
1123 /* Will poll card, await result. */
1124 outb(iobase + WDS_HCR, WDSH_DRQEN);
1125 scb->flags |= SCB_POLLED;
1126 } else {
1127 /* Will send command, let interrupt routine handle result. */
1128 outb(iobase + WDS_HCR, WDSH_IRQEN | WDSH_DRQEN);
1129 }
1130
1131 s = splbio();
1132 wds_queue_scb(sc, scb);
1133 splx(s);
1134
1135 if ((flags & SCSI_POLL) == 0)
1136 return SUCCESSFULLY_QUEUED;
1137
1138 if (wds_poll(sc, xs, scb->timeout)) {
1139 wds_timeout(scb);
1140 if (wds_poll(sc, xs, scb->timeout))
1141 wds_timeout(scb);
1142 }
1143 return COMPLETE;
1144
1145 bad:
1146 xs->error = XS_DRIVER_STUFFUP;
1147 wds_free_scb(sc, scb);
1148 return COMPLETE;
1149 }
1150
1151 /*
1152 * Send a sense request.
1153 */
1154 void
1155 wds_sense(sc, scb)
1156 struct wds_softc *sc;
1157 struct wds_scb *scb;
1158 {
1159 struct scsi_xfer *xs = scb->xs;
1160 struct scsi_sense *ss = (void *)&scb->sense.scb;
1161 int s;
1162
1163 /* XXXXX */
1164
1165 /* Send sense request SCSI command. */
1166 xs->error = XS_SENSE;
1167 scb->flags |= SCB_SENSE;
1168
1169 /* First, save the return values */
1170 if (NEEDBUFFER(sc) && xs->datalen) {
1171 if (xs->flags & SCSI_DATA_IN)
1172 bcopy(scb->buf->data, xs->data, xs->datalen);
1173 }
1174
1175 /* Next, setup a request sense command block */
1176 bzero(ss, sizeof(*ss));
1177 ss->opcode = REQUEST_SENSE;
1178 ss->byte2 = xs->sc_link->lun << 5;
1179 ss->length = sizeof(struct scsi_sense_data);
1180
1181 /* Set up some of the command fields. */
1182 scb->sense.targ = scb->cmd.targ;
1183 scb->sense.write = 0x80;
1184 scb->sense.opcode = WDSX_SCSICMD;
1185 ltophys(KVTOPHYS(&scb->sense_data), scb->sense.data);
1186 ltophys(sizeof(struct scsi_sense_data), scb->sense.len);
1187
1188 s = splbio();
1189 wds_queue_scb(sc, scb);
1190 splx(s);
1191
1192 /*
1193 * There's no reason for us to poll here. There are two cases:
1194 * 1) If it's a polling operation, then we're called from the interrupt
1195 * handler, and we return and continue polling.
1196 * 2) If it's an interrupt-driven operation, then it gets completed
1197 * later on when the REQUEST SENSE finishes.
1198 */
1199 }
1200
1201 /*
1202 * Poll a particular unit, looking for a particular scb
1203 */
1204 int
1205 wds_poll(sc, xs, count)
1206 struct wds_softc *sc;
1207 struct scsi_xfer *xs;
1208 int count;
1209 {
1210 int iobase = sc->sc_iobase;
1211
1212 /* timeouts are in msec, so we loop in 1000 usec cycles */
1213 while (count) {
1214 /*
1215 * If we had interrupts enabled, would we
1216 * have got an interrupt?
1217 */
1218 if (inb(iobase + WDS_STAT) & WDSS_IRQ)
1219 wdsintr(sc);
1220 if (xs->flags & ITSDONE)
1221 return 0;
1222 delay(1000); /* only happens in boot so ok */
1223 count--;
1224 }
1225 return 1;
1226 }
1227
1228 /*
1229 * Poll a particular unit, looking for a particular scb
1230 */
1231 int
1232 wds_ipoll(sc, scb, count)
1233 struct wds_softc *sc;
1234 struct wds_scb *scb;
1235 int count;
1236 {
1237 int iobase = sc->sc_iobase;
1238
1239 /* timeouts are in msec, so we loop in 1000 usec cycles */
1240 while (count) {
1241 /*
1242 * If we had interrupts enabled, would we
1243 * have got an interrupt?
1244 */
1245 if (inb(iobase + WDS_STAT) & WDSS_IRQ)
1246 wdsintr(sc);
1247 if (scb->flags & SCB_DONE)
1248 return 0;
1249 delay(1000); /* only happens in boot so ok */
1250 count--;
1251 }
1252 return 1;
1253 }
1254
1255 void
1256 wds_timeout(arg)
1257 void *arg;
1258 {
1259 struct wds_scb *scb = arg;
1260 struct scsi_xfer *xs = scb->xs;
1261 struct scsi_link *sc_link = xs->sc_link;
1262 struct wds_softc *sc = sc_link->adapter_softc;
1263 int s;
1264
1265 sc_print_addr(sc_link);
1266 printf("timed out");
1267
1268 s = splbio();
1269
1270 #ifdef WDSDIAG
1271 /*
1272 * If The scb's mbx is not free, then the board has gone south?
1273 */
1274 wds_collect_mbo(sc);
1275 if (scb->flags & SCB_SENDING) {
1276 printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1277 Debugger();
1278 }
1279 #endif
1280
1281 /*
1282 * If it has been through before, then
1283 * a previous abort has failed, don't
1284 * try abort again
1285 */
1286 if (scb->flags & SCB_ABORT) {
1287 /* abort timed out */
1288 printf(" AGAIN\n");
1289 /* XXX Must reset! */
1290 } else {
1291 /* abort the operation that has timed out */
1292 printf("\n");
1293 scb->xs->error = XS_TIMEOUT;
1294 scb->timeout = WDS_ABORT_TIMEOUT;
1295 scb->flags |= SCB_ABORT;
1296 wds_queue_scb(sc, scb);
1297 }
1298
1299 splx(s);
1300 }
1301