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