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