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