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