Improved Tx locking
[ralink_drivers/rt2860_fbsd72.git] / rt2860.c
blob634796af2d7a25d30e385c97054abf3e799433b4
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 RT2860_AID2WCID(aid) ((aid) & 0xff)
50 #define RT2860_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
52 #define RT2860_ACK_SIZE 14
54 #define IEEE80211_HAS_ADDR4(wh) \
55 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
57 #define RT2860_MS(_v, _f) (((_v) & _f) >> _f##_S)
58 #define RT2860_SM(_v, _f) (((_v) << _f##_S) & _f)
60 #define RT2860_TX_WATCHDOG_TIMEOUT 5
62 #define RT2860_WCID_RESERVED 0xff
63 #define RT2860_WCID_MCAST 0xf7
66 * Data structures and types
69 struct rt2860_pci_ident
71 uint16_t vendor;
72 uint16_t device;
73 const char *name;
77 * Static function prototypes
80 static int rt2860_probe(device_t dev);
82 static int rt2860_attach(device_t dev);
84 static int rt2860_detach(device_t dev);
86 static int rt2860_shutdown(device_t dev);
88 static int rt2860_suspend(device_t dev);
90 static int rt2860_resume(device_t dev);
92 static void rt2860_init_channels(struct rt2860_softc *sc);
94 static void rt2860_init_channels_ht40(struct rt2860_softc *sc);
96 static void rt2860_init_locked(void *priv);
98 static void rt2860_init(void *priv);
100 static int rt2860_init_bbp(struct rt2860_softc *sc);
102 static void rt2860_stop_locked(void *priv);
104 static void rt2860_stop(void *priv);
106 static void rt2860_start(struct ifnet *ifp);
108 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
110 static int rt2860_reset(struct ifnet *ifp);
112 static int rt2860_newstate(struct ieee80211com *ic,
113 enum ieee80211_state nstate, int arg);
115 static void rt2860_scan_start(struct ieee80211com *ic);
117 static void rt2860_scan_end(struct ieee80211com *ic);
119 static void rt2860_set_channel(struct ieee80211com *ic);
121 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew);
123 static void rt2860_updateslot(struct ifnet *ifp);
125 static int rt2860_wme_update(struct ieee80211com *ic);
127 static void rt2860_update_beacon(struct ieee80211com *ic, int what);
129 static void rt2860_key_update_begin(struct ieee80211com *ic);
131 static void rt2860_key_update_end(struct ieee80211com *ic);
133 static int rt2860_key_set(struct ieee80211com *ic,
134 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN]);
136 static int rt2860_key_delete(struct ieee80211com *ic,
137 const struct ieee80211_key *k);
139 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
140 const struct ieee80211_bpf_params *params);
142 static int rt2860_media_change(struct ifnet *ifp);
144 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211_node_table *nt);
146 static void rt2860_node_cleanup(struct ieee80211_node *ni);
148 static void rt2860_recv_action(struct ieee80211_node *ni,
149 const uint8_t *frm, const uint8_t *efrm);
151 static int rt2860_send_action(struct ieee80211_node *ni,
152 int category, int action, uint16_t args[4]);
154 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
156 static void rt2860_periodic(void *arg);
158 static void rt2860_tx_watchdog(void *arg);
160 static int rt2860_staid_alloc(struct rt2860_softc *sc, int aid);
162 static void rt2860_staid_delete(struct rt2860_softc *sc, int staid);
164 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
165 const uint8_t *bssid);
167 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
168 const uint8_t *addr);
170 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc);
172 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc);
174 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc);
176 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc);
178 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc);
180 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc);
182 static void rt2860_asic_update_txpower(struct rt2860_softc *sc);
184 static void rt2860_asic_update_promisc(struct rt2860_softc *sc);
186 static void rt2860_asic_updateprot(struct rt2860_softc *sc);
188 static void rt2860_asic_updateslot(struct rt2860_softc *sc);
190 static void rt2860_asic_wme_update(struct rt2860_softc *sc);
192 static void rt2860_asic_update_beacon(struct rt2860_softc *sc);
194 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc);
196 static int rt2860_beacon_alloc(struct rt2860_softc *sc);
198 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi);
200 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
201 const struct rt2860_rxwi *rxwi);
203 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
204 uint8_t rssi, uint8_t rxpath);
206 static uint8_t rt2860_rate2mcs(uint8_t rate);
208 static int rt2860_ackrate(struct ieee80211com *ic, int rate);
210 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags);
212 static int rt2860_tx_frame(struct rt2860_softc *sc,
213 struct mbuf *m, struct ieee80211_node *ni, int qid);
215 static int rt2860_tx_raw(struct rt2860_softc *sc,
216 struct mbuf *m, struct ieee80211_node *ni,
217 const struct ieee80211_bpf_params *params);
219 static void rt2860_intr(void *arg);
221 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc);
223 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc);
225 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc);
227 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc);
229 static void rt2860_rx_intr(struct rt2860_softc *sc);
231 static void rt2860_rx_delay_intr(struct rt2860_softc *sc);
233 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid);
235 static void rt2860_tx_delay_intr(struct rt2860_softc *sc);
237 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc);
239 static void rt2860_tbtt_intr(struct rt2860_softc *sc);
241 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc);
243 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc);
245 static void rt2860_gp_timer_intr(struct rt2860_softc *sc);
247 static void rt2860_rx_done_task(void *context, int pending);
249 static void rt2860_tx_done_task(void *context, int pending);
251 static void rt2860_fifo_sta_full_task(void *context, int pending);
253 static void rt2860_periodic_task(void *context, int pending);
255 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit);
257 static void rt2860_tx_eof(struct rt2860_softc *sc,
258 struct rt2860_softc_tx_ring *ring);
260 static void rt2860_update_stats(struct rt2860_softc *sc);
262 static void rt2860_bbp_tuning(struct rt2860_softc *sc);
264 static void rt2860_watchdog(struct rt2860_softc *sc);
266 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc);
268 static void rt2860_update_raw_counters(struct rt2860_softc *sc);
270 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask);
272 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask);
274 static int rt2860_txrx_enable(struct rt2860_softc *sc);
276 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
277 struct rt2860_softc_rx_ring *ring);
279 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
280 struct rt2860_softc_rx_ring *ring);
282 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
283 struct rt2860_softc_rx_ring *ring);
285 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
286 struct rt2860_softc_tx_ring *ring, int qid);
288 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
289 struct rt2860_softc_tx_ring *ring);
291 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
292 struct rt2860_softc_tx_ring *ring);
294 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
295 int nseg, int error);
297 static void rt2860_sysctl_attach(struct rt2860_softc *sc);
300 * Static variables
303 static const struct rt2860_pci_ident rt2860_pci_ids[] =
305 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCI, "Ralink RT2860 PCI" },
306 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCIe, "Ralink RT2860 PCIe" },
307 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760_PCI, "Ralink RT2760 PCI" },
308 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790_PCIe, "Ralink RT2790 PCIe" },
309 { 0, 0, NULL }
312 static const struct
314 uint32_t reg;
315 uint32_t val;
316 } rt2860_def_mac[] =
318 { RT2860_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
319 { RT2860_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
320 { RT2860_REG_LEGACY_BASIC_RATE, 0x0000013f },
321 { RT2860_REG_HT_BASIC_RATE, 0x00008003 },
322 { RT2860_REG_SYS_CTRL, 0x00000000 },
323 { RT2860_REG_RX_FILTER_CFG, 0x00017f97 },
324 { RT2860_REG_BKOFF_SLOT_CFG, 0x00000209 },
325 { RT2860_REG_TX_SW_CFG0, 0x00000000 },
326 { RT2860_REG_TX_SW_CFG1, 0x00080606 },
327 { RT2860_REG_TX_LINK_CFG, 0x00001020 },
328 { RT2860_REG_TX_TIMEOUT_CFG, 0x000a2090 },
329 { RT2860_REG_MAX_LEN_CFG, (1 << 12) | RT2860_MAX_AGG_SIZE },
330 { RT2860_REG_LED_CFG, 0x7f031e46 },
331 { RT2860_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
332 { RT2860_REG_TX_RTY_CFG, 0x47d01f0f },
333 { RT2860_REG_AUTO_RSP_CFG, 0x00000013 },
334 { RT2860_REG_TX_CCK_PROT_CFG, 0x05740003 },
335 { RT2860_REG_TX_OFDM_PROT_CFG, 0x05740003 },
336 { RT2860_REG_TX_GF20_PROT_CFG, 0x01744004 },
337 { RT2860_REG_TX_GF40_PROT_CFG, 0x03f44084 },
338 { RT2860_REG_TX_MM20_PROT_CFG, 0x01744004 },
339 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f54084 },
340 { RT2860_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
341 { RT2860_REG_TX_RTS_CFG, 0x00092b20 },
342 { RT2860_REG_TX_EXP_ACK_TIME, 0x002400ca },
343 { RT2860_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
344 { RT2860_REG_XIFS_TIME_CFG, 0x33a41010 },
345 { RT2860_REG_PWR_PIN_CFG, 0x00000003 },
346 { RT2860_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
347 { RT2860_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
348 { RT2860_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
351 #define RT2860_DEF_MAC_SIZE (sizeof(rt2860_def_mac) / sizeof(rt2860_def_mac[0]))
353 static const struct
355 uint8_t reg;
356 uint8_t val;
357 } rt2860_def_bbp[] =
359 { 65, 0x2c },
360 { 66, 0x38 },
361 { 69, 0x12 },
362 { 70, 0x0a },
363 { 73, 0x10 },
364 { 81, 0x37 },
365 { 82, 0x62 },
366 { 83, 0x6a },
367 { 84, 0x99 },
368 { 86, 0x00 },
369 { 91, 0x04 },
370 { 92, 0x00 },
371 { 103, 0x00 },
372 { 105, 0x05 },
373 { 106, 0x35 },
376 #define RT2860_DEF_BBP_SIZE (sizeof(rt2860_def_bbp) / sizeof(rt2860_def_bbp[0]))
378 SYSCTL_NODE(_hw, OID_AUTO, rt2860, CTLFLAG_RD, 0, "RT2860 driver parameters");
380 #ifdef RT2860_DEBUG
381 static int rt2860_debug = 0;
382 SYSCTL_INT(_hw_rt2860, OID_AUTO, debug, CTLFLAG_RW, &rt2860_debug, 0, "rt2860 debug level");
383 TUNABLE_INT("hw.rt2860.debug", &rt2860_debug);
384 #endif
387 * rt2860_probe
389 static int rt2860_probe(device_t dev)
391 const struct rt2860_pci_ident *ident;
393 for (ident = rt2860_pci_ids; ident->name != NULL; ident++)
395 if (pci_get_vendor(dev) == ident->vendor &&
396 pci_get_device(dev) == ident->device)
398 device_set_desc(dev, ident->name);
399 return 0;
403 return ENXIO;
407 * rt2860_attach
409 static int rt2860_attach(device_t dev)
411 struct rt2860_softc *sc;
412 struct ifnet *ifp;
413 struct ieee80211com *ic;
414 int error, ntries, i;
416 sc = device_get_softc(dev);
418 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
420 printf("%s: chip is in D%d power mode, setting to D0\n",
421 device_get_nameunit(dev), pci_get_powerstate(dev));
422 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
425 /* enable bus-mastering */
427 pci_enable_busmaster(dev);
429 sc->dev = dev;
431 mtx_init(&sc->lock, device_get_nameunit(dev),
432 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
434 sc->mem_rid = PCIR_BAR(0);
435 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
436 &sc->mem_rid, RF_ACTIVE);
437 if (sc->mem == NULL)
439 printf("%s: could not allocate memory resource\n",
440 device_get_nameunit(dev));
441 error = ENXIO;
442 goto fail;
445 sc->bst = rman_get_bustag(sc->mem);
446 sc->bsh = rman_get_bushandle(sc->mem);
448 sc->irq_rid = 0;
449 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
450 &sc->irq_rid, RF_ACTIVE | RF_SHAREABLE);
451 if (sc->irq == NULL)
453 printf("%s: could not allocate interrupt resource\n",
454 device_get_nameunit(dev));
455 error = ENXIO;
456 goto fail;
459 #ifdef RT2860_DEBUG
460 sc->debug = rt2860_debug;
462 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
463 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
464 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2860 debug level");
465 #endif
467 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
468 "%s: attaching\n",
469 device_get_nameunit(sc->dev));
471 /* wait for NIC to initialize */
473 for (ntries = 0; ntries < 100; ntries++)
475 sc->mac_rev = rt2860_io_mac_read(sc, RT2860_REG_MAC_CSR0);
476 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
477 break;
479 DELAY(10);
482 if (ntries == 100)
484 printf("%s: timeout waiting for NIC to initialize\n",
485 device_get_nameunit(dev));
486 error = EIO;
487 goto fail;
490 rt2860_read_eeprom(sc);
492 printf("%s: MAC/BBP RT2860 (rev 0x%08x), RF %s\n",
493 device_get_nameunit(sc->dev), sc->mac_rev,
494 rt2860_rf_name(sc->rf_rev));
496 /* clear key tables */
498 rt2860_asic_clear_keytables(sc);
500 /* allocate Tx and Rx rings */
502 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
504 error = rt2860_alloc_tx_ring(sc, &sc->tx_ring[i], i);
505 if (error != 0)
507 printf("%s: could not allocate Tx ring #%d\n",
508 device_get_nameunit(sc->dev), i);
509 goto fail;
513 sc->tx_ring_mgtqid = 5;
515 error = rt2860_alloc_rx_ring(sc, &sc->rx_ring);
516 if (error != 0)
518 printf("%s: could not allocate Rx ring\n",
519 device_get_nameunit(sc->dev));
520 goto fail;
523 callout_init(&sc->periodic_ch, 0);
524 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
526 ifp = sc->ifp = if_alloc(IFT_ETHER);
527 if (ifp == NULL)
529 printf("%s: could not if_alloc()\n",
530 device_get_nameunit(sc->dev));
531 error = ENOMEM;
532 goto fail;
535 ifp->if_softc = sc;
537 if_initname(ifp, "rt2860", device_get_unit(sc->dev));
539 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
541 ifp->if_init = rt2860_init;
542 ifp->if_ioctl = rt2860_ioctl;
543 ifp->if_start = rt2860_start;
545 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
546 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
547 IFQ_SET_READY(&ifp->if_snd);
549 ic = &sc->ic;
551 ic->ic_ifp = ifp;
553 ic->ic_phytype = IEEE80211_T_HT;
554 ic->ic_opmode = IEEE80211_M_STA;
555 ic->ic_state = IEEE80211_S_INIT;
557 ic->ic_caps = IEEE80211_C_MONITOR |
558 IEEE80211_C_IBSS |
559 IEEE80211_C_AHDEMO |
560 IEEE80211_C_HOSTAP |
561 IEEE80211_C_WDS |
562 IEEE80211_C_BGSCAN |
563 IEEE80211_C_TXPMGT |
564 IEEE80211_C_SHPREAMBLE |
565 IEEE80211_C_SHSLOT |
566 IEEE80211_C_TXFRAG |
567 IEEE80211_C_BURST |
568 IEEE80211_C_WME |
569 IEEE80211_C_WEP |
570 IEEE80211_C_TKIP |
571 IEEE80211_C_AES_CCM |
572 IEEE80211_C_WPA;
574 ic->ic_htcaps = IEEE80211_HTC_HT |
575 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
576 IEEE80211_HTC_AMPDU | /* A-MPDU Tx */
577 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
578 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
579 IEEE80211_HTCAP_GREENFIELD | /* HT greenfield */
580 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
581 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
582 IEEE80211_HTCAP_SMPS_OFF; /* MIMO power save disabled */
584 /* spatial streams */
586 if (sc->nrxpath == 2)
587 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
588 else if (sc->nrxpath == 3)
589 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
590 else
591 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
593 if (sc->ntxpath > 1)
594 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
596 /* delayed BA */
598 if (sc->mac_rev != 0x28600100)
599 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
601 /* init channels */
603 ic->ic_nchans = 0;
604 ic->ic_regdomain = 0;
605 ic->ic_countrycode = CTRY_DEFAULT;
606 ic->ic_location = 0;
608 rt2860_init_channels(sc);
610 rt2860_init_channels_ht40(sc);
612 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->mac_addr);
614 ieee80211_ifattach(ic);
616 sc->newstate = ic->ic_newstate;
617 ic->ic_newstate = rt2860_newstate;
619 ic->ic_reset = rt2860_reset;
620 ic->ic_node_alloc = rt2860_node_alloc;
622 sc->node_cleanup = ic->ic_node_cleanup;
623 ic->ic_node_cleanup = rt2860_node_cleanup;
625 ic->ic_scan_start = rt2860_scan_start;
626 ic->ic_scan_end = rt2860_scan_end;
627 ic->ic_set_channel = rt2860_set_channel;
628 ic->ic_newassoc = rt2860_newassoc;
629 ic->ic_updateslot = rt2860_updateslot;
630 ic->ic_wme.wme_update = rt2860_wme_update;
631 ic->ic_update_beacon = rt2860_update_beacon;
632 ic->ic_crypto.cs_key_update_begin = rt2860_key_update_begin;
633 ic->ic_crypto.cs_key_update_end = rt2860_key_update_end;
634 ic->ic_crypto.cs_key_set = rt2860_key_set;
635 ic->ic_crypto.cs_key_delete = rt2860_key_delete;
636 ic->ic_raw_xmit = rt2860_raw_xmit;
638 sc->recv_action = ic->ic_recv_action;
639 ic->ic_recv_action = rt2860_recv_action;
641 sc->send_action = ic->ic_send_action;
642 ic->ic_send_action = rt2860_send_action;
644 /* override Rx A-MPDU factor */
646 ic->ic_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
647 ic->ic_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
648 ic->ic_ampdu_limit = ic->ic_ampdu_rxmax;
650 /* hardware requires padding between 802.11 frame header and body */
652 ic->ic_flags |= IEEE80211_F_WME | IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
654 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
656 ieee80211_media_init(ic, rt2860_media_change, ieee80211_media_status);
658 rt2860_amrr_init(&sc->amrr, ic,
659 sc->ntxpath,
660 RT2860_AMRR_MIN_SUCCESS_THRESHOLD,
661 RT2860_AMRR_MAX_SUCCESS_THRESHOLD,
662 500);
664 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
665 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
666 &sc->drvbpf);
668 sc->rxtap_len = sizeof(sc->rxtapu);
669 sc->rxtap.ihdr.it_len = htole16(sc->rxtap_len);
670 sc->rxtap.ihdr.it_present = htole32(RT2860_SOFTC_RX_RADIOTAP_PRESENT);
672 sc->txtap_len = sizeof(sc->txtapu);
673 sc->txtap.ihdr.it_len = htole16(sc->txtap_len);
674 sc->txtap.ihdr.it_present = htole32(RT2860_SOFTC_TX_RADIOTAP_PRESENT);
676 /* init task queue */
678 TASK_INIT(&sc->rx_done_task, 0, rt2860_rx_done_task, sc);
679 TASK_INIT(&sc->tx_done_task, 0, rt2860_tx_done_task, sc);
680 TASK_INIT(&sc->fifo_sta_full_task, 0, rt2860_fifo_sta_full_task, sc);
681 TASK_INIT(&sc->periodic_task, 0, rt2860_periodic_task, sc);
683 sc->rx_process_limit = 100;
685 sc->taskqueue = taskqueue_create("rt2860_taskq", M_NOWAIT,
686 taskqueue_thread_enqueue, &sc->taskqueue);
688 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
689 device_get_nameunit(sc->dev));
691 rt2860_sysctl_attach(sc);
693 if (bootverbose)
694 ieee80211_announce(ic);
696 /* set up interrupt */
698 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
699 NULL, rt2860_intr, sc, &sc->irqh);
700 if (error != 0)
702 printf("%s: could not set up interrupt\n",
703 device_get_nameunit(dev));
704 goto fail;
707 return 0;
709 fail:
711 /* free Tx and Rx rings */
713 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
714 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
716 rt2860_free_rx_ring(sc, &sc->rx_ring);
718 mtx_destroy(&sc->lock);
720 if (sc->mem != NULL)
721 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
723 if (sc->irq != NULL)
724 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
726 return error;
730 * rt2860_detach
732 static int rt2860_detach(device_t dev)
734 struct rt2860_softc *sc;
735 struct ieee80211com *ic;
736 struct ifnet *ifp;
737 int i;
739 sc = device_get_softc(dev);
740 ic = &sc->ic;
741 ifp = ic->ic_ifp;
743 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
744 "%s: detaching\n",
745 device_get_nameunit(sc->dev));
747 RT2860_SOFTC_LOCK(sc);
749 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
751 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
753 callout_stop(&sc->periodic_ch);
754 callout_stop(&sc->tx_watchdog_ch);
756 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
757 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
758 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
759 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
761 /* free Tx and Rx rings */
763 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
764 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
766 rt2860_free_rx_ring(sc, &sc->rx_ring);
768 RT2860_SOFTC_UNLOCK(sc);
770 bpfdetach(ifp);
772 ieee80211_ifdetach(ic);
774 if_free(ifp);
776 taskqueue_free(sc->taskqueue);
778 mtx_destroy(&sc->lock);
780 bus_generic_detach(dev);
782 bus_teardown_intr(dev, sc->irq, sc->irqh);
784 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
786 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
788 return 0;
792 * rt2860_shutdown
794 static int rt2860_shutdown(device_t dev)
796 struct rt2860_softc *sc;
798 sc = device_get_softc(dev);
800 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
801 "%s: shutting down\n",
802 device_get_nameunit(sc->dev));
804 rt2860_stop(sc);
806 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
808 return 0;
812 * rt2860_suspend
814 static int rt2860_suspend(device_t dev)
816 struct rt2860_softc *sc;
818 sc = device_get_softc(dev);
820 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
821 "%s: suspending\n",
822 device_get_nameunit(sc->dev));
824 rt2860_stop(sc);
826 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
828 return 0;
832 * rt2860_resume
834 static int rt2860_resume(device_t dev)
836 struct rt2860_softc *sc;
837 struct ifnet *ifp;
839 sc = device_get_softc(dev);
840 ifp = sc->ifp;
842 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
843 "%s: resuming\n",
844 device_get_nameunit(sc->dev));
846 if (ifp->if_flags & IFF_UP)
847 rt2860_init(sc);
849 return 0;
853 * rt2860_init_channels
855 static void rt2860_init_channels(struct rt2860_softc *sc)
857 struct ieee80211com *ic;
858 struct ieee80211_channel *c;
859 int i, flags;
861 ic = &sc->ic;
863 /* set supported channels for 2GHz band */
865 for (i = 1; i <= 14; i++)
867 c = &ic->ic_channels[ic->ic_nchans++];
868 flags = IEEE80211_CHAN_B;
870 c->ic_freq = ieee80211_ieee2mhz(i, flags);
871 c->ic_ieee = i;
872 c->ic_flags = flags;
874 c = &ic->ic_channels[ic->ic_nchans++];
875 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
877 c->ic_freq = ieee80211_ieee2mhz(i, flags);
878 c->ic_ieee = i;
879 c->ic_flags = flags;
881 c = &ic->ic_channels[ic->ic_nchans++];
882 flags = IEEE80211_CHAN_G;
884 c->ic_freq = ieee80211_ieee2mhz(i, flags);
885 c->ic_ieee = i;
886 c->ic_flags = flags;
888 c = &ic->ic_channels[ic->ic_nchans++];
889 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
891 c->ic_freq = ieee80211_ieee2mhz(i, flags);
892 c->ic_ieee = i;
893 c->ic_flags = flags;
896 /* set supported channels for 5GHz band */
898 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
899 sc->rf_rev == RT2860_EEPROM_RF_2750)
901 for (i = 36; i <= 64; i += 4)
903 c = &ic->ic_channels[ic->ic_nchans++];
904 flags = IEEE80211_CHAN_A;
906 c->ic_freq = ieee80211_ieee2mhz(i, flags);
907 c->ic_ieee = i;
908 c->ic_flags = flags;
910 c = &ic->ic_channels[ic->ic_nchans++];
911 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
913 c->ic_freq = ieee80211_ieee2mhz(i, flags);
914 c->ic_ieee = i;
915 c->ic_flags = flags;
918 for (i = 100; i <= 140; i += 4)
920 c = &ic->ic_channels[ic->ic_nchans++];
921 flags = IEEE80211_CHAN_A;
923 c->ic_freq = ieee80211_ieee2mhz(i, flags);
924 c->ic_ieee = i;
925 c->ic_flags = flags;
927 c = &ic->ic_channels[ic->ic_nchans++];
928 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
930 c->ic_freq = ieee80211_ieee2mhz(i, flags);
931 c->ic_ieee = i;
932 c->ic_flags = flags;
935 for (i = 149; i <= 165; i += 4)
937 c = &ic->ic_channels[ic->ic_nchans++];
938 flags = IEEE80211_CHAN_A;
940 c->ic_freq = ieee80211_ieee2mhz(i, flags);
941 c->ic_ieee = i;
942 c->ic_flags = flags;
944 c = &ic->ic_channels[ic->ic_nchans++];
945 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
947 c->ic_freq = ieee80211_ieee2mhz(i, flags);
948 c->ic_ieee = i;
949 c->ic_flags = flags;
955 * rt2860_init_channels_ht40
957 static void rt2860_init_channels_ht40(struct rt2860_softc *sc)
959 struct ieee80211com *ic;
960 struct ieee80211_channel *c, *cent, *ext;
961 int i, flags;
963 ic = &sc->ic;
965 /* set supported channels for 2GHz band */
967 for (i = 1; i <= 14; i++)
969 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
971 /* find the center channel */
973 cent = ieee80211_find_channel_byieee(ic, i,
974 flags & ~IEEE80211_CHAN_HT);
975 if (cent == NULL)
977 printf("%s: skip channel %d, could not find center channel\n",
978 device_get_nameunit(sc->dev), i);
979 continue;
982 /* find the extension channel */
984 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
985 flags & ~IEEE80211_CHAN_HT);
986 if (ext == NULL)
988 printf("%s: skip channel %d, could not find extension channel\n",
989 device_get_nameunit(sc->dev), i);
990 continue;
993 c = &ic->ic_channels[ic->ic_nchans++];
995 *c = *cent;
996 c->ic_extieee = ext->ic_ieee;
997 c->ic_flags &= ~IEEE80211_CHAN_HT;
998 c->ic_flags |= IEEE80211_CHAN_HT40U;
1000 c = &ic->ic_channels[ic->ic_nchans++];
1002 *c = *ext;
1003 c->ic_extieee = cent->ic_ieee;
1004 c->ic_flags &= ~IEEE80211_CHAN_HT;
1005 c->ic_flags |= IEEE80211_CHAN_HT40D;
1008 /* set supported channels for 5GHz band */
1010 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
1011 sc->rf_rev == RT2860_EEPROM_RF_2750)
1013 for (i = 36; i <= 64; i += 4)
1015 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1017 /* find the center channel */
1019 cent = ieee80211_find_channel_byieee(ic, i,
1020 flags & ~IEEE80211_CHAN_HT);
1021 if (cent == NULL)
1023 printf("%s: skip channel %d, could not find center channel\n",
1024 device_get_nameunit(sc->dev), i);
1025 continue;
1028 /* find the extension channel */
1030 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1031 flags & ~IEEE80211_CHAN_HT);
1032 if (ext == NULL)
1034 printf("%s: skip channel %d, could not find extension channel\n",
1035 device_get_nameunit(sc->dev), i);
1036 continue;
1039 c = &ic->ic_channels[ic->ic_nchans++];
1041 *c = *cent;
1042 c->ic_extieee = ext->ic_ieee;
1043 c->ic_flags &= ~IEEE80211_CHAN_HT;
1044 c->ic_flags |= IEEE80211_CHAN_HT40U;
1046 c = &ic->ic_channels[ic->ic_nchans++];
1048 *c = *ext;
1049 c->ic_extieee = cent->ic_ieee;
1050 c->ic_flags &= ~IEEE80211_CHAN_HT;
1051 c->ic_flags |= IEEE80211_CHAN_HT40D;
1054 for (i = 100; i <= 140; i += 4)
1056 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1058 /* find the center channel */
1060 cent = ieee80211_find_channel_byieee(ic, i,
1061 flags & ~IEEE80211_CHAN_HT);
1062 if (cent == NULL)
1064 printf("%s: skip channel %d, could not find center channel\n",
1065 device_get_nameunit(sc->dev), i);
1066 continue;
1069 /* find the extension channel */
1071 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1072 flags & ~IEEE80211_CHAN_HT);
1073 if (ext == NULL)
1075 printf("%s: skip channel %d, could not find extension channel\n",
1076 device_get_nameunit(sc->dev), i);
1077 continue;
1080 c = &ic->ic_channels[ic->ic_nchans++];
1082 *c = *cent;
1083 c->ic_extieee = ext->ic_ieee;
1084 c->ic_flags &= ~IEEE80211_CHAN_HT;
1085 c->ic_flags |= IEEE80211_CHAN_HT40U;
1087 c = &ic->ic_channels[ic->ic_nchans++];
1089 *c = *ext;
1090 c->ic_extieee = cent->ic_ieee;
1091 c->ic_flags &= ~IEEE80211_CHAN_HT;
1092 c->ic_flags |= IEEE80211_CHAN_HT40D;
1095 for (i = 149; i <= 165; i += 4)
1097 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1099 /* find the center channel */
1101 cent = ieee80211_find_channel_byieee(ic, i,
1102 flags & ~IEEE80211_CHAN_HT);
1103 if (cent == NULL)
1105 printf("%s: skip channel %d, could not find center channel\n",
1106 device_get_nameunit(sc->dev), i);
1107 continue;
1110 /* find the extension channel */
1112 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1113 flags & ~IEEE80211_CHAN_HT);
1114 if (ext == NULL)
1116 printf("%s: skip channel %d, could not find extension channel\n",
1117 device_get_nameunit(sc->dev), i);
1118 continue;
1121 c = &ic->ic_channels[ic->ic_nchans++];
1123 *c = *cent;
1124 c->ic_extieee = ext->ic_ieee;
1125 c->ic_flags &= ~IEEE80211_CHAN_HT;
1126 c->ic_flags |= IEEE80211_CHAN_HT40U;
1128 c = &ic->ic_channels[ic->ic_nchans++];
1130 *c = *ext;
1131 c->ic_extieee = cent->ic_ieee;
1132 c->ic_flags &= ~IEEE80211_CHAN_HT;
1133 c->ic_flags |= IEEE80211_CHAN_HT40D;
1139 * rt2860_init_locked
1141 static void rt2860_init_locked(void *priv)
1143 struct rt2860_softc *sc;
1144 struct ieee80211com *ic;
1145 struct ifnet *ifp;
1146 int error, i, ntries;
1147 uint32_t tmp, stacnt[6];
1149 sc = priv;
1150 ic = &sc->ic;
1151 ifp = ic->ic_ifp;
1153 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1154 "%s: initializing\n",
1155 device_get_nameunit(sc->dev));
1157 RT2860_SOFTC_ASSERT_LOCKED(sc);
1159 if (!(sc->flags & RT2860_SOFTC_FLAGS_UCODE_LOADED))
1161 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1162 "%s: loading 8051 microcode\n",
1163 device_get_nameunit(sc->dev));
1165 error = rt2860_io_mcu_load_ucode(sc, rt2860_ucode, sizeof(rt2860_ucode));
1166 if (error != 0)
1168 printf("%s: could not load 8051 microcode\n",
1169 device_get_nameunit(sc->dev));
1170 goto fail;
1173 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1174 "%s: 8051 microcode was successfully loaded\n",
1175 device_get_nameunit(sc->dev));
1177 sc->flags |= RT2860_SOFTC_FLAGS_UCODE_LOADED;
1180 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x2);
1182 /* disable DMA engine */
1184 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1186 tmp &= 0xff0;
1187 tmp |= RT2860_REG_TX_WB_DDONE;
1189 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1191 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, 0xffffffff);
1193 /* PBF hardware reset */
1195 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1196 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1198 /* wait while DMA engine is busy */
1200 for (ntries = 0; ntries < 100; ntries++)
1202 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1203 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1204 break;
1206 DELAY(1000);
1209 if (ntries == 100)
1211 printf("%s: timeout waiting for DMA engine\n",
1212 device_get_nameunit(sc->dev));
1213 goto fail;
1216 tmp &= 0xff0;
1217 tmp |= RT2860_REG_TX_WB_DDONE;
1219 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1221 /* reset Rx and Tx rings */
1223 tmp = RT2860_REG_RST_IDX_RX |
1224 RT2860_REG_RST_IDX_TX_MGMT |
1225 RT2860_REG_RST_IDX_TX_HCCA |
1226 RT2860_REG_RST_IDX_TX_AC3 |
1227 RT2860_REG_RST_IDX_TX_AC2 |
1228 RT2860_REG_RST_IDX_TX_AC1 |
1229 RT2860_REG_RST_IDX_TX_AC0;
1231 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, tmp);
1233 /* PBF hardware reset */
1235 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1236 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1238 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x3);
1240 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1241 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1242 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1244 /* init Tx power per rate */
1246 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
1248 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1249 continue;
1251 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i),
1252 sc->txpow_rate_20mhz[i]);
1255 for (i = 0; i < RT2860_DEF_MAC_SIZE; i++)
1256 rt2860_io_mac_write(sc, rt2860_def_mac[i].reg,
1257 rt2860_def_mac[i].val);
1259 /* wait while MAC is busy */
1261 for (ntries = 0; ntries < 100; ntries++)
1263 if (!(rt2860_io_mac_read(sc, RT2860_REG_STATUS_CFG) &
1264 (RT2860_REG_STATUS_TX_BUSY | RT2860_REG_STATUS_RX_BUSY)))
1265 break;
1267 DELAY(1000);
1270 if (ntries == 100)
1272 printf("%s: timeout waiting for MAC\n",
1273 device_get_nameunit(sc->dev));
1274 goto fail;
1277 /* clear Host to MCU mailbox */
1279 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX_BBP_AGENT, 0);
1280 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX, 0);
1282 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_BOOT,
1283 RT2860_REG_H2M_TOKEN_NO_INTR, 0);
1285 DELAY(1000);
1287 error = rt2860_init_bbp(sc);
1288 if (error != 0)
1289 goto fail;
1291 /* set up maximum buffer sizes */
1293 tmp = (1 << 12) | RT2860_MAX_AGG_SIZE;
1295 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1297 /* set mac address */
1299 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1301 rt2860_asic_set_macaddr(sc, ic->ic_myaddr);
1303 /* clear statistic registers */
1305 rt2860_io_mac_read_multi(sc, RT2860_REG_RX_STA_CNT0,
1306 stacnt, sizeof(stacnt));
1308 /* set RTS threshold */
1310 rt2860_asic_update_rtsthreshold(sc);
1312 /* set Tx power */
1314 rt2860_asic_update_txpower(sc);
1316 /* set up protection mode */
1318 sc->tx_ampdu_sessions = 0;
1320 rt2860_asic_updateprot(sc);
1322 /* clear beacon frame space (entries = 8, entry size = 512) */
1324 rt2860_io_mac_set_region_4(sc, RT2860_REG_BEACON_BASE(0), 0, 1024);
1326 taskqueue_unblock(sc->taskqueue);
1328 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
1330 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1331 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
1333 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1335 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
1336 sc->tx_ring[i].desc_phys_addr);
1337 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
1338 RT2860_SOFTC_TX_RING_DESC_COUNT);
1339 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
1342 /* init Rx ring */
1344 rt2860_reset_rx_ring(sc, &sc->rx_ring);
1346 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
1347 sc->rx_ring.desc_phys_addr);
1348 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
1349 RT2860_SOFTC_RX_RING_DATA_COUNT);
1350 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
1351 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
1353 /* wait while DMA engine is busy */
1355 for (ntries = 0; ntries < 100; ntries++)
1357 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1358 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1359 break;
1361 DELAY(1000);
1364 if (ntries == 100)
1366 printf("%s: timeout waiting for DMA engine\n",
1367 device_get_nameunit(sc->dev));
1368 goto fail;
1371 tmp &= 0xff0;
1372 tmp |= RT2860_REG_TX_WB_DDONE;
1374 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1376 /* disable interrupts mitigation */
1378 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_DELAY_INT_CFG, 0);
1380 /* send LEDs operating mode to microcontroller */
1382 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED1,
1383 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1384 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED2,
1385 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1386 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED3,
1387 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1389 /* turn radio LED on */
1391 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON);
1393 /* write vendor-specific BBP values (from EEPROM) */
1395 for (i = 0; i < RT2860_SOFTC_BBP_EEPROM_COUNT; i++)
1397 if (sc->bbp_eeprom[i].reg == 0x00 ||
1398 sc->bbp_eeprom[i].reg == 0xff)
1399 continue;
1401 rt2860_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1402 sc->bbp_eeprom[i].val);
1406 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_GPIO_CTRL_CFG);
1407 if (tmp & (1 << 2))
1409 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_SLEEP,
1410 RT2860_REG_H2M_TOKEN_RADIOOFF, 0x02ff);
1411 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_RADIOOFF);
1413 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_WAKEUP,
1414 RT2860_REG_H2M_TOKEN_WAKEUP, 0);
1415 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_WAKEUP);
1419 /* disable non-existing Rx chains */
1421 tmp = rt2860_io_bbp_read(sc, 3);
1423 tmp &= ~((1 << 4) | (1 << 3));
1425 if (sc->nrxpath == 3)
1426 tmp |= (1 << 4);
1427 else if (sc->nrxpath == 2)
1428 tmp |= (1 << 3);
1430 rt2860_io_bbp_write(sc, 3, tmp);
1432 /* disable non-existing Tx chains */
1434 tmp = rt2860_io_bbp_read(sc, 1);
1436 tmp &= ~((1 << 4) | (1 << 3));
1438 if (sc->ntxpath == 2)
1439 tmp |= (1 << 4);
1441 rt2860_io_bbp_write(sc, 1, tmp);
1443 /* set current channel */
1445 rt2860_rf_set_chan(sc, ic->ic_curchan);
1447 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1448 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1449 (48 << 16) | 96);
1451 if ((sc->mac_rev & 0xffff) != 0x0101)
1452 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1454 /* clear pending interrupts */
1456 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1458 /* enable interrupts */
1460 tmp = RT2860_REG_INT_TX_COHERENT |
1461 RT2860_REG_INT_RX_COHERENT |
1462 RT2860_REG_INT_GP_TIMER |
1463 RT2860_REG_INT_AUTO_WAKEUP |
1464 RT2860_REG_INT_FIFO_STA_FULL |
1466 RT2860_REG_INT_PRE_TBTT |
1467 RT2860_REG_INT_TBTT |
1469 RT2860_REG_INT_TXRX_COHERENT |
1470 RT2860_REG_INT_MCU_CMD |
1471 RT2860_REG_INT_TX_MGMT_DONE |
1472 RT2860_REG_INT_TX_HCCA_DONE |
1473 RT2860_REG_INT_TX_AC3_DONE |
1474 RT2860_REG_INT_TX_AC2_DONE |
1475 RT2860_REG_INT_TX_AC1_DONE |
1476 RT2860_REG_INT_TX_AC0_DONE |
1477 RT2860_REG_INT_RX_DONE;
1479 sc->intr_enable_mask = tmp;
1481 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1483 if (rt2860_txrx_enable(sc) != 0)
1484 goto fail;
1486 /* clear garbage interrupts */
1488 tmp = rt2860_io_mac_read(sc, 0x1300);
1490 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1491 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1493 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1495 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1496 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1498 else
1500 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1503 sc->periodic_round = 0;
1505 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1507 return;
1509 fail:
1511 rt2860_stop_locked(sc);
1515 * rt2860_init
1517 static void rt2860_init(void *priv)
1519 struct rt2860_softc *sc;
1521 sc = priv;
1523 RT2860_SOFTC_LOCK(sc);
1525 rt2860_init_locked(sc);
1527 RT2860_SOFTC_UNLOCK(sc);
1531 * rt2860_init_bbp
1533 static int rt2860_init_bbp(struct rt2860_softc *sc)
1535 int ntries, i;
1536 uint8_t tmp;
1538 for (ntries = 0; ntries < 20; ntries++)
1540 tmp = rt2860_io_bbp_read(sc, 0);
1541 if (tmp != 0x00 && tmp != 0xff)
1542 break;
1545 if (tmp == 0x00 || tmp == 0xff)
1547 printf("%s: timeout waiting for BBP to wakeup\n",
1548 device_get_nameunit(sc->dev));
1549 return ETIMEDOUT;
1552 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1553 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1554 rt2860_def_bbp[i].val);
1556 if ((sc->mac_rev & 0xffff) != 0x0101)
1557 rt2860_io_bbp_write(sc, 84, 0x19);
1559 return 0;
1563 * rt2860_stop_locked
1565 static void rt2860_stop_locked(void *priv)
1567 struct rt2860_softc *sc;
1568 struct ieee80211com *ic;
1569 struct ifnet *ifp;
1570 uint32_t tmp;
1572 sc = priv;
1573 ic = &sc->ic;
1574 ifp = ic->ic_ifp;
1576 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1577 "%s: stopping\n",
1578 device_get_nameunit(sc->dev));
1580 RT2860_SOFTC_ASSERT_LOCKED(sc);
1582 sc->tx_timer = 0;
1584 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1585 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1587 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1589 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1591 callout_stop(&sc->periodic_ch);
1592 callout_stop(&sc->tx_watchdog_ch);
1594 RT2860_SOFTC_UNLOCK(sc);
1596 taskqueue_block(sc->taskqueue);
1598 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1599 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1600 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1601 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1603 RT2860_SOFTC_LOCK(sc);
1605 /* clear key tables */
1607 rt2860_asic_clear_keytables(sc);
1609 /* disable interrupts */
1611 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1613 /* disable Tx/Rx */
1615 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1617 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1619 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1621 /* reset adapter */
1623 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1624 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1625 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1627 if (sc->beacon_mbuf != NULL)
1629 m_free(sc->beacon_mbuf);
1630 sc->beacon_mbuf = NULL;
1635 * rt2860_stop
1637 static void rt2860_stop(void *priv)
1639 struct rt2860_softc *sc;
1641 sc = priv;
1643 RT2860_SOFTC_LOCK(sc);
1645 rt2860_stop_locked(sc);
1647 RT2860_SOFTC_UNLOCK(sc);
1651 * rt2860_start
1653 static void rt2860_start(struct ifnet *ifp)
1655 struct rt2860_softc *sc;
1656 struct ieee80211com *ic;
1657 struct ieee80211_node *ni;
1658 struct ether_header *eh;
1659 struct mbuf *m;
1660 int qid;
1662 sc = ifp->if_softc;
1663 ic = &sc->ic;
1665 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1666 return;
1668 for (;;)
1670 IF_POLL(&ic->ic_mgtq, m);
1671 if (m != NULL)
1673 RT2860_SOFTC_TX_RING_LOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
1675 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1677 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
1679 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1680 "%s: if_start: Tx ring with qid=%d is full\n",
1681 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
1683 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1685 sc->tx_data_queue_full[sc->tx_ring_mgtqid]++;
1687 break;
1690 IF_DEQUEUE(&ic->ic_mgtq, m);
1691 if (m == NULL)
1693 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
1694 break;
1697 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1699 m->m_pkthdr.rcvif = NULL;
1701 if (bpf_peers_present(ic->ic_rawbpf))
1702 bpf_mtap(ic->ic_rawbpf, m);
1704 if (rt2860_tx_frame(sc, m, ni, sc->tx_ring_mgtqid) != 0)
1706 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
1707 break;
1710 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[sc->tx_ring_mgtqid]);
1712 rt2860_drain_fifo_stats(sc);
1714 else
1716 if (ic->ic_state != IEEE80211_S_RUN)
1717 break;
1719 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1720 if (m == NULL)
1721 break;
1723 if (ic->ic_flags & IEEE80211_F_SCAN)
1724 ieee80211_cancel_scan(ic);
1726 if (m->m_len < sizeof(struct ether_header) &&
1727 !(m = m_pullup(m, sizeof (struct ether_header))))
1728 continue;
1730 eh = mtod(m, struct ether_header *);
1732 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1733 if (ni == NULL)
1735 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1736 "%s: if_start: could not find Tx node\n",
1737 device_get_nameunit(sc->dev));
1739 m_freem(m);
1741 continue;
1744 ieee80211_classify(ic, m, ni);
1746 qid = M_WME_GETAC(m);
1748 RT2860_SOFTC_TX_RING_LOCK(&sc->tx_ring[qid]);
1750 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1752 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
1754 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1755 "%s: if_start: Tx ring with qid=%d is full\n",
1756 device_get_nameunit(sc->dev), qid);
1758 m_freem(m);
1759 ieee80211_free_node(ni);
1761 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1762 ifp->if_oerrors++;
1764 sc->tx_data_queue_full[qid]++;
1766 break;
1769 BPF_MTAP(ifp, m);
1771 m = ieee80211_encap(ic, m, ni);
1772 if (m == NULL)
1774 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
1776 ieee80211_free_node(ni);
1778 ifp->if_oerrors++;
1780 continue;
1783 if (bpf_peers_present(ic->ic_rawbpf))
1784 bpf_mtap(ic->ic_rawbpf, m);
1786 if (rt2860_tx_frame(sc, m, ni, qid) != 0)
1788 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
1790 ieee80211_free_node(ni);
1792 ifp->if_oerrors++;
1794 break;
1797 RT2860_SOFTC_TX_RING_UNLOCK(&sc->tx_ring[qid]);
1799 rt2860_drain_fifo_stats(sc);
1802 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1804 ic->ic_lastdata = ticks;
1806 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1811 * rt2860_ioctl
1813 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1815 struct rt2860_softc *sc;
1816 struct ieee80211com *ic;
1817 int error;
1819 sc = ifp->if_softc;
1820 ic = &sc->ic;
1822 error = 0;
1824 switch (cmd)
1826 case SIOCSIFFLAGS:
1827 RT2860_SOFTC_LOCK(sc);
1829 if (ifp->if_flags & IFF_UP)
1831 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1833 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1834 rt2860_asic_update_promisc(sc);
1836 else
1838 rt2860_init_locked(sc);
1841 else
1843 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1844 rt2860_stop_locked(sc);
1847 sc->if_flags = ifp->if_flags;
1849 RT2860_SOFTC_UNLOCK(sc);
1850 break;
1852 default:
1853 error = ieee80211_ioctl(ic, cmd, data);
1856 if (error == ENETRESET)
1858 RT2860_SOFTC_LOCK(sc);
1860 if ((ifp->if_flags & IFF_UP) &&
1861 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1862 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1864 rt2860_stop_locked(sc);
1865 rt2860_init_locked(sc);
1868 RT2860_SOFTC_UNLOCK(sc);
1870 error = 0;
1873 return error;
1877 * rt2860_reset
1879 static int rt2860_reset(struct ifnet *ifp)
1881 struct rt2860_softc *sc;
1882 struct ieee80211com *ic;
1884 sc = ifp->if_softc;
1885 ic = &sc->ic;
1887 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1888 return ENETRESET;
1890 rt2860_rf_set_chan(sc, ic->ic_curchan);
1892 return 0;
1896 * rt2860_newstate
1898 static int rt2860_newstate(struct ieee80211com *ic,
1899 enum ieee80211_state nstate, int arg)
1901 struct rt2860_softc *sc;
1902 struct ifnet *ifp;
1903 struct ieee80211_node *ni;
1904 int error;
1906 ifp = ic->ic_ifp;
1907 sc = ifp->if_softc;
1909 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1910 "%s: newstate: %s -> %s\n",
1911 device_get_nameunit(sc->dev),
1912 ieee80211_state_name[ic->ic_state], ieee80211_state_name[nstate]);
1914 error = sc->newstate(ic, nstate, arg);
1915 if (error != 0)
1916 return error;
1918 RT2860_SOFTC_LOCK(sc);
1920 /* turn link LED off */
1922 if (nstate != IEEE80211_S_RUN)
1923 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1925 switch (nstate)
1927 case IEEE80211_S_INIT:
1928 rt2860_asic_disable_tsf_sync(sc);
1929 break;
1931 case IEEE80211_S_RUN:
1932 ni = ic->ic_bss;
1934 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1935 rt2860_rf_set_chan(sc, ni->ni_chan);
1937 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1939 rt2860_asic_enable_mrr(sc);
1940 rt2860_asic_set_txpreamble(sc);
1941 rt2860_asic_set_basicrates(sc);
1942 rt2860_asic_update_txpower(sc);
1943 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1946 if (ic->ic_opmode == IEEE80211_M_STA)
1947 rt2860_newassoc(ni, 1);
1949 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1950 ic->ic_opmode == IEEE80211_M_IBSS)
1952 error = rt2860_beacon_alloc(sc);
1953 if (error != 0)
1954 break;
1956 rt2860_asic_update_beacon(sc);
1959 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1960 rt2860_asic_enable_tsf_sync(sc);
1962 /* turn link LED on */
1964 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1966 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1967 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1968 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
1970 break;
1972 case IEEE80211_S_SLEEP:
1973 break;
1975 default:
1976 break;
1979 RT2860_SOFTC_UNLOCK(sc);
1981 return error;
1985 * rt2860_scan_start
1987 static void rt2860_scan_start(struct ieee80211com *ic)
1989 struct rt2860_softc *sc;
1990 struct ifnet *ifp;
1992 ifp = ic->ic_ifp;
1993 sc = ifp->if_softc;
1995 rt2860_asic_disable_tsf_sync(sc);
1999 * rt2860_scan_end
2001 static void rt2860_scan_end(struct ieee80211com *ic)
2003 struct rt2860_softc *sc;
2004 struct ifnet *ifp;
2006 ifp = ic->ic_ifp;
2007 sc = ifp->if_softc;
2009 rt2860_asic_enable_tsf_sync(sc);
2013 * rt2860_set_channel
2015 static void rt2860_set_channel(struct ieee80211com *ic)
2017 struct rt2860_softc *sc;
2018 struct ifnet *ifp;
2020 ifp = ic->ic_ifp;
2021 sc = ifp->if_softc;
2023 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
2024 "%s: set channel: channel=%u, HT%s%s\n",
2025 device_get_nameunit(sc->dev),
2026 ieee80211_chan2ieee(ic, ic->ic_curchan),
2027 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
2028 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
2029 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
2030 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
2032 RT2860_SOFTC_LOCK(sc);
2034 rt2860_rf_set_chan(sc, ic->ic_curchan);
2036 RT2860_SOFTC_UNLOCK(sc);
2040 * rt2860_newassoc
2042 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
2044 struct rt2860_softc *sc;
2045 struct ieee80211com *ic;
2046 struct ifnet *ifp;
2047 struct rt2860_softc_node *rni;
2048 uint16_t aid;
2049 uint8_t wcid;
2051 ic = ni->ni_ic;
2052 ifp = ic->ic_ifp;
2053 sc = ifp->if_softc;
2054 rni = (struct rt2860_softc_node *) ni;
2056 if (isnew)
2058 aid = IEEE80211_AID(ni->ni_associd);
2059 rni->staid = rt2860_staid_alloc(sc, aid);
2060 wcid = rni->staid;
2062 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
2063 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2065 rt2860_amrr_node_init(&sc->amrr, &sc->amrr_node[wcid], ni);
2067 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2068 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2069 device_get_nameunit(sc->dev),
2070 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2071 ni->ni_associd,
2072 (ni->ni_flags & IEEE80211_NODE_HT) ?
2073 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2074 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2075 (ni->ni_flags & IEEE80211_NODE_HT) ?
2076 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2077 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2079 rt2860_asic_updateprot(sc);
2080 rt2860_asic_updateslot(sc);
2081 rt2860_asic_set_txpreamble(sc);
2084 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
2085 "%s: new association: isnew=%d, macaddr=%s, associd=0x%04x, staid=0x%02x, QoS %s, ERP %s, HT %s\n",
2086 device_get_nameunit(sc->dev), isnew, ether_sprintf(ni->ni_macaddr),
2087 ni->ni_associd, rni->staid,
2088 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
2089 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
2090 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
2095 * rt2860_updateslot
2097 static void rt2860_updateslot(struct ifnet *ifp)
2099 struct rt2860_softc *sc;
2101 sc = ifp->if_softc;
2103 rt2860_asic_updateslot(sc);
2107 * rt2860_wme_update
2109 static int rt2860_wme_update(struct ieee80211com *ic)
2111 struct rt2860_softc *sc;
2112 struct ifnet *ifp;
2114 ifp = ic->ic_ifp;
2115 sc = ifp->if_softc;
2117 rt2860_asic_wme_update(sc);
2119 return 0;
2123 * rt2860_update_beacon
2125 static void rt2860_update_beacon(struct ieee80211com *ic, int what)
2127 struct rt2860_softc *sc;
2128 struct ifnet *ifp;
2129 struct mbuf *m;
2130 struct ieee80211_beacon_offsets *bo;
2132 ifp = ic->ic_ifp;
2133 sc = ifp->if_softc;
2134 m = sc->beacon_mbuf;
2135 bo = &sc->beacon_offsets;
2137 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2138 "%s: update beacon: what=%d\n",
2139 device_get_nameunit(sc->dev), what);
2141 setbit(bo->bo_flags, what);
2143 ieee80211_beacon_update(ic->ic_bss, bo, m, 0);
2145 rt2860_asic_update_beacon(sc);
2149 * rt2860_key_update_begin
2151 static void rt2860_key_update_begin(struct ieee80211com *ic)
2153 struct rt2860_softc *sc;
2154 struct ifnet *ifp;
2156 ifp = ic->ic_ifp;
2157 sc = ifp->if_softc;
2159 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2160 "%s: key update begin\n",
2161 device_get_nameunit(sc->dev));
2163 taskqueue_block(sc->taskqueue);
2165 IF_LOCK(&ifp->if_snd);
2169 * rt2860_key_update_end
2171 static void rt2860_key_update_end(struct ieee80211com *ic)
2173 struct rt2860_softc *sc;
2174 struct ifnet *ifp;
2176 ifp = ic->ic_ifp;
2177 sc = ifp->if_softc;
2179 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2180 "%s: key update end\n",
2181 device_get_nameunit(sc->dev));
2183 IF_UNLOCK(&ifp->if_snd);
2185 taskqueue_unblock(sc->taskqueue);
2189 * rt2860_key_set
2191 static int rt2860_key_set(struct ieee80211com *ic,
2192 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2194 struct rt2860_softc *sc;
2195 struct ifnet *ifp;
2196 struct ieee80211_node *ni;
2197 struct rt2860_softc_node *rni;
2198 uint16_t key_base, keymode_base;
2199 uint8_t mode, vapid, wcid, iv[8];
2200 uint32_t tmp;
2202 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2203 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2204 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2205 return 0;
2207 ifp = ic->ic_ifp;
2208 sc = ifp->if_softc;
2210 switch (k->wk_cipher->ic_cipher)
2212 case IEEE80211_CIPHER_WEP:
2213 if(k->wk_keylen < 8)
2214 mode = RT2860_REG_CIPHER_MODE_WEP40;
2215 else
2216 mode = RT2860_REG_CIPHER_MODE_WEP104;
2217 break;
2219 case IEEE80211_CIPHER_TKIP:
2220 mode = RT2860_REG_CIPHER_MODE_TKIP;
2221 break;
2223 case IEEE80211_CIPHER_AES_CCM:
2224 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2225 break;
2227 default:
2228 return 0;
2231 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2232 "%s: set key: keyix=%d, keylen=%d, macaddr=%s, mode=%d, group=%d\n",
2233 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, ether_sprintf(mac),
2234 mode, (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2236 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2238 /* install pairwise key */
2240 ni = ieee80211_find_node(&ic->ic_sta, mac);
2241 rni = (struct rt2860_softc_node *) ni;
2243 vapid = 0;
2244 wcid = rni->staid;
2245 key_base = RT2860_REG_PKEY(wcid);
2247 ieee80211_free_node(ni);
2249 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2251 memset(iv, 0, 8);
2253 iv[3] = (k->wk_keyix << 6);
2255 else
2257 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2259 iv[0] = (k->wk_keytsc >> 8);
2260 iv[1] = ((iv[0] | 0x20) & 0x7f);
2261 iv[2] = k->wk_keytsc;
2263 else
2265 /* AES CCMP */
2267 iv[0] = k->wk_keytsc;
2268 iv[1] = k->wk_keytsc >> 8;
2269 iv[2] = 0;
2272 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2273 iv[4] = (k->wk_keytsc >> 16);
2274 iv[5] = (k->wk_keytsc >> 24);
2275 iv[6] = (k->wk_keytsc >> 32);
2276 iv[7] = (k->wk_keytsc >> 40);
2278 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2279 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2280 device_get_nameunit(sc->dev),
2281 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2284 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2286 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2288 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2290 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2292 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2293 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2295 else
2297 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2298 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2301 else
2303 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2306 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2307 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2309 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2312 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2313 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2315 /* install group key */
2317 vapid = 0;
2318 wcid = RT2860_WCID_MCAST;
2319 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2320 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2322 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2324 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2326 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2328 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2329 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2331 else
2333 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2334 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2337 else
2339 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2342 tmp = rt2860_io_mac_read(sc, keymode_base);
2344 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2345 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2347 rt2860_io_mac_write(sc, keymode_base, tmp);
2349 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2351 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2353 memset(iv, 0, 8);
2355 iv[3] = (k->wk_keyix << 6);
2357 else
2359 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2361 iv[0] = (k->wk_keytsc >> 8);
2362 iv[1] = ((iv[0] | 0x20) & 0x7f);
2363 iv[2] = k->wk_keytsc;
2365 else
2367 /* AES CCMP */
2369 iv[0] = k->wk_keytsc;
2370 iv[1] = k->wk_keytsc >> 8;
2371 iv[2] = 0;
2374 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2375 iv[4] = (k->wk_keytsc >> 16);
2376 iv[5] = (k->wk_keytsc >> 24);
2377 iv[6] = (k->wk_keytsc >> 32);
2378 iv[7] = (k->wk_keytsc >> 40);
2380 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2381 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2382 device_get_nameunit(sc->dev),
2383 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2386 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2388 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2389 (mode << RT2860_REG_CIPHER_MODE_SHIFT);
2391 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2395 return 1;
2399 * rt2860_key_delete
2401 static int rt2860_key_delete(struct ieee80211com *ic,
2402 const struct ieee80211_key *k)
2404 struct rt2860_softc *sc;
2405 uint8_t vapid, wcid;
2406 uint32_t tmp;
2408 sc = ic->ic_ifp->if_softc;
2410 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2411 "%s: delete key: keyix=%d, keylen=%d, group=%d\n",
2412 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen,
2413 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2415 if (k->wk_flags & IEEE80211_KEY_GROUP)
2417 /* remove group key */
2419 vapid = 0;
2420 wcid = RT2860_WCID_MCAST;
2422 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2424 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2425 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2427 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2429 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2431 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2432 (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2434 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2438 return 1;
2442 * rt2860_raw_xmit
2444 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2445 const struct ieee80211_bpf_params *params)
2447 return 0;
2451 * rt2860_media_change
2453 static int rt2860_media_change(struct ifnet *ifp)
2455 struct rt2860_softc *sc;
2456 int error;
2458 sc = ifp->if_softc;
2460 error = ieee80211_media_change(ifp);
2461 if (error != ENETRESET)
2462 return error;
2464 RT2860_SOFTC_LOCK(sc);
2466 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2468 rt2860_stop_locked(sc);
2469 rt2860_init_locked(sc);
2472 RT2860_SOFTC_UNLOCK(sc);
2474 return 0;
2478 * rt2860_node_alloc
2480 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211_node_table *nt)
2482 return malloc(sizeof(struct rt2860_softc_node),
2483 M_80211_NODE, M_NOWAIT | M_ZERO);
2487 * rt2860_node_cleanup
2489 static void rt2860_node_cleanup(struct ieee80211_node *ni)
2491 struct rt2860_softc *sc;
2492 struct ieee80211com *ic;
2493 struct ifnet *ifp;
2494 struct rt2860_softc_node *rni;
2495 uint8_t vapid, wcid;
2496 uint32_t tmp;
2498 ic = ni->ni_ic;
2499 ifp = ic->ic_ifp;
2500 sc = ifp->if_softc;
2501 rni = (struct rt2860_softc_node *) ni;
2503 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
2504 "%s: node cleanup: macaddr=%s, associd=0x%04x, staid=0x%02x\n",
2505 device_get_nameunit(sc->dev), ether_sprintf(ni->ni_macaddr),
2506 ni->ni_associd, rni->staid);
2508 if (rni->staid != 0)
2510 vapid = 0;
2511 wcid = rni->staid;
2513 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2514 (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2516 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2518 rt2860_staid_delete(sc, rni->staid);
2520 rni->staid = 0;
2523 sc->node_cleanup(ni);
2527 * rt2860_recv_action
2529 static void rt2860_recv_action(struct ieee80211_node *ni,
2530 const uint8_t *frm, const uint8_t *efrm)
2532 struct rt2860_softc *sc;
2533 struct ieee80211com *ic;
2534 struct ifnet *ifp;
2535 struct rt2860_softc_node *rni;
2536 const struct ieee80211_action *ia;
2537 struct ieee80211_tx_ampdu *tx_ampdu;
2538 uint16_t status, baparamset;
2539 uint8_t wcid;
2540 int tid, bufsize, ac;
2541 uint32_t tmp;
2543 ic = ni->ni_ic;
2544 ifp = ic->ic_ifp;
2545 sc = ifp->if_softc;
2546 rni = (struct rt2860_softc_node *) ni;
2548 ia = (const struct ieee80211_action *) frm;
2550 sc->recv_action(ni, frm, efrm);
2552 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2553 return;
2555 wcid = rni->staid;
2557 switch (ia->ia_action)
2559 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2560 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2561 baparamset = LE_READ_2(frm + 3);
2562 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2563 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2565 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2566 "%s: received ADDBA request: associd=0x%04x, tid=%d, bufsize=%d\n",
2567 device_get_nameunit(sc->dev), ni->ni_associd, tid, bufsize);
2569 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2571 tmp |= (0x10000 << tid);
2573 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2574 break;
2576 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2577 case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
2578 status = LE_READ_2(frm + 3);
2579 baparamset = LE_READ_2(frm + 4);
2580 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2581 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2582 ac = TID_TO_WME_AC(tid);
2583 tx_ampdu = &ni->ni_tx_ampdu[ac];
2585 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2586 "%s: received ADDBA response: associd=0x%04x, status=%d, tid=%d, bufsize=%d\n",
2587 device_get_nameunit(sc->dev), ni->ni_associd, status, tid, bufsize);
2589 if (sc->mac_rev >= 0x28720200)
2591 if (tx_ampdu->txa_wnd > 13)
2592 tx_ampdu->txa_wnd = 13;
2594 else
2596 if (tx_ampdu->txa_wnd > 7)
2597 tx_ampdu->txa_wnd = 7;
2600 if (status == IEEE80211_STATUS_SUCCESS)
2602 sc->tx_ampdu_sessions++;
2604 rt2860_asic_updateprot(sc);
2606 break;
2608 /* IEEE80211_ACTION_BA_DELBA */
2609 case IEEE80211_ACTION_BA_DELBA:
2610 baparamset = LE_READ_2(frm + 2);
2611 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2613 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2614 "%s: received DELBA request: associd=0x%04x, tid=%d\n",
2615 device_get_nameunit(sc->dev), ni->ni_associd, tid);
2617 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2619 tmp &= ~(0x10000 << tid);
2621 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2622 break;
2627 * rt2860_send_action
2629 static int rt2860_send_action(struct ieee80211_node *ni,
2630 int category, int action, uint16_t args[4])
2632 struct rt2860_softc *sc;
2633 struct ieee80211com *ic;
2634 struct ifnet *ifp;
2635 struct rt2860_softc_node *rni;
2636 uint16_t baparamset;
2637 uint8_t wcid;
2638 int ret, tid;
2639 uint32_t tmp;
2641 ic = ni->ni_ic;
2642 ifp = ic->ic_ifp;
2643 sc = ifp->if_softc;
2644 rni = (struct rt2860_softc_node *) ni;
2646 ret = sc->send_action(ni, category, action, args);
2648 if (category != IEEE80211_ACTION_CAT_BA)
2649 return ret;
2651 wcid = rni->staid;
2653 switch (action)
2655 /* IEEE80211_ACTION_BA_DELBA */
2656 case IEEE80211_ACTION_BA_DELBA:
2657 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) | args[1];
2659 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2661 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2662 "%s: sending DELBA request: associd=0x%04x, tid=%d\n",
2663 device_get_nameunit(sc->dev), ni->ni_associd, tid);
2665 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) != IEEE80211_DELBAPS_INIT)
2667 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2669 tmp &= ~(0x10000 << tid);
2671 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2673 else
2675 sc->tx_ampdu_sessions--;
2677 rt2860_asic_updateprot(sc);
2679 break;
2682 return ret;
2686 * rt2860_amrr_update_iter_func
2688 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2690 struct rt2860_softc *sc;
2691 struct ieee80211com *ic;
2692 struct rt2860_softc_node *rni;
2693 uint8_t wcid;
2695 sc = arg;
2696 ic = &sc->ic;
2697 rni = (struct rt2860_softc_node *) ni;
2699 /* only associated stations */
2701 if (rni->staid != 0)
2703 wcid = rni->staid;
2705 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2706 "%s: AMRR node: staid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2707 device_get_nameunit(sc->dev),
2708 rni->staid, sc->amrr_node[wcid].txcnt, sc->amrr_node[wcid].success, sc->amrr_node[wcid].retrycnt);
2710 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2712 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2713 "%s:%s node Tx rate: associd=0x%04x, staid=0x%02x, rate=0x%02x, max rate=0x%02x\n",
2714 device_get_nameunit(sc->dev),
2715 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2716 ni->ni_associd, rni->staid,
2717 (ni->ni_flags & IEEE80211_NODE_HT) ?
2718 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2719 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2720 (ni->ni_flags & IEEE80211_NODE_HT) ?
2721 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2722 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2727 * rt2860_periodic
2729 static void rt2860_periodic(void *arg)
2731 struct rt2860_softc *sc;
2733 sc = arg;
2735 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2736 "%s: periodic\n",
2737 device_get_nameunit(sc->dev));
2739 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2743 * rt2860_tx_watchdog
2745 static void rt2860_tx_watchdog(void *arg)
2747 struct rt2860_softc *sc;
2748 struct ifnet *ifp;
2750 sc = arg;
2751 ifp = sc->ifp;
2753 if (sc->tx_timer == 0)
2754 return;
2756 if (--sc->tx_timer == 0)
2758 printf("%s: Tx watchdog timeout: resetting\n",
2759 device_get_nameunit(sc->dev));
2761 rt2860_stop_locked(sc);
2762 rt2860_init_locked(sc);
2764 ifp->if_oerrors++;
2766 sc->tx_watchdog_timeouts++;
2769 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2773 * rt2860_staid_alloc
2775 static int rt2860_staid_alloc(struct rt2860_softc *sc, int aid)
2777 int staid;
2779 if ((aid > 0 && aid < RT2860_SOFTC_STAID_COUNT) && isclr(sc->staid_mask, aid))
2781 staid = aid;
2783 else
2785 for (staid = 1; staid < RT2860_SOFTC_STAID_COUNT; staid++)
2787 if (isclr(sc->staid_mask, staid))
2788 break;
2792 setbit(sc->staid_mask, staid);
2794 return staid;
2798 * rt2860_staid_delete
2800 static void rt2860_staid_delete(struct rt2860_softc *sc, int staid)
2802 clrbit(sc->staid_mask, staid);
2806 * rt2860_asic_set_bssid
2808 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2809 const uint8_t *bssid)
2811 uint32_t tmp;
2813 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2814 "%s: set bssid: bssid=%s\n",
2815 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2817 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2819 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2821 tmp = bssid[4] | (bssid[5] << 8);
2823 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2827 * rt2860_asic_set_macaddr
2829 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2830 const uint8_t *addr)
2832 uint32_t tmp;
2834 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2836 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2838 tmp = addr[4] | (addr[5] << 8);
2840 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2844 * rt2860_asic_enable_tsf_sync
2846 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2848 struct ieee80211com *ic;
2849 uint32_t tmp;
2851 ic = &sc->ic;
2853 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2854 "%s: enabling TSF\n",
2855 device_get_nameunit(sc->dev));
2857 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2859 tmp &= ~0x1fffff;
2860 tmp |= ic->ic_bss->ni_intval * 16;
2861 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2863 if (ic->ic_opmode == IEEE80211_M_STA)
2865 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2867 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2869 tmp |= RT2860_REG_BCN_TX_ENABLE;
2870 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2872 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2874 tmp |= RT2860_REG_BCN_TX_ENABLE;
2875 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2878 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2882 * rt2860_asic_disable_tsf_sync
2884 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2886 uint32_t tmp;
2888 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2889 "%s: disabling TSF\n",
2890 device_get_nameunit(sc->dev));
2892 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2894 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2895 RT2860_REG_TSF_TIMER_ENABLE |
2896 RT2860_REG_TBTT_TIMER_ENABLE);
2898 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2899 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2901 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2905 * rt2860_asic_enable_mrr
2907 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2909 #define CCK(mcs) (mcs)
2910 #define OFDM(mcs) ((1 << 3) | (mcs))
2911 #define HT(mcs) (mcs)
2913 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2914 (OFDM(6) << 28) | /* 54 -> 48 */
2915 (OFDM(5) << 24) | /* 48 -> 36 */
2916 (OFDM(4) << 20) | /* 36 -> 24 */
2917 (OFDM(3) << 16) | /* 24 -> 18 */
2918 (OFDM(2) << 12) | /* 18 -> 12 */
2919 (OFDM(1) << 8) | /* 12 -> 9 */
2920 (OFDM(0) << 4) | /* 9 -> 6 */
2921 OFDM(0)); /* 6 -> 6 */
2923 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2924 (CCK(2) << 12) | /* 11 -> 5.5 */
2925 (CCK(1) << 8) | /* 5.5 -> 2 */
2926 (CCK(0) << 4) | /* 2 -> 1 */
2927 CCK(0)); /* 1 -> 1 */
2929 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2930 (HT(6) << 28) |
2931 (HT(5) << 24) |
2932 (HT(4) << 20) |
2933 (HT(3) << 16) |
2934 (HT(2) << 12) |
2935 (HT(1) << 8) |
2936 (HT(0) << 4) |
2937 HT(0));
2939 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2940 (HT(14) << 28) |
2941 (HT(13) << 24) |
2942 (HT(12) << 20) |
2943 (HT(11) << 16) |
2944 (HT(10) << 12) |
2945 (HT(9) << 8) |
2946 (HT(8) << 4) |
2947 HT(7));
2949 #undef HT
2950 #undef OFDM
2951 #undef CCK
2955 * rt2860_asic_set_txpreamble
2957 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2959 struct ieee80211com *ic;
2960 uint32_t tmp;
2962 ic = &sc->ic;
2964 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2965 "%s: %s short Tx preamble\n",
2966 device_get_nameunit(sc->dev),
2967 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2969 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2971 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2973 if (sc->ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2974 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2976 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2980 * rt2860_asic_set_basicrates
2982 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2984 struct ieee80211com *ic;
2986 ic = &sc->ic;
2988 if (ic->ic_curmode == IEEE80211_MODE_11B)
2989 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2990 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2991 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2992 else
2993 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2997 * rt2860_asic_update_rtsthreshold
2999 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
3001 struct ieee80211com *ic;
3002 uint32_t tmp;
3003 uint16_t threshold;
3005 ic = &sc->ic;
3007 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3008 "%s: updating RTS threshold: %d\n",
3009 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
3011 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
3013 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
3015 if (ic->ic_flags_ext & IEEE80211_FEXT_AMSDU_TX)
3016 threshold = 0x1000;
3017 else
3018 threshold = ic->ic_rtsthreshold;
3021 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
3022 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
3024 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
3028 * rt2860_asic_update_txpower
3030 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
3032 struct ieee80211com *ic;
3033 uint32_t *txpow_rate;
3034 int8_t delta;
3035 uint8_t val;
3036 uint32_t tmp;
3037 int i;
3039 ic = &sc->ic;
3041 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3042 "%s: updating Tx power: %d\n",
3043 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
3045 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
3047 txpow_rate = sc->txpow_rate_20mhz;
3049 else
3051 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3052 txpow_rate = sc->txpow_rate_40mhz_2ghz;
3053 else
3054 txpow_rate = sc->txpow_rate_40mhz_5ghz;
3057 delta = 0;
3059 val = rt2860_io_bbp_read(sc, 1);
3060 val &= 0xfc;
3062 if (ic->ic_txpowlimit > 90)
3064 /* do nothing */
3066 else if (ic->ic_txpowlimit > 60)
3068 delta -= 1;
3070 else if (ic->ic_txpowlimit > 30)
3072 delta -= 3;
3074 else if (ic->ic_txpowlimit > 15)
3076 val |= 0x1;
3078 else if (ic->ic_txpowlimit > 9)
3080 val |= 0x1;
3081 delta -= 3;
3083 else
3085 val |= 0x2;
3088 rt2860_io_bbp_write(sc, 1, val);
3090 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
3092 if (txpow_rate[i] == 0xffffffff)
3093 continue;
3095 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
3097 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
3102 * rt2860_asic_update_promisc
3104 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
3106 struct ifnet *ifp;
3107 uint32_t tmp;
3109 ifp = sc->ic.ic_ifp;
3111 printf("%s: %s promiscuous mode\n",
3112 device_get_nameunit(sc->dev),
3113 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
3115 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
3117 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
3119 if (!(ifp->if_flags & IFF_PROMISC))
3120 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
3122 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
3126 * rt2860_asic_updateprot
3128 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
3130 struct ieee80211com *ic;
3131 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
3132 uint8_t htopmode;
3133 enum ieee80211_protmode htprotmode;
3135 ic = &sc->ic;
3137 /* CCK frame protection */
3139 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3140 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
3142 /* set up protection frame phy mode and rate (MCS code) */
3144 if (ic->ic_curmode == IEEE80211_MODE_11A)
3145 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3146 (0 << RT2860_REG_PROT_MCS_SHIFT);
3147 else
3148 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3149 (3 << RT2860_REG_PROT_MCS_SHIFT));
3151 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
3153 /* OFDM frame protection */
3155 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3156 RT2860_REG_TXOP_ALLOW_ALL;
3158 if (ic->ic_flags & IEEE80211_F_USEPROT)
3160 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3161 "%s: updating protection mode: b/g protection mode=%s\n",
3162 device_get_nameunit(sc->dev),
3163 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3164 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3166 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3167 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3168 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3169 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
3170 else
3171 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3173 else
3175 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3176 "%s: updating protection mode: b/g protection mode=%s\n",
3177 device_get_nameunit(sc->dev), "none");
3179 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3182 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
3184 /* HT frame protection */
3186 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
3187 htopmode = ic->ic_bss->ni_htopmode;
3188 else
3189 htopmode = ic->ic_curhtprotmode;
3191 htprotmode = ic->ic_htprotmode;
3193 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3195 if (sc->tx_ampdu_sessions > 0)
3197 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3198 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3199 device_get_nameunit(sc->dev));
3201 htopmode = IEEE80211_HTINFO_OPMODE_MIXED;
3202 htprotmode = IEEE80211_PROT_RTSCTS;
3205 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3206 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3207 device_get_nameunit(sc->dev),
3208 htopmode & IEEE80211_HTINFO_OPMODE,
3209 (htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3210 ((htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3212 switch (htopmode & IEEE80211_HTINFO_OPMODE)
3214 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3215 case IEEE80211_HTINFO_OPMODE_HT20PR:
3216 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3217 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3218 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3219 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3220 (4 << RT2860_REG_PROT_MCS_SHIFT);
3222 gf20_prot = mm20_prot;
3224 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
3225 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3226 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3228 if (htprotmode == IEEE80211_PROT_RTSCTS)
3229 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3230 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3231 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3232 else
3233 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3235 gf40_prot = mm40_prot;
3236 break;
3238 /* IEEE80211_HTINFO_OPMODE_MIXED */
3239 case IEEE80211_HTINFO_OPMODE_MIXED:
3240 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
3241 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3242 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
3244 if (ic->ic_flags & IEEE80211_F_USEPROT)
3245 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3246 (3 << RT2860_REG_PROT_MCS_SHIFT);
3247 else
3248 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3249 (4 << RT2860_REG_PROT_MCS_SHIFT);
3251 if (htprotmode == IEEE80211_PROT_RTSCTS)
3252 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3253 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3254 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
3255 else
3256 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
3258 gf20_prot = mm20_prot;
3260 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
3262 if (ic->ic_flags & IEEE80211_F_USEPROT)
3263 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3264 (3 << RT2860_REG_PROT_MCS_SHIFT);
3265 else
3266 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3267 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3269 if (htprotmode == IEEE80211_PROT_RTSCTS)
3270 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3271 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3272 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3273 else
3274 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3276 gf40_prot = mm40_prot;
3277 break;
3280 * IEEE80211_HTINFO_OPMODE_PURE
3281 * IEEE80211_HTINFO_OPMODE_PROTOPT
3283 case IEEE80211_HTINFO_OPMODE_PURE:
3284 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3285 default:
3286 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3287 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3288 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3289 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3290 (4 << RT2860_REG_PROT_MCS_SHIFT);
3292 gf20_prot = mm20_prot;
3294 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3295 RT2860_REG_TXOP_ALLOW_ALL |
3296 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3297 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3299 gf40_prot = mm40_prot;
3300 break;
3303 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3304 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3305 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3306 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3310 * rt2860_asic_updateslot
3312 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3314 struct ieee80211com *ic;
3315 uint32_t tmp;
3317 ic = &sc->ic;
3319 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3320 "%s: %s short slot time\n",
3321 device_get_nameunit(sc->dev),
3322 (ic->ic_flags & (IEEE80211_F_SHSLOT | IEEE80211_F_BURST)) ? "enabling" : "disabling");
3324 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3326 tmp &= ~0xff;
3327 tmp |= (ic->ic_flags & (IEEE80211_F_SHSLOT |IEEE80211_F_BURST)) ? 9 : 20;
3329 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3333 * rt2860_asic_wme_update
3335 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3337 struct ieee80211com *ic;
3338 struct ieee80211_wme_state *wme;
3339 const struct wmeParams *wmep;
3340 int i;
3342 ic = &sc->ic;
3343 wme = &ic->ic_wme;
3344 wmep = wme->wme_chanParams.cap_wmeParams;
3346 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3347 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3348 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3349 device_get_nameunit(sc->dev),
3350 wmep[WME_AC_VO].wmep_aifsn,
3351 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3352 wmep[WME_AC_VO].wmep_txopLimit,
3353 wmep[WME_AC_VI].wmep_aifsn,
3354 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3355 wmep[WME_AC_VI].wmep_txopLimit,
3356 wmep[WME_AC_BK].wmep_aifsn,
3357 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3358 wmep[WME_AC_BK].wmep_txopLimit,
3359 wmep[WME_AC_BE].wmep_aifsn,
3360 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3361 wmep[WME_AC_BE].wmep_txopLimit);
3363 for (i = 0; i < WME_NUM_AC; i++)
3364 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3365 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3366 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3368 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3369 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3370 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3372 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3373 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3374 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3376 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3377 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3378 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3380 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3381 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3383 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3384 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3388 * rt2860_asic_update_beacon
3390 static void rt2860_asic_update_beacon(struct rt2860_softc *sc)
3392 struct ieee80211com *ic;
3393 struct mbuf *m;
3394 struct rt2860_txwi *txwi;
3395 uint32_t tmp;
3397 ic = &sc->ic;
3399 m = sc->beacon_mbuf;
3400 txwi = &sc->beacon_txwi;
3402 /* disable temporarily TSF sync */
3404 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3406 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3407 RT2860_REG_TSF_TIMER_ENABLE |
3408 RT2860_REG_TBTT_TIMER_ENABLE);
3410 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3412 /* write Tx wireless info and beacon frame to on-chip memory */
3414 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3415 txwi, sizeof(struct rt2860_txwi));
3417 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3418 mtod(m, uint8_t *), m->m_pkthdr.len);
3420 /* enable again TSF sync */
3422 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3424 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3425 RT2860_REG_TSF_TIMER_ENABLE |
3426 RT2860_REG_TBTT_TIMER_ENABLE);
3428 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3432 * rt2860_asic_clear_keytables
3434 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3436 int i;
3438 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3440 for (i = 0; i < 256; i++)
3442 rt2860_io_mac_write(sc, RT2860_REG_WCID(i), 0xffffffff);
3443 rt2860_io_mac_write(sc, RT2860_REG_WCID(i) + 4, 0x0000ffff);
3446 /* clear WCID attribute table (entries = 256, entry size = 4) */
3448 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3450 /* clear IV/EIV table (entries = 256, entry size = 8) */
3452 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3454 /* clear pairwise key table (entries = 64, entry size = 32) */
3456 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 64);
3458 /* clear shared key table (entries = 32, entry size = 32) */
3460 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3462 /* clear shared key mode (entries = 32, entry size = 2) */
3464 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3468 * rt2860_beacon_alloc
3470 static int rt2860_beacon_alloc(struct rt2860_softc *sc)
3472 struct ieee80211com *ic;
3473 struct mbuf *m;
3474 struct rt2860_txwi txwi;
3475 uint8_t rate, mcs;
3477 ic = &sc->ic;
3479 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3480 if (m == NULL)
3481 return ENOMEM;
3483 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3484 mcs = rt2860_rate2mcs(rate);
3486 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3488 txwi.wcid = 0xff;
3489 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3490 RT2860_TXWI_MPDU_LEN_SHIFT);
3491 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3492 txwi.mpdu_density_flags |=
3493 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3494 txwi.bawin_size_xflags |=
3495 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3497 if (rate == 2)
3499 txwi.phymode_ifs_stbc_shortgi =
3500 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3502 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3503 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3505 else
3507 txwi.phymode_ifs_stbc_shortgi =
3508 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3511 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3512 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3514 if (sc->beacon_mbuf != NULL)
3516 m_free(sc->beacon_mbuf);
3517 sc->beacon_mbuf = NULL;
3520 sc->beacon_mbuf = m;
3521 sc->beacon_txwi = txwi;
3523 return 0;
3527 * rt2860_rxrate
3529 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3531 uint8_t mcs, phymode;
3532 uint8_t rate;
3534 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3535 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3536 RT2860_RXWI_PHYMODE_MASK;
3538 rate = 2;
3540 switch (phymode)
3542 case RT2860_RXWI_PHYMODE_CCK:
3543 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3545 case 0: rate = 2; break; /* 1 Mbps */
3546 case 1: rate = 4; break; /* 2 MBps */
3547 case 2: rate = 11; break; /* 5.5 Mbps */
3548 case 3: rate = 22; break; /* 11 Mbps */
3550 break;
3552 case RT2860_RXWI_PHYMODE_OFDM:
3553 switch (mcs)
3555 case 0: rate = 12; break; /* 6 Mbps */
3556 case 1: rate = 18; break; /* 9 Mbps */
3557 case 2: rate = 24; break; /* 12 Mbps */
3558 case 3: rate = 36; break; /* 18 Mbps */
3559 case 4: rate = 48; break; /* 24 Mbps */
3560 case 5: rate = 72; break; /* 36 Mbps */
3561 case 6: rate = 96; break; /* 48 Mbps */
3562 case 7: rate = 108; break; /* 54 Mbps */
3564 break;
3566 case RT2860_RXWI_PHYMODE_HT_MIXED:
3567 case RT2860_RXWI_PHYMODE_HT_GF:
3568 break;
3571 return rate;
3575 * rt2860_maxrssi_rxpath
3577 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3578 const struct rt2860_rxwi *rxwi)
3580 uint8_t rxpath;
3582 rxpath = 0;
3584 if (sc->nrxpath > 1)
3585 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3586 rxpath = 1;
3588 if (sc->nrxpath > 2)
3589 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3590 rxpath = 2;
3592 return rxpath;
3596 * rt2860_rssi2dbm
3598 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3599 uint8_t rssi, uint8_t rxpath)
3601 struct ieee80211com *ic;
3602 struct ieee80211_channel *c;
3603 int chan;
3604 int8_t rssi_off, lna_gain;
3606 if (rssi == 0)
3607 return -99;
3609 ic = &sc->ic;
3610 c = ic->ic_curchan;
3611 chan = ieee80211_chan2ieee(ic, c);
3613 if (IEEE80211_IS_CHAN_5GHZ(c))
3615 rssi_off = sc->rssi_off_5ghz[rxpath];
3617 if (chan <= 64)
3618 lna_gain = sc->lna_gain[1];
3619 else if (chan <= 128)
3620 lna_gain = sc->lna_gain[2];
3621 else
3622 lna_gain = sc->lna_gain[3];
3624 else
3626 rssi_off = sc->rssi_off_2ghz[rxpath];
3627 lna_gain = sc->lna_gain[0];
3630 return (-12 - rssi_off - lna_gain - rssi);
3634 * rt2860_rate2mcs
3636 static uint8_t rt2860_rate2mcs(uint8_t rate)
3638 switch (rate)
3640 /* CCK rates */
3641 case 2: return 0;
3642 case 4: return 1;
3643 case 11: return 2;
3644 case 22: return 3;
3646 /* OFDM rates */
3647 case 12: return 0;
3648 case 18: return 1;
3649 case 24: return 2;
3650 case 36: return 3;
3651 case 48: return 4;
3652 case 72: return 5;
3653 case 96: return 6;
3654 case 108: return 7;
3657 return 0;
3661 * rt2860_ackrate
3663 static int rt2860_ackrate(struct ieee80211com *ic, int rate)
3665 switch (rate)
3667 /* CCK rates */
3669 case 2:
3670 return 2;
3672 case 4:
3673 case 11:
3674 case 22:
3675 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3677 /* OFDM rates */
3679 case 12:
3680 case 18:
3681 return 12;
3683 case 24:
3684 case 36:
3685 return 24;
3687 case 48:
3688 case 72:
3689 case 96:
3690 case 108:
3691 return 48;
3694 /* default to 1Mbps */
3695 return 2;
3699 * rt2860_txtime
3701 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags)
3703 uint16_t txtime;
3705 if (RT2860_RATE_IS_OFDM(rate))
3707 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3708 txtime = 16 + 4 + 4 * txtime + 6;
3710 else
3712 txtime = (16 * len + rate - 1) / rate;
3714 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3715 txtime += 72 + 24;
3716 else
3717 txtime += 144 + 48;
3720 return txtime;
3724 * rt2860_tx_frame
3726 static int rt2860_tx_frame(struct rt2860_softc *sc,
3727 struct mbuf *m, struct ieee80211_node *ni, int qid)
3729 struct ieee80211com *ic;
3730 struct rt2860_softc_node *rni;
3731 struct rt2860_softc_tx_ring *ring;
3732 struct rt2860_softc_tx_data *data;
3733 struct rt2860_txdesc *desc;
3734 struct rt2860_txwi *txwi;
3735 struct ieee80211_frame *wh;
3736 struct ieee80211_tx_ampdu *tx_ampdu;
3737 struct rt2860_softc_tx_radiotap_header *tap;
3738 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3739 u_int hdrsize, hdrspace;
3740 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, mpdu_density, bawin_size, qsel;
3741 uint16_t qos, len, dmalen, mpdu_len, dur;
3742 int error, hasqos, ac, ampdu, mimops, ackrate, ndmasegs, ndescs, i, j;
3744 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3745 ("%s: Tx frame: invalid qid=%d\n",
3746 device_get_nameunit(sc->dev), qid));
3748 RT2860_SOFTC_TX_RING_ASSERT_LOCKED(&sc->tx_ring[qid]);
3750 ic = &sc->ic;
3751 rni = (struct rt2860_softc_node *) ni;
3753 ring = &sc->tx_ring[qid];
3754 desc = &ring->desc[ring->desc_cur];
3755 data = &ring->data[ring->data_cur];
3756 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3758 wh = mtod(m, struct ieee80211_frame *);
3760 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3762 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3763 if (hasqos)
3765 if (IEEE80211_HAS_ADDR4(wh))
3766 qos = le16toh(*(const uint16_t *)
3767 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3768 else
3769 qos = le16toh(*(const uint16_t *)
3770 (((struct ieee80211_qosframe *) wh)->i_qos));
3772 else
3774 qos = 0;
3777 if (ni->ni_flags & IEEE80211_NODE_HT)
3779 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3780 rate = 0;
3781 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3782 rate = ic->ic_fixed_rate;
3783 else
3784 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
3786 else
3788 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3789 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3790 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3791 rate = ic->ic_fixed_rate;
3792 else
3793 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
3796 rate &= IEEE80211_RATE_VAL;
3798 /* fill Tx wireless info */
3800 if (ni->ni_flags & IEEE80211_NODE_HT)
3801 mcs = rate;
3802 else
3803 mcs = rt2860_rate2mcs(rate);
3805 /* management frames do not need encryption */
3807 if (type == IEEE80211_FC0_TYPE_DATA)
3808 wcid = !IEEE80211_IS_MULTICAST(wh->i_addr1) ? rni->staid : RT2860_WCID_MCAST;
3809 else
3810 wcid = RT2860_WCID_RESERVED;
3812 /* calculate MPDU length without padding */
3814 hdrsize = ieee80211_hdrsize(wh);
3815 hdrspace = ieee80211_hdrspace(ic, wh);
3816 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3818 memset(txwi, 0, sizeof(struct rt2860_txwi));
3820 txwi->wcid = wcid;
3822 /* MIMO power save */
3824 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
3825 ((ni->ni_htcap & IEEE80211_HTCAP_SMPS) != IEEE80211_HTCAP_SMPS_OFF))
3827 if (mcs > 7)
3829 if ((ni->ni_htcap & IEEE80211_HTCAP_SMPS) == IEEE80211_HTCAP_SMPS_DYNAMIC)
3831 /* dynamic MIMO power save */
3833 txwi->mpdu_density_flags |=
3834 (RT2860_TXWI_FLAGS_MIMOPS << RT2860_TXWI_FLAGS_SHIFT);
3836 else
3838 /* static MIMO power save */
3840 mcs = 7;
3844 mimops = 1;
3846 else
3848 mimops = 0;
3851 pid = mcs;
3853 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3854 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3855 RT2860_TXWI_MPDU_LEN_SHIFT);
3857 stbc = (ic->ic_htcaps & IEEE80211_HTCAP_TXSTBC) &&
3858 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC);
3860 shortgi = (ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
3861 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_htcap & (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40));
3863 txwi->phymode_ifs_stbc_shortgi |=
3864 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
3865 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3867 if (ni->ni_flags & IEEE80211_NODE_HT)
3869 txwi->phymode_ifs_stbc_shortgi |=
3870 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3872 else
3874 if (!RT2860_RATE_IS_OFDM(rate))
3876 txwi->phymode_ifs_stbc_shortgi |=
3877 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3879 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3880 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3882 else
3884 txwi->phymode_ifs_stbc_shortgi |=
3885 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3889 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_chw == 40))
3890 bw = RT2860_TXWI_BW_40;
3891 else
3892 bw = RT2860_TXWI_BW_20;
3894 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
3895 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3897 if (type != IEEE80211_FC0_TYPE_DATA)
3898 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3899 else
3900 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3902 /* skip ACKs for multicast frames */
3904 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3905 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
3907 txwi->bawin_size_xflags |=
3908 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3910 if (ni->ni_flags & IEEE80211_NODE_HT)
3912 /* preamble + plcp + signal extension + SIFS */
3914 dur = 16 + 4 + 6 + 10;
3916 else
3918 ackrate = rt2860_ackrate(ic, rate);
3920 dur = rt2860_txtime(RT2860_ACK_SIZE, ackrate, ic->ic_flags) +
3921 sc->sifs;
3924 *(uint16_t *) wh->i_dur = htole16(dur);
3927 /* check for A-MPDU */
3929 if ((qos & IEEE80211_QOS_ACKPOLICY) == IEEE80211_QOS_ACKPOLICY_BA)
3931 ac = M_WME_GETAC(m);
3932 tx_ampdu = &ni->ni_tx_ampdu[ac];
3934 mpdu_density = RT2860_MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3935 bawin_size = tx_ampdu->txa_wnd;
3937 txwi->mpdu_density_flags |=
3938 ((mpdu_density & RT2860_TXWI_MPDU_DENSITY_MASK) << RT2860_TXWI_MPDU_DENSITY_SHIFT) |
3939 (RT2860_TXWI_FLAGS_AMPDU << RT2860_TXWI_FLAGS_SHIFT);
3941 txwi->bawin_size_xflags |=
3942 ((bawin_size & RT2860_TXWI_BAWIN_SIZE_MASK) << RT2860_TXWI_BAWIN_SIZE_SHIFT);
3944 if (IEEE80211_HAS_ADDR4(wh))
3945 ((struct ieee80211_qosframe_addr4 *) wh)->i_qos[0] &= ~IEEE80211_QOS_ACKPOLICY;
3946 else
3947 ((struct ieee80211_qosframe *) wh)->i_qos[0] &= ~IEEE80211_QOS_ACKPOLICY;
3949 ampdu = 1;
3951 else
3953 ampdu = 0;
3956 /* ask MAC to insert timestamp into probe responses */
3958 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3959 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3960 txwi->mpdu_density_flags |=
3961 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3963 if (bpf_peers_present(sc->drvbpf))
3965 tap = &sc->txtap;
3967 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3968 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3969 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3970 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3971 tap->chan_maxpow = 0;
3973 if (ni->ni_flags & IEEE80211_NODE_HT)
3974 tap->rate = mcs | IEEE80211_RATE_MCS;
3975 else
3976 tap->rate = rate;
3978 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3979 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3981 if (shortgi)
3982 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3984 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3985 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3987 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3989 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3991 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3993 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3995 else
3997 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
4001 /* copy and trim 802.11 header */
4003 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
4004 m_adj(m, hdrspace);
4006 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4007 dma_seg, &ndmasegs, 0);
4008 if (error != 0)
4010 /* too many fragments, linearize */
4012 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4013 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
4014 device_get_nameunit(sc->dev));
4016 m = m_defrag(m, M_DONTWAIT);
4017 if (m == NULL)
4018 return ENOMEM;
4020 sc->tx_defrag_packets++;
4022 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4023 dma_seg, &ndmasegs, 0);
4024 if (error != 0)
4026 printf("%s: could not load mbuf DMA map: error=%d\n",
4027 device_get_nameunit(sc->dev), error);
4028 m_freem(m);
4029 return error;
4033 if (m->m_pkthdr.len == 0)
4034 ndmasegs = 0;
4036 /* determine how many Tx descs are required */
4038 ndescs = 1 + ndmasegs / 2;
4039 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
4041 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4042 "%s: there are not enough Tx descs\n",
4043 device_get_nameunit(sc->dev));
4045 sc->no_tx_desc_avail++;
4047 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4048 m_freem(m);
4049 return EFBIG;
4052 data->m = m;
4053 data->ni = ni;
4055 /* set up Tx descs */
4057 /* first segment is Tx wireless info and 802.11 header */
4059 len = sizeof(struct rt2860_txwi) + hdrsize;
4061 /* align end on a 4-bytes boundary */
4063 dmalen = (len + 3) & ~ 3;
4065 memset((caddr_t) txwi + len, 0, dmalen - len);
4067 qsel = RT2860_TXDESC_QSEL_EDCA;
4069 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
4070 desc->sdl0 = htole16(dmalen);
4071 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4073 /* set up payload segments */
4075 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
4077 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4078 desc->sdl1 = htole16(dma_seg[j].ds_len);
4080 ring->desc_queued++;
4081 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4083 j++;
4085 desc = &ring->desc[ring->desc_cur];
4087 desc->sdp0 = htole32(dma_seg[j].ds_addr);
4088 desc->sdl0 = htole16(dma_seg[j].ds_len);
4089 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4091 j++;
4094 /* finalize last payload segment */
4096 if (i > 0)
4098 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4099 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
4101 else
4103 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
4104 desc->sdl1 = 0;
4107 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4108 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4109 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, ampdu=%d, mimops=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
4110 device_get_nameunit(sc->dev),
4111 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
4112 bw, stbc, shortgi, mcs, wcid, ampdu, mimops, dmalen, ndmasegs,
4113 dma_seg[0].ds_len, dma_seg[1].ds_len, dma_seg[2].ds_len, dma_seg[3].ds_len, dma_seg[4].ds_len);
4115 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
4116 BUS_DMASYNC_PREWRITE);
4117 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4118 BUS_DMASYNC_PREWRITE);
4119 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4120 BUS_DMASYNC_PREWRITE);
4122 ring->desc_queued++;
4123 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4125 ring->data_queued++;
4126 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4128 /* kick Tx */
4130 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
4132 return 0;
4136 * rt2860_tx_raw
4138 static int rt2860_tx_raw(struct rt2860_softc *sc,
4139 struct mbuf *m, struct ieee80211_node *ni,
4140 const struct ieee80211_bpf_params *params)
4142 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4143 "%s: Tx raw\n",
4144 device_get_nameunit(sc->dev));
4146 return 0;
4150 * rt2860_intr
4152 static void rt2860_intr(void *arg)
4154 struct rt2860_softc *sc;
4155 struct ifnet *ifp;
4156 uint32_t status;
4158 sc = arg;
4159 ifp = sc->ifp;
4161 /* acknowledge interrupts */
4163 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
4164 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
4166 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4167 "%s: interrupt: status = 0x%08x\n",
4168 device_get_nameunit(sc->dev), status);
4170 if (status == 0xffffffff || /* device likely went away */
4171 status == 0) /* not for us */
4172 return;
4174 sc->interrupts++;
4176 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4177 return;
4179 if (status & RT2860_REG_INT_TX_COHERENT)
4180 rt2860_tx_coherent_intr(sc);
4182 if (status & RT2860_REG_INT_RX_COHERENT)
4183 rt2860_rx_coherent_intr(sc);
4185 if (status & RT2860_REG_INT_TXRX_COHERENT)
4186 rt2860_txrx_coherent_intr(sc);
4188 if (status & RT2860_REG_INT_FIFO_STA_FULL)
4189 rt2860_fifo_sta_full_intr(sc);
4191 if (status & RT2860_REG_INT_TX_MGMT_DONE)
4192 rt2860_tx_intr(sc, 5);
4194 if (status & RT2860_REG_INT_RX_DONE)
4195 rt2860_rx_intr(sc);
4197 if (status & RT2860_REG_INT_RX_DELAY_DONE)
4198 rt2860_rx_delay_intr(sc);
4200 if (status & RT2860_REG_INT_TX_HCCA_DONE)
4201 rt2860_tx_intr(sc, 4);
4203 if (status & RT2860_REG_INT_TX_AC3_DONE)
4204 rt2860_tx_intr(sc, 3);
4206 if (status & RT2860_REG_INT_TX_AC2_DONE)
4207 rt2860_tx_intr(sc, 2);
4209 if (status & RT2860_REG_INT_TX_AC1_DONE)
4210 rt2860_tx_intr(sc, 1);
4212 if (status & RT2860_REG_INT_TX_AC0_DONE)
4213 rt2860_tx_intr(sc, 0);
4215 if (status & RT2860_REG_INT_TX_DELAY_DONE)
4216 rt2860_tx_delay_intr(sc);
4218 if (status & RT2860_REG_INT_PRE_TBTT)
4219 rt2860_pre_tbtt_intr(sc);
4221 if (status & RT2860_REG_INT_TBTT)
4222 rt2860_tbtt_intr(sc);
4224 if (status & RT2860_REG_INT_MCU_CMD)
4225 rt2860_mcu_cmd_intr(sc);
4227 if (status & RT2860_REG_INT_AUTO_WAKEUP)
4228 rt2860_auto_wakeup_intr(sc);
4230 if (status & RT2860_REG_INT_GP_TIMER)
4231 rt2860_gp_timer_intr(sc);
4235 * rt2860_tx_coherent_intr
4237 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
4239 uint32_t tmp;
4240 int i;
4242 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4243 "%s: Tx coherent interrupt\n",
4244 device_get_nameunit(sc->dev));
4246 sc->tx_coherent_interrupts++;
4248 /* restart DMA engine */
4250 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4252 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4253 RT2860_REG_RX_DMA_ENABLE |
4254 RT2860_REG_TX_DMA_ENABLE);
4256 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4258 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4260 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4261 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4263 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4265 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4266 sc->tx_ring[i].desc_phys_addr);
4267 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4268 RT2860_SOFTC_TX_RING_DESC_COUNT);
4269 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4272 /* init Rx ring */
4274 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4276 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4277 sc->rx_ring.desc_phys_addr);
4278 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4279 RT2860_SOFTC_RX_RING_DATA_COUNT);
4280 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4281 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4283 rt2860_txrx_enable(sc);
4287 * rt2860_rx_coherent_intr
4289 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
4291 uint32_t tmp;
4292 int i;
4294 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4295 "%s: Rx coherent interrupt\n",
4296 device_get_nameunit(sc->dev));
4298 sc->rx_coherent_interrupts++;
4300 /* restart DMA engine */
4302 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4304 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4305 RT2860_REG_RX_DMA_ENABLE |
4306 RT2860_REG_TX_DMA_ENABLE);
4308 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4310 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4312 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4313 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4315 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4317 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4318 sc->tx_ring[i].desc_phys_addr);
4319 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4320 RT2860_SOFTC_TX_RING_DESC_COUNT);
4321 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4324 /* init Rx ring */
4326 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4328 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4329 sc->rx_ring.desc_phys_addr);
4330 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4331 RT2860_SOFTC_RX_RING_DATA_COUNT);
4332 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4333 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4335 rt2860_txrx_enable(sc);
4339 * rt2860_txrx_coherent_intr
4341 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
4343 uint32_t tmp;
4344 int i;
4346 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4347 "%s: Tx/Rx coherent interrupt\n",
4348 device_get_nameunit(sc->dev));
4350 sc->txrx_coherent_interrupts++;
4352 /* restart DMA engine */
4354 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4356 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4357 RT2860_REG_RX_DMA_ENABLE |
4358 RT2860_REG_TX_DMA_ENABLE);
4360 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4362 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4364 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4365 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4367 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4369 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4370 sc->tx_ring[i].desc_phys_addr);
4371 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4372 RT2860_SOFTC_TX_RING_DESC_COUNT);
4373 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4376 /* init Rx ring */
4378 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4380 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4381 sc->rx_ring.desc_phys_addr);
4382 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4383 RT2860_SOFTC_RX_RING_DATA_COUNT);
4384 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4385 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4387 rt2860_txrx_enable(sc);
4391 * rt2860_fifo_sta_full_intr
4393 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4395 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4396 "%s: FIFO statistic full interrupt\n",
4397 device_get_nameunit(sc->dev));
4399 sc->fifo_sta_full_interrupts++;
4401 RT2860_SOFTC_LOCK(sc);
4403 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4405 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4407 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4410 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4412 RT2860_SOFTC_UNLOCK(sc);
4416 * rt2860_rx_intr
4418 static void rt2860_rx_intr(struct rt2860_softc *sc)
4420 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4421 "%s: Rx interrupt\n",
4422 device_get_nameunit(sc->dev));
4424 sc->rx_interrupts++;
4426 RT2860_SOFTC_LOCK(sc);
4428 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4430 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4432 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4435 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4437 RT2860_SOFTC_UNLOCK(sc);
4441 * rt2860_rx_delay_intr
4443 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4445 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4446 "%s: Rx delay interrupt\n",
4447 device_get_nameunit(sc->dev));
4449 sc->rx_delay_interrupts++;
4453 * rt2860_tx_intr
4455 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4457 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4458 ("%s: Tx interrupt: invalid qid=%d\n",
4459 device_get_nameunit(sc->dev), qid));
4461 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4462 "%s: Tx interrupt: qid=%d\n",
4463 device_get_nameunit(sc->dev), qid);
4465 sc->tx_interrupts[qid]++;
4467 RT2860_SOFTC_LOCK(sc);
4469 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4471 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4473 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4476 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4478 RT2860_SOFTC_UNLOCK(sc);
4482 * rt2860_tx_delay_intr
4484 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4486 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4487 "%s: Tx delay interrupt\n",
4488 device_get_nameunit(sc->dev));
4490 sc->tx_delay_interrupts++;
4494 * rt2860_pre_tbtt_intr
4496 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4498 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4499 "%s: Pre-TBTT interrupt\n",
4500 device_get_nameunit(sc->dev));
4502 sc->pre_tbtt_interrupts++;
4506 * rt2860_tbtt_intr
4508 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4510 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4511 "%s: TBTT interrupt\n",
4512 device_get_nameunit(sc->dev));
4514 sc->tbtt_interrupts++;
4518 * rt2860_mcu_cmd_intr
4520 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4522 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4523 "%s: MCU command interrupt\n",
4524 device_get_nameunit(sc->dev));
4526 sc->mcu_cmd_interrupts++;
4530 * rt2860_auto_wakeup_intr
4532 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4534 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4535 "%s: auto wakeup interrupt\n",
4536 device_get_nameunit(sc->dev));
4538 sc->auto_wakeup_interrupts++;
4542 * rt2860_gp_timer_intr
4544 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4546 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4547 "%s: GP timer interrupt\n",
4548 device_get_nameunit(sc->dev));
4550 sc->gp_timer_interrupts++;
4554 * rt2860_rx_done_task
4556 static void rt2860_rx_done_task(void *context, int pending)
4558 struct rt2860_softc *sc;
4559 struct ifnet *ifp;
4560 int again;
4562 sc = context;
4563 ifp = sc->ifp;
4565 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4566 "%s: Rx done task\n",
4567 device_get_nameunit(sc->dev));
4569 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4570 return;
4572 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4574 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4576 RT2860_SOFTC_LOCK(sc);
4578 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4580 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4581 "%s: Rx done task: scheduling again\n",
4582 device_get_nameunit(sc->dev));
4584 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4586 else
4588 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4591 RT2860_SOFTC_UNLOCK(sc);
4595 * rt2860_tx_done_task
4597 static void rt2860_tx_done_task(void *context, int pending)
4599 struct rt2860_softc *sc;
4600 struct ifnet *ifp;
4601 struct ieee80211com *ic;
4602 uint32_t intr_mask;
4603 int i;
4605 sc = context;
4606 ifp = sc->ifp;
4607 ic = &sc->ic;
4609 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4610 "%s: Tx done task\n",
4611 device_get_nameunit(sc->dev));
4613 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4614 return;
4616 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4618 if (sc->intr_pending_mask & (RT2860_REG_INT_TX_AC0_DONE << i))
4620 sc->intr_pending_mask &= ~(RT2860_REG_INT_TX_AC0_DONE << i);
4622 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4626 sc->tx_timer = 0;
4628 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4630 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4631 RT2860_REG_INT_TX_HCCA_DONE |
4632 RT2860_REG_INT_TX_AC3_DONE |
4633 RT2860_REG_INT_TX_AC2_DONE |
4634 RT2860_REG_INT_TX_AC1_DONE |
4635 RT2860_REG_INT_TX_AC0_DONE);
4637 RT2860_SOFTC_LOCK(sc);
4639 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4640 (sc->intr_disable_mask & intr_mask));
4642 if (sc->intr_pending_mask & intr_mask)
4644 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4645 "%s: Tx done task: scheduling again\n",
4646 device_get_nameunit(sc->dev));
4648 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4651 RT2860_SOFTC_UNLOCK(sc);
4653 if (!IFQ_IS_EMPTY(&ic->ic_mgtq) || !IFQ_IS_EMPTY(&ifp->if_snd))
4654 rt2860_start(ifp);
4658 * rt2860_fifo_sta_full_task
4660 static void rt2860_fifo_sta_full_task(void *context, int pending)
4662 struct rt2860_softc *sc;
4663 struct ifnet *ifp;
4664 struct ieee80211com *ic;
4666 sc = context;
4667 ifp = sc->ifp;
4668 ic = &sc->ic;
4670 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4671 "%s: FIFO statistic full task\n",
4672 device_get_nameunit(sc->dev));
4674 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4675 return;
4677 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4679 rt2860_drain_fifo_stats(sc);
4681 RT2860_SOFTC_LOCK(sc);
4683 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4685 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4686 "%s: FIFO statistic full task: scheduling again\n",
4687 device_get_nameunit(sc->dev));
4689 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4691 else
4693 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4696 RT2860_SOFTC_UNLOCK(sc);
4698 if (!IFQ_IS_EMPTY(&ic->ic_mgtq) || !IFQ_IS_EMPTY(&ifp->if_snd))
4699 rt2860_start(ifp);
4703 * rt2860_periodic_task
4705 static void rt2860_periodic_task(void *context, int pending)
4707 struct rt2860_softc *sc;
4708 struct ifnet *ifp;
4709 struct ieee80211com *ic;
4711 sc = context;
4712 ifp = sc->ifp;
4713 ic = &sc->ic;
4715 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4716 "%s: periodic task: round=%lu\n",
4717 device_get_nameunit(sc->dev), sc->periodic_round);
4719 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4720 return;
4722 RT2860_SOFTC_LOCK(sc);
4724 sc->periodic_round++;
4726 rt2860_update_stats(sc);
4728 if ((sc->periodic_round % 10) == 0)
4730 rt2860_bbp_tuning(sc);
4732 rt2860_update_raw_counters(sc);
4734 rt2860_watchdog(sc);
4736 if (ic->ic_opmode != IEEE80211_M_MONITOR && ic->ic_state == IEEE80211_S_RUN)
4738 if (ic->ic_opmode == IEEE80211_M_STA)
4739 rt2860_amrr_update_iter_func(sc, ic->ic_bss);
4740 else
4741 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, sc);
4745 RT2860_SOFTC_UNLOCK(sc);
4747 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4751 * rt2860_rx_eof
4753 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4755 struct ieee80211com *ic;
4756 struct ifnet *ifp;
4757 struct ieee80211_frame *wh;
4758 struct ieee80211_node *ni;
4759 struct rt2860_softc_node *rni;
4760 struct rt2860_softc_rx_radiotap_header *tap;
4761 struct rt2860_softc_rx_ring *ring;
4762 struct rt2860_rxdesc *desc;
4763 struct rt2860_softc_rx_data *data;
4764 struct rt2860_rxwi *rxwi;
4765 struct mbuf *m, *mnew;
4766 bus_dma_segment_t segs[1];
4767 bus_dmamap_t dma_map;
4768 uint32_t index, desc_flags;
4769 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs, keyidx;
4770 int8_t rssi_dbm;
4771 int error, nsegs, len, ampdu, amsdu, nframes, i;
4773 ic = &sc->ic;
4774 ifp = sc->ifp;
4775 ring = &sc->rx_ring;
4777 nframes = 0;
4779 while (limit != 0)
4781 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4782 if (ring->cur == index)
4783 break;
4785 desc = &ring->desc[ring->cur];
4786 desc_flags = le32toh(desc->flags);
4787 data = &ring->data[ring->cur];
4789 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4790 BUS_DMASYNC_POSTREAD);
4792 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4793 break;
4795 nframes++;
4797 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4798 if (mnew == NULL)
4800 sc->rx_mbuf_alloc_errors++;
4801 ifp->if_ierrors++;
4802 goto skip;
4805 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4807 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, ring->spare_dma_map,
4808 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4809 if (error != 0)
4811 m_freem(mnew);
4813 sc->rx_mbuf_dmamap_errors++;
4814 ifp->if_ierrors++;
4816 goto skip;
4819 KASSERT(nsegs == 1, ("%s: too many DMA segments",
4820 device_get_name(sc->dev)));
4822 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4823 BUS_DMASYNC_POSTREAD);
4824 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4826 dma_map = data->dma_map;
4827 data->dma_map = ring->spare_dma_map;
4828 ring->spare_dma_map = dma_map;
4830 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4831 BUS_DMASYNC_PREREAD);
4833 m = data->m;
4835 data->m = mnew;
4836 desc->sdp0 = htole32(segs[0].ds_addr);
4838 /* get Rx wireless info */
4840 rxwi = mtod(m, struct rt2860_rxwi *);
4841 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4842 RT2860_RXWI_SIZE_MASK;
4844 /* check for L2 padding between IEEE 802.11 frame header and body */
4846 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4848 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4849 "%s: L2 padding: len=%d\n",
4850 device_get_nameunit(sc->dev), len);
4852 len += 2;
4855 m->m_pkthdr.rcvif = ifp;
4856 m->m_data = (caddr_t) (rxwi + 1);
4857 m->m_pkthdr.len = m->m_len = len;
4859 /* check for crc errors */
4861 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4863 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4864 "%s: rxdesc: crc error\n",
4865 device_get_nameunit(sc->dev));
4867 ifp->if_ierrors++;
4869 if (!(ifp->if_flags & IFF_PROMISC))
4871 m_freem(m);
4872 goto skip;
4876 wh = (struct ieee80211_frame *) (rxwi + 1);
4878 /* check for cipher errors */
4880 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4882 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4883 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4884 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4886 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4887 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4889 m->m_flags |= M_WEP;
4891 sc->rx_cipher_no_errors++;
4893 else
4895 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4896 "%s: rxdesc: cipher error=0x%02x\n",
4897 device_get_nameunit(sc->dev), cipher_err);
4899 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_ICV)
4900 sc->rx_cipher_icv_errors++;
4901 else if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_MIC)
4902 sc->rx_cipher_mic_errors++;
4903 else if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_INVALID_KEY)
4904 sc->rx_cipher_invalid_key_errors++;
4906 if ((cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_MIC) &&
4907 (desc_flags & RT2860_RXDESC_FLAGS_MYBSS))
4909 keyidx = (rxwi->udf_bssidx_keyidx >> RT2860_RXWI_KEYIDX_SHIFT) &
4910 RT2860_RXWI_KEYIDX_MASK;
4912 ieee80211_notify_michael_failure(ic, wh, keyidx);
4915 ifp->if_ierrors++;
4917 if (!(ifp->if_flags & IFF_PROMISC))
4919 m_free(m);
4920 goto skip;
4924 else
4926 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4928 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4929 "%s: rxdesc: not decrypted but protected flag set\n",
4930 device_get_nameunit(sc->dev));
4932 ifp->if_ierrors++;
4934 if (!(ifp->if_flags & IFF_PROMISC))
4936 m_free(m);
4937 goto skip;
4942 /* check for A-MPDU */
4944 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
4946 m->m_flags |= M_AMPDU;
4948 sc->rx_ampdu++;
4950 ampdu = 1;
4952 else
4954 ampdu = 0;
4957 /* check for A-MSDU */
4959 if (desc_flags & RT2860_RXDESC_FLAGS_AMSDU)
4961 sc->rx_amsdu++;
4963 amsdu = 1;
4965 else
4967 amsdu = 0;
4970 ant = rt2860_maxrssi_rxpath(sc, rxwi);
4971 rssi = rxwi->rssi[ant];
4972 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
4973 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4974 RT2860_RXWI_PHYMODE_MASK);
4975 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
4976 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
4977 RT2860_RXWI_SHORTGI_MASK);
4978 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
4980 if (bpf_peers_present(sc->drvbpf))
4982 tap = &sc->rxtap;
4984 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4985 tap->dbm_antsignal = rssi_dbm;
4986 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
4987 tap->antenna = ant;
4988 tap->antsignal = rssi;
4989 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4990 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4991 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4992 tap->chan_maxpow = 0;
4994 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
4995 tap->rate = mcs | IEEE80211_RATE_MCS;
4996 else
4997 tap->rate = rt2860_rxrate(rxwi);
4999 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
5000 tap->flags |= IEEE80211_RADIOTAP_F_BADFCS;
5002 if (desc_flags & RT2860_RXDESC_FLAGS_FRAG)
5003 tap->flags |= IEEE80211_RADIOTAP_F_FRAG;
5005 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
5006 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5008 if ((desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED) ||
5009 (wh->i_fc[1] & IEEE80211_FC1_WEP))
5010 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
5012 if (shortgi)
5013 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
5015 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
5018 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5019 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
5020 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
5021 device_get_nameunit(sc->dev),
5022 len, phymode, bw, shortgi, mcs,
5023 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
5024 rxwi->snr[0], rxwi->snr[1],
5025 rxwi->wcid, ampdu, amsdu);
5027 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
5029 if (ni != NULL)
5031 rni = (struct rt2860_softc_node *) ni;
5033 for (i = 0; i < RT2860_SOFTC_RSSI_COUNT; i++)
5035 rni->last_rssi[i] = rxwi->rssi[i];
5036 rni->last_rssi_dbm[i] = rt2860_rssi2dbm(sc, rxwi->rssi[i], i);
5040 ieee80211_input(ic, m, ni, rssi_dbm, RT2860_NOISE_FLOOR, 0);
5042 ieee80211_free_node(ni);
5044 skip:
5046 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5048 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5049 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5051 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
5053 limit--;
5056 if (ring->cur == 0)
5057 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5058 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
5059 else
5060 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5061 ring->cur - 1);
5063 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5064 "%s: Rx eof: nframes=%d\n",
5065 device_get_nameunit(sc->dev), nframes);
5067 sc->rx_packets += nframes;
5069 return (limit == 0);
5073 * rt2860_tx_eof
5075 static void rt2860_tx_eof(struct rt2860_softc *sc,
5076 struct rt2860_softc_tx_ring *ring)
5078 struct ifnet *ifp;
5079 struct rt2860_txdesc *desc;
5080 struct rt2860_softc_tx_data *data;
5081 uint32_t index;
5082 int ndescs, nframes;
5084 ifp = sc->ifp;
5086 ndescs = 0;
5087 nframes = 0;
5089 for (;;)
5091 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
5092 if (ring->desc_next == index)
5093 break;
5095 ndescs++;
5097 rt2860_drain_fifo_stats(sc);
5099 desc = &ring->desc[ring->desc_next];
5101 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5102 BUS_DMASYNC_POSTREAD);
5104 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
5105 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
5107 nframes++;
5109 data = &ring->data[ring->data_next];
5111 if (data->m->m_flags & M_TXCB)
5112 ieee80211_process_callback(data->ni, data->m, 0);
5114 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5115 BUS_DMASYNC_POSTWRITE);
5116 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5118 m_freem(data->m);
5120 ieee80211_free_node(data->ni);
5122 data->m = NULL;
5123 data->ni = NULL;
5125 ifp->if_opackets++;
5127 RT2860_SOFTC_TX_RING_LOCK(ring);
5129 ring->data_queued--;
5130 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
5132 RT2860_SOFTC_TX_RING_UNLOCK(ring);
5135 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
5137 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5138 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5140 RT2860_SOFTC_TX_RING_LOCK(ring);
5142 ring->desc_queued--;
5143 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
5145 RT2860_SOFTC_TX_RING_UNLOCK(ring);
5148 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
5149 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
5150 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
5154 * rt2860_update_stats
5156 static void rt2860_update_stats(struct rt2860_softc *sc)
5158 struct ifnet *ifp;
5159 uint32_t stacnt[3];
5160 int beacons, noretryok, retryok, failed, underflows, zerolen;
5162 ifp = sc->ifp;
5164 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5165 "%s: update statistic\n",
5166 device_get_nameunit(sc->dev));
5168 rt2860_drain_fifo_stats(sc);
5170 /* read and clear Tx statistic registers */
5172 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
5173 stacnt, sizeof(stacnt));
5175 stacnt[0] = le32toh(stacnt[0]);
5176 stacnt[1] = le32toh(stacnt[1]);
5177 stacnt[2] = le32toh(stacnt[2]);
5179 beacons = stacnt[0] >> 16;
5180 noretryok = stacnt[1] & 0xffff;
5181 retryok = stacnt[1] >> 16;
5182 failed = stacnt[0] & 0xffff;
5183 underflows = stacnt[2] >> 16;
5184 zerolen = stacnt[2] & 0xffff;
5186 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5187 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
5188 device_get_nameunit(sc->dev),
5189 beacons, noretryok, retryok, failed, underflows, zerolen);
5191 ifp->if_oerrors += failed;
5193 sc->tx_beacons += beacons;
5194 sc->tx_noretryok += noretryok;
5195 sc->tx_retryok += retryok;
5196 sc->tx_failed += failed;
5197 sc->tx_underflows += underflows;
5198 sc->tx_zerolen += zerolen;
5202 * rt2860_bbp_tuning
5204 static void rt2860_bbp_tuning(struct rt2860_softc *sc)
5206 struct ieee80211com *ic;
5207 struct ieee80211_node *ni;
5208 int chan, group;
5209 int8_t rssi, old, new;
5211 /* RT2860C does not support BBP tuning */
5213 if (sc->mac_rev == 0x28600100)
5214 return;
5216 ic = &sc->ic;
5218 if ((ic->ic_flags & IEEE80211_F_SCAN) ||
5219 ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
5220 return;
5222 ni = ic->ic_bss;
5224 chan = ieee80211_chan2ieee(ic, ni->ni_chan);
5226 if (chan <= 14)
5227 group = 0;
5228 else if (chan <= 64)
5229 group = 1;
5230 else if (chan <= 128)
5231 group = 2;
5232 else
5233 group = 3;
5235 rssi = ieee80211_getrssi(ic);
5237 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
5239 new = 0x2e + sc->lna_gain[group];
5241 else
5243 if (!IEEE80211_IS_CHAN_HT40(ni->ni_chan))
5244 new = 0x32 + sc->lna_gain[group] * 5 / 3;
5245 else
5246 new = 0x3a + sc->lna_gain[group] * 5 / 3;
5249 /* Tune if absolute average RSSI is greater than -80 */
5251 if (rssi > -80)
5252 new += 0x10;
5254 old = rt2860_io_bbp_read(sc, 66);
5256 if (old != new)
5257 rt2860_io_bbp_write(sc, 66, new);
5261 * rt2860_watchdog
5263 static void rt2860_watchdog(struct rt2860_softc *sc)
5265 uint32_t tmp;
5266 int ntries;
5268 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5270 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
5271 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
5272 device_get_nameunit(sc->dev), tmp);
5274 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
5276 sc->tx_queue_not_empty[0]++;
5278 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
5280 for (ntries = 0; ntries < 10; ntries++)
5282 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5283 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
5284 break;
5286 DELAY(1);
5289 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5292 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
5294 sc->tx_queue_not_empty[1]++;
5296 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
5298 for (ntries = 0; ntries < 10; ntries++)
5300 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5301 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
5302 break;
5304 DELAY(1);
5307 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5312 * rt2860_drain_fifo_stats
5314 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
5316 struct ifnet *ifp;
5317 uint32_t stats;
5318 uint8_t wcid, mcs, pid;
5319 int ok, agg, retrycnt;
5321 ifp = sc->ic.ic_ifp;
5323 /* drain Tx status FIFO (maxsize = 16) */
5325 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
5326 RT2860_REG_TX_STA_FIFO_VALID)
5328 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
5329 RT2860_REG_TX_STA_FIFO_WCID_MASK;
5331 /* if no ACK was requested, no feedback is available */
5333 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == RT2860_WCID_RESERVED)
5334 continue;
5336 /* update AMRR statistic */
5338 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
5339 agg = (stats & RT2860_REG_TX_STA_FIFO_AGG) ? 1 : 0;
5340 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
5341 RT2860_REG_TX_STA_FIFO_MCS_MASK;
5342 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
5343 RT2860_REG_TX_STA_FIFO_PID_MASK;
5344 retrycnt = pid - mcs;
5346 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5347 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
5348 device_get_nameunit(sc->dev),
5349 wcid, ok, agg, mcs, pid, retrycnt);
5351 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
5353 if (!ok)
5354 ifp->if_oerrors++;
5359 * rt2860_update_raw_counters
5361 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
5363 uint32_t tmp;
5365 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT);
5367 sc->tx_nonagg += tmp & 0xffff;
5368 sc->tx_agg += tmp >> 16;
5370 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT0);
5372 sc->tx_ampdu += (tmp & 0xffff) / 1 + (tmp >> 16) / 2;
5374 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT1);
5376 sc->tx_ampdu += (tmp & 0xffff) / 3 + (tmp >> 16) / 4;
5378 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT2);
5380 sc->tx_ampdu += (tmp & 0xffff) / 5 + (tmp >> 16) / 6;
5382 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT3);
5384 sc->tx_ampdu += (tmp & 0xffff) / 7 + (tmp >> 16) / 8;
5386 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT4);
5388 sc->tx_ampdu += (tmp & 0xffff) / 9 + (tmp >> 16) / 10;
5390 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT5);
5392 sc->tx_ampdu += (tmp & 0xffff) / 11 + (tmp >> 16) / 12;
5394 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT6);
5396 sc->tx_ampdu += (tmp & 0xffff) / 13 + (tmp >> 16) / 14;
5398 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT7);
5400 sc->tx_ampdu += (tmp & 0xffff) / 15 + (tmp >> 16) / 16;
5402 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
5404 sc->rx_crc_errors += tmp & 0xffff;
5405 sc->rx_phy_errors += tmp >> 16;
5407 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
5409 sc->rx_false_ccas += tmp & 0xffff;
5410 sc->rx_plcp_errors += tmp >> 16;
5412 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
5414 sc->rx_dup_packets += tmp & 0xffff;
5415 sc->rx_fifo_overflows += tmp >> 16;
5417 tmp = rt2860_io_mac_read(sc, RT2860_REG_TXRX_MPDU_DEN_CNT);
5419 sc->tx_mpdu_zero_density += tmp & 0xffff;
5420 sc->rx_mpdu_zero_density += tmp >> 16;
5424 * rt2860_intr_enable
5426 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
5428 uint32_t tmp;
5430 sc->intr_disable_mask &= ~intr_mask;
5432 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5434 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5438 * rt2860_intr_disable
5440 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
5442 uint32_t tmp;
5444 sc->intr_disable_mask |= intr_mask;
5446 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5448 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5452 * rt2860_txrx_enable
5454 static int rt2860_txrx_enable(struct rt2860_softc *sc)
5456 struct ieee80211com *ic;
5457 struct ifnet *ifp;
5458 uint32_t tmp;
5459 int ntries;
5461 ic = &sc->ic;
5462 ifp = ic->ic_ifp;
5464 /* enable Tx/Rx DMA engine */
5466 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
5468 for (ntries = 0; ntries < 200; ntries++)
5470 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
5471 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
5472 break;
5474 DELAY(1000);
5477 if (ntries == 200)
5479 printf("%s: timeout waiting for DMA engine\n",
5480 device_get_nameunit(sc->dev));
5481 return -1;
5484 DELAY(50);
5486 tmp |= RT2860_REG_TX_WB_DDONE |
5487 RT2860_REG_RX_DMA_ENABLE |
5488 RT2860_REG_TX_DMA_ENABLE |
5489 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
5491 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5493 /* set Rx filter */
5495 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
5496 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
5498 if (ic->ic_opmode != IEEE80211_M_MONITOR)
5500 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
5501 RT2860_REG_RX_FILTER_DROP_CTS |
5502 RT2860_REG_RX_FILTER_DROP_BA |
5503 RT2860_REG_RX_FILTER_DROP_ACK |
5504 RT2860_REG_RX_FILTER_DROP_VER_ERR |
5505 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
5506 RT2860_REG_RX_FILTER_DROP_CFACK |
5507 RT2860_REG_RX_FILTER_DROP_CFEND;
5509 if (ic->ic_opmode == IEEE80211_M_STA)
5510 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
5511 RT2860_REG_RX_FILTER_DROP_PSPOLL;
5513 if (!(ifp->if_flags & IFF_PROMISC))
5514 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
5517 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
5519 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
5520 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
5522 return 0;
5526 * rt2860_alloc_rx_ring
5528 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
5529 struct rt2860_softc_rx_ring *ring)
5531 struct rt2860_rxdesc *desc;
5532 struct rt2860_softc_rx_data *data;
5533 bus_dma_segment_t segs[1];
5534 int i, nsegs, error;
5536 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5537 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5538 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
5539 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5540 0, NULL, NULL, &ring->desc_dma_tag);
5541 if (error != 0)
5543 printf("%s: could not create Rx desc DMA tag\n",
5544 device_get_nameunit(sc->dev));
5545 goto fail;
5548 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5549 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5550 if (error != 0)
5552 printf("%s: could not allocate Rx desc DMA memory\n",
5553 device_get_nameunit(sc->dev));
5554 goto fail;
5557 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5558 ring->desc,
5559 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5560 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5561 if (error != 0)
5563 printf("%s: could not load Rx desc DMA map\n",
5564 device_get_nameunit(sc->dev));
5565 goto fail;
5568 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5569 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5570 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
5571 &ring->data_dma_tag);
5572 if (error != 0)
5574 printf("%s: could not create Rx data DMA tag\n",
5575 device_get_nameunit(sc->dev));
5576 goto fail;
5579 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5581 desc = &ring->desc[i];
5582 data = &ring->data[i];
5584 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5585 if (error != 0)
5587 printf("%s: could not create Rx data DMA map\n",
5588 device_get_nameunit(sc->dev));
5589 goto fail;
5592 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5593 if (data->m == NULL)
5595 printf("%s: could not allocate Rx mbuf\n",
5596 device_get_nameunit(sc->dev));
5597 error = ENOMEM;
5598 goto fail;
5601 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5603 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5604 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5605 if (error != 0)
5607 printf("%s: could not load Rx mbuf DMA map\n",
5608 device_get_nameunit(sc->dev));
5609 goto fail;
5612 KASSERT(nsegs == 1, ("%s: too many DMA segments",
5613 device_get_name(sc->dev)));
5615 desc->sdp0 = htole32(segs[0].ds_addr);
5616 desc->sdl0 = htole16(MJUMPAGESIZE);
5619 error = bus_dmamap_create(ring->data_dma_tag, 0, &ring->spare_dma_map);
5620 if (error != 0)
5622 printf("%s: could not create Rx spare DMA map\n",
5623 device_get_nameunit(sc->dev));
5624 goto fail;
5627 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5628 BUS_DMASYNC_PREWRITE);
5630 return 0;
5632 fail:
5634 rt2860_free_rx_ring(sc, ring);
5636 return error;
5640 * rt2860_reset_rx_ring
5642 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5643 struct rt2860_softc_rx_ring *ring)
5645 struct rt2860_rxdesc *desc;
5646 int i;
5648 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5650 desc = &ring->desc[i];
5652 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5655 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5656 BUS_DMASYNC_PREWRITE);
5658 ring->cur = 0;
5662 * rt2860_free_rx_ring
5664 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5665 struct rt2860_softc_rx_ring *ring)
5667 struct rt2860_softc_rx_data *data;
5668 int i;
5670 if (ring->desc != NULL)
5672 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5673 BUS_DMASYNC_POSTWRITE);
5674 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5675 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5676 ring->desc_dma_map);
5679 if (ring->desc_dma_tag != NULL)
5680 bus_dma_tag_destroy(ring->desc_dma_tag);
5682 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5684 data = &ring->data[i];
5686 if (data->m != NULL)
5688 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5689 BUS_DMASYNC_POSTREAD);
5690 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5691 m_freem(data->m);
5694 if (data->dma_map != NULL)
5695 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5698 if (ring->spare_dma_map != NULL)
5699 bus_dmamap_destroy(ring->data_dma_tag, ring->spare_dma_map);
5701 if (ring->data_dma_tag != NULL)
5702 bus_dma_tag_destroy(ring->data_dma_tag);
5706 * rt2860_alloc_tx_ring
5708 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5709 struct rt2860_softc_tx_ring *ring, int qid)
5711 struct rt2860_softc_tx_data *data;
5712 int error, i;
5714 mtx_init(&ring->lock, device_get_nameunit(sc->dev), NULL, MTX_DEF);
5716 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5717 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5718 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5719 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5720 0, NULL, NULL, &ring->desc_dma_tag);
5721 if (error != 0)
5723 printf("%s: could not create Tx desc DMA tag\n",
5724 device_get_nameunit(sc->dev));
5725 goto fail;
5728 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5729 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5730 if (error != 0)
5732 printf("%s: could not allocate Tx desc DMA memory\n",
5733 device_get_nameunit(sc->dev));
5734 goto fail;
5737 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5738 ring->desc,
5739 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5740 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5741 if (error != 0)
5743 printf("%s: could not load Tx desc DMA map\n",
5744 device_get_nameunit(sc->dev));
5745 goto fail;
5748 ring->desc_queued = 0;
5749 ring->desc_cur = 0;
5750 ring->desc_next = 0;
5752 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5753 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5754 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5755 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5756 0, NULL, NULL, &ring->seg0_dma_tag);
5757 if (error != 0)
5759 printf("%s: could not create Tx seg0 DMA tag\n",
5760 device_get_nameunit(sc->dev));
5761 goto fail;
5764 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5765 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5766 if (error != 0)
5768 printf("%s: could not allocate Tx seg0 DMA memory\n",
5769 device_get_nameunit(sc->dev));
5770 goto fail;
5773 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5774 ring->seg0,
5775 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5776 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5777 if (error != 0)
5779 printf("%s: could not load Tx seg0 DMA map\n",
5780 device_get_nameunit(sc->dev));
5781 goto fail;
5784 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5785 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5786 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5787 &ring->data_dma_tag);
5788 if (error != 0)
5790 printf("%s: could not create Tx data DMA tag\n",
5791 device_get_nameunit(sc->dev));
5792 goto fail;
5795 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5797 data = &ring->data[i];
5799 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5800 if (error != 0)
5802 printf("%s: could not create Tx data DMA map\n",
5803 device_get_nameunit(sc->dev));
5804 goto fail;
5808 ring->data_queued = 0;
5809 ring->data_cur = 0;
5810 ring->data_next = 0;
5812 ring->qid = qid;
5814 return 0;
5816 fail:
5818 rt2860_free_tx_ring(sc, ring);
5820 return error;
5824 * rt2860_reset_tx_ring
5826 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5827 struct rt2860_softc_tx_ring *ring)
5829 struct rt2860_softc_tx_data *data;
5830 struct rt2860_txdesc *desc;
5831 int i;
5833 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5835 desc = &ring->desc[i];
5837 desc->sdl0 = 0;
5838 desc->sdl1 = 0;
5841 ring->desc_queued = 0;
5842 ring->desc_cur = 0;
5843 ring->desc_next = 0;
5845 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5846 BUS_DMASYNC_PREWRITE);
5848 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5849 BUS_DMASYNC_PREWRITE);
5851 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5853 data = &ring->data[i];
5855 if (data->m != NULL)
5857 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5858 BUS_DMASYNC_POSTWRITE);
5859 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5860 m_freem(data->m);
5861 data->m = NULL;
5864 if (data->ni != NULL)
5866 ieee80211_free_node(data->ni);
5867 data->ni = NULL;
5871 ring->data_queued = 0;
5872 ring->data_cur = 0;
5873 ring->data_next = 0;
5877 * rt2860_free_tx_ring
5879 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5880 struct rt2860_softc_tx_ring *ring)
5882 struct rt2860_softc_tx_data *data;
5883 int i;
5885 if (ring->desc != NULL)
5887 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5888 BUS_DMASYNC_POSTWRITE);
5889 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5890 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5891 ring->desc_dma_map);
5894 if (ring->desc_dma_tag != NULL)
5895 bus_dma_tag_destroy(ring->desc_dma_tag);
5897 if (ring->seg0 != NULL)
5899 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5900 BUS_DMASYNC_POSTWRITE);
5901 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5902 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5903 ring->seg0_dma_map);
5906 if (ring->seg0_dma_tag != NULL)
5907 bus_dma_tag_destroy(ring->seg0_dma_tag);
5909 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5911 data = &ring->data[i];
5913 if (data->m != NULL)
5915 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5916 BUS_DMASYNC_POSTWRITE);
5917 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5918 m_freem(data->m);
5921 if (data->ni != NULL)
5922 ieee80211_free_node(data->ni);
5924 if (data->dma_map != NULL)
5925 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5928 if (ring->data_dma_tag != NULL)
5929 bus_dma_tag_destroy(ring->data_dma_tag);
5931 mtx_destroy(&ring->lock);
5935 * rt2860_dma_map_addr
5937 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5938 int nseg, int error)
5940 if (error != 0)
5941 return;
5943 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5945 *(bus_addr_t *) arg = segs[0].ds_addr;
5949 * rt2860_sysctl_attach
5951 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5953 struct sysctl_ctx_list *ctx;
5954 struct sysctl_oid *tree;
5955 struct sysctl_oid *stats;
5957 ctx = device_get_sysctl_ctx(sc->dev);
5958 tree = device_get_sysctl_tree(sc->dev);
5960 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5961 "stats", CTLFLAG_RD, 0, "statistic");
5963 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5964 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5965 "all interrupts");
5967 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5968 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5969 "Tx coherent interrupts");
5971 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5972 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5973 "Rx coherent interrupts");
5975 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5976 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5977 "Tx/Rx coherent interrupts");
5979 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5980 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5981 "FIFO statistic full interrupts");
5983 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5984 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5985 "Rx interrupts");
5987 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5988 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5989 "Rx delay interrupts");
5991 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5992 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5993 "Tx MGMT interrupts");
5995 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5996 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5997 "Tx HCCA interrupts");
5999 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6000 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
6001 "Tx AC3 interrupts");
6003 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6004 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
6005 "Tx AC2 interrupts");
6007 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6008 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
6009 "Tx AC1 interrupts");
6011 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6012 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
6013 "Tx AC0 interrupts");
6015 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6016 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
6017 "Tx delay interrupts");
6019 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6020 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
6021 "Pre-TBTT interrupts");
6023 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6024 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
6025 "TBTT interrupts");
6027 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6028 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
6029 "MCU command interrupts");
6031 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6032 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
6033 "auto wakeup interrupts");
6035 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6036 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
6037 "GP timer interrupts");
6039 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6040 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
6041 "Tx MGMT descriptors queued");
6043 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6044 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
6045 "Tx MGMT data queued");
6047 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6048 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
6049 "Tx HCCA descriptors queued");
6051 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6052 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
6053 "Tx HCCA data queued");
6055 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6056 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
6057 "Tx AC3 descriptors queued");
6059 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6060 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
6061 "Tx AC3 data queued");
6063 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6064 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
6065 "Tx AC2 descriptors queued");
6067 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6068 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
6069 "Tx AC2 data queued");
6071 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6072 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
6073 "Tx AC1 descriptors queued");
6075 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6076 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
6077 "Tx AC1 data queued");
6079 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6080 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
6081 "Tx AC0 descriptors queued");
6083 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6084 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
6085 "Tx AC0 data queued");
6087 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6088 "tx_mgmt_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[5], 0,
6089 "Tx MGMT data queue full");
6091 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6092 "tx_hcca_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[4], 0,
6093 "Tx HCCA data queue full");
6095 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6096 "tx_ac3_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[3], 0,
6097 "Tx AC3 data queue full");
6099 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6100 "tx_ac2_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[2], 0,
6101 "Tx AC2 data queue full");
6103 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6104 "tx_ac1_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[1], 0,
6105 "Tx AC1 data queue full");
6107 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6108 "tx_ac0_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[0], 0,
6109 "Tx AC0 data queue full");
6111 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6112 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
6113 "Tx watchdog timeouts");
6115 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6116 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
6117 "Tx defragmented packets");
6119 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6120 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
6121 "no Tx descriptors available");
6123 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6124 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
6125 "Rx mbuf allocation errors");
6127 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6128 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
6129 "Rx mbuf DMA mapping errors");
6131 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6132 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
6133 "Tx queue 0 not empty");
6135 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6136 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
6137 "Tx queue 1 not empty");
6139 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6140 "tx_beacons", CTLFLAG_RD, &sc->tx_beacons, 0,
6141 "Tx beacons");
6143 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6144 "tx_noretryok", CTLFLAG_RD, &sc->tx_noretryok, 0,
6145 "Tx successfull without retries");
6147 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6148 "tx_retryok", CTLFLAG_RD, &sc->tx_retryok, 0,
6149 "Tx successfull with retries");
6151 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6152 "tx_failed", CTLFLAG_RD, &sc->tx_failed, 0,
6153 "Tx failed");
6155 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6156 "tx_underflows", CTLFLAG_RD, &sc->tx_underflows, 0,
6157 "Tx underflows");
6159 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6160 "tx_zerolen", CTLFLAG_RD, &sc->tx_zerolen, 0,
6161 "Tx zero length");
6163 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6164 "tx_nonagg", CTLFLAG_RD, &sc->tx_nonagg, 0,
6165 "Tx non-aggregated");
6167 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6168 "tx_agg", CTLFLAG_RD, &sc->tx_agg, 0,
6169 "Tx aggregated");
6171 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6172 "tx_ampdu", CTLFLAG_RD, &sc->tx_ampdu, 0,
6173 "Tx A-MPDU");
6175 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6176 "tx_mpdu_zero_density", CTLFLAG_RD, &sc->tx_mpdu_zero_density, 0,
6177 "Tx MPDU with zero density");
6179 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6180 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
6181 "Rx packets");
6183 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6184 "rx_ampdu", CTLFLAG_RD, &sc->rx_ampdu, 0,
6185 "Rx A-MPDU");
6187 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6188 "rx_mpdu_zero_density", CTLFLAG_RD, &sc->rx_mpdu_zero_density, 0,
6189 "Rx MPDU with zero density");
6191 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6192 "rx_amsdu", CTLFLAG_RD, &sc->rx_amsdu, 0,
6193 "Rx A-MSDU");
6195 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6196 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
6197 "Rx CRC errors");
6199 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6200 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
6201 "Rx PHY errors");
6203 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6204 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
6205 "Rx false CCAs");
6207 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6208 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
6209 "Rx PLCP errors");
6211 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6212 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
6213 "Rx duplicate packets");
6215 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6216 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
6217 "Rx FIFO overflows");
6219 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6220 "rx_cipher_no_errors", CTLFLAG_RD, &sc->rx_cipher_no_errors, 0,
6221 "Rx cipher no errors");
6223 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6224 "rx_cipher_icv_errors", CTLFLAG_RD, &sc->rx_cipher_icv_errors, 0,
6225 "Rx cipher ICV errors");
6227 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6228 "rx_cipher_mic_errors", CTLFLAG_RD, &sc->rx_cipher_mic_errors, 0,
6229 "Rx cipher MIC errors");
6231 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6232 "rx_cipher_invalid_key_errors", CTLFLAG_RD, &sc->rx_cipher_invalid_key_errors, 0,
6233 "Rx cipher invalid key errors");
6236 static device_method_t rt2860_dev_methods[] =
6238 DEVMETHOD(device_probe, rt2860_probe),
6239 DEVMETHOD(device_attach, rt2860_attach),
6240 DEVMETHOD(device_detach, rt2860_detach),
6241 DEVMETHOD(device_shutdown, rt2860_shutdown),
6242 DEVMETHOD(device_suspend, rt2860_suspend),
6243 DEVMETHOD(device_resume, rt2860_resume),
6244 { 0, 0 }
6247 static driver_t rt2860_driver =
6249 "rt2860",
6250 rt2860_dev_methods,
6251 sizeof(struct rt2860_softc)
6254 static devclass_t rt2860_dev_class;
6256 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
6258 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
6259 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);