Added more Tx/Rx statistic counters
[ralink_drivers/rt2860_fbsd72.git] / rt2860.c
blob872bf40167e0047b69e4ef54415e30fb81d5e3b9
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
63 * Data structures and types
66 struct rt2860_pci_ident
68 uint16_t vendor;
69 uint16_t device;
70 const char *name;
74 * Static function prototypes
77 static int rt2860_probe(device_t dev);
79 static int rt2860_attach(device_t dev);
81 static int rt2860_detach(device_t dev);
83 static int rt2860_shutdown(device_t dev);
85 static int rt2860_suspend(device_t dev);
87 static int rt2860_resume(device_t dev);
89 static void rt2860_init_channels(struct rt2860_softc *sc);
91 static void rt2860_init_channels_ht40(struct rt2860_softc *sc);
93 static void rt2860_init_locked(void *priv);
95 static void rt2860_init(void *priv);
97 static int rt2860_init_bbp(struct rt2860_softc *sc);
99 static void rt2860_stop_locked(void *priv);
101 static void rt2860_stop(void *priv);
103 static void rt2860_start(struct ifnet *ifp);
105 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
107 static int rt2860_reset(struct ifnet *ifp);
109 static int rt2860_newstate(struct ieee80211com *ic,
110 enum ieee80211_state nstate, int arg);
112 static void rt2860_scan_start(struct ieee80211com *ic);
114 static void rt2860_scan_end(struct ieee80211com *ic);
116 static void rt2860_set_channel(struct ieee80211com *ic);
118 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew);
120 static void rt2860_updateslot(struct ifnet *ifp);
122 static int rt2860_wme_update(struct ieee80211com *ic);
124 static void rt2860_update_beacon(struct ieee80211com *ic, int what);
126 static void rt2860_key_update_begin(struct ieee80211com *ic);
128 static void rt2860_key_update_end(struct ieee80211com *ic);
130 static int rt2860_key_set(struct ieee80211com *ic,
131 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN]);
133 static int rt2860_key_delete(struct ieee80211com *ic,
134 const struct ieee80211_key *k);
136 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
137 const struct ieee80211_bpf_params *params);
139 static int rt2860_media_change(struct ifnet *ifp);
141 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211_node_table *nt);
143 static void rt2860_recv_action(struct ieee80211_node *ni,
144 const uint8_t *frm, const uint8_t *efrm);
146 static int rt2860_send_action(struct ieee80211_node *ni,
147 int category, int action, uint16_t args[4]);
149 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
151 static void rt2860_periodic(void *arg);
153 static void rt2860_tx_watchdog(void *arg);
155 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
156 const uint8_t *bssid);
158 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
159 const uint8_t *addr);
161 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc);
163 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc);
165 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc);
167 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc);
169 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc);
171 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc);
173 static void rt2860_asic_update_txpower(struct rt2860_softc *sc);
175 static void rt2860_asic_update_promisc(struct rt2860_softc *sc);
177 static void rt2860_asic_updateprot(struct rt2860_softc *sc);
179 static void rt2860_asic_updateslot(struct rt2860_softc *sc);
181 static void rt2860_asic_wme_update(struct rt2860_softc *sc);
183 static int rt2860_asic_update_beacon(struct rt2860_softc *sc);
185 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc);
187 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi);
189 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
190 const struct rt2860_rxwi *rxwi);
192 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
193 uint8_t rssi, uint8_t rxpath);
195 static uint8_t rt2860_rate2mcs(uint8_t rate);
197 static int rt2860_ackrate(struct ieee80211com *ic, int rate);
199 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags);
201 static int rt2860_tx_frame(struct rt2860_softc *sc,
202 struct mbuf *m, struct ieee80211_node *ni, int qid);
204 static int rt2860_tx_raw(struct rt2860_softc *sc,
205 struct mbuf *m, struct ieee80211_node *ni,
206 const struct ieee80211_bpf_params *params);
208 static void rt2860_intr(void *arg);
210 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc);
212 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc);
214 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc);
216 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc);
218 static void rt2860_rx_intr(struct rt2860_softc *sc);
220 static void rt2860_rx_delay_intr(struct rt2860_softc *sc);
222 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid);
224 static void rt2860_tx_delay_intr(struct rt2860_softc *sc);
226 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc);
228 static void rt2860_tbtt_intr(struct rt2860_softc *sc);
230 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc);
232 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc);
234 static void rt2860_gp_timer_intr(struct rt2860_softc *sc);
236 static void rt2860_rx_done_task(void *context, int pending);
238 static void rt2860_tx_done_task(void *context, int pending);
240 static void rt2860_fifo_sta_full_task(void *context, int pending);
242 static void rt2860_periodic_task(void *context, int pending);
244 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit);
246 static void rt2860_tx_eof(struct rt2860_softc *sc,
247 struct rt2860_softc_tx_ring *ring);
249 static void rt2860_update_stats(struct rt2860_softc *sc);
251 static void rt2860_bbp_tuning(struct rt2860_softc *sc);
253 static void rt2860_watchdog(struct rt2860_softc *sc);
255 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc);
257 static void rt2860_update_raw_counters(struct rt2860_softc *sc);
259 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask);
261 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask);
263 static int rt2860_txrx_enable(struct rt2860_softc *sc);
265 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
266 struct rt2860_softc_rx_ring *ring);
268 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
269 struct rt2860_softc_rx_ring *ring);
271 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
272 struct rt2860_softc_rx_ring *ring);
274 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
275 struct rt2860_softc_tx_ring *ring, int qid);
277 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
278 struct rt2860_softc_tx_ring *ring);
280 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
281 struct rt2860_softc_tx_ring *ring);
283 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
284 int nseg, int error);
286 static void rt2860_sysctl_attach(struct rt2860_softc *sc);
289 * Static variables
292 static const struct rt2860_pci_ident rt2860_pci_ids[] =
294 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCI, "Ralink RT2860 PCI" },
295 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCIe, "Ralink RT2860 PCIe" },
296 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760_PCI, "Ralink RT2760 PCI" },
297 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790_PCIe, "Ralink RT2790 PCIe" },
298 { 0, 0, NULL }
301 static const struct
303 uint32_t reg;
304 uint32_t val;
305 } rt2860_def_mac[] =
307 { RT2860_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
308 { RT2860_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
309 { RT2860_REG_LEGACY_BASIC_RATE, 0x0000013f },
310 { RT2860_REG_HT_BASIC_RATE, 0x00008003 },
311 { RT2860_REG_SYS_CTRL, 0x00000000 },
312 { RT2860_REG_RX_FILTER_CFG, 0x00017f97 },
313 { RT2860_REG_BKOFF_SLOT_CFG, 0x00000209 },
314 { RT2860_REG_TX_SW_CFG0, 0x00000000 },
315 { RT2860_REG_TX_SW_CFG1, 0x00080606 },
316 { RT2860_REG_TX_LINK_CFG, 0x00001020 },
317 { RT2860_REG_TX_TIMEOUT_CFG, 0x000a2090 },
318 { RT2860_REG_MAX_LEN_CFG, (1 << 12) | RT2860_MAX_AGG_SIZE },
319 { RT2860_REG_LED_CFG, 0x7f031e46 },
320 { RT2860_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
321 { RT2860_REG_TX_RTY_CFG, 0x47d01f0f },
322 { RT2860_REG_AUTO_RSP_CFG, 0x00000013 },
323 { RT2860_REG_TX_CCK_PROT_CFG, 0x05740003 },
324 { RT2860_REG_TX_OFDM_PROT_CFG, 0x05740003 },
325 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f44084 },
326 { RT2860_REG_SCHDMA_WPDMA_GLO_CFG, 0x00000030 },
327 { RT2860_REG_TX_GF20_PROT_CFG, 0x01744004 },
328 { RT2860_REG_TX_GF40_PROT_CFG, 0x03f44084 },
329 { RT2860_REG_TX_MM20_PROT_CFG, 0x01744004 },
330 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f54084 },
331 { RT2860_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
332 { RT2860_REG_TX_RTS_CFG, 0x00092b20 },
333 { RT2860_REG_TX_EXP_ACK_TIME, 0x002400ca },
334 { RT2860_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
335 { RT2860_REG_XIFS_TIME_CFG, 0x33a41010 },
336 { RT2860_REG_PWR_PIN_CFG, 0x00000003 },
337 { RT2860_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
338 { RT2860_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
339 { RT2860_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
342 #define RT2860_DEF_MAC_SIZE (sizeof(rt2860_def_mac) / sizeof(rt2860_def_mac[0]))
344 static const struct
346 uint8_t reg;
347 uint8_t val;
348 } rt2860_def_bbp[] =
350 { 65, 0x2c },
351 { 66, 0x38 },
352 { 69, 0x12 },
353 { 70, 0x0a },
354 { 73, 0x10 },
355 { 81, 0x37 },
356 { 82, 0x62 },
357 { 83, 0x6a },
358 { 84, 0x99 },
359 { 86, 0x00 },
360 { 91, 0x04 },
361 { 92, 0x00 },
362 { 103, 0x00 },
363 { 105, 0x05 },
364 { 106, 0x35 },
367 #define RT2860_DEF_BBP_SIZE (sizeof(rt2860_def_bbp) / sizeof(rt2860_def_bbp[0]))
369 SYSCTL_NODE(_hw, OID_AUTO, rt2860, CTLFLAG_RD, 0, "RT2860 driver parameters");
371 #ifdef RT2860_DEBUG
372 static int rt2860_debug = 0;
373 SYSCTL_INT(_hw_rt2860, OID_AUTO, debug, CTLFLAG_RW, &rt2860_debug, 0, "rt2860 debug level");
374 TUNABLE_INT("hw.rt2860.debug", &rt2860_debug);
375 #endif
378 * rt2860_probe
380 static int rt2860_probe(device_t dev)
382 const struct rt2860_pci_ident *ident;
384 for (ident = rt2860_pci_ids; ident->name != NULL; ident++)
386 if (pci_get_vendor(dev) == ident->vendor &&
387 pci_get_device(dev) == ident->device)
389 device_set_desc(dev, ident->name);
390 return 0;
394 return ENXIO;
398 * rt2860_attach
400 static int rt2860_attach(device_t dev)
402 struct rt2860_softc *sc;
403 struct ifnet *ifp;
404 struct ieee80211com *ic;
405 int error, ntries, i;
407 sc = device_get_softc(dev);
409 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
411 printf("%s: chip is in D%d power mode, setting to D0\n",
412 device_get_nameunit(dev), pci_get_powerstate(dev));
413 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
416 /* enable bus-mastering */
418 pci_enable_busmaster(dev);
420 sc->dev = dev;
422 mtx_init(&sc->lock, device_get_nameunit(dev),
423 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
425 sc->mem_rid = PCIR_BAR(0);
426 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
427 &sc->mem_rid, RF_ACTIVE);
428 if (sc->mem == NULL)
430 printf("%s: could not allocate memory resource\n",
431 device_get_nameunit(dev));
432 error = ENXIO;
433 goto fail;
436 sc->bst = rman_get_bustag(sc->mem);
437 sc->bsh = rman_get_bushandle(sc->mem);
439 sc->irq_rid = 0;
440 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
441 &sc->irq_rid, RF_ACTIVE | RF_SHAREABLE);
442 if (sc->irq == NULL)
444 printf("%s: could not allocate interrupt resource\n",
445 device_get_nameunit(dev));
446 error = ENXIO;
447 goto fail;
450 #ifdef RT2860_DEBUG
451 sc->debug = rt2860_debug;
453 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
454 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
455 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2860 debug level");
456 #endif
458 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
459 "%s: attaching\n",
460 device_get_nameunit(sc->dev));
462 /* wait for NIC to initialize */
464 for (ntries = 0; ntries < 100; ntries++)
466 sc->mac_rev = rt2860_io_mac_read(sc, RT2860_REG_MAC_CSR0);
467 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
468 break;
470 DELAY(10);
473 if (ntries == 100)
475 printf("%s: timeout waiting for NIC to initialize\n",
476 device_get_nameunit(dev));
477 error = EIO;
478 goto fail;
481 rt2860_read_eeprom(sc);
483 printf("%s: MAC/BBP RT2860 (rev 0x%08x), RF %s\n",
484 device_get_nameunit(sc->dev), sc->mac_rev,
485 rt2860_rf_name(sc->rf_rev));
487 /* allocate Tx and Rx rings */
489 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
491 error = rt2860_alloc_tx_ring(sc, &sc->tx_ring[i], i);
492 if (error != 0)
494 printf("%s: could not allocate Tx ring #%d\n",
495 device_get_nameunit(sc->dev), i);
496 goto fail;
500 sc->tx_ring_mgtqid = 5;
502 error = rt2860_alloc_rx_ring(sc, &sc->rx_ring);
503 if (error != 0)
505 printf("%s: could not allocate Rx ring\n",
506 device_get_nameunit(sc->dev));
507 goto fail;
510 callout_init(&sc->periodic_ch, 0);
511 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
513 ifp = sc->ifp = if_alloc(IFT_ETHER);
514 if (ifp == NULL)
516 printf("%s: could not if_alloc()\n",
517 device_get_nameunit(sc->dev));
518 error = ENOMEM;
519 goto fail;
522 ifp->if_softc = sc;
524 if_initname(ifp, "rt2860", device_get_unit(sc->dev));
526 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
528 ifp->if_init = rt2860_init;
529 ifp->if_ioctl = rt2860_ioctl;
530 ifp->if_start = rt2860_start;
532 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
533 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
534 IFQ_SET_READY(&ifp->if_snd);
536 ic = &sc->ic;
538 ic->ic_ifp = ifp;
540 ic->ic_phytype = IEEE80211_T_HT;
541 ic->ic_opmode = IEEE80211_M_STA;
542 ic->ic_state = IEEE80211_S_INIT;
544 ic->ic_caps = IEEE80211_C_MONITOR |
545 IEEE80211_C_IBSS |
546 IEEE80211_C_AHDEMO |
547 IEEE80211_C_HOSTAP |
548 IEEE80211_C_WDS |
549 /* IEEE80211_C_BGSCAN | */
550 IEEE80211_C_TXPMGT |
551 IEEE80211_C_SHPREAMBLE |
552 IEEE80211_C_SHSLOT |
553 IEEE80211_C_TXFRAG |
554 IEEE80211_C_BURST |
555 IEEE80211_C_WME |
556 IEEE80211_C_WEP |
557 IEEE80211_C_TKIP |
558 IEEE80211_C_AES_CCM |
559 IEEE80211_C_WPA;
561 ic->ic_htcaps = IEEE80211_HTC_HT |
562 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
563 IEEE80211_HTC_AMPDU | /* A-MPDU Tx */
564 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
565 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
566 IEEE80211_HTCAP_GREENFIELD | /* HT greenfield */
567 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
568 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
569 IEEE80211_HTCAP_DSSSCCK40; /* HT 40MHz DSSS/CCK modulation */
571 /* spatial streams */
573 if (sc->nrxpath == 2)
574 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
575 else if (sc->nrxpath == 3)
576 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
577 else
578 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
580 if (sc->ntxpath > 1)
581 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
583 /* delayed BA */
585 if (sc->mac_rev != 0x28600100)
586 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
588 /* init channels */
590 ic->ic_nchans = 0;
591 ic->ic_regdomain = 0;
592 ic->ic_countrycode = CTRY_DEFAULT;
593 ic->ic_location = 0;
595 rt2860_init_channels(sc);
597 rt2860_init_channels_ht40(sc);
599 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->mac_addr);
601 ieee80211_ifattach(ic);
603 sc->newstate = ic->ic_newstate;
604 ic->ic_newstate = rt2860_newstate;
606 ic->ic_reset = rt2860_reset;
607 ic->ic_node_alloc = rt2860_node_alloc;
608 ic->ic_scan_start = rt2860_scan_start;
609 ic->ic_scan_end = rt2860_scan_end;
610 ic->ic_set_channel = rt2860_set_channel;
611 ic->ic_newassoc = rt2860_newassoc;
612 ic->ic_updateslot = rt2860_updateslot;
613 ic->ic_wme.wme_update = rt2860_wme_update;
614 ic->ic_update_beacon = rt2860_update_beacon;
615 ic->ic_crypto.cs_key_update_begin = rt2860_key_update_begin;
616 ic->ic_crypto.cs_key_update_end = rt2860_key_update_end;
617 ic->ic_crypto.cs_key_set = rt2860_key_set;
618 ic->ic_crypto.cs_key_delete = rt2860_key_delete;
619 ic->ic_raw_xmit = rt2860_raw_xmit;
621 sc->recv_action = ic->ic_recv_action;
622 ic->ic_recv_action = rt2860_recv_action;
624 sc->send_action = ic->ic_send_action;
625 ic->ic_send_action = rt2860_send_action;
627 /* override Rx A-MPDU factor */
629 ic->ic_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
630 ic->ic_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
631 ic->ic_ampdu_limit = ic->ic_ampdu_rxmax;
633 /* hardware requires padding between 802.11 frame header and body */
635 ic->ic_flags |= IEEE80211_F_WME | IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
637 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
639 ieee80211_media_init(ic, rt2860_media_change, ieee80211_media_status);
641 rt2860_amrr_init(&sc->amrr, ic,
642 sc->ntxpath,
643 RT2860_AMRR_MIN_SUCCESS_THRESHOLD,
644 RT2860_AMRR_MAX_SUCCESS_THRESHOLD,
645 500);
647 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
648 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
649 &sc->drvbpf);
651 sc->rxtap_len = sizeof(sc->rxtapu);
652 sc->rxtap.ihdr.it_len = htole16(sc->rxtap_len);
653 sc->rxtap.ihdr.it_present = htole32(RT2860_SOFTC_RX_RADIOTAP_PRESENT);
655 sc->txtap_len = sizeof(sc->txtapu);
656 sc->txtap.ihdr.it_len = htole16(sc->txtap_len);
657 sc->txtap.ihdr.it_present = htole32(RT2860_SOFTC_TX_RADIOTAP_PRESENT);
659 /* init task queue */
661 TASK_INIT(&sc->rx_done_task, 0, rt2860_rx_done_task, sc);
662 TASK_INIT(&sc->tx_done_task, 0, rt2860_tx_done_task, sc);
663 TASK_INIT(&sc->fifo_sta_full_task, 0, rt2860_fifo_sta_full_task, sc);
664 TASK_INIT(&sc->periodic_task, 0, rt2860_periodic_task, sc);
666 sc->rx_process_limit = 100;
668 sc->taskqueue = taskqueue_create("rt2860_taskq", M_NOWAIT,
669 taskqueue_thread_enqueue, &sc->taskqueue);
671 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
672 device_get_nameunit(sc->dev));
674 rt2860_sysctl_attach(sc);
676 if (bootverbose)
677 ieee80211_announce(ic);
679 /* set up interrupt */
681 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
682 NULL, rt2860_intr, sc, &sc->irqh);
683 if (error != 0)
685 printf("%s: could not set up interrupt\n",
686 device_get_nameunit(dev));
687 goto fail;
690 return 0;
692 fail:
694 /* free Tx and Rx rings */
696 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
697 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
699 rt2860_free_rx_ring(sc, &sc->rx_ring);
701 mtx_destroy(&sc->lock);
703 if (sc->mem != NULL)
704 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
706 if (sc->irq != NULL)
707 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
709 return error;
713 * rt2860_detach
715 static int rt2860_detach(device_t dev)
717 struct rt2860_softc *sc;
718 struct ieee80211com *ic;
719 struct ifnet *ifp;
720 int i;
722 sc = device_get_softc(dev);
723 ic = &sc->ic;
724 ifp = ic->ic_ifp;
726 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
727 "%s: detaching\n",
728 device_get_nameunit(sc->dev));
730 RT2860_SOFTC_LOCK(sc);
732 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
734 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
736 callout_stop(&sc->periodic_ch);
737 callout_stop(&sc->tx_watchdog_ch);
739 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
740 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
741 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
742 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
744 /* free Tx and Rx rings */
746 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
747 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
749 rt2860_free_rx_ring(sc, &sc->rx_ring);
751 RT2860_SOFTC_UNLOCK(sc);
753 bpfdetach(ifp);
755 ieee80211_ifdetach(ic);
757 if_free(ifp);
759 taskqueue_free(sc->taskqueue);
761 mtx_destroy(&sc->lock);
763 bus_generic_detach(dev);
765 bus_teardown_intr(dev, sc->irq, sc->irqh);
767 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
769 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
771 return 0;
775 * rt2860_shutdown
777 static int rt2860_shutdown(device_t dev)
779 struct rt2860_softc *sc;
781 sc = device_get_softc(dev);
783 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
784 "%s: shutting down\n",
785 device_get_nameunit(sc->dev));
787 rt2860_stop(sc);
789 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
791 return 0;
795 * rt2860_suspend
797 static int rt2860_suspend(device_t dev)
799 struct rt2860_softc *sc;
801 sc = device_get_softc(dev);
803 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
804 "%s: suspending\n",
805 device_get_nameunit(sc->dev));
807 rt2860_stop(sc);
809 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
811 return 0;
815 * rt2860_resume
817 static int rt2860_resume(device_t dev)
819 struct rt2860_softc *sc;
820 struct ifnet *ifp;
822 sc = device_get_softc(dev);
823 ifp = sc->ifp;
825 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
826 "%s: resuming\n",
827 device_get_nameunit(sc->dev));
829 if (ifp->if_flags & IFF_UP)
830 rt2860_init(sc);
832 return 0;
836 * rt2860_init_channels
838 static void rt2860_init_channels(struct rt2860_softc *sc)
840 struct ieee80211com *ic;
841 struct ieee80211_channel *c;
842 int i, flags;
844 ic = &sc->ic;
846 /* set supported channels for 2GHz band */
848 for (i = 1; i <= 14; i++)
850 c = &ic->ic_channels[ic->ic_nchans++];
851 flags = IEEE80211_CHAN_B;
853 c->ic_freq = ieee80211_ieee2mhz(i, flags);
854 c->ic_ieee = i;
855 c->ic_flags = flags;
857 c = &ic->ic_channels[ic->ic_nchans++];
858 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
860 c->ic_freq = ieee80211_ieee2mhz(i, flags);
861 c->ic_ieee = i;
862 c->ic_flags = flags;
864 c = &ic->ic_channels[ic->ic_nchans++];
865 flags = IEEE80211_CHAN_G;
867 c->ic_freq = ieee80211_ieee2mhz(i, flags);
868 c->ic_ieee = i;
869 c->ic_flags = flags;
871 c = &ic->ic_channels[ic->ic_nchans++];
872 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
874 c->ic_freq = ieee80211_ieee2mhz(i, flags);
875 c->ic_ieee = i;
876 c->ic_flags = flags;
879 /* set supported channels for 5GHz band */
881 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
882 sc->rf_rev == RT2860_EEPROM_RF_2750)
884 for (i = 36; i <= 64; i += 4)
886 c = &ic->ic_channels[ic->ic_nchans++];
887 flags = IEEE80211_CHAN_A;
889 c->ic_freq = ieee80211_ieee2mhz(i, flags);
890 c->ic_ieee = i;
891 c->ic_flags = flags;
893 c = &ic->ic_channels[ic->ic_nchans++];
894 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
896 c->ic_freq = ieee80211_ieee2mhz(i, flags);
897 c->ic_ieee = i;
898 c->ic_flags = flags;
901 for (i = 100; i <= 140; 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 = 149; i <= 165; 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;
938 * rt2860_init_channels_ht40
940 static void rt2860_init_channels_ht40(struct rt2860_softc *sc)
942 struct ieee80211com *ic;
943 struct ieee80211_channel *c, *cent, *ext;
944 int i, flags;
946 ic = &sc->ic;
948 /* set supported channels for 2GHz band */
950 for (i = 1; i <= 14; i++)
952 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
954 /* find the center channel */
956 cent = ieee80211_find_channel_byieee(ic, i,
957 flags & ~IEEE80211_CHAN_HT);
958 if (cent == NULL)
960 printf("%s: skip channel %d, could not find center channel\n",
961 device_get_nameunit(sc->dev), i);
962 continue;
965 /* find the extension channel */
967 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
968 flags & ~IEEE80211_CHAN_HT);
969 if (ext == NULL)
971 printf("%s: skip channel %d, could not find extension channel\n",
972 device_get_nameunit(sc->dev), i);
973 continue;
976 c = &ic->ic_channels[ic->ic_nchans++];
978 *c = *cent;
979 c->ic_extieee = ext->ic_ieee;
980 c->ic_flags &= ~IEEE80211_CHAN_HT;
981 c->ic_flags |= IEEE80211_CHAN_HT40U;
983 c = &ic->ic_channels[ic->ic_nchans++];
985 *c = *ext;
986 c->ic_extieee = cent->ic_ieee;
987 c->ic_flags &= ~IEEE80211_CHAN_HT;
988 c->ic_flags |= IEEE80211_CHAN_HT40D;
991 /* set supported channels for 5GHz band */
993 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
994 sc->rf_rev == RT2860_EEPROM_RF_2750)
996 for (i = 36; i <= 64; i += 4)
998 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1000 /* find the center channel */
1002 cent = ieee80211_find_channel_byieee(ic, i,
1003 flags & ~IEEE80211_CHAN_HT);
1004 if (cent == NULL)
1006 printf("%s: skip channel %d, could not find center channel\n",
1007 device_get_nameunit(sc->dev), i);
1008 continue;
1011 /* find the extension channel */
1013 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1014 flags & ~IEEE80211_CHAN_HT);
1015 if (ext == NULL)
1017 printf("%s: skip channel %d, could not find extension channel\n",
1018 device_get_nameunit(sc->dev), i);
1019 continue;
1022 c = &ic->ic_channels[ic->ic_nchans++];
1024 *c = *cent;
1025 c->ic_extieee = ext->ic_ieee;
1026 c->ic_flags &= ~IEEE80211_CHAN_HT;
1027 c->ic_flags |= IEEE80211_CHAN_HT40U;
1029 c = &ic->ic_channels[ic->ic_nchans++];
1031 *c = *ext;
1032 c->ic_extieee = cent->ic_ieee;
1033 c->ic_flags &= ~IEEE80211_CHAN_HT;
1034 c->ic_flags |= IEEE80211_CHAN_HT40D;
1037 for (i = 100; i <= 140; i += 4)
1039 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1041 /* find the center channel */
1043 cent = ieee80211_find_channel_byieee(ic, i,
1044 flags & ~IEEE80211_CHAN_HT);
1045 if (cent == NULL)
1047 printf("%s: skip channel %d, could not find center channel\n",
1048 device_get_nameunit(sc->dev), i);
1049 continue;
1052 /* find the extension channel */
1054 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1055 flags & ~IEEE80211_CHAN_HT);
1056 if (ext == NULL)
1058 printf("%s: skip channel %d, could not find extension channel\n",
1059 device_get_nameunit(sc->dev), i);
1060 continue;
1063 c = &ic->ic_channels[ic->ic_nchans++];
1065 *c = *cent;
1066 c->ic_extieee = ext->ic_ieee;
1067 c->ic_flags &= ~IEEE80211_CHAN_HT;
1068 c->ic_flags |= IEEE80211_CHAN_HT40U;
1070 c = &ic->ic_channels[ic->ic_nchans++];
1072 *c = *ext;
1073 c->ic_extieee = cent->ic_ieee;
1074 c->ic_flags &= ~IEEE80211_CHAN_HT;
1075 c->ic_flags |= IEEE80211_CHAN_HT40D;
1078 for (i = 149; i <= 165; i += 4)
1080 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1082 /* find the center channel */
1084 cent = ieee80211_find_channel_byieee(ic, i,
1085 flags & ~IEEE80211_CHAN_HT);
1086 if (cent == NULL)
1088 printf("%s: skip channel %d, could not find center channel\n",
1089 device_get_nameunit(sc->dev), i);
1090 continue;
1093 /* find the extension channel */
1095 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1096 flags & ~IEEE80211_CHAN_HT);
1097 if (ext == NULL)
1099 printf("%s: skip channel %d, could not find extension channel\n",
1100 device_get_nameunit(sc->dev), i);
1101 continue;
1104 c = &ic->ic_channels[ic->ic_nchans++];
1106 *c = *cent;
1107 c->ic_extieee = ext->ic_ieee;
1108 c->ic_flags &= ~IEEE80211_CHAN_HT;
1109 c->ic_flags |= IEEE80211_CHAN_HT40U;
1111 c = &ic->ic_channels[ic->ic_nchans++];
1113 *c = *ext;
1114 c->ic_extieee = cent->ic_ieee;
1115 c->ic_flags &= ~IEEE80211_CHAN_HT;
1116 c->ic_flags |= IEEE80211_CHAN_HT40D;
1122 * rt2860_init_locked
1124 static void rt2860_init_locked(void *priv)
1126 struct rt2860_softc *sc;
1127 struct ieee80211com *ic;
1128 struct ifnet *ifp;
1129 int error, i, ntries;
1130 uint32_t tmp, stacnt[6];
1132 sc = priv;
1133 ic = &sc->ic;
1134 ifp = ic->ic_ifp;
1136 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1137 "%s: initializing\n",
1138 device_get_nameunit(sc->dev));
1140 RT2860_SOFTC_ASSERT_LOCKED(sc);
1142 if (!(sc->flags & RT2860_SOFTC_FLAGS_UCODE_LOADED))
1144 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1145 "%s: loading 8051 microcode\n",
1146 device_get_nameunit(sc->dev));
1148 error = rt2860_io_mcu_load_ucode(sc, rt2860_ucode, sizeof(rt2860_ucode));
1149 if (error != 0)
1151 printf("%s: could not load 8051 microcode\n",
1152 device_get_nameunit(sc->dev));
1153 goto fail;
1156 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1157 "%s: 8051 microcode was successfully loaded\n",
1158 device_get_nameunit(sc->dev));
1160 sc->flags |= RT2860_SOFTC_FLAGS_UCODE_LOADED;
1163 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x2);
1165 /* disable DMA engine */
1167 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1169 tmp &= 0xff0;
1170 tmp |= RT2860_REG_TX_WB_DDONE;
1172 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1174 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, 0xffffffff);
1176 /* PBF hardware reset */
1178 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1179 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1181 /* wait while DMA engine is busy */
1183 for (ntries = 0; ntries < 100; ntries++)
1185 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1186 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1187 break;
1189 DELAY(1000);
1192 if (ntries == 100)
1194 printf("%s: timeout waiting for DMA engine\n",
1195 device_get_nameunit(sc->dev));
1196 goto fail;
1199 tmp &= 0xff0;
1200 tmp |= RT2860_REG_TX_WB_DDONE;
1202 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1204 /* reset Rx and Tx rings */
1206 tmp = RT2860_REG_RST_IDX_RX |
1207 RT2860_REG_RST_IDX_TX_MGMT |
1208 RT2860_REG_RST_IDX_TX_HCCA |
1209 RT2860_REG_RST_IDX_TX_AC3 |
1210 RT2860_REG_RST_IDX_TX_AC2 |
1211 RT2860_REG_RST_IDX_TX_AC1 |
1212 RT2860_REG_RST_IDX_TX_AC0;
1214 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, tmp);
1216 /* PBF hardware reset */
1218 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1219 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1221 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x3);
1223 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1224 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1225 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1227 /* init Tx power per rate */
1229 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
1231 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1232 continue;
1234 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i),
1235 sc->txpow_rate_20mhz[i]);
1238 for (i = 0; i < RT2860_DEF_MAC_SIZE; i++)
1239 rt2860_io_mac_write(sc, rt2860_def_mac[i].reg,
1240 rt2860_def_mac[i].val);
1242 /* wait while MAC is busy */
1244 for (ntries = 0; ntries < 100; ntries++)
1246 if (!(rt2860_io_mac_read(sc, RT2860_REG_STATUS_CFG) &
1247 (RT2860_REG_STATUS_TX_BUSY | RT2860_REG_STATUS_RX_BUSY)))
1248 break;
1250 DELAY(1000);
1253 if (ntries == 100)
1255 printf("%s: timeout waiting for MAC\n",
1256 device_get_nameunit(sc->dev));
1257 goto fail;
1260 /* clear Host to MCU mailbox */
1262 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX_BBP_AGENT, 0);
1263 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX, 0);
1265 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_BOOT,
1266 RT2860_REG_H2M_TOKEN_NO_INTR, 0);
1268 DELAY(1000);
1270 error = rt2860_init_bbp(sc);
1271 if (error != 0)
1272 goto fail;
1274 /* set up maximum buffer sizes */
1276 tmp = (1 << 12) | RT2860_MAX_AGG_SIZE;
1278 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1280 /* set mac address */
1282 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1284 rt2860_asic_set_macaddr(sc, ic->ic_myaddr);
1286 /* clear statistic registers */
1288 rt2860_io_mac_read_multi(sc, RT2860_REG_RX_STA_CNT0,
1289 stacnt, sizeof(stacnt));
1291 /* set RTS threshold */
1293 rt2860_asic_update_rtsthreshold(sc);
1295 /* set Tx power */
1297 rt2860_asic_update_txpower(sc);
1299 /* set up protection mode */
1301 rt2860_asic_updateprot(sc);
1303 /* clear key tables */
1305 rt2860_asic_clear_keytables(sc);
1307 /* clear beacon frame space (entries = 8, entry size = 512) */
1309 rt2860_io_mac_set_region_4(sc, RT2860_REG_BEACON_BASE(0), 0, 1024);
1311 taskqueue_unblock(sc->taskqueue);
1313 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
1315 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1316 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
1318 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1320 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
1321 sc->tx_ring[i].desc_phys_addr);
1322 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
1323 RT2860_SOFTC_TX_RING_DESC_COUNT);
1324 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
1327 /* init Rx ring */
1329 rt2860_reset_rx_ring(sc, &sc->rx_ring);
1331 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
1332 sc->rx_ring.desc_phys_addr);
1333 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
1334 RT2860_SOFTC_RX_RING_DATA_COUNT);
1335 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
1336 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
1338 /* wait while DMA engine is busy */
1340 for (ntries = 0; ntries < 100; ntries++)
1342 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1343 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1344 break;
1346 DELAY(1000);
1349 if (ntries == 100)
1351 printf("%s: timeout waiting for DMA engine\n",
1352 device_get_nameunit(sc->dev));
1353 goto fail;
1356 tmp &= 0xff0;
1357 tmp |= RT2860_REG_TX_WB_DDONE;
1359 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1361 /* disable interrupts mitigation */
1363 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_DELAY_INT_CFG, 0);
1365 /* send LEDs operating mode to microcontroller */
1367 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED1,
1368 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1369 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED2,
1370 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1371 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED3,
1372 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1374 /* turn radio LED on */
1376 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON);
1378 /* write vendor-specific BBP values (from EEPROM) */
1380 for (i = 0; i < RT2860_SOFTC_BBP_EEPROM_COUNT; i++)
1382 if (sc->bbp_eeprom[i].reg == 0x00 ||
1383 sc->bbp_eeprom[i].reg == 0xff)
1384 continue;
1386 rt2860_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1387 sc->bbp_eeprom[i].val);
1391 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_GPIO_CTRL_CFG);
1392 if (tmp & (1 << 2))
1394 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_SLEEP,
1395 RT2860_REG_H2M_TOKEN_RADIOOFF, 0x02ff);
1396 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_RADIOOFF);
1398 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_WAKEUP,
1399 RT2860_REG_H2M_TOKEN_WAKEUP, 0);
1400 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_WAKEUP);
1404 /* disable non-existing Rx chains */
1406 tmp = rt2860_io_bbp_read(sc, 3);
1408 tmp &= ~((1 << 4) | (1 << 3));
1410 if (sc->nrxpath == 3)
1411 tmp |= (1 << 4);
1412 else if (sc->nrxpath == 2)
1413 tmp |= (1 << 3);
1415 rt2860_io_bbp_write(sc, 3, tmp);
1417 /* disable non-existing Tx chains */
1419 tmp = rt2860_io_bbp_read(sc, 1);
1421 tmp &= ~((1 << 4) | (1 << 3));
1423 if (sc->ntxpath == 2)
1424 tmp |= (1 << 4);
1426 rt2860_io_bbp_write(sc, 1, tmp);
1428 /* set current channel */
1430 rt2860_rf_set_chan(sc, ic->ic_curchan);
1432 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1433 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1434 (48 << 16) | 96);
1436 if ((sc->mac_rev & 0xffff) != 0x0101)
1437 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1439 /* clear pending interrupts */
1441 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1443 /* enable interrupts */
1445 tmp = RT2860_REG_INT_TX_COHERENT |
1446 RT2860_REG_INT_RX_COHERENT |
1447 RT2860_REG_INT_GP_TIMER |
1448 RT2860_REG_INT_AUTO_WAKEUP |
1449 RT2860_REG_INT_FIFO_STA_FULL |
1451 RT2860_REG_INT_PRE_TBTT |
1452 RT2860_REG_INT_TBTT |
1454 RT2860_REG_INT_TXRX_COHERENT |
1455 RT2860_REG_INT_MCU_CMD |
1456 RT2860_REG_INT_TX_MGMT_DONE |
1457 RT2860_REG_INT_TX_HCCA_DONE |
1458 RT2860_REG_INT_TX_AC3_DONE |
1459 RT2860_REG_INT_TX_AC2_DONE |
1460 RT2860_REG_INT_TX_AC1_DONE |
1461 RT2860_REG_INT_TX_AC0_DONE |
1462 RT2860_REG_INT_RX_DONE;
1464 sc->intr_enable_mask = tmp;
1466 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1468 if (rt2860_txrx_enable(sc) != 0)
1469 goto fail;
1471 /* clear garbage interrupts */
1473 tmp = rt2860_io_mac_read(sc, 0x1300);
1475 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1476 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1478 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1480 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1481 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1483 else
1485 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1488 sc->periodic_round = 0;
1490 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1492 return;
1494 fail:
1496 rt2860_stop_locked(sc);
1500 * rt2860_init
1502 static void rt2860_init(void *priv)
1504 struct rt2860_softc *sc;
1506 sc = priv;
1508 RT2860_SOFTC_LOCK(sc);
1510 rt2860_init_locked(sc);
1512 RT2860_SOFTC_UNLOCK(sc);
1516 * rt2860_init_bbp
1518 static int rt2860_init_bbp(struct rt2860_softc *sc)
1520 int ntries, i;
1521 uint8_t tmp;
1523 for (ntries = 0; ntries < 20; ntries++)
1525 tmp = rt2860_io_bbp_read(sc, 0);
1526 if (tmp != 0x00 && tmp != 0xff)
1527 break;
1530 if (tmp == 0x00 || tmp == 0xff)
1532 printf("%s: timeout waiting for BBP to wakeup\n",
1533 device_get_nameunit(sc->dev));
1534 return ETIMEDOUT;
1537 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1538 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1539 rt2860_def_bbp[i].val);
1541 if ((sc->mac_rev & 0xffff) != 0x0101)
1542 rt2860_io_bbp_write(sc, 84, 0x19);
1544 return 0;
1548 * rt2860_stop_locked
1550 static void rt2860_stop_locked(void *priv)
1552 struct rt2860_softc *sc;
1553 struct ieee80211com *ic;
1554 struct ifnet *ifp;
1555 uint32_t tmp;
1557 sc = priv;
1558 ic = &sc->ic;
1559 ifp = ic->ic_ifp;
1561 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1562 "%s: stopping\n",
1563 device_get_nameunit(sc->dev));
1565 RT2860_SOFTC_ASSERT_LOCKED(sc);
1567 sc->tx_timer = 0;
1569 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1570 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1572 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1574 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1576 callout_stop(&sc->periodic_ch);
1577 callout_stop(&sc->tx_watchdog_ch);
1579 RT2860_SOFTC_UNLOCK(sc);
1581 taskqueue_block(sc->taskqueue);
1583 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1584 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1585 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1586 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1588 RT2860_SOFTC_LOCK(sc);
1590 /* clear key tables */
1592 rt2860_asic_clear_keytables(sc);
1594 /* disable interrupts */
1596 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1598 /* disable Tx/Rx */
1600 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1602 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1604 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1606 /* reset adapter */
1608 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1609 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1610 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1614 * rt2860_stop
1616 static void rt2860_stop(void *priv)
1618 struct rt2860_softc *sc;
1620 sc = priv;
1622 RT2860_SOFTC_LOCK(sc);
1624 rt2860_stop_locked(sc);
1626 RT2860_SOFTC_UNLOCK(sc);
1630 * rt2860_start
1632 static void rt2860_start(struct ifnet *ifp)
1634 struct rt2860_softc *sc;
1635 struct ieee80211com *ic;
1636 struct ieee80211_node *ni;
1637 struct ether_header *eh;
1638 struct mbuf *m;
1639 int qid;
1641 sc = ifp->if_softc;
1642 ic = &sc->ic;
1644 RT2860_SOFTC_LOCK(sc);
1646 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1648 RT2860_SOFTC_UNLOCK(sc);
1649 return;
1652 for (;;)
1654 IF_POLL(&ic->ic_mgtq, m);
1655 if (m != NULL)
1657 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1659 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1660 "%s: if_start: Tx ring with qid=%d is full\n",
1661 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
1663 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1665 sc->tx_data_queue_full[sc->tx_ring_mgtqid]++;
1667 break;
1670 IF_DEQUEUE(&ic->ic_mgtq, m);
1672 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1673 m->m_pkthdr.rcvif = NULL;
1675 if (bpf_peers_present(ic->ic_rawbpf))
1676 bpf_mtap(ic->ic_rawbpf, m);
1678 if (rt2860_tx_frame(sc, m, ni, sc->tx_ring_mgtqid) != 0)
1679 break;
1681 rt2860_drain_fifo_stats(sc);
1683 else
1685 if (ic->ic_state != IEEE80211_S_RUN)
1686 break;
1688 IF_POLL(&ifp->if_snd, m);
1689 if (m == NULL)
1690 break;
1692 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1694 if (ic->ic_flags & IEEE80211_F_SCAN)
1695 ieee80211_cancel_scan(ic);
1697 if (m->m_len < sizeof(struct ether_header) &&
1698 !(m = m_pullup(m, sizeof (struct ether_header))))
1699 continue;
1701 eh = mtod(m, struct ether_header *);
1703 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1704 if (ni == NULL)
1706 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1707 "%s: if_start: could not find Tx node\n",
1708 device_get_nameunit(sc->dev));
1710 m_freem(m);
1711 continue;
1714 ieee80211_classify(ic, m, ni);
1716 qid = M_WME_GETAC(m);
1718 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1720 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1721 "%s: if_start: Tx ring with qid=%d is full\n",
1722 device_get_nameunit(sc->dev), qid);
1724 m_freem(m);
1725 ieee80211_free_node(ni);
1727 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1728 ifp->if_oerrors++;
1730 sc->tx_data_queue_full[qid]++;
1732 break;
1735 BPF_MTAP(ifp, m);
1737 m = ieee80211_encap(ic, m, ni);
1738 if (m == NULL)
1740 ieee80211_free_node(ni);
1741 ifp->if_oerrors++;
1742 continue;
1745 if (bpf_peers_present(ic->ic_rawbpf))
1746 bpf_mtap(ic->ic_rawbpf, m);
1748 if (rt2860_tx_frame(sc, m, ni, qid) != 0)
1750 ieee80211_free_node(ni);
1752 ifp->if_oerrors++;
1754 break;
1757 rt2860_drain_fifo_stats(sc);
1760 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1762 ic->ic_lastdata = ticks;
1764 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1767 RT2860_SOFTC_UNLOCK(sc);
1771 * rt2860_ioctl
1773 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1775 struct rt2860_softc *sc;
1776 struct ieee80211com *ic;
1777 int error;
1779 sc = ifp->if_softc;
1780 ic = &sc->ic;
1782 error = 0;
1784 switch (cmd)
1786 case SIOCSIFFLAGS:
1787 RT2860_SOFTC_LOCK(sc);
1789 if (ifp->if_flags & IFF_UP)
1791 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1793 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1794 rt2860_asic_update_promisc(sc);
1796 else
1798 rt2860_init_locked(sc);
1801 else
1803 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1804 rt2860_stop_locked(sc);
1807 sc->if_flags = ifp->if_flags;
1809 RT2860_SOFTC_UNLOCK(sc);
1810 break;
1812 default:
1813 error = ieee80211_ioctl(ic, cmd, data);
1816 if (error == ENETRESET)
1818 RT2860_SOFTC_LOCK(sc);
1820 if ((ifp->if_flags & IFF_UP) &&
1821 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1822 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1824 rt2860_stop_locked(sc);
1825 rt2860_init_locked(sc);
1828 RT2860_SOFTC_UNLOCK(sc);
1830 error = 0;
1833 return error;
1837 * rt2860_reset
1839 static int rt2860_reset(struct ifnet *ifp)
1841 struct rt2860_softc *sc;
1842 struct ieee80211com *ic;
1844 sc = ifp->if_softc;
1845 ic = &sc->ic;
1847 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1848 return ENETRESET;
1850 rt2860_rf_set_chan(sc, ic->ic_curchan);
1852 return 0;
1856 * rt2860_newstate
1858 static int rt2860_newstate(struct ieee80211com *ic,
1859 enum ieee80211_state nstate, int arg)
1861 struct rt2860_softc *sc;
1862 struct ifnet *ifp;
1863 struct ieee80211_node *ni;
1864 int error;
1866 ifp = ic->ic_ifp;
1867 sc = ifp->if_softc;
1869 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1870 "%s: newstate: %s -> %s\n",
1871 device_get_nameunit(sc->dev),
1872 ieee80211_state_name[ic->ic_state], ieee80211_state_name[nstate]);
1874 error = sc->newstate(ic, nstate, arg);
1875 if (error != 0)
1876 return error;
1878 RT2860_SOFTC_LOCK(sc);
1880 /* turn link LED off */
1882 if (nstate != IEEE80211_S_RUN)
1883 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1885 switch (nstate)
1887 case IEEE80211_S_INIT:
1888 rt2860_asic_disable_tsf_sync(sc);
1889 break;
1891 case IEEE80211_S_RUN:
1892 ni = ic->ic_bss;
1894 rt2860_rf_set_chan(sc, ni->ni_chan);
1896 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1898 rt2860_asic_enable_mrr(sc);
1899 rt2860_asic_set_txpreamble(sc);
1900 rt2860_asic_set_basicrates(sc);
1901 rt2860_asic_update_txpower(sc);
1902 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1905 if (ic->ic_opmode == IEEE80211_M_STA)
1906 rt2860_newassoc(ni, 1);
1908 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1909 ic->ic_opmode == IEEE80211_M_IBSS)
1911 error = rt2860_asic_update_beacon(sc);
1912 if (error != 0)
1913 break;
1916 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1917 rt2860_asic_enable_tsf_sync(sc);
1919 /* turn link LED on */
1921 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1923 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1924 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1925 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
1927 break;
1929 default:
1930 break;
1933 RT2860_SOFTC_UNLOCK(sc);
1935 return error;
1939 * rt2860_scan_start
1941 static void rt2860_scan_start(struct ieee80211com *ic)
1943 struct rt2860_softc *sc;
1944 struct ifnet *ifp;
1946 ifp = ic->ic_ifp;
1947 sc = ifp->if_softc;
1949 rt2860_asic_disable_tsf_sync(sc);
1953 * rt2860_scan_end
1955 static void rt2860_scan_end(struct ieee80211com *ic)
1957 struct rt2860_softc *sc;
1958 struct ifnet *ifp;
1960 ifp = ic->ic_ifp;
1961 sc = ifp->if_softc;
1963 rt2860_asic_enable_tsf_sync(sc);
1967 * rt2860_set_channel
1969 static void rt2860_set_channel(struct ieee80211com *ic)
1971 struct rt2860_softc *sc;
1972 struct ifnet *ifp;
1974 ifp = ic->ic_ifp;
1975 sc = ifp->if_softc;
1977 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
1978 "%s: set channel: channel=%u, HT%s%s\n",
1979 device_get_nameunit(sc->dev),
1980 ieee80211_chan2ieee(ic, ic->ic_curchan),
1981 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
1982 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
1983 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
1984 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
1986 RT2860_SOFTC_LOCK(sc);
1988 rt2860_rf_set_chan(sc, ic->ic_curchan);
1990 RT2860_SOFTC_UNLOCK(sc);
1994 * rt2860_newassoc
1996 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
1998 struct rt2860_softc *sc;
1999 struct ieee80211com *ic;
2000 struct ifnet *ifp;
2001 uint16_t associd;
2002 uint8_t wcid;
2004 ic = ni->ni_ic;
2005 ifp = ic->ic_ifp;
2006 sc = ifp->if_softc;
2008 associd = (ni != NULL) ? ni->ni_associd : 0;
2009 wcid = RT2860_AID2WCID(associd);
2011 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
2012 "%s: new association: wcid=0x%02x, "
2013 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2014 device_get_nameunit(sc->dev), wcid,
2015 ether_sprintf(ni->ni_macaddr),
2016 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
2017 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
2018 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
2020 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
2021 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2023 rt2860_amrr_node_init(&sc->amrr, &sc->amrr_node[wcid], ni);
2025 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2026 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2027 device_get_nameunit(sc->dev),
2028 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2029 ni->ni_associd,
2030 (ni->ni_flags & IEEE80211_NODE_HT) ?
2031 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2032 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2033 (ni->ni_flags & IEEE80211_NODE_HT) ?
2034 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2035 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2037 rt2860_asic_updateprot(sc);
2038 rt2860_asic_updateslot(sc);
2039 rt2860_asic_set_txpreamble(sc);
2043 * rt2860_updateslot
2045 static void rt2860_updateslot(struct ifnet *ifp)
2047 struct rt2860_softc *sc;
2049 sc = ifp->if_softc;
2051 rt2860_asic_updateslot(sc);
2055 * rt2860_wme_update
2057 static int rt2860_wme_update(struct ieee80211com *ic)
2059 struct rt2860_softc *sc;
2060 struct ifnet *ifp;
2062 ifp = ic->ic_ifp;
2063 sc = ifp->if_softc;
2065 rt2860_asic_wme_update(sc);
2067 return 0;
2071 * rt2860_update_beacon
2073 static void rt2860_update_beacon(struct ieee80211com *ic, int what)
2075 struct rt2860_softc *sc;
2076 struct ifnet *ifp;
2078 ifp = ic->ic_ifp;
2079 sc = ifp->if_softc;
2081 rt2860_asic_update_beacon(sc);
2085 * rt2860_key_update_begin
2087 static void rt2860_key_update_begin(struct ieee80211com *ic)
2089 struct rt2860_softc *sc;
2090 struct ifnet *ifp;
2092 ifp = ic->ic_ifp;
2093 sc = ifp->if_softc;
2095 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2096 "%s: key update begin\n",
2097 device_get_nameunit(sc->dev));
2099 taskqueue_block(sc->taskqueue);
2101 IF_LOCK(&ifp->if_snd);
2105 * rt2860_key_update_end
2107 static void rt2860_key_update_end(struct ieee80211com *ic)
2109 struct rt2860_softc *sc;
2110 struct ifnet *ifp;
2112 ifp = ic->ic_ifp;
2113 sc = ifp->if_softc;
2115 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2116 "%s: key update end\n",
2117 device_get_nameunit(sc->dev));
2119 IF_UNLOCK(&ifp->if_snd);
2121 taskqueue_unblock(sc->taskqueue);
2125 * rt2860_key_set
2127 static int rt2860_key_set(struct ieee80211com *ic,
2128 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2130 struct rt2860_softc *sc;
2131 struct ifnet *ifp;
2132 struct ieee80211_node *ni;
2133 uint16_t associd, key_base, keymode_base;
2134 uint8_t mode, vapid, wcid, iv[8];
2135 uint32_t tmp;
2137 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2138 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2139 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2140 return EINVAL;
2142 ifp = ic->ic_ifp;
2143 sc = ifp->if_softc;
2145 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2146 ni = ic->ic_bss;
2147 else
2148 ni = ieee80211_find_node(&ic->ic_sta, mac);
2150 associd = (ni != NULL) ? ni->ni_associd : 0;
2152 if ((ic->ic_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2153 ieee80211_free_node(ni);
2155 switch (k->wk_cipher->ic_cipher)
2157 case IEEE80211_CIPHER_WEP:
2158 if(k->wk_keylen < 8)
2159 mode = RT2860_REG_CIPHER_MODE_WEP40;
2160 else
2161 mode = RT2860_REG_CIPHER_MODE_WEP104;
2162 break;
2164 case IEEE80211_CIPHER_TKIP:
2165 mode = RT2860_REG_CIPHER_MODE_TKIP;
2166 break;
2168 case IEEE80211_CIPHER_AES_CCM:
2169 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2170 break;
2172 default:
2173 return EINVAL;
2176 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2177 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2178 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2179 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2181 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2183 /* install pairwise key */
2185 vapid = 0;
2186 wcid = RT2860_AID2WCID(associd);
2187 key_base = RT2860_REG_PKEY(wcid);
2189 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2191 memset(iv, 0, 8);
2193 iv[3] = (k->wk_keyix << 6);
2195 else
2197 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2199 iv[0] = (k->wk_keytsc >> 8);
2200 iv[1] = ((iv[0] | 0x20) & 0x7f);
2201 iv[2] = k->wk_keytsc;
2203 else
2205 /* AES CCMP */
2207 iv[0] = k->wk_keytsc;
2208 iv[1] = k->wk_keytsc >> 8;
2209 iv[2] = 0;
2212 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2213 iv[4] = (k->wk_keytsc >> 16);
2214 iv[5] = (k->wk_keytsc >> 24);
2215 iv[6] = (k->wk_keytsc >> 32);
2216 iv[7] = (k->wk_keytsc >> 40);
2218 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2219 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2220 device_get_nameunit(sc->dev),
2221 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2224 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2226 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2228 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2230 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2232 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2233 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2235 else
2237 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2238 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2241 else
2243 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2246 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2247 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2249 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2252 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2253 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2255 /* install group key */
2257 vapid = 0;
2258 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2259 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2261 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2263 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2265 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2267 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2268 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2270 else
2272 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2273 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2276 else
2278 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2281 tmp = rt2860_io_mac_read(sc, keymode_base);
2283 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2284 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2286 rt2860_io_mac_write(sc, keymode_base, tmp);
2289 return 1;
2293 * rt2860_key_delete
2295 static int rt2860_key_delete(struct ieee80211com *ic,
2296 const struct ieee80211_key *k)
2298 struct rt2860_softc *sc;
2299 struct ieee80211_node *ni;
2300 uint16_t associd;
2301 uint8_t vapid, wcid;
2302 uint32_t tmp;
2304 sc = ic->ic_ifp->if_softc;
2305 ni = ic->ic_bss;
2306 associd = (ni != NULL) ? ni->ni_associd : 0;
2308 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2309 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2310 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2311 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2313 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2315 /* remove pairwise key */
2317 vapid = 0;
2318 wcid = RT2860_AID2WCID(associd);
2320 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2321 (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2323 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2325 else
2327 /* remove group key */
2329 vapid = 0;
2331 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2333 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2334 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2336 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2339 return 1;
2343 * rt2860_raw_xmit
2345 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2346 const struct ieee80211_bpf_params *params)
2348 return 0;
2352 * rt2860_media_change
2354 static int rt2860_media_change(struct ifnet *ifp)
2356 struct rt2860_softc *sc;
2357 int error;
2359 sc = ifp->if_softc;
2361 error = ieee80211_media_change(ifp);
2362 if (error != ENETRESET)
2363 return error;
2365 RT2860_SOFTC_LOCK(sc);
2367 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2369 rt2860_stop_locked(sc);
2370 rt2860_init_locked(sc);
2373 RT2860_SOFTC_UNLOCK(sc);
2375 return 0;
2379 * rt2860_node_alloc
2381 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211_node_table *nt)
2383 return malloc(sizeof(struct rt2860_softc_node),
2384 M_80211_NODE, M_NOWAIT | M_ZERO);
2388 * rt2860_recv_action
2390 static void rt2860_recv_action(struct ieee80211_node *ni,
2391 const uint8_t *frm, const uint8_t *efrm)
2393 struct rt2860_softc *sc;
2394 struct ieee80211com *ic;
2395 struct ifnet *ifp;
2396 const struct ieee80211_action *ia;
2397 uint16_t associd, status, baparamset;
2398 uint8_t wcid;
2399 int tid, bufsize;
2400 uint32_t tmp;
2402 ic = ni->ni_ic;
2403 ifp = ic->ic_ifp;
2404 sc = ifp->if_softc;
2406 ia = (const struct ieee80211_action *) frm;
2408 sc->recv_action(ni, frm, efrm);
2410 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2411 return;
2413 associd = (ni != NULL) ? ni->ni_associd : 0;
2414 wcid = RT2860_AID2WCID(associd);
2416 switch (ia->ia_action)
2418 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2419 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2420 baparamset = LE_READ_2(frm + 3);
2421 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2422 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2424 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2425 "%s: received ADDBA request: associd=0x%04x, tid=%d, bufsize=%d\n",
2426 device_get_nameunit(sc->dev), associd, tid, bufsize);
2428 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2430 tmp |= (0x10000 << tid);
2432 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2433 break;
2435 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2436 case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
2437 status = LE_READ_2(frm + 3);
2438 baparamset = LE_READ_2(frm + 4);
2439 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2440 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2442 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2443 "%s: received ADDBA response: associd=0x%04x, status=%d, tid=%d, bufsize=%d\n",
2444 device_get_nameunit(sc->dev), associd, status, tid, bufsize);
2446 if (status == IEEE80211_STATUS_SUCCESS)
2448 sc->tx_ampdu_sessions++;
2450 rt2860_asic_updateprot(sc);
2452 break;
2454 /* IEEE80211_ACTION_BA_DELBA */
2455 case IEEE80211_ACTION_BA_DELBA:
2456 baparamset = LE_READ_2(frm + 2);
2457 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2459 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2460 "%s: received DELBA request: associd=0x%04x, tid=%d\n",
2461 device_get_nameunit(sc->dev), associd, tid);
2463 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2465 tmp &= ~(0x10000 << tid);
2467 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2468 break;
2473 * rt2860_send_action
2475 static int rt2860_send_action(struct ieee80211_node *ni,
2476 int category, int action, uint16_t args[4])
2478 struct rt2860_softc *sc;
2479 struct ieee80211com *ic;
2480 struct ifnet *ifp;
2481 uint16_t associd, baparamset;
2482 uint8_t wcid;
2483 int ret, tid;
2484 uint32_t tmp;
2486 ic = ni->ni_ic;
2487 ifp = ic->ic_ifp;
2488 sc = ifp->if_softc;
2490 ret = sc->send_action(ni, category, action, args);
2492 if (category != IEEE80211_ACTION_CAT_BA)
2493 return ret;
2495 associd = (ni != NULL) ? ni->ni_associd : 0;
2496 wcid = RT2860_AID2WCID(associd);
2498 switch (action)
2500 /* IEEE80211_ACTION_BA_DELBA */
2501 case IEEE80211_ACTION_BA_DELBA:
2502 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) | args[1];
2504 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2506 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2507 "%s: sending DELBA request: associd=0x%04x, tid=%d\n",
2508 device_get_nameunit(sc->dev), associd, tid);
2510 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) != IEEE80211_DELBAPS_INIT)
2512 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2514 tmp &= ~(0x10000 << tid);
2516 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2518 else
2520 sc->tx_ampdu_sessions--;
2522 rt2860_asic_updateprot(sc);
2524 break;
2527 return ret;
2531 * rt2860_amrr_update_iter_func
2533 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2535 struct rt2860_softc *sc;
2536 struct ieee80211com *ic;
2537 uint8_t wcid;
2539 sc = arg;
2540 ic = &sc->ic;
2542 /* only associated stations */
2544 if (ni->ni_associd != 0)
2546 wcid = RT2860_AID2WCID(ni->ni_associd);
2548 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2549 "%s: AMRR node: wcid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2550 device_get_nameunit(sc->dev),
2551 wcid, sc->amrr_node[wcid].txcnt, sc->amrr_node[wcid].success, sc->amrr_node[wcid].retrycnt);
2553 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2555 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2556 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2557 device_get_nameunit(sc->dev),
2558 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2559 ni->ni_associd,
2560 (ni->ni_flags & IEEE80211_NODE_HT) ?
2561 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2562 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2563 (ni->ni_flags & IEEE80211_NODE_HT) ?
2564 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2565 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2570 * rt2860_periodic
2572 static void rt2860_periodic(void *arg)
2574 struct rt2860_softc *sc;
2576 sc = arg;
2578 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2579 "%s: periodic\n",
2580 device_get_nameunit(sc->dev));
2582 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2586 * rt2860_tx_watchdog
2588 static void rt2860_tx_watchdog(void *arg)
2590 struct rt2860_softc *sc;
2591 struct ifnet *ifp;
2593 sc = arg;
2594 ifp = sc->ifp;
2596 if (sc->tx_timer == 0)
2597 return;
2599 if (--sc->tx_timer == 0)
2601 printf("%s: Tx watchdog timeout: resetting\n",
2602 device_get_nameunit(sc->dev));
2604 rt2860_stop_locked(sc);
2605 rt2860_init_locked(sc);
2607 ifp->if_oerrors++;
2609 sc->tx_watchdog_timeouts++;
2612 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2616 * rt2860_asic_set_bssid
2618 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2619 const uint8_t *bssid)
2621 uint32_t tmp;
2623 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2624 "%s: set bssid: bssid=%s\n",
2625 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2627 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2629 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2631 tmp = bssid[4] | (bssid[5] << 8);
2633 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2637 * rt2860_asic_set_macaddr
2639 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2640 const uint8_t *addr)
2642 uint32_t tmp;
2644 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2646 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2648 tmp = addr[4] | (addr[5] << 8);
2650 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2654 * rt2860_asic_enable_tsf_sync
2656 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2658 struct ieee80211com *ic;
2659 uint32_t tmp;
2661 ic = &sc->ic;
2663 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2664 "%s: enabling TSF\n",
2665 device_get_nameunit(sc->dev));
2667 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2669 tmp &= ~0x1fffff;
2670 tmp |= ic->ic_bss->ni_intval * 16;
2671 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2673 if (ic->ic_opmode == IEEE80211_M_STA)
2675 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2677 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2679 tmp |= RT2860_REG_BCN_TX_ENABLE;
2680 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2682 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2684 tmp |= RT2860_REG_BCN_TX_ENABLE;
2685 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2688 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2692 * rt2860_asic_disable_tsf_sync
2694 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2696 uint32_t tmp;
2698 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2699 "%s: disabling TSF\n",
2700 device_get_nameunit(sc->dev));
2702 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2704 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2705 RT2860_REG_TSF_TIMER_ENABLE |
2706 RT2860_REG_TBTT_TIMER_ENABLE);
2708 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2709 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2711 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2715 * rt2860_asic_enable_mrr
2717 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2719 #define CCK(mcs) (mcs)
2720 #define OFDM(mcs) ((1 << 3) | (mcs))
2721 #define HT(mcs) (mcs)
2723 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2724 (OFDM(6) << 28) | /* 54 -> 48 */
2725 (OFDM(5) << 24) | /* 48 -> 36 */
2726 (OFDM(4) << 20) | /* 36 -> 24 */
2727 (OFDM(3) << 16) | /* 24 -> 18 */
2728 (OFDM(2) << 12) | /* 18 -> 12 */
2729 (OFDM(1) << 8) | /* 12 -> 9 */
2730 (OFDM(0) << 4) | /* 9 -> 6 */
2731 OFDM(0)); /* 6 -> 6 */
2733 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2734 (CCK(2) << 12) | /* 11 -> 5.5 */
2735 (CCK(1) << 8) | /* 5.5 -> 2 */
2736 (CCK(0) << 4) | /* 2 -> 1 */
2737 CCK(0)); /* 1 -> 1 */
2739 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2740 (HT(6) << 28) |
2741 (HT(5) << 24) |
2742 (HT(4) << 20) |
2743 (HT(3) << 16) |
2744 (HT(2) << 12) |
2745 (HT(1) << 8) |
2746 (HT(0) << 4) |
2747 HT(0));
2749 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2750 (HT(14) << 28) |
2751 (HT(13) << 24) |
2752 (HT(12) << 20) |
2753 (HT(11) << 16) |
2754 (HT(10) << 12) |
2755 (HT(9) << 8) |
2756 (HT(8) << 4) |
2757 HT(7));
2759 #undef HT
2760 #undef OFDM
2761 #undef CCK
2765 * rt2860_asic_set_txpreamble
2767 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2769 struct ieee80211com *ic;
2770 uint32_t tmp;
2772 ic = &sc->ic;
2774 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2775 "%s: %s short Tx preamble\n",
2776 device_get_nameunit(sc->dev),
2777 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2779 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2781 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2783 if (sc->ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2784 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2786 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2790 * rt2860_asic_set_basicrates
2792 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2794 struct ieee80211com *ic;
2796 ic = &sc->ic;
2798 if (ic->ic_curmode == IEEE80211_MODE_11B)
2799 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2800 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2801 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2802 else
2803 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2807 * rt2860_asic_update_rtsthreshold
2809 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2811 struct ieee80211com *ic;
2812 uint32_t tmp;
2813 uint16_t threshold;
2815 ic = &sc->ic;
2817 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2818 "%s: updating RTS threshold: %d\n",
2819 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
2821 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2823 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2825 threshold = (ic->ic_rtsthreshold < IEEE80211_RTS_MAX) ?
2826 ic->ic_rtsthreshold : 0x1000;
2828 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2829 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2831 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
2835 * rt2860_asic_update_txpower
2837 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
2839 struct ieee80211com *ic;
2840 uint32_t *txpow_rate;
2841 int8_t delta;
2842 uint8_t val;
2843 uint32_t tmp;
2844 int i;
2846 ic = &sc->ic;
2848 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2849 "%s: updating Tx power: %d\n",
2850 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
2852 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
2854 txpow_rate = sc->txpow_rate_20mhz;
2856 else
2858 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2859 txpow_rate = sc->txpow_rate_40mhz_2ghz;
2860 else
2861 txpow_rate = sc->txpow_rate_40mhz_5ghz;
2864 delta = 0;
2866 val = rt2860_io_bbp_read(sc, 1);
2867 val &= 0xfc;
2869 if (ic->ic_txpowlimit > 90)
2871 /* do nothing */
2873 else if (ic->ic_txpowlimit > 60)
2875 delta -= 1;
2877 else if (ic->ic_txpowlimit > 30)
2879 delta -= 3;
2881 else if (ic->ic_txpowlimit > 15)
2883 val |= 0x1;
2885 else if (ic->ic_txpowlimit > 9)
2887 val |= 0x1;
2888 delta -= 3;
2890 else
2892 val |= 0x2;
2895 rt2860_io_bbp_write(sc, 1, val);
2897 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
2899 if (txpow_rate[i] == 0xffffffff)
2900 continue;
2902 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
2904 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
2909 * rt2860_asic_update_promisc
2911 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
2913 struct ifnet *ifp;
2914 uint32_t tmp;
2916 ifp = sc->ic.ic_ifp;
2918 printf("%s: %s promiscuous mode\n",
2919 device_get_nameunit(sc->dev),
2920 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
2922 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
2924 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
2926 if (!(ifp->if_flags & IFF_PROMISC))
2927 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
2929 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
2933 * rt2860_asic_updateprot
2935 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
2937 struct ieee80211com *ic;
2938 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
2939 uint8_t htopmode;
2940 enum ieee80211_protmode htprotmode;
2942 ic = &sc->ic;
2944 /* CCK frame protection */
2946 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2947 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
2949 /* set up protection frame phy mode and rate (MCS code) */
2951 if (ic->ic_curmode == IEEE80211_MODE_11A)
2952 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2953 (0 << RT2860_REG_PROT_MCS_SHIFT);
2954 else
2955 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2956 (3 << RT2860_REG_PROT_MCS_SHIFT));
2958 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
2960 /* OFDM frame protection */
2962 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2963 RT2860_REG_TXOP_ALLOW_ALL;
2965 if (ic->ic_flags & IEEE80211_F_USEPROT)
2967 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2968 "%s: updating protection mode: b/g protection mode=%s\n",
2969 device_get_nameunit(sc->dev),
2970 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2971 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2973 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2974 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2975 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2976 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
2977 else
2978 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2980 else
2982 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2983 "%s: updating protection mode: b/g protection mode=%s\n",
2984 device_get_nameunit(sc->dev), "none");
2986 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2989 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
2991 /* HT frame protection */
2993 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
2994 htopmode = ic->ic_bss->ni_htopmode;
2995 else
2996 htopmode = ic->ic_curhtprotmode;
2998 htprotmode = ic->ic_htprotmode;
3000 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3002 if (sc->tx_ampdu_sessions > 0)
3004 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3005 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3006 device_get_nameunit(sc->dev));
3008 htopmode = IEEE80211_HTINFO_OPMODE_MIXED;
3009 htprotmode = IEEE80211_PROT_RTSCTS;
3012 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3013 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3014 device_get_nameunit(sc->dev),
3015 htopmode & IEEE80211_HTINFO_OPMODE,
3016 (htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3017 ((htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3019 switch (htopmode & IEEE80211_HTINFO_OPMODE)
3021 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3022 case IEEE80211_HTINFO_OPMODE_HT20PR:
3023 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3024 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3025 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3026 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3027 (4 << RT2860_REG_PROT_MCS_SHIFT);
3029 gf20_prot = mm20_prot;
3031 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
3032 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3033 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3035 if (htprotmode == IEEE80211_PROT_RTSCTS)
3036 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3037 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3038 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3039 else
3040 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3042 gf40_prot = mm40_prot;
3043 break;
3045 /* IEEE80211_HTINFO_OPMODE_MIXED */
3046 case IEEE80211_HTINFO_OPMODE_MIXED:
3047 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
3048 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3049 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
3051 if (ic->ic_flags & IEEE80211_F_USEPROT)
3052 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3053 (3 << RT2860_REG_PROT_MCS_SHIFT);
3054 else
3055 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3056 (4 << RT2860_REG_PROT_MCS_SHIFT);
3058 if (htprotmode == IEEE80211_PROT_RTSCTS)
3059 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3060 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3061 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
3062 else
3063 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
3065 gf20_prot = mm20_prot;
3067 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
3069 if (ic->ic_flags & IEEE80211_F_USEPROT)
3070 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3071 (3 << RT2860_REG_PROT_MCS_SHIFT);
3072 else
3073 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3074 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3076 if (htprotmode == IEEE80211_PROT_RTSCTS)
3077 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3078 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3079 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3080 else
3081 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3083 gf40_prot = mm40_prot;
3084 break;
3087 * IEEE80211_HTINFO_OPMODE_PURE
3088 * IEEE80211_HTINFO_OPMODE_PROTOPT
3090 case IEEE80211_HTINFO_OPMODE_PURE:
3091 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3092 default:
3093 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3094 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3095 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3096 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3097 (4 << RT2860_REG_PROT_MCS_SHIFT);
3099 gf20_prot = mm20_prot;
3101 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3102 RT2860_REG_TXOP_ALLOW_ALL |
3103 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3104 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3106 gf40_prot = mm40_prot;
3107 break;
3110 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3111 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3112 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3113 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3117 * rt2860_asic_updateslot
3119 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3121 struct ieee80211com *ic;
3122 uint32_t tmp;
3124 ic = &sc->ic;
3126 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3127 "%s: %s short slot time\n",
3128 device_get_nameunit(sc->dev),
3129 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3131 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3133 tmp &= ~0xff;
3134 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3136 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3140 * rt2860_asic_wme_update
3142 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3144 struct ieee80211com *ic;
3145 struct ieee80211_wme_state *wme;
3146 const struct wmeParams *wmep;
3147 int i;
3149 ic = &sc->ic;
3150 wme = &ic->ic_wme;
3151 wmep = wme->wme_chanParams.cap_wmeParams;
3153 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3154 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3155 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3156 device_get_nameunit(sc->dev),
3157 wmep[WME_AC_VO].wmep_aifsn,
3158 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3159 wmep[WME_AC_VO].wmep_txopLimit,
3160 wmep[WME_AC_VI].wmep_aifsn,
3161 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3162 wmep[WME_AC_VI].wmep_txopLimit,
3163 wmep[WME_AC_BK].wmep_aifsn,
3164 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3165 wmep[WME_AC_BK].wmep_txopLimit,
3166 wmep[WME_AC_BE].wmep_aifsn,
3167 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3168 wmep[WME_AC_BE].wmep_txopLimit);
3170 for (i = 0; i < WME_NUM_AC; i++)
3171 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3172 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3173 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3175 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3176 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3177 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3179 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3180 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3181 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3183 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3184 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3185 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3187 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3188 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3190 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3191 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3195 * rt2860_asic_update_beacon
3197 static int rt2860_asic_update_beacon(struct rt2860_softc *sc)
3199 struct ieee80211com *ic;
3200 struct mbuf *m;
3201 struct rt2860_txwi txwi;
3202 uint8_t rate, mcs;
3203 uint32_t tmp;
3205 ic = &sc->ic;
3207 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3208 if (m == NULL)
3209 return ENOMEM;
3211 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3212 mcs = rt2860_rate2mcs(rate);
3214 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3216 txwi.wcid = 0xff;
3217 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3218 RT2860_TXWI_MPDU_LEN_SHIFT);
3219 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3220 txwi.mpdu_density_flags |=
3221 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3222 txwi.bawin_size_xflags |=
3223 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3225 if (rate == 2)
3227 txwi.phymode_ifs_stbc_shortgi =
3228 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3230 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3231 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3233 else
3235 txwi.phymode_ifs_stbc_shortgi =
3236 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3239 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3240 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3242 /* disable temporarily TSF sync */
3244 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3246 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3247 RT2860_REG_TSF_TIMER_ENABLE |
3248 RT2860_REG_TBTT_TIMER_ENABLE);
3250 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3252 /* write Tx wireless info and beacon frame to on-chip memory */
3254 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3255 &txwi, sizeof(struct rt2860_txwi));
3257 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3258 mtod(m, uint8_t *), m->m_pkthdr.len);
3260 /* enable again TSF sync */
3262 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3264 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3265 RT2860_REG_TSF_TIMER_ENABLE |
3266 RT2860_REG_TBTT_TIMER_ENABLE);
3268 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3270 m_freem(m);
3272 return 0;
3276 * rt2860_asic_clear_keytables
3278 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3280 int i;
3282 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3284 for (i = 0; i < 256; i++)
3286 rt2860_io_mac_write(sc, RT2860_REG_WCID(i), 0xffffffff);
3287 rt2860_io_mac_write(sc, RT2860_REG_WCID(i) + 4, 0x0000ffff);
3290 /* clear WCID attribute table (entries = 256, entry size = 4) */
3292 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3294 /* clear IV/EIV table (entries = 256, entry size = 8) */
3296 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3298 /* clear pairwise key table (entries = 256, entry size = 32) */
3300 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3302 /* clear shared key table (entries = 32, entry size = 32) */
3304 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3306 /* clear shared key mode (entries = 32, entry size = 2) */
3308 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3312 * rt2860_rxrate
3314 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3316 uint8_t mcs, phymode;
3317 uint8_t rate;
3319 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3320 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3321 RT2860_RXWI_PHYMODE_MASK;
3323 rate = 2;
3325 switch (phymode)
3327 case RT2860_RXWI_PHYMODE_CCK:
3328 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3330 case 0: rate = 2; break; /* 1 Mbps */
3331 case 1: rate = 4; break; /* 2 MBps */
3332 case 2: rate = 11; break; /* 5.5 Mbps */
3333 case 3: rate = 22; break; /* 11 Mbps */
3335 break;
3337 case RT2860_RXWI_PHYMODE_OFDM:
3338 switch (mcs)
3340 case 0: rate = 12; break; /* 6 Mbps */
3341 case 1: rate = 18; break; /* 9 Mbps */
3342 case 2: rate = 24; break; /* 12 Mbps */
3343 case 3: rate = 36; break; /* 18 Mbps */
3344 case 4: rate = 48; break; /* 24 Mbps */
3345 case 5: rate = 72; break; /* 36 Mbps */
3346 case 6: rate = 96; break; /* 48 Mbps */
3347 case 7: rate = 108; break; /* 54 Mbps */
3349 break;
3351 case RT2860_RXWI_PHYMODE_HT_MIXED:
3352 case RT2860_RXWI_PHYMODE_HT_GF:
3353 break;
3356 return rate;
3360 * rt2860_maxrssi_rxpath
3362 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3363 const struct rt2860_rxwi *rxwi)
3365 uint8_t rxpath;
3367 rxpath = 0;
3369 if (sc->nrxpath > 1)
3370 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3371 rxpath = 1;
3373 if (sc->nrxpath > 2)
3374 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3375 rxpath = 2;
3377 return rxpath;
3381 * rt2860_rssi2dbm
3383 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3384 uint8_t rssi, uint8_t rxpath)
3386 struct ieee80211com *ic;
3387 struct ieee80211_channel *c;
3388 int chan;
3389 int8_t rssi_off, lna_gain;
3391 if (rssi == 0)
3392 return -99;
3394 ic = &sc->ic;
3395 c = ic->ic_curchan;
3396 chan = ieee80211_chan2ieee(ic, c);
3398 if (IEEE80211_IS_CHAN_5GHZ(c))
3400 rssi_off = sc->rssi_off_5ghz[rxpath];
3402 if (chan <= 64)
3403 lna_gain = sc->lna_gain[1];
3404 else if (chan <= 128)
3405 lna_gain = sc->lna_gain[2];
3406 else
3407 lna_gain = sc->lna_gain[3];
3409 else
3411 rssi_off = sc->rssi_off_2ghz[rxpath];
3412 lna_gain = sc->lna_gain[0];
3415 return (-12 - rssi_off - lna_gain - rssi);
3419 * rt2860_rate2mcs
3421 static uint8_t rt2860_rate2mcs(uint8_t rate)
3423 switch (rate)
3425 /* CCK rates */
3426 case 2: return 0;
3427 case 4: return 1;
3428 case 11: return 2;
3429 case 22: return 3;
3431 /* OFDM rates */
3432 case 12: return 0;
3433 case 18: return 1;
3434 case 24: return 2;
3435 case 36: return 3;
3436 case 48: return 4;
3437 case 72: return 5;
3438 case 96: return 6;
3439 case 108: return 7;
3442 return 0;
3446 * rt2860_ackrate
3448 static int rt2860_ackrate(struct ieee80211com *ic, int rate)
3450 switch (rate)
3452 /* CCK rates */
3454 case 2:
3455 return 2;
3457 case 4:
3458 case 11:
3459 case 22:
3460 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3462 /* OFDM rates */
3464 case 12:
3465 case 18:
3466 return 12;
3468 case 24:
3469 case 36:
3470 return 24;
3472 case 48:
3473 case 72:
3474 case 96:
3475 case 108:
3476 return 48;
3479 /* default to 1Mbps */
3480 return 2;
3484 * rt2860_txtime
3486 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags)
3488 uint16_t txtime;
3490 if (RT2860_RATE_IS_OFDM(rate))
3492 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3493 txtime = 16 + 4 + 4 * txtime + 6;
3495 else
3497 txtime = (16 * len + rate - 1) / rate;
3499 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3500 txtime += 72 + 24;
3501 else
3502 txtime += 144 + 48;
3505 return txtime;
3509 * rt2860_tx_frame
3511 static int rt2860_tx_frame(struct rt2860_softc *sc,
3512 struct mbuf *m, struct ieee80211_node *ni, int qid)
3514 struct ieee80211com *ic;
3515 struct rt2860_softc_tx_ring *ring;
3516 struct rt2860_softc_tx_data *data;
3517 struct rt2860_txdesc *desc;
3518 struct rt2860_txwi *txwi;
3519 struct ieee80211_frame *wh;
3520 struct ieee80211_tx_ampdu *tx_ampdu;
3521 struct rt2860_softc_tx_radiotap_header *tap;
3522 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3523 u_int hdrsize, hdrspace;
3524 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, mpdu_density, bawin_size, qsel;
3525 uint16_t qos, len, dmalen, mpdu_len, dur;
3526 int error, hasqos, ac, ampdu, ackrate, ndmasegs, ndescs, i, j;
3528 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3529 ("%s: Tx frame: invalid qid=%d\n",
3530 device_get_nameunit(sc->dev), qid));
3532 ic = &sc->ic;
3534 ring = &sc->tx_ring[qid];
3535 desc = &ring->desc[ring->desc_cur];
3536 data = &ring->data[ring->data_cur];
3537 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3539 wh = mtod(m, struct ieee80211_frame *);
3541 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3543 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3544 if (hasqos)
3546 if (IEEE80211_HAS_ADDR4(wh))
3547 qos = le16toh(*(const uint16_t *)
3548 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3549 else
3550 qos = le16toh(*(const uint16_t *)
3551 (((struct ieee80211_qosframe *) wh)->i_qos));
3553 else
3555 qos = 0;
3558 if (ni->ni_flags & IEEE80211_NODE_HT)
3560 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3561 rate = 0;
3562 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3563 rate = ic->ic_fixed_rate;
3564 else
3565 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
3567 else
3569 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3570 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3571 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3572 rate = ic->ic_fixed_rate;
3573 else
3574 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
3577 rate &= IEEE80211_RATE_VAL;
3579 /* fill Tx wireless info */
3581 if (ni->ni_flags & IEEE80211_NODE_HT)
3582 mcs = rate;
3583 else
3584 mcs = rt2860_rate2mcs(rate);
3586 pid = mcs;
3588 /* management frames do not need encryption */
3590 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3591 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3593 /* calculate MPDU length without padding */
3595 hdrsize = ieee80211_hdrsize(wh);
3596 hdrspace = ieee80211_hdrspace(ic, wh);
3597 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3599 memset(txwi, 0, sizeof(struct rt2860_txwi));
3601 txwi->wcid = wcid;
3603 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3604 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3605 RT2860_TXWI_MPDU_LEN_SHIFT);
3607 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
3609 shortgi = (ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
3610 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_htcap & (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40));
3612 txwi->phymode_ifs_stbc_shortgi |=
3613 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
3614 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3616 if (ni->ni_flags & IEEE80211_NODE_HT)
3618 txwi->phymode_ifs_stbc_shortgi |=
3619 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3621 else
3623 if (!RT2860_RATE_IS_OFDM(rate))
3625 txwi->phymode_ifs_stbc_shortgi |=
3626 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3628 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3629 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3631 else
3633 txwi->phymode_ifs_stbc_shortgi |=
3634 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3638 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_chw == 40))
3639 bw = RT2860_TXWI_BW_40;
3640 else
3641 bw = RT2860_TXWI_BW_20;
3643 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
3644 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3646 if (type != IEEE80211_FC0_TYPE_DATA)
3647 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3648 else
3649 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3651 /* skip ACKs for multicast frames and probe responses */
3653 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3654 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3655 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
3656 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
3658 txwi->bawin_size_xflags |=
3659 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3661 if (ni->ni_flags & IEEE80211_NODE_HT)
3663 /* preamble + plcp + signal extension + SIFS */
3665 dur = 16 + 4 + 6 + 10;
3667 else
3669 ackrate = rt2860_ackrate(ic, rate);
3671 dur = rt2860_txtime(RT2860_ACK_SIZE, ackrate, ic->ic_flags) +
3672 sc->sifs;
3675 *(uint16_t *) wh->i_dur = htole16(dur);
3678 /* check for A-MPDU */
3680 if ((qos & IEEE80211_QOS_ACKPOLICY) == IEEE80211_QOS_ACKPOLICY_BA)
3682 ac = M_WME_GETAC(m);
3683 tx_ampdu = &ni->ni_tx_ampdu[ac];
3685 mpdu_density = RT2860_MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
3686 bawin_size = 13; //tx_ampdu->txa_wnd;
3688 txwi->mpdu_density_flags |=
3689 ((mpdu_density & RT2860_TXWI_MPDU_DENSITY_MASK) << RT2860_TXWI_MPDU_DENSITY_SHIFT) |
3690 (RT2860_TXWI_FLAGS_AMPDU << RT2860_TXWI_FLAGS_SHIFT);
3692 txwi->bawin_size_xflags |=
3693 ((bawin_size & RT2860_TXWI_BAWIN_SIZE_MASK) << RT2860_TXWI_BAWIN_SIZE_SHIFT);
3695 if (IEEE80211_HAS_ADDR4(wh))
3696 ((struct ieee80211_qosframe_addr4 *) wh)->i_qos[0] &= ~IEEE80211_QOS_ACKPOLICY;
3697 else
3698 ((struct ieee80211_qosframe *) wh)->i_qos[0] &= ~IEEE80211_QOS_ACKPOLICY;
3700 ampdu = 1;
3702 else
3704 ampdu = 0;
3707 /* ask MAC to insert timestamp into probe responses */
3709 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3710 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3711 txwi->mpdu_density_flags |=
3712 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3714 if (bpf_peers_present(sc->drvbpf))
3716 tap = &sc->txtap;
3718 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3719 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3720 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3721 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3722 tap->chan_maxpow = 0;
3724 if (ni->ni_flags & IEEE80211_NODE_HT)
3725 tap->rate = mcs | IEEE80211_RATE_MCS;
3726 else
3727 tap->rate = rate;
3729 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3730 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3732 if (shortgi)
3733 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3735 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3736 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3738 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3740 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3742 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3744 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3746 else
3748 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3752 /* copy and trim 802.11 header */
3754 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3755 m_adj(m, hdrspace);
3757 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3758 dma_seg, &ndmasegs, 0);
3759 if (error != 0)
3761 /* too many fragments, linearize */
3763 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3764 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3765 device_get_nameunit(sc->dev));
3767 m = m_defrag(m, M_DONTWAIT);
3768 if (m == NULL)
3769 return ENOMEM;
3771 sc->tx_defrag_packets++;
3773 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3774 dma_seg, &ndmasegs, 0);
3775 if (error != 0)
3777 printf("%s: could not load mbuf DMA map: error=%d\n",
3778 device_get_nameunit(sc->dev), error);
3779 m_freem(m);
3780 return error;
3784 if (m->m_pkthdr.len == 0)
3785 ndmasegs = 0;
3787 /* determine how many Tx descs are required */
3789 ndescs = 1 + ndmasegs / 2;
3790 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3792 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3793 "%s: there are not enough Tx descs\n",
3794 device_get_nameunit(sc->dev));
3796 sc->no_tx_desc_avail++;
3798 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3799 m_freem(m);
3800 return EFBIG;
3803 data->m = m;
3804 data->ni = ni;
3806 /* set up Tx descs */
3808 /* first segment is Tx wireless info and 802.11 header */
3810 len = sizeof(struct rt2860_txwi) + hdrsize;
3812 /* align end on a 4-bytes boundary */
3814 dmalen = (len + 3) & ~ 3;
3816 memset((caddr_t) txwi + len, 0, dmalen - len);
3818 qsel = RT2860_TXDESC_QSEL_EDCA;
3820 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3821 desc->sdl0 = htole16(dmalen);
3822 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3824 /* set up payload segments */
3826 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3828 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3829 desc->sdl1 = htole16(dma_seg[j].ds_len);
3831 ring->desc_queued++;
3832 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3834 j++;
3836 desc = &ring->desc[ring->desc_cur];
3838 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3839 desc->sdl0 = htole16(dma_seg[j].ds_len);
3840 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3842 j++;
3845 /* finalize last payload segment */
3847 if (i > 0)
3849 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3850 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3852 else
3854 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3855 desc->sdl1 = 0;
3858 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3859 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3860 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, ampdu=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3861 device_get_nameunit(sc->dev),
3862 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3863 bw, stbc, shortgi, mcs, wcid, ampdu, dmalen, ndmasegs,
3864 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);
3866 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3867 BUS_DMASYNC_PREWRITE);
3868 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3869 BUS_DMASYNC_PREWRITE);
3870 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3871 BUS_DMASYNC_PREWRITE);
3873 ring->desc_queued++;
3874 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3876 ring->data_queued++;
3877 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3879 /* kick Tx */
3881 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3883 return 0;
3887 * rt2860_tx_raw
3889 static int rt2860_tx_raw(struct rt2860_softc *sc,
3890 struct mbuf *m, struct ieee80211_node *ni,
3891 const struct ieee80211_bpf_params *params)
3893 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3894 "%s: Tx raw\n",
3895 device_get_nameunit(sc->dev));
3897 return 0;
3901 * rt2860_intr
3903 static void rt2860_intr(void *arg)
3905 struct rt2860_softc *sc;
3906 struct ifnet *ifp;
3907 uint32_t status;
3909 sc = arg;
3910 ifp = sc->ifp;
3912 /* acknowledge interrupts */
3914 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
3915 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
3917 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3918 "%s: interrupt: status = 0x%08x\n",
3919 device_get_nameunit(sc->dev), status);
3921 if (status == 0xffffffff || /* device likely went away */
3922 status == 0) /* not for us */
3923 return;
3925 sc->interrupts++;
3927 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
3928 return;
3930 if (status & RT2860_REG_INT_TX_COHERENT)
3931 rt2860_tx_coherent_intr(sc);
3933 if (status & RT2860_REG_INT_RX_COHERENT)
3934 rt2860_rx_coherent_intr(sc);
3936 if (status & RT2860_REG_INT_TXRX_COHERENT)
3937 rt2860_txrx_coherent_intr(sc);
3939 if (status & RT2860_REG_INT_FIFO_STA_FULL)
3940 rt2860_fifo_sta_full_intr(sc);
3942 if (status & RT2860_REG_INT_TX_MGMT_DONE)
3943 rt2860_tx_intr(sc, 5);
3945 if (status & RT2860_REG_INT_RX_DONE)
3946 rt2860_rx_intr(sc);
3948 if (status & RT2860_REG_INT_RX_DELAY_DONE)
3949 rt2860_rx_delay_intr(sc);
3951 if (status & RT2860_REG_INT_TX_HCCA_DONE)
3952 rt2860_tx_intr(sc, 4);
3954 if (status & RT2860_REG_INT_TX_AC3_DONE)
3955 rt2860_tx_intr(sc, 3);
3957 if (status & RT2860_REG_INT_TX_AC2_DONE)
3958 rt2860_tx_intr(sc, 2);
3960 if (status & RT2860_REG_INT_TX_AC1_DONE)
3961 rt2860_tx_intr(sc, 1);
3963 if (status & RT2860_REG_INT_TX_AC0_DONE)
3964 rt2860_tx_intr(sc, 0);
3966 if (status & RT2860_REG_INT_TX_DELAY_DONE)
3967 rt2860_tx_delay_intr(sc);
3969 if (status & RT2860_REG_INT_PRE_TBTT)
3970 rt2860_pre_tbtt_intr(sc);
3972 if (status & RT2860_REG_INT_TBTT)
3973 rt2860_tbtt_intr(sc);
3975 if (status & RT2860_REG_INT_MCU_CMD)
3976 rt2860_mcu_cmd_intr(sc);
3978 if (status & RT2860_REG_INT_AUTO_WAKEUP)
3979 rt2860_auto_wakeup_intr(sc);
3981 if (status & RT2860_REG_INT_GP_TIMER)
3982 rt2860_gp_timer_intr(sc);
3986 * rt2860_tx_coherent_intr
3988 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
3990 uint32_t tmp;
3991 int i;
3993 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3994 "%s: Tx coherent interrupt\n",
3995 device_get_nameunit(sc->dev));
3997 sc->tx_coherent_interrupts++;
3999 /* restart DMA engine */
4001 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4003 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4004 RT2860_REG_RX_DMA_ENABLE |
4005 RT2860_REG_TX_DMA_ENABLE);
4007 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4009 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4011 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4012 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4014 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4016 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4017 sc->tx_ring[i].desc_phys_addr);
4018 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4019 RT2860_SOFTC_TX_RING_DESC_COUNT);
4020 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4023 /* init Rx ring */
4025 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4027 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4028 sc->rx_ring.desc_phys_addr);
4029 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4030 RT2860_SOFTC_RX_RING_DATA_COUNT);
4031 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4032 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4034 rt2860_txrx_enable(sc);
4038 * rt2860_rx_coherent_intr
4040 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
4042 uint32_t tmp;
4043 int i;
4045 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4046 "%s: Rx coherent interrupt\n",
4047 device_get_nameunit(sc->dev));
4049 sc->rx_coherent_interrupts++;
4051 /* restart DMA engine */
4053 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4055 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4056 RT2860_REG_RX_DMA_ENABLE |
4057 RT2860_REG_TX_DMA_ENABLE);
4059 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4061 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4063 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4064 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4066 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4068 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4069 sc->tx_ring[i].desc_phys_addr);
4070 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4071 RT2860_SOFTC_TX_RING_DESC_COUNT);
4072 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4075 /* init Rx ring */
4077 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4079 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4080 sc->rx_ring.desc_phys_addr);
4081 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4082 RT2860_SOFTC_RX_RING_DATA_COUNT);
4083 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4084 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4086 rt2860_txrx_enable(sc);
4090 * rt2860_txrx_coherent_intr
4092 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
4094 uint32_t tmp;
4095 int i;
4097 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4098 "%s: Tx/Rx coherent interrupt\n",
4099 device_get_nameunit(sc->dev));
4101 sc->txrx_coherent_interrupts++;
4103 /* restart DMA engine */
4105 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4107 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4108 RT2860_REG_RX_DMA_ENABLE |
4109 RT2860_REG_TX_DMA_ENABLE);
4111 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4113 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4115 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4116 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4118 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4120 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4121 sc->tx_ring[i].desc_phys_addr);
4122 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4123 RT2860_SOFTC_TX_RING_DESC_COUNT);
4124 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4127 /* init Rx ring */
4129 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4131 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4132 sc->rx_ring.desc_phys_addr);
4133 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4134 RT2860_SOFTC_RX_RING_DATA_COUNT);
4135 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4136 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4138 rt2860_txrx_enable(sc);
4142 * rt2860_fifo_sta_full_intr
4144 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4146 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4147 "%s: FIFO statistic full interrupt\n",
4148 device_get_nameunit(sc->dev));
4150 sc->fifo_sta_full_interrupts++;
4152 RT2860_SOFTC_LOCK(sc);
4154 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4156 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4158 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4161 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4163 RT2860_SOFTC_UNLOCK(sc);
4167 * rt2860_rx_intr
4169 static void rt2860_rx_intr(struct rt2860_softc *sc)
4171 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4172 "%s: Rx interrupt\n",
4173 device_get_nameunit(sc->dev));
4175 sc->rx_interrupts++;
4177 RT2860_SOFTC_LOCK(sc);
4179 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4181 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4183 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4186 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4188 RT2860_SOFTC_UNLOCK(sc);
4192 * rt2860_rx_delay_intr
4194 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4196 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4197 "%s: Rx delay interrupt\n",
4198 device_get_nameunit(sc->dev));
4200 sc->rx_delay_interrupts++;
4204 * rt2860_tx_intr
4206 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4208 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4209 ("%s: Tx interrupt: invalid qid=%d\n",
4210 device_get_nameunit(sc->dev), qid));
4212 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4213 "%s: Tx interrupt: qid=%d\n",
4214 device_get_nameunit(sc->dev), qid);
4216 sc->tx_interrupts[qid]++;
4218 RT2860_SOFTC_LOCK(sc);
4220 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4222 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4224 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4227 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4229 RT2860_SOFTC_UNLOCK(sc);
4233 * rt2860_tx_delay_intr
4235 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4237 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4238 "%s: Tx delay interrupt\n",
4239 device_get_nameunit(sc->dev));
4241 sc->tx_delay_interrupts++;
4245 * rt2860_pre_tbtt_intr
4247 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4249 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4250 "%s: Pre-TBTT interrupt\n",
4251 device_get_nameunit(sc->dev));
4253 sc->pre_tbtt_interrupts++;
4257 * rt2860_tbtt_intr
4259 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4261 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4262 "%s: TBTT interrupt\n",
4263 device_get_nameunit(sc->dev));
4265 sc->tbtt_interrupts++;
4269 * rt2860_mcu_cmd_intr
4271 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4273 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4274 "%s: MCU command interrupt\n",
4275 device_get_nameunit(sc->dev));
4277 sc->mcu_cmd_interrupts++;
4281 * rt2860_auto_wakeup_intr
4283 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4285 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4286 "%s: auto wakeup interrupt\n",
4287 device_get_nameunit(sc->dev));
4289 sc->auto_wakeup_interrupts++;
4293 * rt2860_gp_timer_intr
4295 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4297 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4298 "%s: GP timer interrupt\n",
4299 device_get_nameunit(sc->dev));
4301 sc->gp_timer_interrupts++;
4305 * rt2860_rx_done_task
4307 static void rt2860_rx_done_task(void *context, int pending)
4309 struct rt2860_softc *sc;
4310 struct ifnet *ifp;
4311 int again;
4313 sc = context;
4314 ifp = sc->ifp;
4316 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4317 "%s: Rx done task\n",
4318 device_get_nameunit(sc->dev));
4320 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4321 return;
4323 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4325 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4327 RT2860_SOFTC_LOCK(sc);
4329 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4331 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4332 "%s: Rx done task: scheduling again\n",
4333 device_get_nameunit(sc->dev));
4335 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4337 else
4339 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4342 RT2860_SOFTC_UNLOCK(sc);
4346 * rt2860_tx_done_task
4348 static void rt2860_tx_done_task(void *context, int pending)
4350 struct rt2860_softc *sc;
4351 struct ifnet *ifp;
4352 uint32_t intr_mask;
4353 int i;
4355 sc = context;
4356 ifp = sc->ifp;
4358 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4359 "%s: Tx done task\n",
4360 device_get_nameunit(sc->dev));
4362 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4363 return;
4365 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4367 if (sc->intr_pending_mask & (RT2860_REG_INT_TX_AC0_DONE << i))
4369 sc->intr_pending_mask &= ~(RT2860_REG_INT_TX_AC0_DONE << i);
4371 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4375 sc->tx_timer = 0;
4377 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4379 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4380 RT2860_REG_INT_TX_HCCA_DONE |
4381 RT2860_REG_INT_TX_AC3_DONE |
4382 RT2860_REG_INT_TX_AC2_DONE |
4383 RT2860_REG_INT_TX_AC1_DONE |
4384 RT2860_REG_INT_TX_AC0_DONE);
4386 RT2860_SOFTC_LOCK(sc);
4388 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4389 (sc->intr_disable_mask & intr_mask));
4391 if (sc->intr_pending_mask & intr_mask)
4393 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4394 "%s: Tx done task: scheduling again\n",
4395 device_get_nameunit(sc->dev));
4397 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4400 RT2860_SOFTC_UNLOCK(sc);
4404 * rt2860_fifo_sta_full_task
4406 static void rt2860_fifo_sta_full_task(void *context, int pending)
4408 struct rt2860_softc *sc;
4409 struct ifnet *ifp;
4411 sc = context;
4412 ifp = sc->ifp;
4414 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4415 "%s: FIFO statistic full task\n",
4416 device_get_nameunit(sc->dev));
4418 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4419 return;
4421 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4423 rt2860_drain_fifo_stats(sc);
4425 RT2860_SOFTC_LOCK(sc);
4427 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4429 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4430 "%s: FIFO statistic full task: scheduling again\n",
4431 device_get_nameunit(sc->dev));
4433 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4435 else
4437 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4440 RT2860_SOFTC_UNLOCK(sc);
4444 * rt2860_periodic_task
4446 static void rt2860_periodic_task(void *context, int pending)
4448 struct rt2860_softc *sc;
4449 struct ifnet *ifp;
4450 struct ieee80211com *ic;
4452 sc = context;
4453 ifp = sc->ifp;
4454 ic = &sc->ic;
4456 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4457 "%s: periodic task: round=%lu\n",
4458 device_get_nameunit(sc->dev), sc->periodic_round);
4460 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4461 return;
4463 RT2860_SOFTC_LOCK(sc);
4465 sc->periodic_round++;
4467 rt2860_update_stats(sc);
4469 if ((sc->periodic_round % 10) == 0)
4471 rt2860_bbp_tuning(sc);
4473 rt2860_update_raw_counters(sc);
4475 rt2860_watchdog(sc);
4477 if (ic->ic_opmode != IEEE80211_M_MONITOR && ic->ic_state == IEEE80211_S_RUN)
4479 if (ic->ic_opmode == IEEE80211_M_STA)
4480 rt2860_amrr_update_iter_func(sc, ic->ic_bss);
4481 else
4482 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, sc);
4486 RT2860_SOFTC_UNLOCK(sc);
4488 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4492 * rt2860_rx_eof
4494 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4496 struct ieee80211com *ic;
4497 struct ifnet *ifp;
4498 struct ieee80211_frame *wh;
4499 struct ieee80211_node *ni;
4500 struct rt2860_softc_node *rni;
4501 struct rt2860_softc_rx_radiotap_header *tap;
4502 struct rt2860_softc_rx_ring *ring;
4503 struct rt2860_rxdesc *desc;
4504 struct rt2860_softc_rx_data *data;
4505 struct rt2860_rxwi *rxwi;
4506 struct mbuf *m, *mnew;
4507 bus_dma_segment_t segs[1];
4508 bus_dmamap_t dma_map;
4509 uint32_t index, desc_flags;
4510 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4511 int8_t rssi_dbm;
4512 int error, nsegs, len, ampdu, amsdu, nframes, i;
4514 ic = &sc->ic;
4515 ifp = sc->ifp;
4516 ring = &sc->rx_ring;
4518 nframes = 0;
4520 while (limit != 0)
4522 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4523 if (ring->cur == index)
4524 break;
4526 desc = &ring->desc[ring->cur];
4527 desc_flags = le32toh(desc->flags);
4528 data = &ring->data[ring->cur];
4530 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4531 BUS_DMASYNC_POSTREAD);
4533 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4534 break;
4536 nframes++;
4538 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4539 if (mnew == NULL)
4541 sc->rx_mbuf_alloc_errors++;
4542 ifp->if_ierrors++;
4543 goto skip;
4546 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4548 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, ring->spare_dma_map,
4549 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4550 if (error != 0)
4552 m_freem(mnew);
4554 sc->rx_mbuf_dmamap_errors++;
4555 ifp->if_ierrors++;
4557 goto skip;
4560 KASSERT(nsegs == 1, ("%s: too many DMA segments",
4561 device_get_name(sc->dev)));
4563 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4564 BUS_DMASYNC_POSTREAD);
4565 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4567 dma_map = data->dma_map;
4568 data->dma_map = ring->spare_dma_map;
4569 ring->spare_dma_map = dma_map;
4571 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4572 BUS_DMASYNC_PREREAD);
4574 m = data->m;
4576 data->m = mnew;
4577 desc->sdp0 = htole32(segs[0].ds_addr);
4579 /* get Rx wireless info */
4581 rxwi = mtod(m, struct rt2860_rxwi *);
4582 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4583 RT2860_RXWI_SIZE_MASK;
4585 /* check for L2 padding between IEEE 802.11 frame header and body */
4587 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4589 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4590 "%s: L2 padding: len=%d\n",
4591 device_get_nameunit(sc->dev), len);
4593 len += 2;
4596 m->m_pkthdr.rcvif = ifp;
4597 m->m_data = (caddr_t) (rxwi + 1);
4598 m->m_pkthdr.len = m->m_len = len;
4600 /* check for crc errors */
4602 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4604 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4605 "%s: rxdesc: crc error\n",
4606 device_get_nameunit(sc->dev));
4608 ifp->if_ierrors++;
4610 if (!(ifp->if_flags & IFF_PROMISC))
4612 m_freem(m);
4613 goto skip;
4617 wh = (struct ieee80211_frame *) (rxwi + 1);
4619 /* check for cipher errors */
4621 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4623 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4624 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4625 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4627 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4628 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4630 m->m_flags |= M_WEP;
4632 else
4634 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4635 "%s: rxdesc: cipher error=0x%02x\n",
4636 device_get_nameunit(sc->dev), cipher_err);
4638 ifp->if_ierrors++;
4640 if (!(ifp->if_flags & IFF_PROMISC))
4642 m_free(m);
4643 goto skip;
4647 else
4649 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4651 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4652 "%s: rxdesc: not decrypted but protected flag set\n",
4653 device_get_nameunit(sc->dev));
4655 ifp->if_ierrors++;
4657 if (!(ifp->if_flags & IFF_PROMISC))
4659 m_free(m);
4660 goto skip;
4665 /* check for A-MPDU */
4667 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
4669 m->m_flags |= M_AMPDU;
4671 sc->rx_ampdu++;
4673 ampdu = 1;
4675 else
4677 ampdu = 0;
4680 /* check for A-MSDU */
4682 if (desc_flags & RT2860_RXDESC_FLAGS_AMSDU)
4684 sc->rx_amsdu++;
4686 amsdu = 1;
4688 else
4690 amsdu = 0;
4693 ant = rt2860_maxrssi_rxpath(sc, rxwi);
4694 rssi = rxwi->rssi[ant];
4695 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
4696 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4697 RT2860_RXWI_PHYMODE_MASK);
4698 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
4699 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
4700 RT2860_RXWI_SHORTGI_MASK);
4701 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
4703 if (bpf_peers_present(sc->drvbpf))
4705 tap = &sc->rxtap;
4707 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4708 tap->dbm_antsignal = rssi_dbm;
4709 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
4710 tap->antenna = ant;
4711 tap->antsignal = rssi;
4712 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4713 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4714 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4715 tap->chan_maxpow = 0;
4717 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
4718 tap->rate = mcs | IEEE80211_RATE_MCS;
4719 else
4720 tap->rate = rt2860_rxrate(rxwi);
4722 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
4723 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4725 if (shortgi)
4726 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4728 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
4731 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4732 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4733 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
4734 device_get_nameunit(sc->dev),
4735 len, phymode, bw, shortgi, mcs,
4736 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
4737 rxwi->snr[0], rxwi->snr[1],
4738 rxwi->wcid, ampdu, amsdu);
4740 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
4742 if (ni != NULL)
4744 rni = (struct rt2860_softc_node *) ni;
4746 for (i = 0; i < RT2860_SOFTC_RSSI_DBM_COUNT; i++)
4747 rni->last_rssi_dbm[i] = rt2860_rssi2dbm(sc, rxwi->rssi[i], i);
4750 ieee80211_input(ic, m, ni, rssi_dbm, RT2860_NOISE_FLOOR, 0);
4752 ieee80211_free_node(ni);
4754 skip:
4756 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
4758 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4759 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4761 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
4763 limit--;
4766 if (ring->cur == 0)
4767 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4768 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4769 else
4770 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4771 ring->cur - 1);
4773 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4774 "%s: Rx eof: nframes=%d\n",
4775 device_get_nameunit(sc->dev), nframes);
4777 sc->rx_packets += nframes;
4779 return (limit == 0);
4783 * rt2860_tx_eof
4785 static void rt2860_tx_eof(struct rt2860_softc *sc,
4786 struct rt2860_softc_tx_ring *ring)
4788 struct ifnet *ifp;
4789 struct rt2860_txdesc *desc;
4790 struct rt2860_softc_tx_data *data;
4791 uint32_t index;
4792 int ndescs, nframes;
4794 ifp = sc->ifp;
4796 ndescs = 0;
4797 nframes = 0;
4799 for (;;)
4801 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
4802 if (ring->desc_next == index)
4803 break;
4805 ndescs++;
4807 rt2860_drain_fifo_stats(sc);
4809 desc = &ring->desc[ring->desc_next];
4811 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4812 BUS_DMASYNC_POSTREAD);
4814 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
4815 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
4817 nframes++;
4819 data = &ring->data[ring->data_next];
4821 if (data->m->m_flags & M_TXCB)
4822 ieee80211_process_callback(data->ni, data->m, 0);
4824 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4825 BUS_DMASYNC_POSTWRITE);
4826 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4828 m_freem(data->m);
4830 ieee80211_free_node(data->ni);
4832 data->m = NULL;
4833 data->ni = NULL;
4835 ifp->if_opackets++;
4837 RT2860_SOFTC_LOCK(sc);
4839 ring->data_queued--;
4840 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4842 RT2860_SOFTC_UNLOCK(sc);
4845 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
4847 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4848 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4850 RT2860_SOFTC_LOCK(sc);
4852 ring->desc_queued--;
4853 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4855 RT2860_SOFTC_UNLOCK(sc);
4858 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4859 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
4860 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
4864 * rt2860_update_stats
4866 static void rt2860_update_stats(struct rt2860_softc *sc)
4868 struct ifnet *ifp;
4869 struct ieee80211com *ic;
4870 struct ieee80211_node *ni;
4871 uint32_t stacnt[3];
4872 int beacons, noretryok, retryok, failed, underflows, zerolen;
4873 uint16_t associd;
4874 uint8_t wcid;
4876 ifp = sc->ifp;
4877 ic = &sc->ic;
4879 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4880 "%s: update statistic\n",
4881 device_get_nameunit(sc->dev));
4883 rt2860_drain_fifo_stats(sc);
4885 /* read and clear Tx statistic registers */
4887 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
4888 stacnt, sizeof(stacnt));
4890 stacnt[0] = le32toh(stacnt[0]);
4891 stacnt[1] = le32toh(stacnt[1]);
4892 stacnt[2] = le32toh(stacnt[2]);
4894 beacons = stacnt[0] >> 16;
4895 noretryok = stacnt[1] & 0xffff;
4896 retryok = stacnt[1] >> 16;
4897 failed = stacnt[0] & 0xffff;
4898 underflows = stacnt[2] >> 16;
4899 zerolen = stacnt[2] & 0xffff;
4901 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4902 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
4903 device_get_nameunit(sc->dev),
4904 beacons, noretryok, retryok, failed, underflows, zerolen);
4906 ifp->if_oerrors += failed;
4908 sc->tx_beacons += beacons;
4909 sc->tx_noretryok += noretryok;
4910 sc->tx_retryok += retryok;
4911 sc->tx_failed += failed;
4912 sc->tx_underflows += underflows;
4913 sc->tx_zerolen += zerolen;
4915 if (ic->ic_opmode == IEEE80211_M_STA && ic->ic_state == IEEE80211_S_RUN)
4917 ni = ic->ic_bss;
4919 associd = (ni != NULL) ? ni->ni_associd : 0;
4920 wcid = RT2860_AID2WCID(associd);
4922 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
4923 noretryok + retryok + failed, noretryok + retryok, retryok + failed);
4928 * rt2860_bbp_tuning
4930 static void rt2860_bbp_tuning(struct rt2860_softc *sc)
4932 struct ieee80211com *ic;
4933 struct ieee80211_node *ni;
4934 int chan, group;
4935 int8_t rssi, old, new;
4937 /* RT2860C does not support BBP tuning */
4939 if (sc->mac_rev == 0x28600100)
4940 return;
4942 ic = &sc->ic;
4944 if ((ic->ic_flags & IEEE80211_F_SCAN) ||
4945 ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
4946 return;
4948 ni = ic->ic_bss;
4950 chan = ieee80211_chan2ieee(ic, ni->ni_chan);
4952 if (chan <= 14)
4953 group = 0;
4954 else if (chan <= 64)
4955 group = 1;
4956 else if (chan <= 128)
4957 group = 2;
4958 else
4959 group = 3;
4961 rssi = ieee80211_getrssi(ic);
4963 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
4965 new = 0x2e + sc->lna_gain[group];
4967 else
4969 if (!IEEE80211_IS_CHAN_HT40(ni->ni_chan))
4970 new = 0x32 + sc->lna_gain[group] * 5 / 3;
4971 else
4972 new = 0x3a + sc->lna_gain[group] * 5 / 3;
4975 /* Tune if absolute average RSSI is greater than -80 */
4977 if (rssi > -80)
4978 new += 0x10;
4980 old = rt2860_io_bbp_read(sc, 66);
4982 if (old != new)
4983 rt2860_io_bbp_write(sc, 66, new);
4987 * rt2860_watchdog
4989 static void rt2860_watchdog(struct rt2860_softc *sc)
4991 uint32_t tmp;
4992 int ntries;
4994 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4996 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
4997 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4998 device_get_nameunit(sc->dev), tmp);
5000 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
5002 sc->tx_queue_not_empty[0]++;
5004 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
5006 for (ntries = 0; ntries < 10; ntries++)
5008 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5009 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
5010 break;
5012 DELAY(1);
5015 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5018 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
5020 sc->tx_queue_not_empty[1]++;
5022 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
5024 for (ntries = 0; ntries < 10; ntries++)
5026 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5027 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
5028 break;
5030 DELAY(1);
5033 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5038 * rt2860_drain_fifo_stats
5040 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
5042 struct ifnet *ifp;
5043 uint32_t stats;
5044 uint8_t wcid, mcs, pid;
5045 int ok, agg, retrycnt;
5047 ifp = sc->ic.ic_ifp;
5049 /* drain Tx status FIFO (maxsize = 16) */
5051 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
5052 RT2860_REG_TX_STA_FIFO_VALID)
5054 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
5055 RT2860_REG_TX_STA_FIFO_WCID_MASK;
5057 /* if no ACK was requested, no feedback is available */
5059 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
5060 continue;
5062 /* update AMRR statistic */
5064 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
5065 agg = (stats & RT2860_REG_TX_STA_FIFO_AGG) ? 1 : 0;
5066 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
5067 RT2860_REG_TX_STA_FIFO_MCS_MASK;
5068 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
5069 RT2860_REG_TX_STA_FIFO_PID_MASK;
5070 retrycnt = pid - mcs;
5072 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5073 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
5074 device_get_nameunit(sc->dev),
5075 wcid, ok, agg, mcs, pid, retrycnt);
5077 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
5079 if (!ok)
5080 ifp->if_oerrors++;
5085 * rt2860_update_raw_counters
5087 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
5089 uint32_t tmp;
5091 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT);
5093 sc->tx_nonagg += tmp & 0xffff;
5094 sc->tx_agg += tmp >> 16;
5096 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT0);
5098 sc->tx_ampdu += (tmp & 0xffff) / 1 + (tmp >> 16) / 2;
5100 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT1);
5102 sc->tx_ampdu += (tmp & 0xffff) / 3 + (tmp >> 16) / 4;
5104 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT2);
5106 sc->tx_ampdu += (tmp & 0xffff) / 5 + (tmp >> 16) / 6;
5108 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT3);
5110 sc->tx_ampdu += (tmp & 0xffff) / 7 + (tmp >> 16) / 8;
5112 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT4);
5114 sc->tx_ampdu += (tmp & 0xffff) / 9 + (tmp >> 16) / 10;
5116 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT5);
5118 sc->tx_ampdu += (tmp & 0xffff) / 11 + (tmp >> 16) / 12;
5120 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT6);
5122 sc->tx_ampdu += (tmp & 0xffff) / 13 + (tmp >> 16) / 14;
5124 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT7);
5126 sc->tx_ampdu += (tmp & 0xffff) / 15 + (tmp >> 16) / 16;
5128 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
5130 sc->rx_crc_errors += tmp & 0xffff;
5131 sc->rx_phy_errors += tmp >> 16;
5133 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
5135 sc->rx_false_ccas += tmp & 0xffff;
5136 sc->rx_plcp_errors += tmp >> 16;
5138 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
5140 sc->rx_dup_packets += tmp & 0xffff;
5141 sc->rx_fifo_overflows += tmp >> 16;
5143 tmp = rt2860_io_mac_read(sc, RT2860_REG_TXRX_MPDU_DEN_CNT);
5145 sc->tx_mpdu_zero_density += tmp & 0xffff;
5146 sc->rx_mpdu_zero_density += tmp >> 16;
5150 * rt2860_intr_enable
5152 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
5154 uint32_t tmp;
5156 sc->intr_disable_mask &= ~intr_mask;
5158 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5160 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5164 * rt2860_intr_disable
5166 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
5168 uint32_t tmp;
5170 sc->intr_disable_mask |= intr_mask;
5172 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5174 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5178 * rt2860_txrx_enable
5180 static int rt2860_txrx_enable(struct rt2860_softc *sc)
5182 struct ieee80211com *ic;
5183 struct ifnet *ifp;
5184 uint32_t tmp;
5185 int ntries;
5187 ic = &sc->ic;
5188 ifp = ic->ic_ifp;
5190 /* enable Tx/Rx DMA engine */
5192 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
5194 for (ntries = 0; ntries < 200; ntries++)
5196 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
5197 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
5198 break;
5200 DELAY(1000);
5203 if (ntries == 200)
5205 printf("%s: timeout waiting for DMA engine\n",
5206 device_get_nameunit(sc->dev));
5207 return -1;
5210 DELAY(50);
5212 tmp |= RT2860_REG_TX_WB_DDONE |
5213 RT2860_REG_RX_DMA_ENABLE |
5214 RT2860_REG_TX_DMA_ENABLE |
5215 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
5217 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5219 /* set Rx filter */
5221 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
5222 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
5224 if (ic->ic_opmode != IEEE80211_M_MONITOR)
5226 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
5227 RT2860_REG_RX_FILTER_DROP_CTS |
5228 RT2860_REG_RX_FILTER_DROP_BA |
5229 RT2860_REG_RX_FILTER_DROP_ACK |
5230 RT2860_REG_RX_FILTER_DROP_VER_ERR |
5231 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
5232 RT2860_REG_RX_FILTER_DROP_CFACK |
5233 RT2860_REG_RX_FILTER_DROP_CFEND;
5235 if (ic->ic_opmode == IEEE80211_M_STA)
5236 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
5237 RT2860_REG_RX_FILTER_DROP_PSPOLL;
5239 if (!(ifp->if_flags & IFF_PROMISC))
5240 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
5243 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
5245 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
5246 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
5248 return 0;
5252 * rt2860_alloc_rx_ring
5254 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
5255 struct rt2860_softc_rx_ring *ring)
5257 struct rt2860_rxdesc *desc;
5258 struct rt2860_softc_rx_data *data;
5259 bus_dma_segment_t segs[1];
5260 int i, nsegs, error;
5262 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5263 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5264 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
5265 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5266 0, NULL, NULL, &ring->desc_dma_tag);
5267 if (error != 0)
5269 printf("%s: could not create Rx desc DMA tag\n",
5270 device_get_nameunit(sc->dev));
5271 goto fail;
5274 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5275 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5276 if (error != 0)
5278 printf("%s: could not allocate Rx desc DMA memory\n",
5279 device_get_nameunit(sc->dev));
5280 goto fail;
5283 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5284 ring->desc,
5285 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5286 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5287 if (error != 0)
5289 printf("%s: could not load Rx desc DMA map\n",
5290 device_get_nameunit(sc->dev));
5291 goto fail;
5294 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5295 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5296 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
5297 &ring->data_dma_tag);
5298 if (error != 0)
5300 printf("%s: could not create Rx data DMA tag\n",
5301 device_get_nameunit(sc->dev));
5302 goto fail;
5305 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5307 desc = &ring->desc[i];
5308 data = &ring->data[i];
5310 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5311 if (error != 0)
5313 printf("%s: could not create Rx data DMA map\n",
5314 device_get_nameunit(sc->dev));
5315 goto fail;
5318 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5319 if (data->m == NULL)
5321 printf("%s: could not allocate Rx mbuf\n",
5322 device_get_nameunit(sc->dev));
5323 error = ENOMEM;
5324 goto fail;
5327 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5329 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5330 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5331 if (error != 0)
5333 printf("%s: could not load Rx mbuf DMA map\n",
5334 device_get_nameunit(sc->dev));
5335 goto fail;
5338 KASSERT(nsegs == 1, ("%s: too many DMA segments",
5339 device_get_name(sc->dev)));
5341 desc->sdp0 = htole32(segs[0].ds_addr);
5342 desc->sdl0 = htole16(MJUMPAGESIZE);
5345 error = bus_dmamap_create(ring->data_dma_tag, 0, &ring->spare_dma_map);
5346 if (error != 0)
5348 printf("%s: could not create Rx spare DMA map\n",
5349 device_get_nameunit(sc->dev));
5350 goto fail;
5353 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5354 BUS_DMASYNC_PREWRITE);
5356 return 0;
5358 fail:
5360 rt2860_free_rx_ring(sc, ring);
5362 return error;
5366 * rt2860_reset_rx_ring
5368 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5369 struct rt2860_softc_rx_ring *ring)
5371 struct rt2860_rxdesc *desc;
5372 int i;
5374 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5376 desc = &ring->desc[i];
5378 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5381 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5382 BUS_DMASYNC_PREWRITE);
5384 ring->cur = 0;
5388 * rt2860_free_rx_ring
5390 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5391 struct rt2860_softc_rx_ring *ring)
5393 struct rt2860_softc_rx_data *data;
5394 int i;
5396 if (ring->desc != NULL)
5398 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5399 BUS_DMASYNC_POSTWRITE);
5400 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5401 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5402 ring->desc_dma_map);
5405 if (ring->desc_dma_tag != NULL)
5406 bus_dma_tag_destroy(ring->desc_dma_tag);
5408 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5410 data = &ring->data[i];
5412 if (data->m != NULL)
5414 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5415 BUS_DMASYNC_POSTREAD);
5416 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5417 m_freem(data->m);
5420 if (data->dma_map != NULL)
5421 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5424 if (ring->spare_dma_map != NULL)
5425 bus_dmamap_destroy(ring->data_dma_tag, ring->spare_dma_map);
5427 if (ring->data_dma_tag != NULL)
5428 bus_dma_tag_destroy(ring->data_dma_tag);
5432 * rt2860_alloc_tx_ring
5434 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5435 struct rt2860_softc_tx_ring *ring, int qid)
5437 struct rt2860_softc_tx_data *data;
5438 int error, i;
5440 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5441 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5442 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5443 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5444 0, NULL, NULL, &ring->desc_dma_tag);
5445 if (error != 0)
5447 printf("%s: could not create Tx desc DMA tag\n",
5448 device_get_nameunit(sc->dev));
5449 goto fail;
5452 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5453 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5454 if (error != 0)
5456 printf("%s: could not allocate Tx desc DMA memory\n",
5457 device_get_nameunit(sc->dev));
5458 goto fail;
5461 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5462 ring->desc,
5463 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5464 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5465 if (error != 0)
5467 printf("%s: could not load Tx desc DMA map\n",
5468 device_get_nameunit(sc->dev));
5469 goto fail;
5472 ring->desc_queued = 0;
5473 ring->desc_cur = 0;
5474 ring->desc_next = 0;
5476 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5477 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5478 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5479 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5480 0, NULL, NULL, &ring->seg0_dma_tag);
5481 if (error != 0)
5483 printf("%s: could not create Tx seg0 DMA tag\n",
5484 device_get_nameunit(sc->dev));
5485 goto fail;
5488 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5489 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5490 if (error != 0)
5492 printf("%s: could not allocate Tx seg0 DMA memory\n",
5493 device_get_nameunit(sc->dev));
5494 goto fail;
5497 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5498 ring->seg0,
5499 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5500 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5501 if (error != 0)
5503 printf("%s: could not load Tx seg0 DMA map\n",
5504 device_get_nameunit(sc->dev));
5505 goto fail;
5508 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
5509 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5510 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5511 &ring->data_dma_tag);
5512 if (error != 0)
5514 printf("%s: could not create Tx data DMA tag\n",
5515 device_get_nameunit(sc->dev));
5516 goto fail;
5519 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5521 data = &ring->data[i];
5523 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5524 if (error != 0)
5526 printf("%s: could not create Tx data DMA map\n",
5527 device_get_nameunit(sc->dev));
5528 goto fail;
5532 ring->data_queued = 0;
5533 ring->data_cur = 0;
5534 ring->data_next = 0;
5536 ring->qid = qid;
5538 return 0;
5540 fail:
5542 rt2860_free_tx_ring(sc, ring);
5544 return error;
5548 * rt2860_reset_tx_ring
5550 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5551 struct rt2860_softc_tx_ring *ring)
5553 struct rt2860_softc_tx_data *data;
5554 struct rt2860_txdesc *desc;
5555 int i;
5557 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5559 desc = &ring->desc[i];
5561 desc->sdl0 = 0;
5562 desc->sdl1 = 0;
5565 ring->desc_queued = 0;
5566 ring->desc_cur = 0;
5567 ring->desc_next = 0;
5569 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5570 BUS_DMASYNC_PREWRITE);
5572 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5573 BUS_DMASYNC_PREWRITE);
5575 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5577 data = &ring->data[i];
5579 if (data->m != NULL)
5581 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5582 BUS_DMASYNC_POSTWRITE);
5583 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5584 m_freem(data->m);
5585 data->m = NULL;
5588 if (data->ni != NULL)
5590 ieee80211_free_node(data->ni);
5591 data->ni = NULL;
5595 ring->data_queued = 0;
5596 ring->data_cur = 0;
5597 ring->data_next = 0;
5601 * rt2860_free_tx_ring
5603 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5604 struct rt2860_softc_tx_ring *ring)
5606 struct rt2860_softc_tx_data *data;
5607 int i;
5609 if (ring->desc != NULL)
5611 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5612 BUS_DMASYNC_POSTWRITE);
5613 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5614 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5615 ring->desc_dma_map);
5618 if (ring->desc_dma_tag != NULL)
5619 bus_dma_tag_destroy(ring->desc_dma_tag);
5621 if (ring->seg0 != NULL)
5623 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5624 BUS_DMASYNC_POSTWRITE);
5625 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5626 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5627 ring->seg0_dma_map);
5630 if (ring->seg0_dma_tag != NULL)
5631 bus_dma_tag_destroy(ring->seg0_dma_tag);
5633 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5635 data = &ring->data[i];
5637 if (data->m != NULL)
5639 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5640 BUS_DMASYNC_POSTWRITE);
5641 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5642 m_freem(data->m);
5645 if (data->ni != NULL)
5646 ieee80211_free_node(data->ni);
5648 if (data->dma_map != NULL)
5649 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5652 if (ring->data_dma_tag != NULL)
5653 bus_dma_tag_destroy(ring->data_dma_tag);
5657 * rt2860_dma_map_addr
5659 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5660 int nseg, int error)
5662 if (error != 0)
5663 return;
5665 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5667 *(bus_addr_t *) arg = segs[0].ds_addr;
5671 * rt2860_sysctl_attach
5673 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5675 struct sysctl_ctx_list *ctx;
5676 struct sysctl_oid *tree;
5677 struct sysctl_oid *stats;
5679 ctx = device_get_sysctl_ctx(sc->dev);
5680 tree = device_get_sysctl_tree(sc->dev);
5682 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5683 "stats", CTLFLAG_RD, 0, "statistic");
5685 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5686 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5687 "all interrupts");
5689 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5690 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5691 "Tx coherent interrupts");
5693 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5694 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5695 "Rx coherent interrupts");
5697 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5698 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5699 "Tx/Rx coherent interrupts");
5701 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5702 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5703 "FIFO statistic full interrupts");
5705 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5706 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5707 "Rx interrupts");
5709 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5710 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5711 "Rx delay interrupts");
5713 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5714 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5715 "Tx MGMT interrupts");
5717 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5718 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5719 "Tx HCCA interrupts");
5721 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5722 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5723 "Tx AC3 interrupts");
5725 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5726 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5727 "Tx AC2 interrupts");
5729 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5730 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5731 "Tx AC1 interrupts");
5733 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5734 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5735 "Tx AC0 interrupts");
5737 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5738 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
5739 "Tx delay interrupts");
5741 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5742 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
5743 "Pre-TBTT interrupts");
5745 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5746 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
5747 "TBTT interrupts");
5749 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5750 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
5751 "MCU command interrupts");
5753 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5754 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
5755 "auto wakeup interrupts");
5757 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5758 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
5759 "GP timer interrupts");
5761 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5762 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
5763 "Tx MGMT descriptors queued");
5765 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5766 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
5767 "Tx MGMT data queued");
5769 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5770 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
5771 "Tx HCCA descriptors queued");
5773 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5774 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
5775 "Tx HCCA data queued");
5777 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5778 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
5779 "Tx AC3 descriptors queued");
5781 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5782 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
5783 "Tx AC3 data queued");
5785 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5786 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
5787 "Tx AC2 descriptors queued");
5789 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5790 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
5791 "Tx AC2 data queued");
5793 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5794 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
5795 "Tx AC1 descriptors queued");
5797 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5798 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
5799 "Tx AC1 data queued");
5801 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5802 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
5803 "Tx AC0 descriptors queued");
5805 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5806 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
5807 "Tx AC0 data queued");
5809 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5810 "tx_mgmt_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[5], 0,
5811 "Tx MGMT data queue full");
5813 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5814 "tx_hcca_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[4], 0,
5815 "Tx HCCA data queue full");
5817 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5818 "tx_ac3_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[3], 0,
5819 "Tx AC3 data queue full");
5821 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5822 "tx_ac2_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[2], 0,
5823 "Tx AC2 data queue full");
5825 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5826 "tx_ac1_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[1], 0,
5827 "Tx AC1 data queue full");
5829 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5830 "tx_ac0_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[0], 0,
5831 "Tx AC0 data queue full");
5833 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5834 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5835 "Tx watchdog timeouts");
5837 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5838 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
5839 "Tx defragmented packets");
5841 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5842 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
5843 "no Tx descriptors available");
5845 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5846 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
5847 "Rx mbuf allocation errors");
5849 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5850 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
5851 "Rx mbuf DMA mapping errors");
5853 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5854 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5855 "Tx queue 0 not empty");
5857 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5858 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5859 "Tx queue 1 not empty");
5861 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5862 "tx_beacons", CTLFLAG_RD, &sc->tx_beacons, 0,
5863 "Tx beacons");
5865 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5866 "tx_noretryok", CTLFLAG_RD, &sc->tx_noretryok, 0,
5867 "Tx successfull without retries");
5869 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5870 "tx_retryok", CTLFLAG_RD, &sc->tx_retryok, 0,
5871 "Tx successfull with retries");
5873 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5874 "tx_failed", CTLFLAG_RD, &sc->tx_failed, 0,
5875 "Tx failed");
5877 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5878 "tx_underflows", CTLFLAG_RD, &sc->tx_underflows, 0,
5879 "Tx underflows");
5881 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5882 "tx_zerolen", CTLFLAG_RD, &sc->tx_zerolen, 0,
5883 "Tx zero length");
5885 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5886 "tx_nonagg", CTLFLAG_RD, &sc->tx_nonagg, 0,
5887 "Tx non-aggregated");
5889 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5890 "tx_agg", CTLFLAG_RD, &sc->tx_agg, 0,
5891 "Tx aggregated");
5893 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5894 "tx_ampdu", CTLFLAG_RD, &sc->tx_ampdu, 0,
5895 "Tx A-MPDU");
5897 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5898 "tx_mpdu_zero_density", CTLFLAG_RD, &sc->tx_mpdu_zero_density, 0,
5899 "Tx MPDU with zero density");
5901 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5902 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
5903 "Rx packets");
5905 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5906 "rx_ampdu", CTLFLAG_RD, &sc->rx_ampdu, 0,
5907 "Rx A-MPDU");
5909 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5910 "rx_mpdu_zero_density", CTLFLAG_RD, &sc->rx_mpdu_zero_density, 0,
5911 "Rx MPDU with zero density");
5913 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5914 "rx_amsdu", CTLFLAG_RD, &sc->rx_amsdu, 0,
5915 "Rx A-MSDU");
5917 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5918 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
5919 "Rx CRC errors");
5921 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5922 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
5923 "Rx PHY errors");
5925 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5926 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
5927 "Rx false CCAs");
5929 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5930 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
5931 "Rx PLCP errors");
5933 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5934 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
5935 "Rx duplicate packets");
5937 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5938 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
5939 "Rx FIFO overflows");
5942 static device_method_t rt2860_dev_methods[] =
5944 DEVMETHOD(device_probe, rt2860_probe),
5945 DEVMETHOD(device_attach, rt2860_attach),
5946 DEVMETHOD(device_detach, rt2860_detach),
5947 DEVMETHOD(device_shutdown, rt2860_shutdown),
5948 DEVMETHOD(device_suspend, rt2860_suspend),
5949 DEVMETHOD(device_resume, rt2860_resume),
5950 { 0, 0 }
5953 static driver_t rt2860_driver =
5955 "rt2860",
5956 rt2860_dev_methods,
5957 sizeof(struct rt2860_softc)
5960 static devclass_t rt2860_dev_class;
5962 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
5964 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
5965 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);