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