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