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