1 /*	$OpenBSD: bha.c,v 1.6 2003/10/21 18:58:49 jmc Exp $	*/
2 /*	$NetBSD: bha.c,v 1.27 1998/11/19 21:53:00 thorpej Exp $	*/
3 
4 #undef BHADEBUG
5 #ifdef DDB
6 #define	integrate
7 #else
8 #define	integrate	static inline
9 #endif
10 
11 /*-
12  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
13  * All rights reserved.
14  *
15  * This code is derived from software contributed to The NetBSD Foundation
16  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
17  * Simulation Facility, NASA Ames Research Center.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *	This product includes software developed by the NetBSD
30  *	Foundation, Inc. and its contributors.
31  * 4. Neither the name of The NetBSD Foundation nor the names of its
32  *    contributors may be used to endorse or promote products derived
33  *    from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
36  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
37  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
43  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  * POSSIBILITY OF SUCH DAMAGE.
46  */
47 
48 /*
49  * Originally written by Julian Elischer (julian@tfs.com)
50  * for TRW Financial Systems for use under the MACH(2.5) operating system.
51  *
52  * TRW Financial Systems, in accordance with their agreement with Carnegie
53  * Mellon University, makes this software available to CMU to distribute
54  * or use in any manner that they see fit as long as this message is kept with
55  * the software. For this reason TFS also grants any other persons or
56  * organisations permission to use or modify this software.
57  *
58  * TFS supplies this software to be publicly redistributed
59  * on the understanding that TFS is not responsible for the correct
60  * functioning of this software in any circumstances.
61  */
62 
63 #include <sys/types.h>
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/errno.h>
68 #include <sys/ioctl.h>
69 #include <sys/device.h>
70 #include <sys/malloc.h>
71 #include <sys/buf.h>
72 #include <sys/proc.h>
73 #include <sys/user.h>
74 
75 #include <machine/bus.h>
76 #include <machine/intr.h>
77 
78 #include <scsi/scsi_all.h>
79 #include <scsi/scsiconf.h>
80 
81 #include <dev/ic/bhareg.h>
82 #include <dev/ic/bhavar.h>
83 
84 #ifndef DDB
85 #define Debugger() panic("should call debugger here (bha.c)")
86 #endif /* ! DDB */
87 
88 #define	BHA_MAXXFER	((BHA_NSEG - 1) << PGSHIFT)
89 #define	ISWIDE(sc)	((sc)->sc_iswide)
90 
91 #ifdef BHADEBUG
92 int     bha_debug = 1;
93 #endif /* BHADEBUG */
94 
95 integrate void bha_finish_ccbs(struct bha_softc *);
96 integrate void bha_reset_ccb(struct bha_softc *, struct bha_ccb *);
97 void bha_free_ccb(struct bha_softc *, struct bha_ccb *);
98 integrate int bha_init_ccb(struct bha_softc *, struct bha_ccb *);
99 struct bha_ccb *bha_get_ccb(struct bha_softc *, int);
100 struct bha_ccb *bha_ccb_phys_kv(struct bha_softc *, u_long);
101 void bha_queue_ccb(struct bha_softc *, struct bha_ccb *);
102 void bha_collect_mbo(struct bha_softc *);
103 void bha_start_ccbs(struct bha_softc *);
104 void bha_done(struct bha_softc *, struct bha_ccb *);
105 int bha_init(struct bha_softc *);
106 void bhaminphys(struct buf *);
107 int bha_scsi_cmd(struct scsi_xfer *);
108 int bha_poll(struct bha_softc *, struct scsi_xfer *, int);
109 void bha_timeout(void *arg);
110 int bha_create_ccbs(struct bha_softc *, struct bha_ccb *, int);
111 void bha_enqueue(struct bha_softc *, struct scsi_xfer *, int);
112 struct scsi_xfer *bha_dequeue(struct bha_softc *);
113 
114 struct cfdriver bha_cd = {
115 	NULL, "bha", DV_DULL
116 };
117 
118 /* the below structure is so we have a default dev struct for out link struct */
119 struct scsi_device bha_dev = {
120 	NULL,			/* Use default error handler */
121 	NULL,			/* have a queue, served by this */
122 	NULL,			/* have no async handler */
123 	NULL,			/* Use default 'done' routine */
124 };
125 
126 #define BHA_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
127 #define	BHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
128 
129 /*
130  * Insert a scsi_xfer into the software queue.  We overload xs->free_list
131  * to avoid having to allocate additional resources (since we're used
132  * only during resource shortages anyhow.
133  */
134 void
bha_enqueue(sc,xs,infront)135 bha_enqueue(sc, xs, infront)
136 	struct bha_softc *sc;
137 	struct scsi_xfer *xs;
138 	int infront;
139 {
140 
141 	if (infront || sc->sc_queue.lh_first == NULL) {
142 		if (sc->sc_queue.lh_first == NULL)
143 			sc->sc_queuelast = xs;
144 		LIST_INSERT_HEAD(&sc->sc_queue, xs, free_list);
145 		return;
146 	}
147 
148 	LIST_INSERT_AFTER(sc->sc_queuelast, xs, free_list);
149 	sc->sc_queuelast = xs;
150 }
151 
152 /*
153  * Pull a scsi_xfer off the front of the software queue.
154  */
155 struct scsi_xfer *
bha_dequeue(sc)156 bha_dequeue(sc)
157 	struct bha_softc *sc;
158 {
159 	struct scsi_xfer *xs;
160 
161 	xs = sc->sc_queue.lh_first;
162 	LIST_REMOVE(xs, free_list);
163 
164 	if (sc->sc_queue.lh_first == NULL)
165 		sc->sc_queuelast = NULL;
166 
167 	return (xs);
168 }
169 
170 /*
171  * bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
172  *
173  * Activate Adapter command
174  *    icnt:   number of args (outbound bytes including opcode)
175  *    ibuf:   argument buffer
176  *    ocnt:   number of expected returned bytes
177  *    obuf:   result buffer
178  *    wait:   number of seconds to wait for response
179  *
180  * Performs an adapter command through the ports.  Not to be confused with a
181  * scsi command, which is read in via the dma; one of the adapter commands
182  * tells it to read in a scsi command.
183  */
184 int
bha_cmd(iot,ioh,sc,icnt,ibuf,ocnt,obuf)185 bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
186 	bus_space_tag_t iot;
187 	bus_space_handle_t ioh;
188 	struct bha_softc *sc;
189 	int icnt, ocnt;
190 	u_char *ibuf, *obuf;
191 {
192 	const char *name;
193 	register int i;
194 	int wait;
195 	u_char sts;
196 	u_char opcode = ibuf[0];
197 
198 	if (sc != NULL)
199 		name = sc->sc_dev.dv_xname;
200 	else
201 		name = "(bha probe)";
202 
203 	/*
204 	 * Calculate a reasonable timeout for the command.
205 	 */
206 	switch (opcode) {
207 	case BHA_INQUIRE_DEVICES:
208 	case BHA_INQUIRE_DEVICES_2:
209 		wait = 90 * 20000;
210 		break;
211 	default:
212 		wait = 1 * 20000;
213 		break;
214 	}
215 
216 	/*
217 	 * Wait for the adapter to go idle, unless it's one of
218 	 * the commands which don't need this
219 	 */
220 	if (opcode != BHA_MBO_INTR_EN) {
221 		for (i = 20000; i; i--) {	/* 1 sec? */
222 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
223 			if (sts & BHA_STAT_IDLE)
224 				break;
225 			delay(50);
226 		}
227 		if (!i) {
228 			printf("%s: bha_cmd, host not idle(0x%x)\n",
229 			    name, sts);
230 			return (1);
231 		}
232 	}
233 	/*
234 	 * Now that it is idle, if we expect output, preflush the
235 	 * queue feeding to us.
236 	 */
237 	if (ocnt) {
238 		while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
239 		    BHA_STAT_DF)
240 			bus_space_read_1(iot, ioh, BHA_DATA_PORT);
241 	}
242 	/*
243 	 * Output the command and the number of arguments given
244 	 * for each byte, first check the port is empty.
245 	 */
246 	while (icnt--) {
247 		for (i = wait; i; i--) {
248 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
249 			if (!(sts & BHA_STAT_CDF))
250 				break;
251 			delay(50);
252 		}
253 		if (!i) {
254 			if (opcode != BHA_INQUIRE_REVISION)
255 				printf("%s: bha_cmd, cmd/data port full\n",
256 				    name);
257 			goto bad;
258 		}
259 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
260 	}
261 	/*
262 	 * If we expect input, loop that many times, each time,
263 	 * looking for the data register to have valid data
264 	 */
265 	while (ocnt--) {
266 		for (i = wait; i; i--) {
267 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
268 			if (sts & BHA_STAT_DF)
269 				break;
270 			delay(50);
271 		}
272 		if (!i) {
273 			if (opcode != BHA_INQUIRE_REVISION)
274 				printf("%s: bha_cmd, cmd/data port empty %d\n",
275 				    name, ocnt);
276 			goto bad;
277 		}
278 		*obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
279 	}
280 	/*
281 	 * Wait for the board to report a finished instruction.
282 	 * We may get an extra interrupt for the HACC signal, but this is
283 	 * unimportant.
284 	 */
285 	if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
286 		for (i = 20000; i; i--) {	/* 1 sec? */
287 			sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
288 			/* XXX Need to save this in the interrupt handler? */
289 			if (sts & BHA_INTR_HACC)
290 				break;
291 			delay(50);
292 		}
293 		if (!i) {
294 			printf("%s: bha_cmd, host not finished(0x%x)\n",
295 			    name, sts);
296 			return (1);
297 		}
298 	}
299 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
300 	return (0);
301 
302 bad:
303 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
304 	return (1);
305 }
306 
307 /*
308  * Attach all the sub-devices we can find
309  */
310 void
bha_attach(sc,bpd)311 bha_attach(sc, bpd)
312 	struct bha_softc *sc;
313 	struct bha_probe_data *bpd;
314 {
315 	int s;
316 
317 	/*
318 	 * Fill in the adapter.
319 	 */
320 	sc->sc_adapter.scsi_cmd = bha_scsi_cmd;
321 	sc->sc_adapter.scsi_minphys = bhaminphys;
322 
323 	/*
324 	 * fill in the prototype scsi_link.
325 	 */
326 	sc->sc_link.adapter_softc = sc;
327 	sc->sc_link.adapter_target = bpd->sc_scsi_dev;
328 	sc->sc_link.adapter = &sc->sc_adapter;
329 	sc->sc_link.device = &bha_dev;
330 	sc->sc_link.openings = 4;
331 
332 	TAILQ_INIT(&sc->sc_free_ccb);
333 	TAILQ_INIT(&sc->sc_waiting_ccb);
334 	LIST_INIT(&sc->sc_queue);
335 
336 	s = splbio();
337 	bha_inquire_setup_information(sc);
338 
339 	printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
340 	    sc->sc_model, sc->sc_firmware);
341 
342 	if (bha_init(sc) != 0) {
343 		/* Error during initialization! */
344 		splx(s);
345 		return;
346 	}
347 
348 	splx(s);
349 
350 	/*
351 	 * ask the adapter what subunits are present
352 	 */
353 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
354 }
355 
356 integrate void
bha_finish_ccbs(sc)357 bha_finish_ccbs(sc)
358 	struct bha_softc *sc;
359 {
360 	struct bha_mbx_in *wmbi;
361 	struct bha_ccb *ccb;
362 	int i;
363 
364 	wmbi = wmbx->tmbi;
365 
366 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
367 	    0, sc->sc_dmamap_control->dm_mapsize,
368 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
369 
370 	if (wmbi->comp_stat == BHA_MBI_FREE) {
371 		for (i = 0; i < BHA_MBX_SIZE; i++) {
372 			if (wmbi->comp_stat != BHA_MBI_FREE) {
373 				printf("%s: mbi not in round-robin order\n",
374 				    sc->sc_dev.dv_xname);
375 				goto AGAIN;
376 			}
377 			bha_nextmbx(wmbi, wmbx, mbi);
378 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
379 			    0, sc->sc_dmamap_control->dm_mapsize,
380 			    BUS_DMASYNC_POSTREAD);
381 		}
382 #ifdef BHADIAGnot
383 		printf("%s: mbi interrupt with no full mailboxes\n",
384 		    sc->sc_dev.dv_xname);
385 #endif
386 		return;
387 	}
388 
389 AGAIN:
390 	do {
391 		ccb = bha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
392 		if (!ccb) {
393 			printf("%s: bad mbi ccb pointer; skipping\n",
394 			    sc->sc_dev.dv_xname);
395 			goto next;
396 		}
397 
398 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
399 		    0, sc->sc_dmamap_control->dm_mapsize,
400 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
401 
402 #ifdef BHADEBUG
403 		if (bha_debug) {
404 			u_int8_t *cp = ccb->scsi_cmd.bytes;
405 			printf("op=%x %x %x %x %x %x\n",
406 				ccb->scsi_cmd.opcode,
407 				cp[0], cp[1], cp[2], cp[3], cp[4]);
408 			printf("stat %x for mbi addr = 0x%08x, ",
409 				wmbi->comp_stat, wmbi);
410 			printf("ccb addr = 0x%x\n", ccb);
411 		}
412 #endif /* BHADEBUG */
413 
414 		switch (wmbi->comp_stat) {
415 		case BHA_MBI_OK:
416 		case BHA_MBI_ERROR:
417 			if ((ccb->flags & CCB_ABORT) != 0) {
418 				/*
419 				 * If we already started an abort, wait for it
420 				 * to complete before clearing the CCB.  We
421 				 * could instead just clear CCB_SENDING, but
422 				 * what if the mailbox was already received?
423 				 * The worst that happens here is that we clear
424 				 * the CCB a bit later than we need to.  BFD.
425 				 */
426 				goto next;
427 			}
428 			break;
429 
430 		case BHA_MBI_ABORT:
431 		case BHA_MBI_UNKNOWN:
432 			/*
433 			 * Even if the CCB wasn't found, we clear it anyway.
434 			 * See preceding comment.
435 			 */
436 			break;
437 
438 		default:
439 			printf("%s: bad mbi status %02x; skipping\n",
440 			    sc->sc_dev.dv_xname, wmbi->comp_stat);
441 			goto next;
442 		}
443 
444 		timeout_del(&ccb->xs->stimeout);
445 		bha_done(sc, ccb);
446 
447 	next:
448 		wmbi->comp_stat = BHA_MBI_FREE;
449 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
450 		    0, sc->sc_dmamap_control->dm_mapsize,
451 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
452 		bha_nextmbx(wmbi, wmbx, mbi);
453 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
454 		    0, sc->sc_dmamap_control->dm_mapsize,
455 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
456 	} while (wmbi->comp_stat != BHA_MBI_FREE);
457 
458 	wmbx->tmbi = wmbi;
459 }
460 
461 /*
462  * Catch an interrupt from the adaptor
463  */
464 int
bha_intr(arg)465 bha_intr(arg)
466 	void *arg;
467 {
468 	struct bha_softc *sc = arg;
469 	bus_space_tag_t iot = sc->sc_iot;
470 	bus_space_handle_t ioh = sc->sc_ioh;
471 	u_char sts;
472 
473 #ifdef BHADEBUG
474 	printf("%s: bha_intr ", sc->sc_dev.dv_xname);
475 #endif /* BHADEBUG */
476 
477 	/*
478 	 * First acknowledge the interrupt, Then if it's not telling about
479 	 * a completed operation just return.
480 	 */
481 	sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
482 	if ((sts & BHA_INTR_ANYINTR) == 0)
483 		return (0);
484 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
485 
486 #ifdef BHADIAG
487 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
488 	bha_collect_mbo(sc);
489 #endif
490 
491 	/* Mail box out empty? */
492 	if (sts & BHA_INTR_MBOA) {
493 		struct bha_toggle toggle;
494 
495 		toggle.cmd.opcode = BHA_MBO_INTR_EN;
496 		toggle.cmd.enable = 0;
497 		bha_cmd(iot, ioh, sc,
498 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
499 		    0, (u_char *)0);
500 		bha_start_ccbs(sc);
501 	}
502 
503 	/* Mail box in full? */
504 	if (sts & BHA_INTR_MBIF)
505 		bha_finish_ccbs(sc);
506 
507 	return (1);
508 }
509 
510 integrate void
bha_reset_ccb(sc,ccb)511 bha_reset_ccb(sc, ccb)
512 	struct bha_softc *sc;
513 	struct bha_ccb *ccb;
514 {
515 
516 	ccb->flags = 0;
517 }
518 
519 /*
520  * A ccb is put onto the free list.
521  */
522 void
bha_free_ccb(sc,ccb)523 bha_free_ccb(sc, ccb)
524 	struct bha_softc *sc;
525 	struct bha_ccb *ccb;
526 {
527 	int s;
528 
529 	s = splbio();
530 
531 	bha_reset_ccb(sc, ccb);
532 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
533 
534 	/*
535 	 * If there were none, wake anybody waiting for one to come free,
536 	 * starting with queued entries.
537 	 */
538 	if (ccb->chain.tqe_next == 0)
539 		wakeup(&sc->sc_free_ccb);
540 
541 	splx(s);
542 }
543 
544 integrate int
bha_init_ccb(sc,ccb)545 bha_init_ccb(sc, ccb)
546 	struct bha_softc *sc;
547 	struct bha_ccb *ccb;
548 {
549 	bus_dma_tag_t dmat = sc->sc_dmat;
550 	int hashnum, error;
551 
552 	/*
553 	 * Create the DMA map for this CCB.
554 	 */
555 	error = bus_dmamap_create(dmat, BHA_MAXXFER, BHA_NSEG, BHA_MAXXFER,
556 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
557 	    &ccb->dmamap_xfer);
558 	if (error) {
559 		printf("%s: unable to create ccb DMA map, error = %d\n",
560 		    sc->sc_dev.dv_xname, error);
561 		return (error);
562 	}
563 
564 	/*
565 	 * put in the phystokv hash table
566 	 * Never gets taken out.
567 	 */
568 	ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
569 	    BHA_CCB_OFF(ccb);
570 	hashnum = CCB_HASH(ccb->hashkey);
571 	ccb->nexthash = sc->sc_ccbhash[hashnum];
572 	sc->sc_ccbhash[hashnum] = ccb;
573 	bha_reset_ccb(sc, ccb);
574 	return (0);
575 }
576 
577 /*
578  * Create a set of ccbs and add them to the free list.  Called once
579  * by bha_init().  We return the number of CCBs successfully created.
580  */
581 int
bha_create_ccbs(sc,ccbstore,count)582 bha_create_ccbs(sc, ccbstore, count)
583 	struct bha_softc *sc;
584 	struct bha_ccb *ccbstore;
585 	int count;
586 {
587 	struct bha_ccb *ccb;
588 	int i, error;
589 
590 	bzero(ccbstore, sizeof(struct bha_ccb) * count);
591 	for (i = 0; i < count; i++) {
592 		ccb = &ccbstore[i];
593 		if ((error = bha_init_ccb(sc, ccb)) != 0) {
594 			printf("%s: unable to initialize ccb, error = %d\n",
595 			    sc->sc_dev.dv_xname, error);
596 			goto out;
597 		}
598 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
599 	}
600  out:
601 	return (i);
602 }
603 
604 /*
605  * Get a free ccb
606  *
607  * If there are none, see if we can allocate a new one.  If so, put it in
608  * the hash table too otherwise either return an error or sleep.
609  */
610 struct bha_ccb *
bha_get_ccb(sc,flags)611 bha_get_ccb(sc, flags)
612 	struct bha_softc *sc;
613 	int flags;
614 {
615 	struct bha_ccb *ccb;
616 	int s;
617 
618 	s = splbio();
619 
620 	/*
621 	 * If we can and have to, sleep waiting for one to come free
622 	 * but only if we can't allocate a new one.
623 	 */
624 	for (;;) {
625 		ccb = sc->sc_free_ccb.tqh_first;
626 		if (ccb) {
627 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
628 			break;
629 		}
630 		if ((flags & SCSI_NOSLEEP) != 0)
631 			goto out;
632 		tsleep(&sc->sc_free_ccb, PRIBIO, "bhaccb", 0);
633 	}
634 
635 	ccb->flags |= CCB_ALLOC;
636 
637 out:
638 	splx(s);
639 	return (ccb);
640 }
641 
642 /*
643  * Given a physical address, find the ccb that it corresponds to.
644  */
645 struct bha_ccb *
bha_ccb_phys_kv(sc,ccb_phys)646 bha_ccb_phys_kv(sc, ccb_phys)
647 	struct bha_softc *sc;
648 	u_long ccb_phys;
649 {
650 	int hashnum = CCB_HASH(ccb_phys);
651 	struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
652 
653 	while (ccb) {
654 		if (ccb->hashkey == ccb_phys)
655 			break;
656 		ccb = ccb->nexthash;
657 	}
658 	return (ccb);
659 }
660 
661 /*
662  * Queue a CCB to be sent to the controller, and send it if possible.
663  */
664 void
bha_queue_ccb(sc,ccb)665 bha_queue_ccb(sc, ccb)
666 	struct bha_softc *sc;
667 	struct bha_ccb *ccb;
668 {
669 
670 	timeout_set(&ccb->xs->stimeout, bha_timeout, ccb);
671 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
672 	bha_start_ccbs(sc);
673 }
674 
675 /*
676  * Garbage collect mailboxes that are no longer in use.
677  */
678 void
bha_collect_mbo(sc)679 bha_collect_mbo(sc)
680 	struct bha_softc *sc;
681 {
682 	struct bha_mbx_out *wmbo;	/* Mail Box Out pointer */
683 #ifdef BHADIAG
684 	struct bha_ccb *ccb;
685 #endif
686 
687 	wmbo = wmbx->cmbo;
688 
689 	while (sc->sc_mbofull > 0) {
690 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
691 		    0, sc->sc_dmamap_control->dm_mapsize,
692 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
693 		if (wmbo->cmd != BHA_MBO_FREE)
694 			break;
695 
696 #ifdef BHADIAG
697 		ccb = bha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
698 		ccb->flags &= ~CCB_SENDING;
699 #endif
700 
701 		--sc->sc_mbofull;
702 		bha_nextmbx(wmbo, wmbx, mbo);
703 	}
704 
705 	wmbx->cmbo = wmbo;
706 }
707 
708 /*
709  * Send as many CCBs as we have empty mailboxes for.
710  */
711 void
bha_start_ccbs(sc)712 bha_start_ccbs(sc)
713 	struct bha_softc *sc;
714 {
715 	bus_space_tag_t iot = sc->sc_iot;
716 	bus_space_handle_t ioh = sc->sc_ioh;
717 	struct bha_mbx_out *wmbo;	/* Mail Box Out pointer */
718 	struct bha_ccb *ccb;
719 	struct scsi_xfer *xs;
720 
721 	wmbo = wmbx->tmbo;
722 
723 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
724 
725 		xs = ccb->xs;
726 		if (sc->sc_mbofull >= BHA_MBX_SIZE) {
727 			bha_collect_mbo(sc);
728 			if (sc->sc_mbofull >= BHA_MBX_SIZE) {
729 				struct bha_toggle toggle;
730 
731 				toggle.cmd.opcode = BHA_MBO_INTR_EN;
732 				toggle.cmd.enable = 1;
733 				bha_cmd(iot, ioh, sc,
734 				    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
735 				    0, (u_char *)0);
736 				break;
737 			}
738 		}
739 
740 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
741 #ifdef BHADIAG
742 		ccb->flags |= CCB_SENDING;
743 #endif
744 
745 		/* Link ccb to mbo. */
746 		ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
747 		    BHA_CCB_OFF(ccb), wmbo->ccb_addr);
748 		if (ccb->flags & CCB_ABORT)
749 			wmbo->cmd = BHA_MBO_ABORT;
750 		else
751 			wmbo->cmd = BHA_MBO_START;
752 
753 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
754 		    0, sc->sc_dmamap_control->dm_mapsize,
755 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
756 
757 		/* Tell the card to poll immediately. */
758 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
759 
760 		if ((xs->flags & SCSI_POLL) == 0)
761 			timeout_add(&xs->stimeout, (ccb->timeout * hz) / 1000);
762 
763 		++sc->sc_mbofull;
764 		bha_nextmbx(wmbo, wmbx, mbo);
765 	}
766 
767 	wmbx->tmbo = wmbo;
768 }
769 
770 /*
771  * We have a ccb which has been processed by the
772  * adaptor, now we look to see how the operation
773  * went. Wake up the owner if waiting
774  */
775 void
bha_done(sc,ccb)776 bha_done(sc, ccb)
777 	struct bha_softc *sc;
778 	struct bha_ccb *ccb;
779 {
780 	bus_dma_tag_t dmat = sc->sc_dmat;
781 	struct scsi_sense_data *s1, *s2;
782 	struct scsi_xfer *xs = ccb->xs;
783 
784 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("bha_done\n"));
785 
786 	/*
787 	 * If we were a data transfer, unload the map that described
788 	 * the data buffer.
789 	 */
790 	if (xs->datalen) {
791 		bus_dmamap_sync(dmat, ccb->dmamap_xfer,
792 		    0, ccb->dmamap_xfer->dm_mapsize,
793 		    (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_POSTREAD :
794 		    BUS_DMASYNC_POSTWRITE);
795 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
796 	}
797 
798 	/*
799 	 * Otherwise, put the results of the operation
800 	 * into the xfer and call whoever started it
801 	 */
802 #ifdef BHADIAG
803 	if (ccb->flags & CCB_SENDING) {
804 		printf("%s: exiting ccb still in transit!\n",
805 		    sc->sc_dev.dv_xname);
806 		Debugger();
807 		return;
808 	}
809 #endif
810 	if ((ccb->flags & CCB_ALLOC) == 0) {
811 		printf("%s: exiting ccb not allocated!\n",
812 		    sc->sc_dev.dv_xname);
813 		Debugger();
814 		return;
815 	}
816 	if (xs->error == XS_NOERROR) {
817 		if (ccb->host_stat != BHA_OK) {
818 			switch (ccb->host_stat) {
819 			case BHA_SEL_TIMEOUT:	/* No response */
820 				xs->error = XS_SELTIMEOUT;
821 				break;
822 			default:	/* Other scsi protocol messes */
823 				printf("%s: host_stat %x\n",
824 				    sc->sc_dev.dv_xname, ccb->host_stat);
825 				xs->error = XS_DRIVER_STUFFUP;
826 				break;
827 			}
828 		} else if (ccb->target_stat != SCSI_OK) {
829 			switch (ccb->target_stat) {
830 			case SCSI_CHECK:
831 				s1 = &ccb->scsi_sense;
832 				s2 = &xs->sense;
833 				*s2 = *s1;
834 				xs->error = XS_SENSE;
835 				break;
836 			case SCSI_BUSY:
837 				xs->error = XS_BUSY;
838 				break;
839 			default:
840 				printf("%s: target_stat %x\n",
841 				    sc->sc_dev.dv_xname, ccb->target_stat);
842 				xs->error = XS_DRIVER_STUFFUP;
843 				break;
844 			}
845 		} else
846 			xs->resid = 0;
847 	}
848 	bha_free_ccb(sc, ccb);
849 	xs->flags |= ITSDONE;
850 	scsi_done(xs);
851 
852 	/*
853 	 * If there are queue entries in the software queue, try to
854 	 * run the first one.  We should be more or less guaranteed
855 	 * to succeed, since we just freed a CCB.
856 	 *
857 	 * NOTE: bha_scsi_cmd() relies on our calling it with
858 	 * the first entry in the queue.
859 	 */
860 	if ((xs = sc->sc_queue.lh_first) != NULL)
861 		(void) bha_scsi_cmd(xs);
862 }
863 
864 /*
865  * Find the board and find it's irq/drq
866  */
867 int
bha_find(iot,ioh,sc)868 bha_find(iot, ioh, sc)
869 	bus_space_tag_t iot;
870 	bus_space_handle_t ioh;
871 	struct bha_probe_data *sc;
872 {
873 	int i, iswide;
874 	u_char sts;
875 	struct bha_extended_inquire inquire;
876 	struct bha_config config;
877 	int irq, drq;
878 
879 	/* Check something is at the ports we need to access */
880 	sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
881 	if (sts == 0xFF) {
882 #ifdef BHADEBUG
883 		if (bha_debug)
884 			printf("bha_find: Not present\n");
885 #endif /* BHADEBUG */
886 		return (0);
887 	}
888 
889 	/*
890 	 * Reset board, If it doesn't respond, assume
891 	 * that it's not there.. good for the probe
892 	 */
893 
894 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
895 	    BHA_CTRL_HRST | BHA_CTRL_SRST);
896 
897 	for (i = BHA_RESET_TIMEOUT; i--;) {
898 		delay(100);
899 		sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
900 		if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
901 			break;
902 	}
903 	if (i < 0) {
904 #ifdef BHADEBUG
905 		if (bha_debug)
906 			printf("bha_find: No answer from board a=%x sts=%b\n",
907 			    ioh, sts, BHA_STAT_BITS);
908 #endif /* BHADEBUG */
909 		return (0);
910 	}
911 
912 	/*
913 	 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
914 	 * interface. The native bha interface is not compatible with
915 	 * an aha. 1542. We need to ensure that we never match an
916 	 * Adaptec 1542. We must also avoid sending Adaptec-compatible
917 	 * commands to a real bha, lest it go into 1542 emulation mode.
918 	 * (On an indirect bus like ISA, we should always probe for BusLogic
919 	 * interfaces  before Adaptec interfaces).
920 	 */
921 
922 	/*
923 	 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
924 	 * for an extended-geometry register.  The 1542[AB] don't have one.
925 	 */
926 	sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
927 	if (sts == 0xFF)
928 		return (0);
929 
930 	/*
931 	 * Check that we actually know how to use this board.
932 	 */
933 	delay(1000);
934 	inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
935 	inquire.cmd.len = sizeof(inquire.reply);
936 	i = bha_cmd(iot, ioh, NULL,
937 	    sizeof(inquire.cmd), (u_char *)&inquire.cmd,
938 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
939 
940 	/*
941 	 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
942 	 * have the extended-geometry register and also respond to
943 	 * BHA_INQUIRE_EXTENDED.  Make sure we never  match such cards,
944 	 * by checking the size of the reply is what a BusLogic card returns.
945 	 */
946 	if (i) {
947 #ifdef BHADEBUG
948 		printf("bha_find: board returned %d instead of %d to %s\n",
949 		       i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
950 #endif
951 		return (0);
952 	}
953 
954 	/* OK, we know we've found a buslogic adaptor. */
955 
956 	switch (inquire.reply.bus_type) {
957 	case BHA_BUS_TYPE_24BIT:
958 	case BHA_BUS_TYPE_32BIT:
959 		break;
960 	case BHA_BUS_TYPE_MCA:
961 		/* We don't grok MicroChannel (yet). */
962 		return (0);
963 	default:
964 		printf("bha_find: illegal bus type %c\n",
965 		    inquire.reply.bus_type);
966 		return (0);
967 	}
968 
969 	/* Note if we have a wide bus. */
970 	iswide = inquire.reply.scsi_flags & BHA_SCSI_WIDE;
971 
972 	/*
973 	 * Assume we have a board at this stage setup dma channel from
974 	 * jumpers and save int level
975 	 */
976 	delay(1000);
977 	config.cmd.opcode = BHA_INQUIRE_CONFIG;
978 	bha_cmd(iot, ioh, NULL,
979 	    sizeof(config.cmd), (u_char *)&config.cmd,
980 	    sizeof(config.reply), (u_char *)&config.reply);
981 	switch (config.reply.chan) {
982 	case EISADMA:
983 		drq = -1;
984 		break;
985 	case CHAN0:
986 		drq = 0;
987 		break;
988 	case CHAN5:
989 		drq = 5;
990 		break;
991 	case CHAN6:
992 		drq = 6;
993 		break;
994 	case CHAN7:
995 		drq = 7;
996 		break;
997 	default:
998 		printf("bha_find: illegal drq setting %x\n",
999 		    config.reply.chan);
1000 		return (0);
1001 	}
1002 
1003 	switch (config.reply.intr) {
1004 	case INT9:
1005 		irq = 9;
1006 		break;
1007 	case INT10:
1008 		irq = 10;
1009 		break;
1010 	case INT11:
1011 		irq = 11;
1012 		break;
1013 	case INT12:
1014 		irq = 12;
1015 		break;
1016 	case INT14:
1017 		irq = 14;
1018 		break;
1019 	case INT15:
1020 		irq = 15;
1021 		break;
1022 	default:
1023 		printf("bha_find: illegal irq setting %x\n",
1024 		    config.reply.intr);
1025 		return (0);
1026 	}
1027 
1028 	/* if we want to fill in softc, do so now */
1029 	if (sc != NULL) {
1030 		sc->sc_irq = irq;
1031 		sc->sc_drq = drq;
1032 		sc->sc_scsi_dev = config.reply.scsi_dev;
1033 		sc->sc_iswide = iswide;
1034 	}
1035 
1036 	return (1);
1037 }
1038 
1039 
1040 /*
1041  * Disable the ISA-compatibility ioports on PCI bha devices,
1042  * to ensure they're not autoconfigured a second time as an ISA bha.
1043  */
1044 int
bha_disable_isacompat(sc)1045 bha_disable_isacompat(sc)
1046 	struct bha_softc *sc;
1047 {
1048 	struct bha_isadisable isa_disable;
1049 
1050 	isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
1051 	isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
1052 	bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
1053 	    sizeof(isa_disable.cmd), (u_char *)&isa_disable.cmd,
1054 	    0, (u_char *)0);
1055 	return (0);
1056 }
1057 
1058 
1059 /*
1060  * Start the board, ready for normal operation
1061  */
1062 int
bha_init(sc)1063 bha_init(sc)
1064 	struct bha_softc *sc;
1065 {
1066 	bus_space_tag_t iot = sc->sc_iot;
1067 	bus_space_handle_t ioh = sc->sc_ioh;
1068 	bus_dma_segment_t seg;
1069 	struct bha_devices devices;
1070 	struct bha_setup setup;
1071 	struct bha_mailbox mailbox;
1072 	struct bha_period period;
1073 	int error, i, j, initial_ccbs, rlen, rseg;
1074 
1075 	/* Enable round-robin scheme - appeared at firmware rev. 3.31. */
1076 	if (strcmp(sc->sc_firmware, "3.31") >= 0) {
1077 		struct bha_toggle toggle;
1078 
1079 		toggle.cmd.opcode = BHA_ROUND_ROBIN;
1080 		toggle.cmd.enable = 1;
1081 		bha_cmd(iot, ioh, sc,
1082 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
1083 		    0, (u_char *)0);
1084 	}
1085 
1086 	/*
1087 	 * Inquire installed devices (to force synchronous negotiation).
1088 	 */
1089 
1090 	/*
1091 	 * Poll targets 0 - 7.
1092 	 */
1093 	devices.cmd.opcode = BHA_INQUIRE_DEVICES;
1094 	bha_cmd(iot, ioh, sc,
1095 	    sizeof(devices.cmd), (u_char *)&devices.cmd,
1096 	    sizeof(devices.reply), (u_char *)&devices.reply);
1097 
1098 	/* Count installed units. */
1099 	initial_ccbs = 0;
1100 	for (i = 0; i < 8; i++) {
1101 		for (j = 0; j < 8; j++) {
1102 			if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1103 				initial_ccbs++;
1104 		}
1105 	}
1106 
1107 	/*
1108 	 * Poll targets 8 - 15 if we have a wide bus.
1109          */
1110 	if (ISWIDE(sc)) {
1111 		devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
1112 		bha_cmd(iot, ioh, sc,
1113 		    sizeof(devices.cmd), (u_char *)&devices.cmd,
1114 		    sizeof(devices.reply), (u_char *)&devices.reply);
1115 
1116 		for (i = 0; i < 8; i++) {
1117 			for (j = 0; j < 8; j++) {
1118 				if (((devices.reply.lun_map[i] >> j) & 1) == 1)
1119 					initial_ccbs++;
1120 			}
1121 		}
1122 	}
1123 
1124 	initial_ccbs *= sc->sc_link.openings;
1125 	if (initial_ccbs > BHA_CCB_MAX)
1126 		initial_ccbs = BHA_CCB_MAX;
1127 	if (initial_ccbs == 0)	/* yes, this can happen */
1128 		initial_ccbs = sc->sc_link.openings;
1129 
1130 	/* Obtain setup information from. */
1131 	rlen = sizeof(setup.reply) +
1132 	    (ISWIDE(sc) ? sizeof(setup.reply_w) : 0);
1133 	setup.cmd.opcode = BHA_INQUIRE_SETUP;
1134 	setup.cmd.len = rlen;
1135 	bha_cmd(iot, ioh, sc,
1136 	    sizeof(setup.cmd), (u_char *)&setup.cmd,
1137 	    rlen, (u_char *)&setup.reply);
1138 
1139 	printf("%s: %s, %s\n", sc->sc_dev.dv_xname,
1140 	    setup.reply.sync_neg ? "sync" : "async",
1141 	    setup.reply.parity ? "parity" : "no parity");
1142 
1143 	for (i = 0; i < 8; i++)
1144 		period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
1145 	if (ISWIDE(sc)) {
1146 		for (i = 0; i < 8; i++)
1147 			period.reply_w.period[i] =
1148 			    setup.reply_w.sync_high[i].period * 5 + 20;
1149 	}
1150 
1151 	if (sc->sc_firmware[0] >= '3') {
1152 		rlen = sizeof(period.reply) +
1153 		    (ISWIDE(sc) ? sizeof(period.reply_w) : 0);
1154 		period.cmd.opcode = BHA_INQUIRE_PERIOD;
1155 		period.cmd.len = sizeof(period.reply);
1156 		bha_cmd(iot, ioh, sc,
1157 		    sizeof(period.cmd), (u_char *)&period.cmd,
1158 		    rlen, (u_char *)&period.reply);
1159 	}
1160 
1161 	for (i = 0; i < 8; i++) {
1162 		if (!setup.reply.sync[i].valid ||
1163 		    (!setup.reply.sync[i].offset &&
1164 		     !setup.reply.sync[i].period))
1165 			continue;
1166 		printf("%s targ %d: sync, offset %d, period %dnsec\n",
1167 		    sc->sc_dev.dv_xname, i,
1168 		    setup.reply.sync[i].offset, period.reply.period[i] * 10);
1169 	}
1170 	if (ISWIDE(sc)) {
1171 		for (i = 0; i < 8; i++) {
1172 			if (!setup.reply_w.sync_high[i].valid ||
1173 			    (!setup.reply_w.sync_high[i].offset &&
1174 			     !setup.reply_w.sync_high[i].period))
1175 				continue;
1176 			printf("%s targ %d: sync, offset %d, period %dnsec\n",
1177 			    sc->sc_dev.dv_xname, i + 8,
1178 			    setup.reply_w.sync_high[i].offset,
1179 			    period.reply_w.period[i] * 10);
1180 		}
1181 	}
1182 
1183 	/*
1184 	 * Allocate the mailbox and control blocks.
1185 	 */
1186 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct bha_control),
1187 	    NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
1188 		printf("%s: unable to allocate control structures, "
1189 		    "error = %d\n", sc->sc_dev.dv_xname, error);
1190 		return (error);
1191 	}
1192 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
1193 	    sizeof(struct bha_control), (caddr_t *)&sc->sc_control,
1194 	    BUS_DMA_NOWAIT)) != 0) {
1195 		printf("%s: unable to map control structures, error = %d\n",
1196 		    sc->sc_dev.dv_xname, error);
1197 		return (error);
1198 	}
1199 
1200 	/*
1201 	 * Create and load the DMA map used for the mailbox and
1202 	 * control blocks.
1203 	 */
1204 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct bha_control),
1205 	    1, sizeof(struct bha_control), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
1206 	    &sc->sc_dmamap_control)) != 0) {
1207 		printf("%s: unable to create control DMA map, error = %d\n",
1208 		    sc->sc_dev.dv_xname, error);
1209 		return (error);
1210 	}
1211 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
1212 	    sc->sc_control, sizeof(struct bha_control), NULL,
1213 	    BUS_DMA_NOWAIT)) != 0) {
1214 		printf("%s: unable to load control DMA map, error = %d\n",
1215 		    sc->sc_dev.dv_xname, error);
1216 		return (error);
1217 	}
1218 
1219 	/*
1220 	 * Initialize the control blocks.
1221 	 */
1222 	i = bha_create_ccbs(sc, sc->sc_control->bc_ccbs, initial_ccbs);
1223 	if (i == 0) {
1224 		printf("%s: unable to create control blocks\n",
1225 		    sc->sc_dev.dv_xname);
1226 		return (ENOMEM);
1227 	} else if (i != initial_ccbs) {
1228 		printf("%s: WARNING: only %d of %d control blocks created\n",
1229 		    sc->sc_dev.dv_xname, i, initial_ccbs);
1230 	}
1231 
1232 	/*
1233 	 * Set up initial mail box for round-robin operation.
1234 	 */
1235 	for (i = 0; i < BHA_MBX_SIZE; i++) {
1236 		wmbx->mbo[i].cmd = BHA_MBO_FREE;
1237 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1238 		    0, sc->sc_dmamap_control->dm_mapsize,
1239 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1240 		wmbx->mbi[i].comp_stat = BHA_MBI_FREE;
1241 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1242 		    0, sc->sc_dmamap_control->dm_mapsize,
1243 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1244 	}
1245 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
1246 	wmbx->tmbi = &wmbx->mbi[0];
1247 	sc->sc_mbofull = 0;
1248 
1249 	/* Initialize mail box. */
1250 	mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
1251 	mailbox.cmd.nmbx = BHA_MBX_SIZE;
1252 	ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1253 	    offsetof(struct bha_control, bc_mbx), mailbox.cmd.addr);
1254 	bha_cmd(iot, ioh, sc,
1255 	    sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
1256 	    0, (u_char *)0);
1257 	return (0);
1258 }
1259 
1260 void
bha_inquire_setup_information(sc)1261 bha_inquire_setup_information(sc)
1262 	struct bha_softc *sc;
1263 {
1264 	bus_space_tag_t iot = sc->sc_iot;
1265 	bus_space_handle_t ioh = sc->sc_ioh;
1266 	struct bha_model model;
1267 	struct bha_revision revision;
1268 	struct bha_digit digit;
1269 	char *p;
1270 
1271 	/*
1272 	 * Get the firmware revision.
1273 	 */
1274 	p = sc->sc_firmware;
1275 	revision.cmd.opcode = BHA_INQUIRE_REVISION;
1276 	bha_cmd(iot, ioh, sc,
1277 	    sizeof(revision.cmd), (u_char *)&revision.cmd,
1278 	    sizeof(revision.reply), (u_char *)&revision.reply);
1279 	*p++ = revision.reply.firm_revision;
1280 	*p++ = '.';
1281 	*p++ = revision.reply.firm_version;
1282 	digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
1283 	bha_cmd(iot, ioh, sc,
1284 	    sizeof(digit.cmd), (u_char *)&digit.cmd,
1285 	    sizeof(digit.reply), (u_char *)&digit.reply);
1286 	*p++ = digit.reply.digit;
1287 	if (revision.reply.firm_revision >= '3' ||
1288 	    (revision.reply.firm_revision == '3' &&
1289 	     revision.reply.firm_version >= '3')) {
1290 		digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
1291 		bha_cmd(iot, ioh, sc,
1292 		    sizeof(digit.cmd), (u_char *)&digit.cmd,
1293 		    sizeof(digit.reply), (u_char *)&digit.reply);
1294 		*p++ = digit.reply.digit;
1295 	}
1296 	while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
1297 		p--;
1298 	*p = '\0';
1299 
1300 	/*
1301 	 * Get the model number.
1302 	 */
1303 	if (revision.reply.firm_revision >= '3') {
1304 		p = sc->sc_model;
1305 		model.cmd.opcode = BHA_INQUIRE_MODEL;
1306 		model.cmd.len = sizeof(model.reply);
1307 		bha_cmd(iot, ioh, sc,
1308 		    sizeof(model.cmd), (u_char *)&model.cmd,
1309 		    sizeof(model.reply), (u_char *)&model.reply);
1310 		*p++ = model.reply.id[0];
1311 		*p++ = model.reply.id[1];
1312 		*p++ = model.reply.id[2];
1313 		*p++ = model.reply.id[3];
1314 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1315 			p--;
1316 		*p++ = model.reply.version[0];
1317 		*p++ = model.reply.version[1];
1318 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
1319 			p--;
1320 		*p = '\0';
1321 	} else
1322 		strlcpy(sc->sc_model, "542B", sizeof sc->sc_model);
1323 }
1324 
1325 void
bhaminphys(bp)1326 bhaminphys(bp)
1327 	struct buf *bp;
1328 {
1329 
1330 	if (bp->b_bcount > BHA_MAXXFER)
1331 		bp->b_bcount = BHA_MAXXFER;
1332 	minphys(bp);
1333 }
1334 
1335 /*
1336  * start a scsi operation given the command and the data address.  Also needs
1337  * the unit, target and lu.
1338  */
1339 int
bha_scsi_cmd(xs)1340 bha_scsi_cmd(xs)
1341 	struct scsi_xfer *xs;
1342 {
1343 	struct scsi_link *sc_link = xs->sc_link;
1344 	struct bha_softc *sc = sc_link->adapter_softc;
1345 	bus_dma_tag_t dmat = sc->sc_dmat;
1346 	struct bha_ccb *ccb;
1347 	int error, seg, flags, s;
1348 	int fromqueue = 0, dontqueue = 0;
1349 
1350 	SC_DEBUG(sc_link, SDEV_DB2, ("bha_scsi_cmd\n"));
1351 
1352 	s = splbio();		/* protect the queue */
1353 
1354 	/*
1355 	 * If we're running the queue from bha_done(), we've been
1356 	 * called with the first queue entry as our argument.
1357 	 */
1358 	if (xs == sc->sc_queue.lh_first) {
1359 		xs = bha_dequeue(sc);
1360 		fromqueue = 1;
1361 		goto get_ccb;
1362 	}
1363 
1364 	/* Polled requests can't be queued for later. */
1365 	dontqueue = xs->flags & SCSI_POLL;
1366 
1367 	/*
1368 	 * If there are jobs in the queue, run them first.
1369 	 */
1370 	if (sc->sc_queue.lh_first != NULL) {
1371 		/*
1372 		 * If we can't queue, we have to abort, since
1373 		 * we have to preserve order.
1374 		 */
1375 		if (dontqueue) {
1376 			splx(s);
1377 			xs->error = XS_DRIVER_STUFFUP;
1378 			return (TRY_AGAIN_LATER);
1379 		}
1380 
1381 		/*
1382 		 * Swap with the first queue entry.
1383 		 */
1384 		bha_enqueue(sc, xs, 0);
1385 		xs = bha_dequeue(sc);
1386 		fromqueue = 1;
1387 	}
1388 
1389  get_ccb:
1390 	/*
1391 	 * get a ccb to use. If the transfer
1392 	 * is from a buf (possibly from interrupt time)
1393 	 * then we can't allow it to sleep
1394 	 */
1395 	flags = xs->flags;
1396 	if ((ccb = bha_get_ccb(sc, flags)) == NULL) {
1397 		/*
1398 		 * If we can't queue, we lose.
1399 		 */
1400 		if (dontqueue) {
1401 			splx(s);
1402 			xs->error = XS_DRIVER_STUFFUP;
1403 			return (TRY_AGAIN_LATER);
1404 		}
1405 
1406 		/*
1407 		 * Stuff ourselves into the queue, in front
1408 		 * if we came off in the first place.
1409 		 */
1410 		bha_enqueue(sc, xs, fromqueue);
1411 		splx(s);
1412 		return (SUCCESSFULLY_QUEUED);
1413 	}
1414 
1415 	splx(s);		/* done playing with the queue */
1416 
1417 	ccb->xs = xs;
1418 	ccb->timeout = xs->timeout;
1419 
1420 	/*
1421 	 * Put all the arguments for the xfer in the ccb
1422 	 */
1423 	if (flags & SCSI_RESET) {
1424 		ccb->opcode = BHA_RESET_CCB;
1425 		ccb->scsi_cmd_length = 0;
1426 	} else {
1427 		/* can't use S/G if zero length */
1428 		ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
1429 					   : BHA_INITIATOR_CCB);
1430 		bcopy(xs->cmd, &ccb->scsi_cmd,
1431 		    ccb->scsi_cmd_length = xs->cmdlen);
1432 	}
1433 
1434 	if (xs->datalen) {
1435 		/*
1436 		 * Map the DMA transfer.
1437 		 */
1438 #ifdef TFS
1439 		if (flags & SCSI_DATA_UIO) {
1440 			error = bus_dmamap_load_uio(dmat,
1441 			    ccb->dmamap_xfer, (struct uio *)xs->data,
1442 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1443 			    BUS_DMA_WAITOK);
1444 		} else
1445 #endif /* TFS */
1446 		{
1447 			error = bus_dmamap_load(dmat,
1448 			    ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
1449 			    (flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT :
1450 			    BUS_DMA_WAITOK);
1451 		}
1452 
1453 		if (error) {
1454 			if (error == EFBIG) {
1455 				printf("%s: bha_scsi_cmd, more than %d"
1456 				    " dma segments\n",
1457 				    sc->sc_dev.dv_xname, BHA_NSEG);
1458 			} else {
1459 				printf("%s: bha_scsi_cmd, error %d loading"
1460 				    " dma map\n",
1461 				    sc->sc_dev.dv_xname, error);
1462 			}
1463 			goto bad;
1464 		}
1465 
1466 		bus_dmamap_sync(dmat, ccb->dmamap_xfer,
1467 		    0, ccb->dmamap_xfer->dm_mapsize,
1468 		    (flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
1469 		    BUS_DMASYNC_PREWRITE);
1470 
1471 		/*
1472 		 * Load the hardware scatter/gather map with the
1473 		 * contents of the DMA map.
1474 		 */
1475 		for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
1476 			ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
1477 			    ccb->scat_gath[seg].seg_addr);
1478 			ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
1479 			    ccb->scat_gath[seg].seg_len);
1480 		}
1481 
1482 		ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1483 		    BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scat_gath),
1484 		    ccb->data_addr);
1485 		ltophys(ccb->dmamap_xfer->dm_nsegs *
1486 		    sizeof(struct bha_scat_gath), ccb->data_length);
1487 	} else {
1488 		/*
1489 		 * No data xfer, use non S/G values.
1490 		 */
1491 		ltophys(0, ccb->data_addr);
1492 		ltophys(0, ccb->data_length);
1493 	}
1494 
1495 	ccb->data_out = 0;
1496 	ccb->data_in = 0;
1497 	ccb->target = sc_link->target;
1498 	ccb->lun = sc_link->lun;
1499 	ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
1500 	    BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scsi_sense),
1501 	    ccb->sense_ptr);
1502 	ccb->req_sense_length = sizeof(ccb->scsi_sense);
1503 	ccb->host_stat = 0x00;
1504 	ccb->target_stat = 0x00;
1505 	ccb->link_id = 0;
1506 	ltophys(0, ccb->link_addr);
1507 
1508 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
1509 	    0, sc->sc_dmamap_control->dm_mapsize,
1510 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1511 
1512 	s = splbio();
1513 	bha_queue_ccb(sc, ccb);
1514 	splx(s);
1515 
1516 	/*
1517 	 * Usually return SUCCESSFULLY QUEUED
1518 	 */
1519 	SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
1520 	if ((flags & SCSI_POLL) == 0)
1521 		return (SUCCESSFULLY_QUEUED);
1522 
1523 	/*
1524 	 * If we can't use interrupts, poll on completion
1525 	 */
1526 	if (bha_poll(sc, xs, ccb->timeout)) {
1527 		bha_timeout(ccb);
1528 		if (bha_poll(sc, xs, ccb->timeout))
1529 			bha_timeout(ccb);
1530 	}
1531 	return (COMPLETE);
1532 
1533 bad:
1534 	xs->error = XS_DRIVER_STUFFUP;
1535 	bha_free_ccb(sc, ccb);
1536 	return (COMPLETE);
1537 }
1538 
1539 /*
1540  * Poll a particular unit, looking for a particular xs
1541  */
1542 int
bha_poll(sc,xs,count)1543 bha_poll(sc, xs, count)
1544 	struct bha_softc *sc;
1545 	struct scsi_xfer *xs;
1546 	int count;
1547 {
1548 	bus_space_tag_t iot = sc->sc_iot;
1549 	bus_space_handle_t ioh = sc->sc_ioh;
1550 
1551 	/* timeouts are in msec, so we loop in 1000 usec cycles */
1552 	while (count) {
1553 		/*
1554 		 * If we had interrupts enabled, would we
1555 		 * have got an interrupt?
1556 		 */
1557 		if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
1558 		    BHA_INTR_ANYINTR)
1559 			bha_intr(sc);
1560 		if (xs->flags & ITSDONE)
1561 			return (0);
1562 		delay(1000);	/* only happens in boot so ok */
1563 		count--;
1564 	}
1565 	return (1);
1566 }
1567 
1568 void
bha_timeout(arg)1569 bha_timeout(arg)
1570 	void *arg;
1571 {
1572 	struct bha_ccb *ccb = arg;
1573 	struct scsi_xfer *xs = ccb->xs;
1574 	struct scsi_link *sc_link = xs->sc_link;
1575 	struct bha_softc *sc = sc_link->adapter_softc;
1576 	int s;
1577 
1578 	sc_print_addr(sc_link);
1579 	printf("timed out");
1580 
1581 	s = splbio();
1582 
1583 #ifdef BHADIAG
1584 	/*
1585 	 * If the ccb's mbx is not free, then the board has gone Far East?
1586 	 */
1587 	bha_collect_mbo(sc);
1588 	if (ccb->flags & CCB_SENDING) {
1589 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
1590 		Debugger();
1591 	}
1592 #endif
1593 
1594 	/*
1595 	 * If it has been through before, then
1596 	 * a previous abort has failed, don't
1597 	 * try abort again
1598 	 */
1599 	if (ccb->flags & CCB_ABORT) {
1600 		/* abort timed out */
1601 		printf(" AGAIN\n");
1602 		/* XXX Must reset! */
1603 	} else {
1604 		/* abort the operation that has timed out */
1605 		printf("\n");
1606 		ccb->xs->error = XS_TIMEOUT;
1607 		ccb->timeout = BHA_ABORT_TIMEOUT;
1608 		ccb->flags |= CCB_ABORT;
1609 		bha_queue_ccb(sc, ccb);
1610 	}
1611 
1612 	splx(s);
1613 }
1614