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