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.23 2008/01/15 09:01:13 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/malloc.h>
82 #include <sys/serialize.h>
83 #include <sys/socket.h>
84 #include <sys/sockio.h>
85 #include <sys/sysctl.h>
87 #include <net/ethernet.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>
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>
100 #include <bus/pci/pcireg.h>
101 #include <bus/pci/pcivar.h>
102 #include <bus/pci/pcidevs.h>
106 #include <dev/netif/acx/if_acxreg.h>
107 #include <dev/netif/acx/if_acxvar.h>
108 #include <dev/netif/acx/acxcmd.h>
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 */
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);
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 *);
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 *);
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 *);
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 *);
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 *);
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 *);
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 *,
167 static int acx_load_base_firmware(struct acx_softc *, const uint8_t *,
170 static void acx_next_scan(void *);
171 static int acx_set_chan(struct acx_softc *, struct ieee80211_channel *);
173 static int acx_media_change(struct ifnet *);
174 static int acx_newstate(struct ieee80211com *, enum ieee80211_state, int);
176 static int acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS);
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 } };
183 static const struct acx_device {
186 void (*set_param)(device_t);
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" },
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),
204 DEVMETHOD(device_suspend, acx_suspend),
205 DEVMETHOD(device_resume, acx_resume),
210 static driver_t acx_driver = {
213 sizeof(struct acx_softc)
216 static devclass_t acx_devclass;
218 DRIVER_MODULE(acx, pci, acx_driver, acx_devclass, 0, 0);
219 DRIVER_MODULE(acx, cardbus, acx_driver, acx_devclass, 0, 0);
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);
228 acx_get_rssi(struct acx_softc *sc, uint8_t raw)
232 rssi = ((sc->chip_rssi_corr / 2) + (raw * 5)) / sc->chip_rssi_corr;
233 return rssi > 100 ? 100 : rssi;
237 acx_probe(device_t dev)
239 const struct acx_device *a;
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) {
247 device_set_desc(dev, a->desc);
255 acx_attach(device_t dev)
257 struct acx_softc *sc;
259 struct ieee80211com *ic;
262 sc = device_get_softc(dev);
266 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
269 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
270 uint32_t mem1, mem2, irq;
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);
276 device_printf(dev, "chip is in D%d power mode "
277 "-- setting to D0\n", pci_get_powerstate(dev));
279 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
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);
285 #endif /* !BURN_BRIDGE */
287 /* Enable bus mastering */
288 pci_enable_busmaster(dev);
290 /* Allocate IO memory 1 */
291 sc->sc_mem1_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
294 if (sc->sc_mem1_res == NULL) {
296 device_printf(dev, "can't allocate IO mem1\n");
299 sc->sc_mem1_bt = rman_get_bustag(sc->sc_mem1_res);
300 sc->sc_mem1_bh = rman_get_bushandle(sc->sc_mem1_res);
302 /* Allocate IO memory 2 */
303 sc->sc_mem2_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
306 if (sc->sc_mem2_res == NULL) {
308 device_printf(dev, "can't allocate IO mem2\n");
311 sc->sc_mem2_bt = rman_get_bustag(sc->sc_mem2_res);
312 sc->sc_mem2_bh = rman_get_bushandle(sc->sc_mem2_res);
315 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
317 RF_SHAREABLE | RF_ACTIVE);
318 if (sc->sc_irq_res == NULL) {
320 device_printf(dev, "can't allocate intr\n");
324 /* Initilize channel scanning timer */
325 callout_init(&sc->sc_scan_timer);
327 /* Allocate busdma stuffs */
328 error = acx_dma_alloc(sc);
333 error = acx_reset(sc);
337 /* Disable interrupts before firmware is loaded */
338 acx_disable_intr(sc);
340 /* Get radio type and form factor */
341 #define EEINFO_RETRY_MAX 50
342 for (i = 0; i < EEINFO_RETRY_MAX; ++i) {
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);
353 if (i == EEINFO_RETRY_MAX) {
357 #undef EEINFO_RETRY_MAX
359 DPRINTF((&sc->sc_ic.ic_if, "radio type %02x\n", sc->sc_radio_type));
362 for (i = 0; i < 0x40; ++i) {
365 error = acx_read_eeprom(sc, i, &val);
368 kprintf("%02x ", val);
371 #endif /* DUMP_EEPROM */
373 /* Get EEPROM version */
374 error = acx_read_eeprom(sc, ACX_EE_VERSION_OFS, &sc->sc_eeprom_ver);
377 DPRINTF((&sc->sc_ic.ic_if, "EEPROM version %u\n", sc->sc_eeprom_ver));
380 * Initialize device sysctl before ieee80211_ifattach()
382 sc->sc_long_retry_limit = 4;
383 sc->sc_msdu_lifetime = 4096;
384 sc->sc_scan_dwell = 200; /* 200 milliseconds */
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),
390 device_get_nameunit(dev),
392 if (sc->sc_sysctl_tree == NULL) {
393 device_printf(dev, "can't add sysctl node\n");
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",
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);
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;
420 ic->ic_opmode = IEEE80211_M_STA;
421 ic->ic_state = IEEE80211_S_INIT;
424 * NOTE: Don't overwrite ic_caps set by chip specific code
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 */
434 for (i = 0; i < IEEE80211_ADDR_LEN; ++i) {
435 error = acx_read_eeprom(sc, sc->chip_ee_eaddr_ofs - i,
439 ieee80211_ifattach(ic);
441 /* Enable software beacon missing */
442 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
444 /* Override newstate */
445 sc->sc_newstate = ic->ic_newstate;
446 ic->ic_newstate = acx_newstate;
448 ieee80211_media_init(ic, acx_media_change, ieee80211_media_status);
451 * Radio tap attaching
453 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
454 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
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);
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);
465 error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE, acx_intr, sc,
466 &sc->sc_irq_handle, ifp->if_serializer);
468 device_printf(dev, "can't set up interrupt\n");
470 ieee80211_ifdetach(ic);
475 ieee80211_announce(ic);
484 acx_detach(device_t dev)
486 struct acx_softc *sc = device_get_softc(dev);
488 if (device_is_attached(dev)) {
489 struct ieee80211com *ic = &sc->sc_ic;
490 struct ifnet *ifp = &ic->ic_if;
492 lwkt_serialize_enter(ifp->if_serializer);
495 acx_free_firmware(sc);
496 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
498 lwkt_serialize_exit(ifp->if_serializer);
501 ieee80211_ifdetach(ic);
504 if (sc->sc_sysctl_tree != NULL)
505 sysctl_ctx_free(&sc->sc_sysctl_ctx);
507 if (sc->sc_irq_res != NULL) {
508 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
511 if (sc->sc_mem1_res != NULL) {
512 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem1_rid,
515 if (sc->sc_mem2_res != NULL) {
516 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem2_rid,
525 acx_shutdown(device_t dev)
527 struct acx_softc *sc = device_get_softc(dev);
529 lwkt_serialize_enter(sc->sc_ic.ic_if.if_serializer);
531 lwkt_serialize_exit(sc->sc_ic.ic_if.if_serializer);
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;
544 error = acx_stop(sc);
548 if (fw->base_fw == NULL) {
550 if_printf(ifp, "base firmware is not loaded yet\n");
554 error = acx_init_tx_ring(sc);
556 if_printf(ifp, "can't initialize TX ring\n");
560 error = acx_init_rx_ring(sc);
562 if_printf(ifp, "can't initialize RX ring\n");
566 error = acx_load_base_firmware(sc, fw->base_fw, fw->base_fw_len);
571 * Initialize command and information registers
572 * NOTE: This should be done after base firmware is loaded
574 acx_init_cmd_reg(sc);
575 acx_init_info_reg(sc);
577 sc->sc_flags |= ACX_FLAG_FW_LOADED;
580 if (sc->chip_post_basefw != NULL) {
581 error = sc->chip_post_basefw(sc);
587 if (fw->radio_fw != NULL) {
588 error = acx_load_radio_firmware(sc, fw->radio_fw,
594 error = sc->chip_init(sc);
598 /* Get and set device various configuration */
599 error = acx_config(sc);
603 /* Setup crypto stuffs */
604 if (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) {
605 error = acx_set_crypt_keys(sc);
608 sc->sc_ic.ic_flags &= ~IEEE80211_F_DROPUNENC;
611 /* Turn on power led */
612 CSR_CLRB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
616 ifp->if_flags |= IFF_RUNNING;
617 ifp->if_flags &= ~IFF_OACTIVE;
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);
623 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
631 acx_init_info_reg(struct acx_softc *sc)
633 sc->sc_info = CSR_READ_4(sc, ACXREG_INFO_REG_OFFSET);
634 sc->sc_info_param = sc->sc_info + ACX_INFO_REG_SIZE;
638 acx_set_crypt_keys(struct acx_softc *sc)
640 struct ieee80211com *ic = &sc->sc_ic;
641 struct acx_conf_wep_txkey wep_txkey;
642 int i, error, got_wk = 0;
644 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
645 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
647 if (wk->wk_keylen == 0)
650 if (sc->chip_hw_crypt) {
651 error = sc->chip_set_wepkey(sc, wk, i);
655 } else if (wk->wk_flags & IEEE80211_KEY_XMIT) {
656 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
660 if (!got_wk || sc->chip_hw_crypt ||
661 ic->ic_def_txkey == IEEE80211_KEYIX_NONE)
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");
674 acx_next_scan(void *arg)
676 struct acx_softc *sc = arg;
677 struct ieee80211com *ic = &sc->sc_ic;
678 struct ifnet *ifp = &ic->ic_if;
680 lwkt_serialize_enter(ifp->if_serializer);
682 if (ic->ic_state == IEEE80211_S_SCAN)
683 ieee80211_next_scan(ic);
685 lwkt_serialize_exit(ifp->if_serializer);
689 acx_stop(struct acx_softc *sc)
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;
697 ASSERT_SERIALIZED(ifp->if_serializer);
699 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
701 sc->sc_firmware_ver = 0;
702 sc->sc_hardware_id = 0;
705 error = acx_reset(sc);
709 /* Firmware no longer functions after hardware reset */
710 sc->sc_flags &= ~ACX_FLAG_FW_LOADED;
712 acx_disable_intr(sc);
714 /* Stop backgroud scanning */
715 callout_stop(&sc->sc_scan_timer);
717 /* Turn off power led */
718 CSR_SETB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
721 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
722 struct acx_txbuf *buf;
724 buf = &bd->tx_buf[i];
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);
733 if (buf->tb_node != NULL)
734 ieee80211_free_node(buf->tb_node);
738 /* Clear TX host descriptors */
739 bzero(rd->tx_ring, ACX_TX_RING_SIZE);
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;
751 /* Clear RX host descriptors */
752 bzero(rd->rx_ring, ACX_RX_RING_SIZE);
756 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
762 acx_config(struct acx_softc *sc)
764 struct acx_config conf;
767 error = acx_read_config(sc, &conf);
771 error = acx_write_config(sc, &conf);
775 error = acx_rx_config(sc, sc->sc_flags & ACX_FLAG_PROMISC);
779 if (acx_set_probe_req_tmplt(sc, "", 0) != 0) {
780 if_printf(&sc->sc_ic.ic_if, "can't set probe req template "
786 if (acx_set_null_tmplt(sc) != 0) {
787 if_printf(&sc->sc_ic.ic_if, "can't set null data template\n");
794 acx_read_config(struct acx_softc *sc, struct acx_config *conf)
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;
805 if (acx_get_eaddr_conf(sc, &addr) != 0) {
806 if_printf(&sc->sc_ic.ic_if, "can't get station id\n");
811 * Get and print station id in case that EEPROM station id's
812 * offset is not correct
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",
819 /* Get region domain */
820 if (acx_get_regdom_conf(sc, ®_dom) != 0) {
821 if_printf(&sc->sc_ic.ic_if, "can't get region domain\n");
824 conf->regdom = reg_dom.regdom;
825 DPRINTF((&sc->sc_ic.ic_if, "regdom %02x\n", reg_dom.regdom));
828 if (acx_get_antenna_conf(sc, &ant) != 0) {
829 if_printf(&sc->sc_ic.ic_if, "can't get antenna\n");
832 conf->antenna = ant.antenna;
833 DPRINTF((&sc->sc_ic.ic_if, "antenna %02x\n", ant.antenna));
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);
841 if_printf(&sc->sc_ic.ic_if, "can't get sensitivity\n");
847 DPRINTF((&sc->sc_ic.ic_if, "sensitivity %02x\n", sen));
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");
855 if (strncmp(fw_rev.fw_rev, "Rev ", 4) != 0) {
856 if_printf(&sc->sc_ic.ic_if, "strange revision string -- %s\n",
858 fw_rev_no = 0x01090407;
867 s = &fw_rev.fw_rev[4];
869 for (i = 0; i < 4; ++i) {
872 val = strtoul(s, &endp, 16);
873 fw_rev_no |= val << ((3 - i) * 8);
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));
886 if (sc->chip_read_config != NULL) {
887 error = sc->chip_read_config(sc, conf);
895 acx_write_config(struct acx_softc *sc, struct acx_config *conf)
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;
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");
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");
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");
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");
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");
940 /* Set region domain */
941 reg_dom.regdom = conf->regdom;
942 if (acx_set_regdom_conf(sc, ®_dom) != 0) {
943 if_printf(&sc->sc_ic.ic_if, "can't set region domain\n");
947 if (sc->chip_write_config != NULL) {
948 error = sc->chip_write_config(sc, conf);
957 acx_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
959 struct acx_softc *sc = ifp->if_softc;
960 struct ieee80211com *ic = &sc->sc_ic;
965 req = (struct ifreq *)data;
969 error = suser_cred(cr, NULL_CRED_OKAY);
973 error = acx_copyin_firmware(sc, req);
976 error = suser_cred(cr, NULL_CRED_OKAY);
979 acx_free_firmware(sc);
982 error = copyout(&sc->sc_radio_type, req->ifr_data,
983 sizeof(sc->sc_radio_type));
986 error = copyout(&sc->sc_firmware_ver, req->ifr_data,
987 sizeof(sc->sc_firmware_ver));
990 error = copyout(&sc->sc_hardware_id, req->ifr_data,
991 sizeof(sc->sc_hardware_id));
994 error = copyout(&sc->sc_stats, req->ifr_data,
995 sizeof(sc->sc_stats));
998 if (ifp->if_flags & IFF_UP) {
999 if ((ifp->if_flags & IFF_RUNNING)) {
1002 if ((ifp->if_flags & IFF_PROMISC) &&
1003 (sc->sc_flags & ACX_FLAG_PROMISC) == 0)
1005 else if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1006 (sc->sc_flags & ACX_FLAG_PROMISC))
1010 * Promisc mode is always enabled when
1011 * operation mode is Monitor.
1013 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1015 error = acx_rx_config(sc, promisc);
1020 if (ifp->if_flags & IFF_RUNNING)
1024 if (ifp->if_flags & IFF_PROMISC)
1025 sc->sc_flags |= ACX_FLAG_PROMISC;
1027 sc->sc_flags &= ~ACX_FLAG_PROMISC;
1034 error = ieee80211_ioctl(ic, cmd, data, cr);
1038 if (error == ENETRESET) {
1039 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
1040 (IFF_RUNNING | IFF_UP))
1048 acx_start(struct ifnet *ifp)
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;
1056 ASSERT_SERIALIZED(ifp->if_serializer);
1058 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0 ||
1059 (ifp->if_flags & IFF_RUNNING) == 0 ||
1060 (ifp->if_flags & IFF_OACTIVE))
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.
1069 idx = bd->tx_free_start;
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;
1078 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1079 IF_DEQUEUE(&ic->ic_mgtq, m);
1081 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1082 m->m_pkthdr.rcvif = NULL;
1087 * Don't transmit probe response firmware will
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) {
1096 ieee80211_free_node(ni);
1100 } else if (!ifq_is_empty(&ifp->if_snd)) {
1101 struct ether_header *eh;
1103 if (ic->ic_state != IEEE80211_S_RUN)
1106 m = ifq_dequeue(&ifp->if_snd, NULL);
1110 if (m->m_len < sizeof(struct ether_header)) {
1111 m = m_pullup(m, sizeof(struct ether_header));
1117 eh = mtod(m, struct ether_header *);
1119 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1126 /* TODO power save */
1130 m = ieee80211_encap(ic, m, ni);
1132 ieee80211_free_node(ni);
1140 if (ic->ic_rawbpf != NULL)
1141 bpf_mtap(ic->ic_rawbpf, m);
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);
1155 * Since mgmt data are transmitted at fixed rate
1156 * they will not be used to do rate control.
1158 if (mgmt_pkt && ni != NULL) {
1159 ieee80211_free_node(ni);
1163 if (acx_encap(sc, buf, m, ni) != 0) {
1165 * NOTE: `m' will be freed in acx_encap()
1169 ieee80211_free_node(ni);
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
1183 bd->tx_used_count++;
1184 idx = (idx + 1) % ACX_TX_DESC_CNT;
1186 bd->tx_free_start = idx;
1188 if (bd->tx_used_count == ACX_TX_DESC_CNT)
1189 ifp->if_flags |= IFF_OACTIVE;
1191 if (trans && sc->sc_tx_timer == 0)
1192 sc->sc_tx_timer = 5;
1197 acx_watchdog(struct ifnet *ifp)
1199 struct acx_softc *sc = ifp->if_softc;
1203 if ((ifp->if_flags & IFF_RUNNING) == 0)
1206 if (sc->sc_tx_timer) {
1207 if (--sc->sc_tx_timer == 0) {
1208 if_printf(ifp, "watchdog timeout\n");
1210 acx_txeof(ifp->if_softc);
1215 ieee80211_watchdog(&sc->sc_ic);
1221 struct acx_softc *sc = arg;
1222 uint16_t intr_status;
1224 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0)
1227 intr_status = CSR_READ_2(sc, ACXREG_INTR_STATUS_CLR);
1228 if (intr_status == ACXRV_INTR_ALL) {
1229 /* not our interrupt */
1233 intr_status &= sc->chip_intr_enable;
1234 if (intr_status == 0) {
1235 /* not interrupts we care about */
1239 /* Acknowledge all interrupts */
1240 CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_ALL);
1242 if (intr_status & ACXRV_INTR_TX_FINI)
1245 if (intr_status & ACXRV_INTR_RX_FINI)
1250 acx_disable_intr(struct acx_softc *sc)
1252 CSR_WRITE_2(sc, ACXREG_INTR_MASK, sc->chip_intr_disable);
1253 CSR_WRITE_2(sc, ACXREG_EVENT_MASK, 0);
1257 acx_enable_intr(struct acx_softc *sc)
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);
1265 acx_txeof(struct acx_softc *sc)
1267 struct acx_buf_data *bd;
1268 struct acx_txbuf *buf;
1272 ifp = &sc->sc_ic.ic_if;
1273 ASSERT_SERIALIZED(ifp->if_serializer);
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;
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))
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;
1292 error = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_error);
1294 acx_txerr(sc, error);
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;
1306 FW_TXDESC_SETFIELD_1(sc, buf, f_tx_ctrl, DESC_CTRL_HOSTOWN);
1308 bd->tx_used_count--;
1310 idx = (idx + 1) % ACX_TX_DESC_CNT;
1312 bd->tx_used_start = idx;
1314 sc->sc_tx_timer = bd->tx_used_count == 0 ? 0 : 5;
1316 if (bd->tx_used_count != ACX_TX_DESC_CNT) {
1317 ifp->if_flags &= ~IFF_OACTIVE;
1323 acx_txerr(struct acx_softc *sc, uint8_t err)
1325 struct ifnet *ifp = &sc->sc_ic.ic_if;
1326 struct acx_stats *stats = &sc->sc_stats;
1328 if (err == DESC_ERR_EXCESSIVE_RETRY) {
1330 * This a common error (see comment below),
1331 * so print it using DPRINTF()
1333 DPRINTF((ifp, "TX failed -- excessive retry\n"));
1335 if_printf(ifp, "TX failed -- ");
1339 * Although `err' looks like bitmask, it never
1340 * has multiple bits set.
1344 case DESC_ERR_OTHER_FRAG:
1345 /* XXX what's this */
1346 kprintf("error in other fragment\n");
1347 stats->err_oth_frag++;
1350 case DESC_ERR_ABORT:
1351 kprintf("aborted\n");
1354 case DESC_ERR_PARAM:
1355 kprintf("wrong parameters in descriptor\n");
1358 case DESC_ERR_NO_WEPKEY:
1359 kprintf("WEP key missing\n");
1360 stats->err_no_wepkey++;
1362 case DESC_ERR_MSDU_TIMEOUT:
1363 kprintf("MSDU life timeout\n");
1364 stats->err_msdu_timeout++;
1366 case DESC_ERR_EXCESSIVE_RETRY:
1369 * 1) Distance is too long
1370 * 2) Transmit failed (e.g. no MAC level ACK)
1371 * 3) Chip overheated (this should be rare)
1373 stats->err_ex_retry++;
1375 case DESC_ERR_BUF_OVERFLOW:
1376 kprintf("buffer overflow\n");
1377 stats->err_buf_oflow++;
1380 kprintf("DMA error\n");
1384 kprintf("unknown error %d\n", err);
1391 acx_rxeof(struct acx_softc *sc)
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;
1399 ASSERT_SERIALIZED(ic->ic_if.if_serializer);
1401 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1402 BUS_DMASYNC_POSTREAD);
1405 * Locate first "ready" rx buffer,
1406 * start from last stopped position
1408 idx = bd->rx_scan_start;
1411 struct acx_rxbuf *buf;
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))) {
1419 idx = (idx + 1) % ACX_RX_DESC_CNT;
1420 } while (idx != bd->rx_scan_start);
1426 * NOTE: don't mess up `idx' here, it will
1427 * be used in the following code
1431 struct acx_rxbuf_hdr *head;
1432 struct acx_rxbuf *buf;
1433 struct ieee80211_frame_min *wh;
1435 uint32_t desc_status;
1437 int len, error, rssi, is_priv;
1439 buf = &bd->rx_buf[idx];
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))
1447 bus_dmamap_sync(bd->mbuf_dma_tag, buf->rb_mbuf_dmamap,
1448 BUS_DMASYNC_POSTREAD);
1452 error = acx_newbuf(sc, buf, 0);
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);
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;
1466 wh = mtod(m, struct ieee80211_frame_min *);
1467 is_priv = (wh->i_fc[1] & IEEE80211_FC1_WEP);
1469 if (sc->sc_drvbpf != NULL) {
1470 sc->sc_rx_th.wr_tsf = htole32(head->rbh_time);
1472 sc->sc_rx_th.wr_flags = 0;
1474 sc->sc_rx_th.wr_flags |=
1475 IEEE80211_RADIOTAP_F_WEP;
1477 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_SHPRE) {
1478 sc->sc_rx_th.wr_flags |=
1479 IEEE80211_RADIOTAP_F_SHORTPRE;
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);
1487 sc->sc_rx_th.wr_rate =
1488 ieee80211_plcp2rate(head->rbh_plcp, 0);
1491 sc->sc_rx_th.wr_antsignal = rssi;
1493 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_ANT1)
1494 sc->sc_rx_th.wr_antenna = 1;
1496 sc->sc_rx_th.wr_antenna = 0;
1498 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th,
1502 if (len >= sizeof(struct ieee80211_frame_min) &&
1504 struct ieee80211_node *ni;
1506 if (is_priv && sc->chip_hw_crypt) {
1507 /* Short circuit software WEP */
1508 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1510 /* Do chip specific RX buffer processing */
1511 if (sc->chip_proc_wep_rxbuf != NULL) {
1512 sc->chip_proc_wep_rxbuf(sc, m, &len);
1514 struct ieee80211_frame_min *);
1517 m->m_len = m->m_pkthdr.len = len;
1519 ni = ieee80211_find_rxnode(ic, wh);
1520 ieee80211_input(ic, m, ni, rssi,
1521 le32toh(head->rbh_time));
1522 ieee80211_free_node(ni);
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);
1531 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1532 ic->ic_stats.is_rx_tooshort++;
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);
1542 idx = (idx + 1) % ACX_RX_DESC_CNT;
1543 } while (idx != bd->rx_scan_start);
1546 * Record the position so that next
1547 * time we can start from it
1549 bd->rx_scan_start = idx;
1553 acx_reset(struct acx_softc *sc)
1558 CSR_SETB_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_HALT);
1560 /* Software reset */
1561 reg = CSR_READ_2(sc, ACXREG_SOFT_RESET);
1562 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg | ACXRV_SOFT_RESET);
1564 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg);
1566 /* Initialize EEPROM */
1567 CSR_SETB_2(sc, ACXREG_EEPROM_INIT, ACXRV_EEPROM_INIT);
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");
1580 acx_read_eeprom(struct acx_softc *sc, uint32_t offset, uint8_t *val)
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);
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)
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);
1599 #undef EE_READ_RETRY_MAX
1601 *val = CSR_READ_1(sc, ACXREG_EEPROM_DATA);
1606 acx_read_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t *val)
1610 CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1611 CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_READ);
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)
1619 if (i == PHY_READ_RETRY_MAX) {
1620 if_printf(&sc->sc_ic.ic_if, "can't read phy reg %x (timeout)\n",
1624 #undef PHY_READ_RETRY_MAX
1626 *val = CSR_READ_1(sc, ACXREG_PHY_DATA);
1631 acx_write_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t val)
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);
1639 acx_copyin_firmware(struct acx_softc *sc, struct ifreq *req)
1641 struct acx_firmware ufw, *kfw;
1642 uint8_t *base_fw, *radio_fw;
1645 kfw = &sc->sc_firmware;
1649 error = copyin(req->ifr_data, &ufw, sizeof(ufw));
1654 * For combined base firmware, there is no radio firmware.
1655 * But base firmware must exist.
1657 if (ufw.base_fw_len <= 0 || ufw.radio_fw_len < 0)
1660 base_fw = kmalloc(ufw.base_fw_len, M_DEVBUF, M_INTWAIT);
1661 error = copyin(ufw.base_fw, base_fw, ufw.base_fw_len);
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);
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;
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;
1684 if (base_fw != NULL)
1685 kfree(base_fw, M_DEVBUF);
1686 if (radio_fw != NULL)
1687 kfree(radio_fw, M_DEVBUF);
1692 acx_free_firmware(struct acx_softc *sc)
1694 struct acx_firmware *fw = &sc->sc_firmware;
1696 if (fw->base_fw != NULL) {
1697 kfree(fw->base_fw, M_DEVBUF);
1699 fw->base_fw_len = 0;
1701 if (fw->radio_fw != NULL) {
1702 kfree(fw->radio_fw, M_DEVBUF);
1703 fw->radio_fw = NULL;
1704 fw->radio_fw_len = 0;
1709 acx_load_base_firmware(struct acx_softc *sc, const uint8_t *base_fw,
1710 uint32_t base_fw_len)
1714 /* Load base firmware */
1715 error = acx_load_firmware(sc, 0, base_fw, base_fw_len);
1717 if_printf(&sc->sc_ic.ic_if, "can't load base firmware\n");
1720 DPRINTF((&sc->sc_ic.ic_if, "base firmware loaded\n"));
1723 CSR_WRITE_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_START);
1725 /* Wait for ECPU to be up */
1726 for (i = 0; i < 500; ++i) {
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);
1737 if_printf(&sc->sc_ic.ic_if, "can't initialize ECPU (timeout)\n");
1742 acx_load_radio_firmware(struct acx_softc *sc, const uint8_t *radio_fw,
1743 uint32_t radio_fw_len)
1745 struct acx_conf_mmap mem_map;
1746 uint32_t radio_fw_ofs;
1750 * Get the position, where base firmware is loaded, so that
1751 * radio firmware can be loaded after it.
1753 if (acx_get_mmap_conf(sc, &mem_map) != 0)
1755 radio_fw_ofs = le32toh(mem_map.code_end);
1757 /* Put ECPU into sleeping state, before loading radio firmware */
1758 if (acx_sleep(sc) != 0)
1761 /* Load radio firmware */
1762 error = acx_load_firmware(sc, radio_fw_ofs, radio_fw, radio_fw_len);
1764 if_printf(&sc->sc_ic.ic_if, "can't load radio firmware\n");
1767 DPRINTF((&sc->sc_ic.ic_if, "radio firmware loaded\n"));
1769 /* Wake up sleeping ECPU, after radio firmware is loaded */
1770 if (acx_wakeup(sc) != 0)
1773 /* Initialize radio */
1774 if (acx_init_radio(sc, radio_fw_ofs, radio_fw_len) != 0)
1777 /* Verify radio firmware's loading position */
1778 if (acx_get_mmap_conf(sc, &mem_map) != 0)
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 "
1786 DPRINTF((&sc->sc_ic.ic_if, "radio firmware initialized\n"));
1791 acx_load_firmware(struct acx_softc *sc, uint32_t offset, const uint8_t *data,
1797 fw = (const uint32_t *)data;
1798 fw_len = data_len / sizeof(uint32_t);
1801 * LOADFW_AUTO_INC only works with some older firmware:
1802 * 1) acx100's firmware
1803 * 2) acx111's firmware whose rev is 0x00010011
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);
1811 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1812 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1815 for (i = 0; i < fw_len; ++i) {
1816 #ifndef LOADFW_AUTO_INC
1817 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1819 CSR_WRITE_4(sc, ACXREG_FWMEM_DATA, be32toh(fw[i]));
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);
1827 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1828 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1831 for (i = 0; i < fw_len; ++i) {
1834 #ifndef LOADFW_AUTO_INC
1835 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
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);
1848 acx_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
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;
1856 ASSERT_SERIALIZED(ifp->if_serializer);
1858 ieee80211_ratectl_newstate(ic, nstate);
1859 callout_stop(&sc->sc_scan_timer);
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,
1868 case IEEE80211_S_AUTH:
1869 if (ic->ic_opmode == IEEE80211_M_STA) {
1872 mode = ACX_MODE_STA;
1875 case IEEE80211_S_RUN:
1876 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1877 ic->ic_opmode == IEEE80211_M_HOSTAP) {
1880 if (ic->ic_opmode == IEEE80211_M_IBSS)
1881 mode = ACX_MODE_ADHOC;
1885 if (acx_set_beacon_tmplt(sc, ni) != 0) {
1886 if_printf(ifp, "set bescon template failed\n");
1889 if (acx_set_probe_resp_tmplt(sc, ni) != 0) {
1890 if_printf(ifp, "set probe response template"
1894 } else if (ic->ic_opmode == IEEE80211_M_MONITOR) {
1897 mode = ACX_MODE_STA;
1905 KKASSERT(c != NULL);
1907 if (acx_set_chan(sc, c) != 0)
1910 if (acx_join_bss(sc, mode, ni, c) != 0) {
1911 if_printf(ifp, "join BSS failed\n");
1920 nstate = IEEE80211_S_INIT;
1923 return sc->sc_newstate(ic, nstate, arg);
1927 acx_init_tmplt_ordered(struct acx_softc *sc)
1929 #define INIT_TMPLT(name) \
1931 if (acx_init_##name##_tmplt(sc) != 0) \
1937 * Order of templates initialization:
1943 * Above order is critical to get a correct memory map.
1945 INIT_TMPLT(probe_req);
1946 INIT_TMPLT(null_data);
1949 INIT_TMPLT(probe_resp);
1956 acx_ring_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1958 *((uint32_t *)arg) = seg->ds_addr;
1962 acx_dma_alloc(struct acx_softc *sc)
1964 struct acx_ring_data *rd = &sc->sc_ring_data;
1965 struct acx_buf_data *bd = &sc->sc_buf_data;
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,
1972 ACX_RX_RING_SIZE, 1, ACX_RX_RING_SIZE,
1973 0, &rd->rx_ring_dma_tag);
1975 if_printf(&sc->sc_ic.ic_if, "can't create rx ring dma tag\n");
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);
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;
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,
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;
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,
2007 ACX_TX_RING_SIZE, 1, ACX_TX_RING_SIZE,
2008 0, &rd->tx_ring_dma_tag);
2010 if_printf(&sc->sc_ic.ic_if, "can't create tx ring dma tag\n");
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);
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;
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,
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;
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,
2042 MCLBYTES, 1, MCLBYTES,
2043 0, &bd->mbuf_dma_tag);
2045 if_printf(&sc->sc_ic.ic_if, "can't create mbuf dma tag\n");
2049 /* Create a spare RX DMA map */
2050 error = bus_dmamap_create(bd->mbuf_dma_tag, 0, &bd->mbuf_tmp_dmamap);
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;
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);
2063 if_printf(&sc->sc_ic.ic_if, "can't create rx mbuf "
2064 "dma map (%d)\n", i);
2067 bd->rx_buf[i].rb_desc = &rd->rx_ring[i];
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);
2075 if_printf(&sc->sc_ic.ic_if, "can't create tx mbuf "
2076 "dma map (%d)\n", i);
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];
2087 acx_dma_free(struct acx_softc *sc)
2089 struct acx_ring_data *rd = &sc->sc_ring_data;
2090 struct acx_buf_data *bd = &sc->sc_buf_data;
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);
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);
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);
2114 bus_dmamap_destroy(bd->mbuf_dma_tag,
2115 bd->rx_buf[i].rb_mbuf_dmamap);
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);
2126 bus_dmamap_destroy(bd->mbuf_dma_tag,
2127 bd->tx_buf[i].tb_mbuf_dmamap);
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);
2138 acx_init_tx_ring(struct acx_softc *sc)
2140 struct acx_ring_data *rd;
2141 struct acx_buf_data *bd;
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);
2150 rd->tx_ring[i].h_ctrl = htole16(DESC_CTRL_HOSTOWN);
2152 if (i == (ACX_TX_DESC_CNT * 2) - 1)
2153 rd->tx_ring[i].h_next_desc = htole32(rd->tx_ring_paddr);
2155 rd->tx_ring[i].h_next_desc = htole32(paddr);
2158 bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2159 BUS_DMASYNC_PREWRITE);
2161 bd = &sc->sc_buf_data;
2162 bd->tx_free_start = 0;
2163 bd->tx_used_start = 0;
2164 bd->tx_used_count = 0;
2170 acx_init_rx_ring(struct acx_softc *sc)
2172 struct acx_ring_data *rd;
2173 struct acx_buf_data *bd;
2177 bd = &sc->sc_buf_data;
2178 rd = &sc->sc_ring_data;
2179 paddr = rd->rx_ring_paddr;
2181 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2184 paddr += sizeof(struct acx_host_desc);
2186 error = acx_newbuf(sc, &bd->rx_buf[i], 1);
2190 if (i == ACX_RX_DESC_CNT - 1)
2191 rd->rx_ring[i].h_next_desc = htole32(rd->rx_ring_paddr);
2193 rd->rx_ring[i].h_next_desc = htole32(paddr);
2196 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2197 BUS_DMASYNC_PREWRITE);
2199 bd->rx_scan_start = 0;
2204 acx_buf_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2205 bus_size_t mapsz, int error)
2211 KASSERT(nseg == 1, ("too many RX dma segments\n"));
2212 *((uint32_t *)arg) = seg->ds_addr;
2216 acx_newbuf(struct acx_softc *sc, struct acx_rxbuf *rb, int wait)
2218 struct acx_buf_data *bd;
2224 bd = &sc->sc_buf_data;
2226 m = m_getcl(wait ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2230 m->m_len = m->m_pkthdr.len = MCLBYTES;
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);
2237 if_printf(&sc->sc_ic.ic_if, "can't map rx mbuf %d\n", error);
2241 /* Unload originally mapped mbuf */
2242 bus_dmamap_unload(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap);
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;
2250 rb->rb_desc->h_data_paddr = htole32(paddr);
2251 rb->rb_desc->h_data_len = htole16(m->m_len);
2253 bus_dmamap_sync(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap,
2254 BUS_DMASYNC_PREREAD);
2259 acx_encap(struct acx_softc *sc, struct acx_txbuf *txbuf, struct mbuf *m,
2260 struct ieee80211_node *ni)
2262 struct acx_buf_data *bd = &sc->sc_buf_data;
2263 struct acx_ring_data *rd = &sc->sc_ring_data;
2268 KASSERT(txbuf->tb_mbuf == NULL, ("free TX buf has mbuf installed\n"));
2271 if (m->m_pkthdr.len > MCLBYTES) {
2272 if_printf(&sc->sc_ic.ic_if, "mbuf too big\n");
2275 } else if (m->m_pkthdr.len < ACX_FRAME_HDRLEN) {
2276 if_printf(&sc->sc_ic.ic_if, "mbuf too small\n");
2281 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2282 m, acx_buf_dma_addr, &paddr,
2284 if (error && error != EFBIG) {
2285 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf1 %d\n", error);
2289 if (error) { /* error == EFBIG */
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");
2301 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag,
2302 txbuf->tb_mbuf_dmamap, m,
2303 acx_buf_dma_addr, &paddr,
2306 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf2 %d\n",
2314 bus_dmamap_sync(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2315 BUS_DMASYNC_PREWRITE);
2318 txbuf->tb_node = ni;
2321 * TX buffers are accessed in following way:
2322 * acx_fw_txdesc -> acx_host_desc -> buffer
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
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
2332 * host_desc1.h_data_len = buffer_len
2333 * host_desc2.h_data_len = buffer_len - mac_header_len
2336 * host_desc1.h_data_len = mac_header_len
2337 * host_desc2.h_data_len = buffer_len - mac_header_len
2340 txbuf->tb_desc1->h_data_paddr = htole32(paddr);
2341 txbuf->tb_desc2->h_data_paddr = htole32(paddr + ACX_FRAME_HDRLEN);
2343 txbuf->tb_desc1->h_data_len =
2344 htole16(sc->chip_txdesc1_len ? sc->chip_txdesc1_len
2346 txbuf->tb_desc2->h_data_len =
2347 htole16(m->m_pkthdr.len - ACX_FRAME_HDRLEN);
2351 * We can't simply assign f_tx_ctrl, we will first read it back
2352 * and change it bit by bit
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);
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);
2365 if (sc->sc_drvbpf != NULL) {
2366 struct ieee80211_frame_min *wh;
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;
2374 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_tx_th, sc->sc_tx_th_len);
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);
2382 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl2, 0);
2383 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl, ctrl);
2385 /* Tell chip to inform us about TX completion */
2386 CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_TX_FINI);
2394 acx_set_null_tmplt(struct acx_softc *sc)
2396 struct acx_tmplt_null_data n;
2397 struct ieee80211_frame *f;
2399 bzero(&n, sizeof(n));
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);
2408 return _acx_set_null_data_tmplt(sc, &n, sizeof(n));
2412 acx_set_probe_req_tmplt(struct acx_softc *sc, const char *ssid, int ssid_len)
2414 struct acx_tmplt_probe_req req;
2415 struct ieee80211_frame *f;
2419 bzero(&req, sizeof(req));
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);
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;
2434 return _acx_set_probe_req_tmplt(sc, &req,
2435 ACX_TMPLT_PROBE_REQ_SIZ(vlen));
2439 acx_set_probe_resp_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2441 struct ieee80211com *ic = &sc->sc_ic;
2442 struct acx_tmplt_probe_resp resp;
2443 struct ieee80211_frame *f;
2447 m = ieee80211_probe_resp_alloc(ic, ni);
2450 DPRINTF((&ic->ic_if, "%s alloc probe resp size %d\n", __func__,
2453 f = mtod(m, struct ieee80211_frame *);
2454 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
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);
2461 return _acx_set_probe_resp_tmplt(sc, &resp, len);
2465 acx_set_beacon_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
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;
2472 int beacon_tmplt_len = 0, tim_tmplt_len = 0;
2474 bzero(&bo, sizeof(bo));
2475 m = ieee80211_beacon_alloc(ic, ni, &bo);
2478 DPRINTF((&ic->ic_if, "%s alloc beacon size %d\n", __func__,
2481 if (bo.bo_tim_len == 0) {
2482 beacon_tmplt_len = m->m_pkthdr.len;
2484 beacon_tmplt_len = bo.bo_tim - mtod(m, uint8_t *);
2485 tim_tmplt_len = m->m_pkthdr.len - beacon_tmplt_len;
2488 bzero(&beacon, sizeof(beacon));
2489 bzero(&tim, sizeof(tim));
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);
2498 beacon_tmplt_len += sizeof(beacon.size);
2499 if (_acx_set_beacon_tmplt(sc, &beacon, beacon_tmplt_len) != 0)
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)
2511 acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS)
2513 struct acx_softc *sc = arg1;
2514 struct ifnet *ifp = &sc->sc_ic.ic_if;
2517 lwkt_serialize_enter(ifp->if_serializer);
2519 v = sc->sc_msdu_lifetime;
2520 error = sysctl_handle_int(oidp, &v, 0, req);
2521 if (error || req->newptr == NULL)
2528 if (sc->sc_flags & ACX_FLAG_FW_LOADED) {
2529 struct acx_conf_msdu_lifetime msdu_lifetime;
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");
2539 sc->sc_msdu_lifetime = v;
2541 lwkt_serialize_exit(ifp->if_serializer);
2546 acx_media_change(struct ifnet *ifp)
2550 error = ieee80211_media_change(ifp);
2551 if (error != ENETRESET)
2554 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
2555 acx_init(ifp->if_softc);
2560 acx_rx_config(struct acx_softc *sc, int promisc)
2562 struct acx_conf_rxopt rx_opt;
2563 struct ieee80211com *ic = &sc->sc_ic;
2566 * What we want to receive and how to receive
2569 /* Common for all operational modes */
2570 rx_opt.opt1 = RXOPT1_INCL_RXBUF_HDR;
2571 rx_opt.opt2 = RXOPT2_RECV_ASSOC_REQ |
2573 RXOPT2_RECV_BEACON |
2578 RXOPT2_RECV_PROBE_REQ |
2579 RXOPT2_RECV_PROBE_RESP |
2582 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2583 rx_opt.opt1 |= RXOPT1_PROMISC;
2584 rx_opt.opt2 |= RXOPT2_RECV_BROKEN | RXOPT2_RECV_ACK;
2586 rx_opt.opt1 |= promisc ? RXOPT1_PROMISC : RXOPT1_FILT_FDEST;
2589 if (acx_set_rxopt_conf(sc, &rx_opt) != 0) {
2590 if_printf(&sc->sc_ic.ic_if, "can't config RX\n");
2597 acx_set_chan(struct acx_softc *sc, struct ieee80211_channel *c)
2599 struct ieee80211com *ic = &sc->sc_ic;
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);
2609 if (acx_enable_rxchan(sc, chan) != 0) {
2610 if_printf(&ic->ic_if, "enable RX on channel %d failed\n", chan);
2614 if (IEEE80211_IS_CHAN_G(c))
2615 flags = IEEE80211_CHAN_G;
2617 flags = IEEE80211_CHAN_B;
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 =