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