xref: /netbsd-src/sys/dev/ic/bha.c (revision 06be8101a16cc95f40783b3cb7afd12112103a9a)
1 /*	$NetBSD: bha.c,v 1.47 2001/11/13 13:14:35 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
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 the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Originally written by Julian Elischer (julian@tfs.com)
42  * for TRW Financial Systems for use under the MACH(2.5) operating system.
43  *
44  * TRW Financial Systems, in accordance with their agreement with Carnegie
45  * Mellon University, makes this software available to CMU to distribute
46  * or use in any manner that they see fit as long as this message is kept with
47  * the software. For this reason TFS also grants any other persons or
48  * organisations permission to use or modify this software.
49  *
50  * TFS supplies this software to be publicly redistributed
51  * on the understanding that TFS is not responsible for the correct
52  * functioning of this software in any circumstances.
53  */
54 
55 #include <sys/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: bha.c,v 1.47 2001/11/13 13:14:35 lukem Exp $");
57 
58 #include "opt_ddb.h"
59 
60 #include <sys/types.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/callout.h>
64 #include <sys/kernel.h>
65 #include <sys/errno.h>
66 #include <sys/ioctl.h>
67 #include <sys/device.h>
68 #include <sys/malloc.h>
69 #include <sys/buf.h>
70 #include <sys/proc.h>
71 #include <sys/user.h>
72 
73 #include <uvm/uvm_extern.h>
74 
75 #include <machine/bus.h>
76 #include <machine/intr.h>
77 
78 #include <dev/scsipi/scsi_all.h>
79 #include <dev/scsipi/scsipi_all.h>
80 #include <dev/scsipi/scsiconf.h>
81 
82 #include <dev/ic/bhareg.h>
83 #include <dev/ic/bhavar.h>
84 
85 #ifndef DDB
86 #define Debugger() panic("should call debugger here (bha.c)")
87 #endif /* ! DDB */
88 
89 #define	BHA_MAXXFER	((BHA_NSEG - 1) << PGSHIFT)
90 
91 #ifdef BHADEBUG
92 int     bha_debug = 0;
93 #endif /* BHADEBUG */
94 
95 static int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, char *, int,
96 	    u_char *, int, u_char *));
97 
98 static void bha_scsipi_request __P((struct scsipi_channel *,
99 	    scsipi_adapter_req_t, void *));
100 static void bha_minphys __P((struct buf *));
101 
102 static void bha_get_xfer_mode __P((struct bha_softc *,
103 	    struct scsipi_xfer_mode *));
104 
105 static void bha_done __P((struct bha_softc *, struct bha_ccb *));
106 int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
107 static void bha_timeout __P((void *arg));
108 
109 static int bha_init __P((struct bha_softc *));
110 
111 static int bha_create_mailbox __P((struct bha_softc *));
112 static void bha_collect_mbo __P((struct bha_softc *));
113 
114 static void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
115 static void bha_start_ccbs __P((struct bha_softc *));
116 static void bha_finish_ccbs __P((struct bha_softc *));
117 
118 struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, bus_addr_t));
119 void	bha_create_ccbs __P((struct bha_softc *, int));
120 int	bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
121 struct bha_ccb *bha_get_ccb __P((struct bha_softc *));
122 void	bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
123 
124 #define BHA_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
125 #define	BHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
126 
127 /*
128  * Number of CCBs in an allocation group; must be computed at run-time.
129  */
130 int	bha_ccbs_per_group;
131 
132 __inline struct bha_mbx_out *bha_nextmbo __P((struct bha_softc *,
133 	struct bha_mbx_out *));
134 __inline struct bha_mbx_in *bha_nextmbi __P((struct bha_softc *,
135 	struct bha_mbx_in *));
136 
137 __inline struct bha_mbx_out *
138 bha_nextmbo(sc, mbo)
139 	struct bha_softc *sc;
140 	struct bha_mbx_out *mbo;
141 {
142 
143 	if (mbo == &sc->sc_mbo[sc->sc_mbox_count - 1])
144 		return (&sc->sc_mbo[0]);
145 	return (mbo + 1);
146 }
147 
148 __inline struct bha_mbx_in *
149 bha_nextmbi(sc, mbi)
150 	struct bha_softc *sc;
151 	struct bha_mbx_in *mbi;
152 {
153 	if (mbi == &sc->sc_mbi[sc->sc_mbox_count - 1])
154 		return (&sc->sc_mbi[0]);
155 	return (mbi + 1);
156 }
157 
158 /*
159  * bha_attach:
160  *
161  *	Finish attaching a Buslogic controller, and configure children.
162  */
163 void
164 bha_attach(sc)
165 	struct bha_softc *sc;
166 {
167 	struct scsipi_adapter *adapt = &sc->sc_adapter;
168 	struct scsipi_channel *chan = &sc->sc_channel;
169 	int initial_ccbs;
170 
171 	/*
172 	 * Initialize the number of CCBs per group.
173 	 */
174 	if (bha_ccbs_per_group == 0)
175 		bha_ccbs_per_group = BHA_CCBS_PER_GROUP;
176 
177 	initial_ccbs = bha_info(sc);
178 	if (initial_ccbs == 0) {
179 		printf("%s: unable to get adapter info\n",
180 		    sc->sc_dev.dv_xname);
181 		return;
182 	}
183 
184 	/*
185 	 * Fill in the scsipi_adapter.
186 	 */
187 	memset(adapt, 0, sizeof(*adapt));
188 	adapt->adapt_dev = &sc->sc_dev;
189 	adapt->adapt_nchannels = 1;
190 	/* adapt_openings initialized below */
191 	adapt->adapt_max_periph = sc->sc_mbox_count;
192 	adapt->adapt_request = bha_scsipi_request;
193 	adapt->adapt_minphys = bha_minphys;
194 
195 	/*
196 	 * Fill in the scsipi_channel.
197 	 */
198 	memset(chan, 0, sizeof(*chan));
199 	chan->chan_adapter = adapt;
200 	chan->chan_bustype = &scsi_bustype;
201 	chan->chan_channel = 0;
202 	chan->chan_flags = SCSIPI_CHAN_CANGROW;
203 	chan->chan_ntargets = (sc->sc_flags & BHAF_WIDE) ? 16 : 8;
204 	chan->chan_nluns = (sc->sc_flags & BHAF_WIDE_LUN) ? 32 : 8;
205 	chan->chan_id = sc->sc_scsi_id;
206 
207 	TAILQ_INIT(&sc->sc_free_ccb);
208 	TAILQ_INIT(&sc->sc_waiting_ccb);
209 	TAILQ_INIT(&sc->sc_allocating_ccbs);
210 
211 	if (bha_create_mailbox(sc) != 0)
212 		return;
213 
214 	bha_create_ccbs(sc, initial_ccbs);
215 	if (sc->sc_cur_ccbs < 2) {
216 		printf("%s: not enough CCBs to run\n",
217 		    sc->sc_dev.dv_xname);
218 		return;
219 	}
220 
221 	adapt->adapt_openings = sc->sc_cur_ccbs;
222 
223 	if (bha_init(sc) != 0)
224 		return;
225 
226 	(void) config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
227 }
228 
229 /*
230  * bha_intr:
231  *
232  *	Interrupt service routine.
233  */
234 int
235 bha_intr(arg)
236 	void *arg;
237 {
238 	struct bha_softc *sc = arg;
239 	bus_space_tag_t iot = sc->sc_iot;
240 	bus_space_handle_t ioh = sc->sc_ioh;
241 	u_char sts;
242 
243 #ifdef BHADEBUG
244 	printf("%s: bha_intr ", sc->sc_dev.dv_xname);
245 #endif /* BHADEBUG */
246 
247 	/*
248 	 * First acknowlege the interrupt, Then if it's not telling about
249 	 * a completed operation just return.
250 	 */
251 	sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
252 	if ((sts & BHA_INTR_ANYINTR) == 0)
253 		return (0);
254 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
255 
256 #ifdef BHADIAG
257 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
258 	bha_collect_mbo(sc);
259 #endif
260 
261 	/* Mail box out empty? */
262 	if (sts & BHA_INTR_MBOA) {
263 		struct bha_toggle toggle;
264 
265 		toggle.cmd.opcode = BHA_MBO_INTR_EN;
266 		toggle.cmd.enable = 0;
267 		bha_cmd(iot, ioh, sc->sc_dev.dv_xname,
268 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
269 		    0, (u_char *)0);
270 		bha_start_ccbs(sc);
271 	}
272 
273 	/* Mail box in full? */
274 	if (sts & BHA_INTR_MBIF)
275 		bha_finish_ccbs(sc);
276 
277 	return (1);
278 }
279 
280 /*****************************************************************************
281  * SCSI interface routines
282  *****************************************************************************/
283 
284 /*
285  * bha_scsipi_request:
286  *
287  *	Perform a request for the SCSIPI layer.
288  */
289 void
290 bha_scsipi_request(chan, req, arg)
291 	struct scsipi_channel *chan;
292 	scsipi_adapter_req_t req;
293 	void *arg;
294 {
295 	struct scsipi_adapter *adapt = chan->chan_adapter;
296 	struct bha_softc *sc = (void *)adapt->adapt_dev;
297 	struct scsipi_xfer *xs;
298 	struct scsipi_periph *periph;
299 	bus_dma_tag_t dmat = sc->sc_dmat;
300 	struct bha_ccb *ccb;
301 	int error, seg, flags, s;
302 
303 	switch (req) {
304 	case ADAPTER_REQ_RUN_XFER:
305 		xs = arg;
306 		periph = xs->xs_periph;
307 		flags = xs->xs_control;
308 
309 		SC_DEBUG(periph, SCSIPI_DB2, ("bha_scsipi_request\n"));
310 
311 		/* Get a CCB to use. */
312 		ccb = bha_get_ccb(sc);
313 #ifdef DIAGNOSTIC
314 		/*
315 		 * This should never happen as we track the resources
316 		 * in the mid-layer.
317 		 */
318 		if (ccb == NULL) {
319 			scsipi_printaddr(periph);
320 			printf("unable to allocate ccb\n");
321 			panic("bha_scsipi_request");
322 		}
323 #endif
324 
325 		ccb->xs = xs;
326 		ccb->timeout = xs->timeout;
327 
328 		/*
329 		 * Put all the arguments for the xfer in the ccb
330 		 */
331 		if (flags & XS_CTL_RESET) {
332 			ccb->opcode = BHA_RESET_CCB;
333 			ccb->scsi_cmd_length = 0;
334 		} else {
335 			/* can't use S/G if zero length */
336 			ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
337 						   : BHA_INITIATOR_CCB);
338 			memcpy(&ccb->scsi_cmd, xs->cmd,
339 			    ccb->scsi_cmd_length = xs->cmdlen);
340 		}
341 
342 		if (xs->datalen) {
343 			/*
344 			 * Map the DMA transfer.
345 			 */
346 #ifdef TFS
347 			if (flags & XS_CTL_DATA_UIO) {
348 				error = bus_dmamap_load_uio(dmat,
349 				    ccb->dmamap_xfer, (struct uio *)xs->data,
350 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
351 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
352 				     ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
353 				      BUS_DMA_WRITE));
354 			} else
355 #endif /* TFS */
356 			{
357 				error = bus_dmamap_load(dmat,
358 				    ccb->dmamap_xfer, xs->data, xs->datalen,
359 				    NULL,
360 				    ((flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
361 				     BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
362 				     ((flags & XS_CTL_DATA_IN) ? BUS_DMA_READ :
363 				      BUS_DMA_WRITE));
364 			}
365 
366 			switch (error) {
367 			case 0:
368 				break;
369 
370 			case ENOMEM:
371 			case EAGAIN:
372 				xs->error = XS_RESOURCE_SHORTAGE;
373 				goto out_bad;
374 
375 			default:
376 				xs->error = XS_DRIVER_STUFFUP;
377 				printf("%s: error %d loading DMA map\n",
378 				    sc->sc_dev.dv_xname, error);
379  out_bad:
380 				bha_free_ccb(sc, ccb);
381 				scsipi_done(xs);
382 				return;
383 			}
384 
385 			bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
386 			    ccb->dmamap_xfer->dm_mapsize,
387 			    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
388 			    BUS_DMASYNC_PREWRITE);
389 
390 			/*
391 			 * Load the hardware scatter/gather map with the
392 			 * contents of the DMA map.
393 			 */
394 			for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
395 				ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
396 				    ccb->scat_gath[seg].seg_addr);
397 				ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
398 				    ccb->scat_gath[seg].seg_len);
399 			}
400 
401 			ltophys(ccb->hashkey + offsetof(struct bha_ccb,
402 			    scat_gath), ccb->data_addr);
403 			ltophys(ccb->dmamap_xfer->dm_nsegs *
404 			    sizeof(struct bha_scat_gath), ccb->data_length);
405 		} else {
406 			/*
407 			 * No data xfer, use non S/G values.
408 			 */
409 			ltophys(0, ccb->data_addr);
410 			ltophys(0, ccb->data_length);
411 		}
412 
413 		if (XS_CTL_TAGTYPE(xs) != 0) {
414 			ccb->tag_enable = 1;
415 			ccb->tag_type = xs->xs_tag_type & 0x03;
416 		} else {
417 			ccb->tag_enable = 0;
418 			ccb->tag_type = 0;
419 		}
420 
421 		ccb->data_out = 0;
422 		ccb->data_in = 0;
423 		ccb->target = periph->periph_target;
424 		ccb->lun = periph->periph_lun;
425 		ltophys(ccb->hashkey + offsetof(struct bha_ccb, scsi_sense),
426 		    ccb->sense_ptr);
427 		ccb->req_sense_length = sizeof(ccb->scsi_sense);
428 		ccb->host_stat = 0x00;
429 		ccb->target_stat = 0x00;
430 		ccb->link_id = 0;
431 		ltophys(0, ccb->link_addr);
432 
433 		BHA_CCB_SYNC(sc, ccb, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
434 
435 		s = splbio();
436 		bha_queue_ccb(sc, ccb);
437 		splx(s);
438 
439 		SC_DEBUG(periph, SCSIPI_DB3, ("cmd_sent\n"));
440 		if ((flags & XS_CTL_POLL) == 0)
441 			return;
442 
443 		/*
444 		 * If we can't use interrupts, poll on completion
445 		 */
446 		if (bha_poll(sc, xs, ccb->timeout)) {
447 			bha_timeout(ccb);
448 			if (bha_poll(sc, xs, ccb->timeout))
449 				bha_timeout(ccb);
450 		}
451 		return;
452 
453 	case ADAPTER_REQ_GROW_RESOURCES:
454 		if (sc->sc_cur_ccbs == sc->sc_max_ccbs) {
455 			chan->chan_flags &= ~SCSIPI_CHAN_CANGROW;
456 			return;
457 		}
458 		seg = sc->sc_cur_ccbs;
459 		bha_create_ccbs(sc, bha_ccbs_per_group);
460 		adapt->adapt_openings += sc->sc_cur_ccbs - seg;
461 		return;
462 
463 	case ADAPTER_REQ_SET_XFER_MODE:
464 		/*
465 		 * Can't really do this on the Buslogic.  It has its
466 		 * own setup info.  But we do know how to query what
467 		 * the settings are.
468 		 */
469 		bha_get_xfer_mode(sc, (struct scsipi_xfer_mode *)arg);
470 		return;
471 	}
472 }
473 
474 /*
475  * bha_minphys:
476  *
477  *	Limit a transfer to our maximum transfer size.
478  */
479 void
480 bha_minphys(bp)
481 	struct buf *bp;
482 {
483 
484 	if (bp->b_bcount > BHA_MAXXFER)
485 		bp->b_bcount = BHA_MAXXFER;
486 	minphys(bp);
487 }
488 
489 /*****************************************************************************
490  * SCSI job execution helper routines
491  *****************************************************************************/
492 
493 /*
494  * bha_get_xfer_mode;
495  *
496  *	Negotiate the xfer mode for the specified periph, and report
497  *	back the mode to the midlayer.
498  *
499  *	NOTE: we must be called at splbio().
500  */
501 void
502 bha_get_xfer_mode(sc, xm)
503 	struct bha_softc *sc;
504 	struct scsipi_xfer_mode *xm;
505 {
506 	struct bha_setup hwsetup;
507 	struct bha_period hwperiod;
508 	struct bha_sync *bs;
509 	int toff = xm->xm_target & 7, tmask = (1 << toff);
510 	int wide, period, offset, rlen;
511 
512 	/*
513 	 * Issue an Inquire Setup Information.  We can extract
514 	 * sync and wide information from here.
515 	 */
516 	rlen = sizeof(hwsetup.reply) +
517 	    ((sc->sc_flags & BHAF_WIDE) ? sizeof(hwsetup.reply_w) : 0);
518 	hwsetup.cmd.opcode = BHA_INQUIRE_SETUP;
519 	hwsetup.cmd.len = rlen;
520 	bha_cmd(sc->sc_iot, sc->sc_ioh, sc->sc_dev.dv_xname,
521 	    sizeof(hwsetup.cmd), (u_char *)&hwsetup.cmd,
522 	    rlen, (u_char *)&hwsetup.reply);
523 
524 	xm->xm_mode = 0;
525 	xm->xm_period = 0;
526 	xm->xm_offset = 0;
527 
528 	/*
529 	 * First check for wide.  On later boards, we can check
530 	 * directly in the setup info if wide is currently active.
531 	 *
532 	 * On earlier boards, we have to make an educated guess.
533 	 */
534 	if (sc->sc_flags & BHAF_WIDE) {
535 		if (strcmp(sc->sc_firmware, "5.06L") >= 0) {
536 			if (xm->xm_target > 7) {
537 				wide =
538 				    hwsetup.reply_w.high_wide_active & tmask;
539 			} else {
540 				wide =
541 				    hwsetup.reply_w.low_wide_active & tmask;
542 			}
543 			if (wide)
544 				xm->xm_mode |= PERIPH_CAP_WIDE16;
545 		} else {
546 			/* XXX Check `wide permitted' in the config info. */
547 			xm->xm_mode |= PERIPH_CAP_WIDE16;
548 		}
549 	}
550 
551 	/*
552 	 * Now get basic sync info.
553 	 */
554 	bs = (xm->xm_target > 7) ?
555 	     &hwsetup.reply_w.sync_high[toff] :
556 	     &hwsetup.reply.sync_low[toff];
557 
558 	if (bs->valid) {
559 		xm->xm_mode |= PERIPH_CAP_SYNC;
560 		period = (bs->period * 50) + 20;
561 		offset = bs->offset;
562 
563 		/*
564 		 * On boards that can do Fast and Ultra, use the Inquire Period
565 		 * command to get the period.
566 		 */
567 		if (sc->sc_firmware[0] >= '3') {
568 			rlen = sizeof(hwperiod.reply) +
569 			    ((sc->sc_flags & BHAF_WIDE) ?
570 			      sizeof(hwperiod.reply_w) : 0);
571 			hwperiod.cmd.opcode = BHA_INQUIRE_PERIOD;
572 			hwperiod.cmd.len = rlen;
573 			bha_cmd(sc->sc_iot, sc->sc_ioh, sc->sc_dev.dv_xname,
574 			    sizeof(hwperiod.cmd), (u_char *)&hwperiod.cmd,
575 			    rlen, (u_char *)&hwperiod.reply);
576 
577 			if (xm->xm_target > 7)
578 				period = hwperiod.reply_w.period[toff];
579 			else
580 				period = hwperiod.reply.period[toff];
581 
582 			period *= 10;
583 		}
584 
585 		xm->xm_period =
586 		    scsipi_sync_period_to_factor(period * 10);
587 		xm->xm_offset = offset;
588 	}
589 
590 	/*
591 	 * Now check for tagged queueing support.
592 	 *
593 	 * XXX Check `tags permitted' in the config info.
594 	 */
595 	if (sc->sc_flags & BHAF_TAGGED_QUEUEING)
596 		xm->xm_mode |= PERIPH_CAP_TQING;
597 
598 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, xm);
599 }
600 
601 /*
602  * bha_done:
603  *
604  *	A CCB has completed execution.  Pass the status back to the
605  *	upper layer.
606  */
607 void
608 bha_done(sc, ccb)
609 	struct bha_softc *sc;
610 	struct bha_ccb *ccb;
611 {
612 	bus_dma_tag_t dmat = sc->sc_dmat;
613 	struct scsipi_xfer *xs = ccb->xs;
614 
615 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("bha_done\n"));
616 
617 #ifdef BHADIAG
618 	if (ccb->flags & CCB_SENDING) {
619 		printf("%s: exiting ccb still in transit!\n",
620 		    sc->sc_dev.dv_xname);
621 		Debugger();
622 		return;
623 	}
624 #endif
625 	if ((ccb->flags & CCB_ALLOC) == 0) {
626 		printf("%s: exiting ccb not allocated!\n",
627 		    sc->sc_dev.dv_xname);
628 		Debugger();
629 		return;
630 	}
631 
632 	/*
633 	 * If we were a data transfer, unload the map that described
634 	 * the data buffer.
635 	 */
636 	if (xs->datalen) {
637 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
638 		    ccb->dmamap_xfer->dm_mapsize,
639 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
640 		    BUS_DMASYNC_POSTWRITE);
641 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
642 	}
643 
644 	if (xs->error == XS_NOERROR) {
645 		if (ccb->host_stat != BHA_OK) {
646 			switch (ccb->host_stat) {
647 			case BHA_SEL_TIMEOUT:	/* No response */
648 				xs->error = XS_SELTIMEOUT;
649 				break;
650 			default:	/* Other scsi protocol messes */
651 				printf("%s: host_stat %x\n",
652 				    sc->sc_dev.dv_xname, ccb->host_stat);
653 				xs->error = XS_DRIVER_STUFFUP;
654 				break;
655 			}
656 		} else if (ccb->target_stat != SCSI_OK) {
657 			switch (ccb->target_stat) {
658 			case SCSI_CHECK:
659 				memcpy(&xs->sense.scsi_sense,
660 				    &ccb->scsi_sense,
661 				    sizeof(xs->sense.scsi_sense));
662 				xs->error = XS_SENSE;
663 				break;
664 			case SCSI_BUSY:
665 				xs->error = XS_BUSY;
666 				break;
667 			default:
668 				printf("%s: target_stat %x\n",
669 				    sc->sc_dev.dv_xname, ccb->target_stat);
670 				xs->error = XS_DRIVER_STUFFUP;
671 				break;
672 			}
673 		} else
674 			xs->resid = 0;
675 	}
676 
677 	bha_free_ccb(sc, ccb);
678 	scsipi_done(xs);
679 }
680 
681 /*
682  * bha_poll:
683  *
684  *	Poll for completion of the specified job.
685  */
686 int
687 bha_poll(sc, xs, count)
688 	struct bha_softc *sc;
689 	struct scsipi_xfer *xs;
690 	int count;
691 {
692 	bus_space_tag_t iot = sc->sc_iot;
693 	bus_space_handle_t ioh = sc->sc_ioh;
694 
695 	/* timeouts are in msec, so we loop in 1000 usec cycles */
696 	while (count) {
697 		/*
698 		 * If we had interrupts enabled, would we
699 		 * have got an interrupt?
700 		 */
701 		if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
702 		    BHA_INTR_ANYINTR)
703 			bha_intr(sc);
704 		if (xs->xs_status & XS_STS_DONE)
705 			return (0);
706 		delay(1000);	/* only happens in boot so ok */
707 		count--;
708 	}
709 	return (1);
710 }
711 
712 /*
713  * bha_timeout:
714  *
715  *	CCB timeout handler.
716  */
717 void
718 bha_timeout(arg)
719 	void *arg;
720 {
721 	struct bha_ccb *ccb = arg;
722 	struct scsipi_xfer *xs = ccb->xs;
723 	struct scsipi_periph *periph = xs->xs_periph;
724 	struct bha_softc *sc =
725 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
726 	int s;
727 
728 	scsipi_printaddr(periph);
729 	printf("timed out");
730 
731 	s = splbio();
732 
733 #ifdef BHADIAG
734 	/*
735 	 * If the ccb's mbx is not free, then the board has gone Far East?
736 	 */
737 	bha_collect_mbo(sc);
738 	if (ccb->flags & CCB_SENDING) {
739 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
740 		Debugger();
741 	}
742 #endif
743 
744 	/*
745 	 * If it has been through before, then
746 	 * a previous abort has failed, don't
747 	 * try abort again
748 	 */
749 	if (ccb->flags & CCB_ABORT) {
750 		/* abort timed out */
751 		printf(" AGAIN\n");
752 		/* XXX Must reset! */
753 	} else {
754 		/* abort the operation that has timed out */
755 		printf("\n");
756 		ccb->xs->error = XS_TIMEOUT;
757 		ccb->timeout = BHA_ABORT_TIMEOUT;
758 		ccb->flags |= CCB_ABORT;
759 		bha_queue_ccb(sc, ccb);
760 	}
761 
762 	splx(s);
763 }
764 
765 /*****************************************************************************
766  * Misc. subroutines.
767  *****************************************************************************/
768 
769 /*
770  * bha_cmd:
771  *
772  *	Send a command to the Buglogic controller.
773  */
774 int
775 bha_cmd(iot, ioh, name, icnt, ibuf, ocnt, obuf)
776 	bus_space_tag_t iot;
777 	bus_space_handle_t ioh;
778 	char *name;
779 	int icnt, ocnt;
780 	u_char *ibuf, *obuf;
781 {
782 	int i;
783 	int wait;
784 	u_char sts;
785 	u_char opcode = ibuf[0];
786 
787 	/*
788 	 * Calculate a reasonable timeout for the command.
789 	 */
790 	switch (opcode) {
791 	case BHA_INQUIRE_DEVICES:
792 	case BHA_INQUIRE_DEVICES_2:
793 		wait = 90 * 20000;
794 		break;
795 	default:
796 		wait = 1 * 20000;
797 		break;
798 	}
799 
800 	/*
801 	 * Wait for the adapter to go idle, unless it's one of
802 	 * the commands which don't need this
803 	 */
804 	if (opcode != BHA_MBO_INTR_EN) {
805 		for (i = 20000; i; i--) {	/* 1 sec? */
806 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
807 			if (sts & BHA_STAT_IDLE)
808 				break;
809 			delay(50);
810 		}
811 		if (!i) {
812 			printf("%s: bha_cmd, host not idle(0x%x)\n",
813 			    name, sts);
814 			return (1);
815 		}
816 	}
817 
818 	/*
819 	 * Now that it is idle, if we expect output, preflush the
820 	 * queue feeding to us.
821 	 */
822 	if (ocnt) {
823 		while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
824 		    BHA_STAT_DF)
825 			bus_space_read_1(iot, ioh, BHA_DATA_PORT);
826 	}
827 
828 	/*
829 	 * Output the command and the number of arguments given
830 	 * for each byte, first check the port is empty.
831 	 */
832 	while (icnt--) {
833 		for (i = wait; i; i--) {
834 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
835 			if (!(sts & BHA_STAT_CDF))
836 				break;
837 			delay(50);
838 		}
839 		if (!i) {
840 			if (opcode != BHA_INQUIRE_REVISION)
841 				printf("%s: bha_cmd, cmd/data port full\n",
842 				    name);
843 			goto bad;
844 		}
845 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
846 	}
847 
848 	/*
849 	 * If we expect input, loop that many times, each time,
850 	 * looking for the data register to have valid data
851 	 */
852 	while (ocnt--) {
853 		for (i = wait; i; i--) {
854 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
855 			if (sts & BHA_STAT_DF)
856 				break;
857 			delay(50);
858 		}
859 		if (!i) {
860 #ifdef BHADEBUG
861 			if (opcode != BHA_INQUIRE_REVISION)
862 				printf("%s: bha_cmd, cmd/data port empty %d\n",
863 				    name, ocnt);
864 #endif /* BHADEBUG */
865 			goto bad;
866 		}
867 		*obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
868 	}
869 
870 	/*
871 	 * Wait for the board to report a finished instruction.
872 	 * We may get an extra interrupt for the HACC signal, but this is
873 	 * unimportant.
874 	 */
875 	if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
876 		for (i = 20000; i; i--) {	/* 1 sec? */
877 			sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
878 			/* XXX Need to save this in the interrupt handler? */
879 			if (sts & BHA_INTR_HACC)
880 				break;
881 			delay(50);
882 		}
883 		if (!i) {
884 			printf("%s: bha_cmd, host not finished(0x%x)\n",
885 			    name, sts);
886 			return (1);
887 		}
888 	}
889 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
890 	return (0);
891 
892 bad:
893 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
894 	return (1);
895 }
896 
897 /*
898  * bha_find:
899  *
900  *	Find the board.
901  */
902 int
903 bha_find(iot, ioh)
904 	bus_space_tag_t iot;
905 	bus_space_handle_t ioh;
906 {
907 	int i;
908 	u_char sts;
909 	struct bha_extended_inquire inquire;
910 
911 	/* Check something is at the ports we need to access */
912 	sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
913 	if (sts == 0xFF)
914 		return (0);
915 
916 	/*
917 	 * Reset board, If it doesn't respond, assume
918 	 * that it's not there.. good for the probe
919 	 */
920 
921 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
922 	    BHA_CTRL_HRST | BHA_CTRL_SRST);
923 
924 	delay(100);
925 	for (i = BHA_RESET_TIMEOUT; i; i--) {
926 		sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
927 		if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
928 			break;
929 		delay(1000);
930 	}
931 	if (!i) {
932 #ifdef BHADEBUG
933 		if (bha_debug)
934 			printf("bha_find: No answer from buslogic board\n");
935 #endif /* BHADEBUG */
936 		return (0);
937 	}
938 
939 	/*
940 	 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
941 	 * interface. The native bha interface is not compatible with
942 	 * an aha. 1542. We need to ensure that we never match an
943 	 * Adaptec 1542. We must also avoid sending Adaptec-compatible
944 	 * commands to a real bha, lest it go into 1542 emulation mode.
945 	 * (On an indirect bus like ISA, we should always probe for BusLogic
946 	 * interfaces before Adaptec interfaces).
947 	 */
948 
949 	/*
950 	 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
951 	 * for an extended-geometry register.  The 1542[AB] don't have one.
952 	 */
953 	sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
954 	if (sts == 0xFF)
955 		return (0);
956 
957 	/*
958 	 * Check that we actually know how to use this board.
959 	 */
960 	delay(1000);
961 	inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
962 	inquire.cmd.len = sizeof(inquire.reply);
963 	i = bha_cmd(iot, ioh, "(bha_find)",
964 	    sizeof(inquire.cmd), (u_char *)&inquire.cmd,
965 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
966 
967 	/*
968 	 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
969 	 * have the extended-geometry register and also respond to
970 	 * BHA_INQUIRE_EXTENDED.  Make sure we never match such cards,
971 	 * by checking the size of the reply is what a BusLogic card returns.
972 	 */
973 	if (i) {
974 #ifdef BHADEBUG
975 		printf("bha_find: board returned %d instead of %d to %s\n",
976 		       i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
977 #endif
978 		return (0);
979 	}
980 
981 	/* OK, we know we've found a buslogic adaptor. */
982 
983 	switch (inquire.reply.bus_type) {
984 	case BHA_BUS_TYPE_24BIT:
985 	case BHA_BUS_TYPE_32BIT:
986 		break;
987 	case BHA_BUS_TYPE_MCA:
988 		/* We don't grok MicroChannel (yet). */
989 		return (0);
990 	default:
991 		printf("bha_find: illegal bus type %c\n",
992 		    inquire.reply.bus_type);
993 		return (0);
994 	}
995 
996 	return (1);
997 }
998 
999 
1000 /*
1001  * bha_inquire_config:
1002  *
1003  *	Determine irq/drq.
1004  */
1005 int
1006 bha_inquire_config(bus_space_tag_t iot, bus_space_handle_t ioh,
1007 	    struct bha_probe_data *sc)
1008 {
1009 	int irq, drq;
1010 	struct bha_config config;
1011 
1012 	/*
1013 	 * Assume we have a board at this stage setup dma channel from
1014 	 * jumpers and save int level
1015 	 */
1016 	delay(1000);
1017 	config.cmd.opcode = BHA_INQUIRE_CONFIG;
1018 	bha_cmd(iot, ioh, "(bha_inquire_config)",
1019 	    sizeof(config.cmd), (u_char *)&config.cmd,
1020 	    sizeof(config.reply), (u_char *)&config.reply);
1021 	switch (config.reply.chan) {
1022 	case EISADMA:
1023 		drq = -1;
1024 		break;
1025 	case CHAN0:
1026 		drq = 0;
1027 		break;
1028 	case CHAN5:
1029 		drq = 5;
1030 		break;
1031 	case CHAN6:
1032 		drq = 6;
1033 		break;
1034 	case CHAN7:
1035 		drq = 7;
1036 		break;
1037 	default:
1038 		printf("bha: illegal drq setting %x\n",
1039 		    config.reply.chan);
1040 		return (0);
1041 	}
1042 
1043 	switch (config.reply.intr) {
1044 	case INT9:
1045 		irq = 9;
1046 		break;
1047 	case INT10:
1048 		irq = 10;
1049 		break;
1050 	case INT11:
1051 		irq = 11;
1052 		break;
1053 	case INT12:
1054 		irq = 12;
1055 		break;
1056 	case INT14:
1057 		irq = 14;
1058 		break;
1059 	case INT15:
1060 		irq = 15;
1061 		break;
1062 	default:
1063 		printf("bha: illegal irq setting %x\n",
1064 		    config.reply.intr);
1065 		return (0);
1066 	}
1067 
1068 	/* if we want to fill in softc, do so now */
1069 	if (sc != NULL) {
1070 		sc->sc_irq = irq;
1071 		sc->sc_drq = drq;
1072 	}
1073 
1074 	return (1);
1075 }
1076 
1077 int
1078 bha_probe_inquiry(bus_space_tag_t iot, bus_space_handle_t ioh,
1079     struct bha_probe_data *bpd)
1080 {
1081 	return bha_find(iot, ioh) && bha_inquire_config(iot, ioh, bpd);
1082 }
1083 
1084 /*
1085  * bha_disable_isacompat:
1086  *
1087  *	Disable the ISA-compatiblity ioports on PCI bha devices,
1088  *	to ensure they're not autoconfigured a second time as an ISA bha.
1089  */
1090 int
1091 bha_disable_isacompat(sc)
1092 	struct bha_softc *sc;
1093 {
1094 	struct bha_isadisable isa_disable;
1095 
1096 	isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
1097 	isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
1098 	bha_cmd(sc->sc_iot, sc->sc_ioh, sc->sc_dev.dv_xname,
1099 	    sizeof(isa_disable.cmd), (u_char*)&isa_disable.cmd,
1100 	    0, (u_char *)0);
1101 	return (0);
1102 }
1103 
1104 /*
1105  * bha_info:
1106  *
1107  *	Get information about the board, and report it.  We
1108  *	return the initial number of CCBs, 0 if we failed.
1109  */
1110 int
1111 bha_info(sc)
1112 	struct bha_softc *sc;
1113 {
1114 	bus_space_tag_t iot = sc->sc_iot;
1115 	bus_space_handle_t ioh = sc->sc_ioh;
1116 	struct bha_extended_inquire inquire;
1117 	struct bha_config config;
1118 	struct bha_devices devices;
1119 	struct bha_setup setup;
1120 	struct bha_model model;
1121 	struct bha_revision revision;
1122 	struct bha_digit digit;
1123 	int i, j, initial_ccbs, rlen;
1124 	char *name = sc->sc_dev.dv_xname;
1125 	char *p;
1126 
1127 	/*
1128 	 * Fetch the extended inquire information.
1129 	 */
1130 	inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
1131 	inquire.cmd.len = sizeof(inquire.reply);
1132 	bha_cmd(iot, ioh, name,
1133 	    sizeof(inquire.cmd), (u_char *)&inquire.cmd,
1134 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
1135 
1136 	/*
1137 	 * Fetch the configuration information.
1138 	 */
1139 	config.cmd.opcode = BHA_INQUIRE_CONFIG;
1140 	bha_cmd(iot, ioh, name,
1141 	    sizeof(config.cmd), (u_char *)&config.cmd,
1142 	    sizeof(config.reply), (u_char *)&config.reply);
1143 
1144 	sc->sc_scsi_id = config.reply.scsi_dev;
1145 
1146 	/*
1147 	 * Get the firmware revision.
1148 	 */
1149 	p = sc->sc_firmware;
1150 	revision.cmd.opcode = BHA_INQUIRE_REVISION;
1151 	bha_cmd(iot, ioh, name,
1152 	    sizeof(revision.cmd), (u_char *)&revision.cmd,
1153 	    sizeof(revision.reply), (u_char *)&revision.reply);
1154 	*p++ = revision.reply.firm_revision;
1155 	*p++ = '.';
1156 	*p++ = revision.reply.firm_version;
1157 	digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
1158 	bha_cmd(iot, ioh, name,
1159 	    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' &&
1164 	     revision.reply.firm_version >= '3')) {
1165 		digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
1166 		bha_cmd(iot, ioh, name,
1167 		    sizeof(digit.cmd), (u_char *)&digit.cmd,
1168 		    sizeof(digit.reply), (u_char *)&digit.reply);
1169 		*p++ = digit.reply.digit;
1170 	}
1171 	while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
1172 		p--;
1173 	*p = '\0';
1174 
1175 	/*
1176 	 * Get the model number.
1177 	 *
1178 	 * Some boards do not handle the Inquire Board Model Number
1179 	 * command correctly, or don't give correct information.
1180 	 *
1181 	 * So, we use the Firmware Revision and Extended Setup
1182 	 * information to fixup the model number in these cases.
1183 	 *
1184 	 * The firmware version indicates:
1185 	 *
1186 	 *	5.xx	BusLogic "W" Series Host Adapters
1187 	 *		BT-948/958/958D
1188 	 *
1189 	 *	4.xx	BusLogic "C" Series Host Adapters
1190 	 *		BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
1191 	 *
1192 	 *	3.xx	BusLogic "S" Series Host Adapters
1193 	 *		BT-747S/747D/757S/757D/445S/545S/542D
1194 	 *		BT-542B/742A (revision H)
1195 	 *
1196 	 *	2.xx	BusLogic "A" Series Host Adapters
1197 	 *		BT-542B/742A (revision G and below)
1198 	 *
1199 	 *	0.xx	AMI FastDisk VLB/EISA BusLogic Clone Host Adapter
1200 	 */
1201 	if (inquire.reply.bus_type == BHA_BUS_TYPE_24BIT &&
1202 	    sc->sc_firmware[0] < '3')
1203 		sprintf(sc->sc_model, "542B");
1204 	else if (inquire.reply.bus_type == BHA_BUS_TYPE_32BIT &&
1205 	    sc->sc_firmware[0] == '2' &&
1206 	    (sc->sc_firmware[2] == '1' ||
1207 	     (sc->sc_firmware[2] == '2' && sc->sc_firmware[3] == '0')))
1208 		sprintf(sc->sc_model, "742A");
1209 	else if (inquire.reply.bus_type == BHA_BUS_TYPE_32BIT &&
1210 	    sc->sc_firmware[0] == '0')
1211 		sprintf(sc->sc_model, "747A");
1212 	else {
1213 		p = sc->sc_model;
1214 		model.cmd.opcode = BHA_INQUIRE_MODEL;
1215 		model.cmd.len = sizeof(model.reply);
1216 		bha_cmd(iot, ioh, name,
1217 		    sizeof(model.cmd), (u_char *)&model.cmd,
1218 		    sizeof(model.reply), (u_char *)&model.reply);
1219 		*p++ = model.reply.id[0];
1220 		*p++ = model.reply.id[1];
1221 		*p++ = model.reply.id[2];
1222 		*p++ = model.reply.id[3];
1223 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1224 			p--;
1225 		*p++ = model.reply.version[0];
1226 		*p++ = model.reply.version[1];
1227 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1228 			p--;
1229 		*p = '\0';
1230 	}
1231 
1232 	/* Enable round-robin scheme - appeared at firmware rev. 3.31. */
1233 	if (strcmp(sc->sc_firmware, "3.31") >= 0)
1234 		sc->sc_flags |= BHAF_STRICT_ROUND_ROBIN;
1235 
1236 	/*
1237 	 * Determine some characteristics about our bus.
1238 	 */
1239 	if (inquire.reply.scsi_flags & BHA_SCSI_WIDE)
1240 		sc->sc_flags |= BHAF_WIDE;
1241 	if (inquire.reply.scsi_flags & BHA_SCSI_DIFFERENTIAL)
1242 		sc->sc_flags |= BHAF_DIFFERENTIAL;
1243 	if (inquire.reply.scsi_flags & BHA_SCSI_ULTRA)
1244 		sc->sc_flags |= BHAF_ULTRA;
1245 
1246 	/*
1247 	 * Determine some characterists of the board.
1248 	 */
1249 	sc->sc_max_dmaseg = inquire.reply.sg_limit;
1250 
1251 	/*
1252 	 * Determine the maximum CCB count and whether or not
1253 	 * tagged queueing is available on this host adapter.
1254 	 *
1255 	 * Tagged queueing works on:
1256 	 *
1257 	 *	"W" Series adapters
1258 	 *	"C" Series adapters with firmware >= 4.22
1259 	 *	"S" Series adapters with firmware >= 3.35
1260 	 *
1261 	 * The internal CCB counts are:
1262 	 *
1263 	 *	192	BT-948/958/958D
1264 	 *	100	BT-946C/956C/956CD/747C/757C/757CD/445C
1265 	 *	50	BT-545C/540CF
1266 	 *	30	BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
1267 	 */
1268 	switch (sc->sc_firmware[0]) {
1269 	case '5':
1270 		sc->sc_max_ccbs = 192;
1271 		sc->sc_flags |= BHAF_TAGGED_QUEUEING;
1272 		break;
1273 
1274 	case '4':
1275 		if (sc->sc_model[0] == '5')
1276 			sc->sc_max_ccbs = 50;
1277 		else
1278 			sc->sc_max_ccbs = 100;
1279 		if (strcmp(sc->sc_firmware, "4.22") >= 0)
1280 			sc->sc_flags |= BHAF_TAGGED_QUEUEING;
1281 		break;
1282 
1283 	case '3':
1284 		if (strcmp(sc->sc_firmware, "3.35") >= 0)
1285 			sc->sc_flags |= BHAF_TAGGED_QUEUEING;
1286 		/* FALLTHROUGH */
1287 
1288 	default:
1289 		sc->sc_max_ccbs = 30;
1290 	}
1291 
1292 	/*
1293 	 * Set the mailbox count to precisely the number of HW CCBs
1294 	 * available.  A mailbox isn't required while a CCB is executing,
1295 	 * but this allows us to actually enqueue up to our resource
1296 	 * limit.
1297 	 *
1298 	 * This will keep the mailbox count small on boards which don't
1299 	 * have strict round-robin (they have to scan the entire set of
1300 	 * mailboxes each time they run a command).
1301 	 */
1302 	sc->sc_mbox_count = sc->sc_max_ccbs;
1303 
1304 	/*
1305 	 * Obtain setup information.
1306 	 */
1307 	rlen = sizeof(setup.reply) +
1308 	    ((sc->sc_flags & BHAF_WIDE) ? sizeof(setup.reply_w) : 0);
1309 	setup.cmd.opcode = BHA_INQUIRE_SETUP;
1310 	setup.cmd.len = rlen;
1311 	bha_cmd(iot, ioh, name,
1312 	    sizeof(setup.cmd), (u_char *)&setup.cmd,
1313 	    rlen, (u_char *)&setup.reply);
1314 
1315 	printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
1316 	    sc->sc_model, sc->sc_firmware);
1317 
1318 	printf("%s: %d H/W CCBs", sc->sc_dev.dv_xname, sc->sc_max_ccbs);
1319 	if (setup.reply.sync_neg)
1320 		printf(", sync");
1321 	if (setup.reply.parity)
1322 		printf(", parity");
1323 	if (sc->sc_flags & BHAF_TAGGED_QUEUEING)
1324 		printf(", tagged queueing");
1325 	if (sc->sc_flags & BHAF_WIDE_LUN)
1326 		printf(", wide LUN support");
1327 	printf("\n");
1328 
1329 	/*
1330 	 * Poll targets 0 - 7.
1331 	 */
1332 	devices.cmd.opcode = BHA_INQUIRE_DEVICES;
1333 	bha_cmd(iot, ioh, name,
1334 	    sizeof(devices.cmd), (u_char *)&devices.cmd,
1335 	    sizeof(devices.reply), (u_char *)&devices.reply);
1336 
1337 	/* Count installed units. */
1338 	initial_ccbs = 0;
1339 	for (i = 0; i < 8; i++) {
1340 		for (j = 0; j < 8; j++) {
1341 			if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1342 				initial_ccbs++;
1343 		}
1344 	}
1345 
1346 	/*
1347 	 * Poll targets 8 - 15 if we have a wide bus.
1348 	 */
1349 	if (sc->sc_flags & BHAF_WIDE) {
1350 		devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
1351 		bha_cmd(iot, ioh, name,
1352 		    sizeof(devices.cmd), (u_char *)&devices.cmd,
1353 		    sizeof(devices.reply), (u_char *)&devices.reply);
1354 
1355 		for (i = 0; i < 8; i++) {
1356 			for (j = 0; j < 8; j++) {
1357 				if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1358 					initial_ccbs++;
1359 			}
1360 		}
1361 	}
1362 
1363 	/*
1364 	 * Double the initial CCB count, for good measure.
1365 	 */
1366 	initial_ccbs *= 2;
1367 
1368 	/*
1369 	 * Sanity check the initial CCB count; don't create more than
1370 	 * we can enqueue (sc_max_ccbs), and make sure there are some
1371 	 * at all.
1372 	 */
1373 	if (initial_ccbs > sc->sc_max_ccbs)
1374 		initial_ccbs = sc->sc_max_ccbs;
1375 	if (initial_ccbs == 0)
1376 		initial_ccbs = 2;
1377 
1378 	return (initial_ccbs);
1379 }
1380 
1381 /*
1382  * bha_init:
1383  *
1384  *	Initialize the board.
1385  */
1386 int
1387 bha_init(sc)
1388 	struct bha_softc *sc;
1389 {
1390 	char *name = sc->sc_dev.dv_xname;
1391 	struct bha_toggle toggle;
1392 	struct bha_mailbox mailbox;
1393 	struct bha_mbx_out *mbo;
1394 	struct bha_mbx_in *mbi;
1395 	int i;
1396 
1397 	/*
1398 	 * Set up the mailbox.  We always run the mailbox in round-robin.
1399 	 */
1400 	for (i = 0; i < sc->sc_mbox_count; i++) {
1401 		mbo = &sc->sc_mbo[i];
1402 		mbi = &sc->sc_mbi[i];
1403 
1404 		mbo->cmd = BHA_MBO_FREE;
1405 		BHA_MBO_SYNC(sc, mbo, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1406 
1407 		mbi->comp_stat = BHA_MBI_FREE;
1408 		BHA_MBI_SYNC(sc, mbi, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1409 	}
1410 
1411 	sc->sc_cmbo = sc->sc_tmbo = &sc->sc_mbo[0];
1412 	sc->sc_tmbi = &sc->sc_mbi[0];
1413 
1414 	sc->sc_mbofull = 0;
1415 
1416 	/*
1417 	 * If the board supports strict round-robin, enable that.
1418 	 */
1419 	if (sc->sc_flags & BHAF_STRICT_ROUND_ROBIN) {
1420 		toggle.cmd.opcode = BHA_ROUND_ROBIN;
1421 		toggle.cmd.enable = 1;
1422 		bha_cmd(sc->sc_iot, sc->sc_ioh, name,
1423 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1424 		    0, NULL);
1425 	}
1426 
1427 	/*
1428 	 * Give the mailbox to the board.
1429 	 */
1430 	mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
1431 	mailbox.cmd.nmbx = sc->sc_mbox_count;
1432 	ltophys(sc->sc_dmamap_mbox->dm_segs[0].ds_addr, mailbox.cmd.addr);
1433 	bha_cmd(sc->sc_iot, sc->sc_ioh, name,
1434 	    sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
1435 	    0, (u_char *)0);
1436 
1437 	return (0);
1438 }
1439 
1440 /*****************************************************************************
1441  * CCB execution engine
1442  *****************************************************************************/
1443 
1444 /*
1445  * bha_queue_ccb:
1446  *
1447  *	Queue a CCB to be sent to the controller, and send it if possible.
1448  */
1449 void
1450 bha_queue_ccb(sc, ccb)
1451 	struct bha_softc *sc;
1452 	struct bha_ccb *ccb;
1453 {
1454 
1455 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
1456 	bha_start_ccbs(sc);
1457 }
1458 
1459 /*
1460  * bha_start_ccbs:
1461  *
1462  *	Send as many CCBs as we have empty mailboxes for.
1463  */
1464 void
1465 bha_start_ccbs(sc)
1466 	struct bha_softc *sc;
1467 {
1468 	bus_space_tag_t iot = sc->sc_iot;
1469 	bus_space_handle_t ioh = sc->sc_ioh;
1470 	struct bha_ccb_group *bcg;
1471 	struct bha_mbx_out *mbo;
1472 	struct bha_ccb *ccb;
1473 
1474 	mbo = sc->sc_tmbo;
1475 
1476 	while ((ccb = TAILQ_FIRST(&sc->sc_waiting_ccb)) != NULL) {
1477 		if (sc->sc_mbofull >= sc->sc_mbox_count) {
1478 #ifdef DIAGNOSTIC
1479 			if (sc->sc_mbofull > sc->sc_mbox_count)
1480 				panic("bha_start_ccbs: mbofull > mbox_count");
1481 #endif
1482 			/*
1483 			 * No mailboxes available; attempt to collect ones
1484 			 * that have already been used.
1485 			 */
1486 			bha_collect_mbo(sc);
1487 			if (sc->sc_mbofull == sc->sc_mbox_count) {
1488 				/*
1489 				 * Still no more available; have the
1490 				 * controller interrupt us when it
1491 				 * frees one.
1492 				 */
1493 				struct bha_toggle toggle;
1494 
1495 				toggle.cmd.opcode = BHA_MBO_INTR_EN;
1496 				toggle.cmd.enable = 1;
1497 				bha_cmd(iot, ioh, sc->sc_dev.dv_xname,
1498 				    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1499 				    0, (u_char *)0);
1500 				break;
1501 			}
1502 		}
1503 
1504 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
1505 #ifdef BHADIAG
1506 		ccb->flags |= CCB_SENDING;
1507 #endif
1508 
1509 		/*
1510 		 * Put the CCB in the mailbox.
1511 		 */
1512 		bcg = BHA_CCB_GROUP(ccb);
1513 		ltophys(bcg->bcg_dmamap->dm_segs[0].ds_addr +
1514 		    BHA_CCB_OFFSET(ccb), mbo->ccb_addr);
1515 		if (ccb->flags & CCB_ABORT)
1516 			mbo->cmd = BHA_MBO_ABORT;
1517 		else
1518 			mbo->cmd = BHA_MBO_START;
1519 
1520 		BHA_MBO_SYNC(sc, mbo,
1521 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1522 
1523 		/* Tell the card to poll immediately. */
1524 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
1525 
1526 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
1527 			callout_reset(&ccb->xs->xs_callout,
1528 			    (ccb->timeout * hz) / 1000, bha_timeout, ccb);
1529 
1530 		++sc->sc_mbofull;
1531 		mbo = bha_nextmbo(sc, mbo);
1532 	}
1533 
1534 	sc->sc_tmbo = mbo;
1535 }
1536 
1537 /*
1538  * bha_finish_ccbs:
1539  *
1540  *	Finalize the execution of CCBs in our incoming mailbox.
1541  */
1542 void
1543 bha_finish_ccbs(sc)
1544 	struct bha_softc *sc;
1545 {
1546 	struct bha_mbx_in *mbi;
1547 	struct bha_ccb *ccb;
1548 	int i;
1549 
1550 	mbi = sc->sc_tmbi;
1551 
1552 	BHA_MBI_SYNC(sc, mbi, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1553 
1554 	if (mbi->comp_stat == BHA_MBI_FREE) {
1555 		for (i = 0; i < sc->sc_mbox_count; i++) {
1556 			if (mbi->comp_stat != BHA_MBI_FREE) {
1557 #ifdef BHADIAG
1558 				/*
1559 				 * This can happen in normal operation if
1560 				 * we use all mailbox slots.
1561 				 */
1562 				printf("%s: mbi not in round-robin order\n",
1563 				    sc->sc_dev.dv_xname);
1564 #endif
1565 				goto again;
1566 			}
1567 			mbi = bha_nextmbi(sc, mbi);
1568 			BHA_MBI_SYNC(sc, mbi,
1569 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1570 		}
1571 #ifdef BHADIAGnot
1572 		printf("%s: mbi interrupt with no full mailboxes\n",
1573 		    sc->sc_dev.dv_xname);
1574 #endif
1575 		return;
1576 	}
1577 
1578  again:
1579 	do {
1580 		ccb = bha_ccb_phys_kv(sc, phystol(mbi->ccb_addr));
1581 		if (ccb == NULL) {
1582 			printf("%s: bad mbi ccb pointer 0x%08x; skipping\n",
1583 			    sc->sc_dev.dv_xname, phystol(mbi->ccb_addr));
1584 			goto next;
1585 		}
1586 
1587 		BHA_CCB_SYNC(sc, ccb,
1588 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1589 
1590 #ifdef BHADEBUG
1591 		if (bha_debug) {
1592 			struct scsi_generic *cmd = &ccb->scsi_cmd;
1593 			printf("op=%x %x %x %x %x %x\n",
1594 			    cmd->opcode, cmd->bytes[0], cmd->bytes[1],
1595 			    cmd->bytes[2], cmd->bytes[3], cmd->bytes[4]);
1596 			printf("comp_stat %x for mbi addr = 0x%p, ",
1597 			    mbi->comp_stat, mbi);
1598 			printf("ccb addr = %p\n", ccb);
1599 		}
1600 #endif /* BHADEBUG */
1601 
1602 		switch (mbi->comp_stat) {
1603 		case BHA_MBI_OK:
1604 		case BHA_MBI_ERROR:
1605 			if ((ccb->flags & CCB_ABORT) != 0) {
1606 				/*
1607 				 * If we already started an abort, wait for it
1608 				 * to complete before clearing the CCB.  We
1609 				 * could instead just clear CCB_SENDING, but
1610 				 * what if the mailbox was already received?
1611 				 * The worst that happens here is that we clear
1612 				 * the CCB a bit later than we need to.  BFD.
1613 				 */
1614 				goto next;
1615 			}
1616 			break;
1617 
1618 		case BHA_MBI_ABORT:
1619 		case BHA_MBI_UNKNOWN:
1620 			/*
1621 			 * Even if the CCB wasn't found, we clear it anyway.
1622 			 * See preceding comment.
1623 			 */
1624 			break;
1625 
1626 		default:
1627 			printf("%s: bad mbi comp_stat %02x; skipping\n",
1628 			    sc->sc_dev.dv_xname, mbi->comp_stat);
1629 			goto next;
1630 		}
1631 
1632 		callout_stop(&ccb->xs->xs_callout);
1633 		bha_done(sc, ccb);
1634 
1635 	next:
1636 		mbi->comp_stat = BHA_MBI_FREE;
1637 		BHA_CCB_SYNC(sc, ccb,
1638 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1639 
1640 		mbi = bha_nextmbi(sc, mbi);
1641 		BHA_MBI_SYNC(sc, mbi,
1642 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1643 	} while (mbi->comp_stat != BHA_MBI_FREE);
1644 
1645 	sc->sc_tmbi = mbi;
1646 }
1647 
1648 /*****************************************************************************
1649  * Mailbox management functions.
1650  *****************************************************************************/
1651 
1652 /*
1653  * bha_create_mailbox:
1654  *
1655  *	Create the mailbox structures.  Helper function for bha_attach().
1656  *
1657  *	NOTE: The Buslogic hardware only gets one DMA address for the
1658  *	mailbox!  It expects:
1659  *
1660  *		mailbox_out[mailbox_size]
1661  *		mailbox_in[mailbox_size]
1662  */
1663 int
1664 bha_create_mailbox(sc)
1665 	struct bha_softc *sc;
1666 {
1667 	bus_dma_segment_t seg;
1668 	size_t size;
1669 	int error, rseg;
1670 
1671 	size = (sizeof(struct bha_mbx_out) * sc->sc_mbox_count) +
1672 	       (sizeof(struct bha_mbx_in)  * sc->sc_mbox_count);
1673 
1674 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg,
1675 	    1, &rseg, sc->sc_dmaflags);
1676 	if (error) {
1677 		printf("%s: unable to allocate mailboxes, error = %d\n",
1678 		    sc->sc_dev.dv_xname, error);
1679 		goto bad_0;
1680 	}
1681 
1682 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
1683 	    (caddr_t *)&sc->sc_mbo, sc->sc_dmaflags | BUS_DMA_COHERENT);
1684 	if (error) {
1685 		printf("%s: unable to map mailboxes, error = %d\n",
1686 		    sc->sc_dev.dv_xname, error);
1687 		goto bad_1;
1688 	}
1689 
1690 	memset(sc->sc_mbo, 0, size);
1691 
1692 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1693 	    sc->sc_dmaflags, &sc->sc_dmamap_mbox);
1694 	if (error) {
1695 		printf("%s: unable to create mailbox DMA map, error = %d\n",
1696 		    sc->sc_dev.dv_xname, error);
1697 		goto bad_2;
1698 	}
1699 
1700 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox,
1701 	    sc->sc_mbo, size, NULL, 0);
1702 	if (error) {
1703 		printf("%s: unable to load mailbox DMA map, error = %d\n",
1704 		    sc->sc_dev.dv_xname, error);
1705 		goto bad_3;
1706 	}
1707 
1708 	sc->sc_mbi = (struct bha_mbx_in *)(sc->sc_mbo + sc->sc_mbox_count);
1709 
1710 	return (0);
1711 
1712  bad_3:
1713 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmamap_mbox);
1714  bad_2:
1715 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_mbo, size);
1716  bad_1:
1717 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1718  bad_0:
1719 	return (error);
1720 }
1721 
1722 /*
1723  * bha_collect_mbo:
1724  *
1725  *	Garbage collect mailboxes that are no longer in use.
1726  */
1727 void
1728 bha_collect_mbo(sc)
1729 	struct bha_softc *sc;
1730 {
1731 	struct bha_mbx_out *mbo;
1732 #ifdef BHADIAG
1733 	struct bha_ccb *ccb;
1734 #endif
1735 
1736 	mbo = sc->sc_cmbo;
1737 
1738 	while (sc->sc_mbofull > 0) {
1739 		BHA_MBO_SYNC(sc, mbo,
1740 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1741 		if (mbo->cmd != BHA_MBO_FREE)
1742 			break;
1743 
1744 #ifdef BHADIAG
1745 		ccb = bha_ccb_phys_kv(sc, phystol(mbo->ccb_addr));
1746 		ccb->flags &= ~CCB_SENDING;
1747 #endif
1748 
1749 		--sc->sc_mbofull;
1750 		mbo = bha_nextmbo(sc, mbo);
1751 	}
1752 
1753 	sc->sc_cmbo = mbo;
1754 }
1755 
1756 /*****************************************************************************
1757  * CCB management functions
1758  *****************************************************************************/
1759 
1760 __inline void bha_reset_ccb __P((struct bha_ccb *));
1761 
1762 __inline void
1763 bha_reset_ccb(ccb)
1764 	struct bha_ccb *ccb;
1765 {
1766 
1767 	ccb->flags = 0;
1768 }
1769 
1770 /*
1771  * bha_create_ccbs:
1772  *
1773  *	Create a set of CCBs.
1774  *
1775  *	We determine the target CCB count, and then keep creating them
1776  *	until we reach the target, or fail.  CCBs that are allocated
1777  *	but not "created" are left on the allocating list.
1778  */
1779 void
1780 bha_create_ccbs(sc, count)
1781 	struct bha_softc *sc;
1782 	int count;
1783 {
1784 	struct bha_ccb_group *bcg;
1785 	struct bha_ccb *ccb;
1786 	bus_dma_segment_t seg;
1787 	bus_dmamap_t ccbmap;
1788 	int target, i, error, rseg;
1789 
1790 	/*
1791 	 * If the current CCB count is already the max number we're
1792 	 * allowed to have, bail out now.
1793 	 */
1794 	if (sc->sc_cur_ccbs == sc->sc_max_ccbs)
1795 		return;
1796 
1797 	/*
1798 	 * Compute our target count, and clamp it down to the max
1799 	 * number we're allowed to have.
1800 	 */
1801 	target = sc->sc_cur_ccbs + count;
1802 	if (target > sc->sc_max_ccbs)
1803 		target = sc->sc_max_ccbs;
1804 
1805 	/*
1806 	 * If there are CCBs on the allocating list, don't allocate a
1807 	 * CCB group yet.
1808 	 */
1809 	if (TAILQ_FIRST(&sc->sc_allocating_ccbs) != NULL)
1810 		goto have_allocating_ccbs;
1811 
1812  allocate_group:
1813 	error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE,
1814 	    PAGE_SIZE, 0, &seg, 1, &rseg, sc->sc_dmaflags | BUS_DMA_NOWAIT);
1815 	if (error) {
1816 		printf("%s: unable to allocate CCB group, error = %d\n",
1817 		    sc->sc_dev.dv_xname, error);
1818 		goto bad_0;
1819 	}
1820 
1821 	error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
1822 	    (caddr_t *)&bcg,
1823 	    sc->sc_dmaflags | BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
1824 	if (error) {
1825 		printf("%s: unable to map CCB group, error = %d\n",
1826 		    sc->sc_dev.dv_xname, error);
1827 		goto bad_1;
1828 	}
1829 
1830 	memset(bcg, 0, PAGE_SIZE);
1831 
1832 	error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE,
1833 	    1, PAGE_SIZE, 0, sc->sc_dmaflags | BUS_DMA_NOWAIT, &ccbmap);
1834 	if (error) {
1835 		printf("%s: unable to create CCB group DMA map, error = %d\n",
1836 		    sc->sc_dev.dv_xname, error);
1837 		goto bad_2;
1838 	}
1839 
1840 	error = bus_dmamap_load(sc->sc_dmat, ccbmap, bcg, PAGE_SIZE, NULL,
1841 	    sc->sc_dmaflags | BUS_DMA_NOWAIT);
1842 	if (error) {
1843 		printf("%s: unable to load CCB group DMA map, error = %d\n",
1844 		    sc->sc_dev.dv_xname, error);
1845 		goto bad_3;
1846 	}
1847 
1848 	bcg->bcg_dmamap = ccbmap;
1849 
1850 #ifdef DIAGNOSTIC
1851 	if (BHA_CCB_GROUP(&bcg->bcg_ccbs[0]) !=
1852 	    BHA_CCB_GROUP(&bcg->bcg_ccbs[bha_ccbs_per_group - 1]))
1853 		panic("bha_create_ccbs: CCB group size botch");
1854 #endif
1855 
1856 	/*
1857 	 * Add all of the CCBs in this group to the allocating list.
1858 	 */
1859 	for (i = 0; i < bha_ccbs_per_group; i++) {
1860 		ccb = &bcg->bcg_ccbs[i];
1861 		TAILQ_INSERT_TAIL(&sc->sc_allocating_ccbs, ccb, chain);
1862 	}
1863 
1864  have_allocating_ccbs:
1865 	/*
1866 	 * Loop over the allocating list until we reach our CCB target.
1867 	 * If we run out on the list, we'll allocate another group's
1868 	 * worth.
1869 	 */
1870 	while (sc->sc_cur_ccbs < target) {
1871 		ccb = TAILQ_FIRST(&sc->sc_allocating_ccbs);
1872 		if (ccb == NULL)
1873 			goto allocate_group;
1874 		if (bha_init_ccb(sc, ccb) != 0) {
1875 			/*
1876 			 * We were unable to initialize the CCB.
1877 			 * This is likely due to a resource shortage,
1878 			 * so bail out now.
1879 			 */
1880 			return;
1881 		}
1882 	}
1883 
1884 	/*
1885 	 * If we got here, we've reached our target!
1886 	 */
1887 	return;
1888 
1889  bad_3:
1890 	bus_dmamap_destroy(sc->sc_dmat, ccbmap);
1891  bad_2:
1892 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)bcg, PAGE_SIZE);
1893  bad_1:
1894 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
1895  bad_0:
1896 	return;
1897 }
1898 
1899 /*
1900  * bha_init_ccb:
1901  *
1902  *	Initialize a CCB; helper function for bha_create_ccbs().
1903  */
1904 int
1905 bha_init_ccb(sc, ccb)
1906 	struct bha_softc *sc;
1907 	struct bha_ccb *ccb;
1908 {
1909 	struct bha_ccb_group *bcg = BHA_CCB_GROUP(ccb);
1910 	int hashnum, error;
1911 
1912 	/*
1913 	 * Create the DMA map for this CCB.
1914 	 *
1915 	 * XXX ALLOCNOW is a hack to prevent bounce buffer shortages
1916 	 * XXX in the ISA case.  A better solution is needed.
1917 	 */
1918 	error = bus_dmamap_create(sc->sc_dmat, BHA_MAXXFER, BHA_NSEG,
1919 	    BHA_MAXXFER, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
1920 	    &ccb->dmamap_xfer);
1921 	if (error) {
1922 		printf("%s: unable to create CCB DMA map, error = %d\n",
1923 		    sc->sc_dev.dv_xname, error);
1924 		return (error);
1925 	}
1926 
1927 	TAILQ_REMOVE(&sc->sc_allocating_ccbs, ccb, chain);
1928 
1929 	/*
1930 	 * Put the CCB into the phystokv hash table.
1931 	 */
1932 	ccb->hashkey = bcg->bcg_dmamap->dm_segs[0].ds_addr +
1933 	    BHA_CCB_OFFSET(ccb);
1934 	hashnum = CCB_HASH(ccb->hashkey);
1935 	ccb->nexthash = sc->sc_ccbhash[hashnum];
1936 	sc->sc_ccbhash[hashnum] = ccb;
1937 	bha_reset_ccb(ccb);
1938 
1939 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
1940 	sc->sc_cur_ccbs++;
1941 
1942 	return (0);
1943 }
1944 
1945 /*
1946  * bha_get_ccb:
1947  *
1948  *	Get a CCB for the SCSI operation.  If there are none left,
1949  *	wait until one becomes available, if we can.
1950  */
1951 struct bha_ccb *
1952 bha_get_ccb(sc)
1953 	struct bha_softc *sc;
1954 {
1955 	struct bha_ccb *ccb;
1956 	int s;
1957 
1958 	s = splbio();
1959 	ccb = TAILQ_FIRST(&sc->sc_free_ccb);
1960 	if (ccb != NULL) {
1961 		TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
1962 		ccb->flags |= CCB_ALLOC;
1963 	}
1964 	splx(s);
1965 	return (ccb);
1966 }
1967 
1968 /*
1969  * bha_free_ccb:
1970  *
1971  *	Put a CCB back onto the free list.
1972  */
1973 void
1974 bha_free_ccb(sc, ccb)
1975 	struct bha_softc *sc;
1976 	struct bha_ccb *ccb;
1977 {
1978 	int s;
1979 
1980 	s = splbio();
1981 	bha_reset_ccb(ccb);
1982 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
1983 	splx(s);
1984 }
1985 
1986 /*
1987  * bha_ccb_phys_kv:
1988  *
1989  *	Given a CCB DMA address, locate the CCB in kernel virtual space.
1990  */
1991 struct bha_ccb *
1992 bha_ccb_phys_kv(sc, ccb_phys)
1993 	struct bha_softc *sc;
1994 	bus_addr_t ccb_phys;
1995 {
1996 	int hashnum = CCB_HASH(ccb_phys);
1997 	struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
1998 
1999 	while (ccb) {
2000 		if (ccb->hashkey == ccb_phys)
2001 			break;
2002 		ccb = ccb->nexthash;
2003 	}
2004 	return (ccb);
2005 }
2006