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