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