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