AMSDU flag fix in rt2860_rx_eof
[ralink_drivers/rt2860_fbsd72.git] / rt2860.c
blob2c52fbe882594ab4565b72fbfce6b67340701abe
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_NODE(_hw, OID_AUTO, rt2860, CTLFLAG_RD, 0, "RT2860 driver parameters");
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 | /* HT greenfield */
562 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
563 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
564 IEEE80211_HTCAP_DSSSCCK40; /* HT 40MHz DSSS/CCK modulation */
566 /* spatial streams */
568 if (sc->nrxpath == 2)
569 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
570 else if (sc->nrxpath == 3)
571 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
572 else
573 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
575 if (sc->ntxpath > 1)
576 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
578 /* delayed BA */
580 if (sc->mac_rev != 0x28600100)
581 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
583 /* init channels */
585 ic->ic_nchans = 0;
586 ic->ic_regdomain = 0;
587 ic->ic_countrycode = CTRY_DEFAULT;
588 ic->ic_location = 0;
590 rt2860_init_channels(sc);
592 rt2860_init_channels_ht40(sc);
594 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->mac_addr);
596 ieee80211_ifattach(ic);
598 sc->newstate = ic->ic_newstate;
599 ic->ic_newstate = rt2860_newstate;
601 ic->ic_reset = rt2860_reset;
602 ic->ic_scan_start = rt2860_scan_start;
603 ic->ic_scan_end = rt2860_scan_end;
604 ic->ic_set_channel = rt2860_set_channel;
605 ic->ic_newassoc = rt2860_newassoc;
606 ic->ic_updateslot = rt2860_updateslot;
607 ic->ic_wme.wme_update = rt2860_wme_update;
608 ic->ic_update_beacon = rt2860_update_beacon;
609 ic->ic_crypto.cs_key_update_begin = rt2860_key_update_begin;
610 ic->ic_crypto.cs_key_update_end = rt2860_key_update_end;
611 ic->ic_crypto.cs_key_set = rt2860_key_set;
612 ic->ic_crypto.cs_key_delete = rt2860_key_delete;
613 ic->ic_raw_xmit = rt2860_raw_xmit;
615 sc->recv_action = ic->ic_recv_action;
616 ic->ic_recv_action = rt2860_recv_action;
618 sc->send_action = ic->ic_send_action;
619 ic->ic_send_action = rt2860_send_action;
621 /* hardware requires padding between 802.11 frame header and body */
623 ic->ic_flags |= IEEE80211_F_WME | IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
625 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
627 ieee80211_media_init(ic, rt2860_media_change, ieee80211_media_status);
629 rt2860_amrr_init(&sc->amrr, ic,
630 RT2860_AMRR_MIN_SUCCESS_THRESHOLD,
631 RT2860_AMRR_MAX_SUCCESS_THRESHOLD,
632 500);
634 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
635 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
636 &sc->drvbpf);
638 sc->rxtap_len = sizeof(sc->rxtapu);
639 sc->rxtap.ihdr.it_len = htole16(sc->rxtap_len);
640 sc->rxtap.ihdr.it_present = htole32(RT2860_SOFTC_RX_RADIOTAP_PRESENT);
642 sc->txtap_len = sizeof(sc->txtapu);
643 sc->txtap.ihdr.it_len = htole16(sc->txtap_len);
644 sc->txtap.ihdr.it_present = htole32(RT2860_SOFTC_TX_RADIOTAP_PRESENT);
646 /* init task queue */
648 TASK_INIT(&sc->rx_done_task, 0, rt2860_rx_done_task, sc);
649 TASK_INIT(&sc->tx_done_task, 0, rt2860_tx_done_task, sc);
650 TASK_INIT(&sc->fifo_sta_full_task, 0, rt2860_fifo_sta_full_task, sc);
651 TASK_INIT(&sc->periodic_task, 0, rt2860_periodic_task, sc);
653 sc->rx_process_limit = 100;
655 sc->taskqueue = taskqueue_create("rt2860_taskq", M_NOWAIT,
656 taskqueue_thread_enqueue, &sc->taskqueue);
658 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
659 device_get_nameunit(sc->dev));
661 rt2860_sysctl_attach(sc);
663 if (bootverbose)
664 ieee80211_announce(ic);
666 /* set up interrupt */
668 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
669 NULL, rt2860_intr, sc, &sc->irqh);
670 if (error != 0)
672 printf("%s: could not set up interrupt\n",
673 device_get_nameunit(dev));
674 goto fail;
677 return 0;
679 fail:
681 /* free Tx and Rx rings */
683 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
684 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
686 rt2860_free_rx_ring(sc, &sc->rx_ring);
688 mtx_destroy(&sc->lock);
690 if (sc->mem != NULL)
691 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
693 if (sc->irq != NULL)
694 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
696 return error;
700 * rt2860_detach
702 static int rt2860_detach(device_t dev)
704 struct rt2860_softc *sc;
705 struct ieee80211com *ic;
706 struct ifnet *ifp;
707 int i;
709 sc = device_get_softc(dev);
710 ic = &sc->ic;
711 ifp = ic->ic_ifp;
713 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
714 "%s: detaching\n",
715 device_get_nameunit(sc->dev));
717 RT2860_SOFTC_LOCK(sc);
719 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
721 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
723 callout_stop(&sc->periodic_ch);
724 callout_stop(&sc->tx_watchdog_ch);
726 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
727 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
728 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
729 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
731 /* free Tx and Rx rings */
733 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
734 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
736 rt2860_free_rx_ring(sc, &sc->rx_ring);
738 RT2860_SOFTC_UNLOCK(sc);
740 bpfdetach(ifp);
742 ieee80211_ifdetach(ic);
744 if_free(ifp);
746 taskqueue_free(sc->taskqueue);
748 mtx_destroy(&sc->lock);
750 bus_generic_detach(dev);
752 bus_teardown_intr(dev, sc->irq, sc->irqh);
754 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
756 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
758 return 0;
762 * rt2860_shutdown
764 static int rt2860_shutdown(device_t dev)
766 struct rt2860_softc *sc;
768 sc = device_get_softc(dev);
770 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
771 "%s: shutting down\n",
772 device_get_nameunit(sc->dev));
774 rt2860_stop(sc);
776 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
778 return 0;
782 * rt2860_suspend
784 static int rt2860_suspend(device_t dev)
786 struct rt2860_softc *sc;
788 sc = device_get_softc(dev);
790 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
791 "%s: suspending\n",
792 device_get_nameunit(sc->dev));
794 rt2860_stop(sc);
796 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
798 return 0;
802 * rt2860_resume
804 static int rt2860_resume(device_t dev)
806 struct rt2860_softc *sc;
807 struct ifnet *ifp;
809 sc = device_get_softc(dev);
810 ifp = sc->ifp;
812 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
813 "%s: resuming\n",
814 device_get_nameunit(sc->dev));
816 if (ifp->if_flags & IFF_UP)
817 rt2860_init(sc);
819 return 0;
823 * rt2860_init_channels
825 static void rt2860_init_channels(struct rt2860_softc *sc)
827 struct ieee80211com *ic;
828 struct ieee80211_channel *c;
829 int i, flags;
831 ic = &sc->ic;
833 /* set supported channels for 2GHz band */
835 for (i = 1; i <= 14; i++)
837 c = &ic->ic_channels[ic->ic_nchans++];
838 flags = IEEE80211_CHAN_B;
840 c->ic_freq = ieee80211_ieee2mhz(i, flags);
841 c->ic_ieee = i;
842 c->ic_flags = flags;
844 c = &ic->ic_channels[ic->ic_nchans++];
845 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
847 c->ic_freq = ieee80211_ieee2mhz(i, flags);
848 c->ic_ieee = i;
849 c->ic_flags = flags;
851 c = &ic->ic_channels[ic->ic_nchans++];
852 flags = IEEE80211_CHAN_G;
854 c->ic_freq = ieee80211_ieee2mhz(i, flags);
855 c->ic_ieee = i;
856 c->ic_flags = flags;
858 c = &ic->ic_channels[ic->ic_nchans++];
859 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
861 c->ic_freq = ieee80211_ieee2mhz(i, flags);
862 c->ic_ieee = i;
863 c->ic_flags = flags;
866 /* set supported channels for 5GHz band */
868 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
869 sc->rf_rev == RT2860_EEPROM_RF_2750)
871 for (i = 36; i <= 64; 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 = 100; i <= 140; 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;
905 for (i = 149; i <= 165; i += 4)
907 c = &ic->ic_channels[ic->ic_nchans++];
908 flags = IEEE80211_CHAN_A;
910 c->ic_freq = ieee80211_ieee2mhz(i, flags);
911 c->ic_ieee = i;
912 c->ic_flags = flags;
914 c = &ic->ic_channels[ic->ic_nchans++];
915 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
917 c->ic_freq = ieee80211_ieee2mhz(i, flags);
918 c->ic_ieee = i;
919 c->ic_flags = flags;
925 * rt2860_init_channels_ht40
927 static void rt2860_init_channels_ht40(struct rt2860_softc *sc)
929 struct ieee80211com *ic;
930 struct ieee80211_channel *c, *cent, *ext;
931 int i, flags;
933 ic = &sc->ic;
935 /* set supported channels for 2GHz band */
937 for (i = 1; i <= 14; i++)
939 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
941 /* find the center channel */
943 cent = ieee80211_find_channel_byieee(ic, i,
944 flags & ~IEEE80211_CHAN_HT);
945 if (cent == NULL)
947 printf("%s: skip channel %d, could not find center channel\n",
948 device_get_nameunit(sc->dev), i);
949 continue;
952 /* find the extension channel */
954 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
955 flags & ~IEEE80211_CHAN_HT);
956 if (ext == NULL)
958 printf("%s: skip channel %d, could not find extension channel\n",
959 device_get_nameunit(sc->dev), i);
960 continue;
963 c = &ic->ic_channels[ic->ic_nchans++];
965 *c = *cent;
966 c->ic_extieee = ext->ic_ieee;
967 c->ic_flags &= ~IEEE80211_CHAN_HT;
968 c->ic_flags |= IEEE80211_CHAN_HT40U;
970 c = &ic->ic_channels[ic->ic_nchans++];
972 *c = *ext;
973 c->ic_extieee = cent->ic_ieee;
974 c->ic_flags &= ~IEEE80211_CHAN_HT;
975 c->ic_flags |= IEEE80211_CHAN_HT40D;
978 /* set supported channels for 5GHz band */
980 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
981 sc->rf_rev == RT2860_EEPROM_RF_2750)
983 for (i = 36; i <= 64; i += 4)
985 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
987 /* find the center channel */
989 cent = ieee80211_find_channel_byieee(ic, i,
990 flags & ~IEEE80211_CHAN_HT);
991 if (cent == NULL)
993 printf("%s: skip channel %d, could not find center channel\n",
994 device_get_nameunit(sc->dev), i);
995 continue;
998 /* find the extension channel */
1000 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1001 flags & ~IEEE80211_CHAN_HT);
1002 if (ext == NULL)
1004 printf("%s: skip channel %d, could not find extension channel\n",
1005 device_get_nameunit(sc->dev), i);
1006 continue;
1009 c = &ic->ic_channels[ic->ic_nchans++];
1011 *c = *cent;
1012 c->ic_extieee = ext->ic_ieee;
1013 c->ic_flags &= ~IEEE80211_CHAN_HT;
1014 c->ic_flags |= IEEE80211_CHAN_HT40U;
1016 c = &ic->ic_channels[ic->ic_nchans++];
1018 *c = *ext;
1019 c->ic_extieee = cent->ic_ieee;
1020 c->ic_flags &= ~IEEE80211_CHAN_HT;
1021 c->ic_flags |= IEEE80211_CHAN_HT40D;
1024 for (i = 100; i <= 140; i += 4)
1026 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1028 /* find the center channel */
1030 cent = ieee80211_find_channel_byieee(ic, i,
1031 flags & ~IEEE80211_CHAN_HT);
1032 if (cent == NULL)
1034 printf("%s: skip channel %d, could not find center channel\n",
1035 device_get_nameunit(sc->dev), i);
1036 continue;
1039 /* find the extension channel */
1041 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1042 flags & ~IEEE80211_CHAN_HT);
1043 if (ext == NULL)
1045 printf("%s: skip channel %d, could not find extension channel\n",
1046 device_get_nameunit(sc->dev), i);
1047 continue;
1050 c = &ic->ic_channels[ic->ic_nchans++];
1052 *c = *cent;
1053 c->ic_extieee = ext->ic_ieee;
1054 c->ic_flags &= ~IEEE80211_CHAN_HT;
1055 c->ic_flags |= IEEE80211_CHAN_HT40U;
1057 c = &ic->ic_channels[ic->ic_nchans++];
1059 *c = *ext;
1060 c->ic_extieee = cent->ic_ieee;
1061 c->ic_flags &= ~IEEE80211_CHAN_HT;
1062 c->ic_flags |= IEEE80211_CHAN_HT40D;
1065 for (i = 149; i <= 165; i += 4)
1067 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1069 /* find the center channel */
1071 cent = ieee80211_find_channel_byieee(ic, i,
1072 flags & ~IEEE80211_CHAN_HT);
1073 if (cent == NULL)
1075 printf("%s: skip channel %d, could not find center channel\n",
1076 device_get_nameunit(sc->dev), i);
1077 continue;
1080 /* find the extension channel */
1082 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1083 flags & ~IEEE80211_CHAN_HT);
1084 if (ext == NULL)
1086 printf("%s: skip channel %d, could not find extension channel\n",
1087 device_get_nameunit(sc->dev), i);
1088 continue;
1091 c = &ic->ic_channels[ic->ic_nchans++];
1093 *c = *cent;
1094 c->ic_extieee = ext->ic_ieee;
1095 c->ic_flags &= ~IEEE80211_CHAN_HT;
1096 c->ic_flags |= IEEE80211_CHAN_HT40U;
1098 c = &ic->ic_channels[ic->ic_nchans++];
1100 *c = *ext;
1101 c->ic_extieee = cent->ic_ieee;
1102 c->ic_flags &= ~IEEE80211_CHAN_HT;
1103 c->ic_flags |= IEEE80211_CHAN_HT40D;
1109 * rt2860_init_locked
1111 static void rt2860_init_locked(void *priv)
1113 struct rt2860_softc *sc;
1114 struct ieee80211com *ic;
1115 struct ifnet *ifp;
1116 int error, i, ntries;
1117 uint32_t tmp, stacnt[6];
1119 sc = priv;
1120 ic = &sc->ic;
1121 ifp = ic->ic_ifp;
1123 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1124 "%s: initializing\n",
1125 device_get_nameunit(sc->dev));
1127 RT2860_SOFTC_ASSERT_LOCKED(sc);
1129 if (!(sc->flags & RT2860_SOFTC_FLAGS_UCODE_LOADED))
1131 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1132 "%s: loading 8051 microcode\n",
1133 device_get_nameunit(sc->dev));
1135 error = rt2860_io_mcu_load_ucode(sc, rt2860_ucode, sizeof(rt2860_ucode));
1136 if (error != 0)
1138 printf("%s: could not load 8051 microcode\n",
1139 device_get_nameunit(sc->dev));
1140 goto fail;
1143 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1144 "%s: 8051 microcode was successfully loaded\n",
1145 device_get_nameunit(sc->dev));
1147 sc->flags |= RT2860_SOFTC_FLAGS_UCODE_LOADED;
1150 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x2);
1152 /* disable DMA engine */
1154 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1156 tmp &= 0xff0;
1157 tmp |= RT2860_REG_TX_WB_DDONE;
1159 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1161 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, 0xffffffff);
1163 /* PBF hardware reset */
1165 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1166 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1168 /* wait while DMA engine is busy */
1170 for (ntries = 0; ntries < 100; ntries++)
1172 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1173 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1174 break;
1176 DELAY(1000);
1179 if (ntries == 100)
1181 printf("%s: timeout waiting for DMA engine\n",
1182 device_get_nameunit(sc->dev));
1183 goto fail;
1186 tmp &= 0xff0;
1187 tmp |= RT2860_REG_TX_WB_DDONE;
1189 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1191 /* reset Rx and Tx rings */
1193 tmp = RT2860_REG_RST_IDX_RX |
1194 RT2860_REG_RST_IDX_TX_MGMT |
1195 RT2860_REG_RST_IDX_TX_HCCA |
1196 RT2860_REG_RST_IDX_TX_AC3 |
1197 RT2860_REG_RST_IDX_TX_AC2 |
1198 RT2860_REG_RST_IDX_TX_AC1 |
1199 RT2860_REG_RST_IDX_TX_AC0;
1201 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, tmp);
1203 /* PBF hardware reset */
1205 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1206 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1208 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x3);
1210 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1211 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1212 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1214 /* init Tx power per rate */
1216 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
1218 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1219 continue;
1221 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i),
1222 sc->txpow_rate_20mhz[i]);
1225 for (i = 0; i < RT2860_DEF_MAC_SIZE; i++)
1226 rt2860_io_mac_write(sc, rt2860_def_mac[i].reg,
1227 rt2860_def_mac[i].val);
1229 /* wait while MAC is busy */
1231 for (ntries = 0; ntries < 100; ntries++)
1233 if (!(rt2860_io_mac_read(sc, RT2860_REG_STATUS_CFG) &
1234 (RT2860_REG_STATUS_TX_BUSY | RT2860_REG_STATUS_RX_BUSY)))
1235 break;
1237 DELAY(1000);
1240 if (ntries == 100)
1242 printf("%s: timeout waiting for MAC\n",
1243 device_get_nameunit(sc->dev));
1244 goto fail;
1247 /* clear Host to MCU mailbox */
1249 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX_BBP_AGENT, 0);
1250 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX, 0);
1252 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_BOOT,
1253 RT2860_REG_H2M_TOKEN_NO_INTR, 0);
1255 DELAY(1000);
1257 error = rt2860_init_bbp(sc);
1258 if (error != 0)
1259 goto fail;
1261 /* set up maximum buffer sizes */
1263 tmp = (1 << 12) | RT2860_MAX_AGG_SIZE;
1265 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1267 /* set mac address */
1269 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1271 rt2860_asic_set_macaddr(sc, ic->ic_myaddr);
1273 /* clear statistic registers */
1275 rt2860_io_mac_read_multi(sc, RT2860_REG_RX_STA_CNT0,
1276 stacnt, sizeof(stacnt));
1278 /* set RTS threshold */
1280 rt2860_asic_update_rtsthreshold(sc);
1282 /* set Tx power */
1284 rt2860_asic_update_txpower(sc);
1286 /* set up protection mode */
1288 rt2860_asic_updateprot(sc);
1290 /* clear key tables */
1292 rt2860_asic_clear_keytables(sc);
1294 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
1296 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1297 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
1299 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1301 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
1302 sc->tx_ring[i].desc_phys_addr);
1303 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
1304 RT2860_SOFTC_TX_RING_DESC_COUNT);
1305 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
1308 /* init Rx ring */
1310 rt2860_reset_rx_ring(sc, &sc->rx_ring);
1312 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
1313 sc->rx_ring.desc_phys_addr);
1314 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
1315 RT2860_SOFTC_RX_RING_DATA_COUNT);
1316 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
1317 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
1319 /* wait while DMA engine is busy */
1321 for (ntries = 0; ntries < 100; ntries++)
1323 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1324 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1325 break;
1327 DELAY(1000);
1330 if (ntries == 100)
1332 printf("%s: timeout waiting for DMA engine\n",
1333 device_get_nameunit(sc->dev));
1334 goto fail;
1337 tmp &= 0xff0;
1338 tmp |= RT2860_REG_TX_WB_DDONE;
1340 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1342 /* disable interrupts mitigation */
1344 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_DELAY_INT_CFG, 0);
1346 /* send LEDs operating mode to microcontroller */
1348 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED1,
1349 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1350 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED2,
1351 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1352 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED3,
1353 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1355 /* turn radio LED on */
1357 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON);
1359 /* write vendor-specific BBP values (from EEPROM) */
1361 for (i = 0; i < RT2860_SOFTC_BBP_EEPROM_COUNT; i++)
1363 if (sc->bbp_eeprom[i].reg == 0x00 ||
1364 sc->bbp_eeprom[i].reg == 0xff)
1365 continue;
1367 rt2860_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1368 sc->bbp_eeprom[i].val);
1372 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_GPIO_CTRL_CFG);
1373 if (tmp & (1 << 2))
1375 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_SLEEP,
1376 RT2860_REG_H2M_TOKEN_RADIOOFF, 0x02ff);
1377 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_RADIOOFF);
1379 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_WAKEUP,
1380 RT2860_REG_H2M_TOKEN_WAKEUP, 0);
1381 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_WAKEUP);
1385 /* disable non-existing Rx chains */
1387 tmp = rt2860_io_bbp_read(sc, 3);
1389 tmp &= ~((1 << 4) | (1 << 3));
1391 if (sc->nrxpath == 3)
1392 tmp |= (1 << 4);
1393 else if (sc->nrxpath == 2)
1394 tmp |= (1 << 3);
1396 rt2860_io_bbp_write(sc, 3, tmp);
1398 /* disable non-existing Tx chains */
1400 tmp = rt2860_io_bbp_read(sc, 1);
1402 tmp &= ~((1 << 4) | (1 << 3));
1404 if (sc->ntxpath == 2)
1405 tmp |= (1 << 4);
1407 rt2860_io_bbp_write(sc, 1, tmp);
1409 /* set current channel */
1411 rt2860_rf_set_chan(sc, ic->ic_curchan);
1413 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1414 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1415 (48 << 16) | 96);
1417 if ((sc->mac_rev & 0xffff) != 0x0101)
1418 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1420 /* clear pending interrupts */
1422 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1424 /* enable interrupts */
1426 tmp = RT2860_REG_INT_TX_COHERENT |
1427 RT2860_REG_INT_RX_COHERENT |
1428 RT2860_REG_INT_GP_TIMER |
1429 RT2860_REG_INT_AUTO_WAKEUP |
1430 RT2860_REG_INT_FIFO_STA_FULL |
1432 RT2860_REG_INT_PRE_TBTT |
1433 RT2860_REG_INT_TBTT |
1435 RT2860_REG_INT_TXRX_COHERENT |
1436 RT2860_REG_INT_MCU_CMD |
1437 RT2860_REG_INT_TX_MGMT_DONE |
1438 RT2860_REG_INT_TX_HCCA_DONE |
1439 RT2860_REG_INT_TX_AC3_DONE |
1440 RT2860_REG_INT_TX_AC2_DONE |
1441 RT2860_REG_INT_TX_AC1_DONE |
1442 RT2860_REG_INT_TX_AC0_DONE |
1443 RT2860_REG_INT_RX_DONE;
1445 sc->intr_enable_mask = tmp;
1447 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1449 if (rt2860_txrx_enable(sc) != 0)
1450 goto fail;
1452 /* clear garbage interrupts */
1454 tmp = rt2860_io_mac_read(sc, 0x1300);
1456 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1457 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1459 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1461 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1462 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1464 else
1466 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1469 sc->periodic_round = 0;
1471 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1473 return;
1475 fail:
1477 rt2860_stop_locked(sc);
1481 * rt2860_init
1483 static void rt2860_init(void *priv)
1485 struct rt2860_softc *sc;
1487 sc = priv;
1489 RT2860_SOFTC_LOCK(sc);
1491 rt2860_init_locked(sc);
1493 RT2860_SOFTC_UNLOCK(sc);
1497 * rt2860_init_bbp
1499 static int rt2860_init_bbp(struct rt2860_softc *sc)
1501 int ntries, i;
1502 uint8_t tmp;
1504 for (ntries = 0; ntries < 20; ntries++)
1506 tmp = rt2860_io_bbp_read(sc, 0);
1507 if (tmp != 0x00 && tmp != 0xff)
1508 break;
1511 if (tmp == 0x00 || tmp == 0xff)
1513 printf("%s: timeout waiting for BBP to wakeup\n",
1514 device_get_nameunit(sc->dev));
1515 return ETIMEDOUT;
1518 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1519 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1520 rt2860_def_bbp[i].val);
1522 if ((sc->mac_rev & 0xffff) != 0x0101)
1523 rt2860_io_bbp_write(sc, 84, 0x19);
1525 return 0;
1529 * rt2860_stop_locked
1531 static void rt2860_stop_locked(void *priv)
1533 struct rt2860_softc *sc;
1534 struct ieee80211com *ic;
1535 struct ifnet *ifp;
1536 uint32_t tmp;
1538 sc = priv;
1539 ic = &sc->ic;
1540 ifp = ic->ic_ifp;
1542 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1543 "%s: stopping\n",
1544 device_get_nameunit(sc->dev));
1546 RT2860_SOFTC_ASSERT_LOCKED(sc);
1548 sc->tx_timer = 0;
1550 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1551 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1553 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1555 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1557 callout_stop(&sc->periodic_ch);
1558 callout_stop(&sc->tx_watchdog_ch);
1560 RT2860_SOFTC_UNLOCK(sc);
1562 taskqueue_block(sc->taskqueue);
1564 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1565 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1566 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1567 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1569 RT2860_SOFTC_LOCK(sc);
1571 /* clear key tables */
1573 rt2860_asic_clear_keytables(sc);
1575 /* disable interrupts */
1577 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1579 /* disable Tx/Rx */
1581 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1583 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1585 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1587 /* reset adapter */
1589 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1590 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1591 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1595 * rt2860_stop
1597 static void rt2860_stop(void *priv)
1599 struct rt2860_softc *sc;
1601 sc = priv;
1603 RT2860_SOFTC_LOCK(sc);
1605 rt2860_stop_locked(sc);
1607 RT2860_SOFTC_UNLOCK(sc);
1611 * rt2860_start
1613 static void rt2860_start(struct ifnet *ifp)
1615 struct rt2860_softc *sc;
1616 struct ieee80211com *ic;
1617 struct ieee80211_node *ni;
1618 struct ether_header *eh;
1619 struct mbuf *m;
1620 int qid;
1622 sc = ifp->if_softc;
1623 ic = &sc->ic;
1625 RT2860_SOFTC_LOCK(sc);
1627 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1629 RT2860_SOFTC_UNLOCK(sc);
1630 return;
1633 for (;;)
1635 IF_POLL(&ic->ic_mgtq, m);
1636 if (m != NULL)
1638 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1640 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1641 "%s: if_start: Tx ring with qid=%d is full\n",
1642 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
1644 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1645 break;
1648 IF_DEQUEUE(&ic->ic_mgtq, m);
1650 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1651 m->m_pkthdr.rcvif = NULL;
1653 if (bpf_peers_present(ic->ic_rawbpf))
1654 bpf_mtap(ic->ic_rawbpf, m);
1656 if (rt2860_tx_frame(sc, m, ni, sc->tx_ring_mgtqid) != 0)
1657 break;
1659 rt2860_drain_fifo_stats(sc);
1661 else
1663 if (ic->ic_state != IEEE80211_S_RUN)
1664 break;
1666 IF_POLL(&ifp->if_snd, m);
1667 if (m == NULL)
1668 break;
1670 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1672 if (ic->ic_flags & IEEE80211_F_SCAN)
1673 ieee80211_cancel_scan(ic);
1675 if (m->m_len < sizeof(struct ether_header) &&
1676 !(m = m_pullup(m, sizeof (struct ether_header))))
1677 continue;
1679 eh = mtod(m, struct ether_header *);
1681 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1682 if (ni == NULL)
1684 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1685 "%s: if_start: could not find Tx node\n",
1686 device_get_nameunit(sc->dev));
1688 m_freem(m);
1689 continue;
1692 ieee80211_classify(ic, m, ni);
1694 qid = M_WME_GETAC(m);
1696 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1698 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1699 "%s: if_start: Tx ring with qid=%d is full\n",
1700 device_get_nameunit(sc->dev), qid);
1702 m_freem(m);
1703 ieee80211_free_node(ni);
1704 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1705 ifp->if_oerrors++;
1706 break;
1709 BPF_MTAP(ifp, m);
1711 m = ieee80211_encap(ic, m, ni);
1712 if (m == NULL)
1714 ieee80211_free_node(ni);
1715 ifp->if_oerrors++;
1716 continue;
1719 if (bpf_peers_present(ic->ic_rawbpf))
1720 bpf_mtap(ic->ic_rawbpf, m);
1722 if (rt2860_tx_frame(sc, m, ni, qid) != 0)
1724 ieee80211_free_node(ni);
1725 ifp->if_oerrors++;
1726 break;
1729 rt2860_drain_fifo_stats(sc);
1732 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1734 ic->ic_lastdata = ticks;
1736 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1739 RT2860_SOFTC_UNLOCK(sc);
1743 * rt2860_ioctl
1745 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1747 struct rt2860_softc *sc;
1748 struct ieee80211com *ic;
1749 int error;
1751 sc = ifp->if_softc;
1752 ic = &sc->ic;
1754 error = 0;
1756 switch (cmd)
1758 case SIOCSIFFLAGS:
1759 RT2860_SOFTC_LOCK(sc);
1761 if (ifp->if_flags & IFF_UP)
1763 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1765 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1766 rt2860_asic_update_promisc(sc);
1768 else
1770 rt2860_init_locked(sc);
1773 else
1775 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1776 rt2860_stop_locked(sc);
1779 sc->if_flags = ifp->if_flags;
1781 RT2860_SOFTC_UNLOCK(sc);
1782 break;
1784 default:
1785 error = ieee80211_ioctl(ic, cmd, data);
1788 if (error == ENETRESET)
1790 RT2860_SOFTC_LOCK(sc);
1792 if ((ifp->if_flags & IFF_UP) &&
1793 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1794 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1796 rt2860_stop_locked(sc);
1797 rt2860_init_locked(sc);
1800 RT2860_SOFTC_UNLOCK(sc);
1802 error = 0;
1805 return error;
1809 * rt2860_reset
1811 static int rt2860_reset(struct ifnet *ifp)
1813 struct rt2860_softc *sc;
1814 struct ieee80211com *ic;
1816 sc = ifp->if_softc;
1817 ic = &sc->ic;
1819 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1820 return ENETRESET;
1822 rt2860_rf_set_chan(sc, ic->ic_curchan);
1824 return 0;
1828 * rt2860_newstate
1830 static int rt2860_newstate(struct ieee80211com *ic,
1831 enum ieee80211_state nstate, int arg)
1833 struct rt2860_softc *sc;
1834 struct ifnet *ifp;
1835 struct ieee80211_node *ni;
1836 int error;
1838 ifp = ic->ic_ifp;
1839 sc = ifp->if_softc;
1841 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1842 "%s: newstate: %s -> %s\n",
1843 device_get_nameunit(sc->dev),
1844 ieee80211_state_name[ic->ic_state], ieee80211_state_name[nstate]);
1846 error = sc->newstate(ic, nstate, arg);
1847 if (error != 0)
1848 return error;
1850 RT2860_SOFTC_LOCK(sc);
1852 /* turn link LED off */
1854 if (nstate != IEEE80211_S_RUN)
1855 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1857 switch (nstate)
1859 case IEEE80211_S_INIT:
1860 rt2860_asic_disable_tsf_sync(sc);
1861 break;
1863 case IEEE80211_S_RUN:
1864 ni = ic->ic_bss;
1866 rt2860_rf_set_chan(sc, ni->ni_chan);
1868 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1870 rt2860_asic_enable_mrr(sc);
1871 rt2860_asic_set_txpreamble(sc);
1872 rt2860_asic_set_basicrates(sc);
1873 rt2860_asic_update_txpower(sc);
1874 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1877 if (ic->ic_opmode == IEEE80211_M_STA)
1878 rt2860_newassoc(ni, 1);
1880 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1881 ic->ic_opmode == IEEE80211_M_IBSS)
1883 error = rt2860_asic_update_beacon(sc);
1884 if (error != 0)
1885 break;
1888 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1889 rt2860_asic_enable_tsf_sync(sc);
1891 /* turn link LED on */
1893 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1895 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1896 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1897 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
1899 break;
1901 default:
1902 break;
1905 RT2860_SOFTC_UNLOCK(sc);
1907 return error;
1911 * rt2860_scan_start
1913 static void rt2860_scan_start(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_disable_tsf_sync(sc);
1925 * rt2860_scan_end
1927 static void rt2860_scan_end(struct ieee80211com *ic)
1929 struct rt2860_softc *sc;
1930 struct ifnet *ifp;
1932 ifp = ic->ic_ifp;
1933 sc = ifp->if_softc;
1935 rt2860_asic_enable_tsf_sync(sc);
1939 * rt2860_set_channel
1941 static void rt2860_set_channel(struct ieee80211com *ic)
1943 struct rt2860_softc *sc;
1944 struct ifnet *ifp;
1946 ifp = ic->ic_ifp;
1947 sc = ifp->if_softc;
1949 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
1950 "%s: set channel: channel=%u, HT%s%s\n",
1951 device_get_nameunit(sc->dev),
1952 ieee80211_chan2ieee(ic, ic->ic_curchan),
1953 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
1954 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
1955 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
1956 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
1958 RT2860_SOFTC_LOCK(sc);
1960 rt2860_rf_set_chan(sc, ic->ic_curchan);
1962 RT2860_SOFTC_UNLOCK(sc);
1966 * rt2860_newassoc
1968 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
1970 struct rt2860_softc *sc;
1971 struct ieee80211com *ic;
1972 struct ifnet *ifp;
1973 uint16_t associd;
1974 uint8_t wcid;
1976 ic = ni->ni_ic;
1977 ifp = ic->ic_ifp;
1978 sc = ifp->if_softc;
1980 associd = (ni != NULL) ? ni->ni_associd : 0;
1981 wcid = RT2860_AID2WCID(associd);
1983 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
1984 "%s: new association: wcid=0x%02x, "
1985 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
1986 device_get_nameunit(sc->dev), wcid,
1987 ether_sprintf(ni->ni_macaddr),
1988 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
1989 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
1990 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
1992 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
1993 ni->ni_macaddr, IEEE80211_ADDR_LEN);
1995 rt2860_amrr_node_init(&sc->amrr, &sc->amrr_node[wcid], ni);
1997 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
1998 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
1999 device_get_nameunit(sc->dev),
2000 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2001 ni->ni_associd,
2002 (ni->ni_flags & IEEE80211_NODE_HT) ?
2003 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2004 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2005 (ni->ni_flags & IEEE80211_NODE_HT) ?
2006 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2007 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2009 rt2860_asic_updateprot(sc);
2010 rt2860_asic_updateslot(sc);
2011 rt2860_asic_set_txpreamble(sc);
2015 * rt2860_updateslot
2017 static void rt2860_updateslot(struct ifnet *ifp)
2019 struct rt2860_softc *sc;
2021 sc = ifp->if_softc;
2023 rt2860_asic_updateslot(sc);
2027 * rt2860_wme_update
2029 static int rt2860_wme_update(struct ieee80211com *ic)
2031 struct rt2860_softc *sc;
2032 struct ifnet *ifp;
2034 ifp = ic->ic_ifp;
2035 sc = ifp->if_softc;
2037 rt2860_asic_wme_update(sc);
2039 return 0;
2043 * rt2860_update_beacon
2045 static void rt2860_update_beacon(struct ieee80211com *ic, int what)
2047 struct rt2860_softc *sc;
2048 struct ifnet *ifp;
2050 ifp = ic->ic_ifp;
2051 sc = ifp->if_softc;
2053 rt2860_asic_update_beacon(sc);
2057 * rt2860_key_update_begin
2059 static void rt2860_key_update_begin(struct ieee80211com *ic)
2061 struct rt2860_softc *sc;
2062 struct ifnet *ifp;
2064 ifp = ic->ic_ifp;
2065 sc = ifp->if_softc;
2067 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2068 "%s: key update begin\n",
2069 device_get_nameunit(sc->dev));
2071 taskqueue_block(sc->taskqueue);
2073 IF_LOCK(&ifp->if_snd);
2077 * rt2860_key_update_end
2079 static void rt2860_key_update_end(struct ieee80211com *ic)
2081 struct rt2860_softc *sc;
2082 struct ifnet *ifp;
2084 ifp = ic->ic_ifp;
2085 sc = ifp->if_softc;
2087 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2088 "%s: key update end\n",
2089 device_get_nameunit(sc->dev));
2091 IF_UNLOCK(&ifp->if_snd);
2093 taskqueue_unblock(sc->taskqueue);
2097 * rt2860_key_set
2099 static int rt2860_key_set(struct ieee80211com *ic,
2100 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2102 struct rt2860_softc *sc;
2103 struct ifnet *ifp;
2104 struct ieee80211_node *ni;
2105 uint16_t associd, key_base, keymode_base;
2106 uint8_t mode, vapid, wcid, iv[8];
2107 uint32_t tmp;
2109 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2110 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2111 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2112 return EINVAL;
2114 ifp = ic->ic_ifp;
2115 sc = ifp->if_softc;
2117 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2118 ni = ic->ic_bss;
2119 else
2120 ni = ieee80211_find_node(&ic->ic_sta, mac);
2122 associd = (ni != NULL) ? ni->ni_associd : 0;
2124 if ((ic->ic_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2125 ieee80211_free_node(ni);
2127 switch (k->wk_cipher->ic_cipher)
2129 case IEEE80211_CIPHER_WEP:
2130 if(k->wk_keylen < 8)
2131 mode = RT2860_REG_CIPHER_MODE_WEP40;
2132 else
2133 mode = RT2860_REG_CIPHER_MODE_WEP104;
2134 break;
2136 case IEEE80211_CIPHER_TKIP:
2137 mode = RT2860_REG_CIPHER_MODE_TKIP;
2138 break;
2140 case IEEE80211_CIPHER_AES_CCM:
2141 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2142 break;
2144 default:
2145 return EINVAL;
2148 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2149 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2150 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2151 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2153 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2155 /* install pairwise key */
2157 vapid = 0;
2158 wcid = RT2860_AID2WCID(associd);
2159 key_base = RT2860_REG_PKEY(wcid);
2161 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2163 memset(iv, 0, 8);
2165 iv[3] = (k->wk_keyix << 6);
2167 else
2169 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2171 iv[0] = (k->wk_keytsc >> 8);
2172 iv[1] = ((iv[0] | 0x20) & 0x7f);
2173 iv[2] = k->wk_keytsc;
2175 else
2177 /* AES CCMP */
2179 iv[0] = k->wk_keytsc;
2180 iv[1] = k->wk_keytsc >> 8;
2181 iv[2] = 0;
2184 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2185 iv[4] = (k->wk_keytsc >> 16);
2186 iv[5] = (k->wk_keytsc >> 24);
2187 iv[6] = (k->wk_keytsc >> 32);
2188 iv[7] = (k->wk_keytsc >> 40);
2190 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2191 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2192 device_get_nameunit(sc->dev),
2193 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2196 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2198 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2200 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2202 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2204 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2205 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2207 else
2209 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2210 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2213 else
2215 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2218 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2219 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2221 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2224 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2225 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2227 /* install group key */
2229 vapid = 0;
2230 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2231 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2233 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2235 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2237 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2239 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2240 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2242 else
2244 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2245 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2248 else
2250 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2253 tmp = rt2860_io_mac_read(sc, keymode_base);
2255 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2256 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2258 rt2860_io_mac_write(sc, keymode_base, tmp);
2261 return 1;
2265 * rt2860_key_delete
2267 static int rt2860_key_delete(struct ieee80211com *ic,
2268 const struct ieee80211_key *k)
2270 struct rt2860_softc *sc;
2271 struct ieee80211_node *ni;
2272 uint16_t associd;
2273 uint8_t vapid, wcid;
2274 uint32_t tmp;
2276 sc = ic->ic_ifp->if_softc;
2277 ni = ic->ic_bss;
2278 associd = (ni != NULL) ? ni->ni_associd : 0;
2280 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2281 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2282 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2283 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2285 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2287 /* remove pairwise key */
2289 wcid = RT2860_AID2WCID(associd);
2291 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID_ATTR(wcid));
2293 tmp &= ~0xf;
2294 tmp |= (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT);
2296 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2298 else
2300 /* remove group key */
2302 vapid = 0;
2304 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2306 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2307 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2309 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2312 return 1;
2316 * rt2860_raw_xmit
2318 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2319 const struct ieee80211_bpf_params *params)
2321 return 0;
2325 * rt2860_media_change
2327 static int rt2860_media_change(struct ifnet *ifp)
2329 struct rt2860_softc *sc;
2330 int error;
2332 sc = ifp->if_softc;
2334 error = ieee80211_media_change(ifp);
2335 if (error != ENETRESET)
2336 return error;
2338 RT2860_SOFTC_LOCK(sc);
2340 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2342 rt2860_stop_locked(sc);
2343 rt2860_init_locked(sc);
2346 RT2860_SOFTC_UNLOCK(sc);
2348 return 0;
2352 * rt2860_recv_action
2354 static void rt2860_recv_action(struct ieee80211_node *ni,
2355 const uint8_t *frm, const uint8_t *efrm)
2357 struct rt2860_softc *sc;
2358 struct ieee80211com *ic;
2359 struct ifnet *ifp;
2360 const struct ieee80211_action *ia;
2361 uint16_t associd, baparamset;
2362 uint8_t wcid;
2363 int tid, bufsize;
2364 uint32_t tmp;
2366 ic = ni->ni_ic;
2367 ifp = ic->ic_ifp;
2368 sc = ifp->if_softc;
2370 ia = (const struct ieee80211_action *) frm;
2372 sc->recv_action(ni, frm, efrm);
2374 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2375 return;
2377 associd = (ni != NULL) ? ni->ni_associd : 0;
2378 wcid = RT2860_AID2WCID(associd);
2380 switch (ia->ia_action)
2382 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2383 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2384 baparamset = LE_READ_2(frm + 3);
2385 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2386 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2388 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2389 "%s: received ADDBA: associd=0x%04x, tid=%d, bufsize=%d\n",
2390 device_get_nameunit(sc->dev), associd, tid, bufsize);
2392 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2394 tmp |= (0x10000 << tid);
2396 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2397 break;
2399 /* IEEE80211_ACTION_BA_DELBA */
2400 case IEEE80211_ACTION_BA_DELBA:
2401 baparamset = LE_READ_2(frm + 2);
2402 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2404 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2405 "%s: received DELBA: associd=0x%04x, tid=%d\n",
2406 device_get_nameunit(sc->dev), associd, tid);
2408 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2410 tmp &= ~(0x10000 << tid);
2412 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2413 break;
2418 * rt2860_send_action
2420 static int rt2860_send_action(struct ieee80211_node *ni,
2421 int category, int action, uint16_t args[4])
2423 struct rt2860_softc *sc;
2424 struct ieee80211com *ic;
2425 struct ifnet *ifp;
2426 uint16_t associd, baparamset;
2427 uint8_t wcid;
2428 int ret, tid;
2429 uint32_t tmp;
2431 ic = ni->ni_ic;
2432 ifp = ic->ic_ifp;
2433 sc = ifp->if_softc;
2435 ret = sc->send_action(ni, category, action, args);
2437 if (category != IEEE80211_ACTION_CAT_BA)
2438 return ret;
2440 associd = (ni != NULL) ? ni->ni_associd : 0;
2441 wcid = RT2860_AID2WCID(associd);
2443 switch (action)
2445 /* IEEE80211_ACTION_BA_DELBA */
2446 case IEEE80211_ACTION_BA_DELBA:
2447 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) |
2448 RT2860_SM(args[1], IEEE80211_DELBAPS_INIT);
2450 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) == IEEE80211_DELBAPS_INIT)
2451 break;
2453 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2455 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2456 "%s: sending DELBA: associd=0x%04x, tid=%d\n",
2457 device_get_nameunit(sc->dev), associd, tid);
2459 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2461 tmp &= ~(0x10000 << tid);
2463 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2464 break;
2467 return ret;
2471 * rt2860_amrr_update_iter_func
2473 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2475 struct rt2860_softc *sc;
2476 struct ieee80211com *ic;
2477 uint8_t wcid;
2479 sc = arg;
2480 ic = &sc->ic;
2482 /* only associated stations */
2484 if (ni->ni_associd != 0)
2486 wcid = RT2860_AID2WCID(ni->ni_associd);
2488 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2490 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2491 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2492 device_get_nameunit(sc->dev),
2493 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2494 ni->ni_associd,
2495 (ni->ni_flags & IEEE80211_NODE_HT) ?
2496 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2497 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2498 (ni->ni_flags & IEEE80211_NODE_HT) ?
2499 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2500 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2505 * rt2860_periodic
2507 static void rt2860_periodic(void *arg)
2509 struct rt2860_softc *sc;
2511 sc = arg;
2513 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2514 "%s: periodic\n",
2515 device_get_nameunit(sc->dev));
2517 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2521 * rt2860_tx_watchdog
2523 static void rt2860_tx_watchdog(void *arg)
2525 struct rt2860_softc *sc;
2526 struct ifnet *ifp;
2528 sc = arg;
2529 ifp = sc->ifp;
2531 if (sc->tx_timer == 0)
2532 return;
2534 if (--sc->tx_timer == 0)
2536 printf("%s: Tx watchdog timeout: resetting\n",
2537 device_get_nameunit(sc->dev));
2539 rt2860_stop_locked(sc);
2540 rt2860_init_locked(sc);
2542 ifp->if_oerrors++;
2544 sc->tx_watchdog_timeouts++;
2547 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2551 * rt2860_asic_set_bssid
2553 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2554 const uint8_t *bssid)
2556 uint32_t tmp;
2558 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2559 "%s: set bssid: bssid=%s\n",
2560 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2562 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2564 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2566 tmp = bssid[4] | (bssid[5] << 8);
2568 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2572 * rt2860_asic_set_macaddr
2574 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2575 const uint8_t *addr)
2577 uint32_t tmp;
2579 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2581 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2583 tmp = addr[4] | (addr[5] << 8);
2585 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2589 * rt2860_asic_enable_tsf_sync
2591 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2593 struct ieee80211com *ic;
2594 uint32_t tmp;
2596 ic = &sc->ic;
2598 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2599 "%s: enabling TSF\n",
2600 device_get_nameunit(sc->dev));
2602 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2604 tmp &= ~0x1fffff;
2605 tmp |= ic->ic_bss->ni_intval * 16;
2606 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2608 if (ic->ic_opmode == IEEE80211_M_STA)
2610 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2612 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2614 tmp |= RT2860_REG_BCN_TX_ENABLE;
2615 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2617 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2619 tmp |= RT2860_REG_BCN_TX_ENABLE;
2620 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2623 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2627 * rt2860_asic_disable_tsf_sync
2629 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2631 uint32_t tmp;
2633 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2634 "%s: disabling TSF\n",
2635 device_get_nameunit(sc->dev));
2637 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2639 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2640 RT2860_REG_TSF_TIMER_ENABLE |
2641 RT2860_REG_TBTT_TIMER_ENABLE);
2643 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2644 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2646 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2650 * rt2860_asic_enable_mrr
2652 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2654 #define CCK(mcs) (mcs)
2655 #define OFDM(mcs) ((1 << 3) | (mcs))
2656 #define HT(mcs) (mcs)
2658 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2659 (OFDM(6) << 28) | /* 54 -> 48 */
2660 (OFDM(5) << 24) | /* 48 -> 36 */
2661 (OFDM(4) << 20) | /* 36 -> 24 */
2662 (OFDM(3) << 16) | /* 24 -> 18 */
2663 (OFDM(2) << 12) | /* 18 -> 12 */
2664 (OFDM(1) << 8) | /* 12 -> 9 */
2665 (OFDM(0) << 4) | /* 9 -> 6 */
2666 OFDM(0)); /* 6 -> 6 */
2668 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2669 (CCK(2) << 12) | /* 11 -> 5.5 */
2670 (CCK(1) << 8) | /* 5.5 -> 2 */
2671 (CCK(0) << 4) | /* 2 -> 1 */
2672 CCK(0)); /* 1 -> 1 */
2674 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2675 (HT(6) << 28) |
2676 (HT(5) << 24) |
2677 (HT(4) << 20) |
2678 (HT(3) << 16) |
2679 (HT(2) << 12) |
2680 (HT(1) << 8) |
2681 (HT(0) << 4) |
2682 HT(0));
2684 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2685 (HT(14) << 28) |
2686 (HT(13) << 24) |
2687 (HT(12) << 20) |
2688 (HT(11) << 16) |
2689 (HT(10) << 12) |
2690 (HT(9) << 8) |
2691 (HT(8) << 4) |
2692 HT(8));
2694 #undef HT
2695 #undef OFDM
2696 #undef CCK
2700 * rt2860_asic_set_txpreamble
2702 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2704 struct ieee80211com *ic;
2705 uint32_t tmp;
2707 ic = &sc->ic;
2709 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2710 "%s: %s short Tx preamble\n",
2711 device_get_nameunit(sc->dev),
2712 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2714 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2716 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2718 if (sc->ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2719 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2721 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2725 * rt2860_asic_set_basicrates
2727 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2729 struct ieee80211com *ic;
2731 ic = &sc->ic;
2733 if (ic->ic_curmode == IEEE80211_MODE_11B)
2734 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2735 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2736 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2737 else
2738 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2742 * rt2860_asic_update_rtsthreshold
2744 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2746 struct ieee80211com *ic;
2747 uint32_t tmp;
2748 uint16_t threshold;
2750 ic = &sc->ic;
2752 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2753 "%s: updating RTS threshold: %d\n",
2754 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
2756 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2758 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2760 threshold = (ic->ic_rtsthreshold < IEEE80211_RTS_MAX) ?
2761 ic->ic_rtsthreshold : 0x1000;
2763 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2764 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2766 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
2770 * rt2860_asic_update_txpower
2772 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
2774 struct ieee80211com *ic;
2775 uint32_t *txpow_rate;
2776 int8_t delta;
2777 uint8_t val;
2778 uint32_t tmp;
2779 int i;
2781 ic = &sc->ic;
2783 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2784 "%s: updating Tx power: %d\n",
2785 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
2787 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
2789 txpow_rate = sc->txpow_rate_20mhz;
2791 else
2793 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2794 txpow_rate = sc->txpow_rate_40mhz_2ghz;
2795 else
2796 txpow_rate = sc->txpow_rate_40mhz_5ghz;
2799 delta = 0;
2801 val = rt2860_io_bbp_read(sc, 1);
2802 val &= 0xfc;
2804 if (ic->ic_txpowlimit > 90)
2806 /* do nothing */
2808 else if (ic->ic_txpowlimit > 60)
2810 delta -= 1;
2812 else if (ic->ic_txpowlimit > 30)
2814 delta -= 3;
2816 else if (ic->ic_txpowlimit > 15)
2818 val |= 0x1;
2820 else if (ic->ic_txpowlimit > 9)
2822 val |= 0x1;
2823 delta -= 3;
2825 else
2827 val |= 0x2;
2830 rt2860_io_bbp_write(sc, 1, val);
2832 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
2834 if (txpow_rate[i] == 0xffffffff)
2835 continue;
2837 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
2839 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
2844 * rt2860_asic_update_promisc
2846 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
2848 struct ifnet *ifp;
2849 uint32_t tmp;
2851 ifp = sc->ic.ic_ifp;
2853 printf("%s: %s promiscuous mode\n",
2854 device_get_nameunit(sc->dev),
2855 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
2857 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
2859 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
2861 if (!(ifp->if_flags & IFF_PROMISC))
2862 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
2864 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
2868 * rt2860_asic_updateprot
2870 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
2872 struct ieee80211com *ic;
2873 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
2874 uint8_t htopmode;
2876 ic = &sc->ic;
2878 /* CCK frame protection */
2880 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2881 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
2883 /* set up protection frame phy mode and rate (MCS code) */
2885 if (ic->ic_curmode == IEEE80211_MODE_11A)
2886 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2887 (0 << RT2860_REG_PROT_MCS_SHIFT);
2888 else
2889 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2890 (3 << RT2860_REG_PROT_MCS_SHIFT));
2892 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
2894 /* OFDM frame protection */
2896 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2897 RT2860_REG_TXOP_ALLOW_ALL;
2899 if (ic->ic_flags & IEEE80211_F_USEPROT)
2901 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2902 "%s: updating protection mode: b/g protection mode=%s\n",
2903 device_get_nameunit(sc->dev),
2904 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2905 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2907 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2908 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2909 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2910 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
2911 else
2912 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2914 else
2916 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2917 "%s: updating protection mode: b/g protection mode=%s\n",
2918 device_get_nameunit(sc->dev), "none");
2920 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2923 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
2925 /* HT frame protection */
2927 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
2928 htopmode = ic->ic_bss->ni_htopmode;
2929 else
2930 htopmode = ic->ic_curhtprotmode;
2932 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2933 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
2934 device_get_nameunit(sc->dev),
2935 htopmode & IEEE80211_HTINFO_OPMODE,
2936 (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2937 ((ic->ic_htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2939 switch (htopmode & IEEE80211_HTINFO_OPMODE)
2941 /* IEEE80211_HTINFO_OPMODE_HT20PR */
2942 case IEEE80211_HTINFO_OPMODE_HT20PR:
2943 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
2944 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2945 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
2946 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2947 (4 << RT2860_REG_PROT_MCS_SHIFT);
2949 gf20_prot = mm20_prot;
2951 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
2952 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2953 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2955 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2956 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2957 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2958 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2959 else
2960 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
2962 gf40_prot = mm40_prot;
2963 break;
2965 /* IEEE80211_HTINFO_OPMODE_MIXED */
2966 case IEEE80211_HTINFO_OPMODE_MIXED:
2967 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
2968 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2969 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
2971 if (ic->ic_flags & IEEE80211_F_USEPROT)
2972 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2973 (3 << RT2860_REG_PROT_MCS_SHIFT);
2974 else
2975 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2976 (4 << RT2860_REG_PROT_MCS_SHIFT);
2978 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2979 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2980 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2981 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
2982 else
2983 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
2985 gf20_prot = mm20_prot;
2987 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
2989 if (ic->ic_flags & IEEE80211_F_USEPROT)
2990 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2991 (3 << RT2860_REG_PROT_MCS_SHIFT);
2992 else
2993 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2994 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2996 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2997 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2998 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2999 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3000 else
3001 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3003 gf40_prot = mm40_prot;
3004 break;
3007 * IEEE80211_HTINFO_OPMODE_PURE
3008 * IEEE80211_HTINFO_OPMODE_PROTOPT
3010 case IEEE80211_HTINFO_OPMODE_PURE:
3011 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3012 default:
3013 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3014 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3015 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3016 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3017 (4 << RT2860_REG_PROT_MCS_SHIFT);
3019 gf20_prot = mm20_prot;
3021 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3022 RT2860_REG_TXOP_ALLOW_ALL |
3023 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3024 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3026 gf40_prot = mm40_prot;
3027 break;
3030 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3031 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3032 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3033 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3037 * rt2860_asic_updateslot
3039 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3041 struct ieee80211com *ic;
3042 uint32_t tmp;
3044 ic = &sc->ic;
3046 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3047 "%s: %s short slot time\n",
3048 device_get_nameunit(sc->dev),
3049 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3051 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3053 tmp &= ~0xff;
3054 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3056 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3060 * rt2860_asic_wme_update
3062 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3064 struct ieee80211com *ic;
3065 struct ieee80211_wme_state *wme;
3066 const struct wmeParams *wmep;
3067 int i;
3069 ic = &sc->ic;
3070 wme = &ic->ic_wme;
3071 wmep = wme->wme_chanParams.cap_wmeParams;
3073 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3074 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3075 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3076 device_get_nameunit(sc->dev),
3077 wmep[WME_AC_VO].wmep_aifsn,
3078 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3079 wmep[WME_AC_VO].wmep_txopLimit,
3080 wmep[WME_AC_VI].wmep_aifsn,
3081 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3082 wmep[WME_AC_VI].wmep_txopLimit,
3083 wmep[WME_AC_BK].wmep_aifsn,
3084 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3085 wmep[WME_AC_BK].wmep_txopLimit,
3086 wmep[WME_AC_BE].wmep_aifsn,
3087 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3088 wmep[WME_AC_BE].wmep_txopLimit);
3090 for (i = 0; i < WME_NUM_AC; i++)
3091 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3092 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3093 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3095 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3096 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3097 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3099 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3100 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3101 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3103 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3104 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3105 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3107 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3108 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3110 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3111 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3115 * rt2860_asic_update_beacon
3117 static int rt2860_asic_update_beacon(struct rt2860_softc *sc)
3119 struct ieee80211com *ic;
3120 struct mbuf *m;
3121 struct rt2860_txwi txwi;
3122 uint8_t rate, mcs;
3123 uint32_t tmp;
3125 ic = &sc->ic;
3127 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3128 if (m == NULL)
3129 return ENOMEM;
3131 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3132 mcs = rt2860_rate2mcs(rate);
3134 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3136 txwi.wcid = 0xff;
3137 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3138 RT2860_TXWI_MPDU_LEN_SHIFT);
3139 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3140 txwi.mpdu_density_flags |=
3141 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3142 txwi.bawin_size_xflags |=
3143 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3145 if (rate == 2)
3147 txwi.phymode_ifs_stbc_shortgi =
3148 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3150 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3151 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3153 else
3155 txwi.phymode_ifs_stbc_shortgi =
3156 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3159 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3160 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3162 /* disable temporarily TSF sync */
3164 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3166 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3167 RT2860_REG_TSF_TIMER_ENABLE |
3168 RT2860_REG_TBTT_TIMER_ENABLE);
3170 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3172 /* write Tx wireless info and beacon frame to on-chip memory */
3174 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3175 &txwi, sizeof(struct rt2860_txwi));
3177 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3178 mtod(m, uint8_t *), m->m_pkthdr.len);
3180 /* enable again TSF sync */
3182 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3184 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3185 RT2860_REG_TSF_TIMER_ENABLE |
3186 RT2860_REG_TBTT_TIMER_ENABLE);
3188 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3190 m_freem(m);
3192 return 0;
3196 * rt2860_asic_clear_keytables
3198 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3200 int i;
3202 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3204 for (i = 0; i < 256; i++)
3206 rt2860_io_mac_write(sc, RT2860_REG_WCID(i), 0xffffffff);
3207 rt2860_io_mac_write(sc, RT2860_REG_WCID(i) + 4, 0x0000ffff);
3210 /* clear WCID attribute table (entries = 256, entry size = 4) */
3212 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3214 /* clear IV/EIV table (entries = 256, entry size = 8) */
3216 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3218 /* clear pairwise key table (entries = 256, entry size = 32) */
3220 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3222 /* clear shared key table (entries = 32, entry size = 32) */
3224 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3226 /* clear shared key mode (entries = 32, entry size = 2) */
3228 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3232 * rt2860_rxrate
3234 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3236 uint8_t mcs, phymode;
3237 uint8_t rate;
3239 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3240 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3241 RT2860_RXWI_PHYMODE_MASK;
3243 rate = 2;
3245 switch (phymode)
3247 case RT2860_RXWI_PHYMODE_CCK:
3248 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3250 case 0: rate = 2; break; /* 1 Mbps */
3251 case 1: rate = 4; break; /* 2 MBps */
3252 case 2: rate = 11; break; /* 5.5 Mbps */
3253 case 3: rate = 22; break; /* 11 Mbps */
3255 break;
3257 case RT2860_RXWI_PHYMODE_OFDM:
3258 switch (mcs)
3260 case 0: rate = 12; break; /* 6 Mbps */
3261 case 1: rate = 18; break; /* 9 Mbps */
3262 case 2: rate = 24; break; /* 12 Mbps */
3263 case 3: rate = 36; break; /* 18 Mbps */
3264 case 4: rate = 48; break; /* 24 Mbps */
3265 case 5: rate = 72; break; /* 36 Mbps */
3266 case 6: rate = 96; break; /* 48 Mbps */
3267 case 7: rate = 108; break; /* 54 Mbps */
3269 break;
3271 case RT2860_RXWI_PHYMODE_HT_MIXED:
3272 case RT2860_RXWI_PHYMODE_HT_GF:
3273 break;
3276 return rate;
3280 * rt2860_maxrssi_rxpath
3282 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3283 const struct rt2860_rxwi *rxwi)
3285 uint8_t rxpath;
3287 rxpath = 0;
3289 if (sc->nrxpath > 1)
3290 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3291 rxpath = 1;
3293 if (sc->nrxpath > 2)
3294 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3295 rxpath = 2;
3297 return rxpath;
3301 * rt2860_rssi2dbm
3303 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3304 uint8_t rssi, uint8_t rxpath)
3306 struct ieee80211com *ic;
3307 struct ieee80211_channel *c;
3308 int chan;
3309 int8_t rssi_off, lna_gain;
3311 if (rssi == 0)
3312 return -99;
3314 ic = &sc->ic;
3315 c = ic->ic_curchan;
3316 chan = ieee80211_chan2ieee(ic, c);
3318 if (IEEE80211_IS_CHAN_5GHZ(c))
3320 rssi_off = sc->rssi_off_5ghz[rxpath];
3322 if (chan <= 64)
3323 lna_gain = sc->lna_gain[1];
3324 else if (chan <= 128)
3325 lna_gain = sc->lna_gain[2];
3326 else
3327 lna_gain = sc->lna_gain[3];
3329 else
3331 rssi_off = sc->rssi_off_2ghz[rxpath];
3332 lna_gain = sc->lna_gain[0];
3335 return (-12 - rssi_off - lna_gain - rssi);
3339 * rt2860_rate2mcs
3341 static uint8_t rt2860_rate2mcs(uint8_t rate)
3343 switch (rate)
3345 /* CCK rates */
3346 case 2: return 0;
3347 case 4: return 1;
3348 case 11: return 2;
3349 case 22: return 3;
3351 /* OFDM rates */
3352 case 12: return 0;
3353 case 18: return 1;
3354 case 24: return 2;
3355 case 36: return 3;
3356 case 48: return 4;
3357 case 72: return 5;
3358 case 96: return 6;
3359 case 108: return 7;
3362 return 0;
3366 * rt2860_ackrate
3368 static int rt2860_ackrate(struct ieee80211com *ic, int rate)
3370 switch (rate)
3372 /* CCK rates */
3374 case 2:
3375 return 2;
3377 case 4:
3378 case 11:
3379 case 22:
3380 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3382 /* OFDM rates */
3384 case 12:
3385 case 18:
3386 return 12;
3388 case 24:
3389 case 36:
3390 return 24;
3392 case 48:
3393 case 72:
3394 case 96:
3395 case 108:
3396 return 48;
3399 /* default to 1Mbps */
3400 return 2;
3404 * rt2860_txtime
3406 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags)
3408 uint16_t txtime;
3410 if (RT2860_RATE_IS_OFDM(rate))
3412 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3413 txtime = 16 + 4 + 4 * txtime + 6;
3415 else
3417 txtime = (16 * len + rate - 1) / rate;
3419 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3420 txtime += 72 + 24;
3421 else
3422 txtime += 144 + 48;
3425 return txtime;
3429 * rt2860_tx_frame
3431 static int rt2860_tx_frame(struct rt2860_softc *sc,
3432 struct mbuf *m, struct ieee80211_node *ni, int qid)
3434 struct ieee80211com *ic;
3435 struct rt2860_softc_tx_ring *ring;
3436 struct rt2860_softc_tx_data *data;
3437 struct rt2860_txdesc *desc;
3438 struct rt2860_txwi *txwi;
3439 struct ieee80211_frame *wh;
3440 struct rt2860_softc_tx_radiotap_header *tap;
3441 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3442 u_int hdrsize, hdrspace;
3443 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, qsel;
3444 uint16_t qos, len, dmalen, mpdu_len, dur;
3445 int error, hasqos, ackrate, ndmasegs, ndescs, i, j;
3447 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3448 ("%s: Tx frame: invalid qid=%d\n",
3449 device_get_nameunit(sc->dev), qid));
3451 ic = &sc->ic;
3453 ring = &sc->tx_ring[qid];
3454 desc = &ring->desc[ring->desc_cur];
3455 data = &ring->data[ring->data_cur];
3456 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3458 wh = mtod(m, struct ieee80211_frame *);
3460 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3462 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3463 if (hasqos)
3465 if (IEEE80211_HAS_ADDR4(wh))
3466 qos = le16toh(*(const uint16_t *)
3467 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3468 else
3469 qos = le16toh(*(const uint16_t *)
3470 (((struct ieee80211_qosframe *) wh)->i_qos));
3472 else
3474 qos = 0;
3477 if (ni->ni_flags & IEEE80211_NODE_HT)
3479 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3480 rate = 0;
3481 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3482 rate = ic->ic_fixed_rate;
3483 else
3484 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
3486 else
3488 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3489 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3490 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3491 rate = ic->ic_fixed_rate;
3492 else
3493 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
3496 rate &= IEEE80211_RATE_VAL;
3498 /* fill Tx wireless info */
3500 if (ni->ni_flags & IEEE80211_NODE_HT)
3501 mcs = rate;
3502 else
3503 mcs = rt2860_rate2mcs(rate);
3505 pid = mcs;
3507 /* management frames do not need encryption */
3509 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3510 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3512 /* calculate MPDU length without padding */
3514 hdrsize = ieee80211_hdrsize(wh);
3515 hdrspace = ieee80211_hdrspace(ic, wh);
3516 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3518 memset(txwi, 0, sizeof(struct rt2860_txwi));
3520 txwi->wcid = wcid;
3522 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3523 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3524 RT2860_TXWI_MPDU_LEN_SHIFT);
3526 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
3528 shortgi = (ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
3529 (ni->ni_flags & IEEE80211_NODE_HT);
3531 txwi->phymode_ifs_stbc_shortgi |=
3532 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
3533 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3535 if (ni->ni_flags & IEEE80211_NODE_HT)
3537 txwi->phymode_ifs_stbc_shortgi |=
3538 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3540 else
3542 if (!RT2860_RATE_IS_OFDM(rate))
3544 txwi->phymode_ifs_stbc_shortgi |=
3545 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3547 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3548 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3550 else
3552 txwi->phymode_ifs_stbc_shortgi |=
3553 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3557 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
3558 (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40))
3559 bw = RT2860_TXWI_BW_40;
3560 else
3561 bw = RT2860_TXWI_BW_20;
3563 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
3564 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3566 if (type != IEEE80211_FC0_TYPE_DATA)
3567 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3568 else
3569 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3571 /* skip ACKs for multicast frames and probe responses */
3573 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3574 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3575 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
3576 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
3578 txwi->bawin_size_xflags |=
3579 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3581 if (ni->ni_flags & IEEE80211_NODE_HT)
3583 /* preamble + plcp + signal extension */
3585 dur = 16 + 4 + 6;
3587 else
3589 ackrate = rt2860_ackrate(ic, rate);
3591 dur = rt2860_txtime(RT2860_ACK_SIZE, ackrate, ic->ic_flags) +
3592 sc->sifs;
3595 *(uint16_t *) wh->i_dur = htole16(dur);
3598 /* ask MAC to insert timestamp into probe responses */
3600 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3601 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3602 txwi->mpdu_density_flags |=
3603 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3605 if (bpf_peers_present(sc->drvbpf))
3607 tap = &sc->txtap;
3609 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3610 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3611 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3612 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3613 tap->chan_maxpow = 0;
3615 if (ni->ni_flags & IEEE80211_NODE_HT)
3616 tap->rate = mcs | IEEE80211_RATE_MCS;
3617 else
3618 tap->rate = rate;
3620 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3621 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3623 if (shortgi)
3624 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3626 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3627 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3629 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3631 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3633 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3635 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3637 else
3639 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3643 /* copy and trim 802.11 header */
3645 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3646 m_adj(m, hdrspace);
3648 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3649 dma_seg, &ndmasegs, 0);
3650 if (error != 0)
3652 /* too many fragments, linearize */
3654 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3655 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3656 device_get_nameunit(sc->dev));
3658 m = m_defrag(m, M_DONTWAIT);
3659 if (m == NULL)
3660 return ENOMEM;
3662 sc->tx_defrag_packets++;
3664 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3665 dma_seg, &ndmasegs, 0);
3666 if (error != 0)
3668 printf("%s: could not load mbuf DMA map: error=%d\n",
3669 device_get_nameunit(sc->dev), error);
3670 m_freem(m);
3671 return error;
3675 /* determine how many Tx descs are required */
3677 ndescs = 1 + ndmasegs / 2;
3678 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3680 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3681 "%s: there are not enough Tx descs\n",
3682 device_get_nameunit(sc->dev));
3684 sc->no_tx_desc_avail++;
3686 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3687 m_freem(m);
3688 return EFBIG;
3691 data->m = m;
3692 data->ni = ni;
3694 /* set up Tx descs */
3696 /* first segment is Tx wireless info and 802.11 header */
3698 len = sizeof(struct rt2860_txwi) + hdrsize;
3700 /* align end on a 4-bytes boundary */
3702 dmalen = (len + 3) & ~ 3;
3704 memset((caddr_t) txwi + len, 0, dmalen - len);
3706 qsel = RT2860_TXDESC_QSEL_EDCA;
3708 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3709 desc->sdl0 = htole16(dmalen);
3710 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3712 /* set up payload segments */
3714 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3716 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3717 desc->sdl1 = htole16(dma_seg[j].ds_len);
3719 ring->desc_queued++;
3720 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3722 j++;
3724 desc = &ring->desc[ring->desc_cur];
3726 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3727 desc->sdl0 = htole16(dma_seg[j].ds_len);
3728 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3730 j++;
3733 /* finalize last payload segment */
3735 if (i > 0)
3737 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3738 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3740 else
3742 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3743 desc->sdl1 = 0;
3746 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3747 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3748 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3749 device_get_nameunit(sc->dev),
3750 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3751 bw, stbc, shortgi, mcs, dmalen, ndmasegs,
3752 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);
3754 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3755 BUS_DMASYNC_PREWRITE);
3756 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3757 BUS_DMASYNC_PREWRITE);
3758 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3759 BUS_DMASYNC_PREWRITE);
3761 ring->desc_queued++;
3762 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3764 ring->data_queued++;
3765 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3767 /* kick Tx */
3769 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3771 return 0;
3775 * rt2860_tx_raw
3777 static int rt2860_tx_raw(struct rt2860_softc *sc,
3778 struct mbuf *m, struct ieee80211_node *ni,
3779 const struct ieee80211_bpf_params *params)
3781 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3782 "%s: Tx raw\n",
3783 device_get_nameunit(sc->dev));
3785 return 0;
3789 * rt2860_intr
3791 static void rt2860_intr(void *arg)
3793 struct rt2860_softc *sc;
3794 struct ifnet *ifp;
3795 uint32_t status;
3797 sc = arg;
3798 ifp = sc->ifp;
3800 /* acknowledge interrupts */
3802 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
3803 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
3805 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3806 "%s: interrupt: status = 0x%08x\n",
3807 device_get_nameunit(sc->dev), status);
3809 if (status == 0xffffffff || /* device likely went away */
3810 status == 0) /* not for us */
3811 return;
3813 sc->interrupts++;
3815 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
3816 return;
3818 if (status & RT2860_REG_INT_TX_COHERENT)
3819 rt2860_tx_coherent_intr(sc);
3821 if (status & RT2860_REG_INT_RX_COHERENT)
3822 rt2860_rx_coherent_intr(sc);
3824 if (status & RT2860_REG_INT_TXRX_COHERENT)
3825 rt2860_txrx_coherent_intr(sc);
3827 if (status & RT2860_REG_INT_FIFO_STA_FULL)
3828 rt2860_fifo_sta_full_intr(sc);
3830 if (status & RT2860_REG_INT_TX_MGMT_DONE)
3831 rt2860_tx_intr(sc, 5);
3833 if (status & RT2860_REG_INT_RX_DONE)
3834 rt2860_rx_intr(sc);
3836 if (status & RT2860_REG_INT_RX_DELAY_DONE)
3837 rt2860_rx_delay_intr(sc);
3839 if (status & RT2860_REG_INT_TX_HCCA_DONE)
3840 rt2860_tx_intr(sc, 4);
3842 if (status & RT2860_REG_INT_TX_AC3_DONE)
3843 rt2860_tx_intr(sc, 3);
3845 if (status & RT2860_REG_INT_TX_AC2_DONE)
3846 rt2860_tx_intr(sc, 2);
3848 if (status & RT2860_REG_INT_TX_AC1_DONE)
3849 rt2860_tx_intr(sc, 1);
3851 if (status & RT2860_REG_INT_TX_AC0_DONE)
3852 rt2860_tx_intr(sc, 0);
3854 if (status & RT2860_REG_INT_TX_DELAY_DONE)
3855 rt2860_tx_delay_intr(sc);
3857 if (status & RT2860_REG_INT_PRE_TBTT)
3858 rt2860_pre_tbtt_intr(sc);
3860 if (status & RT2860_REG_INT_TBTT)
3861 rt2860_tbtt_intr(sc);
3863 if (status & RT2860_REG_INT_MCU_CMD)
3864 rt2860_mcu_cmd_intr(sc);
3866 if (status & RT2860_REG_INT_AUTO_WAKEUP)
3867 rt2860_auto_wakeup_intr(sc);
3869 if (status & RT2860_REG_INT_GP_TIMER)
3870 rt2860_gp_timer_intr(sc);
3874 * rt2860_tx_coherent_intr
3876 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
3878 uint32_t tmp;
3879 int i;
3881 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3882 "%s: Tx coherent interrupt\n",
3883 device_get_nameunit(sc->dev));
3885 sc->tx_coherent_interrupts++;
3887 /* restart DMA engine */
3889 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3891 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3892 RT2860_REG_RX_DMA_ENABLE |
3893 RT2860_REG_TX_DMA_ENABLE);
3895 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3897 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3899 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3900 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3902 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3904 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3905 sc->tx_ring[i].desc_phys_addr);
3906 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3907 RT2860_SOFTC_TX_RING_DESC_COUNT);
3908 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3911 /* init Rx ring */
3913 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3915 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3916 sc->rx_ring.desc_phys_addr);
3917 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3918 RT2860_SOFTC_RX_RING_DATA_COUNT);
3919 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3920 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3922 rt2860_txrx_enable(sc);
3926 * rt2860_rx_coherent_intr
3928 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
3930 uint32_t tmp;
3931 int i;
3933 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3934 "%s: Rx coherent interrupt\n",
3935 device_get_nameunit(sc->dev));
3937 sc->rx_coherent_interrupts++;
3939 /* restart DMA engine */
3941 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3943 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3944 RT2860_REG_RX_DMA_ENABLE |
3945 RT2860_REG_TX_DMA_ENABLE);
3947 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3949 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3951 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3952 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3954 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3956 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3957 sc->tx_ring[i].desc_phys_addr);
3958 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3959 RT2860_SOFTC_TX_RING_DESC_COUNT);
3960 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3963 /* init Rx ring */
3965 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3967 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3968 sc->rx_ring.desc_phys_addr);
3969 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3970 RT2860_SOFTC_RX_RING_DATA_COUNT);
3971 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3972 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3974 rt2860_txrx_enable(sc);
3978 * rt2860_txrx_coherent_intr
3980 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
3982 uint32_t tmp;
3983 int i;
3985 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3986 "%s: Tx/Rx coherent interrupt\n",
3987 device_get_nameunit(sc->dev));
3989 sc->txrx_coherent_interrupts++;
3991 /* restart DMA engine */
3993 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3995 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3996 RT2860_REG_RX_DMA_ENABLE |
3997 RT2860_REG_TX_DMA_ENABLE);
3999 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4001 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4003 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4004 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4006 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4008 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4009 sc->tx_ring[i].desc_phys_addr);
4010 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4011 RT2860_SOFTC_TX_RING_DESC_COUNT);
4012 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4015 /* init Rx ring */
4017 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4019 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4020 sc->rx_ring.desc_phys_addr);
4021 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4022 RT2860_SOFTC_RX_RING_DATA_COUNT);
4023 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4024 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4026 rt2860_txrx_enable(sc);
4030 * rt2860_fifo_sta_full_intr
4032 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4034 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4035 "%s: FIFO statistic full interrupt\n",
4036 device_get_nameunit(sc->dev));
4038 sc->fifo_sta_full_interrupts++;
4040 RT2860_SOFTC_LOCK(sc);
4042 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4044 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4046 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4049 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4051 RT2860_SOFTC_UNLOCK(sc);
4055 * rt2860_rx_intr
4057 static void rt2860_rx_intr(struct rt2860_softc *sc)
4059 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4060 "%s: Rx interrupt\n",
4061 device_get_nameunit(sc->dev));
4063 sc->rx_interrupts++;
4065 RT2860_SOFTC_LOCK(sc);
4067 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4069 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4071 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4074 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4076 RT2860_SOFTC_UNLOCK(sc);
4080 * rt2860_rx_delay_intr
4082 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4084 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4085 "%s: Rx delay interrupt\n",
4086 device_get_nameunit(sc->dev));
4088 sc->rx_delay_interrupts++;
4092 * rt2860_tx_intr
4094 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4096 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4097 ("%s: Tx interrupt: invalid qid=%d\n",
4098 device_get_nameunit(sc->dev), qid));
4100 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4101 "%s: Tx interrupt: qid=%d\n",
4102 device_get_nameunit(sc->dev), qid);
4104 sc->tx_interrupts[qid]++;
4106 RT2860_SOFTC_LOCK(sc);
4108 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4110 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4112 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4115 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4117 RT2860_SOFTC_UNLOCK(sc);
4121 * rt2860_tx_delay_intr
4123 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4125 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4126 "%s: Tx delay interrupt\n",
4127 device_get_nameunit(sc->dev));
4129 sc->tx_delay_interrupts++;
4133 * rt2860_pre_tbtt_intr
4135 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4137 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4138 "%s: Pre-TBTT interrupt\n",
4139 device_get_nameunit(sc->dev));
4141 sc->pre_tbtt_interrupts++;
4145 * rt2860_tbtt_intr
4147 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4149 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4150 "%s: TBTT interrupt\n",
4151 device_get_nameunit(sc->dev));
4153 sc->tbtt_interrupts++;
4157 * rt2860_mcu_cmd_intr
4159 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4161 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4162 "%s: MCU command interrupt\n",
4163 device_get_nameunit(sc->dev));
4165 sc->mcu_cmd_interrupts++;
4169 * rt2860_auto_wakeup_intr
4171 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4173 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4174 "%s: auto wakeup interrupt\n",
4175 device_get_nameunit(sc->dev));
4177 sc->auto_wakeup_interrupts++;
4181 * rt2860_gp_timer_intr
4183 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4185 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4186 "%s: GP timer interrupt\n",
4187 device_get_nameunit(sc->dev));
4189 sc->gp_timer_interrupts++;
4193 * rt2860_rx_done_task
4195 static void rt2860_rx_done_task(void *context, int pending)
4197 struct rt2860_softc *sc;
4198 struct ifnet *ifp;
4199 int again;
4201 sc = context;
4202 ifp = sc->ifp;
4204 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4205 "%s: Rx done task\n",
4206 device_get_nameunit(sc->dev));
4208 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4209 return;
4211 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4213 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4215 RT2860_SOFTC_LOCK(sc);
4217 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4219 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4220 "%s: Rx done task: scheduling again\n",
4221 device_get_nameunit(sc->dev));
4223 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4225 else
4227 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4230 RT2860_SOFTC_UNLOCK(sc);
4234 * rt2860_tx_done_task
4236 static void rt2860_tx_done_task(void *context, int pending)
4238 struct rt2860_softc *sc;
4239 struct ifnet *ifp;
4240 uint32_t intr_mask;
4241 int i;
4243 sc = context;
4244 ifp = sc->ifp;
4246 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4247 "%s: Tx done task\n",
4248 device_get_nameunit(sc->dev));
4250 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4251 return;
4253 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4254 RT2860_REG_INT_TX_HCCA_DONE |
4255 RT2860_REG_INT_TX_AC3_DONE |
4256 RT2860_REG_INT_TX_AC2_DONE |
4257 RT2860_REG_INT_TX_AC1_DONE |
4258 RT2860_REG_INT_TX_AC0_DONE);
4260 sc->intr_pending_mask &= ~intr_mask;
4262 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4263 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4265 sc->tx_timer = 0;
4267 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4269 RT2860_SOFTC_LOCK(sc);
4271 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4272 (sc->intr_disable_mask & intr_mask));
4274 if (sc->intr_pending_mask & intr_mask)
4276 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4277 "%s: Tx done task: scheduling again\n",
4278 device_get_nameunit(sc->dev));
4280 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4283 RT2860_SOFTC_UNLOCK(sc);
4287 * rt2860_fifo_sta_full_task
4289 static void rt2860_fifo_sta_full_task(void *context, int pending)
4291 struct rt2860_softc *sc;
4292 struct ifnet *ifp;
4294 sc = context;
4295 ifp = sc->ifp;
4297 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4298 "%s: FIFO statistic full task\n",
4299 device_get_nameunit(sc->dev));
4301 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4302 return;
4304 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4306 rt2860_drain_fifo_stats(sc);
4308 RT2860_SOFTC_LOCK(sc);
4310 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4312 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4313 "%s: FIFO statistic full task: scheduling again\n",
4314 device_get_nameunit(sc->dev));
4316 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4318 else
4320 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4323 RT2860_SOFTC_UNLOCK(sc);
4327 * rt2860_periodic_task
4329 static void rt2860_periodic_task(void *context, int pending)
4331 struct rt2860_softc *sc;
4332 struct ifnet *ifp;
4333 struct ieee80211com *ic;
4335 sc = context;
4336 ifp = sc->ifp;
4337 ic = &sc->ic;
4339 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4340 "%s: periodic task: round=%lu\n",
4341 device_get_nameunit(sc->dev), sc->periodic_round);
4343 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4344 return;
4346 RT2860_SOFTC_LOCK(sc);
4348 sc->periodic_round++;
4350 rt2860_update_stats(sc);
4352 if ((sc->periodic_round % 10) == 0)
4354 rt2860_update_raw_counters(sc);
4356 rt2860_watchdog(sc);
4358 if (ic->ic_opmode == IEEE80211_M_STA)
4359 rt2860_amrr_update_iter_func(sc, ic->ic_bss);
4360 else
4361 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, sc);
4364 RT2860_SOFTC_UNLOCK(sc);
4366 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4370 * rt2860_rx_eof
4372 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4374 struct ieee80211com *ic;
4375 struct ifnet *ifp;
4376 struct ieee80211_frame *wh;
4377 struct ieee80211_node *ni;
4378 struct rt2860_softc_rx_radiotap_header *tap;
4379 struct rt2860_softc_rx_ring *ring;
4380 struct rt2860_rxdesc *desc;
4381 struct rt2860_softc_rx_data *data;
4382 struct rt2860_rxwi *rxwi;
4383 struct mbuf *m, *mnew;
4384 bus_dma_segment_t segs[1];
4385 uint32_t index, desc_flags;
4386 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4387 int8_t rssi_dbm;
4388 int error, nsegs, len, ampdu, amsdu, nframes;
4390 ic = &sc->ic;
4391 ifp = sc->ifp;
4392 ring = &sc->rx_ring;
4394 nframes = 0;
4396 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4397 BUS_DMASYNC_POSTREAD);
4399 while (limit != 0)
4401 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4402 if (ring->cur == index)
4403 break;
4405 desc = &ring->desc[ring->cur];
4406 desc_flags = le32toh(desc->flags);
4407 data = &ring->data[ring->cur];
4409 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4410 break;
4412 nframes++;
4414 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4415 if (mnew == NULL)
4417 sc->rx_mbuf_alloc_errors++;
4418 ifp->if_ierrors++;
4419 goto skip;
4422 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4424 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4425 BUS_DMASYNC_POSTREAD);
4426 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4428 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4429 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4430 if (error != 0)
4432 m_freem(mnew);
4434 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4435 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
4436 if (error != 0)
4437 panic("%s: could not load old Rx mbuf: error=%d\n",
4438 device_get_name(sc->dev), error);
4440 sc->rx_mbuf_dmamap_errors++;
4441 ifp->if_ierrors++;
4443 goto skip;
4446 KASSERT(nsegs == 1, ("%s: too many DMA segments",
4447 device_get_name(sc->dev)));
4449 m = data->m;
4451 data->m = mnew;
4452 desc->sdp0 = htole32(segs[0].ds_addr);
4454 /* get Rx wireless info */
4456 rxwi = mtod(m, struct rt2860_rxwi *);
4457 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4458 RT2860_RXWI_SIZE_MASK;
4460 /* check for L2 padding between IEEE 802.11 frame header and body */
4462 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4464 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4465 "%s: L2 padding: len=%d\n",
4466 device_get_nameunit(sc->dev), len);
4468 len += 2;
4471 m->m_pkthdr.rcvif = ifp;
4472 m->m_data = (caddr_t) (rxwi + 1);
4473 m->m_pkthdr.len = m->m_len = len;
4475 /* check for crc errors */
4477 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4479 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4480 "%s: rxdesc: crc error\n",
4481 device_get_nameunit(sc->dev));
4483 ifp->if_ierrors++;
4485 if (!(ifp->if_flags & IFF_PROMISC))
4487 m_freem(m);
4488 goto skip;
4492 wh = (struct ieee80211_frame *) (rxwi + 1);
4494 /* check for cipher errors */
4496 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4498 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4499 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4500 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4502 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4503 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4505 m->m_flags |= M_WEP;
4507 else
4509 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4510 "%s: rxdesc: cipher error=0x%02x\n",
4511 device_get_nameunit(sc->dev), cipher_err);
4513 ifp->if_ierrors++;
4515 if (!(ifp->if_flags & IFF_PROMISC))
4517 m_free(m);
4518 goto skip;
4522 else
4524 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4526 ifp->if_ierrors++;
4528 if (!(ifp->if_flags & IFF_PROMISC))
4530 m_free(m);
4531 goto skip;
4536 /* check for A-MPDU */
4538 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
4540 m->m_flags |= M_AMPDU;
4541 ampdu = 1;
4543 else
4545 ampdu = 0;
4548 ant = rt2860_maxrssi_rxpath(sc, rxwi);
4549 rssi = rxwi->rssi[ant];
4550 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
4551 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4552 RT2860_RXWI_PHYMODE_MASK);
4553 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
4554 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
4555 RT2860_RXWI_SHORTGI_MASK);
4556 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
4557 amsdu = (desc_flags & RT2860_RXDESC_FLAGS_AMSDU) ? 1 : 0;
4559 if (bpf_peers_present(sc->drvbpf))
4561 tap = &sc->rxtap;
4563 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4564 tap->dbm_antsignal = rssi_dbm;
4565 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
4566 tap->antenna = ant;
4567 tap->antsignal = rssi;
4568 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4569 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4570 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4571 tap->chan_maxpow = 0;
4573 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
4574 tap->rate = mcs | IEEE80211_RATE_MCS;
4575 else
4576 tap->rate = rt2860_rxrate(rxwi);
4578 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
4579 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4581 if (shortgi)
4582 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4584 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
4587 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4588 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4589 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
4590 device_get_nameunit(sc->dev),
4591 len, phymode, bw, shortgi, mcs,
4592 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
4593 rxwi->snr[0], rxwi->snr[1],
4594 rxwi->wcid, ampdu, amsdu);
4596 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
4598 ieee80211_input(ic, m, ni, rssi_dbm - RT2860_NOISE_FLOOR, RT2860_NOISE_FLOOR, 0);
4600 ieee80211_free_node(ni);
4602 skip:
4604 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
4606 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4607 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4609 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
4611 limit--;
4614 if (ring->cur == 0)
4615 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4616 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4617 else
4618 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4619 ring->cur - 1);
4621 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4622 "%s: Rx eof: nframes=%d\n",
4623 device_get_nameunit(sc->dev), nframes);
4625 sc->rx_packets += nframes;
4627 return (limit == 0);
4631 * rt2860_tx_eof
4633 static void rt2860_tx_eof(struct rt2860_softc *sc,
4634 struct rt2860_softc_tx_ring *ring)
4636 struct ifnet *ifp;
4637 struct rt2860_txdesc *desc;
4638 struct rt2860_softc_tx_data *data;
4639 uint32_t index;
4640 int ndescs, nframes;
4642 ifp = sc->ifp;
4644 ndescs = 0;
4645 nframes = 0;
4647 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4648 BUS_DMASYNC_POSTREAD);
4650 for (;;)
4652 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
4653 if (ring->desc_next == index)
4654 break;
4656 ndescs++;
4658 rt2860_drain_fifo_stats(sc);
4660 desc = &ring->desc[ring->desc_next];
4662 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
4663 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
4665 nframes++;
4667 data = &ring->data[ring->data_next];
4669 if (data->m->m_flags & M_TXCB)
4670 ieee80211_process_callback(data->ni, data->m, 0);
4672 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4673 BUS_DMASYNC_POSTWRITE);
4674 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4676 m_freem(data->m);
4678 ieee80211_free_node(data->ni);
4680 data->m = NULL;
4681 data->ni = NULL;
4683 ifp->if_opackets++;
4685 ring->data_queued--;
4686 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4689 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
4691 ring->desc_queued--;
4692 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4695 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4696 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4698 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4699 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
4700 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
4704 * rt2860_update_stats
4706 static void rt2860_update_stats(struct rt2860_softc *sc)
4708 struct ifnet *ifp;
4709 struct ieee80211com *ic;
4710 struct ieee80211_node *ni;
4711 uint16_t associd;
4712 uint8_t wcid;
4713 uint32_t stacnt[3];
4714 int txcnt, retrycnt, failcnt;
4716 ifp = sc->ifp;
4717 ic = &sc->ic;
4719 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4720 "%s: update statistic\n",
4721 device_get_nameunit(sc->dev));
4723 rt2860_drain_fifo_stats(sc);
4725 if (ic->ic_opmode == IEEE80211_M_STA)
4727 ni = ic->ic_bss;
4729 associd = (ni != NULL) ? ni->ni_associd : 0;
4730 wcid = RT2860_AID2WCID(associd);
4732 /* read and clear Tx statistic registers */
4734 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
4735 stacnt, sizeof(stacnt));
4737 txcnt = le32toh(stacnt[1]) & 0xffff;
4738 retrycnt = le32toh(stacnt[1]) >> 16;
4739 failcnt = le32toh(stacnt[0]) & 0xffff;
4741 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4742 "%s: update statistic: associd=0x%04x, txcnt=%d, retrycnt=%d, failcnt=%d\n",
4743 device_get_nameunit(sc->dev),
4744 associd, txcnt, retrycnt, failcnt);
4746 ifp->if_oerrors += failcnt;
4748 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
4749 txcnt + retrycnt + failcnt, txcnt + retrycnt, retrycnt + failcnt);
4754 * rt2860_watchdog
4756 static void rt2860_watchdog(struct rt2860_softc *sc)
4758 uint32_t tmp;
4759 int ntries;
4761 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4763 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
4764 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4765 device_get_nameunit(sc->dev), tmp);
4767 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
4769 sc->tx_queue_not_empty[0]++;
4771 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
4773 for (ntries = 0; ntries < 10; ntries++)
4775 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4776 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
4777 break;
4779 DELAY(1);
4782 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4785 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
4787 sc->tx_queue_not_empty[1]++;
4789 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
4791 for (ntries = 0; ntries < 10; ntries++)
4793 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4794 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
4795 break;
4797 DELAY(1);
4800 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4805 * rt2860_drain_fifo_stats
4807 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
4809 struct ifnet *ifp;
4810 uint32_t stats;
4811 uint8_t wcid, mcs, pid;
4812 int ok, retrycnt;
4814 ifp = sc->ic.ic_ifp;
4816 /* drain Tx status FIFO (maxsize = 16) */
4818 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
4819 RT2860_REG_TX_STA_FIFO_VALID)
4821 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
4822 RT2860_REG_TX_STA_FIFO_WCID_MASK;
4824 /* if no ACK was requested, no feedback is available */
4826 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
4827 continue;
4829 /* update AMRR statistic */
4831 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
4832 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
4833 RT2860_REG_TX_STA_FIFO_MCS_MASK;
4834 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
4835 RT2860_REG_TX_STA_FIFO_PID_MASK;
4836 retrycnt = pid - mcs;
4838 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4839 "%s: FIFO statistic: wcid=0x%02x, ok=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
4840 device_get_nameunit(sc->dev),
4841 wcid, ok, mcs, pid, retrycnt);
4843 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
4845 if (!ok)
4846 ifp->if_oerrors++;
4851 * rt2860_update_raw_counters
4853 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
4855 uint32_t tmp;
4857 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
4859 sc->rx_crc_errors += tmp & 0xffff;
4860 sc->rx_phy_errors += tmp >> 16;
4862 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
4864 sc->rx_false_ccas += tmp & 0xffff;
4865 sc->rx_plcp_errors += tmp >> 16;
4867 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
4869 sc->rx_dup_packets += tmp & 0xffff;
4870 sc->rx_fifo_overflows += tmp >> 16;
4874 * rt2860_intr_enable
4876 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
4878 uint32_t tmp;
4880 sc->intr_disable_mask &= ~intr_mask;
4882 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4884 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4888 * rt2860_intr_disable
4890 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
4892 uint32_t tmp;
4894 sc->intr_disable_mask |= intr_mask;
4896 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4898 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4902 * rt2860_txrx_enable
4904 static int rt2860_txrx_enable(struct rt2860_softc *sc)
4906 struct ieee80211com *ic;
4907 struct ifnet *ifp;
4908 uint32_t tmp;
4909 int ntries;
4911 ic = &sc->ic;
4912 ifp = ic->ic_ifp;
4914 /* enable Tx/Rx DMA engine */
4916 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
4918 for (ntries = 0; ntries < 200; ntries++)
4920 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4921 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
4922 break;
4924 DELAY(1000);
4927 if (ntries == 200)
4929 printf("%s: timeout waiting for DMA engine\n",
4930 device_get_nameunit(sc->dev));
4931 return -1;
4934 DELAY(50);
4936 tmp |= RT2860_REG_TX_WB_DDONE |
4937 RT2860_REG_RX_DMA_ENABLE |
4938 RT2860_REG_TX_DMA_ENABLE |
4939 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
4941 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4943 /* set Rx filter */
4945 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
4946 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
4948 if (ic->ic_opmode != IEEE80211_M_MONITOR)
4950 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
4951 RT2860_REG_RX_FILTER_DROP_CTS |
4952 RT2860_REG_RX_FILTER_DROP_BA |
4953 RT2860_REG_RX_FILTER_DROP_ACK |
4954 RT2860_REG_RX_FILTER_DROP_VER_ERR |
4955 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
4956 RT2860_REG_RX_FILTER_DROP_CFACK |
4957 RT2860_REG_RX_FILTER_DROP_CFEND;
4959 if (ic->ic_opmode == IEEE80211_M_STA)
4960 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
4961 RT2860_REG_RX_FILTER_DROP_PSPOLL;
4963 if (!(ifp->if_flags & IFF_PROMISC))
4964 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
4967 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
4969 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
4970 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
4972 return 0;
4976 * rt2860_alloc_rx_ring
4978 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
4979 struct rt2860_softc_rx_ring *ring)
4981 struct rt2860_rxdesc *desc;
4982 struct rt2860_softc_rx_data *data;
4983 bus_dma_segment_t segs[1];
4984 int i, nsegs, error;
4986 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
4987 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4988 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
4989 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
4990 0, NULL, NULL, &ring->desc_dma_tag);
4991 if (error != 0)
4993 printf("%s: could not create Rx desc DMA tag\n",
4994 device_get_nameunit(sc->dev));
4995 goto fail;
4998 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
4999 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5000 if (error != 0)
5002 printf("%s: could not allocate Rx desc DMA memory\n",
5003 device_get_nameunit(sc->dev));
5004 goto fail;
5007 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5008 ring->desc,
5009 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5010 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5011 if (error != 0)
5013 printf("%s: could not load Rx desc DMA map\n",
5014 device_get_nameunit(sc->dev));
5015 goto fail;
5018 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5019 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5020 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
5021 &ring->data_dma_tag);
5022 if (error != 0)
5024 printf("%s: could not create Rx data DMA tag\n",
5025 device_get_nameunit(sc->dev));
5026 goto fail;
5029 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5031 desc = &ring->desc[i];
5032 data = &ring->data[i];
5034 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5035 if (error != 0)
5037 printf("%s: could not create Rx data DMA map\n",
5038 device_get_nameunit(sc->dev));
5039 goto fail;
5042 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5043 if (data->m == NULL)
5045 printf("%s: could not allocate Rx mbuf\n",
5046 device_get_nameunit(sc->dev));
5047 error = ENOMEM;
5048 goto fail;
5051 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5053 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5054 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5055 if (error != 0)
5057 printf("%s: could not load Rx mbuf DMA map\n",
5058 device_get_nameunit(sc->dev));
5059 goto fail;
5062 KASSERT(nsegs == 1, ("%s: too many DMA segments",
5063 device_get_name(sc->dev)));
5065 desc->sdp0 = htole32(segs[0].ds_addr);
5066 desc->sdl0 = htole16(MJUMPAGESIZE);
5069 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5070 BUS_DMASYNC_PREWRITE);
5072 return 0;
5074 fail:
5076 rt2860_free_rx_ring(sc, ring);
5078 return error;
5082 * rt2860_reset_rx_ring
5084 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5085 struct rt2860_softc_rx_ring *ring)
5087 struct rt2860_rxdesc *desc;
5088 int i;
5090 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5092 desc = &ring->desc[i];
5094 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5097 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5098 BUS_DMASYNC_PREWRITE);
5100 ring->cur = 0;
5104 * rt2860_free_rx_ring
5106 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5107 struct rt2860_softc_rx_ring *ring)
5109 struct rt2860_softc_rx_data *data;
5110 int i;
5112 if (ring->desc != NULL)
5114 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5115 BUS_DMASYNC_POSTWRITE);
5116 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5117 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5118 ring->desc_dma_map);
5121 if (ring->desc_dma_tag != NULL)
5122 bus_dma_tag_destroy(ring->desc_dma_tag);
5124 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5126 data = &ring->data[i];
5128 if (data->m != NULL)
5130 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5131 BUS_DMASYNC_POSTREAD);
5132 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5133 m_freem(data->m);
5136 if (data->dma_map != NULL)
5137 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5140 if (ring->data_dma_tag != NULL)
5141 bus_dma_tag_destroy(ring->data_dma_tag);
5145 * rt2860_alloc_tx_ring
5147 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5148 struct rt2860_softc_tx_ring *ring, int qid)
5150 struct rt2860_softc_tx_data *data;
5151 int error, i;
5153 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5154 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5155 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5156 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5157 0, NULL, NULL, &ring->desc_dma_tag);
5158 if (error != 0)
5160 printf("%s: could not create Tx desc DMA tag\n",
5161 device_get_nameunit(sc->dev));
5162 goto fail;
5165 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5166 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5167 if (error != 0)
5169 printf("%s: could not allocate Tx desc DMA memory\n",
5170 device_get_nameunit(sc->dev));
5171 goto fail;
5174 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5175 ring->desc,
5176 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5177 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5178 if (error != 0)
5180 printf("%s: could not load Tx desc DMA map\n",
5181 device_get_nameunit(sc->dev));
5182 goto fail;
5185 ring->desc_queued = 0;
5186 ring->desc_cur = 0;
5187 ring->desc_next = 0;
5189 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5190 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5191 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5192 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5193 0, NULL, NULL, &ring->seg0_dma_tag);
5194 if (error != 0)
5196 printf("%s: could not create Tx seg0 DMA tag\n",
5197 device_get_nameunit(sc->dev));
5198 goto fail;
5201 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5202 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5203 if (error != 0)
5205 printf("%s: could not allocate Tx seg0 DMA memory\n",
5206 device_get_nameunit(sc->dev));
5207 goto fail;
5210 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5211 ring->seg0,
5212 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5213 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5214 if (error != 0)
5216 printf("%s: could not load Tx seg0 DMA map\n",
5217 device_get_nameunit(sc->dev));
5218 goto fail;
5221 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5222 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5223 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5224 &ring->data_dma_tag);
5225 if (error != 0)
5227 printf("%s: could not create Tx data DMA tag\n",
5228 device_get_nameunit(sc->dev));
5229 goto fail;
5232 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5234 data = &ring->data[i];
5236 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5237 if (error != 0)
5239 printf("%s: could not create Tx data DMA map\n",
5240 device_get_nameunit(sc->dev));
5241 goto fail;
5245 ring->data_queued = 0;
5246 ring->data_cur = 0;
5247 ring->data_next = 0;
5249 ring->qid = qid;
5251 return 0;
5253 fail:
5255 rt2860_free_tx_ring(sc, ring);
5257 return error;
5261 * rt2860_reset_tx_ring
5263 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5264 struct rt2860_softc_tx_ring *ring)
5266 struct rt2860_softc_tx_data *data;
5267 struct rt2860_txdesc *desc;
5268 int i;
5270 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5272 desc = &ring->desc[i];
5274 desc->sdl0 = 0;
5275 desc->sdl1 = 0;
5278 ring->desc_queued = 0;
5279 ring->desc_cur = 0;
5280 ring->desc_next = 0;
5282 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5283 BUS_DMASYNC_PREWRITE);
5285 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5286 BUS_DMASYNC_PREWRITE);
5288 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5290 data = &ring->data[i];
5292 if (data->m != NULL)
5294 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5295 BUS_DMASYNC_POSTWRITE);
5296 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5297 m_freem(data->m);
5298 data->m = NULL;
5301 if (data->ni != NULL)
5303 ieee80211_free_node(data->ni);
5304 data->ni = NULL;
5308 ring->data_queued = 0;
5309 ring->data_cur = 0;
5310 ring->data_next = 0;
5314 * rt2860_free_tx_ring
5316 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5317 struct rt2860_softc_tx_ring *ring)
5319 struct rt2860_softc_tx_data *data;
5320 int i;
5322 if (ring->desc != NULL)
5324 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5325 BUS_DMASYNC_POSTWRITE);
5326 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5327 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5328 ring->desc_dma_map);
5331 if (ring->desc_dma_tag != NULL)
5332 bus_dma_tag_destroy(ring->desc_dma_tag);
5334 if (ring->seg0 != NULL)
5336 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5337 BUS_DMASYNC_POSTWRITE);
5338 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5339 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5340 ring->seg0_dma_map);
5343 if (ring->seg0_dma_tag != NULL)
5344 bus_dma_tag_destroy(ring->seg0_dma_tag);
5346 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5348 data = &ring->data[i];
5350 if (data->m != NULL)
5352 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5353 BUS_DMASYNC_POSTWRITE);
5354 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5355 m_freem(data->m);
5358 if (data->ni != NULL)
5359 ieee80211_free_node(data->ni);
5361 if (data->dma_map != NULL)
5362 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5365 if (ring->data_dma_tag != NULL)
5366 bus_dma_tag_destroy(ring->data_dma_tag);
5370 * rt2860_dma_map_addr
5372 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5373 int nseg, int error)
5375 if (error != 0)
5376 return;
5378 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5380 *(bus_addr_t *) arg = segs[0].ds_addr;
5384 * rt2860_sysctl_attach
5386 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5388 struct sysctl_ctx_list *ctx;
5389 struct sysctl_oid *tree;
5390 struct sysctl_oid *stats;
5392 ctx = device_get_sysctl_ctx(sc->dev);
5393 tree = device_get_sysctl_tree(sc->dev);
5395 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5396 "stats", CTLFLAG_RD, 0, "statistic");
5398 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5399 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5400 "all interrupts");
5402 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5403 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5404 "Tx coherent interrupts");
5406 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5407 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5408 "Rx coherent interrupts");
5410 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5411 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5412 "Tx/Rx coherent interrupts");
5414 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5415 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5416 "FIFO statistic full interrupts");
5418 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5419 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5420 "Rx interrupts");
5422 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5423 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5424 "Rx delay interrupts");
5426 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5427 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5428 "Tx MGMT interrupts");
5430 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5431 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5432 "Tx HCCA interrupts");
5434 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5435 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5436 "Tx AC3 interrupts");
5438 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5439 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5440 "Tx AC2 interrupts");
5442 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5443 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5444 "Tx AC1 interrupts");
5446 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5447 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5448 "Tx AC0 interrupts");
5450 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5451 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
5452 "Tx delay interrupts");
5454 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5455 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
5456 "Pre-TBTT interrupts");
5458 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5459 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
5460 "TBTT interrupts");
5462 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5463 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
5464 "MCU command interrupts");
5466 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5467 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
5468 "auto wakeup interrupts");
5470 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5471 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
5472 "GP timer interrupts");
5474 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5475 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
5476 "Tx MGMT descriptors queued");
5478 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5479 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
5480 "Tx MGMT data queued");
5482 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5483 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
5484 "Tx HCCA descriptors queued");
5486 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5487 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
5488 "Tx HCCA data queued");
5490 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5491 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
5492 "Tx AC3 descriptors queued");
5494 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5495 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
5496 "Tx AC3 data queued");
5498 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5499 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
5500 "Tx AC2 descriptors queued");
5502 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5503 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
5504 "Tx AC2 data queued");
5506 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5507 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
5508 "Tx AC1 descriptors queued");
5510 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5511 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
5512 "Tx AC1 data queued");
5514 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5515 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
5516 "Tx AC0 descriptors queued");
5518 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5519 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
5520 "Tx AC0 data queued");
5522 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5523 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5524 "Tx watchdog timeouts");
5526 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5527 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
5528 "Tx defragmented packets");
5530 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5531 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
5532 "no Tx descriptors available");
5534 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5535 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
5536 "Rx mbuf allocation errors");
5538 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5539 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
5540 "Rx mbuf DMA mapping errors");
5542 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5543 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5544 "Tx queue 0 not empty");
5546 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5547 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5548 "Tx queue 1 not empty");
5550 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5551 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
5552 "Rx packets");
5554 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5555 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
5556 "Rx CRC errors");
5558 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5559 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
5560 "Rx PHY errors");
5562 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5563 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
5564 "Rx false CCAs");
5566 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5567 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
5568 "Rx PLCP errors");
5570 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5571 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
5572 "Rx duplicate packets");
5574 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5575 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
5576 "Rx FIFO overflows");
5579 static device_method_t rt2860_dev_methods[] =
5581 DEVMETHOD(device_probe, rt2860_probe),
5582 DEVMETHOD(device_attach, rt2860_attach),
5583 DEVMETHOD(device_detach, rt2860_detach),
5584 DEVMETHOD(device_shutdown, rt2860_shutdown),
5585 DEVMETHOD(device_suspend, rt2860_suspend),
5586 DEVMETHOD(device_resume, rt2860_resume),
5587 { 0, 0 }
5590 static driver_t rt2860_driver =
5592 "rt2860",
5593 rt2860_dev_methods,
5594 sizeof(struct rt2860_softc)
5597 static devclass_t rt2860_dev_class;
5599 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
5601 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
5602 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);