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