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