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