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