Bug fixed in AMRR
[ralink_drivers/rt2860_fbsd72.git] / rt2860.c
blob5585874d1f61f2d1f358b4c9da9131ad648b8cc3
2 /*-
3 * Copyright (c) 2009-2010 Alexander Egorenkov <egorenar@gmail.com>
4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "rt2860_softc.h"
20 #include "rt2860_reg.h"
21 #include "rt2860_eeprom.h"
22 #include "rt2860_ucode.h"
23 #include "rt2860_txwi.h"
24 #include "rt2860_rxwi.h"
25 #include "rt2860_io.h"
26 #include "rt2860_read_eeprom.h"
27 #include "rt2860_led.h"
28 #include "rt2860_rf.h"
29 #include "rt2860_debug.h"
32 * Defines and macros
35 #define PCI_VENDOR_RALINK 0x1814
36 #define PCI_PRODUCT_RALINK_RT2860_PCI 0x0601
37 #define PCI_PRODUCT_RALINK_RT2860_PCIe 0x0681
38 #define PCI_PRODUCT_RALINK_RT2760_PCI 0x0701
39 #define PCI_PRODUCT_RALINK_RT2790_PCIe 0x0781
41 #define RT2860_MAX_AGG_SIZE 3840
43 #define RT2860_TX_DATA_SEG0_SIZE \
44 (sizeof(struct rt2860_txwi) + sizeof(struct ieee80211_qosframe_addr4))
46 #define RT2860_NOISE_FLOOR -95
48 #define RT2860_AID2WCID(aid) ((aid) & 0xff)
50 #define RT2860_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
52 #define RT2860_ACK_SIZE 14
54 #define IEEE80211_HAS_ADDR4(wh) \
55 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
57 #define RT2860_MS(_v, _f) (((_v) & _f) >> _f##_S)
58 #define RT2860_SM(_v, _f) (((_v) << _f##_S) & _f)
60 #define RT2860_TX_WATCHDOG_TIMEOUT 5
63 * Data structures and types
66 struct rt2860_pci_ident
68 uint16_t vendor;
69 uint16_t device;
70 const char *name;
74 * Static function prototypes
77 static int rt2860_probe(device_t dev);
79 static int rt2860_attach(device_t dev);
81 static int rt2860_detach(device_t dev);
83 static int rt2860_shutdown(device_t dev);
85 static int rt2860_suspend(device_t dev);
87 static int rt2860_resume(device_t dev);
89 static void rt2860_init_channels(struct rt2860_softc *sc);
91 static void rt2860_init_channels_ht40(struct rt2860_softc *sc);
93 static void rt2860_init_locked(void *priv);
95 static void rt2860_init(void *priv);
97 static int rt2860_init_bbp(struct rt2860_softc *sc);
99 static void rt2860_stop_locked(void *priv);
101 static void rt2860_stop(void *priv);
103 static void rt2860_start(struct ifnet *ifp);
105 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
107 static int rt2860_reset(struct ifnet *ifp);
109 static int rt2860_newstate(struct ieee80211com *ic,
110 enum ieee80211_state nstate, int arg);
112 static void rt2860_scan_start(struct ieee80211com *ic);
114 static void rt2860_scan_end(struct ieee80211com *ic);
116 static void rt2860_set_channel(struct ieee80211com *ic);
118 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew);
120 static void rt2860_updateslot(struct ifnet *ifp);
122 static int rt2860_wme_update(struct ieee80211com *ic);
124 static void rt2860_update_beacon(struct ieee80211com *ic, int what);
126 static void rt2860_key_update_begin(struct ieee80211com *ic);
128 static void rt2860_key_update_end(struct ieee80211com *ic);
130 static int rt2860_key_set(struct ieee80211com *ic,
131 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN]);
133 static int rt2860_key_delete(struct ieee80211com *ic,
134 const struct ieee80211_key *k);
136 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
137 const struct ieee80211_bpf_params *params);
139 static int rt2860_media_change(struct ifnet *ifp);
141 static void rt2860_recv_action(struct ieee80211_node *ni,
142 const uint8_t *frm, const uint8_t *efrm);
144 static int rt2860_send_action(struct ieee80211_node *ni,
145 int category, int action, uint16_t args[4]);
147 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
149 static void rt2860_periodic(void *arg);
151 static void rt2860_tx_watchdog(void *arg);
153 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
154 const uint8_t *bssid);
156 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
157 const uint8_t *addr);
159 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc);
161 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc);
163 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc);
165 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc);
167 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc);
169 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc);
171 static void rt2860_asic_update_txpower(struct rt2860_softc *sc);
173 static void rt2860_asic_update_promisc(struct rt2860_softc *sc);
175 static void rt2860_asic_updateprot(struct rt2860_softc *sc);
177 static void rt2860_asic_updateslot(struct rt2860_softc *sc);
179 static void rt2860_asic_wme_update(struct rt2860_softc *sc);
181 static int rt2860_asic_update_beacon(struct rt2860_softc *sc);
183 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc);
185 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi);
187 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
188 const struct rt2860_rxwi *rxwi);
190 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
191 uint8_t rssi, uint8_t rxpath);
193 static uint8_t rt2860_rate2mcs(uint8_t rate);
195 static int rt2860_ackrate(struct ieee80211com *ic, int rate);
197 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags);
199 static int rt2860_tx_frame(struct rt2860_softc *sc,
200 struct mbuf *m, struct ieee80211_node *ni, int qid);
202 static int rt2860_tx_raw(struct rt2860_softc *sc,
203 struct mbuf *m, struct ieee80211_node *ni,
204 const struct ieee80211_bpf_params *params);
206 static void rt2860_intr(void *arg);
208 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc);
210 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc);
212 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc);
214 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc);
216 static void rt2860_rx_intr(struct rt2860_softc *sc);
218 static void rt2860_rx_delay_intr(struct rt2860_softc *sc);
220 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid);
222 static void rt2860_tx_delay_intr(struct rt2860_softc *sc);
224 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc);
226 static void rt2860_tbtt_intr(struct rt2860_softc *sc);
228 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc);
230 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc);
232 static void rt2860_gp_timer_intr(struct rt2860_softc *sc);
234 static void rt2860_rx_done_task(void *context, int pending);
236 static void rt2860_tx_done_task(void *context, int pending);
238 static void rt2860_fifo_sta_full_task(void *context, int pending);
240 static void rt2860_periodic_task(void *context, int pending);
242 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit);
244 static void rt2860_tx_eof(struct rt2860_softc *sc,
245 struct rt2860_softc_tx_ring *ring);
247 static void rt2860_update_stats(struct rt2860_softc *sc);
249 static void rt2860_watchdog(struct rt2860_softc *sc);
251 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc);
253 static void rt2860_update_raw_counters(struct rt2860_softc *sc);
255 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask);
257 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask);
259 static int rt2860_txrx_enable(struct rt2860_softc *sc);
261 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
262 struct rt2860_softc_rx_ring *ring);
264 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
265 struct rt2860_softc_rx_ring *ring);
267 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
268 struct rt2860_softc_rx_ring *ring);
270 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
271 struct rt2860_softc_tx_ring *ring, int qid);
273 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
274 struct rt2860_softc_tx_ring *ring);
276 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
277 struct rt2860_softc_tx_ring *ring);
279 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
280 int nseg, int error);
282 static void rt2860_sysctl_attach(struct rt2860_softc *sc);
285 * Static variables
288 static const struct rt2860_pci_ident rt2860_pci_ids[] =
290 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCI, "Ralink RT2860 PCI" },
291 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCIe, "Ralink RT2860 PCIe" },
292 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760_PCI, "Ralink RT2760 PCI" },
293 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790_PCIe, "Ralink RT2790 PCIe" },
294 { 0, 0, NULL }
297 static const struct
299 uint32_t reg;
300 uint32_t val;
301 } rt2860_def_mac[] =
303 { RT2860_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
304 { RT2860_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
305 { RT2860_REG_LEGACY_BASIC_RATE, 0x0000013f },
306 { RT2860_REG_HT_BASIC_RATE, 0x00008003 },
307 { RT2860_REG_SYS_CTRL, 0x00000000 },
308 { RT2860_REG_RX_FILTER_CFG, 0x00017f97 },
309 { RT2860_REG_BKOFF_SLOT_CFG, 0x00000209 },
310 { RT2860_REG_TX_SW_CFG0, 0x00000000 },
311 { RT2860_REG_TX_SW_CFG1, 0x00080606 },
312 { RT2860_REG_TX_LINK_CFG, 0x00001020 },
313 { RT2860_REG_TX_TIMEOUT_CFG, 0x000a2090 },
314 { RT2860_REG_MAX_LEN_CFG, (1 << 12) | RT2860_MAX_AGG_SIZE },
315 { RT2860_REG_LED_CFG, 0x7f031e46 },
316 { RT2860_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
317 { RT2860_REG_TX_RTY_CFG, 0x47d01f0f },
318 { RT2860_REG_AUTO_RSP_CFG, 0x00000013 },
319 { RT2860_REG_TX_CCK_PROT_CFG, 0x05740003 },
320 { RT2860_REG_TX_OFDM_PROT_CFG, 0x05740003 },
321 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f44084 },
322 { RT2860_REG_SCHDMA_WPDMA_GLO_CFG, 0x00000030 },
323 { RT2860_REG_TX_GF20_PROT_CFG, 0x01744004 },
324 { RT2860_REG_TX_GF40_PROT_CFG, 0x03f44084 },
325 { RT2860_REG_TX_MM20_PROT_CFG, 0x01744004 },
326 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f54084 },
327 { RT2860_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
328 { RT2860_REG_TX_RTS_CFG, 0x00092b20 },
329 { RT2860_REG_TX_EXP_ACK_TIME, 0x002400ca },
330 { RT2860_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
331 { RT2860_REG_XIFS_TIME_CFG, 0x33a41010 },
332 { RT2860_REG_PWR_PIN_CFG, 0x00000003 },
333 { RT2860_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
334 { RT2860_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
335 { RT2860_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
338 #define RT2860_DEF_MAC_SIZE (sizeof(rt2860_def_mac) / sizeof(rt2860_def_mac[0]))
340 static const struct
342 uint8_t reg;
343 uint8_t val;
344 } rt2860_def_bbp[] =
346 { 65, 0x2c },
347 { 66, 0x38 },
348 { 69, 0x12 },
349 { 70, 0x0a },
350 { 73, 0x10 },
351 { 81, 0x37 },
352 { 82, 0x62 },
353 { 83, 0x6a },
354 { 84, 0x99 },
355 { 86, 0x00 },
356 { 91, 0x04 },
357 { 92, 0x00 },
358 { 103, 0x00 },
359 { 105, 0x05 },
360 { 106, 0x35 },
363 #define RT2860_DEF_BBP_SIZE (sizeof(rt2860_def_bbp) / sizeof(rt2860_def_bbp[0]))
365 SYSCTL_DECL(_hw_rt2860);
367 #ifdef RT2860_DEBUG
368 static int rt2860_debug = 0;
369 SYSCTL_INT(_hw_rt2860, OID_AUTO, debug, CTLFLAG_RW, &rt2860_debug, 0, "rt2860 debug level");
370 TUNABLE_INT("hw.rt2860.debug", &rt2860_debug);
371 #endif
374 * rt2860_probe
376 static int rt2860_probe(device_t dev)
378 const struct rt2860_pci_ident *ident;
380 for (ident = rt2860_pci_ids; ident->name != NULL; ident++)
382 if (pci_get_vendor(dev) == ident->vendor &&
383 pci_get_device(dev) == ident->device)
385 device_set_desc(dev, ident->name);
386 return 0;
390 return ENXIO;
394 * rt2860_attach
396 static int rt2860_attach(device_t dev)
398 struct rt2860_softc *sc;
399 struct ifnet *ifp;
400 struct ieee80211com *ic;
401 int error, ntries, i;
403 sc = device_get_softc(dev);
405 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
407 printf("%s: chip is in D%d power mode, setting to D0\n",
408 device_get_nameunit(dev), pci_get_powerstate(dev));
409 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
412 /* enable bus-mastering */
414 pci_enable_busmaster(dev);
416 sc->dev = dev;
418 mtx_init(&sc->lock, device_get_nameunit(dev),
419 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
421 sc->mem_rid = PCIR_BAR(0);
422 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
423 &sc->mem_rid, RF_ACTIVE);
424 if (sc->mem == NULL)
426 printf("%s: could not allocate memory resource\n",
427 device_get_nameunit(dev));
428 error = ENXIO;
429 goto fail;
432 sc->bst = rman_get_bustag(sc->mem);
433 sc->bsh = rman_get_bushandle(sc->mem);
435 sc->irq_rid = 0;
436 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
437 &sc->irq_rid, RF_ACTIVE | RF_SHAREABLE);
438 if (sc->irq == NULL)
440 printf("%s: could not allocate interrupt resource\n",
441 device_get_nameunit(dev));
442 error = ENXIO;
443 goto fail;
446 #ifdef RT2860_DEBUG
447 sc->debug = rt2860_debug;
449 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
450 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
451 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2860 debug level");
452 #endif
454 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
455 "%s: attaching\n",
456 device_get_nameunit(sc->dev));
458 /* wait for NIC to initialize */
460 for (ntries = 0; ntries < 100; ntries++)
462 sc->mac_rev = rt2860_io_mac_read(sc, RT2860_REG_MAC_CSR0);
463 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
464 break;
466 DELAY(10);
469 if (ntries == 100)
471 printf("%s: timeout waiting for NIC to initialize\n",
472 device_get_nameunit(dev));
473 error = EIO;
474 goto fail;
477 rt2860_read_eeprom(sc);
479 printf("%s: MAC/BBP RT2860 (rev 0x%08x), RF %s\n",
480 device_get_nameunit(sc->dev), sc->mac_rev,
481 rt2860_rf_name(sc->rf_rev));
483 /* allocate Tx and Rx rings */
485 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
487 error = rt2860_alloc_tx_ring(sc, &sc->tx_ring[i], i);
488 if (error != 0)
490 printf("%s: could not allocate Tx ring #%d\n",
491 device_get_nameunit(sc->dev), i);
492 goto fail;
496 sc->tx_ring_mgtqid = 5;
498 error = rt2860_alloc_rx_ring(sc, &sc->rx_ring);
499 if (error != 0)
501 printf("%s: could not allocate Rx ring\n",
502 device_get_nameunit(sc->dev));
503 goto fail;
506 callout_init(&sc->periodic_ch, 0);
507 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
509 ifp = sc->ifp = if_alloc(IFT_ETHER);
510 if (ifp == NULL)
512 printf("%s: could not if_alloc()\n",
513 device_get_nameunit(sc->dev));
514 error = ENOMEM;
515 goto fail;
518 ifp->if_softc = sc;
520 if_initname(ifp, "rt2860", device_get_unit(sc->dev));
522 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
524 ifp->if_init = rt2860_init;
525 ifp->if_ioctl = rt2860_ioctl;
526 ifp->if_start = rt2860_start;
528 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
529 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
530 IFQ_SET_READY(&ifp->if_snd);
532 ic = &sc->ic;
534 ic->ic_ifp = ifp;
536 ic->ic_phytype = IEEE80211_T_HT;
537 ic->ic_opmode = IEEE80211_M_STA;
538 ic->ic_state = IEEE80211_S_INIT;
540 ic->ic_caps = IEEE80211_C_MONITOR |
541 IEEE80211_C_IBSS |
542 IEEE80211_C_AHDEMO |
543 IEEE80211_C_HOSTAP |
544 IEEE80211_C_WDS |
545 /* IEEE80211_C_BGSCAN | */
546 IEEE80211_C_TXPMGT |
547 IEEE80211_C_SHPREAMBLE |
548 IEEE80211_C_SHSLOT |
549 IEEE80211_C_TXFRAG |
550 IEEE80211_C_BURST |
551 IEEE80211_C_WME |
552 IEEE80211_C_WEP |
553 IEEE80211_C_TKIP |
554 IEEE80211_C_AES_CCM |
555 IEEE80211_C_WPA;
557 ic->ic_htcaps = IEEE80211_HTC_HT |
558 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
559 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
560 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
561 IEEE80211_HTCAP_GREENFIELD | /* 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 if (sc->ntxpath == 1)
1403 tmp &= ~((1 << 4) | (1 << 3));
1405 rt2860_io_bbp_write(sc, 1, tmp);
1407 tmp = rt2860_io_bbp_read(sc, 4);
1409 tmp &= ~0x18;
1411 rt2860_io_bbp_write(sc, 4, tmp);
1413 /* set current channel */
1415 rt2860_rf_set_chan(sc, ic->ic_curchan);
1417 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1418 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1419 (48 << 16) | 96);
1421 if ((sc->mac_rev & 0xffff) != 0x0101)
1422 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1424 /* clear pending interrupts */
1426 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1428 /* enable interrupts */
1430 tmp = RT2860_REG_INT_TX_COHERENT |
1431 RT2860_REG_INT_RX_COHERENT |
1432 RT2860_REG_INT_GP_TIMER |
1433 RT2860_REG_INT_AUTO_WAKEUP |
1434 RT2860_REG_INT_FIFO_STA_FULL |
1436 RT2860_REG_INT_PRE_TBTT |
1437 RT2860_REG_INT_TBTT |
1439 RT2860_REG_INT_TXRX_COHERENT |
1440 RT2860_REG_INT_MCU_CMD |
1441 RT2860_REG_INT_TX_MGMT_DONE |
1442 RT2860_REG_INT_TX_HCCA_DONE |
1443 RT2860_REG_INT_TX_AC3_DONE |
1444 RT2860_REG_INT_TX_AC2_DONE |
1445 RT2860_REG_INT_TX_AC1_DONE |
1446 RT2860_REG_INT_TX_AC0_DONE |
1447 RT2860_REG_INT_RX_DONE;
1449 sc->intr_enable_mask = tmp;
1451 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1453 if (rt2860_txrx_enable(sc) != 0)
1454 goto fail;
1456 /* clear garbage interrupts */
1458 tmp = rt2860_io_mac_read(sc, 0x1300);
1460 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1461 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1463 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1465 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1466 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1468 else
1470 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1473 sc->periodic_round = 0;
1475 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1477 return;
1479 fail:
1481 rt2860_stop_locked(sc);
1485 * rt2860_init
1487 static void rt2860_init(void *priv)
1489 struct rt2860_softc *sc;
1491 sc = priv;
1493 RT2860_SOFTC_LOCK(sc);
1495 rt2860_init_locked(sc);
1497 RT2860_SOFTC_UNLOCK(sc);
1501 * rt2860_init_bbp
1503 static int rt2860_init_bbp(struct rt2860_softc *sc)
1505 int ntries, i;
1506 uint8_t tmp;
1508 for (ntries = 0; ntries < 20; ntries++)
1510 tmp = rt2860_io_bbp_read(sc, 0);
1511 if (tmp != 0x00 && tmp != 0xff)
1512 break;
1515 if (tmp == 0x00 || tmp == 0xff)
1517 printf("%s: timeout waiting for BBP to wakeup\n",
1518 device_get_nameunit(sc->dev));
1519 return ETIMEDOUT;
1522 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1523 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1524 rt2860_def_bbp[i].val);
1526 if ((sc->mac_rev & 0xffff) != 0x0101)
1527 rt2860_io_bbp_write(sc, 84, 0x19);
1529 return 0;
1533 * rt2860_stop_locked
1535 static void rt2860_stop_locked(void *priv)
1537 struct rt2860_softc *sc;
1538 struct ieee80211com *ic;
1539 struct ifnet *ifp;
1540 uint32_t tmp;
1542 sc = priv;
1543 ic = &sc->ic;
1544 ifp = ic->ic_ifp;
1546 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1547 "%s: stopping\n",
1548 device_get_nameunit(sc->dev));
1550 RT2860_SOFTC_ASSERT_LOCKED(sc);
1552 sc->tx_timer = 0;
1554 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1555 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1557 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1559 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1561 callout_stop(&sc->periodic_ch);
1562 callout_stop(&sc->tx_watchdog_ch);
1564 RT2860_SOFTC_UNLOCK(sc);
1566 taskqueue_block(sc->taskqueue);
1568 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1569 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1570 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1571 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1573 RT2860_SOFTC_LOCK(sc);
1575 /* clear key tables */
1577 rt2860_asic_clear_keytables(sc);
1579 /* disable interrupts */
1581 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1583 /* disable Tx/Rx */
1585 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1587 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1589 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1591 /* reset adapter */
1593 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1594 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1595 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1599 * rt2860_stop
1601 static void rt2860_stop(void *priv)
1603 struct rt2860_softc *sc;
1605 sc = priv;
1607 RT2860_SOFTC_LOCK(sc);
1609 rt2860_stop_locked(sc);
1611 RT2860_SOFTC_UNLOCK(sc);
1615 * rt2860_start
1617 static void rt2860_start(struct ifnet *ifp)
1619 struct rt2860_softc *sc;
1620 struct ieee80211com *ic;
1621 struct ieee80211_node *ni;
1622 struct ether_header *eh;
1623 struct mbuf *m;
1624 int qid;
1626 sc = ifp->if_softc;
1627 ic = &sc->ic;
1629 RT2860_SOFTC_LOCK(sc);
1631 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1633 RT2860_SOFTC_UNLOCK(sc);
1634 return;
1637 for (;;)
1639 IF_POLL(&ic->ic_mgtq, m);
1640 if (m != NULL)
1642 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1644 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1645 "%s: if_start: Tx ring with qid=%d is full\n",
1646 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
1648 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1649 break;
1652 IF_DEQUEUE(&ic->ic_mgtq, m);
1654 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1655 m->m_pkthdr.rcvif = NULL;
1657 if (bpf_peers_present(ic->ic_rawbpf))
1658 bpf_mtap(ic->ic_rawbpf, m);
1660 if (rt2860_tx_frame(sc, m, ni, sc->tx_ring_mgtqid) != 0)
1661 break;
1663 rt2860_drain_fifo_stats(sc);
1665 else
1667 if (ic->ic_state != IEEE80211_S_RUN)
1668 break;
1670 IF_POLL(&ifp->if_snd, m);
1671 if (m == NULL)
1672 break;
1674 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1676 if (ic->ic_flags & IEEE80211_F_SCAN)
1677 ieee80211_cancel_scan(ic);
1679 if (m->m_len < sizeof(struct ether_header) &&
1680 !(m = m_pullup(m, sizeof (struct ether_header))))
1681 continue;
1683 eh = mtod(m, struct ether_header *);
1685 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1686 if (ni == NULL)
1688 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1689 "%s: if_start: could not find Tx node\n",
1690 device_get_nameunit(sc->dev));
1692 m_freem(m);
1693 continue;
1696 ieee80211_classify(ic, m, ni);
1698 qid = M_WME_GETAC(m);
1700 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1702 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1703 "%s: if_start: Tx ring with qid=%d is full\n",
1704 device_get_nameunit(sc->dev), qid);
1706 m_freem(m);
1707 ieee80211_free_node(ni);
1708 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1709 ifp->if_oerrors++;
1710 break;
1713 BPF_MTAP(ifp, m);
1715 m = ieee80211_encap(ic, m, ni);
1716 if (m == NULL)
1718 ieee80211_free_node(ni);
1719 ifp->if_oerrors++;
1720 continue;
1723 if (bpf_peers_present(ic->ic_rawbpf))
1724 bpf_mtap(ic->ic_rawbpf, m);
1726 if (rt2860_tx_frame(sc, m, ni, qid) != 0)
1728 ieee80211_free_node(ni);
1729 ifp->if_oerrors++;
1730 break;
1733 rt2860_drain_fifo_stats(sc);
1736 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1738 ic->ic_lastdata = ticks;
1740 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1743 RT2860_SOFTC_UNLOCK(sc);
1747 * rt2860_ioctl
1749 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1751 struct rt2860_softc *sc;
1752 struct ieee80211com *ic;
1753 int error;
1755 sc = ifp->if_softc;
1756 ic = &sc->ic;
1758 error = 0;
1760 switch (cmd)
1762 case SIOCSIFFLAGS:
1763 RT2860_SOFTC_LOCK(sc);
1765 if (ifp->if_flags & IFF_UP)
1767 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1769 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1770 rt2860_asic_update_promisc(sc);
1772 else
1774 rt2860_init_locked(sc);
1777 else
1779 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1780 rt2860_stop_locked(sc);
1783 sc->if_flags = ifp->if_flags;
1785 RT2860_SOFTC_UNLOCK(sc);
1786 break;
1788 default:
1789 error = ieee80211_ioctl(ic, cmd, data);
1792 if (error == ENETRESET)
1794 RT2860_SOFTC_LOCK(sc);
1796 if ((ifp->if_flags & IFF_UP) &&
1797 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1798 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1800 rt2860_stop_locked(sc);
1801 rt2860_init_locked(sc);
1804 RT2860_SOFTC_UNLOCK(sc);
1806 error = 0;
1809 return error;
1813 * rt2860_reset
1815 static int rt2860_reset(struct ifnet *ifp)
1817 struct rt2860_softc *sc;
1818 struct ieee80211com *ic;
1820 sc = ifp->if_softc;
1821 ic = &sc->ic;
1823 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1824 return ENETRESET;
1826 rt2860_rf_set_chan(sc, ic->ic_curchan);
1828 return 0;
1832 * rt2860_newstate
1834 static int rt2860_newstate(struct ieee80211com *ic,
1835 enum ieee80211_state nstate, int arg)
1837 struct rt2860_softc *sc;
1838 struct ifnet *ifp;
1839 struct ieee80211_node *ni;
1840 int error;
1842 ifp = ic->ic_ifp;
1843 sc = ifp->if_softc;
1845 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1846 "%s: newstate: %s -> %s\n",
1847 device_get_nameunit(sc->dev),
1848 ieee80211_state_name[ic->ic_state], ieee80211_state_name[nstate]);
1850 error = sc->newstate(ic, nstate, arg);
1851 if (error != 0)
1852 return error;
1854 RT2860_SOFTC_LOCK(sc);
1856 /* turn link LED off */
1858 if (nstate != IEEE80211_S_RUN)
1859 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1861 switch (nstate)
1863 case IEEE80211_S_INIT:
1864 rt2860_asic_disable_tsf_sync(sc);
1865 break;
1867 case IEEE80211_S_RUN:
1868 ni = ic->ic_bss;
1870 rt2860_rf_set_chan(sc, ni->ni_chan);
1872 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1874 rt2860_asic_enable_mrr(sc);
1875 rt2860_asic_set_txpreamble(sc);
1876 rt2860_asic_set_basicrates(sc);
1877 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1880 if (ic->ic_opmode == IEEE80211_M_STA)
1881 rt2860_newassoc(ni, 1);
1883 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1884 ic->ic_opmode == IEEE80211_M_IBSS)
1886 error = rt2860_asic_update_beacon(sc);
1887 if (error != 0)
1888 break;
1891 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1892 rt2860_asic_enable_tsf_sync(sc);
1894 /* turn link LED on */
1896 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1898 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1899 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1900 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
1902 break;
1904 default:
1905 break;
1908 RT2860_SOFTC_UNLOCK(sc);
1910 return error;
1914 * rt2860_scan_start
1916 static void rt2860_scan_start(struct ieee80211com *ic)
1918 struct rt2860_softc *sc;
1919 struct ifnet *ifp;
1921 ifp = ic->ic_ifp;
1922 sc = ifp->if_softc;
1924 rt2860_asic_disable_tsf_sync(sc);
1928 * rt2860_scan_end
1930 static void rt2860_scan_end(struct ieee80211com *ic)
1932 struct rt2860_softc *sc;
1933 struct ifnet *ifp;
1935 ifp = ic->ic_ifp;
1936 sc = ifp->if_softc;
1938 rt2860_asic_enable_tsf_sync(sc);
1942 * rt2860_set_channel
1944 static void rt2860_set_channel(struct ieee80211com *ic)
1946 struct rt2860_softc *sc;
1947 struct ifnet *ifp;
1949 ifp = ic->ic_ifp;
1950 sc = ifp->if_softc;
1952 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
1953 "%s: set channel: channel=%u, HT%s%s\n",
1954 device_get_nameunit(sc->dev),
1955 ieee80211_chan2ieee(ic, ic->ic_curchan),
1956 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
1957 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
1958 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
1959 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
1961 RT2860_SOFTC_LOCK(sc);
1963 rt2860_rf_set_chan(sc, ic->ic_curchan);
1965 RT2860_SOFTC_UNLOCK(sc);
1969 * rt2860_newassoc
1971 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
1973 struct rt2860_softc *sc;
1974 struct ieee80211com *ic;
1975 struct ifnet *ifp;
1976 uint16_t associd;
1977 uint8_t wcid;
1979 ic = ni->ni_ic;
1980 ifp = ic->ic_ifp;
1981 sc = ifp->if_softc;
1983 associd = (ni != NULL) ? ni->ni_associd : 0;
1984 wcid = RT2860_AID2WCID(associd);
1986 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
1987 "%s: new association: wcid=0x%02x, "
1988 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
1989 device_get_nameunit(sc->dev), wcid,
1990 ether_sprintf(ni->ni_macaddr),
1991 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
1992 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
1993 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
1995 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
1996 ni->ni_macaddr, IEEE80211_ADDR_LEN);
1998 rt2860_amrr_node_init(&sc->amrr, &sc->amrr_node[wcid], ni);
2000 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2001 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2002 device_get_nameunit(sc->dev),
2003 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2004 ni->ni_associd,
2005 (ni->ni_flags & IEEE80211_NODE_HT) ?
2006 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2007 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2008 (ni->ni_flags & IEEE80211_NODE_HT) ?
2009 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2010 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2012 rt2860_asic_updateprot(sc);
2013 rt2860_asic_updateslot(sc);
2014 rt2860_asic_set_txpreamble(sc);
2018 * rt2860_updateslot
2020 static void rt2860_updateslot(struct ifnet *ifp)
2022 struct rt2860_softc *sc;
2024 sc = ifp->if_softc;
2026 rt2860_asic_updateslot(sc);
2030 * rt2860_wme_update
2032 static int rt2860_wme_update(struct ieee80211com *ic)
2034 struct rt2860_softc *sc;
2035 struct ifnet *ifp;
2037 ifp = ic->ic_ifp;
2038 sc = ifp->if_softc;
2040 rt2860_asic_wme_update(sc);
2042 return 0;
2046 * rt2860_update_beacon
2048 static void rt2860_update_beacon(struct ieee80211com *ic, int what)
2050 struct rt2860_softc *sc;
2051 struct ifnet *ifp;
2053 ifp = ic->ic_ifp;
2054 sc = ifp->if_softc;
2056 rt2860_asic_update_beacon(sc);
2060 * rt2860_key_update_begin
2062 static void rt2860_key_update_begin(struct ieee80211com *ic)
2064 struct rt2860_softc *sc;
2065 struct ifnet *ifp;
2067 ifp = ic->ic_ifp;
2068 sc = ifp->if_softc;
2070 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2071 "%s: key update begin\n",
2072 device_get_nameunit(sc->dev));
2074 taskqueue_block(sc->taskqueue);
2076 IF_LOCK(&ifp->if_snd);
2080 * rt2860_key_update_end
2082 static void rt2860_key_update_end(struct ieee80211com *ic)
2084 struct rt2860_softc *sc;
2085 struct ifnet *ifp;
2087 ifp = ic->ic_ifp;
2088 sc = ifp->if_softc;
2090 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2091 "%s: key update end\n",
2092 device_get_nameunit(sc->dev));
2094 IF_UNLOCK(&ifp->if_snd);
2096 taskqueue_unblock(sc->taskqueue);
2100 * rt2860_key_set
2102 static int rt2860_key_set(struct ieee80211com *ic,
2103 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2105 struct rt2860_softc *sc;
2106 struct ifnet *ifp;
2107 struct ieee80211_node *ni;
2108 uint16_t associd, key_base, keymode_base;
2109 uint8_t mode, vapid, wcid, iv[8];
2110 uint32_t tmp;
2112 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2113 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2114 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2115 return EINVAL;
2117 ifp = ic->ic_ifp;
2118 sc = ifp->if_softc;
2120 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2121 ni = ic->ic_bss;
2122 else
2123 ni = ieee80211_find_node(&ic->ic_sta, mac);
2125 associd = (ni != NULL) ? ni->ni_associd : 0;
2127 if ((ic->ic_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2128 ieee80211_free_node(ni);
2130 switch (k->wk_cipher->ic_cipher)
2132 case IEEE80211_CIPHER_WEP:
2133 if(k->wk_keylen < 8)
2134 mode = RT2860_REG_CIPHER_MODE_WEP40;
2135 else
2136 mode = RT2860_REG_CIPHER_MODE_WEP104;
2137 break;
2139 case IEEE80211_CIPHER_TKIP:
2140 mode = RT2860_REG_CIPHER_MODE_TKIP;
2141 break;
2143 case IEEE80211_CIPHER_AES_CCM:
2144 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2145 break;
2147 default:
2148 return EINVAL;
2151 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2152 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2153 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2154 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2156 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2158 /* install pairwise key */
2160 vapid = 0;
2161 wcid = RT2860_AID2WCID(associd);
2162 key_base = RT2860_REG_PKEY(wcid);
2164 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2166 memset(iv, 0, 8);
2168 iv[3] = (k->wk_keyix << 6);
2170 else
2172 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2174 iv[0] = (k->wk_keytsc >> 8);
2175 iv[1] = ((iv[0] | 0x20) & 0x7f);
2176 iv[2] = k->wk_keytsc;
2178 else
2180 /* AES CCMP */
2182 iv[0] = k->wk_keytsc;
2183 iv[1] = k->wk_keytsc >> 8;
2184 iv[2] = 0;
2187 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2188 iv[4] = (k->wk_keytsc >> 16);
2189 iv[5] = (k->wk_keytsc >> 24);
2190 iv[6] = (k->wk_keytsc >> 32);
2191 iv[7] = (k->wk_keytsc >> 40);
2193 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2194 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2195 device_get_nameunit(sc->dev),
2196 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2199 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2201 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2203 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2205 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2207 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2208 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2210 else
2212 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2213 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2216 else
2218 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2221 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2222 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2224 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2227 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2228 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2230 /* install group key */
2232 vapid = 0;
2233 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2234 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2236 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2238 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2240 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2242 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2243 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2245 else
2247 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2248 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2251 else
2253 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2256 tmp = rt2860_io_mac_read(sc, keymode_base);
2258 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2259 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2261 rt2860_io_mac_write(sc, keymode_base, tmp);
2264 return 1;
2268 * rt2860_key_delete
2270 static int rt2860_key_delete(struct ieee80211com *ic,
2271 const struct ieee80211_key *k)
2273 struct rt2860_softc *sc;
2274 struct ieee80211_node *ni;
2275 uint16_t associd;
2276 uint8_t vapid, wcid;
2277 uint32_t tmp;
2279 sc = ic->ic_ifp->if_softc;
2280 ni = ic->ic_bss;
2281 associd = (ni != NULL) ? ni->ni_associd : 0;
2283 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2284 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2285 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2286 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2288 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2290 /* remove pairwise key */
2292 wcid = RT2860_AID2WCID(associd);
2294 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID_ATTR(wcid));
2296 tmp &= ~0xf;
2297 tmp |= (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT);
2299 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2301 else
2303 /* remove group key */
2305 vapid = 0;
2307 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2309 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2310 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2312 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2315 return 1;
2319 * rt2860_raw_xmit
2321 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2322 const struct ieee80211_bpf_params *params)
2324 return 0;
2328 * rt2860_media_change
2330 static int rt2860_media_change(struct ifnet *ifp)
2332 struct rt2860_softc *sc;
2333 int error;
2335 sc = ifp->if_softc;
2337 error = ieee80211_media_change(ifp);
2338 if (error != ENETRESET)
2339 return error;
2341 RT2860_SOFTC_LOCK(sc);
2343 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2345 rt2860_stop_locked(sc);
2346 rt2860_init_locked(sc);
2349 RT2860_SOFTC_UNLOCK(sc);
2351 return 0;
2355 * rt2860_recv_action
2357 static void rt2860_recv_action(struct ieee80211_node *ni,
2358 const uint8_t *frm, const uint8_t *efrm)
2360 struct rt2860_softc *sc;
2361 struct ieee80211com *ic;
2362 struct ifnet *ifp;
2363 const struct ieee80211_action *ia;
2364 uint16_t associd, baparamset;
2365 uint8_t wcid;
2366 int tid;
2367 uint32_t tmp;
2369 ic = ni->ni_ic;
2370 ifp = ic->ic_ifp;
2371 sc = ifp->if_softc;
2373 ia = (const struct ieee80211_action *) frm;
2375 sc->recv_action(ni, frm, efrm);
2377 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2378 return;
2380 associd = (ni != NULL) ? ni->ni_associd : 0;
2381 wcid = RT2860_AID2WCID(associd);
2383 switch (ia->ia_action)
2385 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2386 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2387 baparamset = LE_READ_2(frm + 3);
2388 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2390 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2391 "%s: adding A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2392 device_get_nameunit(sc->dev), associd, tid);
2394 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2396 tmp |= (0x10000 << tid);
2398 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2399 break;
2401 /* IEEE80211_ACTION_BA_DELBA */
2402 case IEEE80211_ACTION_BA_DELBA:
2403 baparamset = LE_READ_2(frm + 2);
2404 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2406 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2407 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2408 device_get_nameunit(sc->dev), associd, tid);
2410 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2412 tmp &= ~(0x10000 << tid);
2414 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2415 break;
2420 * rt2860_send_action
2422 static int rt2860_send_action(struct ieee80211_node *ni,
2423 int category, int action, uint16_t args[4])
2425 struct rt2860_softc *sc;
2426 struct ieee80211com *ic;
2427 struct ifnet *ifp;
2428 uint16_t associd, baparamset;
2429 uint8_t wcid;
2430 int ret, tid;
2431 uint32_t tmp;
2433 ic = ni->ni_ic;
2434 ifp = ic->ic_ifp;
2435 sc = ifp->if_softc;
2437 ret = sc->send_action(ni, category, action, args);
2439 if (category != IEEE80211_ACTION_CAT_BA)
2440 return ret;
2442 associd = (ni != NULL) ? ni->ni_associd : 0;
2443 wcid = RT2860_AID2WCID(associd);
2445 switch (action)
2447 /* IEEE80211_ACTION_BA_DELBA */
2448 case IEEE80211_ACTION_BA_DELBA:
2449 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) |
2450 RT2860_SM(args[1], IEEE80211_DELBAPS_INIT);
2452 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) == IEEE80211_DELBAPS_INIT)
2453 break;
2455 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2457 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2458 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2459 device_get_nameunit(sc->dev), associd, tid);
2461 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2463 tmp &= ~(0x10000 << tid);
2465 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2466 break;
2469 return ret;
2473 * rt2860_amrr_update_iter_func
2475 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2477 struct rt2860_softc *sc;
2478 struct ieee80211com *ic;
2479 uint8_t wcid;
2481 sc = arg;
2482 ic = &sc->ic;
2484 /* only associated stations */
2486 if (ni->ni_associd != 0)
2488 wcid = RT2860_AID2WCID(ni->ni_associd);
2490 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2492 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2493 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2494 device_get_nameunit(sc->dev),
2495 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2496 ni->ni_associd,
2497 (ni->ni_flags & IEEE80211_NODE_HT) ?
2498 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2499 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2500 (ni->ni_flags & IEEE80211_NODE_HT) ?
2501 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2502 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2507 * rt2860_periodic
2509 static void rt2860_periodic(void *arg)
2511 struct rt2860_softc *sc;
2513 sc = arg;
2515 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2516 "%s: periodic\n",
2517 device_get_nameunit(sc->dev));
2519 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2523 * rt2860_tx_watchdog
2525 static void rt2860_tx_watchdog(void *arg)
2527 struct rt2860_softc *sc;
2528 struct ifnet *ifp;
2530 sc = arg;
2531 ifp = sc->ifp;
2533 if (sc->tx_timer == 0)
2534 return;
2536 if (--sc->tx_timer == 0)
2538 printf("%s: Tx watchdog timeout: resetting\n",
2539 device_get_nameunit(sc->dev));
2541 rt2860_stop_locked(sc);
2542 rt2860_init_locked(sc);
2544 ifp->if_oerrors++;
2546 sc->tx_watchdog_timeouts++;
2549 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2553 * rt2860_asic_set_bssid
2555 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2556 const uint8_t *bssid)
2558 uint32_t tmp;
2560 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2561 "%s: set bssid: bssid=%s\n",
2562 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2564 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2566 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2568 tmp = bssid[4] | (bssid[5] << 8);
2570 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2574 * rt2860_asic_set_macaddr
2576 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2577 const uint8_t *addr)
2579 uint32_t tmp;
2581 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2583 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2585 tmp = addr[4] | (addr[5] << 8);
2587 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2591 * rt2860_asic_enable_tsf_sync
2593 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2595 struct ieee80211com *ic;
2596 uint32_t tmp;
2598 ic = &sc->ic;
2600 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2601 "%s: enabling TSF\n",
2602 device_get_nameunit(sc->dev));
2604 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2606 tmp &= ~0x1fffff;
2607 tmp |= ic->ic_bss->ni_intval * 16;
2608 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2610 if (ic->ic_opmode == IEEE80211_M_STA)
2612 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2614 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2616 tmp |= RT2860_REG_BCN_TX_ENABLE;
2617 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2619 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2621 tmp |= RT2860_REG_BCN_TX_ENABLE;
2622 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2625 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2629 * rt2860_asic_disable_tsf_sync
2631 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2633 uint32_t tmp;
2635 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2636 "%s: disabling TSF\n",
2637 device_get_nameunit(sc->dev));
2639 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2641 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2642 RT2860_REG_TSF_TIMER_ENABLE |
2643 RT2860_REG_TBTT_TIMER_ENABLE);
2645 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2646 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2648 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2652 * rt2860_asic_enable_mrr
2654 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2656 #define CCK(mcs) (mcs)
2657 #define OFDM(mcs) ((1 << 3) | (mcs))
2658 #define HT(mcs) (mcs)
2660 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2661 (OFDM(6) << 28) | /* 54 -> 48 */
2662 (OFDM(5) << 24) | /* 48 -> 36 */
2663 (OFDM(4) << 20) | /* 36 -> 24 */
2664 (OFDM(3) << 16) | /* 24 -> 18 */
2665 (OFDM(2) << 12) | /* 18 -> 12 */
2666 (OFDM(1) << 8) | /* 12 -> 9 */
2667 (OFDM(0) << 4) | /* 9 -> 6 */
2668 OFDM(0)); /* 6 -> 6 */
2670 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2671 (CCK(2) << 12) | /* 11 -> 5.5 */
2672 (CCK(1) << 8) | /* 5.5 -> 2 */
2673 (CCK(0) << 4) | /* 2 -> 1 */
2674 CCK(0)); /* 1 -> 1 */
2676 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2677 (HT(6) << 28) |
2678 (HT(5) << 24) |
2679 (HT(4) << 20) |
2680 (HT(3) << 16) |
2681 (HT(2) << 12) |
2682 (HT(1) << 8) |
2683 (HT(0) << 4) |
2684 HT(0));
2686 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2687 (HT(14) << 28) |
2688 (HT(13) << 24) |
2689 (HT(12) << 20) |
2690 (HT(11) << 16) |
2691 (HT(10) << 12) |
2692 (HT(9) << 8) |
2693 (HT(8) << 4) |
2694 HT(8));
2696 #undef HT
2697 #undef OFDM
2698 #undef CCK
2702 * rt2860_asic_set_txpreamble
2704 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2706 struct ieee80211com *ic;
2707 uint32_t tmp;
2709 ic = &sc->ic;
2711 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2712 "%s: %s short Tx preamble\n",
2713 device_get_nameunit(sc->dev),
2714 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2716 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2718 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2720 if (sc->ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2721 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2723 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2727 * rt2860_asic_set_basicrates
2729 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2731 struct ieee80211com *ic;
2733 ic = &sc->ic;
2735 if (ic->ic_curmode == IEEE80211_MODE_11B)
2736 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2737 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2738 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2739 else
2740 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2744 * rt2860_asic_update_rtsthreshold
2746 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2748 struct ieee80211com *ic;
2749 uint32_t tmp;
2750 uint16_t threshold;
2752 ic = &sc->ic;
2754 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2755 "%s: updating RTS threshold: %d\n",
2756 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
2758 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2760 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2762 threshold = (ic->ic_rtsthreshold < IEEE80211_RTS_MAX) ?
2763 ic->ic_rtsthreshold : 0x1000;
2765 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2766 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2768 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
2772 * rt2860_asic_update_txpower
2774 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
2776 struct ieee80211com *ic;
2777 uint32_t *txpow_rate;
2778 int8_t delta;
2779 uint8_t val;
2780 uint32_t tmp;
2781 int i;
2783 ic = &sc->ic;
2785 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2786 "%s: updating Tx power: %d\n",
2787 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
2789 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
2791 txpow_rate = sc->txpow_rate_20mhz;
2793 else
2795 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2796 txpow_rate = sc->txpow_rate_40mhz_2ghz;
2797 else
2798 txpow_rate = sc->txpow_rate_40mhz_5ghz;
2801 delta = 0;
2803 val = rt2860_io_bbp_read(sc, 1);
2804 val &= 0xfc;
2806 if (ic->ic_txpowlimit > 90)
2808 /* do nothing */
2810 else if (ic->ic_txpowlimit > 60)
2812 delta -= 1;
2814 else if (ic->ic_txpowlimit > 30)
2816 delta -= 3;
2818 else if (ic->ic_txpowlimit > 15)
2820 val |= 0x1;
2822 else if (ic->ic_txpowlimit > 9)
2824 val |= 0x1;
2825 delta -= 3;
2827 else
2829 val |= 0x2;
2832 rt2860_io_bbp_write(sc, 1, val);
2834 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
2836 if (txpow_rate[i] == 0xffffffff)
2837 continue;
2839 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
2841 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
2846 * rt2860_asic_update_promisc
2848 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
2850 struct ifnet *ifp;
2851 uint32_t tmp;
2853 ifp = sc->ic.ic_ifp;
2855 printf("%s: %s promiscuous mode\n",
2856 device_get_nameunit(sc->dev),
2857 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
2859 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
2861 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
2863 if (!(ifp->if_flags & IFF_PROMISC))
2864 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
2866 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
2870 * rt2860_asic_updateprot
2872 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
2874 struct ieee80211com *ic;
2875 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
2876 uint8_t htopmode;
2878 ic = &sc->ic;
2880 /* CCK frame protection */
2882 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2883 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
2885 /* set up protection frame phy mode and rate (MCS code) */
2887 if (ic->ic_curmode == IEEE80211_MODE_11A)
2888 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2889 (0 << RT2860_REG_PROT_MCS_SHIFT);
2890 else
2891 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2892 (3 << RT2860_REG_PROT_MCS_SHIFT));
2894 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
2896 /* OFDM frame protection */
2898 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
2899 RT2860_REG_TXOP_ALLOW_ALL;
2901 if (ic->ic_flags & IEEE80211_F_USEPROT)
2903 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2904 "%s: updating protection mode: b/g protection mode=%s\n",
2905 device_get_nameunit(sc->dev),
2906 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2907 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2909 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2910 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2911 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2912 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
2913 else
2914 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2916 else
2918 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2919 "%s: updating protection mode: b/g protection mode=%s\n",
2920 device_get_nameunit(sc->dev), "none");
2922 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
2925 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
2927 /* HT frame protection */
2929 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
2930 htopmode = ic->ic_bss->ni_htopmode;
2931 else
2932 htopmode = ic->ic_curhtprotmode;
2934 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2935 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
2936 device_get_nameunit(sc->dev),
2937 htopmode & IEEE80211_HTINFO_OPMODE,
2938 (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
2939 ((ic->ic_htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
2941 switch (htopmode & IEEE80211_HTINFO_OPMODE)
2943 /* IEEE80211_HTINFO_OPMODE_HT20PR */
2944 case IEEE80211_HTINFO_OPMODE_HT20PR:
2945 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
2946 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2947 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
2948 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2949 (4 << RT2860_REG_PROT_MCS_SHIFT);
2951 gf20_prot = mm20_prot;
2953 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
2954 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2955 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2957 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2958 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2959 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2960 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
2961 else
2962 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
2964 gf40_prot = mm40_prot;
2965 break;
2967 /* IEEE80211_HTINFO_OPMODE_MIXED */
2968 case IEEE80211_HTINFO_OPMODE_MIXED:
2969 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
2970 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
2971 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
2973 if (ic->ic_flags & IEEE80211_F_USEPROT)
2974 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2975 (3 << RT2860_REG_PROT_MCS_SHIFT);
2976 else
2977 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2978 (4 << RT2860_REG_PROT_MCS_SHIFT);
2980 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2981 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
2982 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
2983 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
2984 else
2985 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
2987 gf20_prot = mm20_prot;
2989 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
2991 if (ic->ic_flags & IEEE80211_F_USEPROT)
2992 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
2993 (3 << RT2860_REG_PROT_MCS_SHIFT);
2994 else
2995 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
2996 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
2998 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
2999 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3000 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
3001 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3002 else
3003 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3005 gf40_prot = mm40_prot;
3006 break;
3009 * IEEE80211_HTINFO_OPMODE_PURE
3010 * IEEE80211_HTINFO_OPMODE_PROTOPT
3012 case IEEE80211_HTINFO_OPMODE_PURE:
3013 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3014 default:
3015 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3016 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3017 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3018 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3019 (4 << RT2860_REG_PROT_MCS_SHIFT);
3021 gf20_prot = mm20_prot;
3023 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3024 RT2860_REG_TXOP_ALLOW_ALL |
3025 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3026 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3028 gf40_prot = mm40_prot;
3029 break;
3032 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3033 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3034 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3035 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3039 * rt2860_asic_updateslot
3041 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3043 struct ieee80211com *ic;
3044 uint32_t tmp;
3046 ic = &sc->ic;
3048 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3049 "%s: %s short slot time\n",
3050 device_get_nameunit(sc->dev),
3051 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3053 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3055 tmp &= ~0xff;
3056 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3058 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3062 * rt2860_asic_wme_update
3064 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3066 struct ieee80211com *ic;
3067 struct ieee80211_wme_state *wme;
3068 const struct wmeParams *wmep;
3069 int i;
3071 ic = &sc->ic;
3072 wme = &ic->ic_wme;
3073 wmep = wme->wme_chanParams.cap_wmeParams;
3075 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3076 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3077 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3078 device_get_nameunit(sc->dev),
3079 wmep[WME_AC_VO].wmep_aifsn,
3080 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3081 wmep[WME_AC_VO].wmep_txopLimit,
3082 wmep[WME_AC_VI].wmep_aifsn,
3083 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3084 wmep[WME_AC_VI].wmep_txopLimit,
3085 wmep[WME_AC_BK].wmep_aifsn,
3086 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3087 wmep[WME_AC_BK].wmep_txopLimit,
3088 wmep[WME_AC_BE].wmep_aifsn,
3089 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3090 wmep[WME_AC_BE].wmep_txopLimit);
3092 for (i = 0; i < WME_NUM_AC; i++)
3093 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3094 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3095 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3097 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3098 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3099 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3101 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3102 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3103 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3105 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3106 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3107 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3109 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3110 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3112 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3113 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3117 * rt2860_asic_update_beacon
3119 static int rt2860_asic_update_beacon(struct rt2860_softc *sc)
3121 struct ieee80211com *ic;
3122 struct mbuf *m;
3123 struct rt2860_txwi txwi;
3124 uint8_t rate, mcs;
3125 uint32_t tmp;
3127 ic = &sc->ic;
3129 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3130 if (m == NULL)
3131 return ENOMEM;
3133 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3134 mcs = rt2860_rate2mcs(rate);
3136 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3138 txwi.wcid = 0xff;
3139 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3140 RT2860_TXWI_MPDU_LEN_SHIFT);
3141 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3142 txwi.mpdu_density_flags |=
3143 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3144 txwi.bawin_size_xflags |=
3145 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3147 if (rate == 2)
3149 txwi.phymode_ifs_stbc_shortgi =
3150 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3152 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3153 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3155 else
3157 txwi.phymode_ifs_stbc_shortgi =
3158 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3161 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3162 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3164 /* disable temporarily TSF sync */
3166 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3168 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3169 RT2860_REG_TSF_TIMER_ENABLE |
3170 RT2860_REG_TBTT_TIMER_ENABLE);
3172 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3174 /* write Tx wireless info and beacon frame to on-chip memory */
3176 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3177 &txwi, sizeof(struct rt2860_txwi));
3179 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3180 mtod(m, uint8_t *), m->m_pkthdr.len);
3182 /* enable again TSF sync */
3184 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3186 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3187 RT2860_REG_TSF_TIMER_ENABLE |
3188 RT2860_REG_TBTT_TIMER_ENABLE);
3190 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3192 m_freem(m);
3194 return 0;
3198 * rt2860_asic_clear_keytables
3200 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3202 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3204 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID(0), 0, 2 * 256);
3206 /* clear WCID attribute table (entries = 256, entry size = 4) */
3208 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3210 /* clear IV/EIV table (entries = 256, entry size = 8) */
3212 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3214 /* clear pairwise key table (entries = 256, entry size = 32) */
3216 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3218 /* clear shared key table (entries = 32, entry size = 32) */
3220 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3222 /* clear shared key mode (entries = 32, entry size = 2) */
3224 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3228 * rt2860_rxrate
3230 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3232 uint8_t mcs, phymode;
3233 uint8_t rate;
3235 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3236 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3237 RT2860_RXWI_PHYMODE_MASK;
3239 rate = 2;
3241 switch (phymode)
3243 case RT2860_RXWI_PHYMODE_CCK:
3244 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3246 case 0: rate = 2; break; /* 1 Mbps */
3247 case 1: rate = 4; break; /* 2 MBps */
3248 case 2: rate = 11; break; /* 5.5 Mbps */
3249 case 3: rate = 22; break; /* 11 Mbps */
3251 break;
3253 case RT2860_RXWI_PHYMODE_OFDM:
3254 switch (mcs)
3256 case 0: rate = 12; break; /* 6 Mbps */
3257 case 1: rate = 18; break; /* 9 Mbps */
3258 case 2: rate = 24; break; /* 12 Mbps */
3259 case 3: rate = 36; break; /* 18 Mbps */
3260 case 4: rate = 48; break; /* 24 Mbps */
3261 case 5: rate = 72; break; /* 36 Mbps */
3262 case 6: rate = 96; break; /* 48 Mbps */
3263 case 7: rate = 108; break; /* 54 Mbps */
3265 break;
3267 case RT2860_RXWI_PHYMODE_HT_MIXED:
3268 case RT2860_RXWI_PHYMODE_HT_GF:
3269 break;
3272 return rate;
3276 * rt2860_maxrssi_rxpath
3278 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3279 const struct rt2860_rxwi *rxwi)
3281 uint8_t rxpath;
3283 rxpath = 0;
3285 if (sc->nrxpath > 1)
3286 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3287 rxpath = 1;
3289 if (sc->nrxpath > 2)
3290 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3291 rxpath = 2;
3293 return rxpath;
3297 * rt2860_rssi2dbm
3299 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3300 uint8_t rssi, uint8_t rxpath)
3302 struct ieee80211com *ic;
3303 struct ieee80211_channel *c;
3304 int chan;
3305 int8_t rssi_off, lna_gain;
3307 if (rssi == 0)
3308 return -99;
3310 ic = &sc->ic;
3311 c = ic->ic_curchan;
3312 chan = ieee80211_chan2ieee(ic, c);
3314 if (IEEE80211_IS_CHAN_5GHZ(c))
3316 rssi_off = sc->rssi_off_5ghz[rxpath];
3318 if (chan <= 64)
3319 lna_gain = sc->lna_gain[1];
3320 else if (chan <= 128)
3321 lna_gain = sc->lna_gain[2];
3322 else
3323 lna_gain = sc->lna_gain[3];
3325 else
3327 rssi_off = sc->rssi_off_2ghz[rxpath];
3328 lna_gain = sc->lna_gain[0];
3331 return (-12 - rssi_off - lna_gain - rssi);
3335 * rt2860_rate2mcs
3337 static uint8_t rt2860_rate2mcs(uint8_t rate)
3339 switch (rate)
3341 /* CCK rates */
3342 case 2: return 0;
3343 case 4: return 1;
3344 case 11: return 2;
3345 case 22: return 3;
3347 /* OFDM rates */
3348 case 12: return 0;
3349 case 18: return 1;
3350 case 24: return 2;
3351 case 36: return 3;
3352 case 48: return 4;
3353 case 72: return 5;
3354 case 96: return 6;
3355 case 108: return 7;
3358 return 0;
3362 * rt2860_ackrate
3364 static int rt2860_ackrate(struct ieee80211com *ic, int rate)
3366 switch (rate)
3368 /* CCK rates */
3370 case 2:
3371 return 2;
3373 case 4:
3374 case 11:
3375 case 22:
3376 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3378 /* OFDM rates */
3380 case 12:
3381 case 18:
3382 return 12;
3384 case 24:
3385 case 36:
3386 return 24;
3388 case 48:
3389 case 72:
3390 case 96:
3391 case 108:
3392 return 48;
3395 /* default to 1Mbps */
3396 return 2;
3400 * rt2860_txtime
3402 static uint16_t rt2860_txtime(int len, int rate, uint32_t flags)
3404 uint16_t txtime;
3406 if (RT2860_RATE_IS_OFDM(rate))
3408 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3409 txtime = 16 + 4 + 4 * txtime + 6;
3411 else
3413 txtime = (16 * len + rate - 1) / rate;
3415 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3416 txtime += 72 + 24;
3417 else
3418 txtime += 144 + 48;
3421 return txtime;
3425 * rt2860_tx_frame
3427 static int rt2860_tx_frame(struct rt2860_softc *sc,
3428 struct mbuf *m, struct ieee80211_node *ni, int qid)
3430 struct ieee80211com *ic;
3431 struct rt2860_softc_tx_ring *ring;
3432 struct rt2860_softc_tx_data *data;
3433 struct rt2860_txdesc *desc;
3434 struct rt2860_txwi *txwi;
3435 struct ieee80211_frame *wh;
3436 struct rt2860_softc_tx_radiotap_header *tap;
3437 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3438 u_int hdrsize, hdrspace;
3439 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, qsel;
3440 uint16_t qos, len, dmalen, mpdu_len, dur;
3441 int error, hasqos, ackrate, ndmasegs, ndescs, i, j;
3443 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3444 ("%s: Tx frame: invalid qid=%d\n",
3445 device_get_nameunit(sc->dev), qid));
3447 ic = &sc->ic;
3449 ring = &sc->tx_ring[qid];
3450 desc = &ring->desc[ring->desc_cur];
3451 data = &ring->data[ring->data_cur];
3452 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3454 wh = mtod(m, struct ieee80211_frame *);
3456 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3458 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3459 if (hasqos)
3461 if (IEEE80211_HAS_ADDR4(wh))
3462 qos = le16toh(*(const uint16_t *)
3463 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3464 else
3465 qos = le16toh(*(const uint16_t *)
3466 (((struct ieee80211_qosframe *) wh)->i_qos));
3468 else
3470 qos = 0;
3473 if (ni->ni_flags & IEEE80211_NODE_HT)
3475 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3476 rate = 0;
3477 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3478 rate = ic->ic_fixed_rate;
3479 else
3480 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
3482 else
3484 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
3485 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3486 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
3487 rate = ic->ic_fixed_rate;
3488 else
3489 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
3492 rate &= IEEE80211_RATE_VAL;
3494 /* fill Tx wireless info */
3496 if (ni->ni_flags & IEEE80211_NODE_HT)
3497 mcs = rate;
3498 else
3499 mcs = rt2860_rate2mcs(rate);
3501 pid = mcs;
3503 /* management frames do not need encryption */
3505 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3506 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3508 /* calculate MPDU length without padding */
3510 hdrsize = ieee80211_hdrsize(wh);
3511 hdrspace = ieee80211_hdrspace(ic, wh);
3512 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3514 memset(txwi, 0, sizeof(struct rt2860_txwi));
3516 txwi->wcid = wcid;
3518 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3519 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3520 RT2860_TXWI_MPDU_LEN_SHIFT);
3522 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
3524 shortgi = (ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
3525 (ni->ni_flags & IEEE80211_NODE_HT);
3527 txwi->phymode_ifs_stbc_shortgi |=
3528 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
3529 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3531 if (ni->ni_flags & IEEE80211_NODE_HT)
3533 txwi->phymode_ifs_stbc_shortgi |=
3534 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3536 else
3538 if (!RT2860_RATE_IS_OFDM(rate))
3540 txwi->phymode_ifs_stbc_shortgi |=
3541 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3543 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3544 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3546 else
3548 txwi->phymode_ifs_stbc_shortgi |=
3549 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3553 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
3554 (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40))
3555 bw = RT2860_TXWI_BW_40;
3556 else
3557 bw = RT2860_TXWI_BW_20;
3559 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
3560 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3562 if (type != IEEE80211_FC0_TYPE_DATA)
3563 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3564 else
3565 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3567 /* skip ACKs for multicast frames and probe responses */
3569 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3570 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3571 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)) &&
3572 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
3574 txwi->bawin_size_xflags |=
3575 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3577 if (ni->ni_flags & IEEE80211_NODE_HT)
3579 /* preamble + plcp + signal extension */
3581 dur = 16 + 4 + 6;
3583 else
3585 ackrate = rt2860_ackrate(ic, rate);
3587 dur = rt2860_txtime(RT2860_ACK_SIZE, ackrate, ic->ic_flags) +
3588 sc->sifs;
3591 *(uint16_t *) wh->i_dur = htole16(dur);
3594 /* ask MAC to insert timestamp into probe responses */
3596 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3597 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3598 txwi->mpdu_density_flags |=
3599 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3601 if (bpf_peers_present(sc->drvbpf))
3603 tap = &sc->txtap;
3605 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3606 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3607 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3608 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3609 tap->chan_maxpow = 0;
3611 if (ni->ni_flags & IEEE80211_NODE_HT)
3612 tap->rate = mcs | IEEE80211_RATE_MCS;
3613 else
3614 tap->rate = rate;
3616 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3617 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3619 if (shortgi)
3620 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3622 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3623 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3625 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3627 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3629 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3631 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3633 else
3635 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
3639 /* copy and trim 802.11 header */
3641 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3642 m_adj(m, hdrspace);
3644 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3645 dma_seg, &ndmasegs, 0);
3646 if (error != 0)
3648 /* too many fragments, linearize */
3650 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3651 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3652 device_get_nameunit(sc->dev));
3654 m = m_defrag(m, M_DONTWAIT);
3655 if (m == NULL)
3656 return ENOMEM;
3658 sc->tx_defrag_packets++;
3660 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3661 dma_seg, &ndmasegs, 0);
3662 if (error != 0)
3664 printf("%s: could not load mbuf DMA map: error=%d\n",
3665 device_get_nameunit(sc->dev), error);
3666 m_freem(m);
3667 return error;
3671 /* determine how many Tx descs are required */
3673 ndescs = 1 + ndmasegs / 2;
3674 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3676 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3677 "%s: there are not enough Tx descs\n",
3678 device_get_nameunit(sc->dev));
3680 sc->no_tx_desc_avail++;
3682 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3683 m_freem(m);
3684 return EFBIG;
3687 data->m = m;
3688 data->ni = ni;
3690 /* set up Tx descs */
3692 /* first segment is Tx wireless info and 802.11 header */
3694 len = sizeof(struct rt2860_txwi) + hdrsize;
3696 /* align end on a 4-bytes boundary */
3698 dmalen = (len + 3) & ~ 3;
3700 memset((caddr_t) txwi + len, 0, dmalen - len);
3702 qsel = RT2860_TXDESC_QSEL_EDCA;
3704 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3705 desc->sdl0 = htole16(dmalen);
3706 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3708 /* set up payload segments */
3710 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3712 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3713 desc->sdl1 = htole16(dma_seg[j].ds_len);
3715 ring->desc_queued++;
3716 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3718 j++;
3720 desc = &ring->desc[ring->desc_cur];
3722 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3723 desc->sdl0 = htole16(dma_seg[j].ds_len);
3724 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3726 j++;
3729 /* finalize last payload segment */
3731 if (i > 0)
3733 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3734 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3736 else
3738 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3739 desc->sdl1 = 0;
3742 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3743 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3744 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3745 device_get_nameunit(sc->dev),
3746 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3747 bw, stbc, shortgi, mcs, dmalen, ndmasegs,
3748 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);
3750 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3751 BUS_DMASYNC_PREWRITE);
3752 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3753 BUS_DMASYNC_PREWRITE);
3754 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3755 BUS_DMASYNC_PREWRITE);
3757 ring->desc_queued++;
3758 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3760 ring->data_queued++;
3761 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3763 /* kick Tx */
3765 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3767 return 0;
3771 * rt2860_tx_raw
3773 static int rt2860_tx_raw(struct rt2860_softc *sc,
3774 struct mbuf *m, struct ieee80211_node *ni,
3775 const struct ieee80211_bpf_params *params)
3777 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3778 "%s: Tx raw\n",
3779 device_get_nameunit(sc->dev));
3781 return 0;
3785 * rt2860_intr
3787 static void rt2860_intr(void *arg)
3789 struct rt2860_softc *sc;
3790 struct ifnet *ifp;
3791 uint32_t status;
3793 sc = arg;
3794 ifp = sc->ifp;
3796 /* acknowledge interrupts */
3798 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
3799 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
3801 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3802 "%s: interrupt: status = 0x%08x\n",
3803 device_get_nameunit(sc->dev), status);
3805 if (status == 0xffffffff || /* device likely went away */
3806 status == 0) /* not for us */
3807 return;
3809 sc->interrupts++;
3811 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
3812 return;
3814 if (status & RT2860_REG_INT_TX_COHERENT)
3815 rt2860_tx_coherent_intr(sc);
3817 if (status & RT2860_REG_INT_RX_COHERENT)
3818 rt2860_rx_coherent_intr(sc);
3820 if (status & RT2860_REG_INT_TXRX_COHERENT)
3821 rt2860_txrx_coherent_intr(sc);
3823 if (status & RT2860_REG_INT_FIFO_STA_FULL)
3824 rt2860_fifo_sta_full_intr(sc);
3826 if (status & RT2860_REG_INT_TX_MGMT_DONE)
3827 rt2860_tx_intr(sc, 5);
3829 if (status & RT2860_REG_INT_RX_DONE)
3830 rt2860_rx_intr(sc);
3832 if (status & RT2860_REG_INT_RX_DELAY_DONE)
3833 rt2860_rx_delay_intr(sc);
3835 if (status & RT2860_REG_INT_TX_HCCA_DONE)
3836 rt2860_tx_intr(sc, 4);
3838 if (status & RT2860_REG_INT_TX_AC3_DONE)
3839 rt2860_tx_intr(sc, 3);
3841 if (status & RT2860_REG_INT_TX_AC2_DONE)
3842 rt2860_tx_intr(sc, 2);
3844 if (status & RT2860_REG_INT_TX_AC1_DONE)
3845 rt2860_tx_intr(sc, 1);
3847 if (status & RT2860_REG_INT_TX_AC0_DONE)
3848 rt2860_tx_intr(sc, 0);
3850 if (status & RT2860_REG_INT_TX_DELAY_DONE)
3851 rt2860_tx_delay_intr(sc);
3853 if (status & RT2860_REG_INT_PRE_TBTT)
3854 rt2860_pre_tbtt_intr(sc);
3856 if (status & RT2860_REG_INT_TBTT)
3857 rt2860_tbtt_intr(sc);
3859 if (status & RT2860_REG_INT_MCU_CMD)
3860 rt2860_mcu_cmd_intr(sc);
3862 if (status & RT2860_REG_INT_AUTO_WAKEUP)
3863 rt2860_auto_wakeup_intr(sc);
3865 if (status & RT2860_REG_INT_GP_TIMER)
3866 rt2860_gp_timer_intr(sc);
3870 * rt2860_tx_coherent_intr
3872 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
3874 uint32_t tmp;
3875 int i;
3877 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3878 "%s: Tx coherent interrupt\n",
3879 device_get_nameunit(sc->dev));
3881 sc->tx_coherent_interrupts++;
3883 /* restart DMA engine */
3885 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3887 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3888 RT2860_REG_RX_DMA_ENABLE |
3889 RT2860_REG_TX_DMA_ENABLE);
3891 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3893 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3895 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3896 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3898 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3900 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3901 sc->tx_ring[i].desc_phys_addr);
3902 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3903 RT2860_SOFTC_TX_RING_DESC_COUNT);
3904 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3907 /* init Rx ring */
3909 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3911 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3912 sc->rx_ring.desc_phys_addr);
3913 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3914 RT2860_SOFTC_RX_RING_DATA_COUNT);
3915 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3916 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3918 rt2860_txrx_enable(sc);
3922 * rt2860_rx_coherent_intr
3924 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
3926 uint32_t tmp;
3927 int i;
3929 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3930 "%s: Rx coherent interrupt\n",
3931 device_get_nameunit(sc->dev));
3933 sc->rx_coherent_interrupts++;
3935 /* restart DMA engine */
3937 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3939 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3940 RT2860_REG_RX_DMA_ENABLE |
3941 RT2860_REG_TX_DMA_ENABLE);
3943 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3945 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3947 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3948 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
3950 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
3952 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
3953 sc->tx_ring[i].desc_phys_addr);
3954 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
3955 RT2860_SOFTC_TX_RING_DESC_COUNT);
3956 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
3959 /* init Rx ring */
3961 rt2860_reset_rx_ring(sc, &sc->rx_ring);
3963 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
3964 sc->rx_ring.desc_phys_addr);
3965 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
3966 RT2860_SOFTC_RX_RING_DATA_COUNT);
3967 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
3968 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
3970 rt2860_txrx_enable(sc);
3974 * rt2860_txrx_coherent_intr
3976 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
3978 uint32_t tmp;
3979 int i;
3981 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
3982 "%s: Tx/Rx coherent interrupt\n",
3983 device_get_nameunit(sc->dev));
3985 sc->txrx_coherent_interrupts++;
3987 /* restart DMA engine */
3989 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
3991 tmp &= ~(RT2860_REG_TX_WB_DDONE |
3992 RT2860_REG_RX_DMA_ENABLE |
3993 RT2860_REG_TX_DMA_ENABLE);
3995 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
3997 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
3999 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4000 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4002 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4004 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4005 sc->tx_ring[i].desc_phys_addr);
4006 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4007 RT2860_SOFTC_TX_RING_DESC_COUNT);
4008 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4011 /* init Rx ring */
4013 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4015 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4016 sc->rx_ring.desc_phys_addr);
4017 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4018 RT2860_SOFTC_RX_RING_DATA_COUNT);
4019 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4020 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4022 rt2860_txrx_enable(sc);
4026 * rt2860_fifo_sta_full_intr
4028 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4030 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4031 "%s: FIFO statistic full interrupt\n",
4032 device_get_nameunit(sc->dev));
4034 sc->fifo_sta_full_interrupts++;
4036 RT2860_SOFTC_LOCK(sc);
4038 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4040 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4042 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4045 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4047 RT2860_SOFTC_UNLOCK(sc);
4051 * rt2860_rx_intr
4053 static void rt2860_rx_intr(struct rt2860_softc *sc)
4055 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4056 "%s: Rx interrupt\n",
4057 device_get_nameunit(sc->dev));
4059 sc->rx_interrupts++;
4061 RT2860_SOFTC_LOCK(sc);
4063 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4065 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4067 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4070 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4072 RT2860_SOFTC_UNLOCK(sc);
4076 * rt2860_rx_delay_intr
4078 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4080 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4081 "%s: Rx delay interrupt\n",
4082 device_get_nameunit(sc->dev));
4084 sc->rx_delay_interrupts++;
4088 * rt2860_tx_intr
4090 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4092 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4093 ("%s: Tx interrupt: invalid qid=%d\n",
4094 device_get_nameunit(sc->dev), qid));
4096 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4097 "%s: Tx interrupt: qid=%d\n",
4098 device_get_nameunit(sc->dev), qid);
4100 sc->tx_interrupts[qid]++;
4102 RT2860_SOFTC_LOCK(sc);
4104 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4106 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4108 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4111 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4113 RT2860_SOFTC_UNLOCK(sc);
4117 * rt2860_tx_delay_intr
4119 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4121 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4122 "%s: Tx delay interrupt\n",
4123 device_get_nameunit(sc->dev));
4125 sc->tx_delay_interrupts++;
4129 * rt2860_pre_tbtt_intr
4131 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4133 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4134 "%s: Pre-TBTT interrupt\n",
4135 device_get_nameunit(sc->dev));
4137 sc->pre_tbtt_interrupts++;
4141 * rt2860_tbtt_intr
4143 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4145 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4146 "%s: TBTT interrupt\n",
4147 device_get_nameunit(sc->dev));
4149 sc->tbtt_interrupts++;
4153 * rt2860_mcu_cmd_intr
4155 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4157 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4158 "%s: MCU command interrupt\n",
4159 device_get_nameunit(sc->dev));
4161 sc->mcu_cmd_interrupts++;
4165 * rt2860_auto_wakeup_intr
4167 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4169 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4170 "%s: auto wakeup interrupt\n",
4171 device_get_nameunit(sc->dev));
4173 sc->auto_wakeup_interrupts++;
4177 * rt2860_gp_timer_intr
4179 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4181 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4182 "%s: GP timer interrupt\n",
4183 device_get_nameunit(sc->dev));
4185 sc->gp_timer_interrupts++;
4189 * rt2860_rx_done_task
4191 static void rt2860_rx_done_task(void *context, int pending)
4193 struct rt2860_softc *sc;
4194 struct ifnet *ifp;
4195 int again;
4197 sc = context;
4198 ifp = sc->ifp;
4200 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4201 "%s: Rx done task\n",
4202 device_get_nameunit(sc->dev));
4204 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4205 return;
4207 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4209 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4211 RT2860_SOFTC_LOCK(sc);
4213 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4215 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4216 "%s: Rx done task: scheduling again\n",
4217 device_get_nameunit(sc->dev));
4219 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4221 else
4223 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4226 RT2860_SOFTC_UNLOCK(sc);
4230 * rt2860_tx_done_task
4232 static void rt2860_tx_done_task(void *context, int pending)
4234 struct rt2860_softc *sc;
4235 struct ifnet *ifp;
4236 uint32_t intr_mask;
4237 int i;
4239 sc = context;
4240 ifp = sc->ifp;
4242 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4243 "%s: Tx done task\n",
4244 device_get_nameunit(sc->dev));
4246 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4247 return;
4249 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4250 RT2860_REG_INT_TX_HCCA_DONE |
4251 RT2860_REG_INT_TX_AC3_DONE |
4252 RT2860_REG_INT_TX_AC2_DONE |
4253 RT2860_REG_INT_TX_AC1_DONE |
4254 RT2860_REG_INT_TX_AC0_DONE);
4256 sc->intr_pending_mask &= ~intr_mask;
4258 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4259 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4261 sc->tx_timer = 0;
4263 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4265 RT2860_SOFTC_LOCK(sc);
4267 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4268 (sc->intr_disable_mask & intr_mask));
4270 if (sc->intr_pending_mask & intr_mask)
4272 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4273 "%s: Tx done task: scheduling again\n",
4274 device_get_nameunit(sc->dev));
4276 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4279 RT2860_SOFTC_UNLOCK(sc);
4283 * rt2860_fifo_sta_full_task
4285 static void rt2860_fifo_sta_full_task(void *context, int pending)
4287 struct rt2860_softc *sc;
4288 struct ifnet *ifp;
4290 sc = context;
4291 ifp = sc->ifp;
4293 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4294 "%s: FIFO statistic full task\n",
4295 device_get_nameunit(sc->dev));
4297 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4298 return;
4300 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4302 rt2860_drain_fifo_stats(sc);
4304 RT2860_SOFTC_LOCK(sc);
4306 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4308 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4309 "%s: FIFO statistic full task: scheduling again\n",
4310 device_get_nameunit(sc->dev));
4312 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4314 else
4316 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4319 RT2860_SOFTC_UNLOCK(sc);
4323 * rt2860_periodic_task
4325 static void rt2860_periodic_task(void *context, int pending)
4327 struct rt2860_softc *sc;
4328 struct ifnet *ifp;
4329 struct ieee80211com *ic;
4331 sc = context;
4332 ifp = sc->ifp;
4333 ic = &sc->ic;
4335 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4336 "%s: periodic task: round=%lu\n",
4337 device_get_nameunit(sc->dev), sc->periodic_round);
4339 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4340 return;
4342 RT2860_SOFTC_LOCK(sc);
4344 sc->periodic_round++;
4346 rt2860_update_stats(sc);
4348 if ((sc->periodic_round % 10) == 0)
4350 rt2860_update_raw_counters(sc);
4352 rt2860_watchdog(sc);
4354 if (ic->ic_opmode == IEEE80211_M_STA)
4355 rt2860_amrr_update_iter_func(sc, ic->ic_bss);
4356 else
4357 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, sc);
4360 RT2860_SOFTC_UNLOCK(sc);
4362 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4366 * rt2860_rx_eof
4368 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4370 struct ieee80211com *ic;
4371 struct ifnet *ifp;
4372 struct ieee80211_frame *wh;
4373 struct ieee80211_node *ni;
4374 struct rt2860_softc_rx_radiotap_header *tap;
4375 struct rt2860_softc_rx_ring *ring;
4376 struct rt2860_rxdesc *desc;
4377 struct rt2860_softc_rx_data *data;
4378 struct rt2860_rxwi *rxwi;
4379 struct mbuf *m, *mnew;
4380 bus_dma_segment_t segs[1];
4381 uint32_t index, desc_flags;
4382 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4383 int8_t rssi_dbm;
4384 int error, nsegs, len, ampdu, amsdu, nframes;
4386 ic = &sc->ic;
4387 ifp = sc->ifp;
4388 ring = &sc->rx_ring;
4390 nframes = 0;
4392 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4393 BUS_DMASYNC_POSTREAD);
4395 while (limit != 0)
4397 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4398 if (ring->cur == index)
4399 break;
4401 desc = &ring->desc[ring->cur];
4402 desc_flags = le32toh(desc->flags);
4403 data = &ring->data[ring->cur];
4405 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4406 break;
4408 nframes++;
4410 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4411 if (mnew == NULL)
4413 sc->rx_mbuf_alloc_errors++;
4414 ifp->if_ierrors++;
4415 goto skip;
4418 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4420 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4421 BUS_DMASYNC_POSTREAD);
4422 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4424 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4425 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4426 if (error != 0)
4428 m_freem(mnew);
4430 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4431 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
4432 if (error != 0)
4433 panic("%s: could not load old Rx mbuf: error=%d\n",
4434 device_get_name(sc->dev), error);
4436 sc->rx_mbuf_dmamap_errors++;
4437 ifp->if_ierrors++;
4439 goto skip;
4442 KASSERT(nsegs == 1, ("%s: too many DMA segments"),
4443 device_get_name(sc->dev));
4445 m = data->m;
4447 data->m = mnew;
4448 desc->sdp0 = htole32(segs[0].ds_addr);
4450 /* get Rx wireless info */
4452 rxwi = mtod(m, struct rt2860_rxwi *);
4453 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4454 RT2860_RXWI_SIZE_MASK;
4456 /* check for L2 padding between IEEE 802.11 frame header and body */
4458 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4460 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4461 "%s: L2 padding: len=%d\n",
4462 device_get_nameunit(sc->dev), len);
4464 len += 2;
4467 m->m_pkthdr.rcvif = ifp;
4468 m->m_data = (caddr_t) (rxwi + 1);
4469 m->m_pkthdr.len = m->m_len = len;
4471 /* check for crc errors */
4473 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4475 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4476 "%s: rxdesc: crc error\n",
4477 device_get_nameunit(sc->dev));
4479 ifp->if_ierrors++;
4481 if (!(ifp->if_flags & IFF_PROMISC))
4483 m_freem(m);
4484 goto skip;
4488 wh = (struct ieee80211_frame *) (rxwi + 1);
4490 /* check for cipher errors */
4492 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4494 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4495 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4496 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4498 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4499 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4501 m->m_flags |= M_WEP;
4503 else
4505 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4506 "%s: rxdesc: cipher error=0x%02x\n",
4507 device_get_nameunit(sc->dev), cipher_err);
4509 ifp->if_ierrors++;
4511 if (!(ifp->if_flags & IFF_PROMISC))
4513 m_free(m);
4514 goto skip;
4518 else
4520 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4522 ifp->if_ierrors++;
4524 if (!(ifp->if_flags & IFF_PROMISC))
4526 m_free(m);
4527 goto skip;
4532 /* check for A-MPDU */
4534 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
4536 m->m_flags |= M_AMPDU;
4537 ampdu = 1;
4539 else
4541 ampdu = 0;
4544 ant = rt2860_maxrssi_rxpath(sc, rxwi);
4545 rssi = rxwi->rssi[ant];
4546 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
4547 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
4548 RT2860_RXWI_PHYMODE_MASK);
4549 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
4550 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
4551 RT2860_RXWI_SHORTGI_MASK);
4552 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
4553 amsdu = (desc_flags & RT2860_RXDESC_FLAGS_AMSDU);
4555 if (bpf_peers_present(sc->drvbpf))
4557 tap = &sc->rxtap;
4559 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4560 tap->dbm_antsignal = rssi_dbm;
4561 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
4562 tap->antenna = ant;
4563 tap->antsignal = rssi;
4564 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4565 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4566 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4567 tap->chan_maxpow = 0;
4569 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
4570 tap->rate = mcs | IEEE80211_RATE_MCS;
4571 else
4572 tap->rate = rt2860_rxrate(rxwi);
4574 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
4575 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4577 if (shortgi)
4578 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4580 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
4583 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4584 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4585 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
4586 device_get_nameunit(sc->dev),
4587 len, phymode, bw, shortgi, mcs,
4588 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
4589 rxwi->snr[0], rxwi->snr[1],
4590 rxwi->wcid, ampdu, amsdu);
4592 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
4594 ieee80211_input(ic, m, ni, rssi_dbm - RT2860_NOISE_FLOOR, RT2860_NOISE_FLOOR, 0);
4596 ieee80211_free_node(ni);
4598 skip:
4600 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
4602 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4603 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4605 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
4607 limit--;
4610 if (ring->cur == 0)
4611 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4612 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4613 else
4614 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4615 ring->cur - 1);
4617 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4618 "%s: Rx eof: nframes=%d\n",
4619 device_get_nameunit(sc->dev), nframes);
4621 sc->rx_packets += nframes;
4623 return (limit == 0);
4627 * rt2860_tx_eof
4629 static void rt2860_tx_eof(struct rt2860_softc *sc,
4630 struct rt2860_softc_tx_ring *ring)
4632 struct ifnet *ifp;
4633 struct rt2860_txdesc *desc;
4634 struct rt2860_softc_tx_data *data;
4635 uint32_t index;
4636 int ndescs, nframes;
4638 ifp = sc->ifp;
4640 ndescs = 0;
4641 nframes = 0;
4643 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4644 BUS_DMASYNC_POSTREAD);
4646 for (;;)
4648 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
4649 if (ring->desc_next == index)
4650 break;
4652 ndescs++;
4654 rt2860_drain_fifo_stats(sc);
4656 desc = &ring->desc[ring->desc_next];
4658 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
4659 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
4661 nframes++;
4663 data = &ring->data[ring->data_next];
4665 if (data->m->m_flags & M_TXCB)
4666 ieee80211_process_callback(data->ni, data->m, 0);
4668 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4669 BUS_DMASYNC_POSTWRITE);
4670 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4672 m_freem(data->m);
4674 ieee80211_free_node(data->ni);
4676 data->m = NULL;
4677 data->ni = NULL;
4679 ifp->if_opackets++;
4681 ring->data_queued--;
4682 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4685 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
4687 ring->desc_queued--;
4688 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4691 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4692 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4694 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4695 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
4696 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
4700 * rt2860_update_stats
4702 static void rt2860_update_stats(struct rt2860_softc *sc)
4704 struct ifnet *ifp;
4705 struct ieee80211com *ic;
4706 struct ieee80211_node *ni;
4707 uint16_t associd;
4708 uint8_t wcid;
4709 uint32_t stacnt[3];
4710 int txcnt, retrycnt, failcnt;
4712 ifp = sc->ifp;
4713 ic = &sc->ic;
4715 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4716 "%s: update statistic\n",
4717 device_get_nameunit(sc->dev));
4719 rt2860_drain_fifo_stats(sc);
4721 if (ic->ic_opmode == IEEE80211_M_STA)
4723 ni = ic->ic_bss;
4725 associd = (ni != NULL) ? ni->ni_associd : 0;
4726 wcid = RT2860_AID2WCID(associd);
4728 /* read and clear Tx statistic registers */
4730 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
4731 stacnt, sizeof(stacnt));
4733 txcnt = le32toh(stacnt[1]) & 0xffff;
4734 retrycnt = le32toh(stacnt[1]) >> 16;
4735 failcnt = le32toh(stacnt[0]) & 0xffff;
4737 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4738 "%s: update statistic: associd=0x%04x, txcnt=%d, retrycnt=%d, failcnt=%d\n",
4739 device_get_nameunit(sc->dev),
4740 associd, txcnt, retrycnt, failcnt);
4742 ifp->if_oerrors += failcnt;
4744 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
4745 txcnt + retrycnt + failcnt, txcnt + retrycnt, retrycnt + failcnt);
4750 * rt2860_watchdog
4752 static void rt2860_watchdog(struct rt2860_softc *sc)
4754 uint32_t tmp;
4755 int ntries;
4757 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4759 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
4760 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4761 device_get_nameunit(sc->dev), tmp);
4763 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
4765 sc->tx_queue_not_empty[0]++;
4767 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
4769 for (ntries = 0; ntries < 10; ntries++)
4771 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4772 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
4773 break;
4775 DELAY(1);
4778 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4781 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
4783 sc->tx_queue_not_empty[1]++;
4785 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
4787 for (ntries = 0; ntries < 10; ntries++)
4789 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
4790 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
4791 break;
4793 DELAY(1);
4796 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
4801 * rt2860_drain_fifo_stats
4803 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
4805 struct ifnet *ifp;
4806 uint32_t stats;
4807 uint8_t wcid, mcs, pid;
4808 int ok, retrycnt;
4810 ifp = sc->ic.ic_ifp;
4812 /* drain Tx status FIFO (maxsize = 16) */
4814 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
4815 RT2860_REG_TX_STA_FIFO_VALID)
4817 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
4818 RT2860_REG_TX_STA_FIFO_WCID_MASK;
4820 /* if no ACK was requested, no feedback is available */
4822 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
4823 continue;
4825 /* update AMRR statistic */
4827 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
4828 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
4829 RT2860_REG_TX_STA_FIFO_MCS_MASK;
4830 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
4831 RT2860_REG_TX_STA_FIFO_PID_MASK;
4832 retrycnt = pid - mcs;
4834 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4835 "%s: FIFO statistic: wcid=0x%02x, ok=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
4836 device_get_nameunit(sc->dev),
4837 wcid, ok, mcs, pid, retrycnt);
4839 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
4841 if (!ok)
4842 ifp->if_oerrors++;
4847 * rt2860_update_raw_counters
4849 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
4851 uint32_t tmp;
4853 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
4855 sc->rx_crc_errors += tmp & 0xffff;
4856 sc->rx_phy_errors += tmp >> 16;
4858 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
4860 sc->rx_false_ccas += tmp & 0xffff;
4861 sc->rx_plcp_errors += tmp >> 16;
4863 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
4865 sc->rx_dup_packets += tmp & 0xffff;
4866 sc->rx_fifo_overflows += tmp >> 16;
4870 * rt2860_intr_enable
4872 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
4874 uint32_t tmp;
4876 sc->intr_disable_mask &= ~intr_mask;
4878 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4880 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4884 * rt2860_intr_disable
4886 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
4888 uint32_t tmp;
4890 sc->intr_disable_mask |= intr_mask;
4892 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
4894 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
4898 * rt2860_txrx_enable
4900 static int rt2860_txrx_enable(struct rt2860_softc *sc)
4902 struct ieee80211com *ic;
4903 struct ifnet *ifp;
4904 uint32_t tmp;
4905 int ntries;
4907 ic = &sc->ic;
4908 ifp = ic->ic_ifp;
4910 /* enable Tx/Rx DMA engine */
4912 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
4914 for (ntries = 0; ntries < 200; ntries++)
4916 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4917 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
4918 break;
4920 DELAY(1000);
4923 if (ntries == 200)
4925 printf("%s: timeout waiting for DMA engine\n",
4926 device_get_nameunit(sc->dev));
4927 return -1;
4930 DELAY(50);
4932 tmp |= RT2860_REG_TX_WB_DDONE |
4933 RT2860_REG_RX_DMA_ENABLE |
4934 RT2860_REG_TX_DMA_ENABLE |
4935 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
4937 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4939 /* set Rx filter */
4941 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
4942 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
4944 if (ic->ic_opmode != IEEE80211_M_MONITOR)
4946 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
4947 RT2860_REG_RX_FILTER_DROP_CTS |
4948 RT2860_REG_RX_FILTER_DROP_BA |
4949 RT2860_REG_RX_FILTER_DROP_ACK |
4950 RT2860_REG_RX_FILTER_DROP_VER_ERR |
4951 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
4952 RT2860_REG_RX_FILTER_DROP_CFACK |
4953 RT2860_REG_RX_FILTER_DROP_CFEND;
4955 if (ic->ic_opmode == IEEE80211_M_STA)
4956 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
4957 RT2860_REG_RX_FILTER_DROP_PSPOLL;
4959 if (!(ifp->if_flags & IFF_PROMISC))
4960 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
4963 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
4965 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
4966 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
4968 return 0;
4972 * rt2860_alloc_rx_ring
4974 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
4975 struct rt2860_softc_rx_ring *ring)
4977 struct rt2860_rxdesc *desc;
4978 struct rt2860_softc_rx_data *data;
4979 bus_dma_segment_t segs[1];
4980 int i, nsegs, error;
4982 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
4983 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
4984 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
4985 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
4986 0, NULL, NULL, &ring->desc_dma_tag);
4987 if (error != 0)
4989 printf("%s: could not create Rx desc DMA tag\n",
4990 device_get_nameunit(sc->dev));
4991 goto fail;
4994 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
4995 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
4996 if (error != 0)
4998 printf("%s: could not allocate Rx desc DMA memory\n",
4999 device_get_nameunit(sc->dev));
5000 goto fail;
5003 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5004 ring->desc,
5005 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5006 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5007 if (error != 0)
5009 printf("%s: could not load Rx desc DMA map\n",
5010 device_get_nameunit(sc->dev));
5011 goto fail;
5014 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5015 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5016 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
5017 &ring->data_dma_tag);
5018 if (error != 0)
5020 printf("%s: could not create Rx data DMA tag\n",
5021 device_get_nameunit(sc->dev));
5022 goto fail;
5025 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5027 desc = &ring->desc[i];
5028 data = &ring->data[i];
5030 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5031 if (error != 0)
5033 printf("%s: could not create Rx data DMA map\n",
5034 device_get_nameunit(sc->dev));
5035 goto fail;
5038 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5039 if (data->m == NULL)
5041 printf("%s: could not allocate Rx mbuf\n",
5042 device_get_nameunit(sc->dev));
5043 error = ENOMEM;
5044 goto fail;
5047 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5049 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5050 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5051 if (error != 0)
5053 printf("%s: could not load Rx mbuf DMA map\n",
5054 device_get_nameunit(sc->dev));
5055 goto fail;
5058 KASSERT(nsegs == 1, ("%s: too many DMA segments"),
5059 device_get_name(sc->dev));
5061 desc->sdp0 = htole32(segs[0].ds_addr);
5062 desc->sdl0 = htole16(MJUMPAGESIZE);
5065 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5066 BUS_DMASYNC_PREWRITE);
5068 return 0;
5070 fail:
5072 rt2860_free_rx_ring(sc, ring);
5074 return error;
5078 * rt2860_reset_rx_ring
5080 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5081 struct rt2860_softc_rx_ring *ring)
5083 struct rt2860_rxdesc *desc;
5084 int i;
5086 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5088 desc = &ring->desc[i];
5090 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5093 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5094 BUS_DMASYNC_PREWRITE);
5096 ring->cur = 0;
5100 * rt2860_free_rx_ring
5102 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5103 struct rt2860_softc_rx_ring *ring)
5105 struct rt2860_softc_rx_data *data;
5106 int i;
5108 if (ring->desc != NULL)
5110 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5111 BUS_DMASYNC_POSTWRITE);
5112 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5113 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5114 ring->desc_dma_map);
5117 if (ring->desc_dma_tag != NULL)
5118 bus_dma_tag_destroy(ring->desc_dma_tag);
5120 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5122 data = &ring->data[i];
5124 if (data->m != NULL)
5126 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5127 BUS_DMASYNC_POSTREAD);
5128 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5129 m_freem(data->m);
5132 if (data->dma_map != NULL)
5133 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5136 if (ring->data_dma_tag != NULL)
5137 bus_dma_tag_destroy(ring->data_dma_tag);
5141 * rt2860_alloc_tx_ring
5143 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5144 struct rt2860_softc_tx_ring *ring, int qid)
5146 struct rt2860_softc_tx_data *data;
5147 int error, i;
5149 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5150 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5151 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5152 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5153 0, NULL, NULL, &ring->desc_dma_tag);
5154 if (error != 0)
5156 printf("%s: could not create Tx desc DMA tag\n",
5157 device_get_nameunit(sc->dev));
5158 goto fail;
5161 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5162 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5163 if (error != 0)
5165 printf("%s: could not allocate Tx desc DMA memory\n",
5166 device_get_nameunit(sc->dev));
5167 goto fail;
5170 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5171 ring->desc,
5172 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5173 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5174 if (error != 0)
5176 printf("%s: could not load Tx desc DMA map\n",
5177 device_get_nameunit(sc->dev));
5178 goto fail;
5181 ring->desc_queued = 0;
5182 ring->desc_cur = 0;
5183 ring->desc_next = 0;
5185 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5186 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5187 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5188 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5189 0, NULL, NULL, &ring->seg0_dma_tag);
5190 if (error != 0)
5192 printf("%s: could not create Tx seg0 DMA tag\n",
5193 device_get_nameunit(sc->dev));
5194 goto fail;
5197 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5198 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5199 if (error != 0)
5201 printf("%s: could not allocate Tx seg0 DMA memory\n",
5202 device_get_nameunit(sc->dev));
5203 goto fail;
5206 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5207 ring->seg0,
5208 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5209 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5210 if (error != 0)
5212 printf("%s: could not load Tx seg0 DMA map\n",
5213 device_get_nameunit(sc->dev));
5214 goto fail;
5217 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5218 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5219 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5220 &ring->data_dma_tag);
5221 if (error != 0)
5223 printf("%s: could not create Tx data DMA tag\n",
5224 device_get_nameunit(sc->dev));
5225 goto fail;
5228 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5230 data = &ring->data[i];
5232 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5233 if (error != 0)
5235 printf("%s: could not create Tx data DMA map\n",
5236 device_get_nameunit(sc->dev));
5237 goto fail;
5241 ring->data_queued = 0;
5242 ring->data_cur = 0;
5243 ring->data_next = 0;
5245 ring->qid = qid;
5247 return 0;
5249 fail:
5251 rt2860_free_tx_ring(sc, ring);
5253 return error;
5257 * rt2860_reset_tx_ring
5259 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5260 struct rt2860_softc_tx_ring *ring)
5262 struct rt2860_softc_tx_data *data;
5263 struct rt2860_txdesc *desc;
5264 int i;
5266 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5268 desc = &ring->desc[i];
5270 desc->sdl0 = 0;
5271 desc->sdl1 = 0;
5274 ring->desc_queued = 0;
5275 ring->desc_cur = 0;
5276 ring->desc_next = 0;
5278 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5279 BUS_DMASYNC_PREWRITE);
5281 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5282 BUS_DMASYNC_PREWRITE);
5284 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5286 data = &ring->data[i];
5288 if (data->m != NULL)
5290 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5291 BUS_DMASYNC_POSTWRITE);
5292 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5293 m_freem(data->m);
5294 data->m = NULL;
5297 if (data->ni != NULL)
5299 ieee80211_free_node(data->ni);
5300 data->ni = NULL;
5304 ring->data_queued = 0;
5305 ring->data_cur = 0;
5306 ring->data_next = 0;
5310 * rt2860_free_tx_ring
5312 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5313 struct rt2860_softc_tx_ring *ring)
5315 struct rt2860_softc_tx_data *data;
5316 int i;
5318 if (ring->desc != NULL)
5320 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5321 BUS_DMASYNC_POSTWRITE);
5322 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5323 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5324 ring->desc_dma_map);
5327 if (ring->desc_dma_tag != NULL)
5328 bus_dma_tag_destroy(ring->desc_dma_tag);
5330 if (ring->seg0 != NULL)
5332 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5333 BUS_DMASYNC_POSTWRITE);
5334 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5335 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5336 ring->seg0_dma_map);
5339 if (ring->seg0_dma_tag != NULL)
5340 bus_dma_tag_destroy(ring->seg0_dma_tag);
5342 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5344 data = &ring->data[i];
5346 if (data->m != NULL)
5348 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5349 BUS_DMASYNC_POSTWRITE);
5350 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5351 m_freem(data->m);
5354 if (data->ni != NULL)
5355 ieee80211_free_node(data->ni);
5357 if (data->dma_map != NULL)
5358 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5361 if (ring->data_dma_tag != NULL)
5362 bus_dma_tag_destroy(ring->data_dma_tag);
5366 * rt2860_dma_map_addr
5368 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5369 int nseg, int error)
5371 if (error != 0)
5372 return;
5374 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5376 *(bus_addr_t *) arg = segs[0].ds_addr;
5380 * rt2860_sysctl_attach
5382 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5384 struct sysctl_ctx_list *ctx;
5385 struct sysctl_oid *tree;
5386 struct sysctl_oid *stats;
5388 ctx = device_get_sysctl_ctx(sc->dev);
5389 tree = device_get_sysctl_tree(sc->dev);
5391 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5392 "stats", CTLFLAG_RD, 0, "statistic");
5394 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5395 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5396 "all interrupts");
5398 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5399 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5400 "Tx coherent interrupts");
5402 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5403 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5404 "Rx coherent interrupts");
5406 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5407 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5408 "Tx/Rx coherent interrupts");
5410 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5411 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5412 "FIFO statistic full interrupts");
5414 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5415 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5416 "Rx interrupts");
5418 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5419 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5420 "Rx delay interrupts");
5422 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5423 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5424 "Tx MGMT interrupts");
5426 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5427 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5428 "Tx HCCA interrupts");
5430 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5431 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5432 "Tx AC3 interrupts");
5434 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5435 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5436 "Tx AC2 interrupts");
5438 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5439 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5440 "Tx AC1 interrupts");
5442 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5443 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5444 "Tx AC0 interrupts");
5446 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5447 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
5448 "Tx delay interrupts");
5450 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5451 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
5452 "Pre-TBTT interrupts");
5454 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5455 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
5456 "TBTT interrupts");
5458 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5459 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
5460 "MCU command interrupts");
5462 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5463 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
5464 "auto wakeup interrupts");
5466 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5467 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
5468 "GP timer interrupts");
5470 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5471 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
5472 "Tx MGMT descriptors queued");
5474 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5475 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
5476 "Tx MGMT data queued");
5478 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5479 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
5480 "Tx HCCA descriptors queued");
5482 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5483 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
5484 "Tx HCCA data queued");
5486 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5487 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
5488 "Tx AC3 descriptors queued");
5490 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5491 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
5492 "Tx AC3 data queued");
5494 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5495 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
5496 "Tx AC2 descriptors queued");
5498 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5499 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
5500 "Tx AC2 data queued");
5502 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5503 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
5504 "Tx AC1 descriptors queued");
5506 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5507 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
5508 "Tx AC1 data queued");
5510 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5511 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
5512 "Tx AC0 descriptors queued");
5514 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5515 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
5516 "Tx AC0 data queued");
5518 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5519 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5520 "Tx watchdog timeouts");
5522 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5523 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
5524 "Tx defragmented packets");
5526 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5527 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
5528 "no Tx descriptors available");
5530 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5531 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
5532 "Rx mbuf allocation errors");
5534 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5535 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
5536 "Rx mbuf DMA mapping errors");
5538 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5539 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5540 "Tx queue 0 not empty");
5542 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5543 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5544 "Tx queue 1 not empty");
5546 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5547 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
5548 "Rx packets");
5550 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5551 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
5552 "Rx CRC errors");
5554 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5555 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
5556 "Rx PHY errors");
5558 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5559 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
5560 "Rx false CCAs");
5562 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5563 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
5564 "Rx PLCP errors");
5566 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5567 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
5568 "Rx duplicate packets");
5570 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5571 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
5572 "Rx FIFO overflows");
5575 static device_method_t rt2860_dev_methods[] =
5577 DEVMETHOD(device_probe, rt2860_probe),
5578 DEVMETHOD(device_attach, rt2860_attach),
5579 DEVMETHOD(device_detach, rt2860_detach),
5580 DEVMETHOD(device_shutdown, rt2860_shutdown),
5581 DEVMETHOD(device_suspend, rt2860_suspend),
5582 DEVMETHOD(device_resume, rt2860_resume),
5583 { 0, 0 }
5586 static driver_t rt2860_driver =
5588 "rt2860",
5589 rt2860_dev_methods,
5590 sizeof(struct rt2860_softc)
5593 static devclass_t rt2860_dev_class;
5595 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
5597 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
5598 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);