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