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