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