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