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