]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ray/if_ray.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / ray / if_ray.c
1 /*      $NetBSD: if_ray.c,v 1.12 2000/02/07 09:36:27 augustss Exp $     */
2 /*-
3  * Copyright (C) 2000
4  * Dr. Duncan McLennan Barclay, dmlb@ragnet.demon.co.uk.
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY DUNCAN BARCLAY AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL DUNCAN BARCLAY OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *
33  */
34
35 /*-
36  * Copyright (c) 2000 Christian E. Hopps
37  * All rights reserved.
38  * 
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the author nor the names of any co-contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  */
63
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66
67 /*
68  * Card configuration
69  * ==================
70  *
71  * This card is unusual in that it uses both common and attribute
72  * memory whilst working. It should use common memory and an IO port.
73  *
74  * The bus resource allocations need to work around the brain deadness
75  * of pccardd (where it reads the CIS for common memory, sets it all
76  * up and then throws it all away assuming the card is an ed
77  * driver...). Note that this could be dangerous (because it doesn't
78  * interact with pccardd) if you use other memory mapped cards in the
79  * same pccard slot as currently old mappings are not cleaned up very well
80  * by the bus_release_resource methods or pccardd.
81  * XXX Are these workarounds necessary with NEWCARD?
82  *
83  * Ad-hoc and infra-structure modes
84  * ================================
85  * 
86  * The driver supports ad-hoc mode for V4 firmware and infrastructure
87  * mode for V5 firmware. V5 firmware in ad-hoc mode is untested and should
88  * work.
89  *
90  * The Linux driver also seems to have the capability to act as an AP.
91  * I wonder what facilities the "AP" can provide within a driver? We can
92  * probably use the BRIDGE code to form an ESS but I don't think
93  * power saving etc. is easy.
94  *
95  *
96  * Packet framing/encapsulation/translation
97  * ========================================
98  * 
99  * Currently we support the Webgear encapsulation:
100  *      802.11  header <net/if_ieee80211.h>struct ieee80211_frame
101  *      802.3   header <net/ethernet.h>struct ether_header
102  *      IP/ARP  payload
103  *
104  * and RFC1042 encapsulation of IP datagrams (translation):
105  *      802.11  header <net/if_ieee80211.h>struct ieee80211_frame
106  *      802.2   LLC header
107  *      802.2   SNAP header
108  *      802.3   Ethertype
109  *      IP/ARP  payload
110  *
111  * Framing should be selected via if_media stuff or link types but
112  * is currently hardcoded to:
113  *      V4      encapsulation
114  *      V5      translation
115  *
116  *
117  * Authentication
118  * ==============
119  *
120  * 802.11 provides two authentication mechanisms. The first is a very
121  * simple host based mechanism (like xhost) called Open System and the
122  * second is a more complex challenge/response called Shared Key built
123  * ontop of WEP.
124  *
125  * This driver only supports Open System and does not implement any
126  * host based control lists. In otherwords authentication is always
127  * granted to hosts wanting to authenticate with this station. This is
128  * the only sensible behaviour as the Open System mechanism uses MAC
129  * addresses to identify hosts. Send me patches if you need it!
130  */
131
132 /*
133  * ***check all XXX_INFRA code - reassoc not done well at all!
134  * ***watchdog to catch screwed up removals?
135  * ***error handling of RAY_COM_RUNQ
136  * ***error handling of ECF command completions
137  * ***can't seem to create a n/w that Win95 wants to see.
138  * ***remove panic in ray_com_ecf by re-quing or timeout
139  * ***use new ioctl stuff - probably need to change RAY_COM_FCHKRUNNING things?
140  *      consider user doing:
141  *              ifconfig ray0 192.168.200.38 -bssid "freed"
142  *              ifconfig ray0 192.168.200.38 -bssid "fred"
143  *      here the second one would be missed in this code
144  * check that v5 needs timeouts on ecf commands
145  * write up driver structure in comments above
146  * UPDATE_PARAMS seems to return via an interrupt - maybe the timeout
147  *      is needed for wrong values?
148  * proper setting of mib_hop_seq_len with country code for v4 firmware
149  *      best done with raycontrol?
150  * countrycode setting is broken I think
151  *      userupdate should trap and do via startjoin etc.
152  * fragmentation when rx level drops?
153  * v5 might not need download
154  *      defaults are as documented apart from hop_seq_length
155  *      settings are sane for ad-hoc not infra
156  *
157  * driver state
158  *      most state is implied by the sequence of commands in the runq
159  *      but in fact any of the rx and tx path that uses variables
160  *      in the sc_c are potentially going to get screwed?
161  *
162  * infra mode stuff
163  *      proper handling of the basic rate set - see the manual
164  *      all ray_sj, ray_assoc sequencues need a "nicer" solution as we
165  *              remember association and authentication
166  *      need to consider WEP
167  *      acting as ap - should be able to get working from the manual
168  *      need to finish RAY_ECMD_REJOIN_DONE
169  *      finish authenitcation code, it doesn't handle errors/timeouts/
170  *      REJOIN etc.
171  *
172  * ray_nw_param
173  *      promisc in here too? - done
174  *      should be able to update the parameters before we download to the
175  *              device. This means we must attach a desired struct to the
176  *              runq entry and maybe have another big case statement to
177  *              move these desired into current when not running.
178  *              init must then use the current settings (pre-loaded
179  *              in attach now!) and pass to download. But we can't access
180  *              current nw params outside of the runq - ahhh
181  *      differeniate between parameters set in attach and init
182  *      sc_station_addr in here too (for changing mac address)
183  *      move desired into the command structure?
184  *      take downloaded MIB from a complete nw_param?
185  *      longer term need to attach a desired nw params to the runq entry
186  *
187  *
188  * RAY_COM_RUNQ errors
189  *
190  * if sleeping in ccs_alloc with eintr/erestart/enxio/enodev
191  *      erestart        try again from the top
192  *                      XXX do not malloc more comqs
193  *                      XXX ccs allocation hard
194  *      eintr           clean up and return
195  *      enxio           clean up and return - done in macro
196  *
197  * if sleeping in runq_arr itself with eintr/erestart/enxio/enodev
198  *      erestart        try again from the top
199  *                      XXX do not malloc more comqs
200  *                      XXX ccs allocation hard
201  *                      XXX reinsert comqs at head of list
202  *      eintr           clean up and return
203  *      enxio           clean up and return - done in macro
204  */
205
206 #define XXX             0
207 #define XXX_ACTING_AP   0
208 #define XXX_INFRA       0
209 #define RAY_DEBUG       (                               \
210                         /* RAY_DBG_AUTH         | */    \
211                         /* RAY_DBG_SUBR         | */    \
212                         /* RAY_DBG_BOOTPARAM    | */    \
213                         /* RAY_DBG_STARTJOIN    | */    \
214                         /* RAY_DBG_CCS          | */    \
215                         /* RAY_DBG_IOCTL        | */    \
216                         /* RAY_DBG_MBUF         | */    \
217                         /* RAY_DBG_RX           | */    \
218                         /* RAY_DBG_CM           | */    \
219                         /* RAY_DBG_COM          | */    \
220                         /* RAY_DBG_STOP         | */    \
221                         /* RAY_DBG_CTL          | */    \
222                         /* RAY_DBG_MGT          | */    \
223                         /* RAY_DBG_TX           | */    \
224                         /* RAY_DBG_DCOM         | */    \
225                         0                               \
226                         )
227
228 /*
229  * XXX build options - move to LINT
230  */
231 #define RAY_CM_RID              0       /* pccardd abuses windows 0 and 1 */
232 #define RAY_AM_RID              3       /* pccardd abuses windows 0 and 1 */
233 #define RAY_COM_TIMEOUT         (hz/2)  /* Timeout for CCS commands */
234 #define RAY_TX_TIMEOUT          (hz/2)  /* Timeout for rescheduling TX */
235 #define RAY_ECF_SPIN_DELAY      1000    /* Wait 1ms before checking ECF ready */
236 #define RAY_ECF_SPIN_TRIES      10      /* Wait this many times for ECF ready */
237 /*
238  * XXX build options - move to LINT
239  */
240
241 #ifndef RAY_DEBUG
242 #define RAY_DEBUG               0x0000
243 #endif /* RAY_DEBUG */
244
245 #include <sys/param.h>
246 #include <sys/systm.h>
247 #include <sys/limits.h>
248 #include <sys/malloc.h>
249 #include <sys/kernel.h>
250 #include <sys/module.h>
251
252 #include <machine/bus.h>
253 #include <machine/resource.h>
254 #include <sys/bus.h>
255 #include <sys/rman.h>
256
257 #include <sys/mbuf.h>
258 #include <sys/socket.h>
259 #include <sys/sockio.h>
260
261 #include <net/bpf.h>
262 #include <net/ethernet.h>
263 #include <net/if.h>
264 #include <net/if_arp.h>
265 #include <net/if_dl.h>
266 #include <net80211/ieee80211.h>
267 #include <net/if_llc.h>
268 #include <net/if_types.h>
269
270 #include <dev/pccard/pccardvar.h>
271 #include "card_if.h"
272
273 #include <dev/ray/if_rayreg.h>
274 #include <dev/ray/if_raymib.h>
275 #include <dev/ray/if_raydbg.h>
276 #include <dev/ray/if_rayvar.h>
277
278 typedef uint8_t *ieee80211_mgt_beacon_t;
279 typedef uint8_t *ieee80211_mgt_auth_t;
280
281 #define IEEE80211_AUTH_ALGORITHM(auth) \
282         ((auth)[0] | ((auth)[1] << 8))
283 #define IEEE80211_AUTH_TRANSACTION(auth) \
284         ((auth)[2] | ((auth)[3] << 8))
285 #define IEEE80211_AUTH_STATUS(auth) \
286         ((auth)[4] | ((auth)[5] << 8))
287
288 static MALLOC_DEFINE(M_RAYCOM, "raycom", "Raylink command queue entry");
289 /*
290  * Prototyping
291  */
292 static int      ray_attach              (device_t);
293 static int      ray_ccs_alloc           (struct ray_softc *sc, size_t *ccsp, char *wmesg);
294 static void     ray_ccs_fill            (struct ray_softc *sc, size_t ccs, u_int cmd);
295 static void     ray_ccs_free            (struct ray_softc *sc, size_t ccs);
296 static int      ray_ccs_tx              (struct ray_softc *sc, size_t *ccsp, size_t *bufpp);
297 static void     ray_com_ecf             (struct ray_softc *sc, struct ray_comq_entry *com);
298 static void     ray_com_ecf_done        (struct ray_softc *sc);
299 static void     ray_com_ecf_timo        (void *xsc);
300 static struct ray_comq_entry *
301                 ray_com_init            (struct ray_comq_entry *com, ray_comqfn_t function, int flags, char *mesg);
302 static struct ray_comq_entry *
303                 ray_com_malloc          (ray_comqfn_t function, int flags, char *mesg);
304 static void     ray_com_runq            (struct ray_softc *sc);
305 static int      ray_com_runq_add        (struct ray_softc *sc, struct ray_comq_entry *com[], int ncom, char *wmesg);
306 static void     ray_com_runq_done       (struct ray_softc *sc);
307 static int      ray_detach              (device_t);
308 static void     ray_init                (void *xsc);
309 static int      ray_init_user           (struct ray_softc *sc);
310 static void     ray_init_assoc          (struct ray_softc *sc, struct ray_comq_entry *com);
311 static void     ray_init_assoc_done     (struct ray_softc *sc, u_int8_t status, size_t ccs);
312 static void     ray_init_auth           (struct ray_softc *sc, struct ray_comq_entry *com);
313 static int      ray_init_auth_send      (struct ray_softc *sc, u_int8_t *dst, int sequence);
314 static void     ray_init_auth_done      (struct ray_softc *sc, u_int8_t status);
315 static void     ray_init_download       (struct ray_softc *sc, struct ray_comq_entry *com);
316 static void     ray_init_download_done  (struct ray_softc *sc, u_int8_t status, size_t ccs);
317 static void     ray_init_download_v4    (struct ray_softc *sc, struct ray_comq_entry *com);
318 static void     ray_init_download_v5    (struct ray_softc *sc, struct ray_comq_entry *com);
319 static void     ray_init_mcast          (struct ray_softc *sc, struct ray_comq_entry *com);
320 static void     ray_init_sj             (struct ray_softc *sc, struct ray_comq_entry *com);
321 static void     ray_init_sj_done        (struct ray_softc *sc, u_int8_t status, size_t ccs);
322 static void     ray_intr                (void *xsc);
323 static void     ray_intr_ccs            (struct ray_softc *sc, u_int8_t cmd, u_int8_t status, size_t ccs);
324 static void     ray_intr_rcs            (struct ray_softc *sc, u_int8_t cmd, size_t ccs);
325 static void     ray_intr_updt_errcntrs  (struct ray_softc *sc);
326 static int      ray_ioctl               (struct ifnet *ifp, u_long command, caddr_t data);
327 static void     ray_mcast               (struct ray_softc *sc, struct ray_comq_entry *com); 
328 static void     ray_mcast_done          (struct ray_softc *sc, u_int8_t status, size_t ccs); 
329 static int      ray_mcast_user          (struct ray_softc *sc); 
330 static int      ray_probe               (device_t);
331 static void     ray_promisc             (struct ray_softc *sc, struct ray_comq_entry *com); 
332 static void     ray_repparams           (struct ray_softc *sc, struct ray_comq_entry *com);
333 static void     ray_repparams_done      (struct ray_softc *sc, u_int8_t status, size_t ccs);
334 static int      ray_repparams_user      (struct ray_softc *sc, struct ray_param_req *pr);
335 static int      ray_repstats_user       (struct ray_softc *sc, struct ray_stats_req *sr);
336 static int      ray_res_alloc_am        (struct ray_softc *sc);
337 static int      ray_res_alloc_cm        (struct ray_softc *sc);
338 static int      ray_res_alloc_irq       (struct ray_softc *sc);
339 static void     ray_res_release         (struct ray_softc *sc);
340 static void     ray_rx                  (struct ray_softc *sc, size_t rcs);
341 static void     ray_rx_ctl              (struct ray_softc *sc, struct mbuf *m0);
342 static void     ray_rx_data             (struct ray_softc *sc, struct mbuf *m0, u_int8_t siglev, u_int8_t antenna);
343 static void     ray_rx_mgt              (struct ray_softc *sc, struct mbuf *m0);
344 static void     ray_rx_mgt_auth         (struct ray_softc *sc, struct mbuf *m0);
345 static void     ray_rx_mgt_beacon       (struct ray_softc *sc, struct mbuf *m0);
346 static void     ray_rx_mgt_info         (struct ray_softc *sc, struct mbuf *m0, union ieee80211_information *elements);
347 static void     ray_rx_update_cache     (struct ray_softc *sc, u_int8_t *src, u_int8_t siglev, u_int8_t antenna);
348 static void     ray_stop                (struct ray_softc *sc, struct ray_comq_entry *com);
349 static int      ray_stop_user           (struct ray_softc *sc);
350 static void     ray_tx                  (struct ifnet *ifp);
351 static void     ray_tx_done             (struct ray_softc *sc, u_int8_t status, size_t ccs);
352 static void     ray_tx_timo             (void *xsc);
353 static int      ray_tx_send             (struct ray_softc *sc, size_t ccs, int pktlen, u_int8_t *dst);
354 static size_t   ray_tx_wrhdr            (struct ray_softc *sc, size_t bufp, u_int8_t type, u_int8_t fc1, u_int8_t *addr1, u_int8_t *addr2, u_int8_t *addr3);
355 static void     ray_upparams            (struct ray_softc *sc, struct ray_comq_entry *com);
356 static void     ray_upparams_done       (struct ray_softc *sc, u_int8_t status, size_t ccs);
357 static int      ray_upparams_user       (struct ray_softc *sc, struct ray_param_req *pr);
358 static void     ray_watchdog            (struct ifnet *ifp);
359 static u_int8_t ray_tx_best_antenna     (struct ray_softc *sc, u_int8_t *dst);
360
361 #if RAY_DEBUG & RAY_DBG_COM
362 static void     ray_com_ecf_check       (struct ray_softc *sc, size_t ccs, char *mesg);
363 #endif /* RAY_DEBUG & RAY_DBG_COM */
364 #if RAY_DEBUG & RAY_DBG_MBUF
365 static void     ray_dump_mbuf           (struct ray_softc *sc, struct mbuf *m, char *s);
366 #endif /* RAY_DEBUG & RAY_DBG_MBUF */
367
368 /*
369  * PC Card (PCMCIA) driver definition
370  */
371 static device_method_t ray_methods[] = {
372         /* Device interface */
373         DEVMETHOD(device_probe,         ray_probe),
374         DEVMETHOD(device_attach,        ray_attach),
375         DEVMETHOD(device_detach,        ray_detach),
376
377         { 0, 0 }
378 };
379
380 static driver_t ray_driver = {
381         "ray",
382         ray_methods,
383         sizeof(struct ray_softc)
384 };
385
386 static devclass_t ray_devclass;
387
388 DRIVER_MODULE(ray, pccard, ray_driver, ray_devclass, 0, 0);
389
390 /* 
391  * Probe for the card by checking its startup results.
392  *
393  * Fixup any bugs/quirks for different firmware.
394  */
395 static int
396 ray_probe(device_t dev)
397 {
398         struct ray_softc *sc = device_get_softc(dev);
399         struct ray_ecf_startup_v5 *ep = &sc->sc_ecf_startup;
400         int error;
401
402         sc->dev = dev;
403         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
404
405         /*
406          * Read startup results from the card.
407          */
408         error = ray_res_alloc_cm(sc);
409         if (error)
410                 return (error);
411         error = ray_res_alloc_am(sc);
412         if (error) {
413                 ray_res_release(sc);
414                 return (error);
415         }
416         RAY_MAP_CM(sc);
417         SRAM_READ_REGION(sc, RAY_ECF_TO_HOST_BASE, ep,
418             sizeof(sc->sc_ecf_startup));
419         ray_res_release(sc);
420
421         /*
422          * Check the card is okay and work out what version we are using.
423          */
424         if (ep->e_status != RAY_ECFS_CARD_OK) {
425                 RAY_PRINTF(sc, "card failed self test 0x%b",
426                     ep->e_status, RAY_ECFS_PRINTFB);
427                 return (ENXIO);
428         }
429         if (sc->sc_version != RAY_ECFS_BUILD_4 &&
430             sc->sc_version != RAY_ECFS_BUILD_5) {
431                 RAY_PRINTF(sc, "unsupported firmware version 0x%0x",
432                     ep->e_fw_build_string);
433                 return (ENXIO);
434         }
435         RAY_DPRINTF(sc, RAY_DBG_BOOTPARAM, "found a card");
436         sc->sc_gone = 0;
437
438         /*
439          * Fixup tib size to be correct - on build 4 it is garbage
440          */
441         if (sc->sc_version == RAY_ECFS_BUILD_4 && sc->sc_tibsize == 0x55)
442                 sc->sc_tibsize = sizeof(struct ray_tx_tib);
443
444         return (0);
445 }
446
447 /*
448  * Attach the card into the kernel
449  */
450 static int
451 ray_attach(device_t dev)
452 {
453         struct ray_softc *sc = device_get_softc(dev);
454         struct ray_ecf_startup_v5 *ep = &sc->sc_ecf_startup;
455         struct ifnet *ifp;
456         size_t ccs;
457         int i, error;
458
459         ifp = sc->ifp = if_alloc(IFT_ETHER);
460         if (ifp == NULL)
461                 return (ENOSPC);
462
463         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
464
465         if ((sc == NULL) || (sc->sc_gone)) {
466                 if_free(ifp);
467                 return (ENXIO);
468         }
469
470         /*
471          * Grab the resources I need
472          */
473         error = ray_res_alloc_cm(sc);
474         if (error) {
475                 if_free(ifp);
476                 return (error);
477         }
478         error = ray_res_alloc_am(sc);
479         if (error) {
480                 if_free(ifp);
481                 ray_res_release(sc);
482                 return (error);
483         }
484         error = ray_res_alloc_irq(sc);
485         if (error) {
486                 if_free(ifp);
487                 ray_res_release(sc);
488                 return (error);
489         }
490
491         /*
492          * Reset any pending interrupts
493          */
494         RAY_HCS_CLEAR_INTR(sc);
495
496         /*
497          * Set the parameters that will survive stop/init and
498          * reset a few things on the card.
499          *
500          * Do not update these in ray_init_download's parameter setup
501          *
502          */
503         RAY_MAP_CM(sc);
504         bzero(&sc->sc_d, sizeof(struct ray_nw_param));
505         bzero(&sc->sc_c, sizeof(struct ray_nw_param));
506
507         /* Clear statistics counters */
508         sc->sc_rxoverflow = 0;
509         sc->sc_rxcksum = 0;
510         sc->sc_rxhcksum = 0;
511         sc->sc_rxnoise = 0;
512
513         /* Clear signal and antenna cache */
514         bzero(sc->sc_siglevs, sizeof(sc->sc_siglevs));
515
516         /* Set all ccs to be free */
517         bzero(sc->sc_ccsinuse, sizeof(sc->sc_ccsinuse));
518         ccs = RAY_CCS_ADDRESS(0);
519         for (i = 0; i < RAY_CCS_LAST; ccs += RAY_CCS_SIZE, i++)
520                 RAY_CCS_FREE(sc, ccs);
521
522         /*
523          * Initialise the network interface structure
524          */
525         ifp->if_softc = sc;
526         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
527         ifp->if_timer = 0;
528         ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
529             IFF_NEEDSGIANT);
530         ifp->if_hdrlen = sizeof(struct ieee80211_frame) + 
531             sizeof(struct ether_header);
532         ifp->if_baudrate = 1000000; /* Is this baud or bps ;-) */
533         ifp->if_start = ray_tx;
534         ifp->if_ioctl = ray_ioctl;
535         ifp->if_watchdog = ray_watchdog;
536         ifp->if_init = ray_init;
537         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
538
539         ether_ifattach(ifp, ep->e_station_addr);
540
541         /*
542          * Initialise the timers and driver
543          */
544         callout_handle_init(&sc->com_timerh);
545         callout_handle_init(&sc->tx_timerh);
546         TAILQ_INIT(&sc->sc_comq);
547
548         /*
549          * Print out some useful information
550          */
551         if (bootverbose || (RAY_DEBUG & RAY_DBG_BOOTPARAM)) {
552                 RAY_PRINTF(sc, "start up results");
553                 if (sc->sc_version == RAY_ECFS_BUILD_4)
554                         printf(".  Firmware version 4\n");
555                 else
556                         printf(".  Firmware version 5\n");
557                 printf(".  Status 0x%b\n", ep->e_status, RAY_ECFS_PRINTFB);
558                 printf(".  Ether address %6D\n", ep->e_station_addr, ":");
559                 if (sc->sc_version == RAY_ECFS_BUILD_4) {
560                         printf(".  Program checksum %0x\n", ep->e_resv0);
561                         printf(".  CIS checksum %0x\n", ep->e_rates[0]);
562                 } else {
563                         printf(".  (reserved word) %0x\n", ep->e_resv0);
564                         printf(".  Supported rates %8D\n", ep->e_rates, ":");
565                 }
566                 printf(".  Japan call sign %12D\n", ep->e_japan_callsign, ":");
567                 if (sc->sc_version == RAY_ECFS_BUILD_5) {
568                         printf(".  Program checksum %0x\n", ep->e_prg_cksum);
569                         printf(".  CIS checksum %0x\n", ep->e_cis_cksum);
570                         printf(".  Firmware version %0x\n",
571                             ep->e_fw_build_string);
572                         printf(".  Firmware revision %0x\n", ep->e_fw_build);
573                         printf(".  (reserved word) %0x\n", ep->e_fw_resv);
574                         printf(".  ASIC version %0x\n", ep->e_asic_version);
575                         printf(".  TIB size %0x\n", ep->e_tibsize);
576                 }
577         }
578
579         return (0);
580 }
581
582 /*
583  * Detach the card
584  *
585  * This is usually called when the card is ejected, but
586  * can be caused by a modunload of a controller driver.
587  * The idea is to reset the driver's view of the device
588  * and ensure that any driver entry points such as
589  * read and write do not hang.
590  */
591 static int
592 ray_detach(device_t dev)
593 {
594         struct ray_softc *sc = device_get_softc(dev);
595         struct ifnet *ifp = sc->ifp;
596         struct ray_comq_entry *com;
597         int s;
598
599         s = splimp();
600
601         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STOP, "");
602
603         if ((sc == NULL) || (sc->sc_gone))
604                 return (0);
605
606         /*
607          * Mark as not running and detach the interface.
608          *
609          * N.B. if_detach can trigger ioctls so we do it first and
610          * then clean the runq.
611          */
612         sc->sc_gone = 1;
613         sc->sc_c.np_havenet = 0;
614         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
615         ether_ifdetach(ifp);
616
617         /*
618          * Stop the runq and wake up anyone sleeping for us.
619          */
620         untimeout(ray_com_ecf_timo, sc, sc->com_timerh);
621         untimeout(ray_tx_timo, sc, sc->tx_timerh);
622         com = TAILQ_FIRST(&sc->sc_comq);
623         TAILQ_FOREACH(com, &sc->sc_comq, c_chain) {
624                 com->c_flags |= RAY_COM_FDETACHED;
625                 com->c_retval = 0;
626                 RAY_DPRINTF(sc, RAY_DBG_STOP, "looking at com %p %b",
627                     com, com->c_flags, RAY_COM_FLAGS_PRINTFB);
628                 if (com->c_flags & RAY_COM_FWOK) {
629                         RAY_DPRINTF(sc, RAY_DBG_STOP, "waking com %p", com);
630                         wakeup(com->c_wakeup);
631                 }
632         }
633         
634         /*
635          * Release resources
636          */
637         ray_res_release(sc);
638         if_free(ifp);
639         RAY_DPRINTF(sc, RAY_DBG_STOP, "unloading complete");
640
641         splx(s);
642
643         return (0);
644 }
645
646 /*
647  * Network ioctl request.
648  */
649 static int
650 ray_ioctl(register struct ifnet *ifp, u_long command, caddr_t data)
651 {
652         struct ray_softc *sc = ifp->if_softc;
653         struct ray_param_req pr;
654         struct ray_stats_req sr;
655         struct ifreq *ifr = (struct ifreq *)data;
656         int s, error, error2;
657
658         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_IOCTL, "");
659
660         if ((sc == NULL) || (sc->sc_gone))
661                 return (ENXIO);
662
663         error = error2 = 0;
664         s = splimp();
665
666         switch (command) {
667         case SIOCSIFFLAGS:
668                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "SIFFLAGS 0x%0x", ifp->if_flags);
669                 /*
670                  * If the interface is marked up we call ray_init_user.
671                  * This will deal with mcast and promisc flags as well as
672                  * initialising the hardware if it needs it.
673                  */
674                 if (ifp->if_flags & IFF_UP)
675                         error = ray_init_user(sc);
676                 else
677                         error = ray_stop_user(sc);
678                 break;
679
680         case SIOCADDMULTI:
681         case SIOCDELMULTI:
682                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "ADDMULTI/DELMULTI");
683                 error = ray_mcast_user(sc);
684                 break;
685
686         case SIOCSRAYPARAM:
687                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "SRAYPARAM");
688                 if ((error = copyin(ifr->ifr_data, &pr, sizeof(pr))))
689                         break;
690                 error = ray_upparams_user(sc, &pr);
691                 error2 = copyout(&pr, ifr->ifr_data, sizeof(pr));
692                 error = error2 ? error2 : error;
693                 break;
694
695         case SIOCGRAYPARAM:
696                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GRAYPARAM");
697                 if ((error = copyin(ifr->ifr_data, &pr, sizeof(pr))))
698                         break;
699                 error = ray_repparams_user(sc, &pr);
700                 error2 = copyout(&pr, ifr->ifr_data, sizeof(pr));
701                 error = error2 ? error2 : error;
702                 break;
703
704         case SIOCGRAYSTATS:
705                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GRAYSTATS");
706                 error = ray_repstats_user(sc, &sr);
707                 error2 = copyout(&sr, ifr->ifr_data, sizeof(sr));
708                 error = error2 ? error2 : error;
709                 break;
710
711         case SIOCGRAYSIGLEV:
712                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GRAYSIGLEV");
713                 error = copyout(sc->sc_siglevs, ifr->ifr_data,
714                     sizeof(sc->sc_siglevs));
715                 break;
716
717         case SIOCGIFFLAGS:
718                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GIFFLAGS");
719                 error = EINVAL;
720                 break;
721
722         case SIOCGIFMETRIC:
723                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GIFMETRIC");
724                 error = EINVAL;
725                 break;
726
727         case SIOCGIFMTU:
728                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GIFMTU");
729                 error = EINVAL;
730                 break;
731
732         case SIOCGIFPHYS:
733                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GIFPYHS");
734                 error = EINVAL;
735                 break;
736
737         case SIOCSIFMEDIA:
738                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "SIFMEDIA");
739                 error = EINVAL;
740                 break;
741
742         case SIOCGIFMEDIA:
743                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "GIFMEDIA");
744                 error = EINVAL;
745                 break;
746
747         default:
748                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "OTHER (pass to ether)");
749                 error = ether_ioctl(ifp, command, data);
750                 break;
751
752         }
753
754         splx(s);
755
756         return (error);
757 }
758
759 /*
760  * Ethernet layer entry to ray_init - discard errors
761  */
762 static void
763 ray_init(void *xsc)
764 {
765         struct ray_softc *sc = (struct ray_softc *)xsc;
766
767         ray_init_user(sc);
768 }
769
770 /*
771  * User land entry to network initialisation and changes in interface flags.
772  * 
773  * We do a very little work here, just creating runq entries to
774  * processes the actions needed to cope with interface flags. We do it
775  * this way in case there are runq entries outstanding from earlier
776  * ioctls that modify the interface flags.
777  *
778  * Returns values are either 0 for success, a varity of resource allocation
779  * failures or errors in the command sent to the card.
780  *
781  * Note, IFF_DRV_RUNNING is eventually set by init_sj_done or init_assoc_done
782  */
783 static int
784 ray_init_user(struct ray_softc *sc)
785 {
786         struct ray_comq_entry *com[6];
787         int error, ncom;
788
789         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
790
791         /*
792          * Create the following runq entries to bring the card up.
793          *
794          *              init_download   - download the network to the card
795          *              init_mcast      - reset multicast list
796          *              init_sj         - find or start a BSS
797          *              init_auth       - authenticate with an ESSID if needed
798          *              init_assoc      - associate with an ESSID if needed
799          *
800          * They are only actually executed if the card is not running.
801          * We may enter this routine from a simple change of IP
802          * address and do not need to get the card to do these things.
803          * However, we cannot perform the check here as there may be
804          * commands in the runq that change the IFF_DRV_RUNNING state of
805          * the interface.
806          */
807         ncom = 0;
808         com[ncom++] = RAY_COM_MALLOC(ray_init_download, RAY_COM_FCHKRUNNING);
809         com[ncom++] = RAY_COM_MALLOC(ray_init_mcast, RAY_COM_FCHKRUNNING);
810         com[ncom++] = RAY_COM_MALLOC(ray_init_sj, RAY_COM_FCHKRUNNING);
811         com[ncom++] = RAY_COM_MALLOC(ray_init_auth, RAY_COM_FCHKRUNNING);
812         com[ncom++] = RAY_COM_MALLOC(ray_init_assoc, RAY_COM_FCHKRUNNING);
813
814         /*
815          * Create runq entries to process flags
816          *
817          *              promisc         - set/reset PROMISC and ALLMULTI flags
818          *
819          * They are only actually executed if the card is running
820          */
821         com[ncom++] = RAY_COM_MALLOC(ray_promisc, 0);
822
823         RAY_COM_RUNQ(sc, com, ncom, "rayinit", error);
824
825         /* XXX no real error processing from anything yet! */
826
827         RAY_COM_FREE(com, ncom);
828
829         return (error);
830 }
831
832 /*
833  * Runq entry for resetting driver and downloading start up structures to card
834  */
835 static void
836 ray_init_download(struct ray_softc *sc, struct ray_comq_entry *com)
837 {
838         struct ifnet *ifp = sc->ifp;
839
840         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
841
842         /* If the card already running we might not need to download */
843         RAY_COM_CHKRUNNING(sc, com, ifp);
844
845         /*
846          * Reset instance variables
847          *
848          * The first set are network parameters that are read back when
849          * the card starts or joins the network.
850          *
851          * The second set are network parameters that are downloaded to
852          * the card.
853          *
854          * The third set are driver parameters.
855          *
856          * All of the variables in these sets can be updated by the
857          * card or ioctls.
858          *
859          */
860         sc->sc_d.np_upd_param = 0;
861         bzero(sc->sc_d.np_bss_id, ETHER_ADDR_LEN);
862         sc->sc_d.np_inited = 0;
863         sc->sc_d.np_def_txrate = RAY_MIB_BASIC_RATE_SET_DEFAULT;
864         sc->sc_d.np_encrypt = 0;
865
866         bzero(sc->sc_d.np_ssid, IEEE80211_NWID_LEN);
867         if (sc->sc_version == RAY_ECFS_BUILD_4) {
868                 sc->sc_d.np_net_type = RAY_MIB_NET_TYPE_V4;
869                 strncpy(sc->sc_d.np_ssid, RAY_MIB_SSID_V4, IEEE80211_NWID_LEN);
870                 sc->sc_d.np_ap_status = RAY_MIB_AP_STATUS_V4;
871                 sc->sc_d.np_framing = RAY_FRAMING_ENCAPSULATION;
872         } else {
873                 sc->sc_d.np_net_type = RAY_MIB_NET_TYPE_V5;
874                 strncpy(sc->sc_d.np_ssid, RAY_MIB_SSID_V5, IEEE80211_NWID_LEN);
875                 sc->sc_d.np_ap_status = RAY_MIB_AP_STATUS_V5;
876                 sc->sc_d.np_framing = RAY_FRAMING_TRANSLATION;
877         }
878         sc->sc_d.np_priv_start = RAY_MIB_PRIVACY_MUST_START_DEFAULT;
879         sc->sc_d.np_priv_join = RAY_MIB_PRIVACY_CAN_JOIN_DEFAULT;
880         sc->sc_d.np_promisc = !!(ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI));
881
882 /* XXX this is a hack whilst I transition the code. The instance
883  * XXX variables above should be set somewhere else. This is needed for
884  * XXX start_join */
885 bcopy(&sc->sc_d, &com->c_desired, sizeof(struct ray_nw_param));
886             
887         /*
888          * Download the right firmware defaults
889          */
890         if (sc->sc_version == RAY_ECFS_BUILD_4)
891                 ray_init_download_v4(sc, com);
892         else
893                 ray_init_download_v5(sc, com);
894
895         /*
896          * Kick the card
897          */
898         ray_ccs_fill(sc, com->c_ccs, RAY_CMD_DOWNLOAD_PARAMS);
899         ray_com_ecf(sc, com);
900 }
901
902 #define PUT2(p, v)      \
903     do { (p)[0] = ((v >> 8) & 0xff); (p)[1] = (v & 0xff); } while(0)
904 /*
905  * Firmware version 4 defaults - see if_raymib.h for details
906  */
907 static void
908 ray_init_download_v4(struct ray_softc *sc, struct ray_comq_entry *com)
909 {
910         struct ray_mib_4 ray_mib_4_default;
911
912         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
913         RAY_MAP_CM(sc);
914
915 #define MIB4(m)         ray_mib_4_default.m
916
917         MIB4(mib_net_type)              = com->c_desired.np_net_type;
918         MIB4(mib_ap_status)             = com->c_desired.np_ap_status;
919         bcopy(com->c_desired.np_ssid, MIB4(mib_ssid), IEEE80211_NWID_LEN);
920         MIB4(mib_scan_mode)             = RAY_MIB_SCAN_MODE_V4;
921         MIB4(mib_apm_mode)              = RAY_MIB_APM_MODE_V4;
922         bcopy(sc->sc_station_addr, MIB4(mib_mac_addr), ETHER_ADDR_LEN);
923    PUT2(MIB4(mib_frag_thresh),            RAY_MIB_FRAG_THRESH_V4);
924    PUT2(MIB4(mib_dwell_time),             RAY_MIB_DWELL_TIME_V4);
925    PUT2(MIB4(mib_beacon_period),          RAY_MIB_BEACON_PERIOD_V4);
926         MIB4(mib_dtim_interval)         = RAY_MIB_DTIM_INTERVAL_V4;
927         MIB4(mib_max_retry)             = RAY_MIB_MAX_RETRY_V4;
928         MIB4(mib_ack_timo)              = RAY_MIB_ACK_TIMO_V4;
929         MIB4(mib_sifs)                  = RAY_MIB_SIFS_V4;
930         MIB4(mib_difs)                  = RAY_MIB_DIFS_V4;
931         MIB4(mib_pifs)                  = RAY_MIB_PIFS_V4;
932    PUT2(MIB4(mib_rts_thresh),             RAY_MIB_RTS_THRESH_V4);
933    PUT2(MIB4(mib_scan_dwell),             RAY_MIB_SCAN_DWELL_V4);
934    PUT2(MIB4(mib_scan_max_dwell),         RAY_MIB_SCAN_MAX_DWELL_V4);
935         MIB4(mib_assoc_timo)            = RAY_MIB_ASSOC_TIMO_V4;
936         MIB4(mib_adhoc_scan_cycle)      = RAY_MIB_ADHOC_SCAN_CYCLE_V4;
937         MIB4(mib_infra_scan_cycle)      = RAY_MIB_INFRA_SCAN_CYCLE_V4;
938         MIB4(mib_infra_super_scan_cycle)
939                                         = RAY_MIB_INFRA_SUPER_SCAN_CYCLE_V4;
940         MIB4(mib_promisc)               = com->c_desired.np_promisc;
941    PUT2(MIB4(mib_uniq_word),              RAY_MIB_UNIQ_WORD_V4);
942         MIB4(mib_slot_time)             = RAY_MIB_SLOT_TIME_V4;
943         MIB4(mib_roam_low_snr_thresh)   = RAY_MIB_ROAM_LOW_SNR_THRESH_V4;
944         MIB4(mib_low_snr_count)         = RAY_MIB_LOW_SNR_COUNT_V4;
945         MIB4(mib_infra_missed_beacon_count)
946                                         = RAY_MIB_INFRA_MISSED_BEACON_COUNT_V4;
947         MIB4(mib_adhoc_missed_beacon_count)     
948                                         = RAY_MIB_ADHOC_MISSED_BEACON_COUNT_V4;
949         MIB4(mib_country_code)          = RAY_MIB_COUNTRY_CODE_V4;
950         MIB4(mib_hop_seq)               = RAY_MIB_HOP_SEQ_V4;
951         MIB4(mib_hop_seq_len)           = RAY_MIB_HOP_SEQ_LEN_V4;
952         MIB4(mib_cw_max)                = RAY_MIB_CW_MAX_V4;
953         MIB4(mib_cw_min)                = RAY_MIB_CW_MIN_V4;
954         MIB4(mib_noise_filter_gain)     = RAY_MIB_NOISE_FILTER_GAIN_DEFAULT;
955         MIB4(mib_noise_limit_offset)    = RAY_MIB_NOISE_LIMIT_OFFSET_DEFAULT;
956         MIB4(mib_rssi_thresh_offset)    = RAY_MIB_RSSI_THRESH_OFFSET_DEFAULT;
957         MIB4(mib_busy_thresh_offset)    = RAY_MIB_BUSY_THRESH_OFFSET_DEFAULT;
958         MIB4(mib_sync_thresh)           = RAY_MIB_SYNC_THRESH_DEFAULT;
959         MIB4(mib_test_mode)             = RAY_MIB_TEST_MODE_DEFAULT;
960         MIB4(mib_test_min_chan)         = RAY_MIB_TEST_MIN_CHAN_DEFAULT;
961         MIB4(mib_test_max_chan)         = RAY_MIB_TEST_MAX_CHAN_DEFAULT;
962 #undef MIB4
963
964         SRAM_WRITE_REGION(sc, RAY_HOST_TO_ECF_BASE,
965             &ray_mib_4_default, sizeof(ray_mib_4_default));
966 }
967
968 /*
969  * Firmware version 5 defaults - see if_raymib.h for details
970  */
971 static void
972 ray_init_download_v5(struct ray_softc *sc, struct ray_comq_entry *com)
973 {
974         struct ray_mib_5 ray_mib_5_default;
975
976         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
977         RAY_MAP_CM(sc);
978
979 #define MIB5(m)         ray_mib_5_default.m
980         MIB5(mib_net_type)              = com->c_desired.np_net_type;
981         MIB5(mib_ap_status)             = com->c_desired.np_ap_status;
982         bcopy(com->c_desired.np_ssid, MIB5(mib_ssid), IEEE80211_NWID_LEN);
983         MIB5(mib_scan_mode)             = RAY_MIB_SCAN_MODE_V5;
984         MIB5(mib_apm_mode)              = RAY_MIB_APM_MODE_V5;
985         bcopy(sc->sc_station_addr, MIB5(mib_mac_addr), ETHER_ADDR_LEN);
986    PUT2(MIB5(mib_frag_thresh),            RAY_MIB_FRAG_THRESH_V5);
987    PUT2(MIB5(mib_dwell_time),             RAY_MIB_DWELL_TIME_V5);
988    PUT2(MIB5(mib_beacon_period),          RAY_MIB_BEACON_PERIOD_V5);
989         MIB5(mib_dtim_interval)         = RAY_MIB_DTIM_INTERVAL_V5;
990         MIB5(mib_max_retry)             = RAY_MIB_MAX_RETRY_V5;
991         MIB5(mib_ack_timo)              = RAY_MIB_ACK_TIMO_V5;
992         MIB5(mib_sifs)                  = RAY_MIB_SIFS_V5;
993         MIB5(mib_difs)                  = RAY_MIB_DIFS_V5;
994         MIB5(mib_pifs)                  = RAY_MIB_PIFS_V5;
995    PUT2(MIB5(mib_rts_thresh),             RAY_MIB_RTS_THRESH_V5);
996    PUT2(MIB5(mib_scan_dwell),             RAY_MIB_SCAN_DWELL_V5);
997    PUT2(MIB5(mib_scan_max_dwell),         RAY_MIB_SCAN_MAX_DWELL_V5);
998         MIB5(mib_assoc_timo)            = RAY_MIB_ASSOC_TIMO_V5;
999         MIB5(mib_adhoc_scan_cycle)      = RAY_MIB_ADHOC_SCAN_CYCLE_V5;
1000         MIB5(mib_infra_scan_cycle)      = RAY_MIB_INFRA_SCAN_CYCLE_V5;
1001         MIB5(mib_infra_super_scan_cycle)
1002                                         = RAY_MIB_INFRA_SUPER_SCAN_CYCLE_V5;
1003         MIB5(mib_promisc)               = com->c_desired.np_promisc;
1004    PUT2(MIB5(mib_uniq_word),              RAY_MIB_UNIQ_WORD_V5);
1005         MIB5(mib_slot_time)             = RAY_MIB_SLOT_TIME_V5;
1006         MIB5(mib_roam_low_snr_thresh)   = RAY_MIB_ROAM_LOW_SNR_THRESH_V5;
1007         MIB5(mib_low_snr_count) = RAY_MIB_LOW_SNR_COUNT_V5;
1008         MIB5(mib_infra_missed_beacon_count)
1009                                         = RAY_MIB_INFRA_MISSED_BEACON_COUNT_V5;
1010         MIB5(mib_adhoc_missed_beacon_count)
1011                                         = RAY_MIB_ADHOC_MISSED_BEACON_COUNT_V5;
1012         MIB5(mib_country_code)          = RAY_MIB_COUNTRY_CODE_V5;
1013         MIB5(mib_hop_seq)               = RAY_MIB_HOP_SEQ_V5;
1014         MIB5(mib_hop_seq_len)           = RAY_MIB_HOP_SEQ_LEN_V5;
1015    PUT2(MIB5(mib_cw_max),                 RAY_MIB_CW_MAX_V5);
1016    PUT2(MIB5(mib_cw_min),                 RAY_MIB_CW_MIN_V5);
1017         MIB5(mib_noise_filter_gain)     = RAY_MIB_NOISE_FILTER_GAIN_DEFAULT;
1018         MIB5(mib_noise_limit_offset)    = RAY_MIB_NOISE_LIMIT_OFFSET_DEFAULT;
1019         MIB5(mib_rssi_thresh_offset)    = RAY_MIB_RSSI_THRESH_OFFSET_DEFAULT;
1020         MIB5(mib_busy_thresh_offset)    = RAY_MIB_BUSY_THRESH_OFFSET_DEFAULT;
1021         MIB5(mib_sync_thresh)           = RAY_MIB_SYNC_THRESH_DEFAULT;
1022         MIB5(mib_test_mode)             = RAY_MIB_TEST_MODE_DEFAULT;
1023         MIB5(mib_test_min_chan)         = RAY_MIB_TEST_MIN_CHAN_DEFAULT;
1024         MIB5(mib_test_max_chan)         = RAY_MIB_TEST_MAX_CHAN_DEFAULT;
1025         MIB5(mib_allow_probe_resp)      = RAY_MIB_ALLOW_PROBE_RESP_DEFAULT;
1026         MIB5(mib_privacy_must_start)    = com->c_desired.np_priv_start;
1027         MIB5(mib_privacy_can_join)      = com->c_desired.np_priv_join;
1028         MIB5(mib_basic_rate_set[0])     = com->c_desired.np_def_txrate;
1029 #undef MIB5
1030
1031         SRAM_WRITE_REGION(sc, RAY_HOST_TO_ECF_BASE,
1032             &ray_mib_5_default, sizeof(ray_mib_5_default));
1033 }
1034 #undef PUT2
1035
1036 /*
1037  * Download completion routine
1038  */
1039 static void
1040 ray_init_download_done(struct ray_softc *sc, u_int8_t status, size_t ccs)
1041 {
1042         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
1043         RAY_COM_CHECK(sc, ccs);
1044
1045         RAY_CCSERR(sc, status, if_oerrors); /* XXX error counter */
1046
1047         ray_com_ecf_done(sc);
1048 }
1049
1050 /*
1051  * Runq entry to empty the multicast filter list
1052  */
1053 static void
1054 ray_init_mcast(struct ray_softc *sc, struct ray_comq_entry *com)
1055 {
1056         struct ifnet *ifp = sc->ifp;
1057
1058         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
1059         RAY_MAP_CM(sc);
1060
1061         /* If the card already running we might not need to reset the list */
1062         RAY_COM_CHKRUNNING(sc, com, ifp);
1063
1064         /*
1065          * Kick the card
1066          */
1067         ray_ccs_fill(sc, com->c_ccs, RAY_CMD_UPDATE_MCAST);
1068         SRAM_WRITE_FIELD_1(sc, com->c_ccs, ray_cmd_update_mcast, c_nmcast, 0);
1069
1070         ray_com_ecf(sc, com);
1071 }
1072
1073 /*
1074  * Runq entry to starting or joining a network
1075  */
1076 static void
1077 ray_init_sj(struct ray_softc *sc, struct ray_comq_entry *com)
1078 {
1079         struct ifnet *ifp = sc->ifp;
1080         struct ray_net_params np;
1081         int update;
1082
1083         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
1084         RAY_MAP_CM(sc);
1085
1086         /* If the card already running we might not need to start the n/w */
1087         RAY_COM_CHKRUNNING(sc, com, ifp);
1088
1089         /*
1090          * Set up the right start or join command and determine
1091          * whether we should tell the card about a change in operating
1092          * parameters.
1093          */
1094         sc->sc_c.np_havenet = 0;
1095         if (sc->sc_d.np_net_type == RAY_MIB_NET_TYPE_ADHOC)
1096                 ray_ccs_fill(sc, com->c_ccs, RAY_CMD_START_NET);
1097         else
1098                 ray_ccs_fill(sc, com->c_ccs, RAY_CMD_JOIN_NET);
1099
1100         update = 0;
1101         if (sc->sc_c.np_net_type != sc->sc_d.np_net_type)
1102                 update++;
1103         if (bcmp(sc->sc_c.np_ssid, sc->sc_d.np_ssid, IEEE80211_NWID_LEN))
1104                 update++;
1105         if (sc->sc_c.np_priv_join != sc->sc_d.np_priv_join)
1106                 update++;
1107         if (sc->sc_c.np_priv_start != sc->sc_d.np_priv_start)
1108                 update++;
1109         RAY_DPRINTF(sc, RAY_DBG_STARTJOIN,
1110             "%s updating nw params", update?"is":"not");
1111         if (update) {
1112                 bzero(&np, sizeof(np));
1113                 np.p_net_type = sc->sc_d.np_net_type;
1114                 bcopy(sc->sc_d.np_ssid, np.p_ssid,  IEEE80211_NWID_LEN);
1115                 np.p_privacy_must_start = sc->sc_d.np_priv_start;
1116                 np.p_privacy_can_join = sc->sc_d.np_priv_join;
1117                 SRAM_WRITE_REGION(sc, RAY_HOST_TO_ECF_BASE, &np, sizeof(np));
1118                 SRAM_WRITE_FIELD_1(sc, com->c_ccs, ray_cmd_net, c_upd_param, 1);
1119         } else
1120                 SRAM_WRITE_FIELD_1(sc, com->c_ccs, ray_cmd_net, c_upd_param, 0);
1121
1122         /*
1123          * Kick the card
1124          */
1125         ray_com_ecf(sc, com);
1126 }
1127
1128 /*
1129  * Complete start command or intermediate step in assoc command
1130  */
1131 static void
1132 ray_init_sj_done(struct ray_softc *sc, u_int8_t status, size_t ccs)
1133 {
1134         struct ifnet *ifp = sc->ifp;
1135
1136         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
1137         RAY_MAP_CM(sc);
1138         RAY_COM_CHECK(sc, ccs);
1139
1140         RAY_CCSERR(sc, status, if_oerrors); /* XXX error counter */
1141
1142         /*
1143          * Read back network parameters that the ECF sets
1144          */
1145         SRAM_READ_REGION(sc, ccs, &sc->sc_c.p_1, sizeof(struct ray_cmd_net));
1146
1147         /* Adjust values for buggy firmware */
1148         if (sc->sc_c.np_inited == 0x55)
1149                 sc->sc_c.np_inited = 0;
1150         if (sc->sc_c.np_def_txrate == 0x55)
1151                 sc->sc_c.np_def_txrate = sc->sc_d.np_def_txrate;
1152         if (sc->sc_c.np_encrypt == 0x55)
1153                 sc->sc_c.np_encrypt = sc->sc_d.np_encrypt;
1154
1155         /*
1156          * Update our local state if we updated the network parameters
1157          * when the START_NET or JOIN_NET was issued.
1158          */
1159         if (sc->sc_c.np_upd_param) {
1160                 RAY_DPRINTF(sc, RAY_DBG_STARTJOIN, "updated parameters");
1161                 SRAM_READ_REGION(sc, RAY_HOST_TO_ECF_BASE,
1162                     &sc->sc_c.p_2, sizeof(struct ray_net_params));
1163         }
1164
1165         /*
1166          * Hurrah! The network is now active.
1167          *
1168          * Clearing IFF_DRV_OACTIVE will ensure that the system will send us
1169          * packets. Just before we return from the interrupt context
1170          * we check to see if packets have been queued.
1171          */
1172         if (SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd) == RAY_CMD_START_NET) {
1173                 sc->sc_c.np_havenet = 1;
1174                 sc->sc_c.np_framing = sc->sc_d.np_framing;
1175                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1176                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1177         }
1178
1179         ray_com_ecf_done(sc);
1180 }
1181
1182 /*
1183  * Runq entry to authenticate with an access point or another station
1184  */
1185 static void
1186 ray_init_auth(struct ray_softc *sc, struct ray_comq_entry *com)
1187 {
1188         struct ifnet *ifp = sc->ifp;
1189
1190         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN | RAY_DBG_AUTH, "");
1191
1192         /* If card already running we might not need to authenticate */
1193         RAY_COM_CHKRUNNING(sc, com, ifp);
1194
1195         /*
1196          * Don't do anything if we are not in a managed network
1197          *
1198          * XXX V4 adhoc does not need this, V5 adhoc unknown
1199          */
1200         if (sc->sc_c.np_net_type != RAY_MIB_NET_TYPE_INFRA) {
1201                 ray_com_runq_done(sc);
1202                 return;
1203         }
1204
1205 /*
1206  * XXX_AUTH need to think of run queue when doing auths from request i.e. would
1207  * XXX_AUTH need to have auth at top of runq?
1208  * XXX_AUTH ditto for sending any auth response packets...what about timeouts?
1209  */
1210
1211         /*
1212          * Kick the card
1213          */
1214 /* XXX_AUTH check exit status and retry or fail as we can't associate without this */
1215         ray_init_auth_send(sc, sc->sc_c.np_bss_id, IEEE80211_AUTH_OPEN_REQUEST);
1216 }
1217
1218 /*
1219  * Build and send an authentication packet
1220  *
1221  * If an error occurs, returns 1 else returns 0.
1222  */
1223 static int
1224 ray_init_auth_send(struct ray_softc *sc, u_int8_t *dst, int sequence)
1225 {
1226         size_t ccs, bufp;
1227         int pktlen = 0;
1228
1229         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN | RAY_DBG_AUTH, "");
1230
1231         /* Get a control block */
1232         if (ray_ccs_tx(sc, &ccs, &bufp)) {
1233                 RAY_RECERR(sc, "could not obtain a ccs");
1234                 return (1);
1235         }
1236
1237         /* Fill the header in */
1238         bufp = ray_tx_wrhdr(sc, bufp,
1239             IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_AUTH,
1240             IEEE80211_FC1_DIR_NODS,
1241             dst,
1242             IF_LLADDR(sc->ifp),
1243             sc->sc_c.np_bss_id);
1244
1245         /* Add algorithm number */
1246         SRAM_WRITE_1(sc, bufp + pktlen++, IEEE80211_AUTH_ALG_OPEN);
1247         SRAM_WRITE_1(sc, bufp + pktlen++, 0);
1248
1249         /* Add sequence number */
1250         SRAM_WRITE_1(sc, bufp + pktlen++, sequence);
1251         SRAM_WRITE_1(sc, bufp + pktlen++, 0);
1252
1253         /* Add status code */
1254         SRAM_WRITE_1(sc, bufp + pktlen++, 0);
1255         SRAM_WRITE_1(sc, bufp + pktlen++, 0);
1256         pktlen += sizeof(struct ieee80211_frame);
1257
1258         return (ray_tx_send(sc, ccs, pktlen, dst));
1259 }
1260
1261 /*
1262  * Complete authentication runq
1263  */
1264 static void
1265 ray_init_auth_done(struct ray_softc *sc, u_int8_t status)
1266 {
1267         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN | RAY_DBG_AUTH, "");
1268
1269         if (status != IEEE80211_STATUS_SUCCESS)
1270                 RAY_RECERR(sc, "authentication failed with status %d", status);
1271 /*
1272  * XXX_AUTH retry? if not just recall ray_init_auth_send and dont clear runq?
1273  * XXX_AUTH association requires that authenitcation is successful
1274  * XXX_AUTH before we associate, and the runq is the only way to halt the
1275  * XXX_AUTH progress of associate.
1276  * XXX_AUTH In this case I might not need the RAY_AUTH_NEEDED state
1277  */
1278         ray_com_runq_done(sc);
1279 }
1280
1281 /*
1282  * Runq entry to starting an association with an access point
1283  */
1284 static void
1285 ray_init_assoc(struct ray_softc *sc, struct ray_comq_entry *com)
1286 {
1287         struct ifnet *ifp = sc->ifp;
1288
1289         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
1290
1291         /* If the card already running we might not need to associate */
1292         RAY_COM_CHKRUNNING(sc, com, ifp);
1293
1294         /*
1295          * Don't do anything if we are not in a managed network
1296          */
1297         if (sc->sc_c.np_net_type != RAY_MIB_NET_TYPE_INFRA) {
1298                 ray_com_runq_done(sc);
1299                 return;
1300         }
1301
1302         /*
1303          * Kick the card
1304          */
1305         ray_ccs_fill(sc, com->c_ccs, RAY_CMD_START_ASSOC);
1306         ray_com_ecf(sc, com);
1307 }
1308
1309 /*
1310  * Complete association
1311  */
1312 static void
1313 ray_init_assoc_done(struct ray_softc *sc, u_int8_t status, size_t ccs)
1314 {
1315         struct ifnet *ifp = sc->ifp;
1316
1317         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
1318         RAY_COM_CHECK(sc, ccs);
1319
1320         RAY_CCSERR(sc, status, if_oerrors); /* XXX error counter */
1321         
1322         /*
1323          * Hurrah! The network is now active.
1324          *
1325          * Clearing IFF_DRV_OACTIVE will ensure that the system will send us
1326          * packets. Just before we return from the interrupt context
1327          * we check to see if packets have been queued.
1328          */
1329         sc->sc_c.np_havenet = 1;
1330         sc->sc_c.np_framing = sc->sc_d.np_framing;
1331         ifp->if_drv_flags |= IFF_DRV_RUNNING;
1332         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1333
1334         ray_com_ecf_done(sc);
1335 }
1336
1337 /*
1338  * Network stop.
1339  *
1340  * Inhibit card - if we can't prevent reception then do not worry;
1341  * stopping a NIC only guarantees no TX.
1342  *
1343  * The change to the interface flags is done via the runq so that any
1344  * existing commands can execute normally.
1345  */
1346 static int
1347 ray_stop_user(struct ray_softc *sc)
1348 {
1349         struct ray_comq_entry *com[1];
1350         int error, ncom;
1351
1352         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STOP, "");
1353
1354         /*
1355          * Schedule the real stop routine
1356          */
1357         ncom = 0;
1358         com[ncom++] = RAY_COM_MALLOC(ray_stop, 0);
1359
1360         RAY_COM_RUNQ(sc, com, ncom, "raystop", error);
1361
1362         /* XXX no real error processing from anything yet! */
1363
1364         RAY_COM_FREE(com, ncom);
1365
1366         return (error);
1367 }
1368
1369 /*
1370  * Runq entry for stopping the interface activity
1371  */
1372 static void
1373 ray_stop(struct ray_softc *sc, struct ray_comq_entry *com)
1374 {
1375         struct ifnet *ifp = sc->ifp;
1376         struct mbuf *m;
1377
1378         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STOP, "");
1379
1380         /*
1381          * Mark as not running and drain output queue
1382          */
1383         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1384         ifp->if_timer = 0;
1385         for (;;) {
1386                 IF_DEQUEUE(&ifp->if_snd, m);
1387                 if (m == NULL)
1388                         break;
1389                 m_freem(m);
1390         }
1391
1392         ray_com_runq_done(sc);
1393 }
1394
1395 static void
1396 ray_watchdog(struct ifnet *ifp)
1397 {
1398         struct ray_softc *sc = ifp->if_softc;
1399
1400         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
1401         RAY_MAP_CM(sc);
1402
1403         if ((sc == NULL) || (sc->sc_gone))
1404                 return;
1405
1406         RAY_PRINTF(sc, "watchdog timeout");
1407 }
1408
1409 /*
1410  * Transmit packet handling
1411  */
1412
1413 /*
1414  * Send a packet.
1415  *
1416  * We make two assumptions here:
1417  *  1) That the current priority is set to splimp _before_ this code
1418  *     is called *and* is returned to the appropriate priority after
1419  *     return
1420  *  2) That the IFF_DRV_OACTIVE flag is checked before this code is called
1421  *     (i.e. that the output part of the interface is idle)
1422  *
1423  * A simple one packet at a time TX routine is used - we don't bother
1424  * chaining TX buffers. Performance is sufficient to max out the
1425  * wireless link on a P75.
1426  *
1427  * AST J30 Windows 95A (100MHz Pentium) to
1428  *   Libretto 50CT FreeBSD-3.1 (75MHz Pentium)          167.37kB/s
1429  *   Nonname box FreeBSD-3.4 (233MHz AMD K6)            161.82kB/s
1430  *
1431  * Libretto 50CT FreeBSD-3.1 (75MHz Pentium) to
1432  *   AST J30 Windows 95A (100MHz Pentium)               167.37kB/s
1433  *   Nonname box FreeBSD-3.4 (233MHz AMD K6)            161.38kB/s
1434  *
1435  * Given that 160kB/s is saturating the 2Mb/s wireless link we
1436  * are about there.
1437  *
1438  * In short I'm happy that the added complexity of chaining TX
1439  * packets together isn't worth it for my machines.
1440  */
1441 static void
1442 ray_tx(struct ifnet *ifp)
1443 {
1444         struct ray_softc *sc = ifp->if_softc;
1445         struct mbuf *m0, *m;
1446         struct ether_header *eh;
1447         struct llc *llc;
1448         size_t ccs, bufp;
1449         int pktlen, len;
1450
1451         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_TX, "");
1452         RAY_MAP_CM(sc);
1453
1454         /*
1455          * Some simple checks first - some are overkill
1456          */
1457         if ((sc == NULL) || (sc->sc_gone))
1458                 return;
1459         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1460                 RAY_RECERR(sc, "cannot transmit - not running");
1461                 return;
1462         }
1463         if (!sc->sc_c.np_havenet) {
1464                 RAY_RECERR(sc, "cannot transmit - no network");
1465                 return;
1466         }
1467         if (!RAY_ECF_READY(sc)) {
1468                 /* Can't assume that the ECF is busy because of this driver */
1469                 if ((sc->tx_timerh.callout == NULL) ||
1470                     (!callout_active(sc->tx_timerh.callout))) {
1471                         sc->tx_timerh =
1472                             timeout(ray_tx_timo, sc, RAY_TX_TIMEOUT);
1473                         return;
1474                     }
1475         } else
1476                 untimeout(ray_tx_timo, sc, sc->tx_timerh);
1477
1478         /*
1479          * We find a ccs before we process the mbuf so that we are sure it
1480          * is worthwhile processing the packet. All errors in the mbuf
1481          * processing are either errors in the mbuf or gross configuration
1482          * errors and the packet wouldn't get through anyway.
1483          */
1484         if (ray_ccs_tx(sc, &ccs, &bufp)) {
1485                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1486                 return;
1487         }
1488     
1489         /*
1490          * Get the mbuf and process it - we have to remember to free the
1491          * ccs if there are any errors.
1492          */
1493         IF_DEQUEUE(&ifp->if_snd, m0);
1494         if (m0 == NULL) {
1495                 RAY_CCS_FREE(sc, ccs);
1496                 return;
1497         }
1498
1499         pktlen = m0->m_pkthdr.len;
1500         if (pktlen > ETHER_MAX_LEN - ETHER_CRC_LEN) {
1501                 RAY_RECERR(sc, "mbuf too long %d", pktlen);
1502                 RAY_CCS_FREE(sc, ccs);
1503                 ifp->if_oerrors++;
1504                 m_freem(m0);
1505                 return;
1506         }
1507
1508         m0 = m_pullup(m0, sizeof(struct ether_header));
1509         if (m0 == NULL) {
1510                 RAY_RECERR(sc, "could not pullup ether");
1511                 RAY_CCS_FREE(sc, ccs);
1512                 ifp->if_oerrors++;
1513                 return;
1514         }
1515         eh = mtod(m0, struct ether_header *);
1516
1517         /*
1518          * Write the 802.11 header according to network type etc.
1519          */
1520         if (sc->sc_c.np_net_type == RAY_MIB_NET_TYPE_ADHOC)
1521                 bufp = ray_tx_wrhdr(sc, bufp,
1522                     IEEE80211_FC0_TYPE_DATA,
1523                     IEEE80211_FC1_DIR_NODS,
1524                     eh->ether_dhost,
1525                     eh->ether_shost,
1526                     sc->sc_c.np_bss_id);
1527         else
1528                 if (sc->sc_c.np_ap_status == RAY_MIB_AP_STATUS_TERMINAL)
1529                         bufp = ray_tx_wrhdr(sc, bufp,
1530                             IEEE80211_FC0_TYPE_DATA,
1531                             IEEE80211_FC1_DIR_TODS,
1532                             sc->sc_c.np_bss_id,
1533                             eh->ether_shost,
1534                             eh->ether_dhost);
1535                 else
1536                         bufp = ray_tx_wrhdr(sc, bufp,
1537                             IEEE80211_FC0_TYPE_DATA,
1538                             IEEE80211_FC1_DIR_FROMDS,
1539                             eh->ether_dhost,
1540                             sc->sc_c.np_bss_id,
1541                             eh->ether_shost);
1542
1543         /*
1544          * Framing
1545          *
1546          * Add to the mbuf.
1547          */
1548         switch (sc->sc_c.np_framing) {
1549
1550         case RAY_FRAMING_ENCAPSULATION:
1551                 /* Nice and easy - nothing! (just add an 802.11 header) */
1552                 break;
1553
1554         case RAY_FRAMING_TRANSLATION:
1555                 /*
1556                  * Drop the first address in the ethernet header and
1557                  * write an LLC and SNAP header over the second.
1558                  */
1559                 m_adj(m0, ETHER_ADDR_LEN);
1560                 if (m0 == NULL) {
1561                         RAY_RECERR(sc, "could not get space for 802.2 header");
1562                         RAY_CCS_FREE(sc, ccs);
1563                         ifp->if_oerrors++;
1564                         return;
1565                 }
1566                 llc = mtod(m0, struct llc *);
1567                 llc->llc_dsap = LLC_SNAP_LSAP;
1568                 llc->llc_ssap = LLC_SNAP_LSAP;
1569                 llc->llc_control = LLC_UI;
1570                 llc->llc_un.type_snap.org_code[0] = 0;
1571                 llc->llc_un.type_snap.org_code[1] = 0;
1572                 llc->llc_un.type_snap.org_code[2] = 0;
1573                 break;
1574
1575         default:
1576                 RAY_RECERR(sc, "unknown framing type %d", sc->sc_c.np_framing);
1577                 RAY_CCS_FREE(sc, ccs);
1578                 ifp->if_oerrors++;
1579                 m_freem(m0);
1580                 return;
1581
1582         }
1583         if (m0 == NULL) {
1584                 RAY_RECERR(sc, "could not frame packet");
1585                 RAY_CCS_FREE(sc, ccs);
1586                 ifp->if_oerrors++;
1587                 return;
1588         }
1589         RAY_MBUF_DUMP(sc, RAY_DBG_TX, m0, "framed packet");
1590
1591         /*
1592          * Copy the mbuf to the buffer in common memory
1593          *
1594          * We drop and don't bother wrapping as Ethernet packets are 1518
1595          * bytes, we checked the mbuf earlier, and our TX buffers are 2048
1596          * bytes. We don't have 530 bytes of headers etc. so something
1597          * must be fubar.
1598          */
1599         pktlen = sizeof(struct ieee80211_frame);
1600         for (m = m0; m != NULL; m = m->m_next) {
1601                 pktlen += m->m_len;
1602                 if ((len = m->m_len) == 0)
1603                         continue;
1604                 if ((bufp + len) < RAY_TX_END)
1605                         SRAM_WRITE_REGION(sc, bufp, mtod(m, u_int8_t *), len);
1606                 else {
1607                         RAY_RECERR(sc, "tx buffer overflow");
1608                         RAY_CCS_FREE(sc, ccs);
1609                         ifp->if_oerrors++;
1610                         m_freem(m0);
1611                         return;
1612                 }
1613                 bufp += len;
1614         }
1615
1616         /*
1617          * Send it off
1618          */
1619         if (ray_tx_send(sc, ccs, pktlen, eh->ether_dhost))
1620                 ifp->if_oerrors++;
1621         else
1622                 ifp->if_opackets++;
1623         m_freem(m0);
1624 }
1625
1626 /*
1627  * Start timeout routine.
1628  *
1629  * Used when card was busy but we needed to send a packet.
1630  */
1631 static void
1632 ray_tx_timo(void *xsc)
1633 {
1634         struct ray_softc *sc = (struct ray_softc *)xsc;
1635         struct ifnet *ifp = sc->ifp;
1636         int s;
1637
1638         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
1639
1640         if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE) && (ifp->if_snd.ifq_head != NULL)) {
1641                 s = splimp();
1642                 ray_tx(ifp);
1643                 splx(s);
1644         }
1645 }
1646
1647 /*
1648  * Write an 802.11 header into the Tx buffer space and return the
1649  * adjusted buffer pointer.
1650  */
1651 static size_t
1652 ray_tx_wrhdr(struct ray_softc *sc, size_t bufp, u_int8_t type, u_int8_t fc1, u_int8_t *addr1, u_int8_t *addr2, u_int8_t *addr3)
1653 {
1654         struct ieee80211_frame header;
1655
1656         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_TX, "");
1657         RAY_MAP_CM(sc);
1658
1659         bzero(&header, sizeof(struct ieee80211_frame));
1660         header.i_fc[0] = (IEEE80211_FC0_VERSION_0 | type);
1661         header.i_fc[1] = fc1;
1662         bcopy(addr1, header.i_addr1, ETHER_ADDR_LEN);
1663         bcopy(addr2, header.i_addr2, ETHER_ADDR_LEN);
1664         bcopy(addr3, header.i_addr3, ETHER_ADDR_LEN);
1665
1666         SRAM_WRITE_REGION(sc, bufp, (u_int8_t *)&header,
1667             sizeof(struct ieee80211_frame));
1668
1669         return (bufp + sizeof(struct ieee80211_frame));
1670 }
1671
1672 /*
1673  * Fill in a few loose ends and kick the card to send the packet
1674  *
1675  * Returns 0 on success, 1 on failure
1676  */
1677 static int
1678 ray_tx_send(struct ray_softc *sc, size_t ccs, int pktlen, u_int8_t *dst)
1679 {
1680         int i = 0;
1681
1682         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_TX, "");
1683         RAY_MAP_CM(sc);
1684
1685         while (!RAY_ECF_READY(sc)) {
1686                 DELAY(RAY_ECF_SPIN_DELAY);
1687                 if (++i > RAY_ECF_SPIN_TRIES) {
1688                         RAY_RECERR(sc, "ECF busy, dropping packet");
1689                         RAY_CCS_FREE(sc, ccs);
1690                         return (1);
1691                 }
1692         }
1693         if (i != 0)
1694                 RAY_RECERR(sc, "spun %d times", i);
1695
1696         SRAM_WRITE_FIELD_2(sc, ccs, ray_cmd_tx, c_len, pktlen);
1697         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_antenna,
1698             ray_tx_best_antenna(sc, dst));
1699         SRAM_WRITE_1(sc, RAY_SCB_CCSI, RAY_CCS_INDEX(ccs));
1700         RAY_ECF_START_CMD(sc);
1701
1702         return (0);
1703 }
1704
1705 /*
1706  * Determine best antenna to use from rx level and antenna cache
1707  */
1708 static u_int8_t
1709 ray_tx_best_antenna(struct ray_softc *sc, u_int8_t *dst)
1710 {
1711         struct ray_siglev *sl;
1712         int i;
1713         u_int8_t antenna;
1714
1715         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_TX, "");
1716
1717         if (sc->sc_version == RAY_ECFS_BUILD_4) 
1718                 return (0);
1719
1720         /* try to find host */
1721         for (i = 0; i < RAY_NSIGLEVRECS; i++) {
1722                 sl = &sc->sc_siglevs[i];
1723                 if (bcmp(sl->rsl_host, dst, ETHER_ADDR_LEN) == 0)
1724                         goto found;
1725         }
1726         /* not found, return default setting */
1727         return (0);
1728
1729 found:
1730         /* This is a simple thresholding scheme that takes the mean
1731          * of the best antenna history. This is okay but as it is a
1732          * filter, it adds a bit of lag in situations where the
1733          * best antenna swaps from one side to the other slowly. Don't know
1734          * how likely this is given the horrible fading though.
1735          */
1736         antenna = 0;
1737         for (i = 0; i < RAY_NANTENNA; i++) {
1738                 antenna += sl->rsl_antennas[i];
1739         }
1740
1741         return (antenna > (RAY_NANTENNA >> 1));
1742 }
1743
1744 /*
1745  * Transmit now complete so clear ccs and network flags.
1746  */
1747 static void
1748 ray_tx_done(struct ray_softc *sc, u_int8_t status, size_t ccs)
1749 {
1750         struct ifnet *ifp = sc->ifp;
1751
1752         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_TX, "");
1753
1754         RAY_CCSERR(sc, status, if_oerrors);
1755
1756         RAY_CCS_FREE(sc, ccs);
1757         ifp->if_timer = 0;
1758         if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
1759             ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1760 }
1761
1762 /*
1763  * Receiver packet handling
1764  */
1765
1766 /*
1767  * Receive a packet from the card
1768  */
1769 static void
1770 ray_rx(struct ray_softc *sc, size_t rcs)
1771 {
1772         struct ieee80211_frame *header;
1773         struct ifnet *ifp = sc->ifp;
1774         struct mbuf *m0;
1775         size_t pktlen, fraglen, readlen, tmplen;
1776         size_t bufp, ebufp;
1777         u_int8_t siglev, antenna;
1778         u_int first, ni, i;
1779         u_int8_t *mp;
1780
1781         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
1782         RAY_MAP_CM(sc);
1783
1784         RAY_DPRINTF(sc, RAY_DBG_CCS, "using rcs 0x%x", rcs);
1785
1786         m0 = NULL;
1787         readlen = 0;
1788
1789         /*
1790          * Get first part of packet and the length. Do some sanity checks
1791          * and get a mbuf.
1792          */
1793         first = RAY_CCS_INDEX(rcs);
1794         pktlen = SRAM_READ_FIELD_2(sc, rcs, ray_cmd_rx, c_pktlen);
1795         siglev = SRAM_READ_FIELD_1(sc, rcs, ray_cmd_rx, c_siglev);
1796         antenna = SRAM_READ_FIELD_1(sc, rcs, ray_cmd_rx, c_antenna);
1797
1798         if ((pktlen > MCLBYTES) || (pktlen < sizeof(struct ieee80211_frame))) {
1799                 RAY_RECERR(sc, "packet too big or too small");
1800                 ifp->if_ierrors++;
1801                 goto skip_read;
1802         }
1803
1804         MGETHDR(m0, M_DONTWAIT, MT_DATA);
1805         if (m0 == NULL) {
1806                 RAY_RECERR(sc, "MGETHDR failed");
1807                 ifp->if_ierrors++;
1808                 goto skip_read;
1809         }
1810         if (pktlen > MHLEN) {
1811                 MCLGET(m0, M_DONTWAIT);
1812                 if (!(m0->m_flags & M_EXT)) {
1813                         RAY_RECERR(sc, "MCLGET failed");
1814                         ifp->if_ierrors++;
1815                         m_freem(m0);
1816                         m0 = NULL;
1817                         goto skip_read;
1818                 }
1819         }
1820         m0->m_pkthdr.rcvif = ifp;
1821         m0->m_pkthdr.len = pktlen;
1822         m0->m_len = pktlen;
1823         mp = mtod(m0, u_int8_t *);
1824
1825         /*
1826          * Walk the fragment chain to build the complete packet.
1827          *
1828          * The use of two index variables removes a race with the
1829          * hardware. If one index were used the clearing of the CCS would
1830          * happen before reading the next pointer and the hardware can get in.
1831          * Not my idea but verbatim from the NetBSD driver.
1832          */
1833         i = ni = first;
1834         while ((i = ni) && (i != RAY_CCS_LINK_NULL)) {
1835                 rcs = RAY_CCS_ADDRESS(i);
1836                 ni = SRAM_READ_FIELD_1(sc, rcs, ray_cmd_rx, c_nextfrag);
1837                 bufp = SRAM_READ_FIELD_2(sc, rcs, ray_cmd_rx, c_bufp);
1838                 fraglen = SRAM_READ_FIELD_2(sc, rcs, ray_cmd_rx, c_len);
1839                 if (fraglen + readlen > pktlen) {
1840                         RAY_RECERR(sc, "bad length current 0x%zx pktlen 0x%zx",
1841                             fraglen + readlen, pktlen);
1842                         ifp->if_ierrors++;
1843                         m_freem(m0);
1844                         m0 = NULL;
1845                         goto skip_read;
1846                 }
1847                 if ((i < RAY_RCS_FIRST) || (i > RAY_RCS_LAST)) {
1848                         RAY_RECERR(sc, "bad rcs index 0x%x", i);
1849                         ifp->if_ierrors++;
1850                         m_freem(m0);
1851                         m0 = NULL;
1852                         goto skip_read;
1853                 }
1854
1855                 ebufp = bufp + fraglen;
1856                 if (ebufp <= RAY_RX_END)
1857                         SRAM_READ_REGION(sc, bufp, mp, fraglen);
1858                 else {
1859                         SRAM_READ_REGION(sc, bufp, mp,
1860                             (tmplen = RAY_RX_END - bufp));
1861                         SRAM_READ_REGION(sc, RAY_RX_BASE, mp + tmplen,
1862                             ebufp - RAY_RX_END);
1863                 }
1864                 mp += fraglen;
1865                 readlen += fraglen;
1866         }
1867
1868 skip_read:
1869
1870         /*
1871          * Walk the chain again to free the rcss.
1872          */
1873         i = ni = first;
1874         while ((i = ni) && (i != RAY_CCS_LINK_NULL)) {
1875                 rcs = RAY_CCS_ADDRESS(i);
1876                 ni = SRAM_READ_FIELD_1(sc, rcs, ray_cmd_rx, c_nextfrag);
1877                 RAY_CCS_FREE(sc, rcs);
1878         }
1879
1880         if (m0 == NULL)
1881                 return;
1882
1883         /*
1884          * Check the 802.11 packet type and hand off to
1885          * appropriate functions.
1886          */
1887         header = mtod(m0, struct ieee80211_frame *);
1888         if ((header->i_fc[0] & IEEE80211_FC0_VERSION_MASK)
1889             != IEEE80211_FC0_VERSION_0) {
1890                 RAY_RECERR(sc, "header not version 0 fc0 0x%x",
1891                     header->i_fc[0]);
1892                 ifp->if_ierrors++;
1893                 m_freem(m0);
1894                 return;
1895         }
1896         switch (header->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
1897
1898         case IEEE80211_FC0_TYPE_DATA:
1899                 ray_rx_data(sc, m0, siglev, antenna);
1900                 break;
1901
1902         case IEEE80211_FC0_TYPE_MGT:
1903                 ray_rx_mgt(sc, m0);
1904                 break;
1905
1906         case IEEE80211_FC0_TYPE_CTL:
1907                 ray_rx_ctl(sc, m0);
1908                 break;
1909
1910         default:
1911                 RAY_RECERR(sc, "unknown packet fc0 0x%x", header->i_fc[0]);
1912                 ifp->if_ierrors++;
1913                 m_freem(m0);
1914         }
1915 }
1916
1917 /*
1918  * Deal with DATA packet types
1919  */
1920 static void
1921 ray_rx_data(struct ray_softc *sc, struct mbuf *m0, u_int8_t siglev, u_int8_t antenna)
1922 {
1923         struct ifnet *ifp = sc->ifp;
1924         struct ieee80211_frame *header = mtod(m0, struct ieee80211_frame *);
1925         struct llc *llc;
1926         u_int8_t *sa = NULL, *da = NULL, *ra = NULL, *ta = NULL;
1927         int trim = 0;
1928
1929         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_RX, "");
1930
1931         /*
1932          * Check the the data packet subtype, some packets have
1933          * nothing in them so we will drop them here.
1934          */
1935         switch (header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
1936
1937         case IEEE80211_FC0_SUBTYPE_DATA:
1938         case IEEE80211_FC0_SUBTYPE_CF_ACK:
1939         case IEEE80211_FC0_SUBTYPE_CF_POLL:
1940         case IEEE80211_FC0_SUBTYPE_CF_ACPL:
1941                 RAY_DPRINTF(sc, RAY_DBG_RX, "DATA packet");
1942                 break;
1943
1944         case IEEE80211_FC0_SUBTYPE_NODATA:
1945         case IEEE80211_FC0_SUBTYPE_CFACK:
1946         case IEEE80211_FC0_SUBTYPE_CFPOLL:
1947         case IEEE80211_FC0_SUBTYPE_CF_ACK_CF_ACK:
1948                 RAY_DPRINTF(sc, RAY_DBG_RX, "NULL packet");
1949                 m_freem(m0);
1950                 return;
1951                 break;
1952
1953         default:
1954                 RAY_RECERR(sc, "reserved DATA packet subtype 0x%x",
1955                     header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
1956                 ifp->if_ierrors++;
1957                 m_freem(m0);
1958                 return;
1959         }
1960
1961         /*
1962          * Parse the To DS and From DS fields to determine the length
1963          * of the 802.11 header for use later on.
1964          *
1965          * Additionally, furtle out the right destination and
1966          * source MAC addresses for the packet. Packets may come via
1967          * APs so the MAC addresses of the immediate node may be
1968          * different from the node that actually sent us the packet.
1969          *
1970          *      da      destination address of final recipient
1971          *      sa      source address of orginator
1972          *      ra      receiver address of immediate recipient
1973          *      ta      transmitter address of immediate orginator
1974          *
1975          * Address matching is performed on da or sa with the AP or
1976          * BSSID in ra and ta.
1977          */
1978         RAY_MBUF_DUMP(sc, RAY_DBG_RX, m0, "(1) packet before framing");
1979         switch (header->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
1980
1981         case IEEE80211_FC1_DIR_NODS:
1982                 da = ra = header->i_addr1;
1983                 sa = ta = header->i_addr2;
1984                 trim = sizeof(struct ieee80211_frame);
1985                 RAY_DPRINTF(sc, RAY_DBG_RX, "from %6D to %6D",
1986                     sa, ":", da, ":");
1987                 break;
1988
1989         case IEEE80211_FC1_DIR_FROMDS:
1990                 da = ra = header->i_addr1;
1991                 ta = header->i_addr2;
1992                 sa = header->i_addr3;
1993                 trim = sizeof(struct ieee80211_frame);
1994                 RAY_DPRINTF(sc, RAY_DBG_RX, "ap %6D from %6D to %6D",
1995                     ta, ":", sa, ":", da, ":");
1996                 break;
1997
1998         case IEEE80211_FC1_DIR_TODS:
1999                 ra = header->i_addr1;
2000                 sa = ta = header->i_addr2;
2001                 da = header->i_addr3;
2002                 trim = sizeof(struct ieee80211_frame);
2003                 RAY_DPRINTF(sc, RAY_DBG_RX, "from %6D to %6D ap %6D",
2004                     sa, ":", da, ":", ra, ":");
2005                 break;
2006
2007         case IEEE80211_FC1_DIR_DSTODS:
2008                 ra = header->i_addr1;
2009                 ta = header->i_addr2;
2010                 da = header->i_addr3;
2011                 sa = (u_int8_t *)header+1;
2012                 trim = sizeof(struct ieee80211_frame) + ETHER_ADDR_LEN;
2013                 RAY_DPRINTF(sc, RAY_DBG_RX, "from %6D to %6D ap %6D to %6D",
2014                     sa, ":", da, ":", ta, ":", ra, ":");
2015                 break;
2016         }
2017
2018         /*
2019          * Framing
2020          *
2021          * Each case must leave an Ethernet header and adjust trim.
2022          */
2023         switch (sc->sc_c.np_framing) {
2024
2025         case RAY_FRAMING_ENCAPSULATION:
2026                 /* A NOP as the Ethernet header is in the packet */
2027                 break;
2028
2029         case RAY_FRAMING_TRANSLATION:
2030                 /* Check that we have an LLC and SNAP sequence */
2031                 llc = (struct llc *)((u_int8_t *)header + trim);
2032                 if (llc->llc_dsap == LLC_SNAP_LSAP &&
2033                     llc->llc_ssap == LLC_SNAP_LSAP &&
2034                     llc->llc_control == LLC_UI &&
2035                     llc->llc_un.type_snap.org_code[0] == 0 &&
2036                     llc->llc_un.type_snap.org_code[1] == 0 &&
2037                     llc->llc_un.type_snap.org_code[2] == 0) {
2038                         struct ether_header *eh;
2039                         /*
2040                          * This is not magic. RFC1042 header is 8
2041                          * bytes, with the last two bytes being the
2042                          * ether type. So all we need is another
2043                          * ETHER_ADDR_LEN bytes to write the
2044                          * destination into.
2045                          */
2046                         trim -= ETHER_ADDR_LEN;
2047                         eh = (struct ether_header *)((u_int8_t *)header + trim);
2048
2049                         /*
2050                          * Copy carefully to avoid mashing the MAC
2051                          * addresses. The address layout in the .11 header
2052                          * does make sense, honest, but it is a pain.
2053                          * 
2054                          * NODS         da sa           no risk              
2055                          * FROMDS       da ta sa        sa then da           
2056                          * DSTODS       ra ta da sa     sa then da           
2057                          * TODS         ra sa da        da then sa           
2058                          */
2059                         if (sa > da) {
2060                                 /* Copy sa first */
2061                                 bcopy(sa, eh->ether_shost, ETHER_ADDR_LEN);
2062                                 bcopy(da, eh->ether_dhost, ETHER_ADDR_LEN);
2063                         } else {
2064                                 /* Copy da first */
2065                                 bcopy(da, eh->ether_dhost, ETHER_ADDR_LEN);
2066                                 bcopy(sa, eh->ether_shost, ETHER_ADDR_LEN);
2067                         }
2068
2069                 } else {
2070
2071                         /* Assume RAY_FRAMING_ENCAPSULATION */
2072                         RAY_RECERR(sc,
2073                             "got encapsulated packet but in translation mode");
2074
2075                 }
2076                 break;
2077
2078         default:
2079                 RAY_RECERR(sc, "unknown framing type %d", sc->sc_c.np_framing);
2080                 ifp->if_ierrors++;
2081                 m_freem(m0);
2082                 return;
2083         }
2084         RAY_MBUF_DUMP(sc, RAY_DBG_RX, m0, "(2) packet after framing");
2085
2086         /*
2087          * Finally, do a bit of house keeping before sending the packet
2088          * up the stack.
2089          */
2090         m_adj(m0, trim);
2091         RAY_MBUF_DUMP(sc, RAY_DBG_RX, m0, "(3) packet after trimming");
2092         ifp->if_ipackets++;
2093         ray_rx_update_cache(sc, header->i_addr2, siglev, antenna);
2094         (*ifp->if_input)(ifp, m0);
2095 }
2096
2097 /*
2098  * Deal with MGT packet types
2099  */
2100 static void
2101 ray_rx_mgt(struct ray_softc *sc, struct mbuf *m0)
2102 {
2103         struct ifnet *ifp = sc->ifp;
2104         struct ieee80211_frame *header = mtod(m0, struct ieee80211_frame *);
2105
2106         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_MGT, "");
2107
2108         if ((header->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
2109             IEEE80211_FC1_DIR_NODS) {
2110                 RAY_RECERR(sc, "MGT TODS/FROMDS wrong fc1 0x%x",
2111                     header->i_fc[1] & IEEE80211_FC1_DIR_MASK);
2112                 ifp->if_ierrors++;
2113                 m_freem(m0);
2114                 return;
2115         }
2116
2117         /*
2118          * Check the the mgt packet subtype, some packets should be
2119          * dropped depending on the mode the station is in. See pg
2120          * 52(60) of docs
2121          *
2122          * P - proccess, J - Junk, E - ECF deals with, I - Illegal
2123          * ECF Proccesses
2124          *  AHDOC procces or junk
2125          *   INFRA STA process or junk
2126          *    INFRA AP process or jumk
2127          * 
2128          * +PPP IEEE80211_FC0_SUBTYPE_BEACON
2129          * +EEE IEEE80211_FC0_SUBTYPE_PROBE_REQ
2130          * +EEE IEEE80211_FC0_SUBTYPE_PROBE_RESP
2131          *  PPP IEEE80211_FC0_SUBTYPE_AUTH
2132          *  PPP IEEE80211_FC0_SUBTYPE_DEAUTH
2133          *  JJP IEEE80211_FC0_SUBTYPE_ASSOC_REQ
2134          *  JPJ IEEE80211_FC0_SUBTYPE_ASSOC_RESP
2135          *  JPP IEEE80211_FC0_SUBTYPE_DISASSOC
2136          *  JJP IEEE80211_FC0_SUBTYPE_REASSOC_REQ
2137          *  JPJ IEEE80211_FC0_SUBTYPE_REASSOC_RESP
2138          * +EEE IEEE80211_FC0_SUBTYPE_ATIM
2139          */
2140         RAY_MBUF_DUMP(sc, RAY_DBG_MGT, m0, "MGT packet");
2141         switch (header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2142
2143         case IEEE80211_FC0_SUBTYPE_BEACON:
2144                 RAY_DPRINTF(sc, RAY_DBG_MGT, "BEACON MGT packet");
2145                 ray_rx_mgt_beacon(sc, m0);
2146                 break;
2147
2148         case IEEE80211_FC0_SUBTYPE_AUTH:
2149                 RAY_DPRINTF(sc, RAY_DBG_MGT, "AUTH MGT packet");
2150                 ray_rx_mgt_auth(sc, m0);
2151                 break;
2152
2153         case IEEE80211_FC0_SUBTYPE_DEAUTH:
2154                 RAY_DPRINTF(sc, RAY_DBG_MGT, "DEAUTH MGT packet");
2155                 /* XXX ray_rx_mgt_deauth(sc, m0); */
2156                 break;
2157
2158         case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
2159         case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
2160                 RAY_DPRINTF(sc, RAY_DBG_MGT, "(RE)ASSOC_REQ MGT packet");
2161                 if ((sc->sc_c.np_net_type == RAY_MIB_NET_TYPE_INFRA) &&
2162                     (sc->sc_c.np_ap_status == RAY_MIB_AP_STATUS_AP))
2163                         RAY_RECERR(sc, "can't be an AP yet"); /* XXX_ACTING_AP */
2164                 break;
2165                         
2166         case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
2167         case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
2168                 RAY_DPRINTF(sc, RAY_DBG_MGT, "(RE)ASSOC_RESP MGT packet");
2169                 if ((sc->sc_c.np_net_type == RAY_MIB_NET_TYPE_INFRA) &&
2170                     (sc->sc_c.np_ap_status == RAY_MIB_AP_STATUS_TERMINAL))
2171                         RAY_RECERR(sc, "can't be in INFRA yet"); /* XXX_INFRA */
2172                 break;
2173
2174         case IEEE80211_FC0_SUBTYPE_DISASSOC:
2175                 RAY_DPRINTF(sc, RAY_DBG_MGT, "DISASSOC MGT packet");
2176                 if (sc->sc_c.np_net_type == RAY_MIB_NET_TYPE_INFRA)
2177                         RAY_RECERR(sc, "can't be in INFRA yet"); /* XXX_INFRA */
2178                 break;
2179
2180         case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
2181         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2182         case IEEE80211_FC0_SUBTYPE_ATIM:
2183                 RAY_RECERR(sc, "unexpected MGT packet subtype 0x%0x",
2184                     header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2185                 ifp->if_ierrors++;
2186                 break;
2187                 
2188         default:
2189                 RAY_RECERR(sc, "reserved MGT packet subtype 0x%x",
2190                     header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2191                 ifp->if_ierrors++;
2192         }
2193
2194         m_freem(m0);
2195 }
2196
2197 /*
2198  * Deal with BEACON management packet types
2199  * XXX furtle anything interesting out
2200  * XXX Note that there are rules governing what beacons to read
2201  * XXX see 8802 S7.2.3, S11.1.2.3
2202  * XXX is this actually useful?
2203  */
2204 static void
2205 ray_rx_mgt_beacon(struct ray_softc *sc, struct mbuf *m0)
2206 {
2207         struct ieee80211_frame *header = mtod(m0, struct ieee80211_frame *);
2208         ieee80211_mgt_beacon_t beacon = (u_int8_t *)(header+1);
2209         union ieee80211_information elements;
2210
2211         u_int64_t *timestamp;
2212
2213         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_MGT, "");
2214
2215         timestamp = (u_int64_t *)beacon;
2216
2217 RAY_DPRINTF(sc, RAY_DBG_MGT, "timestamp\t0x%x", *timestamp);
2218 RAY_DPRINTF(sc, RAY_DBG_MGT, "interval\t\t0x%x", IEEE80211_BEACON_INTERVAL(beacon));
2219 RAY_DPRINTF(sc, RAY_DBG_MGT, "capability\t0x%x", IEEE80211_BEACON_CAPABILITY(beacon));
2220
2221         ray_rx_mgt_info(sc, m0, &elements);
2222
2223 }
2224
2225 static void
2226 ray_rx_mgt_info(struct ray_softc *sc, struct mbuf *m0, union ieee80211_information *elements)
2227 {
2228         struct ifnet *ifp = sc->ifp;
2229         struct ieee80211_frame *header = mtod(m0, struct ieee80211_frame *);
2230         ieee80211_mgt_beacon_t beacon = (u_int8_t *)(header+1);
2231         ieee80211_mgt_beacon_t bp, be;
2232         int len;
2233
2234         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_MGT, "");
2235
2236         bp = beacon + 12;
2237         be = mtod(m0, u_int8_t *) + m0->m_len;
2238         
2239         while (bp < be) {
2240                 len = *(bp + 1);
2241                 RAY_DPRINTF(sc, RAY_DBG_MGT, "id 0x%02x length %d", *bp, len);
2242
2243                 switch (*bp) {
2244
2245                 case IEEE80211_ELEMID_SSID:
2246                         if (len  > IEEE80211_NWID_LEN) {
2247                                 RAY_RECERR(sc, "bad SSD length: %d from %6D",
2248                                     len, header->i_addr2, ":");
2249                         }
2250                         strncpy(elements->ssid, bp + 2, len);
2251                         elements->ssid[len] = 0;
2252                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2253                             "beacon ssid %s", elements->ssid);
2254                         break; 
2255
2256                 case IEEE80211_ELEMID_RATES:
2257                         RAY_DPRINTF(sc, RAY_DBG_MGT, "rates");
2258                         break;
2259
2260                 case IEEE80211_ELEMID_FHPARMS:
2261                         elements->fh.dwell = bp[2] + (bp[3] << 8);
2262                         elements->fh.set = bp[4];
2263                         elements->fh.pattern = bp[5];
2264                         elements->fh.index = bp[6];
2265                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2266                             "fhparams dwell\t0x%04x", elements->fh.dwell);
2267                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2268                             "fhparams set\t0x%02x", elements->fh.set);
2269                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2270                             "fhparams pattern\t0x%02x", elements->fh.pattern);
2271                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2272                             "fhparams index\t0x%02x", elements->fh.index);
2273                         break;
2274
2275                 case IEEE80211_ELEMID_DSPARMS:
2276                         RAY_RECERR(sc, "got direct sequence params!");
2277                         break;
2278
2279                 case IEEE80211_ELEMID_CFPARMS:
2280                         RAY_DPRINTF(sc, RAY_DBG_MGT, "cfparams");
2281                         break;
2282
2283                 case IEEE80211_ELEMID_TIM:
2284                         elements->tim.count = bp[2];
2285                         elements->tim.period = bp[3];
2286                         elements->tim.bitctl = bp[4];
2287                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2288                             "tim count\t0x%02x", elements->tim.count);
2289                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2290                             "tim period\t0x%02x", elements->tim.period);
2291                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2292                             "tim bitctl\t0x%02x", elements->tim.bitctl);
2293 #if RAY_DEBUG & RAY_DBG_MGT
2294                         {
2295                                 int i;
2296                                 for (i = 5; i < len + 1; i++)
2297                                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2298                                             "tim pvt[%03d]\t0x%02x", i-5, bp[i]);
2299                         }
2300 #endif /* (RAY_DEBUG & RAY_DBG_MGT) */
2301                         break;
2302                         
2303                 case IEEE80211_ELEMID_IBSSPARMS:
2304                         elements->ibss.atim = bp[2] + (bp[3] << 8);
2305                         RAY_DPRINTF(sc, RAY_DBG_MGT,
2306                             "ibssparams atim\t0x%02x", elements->ibss.atim);
2307                         break;
2308
2309                 case IEEE80211_ELEMID_CHALLENGE:
2310                         RAY_DPRINTF(sc, RAY_DBG_MGT, "challenge");
2311                         break;
2312
2313                 default:
2314                         RAY_RECERR(sc, "reserved MGT element id 0x%x", *bp);
2315                         ifp->if_ierrors++;break;
2316                 }
2317                 bp += bp[1] + 2; 
2318         }
2319 }
2320
2321 /*
2322  * Deal with AUTH management packet types
2323  */
2324 static void
2325 ray_rx_mgt_auth(struct ray_softc *sc, struct mbuf *m0)
2326 {
2327         struct ieee80211_frame *header = mtod(m0, struct ieee80211_frame *);
2328         ieee80211_mgt_auth_t auth = (u_int8_t *)(header+1);
2329
2330         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_AUTH, "");
2331
2332         switch (IEEE80211_AUTH_ALGORITHM(auth)) {
2333             
2334         case IEEE80211_AUTH_ALG_OPEN:
2335                 RAY_DPRINTF(sc, RAY_DBG_AUTH,
2336                     "open system authentication sequence number %d",
2337                     IEEE80211_AUTH_TRANSACTION(auth));
2338                 if (IEEE80211_AUTH_TRANSACTION(auth) ==
2339                     IEEE80211_AUTH_OPEN_REQUEST) {
2340
2341 /* XXX_AUTH use ray_init_auth_send */
2342
2343                 } else if (IEEE80211_AUTH_TRANSACTION(auth) == 
2344                     IEEE80211_AUTH_OPEN_RESPONSE)
2345                         ray_init_auth_done(sc, IEEE80211_AUTH_STATUS(auth));
2346                 break;
2347
2348         case IEEE80211_AUTH_ALG_SHARED:
2349                 RAY_RECERR(sc,
2350                     "shared key authentication sequence number %d",
2351                     IEEE80211_AUTH_TRANSACTION(auth));
2352                 break;
2353         
2354         default:
2355                 RAY_RECERR(sc,
2356                     "reserved authentication subtype 0x%04hx",
2357                     IEEE80211_AUTH_ALGORITHM(auth));
2358                 break;
2359         }
2360 }
2361
2362 /*
2363  * Deal with CTL packet types
2364  */
2365 static void
2366 ray_rx_ctl(struct ray_softc *sc, struct mbuf *m0)
2367 {
2368         struct ifnet *ifp = sc->ifp;
2369         struct ieee80211_frame *header = mtod(m0, struct ieee80211_frame *);
2370
2371         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_CTL, "");
2372
2373         if ((header->i_fc[1] & IEEE80211_FC1_DIR_MASK) !=
2374             IEEE80211_FC1_DIR_NODS) {
2375                 RAY_RECERR(sc, "CTL TODS/FROMDS wrong fc1 0x%x",
2376                     header->i_fc[1] & IEEE80211_FC1_DIR_MASK);
2377                 ifp->if_ierrors++;
2378                 m_freem(m0);
2379                 return;
2380         }
2381
2382         /*
2383          * Check the the ctl packet subtype, some packets should be
2384          * dropped depending on the mode the station is in. The ECF
2385          * should deal with everything but the power save poll to an
2386          * AP. See pg 52(60) of docs.
2387          */
2388         RAY_MBUF_DUMP(sc, RAY_DBG_CTL, m0, "CTL packet");
2389         switch (header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
2390
2391         case IEEE80211_FC0_SUBTYPE_PS_POLL:
2392                 RAY_DPRINTF(sc, RAY_DBG_CTL, "PS_POLL CTL packet");
2393                 if ((sc->sc_d.np_net_type == RAY_MIB_NET_TYPE_INFRA) &&
2394                     (sc->sc_c.np_ap_status == RAY_MIB_AP_STATUS_AP))
2395                         RAY_RECERR(sc, "can't be an AP yet"); /* XXX_ACTING_AP */
2396                 break;
2397
2398         case IEEE80211_FC0_SUBTYPE_RTS:
2399         case IEEE80211_FC0_SUBTYPE_CTS:
2400         case IEEE80211_FC0_SUBTYPE_ACK:
2401         case IEEE80211_FC0_SUBTYPE_CF_END:
2402         case IEEE80211_FC0_SUBTYPE_CF_END_ACK:
2403                 RAY_RECERR(sc, "unexpected CTL packet subtype 0x%0x",
2404                     header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2405                 ifp->if_ierrors++;
2406                 break;
2407
2408         default:
2409                 RAY_RECERR(sc, "reserved CTL packet subtype 0x%x",
2410                     header->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
2411                 ifp->if_ierrors++;
2412         }
2413
2414         m_freem(m0);
2415 }
2416
2417 /*
2418  * Update rx level and antenna cache
2419  */
2420 static void
2421 ray_rx_update_cache(struct ray_softc *sc, u_int8_t *src, u_int8_t siglev, u_int8_t antenna)
2422 {
2423         struct timeval mint;
2424         struct ray_siglev *sl;
2425         int i, mini;
2426
2427         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2428
2429         /* Try to find host */
2430         for (i = 0; i < RAY_NSIGLEVRECS; i++) {
2431                 sl = &sc->sc_siglevs[i];
2432                 if (bcmp(sl->rsl_host, src, ETHER_ADDR_LEN) == 0)
2433                         goto found;
2434         }
2435         /* Not found, find oldest slot */
2436         mini = 0;
2437         mint.tv_sec = LONG_MAX;
2438         mint.tv_usec = 0;
2439         for (i = 0; i < RAY_NSIGLEVRECS; i++) {
2440                 sl = &sc->sc_siglevs[i];
2441                 if (timevalcmp(&sl->rsl_time, &mint, <)) {
2442                         mini = i;
2443                         mint = sl->rsl_time;
2444                 }
2445         }
2446         sl = &sc->sc_siglevs[mini];
2447         bzero(sl->rsl_siglevs, RAY_NSIGLEV);
2448         bzero(sl->rsl_antennas, RAY_NANTENNA);
2449         bcopy(src, sl->rsl_host, ETHER_ADDR_LEN);
2450
2451 found:
2452         microtime(&sl->rsl_time);
2453         bcopy(sl->rsl_siglevs, &sl->rsl_siglevs[1], RAY_NSIGLEV-1);
2454         sl->rsl_siglevs[0] = siglev;
2455         if (sc->sc_version != RAY_ECFS_BUILD_4) {
2456                 bcopy(sl->rsl_antennas, &sl->rsl_antennas[1], RAY_NANTENNA-1);
2457                 sl->rsl_antennas[0] = antenna;
2458         }
2459 }
2460
2461 /*
2462  * Interrupt handling
2463  */
2464
2465 /*
2466  * Process an interrupt
2467  */
2468 static void
2469 ray_intr(void *xsc)
2470 {
2471         struct ray_softc *sc = (struct ray_softc *)xsc;
2472         struct ifnet *ifp = sc->ifp;
2473         size_t ccs;
2474         u_int8_t cmd, status;
2475         int ccsi;
2476
2477         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2478         RAY_MAP_CM(sc);
2479
2480         if ((sc == NULL) || (sc->sc_gone))
2481                 return;
2482
2483         /*
2484          * Check that the interrupt was for us, if so get the rcs/ccs
2485          * and vector on the command contained within it.
2486          */
2487         if (RAY_HCS_INTR(sc)) {
2488                 ccsi = SRAM_READ_1(sc, RAY_SCB_RCSI);
2489                 ccs = RAY_CCS_ADDRESS(ccsi);
2490                 cmd = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_cmd);
2491                 status = SRAM_READ_FIELD_1(sc, ccs, ray_cmd, c_status);
2492                 if (ccsi <= RAY_CCS_LAST)
2493                         ray_intr_ccs(sc, cmd, status, ccs);
2494                 else if (ccsi <= RAY_RCS_LAST)
2495                         ray_intr_rcs(sc, cmd, ccs);
2496                 else
2497                     RAY_RECERR(sc, "bad ccs index 0x%x", ccsi);
2498                 RAY_HCS_CLEAR_INTR(sc);
2499         }
2500
2501         /* Send any packets lying around and update error counters */
2502         if (!(ifp->if_drv_flags & IFF_DRV_OACTIVE) && (ifp->if_snd.ifq_head != NULL))
2503                 ray_tx(ifp);
2504         if ((++sc->sc_checkcounters % 32) == 0)
2505                 ray_intr_updt_errcntrs(sc);
2506 }
2507
2508 /*
2509  * Read the error counters.
2510  */
2511 static void
2512 ray_intr_updt_errcntrs(struct ray_softc *sc)
2513 {
2514         size_t csc;
2515
2516         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2517         RAY_MAP_CM(sc);
2518
2519         /*
2520          * The card implements the following protocol to keep the
2521          * values from being changed while read: It checks the `own'
2522          * bit and if zero writes the current internal counter value,
2523          * it then sets the `own' bit to 1. If the `own' bit was 1 it
2524          * incremenets its internal counter. The user thus reads the
2525          * counter if the `own' bit is one and then sets the own bit
2526          * to 0.
2527          */
2528         csc = RAY_STATUS_BASE;
2529         if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_mrxo_own)) {
2530                 sc->sc_rxoverflow +=
2531                     SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_mrx_overflow);
2532                 SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_mrxo_own, 0);
2533         }
2534         if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_mrxc_own)) {
2535                 sc->sc_rxcksum +=
2536                     SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_mrx_overflow);
2537                 SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_mrxc_own, 0);
2538         }
2539         if (SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_rxhc_own)) {
2540                 sc->sc_rxhcksum +=
2541                     SRAM_READ_FIELD_2(sc, csc, ray_csc, csc_rx_hcksum);
2542                 SRAM_WRITE_FIELD_1(sc, csc, ray_csc, csc_rxhc_own, 0);
2543         }
2544         sc->sc_rxnoise = SRAM_READ_FIELD_1(sc, csc, ray_csc, csc_rx_noise);
2545 }
2546
2547 /*
2548  * Process CCS command completion
2549  */
2550 static void
2551 ray_intr_ccs(struct ray_softc *sc, u_int8_t cmd, u_int8_t status, size_t ccs)
2552 {
2553         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2554
2555         switch (cmd) {
2556
2557         case RAY_CMD_DOWNLOAD_PARAMS:
2558                 RAY_DPRINTF(sc, RAY_DBG_COM, "START_PARAMS");
2559                 ray_init_download_done(sc, status, ccs);
2560                 break;
2561
2562         case RAY_CMD_UPDATE_PARAMS:
2563                 RAY_DPRINTF(sc, RAY_DBG_COM, "UPDATE_PARAMS");
2564                 ray_upparams_done(sc, status, ccs);
2565                 break;
2566
2567         case RAY_CMD_REPORT_PARAMS:
2568                 RAY_DPRINTF(sc, RAY_DBG_COM, "REPORT_PARAMS");
2569                 ray_repparams_done(sc, status, ccs);
2570                 break;
2571
2572         case RAY_CMD_UPDATE_MCAST:
2573                 RAY_DPRINTF(sc, RAY_DBG_COM, "UPDATE_MCAST");
2574                 ray_mcast_done(sc, status, ccs);
2575                 break;
2576
2577         case RAY_CMD_START_NET:
2578         case RAY_CMD_JOIN_NET:
2579                 RAY_DPRINTF(sc, RAY_DBG_COM, "START|JOIN_NET");
2580                 ray_init_sj_done(sc, status, ccs);
2581                 break;
2582
2583         case RAY_CMD_TX_REQ:
2584                 RAY_DPRINTF(sc, RAY_DBG_COM, "TX_REQ");
2585                 ray_tx_done(sc, status, ccs);
2586                 break;
2587
2588         case RAY_CMD_START_ASSOC:
2589                 RAY_DPRINTF(sc, RAY_DBG_COM, "START_ASSOC");
2590                 ray_init_assoc_done(sc, status, ccs);
2591                 break;
2592
2593         case RAY_CMD_UPDATE_APM:
2594                 RAY_RECERR(sc, "unexpected UPDATE_APM");
2595                 break;
2596
2597         case RAY_CMD_TEST_MEM:
2598                 RAY_RECERR(sc, "unexpected TEST_MEM");
2599                 break;
2600
2601         case RAY_CMD_SHUTDOWN:
2602                 RAY_RECERR(sc, "unexpected SHUTDOWN");
2603                 break;
2604
2605         case RAY_CMD_DUMP_MEM:
2606                 RAY_RECERR(sc, "unexpected DUMP_MEM");
2607                 break;
2608
2609         case RAY_CMD_START_TIMER:
2610                 RAY_RECERR(sc, "unexpected START_TIMER");
2611                 break;
2612
2613         default:
2614                 RAY_RECERR(sc, "unknown command 0x%x", cmd);
2615                 break;
2616         }
2617 }
2618
2619 /*
2620  * Process ECF command request
2621  */
2622 static void
2623 ray_intr_rcs(struct ray_softc *sc, u_int8_t cmd, size_t rcs)
2624 {
2625         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2626
2627         switch (cmd) {
2628
2629         case RAY_ECMD_RX_DONE:
2630                 RAY_DPRINTF(sc, RAY_DBG_RX, "RX_DONE");
2631                 ray_rx(sc, rcs);
2632                 break;
2633
2634         case RAY_ECMD_REJOIN_DONE:
2635                 RAY_DPRINTF(sc, RAY_DBG_RX, "REJOIN_DONE");
2636                 sc->sc_c.np_havenet = 1;
2637                 break;
2638
2639         case RAY_ECMD_ROAM_START:
2640                 RAY_DPRINTF(sc, RAY_DBG_RX, "ROAM_START");
2641                 sc->sc_c.np_havenet = 0;
2642                 break;
2643
2644         case RAY_ECMD_JAPAN_CALL_SIGNAL:
2645                 RAY_RECERR(sc, "unexpected JAPAN_CALL_SIGNAL");
2646                 break;
2647
2648         default:
2649                 RAY_RECERR(sc, "unknown command 0x%x", cmd);
2650                 break;
2651         }
2652
2653         RAY_CCS_FREE(sc, rcs);
2654 }
2655
2656 /*
2657  * User land entry to multicast list changes
2658  */
2659 static int
2660 ray_mcast_user(struct ray_softc *sc)
2661 {
2662         struct ray_comq_entry *com[2];
2663         int error, ncom;
2664
2665         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2666
2667         /*
2668          * Do all checking in the runq to preserve ordering.
2669          *
2670          * We run promisc to pick up changes to the ALL_MULTI
2671          * interface flag.
2672          */
2673         ncom = 0;
2674         com[ncom++] = RAY_COM_MALLOC(ray_mcast, 0);
2675         com[ncom++] = RAY_COM_MALLOC(ray_promisc, 0);
2676
2677         RAY_COM_RUNQ(sc, com, ncom, "raymcast", error);
2678
2679         /* XXX no real error processing from anything yet! */
2680
2681         RAY_COM_FREE(com, ncom);
2682
2683         return (error);
2684 }
2685
2686 /*
2687  * Runq entry to setting the multicast filter list
2688  *
2689  * MUST always be followed by a call to ray_promisc to pick up changes
2690  * to promisc flag
2691  */
2692 static void
2693 ray_mcast(struct ray_softc *sc, struct ray_comq_entry *com)
2694 {
2695         struct ifnet *ifp = sc->ifp;
2696         struct ifmultiaddr *ifma;
2697         size_t bufp;
2698         int count = 0;
2699
2700         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2701         RAY_MAP_CM(sc);
2702
2703         /*
2704          * If card is not running we don't need to update this.
2705          */
2706         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2707                 RAY_DPRINTF(sc, RAY_DBG_IOCTL, "not running");
2708                 ray_com_runq_done(sc);
2709                 return;
2710         }
2711
2712         /*
2713          * The multicast list is only 16 items long so use promiscuous
2714          * mode and don't bother updating the multicast list.
2715          */
2716         IF_ADDR_LOCK(ifp);
2717         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2718                 count++;
2719         if (count == 0) {
2720                 IF_ADDR_UNLOCK(ifp);
2721                 ray_com_runq_done(sc);
2722                 return;
2723         } else if (count > 16) {
2724                 ifp->if_flags |= IFF_ALLMULTI;
2725                 IF_ADDR_UNLOCK(ifp);
2726                 ray_com_runq_done(sc);
2727                 return;
2728         } else if (ifp->if_flags & IFF_ALLMULTI)
2729                 ifp->if_flags &= ~IFF_ALLMULTI;
2730
2731         /*
2732          * Kick the card
2733          */
2734         ray_ccs_fill(sc, com->c_ccs, RAY_CMD_UPDATE_MCAST);
2735         SRAM_WRITE_FIELD_1(sc, com->c_ccs,
2736             ray_cmd_update_mcast, c_nmcast, count);
2737         bufp = RAY_HOST_TO_ECF_BASE;
2738         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2739                 SRAM_WRITE_REGION(
2740                     sc,
2741                     bufp,
2742                     LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2743                     ETHER_ADDR_LEN
2744                 );
2745                 bufp += ETHER_ADDR_LEN;
2746         }
2747         IF_ADDR_UNLOCK(ifp);
2748
2749         ray_com_ecf(sc, com);
2750 }
2751
2752 /*
2753  * Complete the multicast filter list update
2754  */
2755 static void
2756 ray_mcast_done(struct ray_softc *sc, u_int8_t status, size_t ccs)
2757 {
2758         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_STARTJOIN, "");
2759         RAY_COM_CHECK(sc, ccs);
2760
2761         RAY_CCSERR(sc, status, if_oerrors); /* XXX error counter */
2762
2763         ray_com_ecf_done(sc);
2764 }
2765
2766 /*
2767  * Runq entry to set/reset promiscuous mode
2768  */
2769 static void
2770 ray_promisc(struct ray_softc *sc, struct ray_comq_entry *com)
2771 {
2772         struct ifnet *ifp = sc->ifp;
2773
2774         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2775         RAY_MAP_CM(sc);
2776
2777         /*
2778          * If card not running or we already have the right flags
2779          * we don't need to update this
2780          */
2781         sc->sc_d.np_promisc = !!(ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI));
2782         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING) ||
2783             (sc->sc_c.np_promisc == sc->sc_d.np_promisc)) {
2784                 ray_com_runq_done(sc);
2785                 return;
2786         }
2787
2788         /*
2789          * Kick the card
2790          */
2791         ray_ccs_fill(sc, com->c_ccs, RAY_CMD_UPDATE_PARAMS);
2792         SRAM_WRITE_FIELD_1(sc, com->c_ccs,
2793             ray_cmd_update, c_paramid, RAY_MIB_PROMISC);
2794         SRAM_WRITE_FIELD_1(sc, com->c_ccs, ray_cmd_update, c_nparam, 1);
2795         SRAM_WRITE_1(sc, RAY_HOST_TO_ECF_BASE, sc->sc_d.np_promisc);
2796
2797         ray_com_ecf(sc, com);
2798 }
2799
2800 /*
2801  * User land entry to parameter reporting
2802  *
2803  * As we by pass the runq to report current parameters this function
2804  * only provides a snap shot of the driver's state.
2805  */
2806 static int
2807 ray_repparams_user(struct ray_softc *sc, struct ray_param_req *pr)
2808 {
2809         struct ray_comq_entry *com[1];
2810         int error, ncom;
2811
2812         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2813
2814         /*
2815          * Test for illegal values or immediate responses
2816          */
2817         if (pr->r_paramid > RAY_MIB_MAX)
2818                 return (EINVAL);
2819         if ((sc->sc_version == RAY_ECFS_BUILD_4) &&
2820             !(mib_info[pr->r_paramid][0] & RAY_V4))
2821                 return (EINVAL);
2822         if ((sc->sc_version == RAY_ECFS_BUILD_5) &&
2823             !(mib_info[pr->r_paramid][0] & RAY_V5))
2824                 return (EINVAL);
2825         if (pr->r_paramid > RAY_MIB_LASTUSER) {
2826                 switch (pr->r_paramid) {
2827
2828                 case  RAY_MIB_VERSION:
2829                         if (sc->sc_version == RAY_ECFS_BUILD_4)
2830                             *pr->r_data = RAY_V4;
2831                         else
2832                             *pr->r_data = RAY_V5;
2833                         break;
2834                 case  RAY_MIB_CUR_BSSID:
2835                         bcopy(sc->sc_c.np_bss_id, pr->r_data, ETHER_ADDR_LEN);
2836                         break;
2837                 case  RAY_MIB_CUR_INITED:
2838                         *pr->r_data = sc->sc_c.np_inited;
2839                         break;
2840                 case  RAY_MIB_CUR_DEF_TXRATE:
2841                         *pr->r_data = sc->sc_c.np_def_txrate;
2842                         break;
2843                 case  RAY_MIB_CUR_ENCRYPT:
2844                         *pr->r_data = sc->sc_c.np_encrypt;
2845                         break;
2846                 case  RAY_MIB_CUR_NET_TYPE:
2847                         *pr->r_data = sc->sc_c.np_net_type;
2848                         break;
2849                 case  RAY_MIB_CUR_SSID:
2850                         bcopy(sc->sc_c.np_ssid, pr->r_data, IEEE80211_NWID_LEN);
2851                         break;
2852                 case  RAY_MIB_CUR_PRIV_START:
2853                         *pr->r_data = sc->sc_c.np_priv_start;
2854                         break;
2855                 case  RAY_MIB_CUR_PRIV_JOIN:
2856                         *pr->r_data = sc->sc_c.np_priv_join;
2857                         break;
2858                 case  RAY_MIB_DES_BSSID:
2859                         bcopy(sc->sc_d.np_bss_id, pr->r_data, ETHER_ADDR_LEN);
2860                         break;
2861                 case  RAY_MIB_DES_INITED:
2862                         *pr->r_data = sc->sc_d.np_inited;
2863                         break;
2864                 case  RAY_MIB_DES_DEF_TXRATE:
2865                         *pr->r_data = sc->sc_d.np_def_txrate;
2866                         break;
2867                 case  RAY_MIB_DES_ENCRYPT:
2868                         *pr->r_data = sc->sc_d.np_encrypt;
2869                         break;
2870                 case  RAY_MIB_DES_NET_TYPE:
2871                         *pr->r_data = sc->sc_d.np_net_type;
2872                         break;
2873                 case  RAY_MIB_DES_SSID:
2874                         bcopy(sc->sc_d.np_ssid, pr->r_data, IEEE80211_NWID_LEN);
2875                         break;
2876                 case  RAY_MIB_DES_PRIV_START:
2877                         *pr->r_data = sc->sc_d.np_priv_start;
2878                         break;
2879                 case  RAY_MIB_DES_PRIV_JOIN:
2880                         *pr->r_data = sc->sc_d.np_priv_join;
2881                         break;
2882                 case  RAY_MIB_CUR_AP_STATUS:
2883                         *pr->r_data = sc->sc_c.np_ap_status;
2884                         break;
2885                 case  RAY_MIB_CUR_PROMISC:
2886                         *pr->r_data = sc->sc_c.np_promisc;
2887                         break;
2888                 case  RAY_MIB_DES_AP_STATUS:
2889                         *pr->r_data = sc->sc_d.np_ap_status;
2890                         break;
2891                 case  RAY_MIB_DES_PROMISC:
2892                         *pr->r_data = sc->sc_d.np_promisc;
2893                         break;
2894                 case RAY_MIB_CUR_FRAMING:
2895                         *pr->r_data = sc->sc_c.np_framing;
2896                         break;
2897                 case RAY_MIB_DES_FRAMING:
2898                         *pr->r_data = sc->sc_d.np_framing;
2899                         break;
2900
2901                 default:
2902                         return (EINVAL);
2903                         break;
2904                 }
2905                 pr->r_failcause = 0;
2906                 if (sc->sc_version == RAY_ECFS_BUILD_4)
2907                     pr->r_len = mib_info[pr->r_paramid][RAY_MIB_INFO_SIZ4];
2908                 else if (sc->sc_version == RAY_ECFS_BUILD_5)
2909                     pr->r_len = mib_info[pr->r_paramid][RAY_MIB_INFO_SIZ5];
2910                 return (0);
2911         }
2912
2913         pr->r_failcause = 0;
2914         ncom = 0;
2915         com[ncom++] = RAY_COM_MALLOC(ray_repparams, RAY_COM_FWOK);
2916         com[ncom-1]->c_pr = pr;
2917
2918         RAY_COM_RUNQ(sc, com, ncom, "rayrparm", error);
2919
2920         /* XXX no real error processing from anything yet! */
2921         if (!com[0]->c_retval && pr->r_failcause)
2922                 error = EINVAL;
2923
2924         RAY_COM_FREE(com, ncom);
2925
2926         return (error);
2927 }
2928
2929 /*
2930  * Runq entry to read the required parameter
2931  *
2932  * The card and driver are happy for parameters to be read
2933  * whenever the card is plugged in
2934  */
2935 static void
2936 ray_repparams(struct ray_softc *sc, struct ray_comq_entry *com)
2937 {
2938         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2939         RAY_MAP_CM(sc);
2940
2941         /*
2942          * Kick the card
2943          */
2944         ray_ccs_fill(sc, com->c_ccs, RAY_CMD_REPORT_PARAMS);
2945         SRAM_WRITE_FIELD_1(sc, com->c_ccs,
2946             ray_cmd_report, c_paramid, com->c_pr->r_paramid);
2947         SRAM_WRITE_FIELD_1(sc, com->c_ccs, ray_cmd_report, c_nparam, 1);
2948
2949         ray_com_ecf(sc, com);
2950 }
2951
2952 /*
2953  * Complete the parameter reporting
2954  */
2955 static void
2956 ray_repparams_done(struct ray_softc *sc, u_int8_t status, size_t ccs)
2957 {
2958         struct ray_comq_entry *com;
2959
2960         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2961         RAY_MAP_CM(sc);
2962         RAY_COM_CHECK(sc, ccs);
2963
2964         RAY_CCSERR(sc, status, if_oerrors); /* XXX error counter */
2965
2966         com = TAILQ_FIRST(&sc->sc_comq);
2967         com->c_pr->r_failcause =
2968             SRAM_READ_FIELD_1(sc, ccs, ray_cmd_report, c_failcause);
2969         com->c_pr->r_len =
2970             SRAM_READ_FIELD_1(sc, ccs, ray_cmd_report, c_len);
2971         SRAM_READ_REGION(sc, RAY_ECF_TO_HOST_BASE,
2972             com->c_pr->r_data, com->c_pr->r_len);
2973
2974         ray_com_ecf_done(sc);
2975 }
2976
2977 /*
2978  * User land entry (and exit) to the error counters
2979  */
2980 static int
2981 ray_repstats_user(struct ray_softc *sc, struct ray_stats_req *sr)
2982 {
2983         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
2984
2985         sr->rxoverflow = sc->sc_rxoverflow;
2986         sr->rxcksum = sc->sc_rxcksum;
2987         sr->rxhcksum = sc->sc_rxhcksum;
2988         sr->rxnoise = sc->sc_rxnoise;
2989
2990         return (0);
2991 }
2992
2993 /*
2994  * User land entry to parameter update changes
2995  *
2996  * As a parameter change can cause the network parameters to be
2997  * invalid we have to re-start/join.
2998  */
2999 static int
3000 ray_upparams_user(struct ray_softc *sc, struct ray_param_req *pr)
3001 {
3002         struct ray_comq_entry *com[4];
3003         int error, ncom, todo;
3004 #define RAY_UPP_SJ      0x1
3005 #define RAY_UPP_PARAMS  0x2
3006
3007         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
3008
3009         /*
3010          * Check that the parameter is available based on firmware version
3011          */
3012         pr->r_failcause = 0;
3013         if (pr->r_paramid > RAY_MIB_LASTUSER)
3014                 return (EINVAL);
3015         if ((sc->sc_version == RAY_ECFS_BUILD_4) &&
3016             !(mib_info[pr->r_paramid][0] & RAY_V4))
3017                 return (EINVAL);
3018         if ((sc->sc_version == RAY_ECFS_BUILD_5) &&
3019             !(mib_info[pr->r_paramid][0] & RAY_V5))
3020                 return (EINVAL);
3021
3022         /*
3023          * Handle certain parameters specially
3024          */
3025         todo = 0;
3026         switch (pr->r_paramid) {
3027         case RAY_MIB_NET_TYPE:          /* Updated via START_NET JOIN_NET  */
3028                 sc->sc_d.np_net_type = *pr->r_data;
3029                 todo |= RAY_UPP_SJ;
3030                 break;
3031
3032         case RAY_MIB_SSID:              /* Updated via START_NET JOIN_NET  */
3033                 bcopy(pr->r_data, sc->sc_d.np_ssid, IEEE80211_NWID_LEN);
3034                 todo |= RAY_UPP_SJ;
3035                 break;
3036
3037         case RAY_MIB_PRIVACY_MUST_START:/* Updated via START_NET */
3038                 if (sc->sc_c.np_net_type != RAY_MIB_NET_TYPE_ADHOC)
3039                         return (EINVAL);
3040                 sc->sc_d.np_priv_start = *pr->r_data;
3041                 todo |= RAY_UPP_SJ;
3042                 break;
3043
3044         case RAY_MIB_PRIVACY_CAN_JOIN:  /* Updated via START_NET JOIN_NET  */
3045                 sc->sc_d.np_priv_join = *pr->r_data;
3046                 todo |= RAY_UPP_SJ;
3047                 break;
3048
3049         case RAY_MIB_BASIC_RATE_SET:
3050                 sc->sc_d.np_def_txrate = *pr->r_data;
3051                 todo |= RAY_UPP_PARAMS;
3052                 break;
3053
3054         case RAY_MIB_AP_STATUS: /* Unsupported */
3055         case RAY_MIB_MAC_ADDR:  /* XXX Need interface up but could be done */
3056         case RAY_MIB_PROMISC:   /* BPF */
3057                 return (EINVAL);
3058                 break;
3059
3060         default:
3061                 todo |= RAY_UPP_PARAMS;
3062                 todo |= RAY_UPP_SJ;
3063                 break;
3064         }
3065
3066         /*
3067          * Generate the runq entries as needed
3068          */
3069         ncom = 0;
3070         if (todo & RAY_UPP_PARAMS) {
3071                 com[ncom++] = RAY_COM_MALLOC(ray_upparams, 0);
3072                 com[ncom-1]->c_pr = pr;
3073         }
3074         if (todo & RAY_UPP_SJ) {
3075                 com[ncom++] = RAY_COM_MALLOC(ray_init_sj, 0);
3076                 com[ncom++] = RAY_COM_MALLOC(ray_init_auth, 0);
3077                 com[ncom++] = RAY_COM_MALLOC(ray_init_assoc, 0);
3078         }
3079
3080         RAY_COM_RUNQ(sc, com, ncom, "rayuparam", error);
3081
3082         /* XXX no real error processing from anything yet! */
3083         if (!com[0]->c_retval && pr->r_failcause)
3084                 error = EINVAL;
3085
3086         RAY_COM_FREE(com, ncom);
3087
3088         return (error);
3089 }
3090
3091 /*
3092  * Runq entry to update a parameter
3093  *
3094  * The card and driver are basically happy for parameters to be updated
3095  * whenever the card is plugged in. However, there may be a couple of
3096  * network hangs whilst the update is performed. Reading parameters back
3097  * straight away may give the wrong answer and some parameters cannot be
3098  * read at all. Local copies should be kept.
3099  */
3100 static void
3101 ray_upparams(struct ray_softc *sc, struct ray_comq_entry *com)
3102 {
3103         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
3104         RAY_MAP_CM(sc);
3105
3106         ray_ccs_fill(sc, com->c_ccs, RAY_CMD_UPDATE_PARAMS);
3107
3108         SRAM_WRITE_FIELD_1(sc, com->c_ccs,
3109             ray_cmd_update, c_paramid, com->c_pr->r_paramid);
3110         SRAM_WRITE_FIELD_1(sc, com->c_ccs, ray_cmd_update, c_nparam, 1);
3111         SRAM_WRITE_REGION(sc, RAY_HOST_TO_ECF_BASE,
3112             com->c_pr->r_data, com->c_pr->r_len);
3113
3114         ray_com_ecf(sc, com);
3115 }
3116
3117 /*
3118  * Complete the parameter update, note that promisc finishes up here too
3119  */
3120 static void
3121 ray_upparams_done(struct ray_softc *sc, u_int8_t status, size_t ccs)
3122 {
3123         struct ray_comq_entry *com;
3124
3125         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
3126         RAY_MAP_CM(sc);
3127         RAY_COM_CHECK(sc, ccs);
3128
3129         RAY_CCSERR(sc, status, if_oerrors); /* XXX error counter */
3130
3131         com = TAILQ_FIRST(&sc->sc_comq);
3132
3133         switch (SRAM_READ_FIELD_1(sc, ccs, ray_cmd_update, c_paramid)) {
3134
3135         case RAY_MIB_PROMISC:
3136                 sc->sc_c.np_promisc = SRAM_READ_1(sc, RAY_HOST_TO_ECF_BASE);
3137                 RAY_DPRINTF(sc, RAY_DBG_IOCTL,
3138                     "promisc value %d", sc->sc_c.np_promisc);
3139                 break;
3140
3141         default:
3142                 com->c_pr->r_failcause =
3143                     SRAM_READ_FIELD_1(sc, ccs, ray_cmd_update, c_failcause);
3144                 break;
3145
3146         }
3147
3148         ray_com_ecf_done(sc);
3149 }
3150
3151 /*
3152  * Command queuing and execution
3153  */
3154
3155 /*
3156  * Set up a comq entry struct
3157  */
3158 static struct ray_comq_entry *
3159 ray_com_init(struct ray_comq_entry *com, ray_comqfn_t function, int flags, char *mesg)
3160 {
3161         com->c_function = function;
3162         com->c_flags = flags;
3163         com->c_retval = 0;
3164         com->c_ccs = 0;
3165         com->c_wakeup = NULL;
3166         com->c_pr = NULL;
3167         com->c_mesg = mesg;
3168
3169         return (com);
3170 }
3171
3172 /*
3173  * Malloc and set up a comq entry struct
3174  */
3175 static struct ray_comq_entry *
3176 ray_com_malloc(ray_comqfn_t function, int flags, char *mesg)
3177 {
3178         struct ray_comq_entry *com;
3179
3180         MALLOC(com, struct ray_comq_entry *,
3181             sizeof(struct ray_comq_entry), M_RAYCOM, M_WAITOK);
3182     
3183         return (ray_com_init(com, function, flags, mesg));
3184 }
3185
3186 /*
3187  * Add an array of commands to the runq, get some ccs's for them and
3188  * then run, waiting on the last command.
3189  *
3190  * We add the commands to the queue first to preserve ioctl ordering.
3191  *
3192  * On recoverable errors, this routine removes the entries from the
3193  * runq. A caller can requeue the commands (and still preserve its own
3194  * processes ioctl ordering) but doesn't have to. When the card is
3195  * detached we get out quickly to prevent panics and don't bother
3196  * about the runq.
3197  */
3198 static int
3199 ray_com_runq_add(struct ray_softc *sc, struct ray_comq_entry *com[], int ncom, char *wmesg)
3200 {
3201         int i, error;
3202
3203         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_COM, "");
3204
3205         error = 0;
3206         /*
3207          * Add the commands to the runq but don't let it run until
3208          * the ccs's are allocated successfully
3209          */
3210         com[0]->c_flags |= RAY_COM_FWAIT;
3211         for (i = 0; i < ncom; i++) {
3212                 com[i]->c_wakeup = com[ncom-1];
3213                 RAY_DPRINTF(sc, RAY_DBG_COM, "adding %p", com[i]);
3214                 RAY_DCOM(sc, RAY_DBG_DCOM, com[i], "adding");
3215                 TAILQ_INSERT_TAIL(&sc->sc_comq, com[i], c_chain);
3216         }
3217         com[ncom-1]->c_flags |= RAY_COM_FWOK;
3218
3219         /*
3220          * Allocate ccs's for each command.
3221          */
3222         for (i = 0; i < ncom; i++) {
3223                 error = ray_ccs_alloc(sc, &com[i]->c_ccs, wmesg);
3224                 if (error == ENXIO)
3225                         return (ENXIO);
3226                 else if (error)
3227                         goto cleanup;
3228         }
3229
3230         /*
3231          * Allow the queue to run and sleep if needed.
3232          *
3233          * Iff the FDETACHED flag is set in the com entry we waited on
3234          * the driver is in a zombie state! The softc structure has been
3235          * freed by the generic bus detach methods - eek. We tread very
3236          * carefully!
3237          */
3238         com[0]->c_flags &= ~RAY_COM_FWAIT;
3239         ray_com_runq(sc);
3240         if (TAILQ_FIRST(&sc->sc_comq) != NULL) {
3241                 RAY_DPRINTF(sc, RAY_DBG_COM, "sleeping");
3242                 error = tsleep(com[ncom-1], PCATCH | PRIBIO, wmesg, 0);
3243                 if (com[ncom-1]->c_flags & RAY_COM_FDETACHED)
3244                         return (ENXIO);
3245                 RAY_DPRINTF(sc, RAY_DBG_COM,
3246                     "awakened, tsleep returned 0x%x", error);
3247         } else
3248                 error = 0;
3249
3250 cleanup:
3251         /*
3252          * Only clean the queue on real errors - we don't care about it
3253          * when we detach as the queue entries are freed by the callers.
3254          */
3255         if (error && (error != ENXIO))
3256                 for (i = 0; i < ncom; i++)
3257                         if (!(com[i]->c_flags & RAY_COM_FCOMPLETED)) {
3258                                 RAY_DPRINTF(sc, RAY_DBG_COM, "removing %p",
3259                                     com[i]);
3260                                 RAY_DCOM(sc, RAY_DBG_DCOM, com[i], "removing");
3261                                 TAILQ_REMOVE(&sc->sc_comq, com[i], c_chain);
3262                                 ray_ccs_free(sc, com[i]->c_ccs);
3263                                 com[i]->c_ccs = 0;
3264                         }
3265
3266         return (error);
3267 }
3268
3269 /*
3270  * Run the command at the head of the queue (if not already running)
3271  */
3272 static void
3273 ray_com_runq(struct ray_softc *sc)
3274 {
3275         struct ray_comq_entry *com;
3276
3277         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_COM, "");
3278
3279         com = TAILQ_FIRST(&sc->sc_comq);
3280         if ((com == NULL) ||
3281             (com->c_flags & RAY_COM_FRUNNING) ||
3282             (com->c_flags & RAY_COM_FWAIT) ||
3283             (com->c_flags & RAY_COM_FDETACHED))
3284                 return;
3285
3286         com->c_flags |= RAY_COM_FRUNNING;
3287         RAY_DPRINTF(sc, RAY_DBG_COM, "running %p", com);
3288         RAY_DCOM(sc, RAY_DBG_DCOM, com, "running");
3289         com->c_function(sc, com);
3290 }
3291
3292 /*
3293  * Remove run command, free ccs and wakeup caller.
3294  *
3295  * Minimal checks are done here as we ensure that the com and command
3296  * handler were matched up earlier. Must be called at splnet or higher
3297  * so that entries on the command queue are correctly removed.
3298  *
3299  * Remove the com from the comq, and wakeup the caller if it requested
3300  * to be woken. This is used for ensuring a sequence of commands
3301  * completes. Finally, re-run the queue.
3302  */
3303 static void
3304 ray_com_runq_done(struct ray_softc *sc)
3305 {
3306         struct ray_comq_entry *com;
3307
3308         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_COM, "");
3309
3310         com = TAILQ_FIRST(&sc->sc_comq); /* XXX shall we check this as below */
3311         RAY_DPRINTF(sc, RAY_DBG_COM, "removing %p", com);
3312         RAY_DCOM(sc, RAY_DBG_DCOM, com, "removing");
3313         TAILQ_REMOVE(&sc->sc_comq, com, c_chain);
3314
3315         com->c_flags &= ~RAY_COM_FRUNNING;
3316         com->c_flags |= RAY_COM_FCOMPLETED;
3317         com->c_retval = 0;
3318         ray_ccs_free(sc, com->c_ccs);
3319         com->c_ccs = 0;
3320
3321         if (com->c_flags & RAY_COM_FWOK)
3322                 wakeup(com->c_wakeup);
3323
3324         ray_com_runq(sc);
3325
3326         /* XXX what about error on completion then? deal with when i fix
3327          * XXX the status checking
3328          *
3329          * XXX all the runq_done calls from IFF_DRV_RUNNING checks in runq
3330          * XXX routines should return EIO but shouldn't abort the runq
3331          */
3332 }
3333
3334 /*
3335  * Send a command to the ECF.
3336  */
3337 static void
3338 ray_com_ecf(struct ray_softc *sc, struct ray_comq_entry *com)
3339 {
3340         int i = 0;
3341
3342         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_COM, "");
3343         RAY_MAP_CM(sc);
3344
3345         while (!RAY_ECF_READY(sc)) {
3346                 DELAY(RAY_ECF_SPIN_DELAY);
3347                 if (++i > RAY_ECF_SPIN_TRIES)
3348                         RAY_PANIC(sc, "spun too long");
3349         }
3350         if (i != 0)
3351                 RAY_RECERR(sc, "spun %d times", i);
3352
3353         RAY_DPRINTF(sc, RAY_DBG_COM, "sending %p", com);
3354         RAY_DCOM(sc, RAY_DBG_DCOM, com, "sending");
3355         SRAM_WRITE_1(sc, RAY_SCB_CCSI, RAY_CCS_INDEX(com->c_ccs));
3356         RAY_ECF_START_CMD(sc);
3357
3358         if (RAY_COM_NEEDS_TIMO(
3359             SRAM_READ_FIELD_1(sc, com->c_ccs, ray_cmd, c_cmd))) {
3360                 RAY_DPRINTF(sc, RAY_DBG_COM, "adding timeout");
3361                 sc->com_timerh = timeout(ray_com_ecf_timo, sc, RAY_COM_TIMEOUT);
3362         }
3363 }
3364
3365 /*
3366  * Deal with commands that require a timeout to test completion.
3367  *
3368  * This routine is coded to only expect one outstanding request for the
3369  * timed out requests at a time, but thats all that can be outstanding
3370  * per hardware limitations and all that we issue anyway.
3371  *
3372  * We don't do any fancy testing of the command currently issued as we
3373  * know it must be a timeout based one...unless I've got this wrong!
3374  */
3375 static void
3376 ray_com_ecf_timo(void *xsc)
3377 {
3378         struct ray_softc *sc = (struct ray_softc *)xsc;
3379         struct ray_comq_entry *com;
3380         u_int8_t cmd, status;
3381         int s;
3382
3383         s = splnet();
3384
3385         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_COM, "");
3386         RAY_MAP_CM(sc);
3387
3388         com = TAILQ_FIRST(&sc->sc_comq);
3389
3390         cmd = SRAM_READ_FIELD_1(sc, com->c_ccs, ray_cmd, c_cmd);
3391         status = SRAM_READ_FIELD_1(sc, com->c_ccs, ray_cmd, c_status);
3392         switch (status) {
3393
3394         case RAY_CCS_STATUS_COMPLETE:
3395         case RAY_CCS_STATUS_FREE:                       /* Buggy firmware */
3396                 ray_intr_ccs(sc, cmd, status, com->c_ccs);
3397                 break;
3398
3399         case RAY_CCS_STATUS_BUSY:
3400                 sc->com_timerh = timeout(ray_com_ecf_timo, sc, RAY_COM_TIMEOUT);
3401                 break;
3402
3403         default:                                        /* Replicates NetBSD */
3404                 if (sc->sc_ccsinuse[RAY_CCS_INDEX(com->c_ccs)] == 1) {
3405                         /* give a chance for the interrupt to occur */
3406                         sc->sc_ccsinuse[RAY_CCS_INDEX(com->c_ccs)] = 2;
3407                         sc->com_timerh = timeout(ray_com_ecf_timo, sc,
3408                             RAY_COM_TIMEOUT);
3409                 } else
3410                         ray_intr_ccs(sc, cmd, status, com->c_ccs);
3411                 break;
3412
3413         }
3414
3415         splx(s);
3416 }
3417
3418 /*
3419  * Called when interrupt handler for the command has done all it
3420  * needs to. Will be called at splnet.
3421  */
3422 static void
3423 ray_com_ecf_done(struct ray_softc *sc)
3424 {
3425         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_COM, "");
3426
3427         untimeout(ray_com_ecf_timo, sc, sc->com_timerh);
3428
3429         ray_com_runq_done(sc);
3430 }
3431
3432 #if RAY_DEBUG & RAY_DBG_COM
3433 /*
3434  * Process completed ECF commands that probably came from the command queue
3435  *
3436  * This routine is called after vectoring the completed ECF command
3437  * to the appropriate _done routine. It helps check everything is okay.
3438  */
3439 static void
3440 ray_com_ecf_check(struct ray_softc *sc, size_t ccs, char *mesg)
3441 {
3442         struct ray_comq_entry *com;
3443
3444         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_COM, "%s", mesg);
3445
3446         com = TAILQ_FIRST(&sc->sc_comq);
3447
3448         if (com == NULL)
3449                 RAY_PANIC(sc, "no command queue");
3450         if (com->c_ccs != ccs)
3451                 RAY_PANIC(sc, "ccs's don't match");
3452 }
3453 #endif /* RAY_DEBUG & RAY_DBG_COM */
3454
3455 /*
3456  * CCS allocators
3457  */
3458
3459 /*
3460  * Obtain a ccs for a commmand
3461  *
3462  * Returns 0 and in `ccsp' the bus offset of the free ccs. Will block
3463  * awaiting free ccs if needed - if the sleep is interrupted
3464  * EINTR/ERESTART is returned, if the card is ejected we return ENXIO.
3465  */
3466 static int
3467 ray_ccs_alloc(struct ray_softc *sc, size_t *ccsp, char *wmesg)
3468 {
3469         size_t ccs;
3470         u_int i;
3471         int error;
3472
3473         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_CCS, "");
3474         RAY_MAP_CM(sc);
3475
3476         for (;;) {
3477                 for (i = RAY_CCS_CMD_FIRST; i <= RAY_CCS_CMD_LAST; i++) {
3478                         /* we probe here to make the card go */
3479                         (void)SRAM_READ_FIELD_1(sc, RAY_CCS_ADDRESS(i), ray_cmd,
3480                             c_status);
3481                         if (!sc->sc_ccsinuse[i])
3482                                 break;
3483                 }
3484                 if (i > RAY_CCS_CMD_LAST) {
3485                         RAY_DPRINTF(sc, RAY_DBG_CCS, "sleeping");
3486                         error = tsleep(ray_ccs_alloc, PCATCH | PRIBIO,
3487                             wmesg, 0);
3488                         if ((sc == NULL) || (sc->sc_gone))
3489                                 return (ENXIO);
3490                         RAY_DPRINTF(sc, RAY_DBG_CCS,
3491                             "awakened, tsleep returned 0x%x", error);
3492                         if (error)
3493                                 return (error);
3494                 } else
3495                         break;
3496         }
3497         RAY_DPRINTF(sc, RAY_DBG_CCS, "allocated 0x%02x", i);
3498         sc->sc_ccsinuse[i] = 1;
3499         ccs = RAY_CCS_ADDRESS(i);
3500         *ccsp = ccs;
3501
3502         return (0);
3503 }
3504
3505 /*
3506  * Fill the easy bits in of a pre-allocated CCS
3507  */
3508 static void
3509 ray_ccs_fill(struct ray_softc *sc, size_t ccs, u_int cmd)
3510 {
3511         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_CCS, "");
3512         RAY_MAP_CM(sc);
3513
3514         if (ccs == 0)
3515                 RAY_PANIC(sc, "ccs not allocated");
3516
3517         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_status, RAY_CCS_STATUS_BUSY);
3518         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_cmd, cmd);
3519         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd, c_link, RAY_CCS_LINK_NULL);
3520 }
3521
3522 /*
3523  * Free up a ccs allocated via ray_ccs_alloc
3524  *
3525  * Return the old status. This routine is only used for ccs allocated via
3526  * ray_ccs_alloc (not tx, rx or ECF command requests).
3527  */
3528 static void
3529 ray_ccs_free(struct ray_softc *sc, size_t ccs)
3530 {
3531         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_CCS, "");
3532         RAY_MAP_CM(sc);
3533
3534 #if 1 | (RAY_DEBUG & RAY_DBG_CCS)
3535         if (!sc->sc_ccsinuse[RAY_CCS_INDEX(ccs)])
3536                 RAY_RECERR(sc, "freeing free ccs 0x%02zx", RAY_CCS_INDEX(ccs));
3537 #endif /* RAY_DEBUG & RAY_DBG_CCS */
3538         if (!sc->sc_gone)
3539                 RAY_CCS_FREE(sc, ccs);
3540         sc->sc_ccsinuse[RAY_CCS_INDEX(ccs)] = 0;
3541         RAY_DPRINTF(sc, RAY_DBG_CCS, "freed 0x%02zx", RAY_CCS_INDEX(ccs));
3542         wakeup(ray_ccs_alloc);
3543 }
3544
3545 /*
3546  * Obtain a ccs and tx buffer to transmit with and fill them in.
3547  *
3548  * Returns 0 and in `ccsp' the bus offset of the free ccs. Will not block
3549  * and if none available and will returns EAGAIN.
3550  *
3551  * The caller must fill in the length later.
3552  * The caller must clear the ccs on errors.
3553  */
3554 static int
3555 ray_ccs_tx(struct ray_softc *sc, size_t *ccsp, size_t *bufpp)
3556 {
3557         size_t ccs, bufp;
3558         int i;
3559         u_int8_t status;
3560
3561         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_CCS, "");
3562         RAY_MAP_CM(sc);
3563
3564         i = RAY_CCS_TX_FIRST;
3565         do {
3566                 status = SRAM_READ_FIELD_1(sc, RAY_CCS_ADDRESS(i),
3567                     ray_cmd, c_status);
3568                 if (status == RAY_CCS_STATUS_FREE)
3569                         break;
3570                 i++;
3571         } while (i <= RAY_CCS_TX_LAST);
3572         if (i > RAY_CCS_TX_LAST) {
3573                 return (EAGAIN);
3574         }
3575         RAY_DPRINTF(sc, RAY_DBG_CCS, "allocated 0x%02x", i);
3576
3577         /*
3578          * Reserve and fill the ccs - must do the length later.
3579          *
3580          * Even though build 4 and build 5 have different fields all these
3581          * are common apart from tx_rate. Neither the NetBSD driver or Linux
3582          * driver bother to overwrite this for build 4 cards.
3583          *
3584          * The start of the buffer must be aligned to a 256 byte boundary
3585          * (least significant byte of address = 0x00).
3586          */
3587         ccs = RAY_CCS_ADDRESS(i);
3588         bufp = RAY_TX_BASE + i * RAY_TX_BUF_SIZE;
3589         bufp += sc->sc_tibsize;
3590         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_status, RAY_CCS_STATUS_BUSY);
3591         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_cmd, RAY_CMD_TX_REQ);
3592         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_link, RAY_CCS_LINK_NULL);
3593         SRAM_WRITE_FIELD_2(sc, ccs, ray_cmd_tx, c_bufp, bufp);
3594         SRAM_WRITE_FIELD_1(sc,
3595             ccs, ray_cmd_tx, c_tx_rate, sc->sc_c.np_def_txrate);
3596         SRAM_WRITE_FIELD_1(sc, ccs, ray_cmd_tx, c_apm_mode, 0);
3597         bufp += sizeof(struct ray_tx_phy_header);
3598
3599         *ccsp = ccs;
3600         *bufpp = bufp;
3601         return (0);
3602 }
3603
3604 /*
3605  * Routines to obtain resources for the card
3606  */
3607
3608 /*
3609  * Allocate the attribute memory on the card
3610  *
3611  * The attribute memory space is abused by these devices as IO space. As such
3612  * the OS card services don't have a chance of knowing that they need to keep
3613  * the attribute space mapped. We have to do it manually.
3614  */
3615 static int
3616 ray_res_alloc_am(struct ray_softc *sc)
3617 {
3618         int error;
3619
3620         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_CM, "");
3621
3622         sc->am_rid = RAY_AM_RID;
3623         sc->am_res = bus_alloc_resource(sc->dev, SYS_RES_MEMORY,
3624             &sc->am_rid, 0UL, ~0UL, 0x1000, RF_ACTIVE);
3625         if (!sc->am_res) {
3626                 RAY_PRINTF(sc, "Cannot allocate attribute memory");
3627                 return (ENOMEM);
3628         }
3629         error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->dev), sc->dev,
3630             sc->am_rid, 0, NULL);
3631         if (error) {
3632                 RAY_PRINTF(sc, "CARD_SET_MEMORY_OFFSET returned 0x%0x", error);
3633                 return (error);
3634         }
3635         error = CARD_SET_RES_FLAGS(device_get_parent(sc->dev), sc->dev,
3636             SYS_RES_MEMORY, sc->am_rid, PCCARD_A_MEM_ATTR);
3637         if (error) {
3638                 RAY_PRINTF(sc, "CARD_SET_RES_FLAGS returned 0x%0x", error);
3639                 return (error);
3640         }
3641         error = CARD_SET_RES_FLAGS(device_get_parent(sc->dev), sc->dev,
3642             SYS_RES_MEMORY, sc->am_rid, PCCARD_A_MEM_8BIT);
3643         if (error) {
3644                 RAY_PRINTF(sc, "CARD_SET_RES_FLAGS returned 0x%0x", error);
3645                 return (error);
3646         }
3647         sc->am_bsh = rman_get_bushandle(sc->am_res);
3648         sc->am_bst = rman_get_bustag(sc->am_res);
3649
3650 #if RAY_DEBUG & (RAY_DBG_CM | RAY_DBG_BOOTPARAM)
3651 {
3652         u_long flags;
3653         u_int32_t offset;
3654         CARD_GET_RES_FLAGS(device_get_parent(sc->dev), sc->dev,
3655             SYS_RES_MEMORY, sc->am_rid, &flags);
3656         CARD_GET_MEMORY_OFFSET(device_get_parent(sc->dev), sc->dev,
3657             sc->am_rid, &offset);
3658         RAY_PRINTF(sc, "allocated attribute memory:\n"
3659             ".  start 0x%0lx count 0x%0lx flags 0x%0lx offset 0x%0x",
3660             bus_get_resource_start(sc->dev, SYS_RES_MEMORY, sc->am_rid),
3661             bus_get_resource_count(sc->dev, SYS_RES_MEMORY, sc->am_rid),
3662             flags, offset);
3663 }
3664 #endif /* RAY_DEBUG & (RAY_DBG_CM | RAY_DBG_BOOTPARAM) */
3665
3666         return (0);
3667 }
3668
3669 /*
3670  * Allocate the common memory on the card
3671  *
3672  * As this memory is described in the CIS, the OS card services should
3673  * have set the map up okay, but the card uses 8 bit RAM. This is not
3674  * described in the CIS.
3675  */
3676 static int
3677 ray_res_alloc_cm(struct ray_softc *sc)
3678 {
3679         u_long start, count, end;
3680         int error;
3681
3682         RAY_DPRINTF(sc, RAY_DBG_SUBR | RAY_DBG_CM, "");
3683
3684         RAY_DPRINTF(sc,RAY_DBG_CM | RAY_DBG_BOOTPARAM,
3685             "cm start 0x%0lx count 0x%0lx",
3686             bus_get_resource_start(sc->dev, SYS_RES_MEMORY, RAY_CM_RID),
3687             bus_get_resource_count(sc->dev, SYS_RES_MEMORY, RAY_CM_RID));
3688
3689         sc->cm_rid = RAY_CM_RID;
3690         start = bus_get_resource_start(sc->dev, SYS_RES_MEMORY, sc->cm_rid);
3691         count = bus_get_resource_count(sc->dev, SYS_RES_MEMORY, sc->cm_rid);
3692         end = start + count - 1;
3693         sc->cm_res = bus_alloc_resource(sc->dev, SYS_RES_MEMORY,
3694             &sc->cm_rid, start, end, count, RF_ACTIVE);
3695         if (!sc->cm_res) {
3696                 RAY_PRINTF(sc, "Cannot allocate common memory");
3697                 return (ENOMEM);
3698         }
3699         error = CARD_SET_MEMORY_OFFSET(device_get_parent(sc->dev), sc->dev,
3700             sc->cm_rid, 0, NULL);
3701         if (error) {
3702                 RAY_PRINTF(sc, "CARD_SET_MEMORY_OFFSET returned 0x%0x", error);
3703                 return (error);
3704         }
3705         error = CARD_SET_RES_FLAGS(device_get_parent(sc->dev), sc->dev,
3706             SYS_RES_MEMORY, sc->cm_rid, PCCARD_A_MEM_COM);
3707         if (error) {
3708                 RAY_PRINTF(sc, "CARD_SET_RES_FLAGS returned 0x%0x", error);
3709                 return (error);
3710         }
3711         error = CARD_SET_RES_FLAGS(device_get_parent(sc->dev), sc->dev,
3712             SYS_RES_MEMORY, sc->cm_rid, PCCARD_A_MEM_8BIT);
3713         if (error) {
3714                 RAY_PRINTF(sc, "CARD_SET_RES_FLAGS returned 0x%0x", error);
3715                 return (error);
3716         }
3717         sc->cm_bsh = rman_get_bushandle(sc->cm_res);
3718         sc->cm_bst = rman_get_bustag(sc->cm_res);
3719
3720 #if RAY_DEBUG & (RAY_DBG_CM | RAY_DBG_BOOTPARAM)
3721 {
3722         u_long flags;
3723         u_int32_t offset;
3724         CARD_GET_RES_FLAGS(device_get_parent(sc->dev), sc->dev,
3725             SYS_RES_MEMORY, sc->cm_rid, &flags);
3726         CARD_GET_MEMORY_OFFSET(device_get_parent(sc->dev), sc->dev,
3727             sc->cm_rid, &offset);
3728         RAY_PRINTF(sc, "allocated common memory:\n"
3729             ".  start 0x%0lx count 0x%0lx flags 0x%0lx offset 0x%0x",
3730             bus_get_resource_start(sc->dev, SYS_RES_MEMORY, sc->cm_rid),
3731             bus_get_resource_count(sc->dev, SYS_RES_MEMORY, sc->cm_rid),
3732             flags, offset);
3733 }
3734 #endif /* RAY_DEBUG & (RAY_DBG_CM | RAY_DBG_BOOTPARAM) */
3735
3736         return (0);
3737 }
3738
3739 /*
3740  * Get an irq and attach it to the bus
3741  */
3742 static int
3743 ray_res_alloc_irq(struct ray_softc *sc)
3744 {
3745         int error;
3746
3747         RAY_DPRINTF(sc, RAY_DBG_SUBR, "");
3748
3749         RAY_DPRINTF(sc,RAY_DBG_CM | RAY_DBG_BOOTPARAM,
3750             "irq start 0x%0lx count 0x%0lx",
3751             bus_get_resource_start(sc->dev, SYS_RES_IRQ, 0),
3752             bus_get_resource_count(sc->dev, SYS_RES_IRQ, 0));
3753
3754         sc->irq_rid = 0;
3755         sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &sc->irq_rid,
3756             RF_ACTIVE);
3757         if (!sc->irq_res) {
3758                 RAY_PRINTF(sc, "Cannot allocate irq");
3759                 return (ENOMEM);
3760         }
3761         if ((error = bus_setup_intr(sc->dev, sc->irq_res, INTR_TYPE_NET,
3762             NULL, ray_intr, sc, &sc->irq_handle)) != 0) {
3763                 RAY_PRINTF(sc, "Failed to setup irq");
3764                 return (error);
3765         }
3766         RAY_DPRINTF(sc, RAY_DBG_CM | RAY_DBG_BOOTPARAM, "allocated irq:\n"
3767             ".  start 0x%0lx count 0x%0lx",
3768             bus_get_resource_start(sc->dev, SYS_RES_IRQ, sc->irq_rid),
3769             bus_get_resource_count(sc->dev, SYS_RES_IRQ, sc->irq_rid));
3770
3771         return (0);
3772 }
3773
3774 /*
3775  * Release all of the card's resources
3776  */
3777 static void
3778 ray_res_release(struct ray_softc *sc)
3779 {
3780         if (sc->irq_res != 0) {
3781                 bus_teardown_intr(sc->dev, sc->irq_res, sc->irq_handle);
3782                 bus_release_resource(sc->dev, SYS_RES_IRQ,
3783                     sc->irq_rid, sc->irq_res);
3784                 sc->irq_res = 0;
3785         }
3786         if (sc->am_res != 0) {
3787                 bus_release_resource(sc->dev, SYS_RES_MEMORY,
3788                     sc->am_rid, sc->am_res);
3789                 sc->am_res = 0;
3790         }
3791         if (sc->cm_res != 0) {
3792                 bus_release_resource(sc->dev, SYS_RES_MEMORY,
3793                     sc->cm_rid, sc->cm_res);
3794                 sc->cm_res = 0;
3795         }
3796 }
3797
3798 /*
3799  * mbuf dump
3800  */
3801 #if RAY_DEBUG & RAY_DBG_MBUF
3802 static void
3803 ray_dump_mbuf(struct ray_softc *sc, struct mbuf *m, char *s)
3804 {
3805         u_int8_t *d, *ed;
3806         u_int i;
3807         char p[17];
3808
3809         RAY_PRINTF(sc, "%s", s);
3810         RAY_PRINTF(sc, "\nm0->data\t0x%p\nm_pkthdr.len\t%d\nm_len\t%d",
3811             mtod(m, u_int8_t *), m->m_pkthdr.len, m->m_len);
3812         i = 0;
3813         bzero(p, 17);
3814         for (; m; m = m->m_next) {
3815                 d = mtod(m, u_int8_t *);
3816                 ed = d + m->m_len;
3817
3818                 for (; d < ed; i++, d++) {
3819                         if ((i % 16) == 0) {
3820                                 printf("  %s\n\t", p);
3821                         } else if ((i % 8) == 0)
3822                                 printf("  ");
3823                         printf(" %02x", *d);
3824                         p[i % 16] = ((*d >= 0x20) && (*d < 0x80)) ? *d : '.';
3825                 }
3826         }
3827         if ((i - 1) % 16)
3828                 printf("  %s\n", p);
3829 }
3830 #endif /* RAY_DEBUG & RAY_DBG_MBUF */