Resolved compiler warning
[ralink_drivers/rt2860_fbsd72.git] / rt2860.c
blob7694c5f159d3a88aaff25ed6e86d616627427292
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;
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);
2387 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2388 "%s: adding A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2389 device_get_nameunit(sc->dev), associd, tid);
2391 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2393 tmp |= (0x10000 << tid);
2395 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2396 break;
2398 /* IEEE80211_ACTION_BA_DELBA */
2399 case IEEE80211_ACTION_BA_DELBA:
2400 baparamset = LE_READ_2(frm + 2);
2401 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2403 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2404 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2405 device_get_nameunit(sc->dev), associd, tid);
2407 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2409 tmp &= ~(0x10000 << tid);
2411 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2412 break;
2417 * rt2860_send_action
2419 static int rt2860_send_action(struct ieee80211_node *ni,
2420 int category, int action, uint16_t args[4])
2422 struct rt2860_softc *sc;
2423 struct ieee80211com *ic;
2424 struct ifnet *ifp;
2425 uint16_t associd, baparamset;
2426 uint8_t wcid;
2427 int ret, tid;
2428 uint32_t tmp;
2430 ic = ni->ni_ic;
2431 ifp = ic->ic_ifp;
2432 sc = ifp->if_softc;
2434 ret = sc->send_action(ni, category, action, args);
2436 if (category != IEEE80211_ACTION_CAT_BA)
2437 return ret;
2439 associd = (ni != NULL) ? ni->ni_associd : 0;
2440 wcid = RT2860_AID2WCID(associd);
2442 switch (action)
2444 /* IEEE80211_ACTION_BA_DELBA */
2445 case IEEE80211_ACTION_BA_DELBA:
2446 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) |
2447 RT2860_SM(args[1], IEEE80211_DELBAPS_INIT);
2449 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) == IEEE80211_DELBAPS_INIT)
2450 break;
2452 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2454 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2455 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2456 device_get_nameunit(sc->dev), associd, tid);
2458 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2460 tmp &= ~(0x10000 << tid);
2462 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2463 break;
2466 return ret;
2470 * rt2860_amrr_update_iter_func
2472 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2474 struct rt2860_softc *sc;
2475 struct ieee80211com *ic;
2476 uint8_t wcid;
2478 sc = arg;
2479 ic = &sc->ic;
2481 /* only associated stations */
2483 if (ni->ni_associd != 0)
2485 wcid = RT2860_AID2WCID(ni->ni_associd);
2487 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2489 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2490 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2491 device_get_nameunit(sc->dev),
2492 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2493 ni->ni_associd,
2494 (ni->ni_flags & IEEE80211_NODE_HT) ?
2495 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2496 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2497 (ni->ni_flags & IEEE80211_NODE_HT) ?
2498 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2499 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2504 * rt2860_periodic
2506 static void rt2860_periodic(void *arg)
2508 struct rt2860_softc *sc;
2510 sc = arg;
2512 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2513 "%s: periodic\n",
2514 device_get_nameunit(sc->dev));
2516 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2520 * rt2860_tx_watchdog
2522 static void rt2860_tx_watchdog(void *arg)
2524 struct rt2860_softc *sc;
2525 struct ifnet *ifp;
2527 sc = arg;
2528 ifp = sc->ifp;
2530 if (sc->tx_timer == 0)
2531 return;
2533 if (--sc->tx_timer == 0)
2535 printf("%s: Tx watchdog timeout: resetting\n",
2536 device_get_nameunit(sc->dev));
2538 rt2860_stop_locked(sc);
2539 rt2860_init_locked(sc);
2541 ifp->if_oerrors++;
2543 sc->tx_watchdog_timeouts++;
2546 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2550 * rt2860_asic_set_bssid
2552 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2553 const uint8_t *bssid)
2555 uint32_t tmp;
2557 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2558 "%s: set bssid: bssid=%s\n",
2559 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2561 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2563 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2565 tmp = bssid[4] | (bssid[5] << 8);
2567 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2571 * rt2860_asic_set_macaddr
2573 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2574 const uint8_t *addr)
2576 uint32_t tmp;
2578 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2580 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2582 tmp = addr[4] | (addr[5] << 8);
2584 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2588 * rt2860_asic_enable_tsf_sync
2590 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2592 struct ieee80211com *ic;
2593 uint32_t tmp;
2595 ic = &sc->ic;
2597 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2598 "%s: enabling TSF\n",
2599 device_get_nameunit(sc->dev));
2601 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2603 tmp &= ~0x1fffff;
2604 tmp |= ic->ic_bss->ni_intval * 16;
2605 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2607 if (ic->ic_opmode == IEEE80211_M_STA)
2609 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2611 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2613 tmp |= RT2860_REG_BCN_TX_ENABLE;
2614 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2616 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2618 tmp |= RT2860_REG_BCN_TX_ENABLE;
2619 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2622 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2626 * rt2860_asic_disable_tsf_sync
2628 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2630 uint32_t tmp;
2632 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2633 "%s: disabling TSF\n",
2634 device_get_nameunit(sc->dev));
2636 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2638 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2639 RT2860_REG_TSF_TIMER_ENABLE |
2640 RT2860_REG_TBTT_TIMER_ENABLE);
2642 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2643 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2645 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2649 * rt2860_asic_enable_mrr
2651 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2653 #define CCK(mcs) (mcs)
2654 #define OFDM(mcs) ((1 << 3) | (mcs))
2655 #define HT(mcs) (mcs)
2657 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2658 (OFDM(6) << 28) | /* 54 -> 48 */
2659 (OFDM(5) << 24) | /* 48 -> 36 */
2660 (OFDM(4) << 20) | /* 36 -> 24 */
2661 (OFDM(3) << 16) | /* 24 -> 18 */
2662 (OFDM(2) << 12) | /* 18 -> 12 */
2663 (OFDM(1) << 8) | /* 12 -> 9 */
2664 (OFDM(0) << 4) | /* 9 -> 6 */
2665 OFDM(0)); /* 6 -> 6 */
2667 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2668 (CCK(2) << 12) | /* 11 -> 5.5 */
2669 (CCK(1) << 8) | /* 5.5 -> 2 */
2670 (CCK(0) << 4) | /* 2 -> 1 */
2671 CCK(0)); /* 1 -> 1 */
2673 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2674 (HT(6) << 28) |
2675 (HT(5) << 24) |
2676 (HT(4) << 20) |
2677 (HT(3) << 16) |
2678 (HT(2) << 12) |
2679 (HT(1) << 8) |
2680 (HT(0) << 4) |
2681 HT(0));
2683 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2684 (HT(14) << 28) |
2685 (HT(13) << 24) |
2686 (HT(12) << 20) |
2687 (HT(11) << 16) |
2688 (HT(10) << 12) |
2689 (HT(9) << 8) |
2690 (HT(8) << 4) |
2691 HT(8));
2693 #undef HT
2694 #undef OFDM
2695 #undef CCK
2699 * rt2860_asic_set_txpreamble
2701 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2703 struct ieee80211com *ic;
2704 uint32_t tmp;
2706 ic = &sc->ic;
2708 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2709 "%s: %s short Tx preamble\n",
2710 device_get_nameunit(sc->dev),
2711 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2713 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2715 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2717 if (sc->ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2718 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2720 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2724 * rt2860_asic_set_basicrates
2726 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2728 struct ieee80211com *ic;
2730 ic = &sc->ic;
2732 if (ic->ic_curmode == IEEE80211_MODE_11B)
2733 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2734 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2735 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2736 else
2737 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2741 * rt2860_asic_update_rtsthreshold
2743 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2745 struct ieee80211com *ic;
2746 uint32_t tmp;
2747 uint16_t threshold;
2749 ic = &sc->ic;
2751 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2752 "%s: updating RTS threshold: %d\n",
2753 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
2755 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2757 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2759 threshold = (ic->ic_rtsthreshold < IEEE80211_RTS_MAX) ?
2760 ic->ic_rtsthreshold : 0x1000;
2762 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2763 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2765 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
2769 * rt2860_asic_update_txpower
2771 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
2773 struct ieee80211com *ic;
2774 uint32_t *txpow_rate;
2775 int8_t delta;
2776 uint8_t val;
2777 uint32_t tmp;
2778 int i;
2780 ic = &sc->ic;
2782 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2783 "%s: updating Tx power: %d\n",
2784 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
2786 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
2788 txpow_rate = sc->txpow_rate_20mhz;
2790 else
2792 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2793 txpow_rate = sc->txpow_rate_40mhz_2ghz;
2794 else
2795 txpow_rate = sc->txpow_rate_40mhz_5ghz;
2798 delta = 0;
2800 val = rt2860_io_bbp_read(sc, 1);
2801 val &= 0xfc;
2803 if (ic->ic_txpowlimit > 90)
2805 /* do nothing */
2807 else if (ic->ic_txpowlimit > 60)
2809 delta -= 1;
2811 else if (ic->ic_txpowlimit > 30)
2813 delta -= 3;
2815 else if (ic->ic_txpowlimit > 15)
2817 val |= 0x1;
2819 else if (ic->ic_txpowlimit > 9)
2821 val |= 0x1;
2822 delta -= 3;
2824 else
2826 val |= 0x2;
2829 rt2860_io_bbp_write(sc, 1, val);
2831 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
2833 if (txpow_rate[i] == 0xffffffff)
2834 continue;
2836 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
2838 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
2843 * rt2860_asic_update_promisc
2845 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
2847 struct ifnet *ifp;
2848 uint32_t tmp;
2850 ifp = sc->ic.ic_ifp;
2852 printf("%s: %s promiscuous mode\n",
2853 device_get_nameunit(sc->dev),
2854 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
2856 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
2858 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
2860 if (!(ifp->if_flags & IFF_PROMISC))
2861 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
2863 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
2867 * rt2860_asic_updateprot
2869 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
2871 struct ieee80211com *ic;
2872 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
2873 uint8_t htopmode;
2875 ic = &sc->ic;
2877 /* CCK frame protection */
2879 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2880 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
2882 /* set up protection frame phy mode and rate (MCS code) */
2884 if (ic->ic_curmode == IEEE80211_MODE_11A)
2885 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2886 (0 << RT2860_REG_PROT_MCS_SHIFT);
2887 else
2888 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2889 (3 << RT2860_REG_PROT_MCS_SHIFT));
2891 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
2893 /* OFDM frame protection */
2895 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2896 RT2860_REG_TXOP_ALLOW_ALL;
2898 if (ic->ic_flags & IEEE80211_F_USEPROT)
2900 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2901 "%s: updating protection mode: b/g protection mode=%s\n",
2902 device_get_nameunit(sc->dev),
2903 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2904 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2906 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2907 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2908 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2909 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
2910 else
2911 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2913 else
2915 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2916 "%s: updating protection mode: b/g protection mode=%s\n",
2917 device_get_nameunit(sc->dev), "none");
2919 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2922 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
2924 /* HT frame protection */
2926 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
2927 htopmode = ic->ic_bss->ni_htopmode;
2928 else
2929 htopmode = ic->ic_curhtprotmode;
2931 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2932 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
2933 device_get_nameunit(sc->dev),
2934 htopmode & IEEE80211_HTINFO_OPMODE,
2935 (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2936 ((ic->ic_htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2938 switch (htopmode & IEEE80211_HTINFO_OPMODE)
2940 /* IEEE80211_HTINFO_OPMODE_HT20PR */
2941 case IEEE80211_HTINFO_OPMODE_HT20PR:
2942 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
2943 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2944 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
2945 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2946 (4 << RT2860_REG_PROT_MCS_SHIFT);
2948 gf20_prot = mm20_prot;
2950 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
2951 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2952 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2954 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2955 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2956 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2957 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2958 else
2959 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
2961 gf40_prot = mm40_prot;
2962 break;
2964 /* IEEE80211_HTINFO_OPMODE_MIXED */
2965 case IEEE80211_HTINFO_OPMODE_MIXED:
2966 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
2967 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2968 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
2970 if (ic->ic_flags & IEEE80211_F_USEPROT)
2971 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2972 (3 << RT2860_REG_PROT_MCS_SHIFT);
2973 else
2974 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2975 (4 << RT2860_REG_PROT_MCS_SHIFT);
2977 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2978 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2979 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2980 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
2981 else
2982 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
2984 gf20_prot = mm20_prot;
2986 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
2988 if (ic->ic_flags & IEEE80211_F_USEPROT)
2989 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2990 (3 << RT2860_REG_PROT_MCS_SHIFT);
2991 else
2992 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2993 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2995 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2996 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2997 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2998 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2999 else
3000 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3002 gf40_prot = mm40_prot;
3003 break;
3006 * IEEE80211_HTINFO_OPMODE_PURE
3007 * IEEE80211_HTINFO_OPMODE_PROTOPT
3009 case IEEE80211_HTINFO_OPMODE_PURE:
3010 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3011 default:
3012 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3013 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3014 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3015 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3016 (4 << RT2860_REG_PROT_MCS_SHIFT);
3018 gf20_prot = mm20_prot;
3020 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3021 RT2860_REG_TXOP_ALLOW_ALL |
3022 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3023 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3025 gf40_prot = mm40_prot;
3026 break;
3029 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3030 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3031 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3032 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3036 * rt2860_asic_updateslot
3038 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3040 struct ieee80211com *ic;
3041 uint32_t tmp;
3043 ic = &sc->ic;
3045 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3046 "%s: %s short slot time\n",
3047 device_get_nameunit(sc->dev),
3048 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3050 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3052 tmp &= ~0xff;
3053 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3055 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3059 * rt2860_asic_wme_update
3061 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3063 struct ieee80211com *ic;
3064 struct ieee80211_wme_state *wme;
3065 const struct wmeParams *wmep;
3066 int i;
3068 ic = &sc->ic;
3069 wme = &ic->ic_wme;
3070 wmep = wme->wme_chanParams.cap_wmeParams;
3072 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3073 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3074 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3075 device_get_nameunit(sc->dev),
3076 wmep[WME_AC_VO].wmep_aifsn,
3077 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3078 wmep[WME_AC_VO].wmep_txopLimit,
3079 wmep[WME_AC_VI].wmep_aifsn,
3080 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3081 wmep[WME_AC_VI].wmep_txopLimit,
3082 wmep[WME_AC_BK].wmep_aifsn,
3083 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3084 wmep[WME_AC_BK].wmep_txopLimit,
3085 wmep[WME_AC_BE].wmep_aifsn,
3086 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3087 wmep[WME_AC_BE].wmep_txopLimit);
3089 for (i = 0; i < WME_NUM_AC; i++)
3090 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3091 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3092 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3094 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3095 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3096 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3098 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3099 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3100 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3102 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3103 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3104 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3106 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3107 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3109 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3110 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3114 * rt2860_asic_update_beacon
3116 static int rt2860_asic_update_beacon(struct rt2860_softc *sc)
3118 struct ieee80211com *ic;
3119 struct mbuf *m;
3120 struct rt2860_txwi txwi;
3121 uint8_t rate, mcs;
3122 uint32_t tmp;
3124 ic = &sc->ic;
3126 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3127 if (m == NULL)
3128 return ENOMEM;
3130 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3131 mcs = rt2860_rate2mcs(rate);
3133 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3135 txwi.wcid = 0xff;
3136 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3137 RT2860_TXWI_MPDU_LEN_SHIFT);
3138 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3139 txwi.mpdu_density_flags |=
3140 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3141 txwi.bawin_size_xflags |=
3142 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3144 if (rate == 2)
3146 txwi.phymode_ifs_stbc_shortgi =
3147 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3149 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3150 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3152 else
3154 txwi.phymode_ifs_stbc_shortgi =
3155 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3158 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3159 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3161 /* disable temporarily TSF sync */
3163 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3165 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3166 RT2860_REG_TSF_TIMER_ENABLE |
3167 RT2860_REG_TBTT_TIMER_ENABLE);
3169 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3171 /* write Tx wireless info and beacon frame to on-chip memory */
3173 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3174 &txwi, sizeof(struct rt2860_txwi));
3176 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3177 mtod(m, uint8_t *), m->m_pkthdr.len);
3179 /* enable again TSF sync */
3181 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3183 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3184 RT2860_REG_TSF_TIMER_ENABLE |
3185 RT2860_REG_TBTT_TIMER_ENABLE);
3187 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3189 m_freem(m);
3191 return 0;
3195 * rt2860_asic_clear_keytables
3197 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3199 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3201 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID(0), 0, 2 * 256);
3203 /* clear WCID attribute table (entries = 256, entry size = 4) */
3205 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3207 /* clear IV/EIV table (entries = 256, entry size = 8) */
3209 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3211 /* clear pairwise key table (entries = 256, entry size = 32) */
3213 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3215 /* clear shared key table (entries = 32, entry size = 32) */
3217 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3219 /* clear shared key mode (entries = 32, entry size = 2) */
3221 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3225 * rt2860_rxrate
3227 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3229 uint8_t mcs, phymode;
3230 uint8_t rate;
3232 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3233 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3234 RT2860_RXWI_PHYMODE_MASK;
3236 rate = 2;
3238 switch (phymode)
3240 case RT2860_RXWI_PHYMODE_CCK:
3241 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3243 case 0: rate = 2; break; /* 1 Mbps */
3244 case 1: rate = 4; break; /* 2 MBps */
3245 case 2: rate = 11; break; /* 5.5 Mbps */
3246 case 3: rate = 22; break; /* 11 Mbps */
3248 break;
3250 case RT2860_RXWI_PHYMODE_OFDM:
3251 switch (mcs)
3253 case 0: rate = 12; break; /* 6 Mbps */
3254 case 1: rate = 18; break; /* 9 Mbps */
3255 case 2: rate = 24; break; /* 12 Mbps */
3256 case 3: rate = 36; break; /* 18 Mbps */
3257 case 4: rate = 48; break; /* 24 Mbps */
3258 case 5: rate = 72; break; /* 36 Mbps */
3259 case 6: rate = 96; break; /* 48 Mbps */
3260 case 7: rate = 108; break; /* 54 Mbps */
3262 break;
3264 case RT2860_RXWI_PHYMODE_HT_MIXED:
3265 case RT2860_RXWI_PHYMODE_HT_GF:
3266 break;
3269 return rate;
3273 * rt2860_maxrssi_rxpath
3275 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3276 const struct rt2860_rxwi *rxwi)
3278 uint8_t rxpath;
3280 rxpath = 0;
3282 if (sc->nrxpath > 1)
3283 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3284 rxpath = 1;
3286 if (sc->nrxpath > 2)
3287 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3288 rxpath = 2;
3290 return rxpath;
3294 * rt2860_rssi2dbm
3296 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3297 uint8_t rssi, uint8_t rxpath)
3299 struct ieee80211com *ic;
3300 struct ieee80211_channel *c;
3301 int chan;
3302 int8_t rssi_off, lna_gain;
3304 if (rssi == 0)
3305 return -99;
3307 ic = &sc->ic;
3308 c = ic->ic_curchan;
3309 chan = ieee80211_chan2ieee(ic, c);
3311 if (IEEE80211_IS_CHAN_5GHZ(c))
3313 rssi_off = sc->rssi_off_5ghz[rxpath];
3315 if (chan <= 64)
3316 lna_gain = sc->lna_gain[1];
3317 else if (chan <= 128)
3318 lna_gain = sc->lna_gain[2];
3319 else
3320 lna_gain = sc->lna_gain[3];
3322 else
3324 rssi_off = sc->rssi_off_2ghz[rxpath];
3325 lna_gain = sc->lna_gain[0];
3328 return (-12 - rssi_off - lna_gain - rssi);
3332 * rt2860_rate2mcs
3334 static uint8_t rt2860_rate2mcs(uint8_t rate)
3336 switch (rate)
3338 /* CCK rates */
3339 case 2: return 0;
3340 case 4: return 1;
3341 case 11: return 2;
3342 case 22: return 3;
3344 /* OFDM rates */
3345 case 12: return 0;
3346 case 18: return 1;
3347 case 24: return 2;
3348 case 36: return 3;
3349 case 48: return 4;
3350 case 72: return 5;
3351 case 96: return 6;
3352 case 108: return 7;
3355 return 0;
3359 * rt2860_ackrate
3361 static int rt2860_ackrate(struct ieee80211com *ic, int rate)
3363 switch (rate)
3365 /* CCK rates */
3367 case 2:
3368 return 2;
3370 case 4:
3371 case 11:
3372 case 22:
3373 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3375 /* OFDM rates */
3377 case 12:
3378 case 18:
3379 return 12;
3381 case 24:
3382 case 36:
3383 return 24;
3385 case 48:
3386 case 72:
3387 case 96:
3388 case 108:
3389 return 48;
3392 /* default to 1Mbps */
3393 return 2;
3397 * rt2860_txtime
3399 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags)
3401 uint16_t txtime;
3403 if (RT2860_RATE_IS_OFDM(rate))
3405 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3406 txtime = 16 + 4 + 4 * txtime + 6;
3408 else
3410 txtime = (16 * len + rate - 1) / rate;
3412 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3413 txtime += 72 + 24;
3414 else
3415 txtime += 144 + 48;
3418 return txtime;
3422 * rt2860_tx_frame
3424 static int rt2860_tx_frame(struct rt2860_softc *sc,
3425 struct mbuf *m, struct ieee80211_node *ni, int qid)
3427 struct ieee80211com *ic;
3428 struct rt2860_softc_tx_ring *ring;
3429 struct rt2860_softc_tx_data *data;
3430 struct rt2860_txdesc *desc;
3431 struct rt2860_txwi *txwi;
3432 struct ieee80211_frame *wh;
3433 struct rt2860_softc_tx_radiotap_header *tap;
3434 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3435 u_int hdrsize, hdrspace;
3436 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, qsel;
3437 uint16_t qos, len, dmalen, mpdu_len, dur;
3438 int error, hasqos, ackrate, ndmasegs, ndescs, i, j;
3440 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3441 ("%s: Tx frame: invalid qid=%d\n",
3442 device_get_nameunit(sc->dev), qid));
3444 ic = &sc->ic;
3446 ring = &sc->tx_ring[qid];
3447 desc = &ring->desc[ring->desc_cur];
3448 data = &ring->data[ring->data_cur];
3449 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3451 wh = mtod(m, struct ieee80211_frame *);
3453 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3455 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3456 if (hasqos)
3458 if (IEEE80211_HAS_ADDR4(wh))
3459 qos = le16toh(*(const uint16_t *)
3460 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3461 else
3462 qos = le16toh(*(const uint16_t *)
3463 (((struct ieee80211_qosframe *) wh)->i_qos));
3465 else
3467 qos = 0;
3470 if (ni->ni_flags & IEEE80211_NODE_HT)
3472 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3473 rate = 0;
3474 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3475 rate = ic->ic_fixed_rate;
3476 else
3477 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
3479 else
3481 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3482 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3483 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3484 rate = ic->ic_fixed_rate;
3485 else
3486 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
3489 rate &= IEEE80211_RATE_VAL;
3491 /* fill Tx wireless info */
3493 if (ni->ni_flags & IEEE80211_NODE_HT)
3494 mcs = rate;
3495 else
3496 mcs = rt2860_rate2mcs(rate);
3498 pid = mcs;
3500 /* management frames do not need encryption */
3502 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3503 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3505 /* calculate MPDU length without padding */
3507 hdrsize = ieee80211_hdrsize(wh);
3508 hdrspace = ieee80211_hdrspace(ic, wh);
3509 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3511 memset(txwi, 0, sizeof(struct rt2860_txwi));
3513 txwi->wcid = wcid;
3515 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3516 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3517 RT2860_TXWI_MPDU_LEN_SHIFT);
3519 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
3521 shortgi = (ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
3522 (ni->ni_flags & IEEE80211_NODE_HT);
3524 txwi->phymode_ifs_stbc_shortgi |=
3525 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
3526 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3528 if (ni->ni_flags & IEEE80211_NODE_HT)
3530 txwi->phymode_ifs_stbc_shortgi |=
3531 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3533 else
3535 if (!RT2860_RATE_IS_OFDM(rate))
3537 txwi->phymode_ifs_stbc_shortgi |=
3538 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3540 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3541 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3543 else
3545 txwi->phymode_ifs_stbc_shortgi |=
3546 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3550 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
3551 (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40))
3552 bw = RT2860_TXWI_BW_40;
3553 else
3554 bw = RT2860_TXWI_BW_20;
3556 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
3557 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3559 if (type != IEEE80211_FC0_TYPE_DATA)
3560 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3561 else
3562 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3564 /* skip ACKs for multicast frames and probe responses */
3566 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3567 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3568 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
3569 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
3571 txwi->bawin_size_xflags |=
3572 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3574 if (ni->ni_flags & IEEE80211_NODE_HT)
3576 /* preamble + plcp + signal extension */
3578 dur = 16 + 4 + 6;
3580 else
3582 ackrate = rt2860_ackrate(ic, rate);
3584 dur = rt2860_txtime(RT2860_ACK_SIZE, ackrate, ic->ic_flags) +
3585 sc->sifs;
3588 *(uint16_t *) wh->i_dur = htole16(dur);
3591 /* ask MAC to insert timestamp into probe responses */
3593 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3594 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3595 txwi->mpdu_density_flags |=
3596 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3598 if (bpf_peers_present(sc->drvbpf))
3600 tap = &sc->txtap;
3602 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3603 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3604 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3605 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3606 tap->chan_maxpow = 0;
3608 if (ni->ni_flags & IEEE80211_NODE_HT)
3609 tap->rate = mcs | IEEE80211_RATE_MCS;
3610 else
3611 tap->rate = rate;
3613 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3614 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3616 if (shortgi)
3617 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3619 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3620 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3622 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3624 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3626 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3628 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3630 else
3632 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3636 /* copy and trim 802.11 header */
3638 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3639 m_adj(m, hdrspace);
3641 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3642 dma_seg, &ndmasegs, 0);
3643 if (error != 0)
3645 /* too many fragments, linearize */
3647 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3648 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3649 device_get_nameunit(sc->dev));
3651 m = m_defrag(m, M_DONTWAIT);
3652 if (m == NULL)
3653 return ENOMEM;
3655 sc->tx_defrag_packets++;
3657 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3658 dma_seg, &ndmasegs, 0);
3659 if (error != 0)
3661 printf("%s: could not load mbuf DMA map: error=%d\n",
3662 device_get_nameunit(sc->dev), error);
3663 m_freem(m);
3664 return error;
3668 /* determine how many Tx descs are required */
3670 ndescs = 1 + ndmasegs / 2;
3671 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3673 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3674 "%s: there are not enough Tx descs\n",
3675 device_get_nameunit(sc->dev));
3677 sc->no_tx_desc_avail++;
3679 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3680 m_freem(m);
3681 return EFBIG;
3684 data->m = m;
3685 data->ni = ni;
3687 /* set up Tx descs */
3689 /* first segment is Tx wireless info and 802.11 header */
3691 len = sizeof(struct rt2860_txwi) + hdrsize;
3693 /* align end on a 4-bytes boundary */
3695 dmalen = (len + 3) & ~ 3;
3697 memset((caddr_t) txwi + len, 0, dmalen - len);
3699 qsel = RT2860_TXDESC_QSEL_EDCA;
3701 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3702 desc->sdl0 = htole16(dmalen);
3703 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3705 /* set up payload segments */
3707 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3709 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3710 desc->sdl1 = htole16(dma_seg[j].ds_len);
3712 ring->desc_queued++;
3713 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3715 j++;
3717 desc = &ring->desc[ring->desc_cur];
3719 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3720 desc->sdl0 = htole16(dma_seg[j].ds_len);
3721 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3723 j++;
3726 /* finalize last payload segment */
3728 if (i > 0)
3730 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3731 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3733 else
3735 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3736 desc->sdl1 = 0;
3739 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3740 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3741 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3742 device_get_nameunit(sc->dev),
3743 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3744 bw, stbc, shortgi, mcs, dmalen, ndmasegs,
3745 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);
3747 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3748 BUS_DMASYNC_PREWRITE);
3749 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3750 BUS_DMASYNC_PREWRITE);
3751 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3752 BUS_DMASYNC_PREWRITE);
3754 ring->desc_queued++;
3755 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3757 ring->data_queued++;
3758 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3760 /* kick Tx */
3762 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3764 return 0;
3768 * rt2860_tx_raw
3770 static int rt2860_tx_raw(struct rt2860_softc *sc,
3771 struct mbuf *m, struct ieee80211_node *ni,
3772 const struct ieee80211_bpf_params *params)
3774 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3775 "%s: Tx raw\n",
3776 device_get_nameunit(sc->dev));
3778 return 0;
3782 * rt2860_intr
3784 static void rt2860_intr(void *arg)
3786 struct rt2860_softc *sc;
3787 struct ifnet *ifp;
3788 uint32_t status;
3790 sc = arg;
3791 ifp = sc->ifp;
3793 /* acknowledge interrupts */
3795 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
3796 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
3798 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3799 "%s: interrupt: status = 0x%08x\n",
3800 device_get_nameunit(sc->dev), status);
3802 if (status == 0xffffffff || /* device likely went away */
3803 status == 0) /* not for us */
3804 return;
3806 sc->interrupts++;
3808 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
3809 return;
3811 if (status & RT2860_REG_INT_TX_COHERENT)
3812 rt2860_tx_coherent_intr(sc);
3814 if (status & RT2860_REG_INT_RX_COHERENT)
3815 rt2860_rx_coherent_intr(sc);
3817 if (status & RT2860_REG_INT_TXRX_COHERENT)
3818 rt2860_txrx_coherent_intr(sc);
3820 if (status & RT2860_REG_INT_FIFO_STA_FULL)
3821 rt2860_fifo_sta_full_intr(sc);
3823 if (status & RT2860_REG_INT_TX_MGMT_DONE)
3824 rt2860_tx_intr(sc, 5);
3826 if (status & RT2860_REG_INT_RX_DONE)
3827 rt2860_rx_intr(sc);
3829 if (status & RT2860_REG_INT_RX_DELAY_DONE)
3830 rt2860_rx_delay_intr(sc);
3832 if (status & RT2860_REG_INT_TX_HCCA_DONE)
3833 rt2860_tx_intr(sc, 4);
3835 if (status & RT2860_REG_INT_TX_AC3_DONE)
3836 rt2860_tx_intr(sc, 3);
3838 if (status & RT2860_REG_INT_TX_AC2_DONE)
3839 rt2860_tx_intr(sc, 2);
3841 if (status & RT2860_REG_INT_TX_AC1_DONE)
3842 rt2860_tx_intr(sc, 1);
3844 if (status & RT2860_REG_INT_TX_AC0_DONE)
3845 rt2860_tx_intr(sc, 0);
3847 if (status & RT2860_REG_INT_TX_DELAY_DONE)
3848 rt2860_tx_delay_intr(sc);
3850 if (status & RT2860_REG_INT_PRE_TBTT)
3851 rt2860_pre_tbtt_intr(sc);
3853 if (status & RT2860_REG_INT_TBTT)
3854 rt2860_tbtt_intr(sc);
3856 if (status & RT2860_REG_INT_MCU_CMD)
3857 rt2860_mcu_cmd_intr(sc);
3859 if (status & RT2860_REG_INT_AUTO_WAKEUP)
3860 rt2860_auto_wakeup_intr(sc);
3862 if (status & RT2860_REG_INT_GP_TIMER)
3863 rt2860_gp_timer_intr(sc);
3867 * rt2860_tx_coherent_intr
3869 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
3871 uint32_t tmp;
3872 int i;
3874 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3875 "%s: Tx coherent interrupt\n",
3876 device_get_nameunit(sc->dev));
3878 sc->tx_coherent_interrupts++;
3880 /* restart DMA engine */
3882 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3884 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3885 RT2860_REG_RX_DMA_ENABLE |
3886 RT2860_REG_TX_DMA_ENABLE);
3888 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3890 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3892 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3893 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3895 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3897 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3898 sc->tx_ring[i].desc_phys_addr);
3899 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3900 RT2860_SOFTC_TX_RING_DESC_COUNT);
3901 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3904 /* init Rx ring */
3906 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3908 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3909 sc->rx_ring.desc_phys_addr);
3910 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3911 RT2860_SOFTC_RX_RING_DATA_COUNT);
3912 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3913 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3915 rt2860_txrx_enable(sc);
3919 * rt2860_rx_coherent_intr
3921 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
3923 uint32_t tmp;
3924 int i;
3926 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3927 "%s: Rx coherent interrupt\n",
3928 device_get_nameunit(sc->dev));
3930 sc->rx_coherent_interrupts++;
3932 /* restart DMA engine */
3934 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3936 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3937 RT2860_REG_RX_DMA_ENABLE |
3938 RT2860_REG_TX_DMA_ENABLE);
3940 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3942 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3944 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3945 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3947 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3949 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3950 sc->tx_ring[i].desc_phys_addr);
3951 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3952 RT2860_SOFTC_TX_RING_DESC_COUNT);
3953 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3956 /* init Rx ring */
3958 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3960 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3961 sc->rx_ring.desc_phys_addr);
3962 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3963 RT2860_SOFTC_RX_RING_DATA_COUNT);
3964 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3965 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3967 rt2860_txrx_enable(sc);
3971 * rt2860_txrx_coherent_intr
3973 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
3975 uint32_t tmp;
3976 int i;
3978 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3979 "%s: Tx/Rx coherent interrupt\n",
3980 device_get_nameunit(sc->dev));
3982 sc->txrx_coherent_interrupts++;
3984 /* restart DMA engine */
3986 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3988 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3989 RT2860_REG_RX_DMA_ENABLE |
3990 RT2860_REG_TX_DMA_ENABLE);
3992 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3994 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3996 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3997 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3999 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4001 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4002 sc->tx_ring[i].desc_phys_addr);
4003 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4004 RT2860_SOFTC_TX_RING_DESC_COUNT);
4005 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4008 /* init Rx ring */
4010 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4012 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4013 sc->rx_ring.desc_phys_addr);
4014 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4015 RT2860_SOFTC_RX_RING_DATA_COUNT);
4016 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4017 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4019 rt2860_txrx_enable(sc);
4023 * rt2860_fifo_sta_full_intr
4025 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4027 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4028 "%s: FIFO statistic full interrupt\n",
4029 device_get_nameunit(sc->dev));
4031 sc->fifo_sta_full_interrupts++;
4033 RT2860_SOFTC_LOCK(sc);
4035 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4037 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4039 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4042 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4044 RT2860_SOFTC_UNLOCK(sc);
4048 * rt2860_rx_intr
4050 static void rt2860_rx_intr(struct rt2860_softc *sc)
4052 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4053 "%s: Rx interrupt\n",
4054 device_get_nameunit(sc->dev));
4056 sc->rx_interrupts++;
4058 RT2860_SOFTC_LOCK(sc);
4060 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4062 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4064 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4067 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4069 RT2860_SOFTC_UNLOCK(sc);
4073 * rt2860_rx_delay_intr
4075 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4077 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4078 "%s: Rx delay interrupt\n",
4079 device_get_nameunit(sc->dev));
4081 sc->rx_delay_interrupts++;
4085 * rt2860_tx_intr
4087 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4089 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4090 ("%s: Tx interrupt: invalid qid=%d\n",
4091 device_get_nameunit(sc->dev), qid));
4093 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4094 "%s: Tx interrupt: qid=%d\n",
4095 device_get_nameunit(sc->dev), qid);
4097 sc->tx_interrupts[qid]++;
4099 RT2860_SOFTC_LOCK(sc);
4101 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4103 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4105 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4108 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4110 RT2860_SOFTC_UNLOCK(sc);
4114 * rt2860_tx_delay_intr
4116 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4118 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4119 "%s: Tx delay interrupt\n",
4120 device_get_nameunit(sc->dev));
4122 sc->tx_delay_interrupts++;
4126 * rt2860_pre_tbtt_intr
4128 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4130 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4131 "%s: Pre-TBTT interrupt\n",
4132 device_get_nameunit(sc->dev));
4134 sc->pre_tbtt_interrupts++;
4138 * rt2860_tbtt_intr
4140 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4142 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4143 "%s: TBTT interrupt\n",
4144 device_get_nameunit(sc->dev));
4146 sc->tbtt_interrupts++;
4150 * rt2860_mcu_cmd_intr
4152 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4154 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4155 "%s: MCU command interrupt\n",
4156 device_get_nameunit(sc->dev));
4158 sc->mcu_cmd_interrupts++;
4162 * rt2860_auto_wakeup_intr
4164 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4166 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4167 "%s: auto wakeup interrupt\n",
4168 device_get_nameunit(sc->dev));
4170 sc->auto_wakeup_interrupts++;
4174 * rt2860_gp_timer_intr
4176 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4178 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4179 "%s: GP timer interrupt\n",
4180 device_get_nameunit(sc->dev));
4182 sc->gp_timer_interrupts++;
4186 * rt2860_rx_done_task
4188 static void rt2860_rx_done_task(void *context, int pending)
4190 struct rt2860_softc *sc;
4191 struct ifnet *ifp;
4192 int again;
4194 sc = context;
4195 ifp = sc->ifp;
4197 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4198 "%s: Rx done task\n",
4199 device_get_nameunit(sc->dev));
4201 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4202 return;
4204 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4206 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4208 RT2860_SOFTC_LOCK(sc);
4210 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4212 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4213 "%s: Rx done task: scheduling again\n",
4214 device_get_nameunit(sc->dev));
4216 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4218 else
4220 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4223 RT2860_SOFTC_UNLOCK(sc);
4227 * rt2860_tx_done_task
4229 static void rt2860_tx_done_task(void *context, int pending)
4231 struct rt2860_softc *sc;
4232 struct ifnet *ifp;
4233 uint32_t intr_mask;
4234 int i;
4236 sc = context;
4237 ifp = sc->ifp;
4239 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4240 "%s: Tx done task\n",
4241 device_get_nameunit(sc->dev));
4243 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4244 return;
4246 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4247 RT2860_REG_INT_TX_HCCA_DONE |
4248 RT2860_REG_INT_TX_AC3_DONE |
4249 RT2860_REG_INT_TX_AC2_DONE |
4250 RT2860_REG_INT_TX_AC1_DONE |
4251 RT2860_REG_INT_TX_AC0_DONE);
4253 sc->intr_pending_mask &= ~intr_mask;
4255 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4256 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4258 sc->tx_timer = 0;
4260 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4262 RT2860_SOFTC_LOCK(sc);
4264 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4265 (sc->intr_disable_mask & intr_mask));
4267 if (sc->intr_pending_mask & intr_mask)
4269 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4270 "%s: Tx done task: scheduling again\n",
4271 device_get_nameunit(sc->dev));
4273 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4276 RT2860_SOFTC_UNLOCK(sc);
4280 * rt2860_fifo_sta_full_task
4282 static void rt2860_fifo_sta_full_task(void *context, int pending)
4284 struct rt2860_softc *sc;
4285 struct ifnet *ifp;
4287 sc = context;
4288 ifp = sc->ifp;
4290 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4291 "%s: FIFO statistic full task\n",
4292 device_get_nameunit(sc->dev));
4294 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4295 return;
4297 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4299 rt2860_drain_fifo_stats(sc);
4301 RT2860_SOFTC_LOCK(sc);
4303 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4305 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4306 "%s: FIFO statistic full task: scheduling again\n",
4307 device_get_nameunit(sc->dev));
4309 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4311 else
4313 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4316 RT2860_SOFTC_UNLOCK(sc);
4320 * rt2860_periodic_task
4322 static void rt2860_periodic_task(void *context, int pending)
4324 struct rt2860_softc *sc;
4325 struct ifnet *ifp;
4326 struct ieee80211com *ic;
4328 sc = context;
4329 ifp = sc->ifp;
4330 ic = &sc->ic;
4332 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4333 "%s: periodic task: round=%lu\n",
4334 device_get_nameunit(sc->dev), sc->periodic_round);
4336 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4337 return;
4339 RT2860_SOFTC_LOCK(sc);
4341 sc->periodic_round++;
4343 rt2860_update_stats(sc);
4345 if ((sc->periodic_round % 10) == 0)
4347 rt2860_update_raw_counters(sc);
4349 rt2860_watchdog(sc);
4351 if (ic->ic_opmode == IEEE80211_M_STA)
4352 rt2860_amrr_update_iter_func(sc, ic->ic_bss);
4353 else
4354 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, sc);
4357 RT2860_SOFTC_UNLOCK(sc);
4359 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4363 * rt2860_rx_eof
4365 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4367 struct ieee80211com *ic;
4368 struct ifnet *ifp;
4369 struct ieee80211_frame *wh;
4370 struct ieee80211_node *ni;
4371 struct rt2860_softc_rx_radiotap_header *tap;
4372 struct rt2860_softc_rx_ring *ring;
4373 struct rt2860_rxdesc *desc;
4374 struct rt2860_softc_rx_data *data;
4375 struct rt2860_rxwi *rxwi;
4376 struct mbuf *m, *mnew;
4377 bus_dma_segment_t segs[1];
4378 uint32_t index, desc_flags;
4379 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4380 int8_t rssi_dbm;
4381 int error, nsegs, len, ampdu, amsdu, nframes;
4383 ic = &sc->ic;
4384 ifp = sc->ifp;
4385 ring = &sc->rx_ring;
4387 nframes = 0;
4389 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4390 BUS_DMASYNC_POSTREAD);
4392 while (limit != 0)
4394 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4395 if (ring->cur == index)
4396 break;
4398 desc = &ring->desc[ring->cur];
4399 desc_flags = le32toh(desc->flags);
4400 data = &ring->data[ring->cur];
4402 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4403 break;
4405 nframes++;
4407 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4408 if (mnew == NULL)
4410 sc->rx_mbuf_alloc_errors++;
4411 ifp->if_ierrors++;
4412 goto skip;
4415 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4417 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4418 BUS_DMASYNC_POSTREAD);
4419 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4421 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4422 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4423 if (error != 0)
4425 m_freem(mnew);
4427 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4428 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
4429 if (error != 0)
4430 panic("%s: could not load old Rx mbuf: error=%d\n",
4431 device_get_name(sc->dev), error);
4433 sc->rx_mbuf_dmamap_errors++;
4434 ifp->if_ierrors++;
4436 goto skip;
4439 KASSERT(nsegs == 1, ("%s: too many DMA segments",
4440 device_get_name(sc->dev)));
4442 m = data->m;
4444 data->m = mnew;
4445 desc->sdp0 = htole32(segs[0].ds_addr);
4447 /* get Rx wireless info */
4449 rxwi = mtod(m, struct rt2860_rxwi *);
4450 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4451 RT2860_RXWI_SIZE_MASK;
4453 /* check for L2 padding between IEEE 802.11 frame header and body */
4455 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4457 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4458 "%s: L2 padding: len=%d\n",
4459 device_get_nameunit(sc->dev), len);
4461 len += 2;
4464 m->m_pkthdr.rcvif = ifp;
4465 m->m_data = (caddr_t) (rxwi + 1);
4466 m->m_pkthdr.len = m->m_len = len;
4468 /* check for crc errors */
4470 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4472 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4473 "%s: rxdesc: crc error\n",
4474 device_get_nameunit(sc->dev));
4476 ifp->if_ierrors++;
4478 if (!(ifp->if_flags & IFF_PROMISC))
4480 m_freem(m);
4481 goto skip;
4485 wh = (struct ieee80211_frame *) (rxwi + 1);
4487 /* check for cipher errors */
4489 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4491 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4492 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4493 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4495 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4496 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4498 m->m_flags |= M_WEP;
4500 else
4502 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4503 "%s: rxdesc: cipher error=0x%02x\n",
4504 device_get_nameunit(sc->dev), cipher_err);
4506 ifp->if_ierrors++;
4508 if (!(ifp->if_flags & IFF_PROMISC))
4510 m_free(m);
4511 goto skip;
4515 else
4517 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4519 ifp->if_ierrors++;
4521 if (!(ifp->if_flags & IFF_PROMISC))
4523 m_free(m);
4524 goto skip;
4529 /* check for A-MPDU */
4531 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
4533 m->m_flags |= M_AMPDU;
4534 ampdu = 1;
4536 else
4538 ampdu = 0;
4541 ant = rt2860_maxrssi_rxpath(sc, rxwi);
4542 rssi = rxwi->rssi[ant];
4543 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
4544 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4545 RT2860_RXWI_PHYMODE_MASK);
4546 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
4547 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
4548 RT2860_RXWI_SHORTGI_MASK);
4549 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
4550 amsdu = (desc_flags & RT2860_RXDESC_FLAGS_AMSDU);
4552 if (bpf_peers_present(sc->drvbpf))
4554 tap = &sc->rxtap;
4556 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4557 tap->dbm_antsignal = rssi_dbm;
4558 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
4559 tap->antenna = ant;
4560 tap->antsignal = rssi;
4561 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4562 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4563 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4564 tap->chan_maxpow = 0;
4566 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
4567 tap->rate = mcs | IEEE80211_RATE_MCS;
4568 else
4569 tap->rate = rt2860_rxrate(rxwi);
4571 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
4572 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4574 if (shortgi)
4575 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4577 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
4580 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4581 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4582 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
4583 device_get_nameunit(sc->dev),
4584 len, phymode, bw, shortgi, mcs,
4585 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
4586 rxwi->snr[0], rxwi->snr[1],
4587 rxwi->wcid, ampdu, amsdu);
4589 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
4591 ieee80211_input(ic, m, ni, rssi_dbm - RT2860_NOISE_FLOOR, RT2860_NOISE_FLOOR, 0);
4593 ieee80211_free_node(ni);
4595 skip:
4597 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
4599 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4600 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4602 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
4604 limit--;
4607 if (ring->cur == 0)
4608 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4609 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4610 else
4611 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4612 ring->cur - 1);
4614 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4615 "%s: Rx eof: nframes=%d\n",
4616 device_get_nameunit(sc->dev), nframes);
4618 sc->rx_packets += nframes;
4620 return (limit == 0);
4624 * rt2860_tx_eof
4626 static void rt2860_tx_eof(struct rt2860_softc *sc,
4627 struct rt2860_softc_tx_ring *ring)
4629 struct ifnet *ifp;
4630 struct rt2860_txdesc *desc;
4631 struct rt2860_softc_tx_data *data;
4632 uint32_t index;
4633 int ndescs, nframes;
4635 ifp = sc->ifp;
4637 ndescs = 0;
4638 nframes = 0;
4640 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4641 BUS_DMASYNC_POSTREAD);
4643 for (;;)
4645 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
4646 if (ring->desc_next == index)
4647 break;
4649 ndescs++;
4651 rt2860_drain_fifo_stats(sc);
4653 desc = &ring->desc[ring->desc_next];
4655 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
4656 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
4658 nframes++;
4660 data = &ring->data[ring->data_next];
4662 if (data->m->m_flags & M_TXCB)
4663 ieee80211_process_callback(data->ni, data->m, 0);
4665 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4666 BUS_DMASYNC_POSTWRITE);
4667 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4669 m_freem(data->m);
4671 ieee80211_free_node(data->ni);
4673 data->m = NULL;
4674 data->ni = NULL;
4676 ifp->if_opackets++;
4678 ring->data_queued--;
4679 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4682 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
4684 ring->desc_queued--;
4685 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4688 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4689 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4691 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4692 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
4693 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
4697 * rt2860_update_stats
4699 static void rt2860_update_stats(struct rt2860_softc *sc)
4701 struct ifnet *ifp;
4702 struct ieee80211com *ic;
4703 struct ieee80211_node *ni;
4704 uint16_t associd;
4705 uint8_t wcid;
4706 uint32_t stacnt[3];
4707 int txcnt, retrycnt, failcnt;
4709 ifp = sc->ifp;
4710 ic = &sc->ic;
4712 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4713 "%s: update statistic\n",
4714 device_get_nameunit(sc->dev));
4716 rt2860_drain_fifo_stats(sc);
4718 if (ic->ic_opmode == IEEE80211_M_STA)
4720 ni = ic->ic_bss;
4722 associd = (ni != NULL) ? ni->ni_associd : 0;
4723 wcid = RT2860_AID2WCID(associd);
4725 /* read and clear Tx statistic registers */
4727 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
4728 stacnt, sizeof(stacnt));
4730 txcnt = le32toh(stacnt[1]) & 0xffff;
4731 retrycnt = le32toh(stacnt[1]) >> 16;
4732 failcnt = le32toh(stacnt[0]) & 0xffff;
4734 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4735 "%s: update statistic: associd=0x%04x, txcnt=%d, retrycnt=%d, failcnt=%d\n",
4736 device_get_nameunit(sc->dev),
4737 associd, txcnt, retrycnt, failcnt);
4739 ifp->if_oerrors += failcnt;
4741 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
4742 txcnt + retrycnt + failcnt, txcnt + retrycnt, retrycnt + failcnt);
4747 * rt2860_watchdog
4749 static void rt2860_watchdog(struct rt2860_softc *sc)
4751 uint32_t tmp;
4752 int ntries;
4754 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4756 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
4757 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4758 device_get_nameunit(sc->dev), tmp);
4760 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
4762 sc->tx_queue_not_empty[0]++;
4764 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
4766 for (ntries = 0; ntries < 10; ntries++)
4768 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4769 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
4770 break;
4772 DELAY(1);
4775 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4778 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
4780 sc->tx_queue_not_empty[1]++;
4782 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
4784 for (ntries = 0; ntries < 10; ntries++)
4786 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4787 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
4788 break;
4790 DELAY(1);
4793 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4798 * rt2860_drain_fifo_stats
4800 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
4802 struct ifnet *ifp;
4803 uint32_t stats;
4804 uint8_t wcid, mcs, pid;
4805 int ok, retrycnt;
4807 ifp = sc->ic.ic_ifp;
4809 /* drain Tx status FIFO (maxsize = 16) */
4811 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
4812 RT2860_REG_TX_STA_FIFO_VALID)
4814 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
4815 RT2860_REG_TX_STA_FIFO_WCID_MASK;
4817 /* if no ACK was requested, no feedback is available */
4819 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
4820 continue;
4822 /* update AMRR statistic */
4824 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
4825 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
4826 RT2860_REG_TX_STA_FIFO_MCS_MASK;
4827 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
4828 RT2860_REG_TX_STA_FIFO_PID_MASK;
4829 retrycnt = pid - mcs;
4831 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4832 "%s: FIFO statistic: wcid=0x%02x, ok=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
4833 device_get_nameunit(sc->dev),
4834 wcid, ok, mcs, pid, retrycnt);
4836 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
4838 if (!ok)
4839 ifp->if_oerrors++;
4844 * rt2860_update_raw_counters
4846 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
4848 uint32_t tmp;
4850 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
4852 sc->rx_crc_errors += tmp & 0xffff;
4853 sc->rx_phy_errors += tmp >> 16;
4855 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
4857 sc->rx_false_ccas += tmp & 0xffff;
4858 sc->rx_plcp_errors += tmp >> 16;
4860 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
4862 sc->rx_dup_packets += tmp & 0xffff;
4863 sc->rx_fifo_overflows += tmp >> 16;
4867 * rt2860_intr_enable
4869 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
4871 uint32_t tmp;
4873 sc->intr_disable_mask &= ~intr_mask;
4875 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4877 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4881 * rt2860_intr_disable
4883 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
4885 uint32_t tmp;
4887 sc->intr_disable_mask |= intr_mask;
4889 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4891 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4895 * rt2860_txrx_enable
4897 static int rt2860_txrx_enable(struct rt2860_softc *sc)
4899 struct ieee80211com *ic;
4900 struct ifnet *ifp;
4901 uint32_t tmp;
4902 int ntries;
4904 ic = &sc->ic;
4905 ifp = ic->ic_ifp;
4907 /* enable Tx/Rx DMA engine */
4909 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
4911 for (ntries = 0; ntries < 200; ntries++)
4913 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4914 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
4915 break;
4917 DELAY(1000);
4920 if (ntries == 200)
4922 printf("%s: timeout waiting for DMA engine\n",
4923 device_get_nameunit(sc->dev));
4924 return -1;
4927 DELAY(50);
4929 tmp |= RT2860_REG_TX_WB_DDONE |
4930 RT2860_REG_RX_DMA_ENABLE |
4931 RT2860_REG_TX_DMA_ENABLE |
4932 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
4934 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4936 /* set Rx filter */
4938 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
4939 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
4941 if (ic->ic_opmode != IEEE80211_M_MONITOR)
4943 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
4944 RT2860_REG_RX_FILTER_DROP_CTS |
4945 RT2860_REG_RX_FILTER_DROP_BA |
4946 RT2860_REG_RX_FILTER_DROP_ACK |
4947 RT2860_REG_RX_FILTER_DROP_VER_ERR |
4948 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
4949 RT2860_REG_RX_FILTER_DROP_CFACK |
4950 RT2860_REG_RX_FILTER_DROP_CFEND;
4952 if (ic->ic_opmode == IEEE80211_M_STA)
4953 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
4954 RT2860_REG_RX_FILTER_DROP_PSPOLL;
4956 if (!(ifp->if_flags & IFF_PROMISC))
4957 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
4960 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
4962 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
4963 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
4965 return 0;
4969 * rt2860_alloc_rx_ring
4971 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
4972 struct rt2860_softc_rx_ring *ring)
4974 struct rt2860_rxdesc *desc;
4975 struct rt2860_softc_rx_data *data;
4976 bus_dma_segment_t segs[1];
4977 int i, nsegs, error;
4979 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
4980 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4981 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
4982 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
4983 0, NULL, NULL, &ring->desc_dma_tag);
4984 if (error != 0)
4986 printf("%s: could not create Rx desc DMA tag\n",
4987 device_get_nameunit(sc->dev));
4988 goto fail;
4991 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
4992 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
4993 if (error != 0)
4995 printf("%s: could not allocate Rx desc DMA memory\n",
4996 device_get_nameunit(sc->dev));
4997 goto fail;
5000 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5001 ring->desc,
5002 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5003 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5004 if (error != 0)
5006 printf("%s: could not load Rx desc DMA map\n",
5007 device_get_nameunit(sc->dev));
5008 goto fail;
5011 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5012 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5013 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
5014 &ring->data_dma_tag);
5015 if (error != 0)
5017 printf("%s: could not create Rx data DMA tag\n",
5018 device_get_nameunit(sc->dev));
5019 goto fail;
5022 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5024 desc = &ring->desc[i];
5025 data = &ring->data[i];
5027 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5028 if (error != 0)
5030 printf("%s: could not create Rx data DMA map\n",
5031 device_get_nameunit(sc->dev));
5032 goto fail;
5035 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5036 if (data->m == NULL)
5038 printf("%s: could not allocate Rx mbuf\n",
5039 device_get_nameunit(sc->dev));
5040 error = ENOMEM;
5041 goto fail;
5044 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5046 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5047 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5048 if (error != 0)
5050 printf("%s: could not load Rx mbuf DMA map\n",
5051 device_get_nameunit(sc->dev));
5052 goto fail;
5055 KASSERT(nsegs == 1, ("%s: too many DMA segments",
5056 device_get_name(sc->dev)));
5058 desc->sdp0 = htole32(segs[0].ds_addr);
5059 desc->sdl0 = htole16(MJUMPAGESIZE);
5062 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5063 BUS_DMASYNC_PREWRITE);
5065 return 0;
5067 fail:
5069 rt2860_free_rx_ring(sc, ring);
5071 return error;
5075 * rt2860_reset_rx_ring
5077 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5078 struct rt2860_softc_rx_ring *ring)
5080 struct rt2860_rxdesc *desc;
5081 int i;
5083 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5085 desc = &ring->desc[i];
5087 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5090 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5091 BUS_DMASYNC_PREWRITE);
5093 ring->cur = 0;
5097 * rt2860_free_rx_ring
5099 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5100 struct rt2860_softc_rx_ring *ring)
5102 struct rt2860_softc_rx_data *data;
5103 int i;
5105 if (ring->desc != NULL)
5107 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5108 BUS_DMASYNC_POSTWRITE);
5109 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5110 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5111 ring->desc_dma_map);
5114 if (ring->desc_dma_tag != NULL)
5115 bus_dma_tag_destroy(ring->desc_dma_tag);
5117 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5119 data = &ring->data[i];
5121 if (data->m != NULL)
5123 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5124 BUS_DMASYNC_POSTREAD);
5125 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5126 m_freem(data->m);
5129 if (data->dma_map != NULL)
5130 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5133 if (ring->data_dma_tag != NULL)
5134 bus_dma_tag_destroy(ring->data_dma_tag);
5138 * rt2860_alloc_tx_ring
5140 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5141 struct rt2860_softc_tx_ring *ring, int qid)
5143 struct rt2860_softc_tx_data *data;
5144 int error, i;
5146 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5147 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5148 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5149 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5150 0, NULL, NULL, &ring->desc_dma_tag);
5151 if (error != 0)
5153 printf("%s: could not create Tx desc DMA tag\n",
5154 device_get_nameunit(sc->dev));
5155 goto fail;
5158 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5159 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5160 if (error != 0)
5162 printf("%s: could not allocate Tx desc DMA memory\n",
5163 device_get_nameunit(sc->dev));
5164 goto fail;
5167 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5168 ring->desc,
5169 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5170 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5171 if (error != 0)
5173 printf("%s: could not load Tx desc DMA map\n",
5174 device_get_nameunit(sc->dev));
5175 goto fail;
5178 ring->desc_queued = 0;
5179 ring->desc_cur = 0;
5180 ring->desc_next = 0;
5182 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5183 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5184 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5185 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5186 0, NULL, NULL, &ring->seg0_dma_tag);
5187 if (error != 0)
5189 printf("%s: could not create Tx seg0 DMA tag\n",
5190 device_get_nameunit(sc->dev));
5191 goto fail;
5194 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5195 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5196 if (error != 0)
5198 printf("%s: could not allocate Tx seg0 DMA memory\n",
5199 device_get_nameunit(sc->dev));
5200 goto fail;
5203 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5204 ring->seg0,
5205 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5206 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5207 if (error != 0)
5209 printf("%s: could not load Tx seg0 DMA map\n",
5210 device_get_nameunit(sc->dev));
5211 goto fail;
5214 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5215 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5216 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5217 &ring->data_dma_tag);
5218 if (error != 0)
5220 printf("%s: could not create Tx data DMA tag\n",
5221 device_get_nameunit(sc->dev));
5222 goto fail;
5225 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5227 data = &ring->data[i];
5229 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5230 if (error != 0)
5232 printf("%s: could not create Tx data DMA map\n",
5233 device_get_nameunit(sc->dev));
5234 goto fail;
5238 ring->data_queued = 0;
5239 ring->data_cur = 0;
5240 ring->data_next = 0;
5242 ring->qid = qid;
5244 return 0;
5246 fail:
5248 rt2860_free_tx_ring(sc, ring);
5250 return error;
5254 * rt2860_reset_tx_ring
5256 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5257 struct rt2860_softc_tx_ring *ring)
5259 struct rt2860_softc_tx_data *data;
5260 struct rt2860_txdesc *desc;
5261 int i;
5263 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5265 desc = &ring->desc[i];
5267 desc->sdl0 = 0;
5268 desc->sdl1 = 0;
5271 ring->desc_queued = 0;
5272 ring->desc_cur = 0;
5273 ring->desc_next = 0;
5275 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5276 BUS_DMASYNC_PREWRITE);
5278 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5279 BUS_DMASYNC_PREWRITE);
5281 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5283 data = &ring->data[i];
5285 if (data->m != NULL)
5287 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5288 BUS_DMASYNC_POSTWRITE);
5289 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5290 m_freem(data->m);
5291 data->m = NULL;
5294 if (data->ni != NULL)
5296 ieee80211_free_node(data->ni);
5297 data->ni = NULL;
5301 ring->data_queued = 0;
5302 ring->data_cur = 0;
5303 ring->data_next = 0;
5307 * rt2860_free_tx_ring
5309 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5310 struct rt2860_softc_tx_ring *ring)
5312 struct rt2860_softc_tx_data *data;
5313 int i;
5315 if (ring->desc != NULL)
5317 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5318 BUS_DMASYNC_POSTWRITE);
5319 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5320 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5321 ring->desc_dma_map);
5324 if (ring->desc_dma_tag != NULL)
5325 bus_dma_tag_destroy(ring->desc_dma_tag);
5327 if (ring->seg0 != NULL)
5329 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5330 BUS_DMASYNC_POSTWRITE);
5331 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5332 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5333 ring->seg0_dma_map);
5336 if (ring->seg0_dma_tag != NULL)
5337 bus_dma_tag_destroy(ring->seg0_dma_tag);
5339 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5341 data = &ring->data[i];
5343 if (data->m != NULL)
5345 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5346 BUS_DMASYNC_POSTWRITE);
5347 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5348 m_freem(data->m);
5351 if (data->ni != NULL)
5352 ieee80211_free_node(data->ni);
5354 if (data->dma_map != NULL)
5355 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5358 if (ring->data_dma_tag != NULL)
5359 bus_dma_tag_destroy(ring->data_dma_tag);
5363 * rt2860_dma_map_addr
5365 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5366 int nseg, int error)
5368 if (error != 0)
5369 return;
5371 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5373 *(bus_addr_t *) arg = segs[0].ds_addr;
5377 * rt2860_sysctl_attach
5379 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5381 struct sysctl_ctx_list *ctx;
5382 struct sysctl_oid *tree;
5383 struct sysctl_oid *stats;
5385 ctx = device_get_sysctl_ctx(sc->dev);
5386 tree = device_get_sysctl_tree(sc->dev);
5388 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5389 "stats", CTLFLAG_RD, 0, "statistic");
5391 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5392 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5393 "all interrupts");
5395 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5396 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5397 "Tx coherent interrupts");
5399 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5400 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5401 "Rx coherent interrupts");
5403 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5404 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5405 "Tx/Rx coherent interrupts");
5407 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5408 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5409 "FIFO statistic full interrupts");
5411 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5412 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5413 "Rx interrupts");
5415 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5416 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5417 "Rx delay interrupts");
5419 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5420 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5421 "Tx MGMT interrupts");
5423 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5424 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5425 "Tx HCCA interrupts");
5427 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5428 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5429 "Tx AC3 interrupts");
5431 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5432 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5433 "Tx AC2 interrupts");
5435 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5436 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5437 "Tx AC1 interrupts");
5439 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5440 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5441 "Tx AC0 interrupts");
5443 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5444 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
5445 "Tx delay interrupts");
5447 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5448 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
5449 "Pre-TBTT interrupts");
5451 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5452 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
5453 "TBTT interrupts");
5455 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5456 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
5457 "MCU command interrupts");
5459 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5460 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
5461 "auto wakeup interrupts");
5463 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5464 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
5465 "GP timer interrupts");
5467 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5468 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
5469 "Tx MGMT descriptors queued");
5471 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5472 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
5473 "Tx MGMT data queued");
5475 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5476 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
5477 "Tx HCCA descriptors queued");
5479 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5480 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
5481 "Tx HCCA data queued");
5483 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5484 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
5485 "Tx AC3 descriptors queued");
5487 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5488 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
5489 "Tx AC3 data queued");
5491 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5492 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
5493 "Tx AC2 descriptors queued");
5495 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5496 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
5497 "Tx AC2 data queued");
5499 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5500 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
5501 "Tx AC1 descriptors queued");
5503 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5504 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
5505 "Tx AC1 data queued");
5507 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5508 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
5509 "Tx AC0 descriptors queued");
5511 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5512 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
5513 "Tx AC0 data queued");
5515 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5516 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5517 "Tx watchdog timeouts");
5519 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5520 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
5521 "Tx defragmented packets");
5523 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5524 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
5525 "no Tx descriptors available");
5527 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5528 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
5529 "Rx mbuf allocation errors");
5531 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5532 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
5533 "Rx mbuf DMA mapping errors");
5535 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5536 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5537 "Tx queue 0 not empty");
5539 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5540 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5541 "Tx queue 1 not empty");
5543 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5544 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
5545 "Rx packets");
5547 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5548 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
5549 "Rx CRC errors");
5551 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5552 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
5553 "Rx PHY errors");
5555 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5556 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
5557 "Rx false CCAs");
5559 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5560 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
5561 "Rx PLCP errors");
5563 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5564 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
5565 "Rx duplicate packets");
5567 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5568 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
5569 "Rx FIFO overflows");
5572 static device_method_t rt2860_dev_methods[] =
5574 DEVMETHOD(device_probe, rt2860_probe),
5575 DEVMETHOD(device_attach, rt2860_attach),
5576 DEVMETHOD(device_detach, rt2860_detach),
5577 DEVMETHOD(device_shutdown, rt2860_shutdown),
5578 DEVMETHOD(device_suspend, rt2860_suspend),
5579 DEVMETHOD(device_resume, rt2860_resume),
5580 { 0, 0 }
5583 static driver_t rt2860_driver =
5585 "rt2860",
5586 rt2860_dev_methods,
5587 sizeof(struct rt2860_softc)
5590 static devclass_t rt2860_dev_class;
5592 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
5594 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
5595 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);