wds.c revision 1.1 1 /* $NetBSD: wds.c,v 1.1 1996/03/29 20:53:40 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 #define WDS_MBX_SIZE 16
76
77 #define WDS_SCB_MAX 32
78 #define SCB_HASH_SIZE 32 /* hash table size for phystokv */
79 #define SCB_HASH_SHIFT 9
80 #define SCB_HASH(x) ((((long)(x))>>SCB_HASH_SHIFT) & (SCB_HASH_SIZE - 1))
81
82 #define wds_nextmbx(wmb, mbx, mbio) \
83 if ((wmb) == &(mbx)->mbio[WDS_MBX_SIZE - 1]) \
84 (wmb) = &(mbx)->mbio[0]; \
85 else \
86 (wmb)++;
87
88 struct wds_mbx {
89 struct wds_mbx_out mbo[WDS_MBX_SIZE];
90 struct wds_mbx_in mbi[WDS_MBX_SIZE];
91 struct wds_mbx_out *cmbo; /* Collection Mail Box out */
92 struct wds_mbx_out *tmbo; /* Target Mail Box out */
93 struct wds_mbx_in *tmbi; /* Target Mail Box in */
94 };
95
96 #define KVTOPHYS(x) vtophys(x)
97
98 struct wds_softc {
99 struct device sc_dev;
100 struct isadev sc_id;
101 void *sc_ih;
102
103 int sc_iobase;
104 int sc_irq, sc_drq;
105
106 int sc_revision;
107
108 struct wds_mbx sc_mbx;
109 #define wmbx (&sc->sc_mbx)
110 struct wds_scb *sc_scbhash[SCB_HASH_SIZE];
111 TAILQ_HEAD(, wds_scb) sc_free_scb, sc_waiting_scb;
112 int sc_numscbs, sc_mbofull;
113 int sc_scsi_dev;
114 struct scsi_link sc_link; /* prototype for subdevs */
115 };
116
117 /* Define the bounce buffer length... */
118 #define BUFLEN (64*1024)
119 /* ..and how many there are. One per device! Non-FASST boards need these. */
120 #define BUFCNT 8
121 /* The macro for deciding whether the board needs a buffer. */
122 #define NEEDBUFFER(sc) (sc->sc_revision < 0x800)
123
124 struct wds_buf {
125 u_char data[BUFLEN];
126 int busy;
127 TAILQ_ENTRY(wds_buf) chain;
128 } wds_buffer[BUFCNT];
129
130 TAILQ_HEAD(, wds_buf) wds_free_buffer;
131
132 integrate void wds_wait __P((int, int, int));
133 int wds_cmd __P((int, u_char *, int));
134 integrate void wds_finish_scbs __P((struct wds_softc *));
135 int wdsintr __P((void *));
136 integrate void wds_reset_scb __P((struct wds_softc *, struct wds_scb *));
137 void wds_free_scb __P((struct wds_softc *, struct wds_scb *));
138 void wds_free_buf __P((struct wds_softc *, struct wds_buf *));
139 integrate void wds_init_scb __P((struct wds_softc *, struct wds_scb *));
140 struct wds_scb *wds_get_scb __P((struct wds_softc *, int, int));
141 struct wds_buf *wds_get_buf __P((struct wds_softc *, int));
142 struct wds_scb *wds_scb_phys_kv __P((struct wds_softc *, u_long));
143 void wds_queue_scb __P((struct wds_softc *, struct wds_scb *));
144 void wds_collect_mbo __P((struct wds_softc *));
145 void wds_start_scbs __P((struct wds_softc *));
146 void wds_done __P((struct wds_softc *, struct wds_scb *, u_char));
147 int wds_find __P((struct isa_attach_args *, struct wds_softc *));
148 void wds_init __P((struct wds_softc *));
149 void wds_inquire_setup_information __P((struct wds_softc *));
150 void wdsminphys __P((struct buf *));
151 int wds_scsi_cmd __P((struct scsi_xfer *));
152 int wds_sense __P((struct wds_softc *, struct wds_scb *));
153 int wds_poll __P((struct wds_softc *, struct scsi_xfer *, int));
154 int wds_ipoll __P((struct wds_softc *, struct wds_scb *, int));
155 void wds_timeout __P((void *));
156
157 struct scsi_adapter wds_switch = {
158 wds_scsi_cmd,
159 wdsminphys,
160 0,
161 0,
162 };
163
164 /* the below structure is so we have a default dev struct for our link struct */
165 struct scsi_device wds_dev = {
166 NULL, /* Use default error handler */
167 NULL, /* have a queue, served by this */
168 NULL, /* have no async handler */
169 NULL, /* Use default 'done' routine */
170 };
171
172 int wdsprobe __P((struct device *, void *, void *));
173 void wdsattach __P((struct device *, struct device *, void *));
174 int wdsprint __P((void *, char *));
175
176 struct cfattach wds_ca = {
177 sizeof(struct wds_softc), wdsprobe, wdsattach
178 };
179
180 struct cfdriver wds_cd = {
181 NULL, "wds", DV_DULL
182 };
183
184 #define WDS_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
185
186 integrate void
187 wds_wait(port, mask, val)
188 int port;
189 int mask;
190 int val;
191 {
192
193 while ((inb(port) & mask) != val)
194 ;
195 }
196
197 /*
198 * Write a command to the board's I/O ports.
199 */
200 int
201 wds_cmd(iobase, ibuf, icnt)
202 int iobase;
203 u_char *ibuf;
204 int icnt;
205 {
206 u_char c;
207
208 wds_wait(iobase + WDS_STAT, WDSS_RDY, WDSS_RDY);
209
210 while (icnt--) {
211 outb(iobase + WDS_CMD, *ibuf++);
212 wds_wait(iobase + WDS_STAT, WDSS_RDY, WDSS_RDY);
213 c = inb(iobase + WDS_STAT);
214 if (c & WDSS_REJ)
215 return 1;
216 }
217
218 return 0;
219 }
220
221 /*
222 * Check for the presence of a WD7000 SCSI controller.
223 */
224 int
225 wdsprobe(parent, match, aux)
226 struct device *parent;
227 void *match, *aux;
228 {
229 register struct isa_attach_args *ia = aux;
230
231 #ifdef NEWCONFIG
232 if (ia->ia_iobase == IOBASEUNK)
233 return 0;
234 #endif
235
236 /* See if there is a unit at this location. */
237 if (wds_find(ia, NULL) != 0)
238 return 0;
239
240 ia->ia_msize = 0;
241 ia->ia_iosize = 8;
242 return 1;
243 }
244
245 int
246 wdsprint(aux, name)
247 void *aux;
248 char *name;
249 {
250
251 if (name != NULL)
252 printf("%s: scsibus ", name);
253 return UNCONF;
254 }
255
256 /*
257 * Attach all available units.
258 */
259 void
260 wdsattach(parent, self, aux)
261 struct device *parent, *self;
262 void *aux;
263 {
264 struct isa_attach_args *ia = aux;
265 struct wds_softc *sc = (void *)self;
266
267 if (wds_find(ia, sc) != 0)
268 panic("wdsattach: wds_find of %s failed", self->dv_xname);
269 sc->sc_iobase = ia->ia_iobase;
270
271 if (sc->sc_drq != DRQUNK)
272 isa_dmacascade(sc->sc_drq);
273
274 wds_init(sc);
275 TAILQ_INIT(&sc->sc_free_scb);
276 TAILQ_INIT(&sc->sc_waiting_scb);
277 wds_inquire_setup_information(sc);
278
279 /*
280 * fill in the prototype scsi_link.
281 */
282 sc->sc_link.adapter_softc = sc;
283 sc->sc_link.adapter_target = sc->sc_scsi_dev;
284 sc->sc_link.adapter = &wds_switch;
285 sc->sc_link.device = &wds_dev;
286 /* XXX */
287 /* I don't think the -ASE can handle openings > 1. */
288 /* It gives Vendor Error 26 whenever I try it. */
289 sc->sc_link.openings = 1;
290
291 #ifdef NEWCONFIG
292 isa_establish(&sc->sc_id, &sc->sc_dev);
293 #endif
294 sc->sc_ih = isa_intr_establish(sc->sc_irq, IST_EDGE, IPL_BIO, wdsintr,
295 sc);
296
297 /*
298 * ask the adapter what subunits are present
299 */
300 config_found(self, &sc->sc_link, wdsprint);
301 }
302
303 integrate void
304 wds_finish_scbs(sc)
305 struct wds_softc *sc;
306 {
307 struct wds_mbx_in *wmbi;
308 struct wds_scb *scb;
309 int i;
310
311 wmbi = wmbx->tmbi;
312
313 if (wmbi->stat == WDS_MBI_FREE) {
314 for (i = 0; i < WDS_MBX_SIZE; i++) {
315 if (wmbi->stat != WDS_MBI_FREE) {
316 printf("%s: mbi not in round-robin order\n",
317 sc->sc_dev.dv_xname);
318 goto AGAIN;
319 }
320 wds_nextmbx(wmbi, wmbx, mbi);
321 }
322 #ifdef WDSDIAGnot
323 printf("%s: mbi interrupt with no full mailboxes\n",
324 sc->sc_dev.dv_xname);
325 #endif
326 return;
327 }
328
329 AGAIN:
330 do {
331 scb = wds_scb_phys_kv(sc, phystol(wmbi->scb_addr));
332 if (!scb) {
333 printf("%s: bad mbi scb pointer; skipping\n",
334 sc->sc_dev.dv_xname);
335 goto next;
336 }
337
338 #ifdef WDSDEBUG
339 if (wds_debug) {
340 u_char *cp = &scb->scsi_cmd;
341 printf("op=%x %x %x %x %x %x\n",
342 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
343 printf("stat %x for mbi addr = 0x%08x, ",
344 wmbi->stat, wmbi);
345 printf("scb addr = 0x%x\n", scb);
346 }
347 #endif /* WDSDEBUG */
348
349 untimeout(wds_timeout, scb);
350 wds_done(sc, scb, wmbi->stat);
351
352 next:
353 wmbi->stat = WDS_MBI_FREE;
354 wds_nextmbx(wmbi, wmbx, mbi);
355 } while (wmbi->stat != WDS_MBI_FREE);
356
357 wmbx->tmbi = wmbi;
358 }
359
360 /*
361 * Process an interrupt.
362 */
363 int
364 wdsintr(arg)
365 void *arg;
366 {
367 struct wds_softc *sc = arg;
368 int iobase = sc->sc_iobase;
369 u_char sts;
370
371 struct wds_mbx_in *in;
372 struct wds_scb *scb;
373 u_char stat, 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", c);
396 break;
397 }
398
399 return 1;
400 }
401
402 integrate void
403 wds_reset_scb(sc, scb)
404 struct wds_softc *sc;
405 struct wds_scb *scb;
406 {
407
408 scb->flags = 0;
409 }
410
411 /*
412 * Free the command structure, the outgoing mailbox and the data buffer.
413 */
414 void
415 wds_free_scb(sc, scb)
416 struct wds_softc *sc;
417 struct wds_scb *scb;
418 {
419 int s;
420
421 if (scb->buf != 0) {
422 wds_free_buf(sc, scb->buf);
423 scb->buf = 0;
424 }
425
426 s = splbio();
427
428 wds_reset_scb(sc, scb);
429 TAILQ_INSERT_HEAD(&sc->sc_free_scb, scb, chain);
430
431 /*
432 * If there were none, wake anybody waiting for one to come free,
433 * starting with queued entries.
434 */
435 if (scb->chain.tqe_next == 0)
436 wakeup(&sc->sc_free_scb);
437
438 splx(s);
439 }
440
441 void
442 wds_free_buf(sc, buf)
443 struct wds_softc *sc;
444 struct wds_buf *buf;
445 {
446 int s;
447
448 s = splbio();
449
450 buf->busy = 0;
451 TAILQ_INSERT_HEAD(&wds_free_buffer, buf, chain);
452
453 /*
454 * If there were none, wake anybody waiting for one to come free,
455 * starting with queued entries.
456 */
457 if (buf->chain.tqe_next == 0)
458 wakeup(&wds_free_buffer);
459
460 splx(s);
461 }
462
463 integrate void
464 wds_init_scb(sc, scb)
465 struct wds_softc *sc;
466 struct wds_scb *scb;
467 {
468 int hashnum;
469
470 bzero(scb, sizeof(struct wds_scb));
471 /*
472 * put in the phystokv hash table
473 * Never gets taken out.
474 */
475 scb->hashkey = KVTOPHYS(scb);
476 hashnum = SCB_HASH(scb->hashkey);
477 scb->nexthash = sc->sc_scbhash[hashnum];
478 sc->sc_scbhash[hashnum] = scb;
479 wds_reset_scb(sc, scb);
480 }
481
482 /*
483 * Get a free scb
484 *
485 * If there are none, see if we can allocate a new one. If so, put it in
486 * the hash table too otherwise either return an error or sleep.
487 */
488 struct wds_scb *
489 wds_get_scb(sc, flags, needbuffer)
490 struct wds_softc *sc;
491 int flags;
492 int needbuffer;
493 {
494 struct wds_scb *scb;
495 int s;
496
497 s = splbio();
498
499 /*
500 * If we can and have to, sleep waiting for one to come free
501 * but only if we can't allocate a new one.
502 */
503 for (;;) {
504 scb = sc->sc_free_scb.tqh_first;
505 if (scb) {
506 TAILQ_REMOVE(&sc->sc_free_scb, scb, chain);
507 break;
508 }
509 if (sc->sc_numscbs < WDS_SCB_MAX) {
510 scb = (struct wds_scb *) malloc(sizeof(struct wds_scb),
511 M_TEMP, M_NOWAIT);
512 if (!scb) {
513 printf("%s: can't malloc scb\n",
514 sc->sc_dev.dv_xname);
515 goto out;
516 }
517 wds_init_scb(sc, scb);
518 sc->sc_numscbs++;
519 break;
520 }
521 if ((flags & SCSI_NOSLEEP) != 0)
522 goto out;
523 tsleep(&sc->sc_free_scb, PRIBIO, "wdsscb", 0);
524 }
525
526 scb->flags |= SCB_ALLOC;
527
528 if (needbuffer) {
529 scb->buf = wds_get_buf(sc, flags);
530 if (scb->buf == 0)
531 wds_free_scb(sc, scb);
532 scb = 0;
533 }
534
535 out:
536 splx(s);
537 return (scb);
538 }
539
540 struct wds_buf *
541 wds_get_buf(sc, flags)
542 struct wds_softc *sc;
543 int flags;
544 {
545 struct wds_buf *buf;
546 int s;
547
548 s = splbio();
549
550 for (;;) {
551 buf = wds_free_buffer.tqh_first;
552 if (buf) {
553 TAILQ_REMOVE(&wds_free_buffer, buf, chain);
554 break;
555 }
556 if ((flags & SCSI_NOSLEEP) != 0)
557 goto out;
558 tsleep(&wds_free_buffer, PRIBIO, "wdsbuf", 0);
559 }
560
561 buf->busy = 1;
562
563 out:
564 splx(s);
565 return (buf);
566 }
567
568 struct wds_scb *
569 wds_scb_phys_kv(sc, scb_phys)
570 struct wds_softc *sc;
571 u_long scb_phys;
572 {
573 int hashnum = SCB_HASH(scb_phys);
574 struct wds_scb *scb = sc->sc_scbhash[hashnum];
575
576 while (scb) {
577 if (scb->hashkey == scb_phys)
578 break;
579 /* XXX Check to see if it matches the sense command block. */
580 if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
581 break;
582 scb = scb->nexthash;
583 }
584 return scb;
585 }
586
587 /*
588 * Queue a SCB to be sent to the controller, and send it if possible.
589 */
590 void
591 wds_queue_scb(sc, scb)
592 struct wds_softc *sc;
593 struct wds_scb *scb;
594 {
595
596 TAILQ_INSERT_TAIL(&sc->sc_waiting_scb, scb, chain);
597 wds_start_scbs(sc);
598 }
599
600 /*
601 * Garbage collect mailboxes that are no longer in use.
602 */
603 void
604 wds_collect_mbo(sc)
605 struct wds_softc *sc;
606 {
607 struct wds_mbx_out *wmbo; /* Mail Box Out pointer */
608 struct wds_scb *scb;
609
610 wmbo = wmbx->cmbo;
611
612 while (sc->sc_mbofull > 0) {
613 if (wmbo->cmd != WDS_MBO_FREE)
614 break;
615
616 #ifdef WDSDIAG
617 scb = wds_scb_phys_kv(sc, phystol(wmbo->scb_addr));
618 scb->flags &= ~SCB_SENDING;
619 #endif
620
621 --sc->sc_mbofull;
622 wds_nextmbx(wmbo, wmbx, mbo);
623 }
624
625 wmbx->cmbo = wmbo;
626 }
627
628 /*
629 * Send as many SCBs as we have empty mailboxes for.
630 */
631 void
632 wds_start_scbs(sc)
633 struct wds_softc *sc;
634 {
635 int iobase = sc->sc_iobase;
636 struct wds_mbx_out *wmbo; /* Mail Box Out pointer */
637 struct wds_scb *scb;
638 int i;
639 u_char c;
640
641 wmbo = wmbx->tmbo;
642
643 while (scb = sc->sc_waiting_scb.tqh_first) {
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 next:
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 int i, x;
692
693 /* XXXXX */
694
695 /* Don't release the SCB if it was an internal command. */
696 if (xs == 0) {
697 scb->flags |= SCB_DONE;
698 return;
699 }
700
701 /* Sense handling. */
702 if (xs->error == XS_SENSE) {
703 bcopy(&scb->sense_data, &xs->sense, sizeof (struct scsi_sense_data));
704 } else {
705 if (xs->error == XS_NOERROR) {
706 /* If all went well, or an error is acceptable. */
707 if (stat == WDS_MBI_OK) {
708 /* OK, set the result */
709 xs->resid = 0;
710 } else {
711 /* Check the mailbox status. */
712 switch (stat) {
713 case WDS_MBI_OKERR:
714 /* SCSI error recorded in scb, counts as WDS_MBI_OK */
715 switch (scb->cmd.venderr) {
716 case 0x00:
717 printf("%s: Is this an error?\n", sc->sc_dev.dv_xname);
718 xs->error = XS_DRIVER_STUFFUP; /* Experiment */
719 break;
720 case 0x01:
721 /*printf("%s: OK, see SCSI error field.\n", sc->sc_dev.dv_xname);*/
722 if (scb->cmd.stat == SCSI_CHECK) {
723 /* Do sense. */
724 wds_sense (sc, scb);
725 return;
726 } else if (scb->cmd.stat == SCSI_BUSY) {
727 xs->error = XS_BUSY;
728 }
729 break;
730 case 0x40:
731 /*printf("%s: DMA underrun!\n", sc->sc_dev.dv_xname);*/
732 /* Hits this if the target returns fewer that datalen bytes (eg my CD-ROM,
733 which returns a short version string, or if DMA is turned off etc. */
734 xs->resid = 0;
735 break;
736 default:
737 printf("%s: VENDOR ERROR %02x, scsi %02x\n", sc->sc_dev.dv_xname, scb->cmd.venderr, scb->cmd.stat);
738 xs->error = XS_DRIVER_STUFFUP; /* Experiment */
739 break;
740 }
741 break;
742 case WDS_MBI_ETIME:
743 /*
744 * The documentation isn't clear on
745 * what conditions might generate this,
746 * but selection timeouts are the only
747 * one I can think of.
748 */
749 xs->error = XS_SELTIMEOUT;
750 break;
751 case WDS_MBI_ERESET:
752 case WDS_MBI_ETARCMD:
753 case WDS_MBI_ERESEL:
754 case WDS_MBI_ESEL:
755 case WDS_MBI_EABORT:
756 case WDS_MBI_ESRESET:
757 case WDS_MBI_EHRESET:
758 xs->error = XS_DRIVER_STUFFUP;
759 break;
760 }
761 }
762 } /* else sense */
763
764 if (NEEDBUFFER(sc) && xs->datalen) {
765 if (xs->flags & SCSI_DATA_IN)
766 bcopy(scb->buf->data, xs->data, xs->datalen);
767 }
768 } /* XS_NOERROR */
769
770 wds_free_scb(sc, scb);
771 xs->flags |= ITSDONE;
772 scsi_done(xs);
773 }
774
775 int
776 wds_find(ia, sc)
777 struct isa_attach_args *ia;
778 struct wds_softc *sc;
779 {
780 int iobase = ia->ia_iobase;
781 u_char c;
782 int i;
783
784 /* XXXXX */
785
786 /*
787 * Sending a command causes the CMDRDY bit to clear.
788 */
789 c = inb(iobase + WDS_STAT);
790 for (i = 0; i < 4; i++)
791 if ((inb(iobase+WDS_STAT) & WDSS_RDY) != 0) {
792 goto ready;
793 delay(10);
794 }
795 return 1;
796
797 ready:
798 outb(iobase + WDS_CMD, WDSC_NOOP);
799 if (inb(iobase + WDS_STAT) & WDSS_RDY)
800 return 1;
801
802 outb(iobase + WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
803 delay(10000);
804 outb(iobase + WDS_HCR, 0x00);
805 delay(500000);
806 wds_wait(iobase + WDS_STAT, WDSS_RDY, WDSS_RDY);
807 if (inb(iobase + WDS_IRQSTAT) != 1)
808 if (inb(iobase + WDS_IRQSTAT) != 7)
809 printf("%s: failed reset!!! %2x\n", sc->sc_dev.dv_xname, inb(iobase + WDS_IRQSTAT));
810
811 if ((inb(iobase + WDS_STAT) & (WDSS_RDY)) != WDSS_RDY) {
812 printf("%s: waiting for controller to become ready.", sc->sc_dev.dv_xname);
813 for (i = 0; i < 20; i++) {
814 if ((inb(iobase + WDS_STAT) & (WDSS_RDY)) == WDSS_RDY)
815 break;
816 printf(".");
817 delay(10000);
818 }
819 if ((inb(iobase + WDS_STAT) & (WDSS_RDY)) != WDSS_RDY) {
820 printf(" failed\n");
821 return 1;
822 }
823 printf("\n");
824 }
825
826 if (sc != NULL) {
827 /* XXX Can we do this better? */
828 /* who are we on the scsi bus? */
829 sc->sc_scsi_dev = 7;
830
831 sc->sc_iobase = iobase;
832 sc->sc_irq = ia->ia_irq;
833 sc->sc_drq = ia->ia_drq;
834 }
835
836 return 0;
837 }
838
839 /*
840 * Initialise the board and driver.
841 */
842 void
843 wds_init(sc)
844 struct wds_softc *sc;
845 {
846 int iobase = sc->sc_iobase;
847 struct wds_setup init;
848 u_char c;
849 int i;
850
851 /*
852 * Set up initial mail box for round-robin operation.
853 */
854 for (i = 0; i < WDS_MBX_SIZE; i++) {
855 wmbx->mbo[i].cmd = WDS_MBO_FREE;
856 wmbx->mbi[i].stat = WDS_MBO_FREE;
857 }
858 wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
859 wmbx->tmbi = &wmbx->mbi[0];
860 sc->sc_mbofull = 0;
861
862 /* Clear the buffers. */
863 TAILQ_INIT(&wds_free_buffer);
864 for (i = 0; i < BUFCNT; i++) {
865 wds_buffer[i].busy = 0;
866 TAILQ_INSERT_HEAD(&wds_free_buffer, &wds_buffer[i], chain);
867 }
868
869 init.opcode = WDSC_INIT;
870 init.scsi_id = sc->sc_scsi_dev;
871 /* Record scsi id of controller for use in scsi_attach */
872 sc->sc_scsi_dev = init.scsi_id;
873 init.buson_t = 48;
874 init.busoff_t = 24;
875 init.xx = 0;
876 ltophys(KVTOPHYS(wmbx), init.mbaddr);
877 init.nomb = init.nimb = WDS_MBX_SIZE;
878 wds_cmd(iobase, (u_char *)&init, sizeof init);
879
880 wds_wait(iobase + WDS_STAT, WDSS_INIT, WDSS_INIT);
881
882 c = WDSC_DISUNSOL;
883 wds_cmd(iobase, &c, sizeof c);
884
885 outb(iobase + WDS_HCR, WDSH_DRQEN);
886 }
887
888 /*
889 * Read the board's firmware revision information.
890 */
891 void
892 wds_inquire_setup_information(sc)
893 struct wds_softc *sc;
894 {
895 struct wds_scb *scb;
896 int iobase;
897 u_char *j;
898 int s;
899
900 iobase = sc->sc_iobase;
901
902 if ((scb = wds_get_scb(sc, SCSI_NOSLEEP, 0)) == NULL) {
903 printf("%s: no request slot available in getvers()!\n", sc->sc_dev.dv_xname);
904 return;
905 }
906 scb->xs = NULL;
907 scb->timeout = 40;
908
909 bzero(&scb->cmd, sizeof scb->cmd);
910 scb->cmd.write = 0x80;
911 scb->cmd.opcode = WDSX_GETFIRMREV;
912
913 /* Will poll card, await result. */
914 outb(iobase + WDS_HCR, WDSH_DRQEN);
915 scb->flags |= SCB_POLLED;
916
917 s = splbio();
918 wds_queue_scb(sc, scb);
919 splx(s);
920
921 if (wds_ipoll(sc, scb, scb->timeout))
922 goto out;
923
924 /* Print the version number. */
925 printf(": version %x.%02x ", scb->cmd.targ, scb->cmd.scb.opcode);
926 sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb.opcode;
927 /* Print out the version string. */
928 j = 2 + &(scb->cmd.targ);
929 while ((*j >= 32) && (*j < 128)) {
930 printf("%c", *j);
931 j++;
932 }
933
934 out:
935 printf("\n");
936 wds_free_scb(sc, scb);
937 }
938
939 void
940 wdsminphys(bp)
941 struct buf *bp;
942 {
943
944 if (bp->b_bcount > ((WDS_NSEG - 1) << PGSHIFT))
945 bp->b_bcount = ((WDS_NSEG - 1) << PGSHIFT);
946 minphys(bp);
947 }
948
949 /*
950 * Send a SCSI command.
951 */
952 int
953 wds_scsi_cmd(xs)
954 struct scsi_xfer *xs;
955 {
956 struct scsi_link *sc_link = xs->sc_link;
957 struct wds_softc *sc = sc_link->adapter_softc;
958 struct wds_scb *scb;
959 struct wds_scat_gath *sg;
960 int seg;
961 u_long thiskv, thisphys, nextphys;
962 int bytes_this_seg, bytes_this_page, datalen, flags;
963 struct iovec *iovp;
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 int
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 u_char c;
1163 int i;
1164
1165 /* XXXXX */
1166
1167 /* Send sense request SCSI command. */
1168 xs->error = XS_SENSE;
1169 scb->flags |= SCB_SENSE;
1170
1171 /* First, save the return values */
1172 if (NEEDBUFFER(sc) && xs->datalen) {
1173 if (xs->flags & SCSI_DATA_IN)
1174 bcopy(scb->buf->data, xs->data, xs->datalen);
1175 }
1176
1177 /* Next, setup a request sense command block */
1178 bzero(ss, sizeof(*ss));
1179 ss->opcode = REQUEST_SENSE;
1180 ss->byte2 = xs->sc_link->lun << 5;
1181 ss->length = sizeof(struct scsi_sense_data);
1182
1183 /* Set up some of the command fields. */
1184 scb->sense.targ = scb->cmd.targ;
1185 scb->sense.write = 0x80;
1186 scb->sense.opcode = WDSX_SCSICMD;
1187 ltophys(KVTOPHYS(&scb->sense_data), scb->sense.data);
1188 ltophys(sizeof(struct scsi_sense_data), scb->sense.len);
1189
1190 s = splbio();
1191 wds_queue_scb(sc, scb);
1192 splx(s);
1193
1194 /*
1195 * There's no reason for us to poll here. There are two cases:
1196 * 1) If it's a polling operation, then we're called from the interrupt
1197 * handler, and we return and continue polling.
1198 * 2) If it's an interrupt-driven operation, then it gets completed
1199 * later on when the REQUEST SENSE finishes.
1200 */
1201 }
1202
1203 /*
1204 * Poll a particular unit, looking for a particular scb
1205 */
1206 int
1207 wds_poll(sc, xs, count)
1208 struct wds_softc *sc;
1209 struct scsi_xfer *xs;
1210 int count;
1211 {
1212 int iobase = sc->sc_iobase;
1213
1214 /* timeouts are in msec, so we loop in 1000 usec cycles */
1215 while (count) {
1216 /*
1217 * If we had interrupts enabled, would we
1218 * have got an interrupt?
1219 */
1220 if (inb(iobase + WDS_STAT) & WDSS_IRQ)
1221 wdsintr(sc);
1222 if (xs->flags & ITSDONE)
1223 return 0;
1224 delay(1000); /* only happens in boot so ok */
1225 count--;
1226 }
1227 return 1;
1228 }
1229
1230 /*
1231 * Poll a particular unit, looking for a particular scb
1232 */
1233 int
1234 wds_ipoll(sc, scb, count)
1235 struct wds_softc *sc;
1236 struct wds_scb *scb;
1237 int count;
1238 {
1239 int iobase = sc->sc_iobase;
1240
1241 /* timeouts are in msec, so we loop in 1000 usec cycles */
1242 while (count) {
1243 /*
1244 * If we had interrupts enabled, would we
1245 * have got an interrupt?
1246 */
1247 if (inb(iobase + WDS_STAT) & WDSS_IRQ)
1248 wdsintr(sc);
1249 if (scb->flags & SCB_DONE)
1250 return 0;
1251 delay(1000); /* only happens in boot so ok */
1252 count--;
1253 }
1254 return 1;
1255 }
1256
1257 void
1258 wds_timeout(arg)
1259 void *arg;
1260 {
1261 struct wds_scb *scb = arg;
1262 struct scsi_xfer *xs = scb->xs;
1263 struct scsi_link *sc_link = xs->sc_link;
1264 struct wds_softc *sc = sc_link->adapter_softc;
1265 int s;
1266
1267 sc_print_addr(sc_link);
1268 printf("timed out");
1269
1270 s = splbio();
1271
1272 #ifdef WDSDIAG
1273 /*
1274 * If The scb's mbx is not free, then the board has gone south?
1275 */
1276 wds_collect_mbo(sc);
1277 if (scb->flags & SCB_SENDING) {
1278 printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1279 Debugger();
1280 }
1281 #endif
1282
1283 /*
1284 * If it has been through before, then
1285 * a previous abort has failed, don't
1286 * try abort again
1287 */
1288 if (scb->flags & SCB_ABORT) {
1289 /* abort timed out */
1290 printf(" AGAIN\n");
1291 /* XXX Must reset! */
1292 } else {
1293 /* abort the operation that has timed out */
1294 printf("\n");
1295 scb->xs->error = XS_TIMEOUT;
1296 scb->timeout = WDS_ABORT_TIMEOUT;
1297 scb->flags |= SCB_ABORT;
1298 wds_queue_scb(sc, scb);
1299 }
1300
1301 splx(s);
1302 }
1303