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