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.20 2007/02/17 07:05:54 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>
98 #include <bus/pci/pcireg.h>
99 #include <bus/pci/pcivar.h>
100 #include <bus/pci/pcidevs.h>
104 #include <dev/netif/acx/if_acxreg.h>
105 #include <dev/netif/acx/if_acxvar.h>
106 #include <dev/netif/acx/acxcmd.h>
108 #define SIOCSLOADFW _IOW('i', 137, struct ifreq) /* load firmware */
109 #define SIOCGRADIO _IOW('i', 138, struct ifreq) /* get radio type */
110 #define SIOCGSTATS _IOW('i', 139, struct ifreq) /* get acx stats */
111 #define SIOCSKILLFW _IOW('i', 140, struct ifreq) /* free firmware */
112 #define SIOCGFWVER _IOW('i', 141, struct ifreq) /* get firmware ver */
113 #define SIOCGHWID _IOW('i', 142, struct ifreq) /* get hardware id */
115 static int acx_probe(device_t);
116 static int acx_attach(device_t);
117 static int acx_detach(device_t);
118 static int acx_shutdown(device_t);
120 static void acx_init(void *);
121 static void acx_start(struct ifnet *);
122 static int acx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
123 static void acx_watchdog(struct ifnet *);
125 static void acx_intr(void *);
126 static void acx_txeof(struct acx_softc *);
127 static void acx_txerr(struct acx_softc *, uint8_t);
128 static void acx_rxeof(struct acx_softc *);
129 static void acx_disable_intr(struct acx_softc *);
130 static void acx_enable_intr(struct acx_softc *);
132 static int acx_reset(struct acx_softc *);
133 static int acx_stop(struct acx_softc *);
134 static void acx_init_info_reg(struct acx_softc *);
135 static int acx_config(struct acx_softc *);
136 static int acx_read_config(struct acx_softc *, struct acx_config *);
137 static int acx_write_config(struct acx_softc *, struct acx_config *);
138 static int acx_rx_config(struct acx_softc *, int);
139 static int acx_set_crypt_keys(struct acx_softc *);
141 static int acx_dma_alloc(struct acx_softc *);
142 static void acx_dma_free(struct acx_softc *);
143 static int acx_init_tx_ring(struct acx_softc *);
144 static int acx_init_rx_ring(struct acx_softc *);
145 static int acx_newbuf(struct acx_softc *, struct acx_rxbuf *, int);
146 static int acx_encap(struct acx_softc *, struct acx_txbuf *,
147 struct mbuf *, struct ieee80211_node *);
149 static int acx_set_null_tmplt(struct acx_softc *);
150 static int acx_set_probe_req_tmplt(struct acx_softc *, const char *, int);
151 static int acx_set_probe_resp_tmplt(struct acx_softc *,
152 struct ieee80211_node *);
153 static int acx_set_beacon_tmplt(struct acx_softc *,
154 struct ieee80211_node *);
156 static int acx_read_eeprom(struct acx_softc *, uint32_t, uint8_t *);
157 static int acx_read_phyreg(struct acx_softc *, uint32_t, uint8_t *);
159 static int acx_copyin_firmware(struct acx_softc *, struct ifreq *);
160 static void acx_free_firmware(struct acx_softc *);
161 static int acx_load_firmware(struct acx_softc *, uint32_t,
162 const uint8_t *, int);
163 static int acx_load_radio_firmware(struct acx_softc *, const uint8_t *,
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);
176 const struct ieee80211_rateset acx_rates_11b =
177 { 5, { 2, 4, 11, 22, 44 } };
178 const struct ieee80211_rateset acx_rates_11g =
179 { 13, { 2, 4, 11, 22, 44, 12, 18, 24, 36, 48, 72, 96, 108 } };
181 static const struct acx_device {
184 void (*set_param)(device_t);
187 { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX100A, acx100_set_param,
188 "Texas Instruments TNETW1100A Wireless Adapter" },
189 { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX100B, acx100_set_param,
190 "Texas Instruments TNETW1100B Wireless Adapter" },
191 { PCI_VENDOR_TI, PCI_PRODUCT_TI_ACX111, acx111_set_param,
192 "Texas Instruments TNETW1130 Wireless Adapter" },
196 static device_method_t acx_methods[] = {
197 DEVMETHOD(device_probe, acx_probe),
198 DEVMETHOD(device_attach, acx_attach),
199 DEVMETHOD(device_detach, acx_detach),
200 DEVMETHOD(device_shutdown, acx_shutdown),
202 DEVMETHOD(device_suspend, acx_suspend),
203 DEVMETHOD(device_resume, acx_resume),
208 static driver_t acx_driver = {
211 sizeof(struct acx_softc)
214 static devclass_t acx_devclass;
216 DRIVER_MODULE(acx, pci, acx_driver, acx_devclass, 0, 0);
217 DRIVER_MODULE(acx, cardbus, acx_driver, acx_devclass, 0, 0);
219 MODULE_DEPEND(acx, wlan, 1, 1, 1);
220 MODULE_DEPEND(acx, wlan_ratectl_onoe, 1, 1, 1);
221 MODULE_DEPEND(acx, wlan_ratectl_amrr, 1, 1, 1);
222 MODULE_DEPEND(acx, pci, 1, 1, 1);
223 MODULE_DEPEND(acx, cardbus, 1, 1, 1);
226 acx_get_rssi(struct acx_softc *sc, uint8_t raw)
230 rssi = ((sc->chip_rssi_corr / 2) + (raw * 5)) / sc->chip_rssi_corr;
231 return rssi > 100 ? 100 : rssi;
235 acx_probe(device_t dev)
237 const struct acx_device *a;
240 vid = pci_get_vendor(dev);
241 did = pci_get_device(dev);
242 for (a = acx_devices; a->desc != NULL; ++a) {
243 if (vid == a->vid && did == a->did) {
245 device_set_desc(dev, a->desc);
253 acx_attach(device_t dev)
255 struct acx_softc *sc;
257 struct ieee80211com *ic;
260 sc = device_get_softc(dev);
264 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
267 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
268 uint32_t mem1, mem2, irq;
270 mem1 = pci_read_config(dev, sc->chip_mem1_rid, 4);
271 mem2 = pci_read_config(dev, sc->chip_mem2_rid, 4);
272 irq = pci_read_config(dev, PCIR_INTLINE, 4);
274 device_printf(dev, "chip is in D%d power mode "
275 "-- setting to D0\n", pci_get_powerstate(dev));
277 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
279 pci_write_config(dev, sc->chip_mem1_rid, mem1, 4);
280 pci_write_config(dev, sc->chip_mem2_rid, mem2, 4);
281 pci_write_config(dev, PCIR_INTLINE, irq, 4);
283 #endif /* !BURN_BRIDGE */
285 /* Enable bus mastering */
286 pci_enable_busmaster(dev);
288 /* Allocate IO memory 1 */
289 sc->sc_mem1_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
292 if (sc->sc_mem1_res == NULL) {
294 device_printf(dev, "can't allocate IO mem1\n");
297 sc->sc_mem1_bt = rman_get_bustag(sc->sc_mem1_res);
298 sc->sc_mem1_bh = rman_get_bushandle(sc->sc_mem1_res);
300 /* Allocate IO memory 2 */
301 sc->sc_mem2_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
304 if (sc->sc_mem2_res == NULL) {
306 device_printf(dev, "can't allocate IO mem2\n");
309 sc->sc_mem2_bt = rman_get_bustag(sc->sc_mem2_res);
310 sc->sc_mem2_bh = rman_get_bushandle(sc->sc_mem2_res);
313 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
315 RF_SHAREABLE | RF_ACTIVE);
316 if (sc->sc_irq_res == NULL) {
318 device_printf(dev, "can't allocate intr\n");
322 /* Initilize channel scanning timer */
323 callout_init(&sc->sc_scan_timer);
325 /* Allocate busdma stuffs */
326 error = acx_dma_alloc(sc);
331 error = acx_reset(sc);
335 /* Disable interrupts before firmware is loaded */
336 acx_disable_intr(sc);
338 /* Get radio type and form factor */
339 #define EEINFO_RETRY_MAX 50
340 for (i = 0; i < EEINFO_RETRY_MAX; ++i) {
343 ee_info = CSR_READ_2(sc, ACXREG_EEPROM_INFO);
344 if (ACX_EEINFO_HAS_RADIO_TYPE(ee_info)) {
345 sc->sc_form_factor = ACX_EEINFO_FORM_FACTOR(ee_info);
346 sc->sc_radio_type = ACX_EEINFO_RADIO_TYPE(ee_info);
351 if (i == EEINFO_RETRY_MAX) {
355 #undef EEINFO_RETRY_MAX
357 DPRINTF((&sc->sc_ic.ic_if, "radio type %02x\n", sc->sc_radio_type));
360 for (i = 0; i < 0x40; ++i) {
363 error = acx_read_eeprom(sc, i, &val);
366 kprintf("%02x ", val);
369 #endif /* DUMP_EEPROM */
371 /* Get EEPROM version */
372 error = acx_read_eeprom(sc, ACX_EE_VERSION_OFS, &sc->sc_eeprom_ver);
375 DPRINTF((&sc->sc_ic.ic_if, "EEPROM version %u\n", sc->sc_eeprom_ver));
378 * Initialize device sysctl before ieee80211_ifattach()
380 sc->sc_long_retry_limit = 4;
381 sc->sc_msdu_lifetime = 4096;
382 sc->sc_scan_dwell = 200; /* 200 milliseconds */
384 sysctl_ctx_init(&sc->sc_sysctl_ctx);
385 sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
386 SYSCTL_STATIC_CHILDREN(_hw),
388 device_get_nameunit(dev),
390 if (sc->sc_sysctl_tree == NULL) {
391 device_printf(dev, "can't add sysctl node\n");
395 SYSCTL_ADD_PROC(&sc->sc_sysctl_ctx,
396 SYSCTL_CHILDREN(sc->sc_sysctl_tree),
397 OID_AUTO, "msdu_lifetime",
398 CTLTYPE_INT | CTLFLAG_RW,
399 sc, 0, acx_sysctl_msdu_lifetime, "I",
403 ifp->if_init = acx_init;
404 ifp->if_ioctl = acx_ioctl;
405 ifp->if_start = acx_start;
406 ifp->if_watchdog = acx_watchdog;
407 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
408 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
409 ifq_set_ready(&ifp->if_snd);
412 for (i = 1; i <= 14; ++i) {
413 ic->ic_channels[i].ic_freq =
414 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
415 ic->ic_channels[i].ic_flags = sc->chip_chan_flags;
418 ic->ic_opmode = IEEE80211_M_STA;
419 ic->ic_state = IEEE80211_S_INIT;
422 * NOTE: Don't overwrite ic_caps set by chip specific code
424 ic->ic_caps |= IEEE80211_C_WEP | /* WEP */
425 IEEE80211_C_HOSTAP | /* HostAP mode */
426 IEEE80211_C_MONITOR | /* Monitor mode */
427 IEEE80211_C_IBSS | /* IBSS modes */
428 IEEE80211_C_SHPREAMBLE; /* Short preamble */
429 ic->ic_caps_ext = IEEE80211_CEXT_PBCC; /* PBCC modulation */
432 for (i = 0; i < IEEE80211_ADDR_LEN; ++i) {
433 error = acx_read_eeprom(sc, sc->chip_ee_eaddr_ofs - i,
437 ieee80211_ifattach(ic);
439 /* Enable software beacon missing */
440 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
442 /* Override newstate */
443 sc->sc_newstate = ic->ic_newstate;
444 ic->ic_newstate = acx_newstate;
446 ieee80211_media_init(ic, acx_media_change, ieee80211_media_status);
449 * Radio tap attaching
451 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
452 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
455 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(uint32_t));
456 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
457 sc->sc_tx_th.wt_ihdr.it_present = htole32(ACX_TX_RADIOTAP_PRESENT);
459 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(uint32_t));
460 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
461 sc->sc_rx_th.wr_ihdr.it_present = htole32(ACX_RX_RADIOTAP_PRESENT);
463 error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE, acx_intr, sc,
464 &sc->sc_irq_handle, ifp->if_serializer);
466 device_printf(dev, "can't set up interrupt\n");
468 ieee80211_ifdetach(ic);
473 ieee80211_announce(ic);
482 acx_detach(device_t dev)
484 struct acx_softc *sc = device_get_softc(dev);
486 if (device_is_attached(dev)) {
487 struct ieee80211com *ic = &sc->sc_ic;
488 struct ifnet *ifp = &ic->ic_if;
490 lwkt_serialize_enter(ifp->if_serializer);
493 acx_free_firmware(sc);
494 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
496 lwkt_serialize_exit(ifp->if_serializer);
499 ieee80211_ifdetach(ic);
502 if (sc->sc_sysctl_tree != NULL)
503 sysctl_ctx_free(&sc->sc_sysctl_ctx);
505 if (sc->sc_irq_res != NULL) {
506 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
509 if (sc->sc_mem1_res != NULL) {
510 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem1_rid,
513 if (sc->sc_mem2_res != NULL) {
514 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem2_rid,
523 acx_shutdown(device_t dev)
525 struct acx_softc *sc = device_get_softc(dev);
527 lwkt_serialize_enter(sc->sc_ic.ic_if.if_serializer);
529 lwkt_serialize_exit(sc->sc_ic.ic_if.if_serializer);
536 struct acx_softc *sc = arg;
537 struct ieee80211com *ic = &sc->sc_ic;
538 struct ifnet *ifp = &ic->ic_if;
539 struct acx_firmware *fw = &sc->sc_firmware;
542 error = acx_stop(sc);
546 if (fw->base_fw == NULL) {
548 if_printf(ifp, "base firmware is not loaded yet\n");
552 error = acx_init_tx_ring(sc);
554 if_printf(ifp, "can't initialize TX ring\n");
558 error = acx_init_rx_ring(sc);
560 if_printf(ifp, "can't initialize RX ring\n");
564 error = acx_load_base_firmware(sc, fw->base_fw, fw->base_fw_len);
569 * Initialize command and information registers
570 * NOTE: This should be done after base firmware is loaded
572 acx_init_cmd_reg(sc);
573 acx_init_info_reg(sc);
575 sc->sc_flags |= ACX_FLAG_FW_LOADED;
578 if (sc->chip_post_basefw != NULL) {
579 error = sc->chip_post_basefw(sc);
585 if (fw->radio_fw != NULL) {
586 error = acx_load_radio_firmware(sc, fw->radio_fw,
592 error = sc->chip_init(sc);
596 /* Get and set device various configuration */
597 error = acx_config(sc);
601 /* Setup crypto stuffs */
602 if (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) {
603 error = acx_set_crypt_keys(sc);
606 sc->sc_ic.ic_flags &= ~IEEE80211_F_DROPUNENC;
609 /* Turn on power led */
610 CSR_CLRB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
614 ifp->if_flags |= IFF_RUNNING;
615 ifp->if_flags &= ~IFF_OACTIVE;
617 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
618 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
619 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_SCAN, -1);
621 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
629 acx_init_info_reg(struct acx_softc *sc)
631 sc->sc_info = CSR_READ_4(sc, ACXREG_INFO_REG_OFFSET);
632 sc->sc_info_param = sc->sc_info + ACX_INFO_REG_SIZE;
636 acx_set_crypt_keys(struct acx_softc *sc)
638 struct ieee80211com *ic = &sc->sc_ic;
639 struct acx_conf_wep_txkey wep_txkey;
640 int i, error, got_wk = 0;
642 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
643 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
645 if (wk->wk_keylen == 0)
648 if (sc->chip_hw_crypt) {
649 error = sc->chip_set_wepkey(sc, wk, i);
653 } else if (wk->wk_flags & IEEE80211_KEY_XMIT) {
654 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
658 if (!got_wk || sc->chip_hw_crypt ||
659 ic->ic_def_txkey == IEEE80211_KEYIX_NONE)
662 /* Set current WEP key index */
663 wep_txkey.wep_txkey = ic->ic_def_txkey;
664 if (acx_set_wep_txkey_conf(sc, &wep_txkey) != 0) {
665 if_printf(&ic->ic_if, "set WEP txkey failed\n");
672 acx_next_scan(void *arg)
674 struct acx_softc *sc = arg;
675 struct ieee80211com *ic = &sc->sc_ic;
676 struct ifnet *ifp = &ic->ic_if;
678 lwkt_serialize_enter(ifp->if_serializer);
680 if (ic->ic_state == IEEE80211_S_SCAN)
681 ieee80211_next_scan(ic);
683 lwkt_serialize_exit(ifp->if_serializer);
687 acx_stop(struct acx_softc *sc)
689 struct ieee80211com *ic = &sc->sc_ic;
690 struct ifnet *ifp = &ic->ic_if;
691 struct acx_buf_data *bd = &sc->sc_buf_data;
692 struct acx_ring_data *rd = &sc->sc_ring_data;
695 ASSERT_SERIALIZED(ifp->if_serializer);
697 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
699 sc->sc_firmware_ver = 0;
700 sc->sc_hardware_id = 0;
703 error = acx_reset(sc);
707 /* Firmware no longer functions after hardware reset */
708 sc->sc_flags &= ~ACX_FLAG_FW_LOADED;
710 acx_disable_intr(sc);
712 /* Stop backgroud scanning */
713 callout_stop(&sc->sc_scan_timer);
715 /* Turn off power led */
716 CSR_SETB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
719 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
720 struct acx_txbuf *buf;
722 buf = &bd->tx_buf[i];
724 if (buf->tb_mbuf != NULL) {
725 bus_dmamap_unload(bd->mbuf_dma_tag,
726 buf->tb_mbuf_dmamap);
727 m_freem(buf->tb_mbuf);
731 if (buf->tb_node != NULL)
732 ieee80211_free_node(buf->tb_node);
736 /* Clear TX host descriptors */
737 bzero(rd->tx_ring, ACX_TX_RING_SIZE);
740 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
741 if (bd->rx_buf[i].rb_mbuf != NULL) {
742 bus_dmamap_unload(bd->mbuf_dma_tag,
743 bd->rx_buf[i].rb_mbuf_dmamap);
744 m_freem(bd->rx_buf[i].rb_mbuf);
745 bd->rx_buf[i].rb_mbuf = NULL;
749 /* Clear RX host descriptors */
750 bzero(rd->rx_ring, ACX_RX_RING_SIZE);
754 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
760 acx_config(struct acx_softc *sc)
762 struct acx_config conf;
765 error = acx_read_config(sc, &conf);
769 error = acx_write_config(sc, &conf);
773 error = acx_rx_config(sc, sc->sc_flags & ACX_FLAG_PROMISC);
777 if (acx_set_probe_req_tmplt(sc, "", 0) != 0) {
778 if_printf(&sc->sc_ic.ic_if, "can't set probe req template "
784 if (acx_set_null_tmplt(sc) != 0) {
785 if_printf(&sc->sc_ic.ic_if, "can't set null data template\n");
792 acx_read_config(struct acx_softc *sc, struct acx_config *conf)
794 struct acx_conf_eaddr addr;
795 struct acx_conf_regdom reg_dom;
796 struct acx_conf_antenna ant;
797 struct acx_conf_fwrev fw_rev;
803 if (acx_get_eaddr_conf(sc, &addr) != 0) {
804 if_printf(&sc->sc_ic.ic_if, "can't get station id\n");
809 * Get and print station id in case that EEPROM station id's
810 * offset is not correct
812 for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
813 conf->eaddr[IEEE80211_ADDR_LEN - 1 - i] = addr.eaddr[i];
814 if_printf(&sc->sc_ic.ic_if, "MAC address (from firmware): %6D\n",
817 /* Get region domain */
818 if (acx_get_regdom_conf(sc, ®_dom) != 0) {
819 if_printf(&sc->sc_ic.ic_if, "can't get region domain\n");
822 conf->regdom = reg_dom.regdom;
823 DPRINTF((&sc->sc_ic.ic_if, "regdom %02x\n", reg_dom.regdom));
826 if (acx_get_antenna_conf(sc, &ant) != 0) {
827 if_printf(&sc->sc_ic.ic_if, "can't get antenna\n");
830 conf->antenna = ant.antenna;
831 DPRINTF((&sc->sc_ic.ic_if, "antenna %02x\n", ant.antenna));
833 /* Get sensitivity XXX not used */
834 if (sc->sc_radio_type == ACX_RADIO_TYPE_MAXIM ||
835 sc->sc_radio_type == ACX_RADIO_TYPE_RFMD ||
836 sc->sc_radio_type == ACX_RADIO_TYPE_RALINK) {
837 error = acx_read_phyreg(sc, ACXRV_PHYREG_SENSITIVITY, &sen);
839 if_printf(&sc->sc_ic.ic_if, "can't get sensitivity\n");
845 DPRINTF((&sc->sc_ic.ic_if, "sensitivity %02x\n", sen));
847 /* Get firmware revision */
848 if (acx_get_fwrev_conf(sc, &fw_rev) != 0) {
849 if_printf(&sc->sc_ic.ic_if, "can't get firmware revision\n");
853 if (strncmp(fw_rev.fw_rev, "Rev ", 4) != 0) {
854 if_printf(&sc->sc_ic.ic_if, "strange revision string -- %s\n",
856 fw_rev_no = 0x01090407;
865 s = &fw_rev.fw_rev[4];
867 for (i = 0; i < 4; ++i) {
870 val = strtoul(s, &endp, 16);
871 fw_rev_no |= val << ((3 - i) * 8);
879 sc->sc_firmware_ver = fw_rev_no;
880 sc->sc_hardware_id = le32toh(fw_rev.hw_id);
881 DPRINTF((&sc->sc_ic.ic_if, "fw rev %08x, hw id %08x\n",
882 sc->sc_firmware_ver, sc->sc_hardware_id));
884 if (sc->chip_read_config != NULL) {
885 error = sc->chip_read_config(sc, conf);
893 acx_write_config(struct acx_softc *sc, struct acx_config *conf)
895 struct acx_conf_nretry_short sretry;
896 struct acx_conf_nretry_long lretry;
897 struct acx_conf_msdu_lifetime msdu_lifetime;
898 struct acx_conf_rate_fallback rate_fb;
899 struct acx_conf_antenna ant;
900 struct acx_conf_regdom reg_dom;
903 /* Set number of long/short retry */
904 KKASSERT(sc->chip_short_retry_limit > 0);
905 sretry.nretry = sc->chip_short_retry_limit;
906 if (acx_set_nretry_short_conf(sc, &sretry) != 0) {
907 if_printf(&sc->sc_ic.ic_if, "can't set short retry limit\n");
911 lretry.nretry = sc->sc_long_retry_limit;
912 if (acx_set_nretry_long_conf(sc, &lretry) != 0) {
913 if_printf(&sc->sc_ic.ic_if, "can't set long retry limit\n");
917 /* Set MSDU lifetime */
918 msdu_lifetime.lifetime = htole32(sc->sc_msdu_lifetime);
919 if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
920 if_printf(&sc->sc_ic.ic_if, "can't set MSDU lifetime\n");
924 /* Enable rate fallback */
925 rate_fb.ratefb_enable = 1;
926 if (acx_set_rate_fallback_conf(sc, &rate_fb) != 0) {
927 if_printf(&sc->sc_ic.ic_if, "can't enable rate fallback\n");
932 ant.antenna = conf->antenna;
933 if (acx_set_antenna_conf(sc, &ant) != 0) {
934 if_printf(&sc->sc_ic.ic_if, "can't set antenna\n");
938 /* Set region domain */
939 reg_dom.regdom = conf->regdom;
940 if (acx_set_regdom_conf(sc, ®_dom) != 0) {
941 if_printf(&sc->sc_ic.ic_if, "can't set region domain\n");
945 if (sc->chip_write_config != NULL) {
946 error = sc->chip_write_config(sc, conf);
955 acx_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
957 struct acx_softc *sc = ifp->if_softc;
958 struct ieee80211com *ic = &sc->sc_ic;
963 req = (struct ifreq *)data;
967 error = suser_cred(cr, NULL_CRED_OKAY);
971 error = acx_copyin_firmware(sc, req);
974 error = suser_cred(cr, NULL_CRED_OKAY);
977 acx_free_firmware(sc);
980 error = copyout(&sc->sc_radio_type, req->ifr_data,
981 sizeof(sc->sc_radio_type));
984 error = copyout(&sc->sc_firmware_ver, req->ifr_data,
985 sizeof(sc->sc_firmware_ver));
988 error = copyout(&sc->sc_hardware_id, req->ifr_data,
989 sizeof(sc->sc_hardware_id));
992 error = copyout(&sc->sc_stats, req->ifr_data,
993 sizeof(sc->sc_stats));
996 if (ifp->if_flags & IFF_UP) {
997 if ((ifp->if_flags & IFF_RUNNING)) {
1000 if ((ifp->if_flags & IFF_PROMISC) &&
1001 (sc->sc_flags & ACX_FLAG_PROMISC) == 0)
1003 else if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1004 (sc->sc_flags & ACX_FLAG_PROMISC))
1008 * Promisc mode is always enabled when
1009 * operation mode is Monitor.
1011 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1013 error = acx_rx_config(sc, promisc);
1018 if (ifp->if_flags & IFF_RUNNING)
1022 if (ifp->if_flags & IFF_PROMISC)
1023 sc->sc_flags |= ACX_FLAG_PROMISC;
1025 sc->sc_flags &= ~ACX_FLAG_PROMISC;
1032 error = ieee80211_ioctl(ic, cmd, data, cr);
1036 if (error == ENETRESET) {
1037 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
1038 (IFF_RUNNING | IFF_UP))
1046 acx_start(struct ifnet *ifp)
1048 struct acx_softc *sc = ifp->if_softc;
1049 struct ieee80211com *ic = &sc->sc_ic;
1050 struct acx_buf_data *bd = &sc->sc_buf_data;
1051 struct acx_txbuf *buf;
1054 ASSERT_SERIALIZED(ifp->if_serializer);
1056 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0 ||
1057 (ifp->if_flags & IFF_RUNNING) == 0 ||
1058 (ifp->if_flags & IFF_OACTIVE))
1063 * We can't start from a random position that TX descriptor
1064 * is free, since hardware will be confused by that.
1065 * We have to follow the order of the TX ring.
1067 idx = bd->tx_free_start;
1069 for (buf = &bd->tx_buf[idx]; buf->tb_mbuf == NULL;
1070 buf = &bd->tx_buf[idx]) {
1071 struct ieee80211_frame *f;
1072 struct ieee80211_node *ni = NULL;
1076 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1077 IF_DEQUEUE(&ic->ic_mgtq, m);
1079 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1080 m->m_pkthdr.rcvif = NULL;
1085 * Don't transmit probe response firmware will
1088 f = mtod(m, struct ieee80211_frame *);
1089 if ((f->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1090 IEEE80211_FC0_TYPE_MGT &&
1091 (f->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1092 IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1094 ieee80211_free_node(ni);
1098 } else if (!ifq_is_empty(&ifp->if_snd)) {
1099 struct ether_header *eh;
1101 if (ic->ic_state != IEEE80211_S_RUN)
1104 m = ifq_dequeue(&ifp->if_snd, NULL);
1108 if (m->m_len < sizeof(struct ether_header)) {
1109 m = m_pullup(m, sizeof(struct ether_header));
1115 eh = mtod(m, struct ether_header *);
1117 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1124 /* TODO power save */
1128 m = ieee80211_encap(ic, m, ni);
1130 ieee80211_free_node(ni);
1138 if (ic->ic_rawbpf != NULL)
1139 bpf_mtap(ic->ic_rawbpf, m);
1141 f = mtod(m, struct ieee80211_frame *);
1142 if ((f->i_fc[1] & IEEE80211_FC1_WEP) && !sc->chip_hw_crypt) {
1143 KASSERT(ni != NULL, ("TX node is NULL (WEP)\n"));
1144 if (ieee80211_crypto_encap(ic, ni, m) == NULL) {
1145 ieee80211_free_node(ni);
1153 * Since mgmt data are transmitted at fixed rate
1154 * they will not be used to do rate control.
1156 if (mgmt_pkt && ni != NULL) {
1157 ieee80211_free_node(ni);
1161 if (acx_encap(sc, buf, m, ni) != 0) {
1163 * NOTE: `m' will be freed in acx_encap()
1167 ieee80211_free_node(ni);
1174 * 1) `m' should not be touched after acx_encap()
1175 * 2) `node' will be used to do TX rate control during
1176 * acx_txeof(), so it is not freed here. acx_txeof()
1177 * will free it for us
1181 bd->tx_used_count++;
1182 idx = (idx + 1) % ACX_TX_DESC_CNT;
1184 bd->tx_free_start = idx;
1186 if (bd->tx_used_count == ACX_TX_DESC_CNT)
1187 ifp->if_flags |= IFF_OACTIVE;
1189 if (trans && sc->sc_tx_timer == 0)
1190 sc->sc_tx_timer = 5;
1195 acx_watchdog(struct ifnet *ifp)
1197 struct acx_softc *sc = ifp->if_softc;
1201 if ((ifp->if_flags & IFF_RUNNING) == 0)
1204 if (sc->sc_tx_timer) {
1205 if (--sc->sc_tx_timer == 0) {
1206 if_printf(ifp, "watchdog timeout\n");
1208 acx_txeof(ifp->if_softc);
1213 ieee80211_watchdog(&sc->sc_ic);
1219 struct acx_softc *sc = arg;
1220 uint16_t intr_status;
1222 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0)
1225 intr_status = CSR_READ_2(sc, ACXREG_INTR_STATUS_CLR);
1226 if (intr_status == ACXRV_INTR_ALL) {
1227 /* not our interrupt */
1231 intr_status &= sc->chip_intr_enable;
1232 if (intr_status == 0) {
1233 /* not interrupts we care about */
1237 /* Acknowledge all interrupts */
1238 CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_ALL);
1240 if (intr_status & ACXRV_INTR_TX_FINI)
1243 if (intr_status & ACXRV_INTR_RX_FINI)
1248 acx_disable_intr(struct acx_softc *sc)
1250 CSR_WRITE_2(sc, ACXREG_INTR_MASK, sc->chip_intr_disable);
1251 CSR_WRITE_2(sc, ACXREG_EVENT_MASK, 0);
1255 acx_enable_intr(struct acx_softc *sc)
1257 /* Mask out interrupts that are not in the enable set */
1258 CSR_WRITE_2(sc, ACXREG_INTR_MASK, ~sc->chip_intr_enable);
1259 CSR_WRITE_2(sc, ACXREG_EVENT_MASK, ACXRV_EVENT_DISABLE);
1263 acx_txeof(struct acx_softc *sc)
1265 struct acx_buf_data *bd;
1266 struct acx_txbuf *buf;
1270 ifp = &sc->sc_ic.ic_if;
1271 ASSERT_SERIALIZED(ifp->if_serializer);
1273 bd = &sc->sc_buf_data;
1274 idx = bd->tx_used_start;
1275 for (buf = &bd->tx_buf[idx]; buf->tb_mbuf != NULL;
1276 buf = &bd->tx_buf[idx]) {
1277 uint8_t ctrl, error;
1280 ctrl = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_ctrl);
1281 if ((ctrl & (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE)) !=
1282 (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE))
1285 bus_dmamap_unload(bd->mbuf_dma_tag, buf->tb_mbuf_dmamap);
1286 frame_len = buf->tb_mbuf->m_pkthdr.len;
1287 m_freem(buf->tb_mbuf);
1288 buf->tb_mbuf = NULL;
1290 error = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_error);
1292 acx_txerr(sc, error);
1298 if (buf->tb_node != NULL) {
1299 sc->chip_tx_complete(sc, buf, frame_len, error);
1300 ieee80211_free_node(buf->tb_node);
1301 buf->tb_node = NULL;
1304 FW_TXDESC_SETFIELD_1(sc, buf, f_tx_ctrl, DESC_CTRL_HOSTOWN);
1306 bd->tx_used_count--;
1308 idx = (idx + 1) % ACX_TX_DESC_CNT;
1310 bd->tx_used_start = idx;
1312 sc->sc_tx_timer = bd->tx_used_count == 0 ? 0 : 5;
1314 if (bd->tx_used_count != ACX_TX_DESC_CNT) {
1315 ifp->if_flags &= ~IFF_OACTIVE;
1321 acx_txerr(struct acx_softc *sc, uint8_t err)
1323 struct ifnet *ifp = &sc->sc_ic.ic_if;
1324 struct acx_stats *stats = &sc->sc_stats;
1326 if (err == DESC_ERR_EXCESSIVE_RETRY) {
1328 * This a common error (see comment below),
1329 * so print it using DPRINTF()
1331 DPRINTF((ifp, "TX failed -- excessive retry\n"));
1333 if_printf(ifp, "TX failed -- ");
1337 * Although `err' looks like bitmask, it never
1338 * has multiple bits set.
1342 case DESC_ERR_OTHER_FRAG:
1343 /* XXX what's this */
1344 kprintf("error in other fragment\n");
1345 stats->err_oth_frag++;
1348 case DESC_ERR_ABORT:
1349 kprintf("aborted\n");
1352 case DESC_ERR_PARAM:
1353 kprintf("wrong paramters in descriptor\n");
1356 case DESC_ERR_NO_WEPKEY:
1357 kprintf("WEP key missing\n");
1358 stats->err_no_wepkey++;
1360 case DESC_ERR_MSDU_TIMEOUT:
1361 kprintf("MSDU life timeout\n");
1362 stats->err_msdu_timeout++;
1364 case DESC_ERR_EXCESSIVE_RETRY:
1367 * 1) Distance is too long
1368 * 2) Transmit failed (e.g. no MAC level ACK)
1369 * 3) Chip overheated (this should be rare)
1371 stats->err_ex_retry++;
1373 case DESC_ERR_BUF_OVERFLOW:
1374 kprintf("buffer overflow\n");
1375 stats->err_buf_oflow++;
1378 kprintf("DMA error\n");
1382 kprintf("unknown error %d\n", err);
1389 acx_rxeof(struct acx_softc *sc)
1391 struct ieee80211com *ic = &sc->sc_ic;
1392 struct acx_ring_data *rd = &sc->sc_ring_data;
1393 struct acx_buf_data *bd = &sc->sc_buf_data;
1394 struct ifnet *ifp = &ic->ic_if;
1397 ASSERT_SERIALIZED(ic->ic_if.if_serializer);
1399 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1400 BUS_DMASYNC_POSTREAD);
1403 * Locate first "ready" rx buffer,
1404 * start from last stopped position
1406 idx = bd->rx_scan_start;
1409 struct acx_rxbuf *buf;
1411 buf = &bd->rx_buf[idx];
1412 if ((buf->rb_desc->h_ctrl & htole16(DESC_CTRL_HOSTOWN)) &&
1413 (buf->rb_desc->h_status & htole32(DESC_STATUS_FULL))) {
1417 idx = (idx + 1) % ACX_RX_DESC_CNT;
1418 } while (idx != bd->rx_scan_start);
1424 * NOTE: don't mess up `idx' here, it will
1425 * be used in the following code
1429 struct acx_rxbuf_hdr *head;
1430 struct acx_rxbuf *buf;
1431 struct ieee80211_frame_min *wh;
1433 uint32_t desc_status;
1435 int len, error, rssi, is_priv;
1437 buf = &bd->rx_buf[idx];
1439 desc_ctrl = le16toh(buf->rb_desc->h_ctrl);
1440 desc_status = le32toh(buf->rb_desc->h_status);
1441 if (!(desc_ctrl & DESC_CTRL_HOSTOWN) ||
1442 !(desc_status & DESC_STATUS_FULL))
1445 bus_dmamap_sync(bd->mbuf_dma_tag, buf->rb_mbuf_dmamap,
1446 BUS_DMASYNC_POSTREAD);
1450 error = acx_newbuf(sc, buf, 0);
1456 head = mtod(m, struct acx_rxbuf_hdr *);
1457 len = le16toh(head->rbh_len) & ACX_RXBUF_LEN_MASK;
1458 rssi = acx_get_rssi(sc, head->rbh_level);
1460 m_adj(m, sizeof(struct acx_rxbuf_hdr) + sc->chip_rxbuf_exhdr);
1461 m->m_len = m->m_pkthdr.len = len;
1462 m->m_pkthdr.rcvif = &ic->ic_if;
1464 wh = mtod(m, struct ieee80211_frame_min *);
1465 is_priv = (wh->i_fc[1] & IEEE80211_FC1_WEP);
1467 if (sc->sc_drvbpf != NULL) {
1468 sc->sc_rx_th.wr_tsf = htole32(head->rbh_time);
1470 sc->sc_rx_th.wr_flags = 0;
1472 sc->sc_rx_th.wr_flags |=
1473 IEEE80211_RADIOTAP_F_WEP;
1475 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_SHPRE) {
1476 sc->sc_rx_th.wr_flags |=
1477 IEEE80211_RADIOTAP_F_SHORTPRE;
1480 if (sc->chip_phymode == IEEE80211_MODE_11G) {
1481 sc->sc_rx_th.wr_rate =
1482 ieee80211_plcp2rate(head->rbh_plcp,
1483 head->rbh_bbp_stat & ACX_RXBUF_STAT_OFDM);
1485 sc->sc_rx_th.wr_rate =
1486 ieee80211_plcp2rate(head->rbh_plcp, 0);
1489 sc->sc_rx_th.wr_antsignal = rssi;
1491 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_ANT1)
1492 sc->sc_rx_th.wr_antenna = 1;
1494 sc->sc_rx_th.wr_antenna = 0;
1496 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th,
1500 if (len >= sizeof(struct ieee80211_frame_min) &&
1502 struct ieee80211_node *ni;
1504 if (is_priv && sc->chip_hw_crypt) {
1505 /* Short circuit software WEP */
1506 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1508 /* Do chip specific RX buffer processing */
1509 if (sc->chip_proc_wep_rxbuf != NULL) {
1510 sc->chip_proc_wep_rxbuf(sc, m, &len);
1512 struct ieee80211_frame_min *);
1515 m->m_len = m->m_pkthdr.len = len;
1517 ni = ieee80211_find_rxnode(ic, wh);
1518 ieee80211_input(ic, m, ni, rssi,
1519 le32toh(head->rbh_time));
1520 ieee80211_free_node(ni);
1524 if (len < sizeof(struct ieee80211_frame_min)) {
1525 if (ic->ic_rawbpf != NULL &&
1526 len >= sizeof(struct ieee80211_frame_ack))
1527 bpf_mtap(ic->ic_rawbpf, m);
1529 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1530 ic->ic_stats.is_rx_tooshort++;
1535 buf->rb_desc->h_ctrl = htole16(desc_ctrl & ~DESC_CTRL_HOSTOWN);
1536 buf->rb_desc->h_status = 0;
1537 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1538 BUS_DMASYNC_PREWRITE);
1540 idx = (idx + 1) % ACX_RX_DESC_CNT;
1541 } while (idx != bd->rx_scan_start);
1544 * Record the position so that next
1545 * time we can start from it
1547 bd->rx_scan_start = idx;
1551 acx_reset(struct acx_softc *sc)
1556 CSR_SETB_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_HALT);
1558 /* Software reset */
1559 reg = CSR_READ_2(sc, ACXREG_SOFT_RESET);
1560 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg | ACXRV_SOFT_RESET);
1562 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg);
1564 /* Initialize EEPROM */
1565 CSR_SETB_2(sc, ACXREG_EEPROM_INIT, ACXRV_EEPROM_INIT);
1568 /* Test whether ECPU is stopped */
1569 reg = CSR_READ_2(sc, ACXREG_ECPU_CTRL);
1570 if (!(reg & ACXRV_ECPU_HALT)) {
1571 if_printf(&sc->sc_ic.ic_if, "can't halt ECPU\n");
1578 acx_read_eeprom(struct acx_softc *sc, uint32_t offset, uint8_t *val)
1582 CSR_WRITE_4(sc, ACXREG_EEPROM_CONF, 0);
1583 CSR_WRITE_4(sc, ACXREG_EEPROM_ADDR, offset);
1584 CSR_WRITE_4(sc, ACXREG_EEPROM_CTRL, ACXRV_EEPROM_READ);
1586 #define EE_READ_RETRY_MAX 100
1587 for (i = 0; i < EE_READ_RETRY_MAX; ++i) {
1588 if (CSR_READ_2(sc, ACXREG_EEPROM_CTRL) == 0)
1592 if (i == EE_READ_RETRY_MAX) {
1593 if_printf(&sc->sc_ic.ic_if, "can't read EEPROM offset %x "
1594 "(timeout)\n", offset);
1597 #undef EE_READ_RETRY_MAX
1599 *val = CSR_READ_1(sc, ACXREG_EEPROM_DATA);
1604 acx_read_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t *val)
1608 CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1609 CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_READ);
1611 #define PHY_READ_RETRY_MAX 100
1612 for (i = 0; i < PHY_READ_RETRY_MAX; ++i) {
1613 if (CSR_READ_4(sc, ACXREG_PHY_CTRL) == 0)
1617 if (i == PHY_READ_RETRY_MAX) {
1618 if_printf(&sc->sc_ic.ic_if, "can't read phy reg %x (timeout)\n",
1622 #undef PHY_READ_RETRY_MAX
1624 *val = CSR_READ_1(sc, ACXREG_PHY_DATA);
1629 acx_write_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t val)
1631 CSR_WRITE_4(sc, ACXREG_PHY_DATA, val);
1632 CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1633 CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_WRITE);
1637 acx_copyin_firmware(struct acx_softc *sc, struct ifreq *req)
1639 struct acx_firmware ufw, *kfw;
1640 uint8_t *base_fw, *radio_fw;
1643 kfw = &sc->sc_firmware;
1647 error = copyin(req->ifr_data, &ufw, sizeof(ufw));
1652 * For combined base firmware, there is no radio firmware.
1653 * But base firmware must exist.
1655 if (ufw.base_fw_len <= 0 || ufw.radio_fw_len < 0)
1658 base_fw = kmalloc(ufw.base_fw_len, M_DEVBUF, M_INTWAIT);
1659 error = copyin(ufw.base_fw, base_fw, ufw.base_fw_len);
1663 if (ufw.radio_fw_len > 0) {
1664 radio_fw = kmalloc(ufw.radio_fw_len, M_DEVBUF, M_INTWAIT);
1665 error = copyin(ufw.radio_fw, radio_fw, ufw.radio_fw_len);
1670 kfw->base_fw_len = ufw.base_fw_len;
1671 if (kfw->base_fw != NULL)
1672 kfree(kfw->base_fw, M_DEVBUF);
1673 kfw->base_fw = base_fw;
1675 kfw->radio_fw_len = ufw.radio_fw_len;
1676 if (kfw->radio_fw != NULL)
1677 kfree(kfw->radio_fw, M_DEVBUF);
1678 kfw->radio_fw = radio_fw;
1682 if (base_fw != NULL)
1683 kfree(base_fw, M_DEVBUF);
1684 if (radio_fw != NULL)
1685 kfree(radio_fw, M_DEVBUF);
1690 acx_free_firmware(struct acx_softc *sc)
1692 struct acx_firmware *fw = &sc->sc_firmware;
1694 if (fw->base_fw != NULL) {
1695 kfree(fw->base_fw, M_DEVBUF);
1697 fw->base_fw_len = 0;
1699 if (fw->radio_fw != NULL) {
1700 kfree(fw->radio_fw, M_DEVBUF);
1701 fw->radio_fw = NULL;
1702 fw->radio_fw_len = 0;
1707 acx_load_base_firmware(struct acx_softc *sc, const uint8_t *base_fw,
1708 uint32_t base_fw_len)
1712 /* Load base firmware */
1713 error = acx_load_firmware(sc, 0, base_fw, base_fw_len);
1715 if_printf(&sc->sc_ic.ic_if, "can't load base firmware\n");
1718 DPRINTF((&sc->sc_ic.ic_if, "base firmware loaded\n"));
1721 CSR_WRITE_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_START);
1723 /* Wait for ECPU to be up */
1724 for (i = 0; i < 500; ++i) {
1727 reg = CSR_READ_2(sc, ACXREG_INTR_STATUS);
1728 if (reg & ACXRV_INTR_FCS_THRESH) {
1729 CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_FCS_THRESH);
1735 if_printf(&sc->sc_ic.ic_if, "can't initialize ECPU (timeout)\n");
1740 acx_load_radio_firmware(struct acx_softc *sc, const uint8_t *radio_fw,
1741 uint32_t radio_fw_len)
1743 struct acx_conf_mmap mem_map;
1744 uint32_t radio_fw_ofs;
1748 * Get the position, where base firmware is loaded, so that
1749 * radio firmware can be loaded after it.
1751 if (acx_get_mmap_conf(sc, &mem_map) != 0)
1753 radio_fw_ofs = le32toh(mem_map.code_end);
1755 /* Put ECPU into sleeping state, before loading radio firmware */
1756 if (acx_sleep(sc) != 0)
1759 /* Load radio firmware */
1760 error = acx_load_firmware(sc, radio_fw_ofs, radio_fw, radio_fw_len);
1762 if_printf(&sc->sc_ic.ic_if, "can't load radio firmware\n");
1765 DPRINTF((&sc->sc_ic.ic_if, "radio firmware loaded\n"));
1767 /* Wake up sleeping ECPU, after radio firmware is loaded */
1768 if (acx_wakeup(sc) != 0)
1771 /* Initialize radio */
1772 if (acx_init_radio(sc, radio_fw_ofs, radio_fw_len) != 0)
1775 /* Verify radio firmware's loading position */
1776 if (acx_get_mmap_conf(sc, &mem_map) != 0)
1778 if (le32toh(mem_map.code_end) != radio_fw_ofs + radio_fw_len) {
1779 if_printf(&sc->sc_ic.ic_if, "loaded radio firmware position "
1784 DPRINTF((&sc->sc_ic.ic_if, "radio firmware initialized\n"));
1789 acx_load_firmware(struct acx_softc *sc, uint32_t offset, const uint8_t *data,
1795 fw = (const uint32_t *)data;
1796 fw_len = data_len / sizeof(uint32_t);
1799 * LOADFW_AUTO_INC only works with some older firmware:
1800 * 1) acx100's firmware
1801 * 2) acx111's firmware whose rev is 0x00010011
1805 CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1806 #ifndef LOADFW_AUTO_INC
1807 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1809 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1810 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1813 for (i = 0; i < fw_len; ++i) {
1814 #ifndef LOADFW_AUTO_INC
1815 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1817 CSR_WRITE_4(sc, ACXREG_FWMEM_DATA, be32toh(fw[i]));
1820 /* Verify firmware */
1821 CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1822 #ifndef LOADFW_AUTO_INC
1823 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1825 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1826 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1829 for (i = 0; i < fw_len; ++i) {
1832 #ifndef LOADFW_AUTO_INC
1833 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1835 val = CSR_READ_4(sc, ACXREG_FWMEM_DATA);
1836 if (be32toh(fw[i]) != val) {
1837 if_printf(&sc->sc_ic.ic_if, "fireware mismatch "
1838 "fw %08x loaded %08x\n", fw[i], val);
1846 acx_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1848 struct ifnet *ifp = &ic->ic_if;
1849 struct acx_softc *sc = ifp->if_softc;
1850 struct ieee80211_node *ni = NULL;
1851 struct ieee80211_channel *c = NULL;
1852 int error = 1, mode = 0;
1854 ASSERT_SERIALIZED(ifp->if_serializer);
1856 ieee80211_ratectl_newstate(ic, nstate);
1857 callout_stop(&sc->sc_scan_timer);
1860 case IEEE80211_S_SCAN:
1861 acx_set_chan(sc, ic->ic_curchan);
1862 callout_reset(&sc->sc_scan_timer,
1863 (hz * sc->sc_scan_dwell) / 1000,
1866 case IEEE80211_S_AUTH:
1867 if (ic->ic_opmode == IEEE80211_M_STA) {
1870 mode = ACX_MODE_STA;
1873 case IEEE80211_S_RUN:
1874 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1875 ic->ic_opmode == IEEE80211_M_HOSTAP) {
1878 if (ic->ic_opmode == IEEE80211_M_IBSS)
1879 mode = ACX_MODE_ADHOC;
1883 if (acx_set_beacon_tmplt(sc, ni) != 0) {
1884 if_printf(ifp, "set bescon template failed\n");
1887 if (acx_set_probe_resp_tmplt(sc, ni) != 0) {
1888 if_printf(ifp, "set probe response template"
1892 } else if (ic->ic_opmode == IEEE80211_M_MONITOR) {
1895 mode = ACX_MODE_STA;
1903 KKASSERT(c != NULL);
1905 if (acx_set_chan(sc, c) != 0)
1908 if (acx_join_bss(sc, mode, ni, c) != 0) {
1909 if_printf(ifp, "join BSS failed\n");
1918 nstate = IEEE80211_S_INIT;
1921 return sc->sc_newstate(ic, nstate, arg);
1925 acx_init_tmplt_ordered(struct acx_softc *sc)
1927 #define INIT_TMPLT(name) \
1929 if (acx_init_##name##_tmplt(sc) != 0) \
1935 * Order of templates initialization:
1941 * Above order is critical to get a correct memory map.
1943 INIT_TMPLT(probe_req);
1944 INIT_TMPLT(null_data);
1947 INIT_TMPLT(probe_resp);
1954 acx_ring_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1956 *((uint32_t *)arg) = seg->ds_addr;
1960 acx_dma_alloc(struct acx_softc *sc)
1962 struct acx_ring_data *rd = &sc->sc_ring_data;
1963 struct acx_buf_data *bd = &sc->sc_buf_data;
1966 /* Allocate DMA stuffs for RX descriptors */
1967 error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
1968 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1970 ACX_RX_RING_SIZE, 1, ACX_RX_RING_SIZE,
1971 0, &rd->rx_ring_dma_tag);
1973 if_printf(&sc->sc_ic.ic_if, "can't create rx ring dma tag\n");
1977 error = bus_dmamem_alloc(rd->rx_ring_dma_tag, (void **)&rd->rx_ring,
1978 BUS_DMA_WAITOK | BUS_DMA_ZERO,
1979 &rd->rx_ring_dmamap);
1981 if_printf(&sc->sc_ic.ic_if,
1982 "can't allocate rx ring dma memory\n");
1983 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
1984 rd->rx_ring_dma_tag = NULL;
1988 error = bus_dmamap_load(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1989 rd->rx_ring, ACX_RX_RING_SIZE,
1990 acx_ring_dma_addr, &rd->rx_ring_paddr,
1993 if_printf(&sc->sc_ic.ic_if, "can't get rx ring dma address\n");
1994 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
1995 rd->rx_ring_dmamap);
1996 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
1997 rd->rx_ring_dma_tag = NULL;
2001 /* Allocate DMA stuffs for TX descriptors */
2002 error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
2003 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2005 ACX_TX_RING_SIZE, 1, ACX_TX_RING_SIZE,
2006 0, &rd->tx_ring_dma_tag);
2008 if_printf(&sc->sc_ic.ic_if, "can't create tx ring dma tag\n");
2012 error = bus_dmamem_alloc(rd->tx_ring_dma_tag, (void **)&rd->tx_ring,
2013 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2014 &rd->tx_ring_dmamap);
2016 if_printf(&sc->sc_ic.ic_if,
2017 "can't allocate tx ring dma memory\n");
2018 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2019 rd->tx_ring_dma_tag = NULL;
2023 error = bus_dmamap_load(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2024 rd->tx_ring, ACX_TX_RING_SIZE,
2025 acx_ring_dma_addr, &rd->tx_ring_paddr,
2028 if_printf(&sc->sc_ic.ic_if, "can't get tx ring dma address\n");
2029 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2030 rd->tx_ring_dmamap);
2031 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2032 rd->tx_ring_dma_tag = NULL;
2036 /* Create DMA tag for RX/TX mbuf map */
2037 error = bus_dma_tag_create(NULL, 1, 0,
2038 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2040 MCLBYTES, 1, MCLBYTES,
2041 0, &bd->mbuf_dma_tag);
2043 if_printf(&sc->sc_ic.ic_if, "can't create mbuf dma tag\n");
2047 /* Create a spare RX DMA map */
2048 error = bus_dmamap_create(bd->mbuf_dma_tag, 0, &bd->mbuf_tmp_dmamap);
2050 if_printf(&sc->sc_ic.ic_if, "can't create tmp mbuf dma map\n");
2051 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2052 bd->mbuf_dma_tag = NULL;
2056 /* Create DMA map for RX mbufs */
2057 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2058 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2059 &bd->rx_buf[i].rb_mbuf_dmamap);
2061 if_printf(&sc->sc_ic.ic_if, "can't create rx mbuf "
2062 "dma map (%d)\n", i);
2065 bd->rx_buf[i].rb_desc = &rd->rx_ring[i];
2068 /* Create DMA map for TX mbufs */
2069 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2070 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2071 &bd->tx_buf[i].tb_mbuf_dmamap);
2073 if_printf(&sc->sc_ic.ic_if, "can't create tx mbuf "
2074 "dma map (%d)\n", i);
2077 bd->tx_buf[i].tb_desc1 = &rd->tx_ring[i * 2];
2078 bd->tx_buf[i].tb_desc2 = &rd->tx_ring[(i * 2) + 1];
2085 acx_dma_free(struct acx_softc *sc)
2087 struct acx_ring_data *rd = &sc->sc_ring_data;
2088 struct acx_buf_data *bd = &sc->sc_buf_data;
2091 if (rd->rx_ring_dma_tag != NULL) {
2092 bus_dmamap_unload(rd->rx_ring_dma_tag, rd->rx_ring_dmamap);
2093 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
2094 rd->rx_ring_dmamap);
2095 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2098 if (rd->tx_ring_dma_tag != NULL) {
2099 bus_dmamap_unload(rd->tx_ring_dma_tag, rd->tx_ring_dmamap);
2100 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2101 rd->tx_ring_dmamap);
2102 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2105 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2106 if (bd->rx_buf[i].rb_desc != NULL) {
2107 if (bd->rx_buf[i].rb_mbuf != NULL) {
2108 bus_dmamap_unload(bd->mbuf_dma_tag,
2109 bd->rx_buf[i].rb_mbuf_dmamap);
2110 m_freem(bd->rx_buf[i].rb_mbuf);
2112 bus_dmamap_destroy(bd->mbuf_dma_tag,
2113 bd->rx_buf[i].rb_mbuf_dmamap);
2117 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2118 if (bd->tx_buf[i].tb_desc1 != NULL) {
2119 if (bd->tx_buf[i].tb_mbuf != NULL) {
2120 bus_dmamap_unload(bd->mbuf_dma_tag,
2121 bd->tx_buf[i].tb_mbuf_dmamap);
2122 m_freem(bd->tx_buf[i].tb_mbuf);
2124 bus_dmamap_destroy(bd->mbuf_dma_tag,
2125 bd->tx_buf[i].tb_mbuf_dmamap);
2129 if (bd->mbuf_dma_tag != NULL) {
2130 bus_dmamap_destroy(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap);
2131 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2136 acx_init_tx_ring(struct acx_softc *sc)
2138 struct acx_ring_data *rd;
2139 struct acx_buf_data *bd;
2143 rd = &sc->sc_ring_data;
2144 paddr = rd->tx_ring_paddr;
2145 for (i = 0; i < (ACX_TX_DESC_CNT * 2) - 1; ++i) {
2146 paddr += sizeof(struct acx_host_desc);
2148 rd->tx_ring[i].h_ctrl = htole16(DESC_CTRL_HOSTOWN);
2150 if (i == (ACX_TX_DESC_CNT * 2) - 1)
2151 rd->tx_ring[i].h_next_desc = htole32(rd->tx_ring_paddr);
2153 rd->tx_ring[i].h_next_desc = htole32(paddr);
2156 bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2157 BUS_DMASYNC_PREWRITE);
2159 bd = &sc->sc_buf_data;
2160 bd->tx_free_start = 0;
2161 bd->tx_used_start = 0;
2162 bd->tx_used_count = 0;
2168 acx_init_rx_ring(struct acx_softc *sc)
2170 struct acx_ring_data *rd;
2171 struct acx_buf_data *bd;
2175 bd = &sc->sc_buf_data;
2176 rd = &sc->sc_ring_data;
2177 paddr = rd->rx_ring_paddr;
2179 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2182 paddr += sizeof(struct acx_host_desc);
2184 error = acx_newbuf(sc, &bd->rx_buf[i], 1);
2188 if (i == ACX_RX_DESC_CNT - 1)
2189 rd->rx_ring[i].h_next_desc = htole32(rd->rx_ring_paddr);
2191 rd->rx_ring[i].h_next_desc = htole32(paddr);
2194 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2195 BUS_DMASYNC_PREWRITE);
2197 bd->rx_scan_start = 0;
2202 acx_buf_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2203 bus_size_t mapsz, int error)
2209 KASSERT(nseg == 1, ("too many RX dma segments\n"));
2210 *((uint32_t *)arg) = seg->ds_addr;
2214 acx_newbuf(struct acx_softc *sc, struct acx_rxbuf *rb, int wait)
2216 struct acx_buf_data *bd;
2222 bd = &sc->sc_buf_data;
2224 m = m_getcl(wait ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2228 m->m_len = m->m_pkthdr.len = MCLBYTES;
2230 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap,
2231 m, acx_buf_dma_addr, &paddr,
2232 wait ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
2235 if_printf(&sc->sc_ic.ic_if, "can't map rx mbuf %d\n", error);
2239 /* Unload originally mapped mbuf */
2240 bus_dmamap_unload(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap);
2242 /* Swap this dmamap with tmp dmamap */
2243 map = rb->rb_mbuf_dmamap;
2244 rb->rb_mbuf_dmamap = bd->mbuf_tmp_dmamap;
2245 bd->mbuf_tmp_dmamap = map;
2248 rb->rb_desc->h_data_paddr = htole32(paddr);
2249 rb->rb_desc->h_data_len = htole16(m->m_len);
2251 bus_dmamap_sync(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap,
2252 BUS_DMASYNC_PREREAD);
2257 acx_encap(struct acx_softc *sc, struct acx_txbuf *txbuf, struct mbuf *m,
2258 struct ieee80211_node *ni)
2260 struct acx_buf_data *bd = &sc->sc_buf_data;
2261 struct acx_ring_data *rd = &sc->sc_ring_data;
2266 KASSERT(txbuf->tb_mbuf == NULL, ("free TX buf has mbuf installed\n"));
2269 if (m->m_pkthdr.len > MCLBYTES) {
2270 if_printf(&sc->sc_ic.ic_if, "mbuf too big\n");
2273 } else if (m->m_pkthdr.len < ACX_FRAME_HDRLEN) {
2274 if_printf(&sc->sc_ic.ic_if, "mbuf too small\n");
2279 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2280 m, acx_buf_dma_addr, &paddr,
2282 if (error && error != EFBIG) {
2283 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf1 %d\n", error);
2287 if (error) { /* error == EFBIG */
2290 m_new = m_defrag(m, MB_DONTWAIT);
2291 if (m_new == NULL) {
2292 if_printf(&sc->sc_ic.ic_if, "can't defrag tx mbuf\n");
2299 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag,
2300 txbuf->tb_mbuf_dmamap, m,
2301 acx_buf_dma_addr, &paddr,
2304 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf2 %d\n",
2312 bus_dmamap_sync(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2313 BUS_DMASYNC_PREWRITE);
2316 txbuf->tb_node = ni;
2319 * TX buffers are accessed in following way:
2320 * acx_fw_txdesc -> acx_host_desc -> buffer
2322 * It is quite strange that acx also querys acx_host_desc next to
2323 * the one we have assigned to acx_fw_txdesc even if first one's
2324 * acx_host_desc.h_data_len == acx_fw_txdesc.f_tx_len
2326 * So we allocate two acx_host_desc for one acx_fw_txdesc and
2327 * assign the first acx_host_desc to acx_fw_txdesc
2330 * host_desc1.h_data_len = buffer_len
2331 * host_desc2.h_data_len = buffer_len - mac_header_len
2334 * host_desc1.h_data_len = mac_header_len
2335 * host_desc2.h_data_len = buffer_len - mac_header_len
2338 txbuf->tb_desc1->h_data_paddr = htole32(paddr);
2339 txbuf->tb_desc2->h_data_paddr = htole32(paddr + ACX_FRAME_HDRLEN);
2341 txbuf->tb_desc1->h_data_len =
2342 htole16(sc->chip_txdesc1_len ? sc->chip_txdesc1_len
2344 txbuf->tb_desc2->h_data_len =
2345 htole16(m->m_pkthdr.len - ACX_FRAME_HDRLEN);
2349 * We can't simply assign f_tx_ctrl, we will first read it back
2350 * and change it bit by bit
2352 ctrl = FW_TXDESC_GETFIELD_1(sc, txbuf, f_tx_ctrl);
2353 ctrl |= sc->chip_fw_txdesc_ctrl; /* extra chip specific flags */
2354 ctrl &= ~(DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE);
2356 FW_TXDESC_SETFIELD_4(sc, txbuf, f_tx_len, m->m_pkthdr.len);
2357 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_error, 0);
2358 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_data_nretry, 0);
2359 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_nretry, 0);
2360 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_ok, 0);
2361 rate = sc->chip_set_fw_txdesc_rate(sc, txbuf, ni, m->m_pkthdr.len);
2363 if (sc->sc_drvbpf != NULL) {
2364 struct ieee80211_frame_min *wh;
2366 wh = mtod(m, struct ieee80211_frame_min *);
2367 sc->sc_tx_th.wt_flags = 0;
2368 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2369 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2370 sc->sc_tx_th.wt_rate = rate;
2372 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_tx_th, sc->sc_tx_th_len);
2375 txbuf->tb_desc1->h_ctrl = 0;
2376 txbuf->tb_desc2->h_ctrl = 0;
2377 bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2378 BUS_DMASYNC_PREWRITE);
2380 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl2, 0);
2381 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl, ctrl);
2383 /* Tell chip to inform us about TX completion */
2384 CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_TX_FINI);
2392 acx_set_null_tmplt(struct acx_softc *sc)
2394 struct acx_tmplt_null_data n;
2395 struct ieee80211_frame *f;
2397 bzero(&n, sizeof(n));
2400 f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
2401 IEEE80211_FC0_SUBTYPE_NODATA;
2402 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2403 IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2404 IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2406 return _acx_set_null_data_tmplt(sc, &n, sizeof(n));
2410 acx_set_probe_req_tmplt(struct acx_softc *sc, const char *ssid, int ssid_len)
2412 struct acx_tmplt_probe_req req;
2413 struct ieee80211_frame *f;
2417 bzero(&req, sizeof(req));
2419 f = &req.data.u_data.f;
2420 f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2421 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2422 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2423 IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2424 IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2426 v = req.data.u_data.var;
2427 v = ieee80211_add_ssid(v, ssid, ssid_len);
2428 v = ieee80211_add_rates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2429 v = ieee80211_add_xrates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2430 vlen = v - req.data.u_data.var;
2432 return _acx_set_probe_req_tmplt(sc, &req,
2433 ACX_TMPLT_PROBE_REQ_SIZ(vlen));
2437 acx_set_probe_resp_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2439 struct ieee80211com *ic = &sc->sc_ic;
2440 struct acx_tmplt_probe_resp resp;
2441 struct ieee80211_frame *f;
2445 m = ieee80211_probe_resp_alloc(ic, ni);
2448 DPRINTF((&ic->ic_if, "%s alloc probe resp size %d\n", __func__,
2451 f = mtod(m, struct ieee80211_frame *);
2452 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2454 bzero(&resp, sizeof(resp));
2455 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)&resp.data);
2456 len = m->m_pkthdr.len + sizeof(resp.size);
2459 return _acx_set_probe_resp_tmplt(sc, &resp, len);
2463 acx_set_beacon_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2465 struct ieee80211com *ic = &sc->sc_ic;
2466 struct acx_tmplt_beacon beacon;
2467 struct acx_tmplt_tim tim;
2468 struct ieee80211_beacon_offsets bo;
2470 int beacon_tmplt_len = 0, tim_tmplt_len = 0;
2472 bzero(&bo, sizeof(bo));
2473 m = ieee80211_beacon_alloc(ic, ni, &bo);
2476 DPRINTF((&ic->ic_if, "%s alloc beacon size %d\n", __func__,
2479 if (bo.bo_tim_len == 0) {
2480 beacon_tmplt_len = m->m_pkthdr.len;
2482 beacon_tmplt_len = bo.bo_tim - mtod(m, uint8_t *);
2483 tim_tmplt_len = m->m_pkthdr.len - beacon_tmplt_len;
2486 bzero(&beacon, sizeof(beacon));
2487 bzero(&tim, sizeof(tim));
2489 m_copydata(m, 0, beacon_tmplt_len, (caddr_t)&beacon.data);
2490 if (tim_tmplt_len != 0) {
2491 m_copydata(m, beacon_tmplt_len, tim_tmplt_len,
2492 (caddr_t)&tim.data);
2496 beacon_tmplt_len += sizeof(beacon.size);
2497 if (_acx_set_beacon_tmplt(sc, &beacon, beacon_tmplt_len) != 0)
2500 if (tim_tmplt_len != 0) {
2501 tim_tmplt_len += sizeof(tim.size);
2502 if (_acx_set_tim_tmplt(sc, &tim, tim_tmplt_len) != 0)
2509 acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS)
2511 struct acx_softc *sc = arg1;
2512 struct ifnet *ifp = &sc->sc_ic.ic_if;
2515 lwkt_serialize_enter(ifp->if_serializer);
2517 v = sc->sc_msdu_lifetime;
2518 error = sysctl_handle_int(oidp, &v, 0, req);
2519 if (error || req->newptr == NULL)
2526 if (sc->sc_flags & ACX_FLAG_FW_LOADED) {
2527 struct acx_conf_msdu_lifetime msdu_lifetime;
2529 msdu_lifetime.lifetime = htole32(v);
2530 if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
2531 if_printf(&sc->sc_ic.ic_if,
2532 "can't set MSDU lifetime\n");
2537 sc->sc_msdu_lifetime = v;
2539 lwkt_serialize_exit(ifp->if_serializer);
2544 acx_media_change(struct ifnet *ifp)
2548 error = ieee80211_media_change(ifp);
2549 if (error != ENETRESET)
2552 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
2553 acx_init(ifp->if_softc);
2558 acx_rx_config(struct acx_softc *sc, int promisc)
2560 struct acx_conf_rxopt rx_opt;
2561 struct ieee80211com *ic = &sc->sc_ic;
2564 * What we want to receive and how to receive
2567 /* Common for all operational modes */
2568 rx_opt.opt1 = RXOPT1_INCL_RXBUF_HDR;
2569 rx_opt.opt2 = RXOPT2_RECV_ASSOC_REQ |
2571 RXOPT2_RECV_BEACON |
2576 RXOPT2_RECV_PROBE_REQ |
2577 RXOPT2_RECV_PROBE_RESP |
2580 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2581 rx_opt.opt1 |= RXOPT1_PROMISC;
2582 rx_opt.opt2 |= RXOPT2_RECV_BROKEN | RXOPT2_RECV_ACK;
2584 rx_opt.opt1 |= promisc ? RXOPT1_PROMISC : RXOPT1_FILT_FDEST;
2587 if (acx_set_rxopt_conf(sc, &rx_opt) != 0) {
2588 if_printf(&sc->sc_ic.ic_if, "can't config RX\n");
2595 acx_set_chan(struct acx_softc *sc, struct ieee80211_channel *c)
2597 struct ieee80211com *ic = &sc->sc_ic;
2601 chan = ieee80211_chan2ieee(ic, c);
2602 DPRINTF((&ic->ic_if, "to chan %u\n", chan));
2603 if (acx_enable_txchan(sc, chan) != 0) {
2604 if_printf(&ic->ic_if, "enable TX on channel %d failed\n", chan);
2607 if (acx_enable_rxchan(sc, chan) != 0) {
2608 if_printf(&ic->ic_if, "enable RX on channel %d failed\n", chan);
2612 if (IEEE80211_IS_CHAN_G(c))
2613 flags = IEEE80211_CHAN_G;
2615 flags = IEEE80211_CHAN_B;
2617 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2618 htole16(c->ic_freq);
2619 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =