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