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