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