2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sys/dev/netif/acx/if_acx.c,v 1.29 2008/06/06 10:47:14 sephe Exp $
38 * Copyright (c) 2003-2004 wlan.kewl.org Project
39 * All rights reserved.
41 * $Id: LICENSE,v 1.1.1.1 2004/07/01 12:20:39 darron Exp $
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
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.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
57 * This product includes software developed by the wlan.kewl.org Project.
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.
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.
75 #include <sys/param.h>
76 #include <sys/endian.h>
77 #include <sys/kernel.h>
79 #include <sys/firmware.h>
80 #include <sys/interrupt.h>
81 #include <sys/malloc.h>
84 #include <sys/serialize.h>
85 #include <sys/socket.h>
86 #include <sys/sockio.h>
87 #include <sys/sysctl.h>
89 #include <net/ethernet.h>
92 #include <net/if_arp.h>
93 #include <net/if_dl.h>
94 #include <net/if_media.h>
95 #include <net/ifq_var.h>
97 #include <netproto/802_11/ieee80211_var.h>
98 #include <netproto/802_11/ieee80211_radiotap.h>
99 #include <netproto/802_11/wlan_ratectl/amrr/ieee80211_amrr_param.h>
100 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
102 #include <bus/pci/pcireg.h>
103 #include <bus/pci/pcivar.h>
104 #include <bus/pci/pcidevs.h>
108 #include <dev/netif/acx/if_acxreg.h>
109 #include <dev/netif/acx/if_acxvar.h>
110 #include <dev/netif/acx/acxcmd.h>
112 static int acx_probe(device_t);
113 static int acx_attach(device_t);
114 static int acx_detach(device_t);
115 static int acx_shutdown(device_t);
117 static void acx_init(void *);
118 static void acx_start(struct ifnet *);
119 static int acx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
120 static void acx_watchdog(struct ifnet *);
122 static void acx_intr(void *);
123 static void acx_txeof(struct acx_softc *);
124 static void acx_txerr(struct acx_softc *, uint8_t);
125 static void acx_rxeof(struct acx_softc *);
126 static void acx_disable_intr(struct acx_softc *);
127 static void acx_enable_intr(struct acx_softc *);
129 static int acx_reset(struct acx_softc *);
130 static int acx_stop(struct acx_softc *);
131 static void acx_init_info_reg(struct acx_softc *);
132 static int acx_config(struct acx_softc *);
133 static int acx_read_config(struct acx_softc *, struct acx_config *);
134 static int acx_write_config(struct acx_softc *, struct acx_config *);
135 static int acx_rx_config(struct acx_softc *, int);
136 static int acx_set_crypt_keys(struct acx_softc *);
137 static void acx_calibrate(void *);
139 static int acx_dma_alloc(struct acx_softc *);
140 static void acx_dma_free(struct acx_softc *);
141 static int acx_init_tx_ring(struct acx_softc *);
142 static int acx_init_rx_ring(struct acx_softc *);
143 static int acx_newbuf(struct acx_softc *, struct acx_rxbuf *, int);
144 static int acx_encap(struct acx_softc *, struct acx_txbuf *,
145 struct mbuf *, struct ieee80211_node *);
147 static int acx_set_null_tmplt(struct acx_softc *);
148 static int acx_set_probe_req_tmplt(struct acx_softc *, const char *, int);
149 static int acx_set_probe_resp_tmplt(struct acx_softc *,
150 struct ieee80211_node *);
151 static int acx_set_beacon_tmplt(struct acx_softc *,
152 struct ieee80211_node *);
154 static int acx_read_eeprom(struct acx_softc *, uint32_t, uint8_t *);
155 static int acx_read_phyreg(struct acx_softc *, uint32_t, uint8_t *);
157 static int acx_alloc_firmware(struct acx_softc *);
158 static void acx_free_firmware(struct acx_softc *);
159 static int acx_setup_firmware(struct acx_softc *, struct fw_image *,
160 const uint8_t **, int *);
161 static int acx_load_firmware(struct acx_softc *, uint32_t,
162 const uint8_t *, int);
163 static int acx_load_radio_firmware(struct acx_softc *, const uint8_t *,
165 static int acx_load_base_firmware(struct acx_softc *, const uint8_t *,
168 static void acx_next_scan(void *);
169 static int acx_set_chan(struct acx_softc *, struct ieee80211_channel *);
171 static int acx_media_change(struct ifnet *);
172 static int acx_newstate(struct ieee80211com *, enum ieee80211_state, int);
174 static int acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS);
175 static int acx_sysctl_free_firmware(SYSCTL_HANDLER_ARGS);
177 const struct ieee80211_rateset acx_rates_11b =
178 { 5, { 2, 4, 11, 22, 44 } };
179 const struct ieee80211_rateset acx_rates_11g =
180 { 13, { 2, 4, 11, 22, 44, 12, 18, 24, 36, 48, 72, 96, 108 } };
182 static const struct acx_device {
185 void (*set_param)(device_t);
188 { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX100A, acx100_set_param,
189 "Texas Instruments TNETW1100A Wireless Adapter" },
190 { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX100B, acx100_set_param,
191 "Texas Instruments TNETW1100B Wireless Adapter" },
192 { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX111, acx111_set_param,
193 "Texas Instruments TNETW1130 Wireless Adapter" },
197 static device_method_t acx_methods[] = {
198 DEVMETHOD(device_probe, acx_probe),
199 DEVMETHOD(device_attach, acx_attach),
200 DEVMETHOD(device_detach, acx_detach),
201 DEVMETHOD(device_shutdown, acx_shutdown),
203 DEVMETHOD(device_suspend, acx_suspend),
204 DEVMETHOD(device_resume, acx_resume),
209 static driver_t acx_driver = {
212 sizeof(struct acx_softc)
215 static devclass_t acx_devclass;
217 DRIVER_MODULE(acx, pci, acx_driver, acx_devclass, 0, 0);
218 DRIVER_MODULE(acx, cardbus, acx_driver, acx_devclass, 0, 0);
220 MODULE_DEPEND(acx, wlan, 1, 1, 1);
221 MODULE_DEPEND(acx, wlan_ratectl_onoe, 1, 1, 1);
222 MODULE_DEPEND(acx, wlan_ratectl_amrr, 1, 1, 1);
223 MODULE_DEPEND(acx, pci, 1, 1, 1);
224 MODULE_DEPEND(acx, cardbus, 1, 1, 1);
227 acx_get_rssi(struct acx_softc *sc, uint8_t raw)
231 rssi = ((sc->chip_rssi_corr / 2) + (raw * 5)) / sc->chip_rssi_corr;
232 return rssi > 100 ? 100 : rssi;
236 acx_probe(device_t dev)
238 const struct acx_device *a;
241 vid = pci_get_vendor(dev);
242 did = pci_get_device(dev);
243 for (a = acx_devices; a->desc != NULL; ++a) {
244 if (vid == a->vid && did == a->did) {
246 device_set_desc(dev, a->desc);
254 acx_attach(device_t dev)
256 struct acx_softc *sc;
258 struct ieee80211com *ic;
261 sc = device_get_softc(dev);
265 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
268 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
269 uint32_t mem1, mem2, irq;
271 mem1 = pci_read_config(dev, sc->chip_mem1_rid, 4);
272 mem2 = pci_read_config(dev, sc->chip_mem2_rid, 4);
273 irq = pci_read_config(dev, PCIR_INTLINE, 4);
275 device_printf(dev, "chip is in D%d power mode "
276 "-- setting to D0\n", pci_get_powerstate(dev));
278 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
280 pci_write_config(dev, sc->chip_mem1_rid, mem1, 4);
281 pci_write_config(dev, sc->chip_mem2_rid, mem2, 4);
282 pci_write_config(dev, PCIR_INTLINE, irq, 4);
284 #endif /* !BURN_BRIDGE */
286 /* Enable bus mastering */
287 pci_enable_busmaster(dev);
289 /* Allocate IO memory 1 */
290 sc->sc_mem1_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
293 if (sc->sc_mem1_res == NULL) {
295 device_printf(dev, "can't allocate IO mem1\n");
298 sc->sc_mem1_bt = rman_get_bustag(sc->sc_mem1_res);
299 sc->sc_mem1_bh = rman_get_bushandle(sc->sc_mem1_res);
301 /* Allocate IO memory 2 */
302 sc->sc_mem2_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
305 if (sc->sc_mem2_res == NULL) {
307 device_printf(dev, "can't allocate IO mem2\n");
310 sc->sc_mem2_bt = rman_get_bustag(sc->sc_mem2_res);
311 sc->sc_mem2_bh = rman_get_bushandle(sc->sc_mem2_res);
314 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
316 RF_SHAREABLE | RF_ACTIVE);
317 if (sc->sc_irq_res == NULL) {
319 device_printf(dev, "can't allocate intr\n");
323 /* Initialize channel scanning timer */
324 callout_init(&sc->sc_scan_timer);
326 /* Initialize calibration timer */
327 callout_init(&sc->sc_calibrate_timer);
329 /* Allocate busdma stuffs */
330 error = acx_dma_alloc(sc);
335 error = acx_reset(sc);
339 /* Disable interrupts before firmware is loaded */
340 acx_disable_intr(sc);
342 /* Get radio type and form factor */
343 #define EEINFO_RETRY_MAX 50
344 for (i = 0; i < EEINFO_RETRY_MAX; ++i) {
347 ee_info = CSR_READ_2(sc, ACXREG_EEPROM_INFO);
348 if (ACX_EEINFO_HAS_RADIO_TYPE(ee_info)) {
349 sc->sc_form_factor = ACX_EEINFO_FORM_FACTOR(ee_info);
350 sc->sc_radio_type = ACX_EEINFO_RADIO_TYPE(ee_info);
355 if (i == EEINFO_RETRY_MAX) {
359 #undef EEINFO_RETRY_MAX
361 DPRINTF((&sc->sc_ic.ic_if, "radio type %02x\n", sc->sc_radio_type));
364 for (i = 0; i < 0x40; ++i) {
367 error = acx_read_eeprom(sc, i, &val);
370 kprintf("%02x ", val);
373 #endif /* DUMP_EEPROM */
375 /* Get EEPROM version */
376 error = acx_read_eeprom(sc, ACX_EE_VERSION_OFS, &sc->sc_eeprom_ver);
379 DPRINTF((&sc->sc_ic.ic_if, "EEPROM version %u\n", sc->sc_eeprom_ver));
382 * Initialize device sysctl before ieee80211_ifattach()
384 sc->sc_long_retry_limit = 4;
385 sc->sc_msdu_lifetime = 4096;
386 sc->sc_scan_dwell = 200; /* 200 milliseconds */
387 sc->sc_calib_intvl = 3 * 60; /* 3 minutes */
389 sysctl_ctx_init(&sc->sc_sysctl_ctx);
390 sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
391 SYSCTL_STATIC_CHILDREN(_hw),
393 device_get_nameunit(dev),
395 if (sc->sc_sysctl_tree == NULL) {
396 device_printf(dev, "can't add sysctl node\n");
400 SYSCTL_ADD_PROC(&sc->sc_sysctl_ctx,
401 SYSCTL_CHILDREN(sc->sc_sysctl_tree),
402 OID_AUTO, "msdu_lifetime",
403 CTLTYPE_INT | CTLFLAG_RW,
404 sc, 0, acx_sysctl_msdu_lifetime, "I",
406 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
407 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
408 "long_retry_limit", CTLFLAG_RW,
409 &sc->sc_long_retry_limit, 0, "Long retry limit");
410 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
411 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
412 "scan_dwell", CTLFLAG_RW,
413 &sc->sc_scan_dwell, 0, "Scan channel dwell time (ms)");
414 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
415 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
416 "calib_intvl", CTLFLAG_RW,
417 &sc->sc_calib_intvl, 0, "Calibration interval (second)");
420 * Nodes for firmware operation
422 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
423 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
424 "combined_radio_fw", CTLFLAG_RW,
425 &sc->sc_firmware.combined_radio_fw, 0,
426 "Radio and base firmwares are combined");
427 SYSCTL_ADD_PROC(&sc->sc_sysctl_ctx,
428 SYSCTL_CHILDREN(sc->sc_sysctl_tree),
430 CTLTYPE_INT | CTLFLAG_RW,
431 sc, 0, acx_sysctl_free_firmware, "I",
435 * Nodes for statistics
437 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
438 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
439 "frag_error", CTLFLAG_RW, &sc->sc_stats.err_oth_frag,
440 0, "Fragment errors");
441 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
442 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
443 "tx_abort", CTLFLAG_RW, &sc->sc_stats.err_abort,
445 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
446 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
447 "tx_invalid", CTLFLAG_RW, &sc->sc_stats.err_param,
448 0, "Invalid TX param in TX descriptor");
449 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
450 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
451 "no_wepkey", CTLFLAG_RW, &sc->sc_stats.err_no_wepkey,
452 0, "No WEP key exists");
453 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
454 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
455 "msdu_timeout", CTLFLAG_RW,
456 &sc->sc_stats.err_msdu_timeout,
458 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
459 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
460 "ex_txretry", CTLFLAG_RW, &sc->sc_stats.err_ex_retry,
461 0, "Excessive TX retries");
462 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
463 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
464 "buf_oflow", CTLFLAG_RW, &sc->sc_stats.err_buf_oflow,
465 0, "Buffer overflows");
466 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
467 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
468 "dma_error", CTLFLAG_RW, &sc->sc_stats.err_dma,
470 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
471 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
472 "unkn_error", CTLFLAG_RW, &sc->sc_stats.err_unkn,
473 0, "Unknown errors");
476 ifp->if_init = acx_init;
477 ifp->if_ioctl = acx_ioctl;
478 ifp->if_start = acx_start;
479 ifp->if_watchdog = acx_watchdog;
480 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
481 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
482 ifq_set_ready(&ifp->if_snd);
485 for (i = 1; i <= 14; ++i) {
486 ic->ic_channels[i].ic_freq =
487 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
488 ic->ic_channels[i].ic_flags = sc->chip_chan_flags;
491 ic->ic_opmode = IEEE80211_M_STA;
492 ic->ic_state = IEEE80211_S_INIT;
495 * NOTE: Don't overwrite ic_caps set by chip specific code
497 ic->ic_caps |= IEEE80211_C_WEP | /* WEP */
498 IEEE80211_C_HOSTAP | /* HostAP mode */
499 IEEE80211_C_MONITOR | /* Monitor mode */
500 IEEE80211_C_IBSS | /* IBSS modes */
501 IEEE80211_C_SHPREAMBLE; /* Short preamble */
502 ic->ic_caps_ext = IEEE80211_CEXT_PBCC; /* PBCC modulation */
505 for (i = 0; i < IEEE80211_ADDR_LEN; ++i) {
506 error = acx_read_eeprom(sc, sc->chip_ee_eaddr_ofs - i,
510 ieee80211_ifattach(ic);
512 /* Enable software beacon missing */
513 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
515 /* Override newstate */
516 sc->sc_newstate = ic->ic_newstate;
517 ic->ic_newstate = acx_newstate;
519 ieee80211_media_init(ic, acx_media_change, ieee80211_media_status);
522 * Radio tap attaching
524 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
525 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
528 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(uint32_t));
529 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
530 sc->sc_tx_th.wt_ihdr.it_present = htole32(ACX_TX_RADIOTAP_PRESENT);
532 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(uint32_t));
533 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
534 sc->sc_rx_th.wr_ihdr.it_present = htole32(ACX_RX_RADIOTAP_PRESENT);
536 error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE, acx_intr, sc,
537 &sc->sc_irq_handle, ifp->if_serializer);
539 device_printf(dev, "can't set up interrupt\n");
541 ieee80211_ifdetach(ic);
545 ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->sc_irq_res));
546 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
549 ieee80211_announce(ic);
558 acx_detach(device_t dev)
560 struct acx_softc *sc = device_get_softc(dev);
562 if (device_is_attached(dev)) {
563 struct ieee80211com *ic = &sc->sc_ic;
564 struct ifnet *ifp = &ic->ic_if;
566 lwkt_serialize_enter(ifp->if_serializer);
569 acx_free_firmware(sc);
570 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
572 lwkt_serialize_exit(ifp->if_serializer);
575 ieee80211_ifdetach(ic);
578 if (sc->sc_sysctl_tree != NULL)
579 sysctl_ctx_free(&sc->sc_sysctl_ctx);
581 if (sc->sc_irq_res != NULL) {
582 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
585 if (sc->sc_mem1_res != NULL) {
586 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem1_rid,
589 if (sc->sc_mem2_res != NULL) {
590 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem2_rid,
599 acx_shutdown(device_t dev)
601 struct acx_softc *sc = device_get_softc(dev);
603 lwkt_serialize_enter(sc->sc_ic.ic_if.if_serializer);
605 lwkt_serialize_exit(sc->sc_ic.ic_if.if_serializer);
612 struct acx_softc *sc = arg;
613 struct ieee80211com *ic = &sc->sc_ic;
614 struct ifnet *ifp = &ic->ic_if;
615 struct acx_firmware *fw = &sc->sc_firmware;
618 error = acx_stop(sc);
622 error = acx_alloc_firmware(sc);
626 error = acx_init_tx_ring(sc);
628 if_printf(ifp, "can't initialize TX ring\n");
632 error = acx_init_rx_ring(sc);
634 if_printf(ifp, "can't initialize RX ring\n");
638 error = acx_load_base_firmware(sc, fw->base_fw, fw->base_fw_len);
643 * Initialize command and information registers
644 * NOTE: This should be done after base firmware is loaded
646 acx_init_cmd_reg(sc);
647 acx_init_info_reg(sc);
649 sc->sc_flags |= ACX_FLAG_FW_LOADED;
652 if (sc->chip_post_basefw != NULL) {
653 error = sc->chip_post_basefw(sc);
659 if (fw->radio_fw != NULL) {
660 error = acx_load_radio_firmware(sc, fw->radio_fw,
666 error = sc->chip_init(sc);
670 /* Get and set device various configuration */
671 error = acx_config(sc);
675 /* Setup crypto stuffs */
676 if (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) {
677 error = acx_set_crypt_keys(sc);
680 sc->sc_ic.ic_flags &= ~IEEE80211_F_DROPUNENC;
683 /* Turn on power led */
684 CSR_CLRB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
688 ifp->if_flags |= IFF_RUNNING;
689 ifp->if_flags &= ~IFF_OACTIVE;
691 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
692 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
693 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_SCAN, -1);
695 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
703 acx_init_info_reg(struct acx_softc *sc)
705 sc->sc_info = CSR_READ_4(sc, ACXREG_INFO_REG_OFFSET);
706 sc->sc_info_param = sc->sc_info + ACX_INFO_REG_SIZE;
710 acx_set_crypt_keys(struct acx_softc *sc)
712 struct ieee80211com *ic = &sc->sc_ic;
713 struct acx_conf_wep_txkey wep_txkey;
714 int i, error, got_wk = 0;
716 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
717 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
719 if (wk->wk_keylen == 0)
722 if (sc->chip_hw_crypt) {
723 error = sc->chip_set_wepkey(sc, wk, i);
727 } else if (wk->wk_flags & IEEE80211_KEY_XMIT) {
728 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
732 if (!got_wk || sc->chip_hw_crypt ||
733 ic->ic_def_txkey == IEEE80211_KEYIX_NONE)
736 /* Set current WEP key index */
737 wep_txkey.wep_txkey = ic->ic_def_txkey;
738 if (acx_set_wep_txkey_conf(sc, &wep_txkey) != 0) {
739 if_printf(&ic->ic_if, "set WEP txkey failed\n");
746 acx_next_scan(void *arg)
748 struct acx_softc *sc = arg;
749 struct ieee80211com *ic = &sc->sc_ic;
750 struct ifnet *ifp = &ic->ic_if;
752 lwkt_serialize_enter(ifp->if_serializer);
754 if (ic->ic_state == IEEE80211_S_SCAN)
755 ieee80211_next_scan(ic);
757 lwkt_serialize_exit(ifp->if_serializer);
761 acx_stop(struct acx_softc *sc)
763 struct ieee80211com *ic = &sc->sc_ic;
764 struct ifnet *ifp = &ic->ic_if;
765 struct acx_buf_data *bd = &sc->sc_buf_data;
766 struct acx_ring_data *rd = &sc->sc_ring_data;
769 ASSERT_SERIALIZED(ifp->if_serializer);
771 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
773 sc->sc_firmware_ver = 0;
774 sc->sc_hardware_id = 0;
777 error = acx_reset(sc);
781 /* Firmware no longer functions after hardware reset */
782 sc->sc_flags &= ~ACX_FLAG_FW_LOADED;
784 acx_disable_intr(sc);
786 /* Stop backgroud scanning */
787 callout_stop(&sc->sc_scan_timer);
789 /* Turn off power led */
790 CSR_SETB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
793 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
794 struct acx_txbuf *buf;
796 buf = &bd->tx_buf[i];
798 if (buf->tb_mbuf != NULL) {
799 bus_dmamap_unload(bd->mbuf_dma_tag,
800 buf->tb_mbuf_dmamap);
801 m_freem(buf->tb_mbuf);
805 if (buf->tb_node != NULL)
806 ieee80211_free_node(buf->tb_node);
810 /* Clear TX host descriptors */
811 bzero(rd->tx_ring, ACX_TX_RING_SIZE);
814 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
815 if (bd->rx_buf[i].rb_mbuf != NULL) {
816 bus_dmamap_unload(bd->mbuf_dma_tag,
817 bd->rx_buf[i].rb_mbuf_dmamap);
818 m_freem(bd->rx_buf[i].rb_mbuf);
819 bd->rx_buf[i].rb_mbuf = NULL;
823 /* Clear RX host descriptors */
824 bzero(rd->rx_ring, ACX_RX_RING_SIZE);
828 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
834 acx_config(struct acx_softc *sc)
836 struct acx_config conf;
839 error = acx_read_config(sc, &conf);
843 error = acx_write_config(sc, &conf);
847 error = acx_rx_config(sc, sc->sc_flags & ACX_FLAG_PROMISC);
851 if (acx_set_probe_req_tmplt(sc, "", 0) != 0) {
852 if_printf(&sc->sc_ic.ic_if, "can't set probe req template "
858 if (acx_set_null_tmplt(sc) != 0) {
859 if_printf(&sc->sc_ic.ic_if, "can't set null data template\n");
866 acx_read_config(struct acx_softc *sc, struct acx_config *conf)
868 struct acx_conf_eaddr addr;
869 struct acx_conf_regdom reg_dom;
870 struct acx_conf_antenna ant;
871 struct acx_conf_fwrev fw_rev;
877 if (acx_get_eaddr_conf(sc, &addr) != 0) {
878 if_printf(&sc->sc_ic.ic_if, "can't get station id\n");
883 * Get and print station id in case that EEPROM station id's
884 * offset is not correct
886 for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
887 conf->eaddr[IEEE80211_ADDR_LEN - 1 - i] = addr.eaddr[i];
888 if_printf(&sc->sc_ic.ic_if, "MAC address (from firmware): %6D\n",
891 /* Get region domain */
892 if (acx_get_regdom_conf(sc, ®_dom) != 0) {
893 if_printf(&sc->sc_ic.ic_if, "can't get region domain\n");
896 conf->regdom = reg_dom.regdom;
897 DPRINTF((&sc->sc_ic.ic_if, "regdom %02x\n", reg_dom.regdom));
900 if (acx_get_antenna_conf(sc, &ant) != 0) {
901 if_printf(&sc->sc_ic.ic_if, "can't get antenna\n");
904 conf->antenna = ant.antenna;
905 DPRINTF((&sc->sc_ic.ic_if, "antenna %02x\n", ant.antenna));
907 /* Get sensitivity XXX not used */
908 if (sc->sc_radio_type == ACX_RADIO_TYPE_MAXIM ||
909 sc->sc_radio_type == ACX_RADIO_TYPE_RFMD ||
910 sc->sc_radio_type == ACX_RADIO_TYPE_RALINK) {
911 error = acx_read_phyreg(sc, ACXRV_PHYREG_SENSITIVITY, &sen);
913 if_printf(&sc->sc_ic.ic_if, "can't get sensitivity\n");
919 DPRINTF((&sc->sc_ic.ic_if, "sensitivity %02x\n", sen));
921 /* Get firmware revision */
922 if (acx_get_fwrev_conf(sc, &fw_rev) != 0) {
923 if_printf(&sc->sc_ic.ic_if, "can't get firmware revision\n");
927 if (strncmp(fw_rev.fw_rev, "Rev ", 4) != 0) {
928 if_printf(&sc->sc_ic.ic_if, "strange revision string -- %s\n",
930 fw_rev_no = 0x01090407;
939 s = &fw_rev.fw_rev[4];
941 for (i = 0; i < 4; ++i) {
944 val = strtoul(s, &endp, 16);
945 fw_rev_no |= val << ((3 - i) * 8);
953 sc->sc_firmware_ver = fw_rev_no;
954 sc->sc_hardware_id = le32toh(fw_rev.hw_id);
955 DPRINTF((&sc->sc_ic.ic_if, "fw rev %08x, hw id %08x\n",
956 sc->sc_firmware_ver, sc->sc_hardware_id));
958 if (sc->chip_read_config != NULL) {
959 error = sc->chip_read_config(sc, conf);
967 acx_write_config(struct acx_softc *sc, struct acx_config *conf)
969 struct acx_conf_nretry_short sretry;
970 struct acx_conf_nretry_long lretry;
971 struct acx_conf_msdu_lifetime msdu_lifetime;
972 struct acx_conf_rate_fallback rate_fb;
973 struct acx_conf_antenna ant;
974 struct acx_conf_regdom reg_dom;
977 /* Set number of long/short retry */
978 KKASSERT(sc->chip_short_retry_limit > 0);
979 sretry.nretry = sc->chip_short_retry_limit;
980 if (acx_set_nretry_short_conf(sc, &sretry) != 0) {
981 if_printf(&sc->sc_ic.ic_if, "can't set short retry limit\n");
985 lretry.nretry = sc->sc_long_retry_limit;
986 if (acx_set_nretry_long_conf(sc, &lretry) != 0) {
987 if_printf(&sc->sc_ic.ic_if, "can't set long retry limit\n");
991 /* Set MSDU lifetime */
992 msdu_lifetime.lifetime = htole32(sc->sc_msdu_lifetime);
993 if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
994 if_printf(&sc->sc_ic.ic_if, "can't set MSDU lifetime\n");
998 /* Enable rate fallback */
999 rate_fb.ratefb_enable = 1;
1000 if (acx_set_rate_fallback_conf(sc, &rate_fb) != 0) {
1001 if_printf(&sc->sc_ic.ic_if, "can't enable rate fallback\n");
1006 ant.antenna = conf->antenna;
1007 if (acx_set_antenna_conf(sc, &ant) != 0) {
1008 if_printf(&sc->sc_ic.ic_if, "can't set antenna\n");
1012 /* Set region domain */
1013 reg_dom.regdom = conf->regdom;
1014 if (acx_set_regdom_conf(sc, ®_dom) != 0) {
1015 if_printf(&sc->sc_ic.ic_if, "can't set region domain\n");
1019 if (sc->chip_write_config != NULL) {
1020 error = sc->chip_write_config(sc, conf);
1029 acx_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1031 struct acx_softc *sc = ifp->if_softc;
1032 struct ieee80211com *ic = &sc->sc_ic;
1037 req = (struct ifreq *)data;
1041 if (ifp->if_flags & IFF_UP) {
1042 if ((ifp->if_flags & IFF_RUNNING)) {
1045 if ((ifp->if_flags & IFF_PROMISC) &&
1046 (sc->sc_flags & ACX_FLAG_PROMISC) == 0)
1048 else if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1049 (sc->sc_flags & ACX_FLAG_PROMISC))
1053 * Promisc mode is always enabled when
1054 * operation mode is Monitor.
1056 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1058 error = acx_rx_config(sc, promisc);
1063 if (ifp->if_flags & IFF_RUNNING)
1067 if (ifp->if_flags & IFF_PROMISC)
1068 sc->sc_flags |= ACX_FLAG_PROMISC;
1070 sc->sc_flags &= ~ACX_FLAG_PROMISC;
1077 error = ieee80211_ioctl(ic, cmd, data, cr);
1081 if (error == ENETRESET) {
1082 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
1083 (IFF_RUNNING | IFF_UP))
1091 acx_start(struct ifnet *ifp)
1093 struct acx_softc *sc = ifp->if_softc;
1094 struct ieee80211com *ic = &sc->sc_ic;
1095 struct acx_buf_data *bd = &sc->sc_buf_data;
1096 struct acx_txbuf *buf;
1099 ASSERT_SERIALIZED(ifp->if_serializer);
1101 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0) {
1102 ifq_purge(&ifp->if_snd);
1103 ieee80211_drain_mgtq(&ic->ic_mgtq);
1107 if ((ifp->if_flags & IFF_RUNNING) == 0 ||
1108 (ifp->if_flags & IFF_OACTIVE))
1113 * We can't start from a random position that TX descriptor
1114 * is free, since hardware will be confused by that.
1115 * We have to follow the order of the TX ring.
1117 idx = bd->tx_free_start;
1119 for (buf = &bd->tx_buf[idx]; buf->tb_mbuf == NULL;
1120 buf = &bd->tx_buf[idx]) {
1121 struct ieee80211_frame *f;
1122 struct ieee80211_node *ni = NULL;
1126 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1127 IF_DEQUEUE(&ic->ic_mgtq, m);
1129 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1130 m->m_pkthdr.rcvif = NULL;
1135 * Don't transmit probe response firmware will
1138 f = mtod(m, struct ieee80211_frame *);
1139 if ((f->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1140 IEEE80211_FC0_TYPE_MGT &&
1141 (f->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1142 IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1144 ieee80211_free_node(ni);
1148 } else if (!ifq_is_empty(&ifp->if_snd)) {
1149 struct ether_header *eh;
1151 if (ic->ic_state != IEEE80211_S_RUN) {
1152 ifq_purge(&ifp->if_snd);
1156 m = ifq_dequeue(&ifp->if_snd, NULL);
1160 if (m->m_len < sizeof(struct ether_header)) {
1161 m = m_pullup(m, sizeof(struct ether_header));
1167 eh = mtod(m, struct ether_header *);
1169 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1176 /* TODO power save */
1180 m = ieee80211_encap(ic, m, ni);
1182 ieee80211_free_node(ni);
1190 if (ic->ic_rawbpf != NULL)
1191 bpf_mtap(ic->ic_rawbpf, m);
1193 f = mtod(m, struct ieee80211_frame *);
1194 if ((f->i_fc[1] & IEEE80211_FC1_WEP) && !sc->chip_hw_crypt) {
1195 KASSERT(ni != NULL, ("TX node is NULL (WEP)\n"));
1196 if (ieee80211_crypto_encap(ic, ni, m) == NULL) {
1197 ieee80211_free_node(ni);
1205 * Since mgmt data are transmitted at fixed rate
1206 * they will not be used to do rate control.
1208 if (mgmt_pkt && ni != NULL) {
1209 ieee80211_free_node(ni);
1213 if (acx_encap(sc, buf, m, ni) != 0) {
1215 * NOTE: `m' will be freed in acx_encap()
1219 ieee80211_free_node(ni);
1226 * 1) `m' should not be touched after acx_encap()
1227 * 2) `node' will be used to do TX rate control during
1228 * acx_txeof(), so it is not freed here. acx_txeof()
1229 * will free it for us
1233 bd->tx_used_count++;
1234 idx = (idx + 1) % ACX_TX_DESC_CNT;
1236 bd->tx_free_start = idx;
1238 if (bd->tx_used_count == ACX_TX_DESC_CNT)
1239 ifp->if_flags |= IFF_OACTIVE;
1241 if (trans && sc->sc_tx_timer == 0)
1242 sc->sc_tx_timer = 5;
1247 acx_watchdog(struct ifnet *ifp)
1249 struct acx_softc *sc = ifp->if_softc;
1253 if ((ifp->if_flags & IFF_RUNNING) == 0)
1256 if (sc->sc_tx_timer) {
1257 if (--sc->sc_tx_timer == 0) {
1258 if_printf(ifp, "watchdog timeout\n");
1260 acx_txeof(ifp->if_softc);
1265 ieee80211_watchdog(&sc->sc_ic);
1271 struct acx_softc *sc = arg;
1272 uint16_t intr_status;
1274 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0)
1277 intr_status = CSR_READ_2(sc, ACXREG_INTR_STATUS_CLR);
1278 if (intr_status == ACXRV_INTR_ALL) {
1279 /* not our interrupt */
1283 intr_status &= sc->chip_intr_enable;
1284 if (intr_status == 0) {
1285 /* not interrupts we care about */
1289 /* Acknowledge all interrupts */
1290 CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_ALL);
1292 if (intr_status & ACXRV_INTR_TX_FINI)
1295 if (intr_status & ACXRV_INTR_RX_FINI)
1300 acx_disable_intr(struct acx_softc *sc)
1302 CSR_WRITE_2(sc, ACXREG_INTR_MASK, sc->chip_intr_disable);
1303 CSR_WRITE_2(sc, ACXREG_EVENT_MASK, 0);
1307 acx_enable_intr(struct acx_softc *sc)
1309 /* Mask out interrupts that are not in the enable set */
1310 CSR_WRITE_2(sc, ACXREG_INTR_MASK, ~sc->chip_intr_enable);
1311 CSR_WRITE_2(sc, ACXREG_EVENT_MASK, ACXRV_EVENT_DISABLE);
1315 acx_txeof(struct acx_softc *sc)
1317 struct acx_buf_data *bd;
1318 struct acx_txbuf *buf;
1322 ifp = &sc->sc_ic.ic_if;
1323 ASSERT_SERIALIZED(ifp->if_serializer);
1325 bd = &sc->sc_buf_data;
1326 idx = bd->tx_used_start;
1327 for (buf = &bd->tx_buf[idx]; buf->tb_mbuf != NULL;
1328 buf = &bd->tx_buf[idx]) {
1329 uint8_t ctrl, error;
1332 ctrl = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_ctrl);
1333 if ((ctrl & (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE)) !=
1334 (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE))
1337 bus_dmamap_unload(bd->mbuf_dma_tag, buf->tb_mbuf_dmamap);
1338 frame_len = buf->tb_mbuf->m_pkthdr.len;
1339 m_freem(buf->tb_mbuf);
1340 buf->tb_mbuf = NULL;
1342 error = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_error);
1344 acx_txerr(sc, error);
1350 if (buf->tb_node != NULL) {
1351 sc->chip_tx_complete(sc, buf, frame_len, error);
1352 ieee80211_free_node(buf->tb_node);
1353 buf->tb_node = NULL;
1356 FW_TXDESC_SETFIELD_1(sc, buf, f_tx_ctrl, DESC_CTRL_HOSTOWN);
1358 bd->tx_used_count--;
1360 idx = (idx + 1) % ACX_TX_DESC_CNT;
1362 bd->tx_used_start = idx;
1364 sc->sc_tx_timer = bd->tx_used_count == 0 ? 0 : 5;
1366 if (bd->tx_used_count != ACX_TX_DESC_CNT) {
1367 ifp->if_flags &= ~IFF_OACTIVE;
1373 acx_txerr(struct acx_softc *sc, uint8_t err)
1375 struct ifnet *ifp = &sc->sc_ic.ic_if;
1376 struct acx_stats *stats = &sc->sc_stats;
1378 if (err == DESC_ERR_EXCESSIVE_RETRY) {
1380 * This a common error (see comment below),
1381 * so print it using DPRINTF()
1383 DPRINTF((ifp, "TX failed -- excessive retry\n"));
1385 if_printf(ifp, "TX failed -- ");
1389 * Although `err' looks like bitmask, it never
1390 * has multiple bits set.
1394 case DESC_ERR_OTHER_FRAG:
1395 /* XXX what's this */
1396 kprintf("error in other fragment\n");
1397 stats->err_oth_frag++;
1400 case DESC_ERR_ABORT:
1401 kprintf("aborted\n");
1404 case DESC_ERR_PARAM:
1405 kprintf("wrong parameters in descriptor\n");
1408 case DESC_ERR_NO_WEPKEY:
1409 kprintf("WEP key missing\n");
1410 stats->err_no_wepkey++;
1412 case DESC_ERR_MSDU_TIMEOUT:
1413 kprintf("MSDU life timeout\n");
1414 stats->err_msdu_timeout++;
1416 case DESC_ERR_EXCESSIVE_RETRY:
1419 * 1) Distance is too long
1420 * 2) Transmit failed (e.g. no MAC level ACK)
1421 * 3) Chip overheated (this should be rare)
1423 stats->err_ex_retry++;
1425 case DESC_ERR_BUF_OVERFLOW:
1426 kprintf("buffer overflow\n");
1427 stats->err_buf_oflow++;
1430 kprintf("DMA error\n");
1434 kprintf("unknown error %d\n", err);
1441 acx_rxeof(struct acx_softc *sc)
1443 struct ieee80211com *ic = &sc->sc_ic;
1444 struct acx_ring_data *rd = &sc->sc_ring_data;
1445 struct acx_buf_data *bd = &sc->sc_buf_data;
1446 struct ifnet *ifp = &ic->ic_if;
1449 ASSERT_SERIALIZED(ic->ic_if.if_serializer);
1451 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1452 BUS_DMASYNC_POSTREAD);
1455 * Locate first "ready" rx buffer,
1456 * start from last stopped position
1458 idx = bd->rx_scan_start;
1461 struct acx_rxbuf *buf;
1463 buf = &bd->rx_buf[idx];
1464 if ((buf->rb_desc->h_ctrl & htole16(DESC_CTRL_HOSTOWN)) &&
1465 (buf->rb_desc->h_status & htole32(DESC_STATUS_FULL))) {
1469 idx = (idx + 1) % ACX_RX_DESC_CNT;
1470 } while (idx != bd->rx_scan_start);
1476 * NOTE: don't mess up `idx' here, it will
1477 * be used in the following code
1481 struct acx_rxbuf_hdr *head;
1482 struct acx_rxbuf *buf;
1483 struct ieee80211_frame_min *wh;
1485 uint32_t desc_status;
1487 int len, error, rssi, is_priv;
1489 buf = &bd->rx_buf[idx];
1491 desc_ctrl = le16toh(buf->rb_desc->h_ctrl);
1492 desc_status = le32toh(buf->rb_desc->h_status);
1493 if (!(desc_ctrl & DESC_CTRL_HOSTOWN) ||
1494 !(desc_status & DESC_STATUS_FULL))
1497 bus_dmamap_sync(bd->mbuf_dma_tag, buf->rb_mbuf_dmamap,
1498 BUS_DMASYNC_POSTREAD);
1502 error = acx_newbuf(sc, buf, 0);
1508 head = mtod(m, struct acx_rxbuf_hdr *);
1509 len = le16toh(head->rbh_len) & ACX_RXBUF_LEN_MASK;
1510 rssi = acx_get_rssi(sc, head->rbh_level);
1512 m_adj(m, sizeof(struct acx_rxbuf_hdr) + sc->chip_rxbuf_exhdr);
1513 m->m_len = m->m_pkthdr.len = len;
1514 m->m_pkthdr.rcvif = &ic->ic_if;
1516 wh = mtod(m, struct ieee80211_frame_min *);
1517 is_priv = (wh->i_fc[1] & IEEE80211_FC1_WEP);
1519 if (sc->sc_drvbpf != NULL) {
1520 sc->sc_rx_th.wr_tsf = htole32(head->rbh_time);
1522 sc->sc_rx_th.wr_flags = 0;
1524 sc->sc_rx_th.wr_flags |=
1525 IEEE80211_RADIOTAP_F_WEP;
1527 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_SHPRE) {
1528 sc->sc_rx_th.wr_flags |=
1529 IEEE80211_RADIOTAP_F_SHORTPRE;
1532 if (sc->chip_phymode == IEEE80211_MODE_11G) {
1533 sc->sc_rx_th.wr_rate =
1534 ieee80211_plcp2rate(head->rbh_plcp,
1535 head->rbh_bbp_stat & ACX_RXBUF_STAT_OFDM);
1537 sc->sc_rx_th.wr_rate =
1538 ieee80211_plcp2rate(head->rbh_plcp, 0);
1541 sc->sc_rx_th.wr_antsignal = rssi;
1543 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_ANT1)
1544 sc->sc_rx_th.wr_antenna = 1;
1546 sc->sc_rx_th.wr_antenna = 0;
1548 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th,
1552 if (len >= sizeof(struct ieee80211_frame_min) &&
1554 struct ieee80211_node *ni;
1556 if (is_priv && sc->chip_hw_crypt) {
1557 /* Short circuit software WEP */
1558 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1560 /* Do chip specific RX buffer processing */
1561 if (sc->chip_proc_wep_rxbuf != NULL) {
1562 sc->chip_proc_wep_rxbuf(sc, m, &len);
1564 struct ieee80211_frame_min *);
1567 m->m_len = m->m_pkthdr.len = len;
1569 ni = ieee80211_find_rxnode(ic, wh);
1570 ieee80211_input(ic, m, ni, rssi,
1571 le32toh(head->rbh_time));
1572 ieee80211_free_node(ni);
1576 if (len < sizeof(struct ieee80211_frame_min)) {
1577 if (ic->ic_rawbpf != NULL &&
1578 len >= sizeof(struct ieee80211_frame_ack))
1579 bpf_mtap(ic->ic_rawbpf, m);
1581 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1582 ic->ic_stats.is_rx_tooshort++;
1587 buf->rb_desc->h_ctrl = htole16(desc_ctrl & ~DESC_CTRL_HOSTOWN);
1588 buf->rb_desc->h_status = 0;
1589 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1590 BUS_DMASYNC_PREWRITE);
1592 idx = (idx + 1) % ACX_RX_DESC_CNT;
1593 } while (idx != bd->rx_scan_start);
1596 * Record the position so that next
1597 * time we can start from it
1599 bd->rx_scan_start = idx;
1603 acx_reset(struct acx_softc *sc)
1608 CSR_SETB_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_HALT);
1610 /* Software reset */
1611 reg = CSR_READ_2(sc, ACXREG_SOFT_RESET);
1612 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg | ACXRV_SOFT_RESET);
1614 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg);
1616 /* Initialize EEPROM */
1617 CSR_SETB_2(sc, ACXREG_EEPROM_INIT, ACXRV_EEPROM_INIT);
1620 /* Test whether ECPU is stopped */
1621 reg = CSR_READ_2(sc, ACXREG_ECPU_CTRL);
1622 if (!(reg & ACXRV_ECPU_HALT)) {
1623 if_printf(&sc->sc_ic.ic_if, "can't halt ECPU\n");
1630 acx_read_eeprom(struct acx_softc *sc, uint32_t offset, uint8_t *val)
1634 CSR_WRITE_4(sc, ACXREG_EEPROM_CONF, 0);
1635 CSR_WRITE_4(sc, ACXREG_EEPROM_ADDR, offset);
1636 CSR_WRITE_4(sc, ACXREG_EEPROM_CTRL, ACXRV_EEPROM_READ);
1638 #define EE_READ_RETRY_MAX 100
1639 for (i = 0; i < EE_READ_RETRY_MAX; ++i) {
1640 if (CSR_READ_2(sc, ACXREG_EEPROM_CTRL) == 0)
1644 if (i == EE_READ_RETRY_MAX) {
1645 if_printf(&sc->sc_ic.ic_if, "can't read EEPROM offset %x "
1646 "(timeout)\n", offset);
1649 #undef EE_READ_RETRY_MAX
1651 *val = CSR_READ_1(sc, ACXREG_EEPROM_DATA);
1656 acx_read_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t *val)
1660 CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1661 CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_READ);
1663 #define PHY_READ_RETRY_MAX 100
1664 for (i = 0; i < PHY_READ_RETRY_MAX; ++i) {
1665 if (CSR_READ_4(sc, ACXREG_PHY_CTRL) == 0)
1669 if (i == PHY_READ_RETRY_MAX) {
1670 if_printf(&sc->sc_ic.ic_if, "can't read phy reg %x (timeout)\n",
1674 #undef PHY_READ_RETRY_MAX
1676 *val = CSR_READ_1(sc, ACXREG_PHY_DATA);
1681 acx_write_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t val)
1683 CSR_WRITE_4(sc, ACXREG_PHY_DATA, val);
1684 CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1685 CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_WRITE);
1689 acx_alloc_firmware(struct acx_softc *sc)
1691 struct acx_firmware *fw = &sc->sc_firmware;
1692 struct ifnet *ifp = &sc->sc_ic.ic_if;
1693 struct fw_image *img;
1698 * NB: serializer need to be released before loading firmware
1699 * image to avoid possible dead lock
1701 ASSERT_SERIALIZED(ifp->if_serializer);
1703 if (fw->base_fw_image == NULL) {
1704 if (fw->combined_radio_fw) {
1705 ksnprintf(filename, sizeof(filename),
1706 ACX_BASE_RADIO_FW_PATH,
1707 fw->fwdir, sc->sc_radio_type);
1709 ksnprintf(filename, sizeof(filename),
1710 ACX_BASE_FW_PATH, fw->fwdir);
1713 lwkt_serialize_exit(ifp->if_serializer);
1714 img = firmware_image_load(filename, NULL);
1715 lwkt_serialize_enter(ifp->if_serializer);
1717 fw->base_fw_image = img;
1718 if (fw->base_fw_image == NULL) {
1719 if_printf(ifp, "load %s base fw failed\n", filename);
1724 error = acx_setup_firmware(sc, fw->base_fw_image,
1725 &fw->base_fw, &fw->base_fw_len);
1730 if (!fw->combined_radio_fw && fw->radio_fw_image == NULL) {
1731 ksnprintf(filename, sizeof(filename), ACX_RADIO_FW_PATH,
1732 fw->fwdir, sc->sc_radio_type);
1734 lwkt_serialize_exit(ifp->if_serializer);
1735 img = firmware_image_load(filename, NULL);
1736 lwkt_serialize_enter(ifp->if_serializer);
1738 fw->radio_fw_image = img;
1739 if (fw->radio_fw_image == NULL) {
1740 if_printf(ifp, "load %s radio fw failed\n", filename);
1745 error = acx_setup_firmware(sc, fw->radio_fw_image,
1746 &fw->radio_fw, &fw->radio_fw_len);
1750 acx_free_firmware(sc);
1755 acx_setup_firmware(struct acx_softc *sc, struct fw_image *img,
1756 const uint8_t **ptr, int *len)
1758 const struct acx_firmware_hdr *hdr;
1767 * Make sure that the firmware image contains more than just a header
1769 if (img->fw_imglen <= sizeof(*hdr)) {
1770 if_printf(&sc->sc_ic.ic_if, "%s is invalid image, "
1771 "size %u (too small)\n",
1772 img->fw_name, img->fw_imglen);
1775 hdr = (const struct acx_firmware_hdr *)img->fw_image;
1780 if (hdr->fwh_len != img->fw_imglen - sizeof(*hdr)) {
1781 if_printf(&sc->sc_ic.ic_if, "%s is invalid image, "
1782 "size in hdr %u and image size %u mismatches\n",
1783 img->fw_name, hdr->fwh_len, img->fw_imglen);
1791 for (i = 0, p = (const uint8_t *)&hdr->fwh_len;
1792 i < img->fw_imglen - sizeof(hdr->fwh_cksum); ++i, ++p)
1794 if (cksum != hdr->fwh_cksum) {
1795 if_printf(&sc->sc_ic.ic_if, "%s is invalid image, "
1796 "checksum mismatch\n", img->fw_name);
1800 *ptr = ((const uint8_t *)img->fw_image + sizeof(*hdr));
1801 *len = img->fw_imglen - sizeof(*hdr);
1806 acx_free_firmware(struct acx_softc *sc)
1808 struct acx_firmware *fw = &sc->sc_firmware;
1810 if (fw->base_fw_image != NULL) {
1811 firmware_image_unload(fw->base_fw_image);
1812 fw->base_fw_image = NULL;
1814 fw->base_fw_len = 0;
1816 if (fw->radio_fw_image != NULL) {
1817 firmware_image_unload(fw->radio_fw_image);
1818 fw->radio_fw_image = NULL;
1819 fw->radio_fw = NULL;
1820 fw->radio_fw_len = 0;
1825 acx_load_base_firmware(struct acx_softc *sc, const uint8_t *base_fw,
1826 uint32_t base_fw_len)
1830 /* Load base firmware */
1831 error = acx_load_firmware(sc, 0, base_fw, base_fw_len);
1833 if_printf(&sc->sc_ic.ic_if, "can't load base firmware\n");
1836 DPRINTF((&sc->sc_ic.ic_if, "base firmware loaded\n"));
1839 CSR_WRITE_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_START);
1841 /* Wait for ECPU to be up */
1842 for (i = 0; i < 500; ++i) {
1845 reg = CSR_READ_2(sc, ACXREG_INTR_STATUS);
1846 if (reg & ACXRV_INTR_FCS_THRESH) {
1847 CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_FCS_THRESH);
1853 if_printf(&sc->sc_ic.ic_if, "can't initialize ECPU (timeout)\n");
1858 acx_load_radio_firmware(struct acx_softc *sc, const uint8_t *radio_fw,
1859 uint32_t radio_fw_len)
1861 struct acx_conf_mmap mem_map;
1862 uint32_t radio_fw_ofs;
1866 * Get the position, where base firmware is loaded, so that
1867 * radio firmware can be loaded after it.
1869 if (acx_get_mmap_conf(sc, &mem_map) != 0)
1871 radio_fw_ofs = le32toh(mem_map.code_end);
1873 /* Put ECPU into sleeping state, before loading radio firmware */
1874 if (acx_sleep(sc) != 0)
1877 /* Load radio firmware */
1878 error = acx_load_firmware(sc, radio_fw_ofs, radio_fw, radio_fw_len);
1880 if_printf(&sc->sc_ic.ic_if, "can't load radio firmware\n");
1883 DPRINTF((&sc->sc_ic.ic_if, "radio firmware loaded\n"));
1885 /* Wake up sleeping ECPU, after radio firmware is loaded */
1886 if (acx_wakeup(sc) != 0)
1889 /* Initialize radio */
1890 if (acx_init_radio(sc, radio_fw_ofs, radio_fw_len) != 0)
1893 /* Verify radio firmware's loading position */
1894 if (acx_get_mmap_conf(sc, &mem_map) != 0)
1896 if (le32toh(mem_map.code_end) != radio_fw_ofs + radio_fw_len) {
1897 if_printf(&sc->sc_ic.ic_if, "loaded radio firmware position "
1902 DPRINTF((&sc->sc_ic.ic_if, "radio firmware initialized\n"));
1907 acx_load_firmware(struct acx_softc *sc, uint32_t offset, const uint8_t *data,
1913 fw = (const uint32_t *)data;
1914 fw_len = data_len / sizeof(uint32_t);
1917 * LOADFW_AUTO_INC only works with some older firmware:
1918 * 1) acx100's firmware
1919 * 2) acx111's firmware whose rev is 0x00010011
1923 CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1924 #ifndef LOADFW_AUTO_INC
1925 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1927 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1928 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1931 for (i = 0; i < fw_len; ++i) {
1932 #ifndef LOADFW_AUTO_INC
1933 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1935 CSR_WRITE_4(sc, ACXREG_FWMEM_DATA, be32toh(fw[i]));
1938 /* Verify firmware */
1939 CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1940 #ifndef LOADFW_AUTO_INC
1941 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1943 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1944 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1947 for (i = 0; i < fw_len; ++i) {
1950 #ifndef LOADFW_AUTO_INC
1951 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1953 val = CSR_READ_4(sc, ACXREG_FWMEM_DATA);
1954 if (be32toh(fw[i]) != val) {
1955 if_printf(&sc->sc_ic.ic_if, "fireware mismatch "
1956 "fw %08x loaded %08x\n", fw[i], val);
1964 acx_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1966 struct ifnet *ifp = &ic->ic_if;
1967 struct acx_softc *sc = ifp->if_softc;
1968 struct ieee80211_node *ni = NULL;
1969 struct ieee80211_channel *c = NULL;
1970 int error = 1, mode = 0;
1972 ASSERT_SERIALIZED(ifp->if_serializer);
1974 ieee80211_ratectl_newstate(ic, nstate);
1975 callout_stop(&sc->sc_scan_timer);
1976 callout_stop(&sc->sc_calibrate_timer);
1979 case IEEE80211_S_SCAN:
1980 acx_set_chan(sc, ic->ic_curchan);
1981 callout_reset(&sc->sc_scan_timer,
1982 (hz * sc->sc_scan_dwell) / 1000,
1985 case IEEE80211_S_AUTH:
1986 if (ic->ic_opmode == IEEE80211_M_STA) {
1989 mode = ACX_MODE_STA;
1992 case IEEE80211_S_RUN:
1993 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1994 ic->ic_opmode == IEEE80211_M_HOSTAP) {
1997 if (ic->ic_opmode == IEEE80211_M_IBSS)
1998 mode = ACX_MODE_ADHOC;
2002 if (acx_set_beacon_tmplt(sc, ni) != 0) {
2003 if_printf(ifp, "set bescon template failed\n");
2006 if (acx_set_probe_resp_tmplt(sc, ni) != 0) {
2007 if_printf(ifp, "set probe response template"
2011 } else if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2014 mode = ACX_MODE_STA;
2022 KKASSERT(c != NULL);
2024 if (acx_set_chan(sc, c) != 0)
2027 if (acx_join_bss(sc, mode, ni, c) != 0) {
2028 if_printf(ifp, "join BSS failed\n");
2033 if (nstate == IEEE80211_S_RUN) {
2034 int interval = sc->sc_calib_intvl;
2036 if (sc->chip_calibrate != NULL) {
2037 error = sc->chip_calibrate(sc);
2040 * Restart calibration some time later
2044 callout_reset(&sc->sc_calibrate_timer,
2045 hz * interval, acx_calibrate, sc);
2052 nstate = IEEE80211_S_INIT;
2055 return sc->sc_newstate(ic, nstate, arg);
2059 acx_init_tmplt_ordered(struct acx_softc *sc)
2061 #define INIT_TMPLT(name) \
2063 if (acx_init_##name##_tmplt(sc) != 0) \
2069 * Order of templates initialization:
2075 * Above order is critical to get a correct memory map.
2077 INIT_TMPLT(probe_req);
2078 INIT_TMPLT(null_data);
2081 INIT_TMPLT(probe_resp);
2088 acx_ring_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
2090 *((uint32_t *)arg) = seg->ds_addr;
2094 acx_dma_alloc(struct acx_softc *sc)
2096 struct acx_ring_data *rd = &sc->sc_ring_data;
2097 struct acx_buf_data *bd = &sc->sc_buf_data;
2100 /* Allocate DMA stuffs for RX descriptors */
2101 error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
2102 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2104 ACX_RX_RING_SIZE, 1, ACX_RX_RING_SIZE,
2105 0, &rd->rx_ring_dma_tag);
2107 if_printf(&sc->sc_ic.ic_if, "can't create rx ring dma tag\n");
2111 error = bus_dmamem_alloc(rd->rx_ring_dma_tag, (void **)&rd->rx_ring,
2112 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2113 &rd->rx_ring_dmamap);
2115 if_printf(&sc->sc_ic.ic_if,
2116 "can't allocate rx ring dma memory\n");
2117 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2118 rd->rx_ring_dma_tag = NULL;
2122 error = bus_dmamap_load(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2123 rd->rx_ring, ACX_RX_RING_SIZE,
2124 acx_ring_dma_addr, &rd->rx_ring_paddr,
2127 if_printf(&sc->sc_ic.ic_if, "can't get rx ring dma address\n");
2128 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
2129 rd->rx_ring_dmamap);
2130 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2131 rd->rx_ring_dma_tag = NULL;
2135 /* Allocate DMA stuffs for TX descriptors */
2136 error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
2137 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2139 ACX_TX_RING_SIZE, 1, ACX_TX_RING_SIZE,
2140 0, &rd->tx_ring_dma_tag);
2142 if_printf(&sc->sc_ic.ic_if, "can't create tx ring dma tag\n");
2146 error = bus_dmamem_alloc(rd->tx_ring_dma_tag, (void **)&rd->tx_ring,
2147 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2148 &rd->tx_ring_dmamap);
2150 if_printf(&sc->sc_ic.ic_if,
2151 "can't allocate tx ring dma memory\n");
2152 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2153 rd->tx_ring_dma_tag = NULL;
2157 error = bus_dmamap_load(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2158 rd->tx_ring, ACX_TX_RING_SIZE,
2159 acx_ring_dma_addr, &rd->tx_ring_paddr,
2162 if_printf(&sc->sc_ic.ic_if, "can't get tx ring dma address\n");
2163 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2164 rd->tx_ring_dmamap);
2165 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2166 rd->tx_ring_dma_tag = NULL;
2170 /* Create DMA tag for RX/TX mbuf map */
2171 error = bus_dma_tag_create(NULL, 1, 0,
2172 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2174 MCLBYTES, 1, MCLBYTES,
2175 0, &bd->mbuf_dma_tag);
2177 if_printf(&sc->sc_ic.ic_if, "can't create mbuf dma tag\n");
2181 /* Create a spare RX DMA map */
2182 error = bus_dmamap_create(bd->mbuf_dma_tag, 0, &bd->mbuf_tmp_dmamap);
2184 if_printf(&sc->sc_ic.ic_if, "can't create tmp mbuf dma map\n");
2185 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2186 bd->mbuf_dma_tag = NULL;
2190 /* Create DMA map for RX mbufs */
2191 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2192 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2193 &bd->rx_buf[i].rb_mbuf_dmamap);
2195 if_printf(&sc->sc_ic.ic_if, "can't create rx mbuf "
2196 "dma map (%d)\n", i);
2199 bd->rx_buf[i].rb_desc = &rd->rx_ring[i];
2202 /* Create DMA map for TX mbufs */
2203 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2204 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2205 &bd->tx_buf[i].tb_mbuf_dmamap);
2207 if_printf(&sc->sc_ic.ic_if, "can't create tx mbuf "
2208 "dma map (%d)\n", i);
2211 bd->tx_buf[i].tb_desc1 = &rd->tx_ring[i * 2];
2212 bd->tx_buf[i].tb_desc2 = &rd->tx_ring[(i * 2) + 1];
2219 acx_dma_free(struct acx_softc *sc)
2221 struct acx_ring_data *rd = &sc->sc_ring_data;
2222 struct acx_buf_data *bd = &sc->sc_buf_data;
2225 if (rd->rx_ring_dma_tag != NULL) {
2226 bus_dmamap_unload(rd->rx_ring_dma_tag, rd->rx_ring_dmamap);
2227 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
2228 rd->rx_ring_dmamap);
2229 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2232 if (rd->tx_ring_dma_tag != NULL) {
2233 bus_dmamap_unload(rd->tx_ring_dma_tag, rd->tx_ring_dmamap);
2234 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2235 rd->tx_ring_dmamap);
2236 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2239 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2240 if (bd->rx_buf[i].rb_desc != NULL) {
2241 if (bd->rx_buf[i].rb_mbuf != NULL) {
2242 bus_dmamap_unload(bd->mbuf_dma_tag,
2243 bd->rx_buf[i].rb_mbuf_dmamap);
2244 m_freem(bd->rx_buf[i].rb_mbuf);
2246 bus_dmamap_destroy(bd->mbuf_dma_tag,
2247 bd->rx_buf[i].rb_mbuf_dmamap);
2251 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2252 if (bd->tx_buf[i].tb_desc1 != NULL) {
2253 if (bd->tx_buf[i].tb_mbuf != NULL) {
2254 bus_dmamap_unload(bd->mbuf_dma_tag,
2255 bd->tx_buf[i].tb_mbuf_dmamap);
2256 m_freem(bd->tx_buf[i].tb_mbuf);
2258 bus_dmamap_destroy(bd->mbuf_dma_tag,
2259 bd->tx_buf[i].tb_mbuf_dmamap);
2263 if (bd->mbuf_dma_tag != NULL) {
2264 bus_dmamap_destroy(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap);
2265 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2270 acx_init_tx_ring(struct acx_softc *sc)
2272 struct acx_ring_data *rd;
2273 struct acx_buf_data *bd;
2277 rd = &sc->sc_ring_data;
2278 paddr = rd->tx_ring_paddr;
2279 for (i = 0; i < (ACX_TX_DESC_CNT * 2) - 1; ++i) {
2280 paddr += sizeof(struct acx_host_desc);
2282 rd->tx_ring[i].h_ctrl = htole16(DESC_CTRL_HOSTOWN);
2284 if (i == (ACX_TX_DESC_CNT * 2) - 1)
2285 rd->tx_ring[i].h_next_desc = htole32(rd->tx_ring_paddr);
2287 rd->tx_ring[i].h_next_desc = htole32(paddr);
2290 bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2291 BUS_DMASYNC_PREWRITE);
2293 bd = &sc->sc_buf_data;
2294 bd->tx_free_start = 0;
2295 bd->tx_used_start = 0;
2296 bd->tx_used_count = 0;
2302 acx_init_rx_ring(struct acx_softc *sc)
2304 struct acx_ring_data *rd;
2305 struct acx_buf_data *bd;
2309 bd = &sc->sc_buf_data;
2310 rd = &sc->sc_ring_data;
2311 paddr = rd->rx_ring_paddr;
2313 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2316 paddr += sizeof(struct acx_host_desc);
2318 error = acx_newbuf(sc, &bd->rx_buf[i], 1);
2322 if (i == ACX_RX_DESC_CNT - 1)
2323 rd->rx_ring[i].h_next_desc = htole32(rd->rx_ring_paddr);
2325 rd->rx_ring[i].h_next_desc = htole32(paddr);
2328 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2329 BUS_DMASYNC_PREWRITE);
2331 bd->rx_scan_start = 0;
2336 acx_buf_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2337 bus_size_t mapsz, int error)
2343 KASSERT(nseg == 1, ("too many RX dma segments\n"));
2344 *((uint32_t *)arg) = seg->ds_addr;
2348 acx_newbuf(struct acx_softc *sc, struct acx_rxbuf *rb, int wait)
2350 struct acx_buf_data *bd;
2356 bd = &sc->sc_buf_data;
2358 m = m_getcl(wait ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2362 m->m_len = m->m_pkthdr.len = MCLBYTES;
2364 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap,
2365 m, acx_buf_dma_addr, &paddr,
2366 wait ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
2369 if_printf(&sc->sc_ic.ic_if, "can't map rx mbuf %d\n", error);
2373 /* Unload originally mapped mbuf */
2374 bus_dmamap_unload(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap);
2376 /* Swap this dmamap with tmp dmamap */
2377 map = rb->rb_mbuf_dmamap;
2378 rb->rb_mbuf_dmamap = bd->mbuf_tmp_dmamap;
2379 bd->mbuf_tmp_dmamap = map;
2382 rb->rb_desc->h_data_paddr = htole32(paddr);
2383 rb->rb_desc->h_data_len = htole16(m->m_len);
2385 bus_dmamap_sync(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap,
2386 BUS_DMASYNC_PREREAD);
2391 acx_encap(struct acx_softc *sc, struct acx_txbuf *txbuf, struct mbuf *m,
2392 struct ieee80211_node *ni)
2394 struct acx_buf_data *bd = &sc->sc_buf_data;
2395 struct acx_ring_data *rd = &sc->sc_ring_data;
2400 KASSERT(txbuf->tb_mbuf == NULL, ("free TX buf has mbuf installed\n"));
2403 if (m->m_pkthdr.len > MCLBYTES) {
2404 if_printf(&sc->sc_ic.ic_if, "mbuf too big\n");
2407 } else if (m->m_pkthdr.len < ACX_FRAME_HDRLEN) {
2408 if_printf(&sc->sc_ic.ic_if, "mbuf too small\n");
2413 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2414 m, acx_buf_dma_addr, &paddr,
2416 if (error && error != EFBIG) {
2417 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf1 %d\n", error);
2421 if (error) { /* error == EFBIG */
2424 m_new = m_defrag(m, MB_DONTWAIT);
2425 if (m_new == NULL) {
2426 if_printf(&sc->sc_ic.ic_if, "can't defrag tx mbuf\n");
2433 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag,
2434 txbuf->tb_mbuf_dmamap, m,
2435 acx_buf_dma_addr, &paddr,
2438 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf2 %d\n",
2446 bus_dmamap_sync(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2447 BUS_DMASYNC_PREWRITE);
2450 txbuf->tb_node = ni;
2453 * TX buffers are accessed in following way:
2454 * acx_fw_txdesc -> acx_host_desc -> buffer
2456 * It is quite strange that acx also querys acx_host_desc next to
2457 * the one we have assigned to acx_fw_txdesc even if first one's
2458 * acx_host_desc.h_data_len == acx_fw_txdesc.f_tx_len
2460 * So we allocate two acx_host_desc for one acx_fw_txdesc and
2461 * assign the first acx_host_desc to acx_fw_txdesc
2464 * host_desc1.h_data_len = buffer_len
2465 * host_desc2.h_data_len = buffer_len - mac_header_len
2468 * host_desc1.h_data_len = mac_header_len
2469 * host_desc2.h_data_len = buffer_len - mac_header_len
2472 txbuf->tb_desc1->h_data_paddr = htole32(paddr);
2473 txbuf->tb_desc2->h_data_paddr = htole32(paddr + ACX_FRAME_HDRLEN);
2475 txbuf->tb_desc1->h_data_len =
2476 htole16(sc->chip_txdesc1_len ? sc->chip_txdesc1_len
2478 txbuf->tb_desc2->h_data_len =
2479 htole16(m->m_pkthdr.len - ACX_FRAME_HDRLEN);
2483 * We can't simply assign f_tx_ctrl, we will first read it back
2484 * and change it bit by bit
2486 ctrl = FW_TXDESC_GETFIELD_1(sc, txbuf, f_tx_ctrl);
2487 ctrl |= sc->chip_fw_txdesc_ctrl; /* extra chip specific flags */
2488 ctrl &= ~(DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE);
2490 FW_TXDESC_SETFIELD_2(sc, txbuf, f_tx_len, m->m_pkthdr.len);
2491 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_error, 0);
2492 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_data_nretry, 0);
2493 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_nretry, 0);
2494 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_ok, 0);
2495 rate = sc->chip_set_fw_txdesc_rate(sc, txbuf, ni, m->m_pkthdr.len);
2497 if (sc->sc_drvbpf != NULL) {
2498 struct ieee80211_frame_min *wh;
2500 wh = mtod(m, struct ieee80211_frame_min *);
2501 sc->sc_tx_th.wt_flags = 0;
2502 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2503 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2504 sc->sc_tx_th.wt_rate = rate;
2506 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_tx_th, sc->sc_tx_th_len);
2509 txbuf->tb_desc1->h_ctrl = 0;
2510 txbuf->tb_desc2->h_ctrl = 0;
2511 bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2512 BUS_DMASYNC_PREWRITE);
2514 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl2, 0);
2515 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl, ctrl);
2517 /* Tell chip to inform us about TX completion */
2518 CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_TX_FINI);
2526 acx_set_null_tmplt(struct acx_softc *sc)
2528 struct acx_tmplt_null_data n;
2529 struct ieee80211_frame *f;
2531 bzero(&n, sizeof(n));
2534 f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
2535 IEEE80211_FC0_SUBTYPE_NODATA;
2536 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2537 IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2538 IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2540 return _acx_set_null_data_tmplt(sc, &n, sizeof(n));
2544 acx_set_probe_req_tmplt(struct acx_softc *sc, const char *ssid, int ssid_len)
2546 struct acx_tmplt_probe_req req;
2547 struct ieee80211_frame *f;
2551 bzero(&req, sizeof(req));
2553 f = &req.data.u_data.f;
2554 f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2555 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2556 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2557 IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2558 IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2560 v = req.data.u_data.var;
2561 v = ieee80211_add_ssid(v, ssid, ssid_len);
2562 v = ieee80211_add_rates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2563 v = ieee80211_add_xrates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2564 vlen = v - req.data.u_data.var;
2566 return _acx_set_probe_req_tmplt(sc, &req,
2567 ACX_TMPLT_PROBE_REQ_SIZ(vlen));
2571 acx_set_probe_resp_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2573 struct ieee80211com *ic = &sc->sc_ic;
2574 struct acx_tmplt_probe_resp resp;
2575 struct ieee80211_frame *f;
2579 m = ieee80211_probe_resp_alloc(ic, ni);
2582 DPRINTF((&ic->ic_if, "%s alloc probe resp size %d\n", __func__,
2585 f = mtod(m, struct ieee80211_frame *);
2586 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2588 bzero(&resp, sizeof(resp));
2589 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)&resp.data);
2590 len = m->m_pkthdr.len + sizeof(resp.size);
2593 return _acx_set_probe_resp_tmplt(sc, &resp, len);
2597 acx_set_beacon_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2599 struct ieee80211com *ic = &sc->sc_ic;
2600 struct acx_tmplt_beacon beacon;
2601 struct acx_tmplt_tim tim;
2602 struct ieee80211_beacon_offsets bo;
2604 int beacon_tmplt_len = 0, tim_tmplt_len = 0;
2606 bzero(&bo, sizeof(bo));
2607 m = ieee80211_beacon_alloc(ic, ni, &bo);
2610 DPRINTF((&ic->ic_if, "%s alloc beacon size %d\n", __func__,
2613 if (bo.bo_tim_len == 0) {
2614 beacon_tmplt_len = m->m_pkthdr.len;
2616 beacon_tmplt_len = bo.bo_tim - mtod(m, uint8_t *);
2617 tim_tmplt_len = m->m_pkthdr.len - beacon_tmplt_len;
2620 bzero(&beacon, sizeof(beacon));
2621 bzero(&tim, sizeof(tim));
2623 m_copydata(m, 0, beacon_tmplt_len, (caddr_t)&beacon.data);
2624 if (tim_tmplt_len != 0) {
2625 m_copydata(m, beacon_tmplt_len, tim_tmplt_len,
2626 (caddr_t)&tim.data);
2630 beacon_tmplt_len += sizeof(beacon.size);
2631 if (_acx_set_beacon_tmplt(sc, &beacon, beacon_tmplt_len) != 0)
2634 if (tim_tmplt_len != 0) {
2635 tim_tmplt_len += sizeof(tim.size);
2636 if (_acx_set_tim_tmplt(sc, &tim, tim_tmplt_len) != 0)
2643 acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS)
2645 struct acx_softc *sc = arg1;
2646 struct ifnet *ifp = &sc->sc_ic.ic_if;
2649 lwkt_serialize_enter(ifp->if_serializer);
2651 v = sc->sc_msdu_lifetime;
2652 error = sysctl_handle_int(oidp, &v, 0, req);
2653 if (error || req->newptr == NULL)
2660 if (sc->sc_flags & ACX_FLAG_FW_LOADED) {
2661 struct acx_conf_msdu_lifetime msdu_lifetime;
2663 msdu_lifetime.lifetime = htole32(v);
2664 if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
2665 if_printf(&sc->sc_ic.ic_if,
2666 "can't set MSDU lifetime\n");
2671 sc->sc_msdu_lifetime = v;
2673 lwkt_serialize_exit(ifp->if_serializer);
2678 acx_sysctl_free_firmware(SYSCTL_HANDLER_ARGS)
2680 struct acx_softc *sc = arg1;
2681 struct ifnet *ifp = &sc->sc_ic.ic_if;
2684 lwkt_serialize_enter(ifp->if_serializer);
2687 error = sysctl_handle_int(oidp, &v, 0, req);
2688 if (error || req->newptr == NULL)
2690 if (v == 0) /* Do nothing */
2693 acx_free_firmware(sc);
2695 lwkt_serialize_exit(ifp->if_serializer);
2700 acx_media_change(struct ifnet *ifp)
2704 error = ieee80211_media_change(ifp);
2705 if (error != ENETRESET)
2708 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
2709 acx_init(ifp->if_softc);
2714 acx_rx_config(struct acx_softc *sc, int promisc)
2716 struct acx_conf_rxopt rx_opt;
2717 struct ieee80211com *ic = &sc->sc_ic;
2720 * What we want to receive and how to receive
2723 /* Common for all operational modes */
2724 rx_opt.opt1 = RXOPT1_INCL_RXBUF_HDR;
2725 rx_opt.opt2 = RXOPT2_RECV_ASSOC_REQ |
2727 RXOPT2_RECV_BEACON |
2732 RXOPT2_RECV_PROBE_REQ |
2733 RXOPT2_RECV_PROBE_RESP |
2736 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2737 rx_opt.opt1 |= RXOPT1_PROMISC;
2738 rx_opt.opt2 |= RXOPT2_RECV_BROKEN | RXOPT2_RECV_ACK;
2740 rx_opt.opt1 |= promisc ? RXOPT1_PROMISC : RXOPT1_FILT_FDEST;
2743 if (acx_set_rxopt_conf(sc, &rx_opt) != 0) {
2744 if_printf(&sc->sc_ic.ic_if, "can't config RX\n");
2751 acx_set_chan(struct acx_softc *sc, struct ieee80211_channel *c)
2753 struct ieee80211com *ic = &sc->sc_ic;
2757 chan = ieee80211_chan2ieee(ic, c);
2758 if (acx_enable_txchan(sc, chan) != 0) {
2759 if_printf(&ic->ic_if, "enable TX on channel %d failed\n", chan);
2762 if (acx_enable_rxchan(sc, chan) != 0) {
2763 if_printf(&ic->ic_if, "enable RX on channel %d failed\n", chan);
2767 if (IEEE80211_IS_CHAN_G(c))
2768 flags = IEEE80211_CHAN_G;
2770 flags = IEEE80211_CHAN_B;
2772 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2773 htole16(c->ic_freq);
2774 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2780 acx_calibrate(void *xsc)
2782 struct acx_softc *sc = xsc;
2783 struct ifnet *ifp = &sc->sc_ic.ic_if;
2785 lwkt_serialize_enter(ifp->if_serializer);
2786 if (sc->chip_calibrate != NULL &&
2787 sc->sc_ic.ic_state == IEEE80211_S_RUN) {
2788 sc->chip_calibrate(sc);
2789 callout_reset(&sc->sc_calibrate_timer, hz * sc->sc_calib_intvl,
2792 lwkt_serialize_exit(ifp->if_serializer);