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