Home | History | Annotate | Line # | Download | only in pcmcia
if_ray.c revision 1.2
      1 /*	$NetBSD: if_ray.c,v 1.2 2000/01/24 01:32:00 augustss Exp $	*/
      2 /*
      3  * Copyright (c) 2000 Christian E. Hopps
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. Neither the name of the author nor the names of any co-contributors
     15  *    may be used to endorse or promote products derived from this software
     16  *    without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Driver for the Raylink (Raytheon) / WebGear IEEE 802.11 (FH) WLANs
     33  *
     34  *	2-way communication with the card is through command structures
     35  *	stored in shared ram.  To communicate with the card a free
     36  *	command structure is filled in and then the card is interrupted.
     37  *	The card does the same with a different set of command structures.
     38  *	Only one command can be processed at a time.  This is indicated
     39  *	by the interrupt having not been cleared since it was last set.
     40  *	The bit is cleared when the command has been processed (although
     41  *	it may not yet be complete).
     42  *
     43  *	This driver was only tested with the Aviator 2.4 wireless
     44  *	The author didn't have the pro version or raylink to test
     45  *	with.
     46  *
     47  *	N.B. Its unclear yet whether the Aviator 2.4 cards interoperate
     48  *	with other 802.11 FH 2Mbps cards, since this was also untested.
     49  *	Given the nature of the buggy build 4 firmware there may be problems.
     50  */
     51 
     52 #include "opt_inet.h"
     53 #include "bpfilter.h"
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/socket.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/errno.h>
     61 #include <sys/device.h>
     62 #include <sys/kernel.h>
     63 #include <sys/proc.h>
     64 
     65 #include <net/if.h>
     66 #include <net/if_dl.h>
     67 #include <net/if_ether.h>
     68 #include <net/if_media.h>
     69 #include <net/if_llc.h>
     70 #include <net/if_ieee80211.h>
     71 #include <net/if_media.h>
     72 
     73 #ifdef INET
     74 #include <netinet/in.h>
     75 #include <netinet/in_systm.h>
     76 #include <netinet/in_var.h>
     77 #include <netinet/ip.h>
     78 #include <netinet/if_inarp.h>
     79 #endif
     80 
     81 #if NBPFILTER > 0
     82 #include <net/bpf.h>
     83 #include <net/bpfdesc.h>
     84 #endif
     85 
     86 #include <machine/cpu.h>
     87 #include <machine/bus.h>
     88 #include <machine/intr.h>
     89 
     90 #include <dev/pcmcia/pcmciareg.h>
     91 #include <dev/pcmcia/pcmciavar.h>
     92 #include <dev/pcmcia/pcmciadevs.h>
     93 
     94 #include <dev/pcmcia/if_rayreg.h>
     95 
     96 #define	RAY_DEBUG
     97 
     98 #ifndef	RAY_PID_COUNTRY_CODE_DEFAULT
     99 #define	RAY_PID_COUNTRY_CODE_DEFAULT	RAY_PID_COUNTRY_CODE_USA
    100 #endif
    101 
    102 /* amount of time to poll for non-return of certain command status */
    103 #ifndef	RAY_CHECK_CCS_TIMEOUT
    104 #define	RAY_CHECK_CCS_TIMEOUT	(hz / 2)
    105 #endif
    106 
    107 /* ammount of time to consider start/join failed */
    108 #ifndef	RAY_START_TIMEOUT
    109 #define	RAY_START_TIMEOUT	(3 * hz)
    110 #endif
    111 
    112 /*
    113  * if a command cannot execute because device is busy try later
    114  * this is also done after interrupts and other command timeouts
    115  * so we can use a large value safely.
    116  */
    117 #ifndef	RAY_CHECK_SCHED_TIMEOUT
    118 #define	RAY_CHECK_SCHED_TIMEOUT	(hz)	/* XXX 5 */
    119 #endif
    120 
    121 #ifndef	RAY_MODE_DEFAULT
    122 #define	RAY_MODE_DEFAULT	SC_MODE_ADHOC
    123 #endif
    124 
    125 #ifndef	RAY_DEF_NWID
    126 #define	RAY_DEF_NWID	"NETWORK_NAME"
    127 #endif
    128 
    129 /*
    130  * the number of times the HW is reset in 30s before disabling
    131  * this is needed becuase resets take ~2s and currently pcmcia
    132  * spins for the reset
    133  */
    134 #ifndef	RAY_MAX_RESETS
    135 #define	RAY_MAX_RESETS	3
    136 #endif
    137 
    138 /*
    139  * Types
    140  */
    141 
    142 struct ray_softc {
    143 	struct device	sc_dev;
    144 	struct ethercom	sc_ec;
    145 	struct ifmedia	sc_media;
    146 
    147 	struct pcmcia_function		*sc_pf;
    148 	struct pcmcia_mem_handle	sc_mem;
    149 	int				sc_window;
    150 #if 0
    151 	struct pcmcia_mem_handle	sc_amem;
    152 	int				sc_awindow;
    153 #endif
    154 	void				*sc_ih;
    155 	void				*sc_sdhook;
    156 	int				sc_resetloop;
    157 
    158 	struct ray_ecf_startup		sc_ecf_startup;
    159 	struct ray_startup_params_head	sc_startup;
    160 	union {
    161 		struct ray_startup_params_tail_5	u_params_5;
    162 		struct ray_startup_params_tail_4	u_params_4;
    163 	} sc_u;
    164 
    165 	u_int8_t	sc_ccsinuse[64];	/* ccs in use -- not for tx */
    166 	u_int		sc_txfree;	/* a free count for efficiency */
    167 
    168 	u_int8_t	sc_bssid[ETHER_ADDR_LEN];	/* current net values */
    169 	u_int8_t	sc_cnwid[IEEE80211_NWID_LEN];	/* last nwid */
    170 	u_int8_t	sc_dnwid[IEEE80211_NWID_LEN];	/* desired nwid */
    171 	u_int8_t	sc_omode;	/* old operating mode SC_MODE_xx */
    172 	u_int8_t	sc_mode;	/* current operating mode SC_MODE_xx */
    173 	u_int8_t	sc_countrycode;	/* current country code */
    174 	u_int8_t	sc_dcountrycode; /* desired country code */
    175 	int		sc_havenet;	/* true if we have aquired a network */
    176 	bus_size_t	sc_txpad;	/* tib size plus "phy" size */
    177 	u_int8_t	sc_deftxrate;	/* default transfer rate */
    178 	u_int8_t	sc_encrypt;
    179 
    180 
    181 	int		sc_promisc;	/* current set value */
    182 	int		sc_running;	/* things we are doing */
    183 	int		sc_scheduled;	/* things we need to do */
    184 	int		sc_timoneed;	/* set if timeout is sched */
    185 	int		sc_timocheck;	/* set if timeout is sched */
    186 	bus_size_t	sc_startccs;	/* ccs of start/join */
    187 	u_int		sc_startcmd;	/* cmd (start | join) */
    188 
    189 	int		sc_checkcounters;
    190 	u_int64_t	sc_rxoverflow;
    191 	u_int64_t	sc_rxcksum;
    192 	u_int64_t	sc_rxhcksum;
    193 	u_int8_t	sc_rxnoise;
    194 
    195 	/* use to return values to the user */
    196 	struct ray_param_req	*sc_repreq;
    197 	struct ray_param_req	*sc_updreq;
    198 };
    199 #define	sc_memt	sc_mem.memt
    200 #define	sc_memh	sc_mem.memh
    201 #define	sc_ccrt	sc_pf->pf_ccrt
    202 #define	sc_ccrh	sc_pf->pf_ccrh
    203 #define	sc_startup_4	sc_u.u_params_4
    204 #define	sc_startup_5	sc_u.u_params_5
    205 #define	sc_version	sc_ecf_startup.e_fw_build_string
    206 #define	sc_tibsize	sc_ecf_startup.e_tib_size
    207 #define	sc_if		sc_ec.ec_if
    208 #define	sc_xname	sc_dev.dv_xname
    209 
    210 /* modes of operation */
    211 #define	SC_MODE_ADHOC	0	/* ad-hoc mode */
    212 #define	SC_MODE_INFRA	1	/* infrastructure mode */
    213 
    214 /* commands -- priority given to LSB */
    215 #define	SCP_FIRST		0x0001
    216 #define	SCP_UPDATESUBCMD	0x0001
    217 #define	SCP_STARTASSOC		0x0002
    218 #define	SCP_REPORTPARAMS	0x0004
    219 #define	SCP_IFSTART		0x0008
    220 
    221 /* update sub commands -- issues are serialized priority to LSB */
    222 #define	SCP_UPD_FIRST		0x0100
    223 #define	SCP_UPD_STARTUP		0x0100
    224 #define	SCP_UPD_STARTJOIN	0x0200
    225 #define	SCP_UPD_PROMISC		0x0400
    226 #define	SCP_UPD_MCAST		0x0800
    227 #define	SCP_UPD_UPDATEPARAMS	0x1000
    228 #define	SCP_UPD_SHIFT		8
    229 #define	SCP_UPD_MASK		0xff00
    230 
    231 /* these command (a subset of the update set) require timeout checking */
    232 #define	SCP_TIMOCHECK_CMD_MASK	\
    233 	(SCP_UPD_UPDATEPARAMS | SCP_UPD_STARTUP | SCP_UPD_MCAST | \
    234 	SCP_UPD_PROMISC)
    235 
    236 
    237 #define	IFM_ADHOC	\
    238 	IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_FH2, IFM_IEEE80211_ADHOC, 0)
    239 #define	IFM_INFRA	\
    240 	IFM_MAKEWORD(IFM_IEEE80211, IFM_IEEE80211_FH2, 0, 0)
    241 
    242 typedef	void (*ray_cmd_func_t)(struct ray_softc *);
    243 
    244 #define	SC_BUILD_5	0x5
    245 #define	SC_BUILD_4	0x55
    246 
    247 
    248 static int ray_alloc_ccs __P((struct ray_softc *, bus_size_t *, u_int, u_int));
    249 static bus_size_t ray_fill_in_tx_ccs __P((struct ray_softc *, size_t,
    250     u_int, u_int));
    251 static void ray_attach __P((struct device *, struct device *, void *));
    252 static ray_cmd_func_t ray_ccs_done __P((struct ray_softc *, bus_size_t));
    253 static void ray_check_ccs __P((void *));
    254 static void ray_check_scheduled __P((void *));
    255 static void ray_cmd_cancel __P((struct ray_softc *, int));
    256 static void ray_cmd_schedule __P((struct ray_softc *, int));
    257 static void ray_cmd_ran __P((struct ray_softc *, int));
    258 static int ray_cmd_is_running __P((struct ray_softc *, int));
    259 static int ray_cmd_is_scheduled __P((struct ray_softc *, int));
    260 static void ray_cmd_done __P((struct ray_softc *, int));
    261 static int ray_detach __P((struct device *, int));
    262 static void ray_disable __P((struct ray_softc *));
    263 static void ray_download_params __P((struct ray_softc *));
    264 static int ray_enable __P((struct ray_softc *));
    265 static u_int ray_find_free_tx_ccs __P((struct ray_softc *, u_int));
    266 static u_int8_t ray_free_ccs __P((struct ray_softc *, bus_size_t));
    267 static void ray_free_ccs_chain __P((struct ray_softc *, u_int));
    268 static void ray_if_start __P((struct ifnet *));
    269 static int ray_init __P((struct ray_softc *));
    270 static int ray_intr __P((void *));
    271 static void ray_intr_start __P((struct ray_softc *));
    272 static int ray_ioctl __P((struct ifnet *, u_long, caddr_t));
    273 static int ray_issue_cmd __P((struct ray_softc *, bus_size_t, u_int));
    274 static int ray_match __P((struct device *, struct cfdata *, void *));
    275 static int ray_media_change __P((struct ifnet *));
    276 static void ray_media_status __P((struct ifnet *, struct ifmediareq *));
    277 static ray_cmd_func_t ray_rccs_intr __P((struct ray_softc *, bus_size_t));
    278 static void ray_read_region __P((struct ray_softc *, bus_size_t,void *,size_t));
    279 static void ray_recv __P((struct ray_softc *, bus_size_t));
    280 static void ray_report_params __P((struct ray_softc *));
    281 static void ray_reset __P((struct ray_softc *));
    282 static void ray_reset_resetloop __P((void *));
    283 static void ray_set_pending __P((struct ray_softc *, u_int));
    284 static void ray_shutdown __P((void *));
    285 static int ray_simple_cmd __P((struct ray_softc *, u_int, u_int));
    286 static void ray_start_assoc __P((struct ray_softc *));
    287 static void ray_start_join_net __P((struct ray_softc *));
    288 static ray_cmd_func_t ray_start_join_net_done __P((struct ray_softc *,
    289     u_int, bus_size_t, u_int));
    290 static void ray_start_join_timo __P((void *));
    291 static void ray_stop __P((struct ray_softc *));
    292 static void ray_update_error_counters __P((struct ray_softc *));
    293 static void ray_update_mcast __P((struct ray_softc *));
    294 static ray_cmd_func_t ray_update_params_done __P((struct ray_softc *,
    295     bus_size_t, u_int));
    296 static void ray_update_params __P((struct ray_softc *));
    297 static void ray_update_promisc __P((struct ray_softc *));
    298 static void ray_update_subcmd __P((struct ray_softc *));
    299 static int ray_user_report_params __P((struct ray_softc *,
    300     struct ray_param_req *));
    301 static int ray_user_update_params __P((struct ray_softc *,
    302     struct ray_param_req *));
    303 static void ray_write_region __P((struct ray_softc *,bus_size_t,void *,size_t));
    304 
    305 
    306 #ifdef RAY_DEBUG
    307 static int ray_debug = 0;
    308 static int ray_debug_xmit_sum = 0;
    309 static int ray_debug_dump_desc = 0;
    310 static int ray_debug_dump_rx = 0;
    311 static int ray_debug_dump_tx = 0;
    312 static struct timeval rtv, tv1, tv2, *ttp, *ltp;
    313 #define	RAY_DPRINTF(x)	do { if (ray_debug) {	\
    314 	struct timeval *tmp;			\
    315 	microtime(ttp);				\
    316 	timersub(ttp, ltp, &rtv);		\
    317 	tmp = ttp; ttp = ltp; ltp = tmp;	\
    318 	printf("%ld:%ld %ld:%06ld: ", ttp->tv_sec, ttp->tv_usec, rtv.tv_sec, rtv.tv_usec);	\
    319 	printf x ;				\
    320 	} } while (0)
    321 #define	RAY_DPRINTF_XMIT(x)	do { if (ray_debug_xmit_sum) {	\
    322 	struct timeval *tmp;			\
    323 	microtime(ttp);				\
    324 	timersub(ttp, ltp, &rtv);		\
    325 	tmp = ttp; ttp = ltp; ltp = tmp;	\
    326 	printf("%ld:%ld %ld:%06ld: ", ttp->tv_sec, ttp->tv_usec, rtv.tv_sec, rtv.tv_usec);	\
    327 	printf x ;				\
    328 	} } while (0)
    329 
    330 #define	HEXDF_NOCOMPRESS	0x1
    331 #define	HEXDF_NOOFFSET		0x2
    332 #define HEXDF_NOASCII		0x4
    333 void hexdump(const u_int8_t *, int, int, int, int);
    334 static void ray_dump_mbuf __P((struct ray_softc *, struct mbuf *));
    335 
    336 #else	/* !RAY_DEBUG */
    337 
    338 #define	RAY_DPRINTF(x)
    339 #define	RAY_DPRINTF_XMIT(x)
    340 
    341 #endif	/* !RAY_DEBUG */
    342 
    343 
    344 /*
    345  * macros for writing to various regions in the mapped memory space
    346  */
    347 
    348 #if 0
    349 /* read and write the registers in the CCR (attribute) space */
    350 #define	REG_WRITE(sc, off, val) \
    351 	bus_space_write_1((sc)->sc_amem.memt, (sc)->sc_amem.memh, (off), (val))
    352 
    353 #define	REG_READ(sc, off) \
    354 	bus_space_read_1((sc)->sc_amem.memt, (sc)->sc_amem.memh, (off))
    355 #else
    356 	/* use already mapped ccrt */
    357 #define	REG_WRITE(sc, off, val) \
    358 	bus_space_write_1((sc)->sc_ccrt, (sc)->sc_ccrh, (off), (val))
    359 
    360 #define	REG_READ(sc, off) \
    361 	bus_space_read_1((sc)->sc_ccrt, (sc)->sc_ccrh, (off))
    362 #endif
    363 
    364 #define	SRAM_READ_1(sc, off) \
    365 	((u_int8_t)bus_space_read_1((sc)->sc_memt, (sc)->sc_memh, (off)))
    366 
    367 #define	SRAM_READ_FIELD_1(sc, off, s, f) \
    368 	SRAM_READ_1(sc, (off) + offsetof(struct s, f))
    369 
    370 #define	SRAM_READ_FIELD_2(sc, off, s, f)			\
    371 	((((u_int16_t)SRAM_READ_1(sc, (off) + offsetof(struct s, f)) << 8) \
    372 	|(SRAM_READ_1(sc, (off) + 1 + offsetof(struct s, f)))))
    373 
    374 #define	SRAM_READ_FIELD_N(sc, off, s, f, p, n)	\
    375 	ray_read_region(sc, (off) + offsetof(struct s, f), (p), (n))
    376 
    377 #define	SRAM_WRITE_1(sc, off, val)	\
    378 	bus_space_write_1((sc)->sc_memt, (sc)->sc_memh, (off), (val))
    379 
    380 #define	SRAM_WRITE_FIELD_1(sc, off, s, f, v) 	\
    381 	SRAM_WRITE_1(sc, (off) + offsetof(struct s, f), (v))
    382 
    383 #define	SRAM_WRITE_FIELD_2(sc, off, s, f, v) do {	\
    384 	SRAM_WRITE_1(sc, (off) + offsetof(struct s, f), (((v) >> 8 ) & 0xff)); \
    385 	SRAM_WRITE_1(sc, (off) + 1 + offsetof(struct s, f), ((v) & 0xff)); \
    386     } while (0)
    387 
    388 #define	SRAM_WRITE_FIELD_N(sc, off, s, f, p, n)	\
    389 	ray_write_region(sc, (off) + offsetof(struct s, f), (p), (n))
    390 
    391 /*
    392  * Macros of general usefulness
    393  */
    394 
    395 #define	M_PULLUP(m, s) do {	\
    396 	if ((m)->m_len < (s))	\
    397 		(m) = m_pullup((m), (s)); \
    398     } while (0)
    399 
    400 #define	RAY_ECF_READY(sc)	(!(REG_READ(sc, RAY_ECFIR) & RAY_ECSIR_IRQ))
    401 #define	RAY_ECF_START_CMD(sc)	REG_WRITE(sc, RAY_ECFIR, RAY_ECSIR_IRQ)
    402 #define	RAY_GET_INDEX(ccs)	(((ccs) - RAY_CCS_BASE) / RAY_CCS_SIZE)
    403 #define	RAY_GET_CCS(i)		(RAY_CCS_BASE + (i) * RAY_CCS_SIZE)
    404 
    405 /*
    406  * Globals
    407  */
    408 
    409 static u_int8_t llc_snapid[6] = { LLC_SNAP_LSAP, LLC_SNAP_LSAP, LLC_UI, };
    410 
    411 /* based on bit index in SCP_xx */
    412 static ray_cmd_func_t ray_cmdtab[] = {
    413 	ray_update_subcmd,	/* SCP_UPDATESUBCMD */
    414 	ray_start_assoc,	/* SCP_STARTASSOC */
    415 	ray_report_params,	/* SCP_REPORTPARAMS */
    416 	ray_intr_start		/* SCP_IFSTART */
    417 };
    418 static int ray_ncmdtab = sizeof(ray_cmdtab) / sizeof(*ray_cmdtab);
    419 
    420 static ray_cmd_func_t ray_subcmdtab[] = {
    421 	ray_download_params,	/* SCP_UPD_STARTUP */
    422 	ray_start_join_net,	/* SCP_UPD_STARTJOIN */
    423 	ray_update_promisc,	/* SCP_UPD_PROMISC */
    424 	ray_update_mcast,	/* SCP_UPD_MCAST */
    425 	ray_update_params	/* SCP_UPD_UPDATEPARAMS */
    426 };
    427 static int ray_nsubcmdtab = sizeof(ray_subcmdtab) / sizeof(*ray_subcmdtab);
    428 
    429 /* autoconf information */
    430 struct cfattach ray_ca = {
    431 	sizeof(struct ray_softc), ray_match, ray_attach, ray_detach, 0
    432 };
    433 
    434 
    435 /*
    436  * Config Routines
    437  */
    438 
    439 static int
    440 ray_match(parent, match, aux)
    441 	struct device *parent;
    442 	struct cfdata *match;
    443 	void *aux;
    444 {
    445 	struct pcmcia_attach_args *pa = aux;
    446 
    447 #ifdef RAY_DEBUG
    448 	if (!ltp) {
    449 		/* initialize timestamp XXX */
    450 		ttp = &tv1;
    451 		ltp = &tv2;
    452 		microtime(ltp);
    453 	}
    454 #endif
    455 	return (pa->manufacturer == PCMCIA_VENDOR_RAYTHEON
    456 	    && pa->product == PCMCIA_PRODUCT_RAYTHEON_WLAN);
    457 }
    458 
    459 
    460 static void
    461 ray_attach(parent, self, aux)
    462 	struct device *parent, *self;
    463 	void *aux;
    464 {
    465 	struct ray_ecf_startup *ep;
    466 	struct pcmcia_attach_args *pa;
    467 	struct ray_softc *sc;
    468 	struct ifnet *ifp;
    469 	bus_addr_t memoff;
    470 
    471 	pa = aux;
    472 	sc = (struct ray_softc *)self;
    473 	sc->sc_pf = pa->pf;
    474 	ifp = &sc->sc_if;
    475 	sc->sc_window = -1;
    476 #if 0
    477 	sc->sc_awindow = -1;
    478 #endif
    479 
    480 	/* enable the card */
    481 	pcmcia_function_init(sc->sc_pf, sc->sc_pf->cfe_head.sqh_first);
    482 	if (pcmcia_function_enable(sc->sc_pf)) {
    483 		printf(": failed to enable the card");
    484 		return;
    485 	}
    486 
    487 	/*
    488 	 * map in the memory
    489 	 */
    490 	if (pcmcia_mem_alloc(sc->sc_pf, RAY_SRAM_MEM_SIZE, &sc->sc_mem)) {
    491 		printf(": can\'t alloc shared memory\n");
    492 		goto fail;
    493 	}
    494 
    495 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_MEM_COMMON, RAY_SRAM_MEM_BASE,
    496 	    RAY_SRAM_MEM_SIZE, &sc->sc_mem, &memoff, &sc->sc_window)) {
    497 		printf(": can\'t map shared memory\n");
    498 		pcmcia_mem_free(sc->sc_pf, &sc->sc_mem);
    499 		goto fail;
    500 	}
    501 
    502 #if 0
    503 	/* use the already mapped ccrt in our pf */
    504 	/*
    505 	 * map in the memory
    506 	 */
    507 	if (pcmcia_mem_alloc(sc->sc_pf, 0x1000, &sc->sc_amem)) {
    508 		printf(": can\'t alloc attr memory\n");
    509 		goto fail;
    510 	}
    511 
    512 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_MEM_ATTR, 0,
    513 	    0x1000, &sc->sc_amem, &memoff, &sc->sc_awindow)) {
    514 		printf(": can\'t map attr memory\n");
    515 		pcmcia_mem_free(sc->sc_pf, &sc->sc_amem);
    516 		goto fail;
    517 	}
    518 #endif
    519 
    520 	/* get startup results */
    521 	ep = &sc->sc_ecf_startup;
    522 	ray_read_region(sc, RAY_ECF_TO_HOST_BASE, ep,
    523 	    sizeof(sc->sc_ecf_startup));
    524 
    525 	/* check to see that card initialized properly */
    526 	if (ep->e_status != RAY_ECFS_CARD_OK) {
    527 		printf(": card failed self test: status %d\n",
    528 		    sc->sc_ecf_startup.e_status);
    529 		goto fail;
    530 	}
    531 
    532 	/* check firmware version */
    533 	if (sc->sc_version != SC_BUILD_4 && sc->sc_version != SC_BUILD_5) {
    534 		printf(": unsupported firmware version %d\n",
    535 		    ep->e_fw_build_string);
    536 		goto fail;
    537 	}
    538 
    539 	/*
    540 	 * set the parameters that will survive stop/init
    541 	 */
    542 	memset(sc->sc_cnwid, 0, sizeof(sc->sc_cnwid));
    543 	memset(sc->sc_dnwid, 0, sizeof(sc->sc_dnwid));
    544 	strncpy(sc->sc_dnwid, RAY_DEF_NWID, sizeof(sc->sc_dnwid));
    545 	strncpy(sc->sc_cnwid, RAY_DEF_NWID, sizeof(sc->sc_dnwid));
    546 	sc->sc_omode = sc->sc_mode = RAY_MODE_DEFAULT;
    547 	sc->sc_countrycode = sc->sc_dcountrycode = RAY_PID_COUNTRY_CODE_DEFAULT;
    548 
    549 	/*
    550 	 * attach the interface
    551 	 */
    552 	/* The version isn't the most accurate way, but it's easy. */
    553 	if (sc->sc_version == SC_BUILD_4)
    554 		printf(": WebGear Aviator2.4\n");
    555 	else
    556 		printf(": Raytheon Raylink\n");
    557 	printf("%s: firmware version %d\n", sc->sc_dev.dv_xname,sc->sc_version);
    558 	printf("%s: supported rates %0x:%0x:%0x:%0x:%0x:%0x:%0x:%0x\n",
    559 	    sc->sc_xname, ep->e_rates[0], ep->e_rates[1], ep->e_rates[2],
    560 	    ep->e_rates[3], ep->e_rates[4], ep->e_rates[5], ep->e_rates[6],
    561 	    ep->e_rates[7]);
    562 	printf("%s: 802.11 address %s\n", sc->sc_xname,
    563 	    ether_sprintf(ep->e_station_addr));
    564 
    565 	memcpy(ifp->if_xname, sc->sc_xname, IFNAMSIZ);
    566 	ifp->if_softc = sc;
    567 	ifp->if_start = ray_if_start;
    568 	ifp->if_ioctl = ray_ioctl;
    569 	ifp->if_mtu = ETHERMTU;
    570 	ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
    571 	if_attach(ifp);
    572 	ether_ifattach(ifp, ep->e_station_addr);
    573 	/* need enough space for ieee80211_header + (snap or e2) */
    574 	ifp->if_hdrlen =
    575 	    sizeof(struct ieee80211_frame) + sizeof(struct ether_header);
    576 
    577 	ifmedia_init(&sc->sc_media, 0, ray_media_change, ray_media_status);
    578 	ifmedia_add(&sc->sc_media, IFM_ADHOC, 0, sc);
    579 	ifmedia_add(&sc->sc_media, IFM_INFRA, 0, sc);
    580 	if (sc->sc_mode == SC_MODE_ADHOC)
    581 		ifmedia_set(&sc->sc_media, IFM_ADHOC);
    582 	else
    583 		ifmedia_set(&sc->sc_media, IFM_INFRA);
    584 
    585 #if NBPFILTER > 0
    586 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    587 #endif
    588 	/* disable the card */
    589 	pcmcia_function_disable(sc->sc_pf);
    590 
    591 	sc->sc_sdhook = shutdownhook_establish(ray_shutdown, sc);
    592 
    593 	return;
    594 fail:
    595 	/* disable the card */
    596 	pcmcia_function_disable(sc->sc_pf);
    597 
    598 	/* free the alloc/map */
    599 #if 0
    600 	if (sc->sc_awindow != -1) {
    601 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_awindow);
    602 		pcmcia_mem_free(sc->sc_pf, &sc->sc_amem);
    603 	}
    604 #endif
    605 	if (sc->sc_window != -1) {
    606 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_window);
    607 		pcmcia_mem_free(sc->sc_pf, &sc->sc_mem);
    608 	}
    609 }
    610 
    611 static int
    612 ray_detach(self, flags)
    613 	struct device *self;
    614 	int flags;
    615 {
    616 	struct ray_softc *sc;
    617 
    618 	sc = (struct ray_softc *)self;
    619 	RAY_DPRINTF(("%s: detach\n", sc->sc_xname));
    620 
    621 	if (sc->sc_if.if_flags & IFF_RUNNING)
    622 		ray_disable(sc);
    623 
    624 	/* give back the memory */
    625 #if 0
    626 	if (sc->sc_awindow != -1) {
    627 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_awindow);
    628 		pcmcia_mem_free(sc->sc_pf, &sc->sc_amem);
    629 	}
    630 #endif
    631 	if (sc->sc_window != -1) {
    632 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_window);
    633 		pcmcia_mem_free(sc->sc_pf, &sc->sc_mem);
    634 	}
    635 
    636 #ifdef notyet
    637 	/*
    638 	 * Our softc is about to go away, so drop our reference
    639 	 * to the ifnet.
    640 	 */
    641 	if_delref(sc->sc_if);
    642 	return (0);
    643 #else
    644 	return (EBUSY);
    645 #endif
    646 }
    647 
    648 /*
    649  * start the card running
    650  */
    651 static int
    652 ray_enable(sc)
    653 	struct ray_softc *sc;
    654 {
    655 	int error;
    656 
    657 	RAY_DPRINTF(("%s: enable\n", sc->sc_xname));
    658 
    659 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, ray_intr, sc);
    660 	if (sc->sc_ih == NULL)
    661 		return (EIO);
    662 	if ((error = ray_init(sc)))
    663 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    664 	return (error);
    665 }
    666 
    667 /*
    668  * stop the card running
    669  */
    670 static void
    671 ray_disable(sc)
    672 	struct ray_softc *sc;
    673 {
    674 	RAY_DPRINTF(("%s: disable\n", sc->sc_xname));
    675 
    676 	if ((sc->sc_if.if_flags & IFF_RUNNING))
    677 		ray_stop(sc);
    678 
    679 	sc->sc_resetloop = 0;
    680 	sc->sc_rxoverflow = 0;
    681 	sc->sc_rxcksum = 0;
    682 	sc->sc_rxhcksum = 0;
    683 	sc->sc_rxnoise = 0;
    684 
    685 	if (sc->sc_ih)
    686 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    687 	sc->sc_ih = 0;
    688 }
    689 
    690 /*
    691  * start the card running
    692  */
    693 static int
    694 ray_init(sc)
    695 	struct ray_softc *sc;
    696 {
    697 	struct ray_ecf_startup *ep;
    698 	bus_size_t ccs;
    699 	int i;
    700 
    701 	RAY_DPRINTF(("%s: init\n", sc->sc_xname));
    702 
    703 	if ((sc->sc_if.if_flags & IFF_RUNNING))
    704 		ray_stop(sc);
    705 
    706 	if (pcmcia_function_enable(sc->sc_pf))
    707 		return (EIO);
    708 
    709 	/* reset some values */
    710 	memset(sc->sc_ccsinuse, 0, sizeof(sc->sc_ccsinuse));
    711 	sc->sc_havenet = 0;
    712 	memset(sc->sc_bssid, 0, sizeof(sc->sc_bssid));
    713 	sc->sc_deftxrate = 0;
    714 	sc->sc_encrypt = 0;
    715 	sc->sc_txpad = 0;
    716 	sc->sc_promisc = 0;
    717 	sc->sc_scheduled = 0;
    718 	sc->sc_running = 0;
    719 	sc->sc_txfree = RAY_CCS_NTX;
    720 	sc->sc_checkcounters = 0;
    721 
    722 	/* get startup results */
    723 	ep = &sc->sc_ecf_startup;
    724 	ray_read_region(sc, RAY_ECF_TO_HOST_BASE, ep,
    725 	    sizeof(sc->sc_ecf_startup));
    726 
    727 	/* check to see that card initialized properly */
    728 	if (ep->e_status != RAY_ECFS_CARD_OK) {
    729 		pcmcia_function_disable(sc->sc_pf);
    730 		printf("%s: card failed self test: status %d\n",
    731 		    sc->sc_xname, sc->sc_ecf_startup.e_status);
    732 		return (EIO);
    733 	}
    734 
    735 	/* fixup tib size to be correct */
    736 	if (sc->sc_version == SC_BUILD_4 && sc->sc_tibsize == 0x55)
    737 		sc->sc_tibsize = 32;
    738 	sc->sc_txpad = sc->sc_tibsize;
    739 
    740 	/* set all ccs to be free */
    741 	ccs = RAY_GET_CCS(0);
    742 	for (i = 0; i < RAY_CCS_LAST; ccs += RAY_CCS_SIZE, i++)
    743 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status,
    744 		    RAY_CCS_STATUS_FREE);
    745 
    746 #if 0
    747 	/* clear the interrupt if present */
    748 	REG_WRITE(sc, RAY_HCSIR, 0);
    749 #endif
    750 
    751 	/* we are now up and running -- and are busy until download is cplt */
    752 	sc->sc_if.if_flags |= IFF_RUNNING | IFF_OACTIVE;
    753 
    754 	/* set this now so it gets set in the download */
    755 	sc->sc_promisc = !!(sc->sc_if.if_flags & (IFF_PROMISC|IFF_ALLMULTI));
    756 
    757 	/* call after we mark ourselves running */
    758 	ray_download_params(sc);
    759 
    760 	return (0);
    761 }
    762 
    763 /*
    764  * stop the card running
    765  */
    766 static void
    767 ray_stop(sc)
    768 	struct ray_softc *sc;
    769 {
    770 	RAY_DPRINTF(("%s: stop\n", sc->sc_xname));
    771 
    772 	untimeout(ray_check_ccs, sc);
    773 	sc->sc_timocheck = 0;
    774 
    775 	untimeout(ray_check_scheduled, sc);
    776 	sc->sc_timoneed = 0;
    777 
    778 	if (sc->sc_repreq) {
    779 		sc->sc_repreq->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
    780 		wakeup(ray_report_params);
    781 	}
    782 	if (sc->sc_updreq) {
    783 		sc->sc_repreq->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
    784 		wakeup(ray_update_params);
    785 	}
    786 
    787 	sc->sc_if.if_flags &= ~IFF_RUNNING;
    788 	pcmcia_function_disable(sc->sc_pf);
    789 }
    790 
    791 /*
    792  * reset the card
    793  */
    794 static void
    795 ray_reset(sc)
    796 	struct ray_softc *sc;
    797 {
    798 	if (++sc->sc_resetloop >= RAY_MAX_RESETS) {
    799 		if (sc->sc_resetloop == RAY_MAX_RESETS) {
    800 			printf("%s: unable to correct, disabling\n",
    801 			    sc->sc_xname);
    802 			untimeout(ray_reset_resetloop, sc);
    803 			timeout((void (*)(void *))ray_disable, sc, 1);
    804 		}
    805 	} else {
    806 		printf("%s: unexpected failure resetting hw [%d more]\n",
    807 		    sc->sc_xname, RAY_MAX_RESETS - sc->sc_resetloop);
    808 		untimeout(ray_reset_resetloop, sc);
    809 		ray_init(sc);
    810 		timeout(ray_reset_resetloop, sc, 30 * hz);
    811 	}
    812 }
    813 
    814 /*
    815  * return resetloop to zero (enough time has expired to allow user to
    816  * disable a whacked interface)  the main reason for all this nonesense
    817  * is that resets take ~2 seconds and currently the pcmcia code spins
    818  * on these resets
    819  */
    820 static void
    821 ray_reset_resetloop(arg)
    822 	void *arg;
    823 {
    824 	struct ray_softc *sc;
    825 
    826 	sc = arg;
    827 	sc->sc_resetloop = 0;
    828 }
    829 
    830 static void
    831 ray_shutdown(arg)
    832 	void *arg;
    833 {
    834 	struct ray_softc *sc;
    835 
    836 	sc = arg;
    837 	ray_disable(sc);
    838 }
    839 
    840 
    841 static int
    842 ray_ioctl(ifp, cmd, data)
    843 	struct ifnet *ifp;
    844 	u_long cmd;
    845 	caddr_t data;
    846 {
    847 	u_int8_t nwid[IEEE80211_NWID_LEN];
    848 	struct ray_param_req pr;
    849 	struct ray_softc *sc;
    850 	struct ifreq *ifr;
    851 	struct ifaddr *ifa;
    852 	int error, error2, s;
    853 
    854 	sc = ifp->if_softc;
    855 	error = 0;
    856 
    857 	ifr = (struct ifreq *)data;
    858 
    859 	s = splnet();
    860 
    861 	RAY_DPRINTF(("%s: ioctl: cmd 0x%lx data 0x%lx\n", ifp->if_xname,
    862 	    cmd, (long)data));
    863 	switch (cmd) {
    864 	case SIOCSIFADDR:
    865 		RAY_DPRINTF(("%s: ioctl: cmd SIOCSIFADDR\n", ifp->if_xname));
    866 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    867 			if ((error = ray_enable(sc)))
    868 				break;
    869 		ifp->if_flags |= IFF_UP;
    870 		ifa = (struct ifaddr *)data;
    871 		switch (ifa->ifa_addr->sa_family) {
    872 #ifdef INET
    873 		case AF_INET:
    874 			arp_ifinit(&sc->sc_if, ifa);
    875 			break;
    876 #endif
    877 		default:
    878 			break;
    879 		}
    880 		break;
    881 	case SIOCSIFFLAGS:
    882 		RAY_DPRINTF(("%s: ioctl: cmd SIOCSIFFLAGS\n", ifp->if_xname));
    883 		if (ifp->if_flags & IFF_UP) {
    884 			if ((ifp->if_flags & IFF_RUNNING) == 0) {
    885 				if ((error = ray_enable(sc)))
    886 					break;
    887 			} else
    888 				ray_update_promisc(sc);
    889 		} else if (ifp->if_flags & IFF_RUNNING)
    890 			ray_disable(sc);
    891 		break;
    892 	case SIOCADDMULTI:
    893 		RAY_DPRINTF(("%s: ioctl: cmd SIOCADDMULTI\n", ifp->if_xname));
    894 	case SIOCDELMULTI:
    895 		if (cmd == SIOCDELMULTI)
    896 			RAY_DPRINTF(("%s: ioctl: cmd SIOCDELMULTI\n",
    897 			    ifp->if_xname));
    898 		if (cmd == SIOCADDMULTI)
    899 			error = ether_addmulti(ifr, &sc->sc_ec);
    900 		else
    901 			error = ether_delmulti(ifr, &sc->sc_ec);
    902 		if (error == ENETRESET) {
    903 			error = 0;
    904 			ray_update_mcast(sc);
    905 		}
    906 	case SIOCSIFMEDIA:
    907 		RAY_DPRINTF(("%s: ioctl: cmd SIOCSIFMEDIA\n", ifp->if_xname));
    908 	case SIOCGIFMEDIA:
    909 		if (cmd == SIOCGIFMEDIA)
    910 			RAY_DPRINTF(("%s: ioctl: cmd SIOCGIFMEDIA\n",
    911 			    ifp->if_xname));
    912 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    913 		break;
    914 	case SIOCSRAYPARAM:
    915 		RAY_DPRINTF(("%s: ioctl: cmd SIOCSRAYPARAM\n", ifp->if_xname));
    916 		if ((error = copyin(ifr->ifr_data, &pr, sizeof(pr))))
    917 			break;
    918 		/* disallow certain command that have another interface */
    919 		switch (pr.r_paramid) {
    920 		case RAY_PID_NET_TYPE:	/* through media opt */
    921 		case RAY_PID_AP_STATUS:	/* unsupported */
    922 		case RAY_PID_SSID:	/* use SIOC80211[GS]NWID */
    923 		case RAY_PID_MAC_ADDR:	/* XXX need interface? */
    924 		case RAY_PID_PROMISC:	/* bpf */
    925 			error = EINVAL;
    926 			break;
    927 		}
    928 		error = ray_user_update_params(sc, &pr);
    929 		error2 = copyout(&pr, ifr->ifr_data, sizeof(pr));
    930 		error = error2 ? error2 : error;
    931 		break;
    932 	case SIOCGRAYPARAM:
    933 		RAY_DPRINTF(("%s: ioctl: cmd SIOCGRAYPARAM\n", ifp->if_xname));
    934 		if ((error = copyin(ifr->ifr_data, &pr, sizeof(pr))))
    935 			break;
    936 		error = ray_user_report_params(sc, &pr);
    937 		error2 = copyout(&pr, ifr->ifr_data, sizeof(pr));
    938 		error = error2 ? error2 : error;
    939 		break;
    940 	case SIOCS80211NWID:
    941 		RAY_DPRINTF(("%s: ioctl: cmd SIOCSNWID\n", ifp->if_xname));
    942 		/*
    943 		 * if later people overwrite thats ok -- the latest version
    944 		 * will always get start/joined even if it was set by
    945 		 * a previous command
    946 		 */
    947 		if ((error = copyin(ifr->ifr_data, nwid, sizeof(nwid))))
    948 			break;
    949 		if (!memcmp(sc->sc_dnwid, nwid, sizeof(nwid)))
    950 			break;
    951 		memcpy(sc->sc_dnwid, nwid, sizeof(nwid));
    952 		if (ifp->if_flags & IFF_RUNNING)
    953 			ray_start_join_net(sc);
    954 		break;
    955 	case SIOCG80211NWID:
    956 		RAY_DPRINTF(("%s: ioctl: cmd SIOCHNWID\n", ifp->if_xname));
    957 		error2 = copyout(sc->sc_cnwid, ifr->ifr_data,
    958 		    IEEE80211_NWID_LEN);
    959 		break;
    960 	default:
    961 		RAY_DPRINTF(("%s: ioctl: unknown\n", ifp->if_xname));
    962 		error = EINVAL;
    963 		break;
    964 	}
    965 
    966 	splx(s);
    967 
    968 	return (error);
    969 }
    970 
    971 /*
    972  * ifnet interface to start transmission on the interface
    973  */
    974 static void
    975 ray_if_start(ifp)
    976 	struct ifnet *ifp;
    977 {
    978 	struct ray_softc *sc;
    979 
    980 	sc = ifp->if_softc;
    981 	ray_intr_start(sc);
    982 }
    983 
    984 static int
    985 ray_media_change(ifp)
    986 	struct ifnet *ifp;
    987 {
    988 	/* XXX choose between adhoc and infra */
    989 	return (0);
    990 }
    991 
    992 static void
    993 ray_media_status(ifp, imr)
    994 	struct ifnet *ifp;
    995 	struct ifmediareq *imr;
    996 {
    997 	struct ray_softc *sc;
    998 
    999 	sc = ifp->if_softc;
   1000 
   1001 	imr->ifm_status = IFM_AVALID;
   1002 	if (sc->sc_havenet)
   1003 		imr->ifm_status |= IFM_ACTIVE;
   1004 
   1005 	if (sc->sc_mode == SC_MODE_ADHOC)
   1006 		imr->ifm_active = IFM_ADHOC;
   1007 	else
   1008 		imr->ifm_active = IFM_INFRA;
   1009 }
   1010 
   1011 
   1012 
   1013 /*
   1014  * called to start from ray_intr.  We don't check for pending
   1015  * interrupt as a result
   1016  */
   1017 static void
   1018 ray_intr_start(sc)
   1019 	struct ray_softc *sc;
   1020 {
   1021 	struct ieee80211_frame *iframe;
   1022 	struct ether_header *eh;
   1023 	size_t len, pktlen, tmplen;
   1024 	bus_size_t bufp, ebufp;
   1025 	struct mbuf *m0, *m;
   1026 	struct ifnet *ifp;
   1027 	u_int firsti, hinti, previ, i;
   1028 	u_int16_t et;
   1029 	u_int8_t *d;
   1030 
   1031 	ifp = &sc->sc_if;
   1032 
   1033 	RAY_DPRINTF(("%s: start free %d qlen %d qmax %d\n",
   1034 	    ifp->if_xname, sc->sc_txfree, ifp->if_snd.ifq_len,
   1035 	    ifp->if_snd.ifq_maxlen));
   1036 
   1037 	ray_cmd_cancel(sc, SCP_IFSTART);
   1038 
   1039 	if ((ifp->if_flags & IFF_RUNNING) == 0 || !sc->sc_havenet)
   1040 		return;
   1041 
   1042 	if (ifp->if_snd.ifq_len == 0)
   1043 		return;
   1044 
   1045 	firsti = i = previ = RAY_CCS_LINK_NULL;
   1046 	hinti = RAY_CCS_TX_FIRST;
   1047 
   1048 	if (!RAY_ECF_READY(sc)) {
   1049 		ray_cmd_schedule(sc, SCP_IFSTART);
   1050 		return;
   1051 	}
   1052 
   1053 	for (;;) {
   1054 		/* if we have no descriptors be done */
   1055 		if (i == RAY_CCS_LINK_NULL) {
   1056 			i = ray_find_free_tx_ccs(sc, hinti);
   1057 			if (i == RAY_CCS_LINK_NULL) {
   1058 				ifp->if_flags |= IFF_OACTIVE;
   1059 				break;
   1060 			}
   1061 		}
   1062 
   1063 		IF_DEQUEUE(&ifp->if_snd, m0);
   1064 		if (!m0)
   1065 			break;
   1066 		RAY_DPRINTF(("%s: gotmbuf 0x%lx\n", ifp->if_xname, (long)m0));
   1067 		pktlen = m0->m_pkthdr.len;
   1068 		if (pktlen > ETHER_MAX_LEN - ETHER_CRC_LEN) {
   1069 			RAY_DPRINTF((
   1070 			    "%s: mbuf too long %d\n", ifp->if_xname, pktlen));
   1071 			m_freem(m0);
   1072 			continue;
   1073 		}
   1074 		RAY_DPRINTF(("%s: mbuf.m_pkthdr.len %d\n", ifp->if_xname,
   1075 		    (int)pktlen));
   1076 
   1077 		/* we need the ether_header now for pktlen adjustments */
   1078 		M_PULLUP(m0, sizeof(struct ether_header));
   1079 		if (!m0) {
   1080 			RAY_DPRINTF(( "%s: couldn\'t pullup ether header\n",
   1081 			    ifp->if_xname));
   1082 			continue;
   1083 		}
   1084 		RAY_DPRINTF(("%s: got pulled up mbuf 0x%lx\n", ifp->if_xname,
   1085 		    (long)m0));
   1086 
   1087 		/* first peek at the type of packet and figure out what to do */
   1088 		eh = mtod(m0, struct ether_header *);
   1089 		et = ntohs(eh->ether_type);
   1090 		if (ifp->if_flags & IFF_LINK0) {
   1091 			/* don't support llc for windows compat operation */
   1092 			if (et <= ETHERMTU) {
   1093 				m_freem(m0);
   1094 				continue;
   1095 			}
   1096 			tmplen = sizeof(struct ieee80211_frame);
   1097 		} else if (et > ETHERMTU) {
   1098 			/* adjust for LLC/SNAP header */
   1099 			tmplen= sizeof(struct ieee80211_frame) - ETHER_ADDR_LEN;
   1100 		}
   1101 		/* now get our space for the 802.11 frame */
   1102 		M_PREPEND(m0, tmplen, M_DONTWAIT);
   1103 		if (m0)
   1104 			M_PULLUP(m0, sizeof(struct ether_header) + tmplen);
   1105 		if (!m0) {
   1106 			RAY_DPRINTF(("%s: couldn\'t prepend header\n",
   1107 			    ifp->if_xname));
   1108 			continue;
   1109 		}
   1110 		/* copy the frame into the mbuf for tapping */
   1111 		iframe = mtod(m0, struct ieee80211_frame *);
   1112 		eh = (struct ether_header *)((u_int8_t *)iframe + tmplen);
   1113 		iframe->i_fc[0] =
   1114 		    (IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA);
   1115 		iframe->i_fc[1] = IEEE80211_FC1_RCVFROM_TERMINAL;
   1116 		iframe->i_dur[0] = iframe->i_dur[1] = 0;
   1117 
   1118 		/* XXX this only supports ad-hoc [dst, src, bssid] */
   1119 		memcpy(iframe->i_addr1, eh->ether_dhost, ETHER_ADDR_LEN);
   1120 		memcpy(iframe->i_addr2, eh->ether_shost, ETHER_ADDR_LEN);
   1121 		memcpy(iframe->i_addr3, sc->sc_bssid, ETHER_ADDR_LEN);
   1122 		iframe->i_seq[0] = iframe->i_seq[1] = 0;
   1123 
   1124 		/* if not using crummy E2 in 802.11 make it LLC/SNAP */
   1125 		if ((ifp->if_flags & IFF_LINK0) == 0 && et > ETHERMTU)
   1126 			memcpy(iframe + 1, llc_snapid, sizeof(llc_snapid));
   1127 
   1128 		RAY_DPRINTF(("%s: i %d previ %d\n", ifp->if_xname, i, previ));
   1129 
   1130 		if (firsti == RAY_CCS_LINK_NULL)
   1131 			firsti = i;
   1132 
   1133 		pktlen = m0->m_pkthdr.len;
   1134 		bufp = ray_fill_in_tx_ccs(sc, pktlen, i, previ);
   1135 		previ = hinti = i;
   1136 		i = RAY_CCS_LINK_NULL;
   1137 
   1138 		RAY_DPRINTF(("%s: bufp 0x%lx new pktlen %d\n",
   1139 		    ifp->if_xname, (long)bufp, (int)pktlen));
   1140 
   1141 		/* copy out mbuf */
   1142 		for (m = m0; m; m = m->m_next) {
   1143 			if ((len = m->m_len) == 0)
   1144 				continue;
   1145 			RAY_DPRINTF((
   1146 			    "%s: copying mbuf 0x%lx bufp 0x%lx len %d\n",
   1147 			    ifp->if_xname, (long)m, (long)bufp, (int)len));
   1148 			d = mtod(m, u_int8_t *);
   1149 			ebufp = bufp + len;
   1150 			if (ebufp <= RAY_TX_END)
   1151 				ray_write_region(sc, bufp, d, len);
   1152 			else {
   1153 				panic("ray_intr_start");	/* XXX */
   1154 				/* wrapping */
   1155 				tmplen = ebufp - bufp;
   1156 				len -= tmplen;
   1157 				ray_write_region(sc, bufp, d, tmplen);
   1158 				d += tmplen;
   1159 				bufp = RAY_TX_BASE;
   1160 				ray_write_region(sc, bufp, d, len);
   1161 			}
   1162 			bufp += len;
   1163 		}
   1164 #if NBPFILTER > 0
   1165 		if (ifp->if_bpf) {
   1166 			if (ifp->if_flags & IFF_LINK0) {
   1167 				m0->m_data += sizeof(struct ieee80211_frame);
   1168 				m0->m_len -=  sizeof(struct ieee80211_frame);
   1169 				m0->m_pkthdr.len -=  sizeof(struct ieee80211_frame);
   1170 			}
   1171 			bpf_mtap(ifp->if_bpf, m0);
   1172 			if (ifp->if_flags & IFF_LINK0) {
   1173 				m0->m_data -= sizeof(struct ieee80211_frame);
   1174 				m0->m_len +=  sizeof(struct ieee80211_frame);
   1175 				m0->m_pkthdr.len +=  sizeof(struct ieee80211_frame);
   1176 			}
   1177 		}
   1178 #endif
   1179 
   1180 #ifdef RAY_DEBUG
   1181 		if (ray_debug && ray_debug_dump_tx)
   1182 			ray_dump_mbuf(sc, m0);
   1183 #endif
   1184 		m_freem(m0);
   1185 	}
   1186 
   1187 	if (firsti == RAY_CCS_LINK_NULL)
   1188 		return;
   1189 	i = 0;
   1190 	if (!RAY_ECF_READY(sc)) {
   1191 		/*
   1192 		 * if this can really happen perhaps we need to save
   1193 		 * the chain and use it later.  I think this might
   1194 		 * be a confused state though because we check above
   1195 		 * and don't issue any commands between.
   1196 		 */
   1197 		printf("%s: dropping tx packets device busy\n", sc->sc_xname);
   1198 		ray_free_ccs_chain(sc, firsti);
   1199 		return;
   1200 	}
   1201 
   1202 	/* send it off */
   1203 	RAY_DPRINTF(("%s: ray_start issueing %d \n", sc->sc_xname, firsti));
   1204 	SRAM_WRITE_1(sc, RAY_SCB_CCSI, firsti);
   1205 	RAY_ECF_START_CMD(sc);
   1206 
   1207 	RAY_DPRINTF_XMIT(("%s: sent packet: len %d\n", sc->sc_xname,
   1208 	    pktlen));
   1209 }
   1210 
   1211 /*
   1212  * recevice a packet from the card
   1213  */
   1214 static void
   1215 ray_recv(sc, ccs)
   1216 	struct ray_softc *sc;
   1217 	bus_size_t ccs;
   1218 {
   1219 	struct ieee80211_frame *frame;
   1220 	struct ether_header *eh;
   1221 	struct mbuf *m;
   1222 	size_t pktlen, len, lenread;
   1223 	bus_size_t bufp, ebufp, tmp;
   1224 	struct ifnet *ifp;
   1225 	u_int8_t *src, *d;
   1226 	u_int frag, nofrag, ni, i, issnap, first;
   1227 	u_int8_t fc0;
   1228 
   1229 #ifdef RAY_DEBUG
   1230 	/* have a look if you want to see how the card rx works :) */
   1231 	if (ray_debug && ray_debug_dump_desc)
   1232 		hexdump((caddr_t)sc->sc_memh + RAY_RCS_BASE, 0x400,
   1233 		    16, 4, 0);
   1234 #endif
   1235 
   1236 	m = 0;
   1237 	ifp = &sc->sc_if;
   1238 
   1239 	/* it looks like at least with build 4 there is no CRC in length */
   1240 	first = RAY_GET_INDEX(ccs);
   1241 	pktlen = SRAM_READ_FIELD_2(sc, ccs, ray_cmd_rx, c_pktlen);
   1242 
   1243 	RAY_DPRINTF(("%s: recv pktlen %d nofrag %d\n", sc->sc_xname,
   1244 	    pktlen, nofrag));
   1245 	RAY_DPRINTF_XMIT(("%s: received packet: len %d\n", sc->sc_xname,
   1246 	    pktlen));
   1247 	if (pktlen > MCLBYTES
   1248 	    || pktlen < (sizeof(*frame) + sizeof(struct llc))) {
   1249 		RAY_DPRINTF(("%s: PKTLEN TOO BIG OR TOO SMALL\n",
   1250 		    sc->sc_xname));
   1251 		ifp->if_ierrors++;
   1252 		goto done;
   1253 	}
   1254 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1255 	if (!m) {
   1256 		RAY_DPRINTF(("%s: MGETHDR FAILED\n", sc->sc_xname));
   1257 		ifp->if_ierrors++;
   1258 		goto done;
   1259 	}
   1260 	if (pktlen > MHLEN) {
   1261 		/* XXX should allow chaining? */
   1262 		MCLGET(m, M_DONTWAIT);
   1263 		if ((m->m_flags & M_EXT) == 0) {
   1264 			RAY_DPRINTF(("%s: MCLGET FAILED\n", sc->sc_xname));
   1265 			ifp->if_ierrors++;
   1266 			m_freem(m);
   1267 			m = 0;
   1268 			goto done;
   1269 		}
   1270 	}
   1271 	m->m_pkthdr.rcvif = ifp;
   1272 	m->m_pkthdr.len = pktlen;
   1273 	m->m_len = pktlen;
   1274 	d = mtod(m, u_int8_t *);
   1275 
   1276 	RAY_DPRINTF(("%s: recv ccs index %d\n", sc->sc_xname, first));
   1277 	frag = 0;
   1278 	lenread = 0;
   1279 	i = ni = first;
   1280 	while ((i = ni) && i != RAY_CCS_LINK_NULL) {
   1281 		ccs = RAY_GET_CCS(i);
   1282 		bufp = SRAM_READ_FIELD_2(sc, ccs, ray_cmd_rx, c_bufp);
   1283 		len = SRAM_READ_FIELD_2(sc, ccs, ray_cmd_rx, c_len);
   1284 		/* remove the CRC */
   1285 #if 0
   1286 		/* at least with build 4 no crc seems to be here */
   1287 		if (frag++ == 0)
   1288 			len -= 4;
   1289 #endif
   1290 		ni = SRAM_READ_FIELD_1(sc, ccs, ray_cmd_rx, c_nextfrag);
   1291 		RAY_DPRINTF(("%s: recv frag index %d len %d bufp 0x%x ni %d\n",
   1292 		    sc->sc_xname, i, len, (int)bufp, ni));
   1293 		if (len + lenread > pktlen) {
   1294 			RAY_DPRINTF(("%s: BAD LEN current 0x%x pktlen 0x%x\n",
   1295 			    sc->sc_xname, len + lenread, pktlen));
   1296 			ifp->if_ierrors++;
   1297 			m_freem(m);
   1298 			m = 0;
   1299 			goto done;
   1300 		}
   1301 		if (i < RAY_RCCS_FIRST) {
   1302 			printf("ray_recv: bad ccs index 0x%x\n", i);
   1303 			m_freem(m);
   1304 			m = 0;
   1305 			goto done;
   1306 		}
   1307 
   1308 		ebufp = bufp + len;
   1309 		if (ebufp <= RAY_RX_END)
   1310 			ray_read_region(sc, bufp, d, len);
   1311 		else {
   1312 			/* wrapping */
   1313 			ray_read_region(sc, bufp, d, (tmp = RAY_RX_END - bufp));
   1314 			ray_read_region(sc, RAY_RX_BASE, d + tmp, ebufp - RAY_RX_END);
   1315 		}
   1316 		d += len;
   1317 		lenread += len;
   1318 	}
   1319 done:
   1320 
   1321 	RAY_DPRINTF(("%s: recv frag count %d\n", sc->sc_xname, frag));
   1322 
   1323 	/* free the rcss */
   1324 	ni = first;
   1325 	while ((i = ni) && (i != RAY_CCS_LINK_NULL)) {
   1326 		ccs = RAY_GET_CCS(i);
   1327 		ni = SRAM_READ_FIELD_1(sc, ccs, ray_cmd_rx, c_nextfrag);
   1328 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status,
   1329 		    RAY_CCS_STATUS_FREE);
   1330 	}
   1331 
   1332 	if (!m)
   1333 		return;
   1334 
   1335 	RAY_DPRINTF(("%s: recv got packet pktlen %d actual %d\n",
   1336 	    sc->sc_xname, pktlen, lenread));
   1337 #ifdef RAY_DEBUG
   1338 	if (ray_debug && ray_debug_dump_rx)
   1339 		ray_dump_mbuf(sc, m);
   1340 #endif
   1341 	/* receivce the packet */
   1342 	frame = mtod(m, struct ieee80211_frame *);
   1343 	fc0 = frame->i_fc[0]
   1344 	   & (IEEE80211_FC0_VERSION_MASK|IEEE80211_FC0_TYPE_MASK);
   1345 	if ((fc0 & IEEE80211_FC0_VERSION_MASK) != IEEE80211_FC0_VERSION_0) {
   1346 		RAY_DPRINTF(("%s: pkt not version 0 fc 0x%x\n",
   1347 		    sc->sc_xname, fc0));
   1348 		m_freem(m);
   1349 		return;
   1350 	}
   1351 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA) {
   1352 		RAY_DPRINTF(("%s: pkt not type data fc0 0x%x\n",
   1353 		    sc->sc_xname, fc0));
   1354 		m_freem(m);
   1355 		return;
   1356 	}
   1357 
   1358 	if (!memcmp(frame + 1, llc_snapid, sizeof(llc_snapid)))
   1359 		issnap = 1;
   1360 	else {
   1361 		/*
   1362 		 * if user has link0 flag set we allow the weird
   1363 		 * Ethernet2 in 802.11 encapsulation produced by
   1364 		 * the windows driver for the WebGear card
   1365 		 */
   1366 		RAY_DPRINTF(("%s: pkt not snap 0\n", sc->sc_xname));
   1367 		if ((ifp->if_flags & IFF_LINK0) == 0) {
   1368 			m_freem(m);
   1369 			return;
   1370 		}
   1371 		issnap = 0;
   1372 	}
   1373 	switch (frame->i_fc[1] & IEEE80211_FC1_RCVFROM_MASK) {
   1374 	case IEEE80211_FC1_RCVFROM_TERMINAL:
   1375 		src = frame->i_addr2;
   1376 		break;
   1377 	case IEEE80211_FC1_RCVFROM_AP:
   1378 		src = frame->i_addr3;
   1379 		break;
   1380 	case IEEE80211_FC1_RCVFROM_AP2AP:
   1381 		RAY_DPRINTF(("%s: pkt ap2ap\n", sc->sc_xname));
   1382 		m_freem(m);
   1383 		return;
   1384 	default:
   1385 		RAY_DPRINTF(("%s: pkt type unknown\n", sc->sc_xname));
   1386 		m_freem(m);
   1387 		return;
   1388 	}
   1389 	/*
   1390 	 * This is a mess.. we should support other LLC frame types
   1391 	 */
   1392 	if (issnap) {
   1393 		/* create an ether_header over top of the 802.11+SNAP header */
   1394 		eh = (struct ether_header *)((caddr_t)(frame + 1) - 6);
   1395 		memcpy(eh->ether_shost, src, ETHER_ADDR_LEN);
   1396 		memcpy(eh->ether_dhost, frame->i_addr1, ETHER_ADDR_LEN);
   1397 	} else {
   1398 		/* this is the weird e2 in 802.11 encapsulation */
   1399 		eh = (struct ether_header *)(frame + 1);
   1400 	}
   1401 	m_adj(m, (caddr_t)eh - (caddr_t)frame);
   1402 #if NBPFILTER > 0
   1403 	if (ifp->if_bpf)
   1404 		bpf_mtap(ifp->if_bpf, m);
   1405 #endif
   1406 	/* XXX doesn't appear to be included m->m_flags |= M_HASFCS; */
   1407 	(*ifp->if_input)(ifp, m);
   1408 }
   1409 
   1410 
   1411 /*
   1412  * scan for free buffers
   1413  *
   1414  * Note: do _not_ try to optimize this away, there is some kind of
   1415  * horrible interaction with receiving tx interrupts and they
   1416  * have to be done as fast as possible, which means zero processing.
   1417  * this took ~ever to figure out, don't make someone do it again!
   1418  */
   1419 static u_int
   1420 ray_find_free_tx_ccs(sc, hint)
   1421 	struct ray_softc *sc;
   1422 	u_int hint;
   1423 {
   1424 	u_int i, stat;
   1425 
   1426 	for (i = hint; i <= RAY_CCS_TX_LAST; i++) {
   1427 		stat = SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status);
   1428 		if (stat == RAY_CCS_STATUS_FREE)
   1429 			return (i);
   1430 	}
   1431 
   1432 	if (hint == RAY_CCS_TX_FIRST)
   1433 		return (RAY_CCS_LINK_NULL);
   1434 
   1435 	for (i = RAY_CCS_TX_FIRST; i < hint; i++) {
   1436 		stat = SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status);
   1437 		if (stat == RAY_CCS_STATUS_FREE)
   1438 			return (i);
   1439 	}
   1440 	return (RAY_CCS_LINK_NULL);
   1441 }
   1442 
   1443 /*
   1444  * allocate, initialize and link in a tx ccs for the given
   1445  * page and the current chain values
   1446  */
   1447 static bus_size_t
   1448 ray_fill_in_tx_ccs(sc, pktlen, i, pi)
   1449 	struct ray_softc *sc;
   1450 	size_t pktlen;
   1451 	u_int i, pi;
   1452 {
   1453 	bus_size_t ccs, bufp;
   1454 
   1455 	/* pktlen += RAY_TX_PHY_SIZE; */
   1456 	bufp = RAY_TX_BASE + i * RAY_TX_BUF_SIZE;
   1457 	bufp += sc->sc_txpad;
   1458 	ccs = RAY_GET_CCS(i);
   1459 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_status, RAY_CCS_STATUS_BUSY);
   1460 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_cmd, RAY_CMD_TX_REQ);
   1461 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_link, RAY_CCS_LINK_NULL);
   1462 	SRAM_WRITE_FIELD_2(sc, ccs, ray_cmd_tx, c_bufp, bufp);
   1463 	SRAM_WRITE_FIELD_2(sc, ccs, ray_cmd_tx, c_len, pktlen);
   1464 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_tx_rate, sc->sc_deftxrate);
   1465 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_apm_mode, 0);
   1466 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_antenna, 0);
   1467 
   1468 	/* link us in */
   1469 	if (pi != RAY_CCS_LINK_NULL)
   1470 		SRAM_WRITE_FIELD_1(sc, RAY_GET_CCS(pi), ray_cmd_tx, c_link, i);
   1471 
   1472 	RAY_DPRINTF(("%s: ray_alloc_tx_ccs bufp 0x%lx idx %d pidx %d \n",
   1473 	    sc->sc_xname, bufp, i, pi));
   1474 
   1475 	return (bufp + RAY_TX_PHY_SIZE);
   1476 }
   1477 
   1478 /*
   1479  * an update params command has completed lookup which command and
   1480  * the status
   1481  */
   1482 static ray_cmd_func_t
   1483 ray_update_params_done(sc, ccs, stat)
   1484 	struct ray_softc *sc;
   1485 	bus_size_t ccs;
   1486 	u_int stat;
   1487 {
   1488 	ray_cmd_func_t rcmd;
   1489 
   1490 	rcmd = 0;
   1491 
   1492 	RAY_DPRINTF(("%s: ray_update_params_done stat %d\n",
   1493 	   sc->sc_xname, stat));
   1494 
   1495 	/* this will get more complex as we add commands */
   1496 	if (stat == RAY_CCS_STATUS_FAIL) {
   1497 		printf("%s: failed to update a promisc\n", sc->sc_xname);
   1498 		/* XXX should probably reset */
   1499 		/* rcmd = ray_reset; */
   1500 	}
   1501 
   1502 	if (sc->sc_running & SCP_UPD_PROMISC) {
   1503 		ray_cmd_done(sc, SCP_UPD_PROMISC);
   1504 		sc->sc_promisc = SRAM_READ_1(sc, RAY_HOST_TO_ECF_BASE);
   1505 		RAY_DPRINTF(("%s: new promisc value %d\n", sc->sc_xname,
   1506 		    sc->sc_promisc));
   1507 	} else if (sc->sc_updreq) {
   1508 		ray_cmd_done(sc, SCP_UPD_UPDATEPARAMS);
   1509 		/* get the update parameter */
   1510 		sc->sc_updreq->r_failcause =
   1511 		    SRAM_READ_FIELD_1(sc, ccs, ray_cmd_update, c_failcause);
   1512 		sc->sc_updreq = 0;
   1513 		wakeup(ray_update_params);
   1514 
   1515 		rcmd = ray_start_join_net;
   1516 	}
   1517 	return (rcmd);
   1518 }
   1519 
   1520 /*
   1521  *  check too see if we have any pending commands.
   1522  */
   1523 static void
   1524 ray_check_scheduled(arg)
   1525 	void *arg;
   1526 {
   1527 	struct ray_softc *sc;
   1528 	int s, i, mask;
   1529 
   1530 	s = splnet();
   1531 
   1532 	sc = arg;
   1533 	RAY_DPRINTF((
   1534 	    "%s: ray_check_scheduled enter schd 0x%x running 0x%x ready %d\n",
   1535 	    sc->sc_xname, sc->sc_scheduled, sc->sc_running, RAY_ECF_READY(sc)));
   1536 
   1537 	if (sc->sc_timoneed) {
   1538 		untimeout(ray_check_scheduled, sc);
   1539 		sc->sc_timoneed = 0;
   1540 	}
   1541 
   1542 	/* if update subcmd is running -- clear it in scheduled */
   1543 	if (sc->sc_running & SCP_UPDATESUBCMD)
   1544 		sc->sc_scheduled &= ~SCP_UPDATESUBCMD;
   1545 
   1546 	mask = SCP_FIRST;
   1547 	for (i = 0; i < ray_ncmdtab; mask <<= 1, i++) {
   1548 		if ((sc->sc_scheduled & ~SCP_UPD_MASK) == 0)
   1549 			break;
   1550 		if (!RAY_ECF_READY(sc))
   1551 			break;
   1552 		if (sc->sc_scheduled & mask)
   1553 			(*ray_cmdtab[i])(sc);
   1554 	}
   1555 
   1556 	RAY_DPRINTF((
   1557 	    "%s: ray_check_scheduled exit sched 0x%x running 0x%x ready %d\n",
   1558 	    sc->sc_xname, sc->sc_scheduled, sc->sc_running, RAY_ECF_READY(sc)));
   1559 
   1560 	if (sc->sc_scheduled & ~SCP_UPD_MASK)
   1561 		ray_set_pending(sc, sc->sc_scheduled);
   1562 
   1563 	splx(s);
   1564 }
   1565 
   1566 /*
   1567  * check for unreported returns
   1568  *
   1569  * this routine is coded to only expect one outstanding request for the
   1570  * timed out requests at a time, but thats all that can be outstanding
   1571  * per hardware limitations
   1572  */
   1573 static void
   1574 ray_check_ccs(arg)
   1575 	void *arg;
   1576 {
   1577 	ray_cmd_func_t fp;
   1578 	struct ray_softc *sc;
   1579 	u_int i, cmd, stat;
   1580 	bus_size_t ccs;
   1581 	int s;
   1582 
   1583 	s = splnet();
   1584 	sc = arg;
   1585 
   1586 	RAY_DPRINTF(("%s: ray_check_ccs\n", sc->sc_xname));
   1587 
   1588 	sc->sc_timocheck = 0;
   1589 	for (i = RAY_CCS_CMD_FIRST; i <= RAY_CCS_CMD_LAST; i++) {
   1590 		if (!sc->sc_ccsinuse[i])
   1591 			continue;
   1592 		ccs = RAY_GET_CCS(i);
   1593 		cmd = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd);
   1594 		switch (cmd) {
   1595 		case RAY_CMD_START_PARAMS:
   1596 		case RAY_CMD_UPDATE_MCAST:
   1597 		case RAY_CMD_UPDATE_PARAMS:
   1598 			stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   1599 			RAY_DPRINTF(("%s: check ccs idx %d ccs 0x%lx "
   1600 			    "cmd 0x%x stat %d\n", sc->sc_xname, i,
   1601 			    ccs, cmd, stat));
   1602 			goto breakout;
   1603 		}
   1604 	}
   1605 breakout:
   1606 	/* see if we got one of the commands we are looking for */
   1607 	if (i > RAY_CCS_CMD_LAST)
   1608 		; /* nothign */
   1609 	else if (stat == RAY_CCS_STATUS_FREE) {
   1610 		stat = RAY_CCS_STATUS_COMPLETE;
   1611 		if ((fp = ray_ccs_done(sc, ccs)))
   1612 			(*fp)(sc);
   1613 	} else if (stat != RAY_CCS_STATUS_BUSY) {
   1614 		if (sc->sc_ccsinuse[i] == 1) {
   1615 			/* give a chance for the interrupt to occur */
   1616 			sc->sc_ccsinuse[i] = 2;
   1617 			if (!sc->sc_timocheck) {
   1618 				timeout(ray_check_ccs, sc, 1);
   1619 				sc->sc_timocheck = 1;
   1620 			}
   1621 		} else if ((fp = ray_ccs_done(sc, ccs)))
   1622 			(*fp)(sc);
   1623 	} else {
   1624 		timeout(ray_check_ccs, sc, RAY_CHECK_CCS_TIMEOUT);
   1625 		sc->sc_timocheck = 1;
   1626 	}
   1627 	splx(s);
   1628 }
   1629 
   1630 /*
   1631  * read the counters, the card implements the following protocol
   1632  * to keep the values from being changed while read:  It checks
   1633  * the `own' bit and if zero writes the current internal counter
   1634  * value, it then sets the `own' bit to 1.  If the `own' bit was 1 it
   1635  * incremenets its internal counter.  The user thus reads the counter
   1636  * if the `own' bit is one and then sets the own bit to 0.
   1637  */
   1638 static void
   1639 ray_update_error_counters(sc)
   1640 	struct ray_softc *sc;
   1641 {
   1642 	bus_size_t csc;
   1643 
   1644 	/* try and update the error counters */
   1645 	csc = RAY_STATUS_BASE;
   1646 	if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_mrxo_own)) {
   1647 		sc->sc_rxoverflow +=
   1648 		    SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_mrx_overflow);
   1649 		SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_mrxo_own, 0);
   1650 	}
   1651 	if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_mrxc_own)) {
   1652 		sc->sc_rxcksum +=
   1653 		    SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_mrx_overflow);
   1654 		SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_mrxc_own, 0);
   1655 	}
   1656 	if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_rxhc_own)) {
   1657 		sc->sc_rxhcksum +=
   1658 		    SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_rx_hcksum);
   1659 		SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_rxhc_own, 0);
   1660 	}
   1661 	sc->sc_rxnoise = SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_rx_noise);
   1662 }
   1663 
   1664 /*
   1665  * one of the commands we issued has completed, process.
   1666  */
   1667 static ray_cmd_func_t
   1668 ray_ccs_done(sc, ccs)
   1669 	struct ray_softc *sc;
   1670 	bus_size_t ccs;
   1671 {
   1672 	struct ifnet *ifp;
   1673 	ray_cmd_func_t rcmd;
   1674 	u_int cmd, stat;
   1675 
   1676 	ifp = &sc->sc_if;
   1677 	cmd = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd);
   1678 	stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   1679 
   1680 	RAY_DPRINTF(("%s: ray_ccs_done idx %ld cmd 0x%x stat %d\n",
   1681 	    sc->sc_xname, RAY_GET_INDEX(ccs), cmd, stat));
   1682 
   1683 	rcmd = 0;
   1684 	switch (cmd) {
   1685 	/*
   1686 	 * solicited commands
   1687 	 */
   1688 	case RAY_CMD_START_PARAMS:
   1689 		/* start network */
   1690 		ray_cmd_done(sc, SCP_UPD_STARTUP);
   1691 
   1692 		/* ok to start queueing packets */
   1693 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
   1694 
   1695 		sc->sc_omode = sc->sc_mode;
   1696 		memcpy(sc->sc_cnwid, sc->sc_dnwid, sizeof(sc->sc_cnwid));
   1697 
   1698 		rcmd = ray_start_join_net;
   1699 		break;
   1700 	case RAY_CMD_UPDATE_PARAMS:
   1701 		rcmd = ray_update_params_done(sc, ccs, stat);
   1702 		break;
   1703 	case RAY_CMD_REPORT_PARAMS:
   1704 		/* get the reported parameters */
   1705 		ray_cmd_done(sc, SCP_REPORTPARAMS);
   1706 		if (!sc->sc_repreq)
   1707 			break;
   1708 		sc->sc_repreq->r_failcause =
   1709 		    SRAM_READ_FIELD_1(sc, ccs, ray_cmd_report, c_failcause);
   1710 		sc->sc_repreq->r_len =
   1711 		    SRAM_READ_FIELD_1(sc, ccs, ray_cmd_report, c_len);
   1712 		ray_read_region(sc, RAY_ECF_TO_HOST_BASE, sc->sc_repreq->r_data,
   1713 		    sc->sc_repreq->r_len);
   1714 		sc->sc_repreq = 0;
   1715 		wakeup(ray_report_params);
   1716 		break;
   1717 	case RAY_CMD_UPDATE_MCAST:
   1718 		ray_cmd_done(sc, SCP_UPD_MCAST);
   1719 		if (stat == RAY_CCS_STATUS_FAIL)
   1720 			rcmd = ray_reset;
   1721 		break;
   1722 	case RAY_CMD_START_NET:
   1723 	case RAY_CMD_JOIN_NET:
   1724 		rcmd = ray_start_join_net_done(sc, cmd, ccs, stat);
   1725 		break;
   1726 	case RAY_CMD_TX_REQ:
   1727 		if (sc->sc_if.if_flags & IFF_OACTIVE) {
   1728 			sc->sc_if.if_flags &= ~IFF_OACTIVE;
   1729 			/* this may also be a problem */
   1730 			rcmd = ray_intr_start;
   1731 		}
   1732 		/* free it -- no tracking */
   1733 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status,
   1734 		    RAY_CCS_STATUS_FREE);
   1735 		goto done;
   1736 	case RAY_CMD_START_ASSOC:
   1737 		ray_cmd_done(sc, SCP_STARTASSOC);
   1738 		if (stat == RAY_CCS_STATUS_FAIL)
   1739 			rcmd = ray_start_join_net;	/* XXX check */
   1740 		else
   1741 			rcmd = ray_intr_start;
   1742 		break;
   1743 	case RAY_CMD_UPDATE_APM:
   1744 	case RAY_CMD_TEST_MEM:
   1745 	case RAY_CMD_SHUTDOWN:
   1746 	case RAY_CMD_DUMP_MEM:
   1747 	case RAY_CMD_START_TIMER:
   1748 		break;
   1749 	default:
   1750 		printf("%s: intr: unknown command 0x%x\n",
   1751 		    sc->sc_if.if_xname, cmd);
   1752 		break;
   1753 	}
   1754 	ray_free_ccs(sc, ccs);
   1755 done:
   1756 	/*
   1757 	 * see if needed things can be done now that a command
   1758 	 * has completed
   1759 	 */
   1760 	ray_check_scheduled(sc);
   1761 
   1762 	return (rcmd);
   1763 }
   1764 
   1765 /*
   1766  * an unsolicted interrupt, i.e., the ECF is sending us a command
   1767  */
   1768 static ray_cmd_func_t
   1769 ray_rccs_intr(sc, ccs)
   1770 	struct ray_softc *sc;
   1771 	bus_size_t ccs;
   1772 {
   1773 	ray_cmd_func_t rcmd;
   1774 	u_int cmd, stat;
   1775 
   1776 	cmd = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd);
   1777 	stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   1778 
   1779 	RAY_DPRINTF(("%s: ray_rccs_intr idx %ld cmd 0x%x stat %d\n",
   1780 	    sc->sc_xname, RAY_GET_INDEX(ccs), cmd, stat));
   1781 
   1782 	rcmd = 0;
   1783 	switch (cmd) {
   1784 	/*
   1785 	 * unsolicted commands
   1786 	 */
   1787 	case RAY_ECMD_RX_DONE:
   1788 		ray_recv(sc, ccs);
   1789 		goto done;
   1790 	case RAY_ECMD_REJOIN_DONE:
   1791 		rcmd = ray_start_assoc;
   1792 		break;
   1793 	case RAY_ECMD_ROAM_START:
   1794 		break;
   1795 	case RAY_ECMD_JAPAN_CALL_SIGNAL:
   1796 		break;
   1797 	default:
   1798 		ray_update_error_counters(sc);
   1799 
   1800 		/* this is a bogus return from build 4 don't free 0x55 */
   1801 		if (sc->sc_version == SC_BUILD_4 && cmd == 0x55
   1802 		    && RAY_GET_INDEX(ccs) == 0x55) {
   1803 			goto done;
   1804 		}
   1805 		printf("%s: intr: unknown command 0x%x\n",
   1806 		    sc->sc_if.if_xname, cmd);
   1807 		break;
   1808 	}
   1809 	/* free the ccs */
   1810 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status, RAY_CCS_STATUS_FREE);
   1811 done:
   1812 	return (rcmd);
   1813 }
   1814 
   1815 /*
   1816  * process an interrupt
   1817  */
   1818 static int
   1819 ray_intr(arg)
   1820 	void *arg;
   1821 {
   1822 	struct ray_softc *sc;
   1823 	ray_cmd_func_t rcmd;
   1824 	u_int i, count;
   1825 
   1826 	sc = arg;
   1827 
   1828 	RAY_DPRINTF(("%s: ray_intr\n", sc->sc_xname));
   1829 
   1830 	if ((++sc->sc_checkcounters % 32) == 0)
   1831 		ray_update_error_counters(sc);
   1832 
   1833 	count = 0;
   1834 	rcmd = 0;
   1835 	if (!REG_READ(sc, RAY_HCSIR))
   1836 		count = 0;
   1837 	else {
   1838 		count = 1;
   1839 		i = SRAM_READ_1(sc, RAY_SCB_RCCSI);
   1840 		if (i <= RAY_CCS_LAST)
   1841 			rcmd = ray_ccs_done(sc, RAY_GET_CCS(i));
   1842 		else if (i <= RAY_RCCS_LAST)
   1843 			rcmd = ray_rccs_intr(sc, RAY_GET_CCS(i));
   1844 		else
   1845 			printf("%s: intr: bad cmd index %d\n", sc->sc_xname, i);
   1846 	}
   1847 
   1848 	if (rcmd)
   1849 		(*rcmd)(sc);
   1850 
   1851 	if (count)
   1852 		REG_WRITE(sc, RAY_HCSIR, 0);
   1853 
   1854 	RAY_DPRINTF(("%s: interrupt handled %d\n", sc->sc_xname, count));
   1855 
   1856 	return (count ? 1 : 0);
   1857 }
   1858 
   1859 
   1860 /*
   1861  * Generic CCS handling
   1862  */
   1863 
   1864 /*
   1865  * free the chain of descriptors -- used for freeing allocated tx chains
   1866  */
   1867 static void
   1868 ray_free_ccs_chain(sc, ni)
   1869 	struct ray_softc *sc;
   1870 	u_int ni;
   1871 {
   1872 	u_int i;
   1873 
   1874 	while ((i = ni) != RAY_CCS_LINK_NULL) {
   1875 		ni = SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_link);
   1876 		SRAM_WRITE_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status,
   1877 		    RAY_CCS_STATUS_FREE);
   1878 	}
   1879 }
   1880 
   1881 /*
   1882  * free up a cmd and return the old status
   1883  * this routine is only used for commands
   1884  */
   1885 static u_int8_t
   1886 ray_free_ccs(sc, ccs)
   1887 	struct ray_softc *sc;
   1888 	bus_size_t ccs;
   1889 {
   1890 	u_int8_t stat;
   1891 
   1892 	RAY_DPRINTF(("%s: free_ccs idx %ld\n", sc->sc_xname,
   1893 	    RAY_GET_INDEX(ccs)));
   1894 
   1895 	stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   1896 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status, RAY_CCS_STATUS_FREE);
   1897 	if (ccs <= RAY_GET_CCS(RAY_CCS_LAST))
   1898 		sc->sc_ccsinuse[RAY_GET_INDEX(ccs)] = 0;
   1899 
   1900 	return (stat);
   1901 }
   1902 
   1903 /*
   1904  * returns 1 and in `ccb' the bus offset of the free ccb
   1905  * or 0 if none are free
   1906  *
   1907  * If `track' is not zero, handles tracking this command
   1908  * possibly indicating a callback is needed and setting a timeout
   1909  * also if ECF isn't ready we terminate earlier to avoid overhead.
   1910  *
   1911  * this routine is only used for commands
   1912  */
   1913 static int
   1914 ray_alloc_ccs(sc, ccsp, cmd, track)
   1915 	struct ray_softc *sc;
   1916 	bus_size_t *ccsp;
   1917 	u_int cmd, track;
   1918 {
   1919 	bus_size_t ccs;
   1920 	u_int i;
   1921 
   1922 	RAY_DPRINTF(("%s: alloc_ccs cmd %d\n", sc->sc_xname, cmd));
   1923 
   1924 	/* for tracked commands, if not ready just set pending */
   1925 	if (track && !RAY_ECF_READY(sc)) {
   1926 		ray_cmd_schedule(sc, track);
   1927 		return (0);
   1928 	}
   1929 
   1930 	/* first scan our inuse array */
   1931 	for (i = RAY_CCS_CMD_FIRST; i <= RAY_CCS_CMD_LAST; i++) {
   1932 		/* XXX wonder if we have to probe here to make the card go */
   1933 		(void)SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status);
   1934 		if (!sc->sc_ccsinuse[i])
   1935 			break;
   1936 	}
   1937 	if (i > RAY_CCS_CMD_LAST) {
   1938 		if (track)
   1939 			ray_cmd_schedule(sc, track);
   1940 		return (0);
   1941 	}
   1942 	sc->sc_ccsinuse[i] = 1;
   1943 	ccs = RAY_GET_CCS(i);
   1944 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status, RAY_CCS_STATUS_BUSY);
   1945 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_cmd, cmd);
   1946 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_link, RAY_CCS_LINK_NULL);
   1947 
   1948 	*ccsp = ccs;
   1949 	return (1);
   1950 }
   1951 
   1952 
   1953 /*
   1954  * this function sets the pending bit for the command given in 'need'
   1955  * and schedules a timeout if none is scheduled already.  Any command
   1956  * that uses the `host to ecf' region must be serialized.
   1957  */
   1958 static void
   1959 ray_set_pending(sc, cmdf)
   1960 	struct ray_softc *sc;
   1961 	u_int cmdf;
   1962 {
   1963 	RAY_DPRINTF(("%s: ray_set_pending 0x%x\n", sc->sc_xname, cmdf));
   1964 
   1965 	sc->sc_scheduled |= cmdf;
   1966 	if (!sc->sc_timoneed) {
   1967 		RAY_DPRINTF(("%s: ray_set_pending new timo\n", sc->sc_xname));
   1968 		timeout(ray_check_scheduled, sc, RAY_CHECK_SCHED_TIMEOUT);
   1969 		sc->sc_timoneed = 1;
   1970 	}
   1971 }
   1972 
   1973 /*
   1974  * schedule the `cmdf' for completion later
   1975  */
   1976 static void
   1977 ray_cmd_schedule(sc, cmdf)
   1978 	struct ray_softc *sc;
   1979 	int cmdf;
   1980 {
   1981 	int track;
   1982 
   1983 	RAY_DPRINTF(("%s: ray_cmd_schedule 0x%x\n", sc->sc_xname, cmdf));
   1984 
   1985 	track = cmdf;
   1986 	if ((cmdf & SCP_UPD_MASK) == 0)
   1987 		ray_set_pending(sc, track);
   1988 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   1989 		/* don't do timeout mechaniscm if subcmd already going */
   1990 		sc->sc_scheduled |= cmdf;
   1991 	} else
   1992 		ray_set_pending(sc, cmdf | SCP_UPDATESUBCMD);
   1993 }
   1994 
   1995 /*
   1996  * check to see if `cmdf' has been scheduled
   1997  */
   1998 static int
   1999 ray_cmd_is_scheduled(sc, cmdf)
   2000 	struct ray_softc *sc;
   2001 	int cmdf;
   2002 {
   2003 	RAY_DPRINTF(("%s: ray_cmd_is_scheduled 0x%x\n", sc->sc_xname, cmdf));
   2004 
   2005 	return ((sc->sc_scheduled & cmdf) ? 1 : 0);
   2006 }
   2007 
   2008 /*
   2009  * cancel a scheduled command (not a running one though!)
   2010  */
   2011 static void
   2012 ray_cmd_cancel(sc, cmdf)
   2013 	struct ray_softc *sc;
   2014 	int cmdf;
   2015 {
   2016 	RAY_DPRINTF(("%s: ray_cmd_cancel 0x%x\n", sc->sc_xname, cmdf));
   2017 
   2018 	sc->sc_scheduled &= ~cmdf;
   2019 	if ((cmdf & SCP_UPD_MASK) && (sc->sc_scheduled & SCP_UPD_MASK) == 0)
   2020 		sc->sc_scheduled &= ~SCP_UPDATESUBCMD;
   2021 
   2022 	/* if nothing else needed cancel the timer */
   2023 	if (sc->sc_scheduled == 0 && sc->sc_timoneed) {
   2024 		untimeout(ray_check_scheduled, sc);
   2025 		sc->sc_timoneed = 0;
   2026 	}
   2027 }
   2028 
   2029 /*
   2030  * called to indicate the 'cmdf' has been issued
   2031  */
   2032 static void
   2033 ray_cmd_ran(sc, cmdf)
   2034 	struct ray_softc *sc;
   2035 	int cmdf;
   2036 {
   2037 	RAY_DPRINTF(("%s: ray_cmd_ran 0x%x\n", sc->sc_xname, cmdf));
   2038 
   2039 	if (cmdf & SCP_UPD_MASK)
   2040 		sc->sc_running |= cmdf | SCP_UPDATESUBCMD;
   2041 	else
   2042 		sc->sc_running |= cmdf;
   2043 
   2044 	if ((cmdf & SCP_TIMOCHECK_CMD_MASK) && !sc->sc_timocheck) {
   2045 		timeout(ray_check_ccs, sc, RAY_CHECK_CCS_TIMEOUT);
   2046 		sc->sc_timocheck = 1;
   2047 	}
   2048 }
   2049 
   2050 /*
   2051  * check to see if `cmdf' has been issued
   2052  */
   2053 static int
   2054 ray_cmd_is_running(sc, cmdf)
   2055 	struct ray_softc *sc;
   2056 	int cmdf;
   2057 {
   2058 	RAY_DPRINTF(("%s: ray_cmd_is_running 0x%x\n", sc->sc_xname, cmdf));
   2059 
   2060 	return ((sc->sc_running & cmdf) ? 1 : 0);
   2061 }
   2062 
   2063 /*
   2064  * the given `cmdf' that was issued has completed
   2065  */
   2066 static void
   2067 ray_cmd_done(sc, cmdf)
   2068 	struct ray_softc *sc;
   2069 	int cmdf;
   2070 {
   2071 	RAY_DPRINTF(("%s: ray_cmd_done 0x%x\n", sc->sc_xname, cmdf));
   2072 
   2073 	sc->sc_running &= ~cmdf;
   2074 	if (cmdf & SCP_UPD_MASK) {
   2075 		sc->sc_running &= ~SCP_UPDATESUBCMD;
   2076 		if (sc->sc_scheduled & SCP_UPD_MASK)
   2077 			ray_cmd_schedule(sc, sc->sc_scheduled & SCP_UPD_MASK);
   2078 	}
   2079 	if ((sc->sc_running & SCP_TIMOCHECK_CMD_MASK) == 0 && sc->sc_timocheck){
   2080 		untimeout(ray_check_ccs, sc);
   2081 		sc->sc_timocheck = 0;
   2082 	}
   2083 }
   2084 
   2085 /*
   2086  * issue the command
   2087  * only used for commands not tx
   2088  */
   2089 static int
   2090 ray_issue_cmd(sc, ccs, track)
   2091 	struct ray_softc *sc;
   2092 	bus_size_t ccs;
   2093 	u_int track;
   2094 {
   2095 	u_int i;
   2096 
   2097 	RAY_DPRINTF(("%s: ray_cmd_issue 0x%x\n", sc->sc_xname, track));
   2098 
   2099 	/*
   2100 	 * XXX other drivers did this, but I think
   2101 	 * what we really want to do is just make sure we don't
   2102 	 * get here or that spinning is ok
   2103 	 */
   2104 	i = 0;
   2105 	while (!RAY_ECF_READY(sc))
   2106 		if (++i > 50) {
   2107 			ray_free_ccs(sc, ccs);
   2108 			if (track)
   2109 				ray_cmd_schedule(sc, track);
   2110 			return (0);
   2111 		}
   2112 
   2113 	SRAM_WRITE_1(sc, RAY_SCB_CCSI, RAY_GET_INDEX(ccs));
   2114 	RAY_ECF_START_CMD(sc);
   2115 	ray_cmd_ran(sc, track);
   2116 
   2117 	return (1);
   2118 }
   2119 
   2120 /*
   2121  * send a simple command if we can
   2122  */
   2123 static int
   2124 ray_simple_cmd(sc, cmd, track)
   2125 	struct ray_softc *sc;
   2126 	u_int cmd, track;
   2127 {
   2128 	bus_size_t ccs;
   2129 
   2130 	return (ray_alloc_ccs(sc, &ccs, cmd, track) &&
   2131 	    ray_issue_cmd(sc, ccs, track));
   2132 }
   2133 
   2134 /*
   2135  * Functions based on CCS commands
   2136  */
   2137 
   2138 /*
   2139  * run a update subcommand
   2140  */
   2141 static void
   2142 ray_update_subcmd(sc)
   2143 	struct ray_softc *sc;
   2144 {
   2145 	int submask, i;
   2146 
   2147 	RAY_DPRINTF(("%s: ray_update_subcmd\n", sc->sc_xname));
   2148 
   2149 	ray_cmd_cancel(sc, SCP_UPDATESUBCMD);
   2150 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2151 		return;
   2152 	submask = SCP_UPD_FIRST;
   2153 	for (i = 0; i < ray_nsubcmdtab; submask <<= 1, i++) {
   2154 		if ((sc->sc_scheduled & SCP_UPD_MASK) == 0)
   2155 			break;
   2156 		/* when done the next command will be scheduled */
   2157 		if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD))
   2158 			break;
   2159 		if (!RAY_ECF_READY(sc))
   2160 			break;
   2161 		/*
   2162 		 * give priority to LSB -- e.g., if previous loop reschuled
   2163 		 * doing this command after calling the function won't catch
   2164 		 * if a later command sets an earlier bit
   2165 		 */
   2166 		if (sc->sc_scheduled & ((submask - 1) & SCP_UPD_MASK))
   2167 			break;
   2168 		if (sc->sc_scheduled & submask)
   2169 			(*ray_subcmdtab[i])(sc);
   2170 	}
   2171 }
   2172 
   2173 /*
   2174  * report a parameter
   2175  */
   2176 static void
   2177 ray_report_params(sc)
   2178 	struct ray_softc *sc;
   2179 {
   2180 	bus_size_t ccs;
   2181 
   2182 	ray_cmd_cancel(sc, SCP_REPORTPARAMS);
   2183 
   2184 	if (!sc->sc_repreq)
   2185 		return;
   2186 
   2187 	/* do the issue check before equality check */
   2188 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2189 		return;
   2190 	else if (ray_cmd_is_running(sc, SCP_REPORTPARAMS)) {
   2191 		ray_cmd_schedule(sc, SCP_REPORTPARAMS);
   2192 		return;
   2193 	} else if (!ray_alloc_ccs(sc, &ccs, RAY_CMD_REPORT_PARAMS,
   2194 	    SCP_REPORTPARAMS))
   2195 		return;
   2196 
   2197 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_report, c_paramid,
   2198 	    sc->sc_repreq->r_paramid);
   2199 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_report, c_nparam, 1);
   2200 	(void)ray_issue_cmd(sc, ccs, SCP_REPORTPARAMS);
   2201 }
   2202 
   2203 /*
   2204  * start an association
   2205  */
   2206 static void
   2207 ray_start_assoc(sc)
   2208 	struct ray_softc *sc;
   2209 {
   2210 	ray_cmd_cancel(sc, SCP_STARTASSOC);
   2211 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2212 		return;
   2213 	else if (ray_cmd_is_running(sc, SCP_STARTASSOC))
   2214 		return;
   2215 	(void)ray_simple_cmd(sc, RAY_CMD_START_ASSOC, SCP_STARTASSOC);
   2216 }
   2217 
   2218 /*
   2219  * Subcommand functions that use the SCP_UPDATESUBCMD command
   2220  * (and are serialized with respect to other update sub commands
   2221  */
   2222 
   2223 /*
   2224  * download the startup parameters to the card
   2225  *	-- no outstanding commands expected
   2226  */
   2227 static void
   2228 ray_download_params(sc)
   2229 	struct ray_softc *sc;
   2230 {
   2231 	struct ray_startup_params_head *sp;
   2232 	struct ray_startup_params_tail_5 *sp5;
   2233 	struct ray_startup_params_tail_4 *sp4;
   2234 	bus_size_t off;
   2235 
   2236 	RAY_DPRINTF(("%s: init_startup_params\n", sc->sc_xname));
   2237 
   2238 	ray_cmd_cancel(sc, SCP_UPD_STARTUP);
   2239 
   2240 #define	PUT2(p, v) 	\
   2241 	do { (p)[0] = ((v >> 8) & 0xff); (p)[1] = (v & 0xff); } while(0)
   2242 
   2243 	sp = &sc->sc_startup;
   2244 	sp4 = &sc->sc_startup_4;
   2245 	sp5 = &sc->sc_startup_5;
   2246 	memset(sp, 0, sizeof(*sp));
   2247 	if (sc->sc_version == SC_BUILD_4)
   2248 		memset(sp4, 0, sizeof(*sp4));
   2249 	else
   2250 		memset(sp5, 0, sizeof(*sp5));
   2251 	memcpy(sp->sp_ssid, sc->sc_dnwid, sizeof(sp->sp_ssid));
   2252 	sp->sp_scan_mode = 0x1;
   2253 	memcpy(sp->sp_mac_addr, sc->sc_ecf_startup.e_station_addr,
   2254 	    ETHER_ADDR_LEN);
   2255 	PUT2(sp->sp_frag_thresh, 0x7fff);	/* disabled */
   2256 	if (sc->sc_version == SC_BUILD_4) {
   2257 #if 1
   2258 		/* linux/fbsd */
   2259 		PUT2(sp->sp_dwell_time, 0x200);
   2260 		PUT2(sp->sp_beacon_period, 1);
   2261 #else
   2262 		/* divined */
   2263 		PUT2(sp->sp_dwell_time, 0x400);
   2264 		PUT2(sp->sp_beacon_period, 0);
   2265 #endif
   2266 	} else {
   2267 		PUT2(sp->sp_dwell_time, 128);
   2268 		PUT2(sp->sp_beacon_period, 256);
   2269 	}
   2270 	sp->sp_dtim_interval = 1;
   2271 #if 0
   2272 	/* these are the documented defaults for build 5/6 */
   2273 	sp->sp_max_retry = 0x1f;
   2274 	sp->sp_ack_timo = 0x86;
   2275 	sp->sp_sifs = 0x1c;
   2276 #elif 1
   2277 	/* these were scrounged from the linux driver */
   2278 	sp->sp_max_retry = 0x07;
   2279 
   2280 	sp->sp_ack_timo = 0xa3;
   2281 	sp->sp_sifs = 0x1d;
   2282 #else
   2283 	/* these were divined */
   2284 	sp->sp_max_retry = 0x03;
   2285 
   2286 	sp->sp_ack_timo = 0xa3;
   2287 	sp->sp_sifs = 0x1d;
   2288 #endif
   2289 #if 0
   2290 	/* these are the documented defaults for build 5/6 */
   2291 	sp->sp_difs = 0x82;
   2292 	sp->sp_pifs = 0;
   2293 #else
   2294 	/* linux/fbsd */
   2295 	sp->sp_difs = 0x82;
   2296 
   2297 	if (sc->sc_version == SC_BUILD_4)
   2298 		sp->sp_pifs = 0xce;
   2299 	else
   2300 		sp->sp_pifs = 0x4e;
   2301 #endif
   2302 
   2303 	PUT2(sp->sp_rts_thresh, 0x7fff);	/* disabled */
   2304 	if (sc->sc_version == SC_BUILD_4) {
   2305 		PUT2(sp->sp_scan_dwell, 0xfb1e);
   2306 		PUT2(sp->sp_scan_max_dwell, 0xc75c);
   2307 	} else {
   2308 		PUT2(sp->sp_scan_dwell, 0x4e2);
   2309 		PUT2(sp->sp_scan_max_dwell, 0x38a4);
   2310 	}
   2311 	sp->sp_assoc_timo = 0x5;
   2312 	if (sc->sc_version == SC_BUILD_4) {
   2313 #if 0
   2314 		/* linux/fbsd */
   2315 		sp->sp_adhoc_scan_cycle = 0x4;
   2316 		sp->sp_infra_scan_cycle = 0x2;
   2317 		sp->sp_infra_super_scan_cycle = 0x4;
   2318 #else
   2319 		/* divined */
   2320 		sp->sp_adhoc_scan_cycle = 0x8;
   2321 		sp->sp_infra_scan_cycle = 0x1;
   2322 		sp->sp_infra_super_scan_cycle = 0x18;
   2323 #endif
   2324 	} else {
   2325 		sp->sp_adhoc_scan_cycle = 0x8;
   2326 		sp->sp_infra_scan_cycle = 0x2;
   2327 		sp->sp_infra_super_scan_cycle = 0x8;
   2328 	}
   2329 	sp->sp_promisc = sc->sc_promisc;
   2330 	PUT2(sp->sp_uniq_word, 0x0cbd);
   2331 	if (sc->sc_version == SC_BUILD_4) {
   2332 	/* XXX whats this value anyway.. the std says 50us */
   2333 		/* XXX sp->sp_slot_time = 0x4e; */
   2334 		sp->sp_slot_time = 0x4e;
   2335 #if 1
   2336 		/*linux/fbsd*/
   2337 		sp->sp_roam_low_snr_thresh = 0xff;
   2338 #else
   2339 		/*divined*/
   2340 		sp->sp_roam_low_snr_thresh = 0x30;
   2341 #endif
   2342 	} else {
   2343 		sp->sp_slot_time = 0x32;
   2344 		sp->sp_roam_low_snr_thresh = 0xff;	/* disabled */
   2345 	}
   2346 #if 1
   2347 	sp->sp_low_snr_count = 0xff;		/* disabled */
   2348 #else
   2349 	/* divined -- check */
   2350 	sp->sp_low_snr_count = 0x07;		/* disabled */
   2351 #endif
   2352 #if 0
   2353 	sp->sp_infra_missed_beacon_count = 0x2;
   2354 #elif 1
   2355 	/* linux/fbsd */
   2356 	sp->sp_infra_missed_beacon_count = 0x5;
   2357 #else
   2358 	/* divined -- check, looks fishy */
   2359 	sp->sp_infra_missed_beacon_count = 0x7;
   2360 #endif
   2361 	sp->sp_adhoc_missed_beacon_count = 0xff;
   2362 	sp->sp_country_code = sc->sc_dcountrycode;
   2363 	sp->sp_hop_seq = 0x0b;
   2364 	if (sc->sc_version == SC_BUILD_4) {
   2365 		sp->sp_hop_seq_len = 0x4e;
   2366 		sp4->sp_cw_max = 0x3f;	/* single byte on build 4 */
   2367 		sp4->sp_cw_min = 0x0f;	/* single byte on build 4 */
   2368 		sp4->sp_noise_filter_gain = 0x4;
   2369 		sp4->sp_noise_limit_offset = 0x8;
   2370 		sp4->sp_rssi_thresh_offset = 0x28;
   2371 		sp4->sp_busy_thresh_offset = 0x28;
   2372 		sp4->sp_sync_thresh = 0x07;
   2373 		sp4->sp_test_mode = 0x0;
   2374 		sp4->sp_test_min_chan = 0x2;
   2375 		sp4->sp_test_max_chan = 0x2;
   2376 	} else {
   2377 		sp->sp_hop_seq_len = 0x4f;
   2378 		PUT2(sp5->sp_cw_max, 0x3f);
   2379 		PUT2(sp5->sp_cw_min, 0x0f);
   2380 		sp5->sp_noise_filter_gain = 0x4;
   2381 		sp5->sp_noise_limit_offset = 0x8;
   2382 		sp5->sp_rssi_thresh_offset = 0x28;
   2383 		sp5->sp_busy_thresh_offset = 0x28;
   2384 		sp5->sp_sync_thresh = 0x07;
   2385 		sp5->sp_test_mode = 0x0;
   2386 		sp5->sp_test_min_chan = 0x2;
   2387 		sp5->sp_test_max_chan = 0x2;
   2388 #if 0
   2389 		sp5->sp_allow_probe_resp = 0x1;
   2390 #else
   2391 		sp5->sp_allow_probe_resp = 0x0;
   2392 #endif
   2393 		sp5->sp_privacy_must_start = 0x0;
   2394 		sp5->sp_privacy_can_join = 0x0;
   2395 		sp5->sp_basic_rate_set[0] = 0x2;
   2396 		    /* 2 = 1Mbps, 3 = old 2Mbps 4 = 2Mbps */
   2397 	}
   2398 
   2399 	/* we shouldn't be called with some command pending */
   2400 	if (!RAY_ECF_READY(sc))
   2401 		panic("ray_download_params busy");
   2402 
   2403 	/* write the compatible part */
   2404 	off = RAY_HOST_TO_ECF_BASE;
   2405 	ray_write_region(sc, off, sp, sizeof(sc->sc_startup));
   2406 	off += sizeof(sc->sc_startup);
   2407 	if (sc->sc_version == SC_BUILD_4)
   2408 		ray_write_region(sc, off, sp4, sizeof(*sp4));
   2409 	else
   2410 		ray_write_region(sc, off, sp5, sizeof(*sp5));
   2411 	if (!ray_simple_cmd(sc, RAY_CMD_START_PARAMS, SCP_UPD_STARTUP))
   2412 		panic("ray_download_params issue");
   2413 }
   2414 
   2415 /*
   2416  * start or join a network
   2417  */
   2418 static void
   2419 ray_start_join_net(sc)
   2420 	struct ray_softc *sc;
   2421 {
   2422 	struct ray_net_params np;
   2423 	bus_size_t ccs;
   2424 	int cmd;
   2425 
   2426 	ray_cmd_cancel(sc, SCP_UPD_STARTJOIN);
   2427 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2428 		return;
   2429 
   2430 	/* XXX check we may not want to re-issue */
   2431 	if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2432 		ray_cmd_schedule(sc, SCP_UPD_STARTJOIN);
   2433 		return;
   2434 	}
   2435 
   2436 	if (sc->sc_mode == SC_MODE_ADHOC)
   2437 		cmd = RAY_CMD_START_NET;
   2438 	else
   2439 		cmd = RAY_CMD_JOIN_NET;
   2440 
   2441 	if (!ray_alloc_ccs(sc, &ccs, cmd, SCP_UPD_STARTJOIN))
   2442 		return;
   2443 	sc->sc_startccs = ccs;
   2444 	sc->sc_startcmd = cmd;
   2445 	if (!memcmp(sc->sc_cnwid, sc->sc_dnwid, sizeof(sc->sc_cnwid))
   2446 	    && sc->sc_omode == sc->sc_mode)
   2447 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_upd_param, 0);
   2448 	else {
   2449 		memset(&np, 0, sizeof(np));
   2450 		np.p_net_type = sc->sc_mode;
   2451 		memcpy(np.p_ssid, sc->sc_dnwid, sizeof(np.p_ssid));
   2452 		ray_write_region(sc, RAY_HOST_TO_ECF_BASE, &np, sizeof(np));
   2453 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_upd_param, 1);
   2454 	}
   2455 	if (ray_issue_cmd(sc, ccs, SCP_UPD_STARTJOIN))
   2456 		timeout(ray_start_join_timo, sc, RAY_START_TIMEOUT);
   2457 }
   2458 
   2459 static void
   2460 ray_start_join_timo(arg)
   2461 	void *arg;
   2462 {
   2463 	struct ray_softc *sc;
   2464 	u_int stat;
   2465 
   2466 	sc = arg;
   2467 	stat = SRAM_READ_FIELD_1(sc, sc->sc_startccs, ray_cmd, c_status);
   2468 	ray_start_join_net_done(sc, sc->sc_startcmd, sc->sc_startccs, stat);
   2469 }
   2470 
   2471 /*
   2472  * The start/join has completed.  Note: we timeout the start
   2473  * command because it seems to fail to work at least on the
   2474  * build 4 firmware without reporting an error.  This actually
   2475  * may be a result of not putting the correct params in the
   2476  * initial download.  If this is a timeout `stat' will be
   2477  * marked busy.
   2478  */
   2479 static ray_cmd_func_t
   2480 ray_start_join_net_done(sc, cmd, ccs, stat)
   2481 	struct ray_softc *sc;
   2482 	u_int cmd;
   2483 	bus_size_t ccs;
   2484 	u_int stat;
   2485 {
   2486 	struct ray_net_params np;
   2487 
   2488 	untimeout(ray_start_join_timo, sc);
   2489 	ray_cmd_done(sc, SCP_UPD_STARTJOIN);
   2490 
   2491 	if (stat == RAY_CCS_STATUS_FAIL) {
   2492 		/* XXX poke ifmedia when it supports this */
   2493 		sc->sc_havenet = 0;
   2494 		return (ray_start_join_net);
   2495 	}
   2496 	if (stat == RAY_CCS_STATUS_BUSY || stat == RAY_CCS_STATUS_FREE) {
   2497 		/* handle the timeout condition */
   2498 		timeout(ray_start_join_timo, sc, RAY_START_TIMEOUT);
   2499 
   2500 		/* be safe -- not a lot occurs with no net though */
   2501 		if (!RAY_ECF_READY(sc))
   2502 			return (0);
   2503 
   2504 		/* see if our nwid is up to date */
   2505 		if (!memcmp(sc->sc_cnwid, sc->sc_dnwid, sizeof(sc->sc_cnwid))
   2506 		    && sc->sc_omode == sc->sc_mode)
   2507 			SRAM_WRITE_FIELD_1(sc,ccs, ray_cmd_net, c_upd_param, 0);
   2508 		else {
   2509 			memset(&np, 0, sizeof(np));
   2510 			np.p_net_type = sc->sc_mode;
   2511 			memcpy(np.p_ssid, sc->sc_dnwid, sizeof(np.p_ssid));
   2512 			ray_write_region(sc, RAY_HOST_TO_ECF_BASE, &np,
   2513 			    sizeof(np));
   2514 			SRAM_WRITE_FIELD_1(sc,ccs, ray_cmd_net, c_upd_param, 1);
   2515 		}
   2516 
   2517 		if (sc->sc_mode == SC_MODE_ADHOC)
   2518 			cmd = RAY_CMD_START_NET;
   2519 		else
   2520 			cmd = RAY_CMD_JOIN_NET;
   2521 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_cmd,
   2522 		    RAY_CCS_STATUS_BUSY);
   2523 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_status,
   2524 		    RAY_CCS_STATUS_BUSY);
   2525 
   2526 		/* we simply poke the card again issuing the same ccs */
   2527 		SRAM_WRITE_1(sc, RAY_SCB_CCSI, RAY_GET_INDEX(ccs));
   2528 		RAY_ECF_START_CMD(sc);
   2529 		ray_cmd_ran(sc, SCP_UPD_STARTJOIN);
   2530 		return (0);
   2531 	}
   2532 	/* get the current ssid */
   2533 	SRAM_READ_FIELD_N(sc, ccs, ray_cmd_net, c_bss_id, sc->sc_bssid,
   2534 	    sizeof(sc->sc_bssid));
   2535 
   2536 	sc->sc_deftxrate = SRAM_READ_FIELD_1(sc, ccs, ray_cmd_net,c_def_txrate);
   2537 	sc->sc_encrypt = SRAM_READ_FIELD_1(sc, ccs, ray_cmd_net, c_encrypt);
   2538 
   2539 	/* adjust values for buggy build 4 */
   2540 	if (sc->sc_deftxrate == 0x55)
   2541 		sc->sc_deftxrate = RAY_PID_BASIC_RATE_1500K;
   2542 	if (sc->sc_encrypt == 0x55)
   2543 		sc->sc_encrypt = 0;
   2544 
   2545 	if (SRAM_READ_FIELD_1(sc, ccs, ray_cmd_net, c_upd_param)) {
   2546 		ray_read_region(sc, RAY_HOST_TO_ECF_BASE, &np, sizeof(np));
   2547 		memcpy(sc->sc_cnwid, np.p_ssid, sizeof(sc->sc_cnwid));
   2548 		sc->sc_omode = sc->sc_mode;
   2549 		if (np.p_net_type != sc->sc_mode)
   2550 			return (ray_start_join_net);
   2551 	}
   2552 
   2553 	/* network is now active */
   2554 	sc->sc_havenet = 1;
   2555 	ray_cmd_schedule(sc, SCP_UPD_MCAST|SCP_UPD_PROMISC);
   2556 	if (cmd == RAY_CMD_JOIN_NET)
   2557 		return (ray_start_assoc);
   2558 	else
   2559 		return (ray_intr_start);
   2560 }
   2561 
   2562 /*
   2563  * set the card in/out of promiscuous mode
   2564  */
   2565 static void
   2566 ray_update_promisc(sc)
   2567 	struct ray_softc *sc;
   2568 {
   2569 	bus_size_t ccs;
   2570 	int promisc;
   2571 
   2572 	ray_cmd_cancel(sc, SCP_UPD_PROMISC);
   2573 
   2574 	/* do the issue check before equality check */
   2575 	promisc = !!(sc->sc_if.if_flags & (IFF_PROMISC | IFF_ALLMULTI));
   2576 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2577 		return;
   2578 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2579 		ray_cmd_schedule(sc, SCP_UPD_PROMISC);
   2580 		return;
   2581 	} else if (promisc == sc->sc_promisc)
   2582 		return;
   2583 	else if (!ray_alloc_ccs(sc,&ccs,RAY_CMD_UPDATE_PARAMS, SCP_UPD_PROMISC))
   2584 		return;
   2585 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_paramid, RAY_PID_PROMISC);
   2586 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_nparam, 1);
   2587 	SRAM_WRITE_1(sc, RAY_HOST_TO_ECF_BASE, promisc);
   2588 	(void)ray_issue_cmd(sc, ccs, SCP_UPD_PROMISC);
   2589 }
   2590 
   2591 /*
   2592  * update the parameter based on what the user passed in
   2593  */
   2594 static void
   2595 ray_update_params(sc)
   2596 	struct ray_softc *sc;
   2597 {
   2598 	bus_size_t ccs;
   2599 
   2600 	ray_cmd_cancel(sc, SCP_UPD_UPDATEPARAMS);
   2601 	if (!sc->sc_updreq) {
   2602 		/* XXX do we need to wakeup here? */
   2603 		return;
   2604 	}
   2605 
   2606 	/* do the issue check before equality check */
   2607 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2608 		return;
   2609 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2610 		ray_cmd_schedule(sc, SCP_UPD_UPDATEPARAMS);
   2611 		return;
   2612 	} else if (!ray_alloc_ccs(sc, &ccs, RAY_CMD_UPDATE_PARAMS,
   2613 	    SCP_UPD_UPDATEPARAMS))
   2614 		return;
   2615 
   2616 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_paramid,
   2617 	    sc->sc_updreq->r_paramid);
   2618 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_nparam, 1);
   2619 	ray_write_region(sc, RAY_HOST_TO_ECF_BASE, sc->sc_updreq->r_data,
   2620 	    sc->sc_updreq->r_len);
   2621 
   2622 	(void)ray_issue_cmd(sc, ccs, SCP_UPD_UPDATEPARAMS);
   2623 }
   2624 
   2625 /*
   2626  * set the multicast filter list
   2627  */
   2628 static void
   2629 ray_update_mcast(sc)
   2630 	struct ray_softc *sc;
   2631 {
   2632 	bus_size_t ccs;
   2633 	struct ether_multistep step;
   2634 	struct ether_multi *enm;
   2635 	struct ethercom *ec;
   2636 	bus_size_t bufp;
   2637 	int count;
   2638 
   2639 	ec = &sc->sc_ec;
   2640 	ray_cmd_cancel(sc, SCP_UPD_MCAST);
   2641 
   2642 	/* see if we have any ranges */
   2643 	if ((count = sc->sc_ec.ec_multicnt) < 17) {
   2644 		ETHER_FIRST_MULTI(step, ec, enm);
   2645 		while (enm) {
   2646 			/* see if this is a range */
   2647 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2648 				ETHER_ADDR_LEN)) {
   2649 				count = 17;
   2650 				break;
   2651 			}
   2652 			ETHER_NEXT_MULTI(step, enm);
   2653 		}
   2654 	}
   2655 
   2656 	/* track this stuff even when not running */
   2657 	if (count > 16) {
   2658 		sc->sc_if.if_flags |= IFF_ALLMULTI;
   2659 		ray_update_promisc(sc);
   2660 		return;
   2661 	} else if (sc->sc_if.if_flags & IFF_ALLMULTI) {
   2662 		sc->sc_if.if_flags &= ~IFF_ALLMULTI;
   2663 		ray_update_promisc(sc);
   2664 	}
   2665 
   2666 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2667 		return;
   2668 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2669 		ray_cmd_schedule(sc, SCP_UPD_MCAST);
   2670 		return;
   2671 	} else if (!ray_alloc_ccs(sc,&ccs, RAY_CMD_UPDATE_MCAST, SCP_UPD_MCAST))
   2672 		return;
   2673 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update_mcast, c_nmcast, count);
   2674 	bufp = RAY_HOST_TO_ECF_BASE;
   2675 	ETHER_FIRST_MULTI(step, ec, enm);
   2676 	while (enm) {
   2677 		ray_write_region(sc, bufp, enm->enm_addrlo, ETHER_ADDR_LEN);
   2678 		bufp += ETHER_ADDR_LEN;
   2679 		ETHER_NEXT_MULTI(step, enm);
   2680 	}
   2681 	(void)ray_issue_cmd(sc, ccs, SCP_UPD_MCAST);
   2682 }
   2683 
   2684 /*
   2685  * User issued commands
   2686  */
   2687 
   2688 /*
   2689  * issue a update params
   2690  *
   2691  * expected to be called in sleapable context -- intended for user stuff
   2692  */
   2693 static int
   2694 ray_user_update_params(sc, pr)
   2695 	struct ray_softc *sc;
   2696 	struct ray_param_req *pr;
   2697 {
   2698 	int rv;
   2699 
   2700 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   2701 		pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   2702 		return (EIO);
   2703 	}
   2704 
   2705 	/* wait to be able to issue the command */
   2706 	rv = 0;
   2707 	while (ray_cmd_is_running(sc, SCP_UPD_UPDATEPARAMS) ||
   2708 	    ray_cmd_is_scheduled(sc, SCP_UPD_UPDATEPARAMS)) {
   2709 		rv = tsleep(ray_update_params, 0|PCATCH, "cmd in use", 0);
   2710 		if (rv)
   2711 			return (rv);
   2712 		if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   2713 			pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   2714 			return (EIO);
   2715 		}
   2716 	}
   2717 
   2718 	pr->r_failcause = RAY_FAILCAUSE_WAITING;
   2719 	sc->sc_updreq = pr;
   2720 	ray_cmd_schedule(sc, SCP_UPD_UPDATEPARAMS);
   2721 	ray_check_scheduled(sc);
   2722 
   2723 	while (pr->r_failcause == RAY_FAILCAUSE_WAITING)
   2724 		(void)tsleep(ray_update_params, 0, "waiting cmd", 0);
   2725 	wakeup(ray_update_params);
   2726 
   2727 	return (0);
   2728 }
   2729 
   2730 /*
   2731  * issue a report params
   2732  *
   2733  * expected to be called in sleapable context -- intended for user stuff
   2734  */
   2735 static int
   2736 ray_user_report_params(sc, pr)
   2737 	struct ray_softc *sc;
   2738 	struct ray_param_req *pr;
   2739 {
   2740 	int rv;
   2741 
   2742 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   2743 		pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   2744 		return (EIO);
   2745 	}
   2746 
   2747 	/* wait to be able to issue the command */
   2748 	rv = 0;
   2749 	while (ray_cmd_is_running(sc, SCP_REPORTPARAMS)
   2750 	    || ray_cmd_is_scheduled(sc, SCP_REPORTPARAMS)) {
   2751 		rv = tsleep(ray_report_params, 0|PCATCH, "cmd in use", 0);
   2752 		if (rv)
   2753 			return (rv);
   2754 		if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   2755 			pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   2756 			return (EIO);
   2757 		}
   2758 	}
   2759 
   2760 	pr->r_failcause = RAY_FAILCAUSE_WAITING;
   2761 	sc->sc_repreq = pr;
   2762 	ray_cmd_schedule(sc, SCP_REPORTPARAMS);
   2763 	ray_check_scheduled(sc);
   2764 
   2765 	while (pr->r_failcause == RAY_FAILCAUSE_WAITING)
   2766 		(void)tsleep(ray_report_params, 0, "waiting cmd", 0);
   2767 	wakeup(ray_report_params);
   2768 
   2769 	return (0);
   2770 }
   2771 
   2772 
   2773 /*
   2774  * this is a temporary wrapper around bus_space_read_region_1
   2775  * as it seems to mess with gcc.  the line numbers get offset
   2776  * presumably this is related to the inline asm on i386.
   2777  */
   2778 
   2779 static void
   2780 ray_read_region(sc, off, vp, c)
   2781 	struct ray_softc *sc;
   2782 	bus_size_t off;
   2783 	void *vp;
   2784 	size_t c;
   2785 {
   2786 	u_int n2, n4, tmp;
   2787 	u_int8_t *p;
   2788 
   2789 	p = vp;
   2790 
   2791 	/* XXX we may be making poor assumptions here but lets hope */
   2792 	switch ((off|(bus_addr_t)p) & 0x03) {
   2793 	case 0:
   2794 		if ((n4 = c / 4)) {
   2795 			bus_space_read_region_4(sc->sc_memt, sc->sc_memh, off,
   2796 			    p, n4);
   2797 			tmp = c & ~0x3;
   2798 			c &= 0x3;
   2799 			p += tmp;
   2800 			off += tmp;
   2801 		}
   2802 		switch (c) {
   2803 		case 3:
   2804 			*p = bus_space_read_1(sc->sc_memt,sc->sc_memh, off);
   2805 			p++, off++;
   2806 		case 2:
   2807 			*p = bus_space_read_1(sc->sc_memt,sc->sc_memh, off);
   2808 			p++, off++;
   2809 		case 1:
   2810 			*p = bus_space_read_1(sc->sc_memt,sc->sc_memh, off);
   2811 		}
   2812 		break;
   2813 	case 2:
   2814 		if ((n2 = (c >> 1)))
   2815 			bus_space_read_region_2(sc->sc_memt, sc->sc_memh, off,
   2816 			    p, n2);
   2817 		if (c & 1) {
   2818 			c &= ~0x1;
   2819 			*(p + c) = bus_space_read_1(sc->sc_memt, sc->sc_memh,
   2820 			    off + c);
   2821 		}
   2822 		break;
   2823 	case 1:
   2824 	case 3:
   2825 		bus_space_read_region_1(sc->sc_memt, sc->sc_memh, off, p, c);
   2826 		break;
   2827 	}
   2828 }
   2829 
   2830 /*
   2831  * this is a temporary wrapper around bus_space_write_region_1
   2832  * as it seems to mess with gcc.  the line numbers get offset
   2833  * presumably this is related to the inline asm on i386.
   2834  */
   2835 static void
   2836 ray_write_region(sc, off, vp, c)
   2837 	struct ray_softc *sc;
   2838 	bus_size_t off;
   2839 	void *vp;
   2840 	size_t c;
   2841 {
   2842 	size_t n2, n4, tmp;
   2843 	u_int8_t *p;
   2844 
   2845 	p = vp;
   2846 	/* XXX we may be making poor assumptions here but lets hope */
   2847 	switch ((off|(bus_addr_t)p) & 0x03) {
   2848 	case 0:
   2849 		if ((n4 = (c >> 2))) {
   2850 			bus_space_write_region_4(sc->sc_memt, sc->sc_memh, off,
   2851 			    p, n4);
   2852 			tmp = c & ~0x3;
   2853 			c &= 0x3;
   2854 			p += tmp;
   2855 			off += tmp;
   2856 		}
   2857 		switch (c) {
   2858 		case 3:
   2859 			bus_space_write_1(sc->sc_memt,sc->sc_memh, off, *p);
   2860 			p++, off++;
   2861 		case 2:
   2862 			bus_space_write_1(sc->sc_memt,sc->sc_memh, off, *p);
   2863 			p++, off++;
   2864 		case 1:
   2865 			bus_space_write_1(sc->sc_memt,sc->sc_memh, off, *p);
   2866 		}
   2867 		break;
   2868 	case 2:
   2869 		if ((n2 = (c >> 1)))
   2870 			bus_space_write_region_2(sc->sc_memt, sc->sc_memh, off,
   2871 			    p, n2);
   2872 		if (c & 0x1) {
   2873 			c &= ~0x1;
   2874 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
   2875 			    off + c, *(p + c));
   2876 		}
   2877 		break;
   2878 	case 1:
   2879 	case 3:
   2880 		bus_space_write_region_1(sc->sc_memt, sc->sc_memh, off, p, c);
   2881 		break;
   2882 	}
   2883 }
   2884 
   2885 #ifdef RAY_DEBUG
   2886 
   2887 #define PRINTABLE(c) ((c) >= 0x20 && (c) <= 0x7f)
   2888 
   2889 void
   2890 hexdump(const u_int8_t *d, int len, int br, int div, int fl)
   2891 {
   2892 	int i, j, offw, first, tlen, ni, nj, sp;
   2893 
   2894 	sp = br / div;
   2895 	offw = 0;
   2896 	if (len && (fl & HEXDF_NOOFFSET) == 0) {
   2897 		tlen = len;
   2898 		do {
   2899 			offw++;
   2900 		} while (tlen /= br);
   2901 	}
   2902 	if (offw)
   2903 		printf("%0*x: ", offw, 0);
   2904 	for (i = 0; i < len; i++, d++) {
   2905 		if (i && (i % br) == 0) {
   2906 			if ((fl & HEXDF_NOASCII) == 0) {
   2907 				printf("   ");
   2908 				d -= br;
   2909 				for (j = 0; j < br; d++, j++) {
   2910 					if (j && (j % sp) == 0)
   2911 						printf(" ");
   2912 					if (PRINTABLE(*d))
   2913 						printf("%c", (int)*d);
   2914 					else
   2915 						printf(".");
   2916 				}
   2917 			}
   2918 			if (offw)
   2919 				printf("\n%0*x: ", offw, i);
   2920 			else
   2921 				printf("\n");
   2922 			if ((fl & HEXDF_NOCOMPRESS) == 0) {
   2923 				first = 1;
   2924 				while (len - i >= br) {
   2925 					if (memcmp(d, d - br, br))
   2926 						break;
   2927 					d += br;
   2928 					i += br;
   2929 					if (first) {
   2930 						printf("*");
   2931 						first = 0;
   2932 					}
   2933 				}
   2934 				if (len == i) {
   2935 					printf("\n%0*x", offw, i);
   2936 					return;
   2937 				}
   2938 			}
   2939 		} else if (i && (i % sp) == 0)
   2940 			printf(" ");
   2941 		printf("%02x ", *d);
   2942 	}
   2943 	if (len && (((i - 1) % br) || i == 1)) {
   2944 		if ((fl & HEXDF_NOASCII) == 0) {
   2945 			i = i % br ? i % br : br;
   2946 			ni = (br - i) % br;
   2947 			j = (i - 1) / sp;
   2948 			nj = (div - j - 1) % div;
   2949 			j = 3 * ni + nj + 3;
   2950 			printf("%*s", j, "");
   2951 			d -= i;
   2952 			for (j = 0; j < i; d++, j++) {
   2953 				if (j && (j % sp) == 0)
   2954 					printf(" ");
   2955 				if (PRINTABLE(*d))
   2956 					printf("%c", (int)*d);
   2957 				else
   2958 					printf(".");
   2959 			}
   2960 		}
   2961 		printf("\n");
   2962 	}
   2963 }
   2964 
   2965 
   2966 
   2967 static void
   2968 ray_dump_mbuf(sc, m)
   2969 	struct ray_softc *sc;
   2970 	struct mbuf *m;
   2971 {
   2972 	u_int8_t *d, *ed;
   2973 	u_int i;
   2974 
   2975 	printf("%s: pkt dump:", sc->sc_xname);
   2976 	i = 0;
   2977 	for (; m; m = m->m_next) {
   2978 		d = mtod(m, u_int8_t *);
   2979 		ed = d + m->m_len;
   2980 
   2981 		for (; d < ed; i++, d++) {
   2982 			if ((i % 16) == 0)
   2983 				printf("\n\t");
   2984 			else if ((i % 8) == 0)
   2985 				printf("  ");
   2986 			printf(" %02x", *d);
   2987 		}
   2988 	}
   2989 	if ((i - 1) % 16)
   2990 		printf("\n");
   2991 }
   2992 #endif	/* RAY_DEBUG */
   2993