Do not use ShortGI and STBC with management frames; Atheros cards have problems with...
[ralink_drivers/rt2860_fbsd8.git] / rt2860.c
blobc026527ad096254e91c756d5e2c0552033b4780b
2 /*-
3 * Copyright (c) 2009-2010 Alexander Egorenkov <egorenar@gmail.com>
4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "rt2860_softc.h"
20 #include "rt2860_reg.h"
21 #include "rt2860_eeprom.h"
22 #include "rt2860_ucode.h"
23 #include "rt2860_txwi.h"
24 #include "rt2860_rxwi.h"
25 #include "rt2860_io.h"
26 #include "rt2860_read_eeprom.h"
27 #include "rt2860_led.h"
28 #include "rt2860_rf.h"
29 #include "rt2860_debug.h"
32 * Defines and macros
35 #define PCI_VENDOR_RALINK 0x1814
36 #define PCI_PRODUCT_RALINK_RT2860_PCI 0x0601
37 #define PCI_PRODUCT_RALINK_RT2860_PCIe 0x0681
38 #define PCI_PRODUCT_RALINK_RT2760_PCI 0x0701
39 #define PCI_PRODUCT_RALINK_RT2790_PCIe 0x0781
41 #define RT2860_MAX_AGG_SIZE 3840
43 #define RT2860_TX_DATA_SEG0_SIZE \
44 (sizeof(struct rt2860_txwi) + sizeof(struct ieee80211_qosframe_addr4))
46 #define RT2860_NOISE_FLOOR -95
48 #define IEEE80211_HAS_ADDR4(wh) \
49 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
51 #define RT2860_MS(_v, _f) (((_v) & _f) >> _f##_S)
52 #define RT2860_SM(_v, _f) (((_v) << _f##_S) & _f)
54 #define RT2860_TX_WATCHDOG_TIMEOUT 5
56 #define RT2860_WCID_RESERVED 0xff
57 #define RT2860_WCID_MCAST 0xf7
60 * Data structures and types
63 struct rt2860_pci_ident
65 uint16_t vendor;
66 uint16_t device;
67 const char *name;
71 * Static function prototypes
74 static int rt2860_probe(device_t dev);
76 static int rt2860_attach(device_t dev);
78 static int rt2860_detach(device_t dev);
80 static int rt2860_shutdown(device_t dev);
82 static int rt2860_suspend(device_t dev);
84 static int rt2860_resume(device_t dev);
86 static void rt2860_init_channels(struct rt2860_softc *sc);
88 static void rt2860_init_channels_ht40(struct rt2860_softc *sc);
90 static void rt2860_init_locked(void *priv);
92 static void rt2860_init(void *priv);
94 static int rt2860_init_bbp(struct rt2860_softc *sc);
96 static void rt2860_stop_locked(void *priv);
98 static void rt2860_stop(void *priv);
100 static void rt2860_start(struct ifnet *ifp);
102 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
104 static struct ieee80211vap *rt2860_vap_create(struct ieee80211com *ic,
105 const char name[IFNAMSIZ], int unit, int opmode, int flags,
106 const uint8_t bssid[IEEE80211_ADDR_LEN],
107 const uint8_t mac[IEEE80211_ADDR_LEN]);
109 static void rt2860_vap_delete(struct ieee80211vap *vap);
111 static int rt2860_vap_reset(struct ieee80211vap *vap, u_long cmd);
113 static int rt2860_vap_newstate(struct ieee80211vap *vap,
114 enum ieee80211_state nstate, int arg);
116 static void rt2860_vap_key_update_begin(struct ieee80211vap *vap);
118 static void rt2860_vap_key_update_end(struct ieee80211vap *vap);
120 static int rt2860_vap_key_set(struct ieee80211vap *vap,
121 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN]);
123 static int rt2860_vap_key_delete(struct ieee80211vap *vap,
124 const struct ieee80211_key *k);
126 static void rt2860_vap_update_beacon(struct ieee80211vap *vap, int what);
128 static int rt2860_media_change(struct ifnet *ifp);
130 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211vap *vap,
131 const uint8_t mac[IEEE80211_ADDR_LEN]);
133 static void rt2860_node_cleanup(struct ieee80211_node *ni);
135 static void rt2860_node_getmimoinfo(const struct ieee80211_node *ni,
136 struct ieee80211_mimo_info *mi);
138 static int rt2860_setregdomain(struct ieee80211com *ic,
139 struct ieee80211_regdomain *reg,
140 int nchans, struct ieee80211_channel chans[]);
142 static void rt2860_getradiocaps(struct ieee80211com *ic,
143 int maxchans, int *nchans, struct ieee80211_channel chans[]);
145 static void rt2860_scan_start(struct ieee80211com *ic);
147 static void rt2860_scan_end(struct ieee80211com *ic);
149 static void rt2860_set_channel(struct ieee80211com *ic);
151 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew);
153 static void rt2860_updateslot(struct ifnet *ifp);
155 static void rt2860_update_promisc(struct ifnet *ifp);
157 static void rt2860_update_mcast(struct ifnet *ifp);
159 static int rt2860_wme_update(struct ieee80211com *ic);
161 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
162 const struct ieee80211_bpf_params *params);
164 static int rt2860_recv_action(struct ieee80211_node *ni,
165 const struct ieee80211_frame *wh,
166 const uint8_t *frm, const uint8_t *efrm);
168 static int rt2860_send_action(struct ieee80211_node *ni,
169 int cat, int act, void *sa);
171 static int rt2860_addba_response(struct ieee80211_node *ni,
172 struct ieee80211_tx_ampdu *tap,
173 int status, int baparamset, int batimeout);
175 static void rt2860_addba_stop(struct ieee80211_node *ni,
176 struct ieee80211_tx_ampdu *tap);
178 static int rt2860_ampdu_rx_start(struct ieee80211_node *ni,
179 struct ieee80211_rx_ampdu *rap,
180 int baparamset, int batimeout, int baseqctl);
182 static void rt2860_ampdu_rx_stop(struct ieee80211_node *ni,
183 struct ieee80211_rx_ampdu *rap);
185 static int rt2860_send_bar(struct ieee80211_node *ni,
186 struct ieee80211_tx_ampdu *tap, ieee80211_seq seqno);
188 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
190 static void rt2860_periodic(void *arg);
192 static void rt2860_tx_watchdog(void *arg);
194 static int rt2860_staid_alloc(struct rt2860_softc *sc, int aid);
196 static void rt2860_staid_delete(struct rt2860_softc *sc, int staid);
198 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
199 const uint8_t *bssid);
201 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
202 const uint8_t *addr);
204 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc);
206 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc);
208 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc);
210 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc);
212 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc);
214 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc);
216 static void rt2860_asic_update_txpower(struct rt2860_softc *sc);
218 static void rt2860_asic_update_promisc(struct rt2860_softc *sc);
220 static void rt2860_asic_updateprot(struct rt2860_softc *sc);
222 static void rt2860_asic_updateslot(struct rt2860_softc *sc);
224 static void rt2860_asic_wme_update(struct rt2860_softc *sc);
226 static void rt2860_asic_update_beacon(struct rt2860_softc *sc,
227 struct ieee80211vap *vap);
229 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc);
231 static void rt2860_asic_add_ba_session(struct rt2860_softc *sc,
232 uint8_t wcid, int tid);
234 static void rt2860_asic_del_ba_session(struct rt2860_softc *sc,
235 uint8_t wcid, int tid);
237 static int rt2860_beacon_alloc(struct rt2860_softc *sc,
238 struct ieee80211vap *vap);
240 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi);
242 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
243 const struct rt2860_rxwi *rxwi);
245 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
246 uint8_t rssi, uint8_t rxpath);
248 static uint8_t rt2860_rate2mcs(uint8_t rate);
250 static int rt2860_tx_mgmt(struct rt2860_softc *sc,
251 struct mbuf *m, struct ieee80211_node *ni, int qid);
253 static int rt2860_tx_data(struct rt2860_softc *sc,
254 struct mbuf *m, struct ieee80211_node *ni, int qid);
256 static int rt2860_tx_raw(struct rt2860_softc *sc,
257 struct mbuf *m, struct ieee80211_node *ni,
258 const struct ieee80211_bpf_params *params);
260 static void rt2860_intr(void *arg);
262 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc);
264 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc);
266 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc);
268 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc);
270 static void rt2860_rx_intr(struct rt2860_softc *sc);
272 static void rt2860_rx_delay_intr(struct rt2860_softc *sc);
274 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid);
276 static void rt2860_tx_delay_intr(struct rt2860_softc *sc);
278 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc);
280 static void rt2860_tbtt_intr(struct rt2860_softc *sc);
282 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc);
284 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc);
286 static void rt2860_gp_timer_intr(struct rt2860_softc *sc);
288 static void rt2860_rx_done_task(void *context, int pending);
290 static void rt2860_tx_done_task(void *context, int pending);
292 static void rt2860_fifo_sta_full_task(void *context, int pending);
294 static void rt2860_periodic_task(void *context, int pending);
296 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit);
298 static void rt2860_tx_eof(struct rt2860_softc *sc,
299 struct rt2860_softc_tx_ring *ring);
301 static void rt2860_update_stats(struct rt2860_softc *sc);
303 static void rt2860_bbp_tuning(struct rt2860_softc *sc);
305 static void rt2860_watchdog(struct rt2860_softc *sc);
307 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc);
309 static void rt2860_update_raw_counters(struct rt2860_softc *sc);
311 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask);
313 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask);
315 static int rt2860_txrx_enable(struct rt2860_softc *sc);
317 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
318 struct rt2860_softc_rx_ring *ring);
320 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
321 struct rt2860_softc_rx_ring *ring);
323 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
324 struct rt2860_softc_rx_ring *ring);
326 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
327 struct rt2860_softc_tx_ring *ring, int qid);
329 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
330 struct rt2860_softc_tx_ring *ring);
332 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
333 struct rt2860_softc_tx_ring *ring);
335 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
336 int nseg, int error);
338 static void rt2860_sysctl_attach(struct rt2860_softc *sc);
341 * Static variables
344 static const struct rt2860_pci_ident rt2860_pci_ids[] =
346 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCI, "Ralink RT2860 PCI" },
347 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCIe, "Ralink RT2860 PCIe" },
348 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760_PCI, "Ralink RT2760 PCI" },
349 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790_PCIe, "Ralink RT2790 PCIe" },
350 { 0, 0, NULL }
353 static const struct
355 uint32_t reg;
356 uint32_t val;
357 } rt2860_def_mac[] =
359 { RT2860_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
360 { RT2860_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
361 { RT2860_REG_LEGACY_BASIC_RATE, 0x0000013f },
362 { RT2860_REG_HT_BASIC_RATE, 0x00008003 },
363 { RT2860_REG_SYS_CTRL, 0x00000000 },
364 { RT2860_REG_RX_FILTER_CFG, 0x00017f97 },
365 { RT2860_REG_BKOFF_SLOT_CFG, 0x00000209 },
366 { RT2860_REG_TX_SW_CFG0, 0x00000000 },
367 { RT2860_REG_TX_SW_CFG1, 0x00080606 },
368 { RT2860_REG_TX_LINK_CFG, 0x00001020 },
369 { RT2860_REG_TX_TIMEOUT_CFG, 0x000a2090 },
370 { RT2860_REG_MAX_LEN_CFG, (1 << 12) | RT2860_MAX_AGG_SIZE },
371 { RT2860_REG_LED_CFG, 0x7f031e46 },
372 { RT2860_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
373 { RT2860_REG_TX_RTY_CFG, 0x47d01f0f },
374 { RT2860_REG_AUTO_RSP_CFG, 0x00000013 },
375 { RT2860_REG_TX_CCK_PROT_CFG, 0x05740003 },
376 { RT2860_REG_TX_OFDM_PROT_CFG, 0x05740003 },
377 { RT2860_REG_TX_GF20_PROT_CFG, 0x01744004 },
378 { RT2860_REG_TX_GF40_PROT_CFG, 0x03f44084 },
379 { RT2860_REG_TX_MM20_PROT_CFG, 0x01744004 },
380 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f54084 },
381 { RT2860_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
382 { RT2860_REG_TX_RTS_CFG, 0x00092b20 },
383 { RT2860_REG_TX_EXP_ACK_TIME, 0x002400ca },
384 { RT2860_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
385 { RT2860_REG_XIFS_TIME_CFG, 0x33a41010 },
386 { RT2860_REG_PWR_PIN_CFG, 0x00000003 },
387 { RT2860_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
388 { RT2860_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
389 { RT2860_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
392 #define RT2860_DEF_MAC_SIZE (sizeof(rt2860_def_mac) / sizeof(rt2860_def_mac[0]))
394 static const struct
396 uint8_t reg;
397 uint8_t val;
398 } rt2860_def_bbp[] =
400 { 65, 0x2c },
401 { 66, 0x38 },
402 { 69, 0x12 },
403 { 70, 0x0a },
404 { 73, 0x10 },
405 { 81, 0x37 },
406 { 82, 0x62 },
407 { 83, 0x6a },
408 { 84, 0x99 },
409 { 86, 0x00 },
410 { 91, 0x04 },
411 { 92, 0x00 },
412 { 103, 0x00 },
413 { 105, 0x05 },
414 { 106, 0x35 },
417 #define RT2860_DEF_BBP_SIZE (sizeof(rt2860_def_bbp) / sizeof(rt2860_def_bbp[0]))
419 SYSCTL_NODE(_hw, OID_AUTO, rt2860, CTLFLAG_RD, 0, "RT2860 driver parameters");
421 static int rt2860_tx_stbc = 1;
422 SYSCTL_INT(_hw_rt2860, OID_AUTO, tx_stbc, CTLFLAG_RW, &rt2860_tx_stbc, 0, "RT2860 Tx STBC");
423 TUNABLE_INT("hw.rt2860.tx_stbc", &rt2860_tx_stbc);
425 #ifdef RT2860_DEBUG
426 static int rt2860_debug = 0;
427 SYSCTL_INT(_hw_rt2860, OID_AUTO, debug, CTLFLAG_RW, &rt2860_debug, 0, "RT2860 debug level");
428 TUNABLE_INT("hw.rt2860.debug", &rt2860_debug);
429 #endif
432 * rt2860_probe
434 static int rt2860_probe(device_t dev)
436 const struct rt2860_pci_ident *ident;
438 for (ident = rt2860_pci_ids; ident->name != NULL; ident++)
440 if (pci_get_vendor(dev) == ident->vendor &&
441 pci_get_device(dev) == ident->device)
443 device_set_desc(dev, ident->name);
444 return 0;
448 return ENXIO;
452 * rt2860_attach
454 static int rt2860_attach(device_t dev)
456 struct rt2860_softc *sc;
457 struct ifnet *ifp;
458 struct ieee80211com *ic;
459 int error, ntries, i;
461 sc = device_get_softc(dev);
463 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
465 printf("%s: chip is in D%d power mode, setting to D0\n",
466 device_get_nameunit(dev), pci_get_powerstate(dev));
467 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
470 /* enable bus-mastering */
472 pci_enable_busmaster(dev);
474 sc->dev = dev;
476 mtx_init(&sc->lock, device_get_nameunit(dev),
477 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
479 sc->mem_rid = PCIR_BAR(0);
480 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
481 &sc->mem_rid, RF_ACTIVE);
482 if (sc->mem == NULL)
484 printf("%s: could not allocate memory resource\n",
485 device_get_nameunit(dev));
486 error = ENXIO;
487 goto fail;
490 sc->bst = rman_get_bustag(sc->mem);
491 sc->bsh = rman_get_bushandle(sc->mem);
493 sc->irq_rid = 0;
494 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
495 &sc->irq_rid, RF_ACTIVE | RF_SHAREABLE);
496 if (sc->irq == NULL)
498 printf("%s: could not allocate interrupt resource\n",
499 device_get_nameunit(dev));
500 error = ENXIO;
501 goto fail;
504 sc->tx_stbc = rt2860_tx_stbc;
506 #ifdef RT2860_DEBUG
507 sc->debug = rt2860_debug;
509 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
510 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
511 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2860 debug level");
512 #endif
514 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
515 "%s: attaching\n",
516 device_get_nameunit(sc->dev));
518 /* wait for NIC to initialize */
520 for (ntries = 0; ntries < 100; ntries++)
522 sc->mac_rev = rt2860_io_mac_read(sc, RT2860_REG_MAC_CSR0);
523 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
524 break;
526 DELAY(10);
529 if (ntries == 100)
531 printf("%s: timeout waiting for NIC to initialize\n",
532 device_get_nameunit(dev));
533 error = EIO;
534 goto fail;
537 rt2860_read_eeprom(sc);
539 printf("%s: MAC/BBP RT2860 (rev 0x%08x), RF %s\n",
540 device_get_nameunit(sc->dev), sc->mac_rev,
541 rt2860_rf_name(sc->rf_rev));
543 /* clear key tables */
545 rt2860_asic_clear_keytables(sc);
547 /* allocate Tx and Rx rings */
549 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
551 error = rt2860_alloc_tx_ring(sc, &sc->tx_ring[i], i);
552 if (error != 0)
554 printf("%s: could not allocate Tx ring #%d\n",
555 device_get_nameunit(sc->dev), i);
556 goto fail;
560 sc->tx_ring_mgtqid = 5;
562 error = rt2860_alloc_rx_ring(sc, &sc->rx_ring);
563 if (error != 0)
565 printf("%s: could not allocate Rx ring\n",
566 device_get_nameunit(sc->dev));
567 goto fail;
570 callout_init(&sc->periodic_ch, 0);
571 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
573 ifp = sc->ifp = if_alloc(IFT_IEEE80211);
574 if (ifp == NULL)
576 printf("%s: could not if_alloc()\n",
577 device_get_nameunit(sc->dev));
578 error = ENOMEM;
579 goto fail;
582 ifp->if_softc = sc;
584 if_initname(ifp, "rt2860", device_get_unit(sc->dev));
586 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
588 ifp->if_init = rt2860_init;
589 ifp->if_ioctl = rt2860_ioctl;
590 ifp->if_start = rt2860_start;
592 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
593 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
594 IFQ_SET_READY(&ifp->if_snd);
596 ic = ifp->if_l2com;
598 ic->ic_ifp = ifp;
600 ic->ic_phytype = IEEE80211_T_HT;
601 ic->ic_opmode = IEEE80211_M_STA;
603 ic->ic_caps = IEEE80211_C_MONITOR |
604 IEEE80211_C_IBSS |
605 IEEE80211_C_STA |
606 IEEE80211_C_AHDEMO |
607 IEEE80211_C_HOSTAP |
608 IEEE80211_C_WDS |
609 IEEE80211_C_MBSS |
610 IEEE80211_C_BGSCAN |
611 IEEE80211_C_TXPMGT |
612 IEEE80211_C_SHPREAMBLE |
613 IEEE80211_C_SHSLOT |
614 IEEE80211_C_TXFRAG |
615 IEEE80211_C_BURST |
616 IEEE80211_C_WME |
617 IEEE80211_C_WPA;
619 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP |
620 IEEE80211_CRYPTO_TKIP |
621 IEEE80211_CRYPTO_TKIPMIC |
622 IEEE80211_CRYPTO_AES_CCM;
624 ic->ic_htcaps = IEEE80211_HTC_HT |
625 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
626 IEEE80211_HTC_AMPDU | /* A-MPDU Tx */
627 IEEE80211_HTC_SMPS | /* MIMO power save */
628 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
629 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
630 IEEE80211_HTCAP_GREENFIELD | /* HT greenfield */
631 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
632 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
633 IEEE80211_HTCAP_SMPS_OFF; /* MIMO power save disabled */
635 /* spatial streams */
637 if (sc->nrxpath == 2)
638 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
639 else if (sc->nrxpath == 3)
640 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
641 else
642 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
644 if (sc->ntxpath > 1)
645 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
647 /* delayed BA */
649 if (sc->mac_rev != 0x28600100)
650 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
652 /* init channels */
654 ic->ic_nchans = 0;
656 rt2860_init_channels(sc);
658 rt2860_init_channels_ht40(sc);
660 ieee80211_ifattach(ic, sc->mac_addr);
662 ic->ic_vap_create = rt2860_vap_create;
663 ic->ic_vap_delete = rt2860_vap_delete;
665 ic->ic_node_alloc = rt2860_node_alloc;
667 sc->node_cleanup = ic->ic_node_cleanup;
668 ic->ic_node_cleanup = rt2860_node_cleanup;
670 ic->ic_node_getmimoinfo = rt2860_node_getmimoinfo;
671 ic->ic_setregdomain = rt2860_setregdomain;
672 ic->ic_getradiocaps = rt2860_getradiocaps;
673 ic->ic_scan_start = rt2860_scan_start;
674 ic->ic_scan_end = rt2860_scan_end;
675 ic->ic_set_channel = rt2860_set_channel;
676 ic->ic_newassoc = rt2860_newassoc;
677 ic->ic_updateslot = rt2860_updateslot;
678 ic->ic_update_promisc = rt2860_update_promisc;
679 ic->ic_update_mcast = rt2860_update_mcast;
680 ic->ic_wme.wme_update = rt2860_wme_update;
681 ic->ic_raw_xmit = rt2860_raw_xmit;
683 sc->recv_action = ic->ic_recv_action;
684 ic->ic_recv_action = rt2860_recv_action;
686 sc->send_action = ic->ic_send_action;
687 ic->ic_send_action = rt2860_send_action;
689 sc->addba_response = ic->ic_addba_response;
690 ic->ic_addba_response = rt2860_addba_response;
692 sc->addba_stop = ic->ic_addba_stop;
693 ic->ic_addba_stop = rt2860_addba_stop;
695 sc->ampdu_rx_start = ic->ic_ampdu_rx_start;
696 ic->ic_ampdu_rx_start = rt2860_ampdu_rx_start;
698 sc->ampdu_rx_stop = ic->ic_ampdu_rx_stop;
699 ic->ic_ampdu_rx_stop = rt2860_ampdu_rx_stop;
701 /* hardware requires padding between 802.11 frame header and body */
703 ic->ic_flags |= IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
705 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
707 ieee80211_radiotap_attach(ic,
708 &sc->txtap.ihdr, sizeof(sc->txtap),
709 RT2860_SOFTC_TX_RADIOTAP_PRESENT,
710 &sc->rxtap.ihdr, sizeof(sc->rxtap),
711 RT2860_SOFTC_RX_RADIOTAP_PRESENT);
713 /* init task queue */
715 TASK_INIT(&sc->rx_done_task, 0, rt2860_rx_done_task, sc);
716 TASK_INIT(&sc->tx_done_task, 0, rt2860_tx_done_task, sc);
717 TASK_INIT(&sc->fifo_sta_full_task, 0, rt2860_fifo_sta_full_task, sc);
718 TASK_INIT(&sc->periodic_task, 0, rt2860_periodic_task, sc);
720 sc->rx_process_limit = 100;
722 sc->taskqueue = taskqueue_create("rt2860_taskq", M_NOWAIT,
723 taskqueue_thread_enqueue, &sc->taskqueue);
725 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
726 device_get_nameunit(sc->dev));
728 rt2860_sysctl_attach(sc);
730 if (bootverbose)
731 ieee80211_announce(ic);
733 /* set up interrupt */
735 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
736 NULL, rt2860_intr, sc, &sc->irqh);
737 if (error != 0)
739 printf("%s: could not set up interrupt\n",
740 device_get_nameunit(dev));
741 goto fail;
744 return 0;
746 fail:
748 /* free Tx and Rx rings */
750 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
751 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
753 rt2860_free_rx_ring(sc, &sc->rx_ring);
755 mtx_destroy(&sc->lock);
757 if (sc->mem != NULL)
758 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
760 if (sc->irq != NULL)
761 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
763 return error;
767 * rt2860_detach
769 static int rt2860_detach(device_t dev)
771 struct rt2860_softc *sc;
772 struct ifnet *ifp;
773 struct ieee80211com *ic;
774 int i;
776 sc = device_get_softc(dev);
777 ifp = sc->ifp;
778 ic = ifp->if_l2com;
780 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
781 "%s: detaching\n",
782 device_get_nameunit(sc->dev));
784 RT2860_SOFTC_LOCK(sc);
786 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
788 callout_stop(&sc->periodic_ch);
789 callout_stop(&sc->tx_watchdog_ch);
791 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
792 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
793 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
794 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
796 /* free Tx and Rx rings */
798 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
799 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
801 rt2860_free_rx_ring(sc, &sc->rx_ring);
803 RT2860_SOFTC_UNLOCK(sc);
805 ieee80211_ifdetach(ic);
807 if_free(ifp);
809 taskqueue_free(sc->taskqueue);
811 mtx_destroy(&sc->lock);
813 bus_generic_detach(dev);
815 bus_teardown_intr(dev, sc->irq, sc->irqh);
817 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
819 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
821 return 0;
825 * rt2860_shutdown
827 static int rt2860_shutdown(device_t dev)
829 struct rt2860_softc *sc;
831 sc = device_get_softc(dev);
833 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
834 "%s: shutting down\n",
835 device_get_nameunit(sc->dev));
837 rt2860_stop(sc);
839 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
841 return 0;
845 * rt2860_suspend
847 static int rt2860_suspend(device_t dev)
849 struct rt2860_softc *sc;
851 sc = device_get_softc(dev);
853 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
854 "%s: suspending\n",
855 device_get_nameunit(sc->dev));
857 rt2860_stop(sc);
859 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
861 return 0;
865 * rt2860_resume
867 static int rt2860_resume(device_t dev)
869 struct rt2860_softc *sc;
870 struct ifnet *ifp;
872 sc = device_get_softc(dev);
873 ifp = sc->ifp;
875 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
876 "%s: resuming\n",
877 device_get_nameunit(sc->dev));
879 if (ifp->if_flags & IFF_UP)
880 rt2860_init(sc);
882 return 0;
886 * rt2860_init_channels
888 static void rt2860_init_channels(struct rt2860_softc *sc)
890 struct ifnet *ifp;
891 struct ieee80211com *ic;
892 struct ieee80211_channel *c;
893 int i, flags;
895 ifp = sc->ifp;
896 ic = ifp->if_l2com;
898 /* set supported channels for 2GHz band */
900 for (i = 1; i <= 14; i++)
902 c = &ic->ic_channels[ic->ic_nchans++];
903 flags = IEEE80211_CHAN_B;
905 c->ic_freq = ieee80211_ieee2mhz(i, flags);
906 c->ic_ieee = i;
907 c->ic_flags = flags;
909 c = &ic->ic_channels[ic->ic_nchans++];
910 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
912 c->ic_freq = ieee80211_ieee2mhz(i, flags);
913 c->ic_ieee = i;
914 c->ic_flags = flags;
916 c = &ic->ic_channels[ic->ic_nchans++];
917 flags = IEEE80211_CHAN_G;
919 c->ic_freq = ieee80211_ieee2mhz(i, flags);
920 c->ic_ieee = i;
921 c->ic_flags = flags;
923 c = &ic->ic_channels[ic->ic_nchans++];
924 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
926 c->ic_freq = ieee80211_ieee2mhz(i, flags);
927 c->ic_ieee = i;
928 c->ic_flags = flags;
931 /* set supported channels for 5GHz band */
933 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
934 sc->rf_rev == RT2860_EEPROM_RF_2750)
936 for (i = 36; i <= 64; i += 4)
938 c = &ic->ic_channels[ic->ic_nchans++];
939 flags = IEEE80211_CHAN_A;
941 c->ic_freq = ieee80211_ieee2mhz(i, flags);
942 c->ic_ieee = i;
943 c->ic_flags = flags;
945 c = &ic->ic_channels[ic->ic_nchans++];
946 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
948 c->ic_freq = ieee80211_ieee2mhz(i, flags);
949 c->ic_ieee = i;
950 c->ic_flags = flags;
953 for (i = 100; i <= 140; i += 4)
955 c = &ic->ic_channels[ic->ic_nchans++];
956 flags = IEEE80211_CHAN_A;
958 c->ic_freq = ieee80211_ieee2mhz(i, flags);
959 c->ic_ieee = i;
960 c->ic_flags = flags;
962 c = &ic->ic_channels[ic->ic_nchans++];
963 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
965 c->ic_freq = ieee80211_ieee2mhz(i, flags);
966 c->ic_ieee = i;
967 c->ic_flags = flags;
970 for (i = 149; i <= 165; i += 4)
972 c = &ic->ic_channels[ic->ic_nchans++];
973 flags = IEEE80211_CHAN_A;
975 c->ic_freq = ieee80211_ieee2mhz(i, flags);
976 c->ic_ieee = i;
977 c->ic_flags = flags;
979 c = &ic->ic_channels[ic->ic_nchans++];
980 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
982 c->ic_freq = ieee80211_ieee2mhz(i, flags);
983 c->ic_ieee = i;
984 c->ic_flags = flags;
990 * rt2860_init_channels_ht40
992 static void rt2860_init_channels_ht40(struct rt2860_softc *sc)
994 struct ifnet *ifp;
995 struct ieee80211com *ic;
996 struct ieee80211_channel *c, *cent, *ext;
997 int i, flags;
999 ifp = sc->ifp;
1000 ic = ifp->if_l2com;
1002 /* set supported channels for 2GHz band */
1004 for (i = 1; i <= 14; i++)
1006 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
1008 /* find the center channel */
1010 cent = ieee80211_find_channel_byieee(ic, i,
1011 flags & ~IEEE80211_CHAN_HT);
1012 if (cent == NULL)
1014 printf("%s: skip channel %d, could not find center channel\n",
1015 device_get_nameunit(sc->dev), i);
1016 continue;
1019 /* find the extension channel */
1021 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1022 flags & ~IEEE80211_CHAN_HT);
1023 if (ext == NULL)
1025 printf("%s: skip channel %d, could not find extension channel\n",
1026 device_get_nameunit(sc->dev), i);
1027 continue;
1030 c = &ic->ic_channels[ic->ic_nchans++];
1032 *c = *cent;
1033 c->ic_extieee = ext->ic_ieee;
1034 c->ic_flags &= ~IEEE80211_CHAN_HT;
1035 c->ic_flags |= IEEE80211_CHAN_HT40U;
1037 c = &ic->ic_channels[ic->ic_nchans++];
1039 *c = *ext;
1040 c->ic_extieee = cent->ic_ieee;
1041 c->ic_flags &= ~IEEE80211_CHAN_HT;
1042 c->ic_flags |= IEEE80211_CHAN_HT40D;
1045 /* set supported channels for 5GHz band */
1047 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
1048 sc->rf_rev == RT2860_EEPROM_RF_2750)
1050 for (i = 36; i <= 64; i += 4)
1052 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1054 /* find the center channel */
1056 cent = ieee80211_find_channel_byieee(ic, i,
1057 flags & ~IEEE80211_CHAN_HT);
1058 if (cent == NULL)
1060 printf("%s: skip channel %d, could not find center channel\n",
1061 device_get_nameunit(sc->dev), i);
1062 continue;
1065 /* find the extension channel */
1067 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1068 flags & ~IEEE80211_CHAN_HT);
1069 if (ext == NULL)
1071 printf("%s: skip channel %d, could not find extension channel\n",
1072 device_get_nameunit(sc->dev), i);
1073 continue;
1076 c = &ic->ic_channels[ic->ic_nchans++];
1078 *c = *cent;
1079 c->ic_extieee = ext->ic_ieee;
1080 c->ic_flags &= ~IEEE80211_CHAN_HT;
1081 c->ic_flags |= IEEE80211_CHAN_HT40U;
1083 c = &ic->ic_channels[ic->ic_nchans++];
1085 *c = *ext;
1086 c->ic_extieee = cent->ic_ieee;
1087 c->ic_flags &= ~IEEE80211_CHAN_HT;
1088 c->ic_flags |= IEEE80211_CHAN_HT40D;
1091 for (i = 100; i <= 140; i += 4)
1093 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1095 /* find the center channel */
1097 cent = ieee80211_find_channel_byieee(ic, i,
1098 flags & ~IEEE80211_CHAN_HT);
1099 if (cent == NULL)
1101 printf("%s: skip channel %d, could not find center channel\n",
1102 device_get_nameunit(sc->dev), i);
1103 continue;
1106 /* find the extension channel */
1108 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1109 flags & ~IEEE80211_CHAN_HT);
1110 if (ext == NULL)
1112 printf("%s: skip channel %d, could not find extension channel\n",
1113 device_get_nameunit(sc->dev), i);
1114 continue;
1117 c = &ic->ic_channels[ic->ic_nchans++];
1119 *c = *cent;
1120 c->ic_extieee = ext->ic_ieee;
1121 c->ic_flags &= ~IEEE80211_CHAN_HT;
1122 c->ic_flags |= IEEE80211_CHAN_HT40U;
1124 c = &ic->ic_channels[ic->ic_nchans++];
1126 *c = *ext;
1127 c->ic_extieee = cent->ic_ieee;
1128 c->ic_flags &= ~IEEE80211_CHAN_HT;
1129 c->ic_flags |= IEEE80211_CHAN_HT40D;
1132 for (i = 149; i <= 165; i += 4)
1134 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1136 /* find the center channel */
1138 cent = ieee80211_find_channel_byieee(ic, i,
1139 flags & ~IEEE80211_CHAN_HT);
1140 if (cent == NULL)
1142 printf("%s: skip channel %d, could not find center channel\n",
1143 device_get_nameunit(sc->dev), i);
1144 continue;
1147 /* find the extension channel */
1149 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1150 flags & ~IEEE80211_CHAN_HT);
1151 if (ext == NULL)
1153 printf("%s: skip channel %d, could not find extension channel\n",
1154 device_get_nameunit(sc->dev), i);
1155 continue;
1158 c = &ic->ic_channels[ic->ic_nchans++];
1160 *c = *cent;
1161 c->ic_extieee = ext->ic_ieee;
1162 c->ic_flags &= ~IEEE80211_CHAN_HT;
1163 c->ic_flags |= IEEE80211_CHAN_HT40U;
1165 c = &ic->ic_channels[ic->ic_nchans++];
1167 *c = *ext;
1168 c->ic_extieee = cent->ic_ieee;
1169 c->ic_flags &= ~IEEE80211_CHAN_HT;
1170 c->ic_flags |= IEEE80211_CHAN_HT40D;
1176 * rt2860_init_locked
1178 static void rt2860_init_locked(void *priv)
1180 struct rt2860_softc *sc;
1181 struct ifnet *ifp;
1182 struct ieee80211com *ic;
1183 int error, i, ntries;
1184 uint32_t tmp, stacnt[6];
1186 sc = priv;
1187 ifp = sc->ifp;
1188 ic = ifp->if_l2com;
1190 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1191 "%s: initializing\n",
1192 device_get_nameunit(sc->dev));
1194 RT2860_SOFTC_ASSERT_LOCKED(sc);
1196 if (!(sc->flags & RT2860_SOFTC_FLAGS_UCODE_LOADED))
1198 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1199 "%s: loading 8051 microcode\n",
1200 device_get_nameunit(sc->dev));
1202 error = rt2860_io_mcu_load_ucode(sc, rt2860_ucode, sizeof(rt2860_ucode));
1203 if (error != 0)
1205 printf("%s: could not load 8051 microcode\n",
1206 device_get_nameunit(sc->dev));
1207 goto fail;
1210 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1211 "%s: 8051 microcode was successfully loaded\n",
1212 device_get_nameunit(sc->dev));
1214 sc->flags |= RT2860_SOFTC_FLAGS_UCODE_LOADED;
1217 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x2);
1219 /* disable DMA engine */
1221 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1223 tmp &= 0xff0;
1224 tmp |= RT2860_REG_TX_WB_DDONE;
1226 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1228 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, 0xffffffff);
1230 /* PBF hardware reset */
1232 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1233 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1235 /* wait while DMA engine is busy */
1237 for (ntries = 0; ntries < 100; ntries++)
1239 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1240 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1241 break;
1243 DELAY(1000);
1246 if (ntries == 100)
1248 printf("%s: timeout waiting for DMA engine\n",
1249 device_get_nameunit(sc->dev));
1250 goto fail;
1253 tmp &= 0xff0;
1254 tmp |= RT2860_REG_TX_WB_DDONE;
1256 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1258 /* reset Rx and Tx rings */
1260 tmp = RT2860_REG_RST_IDX_RX |
1261 RT2860_REG_RST_IDX_TX_MGMT |
1262 RT2860_REG_RST_IDX_TX_HCCA |
1263 RT2860_REG_RST_IDX_TX_AC3 |
1264 RT2860_REG_RST_IDX_TX_AC2 |
1265 RT2860_REG_RST_IDX_TX_AC1 |
1266 RT2860_REG_RST_IDX_TX_AC0;
1268 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, tmp);
1270 /* PBF hardware reset */
1272 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1273 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1275 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x3);
1277 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1278 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1279 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1281 /* init Tx power per rate */
1283 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
1285 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1286 continue;
1288 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i),
1289 sc->txpow_rate_20mhz[i]);
1292 for (i = 0; i < RT2860_DEF_MAC_SIZE; i++)
1293 rt2860_io_mac_write(sc, rt2860_def_mac[i].reg,
1294 rt2860_def_mac[i].val);
1296 /* wait while MAC is busy */
1298 for (ntries = 0; ntries < 100; ntries++)
1300 if (!(rt2860_io_mac_read(sc, RT2860_REG_STATUS_CFG) &
1301 (RT2860_REG_STATUS_TX_BUSY | RT2860_REG_STATUS_RX_BUSY)))
1302 break;
1304 DELAY(1000);
1307 if (ntries == 100)
1309 printf("%s: timeout waiting for MAC\n",
1310 device_get_nameunit(sc->dev));
1311 goto fail;
1314 /* clear Host to MCU mailbox */
1316 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX_BBP_AGENT, 0);
1317 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX, 0);
1319 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_BOOT,
1320 RT2860_REG_H2M_TOKEN_NO_INTR, 0);
1322 DELAY(1000);
1324 error = rt2860_init_bbp(sc);
1325 if (error != 0)
1326 goto fail;
1328 /* set up maximum buffer sizes */
1330 tmp = (1 << 12) | RT2860_MAX_AGG_SIZE;
1332 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1334 if (sc->mac_rev == 0x28720200)
1336 /* set max. PSDU length from 16K to 32K bytes */
1338 tmp = rt2860_io_mac_read(sc, RT2860_REG_MAX_LEN_CFG);
1340 tmp &= ~(3 << 12);
1341 tmp |= (2 << 12);
1343 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1346 if (sc->mac_rev >= 0x28720200 && sc->mac_rev < 0x30700200)
1348 tmp = rt2860_io_mac_read(sc, RT2860_REG_MAX_LEN_CFG);
1350 tmp &= 0xfff;
1351 tmp |= 0x2000;
1353 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1356 /* set mac address */
1358 rt2860_asic_set_macaddr(sc, IF_LLADDR(ifp));
1360 /* clear statistic registers */
1362 rt2860_io_mac_read_multi(sc, RT2860_REG_RX_STA_CNT0,
1363 stacnt, sizeof(stacnt));
1365 /* set RTS threshold */
1367 rt2860_asic_update_rtsthreshold(sc);
1369 /* set Tx power */
1371 rt2860_asic_update_txpower(sc);
1373 /* set up protection mode */
1375 sc->tx_ampdu_sessions = 0;
1377 rt2860_asic_updateprot(sc);
1379 /* clear beacon frame space (entries = 8, entry size = 512) */
1381 rt2860_io_mac_set_region_4(sc, RT2860_REG_BEACON_BASE(0), 0, 1024);
1383 taskqueue_unblock(sc->taskqueue);
1385 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
1387 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1388 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
1390 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1392 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
1393 sc->tx_ring[i].desc_phys_addr);
1394 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
1395 RT2860_SOFTC_TX_RING_DESC_COUNT);
1396 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
1399 /* init Rx ring */
1401 rt2860_reset_rx_ring(sc, &sc->rx_ring);
1403 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
1404 sc->rx_ring.desc_phys_addr);
1405 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
1406 RT2860_SOFTC_RX_RING_DATA_COUNT);
1407 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
1408 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
1410 /* wait while DMA engine is busy */
1412 for (ntries = 0; ntries < 100; ntries++)
1414 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1415 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1416 break;
1418 DELAY(1000);
1421 if (ntries == 100)
1423 printf("%s: timeout waiting for DMA engine\n",
1424 device_get_nameunit(sc->dev));
1425 goto fail;
1428 tmp &= 0xff0;
1429 tmp |= RT2860_REG_TX_WB_DDONE;
1431 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1433 /* disable interrupts mitigation */
1435 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_DELAY_INT_CFG, 0);
1437 /* send LEDs operating mode to microcontroller */
1439 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED1,
1440 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1441 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED2,
1442 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1443 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED3,
1444 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1446 /* turn radio LED on */
1448 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON);
1450 /* write vendor-specific BBP values (from EEPROM) */
1452 for (i = 0; i < RT2860_SOFTC_BBP_EEPROM_COUNT; i++)
1454 if (sc->bbp_eeprom[i].reg == 0x00 ||
1455 sc->bbp_eeprom[i].reg == 0xff)
1456 continue;
1458 rt2860_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1459 sc->bbp_eeprom[i].val);
1462 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_GPIO_CTRL_CFG);
1463 if (tmp & (1 << 2))
1465 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_SLEEP,
1466 RT2860_REG_H2M_TOKEN_RADIOOFF, 0x02ff);
1467 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_RADIOOFF);
1469 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_WAKEUP,
1470 RT2860_REG_H2M_TOKEN_WAKEUP, 0);
1471 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_WAKEUP);
1474 /* disable non-existing Rx chains */
1476 tmp = rt2860_io_bbp_read(sc, 3);
1478 tmp &= ~((1 << 4) | (1 << 3));
1480 if (sc->nrxpath == 3)
1481 tmp |= (1 << 4);
1482 else if (sc->nrxpath == 2)
1483 tmp |= (1 << 3);
1485 rt2860_io_bbp_write(sc, 3, tmp);
1487 /* disable non-existing Tx chains */
1489 tmp = rt2860_io_bbp_read(sc, 1);
1491 tmp &= ~((1 << 4) | (1 << 3));
1493 if (sc->ntxpath == 2)
1494 tmp |= (1 << 4);
1496 rt2860_io_bbp_write(sc, 1, tmp);
1498 /* set current channel */
1500 rt2860_rf_set_chan(sc, ic->ic_curchan);
1502 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1503 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1504 (48 << 16) | 96);
1506 if ((sc->mac_rev & 0xffff) != 0x0101)
1507 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1509 /* clear pending interrupts */
1511 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1513 /* enable interrupts */
1515 tmp = RT2860_REG_INT_TX_COHERENT |
1516 RT2860_REG_INT_RX_COHERENT |
1517 RT2860_REG_INT_GP_TIMER |
1518 RT2860_REG_INT_AUTO_WAKEUP |
1519 RT2860_REG_INT_FIFO_STA_FULL |
1520 RT2860_REG_INT_PRE_TBTT |
1521 RT2860_REG_INT_TBTT |
1522 RT2860_REG_INT_TXRX_COHERENT |
1523 RT2860_REG_INT_MCU_CMD |
1524 RT2860_REG_INT_TX_MGMT_DONE |
1525 RT2860_REG_INT_TX_HCCA_DONE |
1526 RT2860_REG_INT_TX_AC3_DONE |
1527 RT2860_REG_INT_TX_AC2_DONE |
1528 RT2860_REG_INT_TX_AC1_DONE |
1529 RT2860_REG_INT_TX_AC0_DONE |
1530 RT2860_REG_INT_RX_DONE;
1532 sc->intr_enable_mask = tmp;
1534 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1536 if (rt2860_txrx_enable(sc) != 0)
1537 goto fail;
1539 /* clear garbage interrupts */
1541 tmp = rt2860_io_mac_read(sc, 0x1300);
1543 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1544 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1546 sc->periodic_round = 0;
1548 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1550 return;
1552 fail:
1554 rt2860_stop_locked(sc);
1558 * rt2860_init
1560 static void rt2860_init(void *priv)
1562 struct rt2860_softc *sc;
1564 sc = priv;
1566 RT2860_SOFTC_LOCK(sc);
1568 rt2860_init_locked(sc);
1570 RT2860_SOFTC_UNLOCK(sc);
1574 * rt2860_init_bbp
1576 static int rt2860_init_bbp(struct rt2860_softc *sc)
1578 int ntries, i;
1579 uint8_t tmp;
1581 for (ntries = 0; ntries < 20; ntries++)
1583 tmp = rt2860_io_bbp_read(sc, 0);
1584 if (tmp != 0x00 && tmp != 0xff)
1585 break;
1588 if (tmp == 0x00 || tmp == 0xff)
1590 printf("%s: timeout waiting for BBP to wakeup\n",
1591 device_get_nameunit(sc->dev));
1592 return ETIMEDOUT;
1595 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1596 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1597 rt2860_def_bbp[i].val);
1599 if ((sc->mac_rev & 0xffff) != 0x0101)
1600 rt2860_io_bbp_write(sc, 84, 0x19);
1602 if (sc->mac_rev == 0x28600100)
1604 rt2860_io_bbp_write(sc, 69, 0x16);
1605 rt2860_io_bbp_write(sc, 73, 0x12);
1608 return 0;
1612 * rt2860_stop_locked
1614 static void rt2860_stop_locked(void *priv)
1616 struct rt2860_softc *sc;
1617 struct ifnet *ifp;
1618 struct ieee80211com *ic;
1619 uint32_t tmp;
1621 sc = priv;
1622 ifp = sc->ifp;
1623 ic = ifp->if_l2com;
1625 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1626 "%s: stopping\n",
1627 device_get_nameunit(sc->dev));
1629 RT2860_SOFTC_ASSERT_LOCKED(sc);
1631 sc->tx_timer = 0;
1633 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1634 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1636 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1638 callout_stop(&sc->periodic_ch);
1639 callout_stop(&sc->tx_watchdog_ch);
1641 RT2860_SOFTC_UNLOCK(sc);
1643 taskqueue_block(sc->taskqueue);
1645 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1646 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1647 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1648 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1650 RT2860_SOFTC_LOCK(sc);
1652 /* clear key tables */
1654 rt2860_asic_clear_keytables(sc);
1656 /* disable interrupts */
1658 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1660 /* disable Tx/Rx */
1662 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1664 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1666 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1668 /* reset adapter */
1670 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1671 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1672 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1676 * rt2860_stop
1678 static void rt2860_stop(void *priv)
1680 struct rt2860_softc *sc;
1682 sc = priv;
1684 RT2860_SOFTC_LOCK(sc);
1686 rt2860_stop_locked(sc);
1688 RT2860_SOFTC_UNLOCK(sc);
1692 * rt2860_start
1694 static void rt2860_start(struct ifnet *ifp)
1696 struct rt2860_softc *sc;
1697 struct ieee80211_node *ni;
1698 struct mbuf *m;
1699 int qid;
1701 sc = ifp->if_softc;
1703 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1704 return;
1706 for (;;)
1708 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1709 if (m == NULL)
1710 break;
1712 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1714 m->m_pkthdr.rcvif = NULL;
1716 qid = M_WME_GETAC(m);
1718 RT2860_SOFTC_TX_RING_LOCK(&sc->tx_ring[qid]);
1720 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1722 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
1724 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1725 "%s: if_start: Tx ring with qid=%d is full\n",
1726 device_get_nameunit(sc->dev), qid);
1728 m_freem(m);
1729 ieee80211_free_node(ni);
1731 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1732 ifp->if_oerrors++;
1734 sc->tx_data_queue_full[qid]++;
1736 break;
1739 if (rt2860_tx_data(sc, m, ni, qid) != 0)
1741 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
1743 ieee80211_free_node(ni);
1745 ifp->if_oerrors++;
1747 break;
1750 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
1752 rt2860_drain_fifo_stats(sc);
1754 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1756 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1761 * rt2860_ioctl
1763 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1765 struct rt2860_softc *sc;
1766 struct ieee80211com *ic;
1767 struct ifreq *ifr;
1768 int error, startall;
1770 sc = ifp->if_softc;
1771 ic = ifp->if_l2com;
1772 ifr = (struct ifreq *) data;
1774 error = 0;
1776 switch (cmd)
1778 case SIOCSIFFLAGS:
1779 startall = 0;
1781 RT2860_SOFTC_LOCK(sc);
1783 if (ifp->if_flags & IFF_UP)
1785 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1787 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1788 rt2860_asic_update_promisc(sc);
1790 else
1792 rt2860_init_locked(sc);
1793 startall = 1;
1796 else
1798 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1799 rt2860_stop_locked(sc);
1802 sc->if_flags = ifp->if_flags;
1804 RT2860_SOFTC_UNLOCK(sc);
1806 if (startall)
1807 ieee80211_start_all(ic);
1808 break;
1810 case SIOCGIFMEDIA:
1811 case SIOCSIFMEDIA:
1812 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1813 break;
1815 case SIOCGIFADDR:
1816 error = ether_ioctl(ifp, cmd, data);
1817 break;
1819 default:
1820 error = EINVAL;
1821 break;
1824 return error;
1828 * rt2860_vap_create
1830 static struct ieee80211vap *rt2860_vap_create(struct ieee80211com *ic,
1831 const char name[IFNAMSIZ], int unit, int opmode, int flags,
1832 const uint8_t bssid[IEEE80211_ADDR_LEN],
1833 const uint8_t mac[IEEE80211_ADDR_LEN])
1835 struct rt2860_softc *sc;
1836 struct ifnet *ifp;
1837 struct rt2860_softc_vap *rvap;
1838 struct ieee80211vap *vap;
1840 ifp = ic->ic_ifp;
1841 sc = ifp->if_softc;
1843 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1844 "%s: VAP create: opmode=%s\n",
1845 device_get_nameunit(sc->dev),
1846 ieee80211_opmode_name[opmode]);
1848 switch (opmode)
1850 case IEEE80211_M_IBSS:
1851 case IEEE80211_M_STA:
1852 case IEEE80211_M_AHDEMO:
1853 case IEEE80211_M_HOSTAP:
1854 case IEEE80211_M_MBSS:
1855 if ((sc->napvaps + sc->nadhocvaps + sc->nstavaps) != 0)
1857 if_printf(ifp, "only 1 VAP supported\n");
1858 return NULL;
1861 if (opmode == IEEE80211_M_STA)
1862 flags |= IEEE80211_CLONE_NOBEACONS;
1863 break;
1865 case IEEE80211_M_WDS:
1866 if (sc->napvaps == 0)
1868 if_printf(ifp, "WDS only supported in AP mode\n");
1869 return NULL;
1871 break;
1873 case IEEE80211_M_MONITOR:
1874 break;
1876 default:
1877 if_printf(ifp, "unknown opmode %d\n", opmode);
1878 return NULL;
1881 rvap = (struct rt2860_softc_vap *) malloc(sizeof(struct rt2860_softc_vap),
1882 M_80211_VAP, M_NOWAIT | M_ZERO);
1883 if (rvap == NULL)
1884 return NULL;
1886 vap = &rvap->vap;
1888 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
1890 rvap->newstate = vap->iv_newstate;
1891 vap->iv_newstate = rt2860_vap_newstate;
1893 vap->iv_reset = rt2860_vap_reset;
1894 vap->iv_key_update_begin = rt2860_vap_key_update_begin;
1895 vap->iv_key_update_end = rt2860_vap_key_update_end;
1896 vap->iv_key_set = rt2860_vap_key_set;
1897 vap->iv_key_delete = rt2860_vap_key_delete;
1898 vap->iv_update_beacon = rt2860_vap_update_beacon;
1900 rt2860_amrr_init(&rvap->amrr, vap,
1901 sc->ntxpath,
1902 RT2860_AMRR_MIN_SUCCESS_THRESHOLD,
1903 RT2860_AMRR_MAX_SUCCESS_THRESHOLD,
1904 500);
1906 vap->iv_max_aid = RT2860_SOFTC_STAID_COUNT;
1908 /* overwrite default Rx A-MPDU factor */
1910 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1911 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
1912 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
1914 ieee80211_vap_attach(vap, rt2860_media_change, ieee80211_media_status);
1916 switch (vap->iv_opmode)
1918 case IEEE80211_M_HOSTAP:
1919 case IEEE80211_M_MBSS:
1920 case IEEE80211_M_AHDEMO:
1921 sc->napvaps++;
1922 break;
1924 case IEEE80211_M_IBSS:
1925 sc->nadhocvaps++;
1926 break;
1928 case IEEE80211_M_STA:
1929 sc->nstavaps++;
1930 break;
1932 case IEEE80211_M_WDS:
1933 sc->nwdsvaps++;
1934 break;
1936 default:
1937 break;
1940 sc->nvaps++;
1942 if (sc->napvaps > 0)
1943 ic->ic_opmode = IEEE80211_M_HOSTAP;
1944 else if (sc->nadhocvaps > 0)
1945 ic->ic_opmode = IEEE80211_M_IBSS;
1946 else if (sc->nstavaps > 0)
1947 ic->ic_opmode = IEEE80211_M_STA;
1948 else
1949 ic->ic_opmode = opmode;
1951 return vap;
1955 * rt2860_vap_delete
1957 static void rt2860_vap_delete(struct ieee80211vap *vap)
1959 struct rt2860_softc *sc;
1960 struct ieee80211com *ic;
1961 struct ifnet *ifp;
1962 struct rt2860_softc_vap *rvap;
1963 enum ieee80211_opmode opmode;
1965 ic = vap->iv_ic;
1966 ifp = ic->ic_ifp;
1967 sc = ifp->if_softc;
1968 rvap = (struct rt2860_softc_vap *) vap;
1969 opmode = vap->iv_opmode;
1971 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1972 "%s: VAP delete: opmode=%s\n",
1973 device_get_nameunit(sc->dev), ieee80211_opmode_name[opmode]);
1975 rt2860_amrr_cleanup(&rvap->amrr);
1977 ieee80211_vap_detach(vap);
1979 if (rvap->beacon_mbuf != NULL)
1981 m_free(rvap->beacon_mbuf);
1982 rvap->beacon_mbuf = NULL;
1985 switch (opmode)
1987 case IEEE80211_M_HOSTAP:
1988 case IEEE80211_M_MBSS:
1989 case IEEE80211_M_AHDEMO:
1990 sc->napvaps--;
1991 break;
1993 case IEEE80211_M_IBSS:
1994 sc->nadhocvaps--;
1995 break;
1997 case IEEE80211_M_STA:
1998 sc->nstavaps--;
1999 break;
2001 case IEEE80211_M_WDS:
2002 sc->nwdsvaps--;
2003 break;
2005 default:
2006 break;
2009 sc->nvaps--;
2011 if (sc->napvaps > 0)
2012 ic->ic_opmode = IEEE80211_M_HOSTAP;
2013 else if (sc->nadhocvaps > 0)
2014 ic->ic_opmode = IEEE80211_M_IBSS;
2015 else if (sc->nstavaps > 0)
2016 ic->ic_opmode = IEEE80211_M_STA;
2018 free(rvap, M_80211_VAP);
2022 * rt2860_reset_vap
2024 static int rt2860_vap_reset(struct ieee80211vap *vap, u_long cmd)
2026 struct rt2860_softc *sc;
2027 struct ieee80211com *ic;
2028 struct ifnet *ifp;
2029 struct rt2860_softc_vap *rvap;
2030 int error;
2032 ic = vap->iv_ic;
2033 ifp = ic->ic_ifp;
2034 sc = ifp->if_softc;
2035 rvap = (struct rt2860_softc_vap *) vap;
2037 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2038 "%s: VAP reset: cmd=%lu\n",
2039 device_get_nameunit(sc->dev), cmd);
2041 error = 0;
2043 switch (cmd)
2045 case IEEE80211_IOC_RTSTHRESHOLD:
2046 case IEEE80211_IOC_AMSDU:
2047 rt2860_asic_update_rtsthreshold(sc);
2048 break;
2050 case IEEE80211_IOC_PROTMODE:
2051 case IEEE80211_IOC_HTPROTMODE:
2052 rt2860_asic_updateprot(sc);
2053 break;
2055 case IEEE80211_IOC_TXPOWER:
2056 rt2860_asic_update_txpower(sc);
2057 break;
2059 case IEEE80211_IOC_BURST:
2060 rt2860_asic_updateslot(sc);
2061 break;
2063 case IEEE80211_IOC_SHORTGI:
2064 case IEEE80211_IOC_AMPDU_DENSITY:
2065 case IEEE80211_IOC_SMPS:
2066 break;
2068 default:
2069 error = ENETRESET;
2070 break;
2073 return error;
2077 * rt2860_vap_newstate
2079 static int rt2860_vap_newstate(struct ieee80211vap *vap,
2080 enum ieee80211_state nstate, int arg)
2082 struct rt2860_softc *sc;
2083 struct ieee80211com *ic;
2084 struct ifnet *ifp;
2085 struct rt2860_softc_vap *rvap;
2086 struct ieee80211_node *ni;
2087 enum ieee80211_state ostate;
2088 int error;
2090 ic = vap->iv_ic;
2091 ifp = ic->ic_ifp;
2092 sc = ifp->if_softc;
2093 rvap = (struct rt2860_softc_vap *) vap;
2095 ostate = vap->iv_state;
2097 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2098 "%s: VAP newstate: %s -> %s\n",
2099 device_get_nameunit(sc->dev),
2100 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
2102 error = rvap->newstate(vap, nstate, arg);
2103 if (error != 0)
2104 return error;
2106 IEEE80211_UNLOCK(ic);
2108 RT2860_SOFTC_LOCK(sc);
2110 /* turn link LED off */
2112 if (nstate != IEEE80211_S_RUN)
2113 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
2115 switch (nstate)
2117 case IEEE80211_S_INIT:
2118 rt2860_asic_disable_tsf_sync(sc);
2119 break;
2121 case IEEE80211_S_RUN:
2122 ni = vap->iv_bss;
2124 rt2860_rf_set_chan(sc, ni->ni_chan);
2126 if (vap->iv_opmode != IEEE80211_M_MONITOR)
2128 rt2860_asic_enable_mrr(sc);
2129 rt2860_asic_set_txpreamble(sc);
2130 rt2860_asic_set_basicrates(sc);
2131 rt2860_asic_update_txpower(sc);
2132 rt2860_asic_set_bssid(sc, ni->ni_bssid);
2135 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
2136 vap->iv_opmode == IEEE80211_M_IBSS ||
2137 vap->iv_opmode == IEEE80211_M_MBSS)
2139 error = rt2860_beacon_alloc(sc, vap);
2140 if (error != 0)
2141 break;
2143 rt2860_asic_update_beacon(sc, vap);
2146 if (vap->iv_opmode != IEEE80211_M_MONITOR)
2147 rt2860_asic_enable_tsf_sync(sc);
2149 /* turn link LED on */
2151 if (vap->iv_opmode != IEEE80211_M_MONITOR)
2153 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
2154 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
2155 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
2157 break;
2159 case IEEE80211_S_SLEEP:
2160 break;
2162 default:
2163 break;
2166 RT2860_SOFTC_UNLOCK(sc);
2168 IEEE80211_LOCK(ic);
2170 return error;
2174 * rt2860_vap_key_update_begin
2176 static void rt2860_vap_key_update_begin(struct ieee80211vap *vap)
2178 struct rt2860_softc *sc;
2179 struct ieee80211com *ic;
2180 struct ifnet *ifp;
2182 ic = vap->iv_ic;
2183 ifp = ic->ic_ifp;
2184 sc = ifp->if_softc;
2186 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2187 "%s: VAP key update begin\n",
2188 device_get_nameunit(sc->dev));
2190 taskqueue_block(sc->taskqueue);
2192 IF_LOCK(&ifp->if_snd);
2196 * rt2860_vap_key_update_end
2198 static void rt2860_vap_key_update_end(struct ieee80211vap *vap)
2200 struct rt2860_softc *sc;
2201 struct ieee80211com *ic;
2202 struct ifnet *ifp;
2204 ic = vap->iv_ic;
2205 ifp = ic->ic_ifp;
2206 sc = ifp->if_softc;
2208 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2209 "%s: VAP key update end\n",
2210 device_get_nameunit(sc->dev));
2212 IF_UNLOCK(&ifp->if_snd);
2214 taskqueue_unblock(sc->taskqueue);
2218 * rt2860_vap_key_set
2220 static int rt2860_vap_key_set(struct ieee80211vap *vap,
2221 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2223 struct rt2860_softc *sc;
2224 struct ieee80211com *ic;
2225 struct ifnet *ifp;
2226 struct ieee80211_node *ni;
2227 struct rt2860_softc_node *rni;
2228 uint16_t key_base, keymode_base;
2229 uint8_t mode, vapid, wcid, iv[8];
2230 uint32_t tmp;
2232 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2233 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2234 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2235 return 0;
2237 ic = vap->iv_ic;
2238 ifp = ic->ic_ifp;
2239 sc = ifp->if_softc;
2241 switch (k->wk_cipher->ic_cipher)
2243 case IEEE80211_CIPHER_WEP:
2244 if(k->wk_keylen < 8)
2245 mode = RT2860_REG_CIPHER_MODE_WEP40;
2246 else
2247 mode = RT2860_REG_CIPHER_MODE_WEP104;
2248 break;
2250 case IEEE80211_CIPHER_TKIP:
2251 mode = RT2860_REG_CIPHER_MODE_TKIP;
2252 break;
2254 case IEEE80211_CIPHER_AES_CCM:
2255 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2256 break;
2258 default:
2259 return 0;
2262 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2263 "%s: VAP key set: keyix=%d, keylen=%d, macaddr=%s, mode=%d, group=%d\n",
2264 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, ether_sprintf(k->wk_macaddr),
2265 mode, (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2267 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2269 /* install pairwise key */
2271 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, mac);
2272 rni = (struct rt2860_softc_node *) ni;
2274 vapid = 0;
2275 wcid = rni->staid;
2276 key_base = RT2860_REG_PKEY(wcid);
2278 ieee80211_free_node(ni);
2280 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2282 memset(iv, 0, 8);
2284 iv[3] = (k->wk_keyix << 6);
2286 else
2288 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2290 iv[0] = (k->wk_keytsc >> 8);
2291 iv[1] = ((iv[0] | 0x20) & 0x7f);
2292 iv[2] = k->wk_keytsc;
2294 else
2296 /* AES CCMP */
2298 iv[0] = k->wk_keytsc;
2299 iv[1] = k->wk_keytsc >> 8;
2300 iv[2] = 0;
2303 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2304 iv[4] = (k->wk_keytsc >> 16);
2305 iv[5] = (k->wk_keytsc >> 24);
2306 iv[6] = (k->wk_keytsc >> 32);
2307 iv[7] = (k->wk_keytsc >> 40);
2309 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2310 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2311 device_get_nameunit(sc->dev),
2312 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2315 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2317 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2319 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2321 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2323 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2324 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2326 else
2328 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2329 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2332 else
2334 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2337 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2338 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2340 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2343 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2344 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2346 /* install group key */
2348 vapid = 0;
2349 wcid = RT2860_WCID_MCAST;
2350 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2351 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2353 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2355 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2357 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2359 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2360 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2362 else
2364 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2365 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2368 else
2370 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2373 tmp = rt2860_io_mac_read(sc, keymode_base);
2375 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2376 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2378 rt2860_io_mac_write(sc, keymode_base, tmp);
2380 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2382 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2384 memset(iv, 0, 8);
2386 iv[3] = (k->wk_keyix << 6);
2388 else
2390 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2392 iv[0] = (k->wk_keytsc >> 8);
2393 iv[1] = ((iv[0] | 0x20) & 0x7f);
2394 iv[2] = k->wk_keytsc;
2396 else
2398 /* AES CCMP */
2400 iv[0] = k->wk_keytsc;
2401 iv[1] = k->wk_keytsc >> 8;
2402 iv[2] = 0;
2405 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2406 iv[4] = (k->wk_keytsc >> 16);
2407 iv[5] = (k->wk_keytsc >> 24);
2408 iv[6] = (k->wk_keytsc >> 32);
2409 iv[7] = (k->wk_keytsc >> 40);
2411 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2412 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2413 device_get_nameunit(sc->dev),
2414 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2417 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2419 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2420 (mode << RT2860_REG_CIPHER_MODE_SHIFT);
2422 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2426 return 1;
2430 * rt2860_vap_key_delete
2432 static int rt2860_vap_key_delete(struct ieee80211vap *vap,
2433 const struct ieee80211_key *k)
2435 struct rt2860_softc *sc;
2436 struct ieee80211com *ic;
2437 struct ifnet *ifp;
2438 uint8_t vapid, wcid;
2439 uint32_t tmp;
2441 ic = vap->iv_ic;
2442 ifp = ic->ic_ifp;
2443 sc = ifp->if_softc;
2445 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2446 "%s: VAP key delete: keyix=%d, keylen=%d, macaddr=%s, group=%d\n",
2447 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, ether_sprintf(k->wk_macaddr),
2448 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2450 if (k->wk_flags & IEEE80211_KEY_GROUP)
2452 /* remove group key */
2454 vapid = 0;
2455 wcid = RT2860_WCID_MCAST;
2457 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2459 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2460 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2462 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2464 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2466 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2467 (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2469 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2473 return 1;
2477 * rt2860_vap_update_beacon
2479 static void rt2860_vap_update_beacon(struct ieee80211vap *vap, int what)
2481 struct rt2860_softc *sc;
2482 struct ieee80211com *ic;
2483 struct ifnet *ifp;
2484 struct rt2860_softc_vap *rvap;
2485 struct mbuf *m;
2486 struct ieee80211_beacon_offsets *bo;
2488 ic = vap->iv_ic;
2489 ifp = ic->ic_ifp;
2490 sc = ifp->if_softc;
2491 rvap = (struct rt2860_softc_vap *) vap;
2492 m = rvap->beacon_mbuf;
2493 bo = &rvap->beacon_offsets;
2495 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2496 "%s: VAP update beacon: what=%d\n",
2497 device_get_nameunit(sc->dev), what);
2499 setbit(bo->bo_flags, what);
2501 ieee80211_beacon_update(vap->iv_bss, bo, m, 0);
2503 rt2860_asic_update_beacon(sc, vap);
2507 * rt2860_media_change
2509 static int rt2860_media_change(struct ifnet *ifp)
2511 int error;
2513 error = ieee80211_media_change(ifp);
2515 return (error == ENETRESET ? 0 : error);
2519 * rt2860_node_alloc
2521 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211vap *vap,
2522 const uint8_t mac[IEEE80211_ADDR_LEN])
2524 return malloc(sizeof(struct rt2860_softc_node),
2525 M_80211_NODE, M_NOWAIT | M_ZERO);
2529 * rt2860_node_cleanup
2531 static void rt2860_node_cleanup(struct ieee80211_node *ni)
2533 struct rt2860_softc *sc;
2534 struct ieee80211com *ic;
2535 struct ifnet *ifp;
2536 struct rt2860_softc_node *rni;
2537 uint8_t vapid, wcid;
2538 uint32_t tmp;
2540 ic = ni->ni_ic;
2541 ifp = ic->ic_ifp;
2542 sc = ifp->if_softc;
2543 rni = (struct rt2860_softc_node *) ni;
2545 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
2546 "%s: node cleanup: macaddr=%s, associd=0x%04x, staid=0x%02x\n",
2547 device_get_nameunit(sc->dev), ether_sprintf(ni->ni_macaddr),
2548 ni->ni_associd, rni->staid);
2550 if (rni->staid != 0)
2552 vapid = 0;
2553 wcid = rni->staid;
2555 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2556 (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2558 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2560 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid), 0x00000000);
2561 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, 0x00000000);
2563 rt2860_staid_delete(sc, rni->staid);
2565 rni->staid = 0;
2568 sc->node_cleanup(ni);
2572 * rt2860_node_getmimoinfo
2574 static void rt2860_node_getmimoinfo(const struct ieee80211_node *ni,
2575 struct ieee80211_mimo_info *mi)
2577 const struct rt2860_softc_node *rni;
2578 int i;
2580 rni = (const struct rt2860_softc_node *) ni;
2582 for (i = 0; i < RT2860_SOFTC_RSSI_COUNT; i++)
2584 mi->rssi[i] = rni->last_rssi_dbm[i];
2585 mi->noise[i] = RT2860_NOISE_FLOOR;
2590 * rt2860_setregdomain
2592 static int rt2860_setregdomain(struct ieee80211com *ic,
2593 struct ieee80211_regdomain *reg,
2594 int nchans, struct ieee80211_channel chans[])
2596 struct rt2860_softc *sc;
2597 struct ifnet *ifp;
2599 ifp = ic->ic_ifp;
2600 sc = ifp->if_softc;
2602 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2603 "%s: set regulatory domain: country=%d, country code string=%c%c, location=%c\n",
2604 device_get_nameunit(sc->dev),
2605 reg->country, reg->isocc[0], reg->isocc[1], reg->location);
2607 return 0;
2611 * rt2860_getradiocaps
2613 static void rt2860_getradiocaps(struct ieee80211com *ic,
2614 int maxchans, int *nchans, struct ieee80211_channel chans[])
2616 *nchans = (ic->ic_nchans >= maxchans) ? maxchans : ic->ic_nchans;
2618 memcpy(chans, ic->ic_channels, (*nchans) * sizeof(struct ieee80211_channel));
2622 * rt2860_scan_start
2624 static void rt2860_scan_start(struct ieee80211com *ic)
2626 struct rt2860_softc *sc;
2627 struct ifnet *ifp;
2629 ifp = ic->ic_ifp;
2630 sc = ifp->if_softc;
2632 rt2860_asic_disable_tsf_sync(sc);
2636 * rt2860_scan_end
2638 static void rt2860_scan_end(struct ieee80211com *ic)
2640 struct rt2860_softc *sc;
2641 struct ifnet *ifp;
2643 ifp = ic->ic_ifp;
2644 sc = ifp->if_softc;
2646 rt2860_asic_enable_tsf_sync(sc);
2650 * rt2860_set_channel
2652 static void rt2860_set_channel(struct ieee80211com *ic)
2654 struct rt2860_softc *sc;
2655 struct ifnet *ifp;
2657 ifp = ic->ic_ifp;
2658 sc = ifp->if_softc;
2660 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
2661 "%s: set channel: channel=%u, HT%s%s\n",
2662 device_get_nameunit(sc->dev),
2663 ieee80211_chan2ieee(ic, ic->ic_curchan),
2664 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
2665 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
2666 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
2667 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
2669 RT2860_SOFTC_LOCK(sc);
2671 rt2860_rf_set_chan(sc, ic->ic_curchan);
2673 RT2860_SOFTC_UNLOCK(sc);
2677 * rt2860_newassoc
2679 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
2681 struct rt2860_softc *sc;
2682 struct ieee80211com *ic;
2683 struct ifnet *ifp;
2684 struct ieee80211vap *vap;
2685 struct rt2860_softc_vap *rvap;
2686 struct rt2860_softc_node *rni;
2687 uint16_t aid;
2688 uint8_t wcid;
2689 uint32_t tmp;
2691 vap = ni->ni_vap;
2692 ic = vap->iv_ic;
2693 ifp = ic->ic_ifp;
2694 sc = ifp->if_softc;
2695 rvap = (struct rt2860_softc_vap *) vap;
2696 rni = (struct rt2860_softc_node *) ni;
2698 if (isnew)
2700 aid = IEEE80211_AID(ni->ni_associd);
2701 rni->staid = rt2860_staid_alloc(sc, aid);
2702 wcid = rni->staid;
2704 tmp = (ni->ni_macaddr[3] << 24) |
2705 (ni->ni_macaddr[2] << 16) |
2706 (ni->ni_macaddr[1] << 8) |
2707 ni->ni_macaddr[0];
2709 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid), tmp);
2711 tmp = (ni->ni_macaddr[5] << 8) |
2712 ni->ni_macaddr[4];
2714 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2716 rt2860_amrr_node_init(&rvap->amrr, &sc->amrr_node[wcid], ni);
2718 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2719 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2720 device_get_nameunit(sc->dev),
2721 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2722 ni->ni_associd, ni->ni_txrate,
2723 (ni->ni_flags & IEEE80211_NODE_HT) ?
2724 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2725 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2727 rt2860_asic_updateprot(sc);
2728 rt2860_asic_updateslot(sc);
2729 rt2860_asic_set_txpreamble(sc);
2732 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
2733 "%s: new association: isnew=%d, macaddr=%s, associd=0x%04x, staid=0x%02x, QoS %s, ERP %s, HT %s\n",
2734 device_get_nameunit(sc->dev), isnew, ether_sprintf(ni->ni_macaddr),
2735 ni->ni_associd, rni->staid,
2736 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
2737 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
2738 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
2742 * rt2860_updateslot
2744 static void rt2860_updateslot(struct ifnet *ifp)
2746 struct rt2860_softc *sc;
2748 sc = ifp->if_softc;
2750 rt2860_asic_updateslot(sc);
2754 * rt2860_update_promisc
2756 static void rt2860_update_promisc(struct ifnet *ifp)
2758 struct rt2860_softc *sc;
2760 sc = ifp->if_softc;
2762 rt2860_asic_update_promisc(sc);
2766 * rt2860_update_mcast
2768 static void rt2860_update_mcast(struct ifnet *ifp)
2770 struct rt2860_softc *sc;
2772 sc = ifp->if_softc;
2776 * rt2860_wme_update
2778 static int rt2860_wme_update(struct ieee80211com *ic)
2780 struct rt2860_softc *sc;
2781 struct ifnet *ifp;
2783 ifp = ic->ic_ifp;
2784 sc = ifp->if_softc;
2786 rt2860_asic_wme_update(sc);
2788 return 0;
2792 * rt2860_raw_xmit
2794 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2795 const struct ieee80211_bpf_params *params)
2797 struct rt2860_softc *sc;
2798 struct ieee80211com *ic;
2799 struct ifnet *ifp;
2801 ic = ni->ni_ic;
2802 ifp = ic->ic_ifp;
2803 sc = ifp->if_softc;
2805 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2807 m_freem(m);
2808 ieee80211_free_node(ni);
2810 return ENETDOWN;
2813 RT2860_SOFTC_TX_RING_LOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
2815 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
2817 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
2819 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
2820 "%s: raw xmit: Tx ring with qid=%d is full\n",
2821 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
2823 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2825 m_freem(m);
2826 ieee80211_free_node(ni);
2828 sc->tx_data_queue_full[sc->tx_ring_mgtqid]++;
2830 return ENOBUFS;
2833 if (rt2860_tx_mgmt(sc, m, ni, sc->tx_ring_mgtqid) != 0)
2835 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
2837 ifp->if_oerrors++;
2839 ieee80211_free_node(ni);
2841 return EIO;
2844 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
2846 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
2848 return 0;
2852 * rt2860_recv_action
2854 static int rt2860_recv_action(struct ieee80211_node *ni,
2855 const struct ieee80211_frame *wh,
2856 const uint8_t *frm, const uint8_t *efrm)
2858 struct rt2860_softc *sc;
2859 struct ieee80211com *ic;
2860 struct ifnet *ifp;
2861 struct rt2860_softc_node *rni;
2862 const struct ieee80211_action *ia;
2863 uint16_t baparamset;
2864 uint8_t wcid;
2865 int tid;
2867 ic = ni->ni_ic;
2868 ifp = ic->ic_ifp;
2869 sc = ifp->if_softc;
2870 rni = (struct rt2860_softc_node *) ni;
2872 ia = (const struct ieee80211_action *) frm;
2874 if (ia->ia_category == IEEE80211_ACTION_CAT_BA)
2876 switch (ia->ia_action)
2878 /* IEEE80211_ACTION_BA_DELBA */
2879 case IEEE80211_ACTION_BA_DELBA:
2880 baparamset = LE_READ_2(frm + 2);
2881 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2882 wcid = rni->staid;
2884 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2885 "%s: received DELBA request: associd=0x%04x, staid=0x%02x, tid=%d\n",
2886 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid, tid);
2888 if (rni->staid != 0)
2889 rt2860_asic_del_ba_session(sc, wcid, tid);
2890 break;
2894 return sc->recv_action(ni, wh, frm, efrm);
2898 * rt2860_send_action
2900 static int rt2860_send_action(struct ieee80211_node *ni,
2901 int cat, int act, void *sa)
2903 struct rt2860_softc *sc;
2904 struct ieee80211com *ic;
2905 struct ifnet *ifp;
2906 struct rt2860_softc_node *rni;
2907 uint16_t *args, status, baparamset;
2908 uint8_t wcid;
2909 int tid, bufsize;
2911 ic = ni->ni_ic;
2912 ifp = ic->ic_ifp;
2913 sc = ifp->if_softc;
2914 rni = (struct rt2860_softc_node *) ni;
2916 wcid = rni->staid;
2917 args = sa;
2919 if (cat == IEEE80211_ACTION_CAT_BA)
2921 switch (act)
2923 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2924 case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
2925 status = args[1];
2926 baparamset = args[2];
2927 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2928 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2930 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2931 "%s: sending ADDBA response: associd=0x%04x, staid=0x%02x, status=%d, tid=%d, bufsize=%d\n",
2932 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid, status, tid, bufsize);
2934 if (status == IEEE80211_STATUS_SUCCESS)
2935 rt2860_asic_add_ba_session(sc, wcid, tid);
2936 break;
2938 /* IEEE80211_ACTION_BA_DELBA */
2939 case IEEE80211_ACTION_BA_DELBA:
2940 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) | args[1];
2941 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2943 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2944 "%s: sending DELBA request: associd=0x%04x, staid=0x%02x, tid=%d\n",
2945 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid, tid);
2947 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) != IEEE80211_DELBAPS_INIT)
2948 rt2860_asic_del_ba_session(sc, wcid, tid);
2949 break;
2953 return sc->send_action(ni, cat, act, sa);
2957 * rt2860_addba_response
2959 static int rt2860_addba_response(struct ieee80211_node *ni,
2960 struct ieee80211_tx_ampdu *tap,
2961 int status, int baparamset, int batimeout)
2963 struct rt2860_softc *sc;
2964 struct ieee80211com *ic;
2965 struct ifnet *ifp;
2966 struct rt2860_softc_node *rni;
2967 ieee80211_seq seqno;
2968 int ret, tid, old_bufsize, new_bufsize;
2970 ic = ni->ni_ic;
2971 ifp = ic->ic_ifp;
2972 sc = ifp->if_softc;
2973 rni = (struct rt2860_softc_node *) ni;
2975 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2976 old_bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2977 new_bufsize = old_bufsize;
2979 if (status == IEEE80211_STATUS_SUCCESS)
2981 if (sc->mac_rev >= 0x28830300)
2983 if (sc->mac_rev >= 0x30700200)
2984 new_bufsize = 13;
2985 else
2986 new_bufsize = 31;
2988 else if (sc->mac_rev >= 0x28720200)
2990 new_bufsize = 13;
2992 else
2994 new_bufsize = 7;
2997 if (old_bufsize > new_bufsize)
2999 baparamset &= ~IEEE80211_BAPS_BUFSIZ;
3000 baparamset = RT2860_SM(new_bufsize, IEEE80211_BAPS_BUFSIZ);
3003 if (!(tap->txa_flags & IEEE80211_AGGR_RUNNING))
3005 sc->tx_ampdu_sessions++;
3007 if (sc->tx_ampdu_sessions == 1)
3008 rt2860_asic_updateprot(sc);
3012 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
3013 "%s: received ADDBA response: associd=0x%04x, staid=0x%02x, status=%d, tid=%d, "
3014 "old bufsize=%d, new bufsize=%d\n",
3015 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid, status, tid,
3016 old_bufsize, new_bufsize);
3018 ret = sc->addba_response(ni, tap, status, baparamset, batimeout);
3020 if (status == IEEE80211_STATUS_SUCCESS)
3022 seqno = ni->ni_txseqs[tid];
3024 rt2860_send_bar(ni, tap, seqno);
3027 return ret;
3031 * rt2860_addba_stop
3033 static void rt2860_addba_stop(struct ieee80211_node *ni,
3034 struct ieee80211_tx_ampdu *tap)
3036 struct rt2860_softc *sc;
3037 struct ieee80211com *ic;
3038 struct ifnet *ifp;
3039 struct rt2860_softc_node *rni;
3040 int tid;
3042 ic = ni->ni_ic;
3043 ifp = ic->ic_ifp;
3044 sc = ifp->if_softc;
3045 rni = (struct rt2860_softc_node *) ni;
3047 tid = WME_AC_TO_TID(tap->txa_ac);
3049 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
3050 "%s: stopping A-MPDU Tx: associd=0x%04x, staid=0x%02x, tid=%d\n",
3051 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid, tid);
3053 if (tap->txa_flags & IEEE80211_AGGR_RUNNING)
3055 if (sc->tx_ampdu_sessions > 0)
3057 sc->tx_ampdu_sessions--;
3059 if (sc->tx_ampdu_sessions == 0)
3060 rt2860_asic_updateprot(sc);
3062 else
3064 printf("%s: number of A-MPDU Tx sessions cannot be negative\n",
3065 device_get_nameunit(sc->dev));
3069 sc->addba_stop(ni, tap);
3073 * rt2860_ampdu_rx_start
3075 static int rt2860_ampdu_rx_start(struct ieee80211_node *ni,
3076 struct ieee80211_rx_ampdu *rap,
3077 int baparamset, int batimeout, int baseqctl)
3079 struct rt2860_softc *sc;
3080 struct ieee80211com *ic;
3081 struct ifnet *ifp;
3082 struct rt2860_softc_node *rni;
3083 int tid;
3085 ic = ni->ni_ic;
3086 ifp = ic->ic_ifp;
3087 sc = ifp->if_softc;
3088 rni = (struct rt2860_softc_node *) ni;
3090 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
3092 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
3093 "%s: starting A-MPDU Rx: associd=0x%04x, staid=0x%02x, tid=%d\n",
3094 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid, tid);
3096 if (!(rap->rxa_flags & IEEE80211_AGGR_RUNNING))
3097 sc->rx_ampdu_sessions++;
3099 return sc->ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
3103 * rt2860_ampdu_rx_stop
3105 static void rt2860_ampdu_rx_stop(struct ieee80211_node *ni,
3106 struct ieee80211_rx_ampdu *rap)
3108 struct rt2860_softc *sc;
3109 struct ieee80211com *ic;
3110 struct ifnet *ifp;
3111 struct rt2860_softc_node *rni;
3113 ic = ni->ni_ic;
3114 ifp = ic->ic_ifp;
3115 sc = ifp->if_softc;
3116 rni = (struct rt2860_softc_node *) ni;
3118 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
3119 "%s: stopping A-MPDU Rx: associd=0x%04x, staid=0x%02x\n",
3120 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid);
3122 if (rap->rxa_flags & IEEE80211_AGGR_RUNNING)
3124 if (sc->rx_ampdu_sessions > 0)
3125 sc->rx_ampdu_sessions--;
3126 else
3127 printf("%s: number of A-MPDU Rx sessions cannot be negative\n",
3128 device_get_nameunit(sc->dev));
3131 sc->ampdu_rx_stop(ni, rap);
3135 * rt2860_send_bar
3137 static int rt2860_send_bar(struct ieee80211_node *ni,
3138 struct ieee80211_tx_ampdu *tap, ieee80211_seq seqno)
3140 struct rt2860_softc *sc;
3141 struct ieee80211com *ic;
3142 struct ifnet *ifp;
3143 struct ieee80211vap *vap;
3144 struct ieee80211_frame_bar *bar;
3145 struct rt2860_softc_node *rni;
3146 struct mbuf *m;
3147 uint16_t barctl, barseqctl;
3148 uint8_t *frm;
3149 int ret, tid;
3151 ic = ni->ni_ic;
3152 ifp = ic->ic_ifp;
3153 sc = ifp->if_softc;
3154 vap = ni->ni_vap;
3155 rni = (struct rt2860_softc_node *) ni;
3157 if (!(tap->txa_flags & IEEE80211_AGGR_RUNNING))
3158 return EINVAL;
3160 m = ieee80211_getmgtframe(&frm, ic->ic_headroom, sizeof(struct ieee80211_frame_bar));
3161 if (m == NULL)
3162 return ENOMEM;
3164 bar = mtod(m, struct ieee80211_frame_bar *);
3166 bar->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR;
3167 bar->i_fc[1] = 0;
3169 IEEE80211_ADDR_COPY(bar->i_ra, ni->ni_macaddr);
3170 IEEE80211_ADDR_COPY(bar->i_ta, vap->iv_myaddr);
3172 tid = WME_AC_TO_TID(tap->txa_ac);
3174 barctl = (tap->txa_flags & IEEE80211_AGGR_IMMEDIATE ? 0 : IEEE80211_BAR_NOACK) |
3175 IEEE80211_BAR_COMP |
3176 RT2860_SM(tid, IEEE80211_BAR_TID);
3177 barseqctl = RT2860_SM(seqno, IEEE80211_BAR_SEQ_START);
3179 bar->i_ctl = htole16(barctl);
3180 bar->i_seq = htole16(barseqctl);
3182 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_bar);
3184 tap->txa_start = seqno;
3186 ieee80211_ref_node(ni);
3188 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
3189 "%s: sending BAR: associd=0x%04x, staid=0x%02x, tid=%d, seqno=%d\n",
3190 device_get_nameunit(sc->dev), ni->ni_associd, rni->staid, tid, seqno);
3192 ret = ic->ic_raw_xmit(ni, m, NULL);
3193 if (ret != 0)
3194 ieee80211_free_node(ni);
3196 return ret;
3200 * rt2860_amrr_update_iter_func
3202 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
3204 struct rt2860_softc *sc;
3205 struct ieee80211com *ic;
3206 struct ifnet *ifp;
3207 struct ieee80211vap *vap;
3208 struct rt2860_softc_vap *rvap;
3209 struct rt2860_softc_node *rni;
3210 uint8_t wcid;
3212 vap = arg;
3213 ic = vap->iv_ic;
3214 ifp = ic->ic_ifp;
3215 sc = ifp->if_softc;
3216 rvap = (struct rt2860_softc_vap *) vap;
3217 rni = (struct rt2860_softc_node *) ni;
3219 /* only associated stations */
3221 if ((ni->ni_vap == vap) && (rni->staid != 0))
3223 wcid = rni->staid;
3225 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
3226 "%s: AMRR node: staid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
3227 device_get_nameunit(sc->dev),
3228 rni->staid, sc->amrr_node[wcid].txcnt, sc->amrr_node[wcid].success, sc->amrr_node[wcid].retrycnt);
3230 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
3232 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
3233 "%s:%s node Tx rate: associd=0x%04x, staid=0x%02x, rate=0x%02x, max rate=0x%02x\n",
3234 device_get_nameunit(sc->dev),
3235 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
3236 ni->ni_associd, rni->staid, ni->ni_txrate,
3237 (ni->ni_flags & IEEE80211_NODE_HT) ?
3238 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
3239 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
3244 * rt2860_periodic
3246 static void rt2860_periodic(void *arg)
3248 struct rt2860_softc *sc;
3250 sc = arg;
3252 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
3253 "%s: periodic\n",
3254 device_get_nameunit(sc->dev));
3256 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
3260 * rt2860_tx_watchdog
3262 static void rt2860_tx_watchdog(void *arg)
3264 struct rt2860_softc *sc;
3265 struct ifnet *ifp;
3267 sc = arg;
3268 ifp = sc->ifp;
3270 if (sc->tx_timer == 0)
3271 return;
3273 if (--sc->tx_timer == 0)
3275 printf("%s: Tx watchdog timeout: resetting\n",
3276 device_get_nameunit(sc->dev));
3278 rt2860_stop_locked(sc);
3279 rt2860_init_locked(sc);
3281 ifp->if_oerrors++;
3283 sc->tx_watchdog_timeouts++;
3286 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
3290 * rt2860_staid_alloc
3292 static int rt2860_staid_alloc(struct rt2860_softc *sc, int aid)
3294 int staid;
3296 if ((aid > 0 && aid < RT2860_SOFTC_STAID_COUNT) && isclr(sc->staid_mask, aid))
3298 staid = aid;
3300 else
3302 for (staid = 1; staid < RT2860_SOFTC_STAID_COUNT; staid++)
3304 if (isclr(sc->staid_mask, staid))
3305 break;
3309 setbit(sc->staid_mask, staid);
3311 return staid;
3315 * rt2860_staid_delete
3317 static void rt2860_staid_delete(struct rt2860_softc *sc, int staid)
3319 clrbit(sc->staid_mask, staid);
3323 * rt2860_asic_set_bssid
3325 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
3326 const uint8_t *bssid)
3328 uint32_t tmp;
3330 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3331 "%s: set bssid: bssid=%s\n",
3332 device_get_nameunit(sc->dev),
3333 ether_sprintf(bssid));
3335 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
3337 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
3339 tmp = bssid[4] | (bssid[5] << 8);
3341 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
3345 * rt2860_asic_set_macaddr
3347 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
3348 const uint8_t *addr)
3350 uint32_t tmp;
3352 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3354 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
3356 tmp = addr[4] | (addr[5] << 8) | (0xff << 16);
3358 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
3362 * rt2860_asic_enable_tsf_sync
3364 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
3366 struct ifnet *ifp;
3367 struct ieee80211com *ic;
3368 struct ieee80211vap *vap;
3369 uint32_t tmp;
3371 ifp = sc->ifp;
3372 ic = ifp->if_l2com;
3373 vap = TAILQ_FIRST(&ic->ic_vaps);
3375 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
3376 "%s: enabling TSF\n",
3377 device_get_nameunit(sc->dev));
3379 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3381 tmp &= ~0x1fffff;
3382 tmp |= vap->iv_bss->ni_intval * 16;
3383 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
3385 if (vap->iv_opmode == IEEE80211_M_STA)
3387 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
3389 else if (vap->iv_opmode == IEEE80211_M_IBSS)
3391 tmp |= RT2860_REG_BCN_TX_ENABLE;
3392 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
3394 else if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3396 tmp |= RT2860_REG_BCN_TX_ENABLE;
3397 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
3400 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3404 * rt2860_asic_disable_tsf_sync
3406 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
3408 uint32_t tmp;
3410 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
3411 "%s: disabling TSF\n",
3412 device_get_nameunit(sc->dev));
3414 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3416 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3417 RT2860_REG_TSF_TIMER_ENABLE |
3418 RT2860_REG_TBTT_TIMER_ENABLE);
3420 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
3421 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
3423 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3427 * rt2860_asic_enable_mrr
3429 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
3431 #define CCK(mcs) (mcs)
3432 #define OFDM(mcs) ((1 << 3) | (mcs))
3433 #define HT(mcs) (mcs)
3435 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
3436 (OFDM(6) << 28) | /* 54 -> 48 */
3437 (OFDM(5) << 24) | /* 48 -> 36 */
3438 (OFDM(4) << 20) | /* 36 -> 24 */
3439 (OFDM(3) << 16) | /* 24 -> 18 */
3440 (OFDM(2) << 12) | /* 18 -> 12 */
3441 (OFDM(1) << 8) | /* 12 -> 9 */
3442 (OFDM(0) << 4) | /* 9 -> 6 */
3443 OFDM(0)); /* 6 -> 6 */
3445 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
3446 (CCK(2) << 12) | /* 11 -> 5.5 */
3447 (CCK(1) << 8) | /* 5.5 -> 2 */
3448 (CCK(0) << 4) | /* 2 -> 1 */
3449 CCK(0)); /* 1 -> 1 */
3451 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
3452 (HT(6) << 28) |
3453 (HT(5) << 24) |
3454 (HT(4) << 20) |
3455 (HT(3) << 16) |
3456 (HT(2) << 12) |
3457 (HT(1) << 8) |
3458 (HT(0) << 4) |
3459 HT(0));
3461 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
3462 (HT(14) << 28) |
3463 (HT(13) << 24) |
3464 (HT(12) << 20) |
3465 (HT(11) << 16) |
3466 (HT(10) << 12) |
3467 (HT(9) << 8) |
3468 (HT(8) << 4) |
3469 HT(7));
3471 #undef HT
3472 #undef OFDM
3473 #undef CCK
3477 * rt2860_asic_set_txpreamble
3479 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
3481 struct ifnet *ifp;
3482 struct ieee80211com *ic;
3483 uint32_t tmp;
3485 ifp = sc->ifp;
3486 ic = ifp->if_l2com;
3488 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3489 "%s: %s short Tx preamble\n",
3490 device_get_nameunit(sc->dev),
3491 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
3493 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
3495 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
3497 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3498 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
3500 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
3504 * rt2860_asic_set_basicrates
3506 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
3508 struct ifnet *ifp;
3509 struct ieee80211com *ic;
3511 ifp = sc->ifp;
3512 ic = ifp->if_l2com;
3514 if (ic->ic_curmode == IEEE80211_MODE_11B)
3515 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0xf);
3516 else if (ic->ic_curmode == IEEE80211_MODE_11A)
3517 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
3518 else
3519 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
3523 * rt2860_asic_update_rtsthreshold
3525 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
3527 struct ifnet *ifp;
3528 struct ieee80211com *ic;
3529 struct ieee80211vap *vap;
3530 uint32_t tmp;
3531 uint16_t threshold;
3533 ifp = sc->ifp;
3534 ic = ifp->if_l2com;
3535 vap = TAILQ_FIRST(&ic->ic_vaps);
3537 if (vap == NULL)
3538 threshold = IEEE80211_RTS_MAX;
3539 else if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
3540 threshold = 0x1000;
3541 else
3542 threshold = vap->iv_rtsthreshold;
3544 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3545 "%s: updating RTS threshold: %d\n",
3546 device_get_nameunit(sc->dev), threshold);
3548 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
3550 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
3552 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
3553 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
3555 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
3559 * rt2860_asic_update_txpower
3561 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
3563 struct ifnet *ifp;
3564 struct ieee80211com *ic;
3565 uint32_t *txpow_rate;
3566 int8_t delta;
3567 uint8_t val;
3568 uint32_t tmp;
3569 int i;
3571 ifp = sc->ifp;
3572 ic = ifp->if_l2com;
3574 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3575 "%s: updating Tx power: %d\n",
3576 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
3578 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
3580 txpow_rate = sc->txpow_rate_20mhz;
3582 else
3584 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3585 txpow_rate = sc->txpow_rate_40mhz_2ghz;
3586 else
3587 txpow_rate = sc->txpow_rate_40mhz_5ghz;
3590 delta = 0;
3592 val = rt2860_io_bbp_read(sc, 1);
3593 val &= 0xfc;
3595 if (ic->ic_txpowlimit > 90)
3597 /* do nothing */
3599 else if (ic->ic_txpowlimit > 60)
3601 delta -= 1;
3603 else if (ic->ic_txpowlimit > 30)
3605 delta -= 3;
3607 else if (ic->ic_txpowlimit > 15)
3609 val |= 0x1;
3611 else if (ic->ic_txpowlimit > 9)
3613 val |= 0x1;
3614 delta -= 3;
3616 else
3618 val |= 0x2;
3621 rt2860_io_bbp_write(sc, 1, val);
3623 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
3625 if (txpow_rate[i] == 0xffffffff)
3626 continue;
3628 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
3630 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
3635 * rt2860_asic_update_promisc
3637 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
3639 struct ifnet *ifp;
3640 uint32_t tmp;
3642 ifp = sc->ifp;
3644 printf("%s: %s promiscuous mode\n",
3645 device_get_nameunit(sc->dev),
3646 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
3648 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
3650 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
3652 if (!(ifp->if_flags & IFF_PROMISC))
3653 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
3655 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
3659 * rt2860_asic_updateprot
3661 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
3663 struct ifnet *ifp;
3664 struct ieee80211com *ic;
3665 struct ieee80211vap *vap;
3666 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
3667 uint8_t htopmode;
3668 enum ieee80211_protmode htprotmode;
3670 ifp = sc->ifp;
3671 ic = ifp->if_l2com;
3672 vap = TAILQ_FIRST(&ic->ic_vaps);
3674 /* CCK frame protection */
3676 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3677 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
3679 /* set up protection frame phy mode and rate (MCS code) */
3681 if (ic->ic_curmode == IEEE80211_MODE_11A)
3682 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3683 (0 << RT2860_REG_PROT_MCS_SHIFT);
3684 else
3685 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3686 (3 << RT2860_REG_PROT_MCS_SHIFT));
3688 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
3690 /* OFDM frame protection */
3692 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3693 RT2860_REG_TXOP_ALLOW_ALL;
3695 if (ic->ic_flags & IEEE80211_F_USEPROT)
3697 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3698 "%s: updating protection mode: b/g protection mode=%s\n",
3699 device_get_nameunit(sc->dev),
3700 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3701 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3703 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3704 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3705 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3706 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
3707 else
3708 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3710 else
3712 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3713 "%s: updating protection mode: b/g protection mode=%s\n",
3714 device_get_nameunit(sc->dev), "none");
3716 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3719 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
3721 /* HT frame protection */
3723 if ((vap != NULL) && (vap->iv_opmode == IEEE80211_M_STA) && (vap->iv_state == IEEE80211_S_RUN))
3724 htopmode = vap->iv_bss->ni_htopmode;
3725 else
3726 htopmode = ic->ic_curhtprotmode;
3728 htprotmode = ic->ic_htprotmode;
3730 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3732 if (sc->tx_ampdu_sessions > 0)
3734 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3735 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3736 device_get_nameunit(sc->dev));
3738 htopmode = IEEE80211_HTINFO_OPMODE_MIXED;
3739 htprotmode = IEEE80211_PROT_RTSCTS;
3742 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3743 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3744 device_get_nameunit(sc->dev),
3745 htopmode & IEEE80211_HTINFO_OPMODE,
3746 (htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3747 ((htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3749 switch (htopmode & IEEE80211_HTINFO_OPMODE)
3751 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3752 case IEEE80211_HTINFO_OPMODE_HT20PR:
3753 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3754 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3755 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3756 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3757 (4 << RT2860_REG_PROT_MCS_SHIFT);
3759 gf20_prot = mm20_prot;
3761 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
3762 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3763 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3765 if (htprotmode == IEEE80211_PROT_RTSCTS)
3766 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3767 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3768 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3769 else
3770 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3772 gf40_prot = mm40_prot;
3773 break;
3775 /* IEEE80211_HTINFO_OPMODE_MIXED */
3776 case IEEE80211_HTINFO_OPMODE_MIXED:
3777 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
3778 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3779 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
3781 if (ic->ic_flags & IEEE80211_F_USEPROT)
3782 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3783 (3 << RT2860_REG_PROT_MCS_SHIFT);
3784 else
3785 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3786 (4 << RT2860_REG_PROT_MCS_SHIFT);
3788 if (htprotmode == IEEE80211_PROT_RTSCTS)
3789 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3790 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3791 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
3792 else
3793 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
3795 gf20_prot = mm20_prot;
3797 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
3799 if (ic->ic_flags & IEEE80211_F_USEPROT)
3800 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3801 (3 << RT2860_REG_PROT_MCS_SHIFT);
3802 else
3803 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3804 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3806 if (htprotmode == IEEE80211_PROT_RTSCTS)
3807 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3808 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3809 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3810 else
3811 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3813 gf40_prot = mm40_prot;
3814 break;
3817 * IEEE80211_HTINFO_OPMODE_PURE
3818 * IEEE80211_HTINFO_OPMODE_PROTOPT
3820 case IEEE80211_HTINFO_OPMODE_PURE:
3821 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3822 default:
3823 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3824 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3825 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3826 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3827 (4 << RT2860_REG_PROT_MCS_SHIFT);
3829 gf20_prot = mm20_prot;
3831 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3832 RT2860_REG_TXOP_ALLOW_ALL |
3833 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3834 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3836 gf40_prot = mm40_prot;
3837 break;
3840 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3841 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3842 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3843 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3847 * rt2860_asic_updateslot
3849 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3851 struct ifnet *ifp;
3852 struct ieee80211com *ic;
3853 struct ieee80211vap *vap;
3854 uint32_t tmp;
3856 ifp = sc->ifp;
3857 ic = ifp->if_l2com;
3858 vap = TAILQ_FIRST(&ic->ic_vaps);
3860 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3861 "%s: %s short slot time\n",
3862 device_get_nameunit(sc->dev),
3863 ((ic->ic_flags & IEEE80211_F_SHSLOT) ||
3864 ((vap != NULL) && (vap->iv_flags & IEEE80211_F_BURST))) ? "enabling" : "disabling");
3866 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3868 tmp &= ~0xff;
3870 if ((ic->ic_flags & IEEE80211_F_SHSLOT) ||
3871 ((vap != NULL) && (vap->iv_flags & IEEE80211_F_BURST)))
3872 tmp |= IEEE80211_DUR_SHSLOT;
3873 else
3874 tmp |= IEEE80211_DUR_SLOT;
3876 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3880 * rt2860_asic_wme_update
3882 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3884 struct ifnet *ifp;
3885 struct ieee80211com *ic;
3886 struct ieee80211_wme_state *wme;
3887 const struct wmeParams *wmep;
3888 int i;
3890 ifp = sc->ifp;
3891 ic = ifp->if_l2com;
3892 wme = &ic->ic_wme;
3893 wmep = wme->wme_chanParams.cap_wmeParams;
3895 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3896 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3897 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3898 device_get_nameunit(sc->dev),
3899 wmep[WME_AC_VO].wmep_aifsn,
3900 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3901 wmep[WME_AC_VO].wmep_txopLimit,
3902 wmep[WME_AC_VI].wmep_aifsn,
3903 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3904 wmep[WME_AC_VI].wmep_txopLimit,
3905 wmep[WME_AC_BK].wmep_aifsn,
3906 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3907 wmep[WME_AC_BK].wmep_txopLimit,
3908 wmep[WME_AC_BE].wmep_aifsn,
3909 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3910 wmep[WME_AC_BE].wmep_txopLimit);
3912 for (i = 0; i < WME_NUM_AC; i++)
3913 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3914 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3915 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3917 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3918 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3919 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3921 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3922 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3923 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3925 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3926 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3927 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3929 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3930 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3932 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3933 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3937 * rt2860_asic_update_beacon
3939 static void rt2860_asic_update_beacon(struct rt2860_softc *sc,
3940 struct ieee80211vap *vap)
3942 struct rt2860_softc_vap *rvap;
3943 struct mbuf *m;
3944 struct rt2860_txwi *txwi;
3945 uint32_t tmp;
3947 rvap = (struct rt2860_softc_vap *) vap;
3949 m = rvap->beacon_mbuf;
3950 txwi = &rvap->beacon_txwi;
3952 /* disable temporarily TSF sync */
3954 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3956 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3957 RT2860_REG_TSF_TIMER_ENABLE |
3958 RT2860_REG_TBTT_TIMER_ENABLE);
3960 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3962 /* write Tx wireless info and beacon frame to on-chip memory */
3964 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3965 txwi, sizeof(struct rt2860_txwi));
3967 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3968 mtod(m, uint8_t *), m->m_pkthdr.len);
3970 /* enable again TSF sync */
3972 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3974 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3975 RT2860_REG_TSF_TIMER_ENABLE |
3976 RT2860_REG_TBTT_TIMER_ENABLE);
3978 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3982 * rt2860_asic_clear_keytables
3984 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3986 int i;
3988 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3990 for (i = 0; i < 256; i++)
3992 rt2860_io_mac_write(sc, RT2860_REG_WCID(i), 0xffffffff);
3993 rt2860_io_mac_write(sc, RT2860_REG_WCID(i) + 4, 0x0000ffff);
3996 /* clear WCID attribute table (entries = 256, entry size = 4) */
3998 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
4000 /* clear IV/EIV table (entries = 256, entry size = 8) */
4002 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
4004 /* clear pairwise key table (entries = 64, entry size = 32) */
4006 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 64);
4008 /* clear shared key table (entries = 32, entry size = 32) */
4010 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
4012 /* clear shared key mode (entries = 32, entry size = 2) */
4014 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
4018 * rt2860_asic_add_ba_session
4020 static void rt2860_asic_add_ba_session(struct rt2860_softc *sc,
4021 uint8_t wcid, int tid)
4023 uint32_t tmp;
4025 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
4026 "%s: adding BA session: wcid=0x%02x, tid=%d\n",
4027 device_get_nameunit(sc->dev), wcid, tid);
4029 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
4031 tmp |= (0x10000 << tid);
4033 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
4037 * rt2860_asic_del_ba_session
4039 static void rt2860_asic_del_ba_session(struct rt2860_softc *sc,
4040 uint8_t wcid, int tid)
4042 uint32_t tmp;
4044 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
4045 "%s: deleting BA session: wcid=0x%02x, tid=%d\n",
4046 device_get_nameunit(sc->dev), wcid, tid);
4048 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
4050 tmp &= ~(0x10000 << tid);
4052 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
4056 * rt2860_beacon_alloc
4058 static int rt2860_beacon_alloc(struct rt2860_softc *sc,
4059 struct ieee80211vap *vap)
4061 struct ieee80211com *ic;
4062 struct rt2860_softc_vap *rvap;
4063 struct mbuf *m;
4064 struct rt2860_txwi txwi;
4065 uint8_t rate, mcs;
4067 ic = vap->iv_ic;
4068 rvap = (struct rt2860_softc_vap *) vap;
4070 m = ieee80211_beacon_alloc(vap->iv_bss, &rvap->beacon_offsets);
4071 if (m == NULL)
4072 return ENOMEM;
4074 rate = IEEE80211_IS_CHAN_5GHZ(vap->iv_bss->ni_chan) ? 12 : 2;
4075 mcs = rt2860_rate2mcs(rate);
4077 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
4078 "%s: beacon allocate: mcs=0x%02x\n",
4079 device_get_nameunit(sc->dev), mcs);
4081 memset(&txwi, 0, sizeof(struct rt2860_txwi));
4083 txwi.wcid = RT2860_WCID_RESERVED;
4084 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
4085 RT2860_TXWI_MPDU_LEN_SHIFT);
4086 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
4087 txwi.mpdu_density_flags |=
4088 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
4089 txwi.bawin_size_xflags |=
4090 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
4092 if (rate == 2)
4094 txwi.phymode_ifs_stbc_shortgi =
4095 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
4097 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4098 mcs |= RT2860_TXWI_MCS_SHOTPRE;
4100 else
4102 txwi.phymode_ifs_stbc_shortgi =
4103 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
4106 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
4107 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
4109 if (rvap->beacon_mbuf != NULL)
4111 m_free(rvap->beacon_mbuf);
4112 rvap->beacon_mbuf = NULL;
4115 rvap->beacon_mbuf = m;
4116 rvap->beacon_txwi = txwi;
4118 return 0;
4122 * rt2860_rxrate
4124 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
4126 uint8_t mcs, phymode;
4127 uint8_t rate;
4129 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
4130 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4131 RT2860_RXWI_PHYMODE_MASK;
4133 rate = 2;
4135 switch (phymode)
4137 case RT2860_RXWI_PHYMODE_CCK:
4138 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
4140 case 0: rate = 2; break; /* 1 Mbps */
4141 case 1: rate = 4; break; /* 2 MBps */
4142 case 2: rate = 11; break; /* 5.5 Mbps */
4143 case 3: rate = 22; break; /* 11 Mbps */
4145 break;
4147 case RT2860_RXWI_PHYMODE_OFDM:
4148 switch (mcs)
4150 case 0: rate = 12; break; /* 6 Mbps */
4151 case 1: rate = 18; break; /* 9 Mbps */
4152 case 2: rate = 24; break; /* 12 Mbps */
4153 case 3: rate = 36; break; /* 18 Mbps */
4154 case 4: rate = 48; break; /* 24 Mbps */
4155 case 5: rate = 72; break; /* 36 Mbps */
4156 case 6: rate = 96; break; /* 48 Mbps */
4157 case 7: rate = 108; break; /* 54 Mbps */
4159 break;
4161 case RT2860_RXWI_PHYMODE_HT_MIXED:
4162 case RT2860_RXWI_PHYMODE_HT_GF:
4163 break;
4166 return rate;
4170 * rt2860_maxrssi_rxpath
4172 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
4173 const struct rt2860_rxwi *rxwi)
4175 uint8_t rxpath;
4177 rxpath = 0;
4179 if (sc->nrxpath > 1)
4180 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
4181 rxpath = 1;
4183 if (sc->nrxpath > 2)
4184 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
4185 rxpath = 2;
4187 return rxpath;
4191 * rt2860_rssi2dbm
4193 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
4194 uint8_t rssi, uint8_t rxpath)
4196 struct ifnet *ifp;
4197 struct ieee80211com *ic;
4198 struct ieee80211_channel *c;
4199 int chan;
4200 int8_t rssi_off, lna_gain;
4202 if (rssi == 0)
4203 return -99;
4205 ifp = sc->ifp;
4206 ic = ifp->if_l2com;
4207 c = ic->ic_curchan;
4208 chan = ieee80211_chan2ieee(ic, c);
4210 if (IEEE80211_IS_CHAN_5GHZ(c))
4212 rssi_off = sc->rssi_off_5ghz[rxpath];
4214 if (chan <= 64)
4215 lna_gain = sc->lna_gain[1];
4216 else if (chan <= 128)
4217 lna_gain = sc->lna_gain[2];
4218 else
4219 lna_gain = sc->lna_gain[3];
4221 else
4223 rssi_off = sc->rssi_off_2ghz[rxpath] - sc->lna_gain[0];
4224 lna_gain = sc->lna_gain[0];
4227 return (-12 - rssi_off - lna_gain - rssi);
4231 * rt2860_rate2mcs
4233 static uint8_t rt2860_rate2mcs(uint8_t rate)
4235 switch (rate)
4237 /* CCK rates */
4238 case 2: return 0;
4239 case 4: return 1;
4240 case 11: return 2;
4241 case 22: return 3;
4243 /* OFDM rates */
4244 case 12: return 0;
4245 case 18: return 1;
4246 case 24: return 2;
4247 case 36: return 3;
4248 case 48: return 4;
4249 case 72: return 5;
4250 case 96: return 6;
4251 case 108: return 7;
4254 return 0;
4258 * rt2860_tx_mgmt
4260 static int rt2860_tx_mgmt(struct rt2860_softc *sc,
4261 struct mbuf *m, struct ieee80211_node *ni, int qid)
4263 struct ifnet *ifp;
4264 struct ieee80211com *ic;
4265 struct ieee80211vap *vap;
4266 const struct ieee80211_txparam *tp;
4267 struct rt2860_softc_node *rni;
4268 struct rt2860_softc_tx_ring *ring;
4269 struct rt2860_softc_tx_data *data;
4270 struct rt2860_txdesc *desc;
4271 struct rt2860_txwi *txwi;
4272 struct ieee80211_frame *wh;
4273 struct rt2860_softc_tx_radiotap_header *tap;
4274 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
4275 u_int hdrsize, hdrspace;
4276 uint8_t rate, mcs, pid, qsel;
4277 uint16_t len, dmalen, mpdu_len, dur;
4278 int error, mimops, ndmasegs, ndescs, i, j;
4280 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4281 ("%s: Tx MGMT: invalid qid=%d\n",
4282 device_get_nameunit(sc->dev), qid));
4284 RT2860_SOFTC_TX_RING_ASSERT_LOCKED(&sc->tx_ring[qid]);
4286 ifp = sc->ifp;
4287 ic = ifp->if_l2com;
4288 vap = ni->ni_vap;
4289 rni = (struct rt2860_softc_node *) ni;
4290 tp = ni->ni_txparms;
4292 ring = &sc->tx_ring[qid];
4293 desc = &ring->desc[ring->desc_cur];
4294 data = &ring->data[ring->data_cur];
4295 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
4297 wh = mtod(m, struct ieee80211_frame *);
4299 rate = tp->mgmtrate & IEEE80211_RATE_VAL;
4301 /* fill Tx wireless info */
4303 if (ni->ni_flags & IEEE80211_NODE_HT)
4304 mcs = rate;
4305 else
4306 mcs = rt2860_rate2mcs(rate);
4308 /* calculate MPDU length without padding */
4310 hdrsize = ieee80211_anyhdrsize(wh);
4311 hdrspace = ieee80211_anyhdrspace(ic, wh);
4312 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
4314 memset(txwi, 0, sizeof(struct rt2860_txwi));
4316 /* management frames do not need encryption */
4318 txwi->wcid = RT2860_WCID_RESERVED;
4320 /* MIMO power save */
4322 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_flags & IEEE80211_NODE_MIMO_PS))
4324 if (mcs > 7)
4326 if (ni->ni_flags & IEEE80211_NODE_MIMO_RTS)
4328 /* dynamic MIMO power save */
4330 txwi->mpdu_density_flags |=
4331 (RT2860_TXWI_FLAGS_MIMOPS << RT2860_TXWI_FLAGS_SHIFT);
4333 else
4335 /* static MIMO power save */
4337 mcs = 7;
4341 mimops = 1;
4343 else
4345 mimops = 0;
4348 pid = (mcs < 0xf) ? (mcs + 1) : mcs;
4350 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
4351 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
4352 RT2860_TXWI_MPDU_LEN_SHIFT);
4354 if (ni->ni_flags & IEEE80211_NODE_HT)
4356 txwi->phymode_ifs_stbc_shortgi |=
4357 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
4359 else
4361 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
4363 txwi->phymode_ifs_stbc_shortgi |=
4364 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
4366 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4367 mcs |= RT2860_TXWI_MCS_SHOTPRE;
4369 else
4371 txwi->phymode_ifs_stbc_shortgi |=
4372 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
4376 txwi->bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
4377 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
4379 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
4381 /* skip ACKs for multicast frames */
4383 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
4385 txwi->bawin_size_xflags |=
4386 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
4388 if (ni->ni_flags & IEEE80211_NODE_HT)
4390 /* preamble + plcp + signal extension + SIFS */
4392 dur = 16 + 4 + 6 + 10;
4394 else
4396 dur = ieee80211_ack_duration(ic->ic_rt, rate,
4397 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
4400 *(uint16_t *) wh->i_dur = htole16(dur);
4403 /* ask MAC to insert timestamp into probe responses */
4405 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
4406 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
4407 txwi->mpdu_density_flags |=
4408 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
4410 if (ieee80211_radiotap_active_vap(vap))
4412 tap = &sc->txtap;
4414 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4415 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4416 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4417 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4418 tap->chan_maxpow = 0;
4420 if (ni->ni_flags & IEEE80211_NODE_HT)
4421 tap->rate = mcs | IEEE80211_RATE_MCS;
4422 else
4423 tap->rate = rate;
4425 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
4426 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4428 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4429 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4431 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4433 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4435 ieee80211_radiotap_tx(vap, m);
4437 wh->i_fc[1] |= IEEE80211_FC1_WEP;
4439 else
4441 ieee80211_radiotap_tx(vap, m);
4445 /* copy and trim 802.11 header */
4447 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
4448 m_adj(m, hdrspace);
4450 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4451 dma_seg, &ndmasegs, 0);
4452 if (error != 0)
4454 /* too many fragments, linearize */
4456 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4457 "%s: could not load mbuf DMA map, trying to linearize mbuf: ndmasegs=%d, len=%d, error=%d\n",
4458 device_get_nameunit(sc->dev), ndmasegs, m->m_pkthdr.len, error);
4460 m = m_defrag(m, M_DONTWAIT);
4461 if (m == NULL)
4462 return ENOMEM;
4464 sc->tx_defrag_packets++;
4466 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4467 dma_seg, &ndmasegs, 0);
4468 if (error != 0)
4470 printf("%s: could not load mbuf DMA map: ndmasegs=%d, len=%d, error=%d\n",
4471 device_get_nameunit(sc->dev), ndmasegs, m->m_pkthdr.len, error);
4472 m_freem(m);
4473 return error;
4477 if (m->m_pkthdr.len == 0)
4478 ndmasegs = 0;
4480 /* determine how many Tx descs are required */
4482 ndescs = 1 + ndmasegs / 2;
4483 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
4485 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4486 "%s: there are not enough Tx descs\n",
4487 device_get_nameunit(sc->dev));
4489 sc->no_tx_desc_avail++;
4491 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4492 m_freem(m);
4493 return EFBIG;
4496 data->m = m;
4497 data->ni = ni;
4499 /* set up Tx descs */
4501 /* first segment is Tx wireless info and 802.11 header */
4503 len = sizeof(struct rt2860_txwi) + hdrsize;
4505 /* align end on a 4-bytes boundary */
4507 dmalen = (len + 3) & ~ 3;
4509 memset((caddr_t) txwi + len, 0, dmalen - len);
4511 qsel = RT2860_TXDESC_QSEL_EDCA;
4513 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
4514 desc->sdl0 = htole16(dmalen);
4515 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4517 /* set up payload segments */
4519 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
4521 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4522 desc->sdl1 = htole16(dma_seg[j].ds_len);
4524 ring->desc_queued++;
4525 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4527 j++;
4529 desc = &ring->desc[ring->desc_cur];
4531 desc->sdp0 = htole32(dma_seg[j].ds_addr);
4532 desc->sdl0 = htole16(dma_seg[j].ds_len);
4533 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4535 j++;
4538 /* finalize last payload segment */
4540 if (i > 0)
4542 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4543 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
4545 else
4547 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
4548 desc->sdl1 = 0;
4551 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4552 "%s: sending MGMT frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4553 "mcs=%d, mimops=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
4554 device_get_nameunit(sc->dev),
4555 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
4556 mcs, mimops, dmalen, ndmasegs,
4557 (int) dma_seg[0].ds_len, (int) dma_seg[1].ds_len, (int) dma_seg[2].ds_len, (int) dma_seg[3].ds_len, (int) dma_seg[4].ds_len);
4559 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
4560 BUS_DMASYNC_PREWRITE);
4561 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4562 BUS_DMASYNC_PREWRITE);
4563 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4564 BUS_DMASYNC_PREWRITE);
4566 ring->desc_queued++;
4567 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4569 ring->data_queued++;
4570 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4572 /* kick Tx */
4574 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
4576 return 0;
4580 * rt2860_tx_data
4582 static int rt2860_tx_data(struct rt2860_softc *sc,
4583 struct mbuf *m, struct ieee80211_node *ni, int qid)
4585 struct ifnet *ifp;
4586 struct ieee80211com *ic;
4587 struct ieee80211vap *vap;
4588 const struct ieee80211_txparam *tp;
4589 struct rt2860_softc_node *rni;
4590 struct rt2860_softc_tx_ring *ring;
4591 struct rt2860_softc_tx_data *data;
4592 struct rt2860_txdesc *desc;
4593 struct rt2860_txwi *txwi;
4594 struct ieee80211_frame *wh;
4595 struct ieee80211_tx_ampdu *tx_ampdu;
4596 ieee80211_seq seqno;
4597 struct rt2860_softc_tx_radiotap_header *tap;
4598 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
4599 u_int hdrsize, hdrspace;
4600 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, mpdu_density, bawin_size, qsel;
4601 uint16_t qos, len, dmalen, mpdu_len, dur;
4602 int error, hasqos, ac, tid, ampdu, mimops, ndmasegs, ndescs, i, j;
4604 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4605 ("%s: Tx data: invalid qid=%d\n",
4606 device_get_nameunit(sc->dev), qid));
4608 RT2860_SOFTC_TX_RING_ASSERT_LOCKED(&sc->tx_ring[qid]);
4610 ifp = sc->ifp;
4611 ic = ifp->if_l2com;
4612 vap = ni->ni_vap;
4613 rni = (struct rt2860_softc_node *) ni;
4614 tp = ni->ni_txparms;
4616 ring = &sc->tx_ring[qid];
4617 desc = &ring->desc[ring->desc_cur];
4618 data = &ring->data[ring->data_cur];
4619 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
4621 wh = mtod(m, struct ieee80211_frame *);
4623 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
4625 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
4626 if (hasqos)
4628 if (IEEE80211_HAS_ADDR4(wh))
4629 qos = le16toh(*(const uint16_t *)
4630 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
4631 else
4632 qos = le16toh(*(const uint16_t *)
4633 (((struct ieee80211_qosframe *) wh)->i_qos));
4635 else
4637 qos = 0;
4640 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
4641 rate = tp->mcastrate;
4642 else if (m->m_flags & M_EAPOL)
4643 rate = tp->mgmtrate;
4644 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
4645 rate = tp->ucastrate;
4646 else
4647 rate = ni->ni_txrate;
4649 rate &= IEEE80211_RATE_VAL;
4651 /* fill Tx wireless info */
4653 if (ni->ni_flags & IEEE80211_NODE_HT)
4654 mcs = rate;
4655 else
4656 mcs = rt2860_rate2mcs(rate);
4658 if (type == IEEE80211_FC0_TYPE_DATA)
4659 wcid = !IEEE80211_IS_MULTICAST(wh->i_addr1) ? rni->staid : RT2860_WCID_MCAST;
4660 else
4661 wcid = RT2860_WCID_RESERVED;
4663 /* calculate MPDU length without padding */
4665 hdrsize = ieee80211_anyhdrsize(wh);
4666 hdrspace = ieee80211_anyhdrspace(ic, wh);
4667 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
4669 memset(txwi, 0, sizeof(struct rt2860_txwi));
4671 txwi->wcid = wcid;
4673 /* MIMO power save */
4675 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_flags & IEEE80211_NODE_MIMO_PS))
4677 if (mcs > 7)
4679 if (ni->ni_flags & IEEE80211_NODE_MIMO_RTS)
4681 /* dynamic MIMO power save */
4683 txwi->mpdu_density_flags |=
4684 (RT2860_TXWI_FLAGS_MIMOPS << RT2860_TXWI_FLAGS_SHIFT);
4686 else
4688 /* static MIMO power save */
4690 mcs = 7;
4694 mimops = 1;
4696 else
4698 mimops = 0;
4701 pid = (mcs < 0xf) ? (mcs + 1) : mcs;
4703 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
4704 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
4705 RT2860_TXWI_MPDU_LEN_SHIFT);
4707 stbc = sc->tx_stbc && (mcs <= 7) && (vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) &&
4708 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC);
4710 shortgi = ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20) && (ni->ni_flags & IEEE80211_NODE_SGI20) && (ni->ni_chw == 20)) ||
4711 ((vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40) && (ni->ni_flags & IEEE80211_NODE_SGI40) && (ni->ni_chw == 40));
4713 txwi->phymode_ifs_stbc_shortgi |=
4714 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
4715 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
4717 if (ni->ni_flags & IEEE80211_NODE_HT)
4719 txwi->phymode_ifs_stbc_shortgi |=
4720 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
4722 else
4724 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
4726 txwi->phymode_ifs_stbc_shortgi |=
4727 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
4729 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4730 mcs |= RT2860_TXWI_MCS_SHOTPRE;
4732 else
4734 txwi->phymode_ifs_stbc_shortgi |=
4735 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
4739 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_chw == 40))
4740 bw = RT2860_TXWI_BW_40;
4741 else
4742 bw = RT2860_TXWI_BW_20;
4744 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
4745 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
4747 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
4749 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
4750 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
4752 txwi->bawin_size_xflags |=
4753 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
4755 if (ni->ni_flags & IEEE80211_NODE_HT)
4757 /* preamble + plcp + signal extension + SIFS */
4759 dur = 16 + 4 + 6 + 10;
4761 else
4763 dur = ieee80211_ack_duration(ic->ic_rt, rate,
4764 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
4767 *(uint16_t *) wh->i_dur = htole16(dur);
4770 /* check for A-MPDU */
4772 if (m->m_flags & M_AMPDU_MPDU)
4774 ac = M_WME_GETAC(m);
4775 tid = WME_AC_TO_TID(ac);
4776 tx_ampdu = &ni->ni_tx_ampdu[ac];
4778 mpdu_density = RT2860_MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4779 bawin_size = tx_ampdu->txa_wnd;
4781 txwi->mpdu_density_flags |=
4782 ((mpdu_density & RT2860_TXWI_MPDU_DENSITY_MASK) << RT2860_TXWI_MPDU_DENSITY_SHIFT) |
4783 (RT2860_TXWI_FLAGS_AMPDU << RT2860_TXWI_FLAGS_SHIFT);
4785 txwi->bawin_size_xflags |=
4786 ((bawin_size & RT2860_TXWI_BAWIN_SIZE_MASK) << RT2860_TXWI_BAWIN_SIZE_SHIFT);
4788 seqno = ni->ni_txseqs[tid]++;
4790 *(uint16_t *) &wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
4792 ampdu = 1;
4794 else
4796 mpdu_density = 0;
4797 bawin_size = 0;
4798 ampdu = 0;
4801 /* ask MAC to insert timestamp into probe responses */
4803 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
4804 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
4805 txwi->mpdu_density_flags |=
4806 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
4808 if (ieee80211_radiotap_active_vap(vap))
4810 tap = &sc->txtap;
4812 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4813 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4814 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4815 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4816 tap->chan_maxpow = 0;
4818 if (ni->ni_flags & IEEE80211_NODE_HT)
4819 tap->rate = mcs | IEEE80211_RATE_MCS;
4820 else
4821 tap->rate = rate;
4823 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
4824 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4826 if (shortgi)
4827 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4829 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4830 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4832 /* XXX use temporarily radiotap CFP flag as A-MPDU flag */
4834 if (ampdu)
4835 tap->flags |= IEEE80211_RADIOTAP_F_CFP;
4837 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4839 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4841 ieee80211_radiotap_tx(vap, m);
4843 wh->i_fc[1] |= IEEE80211_FC1_WEP;
4845 else
4847 ieee80211_radiotap_tx(vap, m);
4851 /* copy and trim 802.11 header */
4853 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
4854 m_adj(m, hdrspace);
4856 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4857 dma_seg, &ndmasegs, 0);
4858 if (error != 0)
4860 /* too many fragments, linearize */
4862 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4863 "%s: could not load mbuf DMA map, trying to linearize mbuf: ndmasegs=%d, len=%d, error=%d\n",
4864 device_get_nameunit(sc->dev), ndmasegs, m->m_pkthdr.len, error);
4866 m = m_defrag(m, M_DONTWAIT);
4867 if (m == NULL)
4868 return ENOMEM;
4870 sc->tx_defrag_packets++;
4872 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4873 dma_seg, &ndmasegs, 0);
4874 if (error != 0)
4876 printf("%s: could not load mbuf DMA map: ndmasegs=%d, len=%d, error=%d\n",
4877 device_get_nameunit(sc->dev), ndmasegs, m->m_pkthdr.len, error);
4878 m_freem(m);
4879 return error;
4883 if (m->m_pkthdr.len == 0)
4884 ndmasegs = 0;
4886 /* determine how many Tx descs are required */
4888 ndescs = 1 + ndmasegs / 2;
4889 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
4891 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4892 "%s: there are not enough Tx descs\n",
4893 device_get_nameunit(sc->dev));
4895 sc->no_tx_desc_avail++;
4897 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4898 m_freem(m);
4899 return EFBIG;
4902 data->m = m;
4903 data->ni = ni;
4905 /* set up Tx descs */
4907 /* first segment is Tx wireless info and 802.11 header */
4909 len = sizeof(struct rt2860_txwi) + hdrsize;
4911 /* align end on a 4-bytes boundary */
4913 dmalen = (len + 3) & ~ 3;
4915 memset((caddr_t) txwi + len, 0, dmalen - len);
4917 qsel = RT2860_TXDESC_QSEL_EDCA;
4919 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
4920 desc->sdl0 = htole16(dmalen);
4921 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4923 /* set up payload segments */
4925 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
4927 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4928 desc->sdl1 = htole16(dma_seg[j].ds_len);
4930 ring->desc_queued++;
4931 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4933 j++;
4935 desc = &ring->desc[ring->desc_cur];
4937 desc->sdp0 = htole32(dma_seg[j].ds_addr);
4938 desc->sdl0 = htole16(dma_seg[j].ds_len);
4939 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4941 j++;
4944 /* finalize last payload segment */
4946 if (i > 0)
4948 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4949 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
4951 else
4953 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
4954 desc->sdl1 = 0;
4957 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4958 "%s: sending data: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4959 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, "
4960 "ampdu=%d (density=%d, winsize=%d), mimops=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
4961 device_get_nameunit(sc->dev),
4962 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
4963 bw, stbc, shortgi, mcs, wcid, ampdu, mpdu_density, bawin_size, mimops, dmalen, ndmasegs,
4964 (int) dma_seg[0].ds_len, (int) dma_seg[1].ds_len, (int) dma_seg[2].ds_len, (int) dma_seg[3].ds_len, (int) dma_seg[4].ds_len);
4966 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
4967 BUS_DMASYNC_PREWRITE);
4968 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4969 BUS_DMASYNC_PREWRITE);
4970 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4971 BUS_DMASYNC_PREWRITE);
4973 ring->desc_queued++;
4974 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4976 ring->data_queued++;
4977 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4979 /* kick Tx */
4981 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
4983 return 0;
4987 * rt2860_tx_raw
4989 static int rt2860_tx_raw(struct rt2860_softc *sc,
4990 struct mbuf *m, struct ieee80211_node *ni,
4991 const struct ieee80211_bpf_params *params)
4993 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4994 "%s: Tx raw\n",
4995 device_get_nameunit(sc->dev));
4997 return 0;
5001 * rt2860_intr
5003 static void rt2860_intr(void *arg)
5005 struct rt2860_softc *sc;
5006 struct ifnet *ifp;
5007 uint32_t status;
5009 sc = arg;
5010 ifp = sc->ifp;
5012 /* acknowledge interrupts */
5014 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
5015 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
5017 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5018 "%s: interrupt: status = 0x%08x\n",
5019 device_get_nameunit(sc->dev), status);
5021 if (status == 0xffffffff || /* device likely went away */
5022 status == 0) /* not for us */
5023 return;
5025 sc->interrupts++;
5027 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
5028 return;
5030 if (status & RT2860_REG_INT_TX_COHERENT)
5031 rt2860_tx_coherent_intr(sc);
5033 if (status & RT2860_REG_INT_RX_COHERENT)
5034 rt2860_rx_coherent_intr(sc);
5036 if (status & RT2860_REG_INT_TXRX_COHERENT)
5037 rt2860_txrx_coherent_intr(sc);
5039 if (status & RT2860_REG_INT_FIFO_STA_FULL)
5040 rt2860_fifo_sta_full_intr(sc);
5042 if (status & RT2860_REG_INT_TX_MGMT_DONE)
5043 rt2860_tx_intr(sc, 5);
5045 if (status & RT2860_REG_INT_RX_DONE)
5046 rt2860_rx_intr(sc);
5048 if (status & RT2860_REG_INT_RX_DELAY_DONE)
5049 rt2860_rx_delay_intr(sc);
5051 if (status & RT2860_REG_INT_TX_HCCA_DONE)
5052 rt2860_tx_intr(sc, 4);
5054 if (status & RT2860_REG_INT_TX_AC3_DONE)
5055 rt2860_tx_intr(sc, 3);
5057 if (status & RT2860_REG_INT_TX_AC2_DONE)
5058 rt2860_tx_intr(sc, 2);
5060 if (status & RT2860_REG_INT_TX_AC1_DONE)
5061 rt2860_tx_intr(sc, 1);
5063 if (status & RT2860_REG_INT_TX_AC0_DONE)
5064 rt2860_tx_intr(sc, 0);
5066 if (status & RT2860_REG_INT_TX_DELAY_DONE)
5067 rt2860_tx_delay_intr(sc);
5069 if (status & RT2860_REG_INT_PRE_TBTT)
5070 rt2860_pre_tbtt_intr(sc);
5072 if (status & RT2860_REG_INT_TBTT)
5073 rt2860_tbtt_intr(sc);
5075 if (status & RT2860_REG_INT_MCU_CMD)
5076 rt2860_mcu_cmd_intr(sc);
5078 if (status & RT2860_REG_INT_AUTO_WAKEUP)
5079 rt2860_auto_wakeup_intr(sc);
5081 if (status & RT2860_REG_INT_GP_TIMER)
5082 rt2860_gp_timer_intr(sc);
5086 * rt2860_tx_coherent_intr
5088 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
5090 uint32_t tmp;
5091 int i;
5093 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5094 "%s: Tx coherent interrupt\n",
5095 device_get_nameunit(sc->dev));
5097 sc->tx_coherent_interrupts++;
5099 /* restart DMA engine */
5101 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
5103 tmp &= ~(RT2860_REG_TX_WB_DDONE |
5104 RT2860_REG_RX_DMA_ENABLE |
5105 RT2860_REG_TX_DMA_ENABLE);
5107 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5109 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
5111 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
5112 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
5114 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
5116 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
5117 sc->tx_ring[i].desc_phys_addr);
5118 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
5119 RT2860_SOFTC_TX_RING_DESC_COUNT);
5120 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
5123 /* init Rx ring */
5125 rt2860_reset_rx_ring(sc, &sc->rx_ring);
5127 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
5128 sc->rx_ring.desc_phys_addr);
5129 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
5130 RT2860_SOFTC_RX_RING_DATA_COUNT);
5131 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5132 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
5134 rt2860_txrx_enable(sc);
5138 * rt2860_rx_coherent_intr
5140 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
5142 uint32_t tmp;
5143 int i;
5145 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5146 "%s: Rx coherent interrupt\n",
5147 device_get_nameunit(sc->dev));
5149 sc->rx_coherent_interrupts++;
5151 /* restart DMA engine */
5153 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
5155 tmp &= ~(RT2860_REG_TX_WB_DDONE |
5156 RT2860_REG_RX_DMA_ENABLE |
5157 RT2860_REG_TX_DMA_ENABLE);
5159 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5161 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
5163 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
5164 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
5166 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
5168 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
5169 sc->tx_ring[i].desc_phys_addr);
5170 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
5171 RT2860_SOFTC_TX_RING_DESC_COUNT);
5172 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
5175 /* init Rx ring */
5177 rt2860_reset_rx_ring(sc, &sc->rx_ring);
5179 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
5180 sc->rx_ring.desc_phys_addr);
5181 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
5182 RT2860_SOFTC_RX_RING_DATA_COUNT);
5183 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5184 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
5186 rt2860_txrx_enable(sc);
5190 * rt2860_txrx_coherent_intr
5192 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
5194 uint32_t tmp;
5195 int i;
5197 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5198 "%s: Tx/Rx coherent interrupt\n",
5199 device_get_nameunit(sc->dev));
5201 sc->txrx_coherent_interrupts++;
5203 /* restart DMA engine */
5205 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
5207 tmp &= ~(RT2860_REG_TX_WB_DDONE |
5208 RT2860_REG_RX_DMA_ENABLE |
5209 RT2860_REG_TX_DMA_ENABLE);
5211 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5213 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
5215 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
5216 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
5218 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
5220 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
5221 sc->tx_ring[i].desc_phys_addr);
5222 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
5223 RT2860_SOFTC_TX_RING_DESC_COUNT);
5224 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
5227 /* init Rx ring */
5229 rt2860_reset_rx_ring(sc, &sc->rx_ring);
5231 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
5232 sc->rx_ring.desc_phys_addr);
5233 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
5234 RT2860_SOFTC_RX_RING_DATA_COUNT);
5235 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5236 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
5238 rt2860_txrx_enable(sc);
5242 * rt2860_fifo_sta_full_intr
5244 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
5246 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5247 "%s: FIFO statistic full interrupt\n",
5248 device_get_nameunit(sc->dev));
5250 sc->fifo_sta_full_interrupts++;
5252 RT2860_SOFTC_LOCK(sc);
5254 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
5256 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
5258 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
5261 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
5263 RT2860_SOFTC_UNLOCK(sc);
5267 * rt2860_rx_intr
5269 static void rt2860_rx_intr(struct rt2860_softc *sc)
5271 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5272 "%s: Rx interrupt\n",
5273 device_get_nameunit(sc->dev));
5275 sc->rx_interrupts++;
5277 RT2860_SOFTC_LOCK(sc);
5279 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
5281 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
5283 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
5286 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
5288 RT2860_SOFTC_UNLOCK(sc);
5292 * rt2860_rx_delay_intr
5294 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
5296 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5297 "%s: Rx delay interrupt\n",
5298 device_get_nameunit(sc->dev));
5300 sc->rx_delay_interrupts++;
5304 * rt2860_tx_intr
5306 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
5308 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
5309 ("%s: Tx interrupt: invalid qid=%d\n",
5310 device_get_nameunit(sc->dev), qid));
5312 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5313 "%s: Tx interrupt: qid=%d\n",
5314 device_get_nameunit(sc->dev), qid);
5316 sc->tx_interrupts[qid]++;
5318 RT2860_SOFTC_LOCK(sc);
5320 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
5322 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
5324 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
5327 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
5329 RT2860_SOFTC_UNLOCK(sc);
5333 * rt2860_tx_delay_intr
5335 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
5337 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5338 "%s: Tx delay interrupt\n",
5339 device_get_nameunit(sc->dev));
5341 sc->tx_delay_interrupts++;
5345 * rt2860_pre_tbtt_intr
5347 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
5349 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5350 "%s: Pre-TBTT interrupt\n",
5351 device_get_nameunit(sc->dev));
5353 sc->pre_tbtt_interrupts++;
5357 * rt2860_tbtt_intr
5359 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
5361 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5362 "%s: TBTT interrupt\n",
5363 device_get_nameunit(sc->dev));
5365 sc->tbtt_interrupts++;
5369 * rt2860_mcu_cmd_intr
5371 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
5373 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5374 "%s: MCU command interrupt\n",
5375 device_get_nameunit(sc->dev));
5377 sc->mcu_cmd_interrupts++;
5381 * rt2860_auto_wakeup_intr
5383 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
5385 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5386 "%s: auto wakeup interrupt\n",
5387 device_get_nameunit(sc->dev));
5389 sc->auto_wakeup_interrupts++;
5393 * rt2860_gp_timer_intr
5395 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
5397 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
5398 "%s: GP timer interrupt\n",
5399 device_get_nameunit(sc->dev));
5401 sc->gp_timer_interrupts++;
5405 * rt2860_rx_done_task
5407 static void rt2860_rx_done_task(void *context, int pending)
5409 struct rt2860_softc *sc;
5410 struct ifnet *ifp;
5411 int again;
5413 sc = context;
5414 ifp = sc->ifp;
5416 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5417 "%s: Rx done task\n",
5418 device_get_nameunit(sc->dev));
5420 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
5421 return;
5423 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
5425 again = rt2860_rx_eof(sc, sc->rx_process_limit);
5427 RT2860_SOFTC_LOCK(sc);
5429 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
5431 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5432 "%s: Rx done task: scheduling again\n",
5433 device_get_nameunit(sc->dev));
5435 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
5437 else
5439 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
5442 RT2860_SOFTC_UNLOCK(sc);
5446 * rt2860_tx_done_task
5448 static void rt2860_tx_done_task(void *context, int pending)
5450 struct rt2860_softc *sc;
5451 struct ifnet *ifp;
5452 uint32_t intr_mask;
5453 int i;
5455 sc = context;
5456 ifp = sc->ifp;
5458 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
5459 "%s: Tx done task\n",
5460 device_get_nameunit(sc->dev));
5462 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
5463 return;
5465 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
5467 if (sc->intr_pending_mask & (RT2860_REG_INT_TX_AC0_DONE << i))
5469 sc->intr_pending_mask &= ~(RT2860_REG_INT_TX_AC0_DONE << i);
5471 rt2860_tx_eof(sc, &sc->tx_ring[i]);
5475 sc->tx_timer = 0;
5477 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5479 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
5480 RT2860_REG_INT_TX_HCCA_DONE |
5481 RT2860_REG_INT_TX_AC3_DONE |
5482 RT2860_REG_INT_TX_AC2_DONE |
5483 RT2860_REG_INT_TX_AC1_DONE |
5484 RT2860_REG_INT_TX_AC0_DONE);
5486 RT2860_SOFTC_LOCK(sc);
5488 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
5489 (sc->intr_disable_mask & intr_mask));
5491 if (sc->intr_pending_mask & intr_mask)
5493 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
5494 "%s: Tx done task: scheduling again\n",
5495 device_get_nameunit(sc->dev));
5497 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
5500 RT2860_SOFTC_UNLOCK(sc);
5502 if (!IFQ_IS_EMPTY(&ifp->if_snd))
5503 rt2860_start(ifp);
5507 * rt2860_fifo_sta_full_task
5509 static void rt2860_fifo_sta_full_task(void *context, int pending)
5511 struct rt2860_softc *sc;
5512 struct ifnet *ifp;
5514 sc = context;
5515 ifp = sc->ifp;
5517 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5518 "%s: FIFO statistic full task\n",
5519 device_get_nameunit(sc->dev));
5521 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
5522 return;
5524 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
5526 rt2860_drain_fifo_stats(sc);
5528 RT2860_SOFTC_LOCK(sc);
5530 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
5532 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5533 "%s: FIFO statistic full task: scheduling again\n",
5534 device_get_nameunit(sc->dev));
5536 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
5538 else
5540 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
5543 RT2860_SOFTC_UNLOCK(sc);
5545 if (!IFQ_IS_EMPTY(&ifp->if_snd))
5546 rt2860_start(ifp);
5550 * rt2860_periodic_task
5552 static void rt2860_periodic_task(void *context, int pending)
5554 struct rt2860_softc *sc;
5555 struct ifnet *ifp;
5556 struct ieee80211com *ic;
5557 struct ieee80211vap *vap;
5559 sc = context;
5560 ifp = sc->ifp;
5561 ic = ifp->if_l2com;
5562 vap = TAILQ_FIRST(&ic->ic_vaps);
5564 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
5565 "%s: periodic task: round=%lu\n",
5566 device_get_nameunit(sc->dev), sc->periodic_round);
5568 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
5569 return;
5571 RT2860_SOFTC_LOCK(sc);
5573 sc->periodic_round++;
5575 rt2860_update_stats(sc);
5577 if ((sc->periodic_round % 10) == 0)
5579 rt2860_bbp_tuning(sc);
5581 rt2860_update_raw_counters(sc);
5583 rt2860_watchdog(sc);
5585 if (vap != NULL && vap->iv_opmode != IEEE80211_M_MONITOR && vap->iv_state == IEEE80211_S_RUN)
5587 if (vap->iv_opmode == IEEE80211_M_STA)
5588 rt2860_amrr_update_iter_func(vap, vap->iv_bss);
5589 else
5590 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, vap);
5594 RT2860_SOFTC_UNLOCK(sc);
5596 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
5600 * rt2860_rx_eof
5602 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
5604 struct ifnet *ifp;
5605 struct ieee80211com *ic;
5606 struct ieee80211_frame *wh;
5607 struct ieee80211_node *ni;
5608 struct rt2860_softc_node *rni;
5609 struct rt2860_softc_rx_radiotap_header *tap;
5610 struct rt2860_softc_rx_ring *ring;
5611 struct rt2860_rxdesc *desc;
5612 struct rt2860_softc_rx_data *data;
5613 struct rt2860_rxwi *rxwi;
5614 struct mbuf *m, *mnew;
5615 bus_dma_segment_t segs[1];
5616 bus_dmamap_t dma_map;
5617 uint32_t index, desc_flags;
5618 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, stbc, mcs, keyidx, tid, frag;
5619 uint16_t seq;
5620 int8_t rssi_dbm;
5621 int error, nsegs, len, ampdu, amsdu, rssi_dbm_rel, nframes, i;
5623 ifp = sc->ifp;
5624 ic = ifp->if_l2com;
5625 ring = &sc->rx_ring;
5627 nframes = 0;
5629 while (limit != 0)
5631 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
5632 if (ring->cur == index)
5633 break;
5635 desc = &ring->desc[ring->cur];
5636 data = &ring->data[ring->cur];
5638 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5639 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
5641 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
5642 break;
5644 nframes++;
5646 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5647 if (mnew == NULL)
5649 sc->rx_mbuf_alloc_errors++;
5650 ifp->if_ierrors++;
5651 goto skip;
5654 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
5656 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, ring->spare_dma_map,
5657 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
5658 if (error != 0)
5660 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5661 "%s: could not load Rx mbuf DMA map: error=%d, nsegs=%d\n",
5662 device_get_nameunit(sc->dev), error, nsegs);
5664 m_freem(mnew);
5666 sc->rx_mbuf_dmamap_errors++;
5667 ifp->if_ierrors++;
5669 goto skip;
5672 KASSERT(nsegs == 1, ("%s: too many DMA segments",
5673 device_get_nameunit(sc->dev)));
5675 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5676 BUS_DMASYNC_POSTREAD);
5677 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5679 dma_map = data->dma_map;
5680 data->dma_map = ring->spare_dma_map;
5681 ring->spare_dma_map = dma_map;
5683 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5684 BUS_DMASYNC_PREREAD);
5686 m = data->m;
5688 data->m = mnew;
5689 desc->sdp0 = htole32(segs[0].ds_addr);
5691 desc_flags = le32toh(desc->flags);
5693 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5694 "%s: Rx frame: rxdesc flags=0x%08x\n",
5695 device_get_nameunit(sc->dev), desc_flags);
5697 /* get Rx wireless info */
5699 rxwi = mtod(m, struct rt2860_rxwi *);
5700 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
5701 RT2860_RXWI_SIZE_MASK;
5703 /* check for L2 padding between IEEE 802.11 frame header and body */
5705 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
5707 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5708 "%s: L2 padding: len=%d\n",
5709 device_get_nameunit(sc->dev), len);
5711 len += 2;
5714 m->m_pkthdr.rcvif = ifp;
5715 m->m_data = (caddr_t) (rxwi + 1);
5716 m->m_pkthdr.len = m->m_len = len;
5718 /* check for crc errors */
5720 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
5722 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5723 "%s: rxdesc: crc error\n",
5724 device_get_nameunit(sc->dev));
5726 ifp->if_ierrors++;
5728 if (!(ifp->if_flags & IFF_PROMISC))
5730 m_freem(m);
5731 goto skip;
5735 wh = (struct ieee80211_frame *) (rxwi + 1);
5737 /* check for cipher errors */
5739 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
5741 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
5742 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
5743 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
5745 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
5746 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
5748 m->m_flags |= M_WEP;
5750 sc->rx_cipher_no_errors++;
5752 else
5754 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5755 "%s: rxdesc: cipher error=0x%02x\n",
5756 device_get_nameunit(sc->dev), cipher_err);
5758 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_ICV)
5759 sc->rx_cipher_icv_errors++;
5760 else if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_MIC)
5761 sc->rx_cipher_mic_errors++;
5762 else if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_INVALID_KEY)
5763 sc->rx_cipher_invalid_key_errors++;
5765 if ((cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_MIC) &&
5766 (desc_flags & RT2860_RXDESC_FLAGS_MYBSS))
5768 ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
5769 if (ni != NULL)
5771 keyidx = (rxwi->udf_bssidx_keyidx >> RT2860_RXWI_KEYIDX_SHIFT) &
5772 RT2860_RXWI_KEYIDX_MASK;
5774 ieee80211_notify_michael_failure(ni->ni_vap, wh, keyidx);
5776 ieee80211_free_node(ni);
5780 ifp->if_ierrors++;
5782 if (!(ifp->if_flags & IFF_PROMISC))
5784 m_free(m);
5785 goto skip;
5789 else
5791 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
5793 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5794 "%s: rxdesc: not decrypted but protected flag set\n",
5795 device_get_nameunit(sc->dev));
5797 ifp->if_ierrors++;
5799 if (!(ifp->if_flags & IFF_PROMISC))
5801 m_free(m);
5802 goto skip;
5807 /* check for A-MPDU */
5809 if (desc_flags & RT2860_RXDESC_FLAGS_BA)
5811 m->m_flags |= M_AMPDU;
5813 sc->rx_ampdu++;
5815 if (wh->i_fc[1] & IEEE80211_FC1_RETRY)
5816 sc->rx_ampdu_retries++;
5818 ampdu = 1;
5820 else
5822 ampdu = 0;
5825 /* check for A-MSDU */
5827 if (desc_flags & RT2860_RXDESC_FLAGS_AMSDU)
5829 sc->rx_amsdu++;
5831 amsdu = 1;
5833 else
5835 amsdu = 0;
5838 ant = rt2860_maxrssi_rxpath(sc, rxwi);
5839 rssi = rxwi->rssi[ant];
5840 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
5841 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
5842 RT2860_RXWI_PHYMODE_MASK);
5843 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
5844 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
5845 RT2860_RXWI_SHORTGI_MASK);
5846 stbc = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_STBC_SHIFT) &
5847 RT2860_RXWI_STBC_MASK);
5848 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
5849 tid = ((rxwi->tid_size >> RT2860_RXWI_TID_SHIFT) & RT2860_RXWI_TID_MASK);
5850 seq = ((rxwi->seq_frag >> RT2860_RXWI_SEQ_SHIFT) & RT2860_RXWI_SEQ_MASK);
5851 frag = ((rxwi->seq_frag >> RT2860_RXWI_FRAG_SHIFT) & RT2860_RXWI_FRAG_MASK);
5853 if (ieee80211_radiotap_active(ic))
5855 tap = &sc->rxtap;
5857 tap->flags = (desc_flags & RT2860_RXDESC_FLAGS_L2PAD) ? IEEE80211_RADIOTAP_F_DATAPAD : 0;
5858 tap->dbm_antsignal = rssi_dbm;
5859 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
5860 tap->antenna = ant;
5861 tap->antsignal = rssi;
5862 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
5863 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
5864 tap->chan_ieee = ic->ic_curchan->ic_ieee;
5865 tap->chan_maxpow = 0;
5867 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
5868 tap->rate = mcs | IEEE80211_RATE_MCS;
5869 else
5870 tap->rate = rt2860_rxrate(rxwi);
5872 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
5873 tap->flags |= IEEE80211_RADIOTAP_F_BADFCS;
5875 if (desc_flags & RT2860_RXDESC_FLAGS_FRAG)
5876 tap->flags |= IEEE80211_RADIOTAP_F_FRAG;
5878 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
5879 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5881 if ((desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED) ||
5882 (wh->i_fc[1] & IEEE80211_FC1_WEP))
5883 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
5885 if (shortgi)
5886 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
5888 /* XXX use temporarily radiotap CFP flag as A-MPDU flag */
5890 if (ampdu)
5891 tap->flags |= IEEE80211_RADIOTAP_F_CFP;
5895 * net80211 assumes that RSSI data are in the range [-127..127] and
5896 * in .5 dBm units relative to the current noise floor
5899 rssi_dbm_rel = (rssi_dbm - RT2860_NOISE_FLOOR) * 2;
5900 if (rssi_dbm_rel > 127)
5901 rssi_dbm_rel = 127;
5903 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5904 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, stbc=0x%02x, mcs=%d, "
5905 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d, tid=%d, seq=%d, frag=%d, "
5906 "retry=%d, rssi_dbm=%d, rssi_dbm_rel=%d\n",
5907 device_get_nameunit(sc->dev),
5908 len, phymode, bw, shortgi, stbc, mcs,
5909 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
5910 rxwi->snr[0], rxwi->snr[1],
5911 rxwi->wcid, ampdu, amsdu, tid, seq, frag, (wh->i_fc[1] & IEEE80211_FC1_RETRY) ? 1 : 0,
5912 rssi_dbm, rssi_dbm_rel);
5914 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
5915 if (ni != NULL)
5917 rni = (struct rt2860_softc_node *) ni;
5919 for (i = 0; i < RT2860_SOFTC_RSSI_COUNT; i++)
5921 rni->last_rssi[i] = rxwi->rssi[i];
5922 rni->last_rssi_dbm[i] = rt2860_rssi2dbm(sc, rxwi->rssi[i], i);
5925 ieee80211_input(ni, m, rssi_dbm_rel, RT2860_NOISE_FLOOR);
5926 ieee80211_free_node(ni);
5928 else
5930 ieee80211_input_all(ic, m, rssi_dbm_rel, RT2860_NOISE_FLOOR);
5933 skip:
5935 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5937 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5938 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5940 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
5942 limit--;
5945 if (ring->cur == 0)
5946 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5947 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
5948 else
5949 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5950 ring->cur - 1);
5952 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5953 "%s: Rx eof: nframes=%d\n",
5954 device_get_nameunit(sc->dev), nframes);
5956 sc->rx_packets += nframes;
5958 return (limit == 0);
5962 * rt2860_tx_eof
5964 static void rt2860_tx_eof(struct rt2860_softc *sc,
5965 struct rt2860_softc_tx_ring *ring)
5967 struct ifnet *ifp;
5968 struct rt2860_txdesc *desc;
5969 struct rt2860_softc_tx_data *data;
5970 uint32_t index;
5971 int ndescs, nframes;
5973 ifp = sc->ifp;
5975 ndescs = 0;
5976 nframes = 0;
5978 for (;;)
5980 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
5981 if (ring->desc_next == index)
5982 break;
5984 ndescs++;
5986 rt2860_drain_fifo_stats(sc);
5988 desc = &ring->desc[ring->desc_next];
5990 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5991 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
5993 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
5994 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
5996 nframes++;
5998 data = &ring->data[ring->data_next];
6000 if (data->m->m_flags & M_TXCB)
6001 ieee80211_process_callback(data->ni, data->m, 0);
6003 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
6004 BUS_DMASYNC_POSTWRITE);
6005 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
6007 m_freem(data->m);
6009 ieee80211_free_node(data->ni);
6011 data->m = NULL;
6012 data->ni = NULL;
6014 ifp->if_opackets++;
6016 RT2860_SOFTC_TX_RING_LOCK(ring);
6018 ring->data_queued--;
6019 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
6021 RT2860_SOFTC_TX_RING_UNLOCK(ring);
6024 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
6026 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
6027 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
6029 RT2860_SOFTC_TX_RING_LOCK(ring);
6031 ring->desc_queued--;
6032 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
6034 RT2860_SOFTC_TX_RING_UNLOCK(ring);
6037 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
6038 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
6039 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
6043 * rt2860_update_stats
6045 static void rt2860_update_stats(struct rt2860_softc *sc)
6047 struct ifnet *ifp;
6048 struct ieee80211com *ic;
6049 uint32_t stacnt[3];
6050 int beacons, noretryok, retryok, failed, underflows, zerolen;
6052 ifp = sc->ifp;
6053 ic = ifp->if_l2com;
6055 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
6056 "%s: update statistic\n",
6057 device_get_nameunit(sc->dev));
6059 rt2860_drain_fifo_stats(sc);
6061 /* read and clear Tx statistic registers */
6063 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
6064 stacnt, sizeof(stacnt));
6066 stacnt[0] = le32toh(stacnt[0]);
6067 stacnt[1] = le32toh(stacnt[1]);
6068 stacnt[2] = le32toh(stacnt[2]);
6070 beacons = stacnt[0] >> 16;
6071 noretryok = stacnt[1] & 0xffff;
6072 retryok = stacnt[1] >> 16;
6073 failed = stacnt[0] & 0xffff;
6074 underflows = stacnt[2] >> 16;
6075 zerolen = stacnt[2] & 0xffff;
6077 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
6078 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
6079 device_get_nameunit(sc->dev),
6080 beacons, noretryok, retryok, failed, underflows, zerolen);
6082 ifp->if_oerrors += failed;
6084 sc->tx_beacons += beacons;
6085 sc->tx_noretryok += noretryok;
6086 sc->tx_retryok += retryok;
6087 sc->tx_failed += failed;
6088 sc->tx_underflows += underflows;
6089 sc->tx_zerolen += zerolen;
6093 * rt2860_bbp_tuning
6095 static void rt2860_bbp_tuning(struct rt2860_softc *sc)
6097 struct ifnet *ifp;
6098 struct ieee80211com *ic;
6099 struct ieee80211vap *vap;
6100 struct ieee80211_node *ni;
6101 int chan, group;
6102 int8_t rssi, old, new;
6104 /* RT2860C does not support BBP tuning */
6106 if (sc->mac_rev == 0x28600100)
6107 return;
6109 ifp = sc->ifp;
6110 ic = ifp->if_l2com;
6111 vap = TAILQ_FIRST(&ic->ic_vaps);
6113 if ((ic->ic_flags & IEEE80211_F_SCAN) || vap == NULL ||
6114 vap->iv_opmode != IEEE80211_M_STA || vap->iv_state != IEEE80211_S_RUN)
6115 return;
6117 ni = vap->iv_bss;
6119 chan = ieee80211_chan2ieee(ic, ni->ni_chan);
6121 if (chan <= 14)
6122 group = 0;
6123 else if (chan <= 64)
6124 group = 1;
6125 else if (chan <= 128)
6126 group = 2;
6127 else
6128 group = 3;
6130 rssi = ieee80211_getrssi(vap);
6132 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
6134 new = 0x2e + sc->lna_gain[group];
6136 else
6138 if (!IEEE80211_IS_CHAN_HT40(ni->ni_chan))
6139 new = 0x32 + sc->lna_gain[group] * 5 / 3;
6140 else
6141 new = 0x3a + sc->lna_gain[group] * 5 / 3;
6144 /* Tune if absolute average RSSI is greater than -80 */
6146 if (rssi > 30)
6147 new += 0x10;
6149 old = rt2860_io_bbp_read(sc, 66);
6151 if (old != new)
6152 rt2860_io_bbp_write(sc, 66, new);
6156 * rt2860_watchdog
6158 static void rt2860_watchdog(struct rt2860_softc *sc)
6160 uint32_t tmp;
6161 int ntries;
6163 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
6165 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
6166 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
6167 device_get_nameunit(sc->dev), tmp);
6169 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
6171 sc->tx_queue_not_empty[0]++;
6173 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
6175 for (ntries = 0; ntries < 10; ntries++)
6177 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
6178 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
6179 break;
6181 DELAY(1);
6184 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
6187 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
6189 sc->tx_queue_not_empty[1]++;
6191 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
6193 for (ntries = 0; ntries < 10; ntries++)
6195 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
6196 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
6197 break;
6199 DELAY(1);
6202 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
6207 * rt2860_drain_fifo_stats
6209 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
6211 struct ifnet *ifp;
6212 uint32_t stats;
6213 uint8_t wcid, mcs, pid;
6214 int ok, agg, retrycnt;
6216 ifp = sc->ifp;
6218 /* drain Tx status FIFO (maxsize = 16) */
6220 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
6221 RT2860_REG_TX_STA_FIFO_VALID)
6223 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
6224 RT2860_REG_TX_STA_FIFO_WCID_MASK;
6226 /* if no ACK was requested, no feedback is available */
6228 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == RT2860_WCID_RESERVED)
6229 continue;
6231 /* update AMRR statistic */
6233 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
6234 agg = (stats & RT2860_REG_TX_STA_FIFO_AGG) ? 1 : 0;
6235 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
6236 RT2860_REG_TX_STA_FIFO_MCS_MASK;
6237 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
6238 RT2860_REG_TX_STA_FIFO_PID_MASK;
6239 retrycnt = (mcs < 0xf) ? (pid - mcs - 1) : 0;
6241 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
6242 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
6243 device_get_nameunit(sc->dev),
6244 wcid, ok, agg, mcs, pid, retrycnt);
6246 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
6248 if (!ok)
6249 ifp->if_oerrors++;
6254 * rt2860_update_raw_counters
6256 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
6258 uint32_t tmp;
6260 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT);
6262 sc->tx_nonagg += tmp & 0xffff;
6263 sc->tx_agg += tmp >> 16;
6265 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT0);
6267 sc->tx_ampdu += (tmp & 0xffff) / 1 + (tmp >> 16) / 2;
6269 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT1);
6271 sc->tx_ampdu += (tmp & 0xffff) / 3 + (tmp >> 16) / 4;
6273 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT2);
6275 sc->tx_ampdu += (tmp & 0xffff) / 5 + (tmp >> 16) / 6;
6277 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT3);
6279 sc->tx_ampdu += (tmp & 0xffff) / 7 + (tmp >> 16) / 8;
6281 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT4);
6283 sc->tx_ampdu += (tmp & 0xffff) / 9 + (tmp >> 16) / 10;
6285 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT5);
6287 sc->tx_ampdu += (tmp & 0xffff) / 11 + (tmp >> 16) / 12;
6289 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT6);
6291 sc->tx_ampdu += (tmp & 0xffff) / 13 + (tmp >> 16) / 14;
6293 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT7);
6295 sc->tx_ampdu += (tmp & 0xffff) / 15 + (tmp >> 16) / 16;
6297 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
6299 sc->rx_crc_errors += tmp & 0xffff;
6300 sc->rx_phy_errors += tmp >> 16;
6302 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
6304 sc->rx_false_ccas += tmp & 0xffff;
6305 sc->rx_plcp_errors += tmp >> 16;
6307 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
6309 sc->rx_dup_packets += tmp & 0xffff;
6310 sc->rx_fifo_overflows += tmp >> 16;
6312 tmp = rt2860_io_mac_read(sc, RT2860_REG_TXRX_MPDU_DEN_CNT);
6314 sc->tx_mpdu_zero_density += tmp & 0xffff;
6315 sc->rx_mpdu_zero_density += tmp >> 16;
6319 * rt2860_intr_enable
6321 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
6323 uint32_t tmp;
6325 sc->intr_disable_mask &= ~intr_mask;
6327 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
6329 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
6333 * rt2860_intr_disable
6335 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
6337 uint32_t tmp;
6339 sc->intr_disable_mask |= intr_mask;
6341 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
6343 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
6347 * rt2860_txrx_enable
6349 static int rt2860_txrx_enable(struct rt2860_softc *sc)
6351 struct ieee80211com *ic;
6352 struct ifnet *ifp;
6353 uint32_t tmp;
6354 int ntries;
6356 ifp = sc->ifp;
6357 ic = ifp->if_l2com;
6359 /* enable Tx/Rx DMA engine */
6361 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
6363 for (ntries = 0; ntries < 200; ntries++)
6365 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
6366 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
6367 break;
6369 DELAY(1000);
6372 if (ntries == 200)
6374 printf("%s: timeout waiting for DMA engine\n",
6375 device_get_nameunit(sc->dev));
6376 return -1;
6379 DELAY(50);
6381 tmp |= RT2860_REG_TX_WB_DDONE |
6382 RT2860_REG_RX_DMA_ENABLE |
6383 RT2860_REG_TX_DMA_ENABLE |
6384 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
6386 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
6388 /* set Rx filter */
6390 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
6391 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
6393 if (ic->ic_opmode != IEEE80211_M_MONITOR)
6395 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
6396 RT2860_REG_RX_FILTER_DROP_CTS |
6397 RT2860_REG_RX_FILTER_DROP_BA |
6398 RT2860_REG_RX_FILTER_DROP_ACK |
6399 RT2860_REG_RX_FILTER_DROP_VER_ERR |
6400 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
6401 RT2860_REG_RX_FILTER_DROP_CFACK |
6402 RT2860_REG_RX_FILTER_DROP_CFEND;
6404 if (ic->ic_opmode == IEEE80211_M_STA)
6405 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
6406 RT2860_REG_RX_FILTER_DROP_PSPOLL;
6408 if (!(ifp->if_flags & IFF_PROMISC))
6409 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
6412 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
6414 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
6415 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
6417 return 0;
6421 * rt2860_alloc_rx_ring
6423 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
6424 struct rt2860_softc_rx_ring *ring)
6426 struct rt2860_rxdesc *desc;
6427 struct rt2860_softc_rx_data *data;
6428 bus_dma_segment_t segs[1];
6429 int i, nsegs, error;
6431 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
6432 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
6433 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
6434 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
6435 0, NULL, NULL, &ring->desc_dma_tag);
6436 if (error != 0)
6438 printf("%s: could not create Rx desc DMA tag\n",
6439 device_get_nameunit(sc->dev));
6440 goto fail;
6443 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
6444 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
6445 if (error != 0)
6447 printf("%s: could not allocate Rx desc DMA memory\n",
6448 device_get_nameunit(sc->dev));
6449 goto fail;
6452 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
6453 ring->desc,
6454 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
6455 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
6456 if (error != 0)
6458 printf("%s: could not load Rx desc DMA map\n",
6459 device_get_nameunit(sc->dev));
6460 goto fail;
6463 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
6464 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
6465 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
6466 &ring->data_dma_tag);
6467 if (error != 0)
6469 printf("%s: could not create Rx data DMA tag\n",
6470 device_get_nameunit(sc->dev));
6471 goto fail;
6474 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
6476 desc = &ring->desc[i];
6477 data = &ring->data[i];
6479 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
6480 if (error != 0)
6482 printf("%s: could not create Rx data DMA map\n",
6483 device_get_nameunit(sc->dev));
6484 goto fail;
6487 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
6488 if (data->m == NULL)
6490 printf("%s: could not allocate Rx mbuf\n",
6491 device_get_nameunit(sc->dev));
6492 error = ENOMEM;
6493 goto fail;
6496 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
6498 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
6499 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
6500 if (error != 0)
6502 printf("%s: could not load Rx mbuf DMA map\n",
6503 device_get_nameunit(sc->dev));
6504 goto fail;
6507 KASSERT(nsegs == 1, ("%s: too many DMA segments",
6508 device_get_nameunit(sc->dev)));
6510 desc->sdp0 = htole32(segs[0].ds_addr);
6511 desc->sdl0 = htole16(MJUMPAGESIZE);
6514 error = bus_dmamap_create(ring->data_dma_tag, 0, &ring->spare_dma_map);
6515 if (error != 0)
6517 printf("%s: could not create Rx spare DMA map\n",
6518 device_get_nameunit(sc->dev));
6519 goto fail;
6522 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
6523 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
6525 return 0;
6527 fail:
6529 rt2860_free_rx_ring(sc, ring);
6531 return error;
6535 * rt2860_reset_rx_ring
6537 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
6538 struct rt2860_softc_rx_ring *ring)
6540 struct rt2860_rxdesc *desc;
6541 int i;
6543 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
6545 desc = &ring->desc[i];
6547 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
6550 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
6551 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
6553 ring->cur = 0;
6557 * rt2860_free_rx_ring
6559 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
6560 struct rt2860_softc_rx_ring *ring)
6562 struct rt2860_softc_rx_data *data;
6563 int i;
6565 if (ring->desc != NULL)
6567 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
6568 BUS_DMASYNC_POSTWRITE);
6569 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
6570 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
6571 ring->desc_dma_map);
6574 if (ring->desc_dma_tag != NULL)
6575 bus_dma_tag_destroy(ring->desc_dma_tag);
6577 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
6579 data = &ring->data[i];
6581 if (data->m != NULL)
6583 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
6584 BUS_DMASYNC_POSTREAD);
6585 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
6586 m_freem(data->m);
6589 if (data->dma_map != NULL)
6590 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
6593 if (ring->spare_dma_map != NULL)
6594 bus_dmamap_destroy(ring->data_dma_tag, ring->spare_dma_map);
6596 if (ring->data_dma_tag != NULL)
6597 bus_dma_tag_destroy(ring->data_dma_tag);
6601 * rt2860_alloc_tx_ring
6603 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
6604 struct rt2860_softc_tx_ring *ring, int qid)
6606 struct rt2860_softc_tx_data *data;
6607 int error, i;
6609 mtx_init(&ring->lock, device_get_nameunit(sc->dev), NULL, MTX_DEF);
6611 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
6612 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
6613 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
6614 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
6615 0, NULL, NULL, &ring->desc_dma_tag);
6616 if (error != 0)
6618 printf("%s: could not create Tx desc DMA tag\n",
6619 device_get_nameunit(sc->dev));
6620 goto fail;
6623 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
6624 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
6625 if (error != 0)
6627 printf("%s: could not allocate Tx desc DMA memory\n",
6628 device_get_nameunit(sc->dev));
6629 goto fail;
6632 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
6633 ring->desc,
6634 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
6635 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
6636 if (error != 0)
6638 printf("%s: could not load Tx desc DMA map\n",
6639 device_get_nameunit(sc->dev));
6640 goto fail;
6643 ring->desc_queued = 0;
6644 ring->desc_cur = 0;
6645 ring->desc_next = 0;
6647 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
6648 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
6649 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
6650 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
6651 0, NULL, NULL, &ring->seg0_dma_tag);
6652 if (error != 0)
6654 printf("%s: could not create Tx seg0 DMA tag\n",
6655 device_get_nameunit(sc->dev));
6656 goto fail;
6659 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
6660 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
6661 if (error != 0)
6663 printf("%s: could not allocate Tx seg0 DMA memory\n",
6664 device_get_nameunit(sc->dev));
6665 goto fail;
6668 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
6669 ring->seg0,
6670 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
6671 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
6672 if (error != 0)
6674 printf("%s: could not load Tx seg0 DMA map\n",
6675 device_get_nameunit(sc->dev));
6676 goto fail;
6679 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
6680 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
6681 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
6682 &ring->data_dma_tag);
6683 if (error != 0)
6685 printf("%s: could not create Tx data DMA tag\n",
6686 device_get_nameunit(sc->dev));
6687 goto fail;
6690 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
6692 data = &ring->data[i];
6694 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
6695 if (error != 0)
6697 printf("%s: could not create Tx data DMA map\n",
6698 device_get_nameunit(sc->dev));
6699 goto fail;
6703 ring->data_queued = 0;
6704 ring->data_cur = 0;
6705 ring->data_next = 0;
6707 ring->qid = qid;
6709 return 0;
6711 fail:
6713 rt2860_free_tx_ring(sc, ring);
6715 return error;
6719 * rt2860_reset_tx_ring
6721 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
6722 struct rt2860_softc_tx_ring *ring)
6724 struct rt2860_softc_tx_data *data;
6725 struct rt2860_txdesc *desc;
6726 int i;
6728 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
6730 desc = &ring->desc[i];
6732 desc->sdl0 = 0;
6733 desc->sdl1 = 0;
6736 ring->desc_queued = 0;
6737 ring->desc_cur = 0;
6738 ring->desc_next = 0;
6740 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
6741 BUS_DMASYNC_PREWRITE);
6743 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
6744 BUS_DMASYNC_PREWRITE);
6746 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
6748 data = &ring->data[i];
6750 if (data->m != NULL)
6752 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
6753 BUS_DMASYNC_POSTWRITE);
6754 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
6755 m_freem(data->m);
6756 data->m = NULL;
6759 if (data->ni != NULL)
6761 ieee80211_free_node(data->ni);
6762 data->ni = NULL;
6766 ring->data_queued = 0;
6767 ring->data_cur = 0;
6768 ring->data_next = 0;
6772 * rt2860_free_tx_ring
6774 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
6775 struct rt2860_softc_tx_ring *ring)
6777 struct rt2860_softc_tx_data *data;
6778 int i;
6780 if (ring->desc != NULL)
6782 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
6783 BUS_DMASYNC_POSTWRITE);
6784 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
6785 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
6786 ring->desc_dma_map);
6789 if (ring->desc_dma_tag != NULL)
6790 bus_dma_tag_destroy(ring->desc_dma_tag);
6792 if (ring->seg0 != NULL)
6794 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
6795 BUS_DMASYNC_POSTWRITE);
6796 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
6797 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
6798 ring->seg0_dma_map);
6801 if (ring->seg0_dma_tag != NULL)
6802 bus_dma_tag_destroy(ring->seg0_dma_tag);
6804 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
6806 data = &ring->data[i];
6808 if (data->m != NULL)
6810 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
6811 BUS_DMASYNC_POSTWRITE);
6812 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
6813 m_freem(data->m);
6816 if (data->ni != NULL)
6817 ieee80211_free_node(data->ni);
6819 if (data->dma_map != NULL)
6820 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
6823 if (ring->data_dma_tag != NULL)
6824 bus_dma_tag_destroy(ring->data_dma_tag);
6826 mtx_destroy(&ring->lock);
6830 * rt2860_dma_map_addr
6832 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
6833 int nseg, int error)
6835 if (error != 0)
6836 return;
6838 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
6840 *(bus_addr_t *) arg = segs[0].ds_addr;
6844 * rt2860_sysctl_attach
6846 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
6848 struct sysctl_ctx_list *ctx;
6849 struct sysctl_oid *tree;
6850 struct sysctl_oid *stats;
6852 ctx = device_get_sysctl_ctx(sc->dev);
6853 tree = device_get_sysctl_tree(sc->dev);
6855 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6856 "tx_stbc", CTLFLAG_RW, &sc->tx_stbc, 0,
6857 "Tx STBC");
6859 /* statistic counters */
6861 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6862 "stats", CTLFLAG_RD, 0, "statistic");
6864 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6865 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
6866 "all interrupts");
6868 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6869 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
6870 "Tx coherent interrupts");
6872 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6873 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
6874 "Rx coherent interrupts");
6876 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6877 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
6878 "Tx/Rx coherent interrupts");
6880 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6881 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
6882 "FIFO statistic full interrupts");
6884 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6885 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
6886 "Rx interrupts");
6888 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6889 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
6890 "Rx delay interrupts");
6892 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6893 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
6894 "Tx MGMT interrupts");
6896 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6897 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
6898 "Tx HCCA interrupts");
6900 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6901 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
6902 "Tx AC3 interrupts");
6904 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6905 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
6906 "Tx AC2 interrupts");
6908 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6909 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
6910 "Tx AC1 interrupts");
6912 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6913 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
6914 "Tx AC0 interrupts");
6916 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6917 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
6918 "Tx delay interrupts");
6920 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6921 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
6922 "Pre-TBTT interrupts");
6924 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6925 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
6926 "TBTT interrupts");
6928 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6929 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
6930 "MCU command interrupts");
6932 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6933 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
6934 "auto wakeup interrupts");
6936 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6937 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
6938 "GP timer interrupts");
6940 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6941 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
6942 "Tx MGMT descriptors queued");
6944 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6945 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
6946 "Tx MGMT data queued");
6948 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6949 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
6950 "Tx HCCA descriptors queued");
6952 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6953 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
6954 "Tx HCCA data queued");
6956 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6957 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
6958 "Tx AC3 descriptors queued");
6960 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6961 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
6962 "Tx AC3 data queued");
6964 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6965 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
6966 "Tx AC2 descriptors queued");
6968 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6969 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
6970 "Tx AC2 data queued");
6972 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6973 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
6974 "Tx AC1 descriptors queued");
6976 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6977 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
6978 "Tx AC1 data queued");
6980 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6981 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
6982 "Tx AC0 descriptors queued");
6984 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6985 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
6986 "Tx AC0 data queued");
6988 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6989 "tx_mgmt_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[5], 0,
6990 "Tx MGMT data queue full");
6992 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6993 "tx_hcca_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[4], 0,
6994 "Tx HCCA data queue full");
6996 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6997 "tx_ac3_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[3], 0,
6998 "Tx AC3 data queue full");
7000 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7001 "tx_ac2_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[2], 0,
7002 "Tx AC2 data queue full");
7004 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7005 "tx_ac1_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[1], 0,
7006 "Tx AC1 data queue full");
7008 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7009 "tx_ac0_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[0], 0,
7010 "Tx AC0 data queue full");
7012 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7013 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
7014 "Tx watchdog timeouts");
7016 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7017 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
7018 "Tx defragmented packets");
7020 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7021 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
7022 "no Tx descriptors available");
7024 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7025 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
7026 "Rx mbuf allocation errors");
7028 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7029 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
7030 "Rx mbuf DMA mapping errors");
7032 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7033 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
7034 "Tx queue 0 not empty");
7036 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7037 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
7038 "Tx queue 1 not empty");
7040 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7041 "tx_beacons", CTLFLAG_RD, &sc->tx_beacons, 0,
7042 "Tx beacons");
7044 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7045 "tx_noretryok", CTLFLAG_RD, &sc->tx_noretryok, 0,
7046 "Tx successfull without retries");
7048 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7049 "tx_retryok", CTLFLAG_RD, &sc->tx_retryok, 0,
7050 "Tx successfull with retries");
7052 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7053 "tx_failed", CTLFLAG_RD, &sc->tx_failed, 0,
7054 "Tx failed");
7056 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7057 "tx_underflows", CTLFLAG_RD, &sc->tx_underflows, 0,
7058 "Tx underflows");
7060 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7061 "tx_zerolen", CTLFLAG_RD, &sc->tx_zerolen, 0,
7062 "Tx zero length");
7064 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7065 "tx_nonagg", CTLFLAG_RD, &sc->tx_nonagg, 0,
7066 "Tx non-aggregated");
7068 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7069 "tx_agg", CTLFLAG_RD, &sc->tx_agg, 0,
7070 "Tx aggregated");
7072 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7073 "tx_ampdu", CTLFLAG_RD, &sc->tx_ampdu, 0,
7074 "Tx A-MPDU");
7076 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7077 "tx_mpdu_zero_density", CTLFLAG_RD, &sc->tx_mpdu_zero_density, 0,
7078 "Tx MPDU with zero density");
7080 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7081 "tx_ampdu_sessions", CTLFLAG_RD, &sc->tx_ampdu_sessions, 0,
7082 "Tx A-MPDU sessions");
7084 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7085 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
7086 "Rx packets");
7088 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7089 "rx_ampdu", CTLFLAG_RD, &sc->rx_ampdu, 0,
7090 "Rx A-MPDU");
7092 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7093 "rx_ampdu_retries", CTLFLAG_RD, &sc->rx_ampdu_retries, 0,
7094 "Rx A-MPDU retries");
7096 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7097 "rx_mpdu_zero_density", CTLFLAG_RD, &sc->rx_mpdu_zero_density, 0,
7098 "Rx MPDU with zero density");
7100 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7101 "rx_ampdu_sessions", CTLFLAG_RD, &sc->rx_ampdu_sessions, 0,
7102 "Rx A-MPDU sessions");
7104 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7105 "rx_amsdu", CTLFLAG_RD, &sc->rx_amsdu, 0,
7106 "Rx A-MSDU");
7108 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7109 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
7110 "Rx CRC errors");
7112 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7113 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
7114 "Rx PHY errors");
7116 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7117 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
7118 "Rx false CCAs");
7120 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7121 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
7122 "Rx PLCP errors");
7124 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7125 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
7126 "Rx duplicate packets");
7128 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7129 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
7130 "Rx FIFO overflows");
7132 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7133 "rx_cipher_no_errors", CTLFLAG_RD, &sc->rx_cipher_no_errors, 0,
7134 "Rx cipher no errors");
7136 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7137 "rx_cipher_icv_errors", CTLFLAG_RD, &sc->rx_cipher_icv_errors, 0,
7138 "Rx cipher ICV errors");
7140 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7141 "rx_cipher_mic_errors", CTLFLAG_RD, &sc->rx_cipher_mic_errors, 0,
7142 "Rx cipher MIC errors");
7144 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
7145 "rx_cipher_invalid_key_errors", CTLFLAG_RD, &sc->rx_cipher_invalid_key_errors, 0,
7146 "Rx cipher invalid key errors");
7149 static device_method_t rt2860_dev_methods[] =
7151 DEVMETHOD(device_probe, rt2860_probe),
7152 DEVMETHOD(device_attach, rt2860_attach),
7153 DEVMETHOD(device_detach, rt2860_detach),
7154 DEVMETHOD(device_shutdown, rt2860_shutdown),
7155 DEVMETHOD(device_suspend, rt2860_suspend),
7156 DEVMETHOD(device_resume, rt2860_resume),
7157 { 0, 0 }
7160 static driver_t rt2860_driver =
7162 "rt2860",
7163 rt2860_dev_methods,
7164 sizeof(struct rt2860_softc)
7167 static devclass_t rt2860_dev_class;
7169 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
7171 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
7172 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);