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