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