Added frame length to debug message for received frames; Added hw.rt2860.debug enviro...
[ralink_drivers/rt2860_fbsd72.git] / rt2860.c
blob55ce78e1be0f5e670d75bd2bf15436ffeebed7e5
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 void rt2860_recv_action(struct ieee80211_node *ni,
142 const uint8_t *frm, const uint8_t *efrm);
144 static int rt2860_send_action(struct ieee80211_node *ni,
145 int category, int action, uint16_t args[4]);
147 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
149 static void rt2860_periodic(void *arg);
151 static void rt2860_tx_watchdog(void *arg);
153 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
154 const uint8_t *bssid);
156 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
157 const uint8_t *addr);
159 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc);
161 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc);
163 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc);
165 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc);
167 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc);
169 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc);
171 static void rt2860_asic_update_txpower(struct rt2860_softc *sc);
173 static void rt2860_asic_update_promisc(struct rt2860_softc *sc);
175 static void rt2860_asic_updateprot(struct rt2860_softc *sc);
177 static void rt2860_asic_updateslot(struct rt2860_softc *sc);
179 static void rt2860_asic_wme_update(struct rt2860_softc *sc);
181 static int rt2860_asic_update_beacon(struct rt2860_softc *sc);
183 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc);
185 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi);
187 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
188 const struct rt2860_rxwi *rxwi);
190 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
191 uint8_t rssi, uint8_t rxpath);
193 static uint8_t rt2860_rate2mcs(uint8_t rate);
195 static int rt2860_ackrate(struct ieee80211com *ic, int rate);
197 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags);
199 static int rt2860_tx_frame(struct rt2860_softc *sc,
200 struct mbuf *m, struct ieee80211_node *ni, int qid);
202 static int rt2860_tx_raw(struct rt2860_softc *sc,
203 struct mbuf *m, struct ieee80211_node *ni,
204 const struct ieee80211_bpf_params *params);
206 static void rt2860_intr(void *arg);
208 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc);
210 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc);
212 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc);
214 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc);
216 static void rt2860_rx_intr(struct rt2860_softc *sc);
218 static void rt2860_rx_delay_intr(struct rt2860_softc *sc);
220 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid);
222 static void rt2860_tx_delay_intr(struct rt2860_softc *sc);
224 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc);
226 static void rt2860_tbtt_intr(struct rt2860_softc *sc);
228 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc);
230 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc);
232 static void rt2860_gp_timer_intr(struct rt2860_softc *sc);
234 static void rt2860_rx_done_task(void *context, int pending);
236 static void rt2860_tx_done_task(void *context, int pending);
238 static void rt2860_fifo_sta_full_task(void *context, int pending);
240 static void rt2860_periodic_task(void *context, int pending);
242 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit);
244 static void rt2860_tx_eof(struct rt2860_softc *sc,
245 struct rt2860_softc_tx_ring *ring);
247 static void rt2860_update_stats(struct rt2860_softc *sc);
249 static void rt2860_watchdog(struct rt2860_softc *sc);
251 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc);
253 static void rt2860_update_raw_counters(struct rt2860_softc *sc);
255 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask);
257 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask);
259 static int rt2860_txrx_enable(struct rt2860_softc *sc);
261 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
262 struct rt2860_softc_rx_ring *ring);
264 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
265 struct rt2860_softc_rx_ring *ring);
267 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
268 struct rt2860_softc_rx_ring *ring);
270 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
271 struct rt2860_softc_tx_ring *ring, int qid);
273 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
274 struct rt2860_softc_tx_ring *ring);
276 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
277 struct rt2860_softc_tx_ring *ring);
279 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
280 int nseg, int error);
282 static void rt2860_sysctl_attach(struct rt2860_softc *sc);
285 * Static variables
288 static const struct rt2860_pci_ident rt2860_pci_ids[] =
290 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCI, "Ralink RT2860 PCI" },
291 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCIe, "Ralink RT2860 PCIe" },
292 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760_PCI, "Ralink RT2760 PCI" },
293 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790_PCIe, "Ralink RT2790 PCIe" },
294 { 0, 0, NULL }
297 static const struct
299 uint32_t reg;
300 uint32_t val;
301 } rt2860_def_mac[] =
303 { RT2860_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
304 { RT2860_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
305 { RT2860_REG_LEGACY_BASIC_RATE, 0x0000013f },
306 { RT2860_REG_HT_BASIC_RATE, 0x00008003 },
307 { RT2860_REG_SYS_CTRL, 0x00000000 },
308 { RT2860_REG_RX_FILTER_CFG, 0x00017f97 },
309 { RT2860_REG_BKOFF_SLOT_CFG, 0x00000209 },
310 { RT2860_REG_TX_SW_CFG0, 0x00000000 },
311 { RT2860_REG_TX_SW_CFG1, 0x00080606 },
312 { RT2860_REG_TX_LINK_CFG, 0x00001020 },
313 { RT2860_REG_TX_TIMEOUT_CFG, 0x000a2090 },
314 { RT2860_REG_MAX_LEN_CFG, (1 << 12) | RT2860_MAX_AGG_SIZE },
315 { RT2860_REG_LED_CFG, 0x7f031e46 },
316 { RT2860_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
317 { RT2860_REG_TX_RTY_CFG, 0x47d01f0f },
318 { RT2860_REG_AUTO_RSP_CFG, 0x00000013 },
319 { RT2860_REG_TX_CCK_PROT_CFG, 0x05740003 },
320 { RT2860_REG_TX_OFDM_PROT_CFG, 0x05740003 },
321 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f44084 },
322 { RT2860_REG_SCHDMA_WPDMA_GLO_CFG, 0x00000030 },
323 { RT2860_REG_TX_GF20_PROT_CFG, 0x01744004 },
324 { RT2860_REG_TX_GF40_PROT_CFG, 0x03f44084 },
325 { RT2860_REG_TX_MM20_PROT_CFG, 0x01744004 },
326 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f54084 },
327 { RT2860_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
328 { RT2860_REG_TX_RTS_CFG, 0x00092b20 },
329 { RT2860_REG_TX_EXP_ACK_TIME, 0x002400ca },
330 { RT2860_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
331 { RT2860_REG_XIFS_TIME_CFG, 0x33a41010 },
332 { RT2860_REG_PWR_PIN_CFG, 0x00000003 },
333 { RT2860_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
334 { RT2860_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
335 { RT2860_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
338 #define RT2860_DEF_MAC_SIZE (sizeof(rt2860_def_mac) / sizeof(rt2860_def_mac[0]))
340 static const struct
342 uint8_t reg;
343 uint8_t val;
344 } rt2860_def_bbp[] =
346 { 65, 0x2c },
347 { 66, 0x38 },
348 { 69, 0x12 },
349 { 70, 0x0a },
350 { 73, 0x10 },
351 { 81, 0x37 },
352 { 82, 0x62 },
353 { 83, 0x6a },
354 { 84, 0x99 },
355 { 86, 0x00 },
356 { 91, 0x04 },
357 { 92, 0x00 },
358 { 103, 0x00 },
359 { 105, 0x05 },
360 { 106, 0x35 },
363 #define RT2860_DEF_BBP_SIZE (sizeof(rt2860_def_bbp) / sizeof(rt2860_def_bbp[0]))
365 SYSCTL_DECL(_hw_rt2860);
367 #ifdef RT2860_DEBUG
368 static int rt2860_debug = 0;
369 SYSCTL_INT(_hw_rt2860, OID_AUTO, debug, CTLFLAG_RW, &rt2860_debug, 0, "rt2860 debug level");
370 TUNABLE_INT("hw.rt2860.debug", &rt2860_debug);
371 #endif
374 * rt2860_probe
376 static int rt2860_probe(device_t dev)
378 const struct rt2860_pci_ident *ident;
380 for (ident = rt2860_pci_ids; ident->name != NULL; ident++)
382 if (pci_get_vendor(dev) == ident->vendor &&
383 pci_get_device(dev) == ident->device)
385 device_set_desc(dev, ident->name);
386 return 0;
390 return ENXIO;
394 * rt2860_attach
396 static int rt2860_attach(device_t dev)
398 struct rt2860_softc *sc;
399 struct ifnet *ifp;
400 struct ieee80211com *ic;
401 int error, ntries, i;
403 sc = device_get_softc(dev);
405 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
407 printf("%s: chip is in D%d power mode, setting to D0\n",
408 device_get_nameunit(dev), pci_get_powerstate(dev));
409 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
412 /* enable bus-mastering */
414 pci_enable_busmaster(dev);
416 sc->dev = dev;
418 mtx_init(&sc->lock, device_get_nameunit(dev),
419 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
421 sc->mem_rid = PCIR_BAR(0);
422 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
423 &sc->mem_rid, RF_ACTIVE);
424 if (sc->mem == NULL)
426 printf("%s: could not allocate memory resource\n",
427 device_get_nameunit(dev));
428 error = ENXIO;
429 goto fail;
432 sc->bst = rman_get_bustag(sc->mem);
433 sc->bsh = rman_get_bushandle(sc->mem);
435 sc->irq_rid = 0;
436 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
437 &sc->irq_rid, RF_ACTIVE | RF_SHAREABLE);
438 if (sc->irq == NULL)
440 printf("%s: could not allocate interrupt resource\n",
441 device_get_nameunit(dev));
442 error = ENXIO;
443 goto fail;
446 #ifdef RT2860_DEBUG
447 sc->debug = rt2860_debug;
449 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
450 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
451 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2860 debug level");
452 #endif
454 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
455 "%s: attaching\n",
456 device_get_nameunit(sc->dev));
458 /* wait for NIC to initialize */
460 for (ntries = 0; ntries < 100; ntries++)
462 sc->mac_rev = rt2860_io_mac_read(sc, RT2860_REG_MAC_CSR0);
463 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
464 break;
466 DELAY(10);
469 if (ntries == 100)
471 printf("%s: timeout waiting for NIC to initialize\n",
472 device_get_nameunit(dev));
473 error = EIO;
474 goto fail;
477 rt2860_read_eeprom(sc);
479 printf("%s: MAC/BBP RT2860 (rev 0x%08x), RF %s\n",
480 device_get_nameunit(sc->dev), sc->mac_rev,
481 rt2860_rf_name(sc->rf_rev));
483 /* allocate Tx and Rx rings */
485 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
487 error = rt2860_alloc_tx_ring(sc, &sc->tx_ring[i], i);
488 if (error != 0)
490 printf("%s: could not allocate Tx ring #%d\n",
491 device_get_nameunit(sc->dev), i);
492 goto fail;
496 sc->tx_ring_mgtqid = 5;
498 error = rt2860_alloc_rx_ring(sc, &sc->rx_ring);
499 if (error != 0)
501 printf("%s: could not allocate Rx ring\n",
502 device_get_nameunit(sc->dev));
503 goto fail;
506 callout_init(&sc->periodic_ch, 0);
507 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
509 ifp = sc->ifp = if_alloc(IFT_ETHER);
510 if (ifp == NULL)
512 printf("%s: could not if_alloc()\n",
513 device_get_nameunit(sc->dev));
514 error = ENOMEM;
515 goto fail;
518 ifp->if_softc = sc;
520 if_initname(ifp, "rt2860", device_get_unit(sc->dev));
522 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
524 ifp->if_init = rt2860_init;
525 ifp->if_ioctl = rt2860_ioctl;
526 ifp->if_start = rt2860_start;
528 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
529 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
530 IFQ_SET_READY(&ifp->if_snd);
532 ic = &sc->ic;
534 ic->ic_ifp = ifp;
536 ic->ic_phytype = IEEE80211_T_HT;
537 ic->ic_opmode = IEEE80211_M_STA;
538 ic->ic_state = IEEE80211_S_INIT;
540 ic->ic_caps = IEEE80211_C_MONITOR |
541 IEEE80211_C_IBSS |
542 IEEE80211_C_AHDEMO |
543 IEEE80211_C_HOSTAP |
544 IEEE80211_C_WDS |
545 /* IEEE80211_C_BGSCAN | */
546 IEEE80211_C_TXPMGT |
547 IEEE80211_C_SHPREAMBLE |
548 IEEE80211_C_SHSLOT |
549 IEEE80211_C_TXFRAG |
550 IEEE80211_C_BURST |
551 IEEE80211_C_WME |
552 IEEE80211_C_WEP |
553 IEEE80211_C_TKIP |
554 IEEE80211_C_AES_CCM |
555 IEEE80211_C_WPA;
557 ic->ic_htcaps = IEEE80211_HTC_HT |
558 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
559 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
560 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
561 IEEE80211_HTCAP_GREENFIELD |
562 IEEE80211_HTCAP_SHORTGI20 | /* short GI for HT 20MHz */
563 IEEE80211_HTCAP_SHORTGI40 | /* short GI for HT 40MHz */
564 IEEE80211_HTCAP_RXSTBC_2STREAM;
566 /* init channels */
568 ic->ic_nchans = 0;
569 ic->ic_regdomain = 0;
570 ic->ic_countrycode = CTRY_DEFAULT;
571 ic->ic_location = 0;
573 rt2860_init_channels(sc);
575 rt2860_init_channels_ht40(sc);
577 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->mac_addr);
579 ieee80211_ifattach(ic);
581 sc->newstate = ic->ic_newstate;
582 ic->ic_newstate = rt2860_newstate;
584 ic->ic_reset = rt2860_reset;
585 ic->ic_scan_start = rt2860_scan_start;
586 ic->ic_scan_end = rt2860_scan_end;
587 ic->ic_set_channel = rt2860_set_channel;
588 ic->ic_newassoc = rt2860_newassoc;
589 ic->ic_updateslot = rt2860_updateslot;
590 ic->ic_wme.wme_update = rt2860_wme_update;
591 ic->ic_update_beacon = rt2860_update_beacon;
592 ic->ic_crypto.cs_key_update_begin = rt2860_key_update_begin;
593 ic->ic_crypto.cs_key_update_end = rt2860_key_update_end;
594 ic->ic_crypto.cs_key_set = rt2860_key_set;
595 ic->ic_crypto.cs_key_delete = rt2860_key_delete;
596 ic->ic_raw_xmit = rt2860_raw_xmit;
598 sc->recv_action = ic->ic_recv_action;
599 ic->ic_recv_action = rt2860_recv_action;
601 sc->send_action = ic->ic_send_action;
602 ic->ic_send_action = rt2860_send_action;
604 /* hardware requires padding between 802.11 frame header and body */
606 ic->ic_flags |= IEEE80211_F_WME | IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
608 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
610 ieee80211_media_init(ic, rt2860_media_change, ieee80211_media_status);
612 rt2860_amrr_init(&sc->amrr, ic,
613 RT2860_AMRR_MIN_SUCCESS_THRESHOLD,
614 RT2860_AMRR_MAX_SUCCESS_THRESHOLD,
615 500);
617 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
618 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
619 &sc->drvbpf);
621 sc->rxtap_len = sizeof(sc->rxtapu);
622 sc->rxtap.ihdr.it_len = htole16(sc->rxtap_len);
623 sc->rxtap.ihdr.it_present = htole32(RT2860_SOFTC_RX_RADIOTAP_PRESENT);
625 sc->txtap_len = sizeof(sc->txtapu);
626 sc->txtap.ihdr.it_len = htole16(sc->txtap_len);
627 sc->txtap.ihdr.it_present = htole32(RT2860_SOFTC_TX_RADIOTAP_PRESENT);
629 /* init task queue */
631 TASK_INIT(&sc->rx_done_task, 0, rt2860_rx_done_task, sc);
632 TASK_INIT(&sc->tx_done_task, 0, rt2860_tx_done_task, sc);
633 TASK_INIT(&sc->fifo_sta_full_task, 0, rt2860_fifo_sta_full_task, sc);
634 TASK_INIT(&sc->periodic_task, 0, rt2860_periodic_task, sc);
636 sc->rx_process_limit = 100;
638 sc->taskqueue = taskqueue_create("rt2860_taskq", M_NOWAIT,
639 taskqueue_thread_enqueue, &sc->taskqueue);
641 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
642 device_get_nameunit(sc->dev));
644 rt2860_sysctl_attach(sc);
646 if (bootverbose)
647 ieee80211_announce(ic);
649 /* set up interrupt */
651 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
652 NULL, rt2860_intr, sc, &sc->irqh);
653 if (error != 0)
655 printf("%s: could not set up interrupt\n",
656 device_get_nameunit(dev));
657 goto fail;
660 return 0;
662 fail:
664 /* free Tx and Rx rings */
666 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
667 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
669 rt2860_free_rx_ring(sc, &sc->rx_ring);
671 mtx_destroy(&sc->lock);
673 if (sc->mem != NULL)
674 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
676 if (sc->irq != NULL)
677 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
679 return error;
683 * rt2860_detach
685 static int rt2860_detach(device_t dev)
687 struct rt2860_softc *sc;
688 struct ieee80211com *ic;
689 struct ifnet *ifp;
690 int i;
692 sc = device_get_softc(dev);
693 ic = &sc->ic;
694 ifp = ic->ic_ifp;
696 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
697 "%s: detaching\n",
698 device_get_nameunit(sc->dev));
700 RT2860_SOFTC_LOCK(sc);
702 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
704 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
706 callout_stop(&sc->periodic_ch);
707 callout_stop(&sc->tx_watchdog_ch);
709 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
710 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
711 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
712 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
714 /* free Tx and Rx rings */
716 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
717 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
719 rt2860_free_rx_ring(sc, &sc->rx_ring);
721 RT2860_SOFTC_UNLOCK(sc);
723 bpfdetach(ifp);
725 ieee80211_ifdetach(ic);
727 if_free(ifp);
729 taskqueue_free(sc->taskqueue);
731 mtx_destroy(&sc->lock);
733 bus_generic_detach(dev);
735 bus_teardown_intr(dev, sc->irq, sc->irqh);
737 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
739 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
741 return 0;
745 * rt2860_shutdown
747 static int rt2860_shutdown(device_t dev)
749 struct rt2860_softc *sc;
751 sc = device_get_softc(dev);
753 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
754 "%s: shutting down\n",
755 device_get_nameunit(sc->dev));
757 rt2860_stop(sc);
759 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
761 return 0;
765 * rt2860_suspend
767 static int rt2860_suspend(device_t dev)
769 struct rt2860_softc *sc;
771 sc = device_get_softc(dev);
773 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
774 "%s: suspending\n",
775 device_get_nameunit(sc->dev));
777 rt2860_stop(sc);
779 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
781 return 0;
785 * rt2860_resume
787 static int rt2860_resume(device_t dev)
789 struct rt2860_softc *sc;
790 struct ifnet *ifp;
792 sc = device_get_softc(dev);
793 ifp = sc->ifp;
795 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
796 "%s: resuming\n",
797 device_get_nameunit(sc->dev));
799 if (ifp->if_flags & IFF_UP)
800 rt2860_init(sc);
802 return 0;
806 * rt2860_init_channels
808 static void rt2860_init_channels(struct rt2860_softc *sc)
810 struct ieee80211com *ic;
811 struct ieee80211_channel *c;
812 int i, flags;
814 ic = &sc->ic;
816 /* set supported channels for 2GHz band */
818 for (i = 1; i <= 14; i++)
820 c = &ic->ic_channels[ic->ic_nchans++];
821 flags = IEEE80211_CHAN_B;
823 c->ic_freq = ieee80211_ieee2mhz(i, flags);
824 c->ic_ieee = i;
825 c->ic_flags = flags;
827 c = &ic->ic_channels[ic->ic_nchans++];
828 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
830 c->ic_freq = ieee80211_ieee2mhz(i, flags);
831 c->ic_ieee = i;
832 c->ic_flags = flags;
834 c = &ic->ic_channels[ic->ic_nchans++];
835 flags = IEEE80211_CHAN_G;
837 c->ic_freq = ieee80211_ieee2mhz(i, flags);
838 c->ic_ieee = i;
839 c->ic_flags = flags;
841 c = &ic->ic_channels[ic->ic_nchans++];
842 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
844 c->ic_freq = ieee80211_ieee2mhz(i, flags);
845 c->ic_ieee = i;
846 c->ic_flags = flags;
849 /* set supported channels for 5GHz band */
851 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
852 sc->rf_rev == RT2860_EEPROM_RF_2750)
854 for (i = 36; i <= 64; i += 4)
856 c = &ic->ic_channels[ic->ic_nchans++];
857 flags = IEEE80211_CHAN_A;
859 c->ic_freq = ieee80211_ieee2mhz(i, flags);
860 c->ic_ieee = i;
861 c->ic_flags = flags;
863 c = &ic->ic_channels[ic->ic_nchans++];
864 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
866 c->ic_freq = ieee80211_ieee2mhz(i, flags);
867 c->ic_ieee = i;
868 c->ic_flags = flags;
871 for (i = 100; i <= 140; i += 4)
873 c = &ic->ic_channels[ic->ic_nchans++];
874 flags = IEEE80211_CHAN_A;
876 c->ic_freq = ieee80211_ieee2mhz(i, flags);
877 c->ic_ieee = i;
878 c->ic_flags = flags;
880 c = &ic->ic_channels[ic->ic_nchans++];
881 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
883 c->ic_freq = ieee80211_ieee2mhz(i, flags);
884 c->ic_ieee = i;
885 c->ic_flags = flags;
888 for (i = 149; i <= 165; i += 4)
890 c = &ic->ic_channels[ic->ic_nchans++];
891 flags = IEEE80211_CHAN_A;
893 c->ic_freq = ieee80211_ieee2mhz(i, flags);
894 c->ic_ieee = i;
895 c->ic_flags = flags;
897 c = &ic->ic_channels[ic->ic_nchans++];
898 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
900 c->ic_freq = ieee80211_ieee2mhz(i, flags);
901 c->ic_ieee = i;
902 c->ic_flags = flags;
908 * rt2860_init_channels_ht40
910 static void rt2860_init_channels_ht40(struct rt2860_softc *sc)
912 struct ieee80211com *ic;
913 struct ieee80211_channel *c, *cent, *ext;
914 int i, flags;
916 ic = &sc->ic;
918 /* set supported channels for 2GHz band */
920 for (i = 1; i <= 14; i++)
922 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
924 /* find the center channel */
926 cent = ieee80211_find_channel_byieee(ic, i,
927 flags & ~IEEE80211_CHAN_HT);
928 if (cent == NULL)
930 printf("%s: skip channel %d, could not find center channel\n",
931 device_get_nameunit(sc->dev), i);
932 continue;
935 /* find the extension channel */
937 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
938 flags & ~IEEE80211_CHAN_HT);
939 if (ext == NULL)
941 printf("%s: skip channel %d, could not find extension channel\n",
942 device_get_nameunit(sc->dev), i);
943 continue;
946 c = &ic->ic_channels[ic->ic_nchans++];
948 *c = *cent;
949 c->ic_extieee = ext->ic_ieee;
950 c->ic_flags &= ~IEEE80211_CHAN_HT;
951 c->ic_flags |= IEEE80211_CHAN_HT40U;
953 c = &ic->ic_channels[ic->ic_nchans++];
955 *c = *ext;
956 c->ic_extieee = cent->ic_ieee;
957 c->ic_flags &= ~IEEE80211_CHAN_HT;
958 c->ic_flags |= IEEE80211_CHAN_HT40D;
961 /* set supported channels for 5GHz band */
963 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
964 sc->rf_rev == RT2860_EEPROM_RF_2750)
966 for (i = 36; i <= 64; i += 4)
968 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
970 /* find the center channel */
972 cent = ieee80211_find_channel_byieee(ic, i,
973 flags & ~IEEE80211_CHAN_HT);
974 if (cent == NULL)
976 printf("%s: skip channel %d, could not find center channel\n",
977 device_get_nameunit(sc->dev), i);
978 continue;
981 /* find the extension channel */
983 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
984 flags & ~IEEE80211_CHAN_HT);
985 if (ext == NULL)
987 printf("%s: skip channel %d, could not find extension channel\n",
988 device_get_nameunit(sc->dev), i);
989 continue;
992 c = &ic->ic_channels[ic->ic_nchans++];
994 *c = *cent;
995 c->ic_extieee = ext->ic_ieee;
996 c->ic_flags &= ~IEEE80211_CHAN_HT;
997 c->ic_flags |= IEEE80211_CHAN_HT40U;
999 c = &ic->ic_channels[ic->ic_nchans++];
1001 *c = *ext;
1002 c->ic_extieee = cent->ic_ieee;
1003 c->ic_flags &= ~IEEE80211_CHAN_HT;
1004 c->ic_flags |= IEEE80211_CHAN_HT40D;
1007 for (i = 100; i <= 140; i += 4)
1009 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1011 /* find the center channel */
1013 cent = ieee80211_find_channel_byieee(ic, i,
1014 flags & ~IEEE80211_CHAN_HT);
1015 if (cent == NULL)
1017 printf("%s: skip channel %d, could not find center channel\n",
1018 device_get_nameunit(sc->dev), i);
1019 continue;
1022 /* find the extension channel */
1024 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1025 flags & ~IEEE80211_CHAN_HT);
1026 if (ext == NULL)
1028 printf("%s: skip channel %d, could not find extension channel\n",
1029 device_get_nameunit(sc->dev), i);
1030 continue;
1033 c = &ic->ic_channels[ic->ic_nchans++];
1035 *c = *cent;
1036 c->ic_extieee = ext->ic_ieee;
1037 c->ic_flags &= ~IEEE80211_CHAN_HT;
1038 c->ic_flags |= IEEE80211_CHAN_HT40U;
1040 c = &ic->ic_channels[ic->ic_nchans++];
1042 *c = *ext;
1043 c->ic_extieee = cent->ic_ieee;
1044 c->ic_flags &= ~IEEE80211_CHAN_HT;
1045 c->ic_flags |= IEEE80211_CHAN_HT40D;
1048 for (i = 149; i <= 165; i += 4)
1050 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1052 /* find the center channel */
1054 cent = ieee80211_find_channel_byieee(ic, i,
1055 flags & ~IEEE80211_CHAN_HT);
1056 if (cent == NULL)
1058 printf("%s: skip channel %d, could not find center channel\n",
1059 device_get_nameunit(sc->dev), i);
1060 continue;
1063 /* find the extension channel */
1065 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1066 flags & ~IEEE80211_CHAN_HT);
1067 if (ext == NULL)
1069 printf("%s: skip channel %d, could not find extension channel\n",
1070 device_get_nameunit(sc->dev), i);
1071 continue;
1074 c = &ic->ic_channels[ic->ic_nchans++];
1076 *c = *cent;
1077 c->ic_extieee = ext->ic_ieee;
1078 c->ic_flags &= ~IEEE80211_CHAN_HT;
1079 c->ic_flags |= IEEE80211_CHAN_HT40U;
1081 c = &ic->ic_channels[ic->ic_nchans++];
1083 *c = *ext;
1084 c->ic_extieee = cent->ic_ieee;
1085 c->ic_flags &= ~IEEE80211_CHAN_HT;
1086 c->ic_flags |= IEEE80211_CHAN_HT40D;
1092 * rt2860_init_locked
1094 static void rt2860_init_locked(void *priv)
1096 struct rt2860_softc *sc;
1097 struct ieee80211com *ic;
1098 struct ifnet *ifp;
1099 int error, i, ntries;
1100 uint32_t tmp, stacnt[6];
1102 sc = priv;
1103 ic = &sc->ic;
1104 ifp = ic->ic_ifp;
1106 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1107 "%s: initializing\n",
1108 device_get_nameunit(sc->dev));
1110 RT2860_SOFTC_ASSERT_LOCKED(sc);
1112 if (!(sc->flags & RT2860_SOFTC_FLAGS_UCODE_LOADED))
1114 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1115 "%s: loading 8051 microcode\n",
1116 device_get_nameunit(sc->dev));
1118 error = rt2860_io_mcu_load_ucode(sc, rt2860_ucode, sizeof(rt2860_ucode));
1119 if (error != 0)
1121 printf("%s: could not load 8051 microcode\n",
1122 device_get_nameunit(sc->dev));
1123 goto fail;
1126 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1127 "%s: 8051 microcode was successfully loaded\n",
1128 device_get_nameunit(sc->dev));
1130 sc->flags |= RT2860_SOFTC_FLAGS_UCODE_LOADED;
1133 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x2);
1135 /* disable DMA engine */
1137 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1139 tmp &= 0xff0;
1140 tmp |= RT2860_REG_TX_WB_DDONE;
1142 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1144 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, 0xffffffff);
1146 /* PBF hardware reset */
1148 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1149 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1151 /* wait while DMA engine is busy */
1153 for (ntries = 0; ntries < 100; ntries++)
1155 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1156 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1157 break;
1159 DELAY(1000);
1162 if (ntries == 100)
1164 printf("%s: timeout waiting for DMA engine\n",
1165 device_get_nameunit(sc->dev));
1166 goto fail;
1169 tmp &= 0xff0;
1170 tmp |= RT2860_REG_TX_WB_DDONE;
1172 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1174 /* reset Rx and Tx rings */
1176 tmp = RT2860_REG_RST_IDX_RX |
1177 RT2860_REG_RST_IDX_TX_MGMT |
1178 RT2860_REG_RST_IDX_TX_HCCA |
1179 RT2860_REG_RST_IDX_TX_AC3 |
1180 RT2860_REG_RST_IDX_TX_AC2 |
1181 RT2860_REG_RST_IDX_TX_AC1 |
1182 RT2860_REG_RST_IDX_TX_AC0;
1184 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, tmp);
1186 /* PBF hardware reset */
1188 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1189 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1191 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x3);
1193 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1194 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1195 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1197 /* init Tx power per rate */
1199 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
1201 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1202 continue;
1204 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i),
1205 sc->txpow_rate_20mhz[i]);
1208 for (i = 0; i < RT2860_DEF_MAC_SIZE; i++)
1209 rt2860_io_mac_write(sc, rt2860_def_mac[i].reg,
1210 rt2860_def_mac[i].val);
1212 /* wait while MAC is busy */
1214 for (ntries = 0; ntries < 100; ntries++)
1216 if (!(rt2860_io_mac_read(sc, RT2860_REG_STATUS_CFG) &
1217 (RT2860_REG_STATUS_TX_BUSY | RT2860_REG_STATUS_RX_BUSY)))
1218 break;
1220 DELAY(1000);
1223 if (ntries == 100)
1225 printf("%s: timeout waiting for MAC\n",
1226 device_get_nameunit(sc->dev));
1227 goto fail;
1230 /* clear Host to MCU mailbox */
1232 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX_BBP_AGENT, 0);
1233 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX, 0);
1235 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_BOOT,
1236 RT2860_REG_H2M_TOKEN_NO_INTR, 0);
1238 DELAY(1000);
1240 error = rt2860_init_bbp(sc);
1241 if (error != 0)
1242 goto fail;
1244 /* set up maximum buffer sizes */
1246 tmp = (1 << 12) | RT2860_MAX_AGG_SIZE;
1248 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1250 /* set mac address */
1252 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1254 rt2860_asic_set_macaddr(sc, ic->ic_myaddr);
1256 /* clear statistic registers */
1258 rt2860_io_mac_read_multi(sc, RT2860_REG_RX_STA_CNT0,
1259 stacnt, sizeof(stacnt));
1261 /* set RTS threshold */
1263 rt2860_asic_update_rtsthreshold(sc);
1265 /* set Tx power */
1267 rt2860_asic_update_txpower(sc);
1269 /* set up protection mode */
1271 rt2860_asic_updateprot(sc);
1273 /* clear key tables */
1275 rt2860_asic_clear_keytables(sc);
1277 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
1279 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1280 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
1282 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1284 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
1285 sc->tx_ring[i].desc_phys_addr);
1286 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
1287 RT2860_SOFTC_TX_RING_DESC_COUNT);
1288 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
1291 /* init Rx ring */
1293 rt2860_reset_rx_ring(sc, &sc->rx_ring);
1295 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
1296 sc->rx_ring.desc_phys_addr);
1297 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
1298 RT2860_SOFTC_RX_RING_DATA_COUNT);
1299 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
1300 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
1302 /* wait while DMA engine is busy */
1304 for (ntries = 0; ntries < 100; ntries++)
1306 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1307 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1308 break;
1310 DELAY(1000);
1313 if (ntries == 100)
1315 printf("%s: timeout waiting for DMA engine\n",
1316 device_get_nameunit(sc->dev));
1317 goto fail;
1320 tmp &= 0xff0;
1321 tmp |= RT2860_REG_TX_WB_DDONE;
1323 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1325 /* disable interrupts mitigation */
1327 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_DELAY_INT_CFG, 0);
1329 /* send LEDs operating mode to microcontroller */
1331 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED1,
1332 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1333 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED2,
1334 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1335 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED3,
1336 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1338 /* turn radio LED on */
1340 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON);
1342 /* write vendor-specific BBP values (from EEPROM) */
1344 for (i = 0; i < RT2860_SOFTC_BBP_EEPROM_COUNT; i++)
1346 if (sc->bbp_eeprom[i].reg == 0x00 ||
1347 sc->bbp_eeprom[i].reg == 0xff)
1348 continue;
1350 rt2860_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1351 sc->bbp_eeprom[i].val);
1355 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_GPIO_CTRL_CFG);
1356 if (tmp & (1 << 2))
1358 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_SLEEP,
1359 RT2860_REG_H2M_TOKEN_RADIOOFF, 0x02ff);
1360 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_RADIOOFF);
1362 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_WAKEUP,
1363 RT2860_REG_H2M_TOKEN_WAKEUP, 0);
1364 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_WAKEUP);
1368 /* disable non-existing Rx chains */
1370 tmp = rt2860_io_bbp_read(sc, 3);
1372 tmp &= ~((1 << 4) | (1 << 3));
1374 if (sc->nrxpath == 3)
1375 tmp |= (1 << 4);
1376 else if (sc->nrxpath == 2)
1377 tmp |= (1 << 3);
1379 rt2860_io_bbp_write(sc, 3, tmp);
1381 /* disable non-existing Tx chains */
1383 tmp = rt2860_io_bbp_read(sc, 1);
1385 if (sc->ntxpath == 1)
1386 tmp &= ~((1 << 4) | (1 << 3));
1388 rt2860_io_bbp_write(sc, 1, tmp);
1390 tmp = rt2860_io_bbp_read(sc, 4);
1392 tmp &= ~0x18;
1394 rt2860_io_bbp_write(sc, 4, tmp);
1396 /* set current channel */
1398 rt2860_rf_set_chan(sc, ic->ic_curchan);
1400 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1401 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1402 (48 << 16) | 96);
1404 if ((sc->mac_rev & 0xffff) != 0x0101)
1405 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1407 /* clear pending interrupts */
1409 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1411 /* enable interrupts */
1413 tmp = RT2860_REG_INT_TX_COHERENT |
1414 RT2860_REG_INT_RX_COHERENT |
1415 RT2860_REG_INT_GP_TIMER |
1416 RT2860_REG_INT_AUTO_WAKEUP |
1417 RT2860_REG_INT_FIFO_STA_FULL |
1419 RT2860_REG_INT_PRE_TBTT |
1420 RT2860_REG_INT_TBTT |
1422 RT2860_REG_INT_TXRX_COHERENT |
1423 RT2860_REG_INT_MCU_CMD |
1424 RT2860_REG_INT_TX_MGMT_DONE |
1425 RT2860_REG_INT_TX_HCCA_DONE |
1426 RT2860_REG_INT_TX_AC3_DONE |
1427 RT2860_REG_INT_TX_AC2_DONE |
1428 RT2860_REG_INT_TX_AC1_DONE |
1429 RT2860_REG_INT_TX_AC0_DONE |
1430 RT2860_REG_INT_RX_DONE;
1432 sc->intr_enable_mask = tmp;
1434 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1436 if (rt2860_txrx_enable(sc) != 0)
1437 goto fail;
1439 /* clear garbage interrupts */
1441 tmp = rt2860_io_mac_read(sc, 0x1300);
1443 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1444 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1446 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1448 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1449 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1451 else
1453 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1456 sc->periodic_round = 0;
1458 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1460 return;
1462 fail:
1464 rt2860_stop_locked(sc);
1468 * rt2860_init
1470 static void rt2860_init(void *priv)
1472 struct rt2860_softc *sc;
1474 sc = priv;
1476 RT2860_SOFTC_LOCK(sc);
1478 rt2860_init_locked(sc);
1480 RT2860_SOFTC_UNLOCK(sc);
1484 * rt2860_init_bbp
1486 static int rt2860_init_bbp(struct rt2860_softc *sc)
1488 int ntries, i;
1489 uint8_t tmp;
1491 for (ntries = 0; ntries < 20; ntries++)
1493 tmp = rt2860_io_bbp_read(sc, 0);
1494 if (tmp != 0x00 && tmp != 0xff)
1495 break;
1498 if (tmp == 0x00 || tmp == 0xff)
1500 printf("%s: timeout waiting for BBP to wakeup\n",
1501 device_get_nameunit(sc->dev));
1502 return ETIMEDOUT;
1505 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1506 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1507 rt2860_def_bbp[i].val);
1509 if ((sc->mac_rev & 0xffff) != 0x0101)
1510 rt2860_io_bbp_write(sc, 84, 0x19);
1512 return 0;
1516 * rt2860_stop_locked
1518 static void rt2860_stop_locked(void *priv)
1520 struct rt2860_softc *sc;
1521 struct ieee80211com *ic;
1522 struct ifnet *ifp;
1523 uint32_t tmp;
1525 sc = priv;
1526 ic = &sc->ic;
1527 ifp = ic->ic_ifp;
1529 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1530 "%s: stopping\n",
1531 device_get_nameunit(sc->dev));
1533 RT2860_SOFTC_ASSERT_LOCKED(sc);
1535 sc->tx_timer = 0;
1537 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1538 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1540 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1542 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1544 callout_stop(&sc->periodic_ch);
1545 callout_stop(&sc->tx_watchdog_ch);
1547 RT2860_SOFTC_UNLOCK(sc);
1549 taskqueue_block(sc->taskqueue);
1551 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1552 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1553 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1554 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1556 RT2860_SOFTC_LOCK(sc);
1558 /* clear key tables */
1560 rt2860_asic_clear_keytables(sc);
1562 /* disable interrupts */
1564 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1566 /* disable Tx/Rx */
1568 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1570 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1572 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1574 /* reset adapter */
1576 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1577 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1578 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1582 * rt2860_stop
1584 static void rt2860_stop(void *priv)
1586 struct rt2860_softc *sc;
1588 sc = priv;
1590 RT2860_SOFTC_LOCK(sc);
1592 rt2860_stop_locked(sc);
1594 RT2860_SOFTC_UNLOCK(sc);
1598 * rt2860_start
1600 static void rt2860_start(struct ifnet *ifp)
1602 struct rt2860_softc *sc;
1603 struct ieee80211com *ic;
1604 struct ieee80211_node *ni;
1605 struct ether_header *eh;
1606 struct mbuf *m;
1607 int qid;
1609 sc = ifp->if_softc;
1610 ic = &sc->ic;
1612 RT2860_SOFTC_LOCK(sc);
1614 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1616 RT2860_SOFTC_UNLOCK(sc);
1617 return;
1620 for (;;)
1622 IF_POLL(&ic->ic_mgtq, m);
1623 if (m != NULL)
1625 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1627 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1628 "%s: if_start: Tx ring with qid=%d is full\n",
1629 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
1631 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1632 break;
1635 IF_DEQUEUE(&ic->ic_mgtq, m);
1637 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1638 m->m_pkthdr.rcvif = NULL;
1640 if (bpf_peers_present(ic->ic_rawbpf))
1641 bpf_mtap(ic->ic_rawbpf, m);
1643 if (rt2860_tx_frame(sc, m, ni, sc->tx_ring_mgtqid) != 0)
1644 break;
1646 rt2860_drain_fifo_stats(sc);
1648 else
1650 if (ic->ic_state != IEEE80211_S_RUN)
1651 break;
1653 IF_POLL(&ifp->if_snd, m);
1654 if (m == NULL)
1655 break;
1657 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1659 if (ic->ic_flags & IEEE80211_F_SCAN)
1660 ieee80211_cancel_scan(ic);
1662 if (m->m_len < sizeof(struct ether_header) &&
1663 !(m = m_pullup(m, sizeof (struct ether_header))))
1664 continue;
1666 eh = mtod(m, struct ether_header *);
1668 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1669 if (ni == NULL)
1671 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1672 "%s: if_start: could not find Tx node\n",
1673 device_get_nameunit(sc->dev));
1675 m_freem(m);
1676 continue;
1679 ieee80211_classify(ic, m, ni);
1681 qid = M_WME_GETAC(m);
1683 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1685 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1686 "%s: if_start: Tx ring with qid=%d is full\n",
1687 device_get_nameunit(sc->dev), qid);
1689 m_freem(m);
1690 ieee80211_free_node(ni);
1691 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1692 ifp->if_oerrors++;
1693 break;
1696 BPF_MTAP(ifp, m);
1698 m = ieee80211_encap(ic, m, ni);
1699 if (m == NULL)
1701 ieee80211_free_node(ni);
1702 ifp->if_oerrors++;
1703 continue;
1706 if (bpf_peers_present(ic->ic_rawbpf))
1707 bpf_mtap(ic->ic_rawbpf, m);
1709 if (rt2860_tx_frame(sc, m, ni, qid) != 0)
1711 ieee80211_free_node(ni);
1712 ifp->if_oerrors++;
1713 break;
1716 rt2860_drain_fifo_stats(sc);
1719 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1721 ic->ic_lastdata = ticks;
1723 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1726 RT2860_SOFTC_UNLOCK(sc);
1730 * rt2860_ioctl
1732 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1734 struct rt2860_softc *sc;
1735 struct ieee80211com *ic;
1736 int error;
1738 sc = ifp->if_softc;
1739 ic = &sc->ic;
1741 error = 0;
1743 switch (cmd)
1745 case SIOCSIFFLAGS:
1746 RT2860_SOFTC_LOCK(sc);
1748 if (ifp->if_flags & IFF_UP)
1750 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1752 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1753 rt2860_asic_update_promisc(sc);
1755 else
1757 rt2860_init_locked(sc);
1760 else
1762 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1763 rt2860_stop_locked(sc);
1766 sc->if_flags = ifp->if_flags;
1768 RT2860_SOFTC_UNLOCK(sc);
1769 break;
1771 default:
1772 error = ieee80211_ioctl(ic, cmd, data);
1775 if (error == ENETRESET)
1777 RT2860_SOFTC_LOCK(sc);
1779 if ((ifp->if_flags & IFF_UP) &&
1780 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1781 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1783 rt2860_stop_locked(sc);
1784 rt2860_init_locked(sc);
1787 RT2860_SOFTC_UNLOCK(sc);
1789 error = 0;
1792 return error;
1796 * rt2860_reset
1798 static int rt2860_reset(struct ifnet *ifp)
1800 struct rt2860_softc *sc;
1801 struct ieee80211com *ic;
1803 sc = ifp->if_softc;
1804 ic = &sc->ic;
1806 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1807 return ENETRESET;
1809 rt2860_rf_set_chan(sc, ic->ic_curchan);
1811 return 0;
1815 * rt2860_newstate
1817 static int rt2860_newstate(struct ieee80211com *ic,
1818 enum ieee80211_state nstate, int arg)
1820 struct rt2860_softc *sc;
1821 struct ifnet *ifp;
1822 struct ieee80211_node *ni;
1823 int error;
1825 ifp = ic->ic_ifp;
1826 sc = ifp->if_softc;
1828 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1829 "%s: newstate: %s -> %s\n",
1830 device_get_nameunit(sc->dev),
1831 ieee80211_state_name[ic->ic_state], ieee80211_state_name[nstate]);
1833 error = sc->newstate(ic, nstate, arg);
1834 if (error != 0)
1835 return error;
1837 RT2860_SOFTC_LOCK(sc);
1839 /* turn link LED off */
1841 if (nstate != IEEE80211_S_RUN)
1842 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1844 switch (nstate)
1846 case IEEE80211_S_INIT:
1847 rt2860_asic_disable_tsf_sync(sc);
1848 break;
1850 case IEEE80211_S_RUN:
1851 ni = ic->ic_bss;
1853 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1855 rt2860_asic_enable_mrr(sc);
1856 rt2860_asic_set_txpreamble(sc);
1857 rt2860_asic_set_basicrates(sc);
1858 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1861 if (ic->ic_opmode == IEEE80211_M_STA)
1862 rt2860_newassoc(ni, 1);
1864 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1865 ic->ic_opmode == IEEE80211_M_IBSS)
1867 error = rt2860_asic_update_beacon(sc);
1868 if (error != 0)
1869 break;
1872 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1873 rt2860_asic_enable_tsf_sync(sc);
1875 /* turn link LED on */
1877 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1879 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1880 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1881 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
1883 break;
1885 default:
1886 break;
1889 RT2860_SOFTC_UNLOCK(sc);
1891 return error;
1895 * rt2860_scan_start
1897 static void rt2860_scan_start(struct ieee80211com *ic)
1899 struct rt2860_softc *sc;
1900 struct ifnet *ifp;
1902 ifp = ic->ic_ifp;
1903 sc = ifp->if_softc;
1905 rt2860_asic_disable_tsf_sync(sc);
1909 * rt2860_scan_end
1911 static void rt2860_scan_end(struct ieee80211com *ic)
1913 struct rt2860_softc *sc;
1914 struct ifnet *ifp;
1916 ifp = ic->ic_ifp;
1917 sc = ifp->if_softc;
1919 rt2860_asic_enable_tsf_sync(sc);
1923 * rt2860_set_channel
1925 static void rt2860_set_channel(struct ieee80211com *ic)
1927 struct rt2860_softc *sc;
1928 struct ifnet *ifp;
1930 ifp = ic->ic_ifp;
1931 sc = ifp->if_softc;
1933 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
1934 "%s: set channel: channel=%u, HT%s%s\n",
1935 device_get_nameunit(sc->dev),
1936 ieee80211_chan2ieee(ic, ic->ic_curchan),
1937 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
1938 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
1939 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
1940 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
1942 RT2860_SOFTC_LOCK(sc);
1944 rt2860_rf_set_chan(sc, ic->ic_curchan);
1946 RT2860_SOFTC_UNLOCK(sc);
1950 * rt2860_newassoc
1952 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
1954 struct rt2860_softc *sc;
1955 struct ieee80211com *ic;
1956 struct ifnet *ifp;
1957 uint16_t associd;
1958 uint8_t wcid;
1960 ic = ni->ni_ic;
1961 ifp = ic->ic_ifp;
1962 sc = ifp->if_softc;
1964 associd = (ni != NULL) ? ni->ni_associd : 0;
1965 wcid = RT2860_AID2WCID(associd);
1967 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
1968 "%s: new association: wcid=0x%02x, "
1969 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
1970 device_get_nameunit(sc->dev), wcid,
1971 ether_sprintf(ni->ni_macaddr),
1972 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
1973 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
1974 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
1976 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
1977 ni->ni_macaddr, IEEE80211_ADDR_LEN);
1979 rt2860_amrr_node_init(&sc->amrr, &sc->amrr_node[wcid], ni);
1981 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
1982 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
1983 device_get_nameunit(sc->dev),
1984 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
1985 ni->ni_associd,
1986 (ni->ni_flags & IEEE80211_NODE_HT) ?
1987 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
1988 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
1989 (ni->ni_flags & IEEE80211_NODE_HT) ?
1990 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
1991 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
1993 rt2860_asic_updateprot(sc);
1994 rt2860_asic_updateslot(sc);
1995 rt2860_asic_set_txpreamble(sc);
1999 * rt2860_updateslot
2001 static void rt2860_updateslot(struct ifnet *ifp)
2003 struct rt2860_softc *sc;
2005 sc = ifp->if_softc;
2007 rt2860_asic_updateslot(sc);
2011 * rt2860_wme_update
2013 static int rt2860_wme_update(struct ieee80211com *ic)
2015 struct rt2860_softc *sc;
2016 struct ifnet *ifp;
2018 ifp = ic->ic_ifp;
2019 sc = ifp->if_softc;
2021 rt2860_asic_wme_update(sc);
2023 return 0;
2027 * rt2860_update_beacon
2029 static void rt2860_update_beacon(struct ieee80211com *ic, int what)
2031 struct rt2860_softc *sc;
2032 struct ifnet *ifp;
2034 ifp = ic->ic_ifp;
2035 sc = ifp->if_softc;
2037 rt2860_asic_update_beacon(sc);
2041 * rt2860_key_update_begin
2043 static void rt2860_key_update_begin(struct ieee80211com *ic)
2045 struct rt2860_softc *sc;
2046 struct ifnet *ifp;
2048 ifp = ic->ic_ifp;
2049 sc = ifp->if_softc;
2051 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2052 "%s: key update begin\n",
2053 device_get_nameunit(sc->dev));
2055 taskqueue_block(sc->taskqueue);
2057 IF_LOCK(&ifp->if_snd);
2061 * rt2860_key_update_end
2063 static void rt2860_key_update_end(struct ieee80211com *ic)
2065 struct rt2860_softc *sc;
2066 struct ifnet *ifp;
2068 ifp = ic->ic_ifp;
2069 sc = ifp->if_softc;
2071 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2072 "%s: key update end\n",
2073 device_get_nameunit(sc->dev));
2075 IF_UNLOCK(&ifp->if_snd);
2077 taskqueue_unblock(sc->taskqueue);
2081 * rt2860_key_set
2083 static int rt2860_key_set(struct ieee80211com *ic,
2084 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2086 struct rt2860_softc *sc;
2087 struct ifnet *ifp;
2088 struct ieee80211_node *ni;
2089 uint16_t associd, key_base, keymode_base;
2090 uint8_t mode, vapid, wcid, iv[8];
2091 uint32_t tmp;
2093 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2094 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2095 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2096 return EINVAL;
2098 ifp = ic->ic_ifp;
2099 sc = ifp->if_softc;
2101 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2102 ni = ic->ic_bss;
2103 else
2104 ni = ieee80211_find_node(&ic->ic_sta, mac);
2106 associd = (ni != NULL) ? ni->ni_associd : 0;
2108 if ((ic->ic_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2109 ieee80211_free_node(ni);
2111 switch (k->wk_cipher->ic_cipher)
2113 case IEEE80211_CIPHER_WEP:
2114 if(k->wk_keylen < 8)
2115 mode = RT2860_REG_CIPHER_MODE_WEP40;
2116 else
2117 mode = RT2860_REG_CIPHER_MODE_WEP104;
2118 break;
2120 case IEEE80211_CIPHER_TKIP:
2121 mode = RT2860_REG_CIPHER_MODE_TKIP;
2122 break;
2124 case IEEE80211_CIPHER_AES_CCM:
2125 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2126 break;
2128 default:
2129 return EINVAL;
2132 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2133 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2134 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2135 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2137 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2139 /* install pairwise key */
2141 vapid = 0;
2142 wcid = RT2860_AID2WCID(associd);
2143 key_base = RT2860_REG_PKEY(wcid);
2145 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2147 memset(iv, 0, 8);
2149 iv[3] = (k->wk_keyix << 6);
2151 else
2153 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2155 iv[0] = (k->wk_keytsc >> 8);
2156 iv[1] = ((iv[0] | 0x20) & 0x7f);
2157 iv[2] = k->wk_keytsc;
2159 else
2161 /* AES CCMP */
2163 iv[0] = k->wk_keytsc;
2164 iv[1] = k->wk_keytsc >> 8;
2165 iv[2] = 0;
2168 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2169 iv[4] = (k->wk_keytsc >> 16);
2170 iv[5] = (k->wk_keytsc >> 24);
2171 iv[6] = (k->wk_keytsc >> 32);
2172 iv[7] = (k->wk_keytsc >> 40);
2174 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2175 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2176 device_get_nameunit(sc->dev),
2177 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2180 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2182 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2184 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2186 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2188 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2189 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2191 else
2193 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2194 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2197 else
2199 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2202 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2203 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2205 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2208 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2209 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2211 /* install group key */
2213 vapid = 0;
2214 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2215 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2217 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2219 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2221 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2223 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2224 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2226 else
2228 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2229 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2232 else
2234 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2237 tmp = rt2860_io_mac_read(sc, keymode_base);
2239 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2240 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2242 rt2860_io_mac_write(sc, keymode_base, tmp);
2245 return 1;
2249 * rt2860_key_delete
2251 static int rt2860_key_delete(struct ieee80211com *ic,
2252 const struct ieee80211_key *k)
2254 struct rt2860_softc *sc;
2255 struct ieee80211_node *ni;
2256 uint16_t associd;
2257 uint8_t vapid, wcid;
2258 uint32_t tmp;
2260 sc = ic->ic_ifp->if_softc;
2261 ni = ic->ic_bss;
2262 associd = (ni != NULL) ? ni->ni_associd : 0;
2264 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2265 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2266 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2267 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2269 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2271 /* remove pairwise key */
2273 wcid = RT2860_AID2WCID(associd);
2275 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID_ATTR(wcid));
2277 tmp &= ~0xf;
2278 tmp |= (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT);
2280 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2282 else
2284 /* remove group key */
2286 vapid = 0;
2288 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2290 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2291 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2293 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2296 return 1;
2300 * rt2860_raw_xmit
2302 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2303 const struct ieee80211_bpf_params *params)
2305 return 0;
2309 * rt2860_media_change
2311 static int rt2860_media_change(struct ifnet *ifp)
2313 struct rt2860_softc *sc;
2314 int error;
2316 sc = ifp->if_softc;
2318 error = ieee80211_media_change(ifp);
2319 if (error != ENETRESET)
2320 return error;
2322 RT2860_SOFTC_LOCK(sc);
2324 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2326 rt2860_stop_locked(sc);
2327 rt2860_init_locked(sc);
2330 RT2860_SOFTC_UNLOCK(sc);
2332 return 0;
2336 * rt2860_recv_action
2338 static void rt2860_recv_action(struct ieee80211_node *ni,
2339 const uint8_t *frm, const uint8_t *efrm)
2341 struct rt2860_softc *sc;
2342 struct ieee80211com *ic;
2343 struct ifnet *ifp;
2344 const struct ieee80211_action *ia;
2345 uint16_t associd, baparamset;
2346 uint8_t wcid;
2347 int tid;
2348 uint32_t tmp;
2350 ic = ni->ni_ic;
2351 ifp = ic->ic_ifp;
2352 sc = ifp->if_softc;
2354 ia = (const struct ieee80211_action *) frm;
2356 sc->recv_action(ni, frm, efrm);
2358 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2359 return;
2361 associd = (ni != NULL) ? ni->ni_associd : 0;
2362 wcid = RT2860_AID2WCID(associd);
2364 switch (ia->ia_action)
2366 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2367 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2368 baparamset = LE_READ_2(frm + 3);
2369 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2371 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2372 "%s: adding A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2373 device_get_nameunit(sc->dev), associd, tid);
2375 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2377 tmp |= (0x10000 << tid);
2379 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2380 break;
2382 /* IEEE80211_ACTION_BA_DELBA */
2383 case IEEE80211_ACTION_BA_DELBA:
2384 baparamset = LE_READ_2(frm + 2);
2385 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2387 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2388 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2389 device_get_nameunit(sc->dev), associd, tid);
2391 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2393 tmp &= ~(0x10000 << tid);
2395 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2396 break;
2401 * rt2860_send_action
2403 static int rt2860_send_action(struct ieee80211_node *ni,
2404 int category, int action, uint16_t args[4])
2406 struct rt2860_softc *sc;
2407 struct ieee80211com *ic;
2408 struct ifnet *ifp;
2409 uint16_t associd, baparamset;
2410 uint8_t wcid;
2411 int ret, tid;
2412 uint32_t tmp;
2414 ic = ni->ni_ic;
2415 ifp = ic->ic_ifp;
2416 sc = ifp->if_softc;
2418 ret = sc->send_action(ni, category, action, args);
2420 if (category != IEEE80211_ACTION_CAT_BA)
2421 return ret;
2423 associd = (ni != NULL) ? ni->ni_associd : 0;
2424 wcid = RT2860_AID2WCID(associd);
2426 switch (action)
2428 /* IEEE80211_ACTION_BA_DELBA */
2429 case IEEE80211_ACTION_BA_DELBA:
2430 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) |
2431 RT2860_SM(args[1], IEEE80211_DELBAPS_INIT);
2433 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) == IEEE80211_DELBAPS_INIT)
2434 break;
2436 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2438 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2439 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2440 device_get_nameunit(sc->dev), associd, tid);
2442 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2444 tmp &= ~(0x10000 << tid);
2446 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2447 break;
2450 return ret;
2454 * rt2860_amrr_update_iter_func
2456 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2458 struct rt2860_softc *sc;
2459 struct ieee80211com *ic;
2460 uint8_t wcid;
2462 sc = arg;
2463 ic = &sc->ic;
2465 /* only associated stations */
2467 if (ni->ni_associd != 0)
2469 wcid = RT2860_AID2WCID(ni->ni_associd);
2471 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2473 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2474 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2475 device_get_nameunit(sc->dev),
2476 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2477 ni->ni_associd,
2478 (ni->ni_flags & IEEE80211_NODE_HT) ?
2479 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2480 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2481 (ni->ni_flags & IEEE80211_NODE_HT) ?
2482 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2483 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2488 * rt2860_periodic
2490 static void rt2860_periodic(void *arg)
2492 struct rt2860_softc *sc;
2494 sc = arg;
2496 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2497 "%s: periodic\n",
2498 device_get_nameunit(sc->dev));
2500 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2504 * rt2860_tx_watchdog
2506 static void rt2860_tx_watchdog(void *arg)
2508 struct rt2860_softc *sc;
2509 struct ifnet *ifp;
2511 sc = arg;
2512 ifp = sc->ifp;
2514 if (sc->tx_timer == 0)
2515 return;
2517 if (--sc->tx_timer == 0)
2519 printf("%s: Tx watchdog timeout: resetting\n",
2520 device_get_nameunit(sc->dev));
2522 rt2860_stop_locked(sc);
2523 rt2860_init_locked(sc);
2525 ifp->if_oerrors++;
2527 sc->tx_watchdog_timeouts++;
2530 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2534 * rt2860_asic_set_bssid
2536 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2537 const uint8_t *bssid)
2539 uint32_t tmp;
2541 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2542 "%s: set bssid: bssid=%s\n",
2543 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2545 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2547 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2549 tmp = bssid[4] | (bssid[5] << 8);
2551 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2555 * rt2860_asic_set_macaddr
2557 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2558 const uint8_t *addr)
2560 uint32_t tmp;
2562 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2564 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2566 tmp = addr[4] | (addr[5] << 8);
2568 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2572 * rt2860_asic_enable_tsf_sync
2574 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2576 struct ieee80211com *ic;
2577 uint32_t tmp;
2579 ic = &sc->ic;
2581 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2582 "%s: enabling TSF\n",
2583 device_get_nameunit(sc->dev));
2585 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2587 tmp &= ~0x1fffff;
2588 tmp |= ic->ic_bss->ni_intval * 16;
2589 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2591 if (ic->ic_opmode == IEEE80211_M_STA)
2593 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2595 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2597 tmp |= RT2860_REG_BCN_TX_ENABLE;
2598 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2600 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2602 tmp |= RT2860_REG_BCN_TX_ENABLE;
2603 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2606 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2610 * rt2860_asic_disable_tsf_sync
2612 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2614 uint32_t tmp;
2616 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2617 "%s: disabling TSF\n",
2618 device_get_nameunit(sc->dev));
2620 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2622 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2623 RT2860_REG_TSF_TIMER_ENABLE |
2624 RT2860_REG_TBTT_TIMER_ENABLE);
2626 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2627 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2629 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2633 * rt2860_asic_enable_mrr
2635 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2637 #define CCK(mcs) (mcs)
2638 #define OFDM(mcs) ((1 << 3) | (mcs))
2639 #define HT(mcs) (mcs)
2641 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2642 (OFDM(6) << 28) | /* 54 -> 48 */
2643 (OFDM(5) << 24) | /* 48 -> 36 */
2644 (OFDM(4) << 20) | /* 36 -> 24 */
2645 (OFDM(3) << 16) | /* 24 -> 18 */
2646 (OFDM(2) << 12) | /* 18 -> 12 */
2647 (OFDM(1) << 8) | /* 12 -> 9 */
2648 (OFDM(0) << 4) | /* 9 -> 6 */
2649 OFDM(0)); /* 6 -> 6 */
2651 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2652 (CCK(2) << 12) | /* 11 -> 5.5 */
2653 (CCK(1) << 8) | /* 5.5 -> 2 */
2654 (CCK(0) << 4) | /* 2 -> 1 */
2655 CCK(0)); /* 1 -> 1 */
2657 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2658 (HT(6) << 28) |
2659 (HT(5) << 24) |
2660 (HT(4) << 20) |
2661 (HT(3) << 16) |
2662 (HT(2) << 12) |
2663 (HT(1) << 8) |
2664 (HT(0) << 4) |
2665 HT(0));
2667 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2668 (HT(14) << 28) |
2669 (HT(13) << 24) |
2670 (HT(12) << 20) |
2671 (HT(11) << 16) |
2672 (HT(10) << 12) |
2673 (HT(9) << 8) |
2674 (HT(8) << 4) |
2675 HT(8));
2677 #undef HT
2678 #undef OFDM
2679 #undef CCK
2683 * rt2860_asic_set_txpreamble
2685 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2687 struct ieee80211com *ic;
2688 uint32_t tmp;
2690 ic = &sc->ic;
2692 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2693 "%s: %s short Tx preamble\n",
2694 device_get_nameunit(sc->dev),
2695 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2697 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2699 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2701 if (sc->ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2702 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2704 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2708 * rt2860_asic_set_basicrates
2710 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2712 struct ieee80211com *ic;
2714 ic = &sc->ic;
2716 if (ic->ic_curmode == IEEE80211_MODE_11B)
2717 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2718 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2719 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2720 else
2721 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2725 * rt2860_asic_update_rtsthreshold
2727 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2729 struct ieee80211com *ic;
2730 uint32_t tmp;
2731 uint16_t threshold;
2733 ic = &sc->ic;
2735 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2736 "%s: updating RTS threshold: %d\n",
2737 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
2739 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2741 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2743 threshold = (ic->ic_rtsthreshold < IEEE80211_RTS_MAX) ?
2744 ic->ic_rtsthreshold : 0x1000;
2746 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2747 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2749 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
2753 * rt2860_asic_update_txpower
2755 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
2757 struct ieee80211com *ic;
2758 uint32_t *txpow_rate;
2759 int8_t delta;
2760 uint8_t val;
2761 uint32_t tmp;
2762 int i;
2764 ic = &sc->ic;
2766 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2767 "%s: updating Tx power: %d\n",
2768 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
2770 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
2772 txpow_rate = sc->txpow_rate_20mhz;
2774 else
2776 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2777 txpow_rate = sc->txpow_rate_40mhz_2ghz;
2778 else
2779 txpow_rate = sc->txpow_rate_40mhz_5ghz;
2782 delta = 0;
2784 val = rt2860_io_bbp_read(sc, 1);
2785 val &= 0xfc;
2787 if (ic->ic_txpowlimit > 90)
2789 /* do nothing */
2791 else if (ic->ic_txpowlimit > 60)
2793 delta -= 1;
2795 else if (ic->ic_txpowlimit > 30)
2797 delta -= 3;
2799 else if (ic->ic_txpowlimit > 15)
2801 val |= 0x1;
2803 else if (ic->ic_txpowlimit > 9)
2805 val |= 0x1;
2806 delta -= 3;
2808 else
2810 val |= 0x2;
2813 rt2860_io_bbp_write(sc, 1, val);
2815 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
2817 if (txpow_rate[i] == 0xffffffff)
2818 continue;
2820 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
2822 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
2827 * rt2860_asic_update_promisc
2829 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
2831 struct ifnet *ifp;
2832 uint32_t tmp;
2834 ifp = sc->ic.ic_ifp;
2836 printf("%s: %s promiscuous mode\n",
2837 device_get_nameunit(sc->dev),
2838 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
2840 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
2842 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
2844 if (!(ifp->if_flags & IFF_PROMISC))
2845 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
2847 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
2851 * rt2860_asic_updateprot
2853 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
2855 struct ieee80211com *ic;
2856 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
2857 uint8_t htopmode;
2859 ic = &sc->ic;
2861 /* CCK frame protection */
2863 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2864 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
2866 /* set up protection frame phy mode and rate (MCS code) */
2868 if (ic->ic_curmode == IEEE80211_MODE_11A)
2869 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2870 (0 << RT2860_REG_PROT_MCS_SHIFT);
2871 else
2872 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2873 (3 << RT2860_REG_PROT_MCS_SHIFT));
2875 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
2877 /* OFDM frame protection */
2879 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2880 RT2860_REG_TXOP_ALLOW_ALL;
2882 if (ic->ic_flags & IEEE80211_F_USEPROT)
2884 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2885 "%s: updating protection mode: b/g protection mode=%s\n",
2886 device_get_nameunit(sc->dev),
2887 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2888 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2890 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2891 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2892 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2893 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
2894 else
2895 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2897 else
2899 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2900 "%s: updating protection mode: b/g protection mode=%s\n",
2901 device_get_nameunit(sc->dev), "none");
2903 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2906 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
2908 /* HT frame protection */
2910 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
2911 htopmode = ic->ic_bss->ni_htopmode;
2912 else
2913 htopmode = ic->ic_curhtprotmode;
2915 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2916 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
2917 device_get_nameunit(sc->dev),
2918 htopmode & IEEE80211_HTINFO_OPMODE,
2919 (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2920 ((ic->ic_htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2922 switch (htopmode & IEEE80211_HTINFO_OPMODE)
2924 /* IEEE80211_HTINFO_OPMODE_HT20PR */
2925 case IEEE80211_HTINFO_OPMODE_HT20PR:
2926 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
2927 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2928 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
2929 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2930 (4 << RT2860_REG_PROT_MCS_SHIFT);
2932 gf20_prot = mm20_prot;
2934 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
2935 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2936 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2938 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2939 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2940 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2941 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2942 else
2943 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
2945 gf40_prot = mm40_prot;
2946 break;
2948 /* IEEE80211_HTINFO_OPMODE_MIXED */
2949 case IEEE80211_HTINFO_OPMODE_MIXED:
2950 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
2951 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2952 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
2954 if (ic->ic_flags & IEEE80211_F_USEPROT)
2955 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2956 (3 << RT2860_REG_PROT_MCS_SHIFT);
2957 else
2958 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2959 (4 << RT2860_REG_PROT_MCS_SHIFT);
2961 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2962 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2963 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2964 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
2965 else
2966 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
2968 gf20_prot = mm20_prot;
2970 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
2972 if (ic->ic_flags & IEEE80211_F_USEPROT)
2973 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2974 (3 << RT2860_REG_PROT_MCS_SHIFT);
2975 else
2976 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2977 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2979 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2980 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2981 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2982 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2983 else
2984 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
2986 gf40_prot = mm40_prot;
2987 break;
2990 * IEEE80211_HTINFO_OPMODE_PURE
2991 * IEEE80211_HTINFO_OPMODE_PROTOPT
2993 case IEEE80211_HTINFO_OPMODE_PURE:
2994 case IEEE80211_HTINFO_OPMODE_PROTOPT:
2995 default:
2996 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
2997 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2998 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
2999 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3000 (4 << RT2860_REG_PROT_MCS_SHIFT);
3002 gf20_prot = mm20_prot;
3004 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3005 RT2860_REG_TXOP_ALLOW_ALL |
3006 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3007 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3009 gf40_prot = mm40_prot;
3010 break;
3013 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3014 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3015 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3016 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3020 * rt2860_asic_updateslot
3022 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3024 struct ieee80211com *ic;
3025 uint32_t tmp;
3027 ic = &sc->ic;
3029 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3030 "%s: %s short slot time\n",
3031 device_get_nameunit(sc->dev),
3032 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3034 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3036 tmp &= ~0xff;
3037 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3039 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3043 * rt2860_asic_wme_update
3045 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3047 struct ieee80211com *ic;
3048 struct ieee80211_wme_state *wme;
3049 const struct wmeParams *wmep;
3050 int i;
3052 ic = &sc->ic;
3053 wme = &ic->ic_wme;
3054 wmep = wme->wme_chanParams.cap_wmeParams;
3056 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3057 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3058 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3059 device_get_nameunit(sc->dev),
3060 wmep[WME_AC_VO].wmep_aifsn,
3061 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3062 wmep[WME_AC_VO].wmep_txopLimit,
3063 wmep[WME_AC_VI].wmep_aifsn,
3064 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3065 wmep[WME_AC_VI].wmep_txopLimit,
3066 wmep[WME_AC_BK].wmep_aifsn,
3067 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3068 wmep[WME_AC_BK].wmep_txopLimit,
3069 wmep[WME_AC_BE].wmep_aifsn,
3070 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3071 wmep[WME_AC_BE].wmep_txopLimit);
3073 for (i = 0; i < WME_NUM_AC; i++)
3074 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3075 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3076 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3078 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3079 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3080 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3082 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3083 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3084 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3086 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3087 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3088 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3090 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3091 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3093 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3094 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3098 * rt2860_asic_update_beacon
3100 static int rt2860_asic_update_beacon(struct rt2860_softc *sc)
3102 struct ieee80211com *ic;
3103 struct mbuf *m;
3104 struct rt2860_txwi txwi;
3105 uint8_t rate, mcs;
3106 uint32_t tmp;
3108 ic = &sc->ic;
3110 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3111 if (m == NULL)
3112 return ENOMEM;
3114 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3115 mcs = rt2860_rate2mcs(rate);
3117 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3119 txwi.wcid = 0xff;
3120 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3121 RT2860_TXWI_MPDU_LEN_SHIFT);
3122 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3123 txwi.mpdu_density_flags |=
3124 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3125 txwi.bawin_size_xflags |=
3126 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3128 if (rate == 2)
3130 txwi.phymode_ifs_stbc_shortgi =
3131 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3133 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3134 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3136 else
3138 txwi.phymode_ifs_stbc_shortgi =
3139 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3142 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3143 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3145 /* disable temporarily TSF sync */
3147 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3149 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3150 RT2860_REG_TSF_TIMER_ENABLE |
3151 RT2860_REG_TBTT_TIMER_ENABLE);
3153 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3155 /* write Tx wireless info and beacon frame to on-chip memory */
3157 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3158 &txwi, sizeof(struct rt2860_txwi));
3160 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3161 mtod(m, uint8_t *), m->m_pkthdr.len);
3163 /* enable again TSF sync */
3165 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3167 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3168 RT2860_REG_TSF_TIMER_ENABLE |
3169 RT2860_REG_TBTT_TIMER_ENABLE);
3171 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3173 m_freem(m);
3175 return 0;
3179 * rt2860_asic_clear_keytables
3181 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3183 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3185 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID(0), 0, 2 * 256);
3187 /* clear WCID attribute table (entries = 256, entry size = 4) */
3189 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3191 /* clear IV/EIV table (entries = 256, entry size = 8) */
3193 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3195 /* clear pairwise key table (entries = 256, entry size = 32) */
3197 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3199 /* clear shared key table (entries = 32, entry size = 32) */
3201 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3203 /* clear shared key mode (entries = 32, entry size = 2) */
3205 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3209 * rt2860_rxrate
3211 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3213 uint8_t mcs, phymode;
3214 uint8_t rate;
3216 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3217 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3218 RT2860_RXWI_PHYMODE_MASK;
3220 rate = 2;
3222 switch (phymode)
3224 case RT2860_RXWI_PHYMODE_CCK:
3225 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3227 case 0: rate = 2; break; /* 1 Mbps */
3228 case 1: rate = 4; break; /* 2 MBps */
3229 case 2: rate = 11; break; /* 5.5 Mbps */
3230 case 3: rate = 22; break; /* 11 Mbps */
3232 break;
3234 case RT2860_RXWI_PHYMODE_OFDM:
3235 switch (mcs)
3237 case 0: rate = 12; break; /* 6 Mbps */
3238 case 1: rate = 18; break; /* 9 Mbps */
3239 case 2: rate = 24; break; /* 12 Mbps */
3240 case 3: rate = 36; break; /* 18 Mbps */
3241 case 4: rate = 48; break; /* 24 Mbps */
3242 case 5: rate = 72; break; /* 36 Mbps */
3243 case 6: rate = 96; break; /* 48 Mbps */
3244 case 7: rate = 108; break; /* 54 Mbps */
3246 break;
3248 case RT2860_RXWI_PHYMODE_HT_MIXED:
3249 case RT2860_RXWI_PHYMODE_HT_GF:
3250 break;
3253 return rate;
3257 * rt2860_maxrssi_rxpath
3259 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3260 const struct rt2860_rxwi *rxwi)
3262 uint8_t rxpath;
3264 rxpath = 0;
3266 if (sc->nrxpath > 1)
3267 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3268 rxpath = 1;
3270 if (sc->nrxpath > 2)
3271 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3272 rxpath = 2;
3274 return rxpath;
3278 * rt2860_rssi2dbm
3280 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3281 uint8_t rssi, uint8_t rxpath)
3283 struct ieee80211com *ic;
3284 struct ieee80211_channel *c;
3285 int chan, delta;
3287 ic = &sc->ic;
3288 c = ic->ic_curchan;
3289 chan = ieee80211_chan2ieee(ic, c);
3291 if (IEEE80211_IS_CHAN_5GHZ(c))
3293 delta = sc->rssi_off_5ghz[rxpath];
3295 if (chan <= 64)
3296 delta -= sc->lna_gain[1];
3297 else if (chan <= 128)
3298 delta -= sc->lna_gain[2];
3299 else
3300 delta -= sc->lna_gain[3];
3302 else
3304 delta = sc->rssi_off_2ghz[rxpath] - sc->lna_gain[0];
3307 return (-12 - delta - rssi);
3311 * rt2860_rate2mcs
3313 static uint8_t rt2860_rate2mcs(uint8_t rate)
3315 switch (rate)
3317 /* CCK rates */
3318 case 2: return 0;
3319 case 4: return 1;
3320 case 11: return 2;
3321 case 22: return 3;
3323 /* OFDM rates */
3324 case 12: return 0;
3325 case 18: return 1;
3326 case 24: return 2;
3327 case 36: return 3;
3328 case 48: return 4;
3329 case 72: return 5;
3330 case 96: return 6;
3331 case 108: return 7;
3334 return 0;
3338 * rt2860_ackrate
3340 static int rt2860_ackrate(struct ieee80211com *ic, int rate)
3342 switch (rate)
3344 /* CCK rates */
3346 case 2:
3347 return 2;
3349 case 4:
3350 case 11:
3351 case 22:
3352 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3354 /* OFDM rates */
3356 case 12:
3357 case 18:
3358 return 12;
3360 case 24:
3361 case 36:
3362 return 24;
3364 case 48:
3365 case 72:
3366 case 96:
3367 case 108:
3368 return 48;
3371 /* default to 1Mbps */
3372 return 2;
3376 * rt2860_txtime
3378 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags)
3380 uint16_t txtime;
3382 if (RT2860_RATE_IS_OFDM(rate))
3384 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3385 txtime = 16 + 4 + 4 * txtime + 6;
3387 else
3389 txtime = (16 * len + rate - 1) / rate;
3391 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3392 txtime += 72 + 24;
3393 else
3394 txtime += 144 + 48;
3397 return txtime;
3401 * rt2860_tx_frame
3403 static int rt2860_tx_frame(struct rt2860_softc *sc,
3404 struct mbuf *m, struct ieee80211_node *ni, int qid)
3406 struct ieee80211com *ic;
3407 struct rt2860_softc_tx_ring *ring;
3408 struct rt2860_softc_tx_data *data;
3409 struct rt2860_txdesc *desc;
3410 struct rt2860_txwi *txwi;
3411 struct ieee80211_frame *wh;
3412 struct rt2860_softc_tx_radiotap_header *tap;
3413 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3414 u_int hdrsize, hdrspace;
3415 uint8_t type, rate, bw, shortgi, mcs, pid, wcid, qsel;
3416 uint16_t qos, len, dmalen, mpdu_len, dur;
3417 int error, hasqos, ackrate, ndmasegs, ndescs, i, j;
3419 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3420 ("%s: Tx frame: invalid qid=%d\n",
3421 device_get_nameunit(sc->dev), qid));
3423 ic = &sc->ic;
3425 ring = &sc->tx_ring[qid];
3426 desc = &ring->desc[ring->desc_cur];
3427 data = &ring->data[ring->data_cur];
3428 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3430 wh = mtod(m, struct ieee80211_frame *);
3432 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3434 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3435 if (hasqos)
3437 if (IEEE80211_HAS_ADDR4(wh))
3438 qos = le16toh(*(const uint16_t *)
3439 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3440 else
3441 qos = le16toh(*(const uint16_t *)
3442 (((struct ieee80211_qosframe *) wh)->i_qos));
3444 else
3446 qos = 0;
3449 if (ni->ni_flags & IEEE80211_NODE_HT)
3451 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3452 rate = 0;
3453 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3454 rate = ic->ic_fixed_rate;
3455 else
3456 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
3458 else
3460 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3461 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3462 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3463 rate = ic->ic_fixed_rate;
3464 else
3465 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
3468 rate &= IEEE80211_RATE_VAL;
3470 /* fill Tx wireless info */
3472 if (ni->ni_flags & IEEE80211_NODE_HT)
3473 mcs = rate;
3474 else
3475 mcs = rt2860_rate2mcs(rate);
3477 pid = mcs + 1;
3479 /* management frames do not need encryption */
3481 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3482 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3484 /* calculate MPDU length without padding */
3486 hdrsize = ieee80211_hdrsize(wh);
3487 hdrspace = ieee80211_hdrspace(ic, wh);
3488 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3490 memset(txwi, 0, sizeof(struct rt2860_txwi));
3492 txwi->wcid = wcid;
3494 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3495 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3496 RT2860_TXWI_MPDU_LEN_SHIFT);
3498 if ((ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
3499 (ni->ni_flags & IEEE80211_NODE_HT))
3500 shortgi = 1;
3501 else
3502 shortgi = 0;
3504 txwi->phymode_ifs_stbc_shortgi |=
3505 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3507 if (ni->ni_flags & IEEE80211_NODE_HT)
3509 txwi->phymode_ifs_stbc_shortgi |=
3510 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3512 else
3514 if (!RT2860_RATE_IS_OFDM(rate))
3516 txwi->phymode_ifs_stbc_shortgi |=
3517 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3519 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3520 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3522 else
3524 txwi->phymode_ifs_stbc_shortgi |=
3525 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3529 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
3530 (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40))
3531 bw = RT2860_TXWI_BW_40;
3532 else
3533 bw = RT2860_TXWI_BW_20;
3535 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
3536 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3538 if (type != IEEE80211_FC0_TYPE_DATA)
3539 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3540 else
3541 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3543 /* skip ACKs for multicast frames and probe responses */
3545 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3546 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3547 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
3548 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
3550 txwi->bawin_size_xflags |=
3551 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3553 if (ni->ni_flags & IEEE80211_NODE_HT)
3555 /* preamble + plcp + signal extension */
3557 dur = 16 + 4 + 6;
3559 else
3561 ackrate = rt2860_ackrate(ic, rate);
3563 dur = rt2860_txtime(RT2860_ACK_SIZE, ackrate, ic->ic_flags) +
3564 sc->sifs;
3567 *(uint16_t *) wh->i_dur = htole16(dur);
3570 /* ask MAC to insert timestamp into probe responses */
3572 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3573 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3574 txwi->mpdu_density_flags |=
3575 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3577 if (bpf_peers_present(sc->drvbpf))
3579 tap = &sc->txtap;
3581 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3582 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3583 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3584 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3585 tap->chan_maxpow = 0;
3587 if (ni->ni_flags & IEEE80211_NODE_HT)
3588 tap->rate = mcs | IEEE80211_RATE_MCS;
3589 else
3590 tap->rate = rate;
3592 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3593 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3595 if (shortgi)
3596 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3598 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3599 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3601 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3603 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3605 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3607 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3609 else
3611 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3615 /* copy and trim 802.11 header */
3617 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3618 m_adj(m, hdrspace);
3620 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3621 dma_seg, &ndmasegs, 0);
3622 if (error != 0)
3624 /* too many fragments, linearize */
3626 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3627 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3628 device_get_nameunit(sc->dev));
3630 m = m_defrag(m, M_DONTWAIT);
3631 if (m == NULL)
3632 return ENOMEM;
3634 sc->tx_defrag_packets++;
3636 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3637 dma_seg, &ndmasegs, 0);
3638 if (error != 0)
3640 printf("%s: could not load mbuf DMA map: error=%d\n",
3641 device_get_nameunit(sc->dev), error);
3642 m_freem(m);
3643 return error;
3647 /* determine how many Tx descs are required */
3649 ndescs = 1 + ndmasegs / 2;
3650 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3652 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3653 "%s: there are not enough Tx descs\n",
3654 device_get_nameunit(sc->dev));
3656 sc->no_tx_desc_avail++;
3658 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3659 m_freem(m);
3660 return EFBIG;
3663 data->m = m;
3664 data->ni = ni;
3666 /* set up Tx descs */
3668 /* first segment is Tx wireless info and 802.11 header */
3670 len = sizeof(struct rt2860_txwi) + hdrsize;
3672 /* align end on a 4-bytes boundary */
3674 dmalen = (len + 3) & ~ 3;
3676 memset((caddr_t) txwi + len, 0, dmalen - len);
3678 qsel = RT2860_TXDESC_QSEL_EDCA;
3680 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3681 desc->sdl0 = htole16(dmalen);
3682 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3684 /* set up payload segments */
3686 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3688 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3689 desc->sdl1 = htole16(dma_seg[j].ds_len);
3691 ring->desc_queued++;
3692 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3694 j++;
3696 desc = &ring->desc[ring->desc_cur];
3698 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3699 desc->sdl0 = htole16(dma_seg[j].ds_len);
3700 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3702 j++;
3705 /* finalize last payload segment */
3707 if (i > 0)
3709 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3710 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3712 else
3714 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3715 desc->sdl1 = 0;
3718 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3719 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3720 "bw=%d, shortgi=%d, mcs=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3721 device_get_nameunit(sc->dev),
3722 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3723 bw, shortgi, mcs, dmalen, ndmasegs,
3724 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);
3726 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3727 BUS_DMASYNC_PREWRITE);
3728 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3729 BUS_DMASYNC_PREWRITE);
3730 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3731 BUS_DMASYNC_PREWRITE);
3733 ring->desc_queued++;
3734 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3736 ring->data_queued++;
3737 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3739 /* kick Tx */
3741 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3743 return 0;
3747 * rt2860_tx_raw
3749 static int rt2860_tx_raw(struct rt2860_softc *sc,
3750 struct mbuf *m, struct ieee80211_node *ni,
3751 const struct ieee80211_bpf_params *params)
3753 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3754 "%s: Tx raw\n",
3755 device_get_nameunit(sc->dev));
3757 return 0;
3761 * rt2860_intr
3763 static void rt2860_intr(void *arg)
3765 struct rt2860_softc *sc;
3766 struct ifnet *ifp;
3767 uint32_t status;
3769 sc = arg;
3770 ifp = sc->ifp;
3772 /* acknowledge interrupts */
3774 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
3775 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
3777 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3778 "%s: interrupt: status = 0x%08x\n",
3779 device_get_nameunit(sc->dev), status);
3781 if (status == 0xffffffff || /* device likely went away */
3782 status == 0) /* not for us */
3783 return;
3785 sc->interrupts++;
3787 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
3788 return;
3790 if (status & RT2860_REG_INT_TX_COHERENT)
3791 rt2860_tx_coherent_intr(sc);
3793 if (status & RT2860_REG_INT_RX_COHERENT)
3794 rt2860_rx_coherent_intr(sc);
3796 if (status & RT2860_REG_INT_TXRX_COHERENT)
3797 rt2860_txrx_coherent_intr(sc);
3799 if (status & RT2860_REG_INT_FIFO_STA_FULL)
3800 rt2860_fifo_sta_full_intr(sc);
3802 if (status & RT2860_REG_INT_TX_MGMT_DONE)
3803 rt2860_tx_intr(sc, 5);
3805 if (status & RT2860_REG_INT_RX_DONE)
3806 rt2860_rx_intr(sc);
3808 if (status & RT2860_REG_INT_RX_DELAY_DONE)
3809 rt2860_rx_delay_intr(sc);
3811 if (status & RT2860_REG_INT_TX_HCCA_DONE)
3812 rt2860_tx_intr(sc, 4);
3814 if (status & RT2860_REG_INT_TX_AC3_DONE)
3815 rt2860_tx_intr(sc, 3);
3817 if (status & RT2860_REG_INT_TX_AC2_DONE)
3818 rt2860_tx_intr(sc, 2);
3820 if (status & RT2860_REG_INT_TX_AC1_DONE)
3821 rt2860_tx_intr(sc, 1);
3823 if (status & RT2860_REG_INT_TX_AC0_DONE)
3824 rt2860_tx_intr(sc, 0);
3826 if (status & RT2860_REG_INT_TX_DELAY_DONE)
3827 rt2860_tx_delay_intr(sc);
3829 if (status & RT2860_REG_INT_PRE_TBTT)
3830 rt2860_pre_tbtt_intr(sc);
3832 if (status & RT2860_REG_INT_TBTT)
3833 rt2860_tbtt_intr(sc);
3835 if (status & RT2860_REG_INT_MCU_CMD)
3836 rt2860_mcu_cmd_intr(sc);
3838 if (status & RT2860_REG_INT_AUTO_WAKEUP)
3839 rt2860_auto_wakeup_intr(sc);
3841 if (status & RT2860_REG_INT_GP_TIMER)
3842 rt2860_gp_timer_intr(sc);
3846 * rt2860_tx_coherent_intr
3848 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
3850 uint32_t tmp;
3851 int i;
3853 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3854 "%s: Tx coherent interrupt\n",
3855 device_get_nameunit(sc->dev));
3857 sc->tx_coherent_interrupts++;
3859 /* restart DMA engine */
3861 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3863 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3864 RT2860_REG_RX_DMA_ENABLE |
3865 RT2860_REG_TX_DMA_ENABLE);
3867 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3869 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3871 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3872 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3874 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3876 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3877 sc->tx_ring[i].desc_phys_addr);
3878 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3879 RT2860_SOFTC_TX_RING_DESC_COUNT);
3880 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3883 /* init Rx ring */
3885 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3887 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3888 sc->rx_ring.desc_phys_addr);
3889 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3890 RT2860_SOFTC_RX_RING_DATA_COUNT);
3891 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3892 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3894 rt2860_txrx_enable(sc);
3898 * rt2860_rx_coherent_intr
3900 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
3902 uint32_t tmp;
3903 int i;
3905 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3906 "%s: Rx coherent interrupt\n",
3907 device_get_nameunit(sc->dev));
3909 sc->rx_coherent_interrupts++;
3911 /* restart DMA engine */
3913 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3915 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3916 RT2860_REG_RX_DMA_ENABLE |
3917 RT2860_REG_TX_DMA_ENABLE);
3919 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3921 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3923 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3924 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3926 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3928 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3929 sc->tx_ring[i].desc_phys_addr);
3930 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3931 RT2860_SOFTC_TX_RING_DESC_COUNT);
3932 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3935 /* init Rx ring */
3937 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3939 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3940 sc->rx_ring.desc_phys_addr);
3941 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3942 RT2860_SOFTC_RX_RING_DATA_COUNT);
3943 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3944 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3946 rt2860_txrx_enable(sc);
3950 * rt2860_txrx_coherent_intr
3952 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
3954 uint32_t tmp;
3955 int i;
3957 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3958 "%s: Tx/Rx coherent interrupt\n",
3959 device_get_nameunit(sc->dev));
3961 sc->txrx_coherent_interrupts++;
3963 /* restart DMA engine */
3965 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3967 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3968 RT2860_REG_RX_DMA_ENABLE |
3969 RT2860_REG_TX_DMA_ENABLE);
3971 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3973 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3975 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3976 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3978 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3980 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3981 sc->tx_ring[i].desc_phys_addr);
3982 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3983 RT2860_SOFTC_TX_RING_DESC_COUNT);
3984 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3987 /* init Rx ring */
3989 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3991 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3992 sc->rx_ring.desc_phys_addr);
3993 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3994 RT2860_SOFTC_RX_RING_DATA_COUNT);
3995 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3996 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3998 rt2860_txrx_enable(sc);
4002 * rt2860_fifo_sta_full_intr
4004 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4006 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4007 "%s: FIFO statistic full interrupt\n",
4008 device_get_nameunit(sc->dev));
4010 sc->fifo_sta_full_interrupts++;
4012 RT2860_SOFTC_LOCK(sc);
4014 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4016 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4018 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4021 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4023 RT2860_SOFTC_UNLOCK(sc);
4027 * rt2860_rx_intr
4029 static void rt2860_rx_intr(struct rt2860_softc *sc)
4031 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4032 "%s: Rx interrupt\n",
4033 device_get_nameunit(sc->dev));
4035 sc->rx_interrupts++;
4037 RT2860_SOFTC_LOCK(sc);
4039 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4041 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4043 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4046 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4048 RT2860_SOFTC_UNLOCK(sc);
4052 * rt2860_rx_delay_intr
4054 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4056 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4057 "%s: Rx delay interrupt\n",
4058 device_get_nameunit(sc->dev));
4060 sc->rx_delay_interrupts++;
4064 * rt2860_tx_intr
4066 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4068 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4069 ("%s: Tx interrupt: invalid qid=%d\n",
4070 device_get_nameunit(sc->dev), qid));
4072 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4073 "%s: Tx interrupt: qid=%d\n",
4074 device_get_nameunit(sc->dev), qid);
4076 sc->tx_interrupts[qid]++;
4078 RT2860_SOFTC_LOCK(sc);
4080 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4082 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4084 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4087 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4089 RT2860_SOFTC_UNLOCK(sc);
4093 * rt2860_tx_delay_intr
4095 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4097 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4098 "%s: Tx delay interrupt\n",
4099 device_get_nameunit(sc->dev));
4101 sc->tx_delay_interrupts++;
4105 * rt2860_pre_tbtt_intr
4107 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4109 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4110 "%s: Pre-TBTT interrupt\n",
4111 device_get_nameunit(sc->dev));
4113 sc->pre_tbtt_interrupts++;
4117 * rt2860_tbtt_intr
4119 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4121 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4122 "%s: TBTT interrupt\n",
4123 device_get_nameunit(sc->dev));
4125 sc->tbtt_interrupts++;
4129 * rt2860_mcu_cmd_intr
4131 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4133 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4134 "%s: MCU command interrupt\n",
4135 device_get_nameunit(sc->dev));
4137 sc->mcu_cmd_interrupts++;
4141 * rt2860_auto_wakeup_intr
4143 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4145 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4146 "%s: auto wakeup interrupt\n",
4147 device_get_nameunit(sc->dev));
4149 sc->auto_wakeup_interrupts++;
4153 * rt2860_gp_timer_intr
4155 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4157 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4158 "%s: GP timer interrupt\n",
4159 device_get_nameunit(sc->dev));
4161 sc->gp_timer_interrupts++;
4165 * rt2860_rx_done_task
4167 static void rt2860_rx_done_task(void *context, int pending)
4169 struct rt2860_softc *sc;
4170 struct ifnet *ifp;
4171 int again;
4173 sc = context;
4174 ifp = sc->ifp;
4176 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4177 "%s: Rx done task\n",
4178 device_get_nameunit(sc->dev));
4180 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4181 return;
4183 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4185 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4187 RT2860_SOFTC_LOCK(sc);
4189 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4191 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4192 "%s: Rx done task: scheduling again\n",
4193 device_get_nameunit(sc->dev));
4195 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4197 else
4199 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4202 RT2860_SOFTC_UNLOCK(sc);
4206 * rt2860_tx_done_task
4208 static void rt2860_tx_done_task(void *context, int pending)
4210 struct rt2860_softc *sc;
4211 struct ifnet *ifp;
4212 uint32_t intr_mask;
4213 int i;
4215 sc = context;
4216 ifp = sc->ifp;
4218 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4219 "%s: Tx done task\n",
4220 device_get_nameunit(sc->dev));
4222 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4223 return;
4225 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4226 RT2860_REG_INT_TX_HCCA_DONE |
4227 RT2860_REG_INT_TX_AC3_DONE |
4228 RT2860_REG_INT_TX_AC2_DONE |
4229 RT2860_REG_INT_TX_AC1_DONE |
4230 RT2860_REG_INT_TX_AC0_DONE);
4232 sc->intr_pending_mask &= ~intr_mask;
4234 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4235 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4237 sc->tx_timer = 0;
4239 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4241 RT2860_SOFTC_LOCK(sc);
4243 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4244 (sc->intr_disable_mask & intr_mask));
4246 if (sc->intr_pending_mask & intr_mask)
4248 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4249 "%s: Tx done task: scheduling again\n",
4250 device_get_nameunit(sc->dev));
4252 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4255 RT2860_SOFTC_UNLOCK(sc);
4259 * rt2860_fifo_sta_full_task
4261 static void rt2860_fifo_sta_full_task(void *context, int pending)
4263 struct rt2860_softc *sc;
4264 struct ifnet *ifp;
4266 sc = context;
4267 ifp = sc->ifp;
4269 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4270 "%s: FIFO statistic full task\n",
4271 device_get_nameunit(sc->dev));
4273 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4274 return;
4276 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4278 rt2860_drain_fifo_stats(sc);
4280 RT2860_SOFTC_LOCK(sc);
4282 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4284 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4285 "%s: FIFO statistic full task: scheduling again\n",
4286 device_get_nameunit(sc->dev));
4288 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4290 else
4292 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4295 RT2860_SOFTC_UNLOCK(sc);
4299 * rt2860_periodic_task
4301 static void rt2860_periodic_task(void *context, int pending)
4303 struct rt2860_softc *sc;
4304 struct ifnet *ifp;
4305 struct ieee80211com *ic;
4307 sc = context;
4308 ifp = sc->ifp;
4309 ic = &sc->ic;
4311 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4312 "%s: periodic task: round=%lu\n",
4313 device_get_nameunit(sc->dev), sc->periodic_round);
4315 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4316 return;
4318 RT2860_SOFTC_LOCK(sc);
4320 sc->periodic_round++;
4322 rt2860_update_stats(sc);
4324 if ((sc->periodic_round % 10) == 0)
4326 rt2860_update_raw_counters(sc);
4328 rt2860_watchdog(sc);
4330 if (ic->ic_opmode == IEEE80211_M_STA)
4331 rt2860_amrr_update_iter_func(sc, ic->ic_bss);
4332 else
4333 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, sc);
4336 RT2860_SOFTC_UNLOCK(sc);
4338 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4342 * rt2860_rx_eof
4344 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4346 struct ieee80211com *ic;
4347 struct ifnet *ifp;
4348 struct ieee80211_frame *wh;
4349 struct ieee80211_node *ni;
4350 struct rt2860_softc_rx_radiotap_header *tap;
4351 struct rt2860_softc_rx_ring *ring;
4352 struct rt2860_rxdesc *desc;
4353 struct rt2860_softc_rx_data *data;
4354 struct rt2860_rxwi *rxwi;
4355 struct mbuf *m, *mnew;
4356 uint32_t index, desc_flags, phys_addr;
4357 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4358 int8_t rssi_dbm;
4359 int error, len, ampdu, amsdu, nframes;
4361 ic = &sc->ic;
4362 ifp = sc->ifp;
4363 ring = &sc->rx_ring;
4365 nframes = 0;
4367 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4368 BUS_DMASYNC_POSTREAD);
4370 while (limit != 0)
4372 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4373 if (ring->cur == index)
4374 break;
4376 desc = &ring->desc[ring->cur];
4377 desc_flags = le32toh(desc->flags);
4378 data = &ring->data[ring->cur];
4380 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4381 break;
4383 nframes++;
4385 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
4386 if (mnew == NULL)
4388 ifp->if_ierrors++;
4389 goto skip;
4392 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4393 BUS_DMASYNC_POSTREAD);
4394 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4396 error = bus_dmamap_load(ring->data_dma_tag, data->dma_map,
4397 mtod(mnew, void *), MJUM9BYTES,
4398 rt2860_dma_map_addr, &phys_addr, 0);
4399 if (error != 0)
4401 m_freem(mnew);
4403 error = bus_dmamap_load(ring->data_dma_tag, data->dma_map,
4404 mtod(data->m, void *), MJUM9BYTES,
4405 rt2860_dma_map_addr, &phys_addr, 0);
4406 if (error != 0)
4408 panic("%s: could not load old Rx mbuf\n",
4409 device_get_name(sc->dev));
4412 ifp->if_ierrors++;
4414 goto skip;
4417 m = data->m;
4419 data->m = mnew;
4420 desc->sdp0 = htole32(phys_addr);
4422 /* get Rx wireless info */
4424 rxwi = mtod(m, struct rt2860_rxwi *);
4425 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4426 RT2860_RXWI_SIZE_MASK;
4428 /* check for L2 padding between IEEE 802.11 frame header and body */
4430 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4432 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4433 "%s: L2 padding: len=%d\n",
4434 device_get_nameunit(sc->dev), len);
4436 len += 2;
4439 m->m_pkthdr.rcvif = ifp;
4440 m->m_data = (caddr_t) (rxwi + 1);
4441 m->m_pkthdr.len = m->m_len = len;
4443 /* check for crc errors */
4445 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4447 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4448 "%s: rxdesc: crc error\n",
4449 device_get_nameunit(sc->dev));
4451 ifp->if_ierrors++;
4453 if (!(ifp->if_flags & IFF_PROMISC))
4455 m_freem(m);
4456 goto skip;
4460 wh = (struct ieee80211_frame *) (rxwi + 1);
4462 /* check for cipher errors */
4464 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4466 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4467 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4468 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4470 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4471 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4473 m->m_flags |= M_WEP;
4475 else
4477 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4478 "%s: rxdesc: cipher error=0x%02x\n",
4479 device_get_nameunit(sc->dev), cipher_err);
4481 ifp->if_ierrors++;
4483 if (!(ifp->if_flags & IFF_PROMISC))
4485 m_free(m);
4486 goto skip;
4490 else
4492 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4494 ifp->if_ierrors++;
4496 if (!(ifp->if_flags & IFF_PROMISC))
4498 m_free(m);
4499 goto skip;
4504 /* check for A-MPDU */
4506 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
4508 m->m_flags |= M_AMPDU;
4509 ampdu = 1;
4511 else
4513 ampdu = 0;
4516 ant = rt2860_maxrssi_rxpath(sc, rxwi);
4517 rssi = rxwi->rssi[ant];
4518 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
4519 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4520 RT2860_RXWI_PHYMODE_MASK);
4521 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
4522 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
4523 RT2860_RXWI_SHORTGI_MASK);
4524 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
4525 amsdu = (desc_flags & RT2860_RXDESC_FLAGS_AMSDU);
4527 if (bpf_peers_present(sc->drvbpf))
4529 tap = &sc->rxtap;
4531 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4532 tap->dbm_antsignal = rssi_dbm;
4533 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
4534 tap->antenna = ant;
4535 tap->antsignal = rssi;
4536 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4537 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4538 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4539 tap->chan_maxpow = 0;
4541 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
4542 tap->rate = mcs | IEEE80211_RATE_MCS;
4543 else
4544 tap->rate = rt2860_rxrate(rxwi);
4546 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
4547 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4549 if (shortgi)
4550 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4552 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
4555 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4556 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4557 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
4558 device_get_nameunit(sc->dev),
4559 len, phymode, bw, shortgi, mcs,
4560 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
4561 rxwi->snr[0], rxwi->snr[1],
4562 rxwi->wcid, ampdu, amsdu);
4564 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
4566 ieee80211_input(ic, m, ni, rssi_dbm - RT2860_NOISE_FLOOR, RT2860_NOISE_FLOOR, 0);
4568 ieee80211_free_node(ni);
4570 skip:
4572 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
4574 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4575 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4577 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
4579 limit--;
4582 if (ring->cur == 0)
4583 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4584 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4585 else
4586 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4587 ring->cur - 1);
4589 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4590 "%s: Rx eof: nframes=%d\n",
4591 device_get_nameunit(sc->dev), nframes);
4593 sc->rx_packets += nframes;
4595 return (limit == 0);
4599 * rt2860_tx_eof
4601 static void rt2860_tx_eof(struct rt2860_softc *sc,
4602 struct rt2860_softc_tx_ring *ring)
4604 struct ifnet *ifp;
4605 struct rt2860_txdesc *desc;
4606 struct rt2860_softc_tx_data *data;
4607 uint32_t index;
4608 int ndescs, nframes;
4610 ifp = sc->ifp;
4612 ndescs = 0;
4613 nframes = 0;
4615 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4616 BUS_DMASYNC_POSTREAD);
4618 for (;;)
4620 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
4621 if (ring->desc_next == index)
4622 break;
4624 ndescs++;
4626 rt2860_drain_fifo_stats(sc);
4628 desc = &ring->desc[ring->desc_next];
4630 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
4631 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
4633 nframes++;
4635 data = &ring->data[ring->data_next];
4637 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4638 BUS_DMASYNC_POSTWRITE);
4639 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4641 if (data->m->m_flags & M_TXCB)
4642 ieee80211_process_callback(data->ni, data->m, 0);
4644 m_freem(data->m);
4646 ieee80211_free_node(data->ni);
4648 data->m = NULL;
4649 data->ni = NULL;
4651 ifp->if_opackets++;
4653 ring->data_queued--;
4654 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4657 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
4659 ring->desc_queued--;
4660 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4663 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4664 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4666 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4667 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
4668 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
4672 * rt2860_update_stats
4674 static void rt2860_update_stats(struct rt2860_softc *sc)
4676 struct ifnet *ifp;
4677 struct ieee80211com *ic;
4678 struct ieee80211_node *ni;
4679 uint16_t associd;
4680 uint8_t wcid;
4681 uint32_t stacnt[3];
4682 int txcnt, retrycnt, failcnt;
4684 ifp = sc->ifp;
4685 ic = &sc->ic;
4687 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4688 "%s: update statistic\n",
4689 device_get_nameunit(sc->dev));
4691 rt2860_drain_fifo_stats(sc);
4693 if (ic->ic_opmode == IEEE80211_M_STA)
4695 ni = ic->ic_bss;
4697 associd = (ni != NULL) ? ni->ni_associd : 0;
4698 wcid = RT2860_AID2WCID(associd);
4700 /* read and clear Tx statistic registers */
4702 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
4703 stacnt, sizeof(stacnt));
4705 txcnt = le32toh(stacnt[1]) & 0xffff;
4706 retrycnt = le32toh(stacnt[1]) >> 16;
4707 failcnt = le32toh(stacnt[0]) & 0xffff;
4709 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4710 "%s: update statistic: associd=0x%04x, txcnt=%d, retrycnt=%d, failcnt=%d\n",
4711 device_get_nameunit(sc->dev),
4712 associd, txcnt, retrycnt, failcnt);
4714 ifp->if_oerrors += failcnt;
4716 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
4717 txcnt + retrycnt + failcnt, txcnt + retrycnt, retrycnt + failcnt);
4722 * rt2860_watchdog
4724 static void rt2860_watchdog(struct rt2860_softc *sc)
4726 uint32_t tmp;
4727 int ntries;
4729 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4731 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
4732 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4733 device_get_nameunit(sc->dev), tmp);
4735 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
4737 sc->tx_queue_not_empty[0]++;
4739 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
4741 for (ntries = 0; ntries < 10; ntries++)
4743 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4744 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
4745 break;
4747 DELAY(1);
4750 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4753 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
4755 sc->tx_queue_not_empty[1]++;
4757 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
4759 for (ntries = 0; ntries < 10; ntries++)
4761 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4762 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
4763 break;
4765 DELAY(1);
4768 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4773 * rt2860_drain_fifo_stats
4775 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
4777 struct ifnet *ifp;
4778 uint32_t stats;
4779 uint8_t wcid, mcs, pid;
4780 int ok, retrycnt;
4782 ifp = sc->ic.ic_ifp;
4784 /* drain Tx status FIFO (maxsize = 16) */
4786 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
4787 RT2860_REG_TX_STA_FIFO_VALID)
4789 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
4790 RT2860_REG_TX_STA_FIFO_WCID_MASK;
4792 /* if no ACK was requested, no feedback is available */
4794 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
4795 continue;
4797 /* update AMRR statistic */
4799 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
4800 retrycnt = 0;
4802 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
4803 RT2860_REG_TX_STA_FIFO_MCS_MASK;
4804 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
4805 RT2860_REG_TX_STA_FIFO_PID_MASK;
4807 if (!ok || (mcs + 1) != pid)
4808 retrycnt++;
4810 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
4812 if (!ok)
4813 ifp->if_oerrors++;
4818 * rt2860_update_raw_counters
4820 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
4822 uint32_t tmp;
4824 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
4826 sc->rx_crc_errors += tmp & 0xffff;
4827 sc->rx_phy_errors += tmp >> 16;
4829 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
4831 sc->rx_false_ccas += tmp & 0xffff;
4832 sc->rx_plcp_errors += tmp >> 16;
4834 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
4836 sc->rx_dup_packets += tmp & 0xffff;
4837 sc->rx_fifo_overflows += tmp >> 16;
4841 * rt2860_intr_enable
4843 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
4845 uint32_t tmp;
4847 sc->intr_disable_mask &= ~intr_mask;
4849 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4851 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4855 * rt2860_intr_disable
4857 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
4859 uint32_t tmp;
4861 sc->intr_disable_mask |= intr_mask;
4863 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4865 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4869 * rt2860_txrx_enable
4871 static int rt2860_txrx_enable(struct rt2860_softc *sc)
4873 struct ieee80211com *ic;
4874 struct ifnet *ifp;
4875 uint32_t tmp;
4876 int ntries;
4878 ic = &sc->ic;
4879 ifp = ic->ic_ifp;
4881 /* enable Tx/Rx DMA engine */
4883 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
4885 for (ntries = 0; ntries < 200; ntries++)
4887 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4888 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
4889 break;
4891 DELAY(1000);
4894 if (ntries == 200)
4896 printf("%s: timeout waiting for DMA engine\n",
4897 device_get_nameunit(sc->dev));
4898 return -1;
4901 DELAY(50);
4903 tmp |= RT2860_REG_TX_WB_DDONE |
4904 RT2860_REG_RX_DMA_ENABLE |
4905 RT2860_REG_TX_DMA_ENABLE |
4906 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
4908 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4910 /* set Rx filter */
4912 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
4913 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
4915 if (ic->ic_opmode != IEEE80211_M_MONITOR)
4917 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
4918 RT2860_REG_RX_FILTER_DROP_CTS |
4919 RT2860_REG_RX_FILTER_DROP_BA |
4920 RT2860_REG_RX_FILTER_DROP_ACK |
4921 RT2860_REG_RX_FILTER_DROP_VER_ERR |
4922 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
4923 RT2860_REG_RX_FILTER_DROP_CFACK |
4924 RT2860_REG_RX_FILTER_DROP_CFEND;
4926 if (ic->ic_opmode == IEEE80211_M_STA)
4927 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
4928 RT2860_REG_RX_FILTER_DROP_PSPOLL;
4930 if (!(ifp->if_flags & IFF_PROMISC))
4931 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
4934 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
4936 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
4937 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
4939 return 0;
4943 * rt2860_alloc_rx_ring
4945 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
4946 struct rt2860_softc_rx_ring *ring)
4948 struct rt2860_rxdesc *desc;
4949 struct rt2860_softc_rx_data *data;
4950 bus_addr_t phys_addr;
4951 int i, error;
4953 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
4954 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4955 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
4956 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
4957 0, NULL, NULL, &ring->desc_dma_tag);
4958 if (error != 0)
4960 printf("%s: could not create Rx desc DMA tag\n",
4961 device_get_nameunit(sc->dev));
4962 goto fail;
4965 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
4966 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
4967 if (error != 0)
4969 printf("%s: could not allocate Rx desc DMA memory\n",
4970 device_get_nameunit(sc->dev));
4971 goto fail;
4974 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
4975 ring->desc,
4976 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
4977 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
4978 if (error != 0)
4980 printf("%s: could not load Rx desc DMA map\n",
4981 device_get_nameunit(sc->dev));
4982 goto fail;
4985 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
4986 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4987 MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
4988 &ring->data_dma_tag);
4989 if (error != 0)
4991 printf("%s: could not create Rx data DMA tag\n",
4992 device_get_nameunit(sc->dev));
4993 goto fail;
4996 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
4998 desc = &ring->desc[i];
4999 data = &ring->data[i];
5001 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5002 if (error != 0)
5004 printf("%s: could not create Rx data DMA map\n",
5005 device_get_nameunit(sc->dev));
5006 goto fail;
5009 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
5010 if (data->m == NULL)
5012 printf("%s: could not allocate Rx mbuf\n",
5013 device_get_nameunit(sc->dev));
5014 error = ENOMEM;
5015 goto fail;
5018 error = bus_dmamap_load(ring->data_dma_tag, data->dma_map,
5019 mtod(data->m, void *), MJUM9BYTES,
5020 rt2860_dma_map_addr, &phys_addr, 0);
5021 if (error != 0)
5023 printf("%s: could not load Rx mbuf DMA map\n",
5024 device_get_nameunit(sc->dev));
5025 goto fail;
5028 desc->sdp0 = htole32(phys_addr);
5029 desc->sdl0 = htole16(MJUM9BYTES);
5032 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5033 BUS_DMASYNC_PREWRITE);
5035 return 0;
5037 fail:
5039 rt2860_free_rx_ring(sc, ring);
5041 return error;
5045 * rt2860_reset_rx_ring
5047 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5048 struct rt2860_softc_rx_ring *ring)
5050 struct rt2860_rxdesc *desc;
5051 int i;
5053 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5055 desc = &ring->desc[i];
5057 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5060 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5061 BUS_DMASYNC_PREWRITE);
5063 ring->cur = 0;
5067 * rt2860_free_rx_ring
5069 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5070 struct rt2860_softc_rx_ring *ring)
5072 struct rt2860_softc_rx_data *data;
5073 int i;
5075 if (ring->desc != NULL)
5077 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5078 BUS_DMASYNC_POSTWRITE);
5079 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5080 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5081 ring->desc_dma_map);
5084 if (ring->desc_dma_tag != NULL)
5085 bus_dma_tag_destroy(ring->desc_dma_tag);
5087 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5089 data = &ring->data[i];
5091 if (data->m != NULL)
5093 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5094 BUS_DMASYNC_POSTREAD);
5095 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5096 m_freem(data->m);
5099 if (data->dma_map != NULL)
5100 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5103 if (ring->data_dma_tag != NULL)
5104 bus_dma_tag_destroy(ring->data_dma_tag);
5108 * rt2860_alloc_tx_ring
5110 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5111 struct rt2860_softc_tx_ring *ring, int qid)
5113 struct rt2860_softc_tx_data *data;
5114 int error, i;
5116 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5117 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5118 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5119 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5120 0, NULL, NULL, &ring->desc_dma_tag);
5121 if (error != 0)
5123 printf("%s: could not create Tx desc DMA tag\n",
5124 device_get_nameunit(sc->dev));
5125 goto fail;
5128 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5129 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5130 if (error != 0)
5132 printf("%s: could not allocate Tx desc DMA memory\n",
5133 device_get_nameunit(sc->dev));
5134 goto fail;
5137 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5138 ring->desc,
5139 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5140 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5141 if (error != 0)
5143 printf("%s: could not load Tx desc DMA map\n",
5144 device_get_nameunit(sc->dev));
5145 goto fail;
5148 ring->desc_queued = 0;
5149 ring->desc_cur = 0;
5150 ring->desc_next = 0;
5152 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5153 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5154 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5155 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5156 0, NULL, NULL, &ring->seg0_dma_tag);
5157 if (error != 0)
5159 printf("%s: could not create Tx seg0 DMA tag\n",
5160 device_get_nameunit(sc->dev));
5161 goto fail;
5164 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5165 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5166 if (error != 0)
5168 printf("%s: could not allocate Tx seg0 DMA memory\n",
5169 device_get_nameunit(sc->dev));
5170 goto fail;
5173 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5174 ring->seg0,
5175 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5176 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5177 if (error != 0)
5179 printf("%s: could not load Tx seg0 DMA map\n",
5180 device_get_nameunit(sc->dev));
5181 goto fail;
5184 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5185 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5186 MJUM9BYTES, RT2860_SOFTC_MAX_SCATTER, MJUM9BYTES, 0, NULL, NULL,
5187 &ring->data_dma_tag);
5188 if (error != 0)
5190 printf("%s: could not create Tx data DMA tag\n",
5191 device_get_nameunit(sc->dev));
5192 goto fail;
5195 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5197 data = &ring->data[i];
5199 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5200 if (error != 0)
5202 printf("%s: could not create Tx data DMA map\n",
5203 device_get_nameunit(sc->dev));
5204 goto fail;
5208 ring->data_queued = 0;
5209 ring->data_cur = 0;
5210 ring->data_next = 0;
5212 ring->qid = qid;
5214 return 0;
5216 fail:
5218 rt2860_free_tx_ring(sc, ring);
5220 return error;
5224 * rt2860_reset_tx_ring
5226 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5227 struct rt2860_softc_tx_ring *ring)
5229 struct rt2860_softc_tx_data *data;
5230 struct rt2860_txdesc *desc;
5231 int i;
5233 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5235 desc = &ring->desc[i];
5237 desc->sdl0 = 0;
5238 desc->sdl1 = 0;
5241 ring->desc_queued = 0;
5242 ring->desc_cur = 0;
5243 ring->desc_next = 0;
5245 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5246 BUS_DMASYNC_PREWRITE);
5248 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5249 BUS_DMASYNC_PREWRITE);
5251 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5253 data = &ring->data[i];
5255 if (data->m != NULL)
5257 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5258 BUS_DMASYNC_POSTWRITE);
5259 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5260 m_freem(data->m);
5261 data->m = NULL;
5264 if (data->ni != NULL)
5266 ieee80211_free_node(data->ni);
5267 data->ni = NULL;
5271 ring->data_queued = 0;
5272 ring->data_cur = 0;
5273 ring->data_next = 0;
5277 * rt2860_free_tx_ring
5279 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5280 struct rt2860_softc_tx_ring *ring)
5282 struct rt2860_softc_tx_data *data;
5283 int i;
5285 if (ring->desc != NULL)
5287 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5288 BUS_DMASYNC_POSTWRITE);
5289 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5290 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5291 ring->desc_dma_map);
5294 if (ring->desc_dma_tag != NULL)
5295 bus_dma_tag_destroy(ring->desc_dma_tag);
5297 if (ring->seg0 != NULL)
5299 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5300 BUS_DMASYNC_POSTWRITE);
5301 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5302 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5303 ring->seg0_dma_map);
5306 if (ring->seg0_dma_tag != NULL)
5307 bus_dma_tag_destroy(ring->seg0_dma_tag);
5309 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5311 data = &ring->data[i];
5313 if (data->m != NULL)
5315 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5316 BUS_DMASYNC_POSTWRITE);
5317 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5318 m_freem(data->m);
5321 if (data->ni != NULL)
5322 ieee80211_free_node(data->ni);
5324 if (data->dma_map != NULL)
5325 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5328 if (ring->data_dma_tag != NULL)
5329 bus_dma_tag_destroy(ring->data_dma_tag);
5333 * rt2860_dma_map_addr
5335 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5336 int nseg, int error)
5338 if (error != 0)
5339 return;
5341 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5343 *(bus_addr_t *) arg = segs[0].ds_addr;
5347 * rt2860_sysctl_attach
5349 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5351 struct sysctl_ctx_list *ctx;
5352 struct sysctl_oid *tree;
5353 struct sysctl_oid *stats;
5355 ctx = device_get_sysctl_ctx(sc->dev);
5356 tree = device_get_sysctl_tree(sc->dev);
5358 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5359 "stats", CTLFLAG_RD, 0, "statistic");
5361 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5362 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5363 "all interrupts");
5365 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5366 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5367 "Tx coherent interrupts");
5369 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5370 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5371 "Rx coherent interrupts");
5373 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5374 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5375 "Tx/Rx coherent interrupts");
5377 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5378 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5379 "FIFO statistic full interrupts");
5381 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5382 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5383 "Rx interrupts");
5385 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5386 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5387 "Rx delay interrupts");
5389 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5390 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5391 "Tx MGMT interrupts");
5393 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5394 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5395 "Tx HCCA interrupts");
5397 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5398 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5399 "Tx AC3 interrupts");
5401 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5402 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5403 "Tx AC2 interrupts");
5405 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5406 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5407 "Tx AC1 interrupts");
5409 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5410 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5411 "Tx AC0 interrupts");
5413 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5414 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
5415 "Tx delay interrupts");
5417 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5418 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
5419 "Pre-TBTT interrupts");
5421 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5422 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
5423 "TBTT interrupts");
5425 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5426 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
5427 "MCU command interrupts");
5429 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5430 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
5431 "auto wakeup interrupts");
5433 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5434 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
5435 "GP timer interrupts");
5437 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5438 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
5439 "Tx MGMT descriptors queued");
5441 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5442 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
5443 "Tx MGMT data queued");
5445 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5446 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
5447 "Tx HCCA descriptors queued");
5449 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5450 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
5451 "Tx HCCA data queued");
5453 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5454 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
5455 "Tx AC3 descriptors queued");
5457 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5458 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
5459 "Tx AC3 data queued");
5461 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5462 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
5463 "Tx AC2 descriptors queued");
5465 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5466 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
5467 "Tx AC2 data queued");
5469 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5470 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
5471 "Tx AC1 descriptors queued");
5473 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5474 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
5475 "Tx AC1 data queued");
5477 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5478 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
5479 "Tx AC0 descriptors queued");
5481 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5482 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
5483 "Tx AC0 data queued");
5485 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5486 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5487 "Tx watchdog timeouts");
5489 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5490 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
5491 "Tx defragmented packets");
5493 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5494 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
5495 "no Tx descriptors available");
5497 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5498 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5499 "Tx queue 0 not empty");
5501 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5502 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5503 "Tx queue 1 not empty");
5505 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5506 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
5507 "Rx packets");
5509 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5510 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
5511 "Rx CRC errors");
5513 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5514 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
5515 "Rx PHY errors");
5517 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5518 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
5519 "Rx false CCAs");
5521 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5522 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
5523 "Rx PLCP errors");
5525 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5526 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
5527 "Rx duplicate packets");
5529 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5530 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
5531 "Rx FIFO overflows");
5534 static device_method_t rt2860_dev_methods[] =
5536 DEVMETHOD(device_probe, rt2860_probe),
5537 DEVMETHOD(device_attach, rt2860_attach),
5538 DEVMETHOD(device_detach, rt2860_detach),
5539 DEVMETHOD(device_shutdown, rt2860_shutdown),
5540 DEVMETHOD(device_suspend, rt2860_suspend),
5541 DEVMETHOD(device_resume, rt2860_resume),
5542 { 0, 0 }
5545 static driver_t rt2860_driver =
5547 "rt2860",
5548 rt2860_dev_methods,
5549 sizeof(struct rt2860_softc)
5552 static devclass_t rt2860_dev_class;
5554 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
5556 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
5557 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);