Fixed the problem with HT40- channels
[ralink_drivers/rt2860_fbsd72.git] / rt2860.c
blob90850ac040d1c0316b758fc209911166a273dd63
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 rt2860_rf_set_chan(sc, ni->ni_chan);
1855 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1857 rt2860_asic_enable_mrr(sc);
1858 rt2860_asic_set_txpreamble(sc);
1859 rt2860_asic_set_basicrates(sc);
1860 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1863 if (ic->ic_opmode == IEEE80211_M_STA)
1864 rt2860_newassoc(ni, 1);
1866 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1867 ic->ic_opmode == IEEE80211_M_IBSS)
1869 error = rt2860_asic_update_beacon(sc);
1870 if (error != 0)
1871 break;
1874 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1875 rt2860_asic_enable_tsf_sync(sc);
1877 /* turn link LED on */
1879 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1881 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1882 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1883 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
1885 break;
1887 default:
1888 break;
1891 RT2860_SOFTC_UNLOCK(sc);
1893 return error;
1897 * rt2860_scan_start
1899 static void rt2860_scan_start(struct ieee80211com *ic)
1901 struct rt2860_softc *sc;
1902 struct ifnet *ifp;
1904 ifp = ic->ic_ifp;
1905 sc = ifp->if_softc;
1907 rt2860_asic_disable_tsf_sync(sc);
1911 * rt2860_scan_end
1913 static void rt2860_scan_end(struct ieee80211com *ic)
1915 struct rt2860_softc *sc;
1916 struct ifnet *ifp;
1918 ifp = ic->ic_ifp;
1919 sc = ifp->if_softc;
1921 rt2860_asic_enable_tsf_sync(sc);
1925 * rt2860_set_channel
1927 static void rt2860_set_channel(struct ieee80211com *ic)
1929 struct rt2860_softc *sc;
1930 struct ifnet *ifp;
1932 ifp = ic->ic_ifp;
1933 sc = ifp->if_softc;
1935 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
1936 "%s: set channel: channel=%u, HT%s%s\n",
1937 device_get_nameunit(sc->dev),
1938 ieee80211_chan2ieee(ic, ic->ic_curchan),
1939 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
1940 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
1941 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
1942 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
1944 RT2860_SOFTC_LOCK(sc);
1946 rt2860_rf_set_chan(sc, ic->ic_curchan);
1948 RT2860_SOFTC_UNLOCK(sc);
1952 * rt2860_newassoc
1954 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
1956 struct rt2860_softc *sc;
1957 struct ieee80211com *ic;
1958 struct ifnet *ifp;
1959 uint16_t associd;
1960 uint8_t wcid;
1962 ic = ni->ni_ic;
1963 ifp = ic->ic_ifp;
1964 sc = ifp->if_softc;
1966 associd = (ni != NULL) ? ni->ni_associd : 0;
1967 wcid = RT2860_AID2WCID(associd);
1969 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
1970 "%s: new association: wcid=0x%02x, "
1971 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
1972 device_get_nameunit(sc->dev), wcid,
1973 ether_sprintf(ni->ni_macaddr),
1974 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
1975 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
1976 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
1978 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
1979 ni->ni_macaddr, IEEE80211_ADDR_LEN);
1981 rt2860_amrr_node_init(&sc->amrr, &sc->amrr_node[wcid], ni);
1983 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
1984 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
1985 device_get_nameunit(sc->dev),
1986 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
1987 ni->ni_associd,
1988 (ni->ni_flags & IEEE80211_NODE_HT) ?
1989 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
1990 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
1991 (ni->ni_flags & IEEE80211_NODE_HT) ?
1992 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
1993 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
1995 rt2860_asic_updateprot(sc);
1996 rt2860_asic_updateslot(sc);
1997 rt2860_asic_set_txpreamble(sc);
2001 * rt2860_updateslot
2003 static void rt2860_updateslot(struct ifnet *ifp)
2005 struct rt2860_softc *sc;
2007 sc = ifp->if_softc;
2009 rt2860_asic_updateslot(sc);
2013 * rt2860_wme_update
2015 static int rt2860_wme_update(struct ieee80211com *ic)
2017 struct rt2860_softc *sc;
2018 struct ifnet *ifp;
2020 ifp = ic->ic_ifp;
2021 sc = ifp->if_softc;
2023 rt2860_asic_wme_update(sc);
2025 return 0;
2029 * rt2860_update_beacon
2031 static void rt2860_update_beacon(struct ieee80211com *ic, int what)
2033 struct rt2860_softc *sc;
2034 struct ifnet *ifp;
2036 ifp = ic->ic_ifp;
2037 sc = ifp->if_softc;
2039 rt2860_asic_update_beacon(sc);
2043 * rt2860_key_update_begin
2045 static void rt2860_key_update_begin(struct ieee80211com *ic)
2047 struct rt2860_softc *sc;
2048 struct ifnet *ifp;
2050 ifp = ic->ic_ifp;
2051 sc = ifp->if_softc;
2053 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2054 "%s: key update begin\n",
2055 device_get_nameunit(sc->dev));
2057 taskqueue_block(sc->taskqueue);
2059 IF_LOCK(&ifp->if_snd);
2063 * rt2860_key_update_end
2065 static void rt2860_key_update_end(struct ieee80211com *ic)
2067 struct rt2860_softc *sc;
2068 struct ifnet *ifp;
2070 ifp = ic->ic_ifp;
2071 sc = ifp->if_softc;
2073 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2074 "%s: key update end\n",
2075 device_get_nameunit(sc->dev));
2077 IF_UNLOCK(&ifp->if_snd);
2079 taskqueue_unblock(sc->taskqueue);
2083 * rt2860_key_set
2085 static int rt2860_key_set(struct ieee80211com *ic,
2086 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2088 struct rt2860_softc *sc;
2089 struct ifnet *ifp;
2090 struct ieee80211_node *ni;
2091 uint16_t associd, key_base, keymode_base;
2092 uint8_t mode, vapid, wcid, iv[8];
2093 uint32_t tmp;
2095 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2096 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2097 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2098 return EINVAL;
2100 ifp = ic->ic_ifp;
2101 sc = ifp->if_softc;
2103 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2104 ni = ic->ic_bss;
2105 else
2106 ni = ieee80211_find_node(&ic->ic_sta, mac);
2108 associd = (ni != NULL) ? ni->ni_associd : 0;
2110 if ((ic->ic_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2111 ieee80211_free_node(ni);
2113 switch (k->wk_cipher->ic_cipher)
2115 case IEEE80211_CIPHER_WEP:
2116 if(k->wk_keylen < 8)
2117 mode = RT2860_REG_CIPHER_MODE_WEP40;
2118 else
2119 mode = RT2860_REG_CIPHER_MODE_WEP104;
2120 break;
2122 case IEEE80211_CIPHER_TKIP:
2123 mode = RT2860_REG_CIPHER_MODE_TKIP;
2124 break;
2126 case IEEE80211_CIPHER_AES_CCM:
2127 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2128 break;
2130 default:
2131 return EINVAL;
2134 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2135 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2136 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2137 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2139 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2141 /* install pairwise key */
2143 vapid = 0;
2144 wcid = RT2860_AID2WCID(associd);
2145 key_base = RT2860_REG_PKEY(wcid);
2147 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2149 memset(iv, 0, 8);
2151 iv[3] = (k->wk_keyix << 6);
2153 else
2155 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2157 iv[0] = (k->wk_keytsc >> 8);
2158 iv[1] = ((iv[0] | 0x20) & 0x7f);
2159 iv[2] = k->wk_keytsc;
2161 else
2163 /* AES CCMP */
2165 iv[0] = k->wk_keytsc;
2166 iv[1] = k->wk_keytsc >> 8;
2167 iv[2] = 0;
2170 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2171 iv[4] = (k->wk_keytsc >> 16);
2172 iv[5] = (k->wk_keytsc >> 24);
2173 iv[6] = (k->wk_keytsc >> 32);
2174 iv[7] = (k->wk_keytsc >> 40);
2176 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2177 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2178 device_get_nameunit(sc->dev),
2179 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2182 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2184 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2186 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2188 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2190 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2191 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2193 else
2195 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2196 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2199 else
2201 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2204 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2205 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2207 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2210 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2211 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2213 /* install group key */
2215 vapid = 0;
2216 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2217 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2219 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2221 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2223 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2225 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2226 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2228 else
2230 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2231 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2234 else
2236 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2239 tmp = rt2860_io_mac_read(sc, keymode_base);
2241 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2242 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2244 rt2860_io_mac_write(sc, keymode_base, tmp);
2247 return 1;
2251 * rt2860_key_delete
2253 static int rt2860_key_delete(struct ieee80211com *ic,
2254 const struct ieee80211_key *k)
2256 struct rt2860_softc *sc;
2257 struct ieee80211_node *ni;
2258 uint16_t associd;
2259 uint8_t vapid, wcid;
2260 uint32_t tmp;
2262 sc = ic->ic_ifp->if_softc;
2263 ni = ic->ic_bss;
2264 associd = (ni != NULL) ? ni->ni_associd : 0;
2266 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2267 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2268 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2269 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2271 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2273 /* remove pairwise key */
2275 wcid = RT2860_AID2WCID(associd);
2277 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID_ATTR(wcid));
2279 tmp &= ~0xf;
2280 tmp |= (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT);
2282 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2284 else
2286 /* remove group key */
2288 vapid = 0;
2290 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2292 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2293 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2295 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2298 return 1;
2302 * rt2860_raw_xmit
2304 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2305 const struct ieee80211_bpf_params *params)
2307 return 0;
2311 * rt2860_media_change
2313 static int rt2860_media_change(struct ifnet *ifp)
2315 struct rt2860_softc *sc;
2316 int error;
2318 sc = ifp->if_softc;
2320 error = ieee80211_media_change(ifp);
2321 if (error != ENETRESET)
2322 return error;
2324 RT2860_SOFTC_LOCK(sc);
2326 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2328 rt2860_stop_locked(sc);
2329 rt2860_init_locked(sc);
2332 RT2860_SOFTC_UNLOCK(sc);
2334 return 0;
2338 * rt2860_recv_action
2340 static void rt2860_recv_action(struct ieee80211_node *ni,
2341 const uint8_t *frm, const uint8_t *efrm)
2343 struct rt2860_softc *sc;
2344 struct ieee80211com *ic;
2345 struct ifnet *ifp;
2346 const struct ieee80211_action *ia;
2347 uint16_t associd, baparamset;
2348 uint8_t wcid;
2349 int tid;
2350 uint32_t tmp;
2352 ic = ni->ni_ic;
2353 ifp = ic->ic_ifp;
2354 sc = ifp->if_softc;
2356 ia = (const struct ieee80211_action *) frm;
2358 sc->recv_action(ni, frm, efrm);
2360 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2361 return;
2363 associd = (ni != NULL) ? ni->ni_associd : 0;
2364 wcid = RT2860_AID2WCID(associd);
2366 switch (ia->ia_action)
2368 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2369 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2370 baparamset = LE_READ_2(frm + 3);
2371 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2373 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2374 "%s: adding A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2375 device_get_nameunit(sc->dev), associd, tid);
2377 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2379 tmp |= (0x10000 << tid);
2381 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2382 break;
2384 /* IEEE80211_ACTION_BA_DELBA */
2385 case IEEE80211_ACTION_BA_DELBA:
2386 baparamset = LE_READ_2(frm + 2);
2387 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2389 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2390 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2391 device_get_nameunit(sc->dev), associd, tid);
2393 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2395 tmp &= ~(0x10000 << tid);
2397 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2398 break;
2403 * rt2860_send_action
2405 static int rt2860_send_action(struct ieee80211_node *ni,
2406 int category, int action, uint16_t args[4])
2408 struct rt2860_softc *sc;
2409 struct ieee80211com *ic;
2410 struct ifnet *ifp;
2411 uint16_t associd, baparamset;
2412 uint8_t wcid;
2413 int ret, tid;
2414 uint32_t tmp;
2416 ic = ni->ni_ic;
2417 ifp = ic->ic_ifp;
2418 sc = ifp->if_softc;
2420 ret = sc->send_action(ni, category, action, args);
2422 if (category != IEEE80211_ACTION_CAT_BA)
2423 return ret;
2425 associd = (ni != NULL) ? ni->ni_associd : 0;
2426 wcid = RT2860_AID2WCID(associd);
2428 switch (action)
2430 /* IEEE80211_ACTION_BA_DELBA */
2431 case IEEE80211_ACTION_BA_DELBA:
2432 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) |
2433 RT2860_SM(args[1], IEEE80211_DELBAPS_INIT);
2435 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) == IEEE80211_DELBAPS_INIT)
2436 break;
2438 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2440 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2441 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2442 device_get_nameunit(sc->dev), associd, tid);
2444 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2446 tmp &= ~(0x10000 << tid);
2448 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2449 break;
2452 return ret;
2456 * rt2860_amrr_update_iter_func
2458 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2460 struct rt2860_softc *sc;
2461 struct ieee80211com *ic;
2462 uint8_t wcid;
2464 sc = arg;
2465 ic = &sc->ic;
2467 /* only associated stations */
2469 if (ni->ni_associd != 0)
2471 wcid = RT2860_AID2WCID(ni->ni_associd);
2473 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2475 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2476 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2477 device_get_nameunit(sc->dev),
2478 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2479 ni->ni_associd,
2480 (ni->ni_flags & IEEE80211_NODE_HT) ?
2481 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2482 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2483 (ni->ni_flags & IEEE80211_NODE_HT) ?
2484 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2485 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2490 * rt2860_periodic
2492 static void rt2860_periodic(void *arg)
2494 struct rt2860_softc *sc;
2496 sc = arg;
2498 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2499 "%s: periodic\n",
2500 device_get_nameunit(sc->dev));
2502 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2506 * rt2860_tx_watchdog
2508 static void rt2860_tx_watchdog(void *arg)
2510 struct rt2860_softc *sc;
2511 struct ifnet *ifp;
2513 sc = arg;
2514 ifp = sc->ifp;
2516 if (sc->tx_timer == 0)
2517 return;
2519 if (--sc->tx_timer == 0)
2521 printf("%s: Tx watchdog timeout: resetting\n",
2522 device_get_nameunit(sc->dev));
2524 rt2860_stop_locked(sc);
2525 rt2860_init_locked(sc);
2527 ifp->if_oerrors++;
2529 sc->tx_watchdog_timeouts++;
2532 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2536 * rt2860_asic_set_bssid
2538 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2539 const uint8_t *bssid)
2541 uint32_t tmp;
2543 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2544 "%s: set bssid: bssid=%s\n",
2545 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2547 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2549 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2551 tmp = bssid[4] | (bssid[5] << 8);
2553 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2557 * rt2860_asic_set_macaddr
2559 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2560 const uint8_t *addr)
2562 uint32_t tmp;
2564 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2566 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2568 tmp = addr[4] | (addr[5] << 8);
2570 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2574 * rt2860_asic_enable_tsf_sync
2576 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2578 struct ieee80211com *ic;
2579 uint32_t tmp;
2581 ic = &sc->ic;
2583 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2584 "%s: enabling TSF\n",
2585 device_get_nameunit(sc->dev));
2587 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2589 tmp &= ~0x1fffff;
2590 tmp |= ic->ic_bss->ni_intval * 16;
2591 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2593 if (ic->ic_opmode == IEEE80211_M_STA)
2595 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2597 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2599 tmp |= RT2860_REG_BCN_TX_ENABLE;
2600 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2602 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2604 tmp |= RT2860_REG_BCN_TX_ENABLE;
2605 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2608 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2612 * rt2860_asic_disable_tsf_sync
2614 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2616 uint32_t tmp;
2618 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2619 "%s: disabling TSF\n",
2620 device_get_nameunit(sc->dev));
2622 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2624 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2625 RT2860_REG_TSF_TIMER_ENABLE |
2626 RT2860_REG_TBTT_TIMER_ENABLE);
2628 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2629 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2631 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2635 * rt2860_asic_enable_mrr
2637 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2639 #define CCK(mcs) (mcs)
2640 #define OFDM(mcs) ((1 << 3) | (mcs))
2641 #define HT(mcs) (mcs)
2643 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2644 (OFDM(6) << 28) | /* 54 -> 48 */
2645 (OFDM(5) << 24) | /* 48 -> 36 */
2646 (OFDM(4) << 20) | /* 36 -> 24 */
2647 (OFDM(3) << 16) | /* 24 -> 18 */
2648 (OFDM(2) << 12) | /* 18 -> 12 */
2649 (OFDM(1) << 8) | /* 12 -> 9 */
2650 (OFDM(0) << 4) | /* 9 -> 6 */
2651 OFDM(0)); /* 6 -> 6 */
2653 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2654 (CCK(2) << 12) | /* 11 -> 5.5 */
2655 (CCK(1) << 8) | /* 5.5 -> 2 */
2656 (CCK(0) << 4) | /* 2 -> 1 */
2657 CCK(0)); /* 1 -> 1 */
2659 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2660 (HT(6) << 28) |
2661 (HT(5) << 24) |
2662 (HT(4) << 20) |
2663 (HT(3) << 16) |
2664 (HT(2) << 12) |
2665 (HT(1) << 8) |
2666 (HT(0) << 4) |
2667 HT(0));
2669 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2670 (HT(14) << 28) |
2671 (HT(13) << 24) |
2672 (HT(12) << 20) |
2673 (HT(11) << 16) |
2674 (HT(10) << 12) |
2675 (HT(9) << 8) |
2676 (HT(8) << 4) |
2677 HT(8));
2679 #undef HT
2680 #undef OFDM
2681 #undef CCK
2685 * rt2860_asic_set_txpreamble
2687 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2689 struct ieee80211com *ic;
2690 uint32_t tmp;
2692 ic = &sc->ic;
2694 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2695 "%s: %s short Tx preamble\n",
2696 device_get_nameunit(sc->dev),
2697 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2699 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2701 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2703 if (sc->ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2704 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2706 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2710 * rt2860_asic_set_basicrates
2712 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2714 struct ieee80211com *ic;
2716 ic = &sc->ic;
2718 if (ic->ic_curmode == IEEE80211_MODE_11B)
2719 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2720 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2721 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2722 else
2723 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2727 * rt2860_asic_update_rtsthreshold
2729 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2731 struct ieee80211com *ic;
2732 uint32_t tmp;
2733 uint16_t threshold;
2735 ic = &sc->ic;
2737 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2738 "%s: updating RTS threshold: %d\n",
2739 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
2741 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2743 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2745 threshold = (ic->ic_rtsthreshold < IEEE80211_RTS_MAX) ?
2746 ic->ic_rtsthreshold : 0x1000;
2748 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2749 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2751 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
2755 * rt2860_asic_update_txpower
2757 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
2759 struct ieee80211com *ic;
2760 uint32_t *txpow_rate;
2761 int8_t delta;
2762 uint8_t val;
2763 uint32_t tmp;
2764 int i;
2766 ic = &sc->ic;
2768 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2769 "%s: updating Tx power: %d\n",
2770 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
2772 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
2774 txpow_rate = sc->txpow_rate_20mhz;
2776 else
2778 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2779 txpow_rate = sc->txpow_rate_40mhz_2ghz;
2780 else
2781 txpow_rate = sc->txpow_rate_40mhz_5ghz;
2784 delta = 0;
2786 val = rt2860_io_bbp_read(sc, 1);
2787 val &= 0xfc;
2789 if (ic->ic_txpowlimit > 90)
2791 /* do nothing */
2793 else if (ic->ic_txpowlimit > 60)
2795 delta -= 1;
2797 else if (ic->ic_txpowlimit > 30)
2799 delta -= 3;
2801 else if (ic->ic_txpowlimit > 15)
2803 val |= 0x1;
2805 else if (ic->ic_txpowlimit > 9)
2807 val |= 0x1;
2808 delta -= 3;
2810 else
2812 val |= 0x2;
2815 rt2860_io_bbp_write(sc, 1, val);
2817 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
2819 if (txpow_rate[i] == 0xffffffff)
2820 continue;
2822 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
2824 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
2829 * rt2860_asic_update_promisc
2831 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
2833 struct ifnet *ifp;
2834 uint32_t tmp;
2836 ifp = sc->ic.ic_ifp;
2838 printf("%s: %s promiscuous mode\n",
2839 device_get_nameunit(sc->dev),
2840 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
2842 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
2844 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
2846 if (!(ifp->if_flags & IFF_PROMISC))
2847 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
2849 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
2853 * rt2860_asic_updateprot
2855 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
2857 struct ieee80211com *ic;
2858 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
2859 uint8_t htopmode;
2861 ic = &sc->ic;
2863 /* CCK frame protection */
2865 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2866 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
2868 /* set up protection frame phy mode and rate (MCS code) */
2870 if (ic->ic_curmode == IEEE80211_MODE_11A)
2871 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2872 (0 << RT2860_REG_PROT_MCS_SHIFT);
2873 else
2874 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2875 (3 << RT2860_REG_PROT_MCS_SHIFT));
2877 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
2879 /* OFDM frame protection */
2881 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2882 RT2860_REG_TXOP_ALLOW_ALL;
2884 if (ic->ic_flags & IEEE80211_F_USEPROT)
2886 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2887 "%s: updating protection mode: b/g protection mode=%s\n",
2888 device_get_nameunit(sc->dev),
2889 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2890 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2892 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2893 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2894 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2895 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
2896 else
2897 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2899 else
2901 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2902 "%s: updating protection mode: b/g protection mode=%s\n",
2903 device_get_nameunit(sc->dev), "none");
2905 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2908 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
2910 /* HT frame protection */
2912 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
2913 htopmode = ic->ic_bss->ni_htopmode;
2914 else
2915 htopmode = ic->ic_curhtprotmode;
2917 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2918 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
2919 device_get_nameunit(sc->dev),
2920 htopmode & IEEE80211_HTINFO_OPMODE,
2921 (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2922 ((ic->ic_htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2924 switch (htopmode & IEEE80211_HTINFO_OPMODE)
2926 /* IEEE80211_HTINFO_OPMODE_HT20PR */
2927 case IEEE80211_HTINFO_OPMODE_HT20PR:
2928 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
2929 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2930 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
2931 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2932 (4 << RT2860_REG_PROT_MCS_SHIFT);
2934 gf20_prot = mm20_prot;
2936 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
2937 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2938 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2940 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2941 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2942 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2943 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2944 else
2945 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
2947 gf40_prot = mm40_prot;
2948 break;
2950 /* IEEE80211_HTINFO_OPMODE_MIXED */
2951 case IEEE80211_HTINFO_OPMODE_MIXED:
2952 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
2953 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2954 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
2956 if (ic->ic_flags & IEEE80211_F_USEPROT)
2957 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2958 (3 << RT2860_REG_PROT_MCS_SHIFT);
2959 else
2960 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2961 (4 << RT2860_REG_PROT_MCS_SHIFT);
2963 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2964 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2965 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2966 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
2967 else
2968 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
2970 gf20_prot = mm20_prot;
2972 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
2974 if (ic->ic_flags & IEEE80211_F_USEPROT)
2975 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2976 (3 << RT2860_REG_PROT_MCS_SHIFT);
2977 else
2978 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2979 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2981 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2982 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2983 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2984 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2985 else
2986 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
2988 gf40_prot = mm40_prot;
2989 break;
2992 * IEEE80211_HTINFO_OPMODE_PURE
2993 * IEEE80211_HTINFO_OPMODE_PROTOPT
2995 case IEEE80211_HTINFO_OPMODE_PURE:
2996 case IEEE80211_HTINFO_OPMODE_PROTOPT:
2997 default:
2998 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
2999 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3000 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3001 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3002 (4 << RT2860_REG_PROT_MCS_SHIFT);
3004 gf20_prot = mm20_prot;
3006 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3007 RT2860_REG_TXOP_ALLOW_ALL |
3008 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3009 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3011 gf40_prot = mm40_prot;
3012 break;
3015 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3016 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3017 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3018 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3022 * rt2860_asic_updateslot
3024 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3026 struct ieee80211com *ic;
3027 uint32_t tmp;
3029 ic = &sc->ic;
3031 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3032 "%s: %s short slot time\n",
3033 device_get_nameunit(sc->dev),
3034 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3036 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3038 tmp &= ~0xff;
3039 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3041 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3045 * rt2860_asic_wme_update
3047 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3049 struct ieee80211com *ic;
3050 struct ieee80211_wme_state *wme;
3051 const struct wmeParams *wmep;
3052 int i;
3054 ic = &sc->ic;
3055 wme = &ic->ic_wme;
3056 wmep = wme->wme_chanParams.cap_wmeParams;
3058 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3059 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3060 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3061 device_get_nameunit(sc->dev),
3062 wmep[WME_AC_VO].wmep_aifsn,
3063 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3064 wmep[WME_AC_VO].wmep_txopLimit,
3065 wmep[WME_AC_VI].wmep_aifsn,
3066 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3067 wmep[WME_AC_VI].wmep_txopLimit,
3068 wmep[WME_AC_BK].wmep_aifsn,
3069 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3070 wmep[WME_AC_BK].wmep_txopLimit,
3071 wmep[WME_AC_BE].wmep_aifsn,
3072 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3073 wmep[WME_AC_BE].wmep_txopLimit);
3075 for (i = 0; i < WME_NUM_AC; i++)
3076 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3077 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3078 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3080 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3081 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3082 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3084 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3085 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3086 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3088 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3089 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3090 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3092 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3093 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3095 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3096 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3100 * rt2860_asic_update_beacon
3102 static int rt2860_asic_update_beacon(struct rt2860_softc *sc)
3104 struct ieee80211com *ic;
3105 struct mbuf *m;
3106 struct rt2860_txwi txwi;
3107 uint8_t rate, mcs;
3108 uint32_t tmp;
3110 ic = &sc->ic;
3112 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3113 if (m == NULL)
3114 return ENOMEM;
3116 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3117 mcs = rt2860_rate2mcs(rate);
3119 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3121 txwi.wcid = 0xff;
3122 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3123 RT2860_TXWI_MPDU_LEN_SHIFT);
3124 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3125 txwi.mpdu_density_flags |=
3126 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3127 txwi.bawin_size_xflags |=
3128 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3130 if (rate == 2)
3132 txwi.phymode_ifs_stbc_shortgi =
3133 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3135 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3136 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3138 else
3140 txwi.phymode_ifs_stbc_shortgi =
3141 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3144 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3145 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3147 /* disable temporarily TSF sync */
3149 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3151 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3152 RT2860_REG_TSF_TIMER_ENABLE |
3153 RT2860_REG_TBTT_TIMER_ENABLE);
3155 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3157 /* write Tx wireless info and beacon frame to on-chip memory */
3159 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3160 &txwi, sizeof(struct rt2860_txwi));
3162 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3163 mtod(m, uint8_t *), m->m_pkthdr.len);
3165 /* enable again TSF sync */
3167 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3169 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3170 RT2860_REG_TSF_TIMER_ENABLE |
3171 RT2860_REG_TBTT_TIMER_ENABLE);
3173 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3175 m_freem(m);
3177 return 0;
3181 * rt2860_asic_clear_keytables
3183 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3185 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3187 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID(0), 0, 2 * 256);
3189 /* clear WCID attribute table (entries = 256, entry size = 4) */
3191 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3193 /* clear IV/EIV table (entries = 256, entry size = 8) */
3195 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3197 /* clear pairwise key table (entries = 256, entry size = 32) */
3199 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3201 /* clear shared key table (entries = 32, entry size = 32) */
3203 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3205 /* clear shared key mode (entries = 32, entry size = 2) */
3207 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3211 * rt2860_rxrate
3213 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3215 uint8_t mcs, phymode;
3216 uint8_t rate;
3218 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3219 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3220 RT2860_RXWI_PHYMODE_MASK;
3222 rate = 2;
3224 switch (phymode)
3226 case RT2860_RXWI_PHYMODE_CCK:
3227 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3229 case 0: rate = 2; break; /* 1 Mbps */
3230 case 1: rate = 4; break; /* 2 MBps */
3231 case 2: rate = 11; break; /* 5.5 Mbps */
3232 case 3: rate = 22; break; /* 11 Mbps */
3234 break;
3236 case RT2860_RXWI_PHYMODE_OFDM:
3237 switch (mcs)
3239 case 0: rate = 12; break; /* 6 Mbps */
3240 case 1: rate = 18; break; /* 9 Mbps */
3241 case 2: rate = 24; break; /* 12 Mbps */
3242 case 3: rate = 36; break; /* 18 Mbps */
3243 case 4: rate = 48; break; /* 24 Mbps */
3244 case 5: rate = 72; break; /* 36 Mbps */
3245 case 6: rate = 96; break; /* 48 Mbps */
3246 case 7: rate = 108; break; /* 54 Mbps */
3248 break;
3250 case RT2860_RXWI_PHYMODE_HT_MIXED:
3251 case RT2860_RXWI_PHYMODE_HT_GF:
3252 break;
3255 return rate;
3259 * rt2860_maxrssi_rxpath
3261 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3262 const struct rt2860_rxwi *rxwi)
3264 uint8_t rxpath;
3266 rxpath = 0;
3268 if (sc->nrxpath > 1)
3269 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3270 rxpath = 1;
3272 if (sc->nrxpath > 2)
3273 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3274 rxpath = 2;
3276 return rxpath;
3280 * rt2860_rssi2dbm
3282 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3283 uint8_t rssi, uint8_t rxpath)
3285 struct ieee80211com *ic;
3286 struct ieee80211_channel *c;
3287 int chan, delta;
3289 ic = &sc->ic;
3290 c = ic->ic_curchan;
3291 chan = ieee80211_chan2ieee(ic, c);
3293 if (IEEE80211_IS_CHAN_5GHZ(c))
3295 delta = sc->rssi_off_5ghz[rxpath];
3297 if (chan <= 64)
3298 delta -= sc->lna_gain[1];
3299 else if (chan <= 128)
3300 delta -= sc->lna_gain[2];
3301 else
3302 delta -= sc->lna_gain[3];
3304 else
3306 delta = sc->rssi_off_2ghz[rxpath] - sc->lna_gain[0];
3309 return (-12 - delta - rssi);
3313 * rt2860_rate2mcs
3315 static uint8_t rt2860_rate2mcs(uint8_t rate)
3317 switch (rate)
3319 /* CCK rates */
3320 case 2: return 0;
3321 case 4: return 1;
3322 case 11: return 2;
3323 case 22: return 3;
3325 /* OFDM rates */
3326 case 12: return 0;
3327 case 18: return 1;
3328 case 24: return 2;
3329 case 36: return 3;
3330 case 48: return 4;
3331 case 72: return 5;
3332 case 96: return 6;
3333 case 108: return 7;
3336 return 0;
3340 * rt2860_ackrate
3342 static int rt2860_ackrate(struct ieee80211com *ic, int rate)
3344 switch (rate)
3346 /* CCK rates */
3348 case 2:
3349 return 2;
3351 case 4:
3352 case 11:
3353 case 22:
3354 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3356 /* OFDM rates */
3358 case 12:
3359 case 18:
3360 return 12;
3362 case 24:
3363 case 36:
3364 return 24;
3366 case 48:
3367 case 72:
3368 case 96:
3369 case 108:
3370 return 48;
3373 /* default to 1Mbps */
3374 return 2;
3378 * rt2860_txtime
3380 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags)
3382 uint16_t txtime;
3384 if (RT2860_RATE_IS_OFDM(rate))
3386 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3387 txtime = 16 + 4 + 4 * txtime + 6;
3389 else
3391 txtime = (16 * len + rate - 1) / rate;
3393 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3394 txtime += 72 + 24;
3395 else
3396 txtime += 144 + 48;
3399 return txtime;
3403 * rt2860_tx_frame
3405 static int rt2860_tx_frame(struct rt2860_softc *sc,
3406 struct mbuf *m, struct ieee80211_node *ni, int qid)
3408 struct ieee80211com *ic;
3409 struct rt2860_softc_tx_ring *ring;
3410 struct rt2860_softc_tx_data *data;
3411 struct rt2860_txdesc *desc;
3412 struct rt2860_txwi *txwi;
3413 struct ieee80211_frame *wh;
3414 struct rt2860_softc_tx_radiotap_header *tap;
3415 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3416 u_int hdrsize, hdrspace;
3417 uint8_t type, rate, bw, shortgi, mcs, pid, wcid, qsel;
3418 uint16_t qos, len, dmalen, mpdu_len, dur;
3419 int error, hasqos, ackrate, ndmasegs, ndescs, i, j;
3421 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3422 ("%s: Tx frame: invalid qid=%d\n",
3423 device_get_nameunit(sc->dev), qid));
3425 ic = &sc->ic;
3427 ring = &sc->tx_ring[qid];
3428 desc = &ring->desc[ring->desc_cur];
3429 data = &ring->data[ring->data_cur];
3430 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3432 wh = mtod(m, struct ieee80211_frame *);
3434 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3436 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3437 if (hasqos)
3439 if (IEEE80211_HAS_ADDR4(wh))
3440 qos = le16toh(*(const uint16_t *)
3441 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3442 else
3443 qos = le16toh(*(const uint16_t *)
3444 (((struct ieee80211_qosframe *) wh)->i_qos));
3446 else
3448 qos = 0;
3451 if (ni->ni_flags & IEEE80211_NODE_HT)
3453 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3454 rate = 0;
3455 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3456 rate = ic->ic_fixed_rate;
3457 else
3458 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
3460 else
3462 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3463 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3464 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3465 rate = ic->ic_fixed_rate;
3466 else
3467 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
3470 rate &= IEEE80211_RATE_VAL;
3472 /* fill Tx wireless info */
3474 if (ni->ni_flags & IEEE80211_NODE_HT)
3475 mcs = rate;
3476 else
3477 mcs = rt2860_rate2mcs(rate);
3479 pid = mcs + 1;
3481 /* management frames do not need encryption */
3483 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3484 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3486 /* calculate MPDU length without padding */
3488 hdrsize = ieee80211_hdrsize(wh);
3489 hdrspace = ieee80211_hdrspace(ic, wh);
3490 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3492 memset(txwi, 0, sizeof(struct rt2860_txwi));
3494 txwi->wcid = wcid;
3496 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3497 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3498 RT2860_TXWI_MPDU_LEN_SHIFT);
3500 if ((ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
3501 (ni->ni_flags & IEEE80211_NODE_HT))
3502 shortgi = 1;
3503 else
3504 shortgi = 0;
3506 txwi->phymode_ifs_stbc_shortgi |=
3507 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3509 if (ni->ni_flags & IEEE80211_NODE_HT)
3511 txwi->phymode_ifs_stbc_shortgi |=
3512 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3514 else
3516 if (!RT2860_RATE_IS_OFDM(rate))
3518 txwi->phymode_ifs_stbc_shortgi |=
3519 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3521 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3522 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3524 else
3526 txwi->phymode_ifs_stbc_shortgi |=
3527 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3531 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
3532 (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40))
3533 bw = RT2860_TXWI_BW_40;
3534 else
3535 bw = RT2860_TXWI_BW_20;
3537 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
3538 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3540 if (type != IEEE80211_FC0_TYPE_DATA)
3541 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3542 else
3543 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3545 /* skip ACKs for multicast frames and probe responses */
3547 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3548 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3549 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
3550 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
3552 txwi->bawin_size_xflags |=
3553 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3555 if (ni->ni_flags & IEEE80211_NODE_HT)
3557 /* preamble + plcp + signal extension */
3559 dur = 16 + 4 + 6;
3561 else
3563 ackrate = rt2860_ackrate(ic, rate);
3565 dur = rt2860_txtime(RT2860_ACK_SIZE, ackrate, ic->ic_flags) +
3566 sc->sifs;
3569 *(uint16_t *) wh->i_dur = htole16(dur);
3572 /* ask MAC to insert timestamp into probe responses */
3574 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3575 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3576 txwi->mpdu_density_flags |=
3577 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3579 if (bpf_peers_present(sc->drvbpf))
3581 tap = &sc->txtap;
3583 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3584 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3585 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3586 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3587 tap->chan_maxpow = 0;
3589 if (ni->ni_flags & IEEE80211_NODE_HT)
3590 tap->rate = mcs | IEEE80211_RATE_MCS;
3591 else
3592 tap->rate = rate;
3594 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3595 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3597 if (shortgi)
3598 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3600 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3601 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3603 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3605 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3607 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3609 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3611 else
3613 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3617 /* copy and trim 802.11 header */
3619 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3620 m_adj(m, hdrspace);
3622 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3623 dma_seg, &ndmasegs, 0);
3624 if (error != 0)
3626 /* too many fragments, linearize */
3628 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3629 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3630 device_get_nameunit(sc->dev));
3632 m = m_defrag(m, M_DONTWAIT);
3633 if (m == NULL)
3634 return ENOMEM;
3636 sc->tx_defrag_packets++;
3638 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3639 dma_seg, &ndmasegs, 0);
3640 if (error != 0)
3642 printf("%s: could not load mbuf DMA map: error=%d\n",
3643 device_get_nameunit(sc->dev), error);
3644 m_freem(m);
3645 return error;
3649 /* determine how many Tx descs are required */
3651 ndescs = 1 + ndmasegs / 2;
3652 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3654 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3655 "%s: there are not enough Tx descs\n",
3656 device_get_nameunit(sc->dev));
3658 sc->no_tx_desc_avail++;
3660 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3661 m_freem(m);
3662 return EFBIG;
3665 data->m = m;
3666 data->ni = ni;
3668 /* set up Tx descs */
3670 /* first segment is Tx wireless info and 802.11 header */
3672 len = sizeof(struct rt2860_txwi) + hdrsize;
3674 /* align end on a 4-bytes boundary */
3676 dmalen = (len + 3) & ~ 3;
3678 memset((caddr_t) txwi + len, 0, dmalen - len);
3680 qsel = RT2860_TXDESC_QSEL_EDCA;
3682 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3683 desc->sdl0 = htole16(dmalen);
3684 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3686 /* set up payload segments */
3688 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3690 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3691 desc->sdl1 = htole16(dma_seg[j].ds_len);
3693 ring->desc_queued++;
3694 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3696 j++;
3698 desc = &ring->desc[ring->desc_cur];
3700 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3701 desc->sdl0 = htole16(dma_seg[j].ds_len);
3702 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3704 j++;
3707 /* finalize last payload segment */
3709 if (i > 0)
3711 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3712 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3714 else
3716 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3717 desc->sdl1 = 0;
3720 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3721 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3722 "bw=%d, shortgi=%d, mcs=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3723 device_get_nameunit(sc->dev),
3724 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3725 bw, shortgi, mcs, dmalen, ndmasegs,
3726 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);
3728 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3729 BUS_DMASYNC_PREWRITE);
3730 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3731 BUS_DMASYNC_PREWRITE);
3732 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3733 BUS_DMASYNC_PREWRITE);
3735 ring->desc_queued++;
3736 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3738 ring->data_queued++;
3739 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3741 /* kick Tx */
3743 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3745 return 0;
3749 * rt2860_tx_raw
3751 static int rt2860_tx_raw(struct rt2860_softc *sc,
3752 struct mbuf *m, struct ieee80211_node *ni,
3753 const struct ieee80211_bpf_params *params)
3755 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3756 "%s: Tx raw\n",
3757 device_get_nameunit(sc->dev));
3759 return 0;
3763 * rt2860_intr
3765 static void rt2860_intr(void *arg)
3767 struct rt2860_softc *sc;
3768 struct ifnet *ifp;
3769 uint32_t status;
3771 sc = arg;
3772 ifp = sc->ifp;
3774 /* acknowledge interrupts */
3776 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
3777 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
3779 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3780 "%s: interrupt: status = 0x%08x\n",
3781 device_get_nameunit(sc->dev), status);
3783 if (status == 0xffffffff || /* device likely went away */
3784 status == 0) /* not for us */
3785 return;
3787 sc->interrupts++;
3789 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
3790 return;
3792 if (status & RT2860_REG_INT_TX_COHERENT)
3793 rt2860_tx_coherent_intr(sc);
3795 if (status & RT2860_REG_INT_RX_COHERENT)
3796 rt2860_rx_coherent_intr(sc);
3798 if (status & RT2860_REG_INT_TXRX_COHERENT)
3799 rt2860_txrx_coherent_intr(sc);
3801 if (status & RT2860_REG_INT_FIFO_STA_FULL)
3802 rt2860_fifo_sta_full_intr(sc);
3804 if (status & RT2860_REG_INT_TX_MGMT_DONE)
3805 rt2860_tx_intr(sc, 5);
3807 if (status & RT2860_REG_INT_RX_DONE)
3808 rt2860_rx_intr(sc);
3810 if (status & RT2860_REG_INT_RX_DELAY_DONE)
3811 rt2860_rx_delay_intr(sc);
3813 if (status & RT2860_REG_INT_TX_HCCA_DONE)
3814 rt2860_tx_intr(sc, 4);
3816 if (status & RT2860_REG_INT_TX_AC3_DONE)
3817 rt2860_tx_intr(sc, 3);
3819 if (status & RT2860_REG_INT_TX_AC2_DONE)
3820 rt2860_tx_intr(sc, 2);
3822 if (status & RT2860_REG_INT_TX_AC1_DONE)
3823 rt2860_tx_intr(sc, 1);
3825 if (status & RT2860_REG_INT_TX_AC0_DONE)
3826 rt2860_tx_intr(sc, 0);
3828 if (status & RT2860_REG_INT_TX_DELAY_DONE)
3829 rt2860_tx_delay_intr(sc);
3831 if (status & RT2860_REG_INT_PRE_TBTT)
3832 rt2860_pre_tbtt_intr(sc);
3834 if (status & RT2860_REG_INT_TBTT)
3835 rt2860_tbtt_intr(sc);
3837 if (status & RT2860_REG_INT_MCU_CMD)
3838 rt2860_mcu_cmd_intr(sc);
3840 if (status & RT2860_REG_INT_AUTO_WAKEUP)
3841 rt2860_auto_wakeup_intr(sc);
3843 if (status & RT2860_REG_INT_GP_TIMER)
3844 rt2860_gp_timer_intr(sc);
3848 * rt2860_tx_coherent_intr
3850 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
3852 uint32_t tmp;
3853 int i;
3855 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3856 "%s: Tx coherent interrupt\n",
3857 device_get_nameunit(sc->dev));
3859 sc->tx_coherent_interrupts++;
3861 /* restart DMA engine */
3863 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3865 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3866 RT2860_REG_RX_DMA_ENABLE |
3867 RT2860_REG_TX_DMA_ENABLE);
3869 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3871 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3873 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3874 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3876 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3878 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3879 sc->tx_ring[i].desc_phys_addr);
3880 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3881 RT2860_SOFTC_TX_RING_DESC_COUNT);
3882 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3885 /* init Rx ring */
3887 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3889 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3890 sc->rx_ring.desc_phys_addr);
3891 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3892 RT2860_SOFTC_RX_RING_DATA_COUNT);
3893 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3894 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3896 rt2860_txrx_enable(sc);
3900 * rt2860_rx_coherent_intr
3902 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
3904 uint32_t tmp;
3905 int i;
3907 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3908 "%s: Rx coherent interrupt\n",
3909 device_get_nameunit(sc->dev));
3911 sc->rx_coherent_interrupts++;
3913 /* restart DMA engine */
3915 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3917 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3918 RT2860_REG_RX_DMA_ENABLE |
3919 RT2860_REG_TX_DMA_ENABLE);
3921 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3923 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3925 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3926 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3928 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3930 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3931 sc->tx_ring[i].desc_phys_addr);
3932 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3933 RT2860_SOFTC_TX_RING_DESC_COUNT);
3934 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3937 /* init Rx ring */
3939 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3941 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3942 sc->rx_ring.desc_phys_addr);
3943 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3944 RT2860_SOFTC_RX_RING_DATA_COUNT);
3945 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3946 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3948 rt2860_txrx_enable(sc);
3952 * rt2860_txrx_coherent_intr
3954 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
3956 uint32_t tmp;
3957 int i;
3959 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3960 "%s: Tx/Rx coherent interrupt\n",
3961 device_get_nameunit(sc->dev));
3963 sc->txrx_coherent_interrupts++;
3965 /* restart DMA engine */
3967 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3969 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3970 RT2860_REG_RX_DMA_ENABLE |
3971 RT2860_REG_TX_DMA_ENABLE);
3973 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3975 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3977 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3978 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3980 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3982 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3983 sc->tx_ring[i].desc_phys_addr);
3984 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3985 RT2860_SOFTC_TX_RING_DESC_COUNT);
3986 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3989 /* init Rx ring */
3991 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3993 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3994 sc->rx_ring.desc_phys_addr);
3995 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3996 RT2860_SOFTC_RX_RING_DATA_COUNT);
3997 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3998 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4000 rt2860_txrx_enable(sc);
4004 * rt2860_fifo_sta_full_intr
4006 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4008 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4009 "%s: FIFO statistic full interrupt\n",
4010 device_get_nameunit(sc->dev));
4012 sc->fifo_sta_full_interrupts++;
4014 RT2860_SOFTC_LOCK(sc);
4016 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4018 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4020 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4023 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4025 RT2860_SOFTC_UNLOCK(sc);
4029 * rt2860_rx_intr
4031 static void rt2860_rx_intr(struct rt2860_softc *sc)
4033 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4034 "%s: Rx interrupt\n",
4035 device_get_nameunit(sc->dev));
4037 sc->rx_interrupts++;
4039 RT2860_SOFTC_LOCK(sc);
4041 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4043 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4045 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4048 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4050 RT2860_SOFTC_UNLOCK(sc);
4054 * rt2860_rx_delay_intr
4056 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4058 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4059 "%s: Rx delay interrupt\n",
4060 device_get_nameunit(sc->dev));
4062 sc->rx_delay_interrupts++;
4066 * rt2860_tx_intr
4068 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4070 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4071 ("%s: Tx interrupt: invalid qid=%d\n",
4072 device_get_nameunit(sc->dev), qid));
4074 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4075 "%s: Tx interrupt: qid=%d\n",
4076 device_get_nameunit(sc->dev), qid);
4078 sc->tx_interrupts[qid]++;
4080 RT2860_SOFTC_LOCK(sc);
4082 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4084 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4086 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4089 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4091 RT2860_SOFTC_UNLOCK(sc);
4095 * rt2860_tx_delay_intr
4097 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4099 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4100 "%s: Tx delay interrupt\n",
4101 device_get_nameunit(sc->dev));
4103 sc->tx_delay_interrupts++;
4107 * rt2860_pre_tbtt_intr
4109 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4111 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4112 "%s: Pre-TBTT interrupt\n",
4113 device_get_nameunit(sc->dev));
4115 sc->pre_tbtt_interrupts++;
4119 * rt2860_tbtt_intr
4121 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4123 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4124 "%s: TBTT interrupt\n",
4125 device_get_nameunit(sc->dev));
4127 sc->tbtt_interrupts++;
4131 * rt2860_mcu_cmd_intr
4133 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4135 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4136 "%s: MCU command interrupt\n",
4137 device_get_nameunit(sc->dev));
4139 sc->mcu_cmd_interrupts++;
4143 * rt2860_auto_wakeup_intr
4145 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4147 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4148 "%s: auto wakeup interrupt\n",
4149 device_get_nameunit(sc->dev));
4151 sc->auto_wakeup_interrupts++;
4155 * rt2860_gp_timer_intr
4157 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4159 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4160 "%s: GP timer interrupt\n",
4161 device_get_nameunit(sc->dev));
4163 sc->gp_timer_interrupts++;
4167 * rt2860_rx_done_task
4169 static void rt2860_rx_done_task(void *context, int pending)
4171 struct rt2860_softc *sc;
4172 struct ifnet *ifp;
4173 int again;
4175 sc = context;
4176 ifp = sc->ifp;
4178 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4179 "%s: Rx done task\n",
4180 device_get_nameunit(sc->dev));
4182 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4183 return;
4185 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4187 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4189 RT2860_SOFTC_LOCK(sc);
4191 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4193 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4194 "%s: Rx done task: scheduling again\n",
4195 device_get_nameunit(sc->dev));
4197 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4199 else
4201 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4204 RT2860_SOFTC_UNLOCK(sc);
4208 * rt2860_tx_done_task
4210 static void rt2860_tx_done_task(void *context, int pending)
4212 struct rt2860_softc *sc;
4213 struct ifnet *ifp;
4214 uint32_t intr_mask;
4215 int i;
4217 sc = context;
4218 ifp = sc->ifp;
4220 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4221 "%s: Tx done task\n",
4222 device_get_nameunit(sc->dev));
4224 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4225 return;
4227 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4228 RT2860_REG_INT_TX_HCCA_DONE |
4229 RT2860_REG_INT_TX_AC3_DONE |
4230 RT2860_REG_INT_TX_AC2_DONE |
4231 RT2860_REG_INT_TX_AC1_DONE |
4232 RT2860_REG_INT_TX_AC0_DONE);
4234 sc->intr_pending_mask &= ~intr_mask;
4236 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4237 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4239 sc->tx_timer = 0;
4241 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4243 RT2860_SOFTC_LOCK(sc);
4245 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4246 (sc->intr_disable_mask & intr_mask));
4248 if (sc->intr_pending_mask & intr_mask)
4250 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4251 "%s: Tx done task: scheduling again\n",
4252 device_get_nameunit(sc->dev));
4254 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4257 RT2860_SOFTC_UNLOCK(sc);
4261 * rt2860_fifo_sta_full_task
4263 static void rt2860_fifo_sta_full_task(void *context, int pending)
4265 struct rt2860_softc *sc;
4266 struct ifnet *ifp;
4268 sc = context;
4269 ifp = sc->ifp;
4271 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4272 "%s: FIFO statistic full task\n",
4273 device_get_nameunit(sc->dev));
4275 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4276 return;
4278 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4280 rt2860_drain_fifo_stats(sc);
4282 RT2860_SOFTC_LOCK(sc);
4284 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4286 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4287 "%s: FIFO statistic full task: scheduling again\n",
4288 device_get_nameunit(sc->dev));
4290 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4292 else
4294 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4297 RT2860_SOFTC_UNLOCK(sc);
4301 * rt2860_periodic_task
4303 static void rt2860_periodic_task(void *context, int pending)
4305 struct rt2860_softc *sc;
4306 struct ifnet *ifp;
4307 struct ieee80211com *ic;
4309 sc = context;
4310 ifp = sc->ifp;
4311 ic = &sc->ic;
4313 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4314 "%s: periodic task: round=%lu\n",
4315 device_get_nameunit(sc->dev), sc->periodic_round);
4317 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4318 return;
4320 RT2860_SOFTC_LOCK(sc);
4322 sc->periodic_round++;
4324 rt2860_update_stats(sc);
4326 if ((sc->periodic_round % 10) == 0)
4328 rt2860_update_raw_counters(sc);
4330 rt2860_watchdog(sc);
4332 if (ic->ic_opmode == IEEE80211_M_STA)
4333 rt2860_amrr_update_iter_func(sc, ic->ic_bss);
4334 else
4335 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, sc);
4338 RT2860_SOFTC_UNLOCK(sc);
4340 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4344 * rt2860_rx_eof
4346 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4348 struct ieee80211com *ic;
4349 struct ifnet *ifp;
4350 struct ieee80211_frame *wh;
4351 struct ieee80211_node *ni;
4352 struct rt2860_softc_rx_radiotap_header *tap;
4353 struct rt2860_softc_rx_ring *ring;
4354 struct rt2860_rxdesc *desc;
4355 struct rt2860_softc_rx_data *data;
4356 struct rt2860_rxwi *rxwi;
4357 struct mbuf *m, *mnew;
4358 bus_dma_segment_t segs[1];
4359 uint32_t index, desc_flags;
4360 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4361 int8_t rssi_dbm;
4362 int error, nsegs, len, ampdu, amsdu, nframes;
4364 ic = &sc->ic;
4365 ifp = sc->ifp;
4366 ring = &sc->rx_ring;
4368 nframes = 0;
4370 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4371 BUS_DMASYNC_POSTREAD);
4373 while (limit != 0)
4375 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4376 if (ring->cur == index)
4377 break;
4379 desc = &ring->desc[ring->cur];
4380 desc_flags = le32toh(desc->flags);
4381 data = &ring->data[ring->cur];
4383 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4384 break;
4386 nframes++;
4388 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4389 if (mnew == NULL)
4391 sc->rx_mbuf_alloc_errors++;
4392 ifp->if_ierrors++;
4393 goto skip;
4396 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4398 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4399 BUS_DMASYNC_POSTREAD);
4400 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4402 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4403 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4404 if (error != 0)
4406 m_freem(mnew);
4408 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4409 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
4410 if (error != 0)
4411 panic("%s: could not load old Rx mbuf: error=%d\n",
4412 device_get_name(sc->dev), error);
4414 sc->rx_mbuf_dmamap_errors++;
4415 ifp->if_ierrors++;
4417 goto skip;
4420 KASSERT(nsegs == 1, ("%s: too many DMA segments"),
4421 device_get_name(sc->dev));
4423 m = data->m;
4425 data->m = mnew;
4426 desc->sdp0 = htole32(segs[0].ds_addr);
4428 /* get Rx wireless info */
4430 rxwi = mtod(m, struct rt2860_rxwi *);
4431 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4432 RT2860_RXWI_SIZE_MASK;
4434 /* check for L2 padding between IEEE 802.11 frame header and body */
4436 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4438 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4439 "%s: L2 padding: len=%d\n",
4440 device_get_nameunit(sc->dev), len);
4442 len += 2;
4445 m->m_pkthdr.rcvif = ifp;
4446 m->m_data = (caddr_t) (rxwi + 1);
4447 m->m_pkthdr.len = m->m_len = len;
4449 /* check for crc errors */
4451 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4453 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4454 "%s: rxdesc: crc error\n",
4455 device_get_nameunit(sc->dev));
4457 ifp->if_ierrors++;
4459 if (!(ifp->if_flags & IFF_PROMISC))
4461 m_freem(m);
4462 goto skip;
4466 wh = (struct ieee80211_frame *) (rxwi + 1);
4468 /* check for cipher errors */
4470 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4472 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4473 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4474 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4476 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4477 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4479 m->m_flags |= M_WEP;
4481 else
4483 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4484 "%s: rxdesc: cipher error=0x%02x\n",
4485 device_get_nameunit(sc->dev), cipher_err);
4487 ifp->if_ierrors++;
4489 if (!(ifp->if_flags & IFF_PROMISC))
4491 m_free(m);
4492 goto skip;
4496 else
4498 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4500 ifp->if_ierrors++;
4502 if (!(ifp->if_flags & IFF_PROMISC))
4504 m_free(m);
4505 goto skip;
4510 /* check for A-MPDU */
4512 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
4514 m->m_flags |= M_AMPDU;
4515 ampdu = 1;
4517 else
4519 ampdu = 0;
4522 ant = rt2860_maxrssi_rxpath(sc, rxwi);
4523 rssi = rxwi->rssi[ant];
4524 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
4525 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4526 RT2860_RXWI_PHYMODE_MASK);
4527 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
4528 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
4529 RT2860_RXWI_SHORTGI_MASK);
4530 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
4531 amsdu = (desc_flags & RT2860_RXDESC_FLAGS_AMSDU);
4533 if (bpf_peers_present(sc->drvbpf))
4535 tap = &sc->rxtap;
4537 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4538 tap->dbm_antsignal = rssi_dbm;
4539 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
4540 tap->antenna = ant;
4541 tap->antsignal = rssi;
4542 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4543 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4544 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4545 tap->chan_maxpow = 0;
4547 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
4548 tap->rate = mcs | IEEE80211_RATE_MCS;
4549 else
4550 tap->rate = rt2860_rxrate(rxwi);
4552 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
4553 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4555 if (shortgi)
4556 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4558 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
4561 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4562 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4563 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
4564 device_get_nameunit(sc->dev),
4565 len, phymode, bw, shortgi, mcs,
4566 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
4567 rxwi->snr[0], rxwi->snr[1],
4568 rxwi->wcid, ampdu, amsdu);
4570 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
4572 ieee80211_input(ic, m, ni, rssi_dbm - RT2860_NOISE_FLOOR, RT2860_NOISE_FLOOR, 0);
4574 ieee80211_free_node(ni);
4576 skip:
4578 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
4580 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4581 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4583 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
4585 limit--;
4588 if (ring->cur == 0)
4589 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4590 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4591 else
4592 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4593 ring->cur - 1);
4595 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4596 "%s: Rx eof: nframes=%d\n",
4597 device_get_nameunit(sc->dev), nframes);
4599 sc->rx_packets += nframes;
4601 return (limit == 0);
4605 * rt2860_tx_eof
4607 static void rt2860_tx_eof(struct rt2860_softc *sc,
4608 struct rt2860_softc_tx_ring *ring)
4610 struct ifnet *ifp;
4611 struct rt2860_txdesc *desc;
4612 struct rt2860_softc_tx_data *data;
4613 uint32_t index;
4614 int ndescs, nframes;
4616 ifp = sc->ifp;
4618 ndescs = 0;
4619 nframes = 0;
4621 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4622 BUS_DMASYNC_POSTREAD);
4624 for (;;)
4626 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
4627 if (ring->desc_next == index)
4628 break;
4630 ndescs++;
4632 rt2860_drain_fifo_stats(sc);
4634 desc = &ring->desc[ring->desc_next];
4636 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
4637 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
4639 nframes++;
4641 data = &ring->data[ring->data_next];
4643 if (data->m->m_flags & M_TXCB)
4644 ieee80211_process_callback(data->ni, data->m, 0);
4646 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4647 BUS_DMASYNC_POSTWRITE);
4648 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4650 m_freem(data->m);
4652 ieee80211_free_node(data->ni);
4654 data->m = NULL;
4655 data->ni = NULL;
4657 ifp->if_opackets++;
4659 ring->data_queued--;
4660 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4663 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
4665 ring->desc_queued--;
4666 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4669 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4670 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4672 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4673 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
4674 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
4678 * rt2860_update_stats
4680 static void rt2860_update_stats(struct rt2860_softc *sc)
4682 struct ifnet *ifp;
4683 struct ieee80211com *ic;
4684 struct ieee80211_node *ni;
4685 uint16_t associd;
4686 uint8_t wcid;
4687 uint32_t stacnt[3];
4688 int txcnt, retrycnt, failcnt;
4690 ifp = sc->ifp;
4691 ic = &sc->ic;
4693 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4694 "%s: update statistic\n",
4695 device_get_nameunit(sc->dev));
4697 rt2860_drain_fifo_stats(sc);
4699 if (ic->ic_opmode == IEEE80211_M_STA)
4701 ni = ic->ic_bss;
4703 associd = (ni != NULL) ? ni->ni_associd : 0;
4704 wcid = RT2860_AID2WCID(associd);
4706 /* read and clear Tx statistic registers */
4708 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
4709 stacnt, sizeof(stacnt));
4711 txcnt = le32toh(stacnt[1]) & 0xffff;
4712 retrycnt = le32toh(stacnt[1]) >> 16;
4713 failcnt = le32toh(stacnt[0]) & 0xffff;
4715 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4716 "%s: update statistic: associd=0x%04x, txcnt=%d, retrycnt=%d, failcnt=%d\n",
4717 device_get_nameunit(sc->dev),
4718 associd, txcnt, retrycnt, failcnt);
4720 ifp->if_oerrors += failcnt;
4722 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
4723 txcnt + retrycnt + failcnt, txcnt + retrycnt, retrycnt + failcnt);
4728 * rt2860_watchdog
4730 static void rt2860_watchdog(struct rt2860_softc *sc)
4732 uint32_t tmp;
4733 int ntries;
4735 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4737 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
4738 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4739 device_get_nameunit(sc->dev), tmp);
4741 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
4743 sc->tx_queue_not_empty[0]++;
4745 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
4747 for (ntries = 0; ntries < 10; ntries++)
4749 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4750 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
4751 break;
4753 DELAY(1);
4756 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4759 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
4761 sc->tx_queue_not_empty[1]++;
4763 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
4765 for (ntries = 0; ntries < 10; ntries++)
4767 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4768 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
4769 break;
4771 DELAY(1);
4774 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4779 * rt2860_drain_fifo_stats
4781 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
4783 struct ifnet *ifp;
4784 uint32_t stats;
4785 uint8_t wcid, mcs, pid;
4786 int ok, retrycnt;
4788 ifp = sc->ic.ic_ifp;
4790 /* drain Tx status FIFO (maxsize = 16) */
4792 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
4793 RT2860_REG_TX_STA_FIFO_VALID)
4795 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
4796 RT2860_REG_TX_STA_FIFO_WCID_MASK;
4798 /* if no ACK was requested, no feedback is available */
4800 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
4801 continue;
4803 /* update AMRR statistic */
4805 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
4806 retrycnt = 0;
4808 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
4809 RT2860_REG_TX_STA_FIFO_MCS_MASK;
4810 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
4811 RT2860_REG_TX_STA_FIFO_PID_MASK;
4813 if (!ok || (mcs + 1) != pid)
4814 retrycnt++;
4816 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
4818 if (!ok)
4819 ifp->if_oerrors++;
4824 * rt2860_update_raw_counters
4826 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
4828 uint32_t tmp;
4830 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
4832 sc->rx_crc_errors += tmp & 0xffff;
4833 sc->rx_phy_errors += tmp >> 16;
4835 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
4837 sc->rx_false_ccas += tmp & 0xffff;
4838 sc->rx_plcp_errors += tmp >> 16;
4840 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
4842 sc->rx_dup_packets += tmp & 0xffff;
4843 sc->rx_fifo_overflows += tmp >> 16;
4847 * rt2860_intr_enable
4849 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
4851 uint32_t tmp;
4853 sc->intr_disable_mask &= ~intr_mask;
4855 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4857 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4861 * rt2860_intr_disable
4863 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
4865 uint32_t tmp;
4867 sc->intr_disable_mask |= intr_mask;
4869 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4871 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4875 * rt2860_txrx_enable
4877 static int rt2860_txrx_enable(struct rt2860_softc *sc)
4879 struct ieee80211com *ic;
4880 struct ifnet *ifp;
4881 uint32_t tmp;
4882 int ntries;
4884 ic = &sc->ic;
4885 ifp = ic->ic_ifp;
4887 /* enable Tx/Rx DMA engine */
4889 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
4891 for (ntries = 0; ntries < 200; ntries++)
4893 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4894 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
4895 break;
4897 DELAY(1000);
4900 if (ntries == 200)
4902 printf("%s: timeout waiting for DMA engine\n",
4903 device_get_nameunit(sc->dev));
4904 return -1;
4907 DELAY(50);
4909 tmp |= RT2860_REG_TX_WB_DDONE |
4910 RT2860_REG_RX_DMA_ENABLE |
4911 RT2860_REG_TX_DMA_ENABLE |
4912 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
4914 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4916 /* set Rx filter */
4918 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
4919 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
4921 if (ic->ic_opmode != IEEE80211_M_MONITOR)
4923 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
4924 RT2860_REG_RX_FILTER_DROP_CTS |
4925 RT2860_REG_RX_FILTER_DROP_BA |
4926 RT2860_REG_RX_FILTER_DROP_ACK |
4927 RT2860_REG_RX_FILTER_DROP_VER_ERR |
4928 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
4929 RT2860_REG_RX_FILTER_DROP_CFACK |
4930 RT2860_REG_RX_FILTER_DROP_CFEND;
4932 if (ic->ic_opmode == IEEE80211_M_STA)
4933 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
4934 RT2860_REG_RX_FILTER_DROP_PSPOLL;
4936 if (!(ifp->if_flags & IFF_PROMISC))
4937 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
4940 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
4942 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
4943 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
4945 return 0;
4949 * rt2860_alloc_rx_ring
4951 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
4952 struct rt2860_softc_rx_ring *ring)
4954 struct rt2860_rxdesc *desc;
4955 struct rt2860_softc_rx_data *data;
4956 bus_dma_segment_t segs[1];
4957 int i, nsegs, error;
4959 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
4960 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4961 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
4962 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
4963 0, NULL, NULL, &ring->desc_dma_tag);
4964 if (error != 0)
4966 printf("%s: could not create Rx desc DMA tag\n",
4967 device_get_nameunit(sc->dev));
4968 goto fail;
4971 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
4972 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
4973 if (error != 0)
4975 printf("%s: could not allocate Rx desc DMA memory\n",
4976 device_get_nameunit(sc->dev));
4977 goto fail;
4980 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
4981 ring->desc,
4982 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
4983 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
4984 if (error != 0)
4986 printf("%s: could not load Rx desc DMA map\n",
4987 device_get_nameunit(sc->dev));
4988 goto fail;
4991 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
4992 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4993 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
4994 &ring->data_dma_tag);
4995 if (error != 0)
4997 printf("%s: could not create Rx data DMA tag\n",
4998 device_get_nameunit(sc->dev));
4999 goto fail;
5002 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5004 desc = &ring->desc[i];
5005 data = &ring->data[i];
5007 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5008 if (error != 0)
5010 printf("%s: could not create Rx data DMA map\n",
5011 device_get_nameunit(sc->dev));
5012 goto fail;
5015 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5016 if (data->m == NULL)
5018 printf("%s: could not allocate Rx mbuf\n",
5019 device_get_nameunit(sc->dev));
5020 error = ENOMEM;
5021 goto fail;
5024 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5026 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5027 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5028 if (error != 0)
5030 printf("%s: could not load Rx mbuf DMA map\n",
5031 device_get_nameunit(sc->dev));
5032 goto fail;
5035 KASSERT(nsegs == 1, ("%s: too many DMA segments"),
5036 device_get_name(sc->dev));
5038 desc->sdp0 = htole32(segs[0].ds_addr);
5039 desc->sdl0 = htole16(MJUMPAGESIZE);
5042 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5043 BUS_DMASYNC_PREWRITE);
5045 return 0;
5047 fail:
5049 rt2860_free_rx_ring(sc, ring);
5051 return error;
5055 * rt2860_reset_rx_ring
5057 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5058 struct rt2860_softc_rx_ring *ring)
5060 struct rt2860_rxdesc *desc;
5061 int i;
5063 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5065 desc = &ring->desc[i];
5067 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5070 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5071 BUS_DMASYNC_PREWRITE);
5073 ring->cur = 0;
5077 * rt2860_free_rx_ring
5079 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5080 struct rt2860_softc_rx_ring *ring)
5082 struct rt2860_softc_rx_data *data;
5083 int i;
5085 if (ring->desc != NULL)
5087 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5088 BUS_DMASYNC_POSTWRITE);
5089 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5090 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5091 ring->desc_dma_map);
5094 if (ring->desc_dma_tag != NULL)
5095 bus_dma_tag_destroy(ring->desc_dma_tag);
5097 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5099 data = &ring->data[i];
5101 if (data->m != NULL)
5103 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5104 BUS_DMASYNC_POSTREAD);
5105 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5106 m_freem(data->m);
5109 if (data->dma_map != NULL)
5110 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5113 if (ring->data_dma_tag != NULL)
5114 bus_dma_tag_destroy(ring->data_dma_tag);
5118 * rt2860_alloc_tx_ring
5120 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5121 struct rt2860_softc_tx_ring *ring, int qid)
5123 struct rt2860_softc_tx_data *data;
5124 int error, i;
5126 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5127 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5128 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5129 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5130 0, NULL, NULL, &ring->desc_dma_tag);
5131 if (error != 0)
5133 printf("%s: could not create Tx desc DMA tag\n",
5134 device_get_nameunit(sc->dev));
5135 goto fail;
5138 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5139 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5140 if (error != 0)
5142 printf("%s: could not allocate Tx desc DMA memory\n",
5143 device_get_nameunit(sc->dev));
5144 goto fail;
5147 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5148 ring->desc,
5149 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5150 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5151 if (error != 0)
5153 printf("%s: could not load Tx desc DMA map\n",
5154 device_get_nameunit(sc->dev));
5155 goto fail;
5158 ring->desc_queued = 0;
5159 ring->desc_cur = 0;
5160 ring->desc_next = 0;
5162 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5163 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5164 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5165 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5166 0, NULL, NULL, &ring->seg0_dma_tag);
5167 if (error != 0)
5169 printf("%s: could not create Tx seg0 DMA tag\n",
5170 device_get_nameunit(sc->dev));
5171 goto fail;
5174 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5175 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5176 if (error != 0)
5178 printf("%s: could not allocate Tx seg0 DMA memory\n",
5179 device_get_nameunit(sc->dev));
5180 goto fail;
5183 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5184 ring->seg0,
5185 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5186 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5187 if (error != 0)
5189 printf("%s: could not load Tx seg0 DMA map\n",
5190 device_get_nameunit(sc->dev));
5191 goto fail;
5194 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5195 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5196 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5197 &ring->data_dma_tag);
5198 if (error != 0)
5200 printf("%s: could not create Tx data DMA tag\n",
5201 device_get_nameunit(sc->dev));
5202 goto fail;
5205 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5207 data = &ring->data[i];
5209 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5210 if (error != 0)
5212 printf("%s: could not create Tx data DMA map\n",
5213 device_get_nameunit(sc->dev));
5214 goto fail;
5218 ring->data_queued = 0;
5219 ring->data_cur = 0;
5220 ring->data_next = 0;
5222 ring->qid = qid;
5224 return 0;
5226 fail:
5228 rt2860_free_tx_ring(sc, ring);
5230 return error;
5234 * rt2860_reset_tx_ring
5236 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5237 struct rt2860_softc_tx_ring *ring)
5239 struct rt2860_softc_tx_data *data;
5240 struct rt2860_txdesc *desc;
5241 int i;
5243 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5245 desc = &ring->desc[i];
5247 desc->sdl0 = 0;
5248 desc->sdl1 = 0;
5251 ring->desc_queued = 0;
5252 ring->desc_cur = 0;
5253 ring->desc_next = 0;
5255 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5256 BUS_DMASYNC_PREWRITE);
5258 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5259 BUS_DMASYNC_PREWRITE);
5261 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5263 data = &ring->data[i];
5265 if (data->m != NULL)
5267 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5268 BUS_DMASYNC_POSTWRITE);
5269 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5270 m_freem(data->m);
5271 data->m = NULL;
5274 if (data->ni != NULL)
5276 ieee80211_free_node(data->ni);
5277 data->ni = NULL;
5281 ring->data_queued = 0;
5282 ring->data_cur = 0;
5283 ring->data_next = 0;
5287 * rt2860_free_tx_ring
5289 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5290 struct rt2860_softc_tx_ring *ring)
5292 struct rt2860_softc_tx_data *data;
5293 int i;
5295 if (ring->desc != NULL)
5297 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5298 BUS_DMASYNC_POSTWRITE);
5299 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5300 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5301 ring->desc_dma_map);
5304 if (ring->desc_dma_tag != NULL)
5305 bus_dma_tag_destroy(ring->desc_dma_tag);
5307 if (ring->seg0 != NULL)
5309 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5310 BUS_DMASYNC_POSTWRITE);
5311 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5312 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5313 ring->seg0_dma_map);
5316 if (ring->seg0_dma_tag != NULL)
5317 bus_dma_tag_destroy(ring->seg0_dma_tag);
5319 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5321 data = &ring->data[i];
5323 if (data->m != NULL)
5325 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5326 BUS_DMASYNC_POSTWRITE);
5327 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5328 m_freem(data->m);
5331 if (data->ni != NULL)
5332 ieee80211_free_node(data->ni);
5334 if (data->dma_map != NULL)
5335 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5338 if (ring->data_dma_tag != NULL)
5339 bus_dma_tag_destroy(ring->data_dma_tag);
5343 * rt2860_dma_map_addr
5345 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5346 int nseg, int error)
5348 if (error != 0)
5349 return;
5351 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5353 *(bus_addr_t *) arg = segs[0].ds_addr;
5357 * rt2860_sysctl_attach
5359 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5361 struct sysctl_ctx_list *ctx;
5362 struct sysctl_oid *tree;
5363 struct sysctl_oid *stats;
5365 ctx = device_get_sysctl_ctx(sc->dev);
5366 tree = device_get_sysctl_tree(sc->dev);
5368 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5369 "stats", CTLFLAG_RD, 0, "statistic");
5371 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5372 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5373 "all interrupts");
5375 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5376 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5377 "Tx coherent interrupts");
5379 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5380 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5381 "Rx coherent interrupts");
5383 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5384 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5385 "Tx/Rx coherent interrupts");
5387 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5388 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5389 "FIFO statistic full interrupts");
5391 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5392 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5393 "Rx interrupts");
5395 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5396 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5397 "Rx delay interrupts");
5399 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5400 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5401 "Tx MGMT interrupts");
5403 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5404 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5405 "Tx HCCA interrupts");
5407 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5408 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5409 "Tx AC3 interrupts");
5411 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5412 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5413 "Tx AC2 interrupts");
5415 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5416 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5417 "Tx AC1 interrupts");
5419 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5420 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5421 "Tx AC0 interrupts");
5423 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5424 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
5425 "Tx delay interrupts");
5427 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5428 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
5429 "Pre-TBTT interrupts");
5431 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5432 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
5433 "TBTT interrupts");
5435 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5436 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
5437 "MCU command interrupts");
5439 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5440 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
5441 "auto wakeup interrupts");
5443 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5444 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
5445 "GP timer interrupts");
5447 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5448 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
5449 "Tx MGMT descriptors queued");
5451 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5452 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
5453 "Tx MGMT data queued");
5455 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5456 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
5457 "Tx HCCA descriptors queued");
5459 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5460 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
5461 "Tx HCCA data queued");
5463 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5464 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
5465 "Tx AC3 descriptors queued");
5467 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5468 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
5469 "Tx AC3 data queued");
5471 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5472 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
5473 "Tx AC2 descriptors queued");
5475 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5476 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
5477 "Tx AC2 data queued");
5479 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5480 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
5481 "Tx AC1 descriptors queued");
5483 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5484 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
5485 "Tx AC1 data queued");
5487 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5488 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
5489 "Tx AC0 descriptors queued");
5491 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5492 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
5493 "Tx AC0 data queued");
5495 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5496 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5497 "Tx watchdog timeouts");
5499 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5500 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
5501 "Tx defragmented packets");
5503 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5504 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
5505 "no Tx descriptors available");
5507 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5508 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
5509 "Rx mbuf allocation errors");
5511 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5512 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
5513 "Rx mbuf DMA mapping errors");
5515 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5516 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5517 "Tx queue 0 not empty");
5519 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5520 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5521 "Tx queue 1 not empty");
5523 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5524 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
5525 "Rx packets");
5527 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5528 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
5529 "Rx CRC errors");
5531 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5532 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
5533 "Rx PHY errors");
5535 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5536 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
5537 "Rx false CCAs");
5539 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5540 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
5541 "Rx PLCP errors");
5543 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5544 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
5545 "Rx duplicate packets");
5547 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5548 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
5549 "Rx FIFO overflows");
5552 static device_method_t rt2860_dev_methods[] =
5554 DEVMETHOD(device_probe, rt2860_probe),
5555 DEVMETHOD(device_attach, rt2860_attach),
5556 DEVMETHOD(device_detach, rt2860_detach),
5557 DEVMETHOD(device_shutdown, rt2860_shutdown),
5558 DEVMETHOD(device_suspend, rt2860_suspend),
5559 DEVMETHOD(device_resume, rt2860_resume),
5560 { 0, 0 }
5563 static driver_t rt2860_driver =
5565 "rt2860",
5566 rt2860_dev_methods,
5567 sizeof(struct rt2860_softc)
5570 static devclass_t rt2860_dev_class;
5572 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
5574 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
5575 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);