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