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