btl.c revision 1.3 1 /* $NetBSD: btl.c,v 1.3 2000/02/22 11:26:01 soda Exp $ */
2
3 #undef BTDIAG
4 #define integrate
5
6 #define notyet /* XXX - #undef this, if this driver does actually work */
7
8 /*
9 * Copyright (c) 1994, 1996 Charles M. Hannum. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by Charles M. Hannum.
22 * 4. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 /*
38 * Originally written by Julian Elischer (julian (at) tfs.com)
39 * for TRW Financial Systems for use under the MACH(2.5) operating system.
40 *
41 * TRW Financial Systems, in accordance with their agreement with Carnegie
42 * Mellon University, makes this software available to CMU to distribute
43 * or use in any manner that they see fit as long as this message is kept with
44 * the software. For this reason TFS also grants any other persons or
45 * organisations permission to use or modify this software.
46 *
47 * TFS supplies this software to be publicly redistributed
48 * on the understanding that TFS is not responsible for the correct
49 * functioning of this software in any circumstances.
50 */
51
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/errno.h>
57 #include <sys/malloc.h>
58 #include <sys/ioctl.h>
59 #include <sys/device.h>
60 #include <sys/buf.h>
61 #include <sys/proc.h>
62 #include <sys/user.h>
63
64 #include <machine/intr.h>
65 #include <machine/pio.h>
66
67 #include <arc/dti/desktech.h>
68
69 #include <dev/scsipi/scsi_all.h>
70 #include <dev/scsipi/scsipi_all.h>
71 #include <dev/scsipi/scsiconf.h>
72
73 #include <dev/isa/isavar.h>
74 #include <arc/dti/btlreg.h>
75 #include <arc/arc/arctype.h> /* XXX for cpu types */
76
77 #ifndef DDB
78 #define Debugger() panic("should call debugger here (bt742a.c)")
79 #endif /* ! DDB */
80
81 /*
82 * Mail box defs etc.
83 * these could be bigger but we need the bt_softc to fit on a single page..
84 */
85 #define BT_MBX_SIZE 32 /* mail box size (MAX 255 MBxs) */
86 /* don't need that many really */
87 #define BT_CCB_MAX 32 /* store up to 32 CCBs at one time */
88 #define CCB_HASH_SIZE 32 /* hash table size for phystokv */
89 #define CCB_HASH_SHIFT 9
90 #define CCB_HASH(x) ((((long)(x))>>CCB_HASH_SHIFT) & (CCB_HASH_SIZE - 1))
91
92 #define bt_nextmbx(wmb, mbx, mbio) \
93 if ((wmb) == &(mbx)->mbio[BT_MBX_SIZE - 1]) \
94 (wmb) = &(mbx)->mbio[0]; \
95 else \
96 (wmb)++;
97
98 struct bt_mbx {
99 struct bt_mbx_out mbo[BT_MBX_SIZE];
100 struct bt_mbx_in mbi[BT_MBX_SIZE];
101 struct bt_mbx_out *cmbo; /* Collection Mail Box out */
102 struct bt_mbx_out *tmbo; /* Target Mail Box out */
103 struct bt_mbx_in *tmbi; /* Target Mail Box in */
104 };
105
106 #define KVTOPHYS(x) ((cputype == DESKSTATION_TYNE) ? \
107 (((int)(x) & 0x7fffff) | 0x800000) : ((int)(x)))
108 #define PHYSTOKV(x) ((cputype == DESKSTATION_TYNE) ? \
109 (((int)(x) & 0x7fffff) | TYNE_V_BOUNCE) : ((int)(x)))
110
111 struct bt_softc {
112 struct device sc_dev;
113 void *sc_ih;
114
115 int sc_iobase;
116 int sc_irq, sc_drq;
117
118 char sc_model[7],
119 sc_firmware[6];
120
121 struct bt_mbx *sc_mbx; /* all our mailboxes */
122 #define wmbx (sc->sc_mbx)
123 struct bt_ccb *sc_ccbhash[CCB_HASH_SIZE];
124 TAILQ_HEAD(, bt_ccb) sc_free_ccb, sc_waiting_ccb;
125 TAILQ_HEAD(, bt_buf) sc_free_buf;
126 int sc_numccbs, sc_mbofull;
127 int sc_numbufs;
128 int sc_scsi_dev; /* adapters scsi id */
129 struct scsipi_link sc_link; /* prototype for devs */
130 struct scsipi_adapter sc_adapter;
131 };
132
133 #ifdef BTDEBUG
134 int bt_debug = 0;
135 #endif /* BTDEBUG */
136
137 int bt_cmd __P((int, struct bt_softc *, int, u_char *, int, u_char *));
138 integrate void bt_finish_ccbs __P((struct bt_softc *));
139 int btintr __P((void *));
140 integrate void bt_reset_ccb __P((struct bt_softc *, struct bt_ccb *));
141 void bt_free_ccb __P((struct bt_softc *, struct bt_ccb *));
142 integrate void bt_init_ccb __P((struct bt_softc *, struct bt_ccb *));
143 struct bt_ccb *bt_get_ccb __P((struct bt_softc *, int));
144 struct bt_ccb *bt_ccb_phys_kv __P((struct bt_softc *, u_long));
145 void bt_queue_ccb __P((struct bt_softc *, struct bt_ccb *));
146 void bt_collect_mbo __P((struct bt_softc *));
147 void bt_start_ccbs __P((struct bt_softc *));
148 void bt_done __P((struct bt_softc *, struct bt_ccb *));
149 int bt_find __P((struct isa_attach_args *, struct bt_softc *));
150 void bt_init __P((struct bt_softc *));
151 void bt_inquire_setup_information __P((struct bt_softc *));
152 void btminphys __P((struct buf *));
153 int bt_scsi_cmd __P((struct scsipi_xfer *));
154 int bt_poll __P((struct bt_softc *, struct scsipi_xfer *, int));
155 void bt_timeout __P((void *arg));
156 void bt_free_buf __P((struct bt_softc *, struct bt_buf *));
157 struct bt_buf * bt_get_buf __P((struct bt_softc *, int));
158
159 /* XXX static buffer as a kludge. DMA isn't cache coherent on the rpc44, so
160 * we always use uncached buffers for DMA. */
161 static char rpc44_buffer[ TYNE_S_BOUNCE ];
162
163 /* the below structure is so we have a default dev struct for out link struct */
164 struct scsipi_device bt_dev = {
165 NULL, /* Use default error handler */
166 NULL, /* have a queue, served by this */
167 NULL, /* have no async handler */
168 NULL, /* Use default 'done' routine */
169 };
170
171 int btprobe __P((struct device *, struct cfdata *, void *));
172 void btattach __P((struct device *, struct device *, void *));
173 int btprint __P((void *, const char *));
174
175 struct cfattach btl_ca = {
176 sizeof(struct bt_softc), btprobe, btattach
177 };
178
179 #define BT_RESET_TIMEOUT 2000 /* time to wait for reset (mSec) */
180 #define BT_ABORT_TIMEOUT 2000 /* time to wait for abort (mSec) */
181
182 /*
183 * bt_cmd(iobase, sc, icnt, ibuf, ocnt, obuf)
184 *
185 * Activate Adapter command
186 * icnt: number of args (outbound bytes including opcode)
187 * ibuf: argument buffer
188 * ocnt: number of expected returned bytes
189 * obuf: result buffer
190 * wait: number of seconds to wait for response
191 *
192 * Performs an adapter command through the ports. Not to be confused with a
193 * scsi command, which is read in via the dma; one of the adapter commands
194 * tells it to read in a scsi command.
195 */
196 int
197 bt_cmd(iobase, sc, icnt, ibuf, ocnt, obuf)
198 int iobase;
199 struct bt_softc *sc;
200 int icnt, ocnt;
201 u_char *ibuf, *obuf;
202 {
203 const char *name;
204 register int i;
205 int wait;
206 u_char sts;
207 u_char opcode = ibuf[0];
208
209 if (sc != NULL)
210 name = sc->sc_dev.dv_xname;
211 else
212 name = "(bt probe)";
213
214 /*
215 * Calculate a reasonable timeout for the command.
216 */
217 switch (opcode) {
218 case BT_INQUIRE_DEVICES:
219 wait = 15 * 20000;
220 break;
221 default:
222 wait = 1 * 20000;
223 break;
224 }
225
226 /*
227 * Wait for the adapter to go idle, unless it's one of
228 * the commands which don't need this
229 */
230 if (opcode != BT_MBO_INTR_EN) {
231 for (i = 20000; i; i--) { /* 1 sec? */
232 sts = isa_inb(iobase + BT_STAT_PORT);
233 if (sts & BT_STAT_IDLE)
234 break;
235 delay(50);
236 }
237 if (!i) {
238 printf("%s: bt_cmd, host not idle(0x%x)\n",
239 name, sts);
240 return ENXIO;
241 }
242 }
243 /*
244 * Now that it is idle, if we expect output, preflush the
245 * queue feeding to us.
246 */
247 if (ocnt) {
248 while ((isa_inb(iobase + BT_STAT_PORT)) & BT_STAT_DF)
249 isa_inb(iobase + BT_DATA_PORT);
250 }
251 /*
252 * Output the command and the number of arguments given
253 * for each byte, first check the port is empty.
254 */
255 while (icnt--) {
256 for (i = wait; i; i--) {
257 sts = isa_inb(iobase + BT_STAT_PORT);
258 if (!(sts & BT_STAT_CDF))
259 break;
260 delay(50);
261 }
262 if (!i) {
263 if (opcode != BT_INQUIRE_REVISION &&
264 opcode != BT_INQUIRE_REVISION_3)
265 printf("%s: bt_cmd, cmd/data port full\n", name);
266 isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_SRST);
267 return ENXIO;
268 }
269 isa_outb(iobase + BT_CMD_PORT, *ibuf++);
270 }
271 /*
272 * If we expect input, loop that many times, each time,
273 * looking for the data register to have valid data
274 */
275 while (ocnt--) {
276 for (i = wait; i; i--) {
277 sts = isa_inb(iobase + BT_STAT_PORT);
278 if (sts & BT_STAT_DF)
279 break;
280 delay(50);
281 }
282 if (!i) {
283 if (opcode != BT_INQUIRE_REVISION &&
284 opcode != BT_INQUIRE_REVISION_3)
285 printf("%s: bt_cmd, cmd/data port empty %d\n",
286 name, ocnt);
287 isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_SRST);
288 return ENXIO;
289 }
290 *obuf++ = isa_inb(iobase + BT_DATA_PORT);
291 }
292 /*
293 * Wait for the board to report a finished instruction.
294 * We may get an extra interrupt for the HACC signal, but this is
295 * unimportant.
296 */
297 if (opcode != BT_MBO_INTR_EN) {
298 for (i = 20000; i; i--) { /* 1 sec? */
299 sts = isa_inb(iobase + BT_INTR_PORT);
300 /* XXX Need to save this in the interrupt handler? */
301 if (sts & BT_INTR_HACC)
302 break;
303 delay(50);
304 }
305 if (!i) {
306 printf("%s: bt_cmd, host not finished(0x%x)\n",
307 name, sts);
308 return ENXIO;
309 }
310 }
311 isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_IRST);
312 return 0;
313 }
314
315 /*
316 * Check if the device can be found at the port given
317 * and if so, set it up ready for further work
318 * as an argument, takes the isa_device structure from
319 * autoconf.c
320 */
321 int
322 btprobe(parent, match, aux)
323 struct device *parent;
324 struct cfdata *match;
325 void *aux;
326 {
327 register struct isa_attach_args *ia = aux;
328
329 #ifdef NEWCONFIG
330 if (ia->ia_iobase == IOBASEUNK)
331 return 0;
332 #endif
333
334 /* See if there is a unit at this location. */
335 if (bt_find(ia, NULL) != 0)
336 return 0;
337
338 ia->ia_msize = 0;
339 ia->ia_iosize = 4;
340 /* IRQ and DRQ set by bt_find(). */
341 return 1;
342 }
343
344 /*
345 * Attach all the sub-devices we can find
346 */
347 void
348 btattach(parent, self, aux)
349 struct device *parent, *self;
350 void *aux;
351 {
352 struct isa_attach_args *ia = aux;
353 struct bt_softc *sc = (void *)self;
354 struct bt_ccb *ccb;
355 struct bt_buf *buf;
356 u_int bouncearea;
357 u_int bouncebase;
358 u_int bouncesize;
359
360 if (bt_find(ia, sc) != 0)
361 panic("btattach: bt_find of %s failed", self->dv_xname);
362 sc->sc_iobase = ia->ia_iobase;
363
364 /*
365 * create mbox area
366 */
367 if (cputype == DESKSTATION_TYNE) {
368 bouncebase = TYNE_V_BOUNCE;
369 bouncesize = TYNE_S_BOUNCE;
370 } else {
371 bouncesize = TYNE_S_BOUNCE; /* Good enough? XXX */
372 /* bouncebase = (u_int) malloc( bouncesize, M_DEVBUF, M_NOWAIT);*/
373 bouncebase = (u_int) rpc44_buffer | 0xa0000000;
374 }
375 bouncearea = bouncebase + sizeof(struct bt_mbx);
376 sc->sc_mbx = (struct bt_mbx *)bouncebase;
377
378 bt_inquire_setup_information(sc);
379 bt_init(sc);
380 TAILQ_INIT(&sc->sc_free_ccb);
381 TAILQ_INIT(&sc->sc_free_buf);
382 TAILQ_INIT(&sc->sc_waiting_ccb);
383
384 /*
385 * fill up with ccb's
386 */
387 while (sc->sc_numccbs < BT_CCB_MAX) {
388 ccb = (struct bt_ccb *)bouncearea;
389 bouncearea += sizeof(struct bt_ccb);
390 bt_init_ccb(sc, ccb);
391 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
392 sc->sc_numccbs++;
393 }
394 /*
395 * fill up with bufs's
396 */
397 while ((bouncearea + sizeof(struct bt_buf)) < bouncebase + bouncesize) {
398 buf = (struct bt_buf *)bouncearea;
399 bouncearea += sizeof(struct bt_buf);
400 TAILQ_INSERT_HEAD(&sc->sc_free_buf, buf, chain);
401 sc->sc_numbufs++;
402 }
403 /*
404 * Fill in the adapter.
405 */
406 sc->sc_adapter.scsipi_cmd = bt_scsi_cmd;
407 sc->sc_adapter.scsipi_minphys = btminphys;
408 /*
409 * fill in the prototype scsipi_link.
410 */
411 sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
412 sc->sc_link.adapter_softc = sc;
413 sc->sc_link.scsipi_scsi.adapter_target = sc->sc_scsi_dev;
414 sc->sc_link.adapter = &sc->sc_adapter;
415 sc->sc_link.device = &bt_dev;
416 sc->sc_link.openings = 1;
417 sc->sc_link.scsipi_scsi.max_target = 7;
418 sc->sc_link.scsipi_scsi.max_lun = 7;
419 sc->sc_link.type = BUS_SCSI;
420
421 sc->sc_ih = isa_intr_establish(ia->ia_ic, sc->sc_irq, IST_EDGE,
422 IPL_BIO, btintr, sc);
423
424 /*
425 * ask the adapter what subunits are present
426 */
427 config_found(self, &sc->sc_link, scsiprint);
428 }
429
430 integrate void
431 bt_finish_ccbs(sc)
432 struct bt_softc *sc;
433 {
434 struct bt_mbx_in *wmbi;
435 struct bt_ccb *ccb;
436 int i;
437
438 wmbi = wmbx->tmbi;
439
440 if (wmbi->stat == BT_MBI_FREE) {
441 for (i = 0; i < BT_MBX_SIZE; i++) {
442 if (wmbi->stat != BT_MBI_FREE) {
443 printf("%s: mbi not in round-robin order\n",
444 sc->sc_dev.dv_xname);
445 goto AGAIN;
446 }
447 bt_nextmbx(wmbi, wmbx, mbi);
448 }
449 #ifdef BTDIAGnot
450 printf("%s: mbi interrupt with no full mailboxes\n",
451 sc->sc_dev.dv_xname);
452 #endif
453 return;
454 }
455
456 AGAIN:
457 do {
458 ccb = bt_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
459 if (!ccb) {
460 printf("%s: bad mbi ccb pointer; skipping\n",
461 sc->sc_dev.dv_xname);
462 goto next;
463 }
464
465 #ifdef BTDEBUG
466 if (bt_debug) {
467 u_char *cp = (u_char *) &ccb->scsi_cmd;
468 printf("op=%x %x %x %x %x %x\n",
469 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
470 printf("stat %x for mbi addr = 0x%08x, ",
471 wmbi->stat, wmbi);
472 printf("ccb addr = 0x%x\n", ccb);
473 }
474 #endif /* BTDEBUG */
475
476 switch (wmbi->stat) {
477 case BT_MBI_OK:
478 case BT_MBI_ERROR:
479 if ((ccb->flags & CCB_ABORT) != 0) {
480 /*
481 * If we already started an abort, wait for it
482 * to complete before clearing the CCB. We
483 * could instead just clear CCB_SENDING, but
484 * what if the mailbox was already received?
485 * The worst that happens here is that we clear
486 * the CCB a bit later than we need to. BFD.
487 */
488 goto next;
489 }
490 break;
491
492 case BT_MBI_ABORT:
493 case BT_MBI_UNKNOWN:
494 /*
495 * Even if the CCB wasn't found, we clear it anyway.
496 * See preceeding comment.
497 */
498 break;
499
500 default:
501 printf("%s: bad mbi status %02x; skipping\n",
502 sc->sc_dev.dv_xname, wmbi->stat);
503 goto next;
504 }
505
506 untimeout(bt_timeout, ccb);
507 bt_done(sc, ccb);
508
509 next:
510 wmbi->stat = BT_MBI_FREE;
511 bt_nextmbx(wmbi, wmbx, mbi);
512 } while (wmbi->stat != BT_MBI_FREE);
513
514 wmbx->tmbi = wmbi;
515 }
516
517 /*
518 * Catch an interrupt from the adaptor
519 */
520 int
521 btintr(arg)
522 void *arg;
523 {
524 struct bt_softc *sc = arg;
525 int iobase = sc->sc_iobase;
526 u_char sts;
527
528 #ifdef BTDEBUG
529 printf("%s: btintr ", sc->sc_dev.dv_xname);
530 #endif /* BTDEBUG */
531
532 /*
533 * First acknowlege the interrupt, Then if it's not telling about
534 * a completed operation just return.
535 */
536 sts = isa_inb(iobase + BT_INTR_PORT);
537 if ((sts & BT_INTR_ANYINTR) == 0)
538 return 0;
539 isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_IRST);
540
541 #ifdef BTDIAG
542 /* Make sure we clear CCB_SENDING before finishing a CCB. */
543 bt_collect_mbo(sc);
544 #endif
545
546 /* Mail box out empty? */
547 if (sts & BT_INTR_MBOA) {
548 struct bt_toggle toggle;
549
550 toggle.cmd.opcode = BT_MBO_INTR_EN;
551 toggle.cmd.enable = 0;
552 bt_cmd(iobase, sc, sizeof(toggle.cmd), (u_char *)&toggle.cmd, 0,
553 (u_char *)0);
554 bt_start_ccbs(sc);
555 }
556
557 /* Mail box in full? */
558 if (sts & BT_INTR_MBIF)
559 bt_finish_ccbs(sc);
560
561 return 1;
562 }
563
564 integrate void
565 bt_reset_ccb(sc, ccb)
566 struct bt_softc *sc;
567 struct bt_ccb *ccb;
568 {
569
570 ccb->flags = 0;
571 }
572
573 /*
574 * A ccb is put onto the free list.
575 */
576 void
577 bt_free_ccb(sc, ccb)
578 struct bt_softc *sc;
579 struct bt_ccb *ccb;
580 {
581 int s;
582
583 s = splbio();
584
585 bt_reset_ccb(sc, ccb);
586 TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
587
588 /*
589 * If there were none, wake anybody waiting for one to come free,
590 * starting with queued entries.
591 */
592 if (ccb->chain.tqe_next == 0)
593 wakeup(&sc->sc_free_ccb);
594
595 splx(s);
596 }
597
598 /*
599 * A buf is put onto the free list.
600 */
601 void
602 bt_free_buf(sc, buf)
603 struct bt_softc *sc;
604 struct bt_buf *buf;
605 {
606 int s;
607
608 s = splbio();
609
610 TAILQ_INSERT_HEAD(&sc->sc_free_buf, buf, chain);
611 sc->sc_numbufs++;
612
613 /*
614 * If there were none, wake anybody waiting for one to come free,
615 * starting with queued entries.
616 */
617 if (buf->chain.tqe_next == 0)
618 wakeup(&sc->sc_free_buf);
619
620 splx(s);
621 }
622
623 integrate void
624 bt_init_ccb(sc, ccb)
625 struct bt_softc *sc;
626 struct bt_ccb *ccb;
627 {
628 int hashnum;
629
630 bzero(ccb, sizeof(struct bt_ccb));
631 /*
632 * put in the phystokv hash table
633 * Never gets taken out.
634 */
635 ccb->hashkey = KVTOPHYS(ccb);
636 hashnum = CCB_HASH(ccb->hashkey);
637 ccb->nexthash = sc->sc_ccbhash[hashnum];
638 sc->sc_ccbhash[hashnum] = ccb;
639 bt_reset_ccb(sc, ccb);
640 }
641
642 /*
643 * Get a free ccb
644 *
645 * If there are none, either return an error or sleep.
646 */
647 struct bt_ccb *
648 bt_get_ccb(sc, nosleep)
649 struct bt_softc *sc;
650 int nosleep;
651 {
652 struct bt_ccb *ccb;
653 int s;
654
655 s = splbio();
656
657 /*
658 * If we can and have to, sleep waiting for one to come free.
659 */
660 for (;;) {
661 ccb = sc->sc_free_ccb.tqh_first;
662 if (ccb) {
663 TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
664 break;
665 }
666 if (nosleep)
667 goto out;
668 tsleep(&sc->sc_free_ccb, PRIBIO, "btccb", 0);
669 }
670
671 ccb->flags |= CCB_ALLOC;
672
673 out:
674 splx(s);
675 return ccb;
676 }
677
678 /*
679 * Get a free buf
680 *
681 * If there are none, either return an error or sleep.
682 */
683 struct bt_buf *
684 bt_get_buf(sc, nosleep)
685 struct bt_softc *sc;
686 int nosleep;
687 {
688 struct bt_buf *buf;
689 int s;
690
691 s = splbio();
692
693 /*
694 * If we can and have to, sleep waiting for one to come free.
695 */
696 for (;;) {
697 buf = sc->sc_free_buf.tqh_first;
698 if (buf) {
699 TAILQ_REMOVE(&sc->sc_free_buf, buf, chain);
700 sc->sc_numbufs--;
701 break;
702 }
703 if (nosleep)
704 goto out;
705 tsleep(&sc->sc_free_buf, PRIBIO, "btbuf", 0);
706 }
707
708 out:
709 splx(s);
710 return buf;
711 }
712
713 /*
714 * Given a physical address, find the ccb that it corresponds to.
715 */
716 struct bt_ccb *
717 bt_ccb_phys_kv(sc, ccb_phys)
718 struct bt_softc *sc;
719 u_long ccb_phys;
720 {
721 int hashnum = CCB_HASH(ccb_phys);
722 struct bt_ccb *ccb = sc->sc_ccbhash[hashnum];
723
724 while (ccb) {
725 if (ccb->hashkey == ccb_phys)
726 break;
727 ccb = ccb->nexthash;
728 }
729 return ccb;
730 }
731
732 /*
733 * Queue a CCB to be sent to the controller, and send it if possible.
734 */
735 void
736 bt_queue_ccb(sc, ccb)
737 struct bt_softc *sc;
738 struct bt_ccb *ccb;
739 {
740
741 TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
742 bt_start_ccbs(sc);
743 }
744
745 /*
746 * Garbage collect mailboxes that are no longer in use.
747 */
748 void
749 bt_collect_mbo(sc)
750 struct bt_softc *sc;
751 {
752 struct bt_mbx_out *wmbo; /* Mail Box Out pointer */
753
754 wmbo = wmbx->cmbo;
755
756 while (sc->sc_mbofull > 0) {
757 if (wmbo->cmd != BT_MBO_FREE)
758 break;
759
760 #ifdef BTDIAG
761 ccb = bt_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
762 ccb->flags &= ~CCB_SENDING;
763 #endif
764
765 --sc->sc_mbofull;
766 bt_nextmbx(wmbo, wmbx, mbo);
767 }
768
769 wmbx->cmbo = wmbo;
770 }
771
772 /*
773 * Send as many CCBs as we have empty mailboxes for.
774 */
775 void
776 bt_start_ccbs(sc)
777 struct bt_softc *sc;
778 {
779 int iobase = sc->sc_iobase;
780 struct bt_mbx_out *wmbo; /* Mail Box Out pointer */
781 struct bt_ccb *ccb;
782
783 wmbo = wmbx->tmbo;
784
785 while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
786 if (sc->sc_mbofull >= BT_MBX_SIZE) {
787 bt_collect_mbo(sc);
788 if (sc->sc_mbofull >= BT_MBX_SIZE) {
789 struct bt_toggle toggle;
790
791 toggle.cmd.opcode = BT_MBO_INTR_EN;
792 toggle.cmd.enable = 1;
793 bt_cmd(iobase, sc, sizeof(toggle.cmd),
794 (u_char *)&toggle.cmd, 0, (u_char *)0);
795 break;
796 }
797 }
798
799 TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
800 #ifdef BTDIAG
801 ccb->flags |= CCB_SENDING;
802 #endif
803
804 /* Link ccb to mbo. */
805 ltophys(KVTOPHYS(ccb), wmbo->ccb_addr);
806 if (ccb->flags & CCB_ABORT)
807 wmbo->cmd = BT_MBO_ABORT;
808 else
809 wmbo->cmd = BT_MBO_START;
810
811 /* Tell the card to poll immediately. */
812 isa_outb(iobase + BT_CMD_PORT, BT_START_SCSI);
813
814 if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
815 timeout(bt_timeout, ccb, (ccb->timeout * hz) / 1000);
816
817 ++sc->sc_mbofull;
818 bt_nextmbx(wmbo, wmbx, mbo);
819 }
820
821 wmbx->tmbo = wmbo;
822 }
823
824 /*
825 * We have a ccb which has been processed by the
826 * adaptor, now we look to see how the operation
827 * went. Wake up the owner if waiting
828 */
829 void
830 bt_done(sc, ccb)
831 struct bt_softc *sc;
832 struct bt_ccb *ccb;
833 {
834 struct scsipi_sense_data *s1, *s2;
835 struct scsipi_xfer *xs = ccb->xs;
836
837 u_long thiskv, thisbounce;
838 int bytes_this_page, datalen;
839 struct bt_scat_gath *sg;
840 int seg;
841
842 SC_DEBUG(xs->sc_link, SDEV_DB2, ("bt_done\n"));
843 /*
844 * Otherwise, put the results of the operation
845 * into the xfer and call whoever started it
846 */
847 #ifdef BTDIAG
848 if (ccb->flags & CCB_SENDING) {
849 printf("%s: exiting ccb still in transit!\n", sc->sc_dev.dv_xname);
850 Debugger();
851 return;
852 }
853 #endif
854 if ((ccb->flags & CCB_ALLOC) == 0) {
855 printf("%s: exiting ccb not allocated!\n", sc->sc_dev.dv_xname);
856 Debugger();
857 return;
858 }
859 if (xs->error == XS_NOERROR) {
860 if (ccb->host_stat != BT_OK) {
861 switch (ccb->host_stat) {
862 case BT_SEL_TIMEOUT: /* No response */
863 xs->error = XS_SELTIMEOUT;
864 break;
865 default: /* Other scsi protocol messes */
866 printf("%s: host_stat %x\n",
867 sc->sc_dev.dv_xname, ccb->host_stat);
868 xs->error = XS_DRIVER_STUFFUP;
869 break;
870 }
871 } else if (ccb->target_stat != SCSI_OK) {
872 switch (ccb->target_stat) {
873 case SCSI_CHECK:
874 s1 = &ccb->scsi_sense;
875 s2 = &xs->sense.scsi_sense;
876 *s2 = *s1;
877 xs->error = XS_SENSE;
878 break;
879 case SCSI_BUSY:
880 xs->error = XS_BUSY;
881 break;
882 default:
883 printf("%s: target_stat %x\n",
884 sc->sc_dev.dv_xname, ccb->target_stat);
885 xs->error = XS_DRIVER_STUFFUP;
886 break;
887 }
888 } else
889 xs->resid = 0;
890 }
891
892 if((datalen = xs->datalen) != 0) {
893 thiskv = (int)xs->data;
894 sg = ccb->scat_gath;
895 seg = phystol(ccb->data_length) / sizeof(struct bt_scat_gath);
896
897 while (seg) {
898 thisbounce = PHYSTOKV(phystol(sg->seg_addr));
899 bytes_this_page = phystol(sg->seg_len);
900 if(xs->xs_control & XS_CTL_DATA_IN) {
901 bcopy((void *)thisbounce, (void *)thiskv, bytes_this_page);
902 }
903 bt_free_buf(sc, (struct bt_buf *)thisbounce);
904 thiskv += bytes_this_page;
905 datalen -= bytes_this_page;
906
907 sg++;
908 seg--;
909 }
910 }
911
912 bt_free_ccb(sc, ccb);
913 xs->xs_status |= XS_STS_DONE;
914 scsipi_done(xs);
915 }
916
917 /*
918 * Find the board and find it's irq/drq
919 */
920 int
921 bt_find(ia, sc)
922 struct isa_attach_args *ia;
923 struct bt_softc *sc;
924 {
925 int iobase = ia->ia_iobase;
926 int i;
927 u_char sts;
928 struct bt_extended_inquire inquire;
929 struct bt_config config;
930 int irq, drq;
931
932 #ifndef notyet
933 /* Check something is at the ports we need to access */
934 sts = isa_inb(iobase + BHA_STAT_PORT);
935 if (sts == 0xFF)
936 return (0);
937 #endif
938
939 /*
940 * reset board, If it doesn't respond, assume
941 * that it's not there.. good for the probe
942 */
943
944 isa_outb(iobase + BT_CTRL_PORT, BT_CTRL_HRST | BT_CTRL_SRST);
945
946 delay(100);
947 for (i = BT_RESET_TIMEOUT; i; i--) {
948 sts = isa_inb(iobase + BT_STAT_PORT);
949 if (sts == (BT_STAT_IDLE | BT_STAT_INIT))
950 break;
951 delay(1000);
952 }
953 if (!i) {
954 #ifdef BTDEBUG
955 if (bt_debug)
956 printf("bt_find: No answer from buslogic board\n");
957 #endif /* BTDEBUG */
958 return 1;
959 }
960
961 #ifndef notyet
962 /*
963 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
964 * interface. The native bha interface is not compatible with
965 * an aha. 1542. We need to ensure that we never match an
966 * Adaptec 1542. We must also avoid sending Adaptec-compatible
967 * commands to a real bha, lest it go into 1542 emulation mode.
968 * (On an indirect bus like ISA, we should always probe for BusLogic
969 * interfaces before Adaptec interfaces).
970 */
971
972 /*
973 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
974 * for an extended-geometry register. The 1542[AB] don't have one.
975 */
976 sts = isa_inb(iobase + BT_EXTGEOM_PORT);
977 if (sts == 0xFF)
978 return (0);
979 #endif /* notyet */
980
981 /*
982 * Check that we actually know how to use this board.
983 */
984 delay(1000);
985 bzero(&inquire, sizeof inquire);
986 inquire.cmd.opcode = BT_INQUIRE_EXTENDED;
987 inquire.cmd.len = sizeof(inquire.reply);
988 i = bt_cmd(iobase, sc, sizeof(inquire.cmd), (u_char *)&inquire.cmd,
989 sizeof(inquire.reply), (u_char *)&inquire.reply);
990
991 #ifndef notyet
992 /*
993 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
994 * have the extended-geometry register and also respond to
995 * BHA_INQUIRE_EXTENDED. Make sure we never match such cards,
996 * by checking the size of the reply is what a BusLogic card returns.
997 */
998 if (i) { /* XXX - this doesn't really check the size. ??? see bha.c */
999 #ifdef BTDEBUG
1000 printf("bt_find: board returned %d instead of %d to %s\n",
1001 i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
1002 #endif
1003 return (0);
1004 }
1005
1006 /* OK, we know we've found a buslogic adaptor. */
1007 #endif /* notyet */
1008
1009 switch (inquire.reply.bus_type) {
1010 case BT_BUS_TYPE_24BIT:
1011 case BT_BUS_TYPE_32BIT:
1012 break;
1013 case BT_BUS_TYPE_MCA:
1014 /* We don't grok MicroChannel (yet). */
1015 return 1;
1016 default:
1017 printf("bt_find: illegal bus type %c\n", inquire.reply.bus_type);
1018 return 1;
1019 }
1020
1021 /*
1022 * Assume we have a board at this stage setup dma channel from
1023 * jumpers and save int level
1024 */
1025 delay(1000);
1026 config.cmd.opcode = BT_INQUIRE_CONFIG;
1027 bt_cmd(iobase, sc, sizeof(config.cmd), (u_char *)&config.cmd,
1028 sizeof(config.reply), (u_char *)&config.reply);
1029 switch (config.reply.chan) {
1030 case EISADMA:
1031 drq = DRQUNK;
1032 break;
1033 case CHAN0:
1034 drq = 0;
1035 break;
1036 case CHAN5:
1037 drq = 5;
1038 break;
1039 case CHAN6:
1040 drq = 6;
1041 break;
1042 case CHAN7:
1043 drq = 7;
1044 break;
1045 default:
1046 printf("bt_find: illegal drq setting %x\n", config.reply.chan);
1047 return 1;
1048 }
1049
1050 switch (config.reply.intr) {
1051 case INT9:
1052 irq = 9;
1053 break;
1054 case INT10:
1055 irq = 10;
1056 break;
1057 case INT11:
1058 irq = 11;
1059 break;
1060 case INT12:
1061 irq = 12;
1062 break;
1063 case INT14:
1064 irq = 14;
1065 break;
1066 case INT15:
1067 irq = 15;
1068 break;
1069 default:
1070 printf("bt_find: illegal irq setting %x\n", config.reply.intr);
1071 return 1;
1072 }
1073
1074 if (sc != NULL) {
1075 /* who are we on the scsi bus? */
1076 sc->sc_scsi_dev = config.reply.scsi_dev;
1077
1078 sc->sc_iobase = iobase;
1079 sc->sc_irq = irq;
1080 sc->sc_drq = drq;
1081 } else {
1082 if (ia->ia_irq == IRQUNK)
1083 ia->ia_irq = irq;
1084 else if (ia->ia_irq != irq)
1085 return 1;
1086 if (ia->ia_drq == DRQUNK)
1087 ia->ia_drq = drq;
1088 else if (ia->ia_drq != drq)
1089 return 1;
1090 }
1091
1092 return 0;
1093 }
1094
1095 /*
1096 * Start the board, ready for normal operation
1097 */
1098 void
1099 bt_init(sc)
1100 struct bt_softc *sc;
1101 {
1102 int iobase = sc->sc_iobase;
1103 struct bt_devices devices;
1104 struct bt_setup setup;
1105 struct bt_mailbox mailbox;
1106 struct bt_period period;
1107 int i;
1108
1109 /* Enable round-robin scheme - appeared at firmware rev. 3.31. */
1110 if (strcmp(sc->sc_firmware, "3.31") >= 0) {
1111 struct bt_toggle toggle;
1112
1113 toggle.cmd.opcode = BT_ROUND_ROBIN;
1114 toggle.cmd.enable = 1;
1115 bt_cmd(iobase, sc, sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1116 0, (u_char *)0);
1117 }
1118
1119 /* Inquire Installed Devices (to force synchronous negotiation). */
1120 devices.cmd.opcode = BT_INQUIRE_DEVICES;
1121 bt_cmd(iobase, sc, sizeof(devices.cmd), (u_char *)&devices.cmd,
1122 sizeof(devices.reply), (u_char *)&devices.reply);
1123
1124 /* Obtain setup information from. */
1125 setup.cmd.opcode = BT_INQUIRE_SETUP;
1126 setup.cmd.len = sizeof(setup.reply);
1127 bt_cmd(iobase, sc, sizeof(setup.cmd), (u_char *)&setup.cmd,
1128 sizeof(setup.reply), (u_char *)&setup.reply);
1129
1130 printf("%s: %s, %s\n",
1131 sc->sc_dev.dv_xname,
1132 setup.reply.sync_neg ? "sync" : "async",
1133 setup.reply.parity ? "parity" : "no parity");
1134
1135 for (i = 0; i < 8; i++)
1136 period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
1137
1138 if (sc->sc_firmware[0] >= '3') {
1139 period.cmd.opcode = BT_INQUIRE_PERIOD;
1140 period.cmd.len = sizeof(period.reply);
1141 bt_cmd(iobase, sc, sizeof(period.cmd), (u_char *)&period.cmd,
1142 sizeof(period.reply), (u_char *)&period.reply);
1143 }
1144
1145 for (i = 0; i < 8; i++) {
1146 if (!setup.reply.sync[i].valid ||
1147 (!setup.reply.sync[i].offset && !setup.reply.sync[i].period))
1148 continue;
1149 printf("%s targ %d: sync, offset %d, period %dnsec\n",
1150 sc->sc_dev.dv_xname, i,
1151 setup.reply.sync[i].offset, period.reply.period[i] * 10);
1152 }
1153
1154 /*
1155 * Set up initial mail box for round-robin operation.
1156 */
1157 for (i = 0; i < BT_MBX_SIZE; i++) {
1158 wmbx->mbo[i].cmd = BT_MBO_FREE;
1159 wmbx->mbi[i].stat = BT_MBI_FREE;
1160 }
1161 wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1162 wmbx->tmbi = &wmbx->mbi[0];
1163 sc->sc_mbofull = 0;
1164
1165 /* Initialize mail box. */
1166 mailbox.cmd.opcode = BT_MBX_INIT_EXTENDED;
1167 mailbox.cmd.nmbx = BT_MBX_SIZE;
1168 ltophys(KVTOPHYS(wmbx), mailbox.cmd.addr);
1169 bt_cmd(iobase, sc, sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
1170 0, (u_char *)0);
1171 }
1172
1173 void
1174 bt_inquire_setup_information(sc)
1175 struct bt_softc *sc;
1176 {
1177 int iobase = sc->sc_iobase;
1178 struct bt_model model;
1179 struct bt_revision revision;
1180 struct bt_digit digit;
1181 char *p;
1182
1183 /*
1184 * Get the firmware revision.
1185 */
1186 p = sc->sc_firmware;
1187 revision.cmd.opcode = BT_INQUIRE_REVISION;
1188 bt_cmd(iobase, sc, sizeof(revision.cmd), (u_char *)&revision.cmd,
1189 sizeof(revision.reply), (u_char *)&revision.reply);
1190 *p++ = revision.reply.firm_revision;
1191 *p++ = '.';
1192 *p++ = revision.reply.firm_version;
1193 digit.cmd.opcode = BT_INQUIRE_REVISION_3;
1194 bt_cmd(iobase, sc, sizeof(digit.cmd), (u_char *)&digit.cmd,
1195 sizeof(digit.reply), (u_char *)&digit.reply);
1196 *p++ = digit.reply.digit;
1197 if (revision.reply.firm_revision >= '3' ||
1198 (revision.reply.firm_revision == '3' && revision.reply.firm_version >= '3')) {
1199 digit.cmd.opcode = BT_INQUIRE_REVISION_4;
1200 bt_cmd(iobase, sc, sizeof(digit.cmd), (u_char *)&digit.cmd,
1201 sizeof(digit.reply), (u_char *)&digit.reply);
1202 *p++ = digit.reply.digit;
1203 }
1204 while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
1205 p--;
1206 *p = '\0';
1207
1208 /*
1209 * Get the model number.
1210 */
1211 if (revision.reply.firm_revision >= '3') {
1212 p = sc->sc_model;
1213 model.cmd.opcode = BT_INQUIRE_MODEL;
1214 model.cmd.len = sizeof(model.reply);
1215 bt_cmd(iobase, sc, sizeof(model.cmd), (u_char *)&model.cmd,
1216 sizeof(model.reply), (u_char *)&model.reply);
1217 *p++ = model.reply.id[0];
1218 *p++ = model.reply.id[1];
1219 *p++ = model.reply.id[2];
1220 *p++ = model.reply.id[3];
1221 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1222 p--;
1223 *p++ = model.reply.version[0];
1224 *p++ = model.reply.version[1];
1225 while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1226 p--;
1227 *p = '\0';
1228 } else
1229 strcpy(sc->sc_model, "542B");
1230
1231 printf(": model BT-%s, firmware %s\n", sc->sc_model, sc->sc_firmware);
1232 }
1233
1234 void
1235 btminphys(bp)
1236 struct buf *bp;
1237 {
1238
1239 if (bp->b_bcount > ((BT_NSEG - 1) << PGSHIFT))
1240 bp->b_bcount = ((BT_NSEG - 1) << PGSHIFT);
1241 minphys(bp);
1242 }
1243
1244 /*
1245 * start a scsi operation given the command and the data address. Also needs
1246 * the unit, target and lu.
1247 */
1248 int
1249 bt_scsi_cmd(xs)
1250 struct scsipi_xfer *xs;
1251 {
1252 struct scsipi_link *sc_link = xs->sc_link;
1253 struct bt_softc *sc = sc_link->adapter_softc;
1254 struct bt_ccb *ccb;
1255 struct bt_scat_gath *sg;
1256 int seg; /* scatter gather seg being worked on */
1257 u_long thiskv, thisbounce;
1258 int bytes_this_page, datalen, control;
1259 int s;
1260
1261 SC_DEBUG(sc_link, SDEV_DB2, ("bt_scsi_cmd\n"));
1262 /*
1263 * get a ccb to use. If the transfer
1264 * is from a buf (possibly from interrupt time)
1265 * then we can't allow it to sleep
1266 */
1267 control = xs->xs_control;
1268 if ((ccb = bt_get_ccb(sc, control & XS_CTL_NOSLEEP)) == NULL) {
1269 xs->error = XS_DRIVER_STUFFUP;
1270 return TRY_AGAIN_LATER;
1271 }
1272 ccb->xs = xs;
1273 ccb->timeout = xs->timeout;
1274
1275 /*
1276 * Put all the arguments for the xfer in the ccb
1277 */
1278 if (control & XS_CTL_RESET) {
1279 ccb->opcode = BT_RESET_CCB;
1280 ccb->scsi_cmd_length = 0;
1281 } else {
1282 /* can't use S/G if zero length */
1283 ccb->opcode = (xs->datalen ? BT_INIT_SCAT_GATH_CCB
1284 : BT_INITIATOR_CCB);
1285 bcopy(xs->cmd, &ccb->scsi_cmd,
1286 ccb->scsi_cmd_length = xs->cmdlen);
1287 }
1288
1289 if (xs->datalen) {
1290 sg = ccb->scat_gath;
1291 seg = 0;
1292 /*
1293 * Set up the scatter-gather block.
1294 */
1295 SC_DEBUG(sc_link, SDEV_DB4,
1296 ("%d @0x%x:- ", xs->datalen, xs->data));
1297
1298 datalen = xs->datalen;
1299 thiskv = (int)xs->data;
1300
1301 while (datalen && seg < BT_NSEG) {
1302
1303 /* put in the base address of a buf */
1304 thisbounce = (u_long)
1305 bt_get_buf(sc, control & XS_CTL_NOSLEEP);
1306 if(thisbounce == 0)
1307 break;
1308 ltophys(KVTOPHYS(thisbounce), sg->seg_addr);
1309 bytes_this_page = min(sizeof(struct bt_buf), datalen);
1310 if (control & XS_CTL_DATA_OUT) {
1311 bcopy((void *)thiskv, (void *)thisbounce, bytes_this_page);
1312 }
1313 thiskv += bytes_this_page;
1314 datalen -= bytes_this_page;
1315
1316 ltophys(bytes_this_page, sg->seg_len);
1317 sg++;
1318 seg++;
1319 }
1320 SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
1321 if (datalen) {
1322 printf("%s: bt_scsi_cmd, out of bufs %d of %d left.\n",
1323 sc->sc_dev.dv_xname, datalen, xs->datalen);
1324 goto badbuf;
1325 }
1326 ltophys(KVTOPHYS(ccb->scat_gath), ccb->data_addr);
1327 ltophys(seg * sizeof(struct bt_scat_gath), ccb->data_length);
1328 } else { /* No data xfer, use non S/G values */
1329 ltophys(0, ccb->data_addr);
1330 ltophys(0, ccb->data_length);
1331 }
1332
1333 ccb->data_out = 0;
1334 ccb->data_in = 0;
1335 ccb->target = sc_link->scsipi_scsi.target;
1336 ccb->lun = sc_link->scsipi_scsi.lun;
1337 ltophys(KVTOPHYS(&ccb->scsi_sense), ccb->sense_ptr);
1338 ccb->req_sense_length = sizeof(ccb->scsi_sense);
1339 ccb->host_stat = 0x00;
1340 ccb->target_stat = 0x00;
1341 ccb->link_id = 0;
1342 ltophys(0, ccb->link_addr);
1343
1344 s = splbio();
1345 bt_queue_ccb(sc, ccb);
1346 splx(s);
1347
1348 /*
1349 * Usually return SUCCESSFULLY QUEUED
1350 */
1351 SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
1352 if ((control & XS_CTL_POLL) == 0)
1353 return SUCCESSFULLY_QUEUED;
1354
1355 /*
1356 * If we can't use interrupts, poll on completion
1357 */
1358 if (bt_poll(sc, xs, ccb->timeout)) {
1359 bt_timeout(ccb);
1360 if (bt_poll(sc, xs, ccb->timeout))
1361 bt_timeout(ccb);
1362 }
1363 return COMPLETE;
1364
1365 badbuf:
1366 sg = ccb->scat_gath;
1367 while (seg) {
1368 thisbounce = PHYSTOKV(phystol(sg->seg_addr));
1369 bt_free_buf(sc, (struct bt_buf *)thisbounce);
1370 sg++;
1371 seg--;
1372 }
1373 xs->error = XS_DRIVER_STUFFUP;
1374 bt_free_ccb(sc, ccb);
1375 return TRY_AGAIN_LATER;
1376 }
1377
1378 /*
1379 * Poll a particular unit, looking for a particular xs
1380 */
1381 int
1382 bt_poll(sc, xs, count)
1383 struct bt_softc *sc;
1384 struct scsipi_xfer *xs;
1385 int count;
1386 {
1387 int iobase = sc->sc_iobase;
1388
1389 /* timeouts are in msec, so we loop in 1000 usec cycles */
1390 while (count) {
1391 /*
1392 * If we had interrupts enabled, would we
1393 * have got an interrupt?
1394 */
1395 if (isa_inb(iobase + BT_INTR_PORT) & BT_INTR_ANYINTR)
1396 btintr(sc);
1397 if (xs->xs_status & XS_STS_DONE)
1398 return 0;
1399 delay(1000); /* only happens in boot so ok */
1400 count--;
1401 }
1402 return 1;
1403 }
1404
1405 void
1406 bt_timeout(arg)
1407 void *arg;
1408 {
1409 struct bt_ccb *ccb = arg;
1410 struct scsipi_xfer *xs = ccb->xs;
1411 struct scsipi_link *sc_link = xs->sc_link;
1412 struct bt_softc *sc = sc_link->adapter_softc;
1413 int s;
1414
1415 scsi_print_addr(sc_link);
1416 printf("timed out");
1417
1418 s = splbio();
1419
1420 #ifdef BTDIAG
1421 /*
1422 * If the ccb's mbx is not free, then the board has gone Far East?
1423 */
1424 bt_collect_mbo(sc);
1425 if (ccb->flags & CCB_SENDING) {
1426 printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1427 Debugger();
1428 }
1429 #endif
1430
1431 /*
1432 * If it has been through before, then
1433 * a previous abort has failed, don't
1434 * try abort again
1435 */
1436 if (ccb->flags & CCB_ABORT) {
1437 /* abort timed out */
1438 printf(" AGAIN\n");
1439 /* XXX Must reset! */
1440 } else {
1441 /* abort the operation that has timed out */
1442 printf("\n");
1443 ccb->xs->error = XS_TIMEOUT;
1444 ccb->timeout = BT_ABORT_TIMEOUT;
1445 ccb->flags |= CCB_ABORT;
1446 bt_queue_ccb(sc, ccb);
1447 }
1448
1449 splx(s);
1450 }
1451