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