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