Home | History | Annotate | Line # | Download | only in isa
wds.c revision 1.16
      1  1.16      marc /*	$NetBSD: wds.c,v 1.16 1997/05/01 20:10:57 marc Exp $	*/
      2   1.1   mycroft 
      3   1.8   mycroft #undef WDSDIAG
      4   1.8   mycroft #ifdef DDB
      5   1.1   mycroft #define	integrate
      6   1.8   mycroft #else
      7   1.8   mycroft #define	integrate	static inline
      8   1.8   mycroft #endif
      9   1.1   mycroft 
     10   1.1   mycroft /*
     11   1.1   mycroft  * XXX
     12   1.1   mycroft  * sense data
     13   1.1   mycroft  * aborts
     14   1.1   mycroft  * resets
     15   1.1   mycroft  */
     16   1.1   mycroft 
     17   1.1   mycroft /*
     18   1.1   mycroft  * Copyright (c) 1994, 1995 Julian Highfield.  All rights reserved.
     19  1.15   mycroft  * Portions copyright (c) 1994, 1996, 1997
     20  1.15   mycroft  *	Charles M. Hannum.  All rights reserved.
     21   1.1   mycroft  *
     22   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     23   1.1   mycroft  * modification, are permitted provided that the following conditions
     24   1.1   mycroft  * are met:
     25   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     26   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     27   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     28   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     29   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     30   1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     31   1.1   mycroft  *    must display the following acknowledgement:
     32   1.1   mycroft  *	This product includes software developed by Julian Highfield.
     33   1.1   mycroft  * 4. The name of the author may not be used to endorse or promote products
     34   1.1   mycroft  *    derived from this software without specific prior written permission.
     35   1.1   mycroft  *
     36   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     37   1.1   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     38   1.1   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     39   1.1   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     40   1.1   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     41   1.1   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     42   1.1   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     43   1.1   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     44   1.1   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     45   1.1   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     46   1.1   mycroft  */
     47   1.1   mycroft 
     48   1.1   mycroft /*
     49   1.1   mycroft  * This driver is for the WD7000 family of SCSI controllers:
     50   1.1   mycroft  *   the WD7000-ASC, a bus-mastering DMA controller,
     51   1.1   mycroft  *   the WD7000-FASST2, an -ASC with new firmware and scatter-gather,
     52   1.1   mycroft  *   and the WD7000-ASE, which was custom manufactured for Apollo
     53   1.1   mycroft  *      workstations and seems to include an -ASC as well as floppy
     54   1.1   mycroft  *      and ESDI interfaces.
     55   1.1   mycroft  *
     56   1.1   mycroft  * Loosely based on Theo Deraadt's unfinished attempt.
     57   1.1   mycroft  */
     58   1.1   mycroft 
     59   1.1   mycroft #include <sys/types.h>
     60   1.1   mycroft #include <sys/param.h>
     61   1.1   mycroft #include <sys/systm.h>
     62   1.1   mycroft #include <sys/kernel.h>
     63   1.1   mycroft #include <sys/errno.h>
     64   1.1   mycroft #include <sys/ioctl.h>
     65   1.1   mycroft #include <sys/device.h>
     66   1.1   mycroft #include <sys/malloc.h>
     67   1.1   mycroft #include <sys/buf.h>
     68   1.1   mycroft #include <sys/proc.h>
     69   1.1   mycroft #include <sys/user.h>
     70   1.1   mycroft 
     71   1.7   mycroft #include <machine/intr.h>
     72   1.1   mycroft #include <machine/pio.h>
     73   1.1   mycroft 
     74   1.1   mycroft #include <scsi/scsi_all.h>
     75   1.1   mycroft #include <scsi/scsiconf.h>
     76   1.1   mycroft 
     77   1.1   mycroft #include <dev/isa/isavar.h>
     78   1.1   mycroft #include <dev/isa/isadmavar.h>
     79  1.15   mycroft 
     80   1.1   mycroft #include <dev/isa/wdsreg.h>
     81   1.3       cgd 
     82  1.15   mycroft #define	WDS_ISA_IOSIZE	8
     83  1.15   mycroft 
     84   1.3       cgd #ifndef DDB
     85   1.3       cgd #define Debugger() panic("should call debugger here (wds.c)")
     86   1.3       cgd #endif /* ! DDB */
     87   1.1   mycroft 
     88   1.1   mycroft #define WDS_MBX_SIZE	16
     89   1.1   mycroft 
     90   1.1   mycroft #define WDS_SCB_MAX	32
     91   1.1   mycroft #define	SCB_HASH_SIZE	32	/* hash table size for phystokv */
     92   1.1   mycroft #define	SCB_HASH_SHIFT	9
     93   1.1   mycroft #define	SCB_HASH(x)	((((long)(x))>>SCB_HASH_SHIFT) & (SCB_HASH_SIZE - 1))
     94   1.1   mycroft 
     95   1.1   mycroft #define	wds_nextmbx(wmb, mbx, mbio) \
     96   1.1   mycroft 	if ((wmb) == &(mbx)->mbio[WDS_MBX_SIZE - 1])	\
     97   1.1   mycroft 		(wmb) = &(mbx)->mbio[0];		\
     98   1.1   mycroft 	else						\
     99   1.1   mycroft 		(wmb)++;
    100   1.1   mycroft 
    101   1.1   mycroft struct wds_mbx {
    102   1.1   mycroft 	struct wds_mbx_out mbo[WDS_MBX_SIZE];
    103   1.1   mycroft 	struct wds_mbx_in mbi[WDS_MBX_SIZE];
    104   1.1   mycroft 	struct wds_mbx_out *cmbo;	/* Collection Mail Box out */
    105   1.1   mycroft 	struct wds_mbx_out *tmbo;	/* Target Mail Box out */
    106   1.1   mycroft 	struct wds_mbx_in *tmbi;	/* Target Mail Box in */
    107   1.1   mycroft };
    108   1.1   mycroft 
    109   1.1   mycroft #define	KVTOPHYS(x)	vtophys(x)
    110   1.1   mycroft 
    111   1.1   mycroft struct wds_softc {
    112   1.1   mycroft 	struct device sc_dev;
    113  1.15   mycroft 
    114  1.15   mycroft 	bus_space_tag_t sc_iot;
    115  1.15   mycroft 	bus_space_handle_t sc_ioh;
    116   1.1   mycroft 	void *sc_ih;
    117   1.1   mycroft 
    118   1.1   mycroft 	struct wds_mbx sc_mbx;
    119   1.1   mycroft #define	wmbx	(&sc->sc_mbx)
    120   1.1   mycroft 	struct wds_scb *sc_scbhash[SCB_HASH_SIZE];
    121   1.1   mycroft 	TAILQ_HEAD(, wds_scb) sc_free_scb, sc_waiting_scb;
    122   1.1   mycroft 	int sc_numscbs, sc_mbofull;
    123  1.15   mycroft 	struct scsi_link sc_link;	/* prototype for subdevs */
    124  1.15   mycroft 
    125  1.15   mycroft 	int sc_revision;
    126  1.15   mycroft };
    127  1.15   mycroft 
    128  1.15   mycroft struct wds_probe_data {
    129  1.15   mycroft #ifdef notyet
    130  1.15   mycroft 	int sc_irq, sc_drq;
    131  1.15   mycroft #endif
    132   1.1   mycroft 	int sc_scsi_dev;
    133   1.1   mycroft };
    134   1.1   mycroft 
    135   1.1   mycroft /* Define the bounce buffer length... */
    136   1.1   mycroft #define BUFLEN (64*1024)
    137   1.1   mycroft /* ..and how many there are. One per device! Non-FASST boards need these. */
    138   1.1   mycroft #define BUFCNT 8
    139   1.1   mycroft /* The macro for deciding whether the board needs a buffer. */
    140   1.1   mycroft #define NEEDBUFFER(sc)	(sc->sc_revision < 0x800)
    141   1.1   mycroft 
    142   1.1   mycroft struct wds_buf {
    143   1.1   mycroft 	u_char data[BUFLEN];
    144   1.1   mycroft 	int    busy;
    145   1.1   mycroft 	TAILQ_ENTRY(wds_buf) chain;
    146   1.1   mycroft } wds_buffer[BUFCNT];
    147   1.1   mycroft 
    148   1.1   mycroft TAILQ_HEAD(, wds_buf) wds_free_buffer;
    149   1.1   mycroft 
    150  1.15   mycroft integrate void
    151  1.15   mycroft 	wds_wait __P((bus_space_tag_t, bus_space_handle_t, int, int, int));
    152  1.15   mycroft int     wds_cmd __P((bus_space_tag_t, bus_space_handle_t, u_char *, int));
    153   1.1   mycroft integrate void wds_finish_scbs __P((struct wds_softc *));
    154   1.1   mycroft int     wdsintr __P((void *));
    155   1.1   mycroft integrate void wds_reset_scb __P((struct wds_softc *, struct wds_scb *));
    156   1.1   mycroft void    wds_free_scb __P((struct wds_softc *, struct wds_scb *));
    157   1.1   mycroft void	wds_free_buf __P((struct wds_softc *, struct wds_buf *));
    158   1.1   mycroft integrate void wds_init_scb __P((struct wds_softc *, struct wds_scb *));
    159   1.1   mycroft struct	wds_scb *wds_get_scb __P((struct wds_softc *, int, int));
    160   1.1   mycroft struct	wds_buf *wds_get_buf __P((struct wds_softc *, int));
    161   1.1   mycroft struct	wds_scb *wds_scb_phys_kv __P((struct wds_softc *, u_long));
    162   1.1   mycroft void	wds_queue_scb __P((struct wds_softc *, struct wds_scb *));
    163   1.1   mycroft void	wds_collect_mbo __P((struct wds_softc *));
    164   1.1   mycroft void	wds_start_scbs __P((struct wds_softc *));
    165   1.1   mycroft void    wds_done __P((struct wds_softc *, struct wds_scb *, u_char));
    166  1.15   mycroft int	wds_find __P((bus_space_tag_t, bus_space_handle_t, struct wds_probe_data *));
    167  1.15   mycroft void	wds_attach __P((struct wds_softc *, struct wds_probe_data *));
    168   1.1   mycroft void	wds_init __P((struct wds_softc *));
    169   1.1   mycroft void	wds_inquire_setup_information __P((struct wds_softc *));
    170   1.1   mycroft void    wdsminphys __P((struct buf *));
    171   1.1   mycroft int     wds_scsi_cmd __P((struct scsi_xfer *));
    172   1.2   mycroft void	wds_sense  __P((struct wds_softc *, struct wds_scb *));
    173   1.1   mycroft int	wds_poll __P((struct wds_softc *, struct scsi_xfer *, int));
    174   1.1   mycroft int	wds_ipoll __P((struct wds_softc *, struct wds_scb *, int));
    175   1.1   mycroft void	wds_timeout __P((void *));
    176   1.1   mycroft 
    177   1.1   mycroft struct scsi_adapter wds_switch = {
    178   1.1   mycroft 	wds_scsi_cmd,
    179   1.1   mycroft 	wdsminphys,
    180   1.1   mycroft 	0,
    181   1.1   mycroft 	0,
    182   1.1   mycroft };
    183   1.1   mycroft 
    184   1.1   mycroft /* the below structure is so we have a default dev struct for our link struct */
    185   1.1   mycroft struct scsi_device wds_dev = {
    186   1.1   mycroft 	NULL,			/* Use default error handler */
    187   1.1   mycroft 	NULL,			/* have a queue, served by this */
    188   1.1   mycroft 	NULL,			/* have no async handler */
    189   1.1   mycroft 	NULL,			/* Use default 'done' routine */
    190   1.1   mycroft };
    191   1.1   mycroft 
    192   1.1   mycroft int	wdsprobe __P((struct device *, void *, void *));
    193   1.1   mycroft void	wdsattach __P((struct device *, struct device *, void *));
    194   1.1   mycroft 
    195   1.1   mycroft struct cfattach wds_ca = {
    196   1.1   mycroft 	sizeof(struct wds_softc), wdsprobe, wdsattach
    197   1.1   mycroft };
    198   1.1   mycroft 
    199   1.1   mycroft struct cfdriver wds_cd = {
    200   1.1   mycroft 	NULL, "wds", DV_DULL
    201   1.1   mycroft };
    202   1.1   mycroft 
    203   1.1   mycroft #define	WDS_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    204   1.1   mycroft 
    205   1.1   mycroft integrate void
    206  1.15   mycroft wds_wait(iot, ioh, port, mask, val)
    207  1.15   mycroft 	bus_space_tag_t iot;
    208  1.15   mycroft 	bus_space_handle_t ioh;
    209   1.1   mycroft 	int port;
    210  1.15   mycroft 	int mask, val;
    211   1.1   mycroft {
    212   1.1   mycroft 
    213  1.15   mycroft 	while ((bus_space_read_1(iot, ioh, port) & mask) != val)
    214   1.1   mycroft 		;
    215   1.1   mycroft }
    216   1.1   mycroft 
    217   1.1   mycroft /*
    218   1.1   mycroft  * Write a command to the board's I/O ports.
    219   1.1   mycroft  */
    220   1.1   mycroft int
    221  1.15   mycroft wds_cmd(iot, ioh, ibuf, icnt)
    222  1.15   mycroft 	bus_space_tag_t iot;
    223  1.15   mycroft 	bus_space_handle_t ioh;
    224   1.1   mycroft 	u_char *ibuf;
    225   1.1   mycroft 	int icnt;
    226   1.1   mycroft {
    227   1.1   mycroft 	u_char c;
    228   1.1   mycroft 
    229  1.15   mycroft 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
    230   1.1   mycroft 
    231   1.1   mycroft 	while (icnt--) {
    232  1.15   mycroft 		bus_space_write_1(iot, ioh, WDS_CMD, *ibuf++);
    233  1.15   mycroft 		wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
    234  1.15   mycroft 		c = bus_space_read_1(iot, ioh, WDS_STAT);
    235   1.1   mycroft 		if (c & WDSS_REJ)
    236   1.1   mycroft 			return 1;
    237   1.1   mycroft 	}
    238   1.1   mycroft 
    239   1.1   mycroft 	return 0;
    240   1.1   mycroft }
    241   1.1   mycroft 
    242   1.1   mycroft /*
    243   1.1   mycroft  * Check for the presence of a WD7000 SCSI controller.
    244   1.1   mycroft  */
    245   1.1   mycroft int
    246   1.1   mycroft wdsprobe(parent, match, aux)
    247   1.1   mycroft 	struct device *parent;
    248   1.1   mycroft 	void *match, *aux;
    249   1.1   mycroft {
    250  1.15   mycroft 	struct isa_attach_args *ia = aux;
    251  1.15   mycroft 	bus_space_tag_t iot = ia->ia_iot;
    252  1.15   mycroft 	bus_space_handle_t ioh;
    253  1.15   mycroft 	struct wds_probe_data wpd;
    254  1.15   mycroft 	int rv;
    255  1.15   mycroft 
    256  1.15   mycroft 	if (bus_space_map(iot, ia->ia_iobase, WDS_ISA_IOSIZE, 0, &ioh))
    257  1.15   mycroft 		return (0);
    258  1.15   mycroft 
    259  1.15   mycroft 	rv = wds_find(iot, ioh, &wpd);
    260  1.15   mycroft 
    261  1.15   mycroft 	bus_space_unmap(iot, ioh, WDS_ISA_IOSIZE);
    262  1.15   mycroft 
    263  1.15   mycroft 	if (rv) {
    264  1.15   mycroft #ifdef notyet
    265  1.15   mycroft 		if (ia->ia_irq != -1 && ia->ia_irq != wpd.sc_irq)
    266  1.15   mycroft 			return (0);
    267  1.15   mycroft 		if (ia->ia_drq != -1 && ia->ia_drq != wpd.sc_drq)
    268  1.15   mycroft 			return (0);
    269  1.15   mycroft 		ia->ia_irq = wpd.sc_irq;
    270  1.15   mycroft 		ia->ia_drq = wpd.sc_drq;
    271  1.15   mycroft #else
    272  1.15   mycroft 		if (ia->ia_irq == -1)
    273  1.15   mycroft 			return (0);
    274  1.15   mycroft 		if (ia->ia_drq == -1)
    275  1.15   mycroft 			return (0);
    276   1.1   mycroft #endif
    277  1.15   mycroft 		ia->ia_msize = 0;
    278  1.15   mycroft 		ia->ia_iosize = WDS_ISA_IOSIZE;
    279  1.15   mycroft 	}
    280  1.15   mycroft 	return (rv);
    281   1.1   mycroft }
    282   1.1   mycroft 
    283   1.1   mycroft /*
    284   1.1   mycroft  * Attach all available units.
    285   1.1   mycroft  */
    286   1.1   mycroft void
    287   1.1   mycroft wdsattach(parent, self, aux)
    288   1.1   mycroft 	struct device *parent, *self;
    289   1.1   mycroft 	void *aux;
    290   1.1   mycroft {
    291   1.1   mycroft 	struct isa_attach_args *ia = aux;
    292   1.1   mycroft 	struct wds_softc *sc = (void *)self;
    293  1.15   mycroft 	bus_space_tag_t iot = ia->ia_iot;
    294  1.15   mycroft 	bus_space_handle_t ioh;
    295  1.15   mycroft 	struct wds_probe_data wpd;
    296  1.15   mycroft 	isa_chipset_tag_t ic = ia->ia_ic;
    297  1.15   mycroft 
    298  1.15   mycroft 	printf("\n");
    299  1.15   mycroft 
    300  1.15   mycroft 	if (bus_space_map(iot, ia->ia_iobase, WDS_ISA_IOSIZE, 0, &ioh))
    301  1.15   mycroft 		panic("wdsattach: bus_space_map failed");
    302  1.15   mycroft 
    303  1.15   mycroft 	sc->sc_iot = iot;
    304  1.15   mycroft 	sc->sc_ioh = ioh;
    305  1.15   mycroft 	if (!wds_find(iot, ioh, &wpd))
    306  1.15   mycroft 		panic("wdsattach: wds_find failed");
    307  1.15   mycroft 
    308  1.15   mycroft 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
    309  1.15   mycroft #ifdef notyet
    310  1.15   mycroft 	if (wpd.sc_drq != -1)
    311  1.15   mycroft 		isa_dmacascade(wpd.sc_drq);
    312  1.15   mycroft 
    313  1.15   mycroft 	sc->sc_ih = isa_intr_establish(ic, wpd.sc_irq, IST_EDGE, IPL_BIO,
    314  1.15   mycroft 	    wdsintr, sc);
    315  1.15   mycroft #else
    316  1.15   mycroft 	if (ia->ia_drq != -1)
    317  1.15   mycroft 		isa_dmacascade(ia->ia_drq);
    318   1.1   mycroft 
    319  1.15   mycroft 	sc->sc_ih = isa_intr_establish(ic, ia->ia_irq, IST_EDGE, IPL_BIO,
    320  1.15   mycroft 	    wdsintr, sc);
    321  1.15   mycroft #endif
    322  1.15   mycroft 	if (sc->sc_ih == NULL) {
    323  1.15   mycroft 		printf("%s: couldn't establish interrupt\n",
    324  1.15   mycroft 		    sc->sc_dev.dv_xname);
    325  1.15   mycroft 		return;
    326  1.15   mycroft 	}
    327   1.1   mycroft 
    328  1.15   mycroft 	wds_attach(sc, &wpd);
    329  1.15   mycroft }
    330  1.13   mycroft 
    331  1.15   mycroft void
    332  1.15   mycroft wds_attach(sc, wpd)
    333  1.15   mycroft 	struct wds_softc *sc;
    334  1.15   mycroft 	struct wds_probe_data *wpd;
    335  1.15   mycroft {
    336   1.1   mycroft 
    337  1.15   mycroft 	wds_init(sc);
    338   1.1   mycroft 	TAILQ_INIT(&sc->sc_free_scb);
    339   1.1   mycroft 	TAILQ_INIT(&sc->sc_waiting_scb);
    340   1.1   mycroft 	wds_inquire_setup_information(sc);
    341   1.1   mycroft 
    342   1.1   mycroft 	/*
    343   1.1   mycroft 	 * fill in the prototype scsi_link.
    344   1.1   mycroft 	 */
    345  1.10       cgd 	sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
    346   1.1   mycroft 	sc->sc_link.adapter_softc = sc;
    347  1.15   mycroft 	sc->sc_link.adapter_target = wpd->sc_scsi_dev;
    348   1.1   mycroft 	sc->sc_link.adapter = &wds_switch;
    349   1.1   mycroft 	sc->sc_link.device = &wds_dev;
    350   1.1   mycroft 	/* XXX */
    351   1.1   mycroft 	/* I don't think the -ASE can handle openings > 1. */
    352   1.1   mycroft 	/* It gives Vendor Error 26 whenever I try it.     */
    353   1.1   mycroft 	sc->sc_link.openings = 1;
    354  1.14   thorpej 	sc->sc_link.max_target = 7;
    355   1.1   mycroft 
    356   1.1   mycroft 	/*
    357   1.1   mycroft 	 * ask the adapter what subunits are present
    358   1.1   mycroft 	 */
    359  1.15   mycroft 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    360   1.1   mycroft }
    361   1.1   mycroft 
    362   1.1   mycroft integrate void
    363   1.1   mycroft wds_finish_scbs(sc)
    364   1.1   mycroft 	struct wds_softc *sc;
    365   1.1   mycroft {
    366   1.1   mycroft 	struct wds_mbx_in *wmbi;
    367   1.1   mycroft 	struct wds_scb *scb;
    368   1.1   mycroft 	int i;
    369   1.1   mycroft 
    370   1.1   mycroft 	wmbi = wmbx->tmbi;
    371   1.1   mycroft 
    372   1.1   mycroft 	if (wmbi->stat == WDS_MBI_FREE) {
    373   1.1   mycroft 		for (i = 0; i < WDS_MBX_SIZE; i++) {
    374   1.1   mycroft 			if (wmbi->stat != WDS_MBI_FREE) {
    375  1.12  christos 				printf("%s: mbi not in round-robin order\n",
    376   1.1   mycroft 				    sc->sc_dev.dv_xname);
    377   1.1   mycroft 				goto AGAIN;
    378   1.1   mycroft 			}
    379   1.1   mycroft 			wds_nextmbx(wmbi, wmbx, mbi);
    380   1.1   mycroft 		}
    381   1.1   mycroft #ifdef WDSDIAGnot
    382  1.12  christos 		printf("%s: mbi interrupt with no full mailboxes\n",
    383   1.1   mycroft 		    sc->sc_dev.dv_xname);
    384   1.1   mycroft #endif
    385   1.1   mycroft 		return;
    386   1.1   mycroft 	}
    387   1.1   mycroft 
    388   1.1   mycroft AGAIN:
    389   1.1   mycroft 	do {
    390   1.1   mycroft 		scb = wds_scb_phys_kv(sc, phystol(wmbi->scb_addr));
    391   1.1   mycroft 		if (!scb) {
    392  1.12  christos 			printf("%s: bad mbi scb pointer; skipping\n",
    393   1.1   mycroft 			    sc->sc_dev.dv_xname);
    394   1.1   mycroft 			goto next;
    395   1.1   mycroft 		}
    396   1.1   mycroft 
    397   1.1   mycroft #ifdef WDSDEBUG
    398   1.1   mycroft 		if (wds_debug) {
    399   1.1   mycroft 			u_char *cp = &scb->scsi_cmd;
    400  1.12  christos 			printf("op=%x %x %x %x %x %x\n",
    401   1.1   mycroft 			    cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
    402  1.12  christos 			printf("stat %x for mbi addr = 0x%08x, ",
    403   1.1   mycroft 			    wmbi->stat, wmbi);
    404  1.12  christos 			printf("scb addr = 0x%x\n", scb);
    405   1.1   mycroft 		}
    406   1.1   mycroft #endif /* WDSDEBUG */
    407   1.1   mycroft 
    408   1.1   mycroft 		untimeout(wds_timeout, scb);
    409   1.1   mycroft 		wds_done(sc, scb, wmbi->stat);
    410   1.1   mycroft 
    411   1.1   mycroft 	next:
    412   1.1   mycroft 		wmbi->stat = WDS_MBI_FREE;
    413   1.1   mycroft 		wds_nextmbx(wmbi, wmbx, mbi);
    414   1.1   mycroft 	} while (wmbi->stat != WDS_MBI_FREE);
    415   1.1   mycroft 
    416   1.1   mycroft 	wmbx->tmbi = wmbi;
    417   1.1   mycroft }
    418   1.1   mycroft 
    419   1.1   mycroft /*
    420   1.1   mycroft  * Process an interrupt.
    421   1.1   mycroft  */
    422   1.1   mycroft int
    423   1.1   mycroft wdsintr(arg)
    424   1.1   mycroft 	void *arg;
    425   1.1   mycroft {
    426   1.1   mycroft 	struct wds_softc *sc = arg;
    427  1.15   mycroft 	bus_space_tag_t iot = sc->sc_iot;
    428  1.15   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
    429   1.5  christos 	u_char c;
    430   1.1   mycroft 
    431   1.1   mycroft 	/* Was it really an interrupt from the board? */
    432  1.15   mycroft 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ) == 0)
    433   1.1   mycroft 		return 0;
    434   1.1   mycroft 
    435   1.1   mycroft 	/* Get the interrupt status byte. */
    436  1.15   mycroft 	c = bus_space_read_1(iot, ioh, WDS_IRQSTAT) & WDSI_MASK;
    437   1.1   mycroft 
    438   1.1   mycroft 	/* Acknowledge (which resets) the interrupt. */
    439  1.15   mycroft 	bus_space_write_1(iot, ioh, WDS_IRQACK, 0x00);
    440   1.1   mycroft 
    441   1.1   mycroft 	switch (c) {
    442   1.1   mycroft 	case WDSI_MSVC:
    443   1.1   mycroft 		wds_finish_scbs(sc);
    444   1.1   mycroft 		break;
    445   1.1   mycroft 
    446   1.1   mycroft 	case WDSI_MFREE:
    447   1.1   mycroft 		wds_start_scbs(sc);
    448   1.1   mycroft 		break;
    449   1.1   mycroft 
    450   1.1   mycroft 	default:
    451  1.12  christos 		printf("%s: unrecognized interrupt type %02x",
    452   1.5  christos 		    sc->sc_dev.dv_xname, c);
    453   1.1   mycroft 		break;
    454   1.1   mycroft 	}
    455   1.1   mycroft 
    456   1.1   mycroft 	return 1;
    457   1.1   mycroft }
    458   1.1   mycroft 
    459   1.1   mycroft integrate void
    460   1.1   mycroft wds_reset_scb(sc, scb)
    461   1.1   mycroft 	struct wds_softc *sc;
    462   1.1   mycroft 	struct wds_scb *scb;
    463   1.1   mycroft {
    464   1.1   mycroft 
    465   1.1   mycroft 	scb->flags = 0;
    466   1.1   mycroft }
    467   1.1   mycroft 
    468   1.1   mycroft /*
    469   1.1   mycroft  * Free the command structure, the outgoing mailbox and the data buffer.
    470   1.1   mycroft  */
    471   1.1   mycroft void
    472   1.1   mycroft wds_free_scb(sc, scb)
    473   1.1   mycroft 	struct wds_softc *sc;
    474   1.1   mycroft 	struct wds_scb *scb;
    475   1.1   mycroft {
    476   1.1   mycroft 	int s;
    477   1.1   mycroft 
    478   1.1   mycroft 	if (scb->buf != 0) {
    479   1.1   mycroft 		wds_free_buf(sc, scb->buf);
    480   1.1   mycroft 		scb->buf = 0;
    481   1.1   mycroft 	}
    482   1.1   mycroft 
    483   1.1   mycroft 	s = splbio();
    484   1.1   mycroft 
    485   1.1   mycroft 	wds_reset_scb(sc, scb);
    486   1.1   mycroft 	TAILQ_INSERT_HEAD(&sc->sc_free_scb, scb, chain);
    487   1.1   mycroft 
    488   1.1   mycroft 	/*
    489   1.1   mycroft 	 * If there were none, wake anybody waiting for one to come free,
    490   1.1   mycroft 	 * starting with queued entries.
    491   1.1   mycroft 	 */
    492   1.1   mycroft 	if (scb->chain.tqe_next == 0)
    493   1.1   mycroft 		wakeup(&sc->sc_free_scb);
    494   1.1   mycroft 
    495   1.1   mycroft 	splx(s);
    496   1.1   mycroft }
    497   1.1   mycroft 
    498   1.1   mycroft void
    499   1.1   mycroft wds_free_buf(sc, buf)
    500   1.1   mycroft 	struct wds_softc *sc;
    501   1.1   mycroft 	struct wds_buf *buf;
    502   1.1   mycroft {
    503   1.1   mycroft 	int s;
    504   1.1   mycroft 
    505   1.1   mycroft 	s = splbio();
    506   1.1   mycroft 
    507   1.1   mycroft 	buf->busy = 0;
    508   1.1   mycroft 	TAILQ_INSERT_HEAD(&wds_free_buffer, buf, chain);
    509   1.1   mycroft 
    510   1.1   mycroft 	/*
    511   1.1   mycroft 	 * If there were none, wake anybody waiting for one to come free,
    512   1.1   mycroft 	 * starting with queued entries.
    513   1.1   mycroft 	 */
    514   1.1   mycroft 	if (buf->chain.tqe_next == 0)
    515   1.1   mycroft 		wakeup(&wds_free_buffer);
    516   1.1   mycroft 
    517   1.1   mycroft 	splx(s);
    518   1.1   mycroft }
    519   1.1   mycroft 
    520   1.1   mycroft integrate void
    521   1.1   mycroft wds_init_scb(sc, scb)
    522   1.1   mycroft 	struct wds_softc *sc;
    523   1.1   mycroft 	struct wds_scb *scb;
    524   1.1   mycroft {
    525   1.1   mycroft 	int hashnum;
    526   1.1   mycroft 
    527   1.1   mycroft 	bzero(scb, sizeof(struct wds_scb));
    528   1.1   mycroft 	/*
    529   1.1   mycroft 	 * put in the phystokv hash table
    530   1.1   mycroft 	 * Never gets taken out.
    531   1.1   mycroft 	 */
    532   1.1   mycroft 	scb->hashkey = KVTOPHYS(scb);
    533   1.1   mycroft 	hashnum = SCB_HASH(scb->hashkey);
    534   1.1   mycroft 	scb->nexthash = sc->sc_scbhash[hashnum];
    535   1.1   mycroft 	sc->sc_scbhash[hashnum] = scb;
    536   1.1   mycroft 	wds_reset_scb(sc, scb);
    537   1.1   mycroft }
    538   1.1   mycroft 
    539   1.1   mycroft /*
    540   1.1   mycroft  * Get a free scb
    541   1.1   mycroft  *
    542   1.1   mycroft  * If there are none, see if we can allocate a new one.  If so, put it in
    543   1.1   mycroft  * the hash table too otherwise either return an error or sleep.
    544   1.1   mycroft  */
    545   1.1   mycroft struct wds_scb *
    546   1.1   mycroft wds_get_scb(sc, flags, needbuffer)
    547   1.1   mycroft 	struct wds_softc *sc;
    548   1.1   mycroft 	int flags;
    549   1.1   mycroft 	int needbuffer;
    550   1.1   mycroft {
    551   1.1   mycroft 	struct wds_scb *scb;
    552   1.1   mycroft 	int s;
    553   1.1   mycroft 
    554   1.1   mycroft 	s = splbio();
    555   1.1   mycroft 
    556   1.1   mycroft 	/*
    557   1.1   mycroft 	 * If we can and have to, sleep waiting for one to come free
    558   1.1   mycroft 	 * but only if we can't allocate a new one.
    559   1.1   mycroft 	 */
    560   1.1   mycroft 	for (;;) {
    561   1.1   mycroft 		scb = sc->sc_free_scb.tqh_first;
    562   1.1   mycroft 		if (scb) {
    563   1.1   mycroft 			TAILQ_REMOVE(&sc->sc_free_scb, scb, chain);
    564   1.1   mycroft 			break;
    565   1.1   mycroft 		}
    566   1.1   mycroft 		if (sc->sc_numscbs < WDS_SCB_MAX) {
    567   1.1   mycroft 			scb = (struct wds_scb *) malloc(sizeof(struct wds_scb),
    568   1.1   mycroft 			    M_TEMP, M_NOWAIT);
    569   1.1   mycroft 			if (!scb) {
    570  1.12  christos 				printf("%s: can't malloc scb\n",
    571   1.1   mycroft 				    sc->sc_dev.dv_xname);
    572   1.1   mycroft 				goto out;
    573   1.1   mycroft 			}
    574   1.1   mycroft 			wds_init_scb(sc, scb);
    575   1.1   mycroft 			sc->sc_numscbs++;
    576   1.1   mycroft 			break;
    577   1.1   mycroft 		}
    578   1.1   mycroft 		if ((flags & SCSI_NOSLEEP) != 0)
    579   1.1   mycroft 			goto out;
    580   1.1   mycroft 		tsleep(&sc->sc_free_scb, PRIBIO, "wdsscb", 0);
    581   1.1   mycroft 	}
    582   1.1   mycroft 
    583   1.1   mycroft 	scb->flags |= SCB_ALLOC;
    584   1.1   mycroft 
    585   1.1   mycroft 	if (needbuffer) {
    586   1.1   mycroft 		scb->buf = wds_get_buf(sc, flags);
    587  1.13   mycroft 		if (scb->buf == 0) {
    588   1.1   mycroft 			wds_free_scb(sc, scb);
    589  1.13   mycroft 			scb = 0;
    590  1.13   mycroft 		}
    591   1.1   mycroft 	}
    592   1.1   mycroft 
    593   1.1   mycroft out:
    594   1.1   mycroft 	splx(s);
    595   1.1   mycroft 	return (scb);
    596   1.1   mycroft }
    597   1.1   mycroft 
    598   1.1   mycroft struct wds_buf *
    599   1.1   mycroft wds_get_buf(sc, flags)
    600   1.1   mycroft 	struct wds_softc *sc;
    601   1.1   mycroft 	int flags;
    602   1.1   mycroft {
    603   1.1   mycroft 	struct wds_buf *buf;
    604   1.1   mycroft 	int s;
    605   1.1   mycroft 
    606   1.1   mycroft 	s = splbio();
    607   1.1   mycroft 
    608   1.1   mycroft 	for (;;) {
    609   1.1   mycroft 		buf = wds_free_buffer.tqh_first;
    610   1.1   mycroft 		if (buf) {
    611   1.1   mycroft 			TAILQ_REMOVE(&wds_free_buffer, buf, chain);
    612   1.1   mycroft 			break;
    613   1.1   mycroft 		}
    614   1.1   mycroft 		if ((flags & SCSI_NOSLEEP) != 0)
    615   1.1   mycroft 			goto out;
    616   1.1   mycroft 		tsleep(&wds_free_buffer, PRIBIO, "wdsbuf", 0);
    617   1.1   mycroft 	}
    618   1.1   mycroft 
    619   1.1   mycroft 	buf->busy = 1;
    620   1.1   mycroft 
    621   1.1   mycroft out:
    622   1.1   mycroft 	splx(s);
    623   1.1   mycroft 	return (buf);
    624   1.1   mycroft }
    625   1.1   mycroft 
    626   1.1   mycroft struct wds_scb *
    627   1.1   mycroft wds_scb_phys_kv(sc, scb_phys)
    628   1.1   mycroft 	struct wds_softc *sc;
    629   1.1   mycroft 	u_long scb_phys;
    630   1.1   mycroft {
    631   1.1   mycroft 	int hashnum = SCB_HASH(scb_phys);
    632   1.1   mycroft 	struct wds_scb *scb = sc->sc_scbhash[hashnum];
    633   1.1   mycroft 
    634   1.1   mycroft 	while (scb) {
    635   1.1   mycroft 		if (scb->hashkey == scb_phys)
    636   1.1   mycroft 			break;
    637   1.1   mycroft 		/* XXX Check to see if it matches the sense command block. */
    638   1.1   mycroft 		if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
    639   1.1   mycroft 			break;
    640   1.1   mycroft 		scb = scb->nexthash;
    641   1.1   mycroft 	}
    642   1.1   mycroft 	return scb;
    643   1.1   mycroft }
    644   1.1   mycroft 
    645   1.1   mycroft /*
    646   1.1   mycroft  * Queue a SCB to be sent to the controller, and send it if possible.
    647   1.1   mycroft  */
    648   1.1   mycroft void
    649   1.1   mycroft wds_queue_scb(sc, scb)
    650   1.1   mycroft 	struct wds_softc *sc;
    651   1.1   mycroft 	struct wds_scb *scb;
    652   1.1   mycroft {
    653   1.1   mycroft 
    654   1.1   mycroft 	TAILQ_INSERT_TAIL(&sc->sc_waiting_scb, scb, chain);
    655   1.1   mycroft 	wds_start_scbs(sc);
    656   1.1   mycroft }
    657   1.1   mycroft 
    658   1.1   mycroft /*
    659   1.1   mycroft  * Garbage collect mailboxes that are no longer in use.
    660   1.1   mycroft  */
    661   1.1   mycroft void
    662   1.1   mycroft wds_collect_mbo(sc)
    663   1.1   mycroft 	struct wds_softc *sc;
    664   1.1   mycroft {
    665   1.1   mycroft 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
    666  1.11  christos #ifdef WDSDIAG
    667   1.1   mycroft 	struct wds_scb *scb;
    668  1.11  christos #endif
    669   1.1   mycroft 
    670   1.1   mycroft 	wmbo = wmbx->cmbo;
    671   1.1   mycroft 
    672   1.1   mycroft 	while (sc->sc_mbofull > 0) {
    673   1.1   mycroft 		if (wmbo->cmd != WDS_MBO_FREE)
    674   1.1   mycroft 			break;
    675   1.1   mycroft 
    676   1.1   mycroft #ifdef WDSDIAG
    677   1.1   mycroft 		scb = wds_scb_phys_kv(sc, phystol(wmbo->scb_addr));
    678   1.1   mycroft 		scb->flags &= ~SCB_SENDING;
    679   1.1   mycroft #endif
    680   1.1   mycroft 
    681   1.1   mycroft 		--sc->sc_mbofull;
    682   1.1   mycroft 		wds_nextmbx(wmbo, wmbx, mbo);
    683   1.1   mycroft 	}
    684   1.1   mycroft 
    685   1.1   mycroft 	wmbx->cmbo = wmbo;
    686   1.1   mycroft }
    687   1.1   mycroft 
    688   1.1   mycroft /*
    689   1.1   mycroft  * Send as many SCBs as we have empty mailboxes for.
    690   1.1   mycroft  */
    691   1.1   mycroft void
    692   1.1   mycroft wds_start_scbs(sc)
    693   1.1   mycroft 	struct wds_softc *sc;
    694   1.1   mycroft {
    695  1.15   mycroft 	bus_space_tag_t iot = sc->sc_iot;
    696  1.15   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
    697   1.1   mycroft 	struct wds_mbx_out *wmbo;	/* Mail Box Out pointer */
    698   1.1   mycroft 	struct wds_scb *scb;
    699   1.1   mycroft 	u_char c;
    700   1.1   mycroft 
    701   1.1   mycroft 	wmbo = wmbx->tmbo;
    702   1.1   mycroft 
    703   1.5  christos 	while ((scb = sc->sc_waiting_scb.tqh_first) != NULL) {
    704   1.1   mycroft 		if (sc->sc_mbofull >= WDS_MBX_SIZE) {
    705   1.1   mycroft 			wds_collect_mbo(sc);
    706   1.1   mycroft 			if (sc->sc_mbofull >= WDS_MBX_SIZE) {
    707   1.1   mycroft 				c = WDSC_IRQMFREE;
    708  1.15   mycroft 				wds_cmd(iot, ioh, &c, sizeof c);
    709   1.1   mycroft 				break;
    710   1.1   mycroft 			}
    711   1.1   mycroft 		}
    712   1.1   mycroft 
    713   1.1   mycroft 		TAILQ_REMOVE(&sc->sc_waiting_scb, scb, chain);
    714   1.1   mycroft #ifdef WDSDIAG
    715   1.1   mycroft 		scb->flags |= SCB_SENDING;
    716   1.1   mycroft #endif
    717   1.1   mycroft 
    718   1.1   mycroft 		/* Link scb to mbo. */
    719   1.1   mycroft 		if (scb->flags & SCB_SENSE)
    720   1.1   mycroft 			ltophys(KVTOPHYS(&scb->sense), wmbo->scb_addr);
    721   1.1   mycroft 		else
    722   1.1   mycroft 			ltophys(KVTOPHYS(&scb->cmd), wmbo->scb_addr);
    723   1.1   mycroft 		/* XXX What about aborts? */
    724   1.1   mycroft 		wmbo->cmd = WDS_MBO_START;
    725   1.1   mycroft 
    726   1.1   mycroft 		/* Tell the card to poll immediately. */
    727   1.1   mycroft 		c = WDSC_MSTART(wmbo - wmbx->mbo);
    728  1.15   mycroft 		wds_cmd(sc->sc_iot, sc->sc_ioh, &c, sizeof c);
    729   1.1   mycroft 
    730   1.1   mycroft 		if ((scb->flags & SCB_POLLED) == 0)
    731   1.1   mycroft 			timeout(wds_timeout, scb, (scb->timeout * hz) / 1000);
    732   1.1   mycroft 
    733   1.1   mycroft 		++sc->sc_mbofull;
    734   1.1   mycroft 		wds_nextmbx(wmbo, wmbx, mbo);
    735   1.1   mycroft 	}
    736   1.1   mycroft 
    737   1.1   mycroft 	wmbx->tmbo = wmbo;
    738   1.1   mycroft }
    739   1.1   mycroft 
    740   1.1   mycroft /*
    741   1.1   mycroft  * Process the result of a SCSI command.
    742   1.1   mycroft  */
    743   1.1   mycroft void
    744   1.1   mycroft wds_done(sc, scb, stat)
    745   1.1   mycroft 	struct wds_softc *sc;
    746   1.1   mycroft 	struct wds_scb *scb;
    747   1.1   mycroft 	u_char stat;
    748   1.1   mycroft {
    749   1.1   mycroft 	struct scsi_xfer *xs = scb->xs;
    750   1.1   mycroft 
    751   1.1   mycroft 	/* XXXXX */
    752   1.1   mycroft 
    753   1.1   mycroft 	/* Don't release the SCB if it was an internal command. */
    754   1.1   mycroft 	if (xs == 0) {
    755   1.1   mycroft 		scb->flags |= SCB_DONE;
    756   1.1   mycroft 		return;
    757   1.1   mycroft 	}
    758   1.1   mycroft 
    759   1.1   mycroft 	/* Sense handling. */
    760   1.1   mycroft 	if (xs->error == XS_SENSE) {
    761   1.1   mycroft 		bcopy(&scb->sense_data, &xs->sense, sizeof (struct scsi_sense_data));
    762   1.1   mycroft 	} else {
    763   1.1   mycroft 		if (xs->error == XS_NOERROR) {
    764   1.1   mycroft 			/* If all went well, or an error is acceptable. */
    765   1.1   mycroft 			if (stat == WDS_MBI_OK) {
    766   1.1   mycroft 				/* OK, set the result */
    767   1.1   mycroft 				xs->resid = 0;
    768   1.1   mycroft 			} else {
    769   1.1   mycroft 				/* Check the mailbox status. */
    770   1.1   mycroft 				switch (stat) {
    771   1.1   mycroft 				case WDS_MBI_OKERR:
    772   1.1   mycroft 					/* SCSI error recorded in scb, counts as WDS_MBI_OK */
    773   1.1   mycroft 					switch (scb->cmd.venderr) {
    774   1.1   mycroft 					case 0x00:
    775  1.12  christos 						printf("%s: Is this an error?\n", sc->sc_dev.dv_xname);
    776   1.1   mycroft 						xs->error = XS_DRIVER_STUFFUP; /* Experiment */
    777   1.1   mycroft 						break;
    778   1.1   mycroft 					case 0x01:
    779  1.12  christos 						/*printf("%s: OK, see SCSI error field.\n", sc->sc_dev.dv_xname);*/
    780   1.1   mycroft 						if (scb->cmd.stat == SCSI_CHECK) {
    781   1.1   mycroft 							/* Do sense. */
    782   1.1   mycroft 							wds_sense (sc, scb);
    783   1.1   mycroft 							return;
    784   1.1   mycroft 						} else if (scb->cmd.stat == SCSI_BUSY) {
    785   1.1   mycroft 							xs->error = XS_BUSY;
    786   1.1   mycroft 						}
    787   1.1   mycroft 						break;
    788   1.1   mycroft 					case 0x40:
    789  1.12  christos 						/*printf("%s: DMA underrun!\n", sc->sc_dev.dv_xname);*/
    790   1.1   mycroft 						/* Hits this if the target returns fewer that datalen bytes (eg my CD-ROM,
    791   1.1   mycroft 						which returns a short version string, or if DMA is turned off etc. */
    792   1.1   mycroft 						xs->resid = 0;
    793   1.1   mycroft 						break;
    794   1.1   mycroft 					default:
    795  1.12  christos 						printf("%s: VENDOR ERROR %02x, scsi %02x\n", sc->sc_dev.dv_xname, scb->cmd.venderr, scb->cmd.stat);
    796   1.1   mycroft 						xs->error = XS_DRIVER_STUFFUP; /* Experiment */
    797   1.1   mycroft 						break;
    798   1.1   mycroft 					}
    799   1.1   mycroft 					break;
    800   1.1   mycroft 				case WDS_MBI_ETIME:
    801   1.1   mycroft 					/*
    802   1.1   mycroft 					 * The documentation isn't clear on
    803   1.1   mycroft 					 * what conditions might generate this,
    804   1.1   mycroft 					 * but selection timeouts are the only
    805   1.1   mycroft 					 * one I can think of.
    806   1.1   mycroft 					 */
    807   1.1   mycroft 					xs->error = XS_SELTIMEOUT;
    808   1.1   mycroft 					break;
    809   1.1   mycroft 				case WDS_MBI_ERESET:
    810   1.1   mycroft 				case WDS_MBI_ETARCMD:
    811   1.1   mycroft 				case WDS_MBI_ERESEL:
    812   1.1   mycroft 				case WDS_MBI_ESEL:
    813   1.1   mycroft 				case WDS_MBI_EABORT:
    814   1.1   mycroft 				case WDS_MBI_ESRESET:
    815   1.1   mycroft 				case WDS_MBI_EHRESET:
    816   1.1   mycroft 					xs->error = XS_DRIVER_STUFFUP;
    817   1.1   mycroft 					break;
    818   1.1   mycroft 				}
    819   1.1   mycroft 			}
    820   1.1   mycroft 		} /* else sense */
    821   1.1   mycroft 
    822   1.1   mycroft 		if (NEEDBUFFER(sc) && xs->datalen) {
    823   1.1   mycroft 			if (xs->flags & SCSI_DATA_IN)
    824   1.1   mycroft 				bcopy(scb->buf->data, xs->data, xs->datalen);
    825   1.1   mycroft 		}
    826   1.1   mycroft 	} /* XS_NOERROR */
    827   1.1   mycroft 
    828   1.1   mycroft 	wds_free_scb(sc, scb);
    829   1.1   mycroft 	xs->flags |= ITSDONE;
    830   1.1   mycroft 	scsi_done(xs);
    831   1.1   mycroft }
    832   1.1   mycroft 
    833   1.1   mycroft int
    834  1.15   mycroft wds_find(iot, ioh, sc)
    835  1.15   mycroft 	bus_space_tag_t iot;
    836  1.15   mycroft 	bus_space_handle_t ioh;
    837  1.15   mycroft 	struct wds_probe_data *sc;
    838   1.1   mycroft {
    839   1.1   mycroft 	int i;
    840   1.1   mycroft 
    841   1.1   mycroft 	/* XXXXX */
    842   1.1   mycroft 
    843   1.1   mycroft 	/*
    844   1.1   mycroft 	 * Sending a command causes the CMDRDY bit to clear.
    845   1.1   mycroft  	 */
    846  1.15   mycroft 	for (i = 5; i; i--) {
    847  1.15   mycroft 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    848  1.15   mycroft 			break;
    849  1.15   mycroft 		delay(100);
    850   1.1   mycroft 	}
    851  1.15   mycroft 	if (!i)
    852  1.16      marc 		return 0;
    853   1.1   mycroft 
    854  1.15   mycroft 	bus_space_write_1(iot, ioh, WDS_CMD, WDSC_NOOP);
    855  1.15   mycroft 	if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    856  1.16      marc 		return 0;
    857   1.1   mycroft 
    858  1.15   mycroft 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_SCSIRESET|WDSH_ASCRESET);
    859   1.1   mycroft 	delay(10000);
    860  1.15   mycroft 	bus_space_write_1(iot, ioh, WDS_HCR, 0x00);
    861   1.1   mycroft 	delay(500000);
    862  1.15   mycroft 	wds_wait(iot, ioh, WDS_STAT, WDSS_RDY, WDSS_RDY);
    863  1.15   mycroft 	if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 1)
    864  1.15   mycroft 		if (bus_space_read_1(iot, ioh, WDS_IRQSTAT) != 7)
    865  1.16      marc 			return 0;
    866  1.15   mycroft 
    867  1.15   mycroft 	for (i = 2000; i; i--) {
    868  1.15   mycroft 		if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
    869  1.15   mycroft 			break;
    870  1.15   mycroft 		delay(100);
    871   1.1   mycroft 	}
    872  1.15   mycroft 	if (!i)
    873  1.16      marc 		return 0;
    874   1.1   mycroft 
    875  1.15   mycroft 	if (sc) {
    876  1.15   mycroft #ifdef notyet
    877  1.15   mycroft 		sc->sc_irq = ...;
    878  1.15   mycroft 		sc->sc_drq = ...;
    879  1.15   mycroft #endif
    880   1.1   mycroft 		/* XXX Can we do this better? */
    881   1.1   mycroft 		sc->sc_scsi_dev = 7;
    882   1.1   mycroft 	}
    883   1.1   mycroft 
    884  1.16      marc 	return 1;
    885   1.1   mycroft }
    886   1.1   mycroft 
    887   1.1   mycroft /*
    888   1.1   mycroft  * Initialise the board and driver.
    889   1.1   mycroft  */
    890   1.1   mycroft void
    891   1.1   mycroft wds_init(sc)
    892   1.1   mycroft 	struct wds_softc *sc;
    893   1.1   mycroft {
    894  1.15   mycroft 	bus_space_tag_t iot = sc->sc_iot;
    895  1.15   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
    896   1.1   mycroft 	struct wds_setup init;
    897   1.1   mycroft 	u_char c;
    898   1.1   mycroft 	int i;
    899   1.1   mycroft 
    900   1.1   mycroft 	/*
    901   1.1   mycroft 	 * Set up initial mail box for round-robin operation.
    902   1.1   mycroft 	 */
    903   1.1   mycroft 	for (i = 0; i < WDS_MBX_SIZE; i++) {
    904   1.1   mycroft 		wmbx->mbo[i].cmd = WDS_MBO_FREE;
    905   1.6   mycroft 		wmbx->mbi[i].stat = WDS_MBI_FREE;
    906   1.1   mycroft 	}
    907   1.1   mycroft 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
    908   1.1   mycroft 	wmbx->tmbi = &wmbx->mbi[0];
    909   1.1   mycroft 	sc->sc_mbofull = 0;
    910   1.1   mycroft 
    911   1.1   mycroft 	/* Clear the buffers. */
    912   1.1   mycroft 	TAILQ_INIT(&wds_free_buffer);
    913   1.1   mycroft 	for (i = 0; i < BUFCNT; i++) {
    914   1.1   mycroft 		wds_buffer[i].busy = 0;
    915   1.1   mycroft 		TAILQ_INSERT_HEAD(&wds_free_buffer, &wds_buffer[i], chain);
    916   1.1   mycroft 	}
    917   1.1   mycroft 
    918   1.1   mycroft 	init.opcode = WDSC_INIT;
    919  1.15   mycroft 	init.scsi_id = sc->sc_link.adapter_target;
    920   1.1   mycroft 	init.buson_t = 48;
    921   1.1   mycroft 	init.busoff_t = 24;
    922   1.1   mycroft 	init.xx = 0;
    923   1.1   mycroft 	ltophys(KVTOPHYS(wmbx), init.mbaddr);
    924   1.1   mycroft 	init.nomb = init.nimb = WDS_MBX_SIZE;
    925  1.15   mycroft 	wds_cmd(iot, ioh, (u_char *)&init, sizeof init);
    926   1.1   mycroft 
    927  1.15   mycroft 	wds_wait(iot, ioh, WDS_STAT, WDSS_INIT, WDSS_INIT);
    928   1.1   mycroft 
    929   1.1   mycroft 	c = WDSC_DISUNSOL;
    930  1.15   mycroft 	wds_cmd(iot, ioh, &c, sizeof c);
    931   1.1   mycroft }
    932   1.1   mycroft 
    933   1.1   mycroft /*
    934   1.1   mycroft  * Read the board's firmware revision information.
    935   1.1   mycroft  */
    936   1.1   mycroft void
    937   1.1   mycroft wds_inquire_setup_information(sc)
    938   1.1   mycroft 	struct wds_softc *sc;
    939   1.1   mycroft {
    940  1.15   mycroft 	bus_space_tag_t iot = sc->sc_iot;
    941  1.15   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
    942   1.1   mycroft 	struct wds_scb *scb;
    943   1.1   mycroft 	u_char *j;
    944   1.1   mycroft 	int s;
    945   1.1   mycroft 
    946   1.1   mycroft 	if ((scb = wds_get_scb(sc, SCSI_NOSLEEP, 0)) == NULL) {
    947  1.12  christos 		printf("%s: no request slot available in getvers()!\n", sc->sc_dev.dv_xname);
    948   1.1   mycroft 		return;
    949   1.1   mycroft 	}
    950   1.1   mycroft 	scb->xs = NULL;
    951   1.1   mycroft 	scb->timeout = 40;
    952   1.1   mycroft 
    953   1.1   mycroft 	bzero(&scb->cmd, sizeof scb->cmd);
    954   1.1   mycroft 	scb->cmd.write = 0x80;
    955   1.1   mycroft 	scb->cmd.opcode = WDSX_GETFIRMREV;
    956   1.1   mycroft 
    957   1.1   mycroft 	/* Will poll card, await result. */
    958  1.15   mycroft 	bus_space_write_1(iot, ioh, WDS_HCR, WDSH_DRQEN);
    959   1.1   mycroft 	scb->flags |= SCB_POLLED;
    960   1.1   mycroft 
    961   1.1   mycroft 	s = splbio();
    962   1.1   mycroft 	wds_queue_scb(sc, scb);
    963   1.1   mycroft 	splx(s);
    964   1.1   mycroft 
    965   1.1   mycroft 	if (wds_ipoll(sc, scb, scb->timeout))
    966   1.1   mycroft 		goto out;
    967   1.1   mycroft 
    968   1.1   mycroft 	/* Print the version number. */
    969  1.12  christos 	printf(": version %x.%02x ", scb->cmd.targ, scb->cmd.scb.opcode);
    970   1.1   mycroft 	sc->sc_revision = (scb->cmd.targ << 8) | scb->cmd.scb.opcode;
    971   1.1   mycroft 	/* Print out the version string. */
    972   1.1   mycroft 	j = 2 + &(scb->cmd.targ);
    973   1.1   mycroft 	while ((*j >= 32) && (*j < 128)) {
    974  1.12  christos 		printf("%c", *j);
    975   1.1   mycroft 		j++;
    976   1.1   mycroft 	}
    977   1.1   mycroft 
    978   1.1   mycroft out:
    979  1.12  christos 	printf("\n");
    980   1.1   mycroft 	wds_free_scb(sc, scb);
    981   1.1   mycroft }
    982   1.1   mycroft 
    983   1.1   mycroft void
    984   1.1   mycroft wdsminphys(bp)
    985   1.1   mycroft 	struct buf *bp;
    986   1.1   mycroft {
    987   1.1   mycroft 
    988   1.1   mycroft 	if (bp->b_bcount > ((WDS_NSEG - 1) << PGSHIFT))
    989   1.1   mycroft 		bp->b_bcount = ((WDS_NSEG - 1) << PGSHIFT);
    990   1.1   mycroft 	minphys(bp);
    991   1.1   mycroft }
    992   1.1   mycroft 
    993   1.1   mycroft /*
    994   1.1   mycroft  * Send a SCSI command.
    995   1.1   mycroft  */
    996   1.1   mycroft int
    997   1.1   mycroft wds_scsi_cmd(xs)
    998   1.1   mycroft 	struct scsi_xfer *xs;
    999   1.1   mycroft {
   1000   1.1   mycroft 	struct scsi_link *sc_link = xs->sc_link;
   1001   1.1   mycroft 	struct wds_softc *sc = sc_link->adapter_softc;
   1002   1.1   mycroft 	struct wds_scb *scb;
   1003   1.1   mycroft 	struct wds_scat_gath *sg;
   1004   1.1   mycroft 	int seg;
   1005   1.1   mycroft 	u_long thiskv, thisphys, nextphys;
   1006   1.1   mycroft 	int bytes_this_seg, bytes_this_page, datalen, flags;
   1007   1.5  christos #ifdef TFS
   1008   1.1   mycroft 	struct iovec *iovp;
   1009   1.5  christos #endif
   1010   1.1   mycroft 	int s;
   1011   1.1   mycroft 
   1012   1.1   mycroft 	if (xs->flags & SCSI_RESET) {
   1013   1.1   mycroft 		/* XXX Fix me! */
   1014  1.12  christos 		printf("%s: reset!\n", sc->sc_dev.dv_xname);
   1015   1.1   mycroft 		wds_init(sc);
   1016   1.1   mycroft 		return COMPLETE;
   1017   1.1   mycroft 	}
   1018   1.1   mycroft 
   1019   1.1   mycroft 	flags = xs->flags;
   1020   1.1   mycroft 	if ((scb = wds_get_scb(sc, flags, NEEDBUFFER(sc))) == NULL) {
   1021   1.1   mycroft 		xs->error = XS_DRIVER_STUFFUP;
   1022   1.1   mycroft 		return TRY_AGAIN_LATER;
   1023   1.1   mycroft 	}
   1024   1.1   mycroft 	scb->xs = xs;
   1025   1.1   mycroft 	scb->timeout = xs->timeout;
   1026   1.1   mycroft 
   1027   1.1   mycroft 	if (xs->flags & SCSI_DATA_UIO) {
   1028   1.1   mycroft 		/* XXX Fix me! */
   1029   1.1   mycroft 		/* Let's not worry about UIO. There isn't any code for the *
   1030   1.1   mycroft 		 * non-SG boards anyway! */
   1031  1.12  christos 		printf("%s: UIO is untested and disabled!\n", sc->sc_dev.dv_xname);
   1032   1.1   mycroft 		goto bad;
   1033   1.1   mycroft 	}
   1034   1.1   mycroft 
   1035   1.1   mycroft 	/* Zero out the command structure. */
   1036   1.1   mycroft 	bzero(&scb->cmd, sizeof scb->cmd);
   1037   1.1   mycroft 	bcopy(xs->cmd, &scb->cmd.scb, xs->cmdlen < 12 ? xs->cmdlen : 12);
   1038   1.1   mycroft 
   1039   1.1   mycroft 	/* Set up some of the command fields. */
   1040   1.1   mycroft 	scb->cmd.targ = (xs->sc_link->target << 5) | xs->sc_link->lun;
   1041   1.1   mycroft 
   1042   1.1   mycroft 	/* NOTE: cmd.write may be OK as 0x40 (disable direction checking)
   1043   1.1   mycroft 	 * on boards other than the WD-7000V-ASE. Need this for the ASE:
   1044   1.1   mycroft  	 */
   1045   1.1   mycroft 	scb->cmd.write = (xs->flags & SCSI_DATA_IN) ? 0x80 : 0x00;
   1046   1.1   mycroft 
   1047   1.1   mycroft 	if (!NEEDBUFFER(sc) && xs->datalen) {
   1048   1.1   mycroft 		sg = scb->scat_gath;
   1049   1.1   mycroft 		seg = 0;
   1050   1.1   mycroft #ifdef TFS
   1051   1.1   mycroft 		if (flags & SCSI_DATA_UIO) {
   1052   1.1   mycroft 			iovp = ((struct uio *)xs->data)->uio_iov;
   1053   1.1   mycroft 			datalen = ((struct uio *)xs->data)->uio_iovcnt;
   1054   1.1   mycroft 			xs->datalen = 0;
   1055   1.1   mycroft 			while (datalen && seg < WDS_NSEG) {
   1056   1.1   mycroft 				ltophys(iovp->iov_base, sg->seg_addr);
   1057   1.1   mycroft 				ltophys(iovp->iov_len, sg->seg_len);
   1058   1.1   mycroft 				xs->datalen += iovp->iov_len;
   1059   1.1   mycroft 				SC_DEBUGN(sc_link, SDEV_DB4, ("UIO(0x%x@0x%x)",
   1060   1.1   mycroft 				    iovp->iov_len, iovp->iov_base));
   1061   1.1   mycroft 				sg++;
   1062   1.1   mycroft 				iovp++;
   1063   1.1   mycroft 				seg++;
   1064   1.1   mycroft 				datalen--;
   1065   1.1   mycroft 			}
   1066   1.1   mycroft 		} else
   1067   1.1   mycroft #endif /* TFS */
   1068   1.1   mycroft 		{
   1069   1.1   mycroft 			/*
   1070   1.1   mycroft 			 * Set up the scatter-gather block.
   1071   1.1   mycroft 			 */
   1072   1.1   mycroft 			SC_DEBUG(sc_link, SDEV_DB4,
   1073   1.1   mycroft 			    ("%d @0x%x:- ", xs->datalen, xs->data));
   1074   1.1   mycroft 
   1075   1.1   mycroft 			datalen = xs->datalen;
   1076   1.1   mycroft 			thiskv = (int)xs->data;
   1077   1.1   mycroft 			thisphys = KVTOPHYS(xs->data);
   1078   1.1   mycroft 
   1079   1.1   mycroft 			while (datalen && seg < WDS_NSEG) {
   1080   1.1   mycroft 				bytes_this_seg = 0;
   1081   1.1   mycroft 
   1082   1.1   mycroft 				/* put in the base address */
   1083   1.1   mycroft 				ltophys(thisphys, sg->seg_addr);
   1084   1.1   mycroft 
   1085   1.1   mycroft 				SC_DEBUGN(sc_link, SDEV_DB4, ("0x%x", thisphys));
   1086   1.1   mycroft 
   1087   1.1   mycroft 				/* do it at least once */
   1088   1.1   mycroft 				nextphys = thisphys;
   1089   1.1   mycroft 				while (datalen && thisphys == nextphys) {
   1090   1.1   mycroft 					/*
   1091   1.1   mycroft 					 * This page is contiguous (physically)
   1092   1.1   mycroft 					 * with the the last, just extend the
   1093   1.1   mycroft 					 * length
   1094   1.1   mycroft 					 */
   1095   1.1   mycroft 					/* check it fits on the ISA bus */
   1096   1.1   mycroft 					if (thisphys > 0xFFFFFF) {
   1097  1.12  christos 						printf("%s: DMA beyond"
   1098   1.1   mycroft 							" end of ISA\n",
   1099   1.1   mycroft 							sc->sc_dev.dv_xname);
   1100   1.1   mycroft 						goto bad;
   1101   1.1   mycroft 					}
   1102   1.1   mycroft 					/* how far to the end of the page */
   1103   1.1   mycroft 					nextphys = (thisphys & ~PGOFSET) + NBPG;
   1104   1.1   mycroft 					bytes_this_page = nextphys - thisphys;
   1105   1.1   mycroft 					/**** or the data ****/
   1106   1.1   mycroft 					bytes_this_page = min(bytes_this_page,
   1107   1.1   mycroft 							      datalen);
   1108   1.1   mycroft 					bytes_this_seg += bytes_this_page;
   1109   1.1   mycroft 					datalen -= bytes_this_page;
   1110   1.1   mycroft 
   1111   1.1   mycroft 					/* get more ready for the next page */
   1112   1.1   mycroft 					thiskv = (thiskv & ~PGOFSET) + NBPG;
   1113   1.1   mycroft 					if (datalen)
   1114   1.1   mycroft 						thisphys = KVTOPHYS(thiskv);
   1115   1.1   mycroft 				}
   1116   1.1   mycroft 				/*
   1117   1.1   mycroft 				 * next page isn't contiguous, finish the seg
   1118   1.1   mycroft 				 */
   1119   1.1   mycroft 				SC_DEBUGN(sc_link, SDEV_DB4,
   1120   1.1   mycroft 				    ("(0x%x)", bytes_this_seg));
   1121   1.1   mycroft 				ltophys(bytes_this_seg, sg->seg_len);
   1122   1.1   mycroft 				sg++;
   1123   1.1   mycroft 				seg++;
   1124   1.1   mycroft 			}
   1125   1.1   mycroft 		}
   1126   1.1   mycroft 		/* end of iov/kv decision */
   1127   1.1   mycroft 		SC_DEBUGN(sc_link, SDEV_DB4, ("\n"));
   1128   1.1   mycroft 		if (datalen) {
   1129   1.1   mycroft 			/*
   1130   1.1   mycroft 			 * there's still data, must have run out of segs!
   1131   1.1   mycroft 			 */
   1132  1.12  christos 			printf("%s: wds_scsi_cmd, more than %d dma segs\n",
   1133   1.1   mycroft 			    sc->sc_dev.dv_xname, WDS_NSEG);
   1134   1.1   mycroft 			goto bad;
   1135   1.1   mycroft 		}
   1136   1.1   mycroft 		scb->cmd.opcode = WDSX_SCSISG;
   1137   1.1   mycroft 		ltophys(KVTOPHYS(scb->scat_gath), scb->cmd.data);
   1138   1.1   mycroft 		ltophys(seg * sizeof(struct wds_scat_gath), scb->cmd.len);
   1139   1.1   mycroft 	} else if (xs->datalen > 0) {
   1140   1.1   mycroft 		/* The board is an ASC or ASE. Do not use scatter/gather. */
   1141   1.1   mycroft 		if (xs->datalen > BUFLEN) {
   1142  1.12  christos 			printf("%s: wds_scsi_cmd, I/O too large for bounce buffer\n",
   1143   1.1   mycroft 			    sc->sc_dev.dv_xname);
   1144   1.1   mycroft 			goto bad;
   1145   1.1   mycroft 		}
   1146   1.1   mycroft 		if (xs->flags & SCSI_DATA_OUT)
   1147   1.1   mycroft 			bcopy(xs->data, scb->buf->data, xs->datalen);
   1148   1.1   mycroft 		else
   1149   1.1   mycroft 			bzero(scb->buf->data, xs->datalen);
   1150   1.1   mycroft 		scb->cmd.opcode = WDSX_SCSICMD;
   1151   1.1   mycroft 		ltophys(KVTOPHYS(scb->buf->data), scb->cmd.data);
   1152   1.1   mycroft 		ltophys(xs->datalen, scb->cmd.len);
   1153   1.1   mycroft 	} else {
   1154   1.1   mycroft 		scb->cmd.opcode = WDSX_SCSICMD;
   1155   1.1   mycroft 		ltophys(0, scb->cmd.data);
   1156   1.1   mycroft 		ltophys(0, scb->cmd.len);
   1157   1.1   mycroft 	}
   1158   1.1   mycroft 
   1159   1.1   mycroft 	scb->cmd.stat = 0x00;
   1160   1.1   mycroft 	scb->cmd.venderr = 0x00;
   1161   1.1   mycroft 	ltophys(0, scb->cmd.link);
   1162   1.1   mycroft 
   1163   1.1   mycroft 	/* XXX Do we really want to do this? */
   1164   1.1   mycroft 	if (flags & SCSI_POLL) {
   1165   1.1   mycroft 		/* Will poll card, await result. */
   1166  1.15   mycroft 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR, WDSH_DRQEN);
   1167   1.1   mycroft 		scb->flags |= SCB_POLLED;
   1168   1.1   mycroft 	} else {
   1169   1.1   mycroft 		/* Will send command, let interrupt routine handle result. */
   1170  1.15   mycroft 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
   1171  1.15   mycroft 		    WDSH_IRQEN | WDSH_DRQEN);
   1172   1.1   mycroft 	}
   1173   1.1   mycroft 
   1174   1.1   mycroft 	s = splbio();
   1175   1.1   mycroft 	wds_queue_scb(sc, scb);
   1176   1.1   mycroft 	splx(s);
   1177   1.1   mycroft 
   1178   1.1   mycroft 	if ((flags & SCSI_POLL) == 0)
   1179   1.1   mycroft 		return SUCCESSFULLY_QUEUED;
   1180   1.1   mycroft 
   1181   1.1   mycroft 	if (wds_poll(sc, xs, scb->timeout)) {
   1182   1.1   mycroft 		wds_timeout(scb);
   1183   1.1   mycroft 		if (wds_poll(sc, xs, scb->timeout))
   1184   1.1   mycroft 			wds_timeout(scb);
   1185   1.1   mycroft 	}
   1186   1.1   mycroft 	return COMPLETE;
   1187   1.1   mycroft 
   1188   1.1   mycroft bad:
   1189   1.1   mycroft 	xs->error = XS_DRIVER_STUFFUP;
   1190   1.1   mycroft 	wds_free_scb(sc, scb);
   1191   1.1   mycroft 	return COMPLETE;
   1192   1.1   mycroft }
   1193   1.1   mycroft 
   1194   1.1   mycroft /*
   1195   1.1   mycroft  * Send a sense request.
   1196   1.1   mycroft  */
   1197   1.2   mycroft void
   1198   1.1   mycroft wds_sense(sc, scb)
   1199   1.1   mycroft 	struct wds_softc *sc;
   1200   1.1   mycroft 	struct wds_scb *scb;
   1201   1.1   mycroft {
   1202   1.1   mycroft 	struct scsi_xfer *xs = scb->xs;
   1203   1.1   mycroft 	struct scsi_sense *ss = (void *)&scb->sense.scb;
   1204   1.1   mycroft 	int s;
   1205   1.1   mycroft 
   1206   1.1   mycroft 	/* XXXXX */
   1207   1.1   mycroft 
   1208   1.1   mycroft 	/* Send sense request SCSI command. */
   1209   1.1   mycroft 	xs->error = XS_SENSE;
   1210   1.1   mycroft 	scb->flags |= SCB_SENSE;
   1211   1.1   mycroft 
   1212   1.1   mycroft 	/* First, save the return values */
   1213   1.1   mycroft 	if (NEEDBUFFER(sc) && xs->datalen) {
   1214   1.1   mycroft 		if (xs->flags & SCSI_DATA_IN)
   1215   1.1   mycroft 			bcopy(scb->buf->data, xs->data, xs->datalen);
   1216   1.1   mycroft 	}
   1217   1.1   mycroft 
   1218   1.1   mycroft 	/* Next, setup a request sense command block */
   1219   1.1   mycroft 	bzero(ss, sizeof(*ss));
   1220   1.1   mycroft 	ss->opcode = REQUEST_SENSE;
   1221   1.1   mycroft 	ss->byte2 = xs->sc_link->lun << 5;
   1222   1.1   mycroft 	ss->length = sizeof(struct scsi_sense_data);
   1223   1.1   mycroft 
   1224   1.1   mycroft 	/* Set up some of the command fields. */
   1225   1.1   mycroft 	scb->sense.targ = scb->cmd.targ;
   1226   1.1   mycroft 	scb->sense.write = 0x80;
   1227   1.1   mycroft 	scb->sense.opcode = WDSX_SCSICMD;
   1228   1.1   mycroft 	ltophys(KVTOPHYS(&scb->sense_data), scb->sense.data);
   1229   1.1   mycroft 	ltophys(sizeof(struct scsi_sense_data), scb->sense.len);
   1230   1.1   mycroft 
   1231   1.1   mycroft 	s = splbio();
   1232   1.1   mycroft 	wds_queue_scb(sc, scb);
   1233   1.1   mycroft 	splx(s);
   1234   1.1   mycroft 
   1235   1.1   mycroft 	/*
   1236   1.1   mycroft 	 * There's no reason for us to poll here.  There are two cases:
   1237   1.1   mycroft 	 * 1) If it's a polling operation, then we're called from the interrupt
   1238   1.1   mycroft 	 *    handler, and we return and continue polling.
   1239   1.1   mycroft 	 * 2) If it's an interrupt-driven operation, then it gets completed
   1240   1.1   mycroft 	 *    later on when the REQUEST SENSE finishes.
   1241   1.1   mycroft 	 */
   1242   1.1   mycroft }
   1243   1.1   mycroft 
   1244   1.1   mycroft /*
   1245   1.1   mycroft  * Poll a particular unit, looking for a particular scb
   1246   1.1   mycroft  */
   1247   1.1   mycroft int
   1248   1.1   mycroft wds_poll(sc, xs, count)
   1249   1.1   mycroft 	struct wds_softc *sc;
   1250   1.1   mycroft 	struct scsi_xfer *xs;
   1251   1.1   mycroft 	int count;
   1252   1.1   mycroft {
   1253  1.15   mycroft 	bus_space_tag_t iot = sc->sc_iot;
   1254  1.15   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
   1255   1.1   mycroft 
   1256   1.1   mycroft 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1257   1.1   mycroft 	while (count) {
   1258   1.1   mycroft 		/*
   1259   1.1   mycroft 		 * If we had interrupts enabled, would we
   1260   1.1   mycroft 		 * have got an interrupt?
   1261   1.1   mycroft 		 */
   1262  1.15   mycroft 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
   1263   1.1   mycroft 			wdsintr(sc);
   1264   1.1   mycroft 		if (xs->flags & ITSDONE)
   1265   1.1   mycroft 			return 0;
   1266   1.1   mycroft 		delay(1000);	/* only happens in boot so ok */
   1267   1.1   mycroft 		count--;
   1268   1.1   mycroft 	}
   1269   1.1   mycroft 	return 1;
   1270   1.1   mycroft }
   1271   1.1   mycroft 
   1272   1.1   mycroft /*
   1273   1.1   mycroft  * Poll a particular unit, looking for a particular scb
   1274   1.1   mycroft  */
   1275   1.1   mycroft int
   1276   1.1   mycroft wds_ipoll(sc, scb, count)
   1277   1.1   mycroft 	struct wds_softc *sc;
   1278   1.1   mycroft 	struct wds_scb *scb;
   1279   1.1   mycroft 	int count;
   1280   1.1   mycroft {
   1281  1.15   mycroft 	bus_space_tag_t iot = sc->sc_iot;
   1282  1.15   mycroft 	bus_space_handle_t ioh = sc->sc_ioh;
   1283   1.1   mycroft 
   1284   1.1   mycroft 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1285   1.1   mycroft 	while (count) {
   1286   1.1   mycroft 		/*
   1287   1.1   mycroft 		 * If we had interrupts enabled, would we
   1288   1.1   mycroft 		 * have got an interrupt?
   1289   1.1   mycroft 		 */
   1290  1.15   mycroft 		if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
   1291   1.1   mycroft 			wdsintr(sc);
   1292   1.1   mycroft 		if (scb->flags & SCB_DONE)
   1293   1.1   mycroft 			return 0;
   1294   1.1   mycroft 		delay(1000);	/* only happens in boot so ok */
   1295   1.1   mycroft 		count--;
   1296   1.1   mycroft 	}
   1297   1.1   mycroft 	return 1;
   1298   1.1   mycroft }
   1299   1.1   mycroft 
   1300   1.1   mycroft void
   1301   1.1   mycroft wds_timeout(arg)
   1302   1.1   mycroft 	void *arg;
   1303   1.1   mycroft {
   1304   1.1   mycroft 	struct wds_scb *scb = arg;
   1305   1.1   mycroft 	struct scsi_xfer *xs = scb->xs;
   1306   1.1   mycroft 	struct scsi_link *sc_link = xs->sc_link;
   1307   1.1   mycroft 	struct wds_softc *sc = sc_link->adapter_softc;
   1308   1.1   mycroft 	int s;
   1309   1.1   mycroft 
   1310   1.1   mycroft 	sc_print_addr(sc_link);
   1311  1.12  christos 	printf("timed out");
   1312   1.1   mycroft 
   1313   1.1   mycroft 	s = splbio();
   1314   1.1   mycroft 
   1315   1.1   mycroft #ifdef WDSDIAG
   1316   1.1   mycroft 	/*
   1317   1.1   mycroft 	 * If The scb's mbx is not free, then the board has gone south?
   1318   1.1   mycroft 	 */
   1319   1.1   mycroft 	wds_collect_mbo(sc);
   1320   1.1   mycroft 	if (scb->flags & SCB_SENDING) {
   1321  1.12  christos 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
   1322   1.1   mycroft 		Debugger();
   1323   1.1   mycroft 	}
   1324   1.1   mycroft #endif
   1325   1.1   mycroft 
   1326   1.1   mycroft 	/*
   1327   1.1   mycroft 	 * If it has been through before, then
   1328   1.1   mycroft 	 * a previous abort has failed, don't
   1329   1.1   mycroft 	 * try abort again
   1330   1.1   mycroft 	 */
   1331   1.1   mycroft 	if (scb->flags & SCB_ABORT) {
   1332   1.1   mycroft 		/* abort timed out */
   1333  1.12  christos 		printf(" AGAIN\n");
   1334   1.1   mycroft 		/* XXX Must reset! */
   1335   1.1   mycroft 	} else {
   1336   1.1   mycroft 		/* abort the operation that has timed out */
   1337  1.12  christos 		printf("\n");
   1338   1.1   mycroft 		scb->xs->error = XS_TIMEOUT;
   1339   1.1   mycroft 		scb->timeout = WDS_ABORT_TIMEOUT;
   1340   1.1   mycroft 		scb->flags |= SCB_ABORT;
   1341   1.1   mycroft 		wds_queue_scb(sc, scb);
   1342   1.1   mycroft 	}
   1343   1.1   mycroft 
   1344   1.1   mycroft 	splx(s);
   1345   1.1   mycroft }
   1346