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.27 2008/05/14 11:59:18 sephe Exp $
38 * Copyright (c) 2003-2004 wlan.kewl.org Project
39 * All rights reserved.
41 * $Id: LICENSE,v 1.1.1.1 2004/07/01 12:20:39 darron Exp $
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
57 * This product includes software developed by the wlan.kewl.org Project.
59 * 4. Neither the name of the wlan.kewl.org Project nor the names of its
60 * contributors may be used to endorse or promote products derived from
61 * this software without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
64 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
65 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
66 * THE wlan.kewl.org Project BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
67 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
69 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
70 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
71 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
72 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 #include <sys/param.h>
76 #include <sys/endian.h>
77 #include <sys/kernel.h>
79 #include <sys/firmware.h>
80 #include <sys/interrupt.h>
81 #include <sys/malloc.h>
84 #include <sys/serialize.h>
85 #include <sys/socket.h>
86 #include <sys/sockio.h>
87 #include <sys/sysctl.h>
89 #include <net/ethernet.h>
92 #include <net/if_arp.h>
93 #include <net/if_dl.h>
94 #include <net/if_media.h>
95 #include <net/ifq_var.h>
97 #include <netproto/802_11/ieee80211_var.h>
98 #include <netproto/802_11/ieee80211_radiotap.h>
99 #include <netproto/802_11/wlan_ratectl/amrr/ieee80211_amrr_param.h>
100 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
102 #include <bus/pci/pcireg.h>
103 #include <bus/pci/pcivar.h>
104 #include <bus/pci/pcidevs.h>
108 #include <dev/netif/acx/if_acxreg.h>
109 #include <dev/netif/acx/if_acxvar.h>
110 #include <dev/netif/acx/acxcmd.h>
112 static int acx_probe(device_t);
113 static int acx_attach(device_t);
114 static int acx_detach(device_t);
115 static int acx_shutdown(device_t);
117 static void acx_init(void *);
118 static void acx_start(struct ifnet *);
119 static int acx_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
120 static void acx_watchdog(struct ifnet *);
122 static void acx_intr(void *);
123 static void acx_txeof(struct acx_softc *);
124 static void acx_txerr(struct acx_softc *, uint8_t);
125 static void acx_rxeof(struct acx_softc *);
126 static void acx_disable_intr(struct acx_softc *);
127 static void acx_enable_intr(struct acx_softc *);
129 static int acx_reset(struct acx_softc *);
130 static int acx_stop(struct acx_softc *);
131 static void acx_init_info_reg(struct acx_softc *);
132 static int acx_config(struct acx_softc *);
133 static int acx_read_config(struct acx_softc *, struct acx_config *);
134 static int acx_write_config(struct acx_softc *, struct acx_config *);
135 static int acx_rx_config(struct acx_softc *, int);
136 static int acx_set_crypt_keys(struct acx_softc *);
138 static int acx_dma_alloc(struct acx_softc *);
139 static void acx_dma_free(struct acx_softc *);
140 static int acx_init_tx_ring(struct acx_softc *);
141 static int acx_init_rx_ring(struct acx_softc *);
142 static int acx_newbuf(struct acx_softc *, struct acx_rxbuf *, int);
143 static int acx_encap(struct acx_softc *, struct acx_txbuf *,
144 struct mbuf *, struct ieee80211_node *);
146 static int acx_set_null_tmplt(struct acx_softc *);
147 static int acx_set_probe_req_tmplt(struct acx_softc *, const char *, int);
148 static int acx_set_probe_resp_tmplt(struct acx_softc *,
149 struct ieee80211_node *);
150 static int acx_set_beacon_tmplt(struct acx_softc *,
151 struct ieee80211_node *);
153 static int acx_read_eeprom(struct acx_softc *, uint32_t, uint8_t *);
154 static int acx_read_phyreg(struct acx_softc *, uint32_t, uint8_t *);
156 static int acx_alloc_firmware(struct acx_softc *);
157 static void acx_free_firmware(struct acx_softc *);
158 static int acx_setup_firmware(struct acx_softc *, struct fw_image *,
159 const uint8_t **, int *);
160 static int acx_load_firmware(struct acx_softc *, uint32_t,
161 const uint8_t *, int);
162 static int acx_load_radio_firmware(struct acx_softc *, const uint8_t *,
164 static int acx_load_base_firmware(struct acx_softc *, const uint8_t *,
167 static void acx_next_scan(void *);
168 static int acx_set_chan(struct acx_softc *, struct ieee80211_channel *);
170 static int acx_media_change(struct ifnet *);
171 static int acx_newstate(struct ieee80211com *, enum ieee80211_state, int);
173 static int acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS);
174 static int acx_sysctl_free_firmware(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",
401 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
402 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
403 "long_retry_limit", CTLFLAG_RW,
404 &sc->sc_long_retry_limit, 0, "Long retry limit");
405 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
406 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
407 "scan_dwell", CTLFLAG_RW,
408 &sc->sc_scan_dwell, 0, "Scan channel dwell time (ms)");
411 * Nodes for firmware operation
413 SYSCTL_ADD_INT(&sc->sc_sysctl_ctx,
414 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
415 "combined_radio_fw", CTLFLAG_RW,
416 &sc->sc_firmware.combined_radio_fw, 0,
417 "Radio and base firmwares are combined");
418 SYSCTL_ADD_PROC(&sc->sc_sysctl_ctx,
419 SYSCTL_CHILDREN(sc->sc_sysctl_tree),
421 CTLTYPE_INT | CTLFLAG_RW,
422 sc, 0, acx_sysctl_free_firmware, "I",
426 * Nodes for statistics
428 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
429 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
430 "frag_error", CTLFLAG_RW, &sc->sc_stats.err_oth_frag,
431 0, "Fragment errors");
432 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
433 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
434 "tx_abort", CTLFLAG_RW, &sc->sc_stats.err_abort,
436 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
437 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
438 "tx_invalid", CTLFLAG_RW, &sc->sc_stats.err_param,
439 0, "Invalid TX param in TX descriptor");
440 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
441 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
442 "no_wepkey", CTLFLAG_RW, &sc->sc_stats.err_no_wepkey,
443 0, "No WEP key exists");
444 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
445 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
446 "msdu_timeout", CTLFLAG_RW,
447 &sc->sc_stats.err_msdu_timeout,
449 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
450 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
451 "ex_txretry", CTLFLAG_RW, &sc->sc_stats.err_ex_retry,
452 0, "Excessive TX retries");
453 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
454 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
455 "buf_oflow", CTLFLAG_RW, &sc->sc_stats.err_buf_oflow,
456 0, "Buffer overflows");
457 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
458 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
459 "dma_error", CTLFLAG_RW, &sc->sc_stats.err_dma,
461 SYSCTL_ADD_UQUAD(&sc->sc_sysctl_ctx,
462 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
463 "unkn_error", CTLFLAG_RW, &sc->sc_stats.err_unkn,
464 0, "Unknown errors");
467 ifp->if_init = acx_init;
468 ifp->if_ioctl = acx_ioctl;
469 ifp->if_start = acx_start;
470 ifp->if_watchdog = acx_watchdog;
471 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
472 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
473 ifq_set_ready(&ifp->if_snd);
476 for (i = 1; i <= 14; ++i) {
477 ic->ic_channels[i].ic_freq =
478 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
479 ic->ic_channels[i].ic_flags = sc->chip_chan_flags;
482 ic->ic_opmode = IEEE80211_M_STA;
483 ic->ic_state = IEEE80211_S_INIT;
486 * NOTE: Don't overwrite ic_caps set by chip specific code
488 ic->ic_caps |= IEEE80211_C_WEP | /* WEP */
489 IEEE80211_C_HOSTAP | /* HostAP mode */
490 IEEE80211_C_MONITOR | /* Monitor mode */
491 IEEE80211_C_IBSS | /* IBSS modes */
492 IEEE80211_C_SHPREAMBLE; /* Short preamble */
493 ic->ic_caps_ext = IEEE80211_CEXT_PBCC; /* PBCC modulation */
496 for (i = 0; i < IEEE80211_ADDR_LEN; ++i) {
497 error = acx_read_eeprom(sc, sc->chip_ee_eaddr_ofs - i,
501 ieee80211_ifattach(ic);
503 /* Enable software beacon missing */
504 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
506 /* Override newstate */
507 sc->sc_newstate = ic->ic_newstate;
508 ic->ic_newstate = acx_newstate;
510 ieee80211_media_init(ic, acx_media_change, ieee80211_media_status);
513 * Radio tap attaching
515 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
516 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
519 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(uint32_t));
520 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
521 sc->sc_tx_th.wt_ihdr.it_present = htole32(ACX_TX_RADIOTAP_PRESENT);
523 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(uint32_t));
524 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
525 sc->sc_rx_th.wr_ihdr.it_present = htole32(ACX_RX_RADIOTAP_PRESENT);
527 error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE, acx_intr, sc,
528 &sc->sc_irq_handle, ifp->if_serializer);
530 device_printf(dev, "can't set up interrupt\n");
532 ieee80211_ifdetach(ic);
536 ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->sc_irq_res));
537 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
540 ieee80211_announce(ic);
549 acx_detach(device_t dev)
551 struct acx_softc *sc = device_get_softc(dev);
553 if (device_is_attached(dev)) {
554 struct ieee80211com *ic = &sc->sc_ic;
555 struct ifnet *ifp = &ic->ic_if;
557 lwkt_serialize_enter(ifp->if_serializer);
560 acx_free_firmware(sc);
561 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
563 lwkt_serialize_exit(ifp->if_serializer);
566 ieee80211_ifdetach(ic);
569 if (sc->sc_sysctl_tree != NULL)
570 sysctl_ctx_free(&sc->sc_sysctl_ctx);
572 if (sc->sc_irq_res != NULL) {
573 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
576 if (sc->sc_mem1_res != NULL) {
577 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem1_rid,
580 if (sc->sc_mem2_res != NULL) {
581 bus_release_resource(dev, SYS_RES_MEMORY, sc->chip_mem2_rid,
590 acx_shutdown(device_t dev)
592 struct acx_softc *sc = device_get_softc(dev);
594 lwkt_serialize_enter(sc->sc_ic.ic_if.if_serializer);
596 lwkt_serialize_exit(sc->sc_ic.ic_if.if_serializer);
603 struct acx_softc *sc = arg;
604 struct ieee80211com *ic = &sc->sc_ic;
605 struct ifnet *ifp = &ic->ic_if;
606 struct acx_firmware *fw = &sc->sc_firmware;
609 error = acx_stop(sc);
613 error = acx_alloc_firmware(sc);
617 error = acx_init_tx_ring(sc);
619 if_printf(ifp, "can't initialize TX ring\n");
623 error = acx_init_rx_ring(sc);
625 if_printf(ifp, "can't initialize RX ring\n");
629 error = acx_load_base_firmware(sc, fw->base_fw, fw->base_fw_len);
634 * Initialize command and information registers
635 * NOTE: This should be done after base firmware is loaded
637 acx_init_cmd_reg(sc);
638 acx_init_info_reg(sc);
640 sc->sc_flags |= ACX_FLAG_FW_LOADED;
643 if (sc->chip_post_basefw != NULL) {
644 error = sc->chip_post_basefw(sc);
650 if (fw->radio_fw != NULL) {
651 error = acx_load_radio_firmware(sc, fw->radio_fw,
657 error = sc->chip_init(sc);
661 /* Get and set device various configuration */
662 error = acx_config(sc);
666 /* Setup crypto stuffs */
667 if (sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) {
668 error = acx_set_crypt_keys(sc);
671 sc->sc_ic.ic_flags &= ~IEEE80211_F_DROPUNENC;
674 /* Turn on power led */
675 CSR_CLRB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
679 ifp->if_flags |= IFF_RUNNING;
680 ifp->if_flags &= ~IFF_OACTIVE;
682 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
683 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
684 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_SCAN, -1);
686 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
694 acx_init_info_reg(struct acx_softc *sc)
696 sc->sc_info = CSR_READ_4(sc, ACXREG_INFO_REG_OFFSET);
697 sc->sc_info_param = sc->sc_info + ACX_INFO_REG_SIZE;
701 acx_set_crypt_keys(struct acx_softc *sc)
703 struct ieee80211com *ic = &sc->sc_ic;
704 struct acx_conf_wep_txkey wep_txkey;
705 int i, error, got_wk = 0;
707 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
708 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
710 if (wk->wk_keylen == 0)
713 if (sc->chip_hw_crypt) {
714 error = sc->chip_set_wepkey(sc, wk, i);
718 } else if (wk->wk_flags & IEEE80211_KEY_XMIT) {
719 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
723 if (!got_wk || sc->chip_hw_crypt ||
724 ic->ic_def_txkey == IEEE80211_KEYIX_NONE)
727 /* Set current WEP key index */
728 wep_txkey.wep_txkey = ic->ic_def_txkey;
729 if (acx_set_wep_txkey_conf(sc, &wep_txkey) != 0) {
730 if_printf(&ic->ic_if, "set WEP txkey failed\n");
737 acx_next_scan(void *arg)
739 struct acx_softc *sc = arg;
740 struct ieee80211com *ic = &sc->sc_ic;
741 struct ifnet *ifp = &ic->ic_if;
743 lwkt_serialize_enter(ifp->if_serializer);
745 if (ic->ic_state == IEEE80211_S_SCAN)
746 ieee80211_next_scan(ic);
748 lwkt_serialize_exit(ifp->if_serializer);
752 acx_stop(struct acx_softc *sc)
754 struct ieee80211com *ic = &sc->sc_ic;
755 struct ifnet *ifp = &ic->ic_if;
756 struct acx_buf_data *bd = &sc->sc_buf_data;
757 struct acx_ring_data *rd = &sc->sc_ring_data;
760 ASSERT_SERIALIZED(ifp->if_serializer);
762 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
764 sc->sc_firmware_ver = 0;
765 sc->sc_hardware_id = 0;
768 error = acx_reset(sc);
772 /* Firmware no longer functions after hardware reset */
773 sc->sc_flags &= ~ACX_FLAG_FW_LOADED;
775 acx_disable_intr(sc);
777 /* Stop backgroud scanning */
778 callout_stop(&sc->sc_scan_timer);
780 /* Turn off power led */
781 CSR_SETB_2(sc, ACXREG_GPIO_OUT, sc->chip_gpio_pled);
784 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
785 struct acx_txbuf *buf;
787 buf = &bd->tx_buf[i];
789 if (buf->tb_mbuf != NULL) {
790 bus_dmamap_unload(bd->mbuf_dma_tag,
791 buf->tb_mbuf_dmamap);
792 m_freem(buf->tb_mbuf);
796 if (buf->tb_node != NULL)
797 ieee80211_free_node(buf->tb_node);
801 /* Clear TX host descriptors */
802 bzero(rd->tx_ring, ACX_TX_RING_SIZE);
805 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
806 if (bd->rx_buf[i].rb_mbuf != NULL) {
807 bus_dmamap_unload(bd->mbuf_dma_tag,
808 bd->rx_buf[i].rb_mbuf_dmamap);
809 m_freem(bd->rx_buf[i].rb_mbuf);
810 bd->rx_buf[i].rb_mbuf = NULL;
814 /* Clear RX host descriptors */
815 bzero(rd->rx_ring, ACX_RX_RING_SIZE);
819 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
825 acx_config(struct acx_softc *sc)
827 struct acx_config conf;
830 error = acx_read_config(sc, &conf);
834 error = acx_write_config(sc, &conf);
838 error = acx_rx_config(sc, sc->sc_flags & ACX_FLAG_PROMISC);
842 if (acx_set_probe_req_tmplt(sc, "", 0) != 0) {
843 if_printf(&sc->sc_ic.ic_if, "can't set probe req template "
849 if (acx_set_null_tmplt(sc) != 0) {
850 if_printf(&sc->sc_ic.ic_if, "can't set null data template\n");
857 acx_read_config(struct acx_softc *sc, struct acx_config *conf)
859 struct acx_conf_eaddr addr;
860 struct acx_conf_regdom reg_dom;
861 struct acx_conf_antenna ant;
862 struct acx_conf_fwrev fw_rev;
868 if (acx_get_eaddr_conf(sc, &addr) != 0) {
869 if_printf(&sc->sc_ic.ic_if, "can't get station id\n");
874 * Get and print station id in case that EEPROM station id's
875 * offset is not correct
877 for (i = 0; i < IEEE80211_ADDR_LEN; ++i)
878 conf->eaddr[IEEE80211_ADDR_LEN - 1 - i] = addr.eaddr[i];
879 if_printf(&sc->sc_ic.ic_if, "MAC address (from firmware): %6D\n",
882 /* Get region domain */
883 if (acx_get_regdom_conf(sc, ®_dom) != 0) {
884 if_printf(&sc->sc_ic.ic_if, "can't get region domain\n");
887 conf->regdom = reg_dom.regdom;
888 DPRINTF((&sc->sc_ic.ic_if, "regdom %02x\n", reg_dom.regdom));
891 if (acx_get_antenna_conf(sc, &ant) != 0) {
892 if_printf(&sc->sc_ic.ic_if, "can't get antenna\n");
895 conf->antenna = ant.antenna;
896 DPRINTF((&sc->sc_ic.ic_if, "antenna %02x\n", ant.antenna));
898 /* Get sensitivity XXX not used */
899 if (sc->sc_radio_type == ACX_RADIO_TYPE_MAXIM ||
900 sc->sc_radio_type == ACX_RADIO_TYPE_RFMD ||
901 sc->sc_radio_type == ACX_RADIO_TYPE_RALINK) {
902 error = acx_read_phyreg(sc, ACXRV_PHYREG_SENSITIVITY, &sen);
904 if_printf(&sc->sc_ic.ic_if, "can't get sensitivity\n");
910 DPRINTF((&sc->sc_ic.ic_if, "sensitivity %02x\n", sen));
912 /* Get firmware revision */
913 if (acx_get_fwrev_conf(sc, &fw_rev) != 0) {
914 if_printf(&sc->sc_ic.ic_if, "can't get firmware revision\n");
918 if (strncmp(fw_rev.fw_rev, "Rev ", 4) != 0) {
919 if_printf(&sc->sc_ic.ic_if, "strange revision string -- %s\n",
921 fw_rev_no = 0x01090407;
930 s = &fw_rev.fw_rev[4];
932 for (i = 0; i < 4; ++i) {
935 val = strtoul(s, &endp, 16);
936 fw_rev_no |= val << ((3 - i) * 8);
944 sc->sc_firmware_ver = fw_rev_no;
945 sc->sc_hardware_id = le32toh(fw_rev.hw_id);
946 DPRINTF((&sc->sc_ic.ic_if, "fw rev %08x, hw id %08x\n",
947 sc->sc_firmware_ver, sc->sc_hardware_id));
949 if (sc->chip_read_config != NULL) {
950 error = sc->chip_read_config(sc, conf);
958 acx_write_config(struct acx_softc *sc, struct acx_config *conf)
960 struct acx_conf_nretry_short sretry;
961 struct acx_conf_nretry_long lretry;
962 struct acx_conf_msdu_lifetime msdu_lifetime;
963 struct acx_conf_rate_fallback rate_fb;
964 struct acx_conf_antenna ant;
965 struct acx_conf_regdom reg_dom;
968 /* Set number of long/short retry */
969 KKASSERT(sc->chip_short_retry_limit > 0);
970 sretry.nretry = sc->chip_short_retry_limit;
971 if (acx_set_nretry_short_conf(sc, &sretry) != 0) {
972 if_printf(&sc->sc_ic.ic_if, "can't set short retry limit\n");
976 lretry.nretry = sc->sc_long_retry_limit;
977 if (acx_set_nretry_long_conf(sc, &lretry) != 0) {
978 if_printf(&sc->sc_ic.ic_if, "can't set long retry limit\n");
982 /* Set MSDU lifetime */
983 msdu_lifetime.lifetime = htole32(sc->sc_msdu_lifetime);
984 if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
985 if_printf(&sc->sc_ic.ic_if, "can't set MSDU lifetime\n");
989 /* Enable rate fallback */
990 rate_fb.ratefb_enable = 1;
991 if (acx_set_rate_fallback_conf(sc, &rate_fb) != 0) {
992 if_printf(&sc->sc_ic.ic_if, "can't enable rate fallback\n");
997 ant.antenna = conf->antenna;
998 if (acx_set_antenna_conf(sc, &ant) != 0) {
999 if_printf(&sc->sc_ic.ic_if, "can't set antenna\n");
1003 /* Set region domain */
1004 reg_dom.regdom = conf->regdom;
1005 if (acx_set_regdom_conf(sc, ®_dom) != 0) {
1006 if_printf(&sc->sc_ic.ic_if, "can't set region domain\n");
1010 if (sc->chip_write_config != NULL) {
1011 error = sc->chip_write_config(sc, conf);
1020 acx_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1022 struct acx_softc *sc = ifp->if_softc;
1023 struct ieee80211com *ic = &sc->sc_ic;
1028 req = (struct ifreq *)data;
1032 if (ifp->if_flags & IFF_UP) {
1033 if ((ifp->if_flags & IFF_RUNNING)) {
1036 if ((ifp->if_flags & IFF_PROMISC) &&
1037 (sc->sc_flags & ACX_FLAG_PROMISC) == 0)
1039 else if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1040 (sc->sc_flags & ACX_FLAG_PROMISC))
1044 * Promisc mode is always enabled when
1045 * operation mode is Monitor.
1047 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1049 error = acx_rx_config(sc, promisc);
1054 if (ifp->if_flags & IFF_RUNNING)
1058 if (ifp->if_flags & IFF_PROMISC)
1059 sc->sc_flags |= ACX_FLAG_PROMISC;
1061 sc->sc_flags &= ~ACX_FLAG_PROMISC;
1068 error = ieee80211_ioctl(ic, cmd, data, cr);
1072 if (error == ENETRESET) {
1073 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
1074 (IFF_RUNNING | IFF_UP))
1082 acx_start(struct ifnet *ifp)
1084 struct acx_softc *sc = ifp->if_softc;
1085 struct ieee80211com *ic = &sc->sc_ic;
1086 struct acx_buf_data *bd = &sc->sc_buf_data;
1087 struct acx_txbuf *buf;
1090 ASSERT_SERIALIZED(ifp->if_serializer);
1092 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0) {
1093 ifq_purge(&ifp->if_snd);
1094 ieee80211_drain_mgtq(&ic->ic_mgtq);
1098 if ((ifp->if_flags & IFF_RUNNING) == 0 ||
1099 (ifp->if_flags & IFF_OACTIVE))
1104 * We can't start from a random position that TX descriptor
1105 * is free, since hardware will be confused by that.
1106 * We have to follow the order of the TX ring.
1108 idx = bd->tx_free_start;
1110 for (buf = &bd->tx_buf[idx]; buf->tb_mbuf == NULL;
1111 buf = &bd->tx_buf[idx]) {
1112 struct ieee80211_frame *f;
1113 struct ieee80211_node *ni = NULL;
1117 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1118 IF_DEQUEUE(&ic->ic_mgtq, m);
1120 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1121 m->m_pkthdr.rcvif = NULL;
1126 * Don't transmit probe response firmware will
1129 f = mtod(m, struct ieee80211_frame *);
1130 if ((f->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1131 IEEE80211_FC0_TYPE_MGT &&
1132 (f->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1133 IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1135 ieee80211_free_node(ni);
1139 } else if (!ifq_is_empty(&ifp->if_snd)) {
1140 struct ether_header *eh;
1142 if (ic->ic_state != IEEE80211_S_RUN) {
1143 ifq_purge(&ifp->if_snd);
1147 m = ifq_dequeue(&ifp->if_snd, NULL);
1151 if (m->m_len < sizeof(struct ether_header)) {
1152 m = m_pullup(m, sizeof(struct ether_header));
1158 eh = mtod(m, struct ether_header *);
1160 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1167 /* TODO power save */
1171 m = ieee80211_encap(ic, m, ni);
1173 ieee80211_free_node(ni);
1181 if (ic->ic_rawbpf != NULL)
1182 bpf_mtap(ic->ic_rawbpf, m);
1184 f = mtod(m, struct ieee80211_frame *);
1185 if ((f->i_fc[1] & IEEE80211_FC1_WEP) && !sc->chip_hw_crypt) {
1186 KASSERT(ni != NULL, ("TX node is NULL (WEP)\n"));
1187 if (ieee80211_crypto_encap(ic, ni, m) == NULL) {
1188 ieee80211_free_node(ni);
1196 * Since mgmt data are transmitted at fixed rate
1197 * they will not be used to do rate control.
1199 if (mgmt_pkt && ni != NULL) {
1200 ieee80211_free_node(ni);
1204 if (acx_encap(sc, buf, m, ni) != 0) {
1206 * NOTE: `m' will be freed in acx_encap()
1210 ieee80211_free_node(ni);
1217 * 1) `m' should not be touched after acx_encap()
1218 * 2) `node' will be used to do TX rate control during
1219 * acx_txeof(), so it is not freed here. acx_txeof()
1220 * will free it for us
1224 bd->tx_used_count++;
1225 idx = (idx + 1) % ACX_TX_DESC_CNT;
1227 bd->tx_free_start = idx;
1229 if (bd->tx_used_count == ACX_TX_DESC_CNT)
1230 ifp->if_flags |= IFF_OACTIVE;
1232 if (trans && sc->sc_tx_timer == 0)
1233 sc->sc_tx_timer = 5;
1238 acx_watchdog(struct ifnet *ifp)
1240 struct acx_softc *sc = ifp->if_softc;
1244 if ((ifp->if_flags & IFF_RUNNING) == 0)
1247 if (sc->sc_tx_timer) {
1248 if (--sc->sc_tx_timer == 0) {
1249 if_printf(ifp, "watchdog timeout\n");
1251 acx_txeof(ifp->if_softc);
1256 ieee80211_watchdog(&sc->sc_ic);
1262 struct acx_softc *sc = arg;
1263 uint16_t intr_status;
1265 if ((sc->sc_flags & ACX_FLAG_FW_LOADED) == 0)
1268 intr_status = CSR_READ_2(sc, ACXREG_INTR_STATUS_CLR);
1269 if (intr_status == ACXRV_INTR_ALL) {
1270 /* not our interrupt */
1274 intr_status &= sc->chip_intr_enable;
1275 if (intr_status == 0) {
1276 /* not interrupts we care about */
1280 /* Acknowledge all interrupts */
1281 CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_ALL);
1283 if (intr_status & ACXRV_INTR_TX_FINI)
1286 if (intr_status & ACXRV_INTR_RX_FINI)
1291 acx_disable_intr(struct acx_softc *sc)
1293 CSR_WRITE_2(sc, ACXREG_INTR_MASK, sc->chip_intr_disable);
1294 CSR_WRITE_2(sc, ACXREG_EVENT_MASK, 0);
1298 acx_enable_intr(struct acx_softc *sc)
1300 /* Mask out interrupts that are not in the enable set */
1301 CSR_WRITE_2(sc, ACXREG_INTR_MASK, ~sc->chip_intr_enable);
1302 CSR_WRITE_2(sc, ACXREG_EVENT_MASK, ACXRV_EVENT_DISABLE);
1306 acx_txeof(struct acx_softc *sc)
1308 struct acx_buf_data *bd;
1309 struct acx_txbuf *buf;
1313 ifp = &sc->sc_ic.ic_if;
1314 ASSERT_SERIALIZED(ifp->if_serializer);
1316 bd = &sc->sc_buf_data;
1317 idx = bd->tx_used_start;
1318 for (buf = &bd->tx_buf[idx]; buf->tb_mbuf != NULL;
1319 buf = &bd->tx_buf[idx]) {
1320 uint8_t ctrl, error;
1323 ctrl = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_ctrl);
1324 if ((ctrl & (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE)) !=
1325 (DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE))
1328 bus_dmamap_unload(bd->mbuf_dma_tag, buf->tb_mbuf_dmamap);
1329 frame_len = buf->tb_mbuf->m_pkthdr.len;
1330 m_freem(buf->tb_mbuf);
1331 buf->tb_mbuf = NULL;
1333 error = FW_TXDESC_GETFIELD_1(sc, buf, f_tx_error);
1335 acx_txerr(sc, error);
1341 if (buf->tb_node != NULL) {
1342 sc->chip_tx_complete(sc, buf, frame_len, error);
1343 ieee80211_free_node(buf->tb_node);
1344 buf->tb_node = NULL;
1347 FW_TXDESC_SETFIELD_1(sc, buf, f_tx_ctrl, DESC_CTRL_HOSTOWN);
1349 bd->tx_used_count--;
1351 idx = (idx + 1) % ACX_TX_DESC_CNT;
1353 bd->tx_used_start = idx;
1355 sc->sc_tx_timer = bd->tx_used_count == 0 ? 0 : 5;
1357 if (bd->tx_used_count != ACX_TX_DESC_CNT) {
1358 ifp->if_flags &= ~IFF_OACTIVE;
1364 acx_txerr(struct acx_softc *sc, uint8_t err)
1366 struct ifnet *ifp = &sc->sc_ic.ic_if;
1367 struct acx_stats *stats = &sc->sc_stats;
1369 if (err == DESC_ERR_EXCESSIVE_RETRY) {
1371 * This a common error (see comment below),
1372 * so print it using DPRINTF()
1374 DPRINTF((ifp, "TX failed -- excessive retry\n"));
1376 if_printf(ifp, "TX failed -- ");
1380 * Although `err' looks like bitmask, it never
1381 * has multiple bits set.
1385 case DESC_ERR_OTHER_FRAG:
1386 /* XXX what's this */
1387 kprintf("error in other fragment\n");
1388 stats->err_oth_frag++;
1391 case DESC_ERR_ABORT:
1392 kprintf("aborted\n");
1395 case DESC_ERR_PARAM:
1396 kprintf("wrong parameters in descriptor\n");
1399 case DESC_ERR_NO_WEPKEY:
1400 kprintf("WEP key missing\n");
1401 stats->err_no_wepkey++;
1403 case DESC_ERR_MSDU_TIMEOUT:
1404 kprintf("MSDU life timeout\n");
1405 stats->err_msdu_timeout++;
1407 case DESC_ERR_EXCESSIVE_RETRY:
1410 * 1) Distance is too long
1411 * 2) Transmit failed (e.g. no MAC level ACK)
1412 * 3) Chip overheated (this should be rare)
1414 stats->err_ex_retry++;
1416 case DESC_ERR_BUF_OVERFLOW:
1417 kprintf("buffer overflow\n");
1418 stats->err_buf_oflow++;
1421 kprintf("DMA error\n");
1425 kprintf("unknown error %d\n", err);
1432 acx_rxeof(struct acx_softc *sc)
1434 struct ieee80211com *ic = &sc->sc_ic;
1435 struct acx_ring_data *rd = &sc->sc_ring_data;
1436 struct acx_buf_data *bd = &sc->sc_buf_data;
1437 struct ifnet *ifp = &ic->ic_if;
1440 ASSERT_SERIALIZED(ic->ic_if.if_serializer);
1442 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1443 BUS_DMASYNC_POSTREAD);
1446 * Locate first "ready" rx buffer,
1447 * start from last stopped position
1449 idx = bd->rx_scan_start;
1452 struct acx_rxbuf *buf;
1454 buf = &bd->rx_buf[idx];
1455 if ((buf->rb_desc->h_ctrl & htole16(DESC_CTRL_HOSTOWN)) &&
1456 (buf->rb_desc->h_status & htole32(DESC_STATUS_FULL))) {
1460 idx = (idx + 1) % ACX_RX_DESC_CNT;
1461 } while (idx != bd->rx_scan_start);
1467 * NOTE: don't mess up `idx' here, it will
1468 * be used in the following code
1472 struct acx_rxbuf_hdr *head;
1473 struct acx_rxbuf *buf;
1474 struct ieee80211_frame_min *wh;
1476 uint32_t desc_status;
1478 int len, error, rssi, is_priv;
1480 buf = &bd->rx_buf[idx];
1482 desc_ctrl = le16toh(buf->rb_desc->h_ctrl);
1483 desc_status = le32toh(buf->rb_desc->h_status);
1484 if (!(desc_ctrl & DESC_CTRL_HOSTOWN) ||
1485 !(desc_status & DESC_STATUS_FULL))
1488 bus_dmamap_sync(bd->mbuf_dma_tag, buf->rb_mbuf_dmamap,
1489 BUS_DMASYNC_POSTREAD);
1493 error = acx_newbuf(sc, buf, 0);
1499 head = mtod(m, struct acx_rxbuf_hdr *);
1500 len = le16toh(head->rbh_len) & ACX_RXBUF_LEN_MASK;
1501 rssi = acx_get_rssi(sc, head->rbh_level);
1503 m_adj(m, sizeof(struct acx_rxbuf_hdr) + sc->chip_rxbuf_exhdr);
1504 m->m_len = m->m_pkthdr.len = len;
1505 m->m_pkthdr.rcvif = &ic->ic_if;
1507 wh = mtod(m, struct ieee80211_frame_min *);
1508 is_priv = (wh->i_fc[1] & IEEE80211_FC1_WEP);
1510 if (sc->sc_drvbpf != NULL) {
1511 sc->sc_rx_th.wr_tsf = htole32(head->rbh_time);
1513 sc->sc_rx_th.wr_flags = 0;
1515 sc->sc_rx_th.wr_flags |=
1516 IEEE80211_RADIOTAP_F_WEP;
1518 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_SHPRE) {
1519 sc->sc_rx_th.wr_flags |=
1520 IEEE80211_RADIOTAP_F_SHORTPRE;
1523 if (sc->chip_phymode == IEEE80211_MODE_11G) {
1524 sc->sc_rx_th.wr_rate =
1525 ieee80211_plcp2rate(head->rbh_plcp,
1526 head->rbh_bbp_stat & ACX_RXBUF_STAT_OFDM);
1528 sc->sc_rx_th.wr_rate =
1529 ieee80211_plcp2rate(head->rbh_plcp, 0);
1532 sc->sc_rx_th.wr_antsignal = rssi;
1534 if (head->rbh_bbp_stat & ACX_RXBUF_STAT_ANT1)
1535 sc->sc_rx_th.wr_antenna = 1;
1537 sc->sc_rx_th.wr_antenna = 0;
1539 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th,
1543 if (len >= sizeof(struct ieee80211_frame_min) &&
1545 struct ieee80211_node *ni;
1547 if (is_priv && sc->chip_hw_crypt) {
1548 /* Short circuit software WEP */
1549 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1551 /* Do chip specific RX buffer processing */
1552 if (sc->chip_proc_wep_rxbuf != NULL) {
1553 sc->chip_proc_wep_rxbuf(sc, m, &len);
1555 struct ieee80211_frame_min *);
1558 m->m_len = m->m_pkthdr.len = len;
1560 ni = ieee80211_find_rxnode(ic, wh);
1561 ieee80211_input(ic, m, ni, rssi,
1562 le32toh(head->rbh_time));
1563 ieee80211_free_node(ni);
1567 if (len < sizeof(struct ieee80211_frame_min)) {
1568 if (ic->ic_rawbpf != NULL &&
1569 len >= sizeof(struct ieee80211_frame_ack))
1570 bpf_mtap(ic->ic_rawbpf, m);
1572 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1573 ic->ic_stats.is_rx_tooshort++;
1578 buf->rb_desc->h_ctrl = htole16(desc_ctrl & ~DESC_CTRL_HOSTOWN);
1579 buf->rb_desc->h_status = 0;
1580 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
1581 BUS_DMASYNC_PREWRITE);
1583 idx = (idx + 1) % ACX_RX_DESC_CNT;
1584 } while (idx != bd->rx_scan_start);
1587 * Record the position so that next
1588 * time we can start from it
1590 bd->rx_scan_start = idx;
1594 acx_reset(struct acx_softc *sc)
1599 CSR_SETB_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_HALT);
1601 /* Software reset */
1602 reg = CSR_READ_2(sc, ACXREG_SOFT_RESET);
1603 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg | ACXRV_SOFT_RESET);
1605 CSR_WRITE_2(sc, ACXREG_SOFT_RESET, reg);
1607 /* Initialize EEPROM */
1608 CSR_SETB_2(sc, ACXREG_EEPROM_INIT, ACXRV_EEPROM_INIT);
1611 /* Test whether ECPU is stopped */
1612 reg = CSR_READ_2(sc, ACXREG_ECPU_CTRL);
1613 if (!(reg & ACXRV_ECPU_HALT)) {
1614 if_printf(&sc->sc_ic.ic_if, "can't halt ECPU\n");
1621 acx_read_eeprom(struct acx_softc *sc, uint32_t offset, uint8_t *val)
1625 CSR_WRITE_4(sc, ACXREG_EEPROM_CONF, 0);
1626 CSR_WRITE_4(sc, ACXREG_EEPROM_ADDR, offset);
1627 CSR_WRITE_4(sc, ACXREG_EEPROM_CTRL, ACXRV_EEPROM_READ);
1629 #define EE_READ_RETRY_MAX 100
1630 for (i = 0; i < EE_READ_RETRY_MAX; ++i) {
1631 if (CSR_READ_2(sc, ACXREG_EEPROM_CTRL) == 0)
1635 if (i == EE_READ_RETRY_MAX) {
1636 if_printf(&sc->sc_ic.ic_if, "can't read EEPROM offset %x "
1637 "(timeout)\n", offset);
1640 #undef EE_READ_RETRY_MAX
1642 *val = CSR_READ_1(sc, ACXREG_EEPROM_DATA);
1647 acx_read_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t *val)
1651 CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1652 CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_READ);
1654 #define PHY_READ_RETRY_MAX 100
1655 for (i = 0; i < PHY_READ_RETRY_MAX; ++i) {
1656 if (CSR_READ_4(sc, ACXREG_PHY_CTRL) == 0)
1660 if (i == PHY_READ_RETRY_MAX) {
1661 if_printf(&sc->sc_ic.ic_if, "can't read phy reg %x (timeout)\n",
1665 #undef PHY_READ_RETRY_MAX
1667 *val = CSR_READ_1(sc, ACXREG_PHY_DATA);
1672 acx_write_phyreg(struct acx_softc *sc, uint32_t reg, uint8_t val)
1674 CSR_WRITE_4(sc, ACXREG_PHY_DATA, val);
1675 CSR_WRITE_4(sc, ACXREG_PHY_ADDR, reg);
1676 CSR_WRITE_4(sc, ACXREG_PHY_CTRL, ACXRV_PHY_WRITE);
1680 acx_alloc_firmware(struct acx_softc *sc)
1682 struct acx_firmware *fw = &sc->sc_firmware;
1683 struct ifnet *ifp = &sc->sc_ic.ic_if;
1684 struct fw_image *img;
1689 * NB: serializer need to be released before loading firmware
1690 * image to avoid possible dead lock
1692 ASSERT_SERIALIZED(ifp->if_serializer);
1694 if (fw->base_fw_image == NULL) {
1695 if (fw->combined_radio_fw) {
1696 ksnprintf(filename, sizeof(filename),
1697 ACX_BASE_RADIO_FW_PATH,
1698 fw->fwdir, sc->sc_radio_type);
1700 ksnprintf(filename, sizeof(filename),
1701 ACX_BASE_FW_PATH, fw->fwdir);
1704 lwkt_serialize_exit(ifp->if_serializer);
1705 img = firmware_image_load(filename, NULL);
1706 lwkt_serialize_enter(ifp->if_serializer);
1708 fw->base_fw_image = img;
1709 if (fw->base_fw_image == NULL) {
1710 if_printf(ifp, "load %s base fw failed\n", filename);
1715 error = acx_setup_firmware(sc, fw->base_fw_image,
1716 &fw->base_fw, &fw->base_fw_len);
1721 if (!fw->combined_radio_fw && fw->radio_fw_image == NULL) {
1722 ksnprintf(filename, sizeof(filename), ACX_RADIO_FW_PATH,
1723 fw->fwdir, sc->sc_radio_type);
1725 lwkt_serialize_exit(ifp->if_serializer);
1726 img = firmware_image_load(filename, NULL);
1727 lwkt_serialize_enter(ifp->if_serializer);
1729 fw->radio_fw_image = img;
1730 if (fw->radio_fw_image == NULL) {
1731 if_printf(ifp, "load %s radio fw failed\n", filename);
1736 error = acx_setup_firmware(sc, fw->radio_fw_image,
1737 &fw->radio_fw, &fw->radio_fw_len);
1741 acx_free_firmware(sc);
1746 acx_setup_firmware(struct acx_softc *sc, struct fw_image *img,
1747 const uint8_t **ptr, int *len)
1749 const struct acx_firmware_hdr *hdr;
1758 * Make sure that the firmware image contains more than just a header
1760 if (img->fw_imglen <= sizeof(*hdr)) {
1761 if_printf(&sc->sc_ic.ic_if, "%s is invalid image, "
1762 "size %u (too small)\n",
1763 img->fw_name, img->fw_imglen);
1766 hdr = (const struct acx_firmware_hdr *)img->fw_image;
1771 if (hdr->fwh_len != img->fw_imglen - sizeof(*hdr)) {
1772 if_printf(&sc->sc_ic.ic_if, "%s is invalid image, "
1773 "size in hdr %u and image size %u mismatches\n",
1774 img->fw_name, hdr->fwh_len, img->fw_imglen);
1782 for (i = 0, p = (const uint8_t *)&hdr->fwh_len;
1783 i < img->fw_imglen - sizeof(hdr->fwh_cksum); ++i, ++p)
1785 if (cksum != hdr->fwh_cksum) {
1786 if_printf(&sc->sc_ic.ic_if, "%s is invalid image, "
1787 "checksum mismatch\n", img->fw_name);
1791 *ptr = ((const uint8_t *)img->fw_image + sizeof(*hdr));
1792 *len = img->fw_imglen - sizeof(*hdr);
1797 acx_free_firmware(struct acx_softc *sc)
1799 struct acx_firmware *fw = &sc->sc_firmware;
1801 if (fw->base_fw_image != NULL) {
1802 firmware_image_unload(fw->base_fw_image);
1803 fw->base_fw_image = NULL;
1805 fw->base_fw_len = 0;
1807 if (fw->radio_fw_image != NULL) {
1808 firmware_image_unload(fw->radio_fw_image);
1809 fw->radio_fw_image = NULL;
1810 fw->radio_fw = NULL;
1811 fw->radio_fw_len = 0;
1816 acx_load_base_firmware(struct acx_softc *sc, const uint8_t *base_fw,
1817 uint32_t base_fw_len)
1821 /* Load base firmware */
1822 error = acx_load_firmware(sc, 0, base_fw, base_fw_len);
1824 if_printf(&sc->sc_ic.ic_if, "can't load base firmware\n");
1827 DPRINTF((&sc->sc_ic.ic_if, "base firmware loaded\n"));
1830 CSR_WRITE_2(sc, ACXREG_ECPU_CTRL, ACXRV_ECPU_START);
1832 /* Wait for ECPU to be up */
1833 for (i = 0; i < 500; ++i) {
1836 reg = CSR_READ_2(sc, ACXREG_INTR_STATUS);
1837 if (reg & ACXRV_INTR_FCS_THRESH) {
1838 CSR_WRITE_2(sc, ACXREG_INTR_ACK, ACXRV_INTR_FCS_THRESH);
1844 if_printf(&sc->sc_ic.ic_if, "can't initialize ECPU (timeout)\n");
1849 acx_load_radio_firmware(struct acx_softc *sc, const uint8_t *radio_fw,
1850 uint32_t radio_fw_len)
1852 struct acx_conf_mmap mem_map;
1853 uint32_t radio_fw_ofs;
1857 * Get the position, where base firmware is loaded, so that
1858 * radio firmware can be loaded after it.
1860 if (acx_get_mmap_conf(sc, &mem_map) != 0)
1862 radio_fw_ofs = le32toh(mem_map.code_end);
1864 /* Put ECPU into sleeping state, before loading radio firmware */
1865 if (acx_sleep(sc) != 0)
1868 /* Load radio firmware */
1869 error = acx_load_firmware(sc, radio_fw_ofs, radio_fw, radio_fw_len);
1871 if_printf(&sc->sc_ic.ic_if, "can't load radio firmware\n");
1874 DPRINTF((&sc->sc_ic.ic_if, "radio firmware loaded\n"));
1876 /* Wake up sleeping ECPU, after radio firmware is loaded */
1877 if (acx_wakeup(sc) != 0)
1880 /* Initialize radio */
1881 if (acx_init_radio(sc, radio_fw_ofs, radio_fw_len) != 0)
1884 /* Verify radio firmware's loading position */
1885 if (acx_get_mmap_conf(sc, &mem_map) != 0)
1887 if (le32toh(mem_map.code_end) != radio_fw_ofs + radio_fw_len) {
1888 if_printf(&sc->sc_ic.ic_if, "loaded radio firmware position "
1893 DPRINTF((&sc->sc_ic.ic_if, "radio firmware initialized\n"));
1898 acx_load_firmware(struct acx_softc *sc, uint32_t offset, const uint8_t *data,
1904 fw = (const uint32_t *)data;
1905 fw_len = data_len / sizeof(uint32_t);
1908 * LOADFW_AUTO_INC only works with some older firmware:
1909 * 1) acx100's firmware
1910 * 2) acx111's firmware whose rev is 0x00010011
1914 CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1915 #ifndef LOADFW_AUTO_INC
1916 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1918 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1919 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1922 for (i = 0; i < fw_len; ++i) {
1923 #ifndef LOADFW_AUTO_INC
1924 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1926 CSR_WRITE_4(sc, ACXREG_FWMEM_DATA, be32toh(fw[i]));
1929 /* Verify firmware */
1930 CSR_WRITE_4(sc, ACXREG_FWMEM_START, ACXRV_FWMEM_START_OP);
1931 #ifndef LOADFW_AUTO_INC
1932 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, 0);
1934 CSR_WRITE_4(sc, ACXREG_FWMEM_CTRL, ACXRV_FWMEM_ADDR_AUTOINC);
1935 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset);
1938 for (i = 0; i < fw_len; ++i) {
1941 #ifndef LOADFW_AUTO_INC
1942 CSR_WRITE_4(sc, ACXREG_FWMEM_ADDR, offset + (i * 4));
1944 val = CSR_READ_4(sc, ACXREG_FWMEM_DATA);
1945 if (be32toh(fw[i]) != val) {
1946 if_printf(&sc->sc_ic.ic_if, "fireware mismatch "
1947 "fw %08x loaded %08x\n", fw[i], val);
1955 acx_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1957 struct ifnet *ifp = &ic->ic_if;
1958 struct acx_softc *sc = ifp->if_softc;
1959 struct ieee80211_node *ni = NULL;
1960 struct ieee80211_channel *c = NULL;
1961 int error = 1, mode = 0;
1963 ASSERT_SERIALIZED(ifp->if_serializer);
1965 ieee80211_ratectl_newstate(ic, nstate);
1966 callout_stop(&sc->sc_scan_timer);
1969 case IEEE80211_S_SCAN:
1970 acx_set_chan(sc, ic->ic_curchan);
1971 callout_reset(&sc->sc_scan_timer,
1972 (hz * sc->sc_scan_dwell) / 1000,
1975 case IEEE80211_S_AUTH:
1976 if (ic->ic_opmode == IEEE80211_M_STA) {
1979 mode = ACX_MODE_STA;
1982 case IEEE80211_S_RUN:
1983 if (ic->ic_opmode == IEEE80211_M_IBSS ||
1984 ic->ic_opmode == IEEE80211_M_HOSTAP) {
1987 if (ic->ic_opmode == IEEE80211_M_IBSS)
1988 mode = ACX_MODE_ADHOC;
1992 if (acx_set_beacon_tmplt(sc, ni) != 0) {
1993 if_printf(ifp, "set bescon template failed\n");
1996 if (acx_set_probe_resp_tmplt(sc, ni) != 0) {
1997 if_printf(ifp, "set probe response template"
2001 } else if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2004 mode = ACX_MODE_STA;
2012 KKASSERT(c != NULL);
2014 if (acx_set_chan(sc, c) != 0)
2017 if (acx_join_bss(sc, mode, ni, c) != 0) {
2018 if_printf(ifp, "join BSS failed\n");
2027 nstate = IEEE80211_S_INIT;
2030 return sc->sc_newstate(ic, nstate, arg);
2034 acx_init_tmplt_ordered(struct acx_softc *sc)
2036 #define INIT_TMPLT(name) \
2038 if (acx_init_##name##_tmplt(sc) != 0) \
2044 * Order of templates initialization:
2050 * Above order is critical to get a correct memory map.
2052 INIT_TMPLT(probe_req);
2053 INIT_TMPLT(null_data);
2056 INIT_TMPLT(probe_resp);
2063 acx_ring_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg, int error)
2065 *((uint32_t *)arg) = seg->ds_addr;
2069 acx_dma_alloc(struct acx_softc *sc)
2071 struct acx_ring_data *rd = &sc->sc_ring_data;
2072 struct acx_buf_data *bd = &sc->sc_buf_data;
2075 /* Allocate DMA stuffs for RX descriptors */
2076 error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
2077 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2079 ACX_RX_RING_SIZE, 1, ACX_RX_RING_SIZE,
2080 0, &rd->rx_ring_dma_tag);
2082 if_printf(&sc->sc_ic.ic_if, "can't create rx ring dma tag\n");
2086 error = bus_dmamem_alloc(rd->rx_ring_dma_tag, (void **)&rd->rx_ring,
2087 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2088 &rd->rx_ring_dmamap);
2090 if_printf(&sc->sc_ic.ic_if,
2091 "can't allocate rx ring dma memory\n");
2092 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2093 rd->rx_ring_dma_tag = NULL;
2097 error = bus_dmamap_load(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2098 rd->rx_ring, ACX_RX_RING_SIZE,
2099 acx_ring_dma_addr, &rd->rx_ring_paddr,
2102 if_printf(&sc->sc_ic.ic_if, "can't get rx ring dma address\n");
2103 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
2104 rd->rx_ring_dmamap);
2105 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2106 rd->rx_ring_dma_tag = NULL;
2110 /* Allocate DMA stuffs for TX descriptors */
2111 error = bus_dma_tag_create(NULL, PAGE_SIZE, 0,
2112 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2114 ACX_TX_RING_SIZE, 1, ACX_TX_RING_SIZE,
2115 0, &rd->tx_ring_dma_tag);
2117 if_printf(&sc->sc_ic.ic_if, "can't create tx ring dma tag\n");
2121 error = bus_dmamem_alloc(rd->tx_ring_dma_tag, (void **)&rd->tx_ring,
2122 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2123 &rd->tx_ring_dmamap);
2125 if_printf(&sc->sc_ic.ic_if,
2126 "can't allocate tx ring dma memory\n");
2127 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2128 rd->tx_ring_dma_tag = NULL;
2132 error = bus_dmamap_load(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2133 rd->tx_ring, ACX_TX_RING_SIZE,
2134 acx_ring_dma_addr, &rd->tx_ring_paddr,
2137 if_printf(&sc->sc_ic.ic_if, "can't get tx ring dma address\n");
2138 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2139 rd->tx_ring_dmamap);
2140 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2141 rd->tx_ring_dma_tag = NULL;
2145 /* Create DMA tag for RX/TX mbuf map */
2146 error = bus_dma_tag_create(NULL, 1, 0,
2147 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2149 MCLBYTES, 1, MCLBYTES,
2150 0, &bd->mbuf_dma_tag);
2152 if_printf(&sc->sc_ic.ic_if, "can't create mbuf dma tag\n");
2156 /* Create a spare RX DMA map */
2157 error = bus_dmamap_create(bd->mbuf_dma_tag, 0, &bd->mbuf_tmp_dmamap);
2159 if_printf(&sc->sc_ic.ic_if, "can't create tmp mbuf dma map\n");
2160 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2161 bd->mbuf_dma_tag = NULL;
2165 /* Create DMA map for RX mbufs */
2166 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2167 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2168 &bd->rx_buf[i].rb_mbuf_dmamap);
2170 if_printf(&sc->sc_ic.ic_if, "can't create rx mbuf "
2171 "dma map (%d)\n", i);
2174 bd->rx_buf[i].rb_desc = &rd->rx_ring[i];
2177 /* Create DMA map for TX mbufs */
2178 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2179 error = bus_dmamap_create(bd->mbuf_dma_tag, 0,
2180 &bd->tx_buf[i].tb_mbuf_dmamap);
2182 if_printf(&sc->sc_ic.ic_if, "can't create tx mbuf "
2183 "dma map (%d)\n", i);
2186 bd->tx_buf[i].tb_desc1 = &rd->tx_ring[i * 2];
2187 bd->tx_buf[i].tb_desc2 = &rd->tx_ring[(i * 2) + 1];
2194 acx_dma_free(struct acx_softc *sc)
2196 struct acx_ring_data *rd = &sc->sc_ring_data;
2197 struct acx_buf_data *bd = &sc->sc_buf_data;
2200 if (rd->rx_ring_dma_tag != NULL) {
2201 bus_dmamap_unload(rd->rx_ring_dma_tag, rd->rx_ring_dmamap);
2202 bus_dmamem_free(rd->rx_ring_dma_tag, rd->rx_ring,
2203 rd->rx_ring_dmamap);
2204 bus_dma_tag_destroy(rd->rx_ring_dma_tag);
2207 if (rd->tx_ring_dma_tag != NULL) {
2208 bus_dmamap_unload(rd->tx_ring_dma_tag, rd->tx_ring_dmamap);
2209 bus_dmamem_free(rd->tx_ring_dma_tag, rd->tx_ring,
2210 rd->tx_ring_dmamap);
2211 bus_dma_tag_destroy(rd->tx_ring_dma_tag);
2214 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2215 if (bd->rx_buf[i].rb_desc != NULL) {
2216 if (bd->rx_buf[i].rb_mbuf != NULL) {
2217 bus_dmamap_unload(bd->mbuf_dma_tag,
2218 bd->rx_buf[i].rb_mbuf_dmamap);
2219 m_freem(bd->rx_buf[i].rb_mbuf);
2221 bus_dmamap_destroy(bd->mbuf_dma_tag,
2222 bd->rx_buf[i].rb_mbuf_dmamap);
2226 for (i = 0; i < ACX_TX_DESC_CNT; ++i) {
2227 if (bd->tx_buf[i].tb_desc1 != NULL) {
2228 if (bd->tx_buf[i].tb_mbuf != NULL) {
2229 bus_dmamap_unload(bd->mbuf_dma_tag,
2230 bd->tx_buf[i].tb_mbuf_dmamap);
2231 m_freem(bd->tx_buf[i].tb_mbuf);
2233 bus_dmamap_destroy(bd->mbuf_dma_tag,
2234 bd->tx_buf[i].tb_mbuf_dmamap);
2238 if (bd->mbuf_dma_tag != NULL) {
2239 bus_dmamap_destroy(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap);
2240 bus_dma_tag_destroy(bd->mbuf_dma_tag);
2245 acx_init_tx_ring(struct acx_softc *sc)
2247 struct acx_ring_data *rd;
2248 struct acx_buf_data *bd;
2252 rd = &sc->sc_ring_data;
2253 paddr = rd->tx_ring_paddr;
2254 for (i = 0; i < (ACX_TX_DESC_CNT * 2) - 1; ++i) {
2255 paddr += sizeof(struct acx_host_desc);
2257 rd->tx_ring[i].h_ctrl = htole16(DESC_CTRL_HOSTOWN);
2259 if (i == (ACX_TX_DESC_CNT * 2) - 1)
2260 rd->tx_ring[i].h_next_desc = htole32(rd->tx_ring_paddr);
2262 rd->tx_ring[i].h_next_desc = htole32(paddr);
2265 bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2266 BUS_DMASYNC_PREWRITE);
2268 bd = &sc->sc_buf_data;
2269 bd->tx_free_start = 0;
2270 bd->tx_used_start = 0;
2271 bd->tx_used_count = 0;
2277 acx_init_rx_ring(struct acx_softc *sc)
2279 struct acx_ring_data *rd;
2280 struct acx_buf_data *bd;
2284 bd = &sc->sc_buf_data;
2285 rd = &sc->sc_ring_data;
2286 paddr = rd->rx_ring_paddr;
2288 for (i = 0; i < ACX_RX_DESC_CNT; ++i) {
2291 paddr += sizeof(struct acx_host_desc);
2293 error = acx_newbuf(sc, &bd->rx_buf[i], 1);
2297 if (i == ACX_RX_DESC_CNT - 1)
2298 rd->rx_ring[i].h_next_desc = htole32(rd->rx_ring_paddr);
2300 rd->rx_ring[i].h_next_desc = htole32(paddr);
2303 bus_dmamap_sync(rd->rx_ring_dma_tag, rd->rx_ring_dmamap,
2304 BUS_DMASYNC_PREWRITE);
2306 bd->rx_scan_start = 0;
2311 acx_buf_dma_addr(void *arg, bus_dma_segment_t *seg, int nseg,
2312 bus_size_t mapsz, int error)
2318 KASSERT(nseg == 1, ("too many RX dma segments\n"));
2319 *((uint32_t *)arg) = seg->ds_addr;
2323 acx_newbuf(struct acx_softc *sc, struct acx_rxbuf *rb, int wait)
2325 struct acx_buf_data *bd;
2331 bd = &sc->sc_buf_data;
2333 m = m_getcl(wait ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2337 m->m_len = m->m_pkthdr.len = MCLBYTES;
2339 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, bd->mbuf_tmp_dmamap,
2340 m, acx_buf_dma_addr, &paddr,
2341 wait ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
2344 if_printf(&sc->sc_ic.ic_if, "can't map rx mbuf %d\n", error);
2348 /* Unload originally mapped mbuf */
2349 bus_dmamap_unload(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap);
2351 /* Swap this dmamap with tmp dmamap */
2352 map = rb->rb_mbuf_dmamap;
2353 rb->rb_mbuf_dmamap = bd->mbuf_tmp_dmamap;
2354 bd->mbuf_tmp_dmamap = map;
2357 rb->rb_desc->h_data_paddr = htole32(paddr);
2358 rb->rb_desc->h_data_len = htole16(m->m_len);
2360 bus_dmamap_sync(bd->mbuf_dma_tag, rb->rb_mbuf_dmamap,
2361 BUS_DMASYNC_PREREAD);
2366 acx_encap(struct acx_softc *sc, struct acx_txbuf *txbuf, struct mbuf *m,
2367 struct ieee80211_node *ni)
2369 struct acx_buf_data *bd = &sc->sc_buf_data;
2370 struct acx_ring_data *rd = &sc->sc_ring_data;
2375 KASSERT(txbuf->tb_mbuf == NULL, ("free TX buf has mbuf installed\n"));
2378 if (m->m_pkthdr.len > MCLBYTES) {
2379 if_printf(&sc->sc_ic.ic_if, "mbuf too big\n");
2382 } else if (m->m_pkthdr.len < ACX_FRAME_HDRLEN) {
2383 if_printf(&sc->sc_ic.ic_if, "mbuf too small\n");
2388 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2389 m, acx_buf_dma_addr, &paddr,
2391 if (error && error != EFBIG) {
2392 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf1 %d\n", error);
2396 if (error) { /* error == EFBIG */
2399 m_new = m_defrag(m, MB_DONTWAIT);
2400 if (m_new == NULL) {
2401 if_printf(&sc->sc_ic.ic_if, "can't defrag tx mbuf\n");
2408 error = bus_dmamap_load_mbuf(bd->mbuf_dma_tag,
2409 txbuf->tb_mbuf_dmamap, m,
2410 acx_buf_dma_addr, &paddr,
2413 if_printf(&sc->sc_ic.ic_if, "can't map tx mbuf2 %d\n",
2421 bus_dmamap_sync(bd->mbuf_dma_tag, txbuf->tb_mbuf_dmamap,
2422 BUS_DMASYNC_PREWRITE);
2425 txbuf->tb_node = ni;
2428 * TX buffers are accessed in following way:
2429 * acx_fw_txdesc -> acx_host_desc -> buffer
2431 * It is quite strange that acx also querys acx_host_desc next to
2432 * the one we have assigned to acx_fw_txdesc even if first one's
2433 * acx_host_desc.h_data_len == acx_fw_txdesc.f_tx_len
2435 * So we allocate two acx_host_desc for one acx_fw_txdesc and
2436 * assign the first acx_host_desc to acx_fw_txdesc
2439 * host_desc1.h_data_len = buffer_len
2440 * host_desc2.h_data_len = buffer_len - mac_header_len
2443 * host_desc1.h_data_len = mac_header_len
2444 * host_desc2.h_data_len = buffer_len - mac_header_len
2447 txbuf->tb_desc1->h_data_paddr = htole32(paddr);
2448 txbuf->tb_desc2->h_data_paddr = htole32(paddr + ACX_FRAME_HDRLEN);
2450 txbuf->tb_desc1->h_data_len =
2451 htole16(sc->chip_txdesc1_len ? sc->chip_txdesc1_len
2453 txbuf->tb_desc2->h_data_len =
2454 htole16(m->m_pkthdr.len - ACX_FRAME_HDRLEN);
2458 * We can't simply assign f_tx_ctrl, we will first read it back
2459 * and change it bit by bit
2461 ctrl = FW_TXDESC_GETFIELD_1(sc, txbuf, f_tx_ctrl);
2462 ctrl |= sc->chip_fw_txdesc_ctrl; /* extra chip specific flags */
2463 ctrl &= ~(DESC_CTRL_HOSTOWN | DESC_CTRL_ACXDONE);
2465 FW_TXDESC_SETFIELD_2(sc, txbuf, f_tx_len, m->m_pkthdr.len);
2466 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_error, 0);
2467 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_data_nretry, 0);
2468 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_nretry, 0);
2469 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_rts_ok, 0);
2470 rate = sc->chip_set_fw_txdesc_rate(sc, txbuf, ni, m->m_pkthdr.len);
2472 if (sc->sc_drvbpf != NULL) {
2473 struct ieee80211_frame_min *wh;
2475 wh = mtod(m, struct ieee80211_frame_min *);
2476 sc->sc_tx_th.wt_flags = 0;
2477 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
2478 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2479 sc->sc_tx_th.wt_rate = rate;
2481 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_tx_th, sc->sc_tx_th_len);
2484 txbuf->tb_desc1->h_ctrl = 0;
2485 txbuf->tb_desc2->h_ctrl = 0;
2486 bus_dmamap_sync(rd->tx_ring_dma_tag, rd->tx_ring_dmamap,
2487 BUS_DMASYNC_PREWRITE);
2489 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl2, 0);
2490 FW_TXDESC_SETFIELD_1(sc, txbuf, f_tx_ctrl, ctrl);
2492 /* Tell chip to inform us about TX completion */
2493 CSR_WRITE_2(sc, ACXREG_INTR_TRIG, ACXRV_TRIG_TX_FINI);
2501 acx_set_null_tmplt(struct acx_softc *sc)
2503 struct acx_tmplt_null_data n;
2504 struct ieee80211_frame *f;
2506 bzero(&n, sizeof(n));
2509 f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA |
2510 IEEE80211_FC0_SUBTYPE_NODATA;
2511 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2512 IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2513 IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2515 return _acx_set_null_data_tmplt(sc, &n, sizeof(n));
2519 acx_set_probe_req_tmplt(struct acx_softc *sc, const char *ssid, int ssid_len)
2521 struct acx_tmplt_probe_req req;
2522 struct ieee80211_frame *f;
2526 bzero(&req, sizeof(req));
2528 f = &req.data.u_data.f;
2529 f->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2530 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2531 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2532 IEEE80211_ADDR_COPY(f->i_addr2, IF_LLADDR(&sc->sc_ic.ic_if));
2533 IEEE80211_ADDR_COPY(f->i_addr3, etherbroadcastaddr);
2535 v = req.data.u_data.var;
2536 v = ieee80211_add_ssid(v, ssid, ssid_len);
2537 v = ieee80211_add_rates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2538 v = ieee80211_add_xrates(v, &sc->sc_ic.ic_sup_rates[sc->chip_phymode]);
2539 vlen = v - req.data.u_data.var;
2541 return _acx_set_probe_req_tmplt(sc, &req,
2542 ACX_TMPLT_PROBE_REQ_SIZ(vlen));
2546 acx_set_probe_resp_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2548 struct ieee80211com *ic = &sc->sc_ic;
2549 struct acx_tmplt_probe_resp resp;
2550 struct ieee80211_frame *f;
2554 m = ieee80211_probe_resp_alloc(ic, ni);
2557 DPRINTF((&ic->ic_if, "%s alloc probe resp size %d\n", __func__,
2560 f = mtod(m, struct ieee80211_frame *);
2561 IEEE80211_ADDR_COPY(f->i_addr1, etherbroadcastaddr);
2563 bzero(&resp, sizeof(resp));
2564 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t)&resp.data);
2565 len = m->m_pkthdr.len + sizeof(resp.size);
2568 return _acx_set_probe_resp_tmplt(sc, &resp, len);
2572 acx_set_beacon_tmplt(struct acx_softc *sc, struct ieee80211_node *ni)
2574 struct ieee80211com *ic = &sc->sc_ic;
2575 struct acx_tmplt_beacon beacon;
2576 struct acx_tmplt_tim tim;
2577 struct ieee80211_beacon_offsets bo;
2579 int beacon_tmplt_len = 0, tim_tmplt_len = 0;
2581 bzero(&bo, sizeof(bo));
2582 m = ieee80211_beacon_alloc(ic, ni, &bo);
2585 DPRINTF((&ic->ic_if, "%s alloc beacon size %d\n", __func__,
2588 if (bo.bo_tim_len == 0) {
2589 beacon_tmplt_len = m->m_pkthdr.len;
2591 beacon_tmplt_len = bo.bo_tim - mtod(m, uint8_t *);
2592 tim_tmplt_len = m->m_pkthdr.len - beacon_tmplt_len;
2595 bzero(&beacon, sizeof(beacon));
2596 bzero(&tim, sizeof(tim));
2598 m_copydata(m, 0, beacon_tmplt_len, (caddr_t)&beacon.data);
2599 if (tim_tmplt_len != 0) {
2600 m_copydata(m, beacon_tmplt_len, tim_tmplt_len,
2601 (caddr_t)&tim.data);
2605 beacon_tmplt_len += sizeof(beacon.size);
2606 if (_acx_set_beacon_tmplt(sc, &beacon, beacon_tmplt_len) != 0)
2609 if (tim_tmplt_len != 0) {
2610 tim_tmplt_len += sizeof(tim.size);
2611 if (_acx_set_tim_tmplt(sc, &tim, tim_tmplt_len) != 0)
2618 acx_sysctl_msdu_lifetime(SYSCTL_HANDLER_ARGS)
2620 struct acx_softc *sc = arg1;
2621 struct ifnet *ifp = &sc->sc_ic.ic_if;
2624 lwkt_serialize_enter(ifp->if_serializer);
2626 v = sc->sc_msdu_lifetime;
2627 error = sysctl_handle_int(oidp, &v, 0, req);
2628 if (error || req->newptr == NULL)
2635 if (sc->sc_flags & ACX_FLAG_FW_LOADED) {
2636 struct acx_conf_msdu_lifetime msdu_lifetime;
2638 msdu_lifetime.lifetime = htole32(v);
2639 if (acx_set_msdu_lifetime_conf(sc, &msdu_lifetime) != 0) {
2640 if_printf(&sc->sc_ic.ic_if,
2641 "can't set MSDU lifetime\n");
2646 sc->sc_msdu_lifetime = v;
2648 lwkt_serialize_exit(ifp->if_serializer);
2653 acx_sysctl_free_firmware(SYSCTL_HANDLER_ARGS)
2655 struct acx_softc *sc = arg1;
2656 struct ifnet *ifp = &sc->sc_ic.ic_if;
2659 lwkt_serialize_enter(ifp->if_serializer);
2662 error = sysctl_handle_int(oidp, &v, 0, req);
2663 if (error || req->newptr == NULL)
2665 if (v == 0) /* Do nothing */
2668 acx_free_firmware(sc);
2670 lwkt_serialize_exit(ifp->if_serializer);
2675 acx_media_change(struct ifnet *ifp)
2679 error = ieee80211_media_change(ifp);
2680 if (error != ENETRESET)
2683 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
2684 acx_init(ifp->if_softc);
2689 acx_rx_config(struct acx_softc *sc, int promisc)
2691 struct acx_conf_rxopt rx_opt;
2692 struct ieee80211com *ic = &sc->sc_ic;
2695 * What we want to receive and how to receive
2698 /* Common for all operational modes */
2699 rx_opt.opt1 = RXOPT1_INCL_RXBUF_HDR;
2700 rx_opt.opt2 = RXOPT2_RECV_ASSOC_REQ |
2702 RXOPT2_RECV_BEACON |
2707 RXOPT2_RECV_PROBE_REQ |
2708 RXOPT2_RECV_PROBE_RESP |
2711 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2712 rx_opt.opt1 |= RXOPT1_PROMISC;
2713 rx_opt.opt2 |= RXOPT2_RECV_BROKEN | RXOPT2_RECV_ACK;
2715 rx_opt.opt1 |= promisc ? RXOPT1_PROMISC : RXOPT1_FILT_FDEST;
2718 if (acx_set_rxopt_conf(sc, &rx_opt) != 0) {
2719 if_printf(&sc->sc_ic.ic_if, "can't config RX\n");
2726 acx_set_chan(struct acx_softc *sc, struct ieee80211_channel *c)
2728 struct ieee80211com *ic = &sc->sc_ic;
2732 chan = ieee80211_chan2ieee(ic, c);
2733 DPRINTF((&ic->ic_if, "to chan %u\n", chan));
2734 if (acx_enable_txchan(sc, chan) != 0) {
2735 if_printf(&ic->ic_if, "enable TX on channel %d failed\n", chan);
2738 if (acx_enable_rxchan(sc, chan) != 0) {
2739 if_printf(&ic->ic_if, "enable RX on channel %d failed\n", chan);
2743 if (IEEE80211_IS_CHAN_G(c))
2744 flags = IEEE80211_CHAN_G;
2746 flags = IEEE80211_CHAN_B;
2748 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2749 htole16(c->ic_freq);
2750 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =