Home | History | Annotate | Line # | Download | only in pcmcia
if_ray.c revision 1.24
      1 /*	$NetBSD: if_ray.c,v 1.24 2000/12/11 17:53:19 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 	} else if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA) {
   1581 		RAY_DPRINTF(("%s: pkt not type data fc0 0x%x\n",
   1582 		    sc->sc_xname, fc0));
   1583 		m_freem(m);
   1584 		return;
   1585 	}
   1586 
   1587 	if (pktlen < sizeof(*frame) + sizeof(struct llc)) {
   1588 		RAY_DPRINTF(("%s: pkt too small for llc (%d)\n",
   1589 		    sc->sc_xname, pktlen));
   1590 		m_freem(m);
   1591 		return;
   1592 	}
   1593 
   1594 	if (!memcmp(frame + 1, llc_snapid, sizeof(llc_snapid)))
   1595 		issnap = 1;
   1596 	else {
   1597 		/*
   1598 		 * if user has link0 flag set we allow the weird
   1599 		 * Ethernet2 in 802.11 encapsulation produced by
   1600 		 * the windows driver for the WebGear card
   1601 		 */
   1602 		RAY_DPRINTF(("%s: pkt not snap 0\n", sc->sc_xname));
   1603 		if ((ifp->if_flags & IFF_LINK0) == 0) {
   1604 			m_freem(m);
   1605 			return;
   1606 		}
   1607 		issnap = 0;
   1608 	}
   1609 	switch (frame->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
   1610 	case IEEE80211_FC1_DIR_NODS:
   1611 		src = frame->i_addr2;
   1612 		break;
   1613 	case IEEE80211_FC1_DIR_FROMDS:
   1614 		src = frame->i_addr3;
   1615 		break;
   1616 	case IEEE80211_FC1_DIR_TODS:
   1617 		RAY_DPRINTF(("%s: pkt ap2ap\n", sc->sc_xname));
   1618 		m_freem(m);
   1619 		return;
   1620 	default:
   1621 		RAY_DPRINTF(("%s: pkt type unknown\n", sc->sc_xname));
   1622 		m_freem(m);
   1623 		return;
   1624 	}
   1625 
   1626 #ifdef RAY_DO_SIGLEV
   1627 	ray_update_siglev(sc, src, siglev);
   1628 #endif
   1629 
   1630 	/*
   1631 	 * This is a mess.. we should support other LLC frame types
   1632 	 */
   1633 	if (issnap) {
   1634 		/* create an ether_header over top of the 802.11+SNAP header */
   1635 		eh = (struct ether_header *)((caddr_t)(frame + 1) - 6);
   1636 		memcpy(eh->ether_shost, src, ETHER_ADDR_LEN);
   1637 		memcpy(eh->ether_dhost, frame->i_addr1, ETHER_ADDR_LEN);
   1638 	} else {
   1639 		/* this is the weird e2 in 802.11 encapsulation */
   1640 		eh = (struct ether_header *)(frame + 1);
   1641 	}
   1642 	m_adj(m, (caddr_t)eh - (caddr_t)frame);
   1643 #if NBPFILTER > 0
   1644 	if (ifp->if_bpf)
   1645 		bpf_mtap(ifp->if_bpf, m);
   1646 #endif
   1647 	/* XXX doesn't appear to be included m->m_flags |= M_HASFCS; */
   1648 	ifp->if_ipackets++;
   1649 	(*ifp->if_input)(ifp, m);
   1650 }
   1651 
   1652 /*
   1653  * receive an auth packet
   1654  */
   1655 static void
   1656 ray_recv_auth(sc, frame)
   1657 	struct ray_softc *sc;
   1658 	struct ieee80211_frame *frame;
   1659 {
   1660 	u_int8_t *var = (u_int8_t *)(frame + 1);
   1661 
   1662 	if (sc->sc_mode == SC_MODE_ADHOC) {
   1663 		RAY_DPRINTF(("%s: recv auth packet:\n", sc->sc_dev.dv_xname));
   1664 #ifdef RAY_DEBUG
   1665 		hexdump((const u_int8_t *)frame, sizeof(*frame) + 6, 16, 4, 0);
   1666 #endif
   1667 		RAY_DPRINTF(("\n"));
   1668 
   1669 		if (var[2] == OPEN_AUTH_REQUEST) {
   1670 			RAY_DPRINTF(("%s: Sending authentication response.\n",
   1671 			    sc->sc_dev.dv_xname));
   1672 			if (ray_send_auth(sc, frame->i_addr2,
   1673 			    OPEN_AUTH_RESPONSE) == 0) {
   1674 				sc->sc_authstate = RAY_AUTH_NEEDED;
   1675 				memcpy(sc->sc_authid, frame->i_addr2,
   1676 				    ETHER_ADDR_LEN);
   1677 			}
   1678 		} else if (var[2] == OPEN_AUTH_RESPONSE) {
   1679 			RAY_DPRINTF(("%s: Authenticated!\n",
   1680 			    sc->sc_dev.dv_xname));
   1681 			sc->sc_authstate = RAY_AUTH_AUTH;
   1682 		}
   1683 	}
   1684 }
   1685 
   1686 /*
   1687  * send an auth packet
   1688  */
   1689 static int
   1690 ray_send_auth(sc, dest, auth_type)
   1691 	struct ray_softc *sc;
   1692 	u_int8_t *dest;
   1693 	u_int8_t auth_type;
   1694 {
   1695 	u_int8_t packet[sizeof(struct ieee80211_frame) + ETHER_ADDR_LEN], *var;
   1696 	struct ieee80211_frame *frame;
   1697 	bus_size_t bufp;
   1698 	int ccsindex;
   1699 
   1700 	ccsindex = ray_find_free_tx_ccs(sc, RAY_CCS_TX_FIRST);
   1701 	if (ccsindex == RAY_CCS_LINK_NULL) {
   1702 		RAY_DPRINTF(("%s: send auth failed -- no free tx slots\n",
   1703 		    sc->sc_dev.dv_xname));
   1704 		return (ENOMEM);
   1705 	}
   1706 
   1707 	bufp = ray_fill_in_tx_ccs(sc, sizeof(packet), ccsindex,
   1708 	    RAY_CCS_LINK_NULL);
   1709 	frame = (struct ieee80211_frame *) packet;
   1710 	frame->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_SUBTYPE_AUTH;
   1711 	frame->i_fc[1] = 0;
   1712 	memcpy(frame->i_addr1, dest, ETHER_ADDR_LEN);
   1713 	memcpy(frame->i_addr2, sc->sc_ecf_startup.e_station_addr,
   1714 	    ETHER_ADDR_LEN);
   1715 	memcpy(frame->i_addr3, sc->sc_bssid, ETHER_ADDR_LEN);
   1716 
   1717 	var = (u_int8_t *)(frame + 1);
   1718 	memset(var, 0, ETHER_ADDR_LEN);
   1719 	var[2] = auth_type;
   1720 
   1721 	ray_write_region(sc, bufp, packet, sizeof(packet));
   1722 
   1723 	SRAM_WRITE_1(sc, RAY_SCB_CCSI, ccsindex);
   1724 	RAY_ECF_START_CMD(sc);
   1725 
   1726 	RAY_DPRINTF_XMIT(("%s: sent auth packet: len %lu\n",
   1727 	    sc->sc_dev.dv_xname, (u_long) sizeof(packet)));
   1728 
   1729 	return (0);
   1730 }
   1731 
   1732 /*
   1733  * scan for free buffers
   1734  *
   1735  * Note: do _not_ try to optimize this away, there is some kind of
   1736  * horrible interaction with receiving tx interrupts and they
   1737  * have to be done as fast as possible, which means zero processing.
   1738  * this took ~ever to figure out, don't make someone do it again!
   1739  */
   1740 static u_int
   1741 ray_find_free_tx_ccs(sc, hint)
   1742 	struct ray_softc *sc;
   1743 	u_int hint;
   1744 {
   1745 	u_int i, stat;
   1746 
   1747 	for (i = hint; i <= RAY_CCS_TX_LAST; i++) {
   1748 		stat = SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status);
   1749 		if (stat == RAY_CCS_STATUS_FREE)
   1750 			return (i);
   1751 	}
   1752 
   1753 	if (hint == RAY_CCS_TX_FIRST)
   1754 		return (RAY_CCS_LINK_NULL);
   1755 
   1756 	for (i = RAY_CCS_TX_FIRST; i < hint; i++) {
   1757 		stat = SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status);
   1758 		if (stat == RAY_CCS_STATUS_FREE)
   1759 			return (i);
   1760 	}
   1761 	return (RAY_CCS_LINK_NULL);
   1762 }
   1763 
   1764 /*
   1765  * allocate, initialize and link in a tx ccs for the given
   1766  * page and the current chain values
   1767  */
   1768 static bus_size_t
   1769 ray_fill_in_tx_ccs(sc, pktlen, i, pi)
   1770 	struct ray_softc *sc;
   1771 	size_t pktlen;
   1772 	u_int i, pi;
   1773 {
   1774 	bus_size_t ccs, bufp;
   1775 
   1776 	/* pktlen += RAY_TX_PHY_SIZE; */
   1777 	bufp = RAY_TX_BASE + i * RAY_TX_BUF_SIZE;
   1778 	bufp += sc->sc_txpad;
   1779 	ccs = RAY_GET_CCS(i);
   1780 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_status, RAY_CCS_STATUS_BUSY);
   1781 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_cmd, RAY_CMD_TX_REQ);
   1782 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_link, RAY_CCS_LINK_NULL);
   1783 	SRAM_WRITE_FIELD_2(sc, ccs, ray_cmd_tx, c_bufp, bufp);
   1784 	SRAM_WRITE_FIELD_2(sc, ccs, ray_cmd_tx, c_len, pktlen);
   1785 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_tx_rate, sc->sc_deftxrate);
   1786 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_apm_mode, 0);
   1787 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_antenna, 0);
   1788 
   1789 	/* link us in */
   1790 	if (pi != RAY_CCS_LINK_NULL)
   1791 		SRAM_WRITE_FIELD_1(sc, RAY_GET_CCS(pi), ray_cmd_tx, c_link, i);
   1792 
   1793 	RAY_DPRINTF(("%s: ray_alloc_tx_ccs bufp 0x%lx idx %d pidx %d \n",
   1794 	    sc->sc_xname, bufp, i, pi));
   1795 
   1796 	return (bufp + RAY_TX_PHY_SIZE);
   1797 }
   1798 
   1799 /*
   1800  * an update params command has completed lookup which command and
   1801  * the status
   1802  */
   1803 static ray_cmd_func_t
   1804 ray_update_params_done(sc, ccs, stat)
   1805 	struct ray_softc *sc;
   1806 	bus_size_t ccs;
   1807 	u_int stat;
   1808 {
   1809 	ray_cmd_func_t rcmd;
   1810 
   1811 	rcmd = 0;
   1812 
   1813 	RAY_DPRINTF(("%s: ray_update_params_done stat %d\n",
   1814 	   sc->sc_xname, stat));
   1815 
   1816 	/* this will get more complex as we add commands */
   1817 	if (stat == RAY_CCS_STATUS_FAIL) {
   1818 		printf("%s: failed to update a promisc\n", sc->sc_xname);
   1819 		/* XXX should probably reset */
   1820 		/* rcmd = ray_reset; */
   1821 	}
   1822 
   1823 	if (sc->sc_running & SCP_UPD_PROMISC) {
   1824 		ray_cmd_done(sc, SCP_UPD_PROMISC);
   1825 		sc->sc_promisc = SRAM_READ_1(sc, RAY_HOST_TO_ECF_BASE);
   1826 		RAY_DPRINTF(("%s: new promisc value %d\n", sc->sc_xname,
   1827 		    sc->sc_promisc));
   1828 	} else if (sc->sc_updreq) {
   1829 		ray_cmd_done(sc, SCP_UPD_UPDATEPARAMS);
   1830 		/* get the update parameter */
   1831 		sc->sc_updreq->r_failcause =
   1832 		    SRAM_READ_FIELD_1(sc, ccs, ray_cmd_update, c_failcause);
   1833 		sc->sc_updreq = 0;
   1834 		wakeup(ray_update_params);
   1835 
   1836 		rcmd = ray_start_join_net;
   1837 	}
   1838 	return (rcmd);
   1839 }
   1840 
   1841 /*
   1842  *  check too see if we have any pending commands.
   1843  */
   1844 static void
   1845 ray_check_scheduled(arg)
   1846 	void *arg;
   1847 {
   1848 	struct ray_softc *sc;
   1849 	int s, i, mask;
   1850 
   1851 	s = splnet();
   1852 
   1853 	sc = arg;
   1854 	RAY_DPRINTF((
   1855 	    "%s: ray_check_scheduled enter schd 0x%x running 0x%x ready %d\n",
   1856 	    sc->sc_xname, sc->sc_scheduled, sc->sc_running, RAY_ECF_READY(sc)));
   1857 
   1858 	if (sc->sc_timoneed) {
   1859 		callout_stop(&sc->sc_check_scheduled_ch);
   1860 		sc->sc_timoneed = 0;
   1861 	}
   1862 
   1863 	/* if update subcmd is running -- clear it in scheduled */
   1864 	if (sc->sc_running & SCP_UPDATESUBCMD)
   1865 		sc->sc_scheduled &= ~SCP_UPDATESUBCMD;
   1866 
   1867 	mask = SCP_FIRST;
   1868 	for (i = 0; i < ray_ncmdtab; mask <<= 1, i++) {
   1869 		if ((sc->sc_scheduled & ~SCP_UPD_MASK) == 0)
   1870 			break;
   1871 		if (!RAY_ECF_READY(sc))
   1872 			break;
   1873 		if (sc->sc_scheduled & mask)
   1874 			(*ray_cmdtab[i])(sc);
   1875 	}
   1876 
   1877 	RAY_DPRINTF((
   1878 	    "%s: ray_check_scheduled exit sched 0x%x running 0x%x ready %d\n",
   1879 	    sc->sc_xname, sc->sc_scheduled, sc->sc_running, RAY_ECF_READY(sc)));
   1880 
   1881 	if (sc->sc_scheduled & ~SCP_UPD_MASK)
   1882 		ray_set_pending(sc, sc->sc_scheduled);
   1883 
   1884 	splx(s);
   1885 }
   1886 
   1887 /*
   1888  * check for unreported returns
   1889  *
   1890  * this routine is coded to only expect one outstanding request for the
   1891  * timed out requests at a time, but thats all that can be outstanding
   1892  * per hardware limitations
   1893  */
   1894 static void
   1895 ray_check_ccs(arg)
   1896 	void *arg;
   1897 {
   1898 	ray_cmd_func_t fp;
   1899 	struct ray_softc *sc;
   1900 	u_int i, cmd, stat;
   1901 	bus_size_t ccs;
   1902 	int s;
   1903 
   1904 	s = splnet();
   1905 	sc = arg;
   1906 
   1907 	RAY_DPRINTF(("%s: ray_check_ccs\n", sc->sc_xname));
   1908 
   1909 	sc->sc_timocheck = 0;
   1910 	for (i = RAY_CCS_CMD_FIRST; i <= RAY_CCS_CMD_LAST; i++) {
   1911 		if (!sc->sc_ccsinuse[i])
   1912 			continue;
   1913 		ccs = RAY_GET_CCS(i);
   1914 		cmd = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd);
   1915 		switch (cmd) {
   1916 		case RAY_CMD_START_PARAMS:
   1917 		case RAY_CMD_UPDATE_MCAST:
   1918 		case RAY_CMD_UPDATE_PARAMS:
   1919 			stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   1920 			RAY_DPRINTF(("%s: check ccs idx %d ccs 0x%lx "
   1921 			    "cmd 0x%x stat %d\n", sc->sc_xname, i,
   1922 			    ccs, cmd, stat));
   1923 			goto breakout;
   1924 		}
   1925 	}
   1926 breakout:
   1927 	/* see if we got one of the commands we are looking for */
   1928 	if (i > RAY_CCS_CMD_LAST)
   1929 		; /* nothign */
   1930 	else if (stat == RAY_CCS_STATUS_FREE) {
   1931 		stat = RAY_CCS_STATUS_COMPLETE;
   1932 		if ((fp = ray_ccs_done(sc, ccs)))
   1933 			(*fp)(sc);
   1934 	} else if (stat != RAY_CCS_STATUS_BUSY) {
   1935 		if (sc->sc_ccsinuse[i] == 1) {
   1936 			/* give a chance for the interrupt to occur */
   1937 			sc->sc_ccsinuse[i] = 2;
   1938 			if (!sc->sc_timocheck) {
   1939 				callout_reset(&sc->sc_check_ccs_ch, 1,
   1940 				    ray_check_ccs, sc);
   1941 				sc->sc_timocheck = 1;
   1942 			}
   1943 		} else if ((fp = ray_ccs_done(sc, ccs)))
   1944 			(*fp)(sc);
   1945 	} else {
   1946 		callout_reset(&sc->sc_check_ccs_ch, RAY_CHECK_CCS_TIMEOUT,
   1947 		    ray_check_ccs, sc);
   1948 		sc->sc_timocheck = 1;
   1949 	}
   1950 	splx(s);
   1951 }
   1952 
   1953 /*
   1954  * read the counters, the card implements the following protocol
   1955  * to keep the values from being changed while read:  It checks
   1956  * the `own' bit and if zero writes the current internal counter
   1957  * value, it then sets the `own' bit to 1.  If the `own' bit was 1 it
   1958  * incremenets its internal counter.  The user thus reads the counter
   1959  * if the `own' bit is one and then sets the own bit to 0.
   1960  */
   1961 static void
   1962 ray_update_error_counters(sc)
   1963 	struct ray_softc *sc;
   1964 {
   1965 	bus_size_t csc;
   1966 
   1967 	/* try and update the error counters */
   1968 	csc = RAY_STATUS_BASE;
   1969 	if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_mrxo_own)) {
   1970 		sc->sc_rxoverflow +=
   1971 		    SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_mrx_overflow);
   1972 		SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_mrxo_own, 0);
   1973 	}
   1974 	if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_mrxc_own)) {
   1975 		sc->sc_rxcksum +=
   1976 		    SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_mrx_overflow);
   1977 		SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_mrxc_own, 0);
   1978 	}
   1979 	if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_rxhc_own)) {
   1980 		sc->sc_rxhcksum +=
   1981 		    SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_rx_hcksum);
   1982 		SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_rxhc_own, 0);
   1983 	}
   1984 	sc->sc_rxnoise = SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_rx_noise);
   1985 }
   1986 
   1987 /*
   1988  * one of the commands we issued has completed, process.
   1989  */
   1990 static ray_cmd_func_t
   1991 ray_ccs_done(sc, ccs)
   1992 	struct ray_softc *sc;
   1993 	bus_size_t ccs;
   1994 {
   1995 	struct ifnet *ifp;
   1996 	ray_cmd_func_t rcmd;
   1997 	u_int cmd, stat;
   1998 
   1999 	ifp = &sc->sc_if;
   2000 	cmd = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd);
   2001 	stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   2002 
   2003 	RAY_DPRINTF(("%s: ray_ccs_done idx %ld cmd 0x%x stat %d\n",
   2004 	    sc->sc_xname, RAY_GET_INDEX(ccs), cmd, stat));
   2005 
   2006 	rcmd = 0;
   2007 	switch (cmd) {
   2008 	/*
   2009 	 * solicited commands
   2010 	 */
   2011 	case RAY_CMD_START_PARAMS:
   2012 		/* start network */
   2013 		ray_cmd_done(sc, SCP_UPD_STARTUP);
   2014 
   2015 		/* ok to start queueing packets */
   2016 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
   2017 
   2018 		sc->sc_omode = sc->sc_mode;
   2019 		memcpy(&sc->sc_cnwid, &sc->sc_dnwid, sizeof(sc->sc_cnwid));
   2020 
   2021 		rcmd = ray_start_join_net;
   2022 		break;
   2023 	case RAY_CMD_UPDATE_PARAMS:
   2024 		rcmd = ray_update_params_done(sc, ccs, stat);
   2025 		break;
   2026 	case RAY_CMD_REPORT_PARAMS:
   2027 		/* get the reported parameters */
   2028 		ray_cmd_done(sc, SCP_REPORTPARAMS);
   2029 		if (!sc->sc_repreq)
   2030 			break;
   2031 		sc->sc_repreq->r_failcause =
   2032 		    SRAM_READ_FIELD_1(sc, ccs, ray_cmd_report, c_failcause);
   2033 		sc->sc_repreq->r_len =
   2034 		    SRAM_READ_FIELD_1(sc, ccs, ray_cmd_report, c_len);
   2035 		ray_read_region(sc, RAY_ECF_TO_HOST_BASE, sc->sc_repreq->r_data,
   2036 		    sc->sc_repreq->r_len);
   2037 		sc->sc_repreq = 0;
   2038 		wakeup(ray_report_params);
   2039 		break;
   2040 	case RAY_CMD_UPDATE_MCAST:
   2041 		ray_cmd_done(sc, SCP_UPD_MCAST);
   2042 		if (stat == RAY_CCS_STATUS_FAIL)
   2043 			rcmd = ray_reset;
   2044 		break;
   2045 	case RAY_CMD_START_NET:
   2046 	case RAY_CMD_JOIN_NET:
   2047 		rcmd = ray_start_join_net_done(sc, cmd, ccs, stat);
   2048 		break;
   2049 	case RAY_CMD_TX_REQ:
   2050 		if (sc->sc_if.if_flags & IFF_OACTIVE) {
   2051 			sc->sc_if.if_flags &= ~IFF_OACTIVE;
   2052 			/* this may also be a problem */
   2053 			rcmd = ray_intr_start;
   2054 		}
   2055 		/* free it -- no tracking */
   2056 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status,
   2057 		    RAY_CCS_STATUS_FREE);
   2058 		goto done;
   2059 	case RAY_CMD_START_ASSOC:
   2060 		ray_cmd_done(sc, SCP_STARTASSOC);
   2061 		if (stat == RAY_CCS_STATUS_FAIL)
   2062 			rcmd = ray_start_join_net;	/* XXX check */
   2063 		else {
   2064 			sc->sc_havenet = 1;
   2065 			rcmd = ray_intr_start;
   2066 		}
   2067 		break;
   2068 	case RAY_CMD_UPDATE_APM:
   2069 	case RAY_CMD_TEST_MEM:
   2070 	case RAY_CMD_SHUTDOWN:
   2071 	case RAY_CMD_DUMP_MEM:
   2072 	case RAY_CMD_START_TIMER:
   2073 		break;
   2074 	default:
   2075 		printf("%s: intr: unknown command 0x%x\n",
   2076 		    sc->sc_if.if_xname, cmd);
   2077 		break;
   2078 	}
   2079 	ray_free_ccs(sc, ccs);
   2080 done:
   2081 	/*
   2082 	 * see if needed things can be done now that a command
   2083 	 * has completed
   2084 	 */
   2085 	ray_check_scheduled(sc);
   2086 
   2087 	return (rcmd);
   2088 }
   2089 
   2090 /*
   2091  * an unsolicted interrupt, i.e., the ECF is sending us a command
   2092  */
   2093 static ray_cmd_func_t
   2094 ray_rccs_intr(sc, ccs)
   2095 	struct ray_softc *sc;
   2096 	bus_size_t ccs;
   2097 {
   2098 	ray_cmd_func_t rcmd;
   2099 	u_int cmd, stat;
   2100 
   2101 	cmd = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd);
   2102 	stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   2103 
   2104 	RAY_DPRINTF(("%s: ray_rccs_intr idx %ld cmd 0x%x stat %d\n",
   2105 	    sc->sc_xname, RAY_GET_INDEX(ccs), cmd, stat));
   2106 
   2107 	rcmd = 0;
   2108 	switch (cmd) {
   2109 	/*
   2110 	 * unsolicted commands
   2111 	 */
   2112 	case RAY_ECMD_RX_DONE:
   2113 		ray_recv(sc, ccs);
   2114 		goto done;
   2115 	case RAY_ECMD_REJOIN_DONE:
   2116 		if (sc->sc_mode == SC_MODE_ADHOC)
   2117 			break;
   2118 		/* get the current ssid */
   2119 		SRAM_READ_FIELD_N(sc, ccs, ray_cmd_net, c_bss_id,
   2120 		    sc->sc_bssid, sizeof(sc->sc_bssid));
   2121 		rcmd = ray_start_assoc;
   2122 		break;
   2123 	case RAY_ECMD_ROAM_START:
   2124 		/* no longer have network */
   2125 		sc->sc_havenet = 0;
   2126 		break;
   2127 	case RAY_ECMD_JAPAN_CALL_SIGNAL:
   2128 		break;
   2129 	default:
   2130 		ray_update_error_counters(sc);
   2131 
   2132 		/* this is a bogus return from build 4 don't free 0x55 */
   2133 		if (sc->sc_version == SC_BUILD_4 && cmd == 0x55
   2134 		    && RAY_GET_INDEX(ccs) == 0x55) {
   2135 			goto done;
   2136 		}
   2137 		printf("%s: intr: unknown command 0x%x\n",
   2138 		    sc->sc_if.if_xname, cmd);
   2139 		break;
   2140 	}
   2141 	/* free the ccs */
   2142 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status, RAY_CCS_STATUS_FREE);
   2143 done:
   2144 	return (rcmd);
   2145 }
   2146 
   2147 /*
   2148  * process an interrupt
   2149  */
   2150 static int
   2151 ray_intr(arg)
   2152 	void *arg;
   2153 {
   2154 	struct ray_softc *sc;
   2155 	ray_cmd_func_t rcmd;
   2156 	u_int i, count;
   2157 
   2158 	sc = arg;
   2159 
   2160 	RAY_DPRINTF(("%s: ray_intr\n", sc->sc_xname));
   2161 
   2162 	if ((++sc->sc_checkcounters % 32) == 0)
   2163 		ray_update_error_counters(sc);
   2164 
   2165 	count = 0;
   2166 	rcmd = 0;
   2167 	if (!REG_READ(sc, RAY_HCSIR))
   2168 		count = 0;
   2169 	else {
   2170 		count = 1;
   2171 		i = SRAM_READ_1(sc, RAY_SCB_RCCSI);
   2172 		if (i <= RAY_CCS_LAST)
   2173 			rcmd = ray_ccs_done(sc, RAY_GET_CCS(i));
   2174 		else if (i <= RAY_RCCS_LAST)
   2175 			rcmd = ray_rccs_intr(sc, RAY_GET_CCS(i));
   2176 		else
   2177 			printf("%s: intr: bad cmd index %d\n", sc->sc_xname, i);
   2178 	}
   2179 
   2180 	if (rcmd)
   2181 		(*rcmd)(sc);
   2182 
   2183 	if (count)
   2184 		REG_WRITE(sc, RAY_HCSIR, 0);
   2185 
   2186 	RAY_DPRINTF(("%s: interrupt handled %d\n", sc->sc_xname, count));
   2187 
   2188 	return (count ? 1 : 0);
   2189 }
   2190 
   2191 
   2192 /*
   2193  * Generic CCS handling
   2194  */
   2195 
   2196 /*
   2197  * free the chain of descriptors -- used for freeing allocated tx chains
   2198  */
   2199 static void
   2200 ray_free_ccs_chain(sc, ni)
   2201 	struct ray_softc *sc;
   2202 	u_int ni;
   2203 {
   2204 	u_int i;
   2205 
   2206 	while ((i = ni) != RAY_CCS_LINK_NULL) {
   2207 		ni = SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_link);
   2208 		SRAM_WRITE_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status,
   2209 		    RAY_CCS_STATUS_FREE);
   2210 	}
   2211 }
   2212 
   2213 /*
   2214  * free up a cmd and return the old status
   2215  * this routine is only used for commands
   2216  */
   2217 static u_int8_t
   2218 ray_free_ccs(sc, ccs)
   2219 	struct ray_softc *sc;
   2220 	bus_size_t ccs;
   2221 {
   2222 	u_int8_t stat;
   2223 
   2224 	RAY_DPRINTF(("%s: free_ccs idx %ld\n", sc->sc_xname,
   2225 	    RAY_GET_INDEX(ccs)));
   2226 
   2227 	stat = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
   2228 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status, RAY_CCS_STATUS_FREE);
   2229 	if (ccs <= RAY_GET_CCS(RAY_CCS_LAST))
   2230 		sc->sc_ccsinuse[RAY_GET_INDEX(ccs)] = 0;
   2231 
   2232 	return (stat);
   2233 }
   2234 
   2235 /*
   2236  * returns 1 and in `ccb' the bus offset of the free ccb
   2237  * or 0 if none are free
   2238  *
   2239  * If `track' is not zero, handles tracking this command
   2240  * possibly indicating a callback is needed and setting a timeout
   2241  * also if ECF isn't ready we terminate earlier to avoid overhead.
   2242  *
   2243  * this routine is only used for commands
   2244  */
   2245 static int
   2246 ray_alloc_ccs(sc, ccsp, cmd, track)
   2247 	struct ray_softc *sc;
   2248 	bus_size_t *ccsp;
   2249 	u_int cmd, track;
   2250 {
   2251 	bus_size_t ccs;
   2252 	u_int i;
   2253 
   2254 	RAY_DPRINTF(("%s: alloc_ccs cmd %d\n", sc->sc_xname, cmd));
   2255 
   2256 	/* for tracked commands, if not ready just set pending */
   2257 	if (track && !RAY_ECF_READY(sc)) {
   2258 		ray_cmd_schedule(sc, track);
   2259 		return (0);
   2260 	}
   2261 
   2262 	/* first scan our inuse array */
   2263 	for (i = RAY_CCS_CMD_FIRST; i <= RAY_CCS_CMD_LAST; i++) {
   2264 		/* XXX wonder if we have to probe here to make the card go */
   2265 		(void)SRAM_READ_FIELD_1(sc, RAY_GET_CCS(i), ray_cmd, c_status);
   2266 		if (!sc->sc_ccsinuse[i])
   2267 			break;
   2268 	}
   2269 	if (i > RAY_CCS_CMD_LAST) {
   2270 		if (track)
   2271 			ray_cmd_schedule(sc, track);
   2272 		return (0);
   2273 	}
   2274 	sc->sc_ccsinuse[i] = 1;
   2275 	ccs = RAY_GET_CCS(i);
   2276 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status, RAY_CCS_STATUS_BUSY);
   2277 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_cmd, cmd);
   2278 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_link, RAY_CCS_LINK_NULL);
   2279 
   2280 	*ccsp = ccs;
   2281 	return (1);
   2282 }
   2283 
   2284 
   2285 /*
   2286  * this function sets the pending bit for the command given in 'need'
   2287  * and schedules a timeout if none is scheduled already.  Any command
   2288  * that uses the `host to ecf' region must be serialized.
   2289  */
   2290 static void
   2291 ray_set_pending(sc, cmdf)
   2292 	struct ray_softc *sc;
   2293 	u_int cmdf;
   2294 {
   2295 	RAY_DPRINTF(("%s: ray_set_pending 0x%x\n", sc->sc_xname, cmdf));
   2296 
   2297 	sc->sc_scheduled |= cmdf;
   2298 	if (!sc->sc_timoneed) {
   2299 		RAY_DPRINTF(("%s: ray_set_pending new timo\n", sc->sc_xname));
   2300 		callout_reset(&sc->sc_check_scheduled_ch,
   2301 		    RAY_CHECK_SCHED_TIMEOUT, ray_check_scheduled, sc);
   2302 		sc->sc_timoneed = 1;
   2303 	}
   2304 }
   2305 
   2306 /*
   2307  * schedule the `cmdf' for completion later
   2308  */
   2309 static void
   2310 ray_cmd_schedule(sc, cmdf)
   2311 	struct ray_softc *sc;
   2312 	int cmdf;
   2313 {
   2314 	int track;
   2315 
   2316 	RAY_DPRINTF(("%s: ray_cmd_schedule 0x%x\n", sc->sc_xname, cmdf));
   2317 
   2318 	track = cmdf;
   2319 	if ((cmdf & SCP_UPD_MASK) == 0)
   2320 		ray_set_pending(sc, track);
   2321 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2322 		/* don't do timeout mechaniscm if subcmd already going */
   2323 		sc->sc_scheduled |= cmdf;
   2324 	} else
   2325 		ray_set_pending(sc, cmdf | SCP_UPDATESUBCMD);
   2326 }
   2327 
   2328 /*
   2329  * check to see if `cmdf' has been scheduled
   2330  */
   2331 static int
   2332 ray_cmd_is_scheduled(sc, cmdf)
   2333 	struct ray_softc *sc;
   2334 	int cmdf;
   2335 {
   2336 	RAY_DPRINTF(("%s: ray_cmd_is_scheduled 0x%x\n", sc->sc_xname, cmdf));
   2337 
   2338 	return ((sc->sc_scheduled & cmdf) ? 1 : 0);
   2339 }
   2340 
   2341 /*
   2342  * cancel a scheduled command (not a running one though!)
   2343  */
   2344 static void
   2345 ray_cmd_cancel(sc, cmdf)
   2346 	struct ray_softc *sc;
   2347 	int cmdf;
   2348 {
   2349 	RAY_DPRINTF(("%s: ray_cmd_cancel 0x%x\n", sc->sc_xname, cmdf));
   2350 
   2351 	sc->sc_scheduled &= ~cmdf;
   2352 	if ((cmdf & SCP_UPD_MASK) && (sc->sc_scheduled & SCP_UPD_MASK) == 0)
   2353 		sc->sc_scheduled &= ~SCP_UPDATESUBCMD;
   2354 
   2355 	/* if nothing else needed cancel the timer */
   2356 	if (sc->sc_scheduled == 0 && sc->sc_timoneed) {
   2357 		callout_stop(&sc->sc_check_scheduled_ch);
   2358 		sc->sc_timoneed = 0;
   2359 	}
   2360 }
   2361 
   2362 /*
   2363  * called to indicate the 'cmdf' has been issued
   2364  */
   2365 static void
   2366 ray_cmd_ran(sc, cmdf)
   2367 	struct ray_softc *sc;
   2368 	int cmdf;
   2369 {
   2370 	RAY_DPRINTF(("%s: ray_cmd_ran 0x%x\n", sc->sc_xname, cmdf));
   2371 
   2372 	if (cmdf & SCP_UPD_MASK)
   2373 		sc->sc_running |= cmdf | SCP_UPDATESUBCMD;
   2374 	else
   2375 		sc->sc_running |= cmdf;
   2376 
   2377 	if ((cmdf & SCP_TIMOCHECK_CMD_MASK) && !sc->sc_timocheck) {
   2378 		callout_reset(&sc->sc_check_ccs_ch, RAY_CHECK_CCS_TIMEOUT,
   2379 		    ray_check_ccs, sc);
   2380 		sc->sc_timocheck = 1;
   2381 	}
   2382 }
   2383 
   2384 /*
   2385  * check to see if `cmdf' has been issued
   2386  */
   2387 static int
   2388 ray_cmd_is_running(sc, cmdf)
   2389 	struct ray_softc *sc;
   2390 	int cmdf;
   2391 {
   2392 	RAY_DPRINTF(("%s: ray_cmd_is_running 0x%x\n", sc->sc_xname, cmdf));
   2393 
   2394 	return ((sc->sc_running & cmdf) ? 1 : 0);
   2395 }
   2396 
   2397 /*
   2398  * the given `cmdf' that was issued has completed
   2399  */
   2400 static void
   2401 ray_cmd_done(sc, cmdf)
   2402 	struct ray_softc *sc;
   2403 	int cmdf;
   2404 {
   2405 	RAY_DPRINTF(("%s: ray_cmd_done 0x%x\n", sc->sc_xname, cmdf));
   2406 
   2407 	sc->sc_running &= ~cmdf;
   2408 	if (cmdf & SCP_UPD_MASK) {
   2409 		sc->sc_running &= ~SCP_UPDATESUBCMD;
   2410 		if (sc->sc_scheduled & SCP_UPD_MASK)
   2411 			ray_cmd_schedule(sc, sc->sc_scheduled & SCP_UPD_MASK);
   2412 	}
   2413 	if ((sc->sc_running & SCP_TIMOCHECK_CMD_MASK) == 0 && sc->sc_timocheck){
   2414 		callout_stop(&sc->sc_check_ccs_ch);
   2415 		sc->sc_timocheck = 0;
   2416 	}
   2417 }
   2418 
   2419 /*
   2420  * issue the command
   2421  * only used for commands not tx
   2422  */
   2423 static int
   2424 ray_issue_cmd(sc, ccs, track)
   2425 	struct ray_softc *sc;
   2426 	bus_size_t ccs;
   2427 	u_int track;
   2428 {
   2429 	u_int i;
   2430 
   2431 	RAY_DPRINTF(("%s: ray_cmd_issue 0x%x\n", sc->sc_xname, track));
   2432 
   2433 	/*
   2434 	 * XXX other drivers did this, but I think
   2435 	 * what we really want to do is just make sure we don't
   2436 	 * get here or that spinning is ok
   2437 	 */
   2438 	i = 0;
   2439 	while (!RAY_ECF_READY(sc))
   2440 		if (++i > 50) {
   2441 			ray_free_ccs(sc, ccs);
   2442 			if (track)
   2443 				ray_cmd_schedule(sc, track);
   2444 			return (0);
   2445 		}
   2446 
   2447 	SRAM_WRITE_1(sc, RAY_SCB_CCSI, RAY_GET_INDEX(ccs));
   2448 	RAY_ECF_START_CMD(sc);
   2449 	ray_cmd_ran(sc, track);
   2450 
   2451 	return (1);
   2452 }
   2453 
   2454 /*
   2455  * send a simple command if we can
   2456  */
   2457 static int
   2458 ray_simple_cmd(sc, cmd, track)
   2459 	struct ray_softc *sc;
   2460 	u_int cmd, track;
   2461 {
   2462 	bus_size_t ccs;
   2463 
   2464 	return (ray_alloc_ccs(sc, &ccs, cmd, track) &&
   2465 	    ray_issue_cmd(sc, ccs, track));
   2466 }
   2467 
   2468 /*
   2469  * Functions based on CCS commands
   2470  */
   2471 
   2472 /*
   2473  * run a update subcommand
   2474  */
   2475 static void
   2476 ray_update_subcmd(sc)
   2477 	struct ray_softc *sc;
   2478 {
   2479 	int submask, i;
   2480 
   2481 	RAY_DPRINTF(("%s: ray_update_subcmd\n", sc->sc_xname));
   2482 
   2483 	ray_cmd_cancel(sc, SCP_UPDATESUBCMD);
   2484 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2485 		return;
   2486 	submask = SCP_UPD_FIRST;
   2487 	for (i = 0; i < ray_nsubcmdtab; submask <<= 1, i++) {
   2488 		if ((sc->sc_scheduled & SCP_UPD_MASK) == 0)
   2489 			break;
   2490 		/* when done the next command will be scheduled */
   2491 		if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD))
   2492 			break;
   2493 		if (!RAY_ECF_READY(sc))
   2494 			break;
   2495 		/*
   2496 		 * give priority to LSB -- e.g., if previous loop reschuled
   2497 		 * doing this command after calling the function won't catch
   2498 		 * if a later command sets an earlier bit
   2499 		 */
   2500 		if (sc->sc_scheduled & ((submask - 1) & SCP_UPD_MASK))
   2501 			break;
   2502 		if (sc->sc_scheduled & submask)
   2503 			(*ray_subcmdtab[i])(sc);
   2504 	}
   2505 }
   2506 
   2507 /*
   2508  * report a parameter
   2509  */
   2510 static void
   2511 ray_report_params(sc)
   2512 	struct ray_softc *sc;
   2513 {
   2514 	bus_size_t ccs;
   2515 
   2516 	ray_cmd_cancel(sc, SCP_REPORTPARAMS);
   2517 
   2518 	if (!sc->sc_repreq)
   2519 		return;
   2520 
   2521 	/* do the issue check before equality check */
   2522 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2523 		return;
   2524 	else if (ray_cmd_is_running(sc, SCP_REPORTPARAMS)) {
   2525 		ray_cmd_schedule(sc, SCP_REPORTPARAMS);
   2526 		return;
   2527 	} else if (!ray_alloc_ccs(sc, &ccs, RAY_CMD_REPORT_PARAMS,
   2528 	    SCP_REPORTPARAMS))
   2529 		return;
   2530 
   2531 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_report, c_paramid,
   2532 	    sc->sc_repreq->r_paramid);
   2533 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_report, c_nparam, 1);
   2534 	(void)ray_issue_cmd(sc, ccs, SCP_REPORTPARAMS);
   2535 }
   2536 
   2537 /*
   2538  * start an association
   2539  */
   2540 static void
   2541 ray_start_assoc(sc)
   2542 	struct ray_softc *sc;
   2543 {
   2544 	ray_cmd_cancel(sc, SCP_STARTASSOC);
   2545 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2546 		return;
   2547 	else if (ray_cmd_is_running(sc, SCP_STARTASSOC))
   2548 		return;
   2549 	(void)ray_simple_cmd(sc, RAY_CMD_START_ASSOC, SCP_STARTASSOC);
   2550 }
   2551 
   2552 /*
   2553  * Subcommand functions that use the SCP_UPDATESUBCMD command
   2554  * (and are serialized with respect to other update sub commands
   2555  */
   2556 
   2557 /*
   2558  * download the startup parameters to the card
   2559  *	-- no outstanding commands expected
   2560  */
   2561 static void
   2562 ray_download_params(sc)
   2563 	struct ray_softc *sc;
   2564 {
   2565 	struct ray_startup_params_head *sp;
   2566 	struct ray_startup_params_tail_5 *sp5;
   2567 	struct ray_startup_params_tail_4 *sp4;
   2568 	bus_size_t off;
   2569 
   2570 	RAY_DPRINTF(("%s: init_startup_params\n", sc->sc_xname));
   2571 
   2572 	ray_cmd_cancel(sc, SCP_UPD_STARTUP);
   2573 
   2574 #define	PUT2(p, v) 	\
   2575 	do { (p)[0] = ((v >> 8) & 0xff); (p)[1] = (v & 0xff); } while(0)
   2576 
   2577 	sp = &sc->sc_startup;
   2578 	sp4 = &sc->sc_startup_4;
   2579 	sp5 = &sc->sc_startup_5;
   2580 	memset(sp, 0, sizeof(*sp));
   2581 	if (sc->sc_version == SC_BUILD_4)
   2582 		memset(sp4, 0, sizeof(*sp4));
   2583 	else
   2584 		memset(sp5, 0, sizeof(*sp5));
   2585 	/* XXX: Raylink firmware doesn't have length field for ssid */
   2586 	memcpy(sp->sp_ssid, sc->sc_dnwid.i_nwid, sizeof(sp->sp_ssid));
   2587 	sp->sp_scan_mode = 0x1;
   2588 	memcpy(sp->sp_mac_addr, sc->sc_ecf_startup.e_station_addr,
   2589 	    ETHER_ADDR_LEN);
   2590 	PUT2(sp->sp_frag_thresh, 0x7fff);	/* disabled */
   2591 	if (sc->sc_version == SC_BUILD_4) {
   2592 #if 1
   2593 		/* linux/fbsd */
   2594 		PUT2(sp->sp_dwell_time, 0x200);
   2595 		PUT2(sp->sp_beacon_period, 1);
   2596 #else
   2597 		/* divined */
   2598 		PUT2(sp->sp_dwell_time, 0x400);
   2599 		PUT2(sp->sp_beacon_period, 0);
   2600 #endif
   2601 	} else {
   2602 		PUT2(sp->sp_dwell_time, 128);
   2603 		PUT2(sp->sp_beacon_period, 256);
   2604 	}
   2605 	sp->sp_dtim_interval = 1;
   2606 #if 0
   2607 	/* these are the documented defaults for build 5/6 */
   2608 	sp->sp_max_retry = 0x1f;
   2609 	sp->sp_ack_timo = 0x86;
   2610 	sp->sp_sifs = 0x1c;
   2611 #elif 1
   2612 	/* these were scrounged from the linux driver */
   2613 	sp->sp_max_retry = 0x07;
   2614 
   2615 	sp->sp_ack_timo = 0xa3;
   2616 	sp->sp_sifs = 0x1d;
   2617 #else
   2618 	/* these were divined */
   2619 	sp->sp_max_retry = 0x03;
   2620 
   2621 	sp->sp_ack_timo = 0xa3;
   2622 	sp->sp_sifs = 0x1d;
   2623 #endif
   2624 #if 0
   2625 	/* these are the documented defaults for build 5/6 */
   2626 	sp->sp_difs = 0x82;
   2627 	sp->sp_pifs = 0;
   2628 #else
   2629 	/* linux/fbsd */
   2630 	sp->sp_difs = 0x82;
   2631 
   2632 	if (sc->sc_version == SC_BUILD_4)
   2633 		sp->sp_pifs = 0xce;
   2634 	else
   2635 		sp->sp_pifs = 0x4e;
   2636 #endif
   2637 
   2638 	PUT2(sp->sp_rts_thresh, 0x7fff);	/* disabled */
   2639 	if (sc->sc_version == SC_BUILD_4) {
   2640 		PUT2(sp->sp_scan_dwell, 0xfb1e);
   2641 		PUT2(sp->sp_scan_max_dwell, 0xc75c);
   2642 	} else {
   2643 		PUT2(sp->sp_scan_dwell, 0x4e2);
   2644 		PUT2(sp->sp_scan_max_dwell, 0x38a4);
   2645 	}
   2646 	sp->sp_assoc_timo = 0x5;
   2647 	if (sc->sc_version == SC_BUILD_4) {
   2648 #if 0
   2649 		/* linux/fbsd */
   2650 		sp->sp_adhoc_scan_cycle = 0x4;
   2651 		sp->sp_infra_scan_cycle = 0x2;
   2652 		sp->sp_infra_super_scan_cycle = 0x4;
   2653 #else
   2654 		/* divined */
   2655 		sp->sp_adhoc_scan_cycle = 0x8;
   2656 		sp->sp_infra_scan_cycle = 0x1;
   2657 		sp->sp_infra_super_scan_cycle = 0x18;
   2658 #endif
   2659 	} else {
   2660 		sp->sp_adhoc_scan_cycle = 0x8;
   2661 		sp->sp_infra_scan_cycle = 0x2;
   2662 		sp->sp_infra_super_scan_cycle = 0x8;
   2663 	}
   2664 	sp->sp_promisc = sc->sc_promisc;
   2665 	PUT2(sp->sp_uniq_word, 0x0cbd);
   2666 	if (sc->sc_version == SC_BUILD_4) {
   2667 	/* XXX whats this value anyway.. the std says 50us */
   2668 		/* XXX sp->sp_slot_time = 0x4e; */
   2669 		sp->sp_slot_time = 0x4e;
   2670 #if 1
   2671 		/*linux/fbsd*/
   2672 		sp->sp_roam_low_snr_thresh = 0xff;
   2673 #else
   2674 		/*divined*/
   2675 		sp->sp_roam_low_snr_thresh = 0x30;
   2676 #endif
   2677 	} else {
   2678 		sp->sp_slot_time = 0x32;
   2679 		sp->sp_roam_low_snr_thresh = 0xff;	/* disabled */
   2680 	}
   2681 #if 1
   2682 	sp->sp_low_snr_count = 0xff;		/* disabled */
   2683 #else
   2684 	/* divined -- check */
   2685 	sp->sp_low_snr_count = 0x07;		/* disabled */
   2686 #endif
   2687 #if 0
   2688 	sp->sp_infra_missed_beacon_count = 0x2;
   2689 #elif 1
   2690 	/* linux/fbsd */
   2691 	sp->sp_infra_missed_beacon_count = 0x5;
   2692 #else
   2693 	/* divined -- check, looks fishy */
   2694 	sp->sp_infra_missed_beacon_count = 0x7;
   2695 #endif
   2696 	sp->sp_adhoc_missed_beacon_count = 0xff;
   2697 	sp->sp_country_code = sc->sc_dcountrycode;
   2698 	sp->sp_hop_seq = 0x0b;
   2699 	if (sc->sc_version == SC_BUILD_4) {
   2700 		sp->sp_hop_seq_len = 0x4e;
   2701 		sp4->sp_cw_max = 0x3f;	/* single byte on build 4 */
   2702 		sp4->sp_cw_min = 0x0f;	/* single byte on build 4 */
   2703 		sp4->sp_noise_filter_gain = 0x4;
   2704 		sp4->sp_noise_limit_offset = 0x8;
   2705 		sp4->sp_rssi_thresh_offset = 0x28;
   2706 		sp4->sp_busy_thresh_offset = 0x28;
   2707 		sp4->sp_sync_thresh = 0x07;
   2708 		sp4->sp_test_mode = 0x0;
   2709 		sp4->sp_test_min_chan = 0x2;
   2710 		sp4->sp_test_max_chan = 0x2;
   2711 	} else {
   2712 		sp->sp_hop_seq_len = 0x4f;
   2713 		PUT2(sp5->sp_cw_max, 0x3f);
   2714 		PUT2(sp5->sp_cw_min, 0x0f);
   2715 		sp5->sp_noise_filter_gain = 0x4;
   2716 		sp5->sp_noise_limit_offset = 0x8;
   2717 		sp5->sp_rssi_thresh_offset = 0x28;
   2718 		sp5->sp_busy_thresh_offset = 0x28;
   2719 		sp5->sp_sync_thresh = 0x07;
   2720 		sp5->sp_test_mode = 0x0;
   2721 		sp5->sp_test_min_chan = 0x2;
   2722 		sp5->sp_test_max_chan = 0x2;
   2723 #if 0
   2724 		sp5->sp_allow_probe_resp = 0x1;
   2725 #else
   2726 		sp5->sp_allow_probe_resp = 0x0;
   2727 #endif
   2728 		sp5->sp_privacy_must_start = 0x0;
   2729 		sp5->sp_privacy_can_join = 0x0;
   2730 		sp5->sp_basic_rate_set[0] = 0x2;
   2731 		    /* 2 = 1Mbps, 3 = old 2Mbps 4 = 2Mbps */
   2732 	}
   2733 
   2734 	/* we shouldn't be called with some command pending */
   2735 	if (!RAY_ECF_READY(sc))
   2736 		panic("ray_download_params busy");
   2737 
   2738 	/* write the compatible part */
   2739 	off = RAY_HOST_TO_ECF_BASE;
   2740 	ray_write_region(sc, off, sp, sizeof(sc->sc_startup));
   2741 	off += sizeof(sc->sc_startup);
   2742 	if (sc->sc_version == SC_BUILD_4)
   2743 		ray_write_region(sc, off, sp4, sizeof(*sp4));
   2744 	else
   2745 		ray_write_region(sc, off, sp5, sizeof(*sp5));
   2746 	if (!ray_simple_cmd(sc, RAY_CMD_START_PARAMS, SCP_UPD_STARTUP))
   2747 		panic("ray_download_params issue");
   2748 }
   2749 
   2750 /*
   2751  * start or join a network
   2752  */
   2753 static void
   2754 ray_start_join_net(sc)
   2755 	struct ray_softc *sc;
   2756 {
   2757 	struct ray_net_params np;
   2758 	bus_size_t ccs;
   2759 	int cmd;
   2760 
   2761 	ray_cmd_cancel(sc, SCP_UPD_STARTJOIN);
   2762 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2763 		return;
   2764 
   2765 	/* XXX check we may not want to re-issue */
   2766 	if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2767 		ray_cmd_schedule(sc, SCP_UPD_STARTJOIN);
   2768 		return;
   2769 	}
   2770 
   2771 	if (sc->sc_mode == SC_MODE_ADHOC)
   2772 		cmd = RAY_CMD_START_NET;
   2773 	else
   2774 		cmd = RAY_CMD_JOIN_NET;
   2775 
   2776 	if (!ray_alloc_ccs(sc, &ccs, cmd, SCP_UPD_STARTJOIN))
   2777 		return;
   2778 	sc->sc_startccs = ccs;
   2779 	sc->sc_startcmd = cmd;
   2780 	if (!memcmp(&sc->sc_cnwid, &sc->sc_dnwid, sizeof(sc->sc_cnwid))
   2781 	    && sc->sc_omode == sc->sc_mode)
   2782 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_upd_param, 0);
   2783 	else {
   2784 		sc->sc_havenet = 0;
   2785 		memset(&np, 0, sizeof(np));
   2786 		np.p_net_type = sc->sc_mode;
   2787 		memcpy(np.p_ssid, sc->sc_dnwid.i_nwid, sizeof(np.p_ssid));
   2788 		ray_write_region(sc, RAY_HOST_TO_ECF_BASE, &np, sizeof(np));
   2789 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_upd_param, 1);
   2790 	}
   2791 	if (ray_issue_cmd(sc, ccs, SCP_UPD_STARTJOIN))
   2792 		callout_reset(&sc->sc_start_join_timo_ch, RAY_START_TIMEOUT,
   2793 		    ray_start_join_timo, sc);
   2794 }
   2795 
   2796 static void
   2797 ray_start_join_timo(arg)
   2798 	void *arg;
   2799 {
   2800 	struct ray_softc *sc;
   2801 	u_int stat;
   2802 
   2803 	sc = arg;
   2804 	stat = SRAM_READ_FIELD_1(sc, sc->sc_startccs, ray_cmd, c_status);
   2805 	ray_start_join_net_done(sc, sc->sc_startcmd, sc->sc_startccs, stat);
   2806 }
   2807 
   2808 /*
   2809  * The start/join has completed.  Note: we timeout the start
   2810  * command because it seems to fail to work at least on the
   2811  * build 4 firmware without reporting an error.  This actually
   2812  * may be a result of not putting the correct params in the
   2813  * initial download.  If this is a timeout `stat' will be
   2814  * marked busy.
   2815  */
   2816 static ray_cmd_func_t
   2817 ray_start_join_net_done(sc, cmd, ccs, stat)
   2818 	struct ray_softc *sc;
   2819 	u_int cmd;
   2820 	bus_size_t ccs;
   2821 	u_int stat;
   2822 {
   2823 	int i;
   2824 	struct ray_net_params np;
   2825 
   2826 	callout_stop(&sc->sc_start_join_timo_ch);
   2827 	ray_cmd_done(sc, SCP_UPD_STARTJOIN);
   2828 
   2829 	if (stat == RAY_CCS_STATUS_FAIL) {
   2830 		/* XXX poke ifmedia when it supports this */
   2831 		sc->sc_havenet = 0;
   2832 		return (ray_start_join_net);
   2833 	}
   2834 	if (stat == RAY_CCS_STATUS_BUSY || stat == RAY_CCS_STATUS_FREE) {
   2835 		/* handle the timeout condition */
   2836 		callout_reset(&sc->sc_start_join_timo_ch, RAY_START_TIMEOUT,
   2837 		    ray_start_join_timo, sc);
   2838 
   2839 		/* be safe -- not a lot occurs with no net though */
   2840 		if (!RAY_ECF_READY(sc))
   2841 			return (0);
   2842 
   2843 		/* see if our nwid is up to date */
   2844 		if (!memcmp(&sc->sc_cnwid, &sc->sc_dnwid, sizeof(sc->sc_cnwid))
   2845 		    && sc->sc_omode == sc->sc_mode)
   2846 			SRAM_WRITE_FIELD_1(sc,ccs, ray_cmd_net, c_upd_param, 0);
   2847 		else {
   2848 			memset(&np, 0, sizeof(np));
   2849 			np.p_net_type = sc->sc_mode;
   2850 			memcpy(np.p_ssid, sc->sc_dnwid.i_nwid,
   2851 			    sizeof(np.p_ssid));
   2852 			ray_write_region(sc, RAY_HOST_TO_ECF_BASE, &np,
   2853 			    sizeof(np));
   2854 			SRAM_WRITE_FIELD_1(sc,ccs, ray_cmd_net, c_upd_param, 1);
   2855 		}
   2856 
   2857 		if (sc->sc_mode == SC_MODE_ADHOC)
   2858 			cmd = RAY_CMD_START_NET;
   2859 		else
   2860 			cmd = RAY_CMD_JOIN_NET;
   2861 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_cmd,
   2862 		    RAY_CCS_STATUS_BUSY);
   2863 		SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_net, c_status,
   2864 		    RAY_CCS_STATUS_BUSY);
   2865 
   2866 		/* we simply poke the card again issuing the same ccs */
   2867 		SRAM_WRITE_1(sc, RAY_SCB_CCSI, RAY_GET_INDEX(ccs));
   2868 		RAY_ECF_START_CMD(sc);
   2869 		ray_cmd_ran(sc, SCP_UPD_STARTJOIN);
   2870 		return (0);
   2871 	}
   2872 	/* get the current ssid */
   2873 	SRAM_READ_FIELD_N(sc, ccs, ray_cmd_net, c_bss_id, sc->sc_bssid,
   2874 	    sizeof(sc->sc_bssid));
   2875 
   2876 	sc->sc_deftxrate = SRAM_READ_FIELD_1(sc, ccs, ray_cmd_net,c_def_txrate);
   2877 	sc->sc_encrypt = SRAM_READ_FIELD_1(sc, ccs, ray_cmd_net, c_encrypt);
   2878 
   2879 	/* adjust values for buggy build 4 */
   2880 	if (sc->sc_deftxrate == 0x55)
   2881 		sc->sc_deftxrate = RAY_PID_BASIC_RATE_1500K;
   2882 	if (sc->sc_encrypt == 0x55)
   2883 		sc->sc_encrypt = 0;
   2884 
   2885 	if (SRAM_READ_FIELD_1(sc, ccs, ray_cmd_net, c_upd_param)) {
   2886 		ray_read_region(sc, RAY_HOST_TO_ECF_BASE, &np, sizeof(np));
   2887 		/* XXX: Raylink firmware doesn't have length field for ssid */
   2888 		for (i = 0; i < sizeof(np.p_ssid); i++) {
   2889 			if (np.p_ssid[i] == '\0')
   2890 				break;
   2891 		}
   2892 		sc->sc_cnwid.i_len = i;
   2893 		memcpy(sc->sc_cnwid.i_nwid, np.p_ssid, sizeof(sc->sc_cnwid));
   2894 		sc->sc_omode = sc->sc_mode;
   2895 		if (np.p_net_type != sc->sc_mode)
   2896 			return (ray_start_join_net);
   2897 	}
   2898 	RAY_DPRINTF(("%s: net start/join nwid %.32s bssid %s inited %d\n",
   2899 	    sc->sc_xname, sc->sc_cnwid.i_nwid, ether_sprintf(sc->sc_bssid),
   2900 		SRAM_READ_FIELD_1(sc, ccs, ray_cmd_net, c_inited)));
   2901 
   2902 	/* network is now active */
   2903 	ray_cmd_schedule(sc, SCP_UPD_MCAST|SCP_UPD_PROMISC);
   2904 	if (cmd == RAY_CMD_JOIN_NET)
   2905 		return (ray_start_assoc);
   2906 	else {
   2907 		sc->sc_havenet = 1;
   2908 		return (ray_intr_start);
   2909 	}
   2910 }
   2911 
   2912 /*
   2913  * set the card in/out of promiscuous mode
   2914  */
   2915 static void
   2916 ray_update_promisc(sc)
   2917 	struct ray_softc *sc;
   2918 {
   2919 	bus_size_t ccs;
   2920 	int promisc;
   2921 
   2922 	ray_cmd_cancel(sc, SCP_UPD_PROMISC);
   2923 
   2924 	/* do the issue check before equality check */
   2925 	if (sc->sc_if.if_flags & IFF_ALLMULTI)
   2926 		sc->sc_if.if_flags |= IFF_PROMISC;
   2927 	else if (sc->sc_if.if_pcount == 0)
   2928 		sc->sc_if.if_flags &= ~IFF_PROMISC;
   2929 	promisc = !!(sc->sc_if.if_flags & IFF_PROMISC);
   2930 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2931 		return;
   2932 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2933 		ray_cmd_schedule(sc, SCP_UPD_PROMISC);
   2934 		return;
   2935 	} else if (promisc == sc->sc_promisc)
   2936 		return;
   2937 	else if (!ray_alloc_ccs(sc,&ccs,RAY_CMD_UPDATE_PARAMS, SCP_UPD_PROMISC))
   2938 		return;
   2939 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_paramid, RAY_PID_PROMISC);
   2940 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_nparam, 1);
   2941 	SRAM_WRITE_1(sc, RAY_HOST_TO_ECF_BASE, promisc);
   2942 	(void)ray_issue_cmd(sc, ccs, SCP_UPD_PROMISC);
   2943 }
   2944 
   2945 /*
   2946  * update the parameter based on what the user passed in
   2947  */
   2948 static void
   2949 ray_update_params(sc)
   2950 	struct ray_softc *sc;
   2951 {
   2952 	bus_size_t ccs;
   2953 
   2954 	ray_cmd_cancel(sc, SCP_UPD_UPDATEPARAMS);
   2955 	if (!sc->sc_updreq) {
   2956 		/* XXX do we need to wakeup here? */
   2957 		return;
   2958 	}
   2959 
   2960 	/* do the issue check before equality check */
   2961 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   2962 		return;
   2963 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   2964 		ray_cmd_schedule(sc, SCP_UPD_UPDATEPARAMS);
   2965 		return;
   2966 	} else if (!ray_alloc_ccs(sc, &ccs, RAY_CMD_UPDATE_PARAMS,
   2967 	    SCP_UPD_UPDATEPARAMS))
   2968 		return;
   2969 
   2970 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_paramid,
   2971 	    sc->sc_updreq->r_paramid);
   2972 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update, c_nparam, 1);
   2973 	ray_write_region(sc, RAY_HOST_TO_ECF_BASE, sc->sc_updreq->r_data,
   2974 	    sc->sc_updreq->r_len);
   2975 
   2976 	(void)ray_issue_cmd(sc, ccs, SCP_UPD_UPDATEPARAMS);
   2977 }
   2978 
   2979 /*
   2980  * set the multicast filter list
   2981  */
   2982 static void
   2983 ray_update_mcast(sc)
   2984 	struct ray_softc *sc;
   2985 {
   2986 	bus_size_t ccs;
   2987 	struct ether_multistep step;
   2988 	struct ether_multi *enm;
   2989 	struct ethercom *ec;
   2990 	bus_size_t bufp;
   2991 	int count;
   2992 
   2993 	ec = &sc->sc_ec;
   2994 	ray_cmd_cancel(sc, SCP_UPD_MCAST);
   2995 
   2996 	/* see if we have any ranges */
   2997 	if ((count = sc->sc_ec.ec_multicnt) < 17) {
   2998 		ETHER_FIRST_MULTI(step, ec, enm);
   2999 		while (enm) {
   3000 			/* see if this is a range */
   3001 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   3002 				ETHER_ADDR_LEN)) {
   3003 				count = 17;
   3004 				break;
   3005 			}
   3006 			ETHER_NEXT_MULTI(step, enm);
   3007 		}
   3008 	}
   3009 
   3010 	/* track this stuff even when not running */
   3011 	if (count > 16) {
   3012 		sc->sc_if.if_flags |= IFF_ALLMULTI;
   3013 		ray_update_promisc(sc);
   3014 		return;
   3015 	} else if (sc->sc_if.if_flags & IFF_ALLMULTI) {
   3016 		sc->sc_if.if_flags &= ~IFF_ALLMULTI;
   3017 		ray_update_promisc(sc);
   3018 	}
   3019 
   3020 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
   3021 		return;
   3022 	else if (ray_cmd_is_running(sc, SCP_UPDATESUBCMD)) {
   3023 		ray_cmd_schedule(sc, SCP_UPD_MCAST);
   3024 		return;
   3025 	} else if (!ray_alloc_ccs(sc,&ccs, RAY_CMD_UPDATE_MCAST, SCP_UPD_MCAST))
   3026 		return;
   3027 	SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_update_mcast, c_nmcast, count);
   3028 	bufp = RAY_HOST_TO_ECF_BASE;
   3029 	ETHER_FIRST_MULTI(step, ec, enm);
   3030 	while (enm) {
   3031 		ray_write_region(sc, bufp, enm->enm_addrlo, ETHER_ADDR_LEN);
   3032 		bufp += ETHER_ADDR_LEN;
   3033 		ETHER_NEXT_MULTI(step, enm);
   3034 	}
   3035 	(void)ray_issue_cmd(sc, ccs, SCP_UPD_MCAST);
   3036 }
   3037 
   3038 /*
   3039  * User issued commands
   3040  */
   3041 
   3042 /*
   3043  * issue a update params
   3044  *
   3045  * expected to be called in sleapable context -- intended for user stuff
   3046  */
   3047 static int
   3048 ray_user_update_params(sc, pr)
   3049 	struct ray_softc *sc;
   3050 	struct ray_param_req *pr;
   3051 {
   3052 	int rv;
   3053 
   3054 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   3055 		pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   3056 		return (EIO);
   3057 	}
   3058 
   3059 	/* wait to be able to issue the command */
   3060 	rv = 0;
   3061 	while (ray_cmd_is_running(sc, SCP_UPD_UPDATEPARAMS) ||
   3062 	    ray_cmd_is_scheduled(sc, SCP_UPD_UPDATEPARAMS)) {
   3063 		rv = tsleep(ray_update_params, 0|PCATCH, "cmd in use", 0);
   3064 		if (rv)
   3065 			return (rv);
   3066 		if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   3067 			pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   3068 			return (EIO);
   3069 		}
   3070 	}
   3071 
   3072 	pr->r_failcause = RAY_FAILCAUSE_WAITING;
   3073 	sc->sc_updreq = pr;
   3074 	ray_cmd_schedule(sc, SCP_UPD_UPDATEPARAMS);
   3075 	ray_check_scheduled(sc);
   3076 
   3077 	while (pr->r_failcause == RAY_FAILCAUSE_WAITING)
   3078 		(void)tsleep(ray_update_params, 0, "waiting cmd", 0);
   3079 	wakeup(ray_update_params);
   3080 
   3081 	return (0);
   3082 }
   3083 
   3084 /*
   3085  * issue a report params
   3086  *
   3087  * expected to be called in sleapable context -- intended for user stuff
   3088  */
   3089 static int
   3090 ray_user_report_params(sc, pr)
   3091 	struct ray_softc *sc;
   3092 	struct ray_param_req *pr;
   3093 {
   3094 	int rv;
   3095 
   3096 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   3097 		pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   3098 		return (EIO);
   3099 	}
   3100 
   3101 	/* wait to be able to issue the command */
   3102 	rv = 0;
   3103 	while (ray_cmd_is_running(sc, SCP_REPORTPARAMS)
   3104 	    || ray_cmd_is_scheduled(sc, SCP_REPORTPARAMS)) {
   3105 		rv = tsleep(ray_report_params, 0|PCATCH, "cmd in use", 0);
   3106 		if (rv)
   3107 			return (rv);
   3108 		if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
   3109 			pr->r_failcause = RAY_FAILCAUSE_EDEVSTOP;
   3110 			return (EIO);
   3111 		}
   3112 	}
   3113 
   3114 	pr->r_failcause = RAY_FAILCAUSE_WAITING;
   3115 	sc->sc_repreq = pr;
   3116 	ray_cmd_schedule(sc, SCP_REPORTPARAMS);
   3117 	ray_check_scheduled(sc);
   3118 
   3119 	while (pr->r_failcause == RAY_FAILCAUSE_WAITING)
   3120 		(void)tsleep(ray_report_params, 0, "waiting cmd", 0);
   3121 	wakeup(ray_report_params);
   3122 
   3123 	return (0);
   3124 }
   3125 
   3126 
   3127 /*
   3128  * this is a temporary wrapper around bus_space_read_region_1
   3129  * as it seems to mess with gcc.  the line numbers get offset
   3130  * presumably this is related to the inline asm on i386.
   3131  */
   3132 
   3133 static void
   3134 ray_read_region(sc, off, vp, c)
   3135 	struct ray_softc *sc;
   3136 	bus_size_t off;
   3137 	void *vp;
   3138 	size_t c;
   3139 {
   3140 #ifdef RAY_USE_OPTIMIZED_COPY
   3141 	u_int n2, n4, tmp;
   3142 	u_int8_t *p;
   3143 
   3144 	p = vp;
   3145 
   3146 	/* XXX we may be making poor assumptions here but lets hope */
   3147 	switch ((off|(bus_addr_t)p) & 0x03) {
   3148 	case 0:
   3149 		if ((n4 = c / 4)) {
   3150 			bus_space_read_region_4(sc->sc_memt, sc->sc_memh, off,
   3151 			    p, n4);
   3152 			tmp = c & ~0x3;
   3153 			c &= 0x3;
   3154 			p += tmp;
   3155 			off += tmp;
   3156 		}
   3157 		switch (c) {
   3158 		case 3:
   3159 			*p = bus_space_read_1(sc->sc_memt,sc->sc_memh, off);
   3160 			p++, off++;
   3161 		case 2:
   3162 			*p = bus_space_read_1(sc->sc_memt,sc->sc_memh, off);
   3163 			p++, off++;
   3164 		case 1:
   3165 			*p = bus_space_read_1(sc->sc_memt,sc->sc_memh, off);
   3166 		}
   3167 		break;
   3168 	case 2:
   3169 		if ((n2 = (c >> 1)))
   3170 			bus_space_read_region_2(sc->sc_memt, sc->sc_memh, off,
   3171 			    p, n2);
   3172 		if (c & 1) {
   3173 			c &= ~0x1;
   3174 			*(p + c) = bus_space_read_1(sc->sc_memt, sc->sc_memh,
   3175 			    off + c);
   3176 		}
   3177 		break;
   3178 	case 1:
   3179 	case 3:
   3180 		bus_space_read_region_1(sc->sc_memt, sc->sc_memh, off, p, c);
   3181 		break;
   3182 	}
   3183 #else
   3184 	bus_space_read_region_1(sc->sc_memt, sc->sc_memh, off, vp, c);
   3185 #endif
   3186 }
   3187 
   3188 /*
   3189  * this is a temporary wrapper around bus_space_write_region_1
   3190  * as it seems to mess with gcc.  the line numbers get offset
   3191  * presumably this is related to the inline asm on i386.
   3192  */
   3193 static void
   3194 ray_write_region(sc, off, vp, c)
   3195 	struct ray_softc *sc;
   3196 	bus_size_t off;
   3197 	void *vp;
   3198 	size_t c;
   3199 {
   3200 #ifdef RAY_USE_OPTIMIZED_COPY
   3201 	size_t n2, n4, tmp;
   3202 	u_int8_t *p;
   3203 
   3204 	p = vp;
   3205 	/* XXX we may be making poor assumptions here but lets hope */
   3206 	switch ((off|(bus_addr_t)p) & 0x03) {
   3207 	case 0:
   3208 		if ((n4 = (c >> 2))) {
   3209 			bus_space_write_region_4(sc->sc_memt, sc->sc_memh, off,
   3210 			    p, n4);
   3211 			tmp = c & ~0x3;
   3212 			c &= 0x3;
   3213 			p += tmp;
   3214 			off += tmp;
   3215 		}
   3216 		switch (c) {
   3217 		case 3:
   3218 			bus_space_write_1(sc->sc_memt,sc->sc_memh, off, *p);
   3219 			p++, off++;
   3220 		case 2:
   3221 			bus_space_write_1(sc->sc_memt,sc->sc_memh, off, *p);
   3222 			p++, off++;
   3223 		case 1:
   3224 			bus_space_write_1(sc->sc_memt,sc->sc_memh, off, *p);
   3225 		}
   3226 		break;
   3227 	case 2:
   3228 		if ((n2 = (c >> 1)))
   3229 			bus_space_write_region_2(sc->sc_memt, sc->sc_memh, off,
   3230 			    p, n2);
   3231 		if (c & 0x1) {
   3232 			c &= ~0x1;
   3233 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
   3234 			    off + c, *(p + c));
   3235 		}
   3236 		break;
   3237 	case 1:
   3238 	case 3:
   3239 		bus_space_write_region_1(sc->sc_memt, sc->sc_memh, off, p, c);
   3240 		break;
   3241 	}
   3242 #else
   3243 	bus_space_write_region_1(sc->sc_memt, sc->sc_memh, off, vp, c);
   3244 #endif
   3245 }
   3246 
   3247 #ifdef RAY_DEBUG
   3248 
   3249 #define PRINTABLE(c) ((c) >= 0x20 && (c) <= 0x7f)
   3250 
   3251 void
   3252 hexdump(const u_int8_t *d, int len, int br, int div, int fl)
   3253 {
   3254 	int i, j, offw, first, tlen, ni, nj, sp;
   3255 
   3256 	sp = br / div;
   3257 	offw = 0;
   3258 	if (len && (fl & HEXDF_NOOFFSET) == 0) {
   3259 		tlen = len;
   3260 		do {
   3261 			offw++;
   3262 		} while (tlen /= br);
   3263 	}
   3264 	if (offw)
   3265 		printf("%0*x: ", offw, 0);
   3266 	for (i = 0; i < len; i++, d++) {
   3267 		if (i && (i % br) == 0) {
   3268 			if ((fl & HEXDF_NOASCII) == 0) {
   3269 				printf("   ");
   3270 				d -= br;
   3271 				for (j = 0; j < br; d++, j++) {
   3272 					if (j && (j % sp) == 0)
   3273 						printf(" ");
   3274 					if (PRINTABLE(*d))
   3275 						printf("%c", (int)*d);
   3276 					else
   3277 						printf(".");
   3278 				}
   3279 			}
   3280 			if (offw)
   3281 				printf("\n%0*x: ", offw, i);
   3282 			else
   3283 				printf("\n");
   3284 			if ((fl & HEXDF_NOCOMPRESS) == 0) {
   3285 				first = 1;
   3286 				while (len - i >= br) {
   3287 					if (memcmp(d, d - br, br))
   3288 						break;
   3289 					d += br;
   3290 					i += br;
   3291 					if (first) {
   3292 						printf("*");
   3293 						first = 0;
   3294 					}
   3295 				}
   3296 				if (len == i) {
   3297 					printf("\n%0*x", offw, i);
   3298 					return;
   3299 				}
   3300 			}
   3301 		} else if (i && (i % sp) == 0)
   3302 			printf(" ");
   3303 		printf("%02x ", *d);
   3304 	}
   3305 	if (len && (((i - 1) % br) || i == 1)) {
   3306 		if ((fl & HEXDF_NOASCII) == 0) {
   3307 			i = i % br ? i % br : br;
   3308 			ni = (br - i) % br;
   3309 			j = (i - 1) / sp;
   3310 			nj = (div - j - 1) % div;
   3311 			j = 3 * ni + nj + 3;
   3312 			printf("%*s", j, "");
   3313 			d -= i;
   3314 			for (j = 0; j < i; d++, j++) {
   3315 				if (j && (j % sp) == 0)
   3316 					printf(" ");
   3317 				if (PRINTABLE(*d))
   3318 					printf("%c", (int)*d);
   3319 				else
   3320 					printf(".");
   3321 			}
   3322 		}
   3323 		printf("\n");
   3324 	}
   3325 }
   3326 
   3327 
   3328 
   3329 static void
   3330 ray_dump_mbuf(sc, m)
   3331 	struct ray_softc *sc;
   3332 	struct mbuf *m;
   3333 {
   3334 	u_int8_t *d, *ed;
   3335 	u_int i;
   3336 
   3337 	printf("%s: pkt dump:", sc->sc_xname);
   3338 	i = 0;
   3339 	for (; m; m = m->m_next) {
   3340 		d = mtod(m, u_int8_t *);
   3341 		ed = d + m->m_len;
   3342 
   3343 		for (; d < ed; i++, d++) {
   3344 			if ((i % 16) == 0)
   3345 				printf("\n\t");
   3346 			else if ((i % 8) == 0)
   3347 				printf("  ");
   3348 			printf(" %02x", *d);
   3349 		}
   3350 	}
   3351 	if ((i - 1) % 16)
   3352 		printf("\n");
   3353 }
   3354 #endif	/* RAY_DEBUG */
   3355 
   3356 #ifdef RAY_DO_SIGLEV
   3357 static void
   3358 ray_update_siglev(sc, src, siglev)
   3359 	struct ray_softc *sc;
   3360 	u_int8_t *src;
   3361 	u_int8_t siglev;
   3362 {
   3363 	int i, mini;
   3364 	struct timeval mint;
   3365 	struct ray_siglev *sl;
   3366 
   3367 	/* try to find host */
   3368 	for (i = 0; i < RAY_NSIGLEVRECS; i++) {
   3369 		sl = &sc->sc_siglevs[i];
   3370 		if (memcmp(sl->rsl_host, src, ETHER_ADDR_LEN) == 0)
   3371 			goto found;
   3372 	}
   3373 	/* not found, find oldest slot */
   3374 	mini = 0;
   3375 	mint.tv_sec = LONG_MAX;
   3376 	mint.tv_usec = 0;
   3377 	for (i = 0; i < RAY_NSIGLEVRECS; i++) {
   3378 		sl = &sc->sc_siglevs[i];
   3379 		if (timercmp(&sl->rsl_time, &mint, <)) {
   3380 			mini = i;
   3381 			mint = sl->rsl_time;
   3382 		}
   3383 	}
   3384 	sl = &sc->sc_siglevs[mini];
   3385 	memset(sl->rsl_siglevs, 0, RAY_NSIGLEV);
   3386 	memcpy(sl->rsl_host, src, ETHER_ADDR_LEN);
   3387 
   3388  found:
   3389 	microtime(&sl->rsl_time);
   3390 	memmove(&sl->rsl_siglevs[1], sl->rsl_siglevs, RAY_NSIGLEV-1);
   3391 	sl->rsl_siglevs[0] = siglev;
   3392 }
   3393 #endif
   3394