Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / dev / netif / acx / if_acx.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/dev/netif/acx/if_acx.c,v 1.12 2006/12/22 23:26:18 swildner Exp $
35  */
36
37 /*
38  * Copyright (c) 2003-2004 wlan.kewl.org Project
39  * All rights reserved.
40  * 
41  * $Id: LICENSE,v 1.1.1.1 2004/07/01 12:20:39 darron Exp $
42  *  
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  *    
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  * 
57  *    This product includes software developed by the wlan.kewl.org Project.
58  * 
59  * 4. Neither the name of the wlan.kewl.org Project nor the names of its
60  *    contributors may be used to endorse or promote products derived from
61  *    this software without specific prior written permission.
62  * 
63  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
64  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
65  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
66  * THE wlan.kewl.org Project BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
67  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
69  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
70  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
71  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
72  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73  */
74
75 #include <sys/param.h>
76 #include <sys/endian.h>
77 #include <sys/kernel.h>
78 #include <sys/bus.h>
79 #include <sys/malloc.h>
80 #include <sys/proc.h>
81 #include <sys/rman.h>
82 #include <sys/serialize.h>
83 #include <sys/socket.h>
84 #include <sys/sockio.h>
85 #include <sys/sysctl.h>
86
87 #include <net/ethernet.h>
88 #include <net/if.h>
89 #include <net/bpf.h>
90 #include <net/if_arp.h>
91 #include <net/if_dl.h>
92 #include <net/if_media.h>
93 #include <net/ifq_var.h>
94
95 #include <netproto/802_11/ieee80211_var.h>
96
97 #include <bus/pci/pcireg.h>
98 #include <bus/pci/pcivar.h>
99 #include <bus/pci/pcidevs.h>
100
101 #define ACX_DEBUG
102
103 #include "if_acxreg.h"
104 #include "if_acxvar.h"
105 #include "acxcmd.h"
106
107 #define ACX_ENABLE_TXCHAN(sc, chan)                                     \
108 do {                                                                    \
109         if (acx_enable_txchan((sc), (chan)) != 0) {                     \
110                 if_printf(&(sc)->sc_ic.ic_if,                           \
111                           "enable TX on channel %d failed\n", (chan));  \
112         }                                                               \
113 } while (0)
114
115 #define ACX_ENABLE_RXCHAN(sc, chan)                                     \
116 do {                                                                    \
117         if (acx_enable_rxchan((sc), (chan)) != 0) {                     \
118                 if_printf(&(sc)->sc_ic.ic_if,                           \
119                           "enable RX on channel %d failed\n", (chan));  \
120         }                                                               \
121 } while (0)
122
123 #define SIOCSLOADFW     _IOW('i', 137, struct ifreq)    /* load firmware */
124 #define SIOCGRADIO      _IOW('i', 138, struct ifreq)    /* get radio type */
125 #define SIOCGSTATS      _IOW('i', 139, struct ifreq)    /* get acx stats */
126 #define SIOCSKILLFW     _IOW('i', 140, struct ifreq)    /* free firmware */
127 #define SIOCGFWVER      _IOW('i', 141, struct ifreq)    /* get firmware ver */
128 #define SIOCGHWID       _IOW('i', 142, struct ifreq)    /* get hardware id */
129
130 static int      acx_probe(device_t);
131 static int      acx_attach(device_t);
132 static int      acx_detach(device_t);
133 static int      acx_shutdown(device_t);
134 static int      acx_media_change(struct ifnet *);
135
136 static void     acx_init(void *);
137 static int      acx_stop(struct acx_softc *);
138 static void     acx_init_info_reg(struct acx_softc *);
139 static int      acx_config(struct acx_softc *);
140 static int      acx_read_config(struct acx_softc *, struct acx_config *);
141 static int      acx_write_config(struct acx_softc *, struct acx_config *);
142 static int      acx_set_crypt_keys(struct acx_softc *);
143 #ifdef foo
144 static void     acx_begin_scan(struct acx_softc *);
145 #endif
146 static void     acx_next_scan(void *);
147
148 static void     acx_start(struct ifnet *);
149 static void     acx_watchdog(struct ifnet *);
150
151 static int      acx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
152
153 static void     acx_intr(void *);
154 static void     acx_disable_intr(struct acx_softc *);
155 static void     acx_enable_intr(struct acx_softc *);
156 static void     acx_txeof(struct acx_softc *);
157 static void     acx_txerr(struct acx_softc *, uint8_t);
158 static void     acx_rxeof(struct acx_softc *);
159
160 static int      acx_dma_alloc(struct acx_softc *);
161 static void     acx_dma_free(struct acx_softc *);
162 static int      acx_init_tx_ring(struct acx_softc *);
163 static int      acx_init_rx_ring(struct acx_softc *);
164 static int      acx_newbuf(struct acx_softc *, struct acx_rxbuf *, int);
165 static int      acx_encap(struct acx_softc *, struct acx_txbuf *,
166                           struct mbuf *, struct ieee80211_node *);
167
168 static int      acx_reset(struct acx_softc *);
169
170 static int      acx_set_null_tmplt(struct acx_softc *);
171 static int      acx_set_probe_req_tmplt(struct acx_softc *, const char *, int);
172 static int      acx_set_probe_resp_tmplt(struct acx_softc *,
173                                          struct ieee80211_node *);
174 static int      acx_set_beacon_tmplt(struct acx_softc *,
175                                      struct ieee80211_node *);
176
177 static int      acx_read_eeprom(struct acx_softc *, uint32_t, uint8_t *);
178 static int      acx_read_phyreg(struct acx_softc *, uint32_t, uint8_t *);
179
180 static int      acx_copyin_firmware(struct acx_softc *, struct ifreq *);
181 static void     acx_free_firmware(struct acx_softc *);
182 static int      acx_load_firmware(struct acx_softc *, uint32_t,
183                                   const uint8_t *, int);
184 static int      acx_load_radio_firmware(struct acx_softc *, const uint8_t *,
185                                         uint32_t);
186 static int      acx_load_base_firmware(struct acx_softc *, const uint8_t *,
187                                        uint32_t);
188
189 static int      acx_newstate(struct ieee80211com *, enum ieee80211_state, int);
190
191 static int      acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS);
192
193 const struct ieee80211_rateset  acx_rates_11b =
194         { 5, { 2, 4, 11, 22, 44 } };
195 const struct ieee80211_rateset  acx_rates_11g =
196         { 13, { 2, 4, 11, 22, 44, 12, 18, 24, 36, 48, 72, 96, 108 } };
197
198 static int      acx_chanscan_rate = 5;  /* 5/second */
199 int             acx_beacon_intvl = 100; /* 100 TU */
200
201 static const struct acx_device {
202         uint16_t        vid;
203         uint16_t        did;
204         void            (*set_param)(device_t);
205         const char      *desc;
206 } acx_devices[] = {
207         { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX100A, acx100_set_param,
208           "Texas Instruments TNETW1100A Wireless Adapter" },
209         { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX100B, acx100_set_param,
210           "Texas Instruments TNETW1100B Wireless Adapter" },
211         { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX111, acx111_set_param,
212           "Texas Instruments TNETW1130 Wireless Adapter" },
213         { 0, 0, NULL, NULL }
214 };
215
216 static device_method_t acx_methods[] = {
217         DEVMETHOD(device_probe,         acx_probe),
218         DEVMETHOD(device_attach,        acx_attach),
219         DEVMETHOD(device_detach,        acx_detach),
220         DEVMETHOD(device_shutdown,      acx_shutdown),
221 #if 0
222         DEVMETHOD(device_suspend,       acx_suspend),
223         DEVMETHOD(device_resume,        acx_resume),
224 #endif
225         { 0, 0 }
226 };
227
228 static driver_t acx_driver = {
229         "acx",
230         acx_methods,
231         sizeof(struct acx_softc)
232 };
233
234 static devclass_t acx_devclass;
235
236 DRIVER_MODULE(acx, pci, acx_driver, acx_devclass, 0, 0);
237 DRIVER_MODULE(acx, cardbus, acx_driver, acx_devclass, 0, 0);
238
239 MODULE_DEPEND(acx, wlan, 1, 1, 1);
240 MODULE_DEPEND(acx, wlan_ratectl_onoe, 1, 1, 1);
241 MODULE_DEPEND(acx, wlan_ratectl_amrr, 1, 1, 1);
242 MODULE_DEPEND(acx, pci, 1, 1, 1);
243 MODULE_DEPEND(acx, cardbus, 1, 1, 1);
244
245 static int
246 acx_probe(device_t dev)
247 {
248         const struct acx_device *a;
249         uint16_t did, vid;
250
251         vid = pci_get_vendor(dev);
252         did = pci_get_device(dev);
253         for (a = acx_devices; a->desc != NULL; ++a) {
254                 if (vid == a->vid && did == a->did) {
255                         a->set_param(dev);
256                         device_set_desc(dev, a->desc);
257                         return 0;
258                 }
259         }
260         return ENXIO;
261 }
262
263 static int
264 acx_attach(device_t dev)
265 {
266         struct acx_softc *sc;
267         struct ifnet *ifp;
268         struct ieee80211com *ic;
269         int i, error;
270
271         sc = device_get_softc(dev);
272         ic = &sc->sc_ic;
273         ifp = &ic->ic_if;
274
275         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
276
277 #ifndef BURN_BRIDGES
278         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
279                 uint32_t mem1, mem2, irq;
280
281                 mem1 = pci_read_config(dev, sc->chip_mem1_rid, 4);
282                 mem2 = pci_read_config(dev, sc->chip_mem2_rid, 4);
283                 irq = pci_read_config(dev, PCIR_INTLINE, 4);
284
285                 device_printf(dev, "chip is in D%d power mode "
286                     "-- setting to D0\n", pci_get_powerstate(dev));
287
288                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
289
290                 pci_write_config(dev, sc->chip_mem1_rid, mem1, 4);
291                 pci_write_config(dev, sc->chip_mem2_rid, mem2, 4);
292                 pci_write_config(dev, PCIR_INTLINE, irq, 4);
293         }
294 #endif  /* !BURN_BRIDGE */
295
296         /* Enable bus mastering */
297         pci_enable_busmaster(dev); 
298
299         /* Allocate IO memory 1 */
300         sc->sc_mem1_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
301                                                  &sc->chip_mem1_rid,
302                                                  RF_ACTIVE);
303         if (sc->sc_mem1_res == NULL) {
304                 error = ENXIO;
305                 device_printf(dev, "can't allocate IO mem1\n");
306                 goto fail;
307         }
308         sc->sc_mem1_bt = rman_get_bustag(sc->sc_mem1_res);
309         sc->sc_mem1_bh = rman_get_bushandle(sc->sc_mem1_res);
310
311         /* Allocate IO memory 2 */
312         sc->sc_mem2_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
313                                                  &sc->chip_mem2_rid,
314                                                  RF_ACTIVE);
315         if (sc->sc_mem2_res == NULL) {
316                 error = ENXIO;
317                 device_printf(dev, "can't allocate IO mem2\n");
318                 goto fail;
319         }
320         sc->sc_mem2_bt = rman_get_bustag(sc->sc_mem2_res);
321         sc->sc_mem2_bh = rman_get_bushandle(sc->sc_mem2_res);
322
323         /* Allocate irq */
324         sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
325                                                 &sc->sc_irq_rid,
326                                                 RF_SHAREABLE | RF_ACTIVE);
327         if (sc->sc_irq_res == NULL) {
328                 error = ENXIO;
329                 device_printf(dev, "can't allocate intr\n");
330                 goto fail;
331         }
332
333         /* Initilize channel scanning timer */
334         callout_init(&sc->sc_chanscan_timer);
335
336         /* Allocate busdma stuffs */
337         error = acx_dma_alloc(sc);
338         if (error)
339                 goto fail;
340
341         /* Reset Hardware */
342         error = acx_reset(sc);
343         if (error)
344                 goto fail;
345
346         /* Disable interrupts before firmware is loaded */
347         acx_disable_intr(sc);
348
349         /* Get radio type and form factor */
350 #define EEINFO_RETRY_MAX        50
351         for (i = 0; i < EEINFO_RETRY_MAX; ++i) {
352                 uint16_t ee_info;
353
354                 ee_info = CSR_READ_2(sc, ACXREG_EEPROM_INFO);
355                 if (ACX_EEINFO_HAS_RADIO_TYPE(ee_info)) {
356                         sc->sc_form_factor = ACX_EEINFO_FORM_FACTOR(ee_info);
357                         sc->sc_radio_type = ACX_EEINFO_RADIO_TYPE(ee_info);
358                         break;
359                 }
360                 DELAY(10000);
361         }
362         if (i == EEINFO_RETRY_MAX) {
363                 error = ENXIO;
364                 goto fail;
365         }
366 #undef EEINFO_RETRY_MAX
367
368         DPRINTF((&sc->sc_ic.ic_if, "radio type %02x\n", sc->sc_radio_type));
369
370 #ifdef DUMP_EEPROM
371         for (i = 0; i < 0x40; ++i) {
372                 uint8_t val;
373
374                 error = acx_read_eeprom(sc, i, &val);
375                 if (i % 10 == 0)
376                         kprintf("\n");
377                 kprintf("%02x ", val);
378         }
379         kprintf("\n");
380 #endif  /* DUMP_EEPROM */
381
382         /* Get EEPROM version */
383         error = acx_read_eeprom(sc, ACX_EE_VERSION_OFS, &sc->sc_eeprom_ver);
384         if (error)
385                 goto fail;
386         DPRINTF((&sc->sc_ic.ic_if, "EEPROM version %u\n", sc->sc_eeprom_ver));
387
388         ifp->if_softc = sc;
389         ifp->if_init = acx_init;
390         ifp->if_ioctl = acx_ioctl;
391         ifp->if_start = acx_start;
392         ifp->if_watchdog = acx_watchdog;
393         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
394         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
395         ifq_set_ready(&ifp->if_snd);
396
397         /* Set channels */
398         for (i = 1; i <= 14; ++i) {
399                 ic->ic_channels[i].ic_freq =
400                         ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
401                 ic->ic_channels[i].ic_flags = sc->chip_chan_flags;
402         }
403
404         ic->ic_opmode = IEEE80211_M_STA;
405         ic->ic_state = IEEE80211_S_INIT;
406         ic->ic_bintval = acx_beacon_intvl;
407
408         /*
409          * NOTE: Don't overwrite ic_caps set by chip specific code
410          */
411         ic->ic_caps |= IEEE80211_C_WEP |        /* WEP */
412                        IEEE80211_C_HOSTAP |     /* Host AP modes */
413                        IEEE80211_C_IBSS |       /* IBSS modes */
414                        IEEE80211_C_SHPREAMBLE;  /* Short preamble */
415         ic->ic_caps_ext = IEEE80211_CEXT_PBCC;  /* PBCC modulation */
416
417         /* Get station id */
418         for (i = 0; i < IEEE80211_ADDR_LEN; ++i) {
419                 error = acx_read_eeprom(sc, sc->chip_ee_eaddr_ofs - i,
420                                         &ic->ic_myaddr[i]);
421         }
422
423         ieee80211_ifattach(ic);
424
425         /* Override newstate */
426         sc->sc_newstate = ic->ic_newstate;
427         ic->ic_newstate = acx_newstate;
428
429         ieee80211_media_init(ic, acx_media_change, ieee80211_media_status);
430
431         sc->sc_long_retry_limit = 4;
432         sc->sc_msdu_lifetime = 4096;
433
434         sysctl_ctx_init(&sc->sc_sysctl_ctx);
435         sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
436                                              SYSCTL_STATIC_CHILDREN(_hw),
437                                              OID_AUTO,
438                                              device_get_nameunit(dev),
439                                              CTLFLAG_RD, 0, "");
440         if (sc->sc_sysctl_tree == NULL) {
441                 device_printf(dev, "can't add sysctl node\n");
442                 error = ENXIO;
443                 goto fail1;
444         }
445
446         SYSCTL_ADD_PROC(&sc->sc_sysctl_ctx,
447                         SYSCTL_CHILDREN(sc->sc_sysctl_tree),
448                         OID_AUTO, "msdu_lifetime",
449                         CTLTYPE_INT | CTLFLAG_RW,
450                         sc, 0, acx_sysctl_msdu_lifetime, "I",
451                         "MSDU life time");
452
453         error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE, acx_intr, sc,
454                                &sc->sc_irq_handle, ifp->if_serializer);
455         if (error) {
456                 device_printf(dev, "can't set up interrupt\n");
457                 goto fail1;
458         }
459
460         if (bootverbose)
461                 ieee80211_announce(ic);
462
463         return 0;
464 fail1:
465         ieee80211_ifdetach(ic);
466 fail:
467         acx_detach(dev);
468         return error;
469 }
470
471 static int
472 acx_detach(device_t dev)
473 {
474         struct acx_softc *sc = device_get_softc(dev);
475
476         if (device_is_attached(dev)) {
477                 struct ieee80211com *ic = &sc->sc_ic;
478                 struct ifnet *ifp = &ic->ic_if;
479
480                 lwkt_serialize_enter(ifp->if_serializer);
481
482                 acx_stop(sc);
483                 acx_free_firmware(sc);
484                 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
485
486                 lwkt_serialize_exit(ifp->if_serializer);
487
488                 ieee80211_ifdetach(ic);
489         }
490
491         if (sc->sc_sysctl_tree != NULL)
492                 sysctl_ctx_free(&sc->sc_sysctl_ctx);
493
494         if (sc->sc_irq_res != NULL) {
495                 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
496                                      sc->sc_irq_res);
497         }
498         if (sc->sc_mem1_res != NULL) {
499                 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem1_rid,
500                                      sc->sc_mem1_res);
501         }
502         if (sc->sc_mem2_res != NULL) {
503                 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem2_rid,
504                                      sc->sc_mem2_res);
505         }
506
507         acx_dma_free(sc);
508         return 0;
509 }
510
511 static int
512 acx_shutdown(device_t dev)
513 {
514         struct acx_softc *sc = device_get_softc(dev);
515
516         lwkt_serialize_enter(sc->sc_ic.ic_if.if_serializer);
517         acx_stop(sc);
518         lwkt_serialize_exit(sc->sc_ic.ic_if.if_serializer);
519         return 0;
520 }
521
522 static void
523 acx_init(void *arg)
524 {
525         struct acx_softc *sc = arg;
526         struct ifnet *ifp = &sc->sc_ic.ic_if;
527         struct acx_firmware *fw = &sc->sc_firmware;
528         int error;
529
530         error = acx_stop(sc);
531         if (error)
532                 return;
533
534         if (fw->base_fw == NULL) {
535                 error = EINVAL;
536                 if_printf(ifp, "base firmware is not loaded yet\n");
537                 return;
538         }
539
540         error = acx_init_tx_ring(sc);
541         if (error) {
542                 if_printf(ifp, "can't initialize TX ring\n");
543                 goto back;
544         }
545
546         error = acx_init_rx_ring(sc);
547         if (error) {
548                 if_printf(ifp, "can't initialize RX ring\n");
549                 goto back;
550         }
551
552         error = acx_load_base_firmware(sc, fw->base_fw, fw->base_fw_len);
553         if (error)
554                 goto back;
555
556         /*
557          * Initialize command and information registers
558          * NOTE: This should be done after base firmware is loaded
559          */
560         acx_init_cmd_reg(sc);
561         acx_init_info_reg(sc);
562
563         sc->sc_flags |= ACX_FLAG_FW_LOADED;
564
565 #if 0
566         if (sc->chip_post_basefw != NULL) {
567                 error = sc->chip_post_basefw(sc);
568                 if (error)
569                         goto back;
570         }
571 #endif
572
573         if (fw->radio_fw != NULL) {
574                 error = acx_load_radio_firmware(sc, fw->radio_fw,
575                                                 fw->radio_fw_len);
576                 if (error)
577                         goto back;
578         }
579
580         error = sc->chip_init(sc);
581         if (error)
582                 goto back;
583
584         /* Get and set device various configuration */
585         error = acx_config(sc);
586         if (error)
587                 goto back;
588
589         /* Setup crypto stuffs */
590         if (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) {
591                 error = acx_set_crypt_keys(sc);
592                 if (error)
593                         goto back;
594                 sc->sc_ic.ic_flags &= ~IEEE80211_F_DROPUNENC;
595         }
596
597         /* Turn on power led */
598         CSR_CLRB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
599
600         acx_enable_intr(sc);
601
602         ifp->if_flags |= IFF_RUNNING;
603         ifp->if_flags &= ~IFF_OACTIVE;
604
605         /* Begin background scanning */
606 #ifdef foo
607         acx_begin_scan(sc);
608 #else
609         ieee80211_new_state(&sc->sc_ic, IEEE80211_S_SCAN, -1);
610 #endif
611
612 back:
613         if (error)
614                 acx_stop(sc);
615 }
616
617 static void
618 acx_init_info_reg(struct acx_softc *sc)
619 {
620         sc->sc_info = CSR_READ_4(sc, ACXREG_INFO_REG_OFFSET);
621         sc->sc_info_param = sc->sc_info + ACX_INFO_REG_SIZE;
622 }
623
624 static int
625 acx_set_crypt_keys(struct acx_softc *sc)
626 {
627         struct ieee80211com *ic = &sc->sc_ic;
628         struct acx_conf_wep_txkey wep_txkey;
629         int i, error, got_wk = 0;
630
631         for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
632                 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
633
634                 if (wk->wk_keylen == 0)
635                         continue;
636
637                 if (sc->chip_hw_crypt) {
638                         error = sc->chip_set_wepkey(sc, wk, i);
639                         if (error)
640                                 return error;
641                         got_wk = 1;
642                 } else if (wk->wk_flags & IEEE80211_KEY_XMIT) {
643                         wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
644                 }
645         }
646
647         if (!got_wk || sc->chip_hw_crypt ||
648             ic->ic_def_txkey == IEEE80211_KEYIX_NONE)
649                 return 0;
650
651         /* Set current WEP key index */
652         wep_txkey.wep_txkey = ic->ic_def_txkey;
653         if (acx_set_wep_txkey_conf(sc, &wep_txkey) != 0) {
654                 if_printf(&ic->ic_if, "set WEP txkey failed\n");
655                 return ENXIO;
656         }
657         return 0;
658 }
659
660 #ifdef foo
661 static void
662 acx_begin_scan(struct acx_softc *sc)
663 {
664         struct ieee80211com *ic = &sc->sc_ic;
665         uint8_t chan;
666
667         ieee80211_begin_scan(ic, 1);
668
669         chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
670
671         ACX_ENABLE_TXCHAN(sc, chan);
672         ACX_ENABLE_RXCHAN(sc, chan);
673
674         /* Start background scanning */
675         callout_reset(&sc->sc_chanscan_timer, hz / acx_chanscan_rate,
676                       acx_next_scan, sc);
677 }
678 #endif
679
680 static void
681 acx_next_scan(void *arg)
682 {
683         struct acx_softc *sc = arg;
684         struct ieee80211com *ic = &sc->sc_ic;
685         struct ifnet *ifp = &ic->ic_if;
686
687         lwkt_serialize_enter(ifp->if_serializer);
688
689         if (ic->ic_state == IEEE80211_S_SCAN) {
690 #if 0
691                 uint8_t chan;
692 #endif
693
694                 ieee80211_next_scan(ic);
695
696 #if 0
697                 chan = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
698
699                 ACX_ENABLE_TXCHAN(sc, chan);
700                 ACX_ENABLE_RXCHAN(sc, chan);
701
702                 callout_reset(&sc->sc_chanscan_timer, hz / acx_chanscan_rate,
703                               acx_next_scan, sc);
704 #endif
705         }
706
707         lwkt_serialize_exit(ifp->if_serializer);
708 }
709
710 static int
711 acx_stop(struct acx_softc *sc)
712 {
713         struct ieee80211com *ic = &sc->sc_ic;
714         struct ifnet *ifp = &ic->ic_if;
715         struct acx_buf_data *bd = &sc->sc_buf_data;
716         struct acx_ring_data *rd = &sc->sc_ring_data;
717         int i, error;
718
719         ASSERT_SERIALIZED(ifp->if_serializer);
720
721         sc->sc_firmware_ver = 0;
722         sc->sc_hardware_id = 0;
723
724         /* Reset hardware */
725         error = acx_reset(sc);
726         if (error)
727                 return error;
728
729         /* Firmware no longer functions after hardware reset */
730         sc->sc_flags &= ~ACX_FLAG_FW_LOADED;
731
732         acx_disable_intr(sc);
733
734         /* Stop backgroud scanning */
735         callout_stop(&sc->sc_chanscan_timer);
736
737         /* Turn off power led */
738         CSR_SETB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
739
740         /* Free TX mbuf */
741         for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
742                 struct acx_txbuf *buf;
743
744                 buf = &bd->tx_buf[i];
745
746                 if (buf->tb_mbuf != NULL) {
747                         bus_dmamap_unload(bd->mbuf_dma_tag,
748                                           buf->tb_mbuf_dmamap);
749                         m_freem(buf->tb_mbuf);
750                         buf->tb_mbuf = NULL;
751                 }
752
753                 if (buf->tb_node != NULL)
754                         ieee80211_free_node(buf->tb_node);
755                 buf->tb_node = NULL;
756         }
757
758         /* Clear TX host descriptors */
759         bzero(rd->tx_ring, ACX_TX_RING_SIZE);
760
761         /* Free RX mbuf */
762         for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
763                 if (bd->rx_buf[i].rb_mbuf != NULL) {
764                         bus_dmamap_unload(bd->mbuf_dma_tag,
765                                           bd->rx_buf[i].rb_mbuf_dmamap);
766                         m_freem(bd->rx_buf[i].rb_mbuf);
767                         bd->rx_buf[i].rb_mbuf = NULL;
768                 }
769         }
770
771         /* Clear RX host descriptors */
772         bzero(rd->rx_ring, ACX_RX_RING_SIZE);
773
774         sc->sc_tx_timer = 0;
775         ifp->if_timer = 0;
776         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
777         ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
778
779         return 0;
780 }
781
782 static int
783 acx_config(struct acx_softc *sc)
784 {
785         struct acx_config conf;
786         int error;
787
788         error = acx_read_config(sc, &conf);
789         if (error)
790                 return error;
791
792         error = acx_write_config(sc, &conf);
793         if (error)
794                 return error;
795
796         if (acx_set_probe_req_tmplt(sc, "", 0) != 0) {
797                 if_printf(&sc->sc_ic.ic_if, "can't set probe req template "
798                           "(empty ssid)\n");
799                 return ENXIO;
800         }
801
802         /* XXX for PM?? */
803         if (acx_set_null_tmplt(sc) != 0) {
804                 if_printf(&sc->sc_ic.ic_if, "can't set null data template\n");
805                 return ENXIO;
806         }
807         return 0;
808 }
809
810 static int
811 acx_read_config(struct acx_softc *sc, struct acx_config *conf)
812 {
813         struct acx_conf_eaddr addr;
814         struct acx_conf_regdom reg_dom;
815         struct acx_conf_antenna ant;
816         struct acx_conf_fwrev fw_rev;
817         uint32_t fw_rev_no;
818         uint8_t sen;
819         int i, error;
820
821         /* Get station id */
822         if (acx_get_eaddr_conf(sc, &addr) != 0) {
823                 if_printf(&sc->sc_ic.ic_if, "can't get station id\n");
824                 return ENXIO;
825         }
826
827         /*
828          * Get and print station id in case that EEPROM station id's
829          * offset is not correct
830          */
831         for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
832                 conf->eaddr[IEEE80211_ADDR_LEN - 1 - i] = addr.eaddr[i];
833         if_printf(&sc->sc_ic.ic_if, "MAC address (from firmware): %6D\n",
834                   conf->eaddr, ":");
835
836         /* Get region domain */
837         if (acx_get_regdom_conf(sc, &reg_dom) != 0) {
838                 if_printf(&sc->sc_ic.ic_if, "can't get region domain\n");
839                 return ENXIO;
840         }
841         conf->regdom = reg_dom.regdom;
842         DPRINTF((&sc->sc_ic.ic_if, "regdom %02x\n", reg_dom.regdom));
843
844         /* Get antenna */
845         if (acx_get_antenna_conf(sc, &ant) != 0) {
846                 if_printf(&sc->sc_ic.ic_if, "can't get antenna\n");
847                 return ENXIO;
848         }
849         conf->antenna = ant.antenna;
850         DPRINTF((&sc->sc_ic.ic_if, "antenna %02x\n", ant.antenna));
851
852         /* Get sensitivity XXX not used */
853         if (sc->sc_radio_type == ACX_RADIO_TYPE_MAXIM ||
854             sc->sc_radio_type == ACX_RADIO_TYPE_RFMD ||
855             sc->sc_radio_type == ACX_RADIO_TYPE_RALINK) {
856                 error = acx_read_phyreg(sc, ACXRV_PHYREG_SENSITIVITY, &sen);
857                 if (error) {
858                         if_printf(&sc->sc_ic.ic_if, "can't get sensitivity\n");
859                         return error;
860                 }
861         } else {
862                 sen = 0;
863         }
864         DPRINTF((&sc->sc_ic.ic_if, "sensitivity %02x\n", sen));
865
866         /* Get firmware revision */
867         if (acx_get_fwrev_conf(sc, &fw_rev) != 0) {
868                 if_printf(&sc->sc_ic.ic_if, "can't get firmware revision\n");
869                 return ENXIO;
870         }
871
872         if (strncmp(fw_rev.fw_rev, "Rev ", 4) != 0) {
873                 if_printf(&sc->sc_ic.ic_if, "strange revision string -- %s\n",
874                           fw_rev.fw_rev);
875                 fw_rev_no = 0x01090407;
876         } else {
877                 char *s, *endp;
878
879                 /*
880                  *  01234
881                  * "Rev xx.xx.xx.xx"
882                  *      ^ Start from here
883                  */
884                 s = &fw_rev.fw_rev[4];
885                 fw_rev_no = 0;
886                 for (i = 0; i < 4; ++i) {
887                         uint8_t val;
888
889                         val = strtoul(s, &endp, 16);
890                         fw_rev_no |= val << ((3 - i) * 8);
891
892                         if (*endp == '\0')
893                                 break;
894                         else
895                                 s = ++endp;
896                 }
897         }
898         sc->sc_firmware_ver = fw_rev_no;
899         sc->sc_hardware_id = le32toh(fw_rev.hw_id);
900         DPRINTF((&sc->sc_ic.ic_if, "fw rev %08x, hw id %08x\n",
901                  sc->sc_firmware_ver, sc->sc_hardware_id));
902
903         if (sc->chip_read_config != NULL) {
904                 error = sc->chip_read_config(sc, conf);
905                 if (error)
906                         return error;
907         }
908         return 0;
909 }
910
911 static int
912 acx_write_config(struct acx_softc *sc, struct acx_config *conf)
913 {
914         struct acx_conf_nretry_short sretry;
915         struct acx_conf_nretry_long lretry;
916         struct acx_conf_msdu_lifetime msdu_lifetime;
917         struct acx_conf_rate_fallback rate_fb;
918         struct acx_conf_antenna ant;
919         struct acx_conf_regdom reg_dom;
920         struct acx_conf_rxopt rx_opt;
921         int error;
922
923         /* Set number of long/short retry */
924         KKASSERT(sc->chip_short_retry_limit > 0);
925         sretry.nretry = sc->chip_short_retry_limit;
926         if (acx_set_nretry_short_conf(sc, &sretry) != 0) {
927                 if_printf(&sc->sc_ic.ic_if, "can't set short retry limit\n");
928                 return ENXIO;
929         }
930
931         lretry.nretry = sc->sc_long_retry_limit;
932         if (acx_set_nretry_long_conf(sc, &lretry) != 0) {
933                 if_printf(&sc->sc_ic.ic_if, "can't set long retry limit\n");
934                 return ENXIO;
935         }
936
937         /* Set MSDU lifetime */
938         msdu_lifetime.lifetime = htole32(sc->sc_msdu_lifetime);
939         if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
940                 if_printf(&sc->sc_ic.ic_if, "can't set MSDU lifetime\n");
941                 return ENXIO;
942         }
943
944         /* Enable rate fallback */
945         rate_fb.ratefb_enable = 1;
946         if (acx_set_rate_fallback_conf(sc, &rate_fb) != 0) {
947                 if_printf(&sc->sc_ic.ic_if, "can't enable rate fallback\n");
948                 return ENXIO;
949         }
950
951         /* Set antenna */
952         ant.antenna = conf->antenna;
953         if (acx_set_antenna_conf(sc, &ant) != 0) {
954                 if_printf(&sc->sc_ic.ic_if, "can't set antenna\n");
955                 return ENXIO;
956         }
957
958         /* Set region domain */
959         reg_dom.regdom = conf->regdom;
960         if (acx_set_regdom_conf(sc, &reg_dom) != 0) {
961                 if_printf(&sc->sc_ic.ic_if, "can't set region domain\n");
962                 return ENXIO;
963         }
964
965         if (sc->chip_write_config != NULL) {
966                 error = sc->chip_write_config(sc, conf);
967                 if (error)
968                         return error;
969         }
970
971         /* What we want to receive and how to receive */
972         /* XXX may not belong here, acx_init() */
973         rx_opt.opt1 = RXOPT1_FILT_FDEST | RXOPT1_INCL_RXBUF_HDR;
974         rx_opt.opt2 = RXOPT2_RECV_ASSOC_REQ |
975                       RXOPT2_RECV_AUTH |
976                       RXOPT2_RECV_BEACON |
977                       RXOPT2_RECV_CF |
978                       RXOPT2_RECV_CTRL |
979                       RXOPT2_RECV_DATA |
980                       RXOPT2_RECV_MGMT |
981                       RXOPT2_RECV_PROBE_REQ |
982                       RXOPT2_RECV_PROBE_RESP |
983                       RXOPT2_RECV_OTHER;
984         if (acx_set_rxopt_conf(sc, &rx_opt) != 0) {
985                 if_printf(&sc->sc_ic.ic_if, "can't set RX option\n");
986                 return ENXIO;
987         }
988         return 0;
989 }
990
991 static int
992 acx_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
993 {
994         struct acx_softc *sc = ifp->if_softc;
995         struct ifreq *req;
996         int error;
997
998         error = 0;
999         req = (struct ifreq *)data;
1000
1001         switch (cmd) {
1002         case SIOCSLOADFW:
1003                 error = suser_cred(cr, NULL_CRED_OKAY);
1004                 if (error)
1005                         break;
1006
1007                 error = acx_copyin_firmware(sc, req);
1008                 break;
1009         case SIOCSKILLFW:
1010                 error = suser_cred(cr, NULL_CRED_OKAY);
1011                 if (error)
1012                         break;
1013                 acx_free_firmware(sc);
1014                 break;
1015         case SIOCGRADIO:
1016                 error = copyout(&sc->sc_radio_type, req->ifr_data,
1017                                 sizeof(sc->sc_radio_type));
1018                 break;
1019         case SIOCGFWVER:
1020                 error = copyout(&sc->sc_firmware_ver, req->ifr_data,
1021                                 sizeof(sc->sc_firmware_ver));
1022                 break;
1023         case SIOCGHWID:
1024                 error = copyout(&sc->sc_hardware_id, req->ifr_data,
1025                                 sizeof(sc->sc_hardware_id));
1026                 break;
1027         case SIOCGSTATS:
1028                 error = copyout(&sc->sc_stats, req->ifr_data,
1029                                 sizeof(sc->sc_stats));
1030                 break;
1031         case SIOCSIFFLAGS:
1032                 if (ifp->if_flags & IFF_UP) {
1033                         if ((ifp->if_flags & IFF_RUNNING) == 0)
1034                                 acx_init(sc);
1035                 } else {
1036                         if (ifp->if_flags & IFF_RUNNING)
1037                                 acx_stop(sc);
1038                 }
1039                 break;
1040         case SIOCADDMULTI:
1041         case SIOCDELMULTI:
1042                 /* TODO */
1043                 break;
1044         default:
1045                 error = ieee80211_ioctl(&sc->sc_ic, cmd, data, cr);
1046                 break;
1047         }
1048
1049         if (error == ENETRESET) {
1050                 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
1051                     (IFF_RUNNING | IFF_UP))
1052                         acx_init(sc);
1053                 error = 0;
1054         }
1055         return error;
1056 }
1057
1058 static void
1059 acx_start(struct ifnet *ifp)
1060 {
1061         struct acx_softc *sc = ifp->if_softc;
1062         struct ieee80211com *ic = &sc->sc_ic;
1063         struct acx_buf_data *bd = &sc->sc_buf_data;
1064         struct acx_txbuf *buf;
1065         int trans, idx;
1066
1067         ASSERT_SERIALIZED(ifp->if_serializer);
1068
1069         if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0 ||
1070             (ifp->if_flags & IFF_RUNNING) == 0 ||
1071             (ifp->if_flags & IFF_OACTIVE))
1072                 return;
1073
1074         /*
1075          * NOTE:
1076          * We can't start from a random position that TX descriptor
1077          * is free, since hardware will be confused by that.
1078          * We have to follow the order of the TX ring.
1079          */
1080         idx = bd->tx_free_start;
1081         trans = 0;
1082         for (buf = &bd->tx_buf[idx]; buf->tb_mbuf == NULL;
1083              buf = &bd->tx_buf[idx]) {
1084                 struct ieee80211_frame *f;
1085                 struct ieee80211_node *ni = NULL;
1086                 struct mbuf *m;
1087                 int mgmt_pkt = 0;
1088
1089                 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1090                         IF_DEQUEUE(&ic->ic_mgtq, m);
1091
1092                         ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1093                         m->m_pkthdr.rcvif = NULL;
1094
1095                         mgmt_pkt = 1;
1096
1097                         /*
1098                          * Don't transmit probe response firmware will
1099                          * do it for us.
1100                          */
1101                         f = mtod(m, struct ieee80211_frame *);
1102                         if ((f->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1103                             IEEE80211_FC0_TYPE_MGT &&
1104                             (f->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1105                             IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1106                                 if (ni != NULL)
1107                                         ieee80211_free_node(ni);
1108                                 m_freem(m);
1109                                 continue;
1110                         }
1111                 } else if (!ifq_is_empty(&ifp->if_snd)) {
1112                         struct ether_header *eh;
1113
1114                         if (ic->ic_state != IEEE80211_S_RUN) {
1115                                 if_printf(ifp, "data packet dropped due to "
1116                                           "not RUN.  Current state %d\n",
1117                                           ic->ic_state);
1118                                 break;
1119                         }
1120
1121                         m = ifq_dequeue(&ifp->if_snd, NULL);
1122                         if (m == NULL)
1123                                 break;
1124
1125                         if (m->m_len < sizeof(struct ether_header)) {
1126                                 m = m_pullup(m, sizeof(struct ether_header));
1127                                 if (m == NULL) {
1128                                         ifp->if_oerrors++;
1129                                         continue;
1130                                 }
1131                         }
1132                         eh = mtod(m, struct ether_header *);
1133
1134                         ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1135                         if (ni == NULL) {
1136                                 m_freem(m);
1137                                 ifp->if_oerrors++;
1138                                 continue;
1139                         }
1140
1141                         /* TODO power save */
1142
1143                         m = ieee80211_encap(ic, m, ni);
1144                         if (m == NULL) {
1145                                 ieee80211_free_node(ni);
1146                                 ifp->if_oerrors++;
1147                                 continue;
1148                         }
1149
1150                         BPF_MTAP(ifp, m);
1151                 } else {
1152                         break;
1153                 }
1154
1155                 f = mtod(m, struct ieee80211_frame *);
1156                 if ((f->i_fc[1] & IEEE80211_FC1_WEP) && !sc->chip_hw_crypt) {
1157                         KASSERT(ni != NULL, ("TX node is NULL (WEP)\n"));
1158                         if (ieee80211_crypto_encap(ic, ni, m) == NULL) {
1159                                 ieee80211_free_node(ni);
1160                                 m_freem(m);
1161                                 ifp->if_oerrors++;
1162                                 continue;
1163                         }
1164                 }
1165
1166                 if (ic->ic_rawbpf != NULL)
1167                         bpf_mtap(ic->ic_rawbpf, m);
1168
1169                 /*
1170                  * Since mgmt data are transmitted at fixed rate
1171                  * they will not be used to do rate control.
1172                  */
1173                 if (mgmt_pkt && ni != NULL) {
1174                         ieee80211_free_node(ni);
1175                         ni = NULL;
1176                 }
1177
1178                 if (acx_encap(sc, buf, m, ni) != 0) {
1179                         /*
1180                          * NOTE: `m' will be freed in acx_encap()
1181                          * if we reach here.
1182                          */
1183                         if (ni != NULL)
1184                                 ieee80211_free_node(ni);
1185                         ifp->if_oerrors++;
1186                         continue;
1187                 }
1188
1189                 /*
1190                  * NOTE:
1191                  * 1) `m' should not be touched after acx_encap()
1192                  * 2) `node' will be used to do TX rate control during
1193                  *    acx_txeof(), so it is not freed here.  acx_txeof()
1194                  *    will free it for us
1195                  */
1196
1197                 trans = 1;
1198                 bd->tx_used_count++;
1199                 idx = (idx + 1) % ACX_TX_DESC_CNT;
1200         }
1201         bd->tx_free_start = idx;
1202
1203         if (bd->tx_used_count == ACX_TX_DESC_CNT)
1204                 ifp->if_flags |= IFF_OACTIVE;
1205
1206         if (trans && sc->sc_tx_timer == 0)
1207                 sc->sc_tx_timer = 5;
1208         ifp->if_timer = 1;
1209 }
1210
1211 static void
1212 acx_watchdog(struct ifnet *ifp)
1213 {
1214         struct acx_softc *sc = ifp->if_softc;
1215
1216         ifp->if_timer = 0;
1217
1218         if ((ifp->if_flags & IFF_RUNNING) == 0)
1219                 return;
1220
1221         if (sc->sc_tx_timer) {
1222                 if (--sc->sc_tx_timer == 0) {
1223                         if_printf(ifp, "watchdog timeout\n");
1224                         ifp->if_oerrors++;
1225                         acx_txeof(ifp->if_softc);
1226                 } else {
1227                         ifp->if_timer = 1;
1228                 }
1229         }
1230         ieee80211_watchdog(&sc->sc_ic);
1231 }
1232
1233 static void
1234 acx_intr(void *arg)
1235 {
1236         struct acx_softc *sc = arg;
1237         uint16_t intr_status;
1238
1239         if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0)
1240                 return;
1241
1242         intr_status = CSR_READ_2(sc, ACXREG_INTR_STATUS_CLR);
1243         if (intr_status == ACXRV_INTR_ALL) {
1244                 /* not our interrupt */
1245                 return;
1246         }
1247
1248         intr_status &= sc->chip_intr_enable;
1249         if (intr_status == 0) {
1250                 /* not interrupts we care about */
1251                 return;
1252         }
1253
1254         /* Acknowledge all interrupts */
1255         CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_ALL);
1256
1257         if (intr_status & ACXRV_INTR_TX_FINI)
1258                 acx_txeof(sc);
1259
1260         if (intr_status & ACXRV_INTR_RX_FINI)
1261                 acx_rxeof(sc);
1262 }
1263
1264 static void
1265 acx_disable_intr(struct acx_softc *sc)
1266 {
1267         CSR_WRITE_2(sc, ACXREG_INTR_MASK, sc->chip_intr_disable);
1268         CSR_WRITE_2(sc, ACXREG_EVENT_MASK, 0);
1269 }
1270
1271 static void
1272 acx_enable_intr(struct acx_softc *sc)
1273 {
1274         /* Mask out interrupts that are not in the enable set */
1275         CSR_WRITE_2(sc, ACXREG_INTR_MASK, ~sc->chip_intr_enable);
1276         CSR_WRITE_2(sc, ACXREG_EVENT_MASK, ACXRV_EVENT_DISABLE);
1277 }
1278
1279 static void
1280 acx_txeof(struct acx_softc *sc)
1281 {
1282         struct acx_buf_data *bd;
1283         struct acx_txbuf *buf;
1284         struct ifnet *ifp;
1285         int idx;
1286
1287         ifp = &sc->sc_ic.ic_if;
1288         ASSERT_SERIALIZED(ifp->if_serializer);
1289
1290         bd = &sc->sc_buf_data;
1291         idx = bd->tx_used_start;
1292         for (buf = &bd->tx_buf[idx]; buf->tb_mbuf != NULL;
1293              buf = &bd->tx_buf[idx]) {
1294                 uint8_t ctrl, error;
1295                 int frame_len;
1296
1297                 ctrl = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_ctrl);
1298                 if ((ctrl & (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE)) !=
1299                     (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE))
1300                         break;
1301
1302                 bus_dmamap_unload(bd->mbuf_dma_tag, buf->tb_mbuf_dmamap);
1303                 frame_len = buf->tb_mbuf->m_pkthdr.len;
1304                 m_freem(buf->tb_mbuf);
1305                 buf->tb_mbuf = NULL;
1306
1307                 error = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_error);
1308                 if (error) {
1309                         acx_txerr(sc, error);
1310                         ifp->if_oerrors++;
1311                 } else {
1312                         ifp->if_opackets++;
1313                 }
1314
1315                 if (buf->tb_node != NULL) {
1316                         sc->chip_tx_complete(sc, buf, frame_len, error);
1317                         ieee80211_free_node(buf->tb_node);
1318                         buf->tb_node = NULL;
1319                 }
1320
1321                 FW_TXDESC_SETFIELD_1(sc, buf, f_tx_ctrl, DESC_CTRL_HOSTOWN);
1322
1323                 bd->tx_used_count--;
1324
1325                 idx = (idx + 1) % ACX_TX_DESC_CNT;
1326         }
1327         bd->tx_used_start = idx;
1328
1329         sc->sc_tx_timer = bd->tx_used_count == 0 ? 0 : 5;
1330
1331         if (bd->tx_used_count != ACX_TX_DESC_CNT) {
1332                 ifp->if_flags &= ~IFF_OACTIVE;
1333                 acx_start(ifp);
1334         }
1335 }
1336
1337 static void
1338 acx_txerr(struct acx_softc *sc, uint8_t err)
1339 {
1340         struct ifnet *ifp = &sc->sc_ic.ic_if;
1341         struct acx_stats *stats = &sc->sc_stats;
1342
1343         if (err == DESC_ERR_EXCESSIVE_RETRY) {
1344                 /*
1345                  * This a common error (see comment below),
1346                  * so print it using DPRINTF()
1347                  */
1348                 DPRINTF((ifp, "TX failed -- excessive retry\n"));
1349         } else {
1350                 if_printf(ifp, "TX failed -- ");
1351         }
1352
1353         /*
1354          * Although `err' looks like bitmask, it never
1355          * has multiple bits set.
1356          */
1357         switch (err) {
1358 #if 0
1359         case DESC_ERR_OTHER_FRAG:
1360                 /* XXX what's this */
1361                 kprintf("error in other fragment\n");
1362                 stats->err_oth_frag++;
1363                 break;
1364 #endif
1365         case DESC_ERR_ABORT:
1366                 kprintf("aborted\n");
1367                 stats->err_abort++;
1368                 break;
1369         case DESC_ERR_PARAM:
1370                 kprintf("wrong paramters in descriptor\n");
1371                 stats->err_param++;
1372                 break;
1373         case DESC_ERR_NO_WEPKEY:
1374                 kprintf("WEP key missing\n");
1375                 stats->err_no_wepkey++;
1376                 break;
1377         case DESC_ERR_MSDU_TIMEOUT:
1378                 kprintf("MSDU life timeout\n");
1379                 stats->err_msdu_timeout++;
1380                 break;
1381         case DESC_ERR_EXCESSIVE_RETRY:
1382                 /*
1383                  * Possible causes:
1384                  * 1) Distance is too long
1385                  * 2) Transmit failed (e.g. no MAC level ACK)
1386                  * 3) Chip overheated (this should be rare)
1387                  */
1388                 stats->err_ex_retry++;
1389                 break;
1390         case DESC_ERR_BUF_OVERFLOW:
1391                 kprintf("buffer overflow\n");
1392                 stats->err_buf_oflow++;
1393                 break;
1394         case DESC_ERR_DMA:
1395                 kprintf("DMA error\n");
1396                 stats->err_dma++;
1397                 break;
1398         default:
1399                 kprintf("unknown error %d\n", err);
1400                 stats->err_unkn++;
1401                 break;
1402         }
1403 }
1404
1405 static void
1406 acx_rxeof(struct acx_softc *sc)
1407 {
1408         struct ieee80211com *ic = &sc->sc_ic;
1409         struct acx_ring_data *rd = &sc->sc_ring_data;
1410         struct acx_buf_data *bd = &sc->sc_buf_data;
1411         struct ifnet *ifp = &ic->ic_if;
1412         int idx, ready;
1413
1414         ASSERT_SERIALIZED(ic->ic_if.if_serializer);
1415
1416         bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1417                         BUS_DMASYNC_POSTREAD);
1418
1419         /*
1420          * Locate first "ready" rx buffer,
1421          * start from last stopped position
1422          */
1423         idx = bd->rx_scan_start;
1424         ready = 0;
1425         do {
1426                 struct acx_rxbuf *buf;
1427
1428                 buf = &bd->rx_buf[idx];
1429                 if ((buf->rb_desc->h_ctrl & htole16(DESC_CTRL_HOSTOWN)) &&
1430                     (buf->rb_desc->h_status & htole32(DESC_STATUS_FULL))) {
1431                         ready = 1;
1432                         break;
1433                 }
1434                 idx = (idx + 1) % ACX_RX_DESC_CNT;
1435         } while (idx != bd->rx_scan_start);
1436
1437         if (!ready)
1438                 return;
1439
1440         /*
1441          * NOTE: don't mess up `idx' here, it will
1442          * be used in the following code
1443          */
1444
1445         do {
1446                 struct acx_rxbuf_hdr *head;
1447                 struct acx_rxbuf *buf;
1448                 struct mbuf *m;
1449                 uint32_t desc_status;
1450                 uint16_t desc_ctrl;
1451                 int len, error;
1452
1453                 buf = &bd->rx_buf[idx];
1454
1455                 desc_ctrl = le16toh(buf->rb_desc->h_ctrl);
1456                 desc_status = le32toh(buf->rb_desc->h_status);
1457                 if (!(desc_ctrl & DESC_CTRL_HOSTOWN) ||
1458                     !(desc_status & DESC_STATUS_FULL))
1459                         break;
1460
1461                 bus_dmamap_sync(bd->mbuf_dma_tag, buf->rb_mbuf_dmamap,
1462                                 BUS_DMASYNC_POSTREAD);
1463
1464                 m = buf->rb_mbuf;
1465
1466                 error = acx_newbuf(sc, buf, 0);
1467                 if (error) {
1468                         ifp->if_ierrors++;
1469                         goto next;
1470                 }
1471
1472                 head = mtod(m, struct acx_rxbuf_hdr *);
1473
1474                 len = le16toh(head->rbh_len) & ACX_RXBUF_LEN_MASK;
1475                 if (len >= sizeof(struct ieee80211_frame_min) &&
1476                     len < MCLBYTES) {
1477                         struct ieee80211_frame *f;
1478                         struct ieee80211_node *ni;
1479
1480                         m_adj(m, sizeof(struct acx_rxbuf_hdr) +
1481                                  sc->chip_rxbuf_exhdr);
1482                         f = mtod(m, struct ieee80211_frame *);
1483
1484                         if ((f->i_fc[1] & IEEE80211_FC1_WEP) &&
1485                             sc->chip_hw_crypt) {
1486                                 /* Short circuit software WEP */
1487                                 f->i_fc[1] &= ~IEEE80211_FC1_WEP;
1488
1489                                 /* Do chip specific RX buffer processing */
1490                                 if (sc->chip_proc_wep_rxbuf != NULL) {
1491                                         sc->chip_proc_wep_rxbuf(sc, m, &len);
1492                                         f = mtod(m, struct ieee80211_frame *);
1493                                 }
1494                         }
1495
1496                         ni = ieee80211_find_rxnode(ic,
1497                                 (struct ieee80211_frame_min *)f);
1498
1499                         m->m_len = m->m_pkthdr.len = len;
1500                         m->m_pkthdr.rcvif = &ic->ic_if;
1501
1502                         ieee80211_input(ic, m, ni, head->rbh_level,
1503                                         le32toh(head->rbh_time));
1504
1505                         ieee80211_free_node(ni);
1506                         ifp->if_ipackets++;
1507                 } else {
1508                         m_freem(m);
1509                         ifp->if_ierrors++;
1510                 }
1511
1512 next:
1513                 buf->rb_desc->h_ctrl = htole16(desc_ctrl & ~DESC_CTRL_HOSTOWN);
1514                 buf->rb_desc->h_status = 0;
1515                 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1516                                 BUS_DMASYNC_PREWRITE);
1517
1518                 idx = (idx + 1) % ACX_RX_DESC_CNT;
1519         } while (idx != bd->rx_scan_start);
1520
1521         /*
1522          * Record the position so that next
1523          * time we can start from it
1524          */
1525         bd->rx_scan_start = idx;
1526 }
1527
1528 static int
1529 acx_reset(struct acx_softc *sc)
1530 {
1531         uint16_t reg;
1532
1533         /* Halt ECPU */
1534         CSR_SETB_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_HALT);
1535
1536         /* Software reset */
1537         reg = CSR_READ_2(sc, ACXREG_SOFT_RESET);
1538         CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg | ACXRV_SOFT_RESET);
1539         DELAY(100);
1540         CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg);
1541
1542         /* Initialize EEPROM */
1543         CSR_SETB_2(sc, ACXREG_EEPROM_INIT, ACXRV_EEPROM_INIT);
1544         DELAY(50000);
1545
1546         /* Test whether ECPU is stopped */
1547         reg = CSR_READ_2(sc, ACXREG_ECPU_CTRL);
1548         if (!(reg & ACXRV_ECPU_HALT)) {
1549                 if_printf(&sc->sc_ic.ic_if, "can't halt ECPU\n");
1550                 return ENXIO;
1551         }
1552         return 0;
1553 }
1554
1555 static int
1556 acx_read_eeprom(struct acx_softc *sc, uint32_t offset, uint8_t *val)
1557 {
1558         int i;
1559
1560         CSR_WRITE_4(sc, ACXREG_EEPROM_CONF, 0);
1561         CSR_WRITE_4(sc, ACXREG_EEPROM_ADDR, offset);
1562         CSR_WRITE_4(sc, ACXREG_EEPROM_CTRL, ACXRV_EEPROM_READ);
1563
1564 #define EE_READ_RETRY_MAX       100
1565         for (i = 0; i < EE_READ_RETRY_MAX; ++i) {
1566                 if (CSR_READ_2(sc, ACXREG_EEPROM_CTRL) == 0)
1567                         break;
1568                 DELAY(10000);
1569         }
1570         if (i == EE_READ_RETRY_MAX) {
1571                 if_printf(&sc->sc_ic.ic_if, "can't read EEPROM offset %x "
1572                           "(timeout)\n", offset);
1573                 return ETIMEDOUT;
1574         }
1575 #undef EE_READ_RETRY_MAX
1576
1577         *val = CSR_READ_1(sc, ACXREG_EEPROM_DATA);
1578         return 0;
1579 }
1580
1581 static int
1582 acx_read_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t *val)
1583 {
1584         int i;
1585
1586         CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1587         CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_READ);
1588
1589 #define PHY_READ_RETRY_MAX      100
1590         for (i = 0; i < PHY_READ_RETRY_MAX; ++i) {
1591                 if (CSR_READ_4(sc, ACXREG_PHY_CTRL) == 0)
1592                         break;
1593                 DELAY(10000);
1594         }
1595         if (i == PHY_READ_RETRY_MAX) {
1596                 if_printf(&sc->sc_ic.ic_if, "can't read phy reg %x (timeout)\n",
1597                           reg);
1598                 return ETIMEDOUT;
1599         }
1600 #undef PHY_READ_RETRY_MAX
1601
1602         *val = CSR_READ_1(sc, ACXREG_PHY_DATA);
1603         return 0;
1604 }
1605
1606 void
1607 acx_write_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t val)
1608 {
1609         CSR_WRITE_4(sc, ACXREG_PHY_DATA, val);
1610         CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1611         CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_WRITE);
1612 }
1613
1614 static int
1615 acx_copyin_firmware(struct acx_softc *sc, struct ifreq *req)
1616 {
1617         struct acx_firmware ufw, *kfw;
1618         uint8_t *base_fw, *radio_fw;
1619         int error;
1620
1621         kfw = &sc->sc_firmware;
1622         base_fw = NULL;
1623         radio_fw = NULL;
1624
1625         error = copyin(req->ifr_data, &ufw, sizeof(ufw));
1626         if (error)
1627                 return error;
1628
1629         /*
1630          * For combined base firmware, there is no radio firmware.
1631          * But base firmware must exist.
1632          */
1633         if (ufw.base_fw_len <= 0 || ufw.radio_fw_len < 0)
1634                 return EINVAL;
1635
1636         base_fw = kmalloc(ufw.base_fw_len, M_DEVBUF, M_INTWAIT);
1637         error = copyin(ufw.base_fw, base_fw, ufw.base_fw_len);
1638         if (error)
1639                 goto fail;
1640
1641         if (ufw.radio_fw_len > 0) {
1642                 radio_fw = kmalloc(ufw.radio_fw_len, M_DEVBUF, M_INTWAIT);
1643                 error = copyin(ufw.radio_fw, radio_fw, ufw.radio_fw_len);
1644                 if (error)
1645                         goto fail;
1646         }
1647
1648         kfw->base_fw_len = ufw.base_fw_len;
1649         if (kfw->base_fw != NULL)
1650                 kfree(kfw->base_fw, M_DEVBUF);
1651         kfw->base_fw = base_fw;
1652
1653         kfw->radio_fw_len = ufw.radio_fw_len;
1654         if (kfw->radio_fw != NULL)
1655                 kfree(kfw->radio_fw, M_DEVBUF);
1656         kfw->radio_fw = radio_fw;
1657
1658         return 0;
1659 fail:
1660         if (base_fw != NULL)
1661                 kfree(base_fw, M_DEVBUF);
1662         if (radio_fw != NULL)
1663                 kfree(radio_fw, M_DEVBUF);
1664         return error;
1665 }
1666
1667 static void
1668 acx_free_firmware(struct acx_softc *sc)
1669 {
1670         struct acx_firmware *fw = &sc->sc_firmware;
1671
1672         if (fw->base_fw != NULL) {
1673                 kfree(fw->base_fw, M_DEVBUF);
1674                 fw->base_fw = NULL;
1675                 fw->base_fw_len = 0;
1676         }
1677         if (fw->radio_fw != NULL) {
1678                 kfree(fw->radio_fw, M_DEVBUF);
1679                 fw->radio_fw = NULL;
1680                 fw->radio_fw_len = 0;
1681         }
1682 }
1683
1684 static int
1685 acx_load_base_firmware(struct acx_softc *sc, const uint8_t *base_fw,
1686                        uint32_t base_fw_len)
1687 {
1688         int i, error;
1689
1690         /* Load base firmware */
1691         error = acx_load_firmware(sc, 0, base_fw, base_fw_len);
1692         if (error) {
1693                 if_printf(&sc->sc_ic.ic_if, "can't load base firmware\n");
1694                 return error;
1695         }
1696         DPRINTF((&sc->sc_ic.ic_if, "base firmware loaded\n"));
1697
1698         /* Start ECPU */
1699         CSR_WRITE_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_START);
1700
1701         /* Wait for ECPU to be up */
1702         for (i = 0; i < 500; ++i) {
1703                 uint16_t reg;
1704
1705                 reg = CSR_READ_2(sc, ACXREG_INTR_STATUS);
1706                 if (reg & ACXRV_INTR_FCS_THRESH) {
1707                         CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_FCS_THRESH);
1708                         return 0;
1709                 }
1710                 DELAY(10000);
1711         }
1712
1713         if_printf(&sc->sc_ic.ic_if, "can't initialize ECPU (timeout)\n");
1714         return ENXIO;
1715 }
1716
1717 static int
1718 acx_load_radio_firmware(struct acx_softc *sc, const uint8_t *radio_fw,
1719                         uint32_t radio_fw_len)
1720 {
1721         struct acx_conf_mmap mem_map;
1722         uint32_t radio_fw_ofs;
1723         int error;
1724
1725         /*
1726          * Get the position, where base firmware is loaded, so that
1727          * radio firmware can be loaded after it.
1728          */
1729         if (acx_get_mmap_conf(sc, &mem_map) != 0)
1730                 return ENXIO;
1731         radio_fw_ofs = le32toh(mem_map.code_end);
1732
1733         /* Put ECPU into sleeping state, before loading radio firmware */
1734         if (acx_sleep(sc) != 0)
1735                 return ENXIO;
1736
1737         /* Load radio firmware */
1738         error = acx_load_firmware(sc, radio_fw_ofs, radio_fw, radio_fw_len);
1739         if (error) {
1740                 if_printf(&sc->sc_ic.ic_if, "can't load radio firmware\n");
1741                 return ENXIO;
1742         }
1743         DPRINTF((&sc->sc_ic.ic_if, "radio firmware loaded\n"));
1744
1745         /* Wake up sleeping ECPU, after radio firmware is loaded */
1746         if (acx_wakeup(sc) != 0)
1747                 return ENXIO;
1748
1749         /* Initialize radio */
1750         if (acx_init_radio(sc, radio_fw_ofs, radio_fw_len) != 0)
1751                 return ENXIO;
1752
1753         /* Verify radio firmware's loading position */
1754         if (acx_get_mmap_conf(sc, &mem_map) != 0)
1755                 return ENXIO;
1756         if (le32toh(mem_map.code_end) != radio_fw_ofs + radio_fw_len) {
1757                 if_printf(&sc->sc_ic.ic_if, "loaded radio firmware position "
1758                           "mismatch\n");
1759                 return ENXIO;
1760         }
1761
1762         DPRINTF((&sc->sc_ic.ic_if, "radio firmware initialized\n"));
1763         return 0;
1764 }
1765
1766 static int
1767 acx_load_firmware(struct acx_softc *sc, uint32_t offset, const uint8_t *data,
1768                   int data_len)
1769 {
1770         const uint32_t *fw;
1771         int i, fw_len;
1772
1773         fw = (const uint32_t *)data;
1774         fw_len = data_len / sizeof(uint32_t);
1775
1776         /*
1777          * LOADFW_AUTO_INC only works with some older firmware:
1778          * 1) acx100's firmware
1779          * 2) acx111's firmware whose rev is 0x00010011
1780          */
1781
1782         /* Load firmware */
1783         CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1784 #ifndef LOADFW_AUTO_INC
1785         CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1786 #else
1787         CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1788         CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1789 #endif
1790
1791         for (i = 0; i < fw_len; ++i) {
1792 #ifndef LOADFW_AUTO_INC
1793                 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1794 #endif
1795                 CSR_WRITE_4(sc, ACXREG_FWMEM_DATA, be32toh(fw[i]));
1796         }
1797
1798         /* Verify firmware */
1799         CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1800 #ifndef LOADFW_AUTO_INC
1801         CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1802 #else
1803         CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1804         CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1805 #endif
1806
1807         for (i = 0; i < fw_len; ++i) {
1808                 uint32_t val;
1809
1810 #ifndef LOADFW_AUTO_INC
1811                 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1812 #endif
1813                 val = CSR_READ_4(sc, ACXREG_FWMEM_DATA);
1814                 if (be32toh(fw[i]) != val) {
1815                         if_printf(&sc->sc_ic.ic_if, "fireware mismatch "
1816                                   "fw %08x  loaded %08x\n", fw[i], val);
1817                         return ENXIO;
1818                 }
1819         }
1820         return 0;
1821 }
1822
1823 static int
1824 acx_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1825 {
1826         struct acx_softc *sc = ic->ic_if.if_softc;
1827         int error = 0, mode = 0;
1828
1829         ASSERT_SERIALIZED(ic->ic_if.if_serializer);
1830
1831         ieee80211_ratectl_newstate(ic, nstate);
1832
1833         switch (nstate) {
1834         case IEEE80211_S_SCAN:
1835                 if (ic->ic_state != IEEE80211_S_INIT) {
1836                         uint8_t chan;
1837
1838                         chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
1839                         ACX_ENABLE_TXCHAN(sc, chan);
1840                         ACX_ENABLE_RXCHAN(sc, chan);
1841
1842                         callout_reset(&sc->sc_chanscan_timer,
1843                                       hz / acx_chanscan_rate,
1844                                       acx_next_scan, sc);
1845                 }
1846                 break;
1847         case IEEE80211_S_AUTH:
1848                 if (ic->ic_opmode == IEEE80211_M_STA) {
1849                         struct ieee80211_node *ni;
1850
1851                         ni = ic->ic_bss;
1852
1853                         if (acx_join_bss(sc, ACX_MODE_STA, ni) != 0) {
1854                                 if_printf(&ic->ic_if, "join BSS failed\n");
1855                                 error = 1;
1856                                 goto back;
1857                         }
1858
1859                         DPRINTF((&ic->ic_if, "join BSS\n"));
1860                         if (ic->ic_state == IEEE80211_S_ASSOC) {
1861                                 DPRINTF((&ic->ic_if,
1862                                          "change from assoc to run\n"));
1863                                 ic->ic_state = IEEE80211_S_RUN;
1864                         }
1865                 }
1866                 break;
1867         case IEEE80211_S_RUN:
1868                 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1869                     ic->ic_opmode == IEEE80211_M_HOSTAP) {
1870                         struct ieee80211_node *ni;
1871                         uint8_t chan;
1872
1873                         ni = ic->ic_bss;
1874                         chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1875
1876                         error = 1;
1877
1878                         if (acx_enable_txchan(sc, chan) != 0) {
1879                                 if_printf(&ic->ic_if,
1880                                           "enable TX on channel %d failed\n",
1881                                           chan);
1882                                 goto back;
1883                         }
1884
1885                         if (acx_enable_rxchan(sc, chan) != 0) {
1886                                 if_printf(&ic->ic_if,
1887                                           "enable RX on channel %d failed\n",
1888                                           chan);
1889                                 goto back;
1890                         }
1891
1892                         if (acx_set_beacon_tmplt(sc, ni) != 0) {
1893                                 if_printf(&ic->ic_if,
1894                                           "set bescon template failed\n");
1895                                 goto back;
1896                         }
1897
1898                         if (acx_set_probe_resp_tmplt(sc, ni) != 0) {
1899                                 if_printf(&ic->ic_if, "set probe response "
1900                                           "template failed\n");
1901                                 goto back;
1902                         }
1903
1904                         if (ic->ic_opmode == IEEE80211_M_IBSS)
1905                                 mode = ACX_MODE_ADHOC;
1906                         else
1907                                 mode = ACX_MODE_AP;
1908
1909                         if (acx_join_bss(sc, mode, ni) != 0) {
1910                                 if_printf(&ic->ic_if, "acx_join_ibss failed\n");
1911                                 goto back;
1912                         }
1913
1914                         DPRINTF((&ic->ic_if, "join IBSS\n"));
1915                         error = 0;
1916                 }
1917                 break;
1918         default:
1919                 break;
1920         }
1921
1922 back:
1923         if (error) {
1924                 /* XXX */
1925                 nstate = IEEE80211_S_INIT;
1926                 arg = -1;
1927         }
1928         return sc->sc_newstate(ic, nstate, arg);
1929 }
1930
1931 int
1932 acx_init_tmplt_ordered(struct acx_softc *sc)
1933 {
1934         struct acx_tmplt_tim tim;
1935
1936 #define INIT_TMPLT(name)                        \
1937 do {                                            \
1938         if (acx_init_##name##_tmplt(sc) != 0)   \
1939                 return 1;                       \
1940 } while (0)
1941
1942         /*
1943          * NOTE:
1944          * Order of templates initialization:
1945          * 1) Probe request
1946          * 2) NULL data
1947          * 3) Beacon
1948          * 4) TIM
1949          * 5) Probe response
1950          * Above order is critical to get a correct memory map.
1951          */
1952         INIT_TMPLT(probe_req);
1953         INIT_TMPLT(null_data);
1954         INIT_TMPLT(beacon);
1955         INIT_TMPLT(tim);
1956         INIT_TMPLT(probe_resp);
1957
1958         /* Setup TIM template */
1959         bzero(&tim, sizeof(tim));
1960         tim.tim_eid = IEEE80211_ELEMID_TIM;
1961         tim.tim_len = ACX_TIM_LEN(ACX_TIM_BITMAP_LEN);
1962         if (_acx_set_tim_tmplt(sc, &tim,
1963                                ACX_TMPLT_TIM_SIZ(ACX_TIM_BITMAP_LEN)) != 0) {
1964                 if_printf(&sc->sc_ic.ic_if, "%s can't set tim tmplt\n",
1965                           __func__);
1966                 return 1;
1967         }
1968
1969 #undef INIT_TMPLT
1970         return 0;
1971 }
1972
1973 static void
1974 acx_ring_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1975 {
1976         *((uint32_t *)arg) = seg->ds_addr;
1977 }
1978
1979 static int
1980 acx_dma_alloc(struct acx_softc *sc)
1981 {
1982         struct acx_ring_data *rd = &sc->sc_ring_data;
1983         struct acx_buf_data *bd = &sc->sc_buf_data;
1984         int i, error;
1985
1986         /* Allocate DMA stuffs for RX descriptors  */
1987         error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
1988                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1989                                    NULL, NULL,
1990                                    ACX_RX_RING_SIZE, 1, ACX_RX_RING_SIZE,
1991                                    0, &rd->rx_ring_dma_tag);
1992         if (error) {
1993                 if_printf(&sc->sc_ic.ic_if, "can't create rx ring dma tag\n");
1994                 return error;
1995         }
1996
1997         error = bus_dmamem_alloc(rd->rx_ring_dma_tag, (void **)&rd->rx_ring,
1998                                  BUS_DMA_WAITOK | BUS_DMA_ZERO,
1999                                  &rd->rx_ring_dmamap);
2000         if (error) {
2001                 if_printf(&sc->sc_ic.ic_if,
2002                           "can't allocate rx ring dma memory\n");
2003                 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2004                 rd->rx_ring_dma_tag = NULL;
2005                 return error;
2006         }
2007
2008         error = bus_dmamap_load(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2009                                 rd->rx_ring, ACX_RX_RING_SIZE,
2010                                 acx_ring_dma_addr, &rd->rx_ring_paddr,
2011                                 BUS_DMA_WAITOK);
2012         if (error) {
2013                 if_printf(&sc->sc_ic.ic_if, "can't get rx ring dma address\n");
2014                 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
2015                                 rd->rx_ring_dmamap);
2016                 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2017                 rd->rx_ring_dma_tag = NULL;
2018                 return error;
2019         }
2020
2021         /* Allocate DMA stuffs for TX descriptors */
2022         error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
2023                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2024                                    NULL, NULL,
2025                                    ACX_TX_RING_SIZE, 1, ACX_TX_RING_SIZE,
2026                                    0, &rd->tx_ring_dma_tag);
2027         if (error) {
2028                 if_printf(&sc->sc_ic.ic_if, "can't create tx ring dma tag\n");
2029                 return error;
2030         }
2031
2032         error = bus_dmamem_alloc(rd->tx_ring_dma_tag, (void **)&rd->tx_ring,
2033                                  BUS_DMA_WAITOK | BUS_DMA_ZERO,
2034                                  &rd->tx_ring_dmamap);
2035         if (error) {
2036                 if_printf(&sc->sc_ic.ic_if,
2037                           "can't allocate tx ring dma memory\n");
2038                 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2039                 rd->tx_ring_dma_tag = NULL;
2040                 return error;
2041         }
2042
2043         error = bus_dmamap_load(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2044                                 rd->tx_ring, ACX_TX_RING_SIZE,
2045                                 acx_ring_dma_addr, &rd->tx_ring_paddr,
2046                                 BUS_DMA_WAITOK);
2047         if (error) {
2048                 if_printf(&sc->sc_ic.ic_if, "can't get tx ring dma address\n");
2049                 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2050                                 rd->tx_ring_dmamap);
2051                 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2052                 rd->tx_ring_dma_tag = NULL;
2053                 return error;
2054         }
2055
2056         /* Create DMA tag for RX/TX mbuf map */
2057         error = bus_dma_tag_create(NULL, 1, 0,
2058                                    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2059                                    NULL, NULL,
2060                                    MCLBYTES, 1, MCLBYTES,
2061                                    0, &bd->mbuf_dma_tag);
2062         if (error) {
2063                 if_printf(&sc->sc_ic.ic_if, "can't create mbuf dma tag\n");
2064                 return error;
2065         }
2066
2067         /* Create a spare RX DMA map */
2068         error = bus_dmamap_create(bd->mbuf_dma_tag, 0, &bd->mbuf_tmp_dmamap);
2069         if (error) {
2070                 if_printf(&sc->sc_ic.ic_if, "can't create tmp mbuf dma map\n");
2071                 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2072                 bd->mbuf_dma_tag = NULL;
2073                 return error;
2074         }
2075
2076         /* Create DMA map for RX mbufs */
2077         for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2078                 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2079                                           &bd->rx_buf[i].rb_mbuf_dmamap);
2080                 if (error) {
2081                         if_printf(&sc->sc_ic.ic_if, "can't create rx mbuf "
2082                                   "dma map (%d)\n", i);
2083                         return error;
2084                 }
2085                 bd->rx_buf[i].rb_desc = &rd->rx_ring[i];
2086         }
2087
2088         /* Create DMA map for TX mbufs */
2089         for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2090                 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2091                                           &bd->tx_buf[i].tb_mbuf_dmamap);
2092                 if (error) {
2093                         if_printf(&sc->sc_ic.ic_if, "can't create tx mbuf "
2094                                   "dma map (%d)\n", i);
2095                         return error;
2096                 }
2097                 bd->tx_buf[i].tb_desc1 = &rd->tx_ring[i * 2];
2098                 bd->tx_buf[i].tb_desc2 = &rd->tx_ring[(i * 2) + 1];
2099         }
2100
2101         return 0;
2102 }
2103
2104 static void
2105 acx_dma_free(struct acx_softc *sc)
2106 {
2107         struct acx_ring_data *rd = &sc->sc_ring_data;
2108         struct acx_buf_data *bd = &sc->sc_buf_data;
2109         int i;
2110
2111         if (rd->rx_ring_dma_tag != NULL) {
2112                 bus_dmamap_unload(rd->rx_ring_dma_tag, rd->rx_ring_dmamap);
2113                 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
2114                                 rd->rx_ring_dmamap);
2115                 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2116         }
2117
2118         if (rd->tx_ring_dma_tag != NULL) {
2119                 bus_dmamap_unload(rd->tx_ring_dma_tag, rd->tx_ring_dmamap);
2120                 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2121                                 rd->tx_ring_dmamap);
2122                 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2123         }
2124
2125         for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2126                 if (bd->rx_buf[i].rb_desc != NULL) {
2127                         if (bd->rx_buf[i].rb_mbuf != NULL) {
2128                                 bus_dmamap_unload(bd->mbuf_dma_tag,
2129                                                   bd->rx_buf[i].rb_mbuf_dmamap);
2130                                 m_freem(bd->rx_buf[i].rb_mbuf);
2131                         }
2132                         bus_dmamap_destroy(bd->mbuf_dma_tag,
2133                                            bd->rx_buf[i].rb_mbuf_dmamap);
2134                 }
2135         }
2136
2137         for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2138                 if (bd->tx_buf[i].tb_desc1 != NULL) {
2139                         if (bd->tx_buf[i].tb_mbuf != NULL) {
2140                                 bus_dmamap_unload(bd->mbuf_dma_tag,
2141                                                   bd->tx_buf[i].tb_mbuf_dmamap);
2142                                 m_freem(bd->tx_buf[i].tb_mbuf);
2143                         }
2144                         bus_dmamap_destroy(bd->mbuf_dma_tag,
2145                                            bd->tx_buf[i].tb_mbuf_dmamap);
2146                 }
2147         }
2148
2149         if (bd->mbuf_dma_tag != NULL) {
2150                 bus_dmamap_destroy(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap);
2151                 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2152         }
2153 }
2154
2155 static int
2156 acx_init_tx_ring(struct acx_softc *sc)
2157 {
2158         struct acx_ring_data *rd;
2159         struct acx_buf_data *bd;
2160         uint32_t paddr;
2161         int i;
2162
2163         rd = &sc->sc_ring_data;
2164         paddr = rd->tx_ring_paddr;
2165         for (i = 0; i < (ACX_TX_DESC_CNT * 2) - 1; ++i) {
2166                 paddr += sizeof(struct acx_host_desc);
2167
2168                 rd->tx_ring[i].h_ctrl = htole16(DESC_CTRL_HOSTOWN);
2169
2170                 if (i == (ACX_TX_DESC_CNT * 2) - 1)
2171                         rd->tx_ring[i].h_next_desc = htole32(rd->tx_ring_paddr);
2172                 else
2173                         rd->tx_ring[i].h_next_desc = htole32(paddr);
2174         }
2175
2176         bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2177                         BUS_DMASYNC_PREWRITE);
2178
2179         bd = &sc->sc_buf_data;
2180         bd->tx_free_start = 0;
2181         bd->tx_used_start = 0;
2182         bd->tx_used_count = 0;
2183
2184         return 0;
2185 }
2186
2187 static int
2188 acx_init_rx_ring(struct acx_softc *sc)
2189 {
2190         struct acx_ring_data *rd;
2191         struct acx_buf_data *bd;
2192         uint32_t paddr;
2193         int i;
2194
2195         bd = &sc->sc_buf_data;
2196         rd = &sc->sc_ring_data;
2197         paddr = rd->rx_ring_paddr;
2198
2199         for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2200                 int error;
2201
2202                 paddr += sizeof(struct acx_host_desc);
2203
2204                 error = acx_newbuf(sc, &bd->rx_buf[i], 1);
2205                 if (error)
2206                         return error;
2207
2208                 if (i == ACX_RX_DESC_CNT - 1)
2209                         rd->rx_ring[i].h_next_desc = htole32(rd->rx_ring_paddr);
2210                 else
2211                         rd->rx_ring[i].h_next_desc = htole32(paddr);
2212         }
2213
2214         bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2215                         BUS_DMASYNC_PREWRITE);
2216
2217         bd->rx_scan_start = 0;
2218         return 0;
2219 }
2220
2221 static void
2222 acx_buf_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2223                  bus_size_t mapsz, int error)
2224 {
2225         if (error)
2226                 return;
2227
2228         /* XXX */
2229         KASSERT(nseg == 1, ("too many RX dma segments\n"));
2230         *((uint32_t *)arg) = seg->ds_addr;
2231 }
2232
2233 static int
2234 acx_newbuf(struct acx_softc *sc, struct acx_rxbuf *rb, int wait)
2235 {
2236         struct acx_buf_data *bd;
2237         struct mbuf *m;
2238         bus_dmamap_t map;
2239         uint32_t paddr;
2240         int error;
2241
2242         bd = &sc->sc_buf_data;
2243
2244         m = m_getcl(wait ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2245         if (m == NULL)
2246                 return ENOBUFS;
2247
2248         m->m_len = m->m_pkthdr.len = MCLBYTES;
2249
2250         error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap,
2251                                      m, acx_buf_dma_addr, &paddr,
2252                                      wait ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
2253         if (error) {
2254                 m_freem(m);
2255                 if_printf(&sc->sc_ic.ic_if, "can't map rx mbuf %d\n", error);
2256                 return error;
2257         }
2258
2259         /* Unload originally mapped mbuf */
2260         bus_dmamap_unload(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap);
2261
2262         /* Swap this dmamap with tmp dmamap */
2263         map = rb->rb_mbuf_dmamap;
2264         rb->rb_mbuf_dmamap = bd->mbuf_tmp_dmamap;
2265         bd->mbuf_tmp_dmamap = map;
2266
2267         rb->rb_mbuf = m;
2268         rb->rb_desc->h_data_paddr = htole32(paddr);
2269         rb->rb_desc->h_data_len = htole16(m->m_len);
2270
2271         bus_dmamap_sync(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap,
2272                         BUS_DMASYNC_PREREAD);
2273         return 0;
2274 }
2275
2276 static int
2277 acx_encap(struct acx_softc *sc, struct acx_txbuf *txbuf, struct mbuf *m,
2278           struct ieee80211_node *ni)
2279 {
2280         struct acx_buf_data *bd = &sc->sc_buf_data;
2281         struct acx_ring_data *rd = &sc->sc_ring_data;
2282         uint32_t paddr;
2283         uint8_t ctrl;
2284         int error;
2285
2286         KASSERT(txbuf->tb_mbuf == NULL, ("free TX buf has mbuf installed\n"));
2287         error = 0;
2288
2289         if (m->m_pkthdr.len > MCLBYTES) {
2290                 if_printf(&sc->sc_ic.ic_if, "mbuf too big\n");
2291                 error = E2BIG;
2292                 goto back;
2293         } else if (m->m_pkthdr.len < ACX_FRAME_HDRLEN) {
2294                 if_printf(&sc->sc_ic.ic_if, "mbuf too small\n");
2295                 error = EINVAL;
2296                 goto back;
2297         }
2298
2299         error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2300                                      m, acx_buf_dma_addr, &paddr,
2301                                      BUS_DMA_NOWAIT);
2302         if (error && error != EFBIG) {
2303                 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf1 %d\n", error);
2304                 goto back;
2305         }
2306
2307         if (error) {    /* error == EFBIG */
2308                 struct mbuf *m_new;
2309
2310                 m_new = m_defrag(m, MB_DONTWAIT);
2311                 if (m_new == NULL) {
2312                         if_printf(&sc->sc_ic.ic_if, "can't defrag tx mbuf\n");
2313                         error = ENOBUFS;
2314                         goto back;
2315                 } else {
2316                         m = m_new;
2317                 }
2318
2319                 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag,
2320                                              txbuf->tb_mbuf_dmamap, m,
2321                                              acx_buf_dma_addr, &paddr,
2322                                              BUS_DMA_NOWAIT);
2323                 if (error) {
2324                         if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf2 %d\n",
2325                                   error);
2326                         goto back;
2327                 }
2328         }
2329
2330         error = 0;
2331
2332         bus_dmamap_sync(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2333                         BUS_DMASYNC_PREWRITE);
2334
2335         txbuf->tb_mbuf = m;
2336         txbuf->tb_node = ni;
2337
2338         /*
2339          * TX buffers are accessed in following way:
2340          * acx_fw_txdesc -> acx_host_desc -> buffer
2341          *
2342          * It is quite strange that acx also querys acx_host_desc next to
2343          * the one we have assigned to acx_fw_txdesc even if first one's
2344          * acx_host_desc.h_data_len == acx_fw_txdesc.f_tx_len
2345          *
2346          * So we allocate two acx_host_desc for one acx_fw_txdesc and
2347          * assign the first acx_host_desc to acx_fw_txdesc
2348          *
2349          * For acx111
2350          * host_desc1.h_data_len = buffer_len
2351          * host_desc2.h_data_len = buffer_len - mac_header_len
2352          *
2353          * For acx100
2354          * host_desc1.h_data_len = mac_header_len
2355          * host_desc2.h_data_len = buffer_len - mac_header_len
2356          */
2357
2358         txbuf->tb_desc1->h_data_paddr = htole32(paddr);
2359         txbuf->tb_desc2->h_data_paddr = htole32(paddr + ACX_FRAME_HDRLEN);
2360
2361         txbuf->tb_desc1->h_data_len =
2362                 htole16(sc->chip_txdesc1_len ? sc->chip_txdesc1_len
2363                                              : m->m_pkthdr.len);
2364         txbuf->tb_desc2->h_data_len =
2365                 htole16(m->m_pkthdr.len - ACX_FRAME_HDRLEN);
2366
2367         /*
2368          * NOTE:
2369          * We can't simply assign f_tx_ctrl, we will first read it back
2370          * and change it bit by bit
2371          */
2372         ctrl = FW_TXDESC_GETFIELD_1(sc, txbuf, f_tx_ctrl);
2373         ctrl |= sc->chip_fw_txdesc_ctrl; /* extra chip specific flags */
2374         ctrl &= ~(DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE);
2375
2376         FW_TXDESC_SETFIELD_4(sc, txbuf, f_tx_len, m->m_pkthdr.len);
2377         FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_error, 0);
2378         FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_data_nretry, 0);
2379         FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_nretry, 0);
2380         FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_ok, 0);
2381         sc->chip_set_fw_txdesc_rate(sc, txbuf, ni, m->m_pkthdr.len);
2382
2383         txbuf->tb_desc1->h_ctrl = 0;
2384         txbuf->tb_desc2->h_ctrl = 0;
2385         bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2386                         BUS_DMASYNC_PREWRITE);
2387
2388         FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl2, 0);
2389         FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl, ctrl);
2390
2391         /* Tell chip to inform us about TX completion */
2392         CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_TX_FINI);
2393 back:
2394         if (error)
2395                 m_freem(m);
2396         return error;
2397 }
2398
2399 static int
2400 acx_set_null_tmplt(struct acx_softc *sc)
2401 {
2402         struct acx_tmplt_null_data n;
2403         struct ieee80211_frame *f;
2404
2405         bzero(&n, sizeof(n));
2406
2407         f = &n.data;
2408         f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
2409                      IEEE80211_FC0_SUBTYPE_NODATA;
2410         IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2411         IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2412         IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2413
2414         return _acx_set_null_data_tmplt(sc, &n, sizeof(n));
2415 }
2416
2417 static int
2418 acx_set_probe_req_tmplt(struct acx_softc *sc, const char *ssid, int ssid_len)
2419 {
2420         struct acx_tmplt_probe_req req;
2421         struct ieee80211_frame *f;
2422         uint8_t *v;
2423         int vlen;
2424
2425         bzero(&req, sizeof(req));
2426
2427         f = &req.data.u_data.f;
2428         f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2429                      IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2430         IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2431         IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2432         IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2433
2434         v = req.data.u_data.var;
2435         v = ieee80211_add_ssid(v, ssid, ssid_len);
2436         v = ieee80211_add_rates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2437         v = ieee80211_add_xrates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2438         vlen = v - req.data.u_data.var;
2439
2440         return _acx_set_probe_req_tmplt(sc, &req,
2441                                         ACX_TMPLT_PROBE_REQ_SIZ(vlen));
2442 }
2443
2444 static int
2445 acx_set_probe_resp_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2446 {
2447         struct ieee80211com *ic = &sc->sc_ic;
2448         struct acx_tmplt_probe_resp resp;
2449         struct ieee80211_frame *f;
2450         struct mbuf *m;
2451         int len;
2452
2453         m = ieee80211_probe_resp_alloc(ic, ni);
2454         if (m == NULL)
2455                 return 1;
2456         DPRINTF((&ic->ic_if, "%s alloc probe resp size %d\n", __func__,
2457                  m->m_pkthdr.len));
2458
2459         f = mtod(m, struct ieee80211_frame *);
2460         IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2461
2462         bzero(&resp, sizeof(resp));
2463         m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)&resp.data);
2464         len = m->m_pkthdr.len + sizeof(resp.size);
2465         m_freem(m);
2466
2467         return _acx_set_probe_resp_tmplt(sc, &resp, len);
2468 }
2469
2470 static int
2471 acx_set_beacon_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2472 {
2473         struct ieee80211com *ic = &sc->sc_ic;
2474         struct acx_tmplt_beacon beacon;
2475         struct ieee80211_beacon_offsets bo;
2476         struct mbuf *m;
2477         int len;
2478
2479         bzero(&bo, sizeof(bo));
2480         m = ieee80211_beacon_alloc(ic, ni, &bo);
2481         if (m == NULL)
2482                 return 1;
2483         DPRINTF((&ic->ic_if, "%s alloc beacon size %d\n", __func__,
2484                  m->m_pkthdr.len));
2485
2486         bzero(&beacon, sizeof(beacon));
2487         m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)&beacon.data);
2488         len = m->m_pkthdr.len + sizeof(beacon.size);
2489         m_freem(m);
2490
2491         return _acx_set_beacon_tmplt(sc, &beacon, len);
2492 }
2493
2494 static int
2495 acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS)
2496 {
2497         struct acx_softc *sc = arg1;
2498         struct ifnet *ifp = &sc->sc_ic.ic_if;
2499         int error = 0, v;
2500
2501         lwkt_serialize_enter(ifp->if_serializer);
2502
2503         v = sc->sc_msdu_lifetime;
2504         error = sysctl_handle_int(oidp, &v, 0, req);
2505         if (error || req->newptr == NULL)
2506                 goto back;
2507         if (v <= 0) {
2508                 error = EINVAL;
2509                 goto back;
2510         }
2511
2512         if (sc->sc_flags & ACX_FLAG_FW_LOADED) {
2513                 struct acx_conf_msdu_lifetime msdu_lifetime;
2514
2515                 msdu_lifetime.lifetime = htole32(v);
2516                 if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
2517                         if_printf(&sc->sc_ic.ic_if,
2518                                   "can't set MSDU lifetime\n");
2519                         error = ENXIO;
2520                         goto back;
2521                 }
2522         }
2523         sc->sc_msdu_lifetime = v;
2524 back:
2525         lwkt_serialize_exit(ifp->if_serializer);
2526         return error;
2527 }
2528
2529 static int
2530 acx_media_change(struct ifnet *ifp)
2531 {
2532         int error;
2533
2534         error = ieee80211_media_change(ifp);
2535         if (error != ENETRESET)
2536                 return error;
2537
2538         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
2539                 acx_init(ifp->if_softc);
2540         return 0;
2541 }