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