ADDBA request debugging: added bufsize value to output
[ralink_drivers/rt2860_fbsd8.git] / rt2860.c
blobc535f4d0bbdb037b76733fb7a5b14a238f018868
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_ACK_SIZE 14
52 #define IEEE80211_HAS_ADDR4(wh) \
53 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
55 #define RT2860_MS(_v, _f) (((_v) & _f) >> _f##_S)
56 #define RT2860_SM(_v, _f) (((_v) << _f##_S) & _f)
58 #define RT2860_TX_WATCHDOG_TIMEOUT 5
61 * Data structures and types
64 struct rt2860_pci_ident
66 uint16_t vendor;
67 uint16_t device;
68 const char *name;
72 * Static function prototypes
75 static int rt2860_probe(device_t dev);
77 static int rt2860_attach(device_t dev);
79 static int rt2860_detach(device_t dev);
81 static int rt2860_shutdown(device_t dev);
83 static int rt2860_suspend(device_t dev);
85 static int rt2860_resume(device_t dev);
87 static void rt2860_init_channels(struct rt2860_softc *sc);
89 static void rt2860_init_channels_ht40(struct rt2860_softc *sc);
91 static void rt2860_init_locked(void *priv);
93 static void rt2860_init(void *priv);
95 static int rt2860_init_bbp(struct rt2860_softc *sc);
97 static void rt2860_stop_locked(void *priv);
99 static void rt2860_stop(void *priv);
101 static void rt2860_start(struct ifnet *ifp);
103 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
105 static struct ieee80211vap *rt2860_vap_create(struct ieee80211com *ic,
106 const char name[IFNAMSIZ], int unit, int opmode, int flags,
107 const uint8_t bssid[IEEE80211_ADDR_LEN],
108 const uint8_t mac[IEEE80211_ADDR_LEN]);
110 static void rt2860_vap_delete(struct ieee80211vap *vap);
112 static int rt2860_vap_reset(struct ieee80211vap *vap, u_long cmd);
114 static int rt2860_vap_newstate(struct ieee80211vap *vap,
115 enum ieee80211_state nstate, int arg);
117 static void rt2860_vap_key_update_begin(struct ieee80211vap *vap);
119 static void rt2860_vap_key_update_end(struct ieee80211vap *vap);
121 static int rt2860_vap_key_set(struct ieee80211vap *vap,
122 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN]);
124 static int rt2860_vap_key_delete(struct ieee80211vap *vap,
125 const struct ieee80211_key *k);
127 static void rt2860_vap_update_beacon(struct ieee80211vap *vap, int what);
129 static int rt2860_media_change(struct ifnet *ifp);
131 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211vap *vap,
132 const uint8_t mac[IEEE80211_ADDR_LEN]);
134 static int rt2860_setregdomain(struct ieee80211com *ic,
135 struct ieee80211_regdomain *reg,
136 int nchans, struct ieee80211_channel chans[]);
138 static void rt2860_getradiocaps(struct ieee80211com *ic,
139 int maxchans, int *nchans, struct ieee80211_channel chans[]);
141 static void rt2860_scan_start(struct ieee80211com *ic);
143 static void rt2860_scan_end(struct ieee80211com *ic);
145 static void rt2860_set_channel(struct ieee80211com *ic);
147 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew);
149 static void rt2860_updateslot(struct ifnet *ifp);
151 static void rt2860_update_promisc(struct ifnet *ifp);
153 static void rt2860_update_mcast(struct ifnet *ifp);
155 static int rt2860_wme_update(struct ieee80211com *ic);
157 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
158 const struct ieee80211_bpf_params *params);
160 static int rt2860_recv_action(struct ieee80211_node *ni,
161 const struct ieee80211_frame *wh,
162 const uint8_t *frm, const uint8_t *efrm);
164 static int rt2860_send_action(struct ieee80211_node *ni,
165 int cat, int act, void *sa);
167 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
169 static void rt2860_periodic(void *arg);
171 static void rt2860_tx_watchdog(void *arg);
173 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
174 const uint8_t *bssid);
176 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
177 const uint8_t *addr);
179 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc);
181 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc);
183 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc);
185 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc);
187 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc);
189 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc);
191 static void rt2860_asic_update_txpower(struct rt2860_softc *sc);
193 static void rt2860_asic_update_promisc(struct rt2860_softc *sc);
195 static void rt2860_asic_updateprot(struct rt2860_softc *sc);
197 static void rt2860_asic_updateslot(struct rt2860_softc *sc);
199 static void rt2860_asic_wme_update(struct rt2860_softc *sc);
201 static int rt2860_asic_update_beacon(struct rt2860_softc *sc,
202 struct ieee80211vap *vap);
204 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc);
206 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi);
208 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
209 const struct rt2860_rxwi *rxwi);
211 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
212 uint8_t rssi, uint8_t rxpath);
214 static uint8_t rt2860_rate2mcs(uint8_t rate);
216 static int rt2860_tx_mgmt(struct rt2860_softc *sc,
217 struct mbuf *m, struct ieee80211_node *ni, int qid);
219 static int rt2860_tx_data(struct rt2860_softc *sc,
220 struct mbuf *m, struct ieee80211_node *ni, int qid);
222 static int rt2860_tx_raw(struct rt2860_softc *sc,
223 struct mbuf *m, struct ieee80211_node *ni,
224 const struct ieee80211_bpf_params *params);
226 static void rt2860_intr(void *arg);
228 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc);
230 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc);
232 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc);
234 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc);
236 static void rt2860_rx_intr(struct rt2860_softc *sc);
238 static void rt2860_rx_delay_intr(struct rt2860_softc *sc);
240 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid);
242 static void rt2860_tx_delay_intr(struct rt2860_softc *sc);
244 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc);
246 static void rt2860_tbtt_intr(struct rt2860_softc *sc);
248 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc);
250 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc);
252 static void rt2860_gp_timer_intr(struct rt2860_softc *sc);
254 static void rt2860_rx_done_task(void *context, int pending);
256 static void rt2860_tx_done_task(void *context, int pending);
258 static void rt2860_fifo_sta_full_task(void *context, int pending);
260 static void rt2860_periodic_task(void *context, int pending);
262 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit);
264 static void rt2860_tx_eof(struct rt2860_softc *sc,
265 struct rt2860_softc_tx_ring *ring);
267 static void rt2860_update_stats(struct rt2860_softc *sc);
269 static void rt2860_watchdog(struct rt2860_softc *sc);
271 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc);
273 static void rt2860_update_raw_counters(struct rt2860_softc *sc);
275 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask);
277 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask);
279 static int rt2860_txrx_enable(struct rt2860_softc *sc);
281 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
282 struct rt2860_softc_rx_ring *ring);
284 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
285 struct rt2860_softc_rx_ring *ring);
287 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
288 struct rt2860_softc_rx_ring *ring);
290 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
291 struct rt2860_softc_tx_ring *ring, int qid);
293 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
294 struct rt2860_softc_tx_ring *ring);
296 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
297 struct rt2860_softc_tx_ring *ring);
299 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
300 int nseg, int error);
302 static void rt2860_sysctl_attach(struct rt2860_softc *sc);
305 * Static variables
308 static const struct rt2860_pci_ident rt2860_pci_ids[] =
310 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCI, "Ralink RT2860 PCI" },
311 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCIe, "Ralink RT2860 PCIe" },
312 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760_PCI, "Ralink RT2760 PCI" },
313 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790_PCIe, "Ralink RT2790 PCIe" },
314 { 0, 0, NULL }
317 static const struct
319 uint32_t reg;
320 uint32_t val;
321 } rt2860_def_mac[] =
323 { RT2860_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
324 { RT2860_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
325 { RT2860_REG_LEGACY_BASIC_RATE, 0x0000013f },
326 { RT2860_REG_HT_BASIC_RATE, 0x00008003 },
327 { RT2860_REG_SYS_CTRL, 0x00000000 },
328 { RT2860_REG_RX_FILTER_CFG, 0x00017f97 },
329 { RT2860_REG_BKOFF_SLOT_CFG, 0x00000209 },
330 { RT2860_REG_TX_SW_CFG0, 0x00000000 },
331 { RT2860_REG_TX_SW_CFG1, 0x00080606 },
332 { RT2860_REG_TX_LINK_CFG, 0x00001020 },
333 { RT2860_REG_TX_TIMEOUT_CFG, 0x000a2090 },
334 { RT2860_REG_MAX_LEN_CFG, (1 << 12) | RT2860_MAX_AGG_SIZE },
335 { RT2860_REG_LED_CFG, 0x7f031e46 },
336 { RT2860_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
337 { RT2860_REG_TX_RTY_CFG, 0x47d01f0f },
338 { RT2860_REG_AUTO_RSP_CFG, 0x00000013 },
339 { RT2860_REG_TX_CCK_PROT_CFG, 0x05740003 },
340 { RT2860_REG_TX_OFDM_PROT_CFG, 0x05740003 },
341 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f44084 },
342 { RT2860_REG_SCHDMA_WPDMA_GLO_CFG, 0x00000030 },
343 { RT2860_REG_TX_GF20_PROT_CFG, 0x01744004 },
344 { RT2860_REG_TX_GF40_PROT_CFG, 0x03f44084 },
345 { RT2860_REG_TX_MM20_PROT_CFG, 0x01744004 },
346 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f54084 },
347 { RT2860_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
348 { RT2860_REG_TX_RTS_CFG, 0x00092b20 },
349 { RT2860_REG_TX_EXP_ACK_TIME, 0x002400ca },
350 { RT2860_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
351 { RT2860_REG_XIFS_TIME_CFG, 0x33a41010 },
352 { RT2860_REG_PWR_PIN_CFG, 0x00000003 },
353 { RT2860_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
354 { RT2860_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
355 { RT2860_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
358 #define RT2860_DEF_MAC_SIZE (sizeof(rt2860_def_mac) / sizeof(rt2860_def_mac[0]))
360 static const struct
362 uint8_t reg;
363 uint8_t val;
364 } rt2860_def_bbp[] =
366 { 65, 0x2c },
367 { 66, 0x38 },
368 { 69, 0x12 },
369 { 70, 0x0a },
370 { 73, 0x10 },
371 { 81, 0x37 },
372 { 82, 0x62 },
373 { 83, 0x6a },
374 { 84, 0x99 },
375 { 86, 0x00 },
376 { 91, 0x04 },
377 { 92, 0x00 },
378 { 103, 0x00 },
379 { 105, 0x05 },
380 { 106, 0x35 },
383 #define RT2860_DEF_BBP_SIZE (sizeof(rt2860_def_bbp) / sizeof(rt2860_def_bbp[0]))
385 SYSCTL_NODE(_hw, OID_AUTO, rt2860, CTLFLAG_RD, 0, "RT2860 driver parameters");
387 #ifdef RT2860_DEBUG
388 static int rt2860_debug = 0;
389 SYSCTL_INT(_hw_rt2860, OID_AUTO, debug, CTLFLAG_RW, &rt2860_debug, 0, "rt2860 debug level");
390 TUNABLE_INT("hw.rt2860.debug", &rt2860_debug);
391 #endif
394 * rt2860_probe
396 static int rt2860_probe(device_t dev)
398 const struct rt2860_pci_ident *ident;
400 for (ident = rt2860_pci_ids; ident->name != NULL; ident++)
402 if (pci_get_vendor(dev) == ident->vendor &&
403 pci_get_device(dev) == ident->device)
405 device_set_desc(dev, ident->name);
406 return 0;
410 return ENXIO;
414 * rt2860_attach
416 static int rt2860_attach(device_t dev)
418 struct rt2860_softc *sc;
419 struct ifnet *ifp;
420 struct ieee80211com *ic;
421 int error, ntries, i;
423 sc = device_get_softc(dev);
425 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
427 printf("%s: chip is in D%d power mode, setting to D0\n",
428 device_get_nameunit(dev), pci_get_powerstate(dev));
429 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
432 /* enable bus-mastering */
434 pci_enable_busmaster(dev);
436 sc->dev = dev;
438 mtx_init(&sc->lock, device_get_nameunit(dev),
439 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
441 sc->mem_rid = PCIR_BAR(0);
442 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
443 &sc->mem_rid, RF_ACTIVE);
444 if (sc->mem == NULL)
446 printf("%s: could not allocate memory resource\n",
447 device_get_nameunit(dev));
448 error = ENXIO;
449 goto fail;
452 sc->bst = rman_get_bustag(sc->mem);
453 sc->bsh = rman_get_bushandle(sc->mem);
455 sc->irq_rid = 0;
456 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
457 &sc->irq_rid, RF_ACTIVE | RF_SHAREABLE);
458 if (sc->irq == NULL)
460 printf("%s: could not allocate interrupt resource\n",
461 device_get_nameunit(dev));
462 error = ENXIO;
463 goto fail;
466 #ifdef RT2860_DEBUG
467 sc->debug = rt2860_debug;
469 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
470 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
471 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2860 debug level");
472 #endif
474 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
475 "%s: attaching\n",
476 device_get_nameunit(sc->dev));
478 /* wait for NIC to initialize */
480 for (ntries = 0; ntries < 100; ntries++)
482 sc->mac_rev = rt2860_io_mac_read(sc, RT2860_REG_MAC_CSR0);
483 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
484 break;
486 DELAY(10);
489 if (ntries == 100)
491 printf("%s: timeout waiting for NIC to initialize\n",
492 device_get_nameunit(dev));
493 error = EIO;
494 goto fail;
497 rt2860_read_eeprom(sc);
499 printf("%s: MAC/BBP RT2860 (rev 0x%08x), RF %s\n",
500 device_get_nameunit(sc->dev), sc->mac_rev,
501 rt2860_rf_name(sc->rf_rev));
503 /* allocate Tx and Rx rings */
505 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
507 error = rt2860_alloc_tx_ring(sc, &sc->tx_ring[i], i);
508 if (error != 0)
510 printf("%s: could not allocate Tx ring #%d\n",
511 device_get_nameunit(sc->dev), i);
512 goto fail;
516 sc->tx_ring_mgtqid = 5;
518 error = rt2860_alloc_rx_ring(sc, &sc->rx_ring);
519 if (error != 0)
521 printf("%s: could not allocate Rx ring\n",
522 device_get_nameunit(sc->dev));
523 goto fail;
526 callout_init(&sc->periodic_ch, 0);
527 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
529 ifp = sc->ifp = if_alloc(IFT_IEEE80211);
530 if (ifp == NULL)
532 printf("%s: could not if_alloc()\n",
533 device_get_nameunit(sc->dev));
534 error = ENOMEM;
535 goto fail;
538 ifp->if_softc = sc;
540 if_initname(ifp, "rt2860", device_get_unit(sc->dev));
542 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
544 ifp->if_init = rt2860_init;
545 ifp->if_ioctl = rt2860_ioctl;
546 ifp->if_start = rt2860_start;
548 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
549 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
550 IFQ_SET_READY(&ifp->if_snd);
552 ic = ifp->if_l2com;
554 ic->ic_ifp = ifp;
556 ic->ic_phytype = IEEE80211_T_HT;
557 ic->ic_opmode = IEEE80211_M_STA;
559 ic->ic_caps = IEEE80211_C_MONITOR |
560 IEEE80211_C_IBSS |
561 IEEE80211_C_STA |
562 IEEE80211_C_AHDEMO |
563 IEEE80211_C_HOSTAP |
564 IEEE80211_C_WDS |
565 IEEE80211_C_MBSS |
566 /* IEEE80211_C_BGSCAN | */
567 IEEE80211_C_TXPMGT |
568 IEEE80211_C_SHPREAMBLE |
569 IEEE80211_C_SHSLOT |
570 IEEE80211_C_TXFRAG |
571 IEEE80211_C_BURST |
572 IEEE80211_C_WME |
573 IEEE80211_C_WPA;
575 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP |
576 IEEE80211_CRYPTO_TKIP |
577 IEEE80211_CRYPTO_TKIPMIC |
578 IEEE80211_CRYPTO_AES_CCM;
580 ic->ic_htcaps = IEEE80211_HTC_HT |
581 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
582 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
583 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
584 IEEE80211_HTCAP_GREENFIELD | /* HT greenfield */
585 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
586 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
587 IEEE80211_HTCAP_DSSSCCK40; /* HT 40MHz DSSS/CCK modulation */
589 /* spatial streams */
591 if (sc->nrxpath == 2)
592 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
593 else if (sc->nrxpath == 3)
594 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
595 else
596 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
598 if (sc->ntxpath > 1)
599 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
601 /* delayed BA */
603 if (sc->mac_rev != 0x28600100)
604 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
606 /* init channels */
608 ic->ic_nchans = 0;
610 rt2860_init_channels(sc);
612 rt2860_init_channels_ht40(sc);
614 ieee80211_ifattach(ic, sc->mac_addr);
616 ic->ic_vap_create = rt2860_vap_create;
617 ic->ic_vap_delete = rt2860_vap_delete;
619 ic->ic_node_alloc = rt2860_node_alloc;
620 ic->ic_setregdomain = rt2860_setregdomain;
621 ic->ic_getradiocaps = rt2860_getradiocaps;
622 ic->ic_scan_start = rt2860_scan_start;
623 ic->ic_scan_end = rt2860_scan_end;
624 ic->ic_set_channel = rt2860_set_channel;
625 ic->ic_newassoc = rt2860_newassoc;
626 ic->ic_updateslot = rt2860_updateslot;
627 ic->ic_update_promisc = rt2860_update_promisc;
628 ic->ic_update_mcast = rt2860_update_mcast;
629 ic->ic_wme.wme_update = rt2860_wme_update;
630 ic->ic_raw_xmit = rt2860_raw_xmit;
632 sc->recv_action = ic->ic_recv_action;
633 ic->ic_recv_action = rt2860_recv_action;
635 sc->send_action = ic->ic_send_action;
636 ic->ic_send_action = rt2860_send_action;
638 /* hardware requires padding between 802.11 frame header and body */
640 ic->ic_flags |= IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
642 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
644 ieee80211_radiotap_attach(ic,
645 &sc->txtap.ihdr, sizeof(sc->txtap),
646 RT2860_SOFTC_TX_RADIOTAP_PRESENT,
647 &sc->rxtap.ihdr, sizeof(sc->rxtap),
648 RT2860_SOFTC_RX_RADIOTAP_PRESENT);
650 /* init task queue */
652 TASK_INIT(&sc->rx_done_task, 0, rt2860_rx_done_task, sc);
653 TASK_INIT(&sc->tx_done_task, 0, rt2860_tx_done_task, sc);
654 TASK_INIT(&sc->fifo_sta_full_task, 0, rt2860_fifo_sta_full_task, sc);
655 TASK_INIT(&sc->periodic_task, 0, rt2860_periodic_task, sc);
657 sc->rx_process_limit = 100;
659 sc->taskqueue = taskqueue_create("rt2860_taskq", M_NOWAIT,
660 taskqueue_thread_enqueue, &sc->taskqueue);
662 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
663 device_get_nameunit(sc->dev));
665 rt2860_sysctl_attach(sc);
667 if (bootverbose)
668 ieee80211_announce(ic);
670 /* set up interrupt */
672 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
673 NULL, rt2860_intr, sc, &sc->irqh);
674 if (error != 0)
676 printf("%s: could not set up interrupt\n",
677 device_get_nameunit(dev));
678 goto fail;
681 return 0;
683 fail:
685 /* free Tx and Rx rings */
687 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
688 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
690 rt2860_free_rx_ring(sc, &sc->rx_ring);
692 mtx_destroy(&sc->lock);
694 if (sc->mem != NULL)
695 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
697 if (sc->irq != NULL)
698 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
700 return error;
704 * rt2860_detach
706 static int rt2860_detach(device_t dev)
708 struct rt2860_softc *sc;
709 struct ifnet *ifp;
710 struct ieee80211com *ic;
711 int i;
713 sc = device_get_softc(dev);
714 ifp = sc->ifp;
715 ic = ifp->if_l2com;
717 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
718 "%s: detaching\n",
719 device_get_nameunit(sc->dev));
721 RT2860_SOFTC_LOCK(sc);
723 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
725 callout_stop(&sc->periodic_ch);
726 callout_stop(&sc->tx_watchdog_ch);
728 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
729 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
730 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
731 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
733 /* free Tx and Rx rings */
735 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
736 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
738 rt2860_free_rx_ring(sc, &sc->rx_ring);
740 RT2860_SOFTC_UNLOCK(sc);
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 ifnet *ifp;
828 struct ieee80211com *ic;
829 struct ieee80211_channel *c;
830 int i, flags;
832 ifp = sc->ifp;
833 ic = ifp->if_l2com;
835 /* set supported channels for 2GHz band */
837 for (i = 1; i <= 14; i++)
839 c = &ic->ic_channels[ic->ic_nchans++];
840 flags = IEEE80211_CHAN_B;
842 c->ic_freq = ieee80211_ieee2mhz(i, flags);
843 c->ic_ieee = i;
844 c->ic_flags = flags;
846 c = &ic->ic_channels[ic->ic_nchans++];
847 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
849 c->ic_freq = ieee80211_ieee2mhz(i, flags);
850 c->ic_ieee = i;
851 c->ic_flags = flags;
853 c = &ic->ic_channels[ic->ic_nchans++];
854 flags = IEEE80211_CHAN_G;
856 c->ic_freq = ieee80211_ieee2mhz(i, flags);
857 c->ic_ieee = i;
858 c->ic_flags = flags;
860 c = &ic->ic_channels[ic->ic_nchans++];
861 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
863 c->ic_freq = ieee80211_ieee2mhz(i, flags);
864 c->ic_ieee = i;
865 c->ic_flags = flags;
868 /* set supported channels for 5GHz band */
870 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
871 sc->rf_rev == RT2860_EEPROM_RF_2750)
873 for (i = 36; i <= 64; i += 4)
875 c = &ic->ic_channels[ic->ic_nchans++];
876 flags = IEEE80211_CHAN_A;
878 c->ic_freq = ieee80211_ieee2mhz(i, flags);
879 c->ic_ieee = i;
880 c->ic_flags = flags;
882 c = &ic->ic_channels[ic->ic_nchans++];
883 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
885 c->ic_freq = ieee80211_ieee2mhz(i, flags);
886 c->ic_ieee = i;
887 c->ic_flags = flags;
890 for (i = 100; i <= 140; i += 4)
892 c = &ic->ic_channels[ic->ic_nchans++];
893 flags = IEEE80211_CHAN_A;
895 c->ic_freq = ieee80211_ieee2mhz(i, flags);
896 c->ic_ieee = i;
897 c->ic_flags = flags;
899 c = &ic->ic_channels[ic->ic_nchans++];
900 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
902 c->ic_freq = ieee80211_ieee2mhz(i, flags);
903 c->ic_ieee = i;
904 c->ic_flags = flags;
907 for (i = 149; i <= 165; i += 4)
909 c = &ic->ic_channels[ic->ic_nchans++];
910 flags = IEEE80211_CHAN_A;
912 c->ic_freq = ieee80211_ieee2mhz(i, flags);
913 c->ic_ieee = i;
914 c->ic_flags = flags;
916 c = &ic->ic_channels[ic->ic_nchans++];
917 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
919 c->ic_freq = ieee80211_ieee2mhz(i, flags);
920 c->ic_ieee = i;
921 c->ic_flags = flags;
927 * rt2860_init_channels_ht40
929 static void rt2860_init_channels_ht40(struct rt2860_softc *sc)
931 struct ifnet *ifp;
932 struct ieee80211com *ic;
933 struct ieee80211_channel *c, *cent, *ext;
934 int i, flags;
936 ifp = sc->ifp;
937 ic = ifp->if_l2com;
939 /* set supported channels for 2GHz band */
941 for (i = 1; i <= 14; i++)
943 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
945 /* find the center channel */
947 cent = ieee80211_find_channel_byieee(ic, i,
948 flags & ~IEEE80211_CHAN_HT);
949 if (cent == NULL)
951 printf("%s: skip channel %d, could not find center channel\n",
952 device_get_nameunit(sc->dev), i);
953 continue;
956 /* find the extension channel */
958 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
959 flags & ~IEEE80211_CHAN_HT);
960 if (ext == NULL)
962 printf("%s: skip channel %d, could not find extension channel\n",
963 device_get_nameunit(sc->dev), i);
964 continue;
967 c = &ic->ic_channels[ic->ic_nchans++];
969 *c = *cent;
970 c->ic_extieee = ext->ic_ieee;
971 c->ic_flags &= ~IEEE80211_CHAN_HT;
972 c->ic_flags |= IEEE80211_CHAN_HT40U;
974 c = &ic->ic_channels[ic->ic_nchans++];
976 *c = *ext;
977 c->ic_extieee = cent->ic_ieee;
978 c->ic_flags &= ~IEEE80211_CHAN_HT;
979 c->ic_flags |= IEEE80211_CHAN_HT40D;
982 /* set supported channels for 5GHz band */
984 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
985 sc->rf_rev == RT2860_EEPROM_RF_2750)
987 for (i = 36; i <= 64; i += 4)
989 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
991 /* find the center channel */
993 cent = ieee80211_find_channel_byieee(ic, i,
994 flags & ~IEEE80211_CHAN_HT);
995 if (cent == NULL)
997 printf("%s: skip channel %d, could not find center channel\n",
998 device_get_nameunit(sc->dev), i);
999 continue;
1002 /* find the extension channel */
1004 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1005 flags & ~IEEE80211_CHAN_HT);
1006 if (ext == NULL)
1008 printf("%s: skip channel %d, could not find extension channel\n",
1009 device_get_nameunit(sc->dev), i);
1010 continue;
1013 c = &ic->ic_channels[ic->ic_nchans++];
1015 *c = *cent;
1016 c->ic_extieee = ext->ic_ieee;
1017 c->ic_flags &= ~IEEE80211_CHAN_HT;
1018 c->ic_flags |= IEEE80211_CHAN_HT40U;
1020 c = &ic->ic_channels[ic->ic_nchans++];
1022 *c = *ext;
1023 c->ic_extieee = cent->ic_ieee;
1024 c->ic_flags &= ~IEEE80211_CHAN_HT;
1025 c->ic_flags |= IEEE80211_CHAN_HT40D;
1028 for (i = 100; i <= 140; i += 4)
1030 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1032 /* find the center channel */
1034 cent = ieee80211_find_channel_byieee(ic, i,
1035 flags & ~IEEE80211_CHAN_HT);
1036 if (cent == NULL)
1038 printf("%s: skip channel %d, could not find center channel\n",
1039 device_get_nameunit(sc->dev), i);
1040 continue;
1043 /* find the extension channel */
1045 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1046 flags & ~IEEE80211_CHAN_HT);
1047 if (ext == NULL)
1049 printf("%s: skip channel %d, could not find extension channel\n",
1050 device_get_nameunit(sc->dev), i);
1051 continue;
1054 c = &ic->ic_channels[ic->ic_nchans++];
1056 *c = *cent;
1057 c->ic_extieee = ext->ic_ieee;
1058 c->ic_flags &= ~IEEE80211_CHAN_HT;
1059 c->ic_flags |= IEEE80211_CHAN_HT40U;
1061 c = &ic->ic_channels[ic->ic_nchans++];
1063 *c = *ext;
1064 c->ic_extieee = cent->ic_ieee;
1065 c->ic_flags &= ~IEEE80211_CHAN_HT;
1066 c->ic_flags |= IEEE80211_CHAN_HT40D;
1069 for (i = 149; i <= 165; i += 4)
1071 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1073 /* find the center channel */
1075 cent = ieee80211_find_channel_byieee(ic, i,
1076 flags & ~IEEE80211_CHAN_HT);
1077 if (cent == NULL)
1079 printf("%s: skip channel %d, could not find center channel\n",
1080 device_get_nameunit(sc->dev), i);
1081 continue;
1084 /* find the extension channel */
1086 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1087 flags & ~IEEE80211_CHAN_HT);
1088 if (ext == NULL)
1090 printf("%s: skip channel %d, could not find extension channel\n",
1091 device_get_nameunit(sc->dev), i);
1092 continue;
1095 c = &ic->ic_channels[ic->ic_nchans++];
1097 *c = *cent;
1098 c->ic_extieee = ext->ic_ieee;
1099 c->ic_flags &= ~IEEE80211_CHAN_HT;
1100 c->ic_flags |= IEEE80211_CHAN_HT40U;
1102 c = &ic->ic_channels[ic->ic_nchans++];
1104 *c = *ext;
1105 c->ic_extieee = cent->ic_ieee;
1106 c->ic_flags &= ~IEEE80211_CHAN_HT;
1107 c->ic_flags |= IEEE80211_CHAN_HT40D;
1113 * rt2860_init_locked
1115 static void rt2860_init_locked(void *priv)
1117 struct rt2860_softc *sc;
1118 struct ifnet *ifp;
1119 struct ieee80211com *ic;
1120 int error, i, ntries;
1121 uint32_t tmp, stacnt[6];
1123 sc = priv;
1124 ifp = sc->ifp;
1125 ic = ifp->if_l2com;
1127 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1128 "%s: initializing\n",
1129 device_get_nameunit(sc->dev));
1131 RT2860_SOFTC_ASSERT_LOCKED(sc);
1133 if (!(sc->flags & RT2860_SOFTC_FLAGS_UCODE_LOADED))
1135 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1136 "%s: loading 8051 microcode\n",
1137 device_get_nameunit(sc->dev));
1139 error = rt2860_io_mcu_load_ucode(sc, rt2860_ucode, sizeof(rt2860_ucode));
1140 if (error != 0)
1142 printf("%s: could not load 8051 microcode\n",
1143 device_get_nameunit(sc->dev));
1144 goto fail;
1147 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1148 "%s: 8051 microcode was successfully loaded\n",
1149 device_get_nameunit(sc->dev));
1151 sc->flags |= RT2860_SOFTC_FLAGS_UCODE_LOADED;
1154 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x2);
1156 /* disable DMA engine */
1158 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1160 tmp &= 0xff0;
1161 tmp |= RT2860_REG_TX_WB_DDONE;
1163 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1165 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, 0xffffffff);
1167 /* PBF hardware reset */
1169 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1170 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1172 /* wait while DMA engine is busy */
1174 for (ntries = 0; ntries < 100; ntries++)
1176 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1177 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1178 break;
1180 DELAY(1000);
1183 if (ntries == 100)
1185 printf("%s: timeout waiting for DMA engine\n",
1186 device_get_nameunit(sc->dev));
1187 goto fail;
1190 tmp &= 0xff0;
1191 tmp |= RT2860_REG_TX_WB_DDONE;
1193 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1195 /* reset Rx and Tx rings */
1197 tmp = RT2860_REG_RST_IDX_RX |
1198 RT2860_REG_RST_IDX_TX_MGMT |
1199 RT2860_REG_RST_IDX_TX_HCCA |
1200 RT2860_REG_RST_IDX_TX_AC3 |
1201 RT2860_REG_RST_IDX_TX_AC2 |
1202 RT2860_REG_RST_IDX_TX_AC1 |
1203 RT2860_REG_RST_IDX_TX_AC0;
1205 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, tmp);
1207 /* PBF hardware reset */
1209 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1210 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1212 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x3);
1214 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1215 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1216 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1218 /* init Tx power per rate */
1220 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
1222 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1223 continue;
1225 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i),
1226 sc->txpow_rate_20mhz[i]);
1229 for (i = 0; i < RT2860_DEF_MAC_SIZE; i++)
1230 rt2860_io_mac_write(sc, rt2860_def_mac[i].reg,
1231 rt2860_def_mac[i].val);
1233 /* wait while MAC is busy */
1235 for (ntries = 0; ntries < 100; ntries++)
1237 if (!(rt2860_io_mac_read(sc, RT2860_REG_STATUS_CFG) &
1238 (RT2860_REG_STATUS_TX_BUSY | RT2860_REG_STATUS_RX_BUSY)))
1239 break;
1241 DELAY(1000);
1244 if (ntries == 100)
1246 printf("%s: timeout waiting for MAC\n",
1247 device_get_nameunit(sc->dev));
1248 goto fail;
1251 /* clear Host to MCU mailbox */
1253 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX_BBP_AGENT, 0);
1254 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX, 0);
1256 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_BOOT,
1257 RT2860_REG_H2M_TOKEN_NO_INTR, 0);
1259 DELAY(1000);
1261 error = rt2860_init_bbp(sc);
1262 if (error != 0)
1263 goto fail;
1265 /* set up maximum buffer sizes */
1267 tmp = (1 << 12) | RT2860_MAX_AGG_SIZE;
1269 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1271 if (sc->mac_rev == 0x28720200)
1273 /* set max. PSDU length from 16K to 32K bytes */
1275 tmp = rt2860_io_mac_read(sc, RT2860_REG_MAX_LEN_CFG);
1277 tmp &= ~(3 << 12);
1278 tmp |= (2 << 12);
1280 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1283 if (sc->mac_rev >= 0x28720200 && sc->mac_rev < 0x30700200)
1285 tmp = rt2860_io_mac_read(sc, RT2860_REG_MAX_LEN_CFG);
1287 tmp &= 0xfff;
1288 tmp |= 0x2000;
1290 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1293 /* set mac address */
1295 rt2860_asic_set_macaddr(sc, IF_LLADDR(ifp));
1297 /* clear statistic registers */
1299 rt2860_io_mac_read_multi(sc, RT2860_REG_RX_STA_CNT0,
1300 stacnt, sizeof(stacnt));
1302 /* set RTS threshold */
1304 rt2860_asic_update_rtsthreshold(sc);
1306 /* set Tx power */
1308 rt2860_asic_update_txpower(sc);
1310 /* set up protection mode */
1312 rt2860_asic_updateprot(sc);
1314 /* clear key tables */
1316 rt2860_asic_clear_keytables(sc);
1318 taskqueue_unblock(sc->taskqueue);
1320 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
1322 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1323 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
1325 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1327 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
1328 sc->tx_ring[i].desc_phys_addr);
1329 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
1330 RT2860_SOFTC_TX_RING_DESC_COUNT);
1331 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
1334 /* init Rx ring */
1336 rt2860_reset_rx_ring(sc, &sc->rx_ring);
1338 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
1339 sc->rx_ring.desc_phys_addr);
1340 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
1341 RT2860_SOFTC_RX_RING_DATA_COUNT);
1342 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
1343 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
1345 /* wait while DMA engine is busy */
1347 for (ntries = 0; ntries < 100; ntries++)
1349 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1350 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1351 break;
1353 DELAY(1000);
1356 if (ntries == 100)
1358 printf("%s: timeout waiting for DMA engine\n",
1359 device_get_nameunit(sc->dev));
1360 goto fail;
1363 tmp &= 0xff0;
1364 tmp |= RT2860_REG_TX_WB_DDONE;
1366 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1368 /* disable interrupts mitigation */
1370 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_DELAY_INT_CFG, 0);
1372 /* send LEDs operating mode to microcontroller */
1374 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED1,
1375 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1376 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED2,
1377 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1378 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED3,
1379 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1381 /* turn radio LED on */
1383 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON);
1385 /* write vendor-specific BBP values (from EEPROM) */
1387 for (i = 0; i < RT2860_SOFTC_BBP_EEPROM_COUNT; i++)
1389 if (sc->bbp_eeprom[i].reg == 0x00 ||
1390 sc->bbp_eeprom[i].reg == 0xff)
1391 continue;
1393 rt2860_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1394 sc->bbp_eeprom[i].val);
1398 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_GPIO_CTRL_CFG);
1399 if (tmp & (1 << 2))
1401 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_SLEEP,
1402 RT2860_REG_H2M_TOKEN_RADIOOFF, 0x02ff);
1403 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_RADIOOFF);
1405 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_WAKEUP,
1406 RT2860_REG_H2M_TOKEN_WAKEUP, 0);
1407 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_WAKEUP);
1411 /* disable non-existing Rx chains */
1413 tmp = rt2860_io_bbp_read(sc, 3);
1415 tmp &= ~((1 << 4) | (1 << 3));
1417 if (sc->nrxpath == 3)
1418 tmp |= (1 << 4);
1419 else if (sc->nrxpath == 2)
1420 tmp |= (1 << 3);
1422 rt2860_io_bbp_write(sc, 3, tmp);
1424 /* disable non-existing Tx chains */
1426 tmp = rt2860_io_bbp_read(sc, 1);
1428 tmp &= ~((1 << 4) | (1 << 3));
1430 if (sc->ntxpath == 2)
1431 tmp |= (1 << 4);
1433 rt2860_io_bbp_write(sc, 1, tmp);
1435 /* set current channel */
1437 rt2860_rf_set_chan(sc, ic->ic_curchan);
1439 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1440 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1441 (48 << 16) | 96);
1443 if ((sc->mac_rev & 0xffff) != 0x0101)
1444 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1446 /* clear pending interrupts */
1448 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1450 /* enable interrupts */
1452 tmp = RT2860_REG_INT_TX_COHERENT |
1453 RT2860_REG_INT_RX_COHERENT |
1454 RT2860_REG_INT_GP_TIMER |
1455 RT2860_REG_INT_AUTO_WAKEUP |
1456 RT2860_REG_INT_FIFO_STA_FULL |
1458 RT2860_REG_INT_PRE_TBTT |
1459 RT2860_REG_INT_TBTT |
1461 RT2860_REG_INT_TXRX_COHERENT |
1462 RT2860_REG_INT_MCU_CMD |
1463 RT2860_REG_INT_TX_MGMT_DONE |
1464 RT2860_REG_INT_TX_HCCA_DONE |
1465 RT2860_REG_INT_TX_AC3_DONE |
1466 RT2860_REG_INT_TX_AC2_DONE |
1467 RT2860_REG_INT_TX_AC1_DONE |
1468 RT2860_REG_INT_TX_AC0_DONE |
1469 RT2860_REG_INT_RX_DONE;
1471 sc->intr_enable_mask = tmp;
1473 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1475 if (rt2860_txrx_enable(sc) != 0)
1476 goto fail;
1478 /* clear garbage interrupts */
1480 tmp = rt2860_io_mac_read(sc, 0x1300);
1482 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1483 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1485 sc->periodic_round = 0;
1487 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1489 return;
1491 fail:
1493 rt2860_stop_locked(sc);
1497 * rt2860_init
1499 static void rt2860_init(void *priv)
1501 struct rt2860_softc *sc;
1503 sc = priv;
1505 RT2860_SOFTC_LOCK(sc);
1507 rt2860_init_locked(sc);
1509 RT2860_SOFTC_UNLOCK(sc);
1513 * rt2860_init_bbp
1515 static int rt2860_init_bbp(struct rt2860_softc *sc)
1517 int ntries, i;
1518 uint8_t tmp;
1520 for (ntries = 0; ntries < 20; ntries++)
1522 tmp = rt2860_io_bbp_read(sc, 0);
1523 if (tmp != 0x00 && tmp != 0xff)
1524 break;
1527 if (tmp == 0x00 || tmp == 0xff)
1529 printf("%s: timeout waiting for BBP to wakeup\n",
1530 device_get_nameunit(sc->dev));
1531 return ETIMEDOUT;
1534 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1535 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1536 rt2860_def_bbp[i].val);
1538 if ((sc->mac_rev & 0xffff) != 0x0101)
1539 rt2860_io_bbp_write(sc, 84, 0x19);
1541 if (sc->mac_rev == 0x28600100)
1543 rt2860_io_bbp_write(sc, 69, 0x16);
1544 rt2860_io_bbp_write(sc, 73, 0x12);
1547 return 0;
1551 * rt2860_stop_locked
1553 static void rt2860_stop_locked(void *priv)
1555 struct rt2860_softc *sc;
1556 struct ifnet *ifp;
1557 struct ieee80211com *ic;
1558 uint32_t tmp;
1560 sc = priv;
1561 ifp = sc->ifp;
1562 ic = ifp->if_l2com;
1564 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1565 "%s: stopping\n",
1566 device_get_nameunit(sc->dev));
1568 RT2860_SOFTC_ASSERT_LOCKED(sc);
1570 sc->tx_timer = 0;
1572 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1573 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1575 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1577 callout_stop(&sc->periodic_ch);
1578 callout_stop(&sc->tx_watchdog_ch);
1580 RT2860_SOFTC_UNLOCK(sc);
1582 taskqueue_block(sc->taskqueue);
1584 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1585 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1586 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1587 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1589 RT2860_SOFTC_LOCK(sc);
1591 /* clear key tables */
1593 rt2860_asic_clear_keytables(sc);
1595 /* disable interrupts */
1597 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1599 /* disable Tx/Rx */
1601 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1603 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1605 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1607 /* reset adapter */
1609 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1610 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1611 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1615 * rt2860_stop
1617 static void rt2860_stop(void *priv)
1619 struct rt2860_softc *sc;
1621 sc = priv;
1623 RT2860_SOFTC_LOCK(sc);
1625 rt2860_stop_locked(sc);
1627 RT2860_SOFTC_UNLOCK(sc);
1631 * rt2860_start
1633 static void rt2860_start(struct ifnet *ifp)
1635 struct rt2860_softc *sc;
1636 struct ieee80211_node *ni;
1637 struct mbuf *m;
1638 int qid;
1640 sc = ifp->if_softc;
1642 RT2860_SOFTC_LOCK(sc);
1644 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1646 RT2860_SOFTC_UNLOCK(sc);
1647 return;
1650 for (;;)
1652 IF_POLL(&ifp->if_snd, m);
1653 if (m == NULL)
1654 break;
1656 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1658 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1660 qid = M_WME_GETAC(m);
1662 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1664 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1665 "%s: if_start: Tx ring with qid=%d is full\n",
1666 device_get_nameunit(sc->dev), qid);
1668 m_freem(m);
1669 ieee80211_free_node(ni);
1670 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1671 ifp->if_oerrors++;
1672 break;
1675 if (rt2860_tx_data(sc, m, ni, qid) != 0)
1677 ieee80211_free_node(ni);
1678 ifp->if_oerrors++;
1679 break;
1682 rt2860_drain_fifo_stats(sc);
1684 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1686 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1689 RT2860_SOFTC_UNLOCK(sc);
1693 * rt2860_ioctl
1695 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1697 struct rt2860_softc *sc;
1698 struct ieee80211com *ic;
1699 struct ifreq *ifr;
1700 int error, startall;
1702 sc = ifp->if_softc;
1703 ic = ifp->if_l2com;
1704 ifr = (struct ifreq *) data;
1706 error = 0;
1708 switch (cmd)
1710 case SIOCSIFFLAGS:
1711 startall = 0;
1713 RT2860_SOFTC_LOCK(sc);
1715 if (ifp->if_flags & IFF_UP)
1717 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1719 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1720 rt2860_asic_update_promisc(sc);
1722 else
1724 rt2860_init_locked(sc);
1725 startall = 1;
1728 else
1730 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1731 rt2860_stop_locked(sc);
1734 sc->if_flags = ifp->if_flags;
1736 RT2860_SOFTC_UNLOCK(sc);
1738 if (startall)
1739 ieee80211_start_all(ic);
1740 break;
1742 case SIOCGIFMEDIA:
1743 case SIOCSIFMEDIA:
1744 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1745 break;
1747 case SIOCGIFADDR:
1748 error = ether_ioctl(ifp, cmd, data);
1749 break;
1751 default:
1752 error = EINVAL;
1753 break;
1756 return error;
1760 * rt2860_vap_create
1762 static struct ieee80211vap *rt2860_vap_create(struct ieee80211com *ic,
1763 const char name[IFNAMSIZ], int unit, int opmode, int flags,
1764 const uint8_t bssid[IEEE80211_ADDR_LEN],
1765 const uint8_t mac[IEEE80211_ADDR_LEN])
1767 struct rt2860_softc *sc;
1768 struct ifnet *ifp;
1769 struct rt2860_softc_vap *rvap;
1770 struct ieee80211vap *vap;
1772 ifp = ic->ic_ifp;
1773 sc = ifp->if_softc;
1775 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1776 "%s: VAP create: opmode=%s\n",
1777 device_get_nameunit(sc->dev),
1778 ieee80211_opmode_name[opmode]);
1780 switch (opmode)
1782 case IEEE80211_M_MONITOR:
1783 case IEEE80211_M_IBSS:
1784 case IEEE80211_M_STA:
1785 case IEEE80211_M_AHDEMO:
1786 case IEEE80211_M_HOSTAP:
1787 case IEEE80211_M_MBSS:
1788 if (!TAILQ_EMPTY(&ic->ic_vaps))
1790 if_printf(ifp, "only 1 VAP supported\n");
1791 return NULL;
1794 if (opmode == IEEE80211_M_STA)
1795 flags |= IEEE80211_CLONE_NOBEACONS;
1796 break;
1798 case IEEE80211_M_WDS:
1799 if (TAILQ_EMPTY(&ic->ic_vaps) || ic->ic_opmode != IEEE80211_M_HOSTAP)
1801 if_printf(ifp, "WDS only supported in AP mode\n");
1802 return NULL;
1805 flags &= ~IEEE80211_CLONE_BSSID;
1806 break;
1808 default:
1809 if_printf(ifp, "unknown opmode %d\n", opmode);
1810 return NULL;
1813 rvap = (struct rt2860_softc_vap *) malloc(sizeof(struct rt2860_softc_vap),
1814 M_80211_VAP, M_NOWAIT | M_ZERO);
1815 if (rvap == NULL)
1816 return NULL;
1818 vap = &rvap->vap;
1820 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
1822 rvap->newstate = vap->iv_newstate;
1823 vap->iv_newstate = rt2860_vap_newstate;
1825 vap->iv_reset = rt2860_vap_reset;
1826 vap->iv_key_update_begin = rt2860_vap_key_update_begin;
1827 vap->iv_key_update_end = rt2860_vap_key_update_end;
1828 vap->iv_key_set = rt2860_vap_key_set;
1829 vap->iv_key_delete = rt2860_vap_key_delete;
1830 vap->iv_update_beacon = rt2860_vap_update_beacon;
1832 rt2860_amrr_init(&rvap->amrr, vap,
1833 RT2860_AMRR_MIN_SUCCESS_THRESHOLD,
1834 RT2860_AMRR_MAX_SUCCESS_THRESHOLD,
1835 500);
1837 ieee80211_vap_attach(vap, rt2860_media_change, ieee80211_media_status);
1839 ic->ic_opmode = opmode;
1841 return vap;
1845 * rt2860_vap_delete
1847 static void rt2860_vap_delete(struct ieee80211vap *vap)
1849 struct rt2860_softc *sc;
1850 struct ieee80211com *ic;
1851 struct ifnet *ifp;
1852 struct rt2860_softc_vap *rvap;
1854 ic = vap->iv_ic;
1855 ifp = ic->ic_ifp;
1856 sc = ifp->if_softc;
1857 rvap = (struct rt2860_softc_vap *) vap;
1859 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1860 "%s: VAP delete: opmode=%s\n",
1861 device_get_nameunit(sc->dev),
1862 ieee80211_opmode_name[vap->iv_opmode]);
1864 rt2860_amrr_cleanup(&rvap->amrr);
1866 ieee80211_vap_detach(vap);
1868 free(rvap, M_80211_VAP);
1872 * rt2860_reset_vap
1874 static int rt2860_vap_reset(struct ieee80211vap *vap, u_long cmd)
1876 struct rt2860_softc *sc;
1877 struct ieee80211com *ic;
1878 struct ifnet *ifp;
1879 struct rt2860_softc_vap *rvap;
1880 int error;
1882 ic = vap->iv_ic;
1883 ifp = ic->ic_ifp;
1884 sc = ifp->if_softc;
1885 rvap = (struct rt2860_softc_vap *) vap;
1887 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1888 "%s: VAP reset: cmd=%lu\n",
1889 device_get_nameunit(sc->dev), cmd);
1891 error = 0;
1893 switch (cmd)
1895 case IEEE80211_IOC_RTSTHRESHOLD:
1896 rt2860_asic_update_rtsthreshold(sc);
1897 break;
1899 case IEEE80211_IOC_PROTMODE:
1900 case IEEE80211_IOC_HTPROTMODE:
1901 rt2860_asic_updateprot(sc);
1902 break;
1904 case IEEE80211_IOC_TXPOWER:
1905 rt2860_asic_update_txpower(sc);
1906 break;
1908 case IEEE80211_IOC_SHORTGI:
1909 case IEEE80211_IOC_AMPDU_DENSITY:
1910 break;
1912 default:
1913 error = ENETRESET;
1914 break;
1917 return error;
1921 * rt2860_vap_newstate
1923 static int rt2860_vap_newstate(struct ieee80211vap *vap,
1924 enum ieee80211_state nstate, int arg)
1926 struct rt2860_softc *sc;
1927 struct ieee80211com *ic;
1928 struct ifnet *ifp;
1929 struct rt2860_softc_vap *rvap;
1930 struct ieee80211_node *ni;
1931 enum ieee80211_state ostate;
1932 int error;
1934 ic = vap->iv_ic;
1935 ifp = ic->ic_ifp;
1936 sc = ifp->if_softc;
1937 rvap = (struct rt2860_softc_vap *) vap;
1939 ostate = vap->iv_state;
1941 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1942 "%s: VAP newstate: %s -> %s\n",
1943 device_get_nameunit(sc->dev),
1944 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
1946 error = rvap->newstate(vap, nstate, arg);
1947 if (error != 0)
1948 return error;
1950 IEEE80211_UNLOCK(ic);
1952 RT2860_SOFTC_LOCK(sc);
1954 /* turn link LED off */
1956 if (nstate != IEEE80211_S_RUN)
1957 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1959 switch (nstate)
1961 case IEEE80211_S_INIT:
1962 rt2860_asic_disable_tsf_sync(sc);
1963 break;
1965 case IEEE80211_S_RUN:
1966 ni = vap->iv_bss;
1968 rt2860_rf_set_chan(sc, ni->ni_chan);
1970 if (vap->iv_opmode != IEEE80211_M_MONITOR)
1972 rt2860_asic_enable_mrr(sc);
1973 rt2860_asic_set_txpreamble(sc);
1974 rt2860_asic_set_basicrates(sc);
1975 rt2860_asic_update_txpower(sc);
1976 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1979 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1980 vap->iv_opmode == IEEE80211_M_IBSS ||
1981 vap->iv_opmode == IEEE80211_M_MBSS)
1983 error = rt2860_asic_update_beacon(sc, vap);
1984 if (error != 0)
1985 break;
1988 if (vap->iv_opmode != IEEE80211_M_MONITOR)
1989 rt2860_asic_enable_tsf_sync(sc);
1991 /* turn link LED on */
1993 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1995 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1996 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1997 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
1999 break;
2001 default:
2002 break;
2005 RT2860_SOFTC_UNLOCK(sc);
2007 IEEE80211_LOCK(ic);
2009 return error;
2013 * rt2860_vap_key_update_begin
2015 static void rt2860_vap_key_update_begin(struct ieee80211vap *vap)
2017 struct rt2860_softc *sc;
2018 struct ieee80211com *ic;
2019 struct ifnet *ifp;
2021 ic = vap->iv_ic;
2022 ifp = ic->ic_ifp;
2023 sc = ifp->if_softc;
2025 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2026 "%s: VAP key update begin\n",
2027 device_get_nameunit(sc->dev));
2029 taskqueue_block(sc->taskqueue);
2031 IF_LOCK(&ifp->if_snd);
2035 * rt2860_vap_key_update_end
2037 static void rt2860_vap_key_update_end(struct ieee80211vap *vap)
2039 struct rt2860_softc *sc;
2040 struct ieee80211com *ic;
2041 struct ifnet *ifp;
2043 ic = vap->iv_ic;
2044 ifp = ic->ic_ifp;
2045 sc = ifp->if_softc;
2047 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2048 "%s: VAP key update end\n",
2049 device_get_nameunit(sc->dev));
2051 IF_UNLOCK(&ifp->if_snd);
2053 taskqueue_unblock(sc->taskqueue);
2057 * rt2860_vap_key_set
2059 static int rt2860_vap_key_set(struct ieee80211vap *vap,
2060 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2062 struct rt2860_softc *sc;
2063 struct ieee80211com *ic;
2064 struct ifnet *ifp;
2065 struct ieee80211_node *ni;
2066 uint16_t associd, key_base, keymode_base;
2067 uint8_t mode, vapid, wcid, iv[8];
2068 uint32_t tmp;
2070 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2071 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2072 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2073 return EINVAL;
2075 ic = vap->iv_ic;
2076 ifp = ic->ic_ifp;
2077 sc = ifp->if_softc;
2079 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2080 ni = vap->iv_bss;
2081 else
2082 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, mac);
2084 associd = (ni != NULL) ? ni->ni_associd : 0;
2086 if ((vap->iv_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2087 ieee80211_free_node(ni);
2089 switch (k->wk_cipher->ic_cipher)
2091 case IEEE80211_CIPHER_WEP:
2092 if(k->wk_keylen < 8)
2093 mode = RT2860_REG_CIPHER_MODE_WEP40;
2094 else
2095 mode = RT2860_REG_CIPHER_MODE_WEP104;
2096 break;
2098 case IEEE80211_CIPHER_TKIP:
2099 mode = RT2860_REG_CIPHER_MODE_TKIP;
2100 break;
2102 case IEEE80211_CIPHER_AES_CCM:
2103 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2104 break;
2106 default:
2107 return EINVAL;
2110 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2111 "%s: VAP key set: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2112 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2113 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2115 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2117 /* install pairwise key */
2119 vapid = 0;
2120 wcid = RT2860_AID2WCID(associd);
2121 key_base = RT2860_REG_PKEY(wcid);
2123 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2125 memset(iv, 0, 8);
2127 iv[3] = (k->wk_keyix << 6);
2129 else
2131 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2133 iv[0] = (k->wk_keytsc >> 8);
2134 iv[1] = ((iv[0] | 0x20) & 0x7f);
2135 iv[2] = k->wk_keytsc;
2137 else
2139 /* AES CCMP */
2141 iv[0] = k->wk_keytsc;
2142 iv[1] = k->wk_keytsc >> 8;
2143 iv[2] = 0;
2146 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2147 iv[4] = (k->wk_keytsc >> 16);
2148 iv[5] = (k->wk_keytsc >> 24);
2149 iv[6] = (k->wk_keytsc >> 32);
2150 iv[7] = (k->wk_keytsc >> 40);
2152 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2153 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2154 device_get_nameunit(sc->dev),
2155 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2158 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2160 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2162 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2164 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2166 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2167 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2169 else
2171 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2172 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2175 else
2177 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2180 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2181 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2183 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2186 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2187 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2189 /* install group key */
2191 vapid = 0;
2192 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2193 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2195 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2197 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2199 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2201 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2202 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2204 else
2206 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2207 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2210 else
2212 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2215 tmp = rt2860_io_mac_read(sc, keymode_base);
2217 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2218 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2220 rt2860_io_mac_write(sc, keymode_base, tmp);
2223 return 1;
2227 * rt2860_vap_key_delete
2229 static int rt2860_vap_key_delete(struct ieee80211vap *vap,
2230 const struct ieee80211_key *k)
2232 struct rt2860_softc *sc;
2233 struct ieee80211com *ic;
2234 struct ifnet *ifp;
2235 struct ieee80211_node *ni;
2236 uint16_t associd;
2237 uint8_t vapid, wcid;
2238 uint32_t tmp;
2240 ic = vap->iv_ic;
2241 ifp = ic->ic_ifp;
2242 sc = ifp->if_softc;
2243 ni = vap->iv_bss;
2244 associd = (ni != NULL) ? ni->ni_associd : 0;
2246 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2247 "%s: VAP key delete: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2248 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2249 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2251 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2253 /* remove pairwise key */
2255 wcid = RT2860_AID2WCID(associd);
2257 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID_ATTR(wcid));
2259 tmp &= ~0xf;
2260 tmp |= (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT);
2262 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2264 else
2266 /* remove group key */
2268 vapid = 0;
2270 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2272 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2273 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2275 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2278 return 1;
2282 * rt2860_vap_update_beacon
2284 static void rt2860_vap_update_beacon(struct ieee80211vap *vap, int what)
2286 struct rt2860_softc *sc;
2287 struct ieee80211com *ic;
2288 struct ifnet *ifp;
2290 ic = vap->iv_ic;
2291 ifp = ic->ic_ifp;
2292 sc = ifp->if_softc;
2294 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2295 "%s: VAP update beacon\n",
2296 device_get_nameunit(sc->dev));
2298 rt2860_asic_update_beacon(sc, vap);
2302 * rt2860_media_change
2304 static int rt2860_media_change(struct ifnet *ifp)
2306 int error;
2308 error = ieee80211_media_change(ifp);
2310 return (error == ENETRESET ? 0 : error);
2314 * rt2860_node_alloc
2316 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211vap *vap,
2317 const uint8_t mac[IEEE80211_ADDR_LEN])
2319 return malloc(sizeof(struct rt2860_softc_node),
2320 M_80211_NODE, M_NOWAIT | M_ZERO);
2324 * rt2860_setregdomain
2326 static int rt2860_setregdomain(struct ieee80211com *ic,
2327 struct ieee80211_regdomain *reg,
2328 int nchans, struct ieee80211_channel chans[])
2330 struct rt2860_softc *sc;
2331 struct ifnet *ifp;
2333 ifp = ic->ic_ifp;
2334 sc = ifp->if_softc;
2336 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2337 "%s: set regulatory domain: country=%d, country code string=%c%c, location=%c\n",
2338 device_get_nameunit(sc->dev),
2339 reg->country, reg->isocc[0], reg->isocc[1], reg->location);
2341 return 0;
2345 * rt2860_getradiocaps
2347 static void rt2860_getradiocaps(struct ieee80211com *ic,
2348 int maxchans, int *nchans, struct ieee80211_channel chans[])
2350 *nchans = (ic->ic_nchans >= maxchans) ? maxchans : ic->ic_nchans;
2352 memcpy(chans, ic->ic_channels, (*nchans) * sizeof(struct ieee80211_channel));
2356 * rt2860_scan_start
2358 static void rt2860_scan_start(struct ieee80211com *ic)
2360 struct rt2860_softc *sc;
2361 struct ifnet *ifp;
2363 ifp = ic->ic_ifp;
2364 sc = ifp->if_softc;
2366 rt2860_asic_disable_tsf_sync(sc);
2370 * rt2860_scan_end
2372 static void rt2860_scan_end(struct ieee80211com *ic)
2374 struct rt2860_softc *sc;
2375 struct ifnet *ifp;
2377 ifp = ic->ic_ifp;
2378 sc = ifp->if_softc;
2380 rt2860_asic_enable_tsf_sync(sc);
2384 * rt2860_set_channel
2386 static void rt2860_set_channel(struct ieee80211com *ic)
2388 struct rt2860_softc *sc;
2389 struct ifnet *ifp;
2391 ifp = ic->ic_ifp;
2392 sc = ifp->if_softc;
2394 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
2395 "%s: set channel: channel=%u, HT%s%s\n",
2396 device_get_nameunit(sc->dev),
2397 ieee80211_chan2ieee(ic, ic->ic_curchan),
2398 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
2399 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
2400 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
2401 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
2403 RT2860_SOFTC_LOCK(sc);
2405 rt2860_rf_set_chan(sc, ic->ic_curchan);
2407 RT2860_SOFTC_UNLOCK(sc);
2411 * rt2860_newassoc
2413 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
2415 struct rt2860_softc *sc;
2416 struct ieee80211com *ic;
2417 struct ifnet *ifp;
2418 struct ieee80211vap *vap;
2419 struct rt2860_softc_vap *rvap;
2420 uint16_t associd;
2421 uint8_t wcid;
2423 vap = ni->ni_vap;
2424 ic = vap->iv_ic;
2425 ifp = ic->ic_ifp;
2426 sc = ifp->if_softc;
2427 rvap = (struct rt2860_softc_vap *) vap;
2429 associd = (ni != NULL) ? ni->ni_associd : 0;
2430 wcid = RT2860_AID2WCID(associd);
2432 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
2433 "%s: new association: wcid=0x%02x, "
2434 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2435 device_get_nameunit(sc->dev), wcid,
2436 ether_sprintf(ni->ni_macaddr),
2437 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
2438 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
2439 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
2441 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
2442 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2444 rt2860_amrr_node_init(&rvap->amrr, &sc->amrr_node[wcid], ni);
2446 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2447 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2448 device_get_nameunit(sc->dev),
2449 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2450 ni->ni_associd, ni->ni_txrate,
2451 (ni->ni_flags & IEEE80211_NODE_HT) ?
2452 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2453 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2455 rt2860_asic_updateprot(sc);
2456 rt2860_asic_updateslot(sc);
2457 rt2860_asic_set_txpreamble(sc);
2461 * rt2860_updateslot
2463 static void rt2860_updateslot(struct ifnet *ifp)
2465 struct rt2860_softc *sc;
2467 sc = ifp->if_softc;
2469 rt2860_asic_updateslot(sc);
2473 * rt2860_update_promisc
2475 static void rt2860_update_promisc(struct ifnet *ifp)
2477 struct rt2860_softc *sc;
2479 sc = ifp->if_softc;
2481 rt2860_asic_update_promisc(sc);
2485 * rt2860_update_mcast
2487 static void rt2860_update_mcast(struct ifnet *ifp)
2489 struct rt2860_softc *sc;
2491 sc = ifp->if_softc;
2495 * rt2860_wme_update
2497 static int rt2860_wme_update(struct ieee80211com *ic)
2499 struct rt2860_softc *sc;
2500 struct ifnet *ifp;
2502 ifp = ic->ic_ifp;
2503 sc = ifp->if_softc;
2505 rt2860_asic_wme_update(sc);
2507 return 0;
2511 * rt2860_raw_xmit
2513 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2514 const struct ieee80211_bpf_params *params)
2516 struct rt2860_softc *sc;
2517 struct ieee80211com *ic;
2518 struct ifnet *ifp;
2520 ic = ni->ni_ic;
2521 ifp = ic->ic_ifp;
2522 sc = ifp->if_softc;
2524 RT2860_SOFTC_LOCK(sc);
2526 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2528 RT2860_SOFTC_UNLOCK(sc);
2529 m_freem(m);
2530 ieee80211_free_node(ni);
2531 return ENETDOWN;
2534 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
2536 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
2537 "%s: raw xmit: Tx ring with qid=%d is full\n",
2538 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
2540 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2541 RT2860_SOFTC_UNLOCK(sc);
2542 m_freem(m);
2543 ieee80211_free_node(ni);
2544 return ENOBUFS;
2547 if (rt2860_tx_mgmt(sc, m, ni, sc->tx_ring_mgtqid) != 0)
2549 ifp->if_oerrors++;
2550 RT2860_SOFTC_UNLOCK(sc);
2551 ieee80211_free_node(ni);
2552 return EIO;
2555 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
2557 RT2860_SOFTC_UNLOCK(sc);
2559 return 0;
2563 * rt2860_recv_action
2565 static int rt2860_recv_action(struct ieee80211_node *ni,
2566 const struct ieee80211_frame *wh,
2567 const uint8_t *frm, const uint8_t *efrm)
2569 struct rt2860_softc *sc;
2570 struct ieee80211com *ic;
2571 struct ifnet *ifp;
2572 const struct ieee80211_action *ia;
2573 uint16_t associd, baparamset;
2574 uint8_t wcid;
2575 int ret, tid, bufsize;
2576 uint32_t tmp;
2578 ic = ni->ni_ic;
2579 ifp = ic->ic_ifp;
2580 sc = ifp->if_softc;
2582 ia = (const struct ieee80211_action *) frm;
2584 ret = sc->recv_action(ni, wh, frm, efrm);
2586 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2587 return ret;
2589 associd = (ni != NULL) ? ni->ni_associd : 0;
2590 wcid = RT2860_AID2WCID(associd);
2592 switch (ia->ia_action)
2594 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2595 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2596 baparamset = LE_READ_2(frm + 3);
2597 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2598 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2600 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2601 "%s: adding A-MPDU Rx block ACK: associd=0x%04x, tid=%d, bufsize=%d\n",
2602 device_get_nameunit(sc->dev), associd, tid, bufsize);
2604 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2606 tmp |= (0x10000 << tid);
2608 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2609 break;
2611 /* IEEE80211_ACTION_BA_DELBA */
2612 case IEEE80211_ACTION_BA_DELBA:
2613 baparamset = LE_READ_2(frm + 2);
2614 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2616 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2617 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2618 device_get_nameunit(sc->dev), associd, tid);
2620 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2622 tmp &= ~(0x10000 << tid);
2624 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2625 break;
2628 return ret;
2632 * rt2860_send_action
2634 static int rt2860_send_action(struct ieee80211_node *ni,
2635 int cat, int act, void *sa)
2637 struct rt2860_softc *sc;
2638 struct ieee80211com *ic;
2639 struct ifnet *ifp;
2640 uint16_t associd, *args, baparamset;
2641 uint8_t wcid;
2642 int ret, tid;
2643 uint32_t tmp;
2645 ic = ni->ni_ic;
2646 ifp = ic->ic_ifp;
2647 sc = ifp->if_softc;
2649 ret = sc->send_action(ni, cat, act, sa);
2651 if (cat != IEEE80211_ACTION_CAT_BA)
2652 return ret;
2654 associd = (ni != NULL) ? ni->ni_associd : 0;
2655 wcid = RT2860_AID2WCID(associd);
2656 args = sa;
2658 switch (act)
2660 /* IEEE80211_ACTION_BA_DELBA */
2661 case IEEE80211_ACTION_BA_DELBA:
2662 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) | args[1];
2664 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) == IEEE80211_DELBAPS_INIT)
2665 break;
2667 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2669 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2670 "%s: deleting A-MPDU Rx block ACK: associd=0x%04x, tid=%d\n",
2671 device_get_nameunit(sc->dev), associd, tid);
2673 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2675 tmp &= ~(0x10000 << tid);
2677 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2678 break;
2681 return ret;
2685 * rt2860_amrr_update_iter_func
2687 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2689 struct rt2860_softc *sc;
2690 struct ieee80211com *ic;
2691 struct ifnet *ifp;
2692 struct ieee80211vap *vap;
2693 struct rt2860_softc_vap *rvap;
2694 uint8_t wcid;
2696 vap = arg;
2697 ic = vap->iv_ic;
2698 ifp = ic->ic_ifp;
2699 sc = ifp->if_softc;
2700 rvap = (struct rt2860_softc_vap *) vap;
2702 /* only associated stations */
2704 if ((ni->ni_vap == vap) && (ni->ni_associd != 0))
2706 wcid = RT2860_AID2WCID(ni->ni_associd);
2708 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2710 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2711 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2712 device_get_nameunit(sc->dev),
2713 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2714 ni->ni_associd, ni->ni_txrate,
2715 (ni->ni_flags & IEEE80211_NODE_HT) ?
2716 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2717 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2722 * rt2860_periodic
2724 static void rt2860_periodic(void *arg)
2726 struct rt2860_softc *sc;
2728 sc = arg;
2730 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2731 "%s: periodic\n",
2732 device_get_nameunit(sc->dev));
2734 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2738 * rt2860_tx_watchdog
2740 static void rt2860_tx_watchdog(void *arg)
2742 struct rt2860_softc *sc;
2743 struct ifnet *ifp;
2745 sc = arg;
2746 ifp = sc->ifp;
2748 if (sc->tx_timer == 0)
2749 return;
2751 if (--sc->tx_timer == 0)
2753 printf("%s: Tx watchdog timeout: resetting\n",
2754 device_get_nameunit(sc->dev));
2756 rt2860_stop_locked(sc);
2757 rt2860_init_locked(sc);
2759 ifp->if_oerrors++;
2761 sc->tx_watchdog_timeouts++;
2764 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2768 * rt2860_asic_set_bssid
2770 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2771 const uint8_t *bssid)
2773 uint32_t tmp;
2775 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2776 "%s: set bssid: bssid=%s\n",
2777 device_get_nameunit(sc->dev),
2778 ether_sprintf(bssid));
2780 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2782 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2784 tmp = bssid[4] | (bssid[5] << 8);
2786 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2790 * rt2860_asic_set_macaddr
2792 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2793 const uint8_t *addr)
2795 uint32_t tmp;
2797 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2799 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2801 tmp = addr[4] | (addr[5] << 8);
2803 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2807 * rt2860_asic_enable_tsf_sync
2809 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2811 struct ifnet *ifp;
2812 struct ieee80211com *ic;
2813 struct ieee80211vap *vap;
2814 uint32_t tmp;
2816 ifp = sc->ifp;
2817 ic = ifp->if_l2com;
2818 vap = TAILQ_FIRST(&ic->ic_vaps);
2820 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2821 "%s: enabling TSF\n",
2822 device_get_nameunit(sc->dev));
2824 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2826 tmp &= ~0x1fffff;
2827 tmp |= vap->iv_bss->ni_intval * 16;
2828 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2830 if (vap->iv_opmode == IEEE80211_M_STA)
2832 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2834 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2836 tmp |= RT2860_REG_BCN_TX_ENABLE;
2837 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2839 else if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2841 tmp |= RT2860_REG_BCN_TX_ENABLE;
2842 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2845 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2849 * rt2860_asic_disable_tsf_sync
2851 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2853 uint32_t tmp;
2855 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2856 "%s: disabling TSF\n",
2857 device_get_nameunit(sc->dev));
2859 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2861 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2862 RT2860_REG_TSF_TIMER_ENABLE |
2863 RT2860_REG_TBTT_TIMER_ENABLE);
2865 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2866 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2868 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2872 * rt2860_asic_enable_mrr
2874 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2876 #define CCK(mcs) (mcs)
2877 #define OFDM(mcs) ((1 << 3) | (mcs))
2878 #define HT(mcs) (mcs)
2880 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2881 (OFDM(6) << 28) | /* 54 -> 48 */
2882 (OFDM(5) << 24) | /* 48 -> 36 */
2883 (OFDM(4) << 20) | /* 36 -> 24 */
2884 (OFDM(3) << 16) | /* 24 -> 18 */
2885 (OFDM(2) << 12) | /* 18 -> 12 */
2886 (OFDM(1) << 8) | /* 12 -> 9 */
2887 (OFDM(0) << 4) | /* 9 -> 6 */
2888 OFDM(0)); /* 6 -> 6 */
2890 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2891 (CCK(2) << 12) | /* 11 -> 5.5 */
2892 (CCK(1) << 8) | /* 5.5 -> 2 */
2893 (CCK(0) << 4) | /* 2 -> 1 */
2894 CCK(0)); /* 1 -> 1 */
2896 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2897 (HT(6) << 28) |
2898 (HT(5) << 24) |
2899 (HT(4) << 20) |
2900 (HT(3) << 16) |
2901 (HT(2) << 12) |
2902 (HT(1) << 8) |
2903 (HT(0) << 4) |
2904 HT(0));
2906 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2907 (HT(14) << 28) |
2908 (HT(13) << 24) |
2909 (HT(12) << 20) |
2910 (HT(11) << 16) |
2911 (HT(10) << 12) |
2912 (HT(9) << 8) |
2913 (HT(8) << 4) |
2914 HT(8));
2916 #undef HT
2917 #undef OFDM
2918 #undef CCK
2922 * rt2860_asic_set_txpreamble
2924 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2926 struct ifnet *ifp;
2927 struct ieee80211com *ic;
2928 uint32_t tmp;
2930 ifp = sc->ifp;
2931 ic = ifp->if_l2com;
2933 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2934 "%s: %s short Tx preamble\n",
2935 device_get_nameunit(sc->dev),
2936 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2938 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2940 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2942 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2943 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2945 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2949 * rt2860_asic_set_basicrates
2951 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2953 struct ifnet *ifp;
2954 struct ieee80211com *ic;
2956 ifp = sc->ifp;
2957 ic = ifp->if_l2com;
2959 if (ic->ic_curmode == IEEE80211_MODE_11B)
2960 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2961 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2962 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2963 else
2964 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2968 * rt2860_asic_update_rtsthreshold
2970 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2972 struct ifnet *ifp;
2973 struct ieee80211com *ic;
2974 struct ieee80211vap *vap;
2975 uint32_t tmp;
2976 uint16_t threshold;
2978 ifp = sc->ifp;
2979 ic = ifp->if_l2com;
2980 vap = TAILQ_FIRST(&ic->ic_vaps);
2982 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2983 "%s: updating RTS threshold: %d\n",
2984 device_get_nameunit(sc->dev), vap->iv_rtsthreshold);
2986 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2988 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2990 threshold = (vap->iv_rtsthreshold < IEEE80211_RTS_MAX) ?
2991 vap->iv_rtsthreshold : 0x1000;
2993 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2994 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2996 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
3000 * rt2860_asic_update_txpower
3002 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
3004 struct ifnet *ifp;
3005 struct ieee80211com *ic;
3006 uint32_t *txpow_rate;
3007 int8_t delta;
3008 uint8_t val;
3009 uint32_t tmp;
3010 int i;
3012 ifp = sc->ifp;
3013 ic = ifp->if_l2com;
3015 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3016 "%s: updating Tx power: %d\n",
3017 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
3019 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
3021 txpow_rate = sc->txpow_rate_20mhz;
3023 else
3025 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3026 txpow_rate = sc->txpow_rate_40mhz_2ghz;
3027 else
3028 txpow_rate = sc->txpow_rate_40mhz_5ghz;
3031 delta = 0;
3033 val = rt2860_io_bbp_read(sc, 1);
3034 val &= 0xfc;
3036 if (ic->ic_txpowlimit > 90)
3038 /* do nothing */
3040 else if (ic->ic_txpowlimit > 60)
3042 delta -= 1;
3044 else if (ic->ic_txpowlimit > 30)
3046 delta -= 3;
3048 else if (ic->ic_txpowlimit > 15)
3050 val |= 0x1;
3052 else if (ic->ic_txpowlimit > 9)
3054 val |= 0x1;
3055 delta -= 3;
3057 else
3059 val |= 0x2;
3062 rt2860_io_bbp_write(sc, 1, val);
3064 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
3066 if (txpow_rate[i] == 0xffffffff)
3067 continue;
3069 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
3071 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
3076 * rt2860_asic_update_promisc
3078 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
3080 struct ifnet *ifp;
3081 uint32_t tmp;
3083 ifp = sc->ifp;
3085 printf("%s: %s promiscuous mode\n",
3086 device_get_nameunit(sc->dev),
3087 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
3089 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
3091 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
3093 if (!(ifp->if_flags & IFF_PROMISC))
3094 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
3096 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
3100 * rt2860_asic_updateprot
3102 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
3104 struct ifnet *ifp;
3105 struct ieee80211com *ic;
3106 struct ieee80211vap *vap;
3107 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
3108 uint8_t htopmode;
3110 ifp = sc->ifp;
3111 ic = ifp->if_l2com;
3112 vap = TAILQ_FIRST(&ic->ic_vaps);
3114 /* CCK frame protection */
3116 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3117 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
3119 /* set up protection frame phy mode and rate (MCS code) */
3121 if (ic->ic_curmode == IEEE80211_MODE_11A)
3122 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3123 (0 << RT2860_REG_PROT_MCS_SHIFT);
3124 else
3125 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3126 (3 << RT2860_REG_PROT_MCS_SHIFT));
3128 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
3130 /* OFDM frame protection */
3132 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3133 RT2860_REG_TXOP_ALLOW_ALL;
3135 if (ic->ic_flags & IEEE80211_F_USEPROT)
3137 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3138 "%s: updating protection mode: b/g protection mode=%s\n",
3139 device_get_nameunit(sc->dev),
3140 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3141 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3143 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3144 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3145 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3146 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
3147 else
3148 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3150 else
3152 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3153 "%s: updating protection mode: b/g protection mode=%s\n",
3154 device_get_nameunit(sc->dev), "none");
3156 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3159 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
3161 /* HT frame protection */
3163 if ((vap->iv_opmode == IEEE80211_M_STA) && (vap->iv_state == IEEE80211_S_RUN))
3164 htopmode = vap->iv_bss->ni_htopmode;
3165 else
3166 htopmode = ic->ic_curhtprotmode;
3168 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3169 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3170 device_get_nameunit(sc->dev),
3171 htopmode & IEEE80211_HTINFO_OPMODE,
3172 (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3173 ((ic->ic_htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3175 switch (htopmode & IEEE80211_HTINFO_OPMODE)
3177 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3178 case IEEE80211_HTINFO_OPMODE_HT20PR:
3179 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3180 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3181 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3182 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3183 (4 << RT2860_REG_PROT_MCS_SHIFT);
3185 gf20_prot = mm20_prot;
3187 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
3188 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3189 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3191 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
3192 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3193 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
3194 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3195 else
3196 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3198 gf40_prot = mm40_prot;
3199 break;
3201 /* IEEE80211_HTINFO_OPMODE_MIXED */
3202 case IEEE80211_HTINFO_OPMODE_MIXED:
3203 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
3204 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3205 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
3207 if (ic->ic_flags & IEEE80211_F_USEPROT)
3208 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3209 (3 << RT2860_REG_PROT_MCS_SHIFT);
3210 else
3211 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3212 (4 << RT2860_REG_PROT_MCS_SHIFT);
3214 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
3215 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3216 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
3217 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
3218 else
3219 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
3221 gf20_prot = mm20_prot;
3223 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
3225 if (ic->ic_flags & IEEE80211_F_USEPROT)
3226 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3227 (3 << RT2860_REG_PROT_MCS_SHIFT);
3228 else
3229 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3230 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3232 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
3233 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3234 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
3235 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3236 else
3237 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3239 gf40_prot = mm40_prot;
3240 break;
3243 * IEEE80211_HTINFO_OPMODE_PURE
3244 * IEEE80211_HTINFO_OPMODE_PROTOPT
3246 case IEEE80211_HTINFO_OPMODE_PURE:
3247 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3248 default:
3249 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3250 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3251 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3252 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3253 (4 << RT2860_REG_PROT_MCS_SHIFT);
3255 gf20_prot = mm20_prot;
3257 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3258 RT2860_REG_TXOP_ALLOW_ALL |
3259 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3260 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3262 gf40_prot = mm40_prot;
3263 break;
3266 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3267 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3268 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3269 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3273 * rt2860_asic_updateslot
3275 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3277 struct ifnet *ifp;
3278 struct ieee80211com *ic;
3279 uint32_t tmp;
3281 ifp = sc->ifp;
3282 ic = ifp->if_l2com;
3284 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3285 "%s: %s short slot time\n",
3286 device_get_nameunit(sc->dev),
3287 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3289 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3291 tmp &= ~0xff;
3292 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3294 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3298 * rt2860_asic_wme_update
3300 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3302 struct ifnet *ifp;
3303 struct ieee80211com *ic;
3304 struct ieee80211_wme_state *wme;
3305 const struct wmeParams *wmep;
3306 int i;
3308 ifp = sc->ifp;
3309 ic = ifp->if_l2com;
3310 wme = &ic->ic_wme;
3311 wmep = wme->wme_chanParams.cap_wmeParams;
3313 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3314 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3315 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3316 device_get_nameunit(sc->dev),
3317 wmep[WME_AC_VO].wmep_aifsn,
3318 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3319 wmep[WME_AC_VO].wmep_txopLimit,
3320 wmep[WME_AC_VI].wmep_aifsn,
3321 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3322 wmep[WME_AC_VI].wmep_txopLimit,
3323 wmep[WME_AC_BK].wmep_aifsn,
3324 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3325 wmep[WME_AC_BK].wmep_txopLimit,
3326 wmep[WME_AC_BE].wmep_aifsn,
3327 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3328 wmep[WME_AC_BE].wmep_txopLimit);
3330 for (i = 0; i < WME_NUM_AC; i++)
3331 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3332 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3333 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3335 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3336 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3337 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3339 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3340 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3341 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3343 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3344 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3345 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3347 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3348 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3350 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3351 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3355 * rt2860_asic_update_beacon
3357 static int rt2860_asic_update_beacon(struct rt2860_softc *sc,
3358 struct ieee80211vap *vap)
3360 struct ifnet *ifp;
3361 struct ieee80211com *ic;
3362 struct rt2860_softc_vap *rvap;
3363 const struct ieee80211_txparam *tp;
3364 struct mbuf *m;
3365 struct rt2860_txwi txwi;
3366 uint8_t rate, mcs;
3367 uint32_t tmp;
3369 ifp = sc->ifp;
3370 ic = ifp->if_l2com;
3371 rvap = (struct rt2860_softc_vap *) vap;
3372 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
3374 m = ieee80211_beacon_alloc(vap->iv_bss, &rvap->beacon_offsets);
3375 if (m == NULL)
3376 return ENOMEM;
3378 rate = tp->mgmtrate;
3379 mcs = rt2860_rate2mcs(rate);
3381 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3383 txwi.wcid = 0xff;
3384 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3385 RT2860_TXWI_MPDU_LEN_SHIFT);
3386 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3387 txwi.mpdu_density_flags |=
3388 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3389 txwi.bawin_size_xflags |=
3390 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3392 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
3394 txwi.phymode_ifs_stbc_shortgi =
3395 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3397 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3398 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3400 else
3402 txwi.phymode_ifs_stbc_shortgi =
3403 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3406 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3407 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3409 /* disable temporarily TSF sync */
3411 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3413 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3414 RT2860_REG_TSF_TIMER_ENABLE |
3415 RT2860_REG_TBTT_TIMER_ENABLE);
3417 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3419 /* write Tx wireless info and beacon frame to on-chip memory */
3421 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3422 &txwi, sizeof(struct rt2860_txwi));
3424 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3425 mtod(m, uint8_t *), m->m_pkthdr.len);
3427 /* enable again TSF sync */
3429 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3431 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3432 RT2860_REG_TSF_TIMER_ENABLE |
3433 RT2860_REG_TBTT_TIMER_ENABLE);
3435 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3437 m_freem(m);
3439 return 0;
3443 * rt2860_asic_clear_keytables
3445 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3447 int i;
3449 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3451 for (i = 0; i < 256; i++)
3453 rt2860_io_mac_write(sc, RT2860_REG_WCID(i), 0xffffffff);
3454 rt2860_io_mac_write(sc, RT2860_REG_WCID(i) + 4, 0x0000ffff);
3457 /* clear WCID attribute table (entries = 256, entry size = 4) */
3459 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3461 /* clear IV/EIV table (entries = 256, entry size = 8) */
3463 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3465 /* clear pairwise key table (entries = 256, entry size = 32) */
3467 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3469 /* clear shared key table (entries = 32, entry size = 32) */
3471 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3473 /* clear shared key mode (entries = 32, entry size = 2) */
3475 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3479 * rt2860_rxrate
3481 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3483 uint8_t mcs, phymode;
3484 uint8_t rate;
3486 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3487 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3488 RT2860_RXWI_PHYMODE_MASK;
3490 rate = 2;
3492 switch (phymode)
3494 case RT2860_RXWI_PHYMODE_CCK:
3495 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3497 case 0: rate = 2; break; /* 1 Mbps */
3498 case 1: rate = 4; break; /* 2 MBps */
3499 case 2: rate = 11; break; /* 5.5 Mbps */
3500 case 3: rate = 22; break; /* 11 Mbps */
3502 break;
3504 case RT2860_RXWI_PHYMODE_OFDM:
3505 switch (mcs)
3507 case 0: rate = 12; break; /* 6 Mbps */
3508 case 1: rate = 18; break; /* 9 Mbps */
3509 case 2: rate = 24; break; /* 12 Mbps */
3510 case 3: rate = 36; break; /* 18 Mbps */
3511 case 4: rate = 48; break; /* 24 Mbps */
3512 case 5: rate = 72; break; /* 36 Mbps */
3513 case 6: rate = 96; break; /* 48 Mbps */
3514 case 7: rate = 108; break; /* 54 Mbps */
3516 break;
3518 case RT2860_RXWI_PHYMODE_HT_MIXED:
3519 case RT2860_RXWI_PHYMODE_HT_GF:
3520 break;
3523 return rate;
3527 * rt2860_maxrssi_rxpath
3529 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3530 const struct rt2860_rxwi *rxwi)
3532 uint8_t rxpath;
3534 rxpath = 0;
3536 if (sc->nrxpath > 1)
3537 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3538 rxpath = 1;
3540 if (sc->nrxpath > 2)
3541 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3542 rxpath = 2;
3544 return rxpath;
3548 * rt2860_rssi2dbm
3550 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3551 uint8_t rssi, uint8_t rxpath)
3553 struct ifnet *ifp;
3554 struct ieee80211com *ic;
3555 struct ieee80211_channel *c;
3556 int chan;
3557 int8_t rssi_off, lna_gain;
3559 if (rssi == 0)
3560 return -99;
3562 ifp = sc->ifp;
3563 ic = ifp->if_l2com;
3564 c = ic->ic_curchan;
3565 chan = ieee80211_chan2ieee(ic, c);
3567 if (IEEE80211_IS_CHAN_5GHZ(c))
3569 rssi_off = sc->rssi_off_5ghz[rxpath];
3571 if (chan <= 64)
3572 lna_gain = sc->lna_gain[1];
3573 else if (chan <= 128)
3574 lna_gain = sc->lna_gain[2];
3575 else
3576 lna_gain = sc->lna_gain[3];
3578 else
3580 rssi_off = sc->rssi_off_2ghz[rxpath] - sc->lna_gain[0];
3581 lna_gain = sc->lna_gain[0];
3584 return (-12 - rssi_off - lna_gain - rssi);
3588 * rt2860_rate2mcs
3590 static uint8_t rt2860_rate2mcs(uint8_t rate)
3592 switch (rate)
3594 /* CCK rates */
3595 case 2: return 0;
3596 case 4: return 1;
3597 case 11: return 2;
3598 case 22: return 3;
3600 /* OFDM rates */
3601 case 12: return 0;
3602 case 18: return 1;
3603 case 24: return 2;
3604 case 36: return 3;
3605 case 48: return 4;
3606 case 72: return 5;
3607 case 96: return 6;
3608 case 108: return 7;
3611 return 0;
3615 * rt2860_tx_mgmt
3617 static int rt2860_tx_mgmt(struct rt2860_softc *sc,
3618 struct mbuf *m, struct ieee80211_node *ni, int qid)
3620 struct ifnet *ifp;
3621 struct ieee80211com *ic;
3622 struct ieee80211vap *vap;
3623 const struct ieee80211_txparam *tp;
3624 struct rt2860_softc_node *rni;
3625 struct rt2860_softc_tx_ring *ring;
3626 struct rt2860_softc_tx_data *data;
3627 struct rt2860_txdesc *desc;
3628 struct rt2860_txwi *txwi;
3629 struct ieee80211_frame *wh;
3630 struct rt2860_softc_tx_radiotap_header *tap;
3631 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3632 u_int hdrsize, hdrspace;
3633 uint8_t rate, stbc, shortgi, mcs, pid, qsel;
3634 uint16_t len, dmalen, mpdu_len, dur;
3635 int error, ndmasegs, ndescs, i, j;
3637 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3638 ("%s: Tx MGMT: invalid qid=%d\n",
3639 device_get_nameunit(sc->dev), qid));
3641 ifp = sc->ifp;
3642 ic = ifp->if_l2com;
3643 vap = ni->ni_vap;
3644 rni = (struct rt2860_softc_node *) ni;
3645 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
3647 ring = &sc->tx_ring[qid];
3648 desc = &ring->desc[ring->desc_cur];
3649 data = &ring->data[ring->data_cur];
3650 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3652 wh = mtod(m, struct ieee80211_frame *);
3654 rate = tp->mgmtrate & IEEE80211_RATE_VAL;
3656 /* fill Tx wireless info */
3658 if (ni->ni_flags & IEEE80211_NODE_HT)
3659 mcs = rate;
3660 else
3661 mcs = rt2860_rate2mcs(rate);
3663 pid = mcs;
3665 /* calculate MPDU length without padding */
3667 hdrsize = ieee80211_hdrsize(wh);
3668 hdrspace = ieee80211_hdrspace(ic, wh);
3669 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3671 memset(txwi, 0, sizeof(struct rt2860_txwi));
3673 /* management frames do not need encryption */
3675 txwi->wcid = 0xff;
3677 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3678 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3679 RT2860_TXWI_MPDU_LEN_SHIFT);
3681 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
3683 shortgi = (vap->iv_flags_ht & (IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40)) &&
3684 (ni->ni_flags & IEEE80211_NODE_HT);
3686 txwi->phymode_ifs_stbc_shortgi |=
3687 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
3688 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3690 if (ni->ni_flags & IEEE80211_NODE_HT)
3692 txwi->phymode_ifs_stbc_shortgi |=
3693 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3695 else
3697 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
3699 txwi->phymode_ifs_stbc_shortgi |=
3700 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3702 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3703 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3705 else
3707 txwi->phymode_ifs_stbc_shortgi |=
3708 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3712 txwi->bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3713 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3715 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3717 /* skip ACKs for multicast frames and probe responses */
3719 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3720 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3721 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)))
3723 txwi->bawin_size_xflags |=
3724 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3726 if (ni->ni_flags & IEEE80211_NODE_HT)
3728 /* preamble + plcp + signal extension */
3730 dur = 16 + 4 + 6;
3732 else
3734 dur = ieee80211_ack_duration(ic->ic_rt, rate,
3735 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3738 *(uint16_t *) wh->i_dur = htole16(dur);
3741 /* ask MAC to insert timestamp into probe responses */
3743 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3744 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3745 txwi->mpdu_density_flags |=
3746 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3748 if (ieee80211_radiotap_active_vap(vap))
3750 tap = &sc->txtap;
3752 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3753 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3754 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3755 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3756 tap->chan_maxpow = 0;
3758 if (ni->ni_flags & IEEE80211_NODE_HT)
3759 tap->rate = mcs | IEEE80211_RATE_MCS;
3760 else
3761 tap->rate = rate;
3763 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3764 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3766 if (shortgi)
3767 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3769 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3770 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3772 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3774 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3776 ieee80211_radiotap_tx(vap, m);
3778 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3780 else
3782 ieee80211_radiotap_tx(vap, m);
3786 /* copy and trim 802.11 header */
3788 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3789 m_adj(m, hdrspace);
3791 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3792 dma_seg, &ndmasegs, 0);
3793 if (error != 0)
3795 /* too many fragments, linearize */
3797 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3798 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3799 device_get_nameunit(sc->dev));
3801 m = m_defrag(m, M_DONTWAIT);
3802 if (m == NULL)
3803 return ENOMEM;
3805 sc->tx_defrag_packets++;
3807 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3808 dma_seg, &ndmasegs, 0);
3809 if (error != 0)
3811 printf("%s: could not load mbuf DMA map: error=%d\n",
3812 device_get_nameunit(sc->dev), error);
3813 m_freem(m);
3814 return error;
3818 /* determine how many Tx descs are required */
3820 ndescs = 1 + ndmasegs / 2;
3821 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3823 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3824 "%s: there are not enough Tx descs\n",
3825 device_get_nameunit(sc->dev));
3827 sc->no_tx_desc_avail++;
3829 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3830 m_freem(m);
3831 return EFBIG;
3834 data->m = m;
3835 data->ni = ni;
3837 /* set up Tx descs */
3839 /* first segment is Tx wireless info and 802.11 header */
3841 len = sizeof(struct rt2860_txwi) + hdrsize;
3843 /* align end on a 4-bytes boundary */
3845 dmalen = (len + 3) & ~ 3;
3847 memset((caddr_t) txwi + len, 0, dmalen - len);
3849 qsel = RT2860_TXDESC_QSEL_EDCA;
3851 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3852 desc->sdl0 = htole16(dmalen);
3853 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3855 /* set up payload segments */
3857 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3859 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3860 desc->sdl1 = htole16(dma_seg[j].ds_len);
3862 ring->desc_queued++;
3863 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3865 j++;
3867 desc = &ring->desc[ring->desc_cur];
3869 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3870 desc->sdl0 = htole16(dma_seg[j].ds_len);
3871 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3873 j++;
3876 /* finalize last payload segment */
3878 if (i > 0)
3880 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3881 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3883 else
3885 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3886 desc->sdl1 = 0;
3889 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3890 "%s: sending MGMT frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3891 "stbc=%d, shortgi=%d, mcs=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3892 device_get_nameunit(sc->dev),
3893 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3894 stbc, shortgi, mcs, dmalen, ndmasegs,
3895 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);
3897 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3898 BUS_DMASYNC_PREWRITE);
3899 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3900 BUS_DMASYNC_PREWRITE);
3901 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3902 BUS_DMASYNC_PREWRITE);
3904 ring->desc_queued++;
3905 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3907 ring->data_queued++;
3908 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3910 /* kick Tx */
3912 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3914 return 0;
3918 * rt2860_tx_data
3920 static int rt2860_tx_data(struct rt2860_softc *sc,
3921 struct mbuf *m, struct ieee80211_node *ni, int qid)
3923 struct ifnet *ifp;
3924 struct ieee80211com *ic;
3925 struct ieee80211vap *vap;
3926 const struct ieee80211_txparam *tp;
3927 struct rt2860_softc_node *rni;
3928 struct rt2860_softc_tx_ring *ring;
3929 struct rt2860_softc_tx_data *data;
3930 struct rt2860_txdesc *desc;
3931 struct rt2860_txwi *txwi;
3932 struct ieee80211_frame *wh;
3933 struct rt2860_softc_tx_radiotap_header *tap;
3934 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3935 u_int hdrsize, hdrspace;
3936 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, qsel;
3937 uint16_t qos, len, dmalen, mpdu_len, dur;
3938 int error, hasqos, ndmasegs, ndescs, i, j;
3940 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3941 ("%s: Tx data: invalid qid=%d\n",
3942 device_get_nameunit(sc->dev), qid));
3944 ifp = sc->ifp;
3945 ic = ifp->if_l2com;
3946 vap = ni->ni_vap;
3947 rni = (struct rt2860_softc_node *) ni;
3948 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
3950 ring = &sc->tx_ring[qid];
3951 desc = &ring->desc[ring->desc_cur];
3952 data = &ring->data[ring->data_cur];
3953 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3955 wh = mtod(m, struct ieee80211_frame *);
3957 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3959 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3960 if (hasqos)
3962 if (IEEE80211_HAS_ADDR4(wh))
3963 qos = le16toh(*(const uint16_t *)
3964 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3965 else
3966 qos = le16toh(*(const uint16_t *)
3967 (((struct ieee80211_qosframe *) wh)->i_qos));
3969 else
3971 qos = 0;
3974 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
3975 rate = tp->mcastrate;
3976 else if (m->m_flags & M_EAPOL)
3977 rate = tp->mgmtrate;
3978 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3979 rate = tp->ucastrate;
3980 else
3981 rate = ni->ni_txrate;
3983 rate &= IEEE80211_RATE_VAL;
3985 /* fill Tx wireless info */
3987 if (ni->ni_flags & IEEE80211_NODE_HT)
3988 mcs = rate;
3989 else
3990 mcs = rt2860_rate2mcs(rate);
3992 pid = mcs;
3994 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3995 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3997 /* calculate MPDU length without padding */
3999 hdrsize = ieee80211_hdrsize(wh);
4000 hdrspace = ieee80211_hdrspace(ic, wh);
4001 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
4003 memset(txwi, 0, sizeof(struct rt2860_txwi));
4005 txwi->wcid = wcid;
4007 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
4008 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
4009 RT2860_TXWI_MPDU_LEN_SHIFT);
4011 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
4013 shortgi = (vap->iv_flags_ht & (IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40)) &&
4014 (ni->ni_flags & IEEE80211_NODE_HT);
4016 txwi->phymode_ifs_stbc_shortgi |=
4017 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
4018 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
4020 if (ni->ni_flags & IEEE80211_NODE_HT)
4022 txwi->phymode_ifs_stbc_shortgi |=
4023 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
4025 else
4027 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
4029 txwi->phymode_ifs_stbc_shortgi |=
4030 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
4032 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4033 mcs |= RT2860_TXWI_MCS_SHOTPRE;
4035 else
4037 txwi->phymode_ifs_stbc_shortgi |=
4038 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
4042 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
4043 (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40))
4044 bw = RT2860_TXWI_BW_40;
4045 else
4046 bw = RT2860_TXWI_BW_20;
4048 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
4049 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
4051 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
4053 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
4054 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
4056 txwi->bawin_size_xflags |=
4057 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
4059 if (ni->ni_flags & IEEE80211_NODE_HT)
4061 /* preamble + plcp + signal extension */
4063 dur = 16 + 4 + 6;
4065 else
4067 dur = ieee80211_ack_duration(ic->ic_rt, rate,
4068 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
4071 *(uint16_t *) wh->i_dur = htole16(dur);
4074 /* ask MAC to insert timestamp into probe responses */
4076 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
4077 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
4078 txwi->mpdu_density_flags |=
4079 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
4081 if (ieee80211_radiotap_active_vap(vap))
4083 tap = &sc->txtap;
4085 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4086 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4087 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4088 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4089 tap->chan_maxpow = 0;
4091 if (ni->ni_flags & IEEE80211_NODE_HT)
4092 tap->rate = mcs | IEEE80211_RATE_MCS;
4093 else
4094 tap->rate = rate;
4096 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
4097 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4099 if (shortgi)
4100 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4102 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4103 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4105 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4107 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4109 ieee80211_radiotap_tx(vap, m);
4111 wh->i_fc[1] |= IEEE80211_FC1_WEP;
4113 else
4115 ieee80211_radiotap_tx(vap, m);
4119 /* copy and trim 802.11 header */
4121 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
4122 m_adj(m, hdrspace);
4124 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4125 dma_seg, &ndmasegs, 0);
4126 if (error != 0)
4128 /* too many fragments, linearize */
4130 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4131 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
4132 device_get_nameunit(sc->dev));
4134 m = m_defrag(m, M_DONTWAIT);
4135 if (m == NULL)
4136 return ENOMEM;
4138 sc->tx_defrag_packets++;
4140 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4141 dma_seg, &ndmasegs, 0);
4142 if (error != 0)
4144 printf("%s: could not load mbuf DMA map: error=%d\n",
4145 device_get_nameunit(sc->dev), error);
4146 m_freem(m);
4147 return error;
4151 /* determine how many Tx descs are required */
4153 ndescs = 1 + ndmasegs / 2;
4154 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
4156 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4157 "%s: there are not enough Tx descs\n",
4158 device_get_nameunit(sc->dev));
4160 sc->no_tx_desc_avail++;
4162 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4163 m_freem(m);
4164 return EFBIG;
4167 data->m = m;
4168 data->ni = ni;
4170 /* set up Tx descs */
4172 /* first segment is Tx wireless info and 802.11 header */
4174 len = sizeof(struct rt2860_txwi) + hdrsize;
4176 /* align end on a 4-bytes boundary */
4178 dmalen = (len + 3) & ~ 3;
4180 memset((caddr_t) txwi + len, 0, dmalen - len);
4182 qsel = RT2860_TXDESC_QSEL_EDCA;
4184 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
4185 desc->sdl0 = htole16(dmalen);
4186 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4188 /* set up payload segments */
4190 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
4192 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4193 desc->sdl1 = htole16(dma_seg[j].ds_len);
4195 ring->desc_queued++;
4196 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4198 j++;
4200 desc = &ring->desc[ring->desc_cur];
4202 desc->sdp0 = htole32(dma_seg[j].ds_addr);
4203 desc->sdl0 = htole16(dma_seg[j].ds_len);
4204 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4206 j++;
4209 /* finalize last payload segment */
4211 if (i > 0)
4213 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4214 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
4216 else
4218 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
4219 desc->sdl1 = 0;
4222 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4223 "%s: sending data: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4224 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
4225 device_get_nameunit(sc->dev),
4226 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
4227 bw, stbc, shortgi, mcs, wcid, dmalen, ndmasegs,
4228 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);
4230 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
4231 BUS_DMASYNC_PREWRITE);
4232 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4233 BUS_DMASYNC_PREWRITE);
4234 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4235 BUS_DMASYNC_PREWRITE);
4237 ring->desc_queued++;
4238 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4240 ring->data_queued++;
4241 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4243 /* kick Tx */
4245 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
4247 return 0;
4251 * rt2860_tx_raw
4253 static int rt2860_tx_raw(struct rt2860_softc *sc,
4254 struct mbuf *m, struct ieee80211_node *ni,
4255 const struct ieee80211_bpf_params *params)
4257 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4258 "%s: Tx raw\n",
4259 device_get_nameunit(sc->dev));
4261 return 0;
4265 * rt2860_intr
4267 static void rt2860_intr(void *arg)
4269 struct rt2860_softc *sc;
4270 struct ifnet *ifp;
4271 uint32_t status;
4273 sc = arg;
4274 ifp = sc->ifp;
4276 /* acknowledge interrupts */
4278 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
4279 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
4281 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4282 "%s: interrupt: status = 0x%08x\n",
4283 device_get_nameunit(sc->dev), status);
4285 if (status == 0xffffffff || /* device likely went away */
4286 status == 0) /* not for us */
4287 return;
4289 sc->interrupts++;
4291 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4292 return;
4294 if (status & RT2860_REG_INT_TX_COHERENT)
4295 rt2860_tx_coherent_intr(sc);
4297 if (status & RT2860_REG_INT_RX_COHERENT)
4298 rt2860_rx_coherent_intr(sc);
4300 if (status & RT2860_REG_INT_TXRX_COHERENT)
4301 rt2860_txrx_coherent_intr(sc);
4303 if (status & RT2860_REG_INT_FIFO_STA_FULL)
4304 rt2860_fifo_sta_full_intr(sc);
4306 if (status & RT2860_REG_INT_TX_MGMT_DONE)
4307 rt2860_tx_intr(sc, 5);
4309 if (status & RT2860_REG_INT_RX_DONE)
4310 rt2860_rx_intr(sc);
4312 if (status & RT2860_REG_INT_RX_DELAY_DONE)
4313 rt2860_rx_delay_intr(sc);
4315 if (status & RT2860_REG_INT_TX_HCCA_DONE)
4316 rt2860_tx_intr(sc, 4);
4318 if (status & RT2860_REG_INT_TX_AC3_DONE)
4319 rt2860_tx_intr(sc, 3);
4321 if (status & RT2860_REG_INT_TX_AC2_DONE)
4322 rt2860_tx_intr(sc, 2);
4324 if (status & RT2860_REG_INT_TX_AC1_DONE)
4325 rt2860_tx_intr(sc, 1);
4327 if (status & RT2860_REG_INT_TX_AC0_DONE)
4328 rt2860_tx_intr(sc, 0);
4330 if (status & RT2860_REG_INT_TX_DELAY_DONE)
4331 rt2860_tx_delay_intr(sc);
4333 if (status & RT2860_REG_INT_PRE_TBTT)
4334 rt2860_pre_tbtt_intr(sc);
4336 if (status & RT2860_REG_INT_TBTT)
4337 rt2860_tbtt_intr(sc);
4339 if (status & RT2860_REG_INT_MCU_CMD)
4340 rt2860_mcu_cmd_intr(sc);
4342 if (status & RT2860_REG_INT_AUTO_WAKEUP)
4343 rt2860_auto_wakeup_intr(sc);
4345 if (status & RT2860_REG_INT_GP_TIMER)
4346 rt2860_gp_timer_intr(sc);
4350 * rt2860_tx_coherent_intr
4352 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
4354 uint32_t tmp;
4355 int i;
4357 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4358 "%s: Tx coherent interrupt\n",
4359 device_get_nameunit(sc->dev));
4361 sc->tx_coherent_interrupts++;
4363 /* restart DMA engine */
4365 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4367 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4368 RT2860_REG_RX_DMA_ENABLE |
4369 RT2860_REG_TX_DMA_ENABLE);
4371 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4373 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4375 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4376 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4378 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4380 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4381 sc->tx_ring[i].desc_phys_addr);
4382 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4383 RT2860_SOFTC_TX_RING_DESC_COUNT);
4384 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4387 /* init Rx ring */
4389 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4391 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4392 sc->rx_ring.desc_phys_addr);
4393 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4394 RT2860_SOFTC_RX_RING_DATA_COUNT);
4395 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4396 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4398 rt2860_txrx_enable(sc);
4402 * rt2860_rx_coherent_intr
4404 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
4406 uint32_t tmp;
4407 int i;
4409 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4410 "%s: Rx coherent interrupt\n",
4411 device_get_nameunit(sc->dev));
4413 sc->rx_coherent_interrupts++;
4415 /* restart DMA engine */
4417 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4419 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4420 RT2860_REG_RX_DMA_ENABLE |
4421 RT2860_REG_TX_DMA_ENABLE);
4423 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4425 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4427 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4428 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4430 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4432 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4433 sc->tx_ring[i].desc_phys_addr);
4434 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4435 RT2860_SOFTC_TX_RING_DESC_COUNT);
4436 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4439 /* init Rx ring */
4441 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4443 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4444 sc->rx_ring.desc_phys_addr);
4445 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4446 RT2860_SOFTC_RX_RING_DATA_COUNT);
4447 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4448 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4450 rt2860_txrx_enable(sc);
4454 * rt2860_txrx_coherent_intr
4456 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
4458 uint32_t tmp;
4459 int i;
4461 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4462 "%s: Tx/Rx coherent interrupt\n",
4463 device_get_nameunit(sc->dev));
4465 sc->txrx_coherent_interrupts++;
4467 /* restart DMA engine */
4469 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4471 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4472 RT2860_REG_RX_DMA_ENABLE |
4473 RT2860_REG_TX_DMA_ENABLE);
4475 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4477 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4479 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4480 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4482 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4484 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4485 sc->tx_ring[i].desc_phys_addr);
4486 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4487 RT2860_SOFTC_TX_RING_DESC_COUNT);
4488 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4491 /* init Rx ring */
4493 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4495 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4496 sc->rx_ring.desc_phys_addr);
4497 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4498 RT2860_SOFTC_RX_RING_DATA_COUNT);
4499 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4500 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4502 rt2860_txrx_enable(sc);
4506 * rt2860_fifo_sta_full_intr
4508 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4510 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4511 "%s: FIFO statistic full interrupt\n",
4512 device_get_nameunit(sc->dev));
4514 sc->fifo_sta_full_interrupts++;
4516 RT2860_SOFTC_LOCK(sc);
4518 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4520 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4522 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4525 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4527 RT2860_SOFTC_UNLOCK(sc);
4531 * rt2860_rx_intr
4533 static void rt2860_rx_intr(struct rt2860_softc *sc)
4535 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4536 "%s: Rx interrupt\n",
4537 device_get_nameunit(sc->dev));
4539 sc->rx_interrupts++;
4541 RT2860_SOFTC_LOCK(sc);
4543 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4545 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4547 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4550 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4552 RT2860_SOFTC_UNLOCK(sc);
4556 * rt2860_rx_delay_intr
4558 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4560 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4561 "%s: Rx delay interrupt\n",
4562 device_get_nameunit(sc->dev));
4564 sc->rx_delay_interrupts++;
4568 * rt2860_tx_intr
4570 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4572 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4573 ("%s: Tx interrupt: invalid qid=%d\n",
4574 device_get_nameunit(sc->dev), qid));
4576 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4577 "%s: Tx interrupt: qid=%d\n",
4578 device_get_nameunit(sc->dev), qid);
4580 sc->tx_interrupts[qid]++;
4582 RT2860_SOFTC_LOCK(sc);
4584 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4586 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4588 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4591 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4593 RT2860_SOFTC_UNLOCK(sc);
4597 * rt2860_tx_delay_intr
4599 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4601 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4602 "%s: Tx delay interrupt\n",
4603 device_get_nameunit(sc->dev));
4605 sc->tx_delay_interrupts++;
4609 * rt2860_pre_tbtt_intr
4611 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4613 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4614 "%s: Pre-TBTT interrupt\n",
4615 device_get_nameunit(sc->dev));
4617 sc->pre_tbtt_interrupts++;
4621 * rt2860_tbtt_intr
4623 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4625 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4626 "%s: TBTT interrupt\n",
4627 device_get_nameunit(sc->dev));
4629 sc->tbtt_interrupts++;
4633 * rt2860_mcu_cmd_intr
4635 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4637 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4638 "%s: MCU command interrupt\n",
4639 device_get_nameunit(sc->dev));
4641 sc->mcu_cmd_interrupts++;
4645 * rt2860_auto_wakeup_intr
4647 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4649 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4650 "%s: auto wakeup interrupt\n",
4651 device_get_nameunit(sc->dev));
4653 sc->auto_wakeup_interrupts++;
4657 * rt2860_gp_timer_intr
4659 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4661 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4662 "%s: GP timer interrupt\n",
4663 device_get_nameunit(sc->dev));
4665 sc->gp_timer_interrupts++;
4669 * rt2860_rx_done_task
4671 static void rt2860_rx_done_task(void *context, int pending)
4673 struct rt2860_softc *sc;
4674 struct ifnet *ifp;
4675 int again;
4677 sc = context;
4678 ifp = sc->ifp;
4680 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4681 "%s: Rx done task\n",
4682 device_get_nameunit(sc->dev));
4684 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4685 return;
4687 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4689 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4691 RT2860_SOFTC_LOCK(sc);
4693 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4695 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4696 "%s: Rx done task: scheduling again\n",
4697 device_get_nameunit(sc->dev));
4699 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4701 else
4703 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4706 RT2860_SOFTC_UNLOCK(sc);
4710 * rt2860_tx_done_task
4712 static void rt2860_tx_done_task(void *context, int pending)
4714 struct rt2860_softc *sc;
4715 struct ifnet *ifp;
4716 uint32_t intr_mask;
4717 int i;
4719 sc = context;
4720 ifp = sc->ifp;
4722 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4723 "%s: Tx done task\n",
4724 device_get_nameunit(sc->dev));
4726 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4727 return;
4729 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4730 RT2860_REG_INT_TX_HCCA_DONE |
4731 RT2860_REG_INT_TX_AC3_DONE |
4732 RT2860_REG_INT_TX_AC2_DONE |
4733 RT2860_REG_INT_TX_AC1_DONE |
4734 RT2860_REG_INT_TX_AC0_DONE);
4736 sc->intr_pending_mask &= ~intr_mask;
4738 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4739 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4741 sc->tx_timer = 0;
4743 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4745 RT2860_SOFTC_LOCK(sc);
4747 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4748 (sc->intr_disable_mask & intr_mask));
4750 if (sc->intr_pending_mask & intr_mask)
4752 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4753 "%s: Tx done task: scheduling again\n",
4754 device_get_nameunit(sc->dev));
4756 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4759 RT2860_SOFTC_UNLOCK(sc);
4763 * rt2860_fifo_sta_full_task
4765 static void rt2860_fifo_sta_full_task(void *context, int pending)
4767 struct rt2860_softc *sc;
4768 struct ifnet *ifp;
4770 sc = context;
4771 ifp = sc->ifp;
4773 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4774 "%s: FIFO statistic full task\n",
4775 device_get_nameunit(sc->dev));
4777 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4778 return;
4780 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4782 rt2860_drain_fifo_stats(sc);
4784 RT2860_SOFTC_LOCK(sc);
4786 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4788 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4789 "%s: FIFO statistic full task: scheduling again\n",
4790 device_get_nameunit(sc->dev));
4792 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4794 else
4796 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4799 RT2860_SOFTC_UNLOCK(sc);
4803 * rt2860_periodic_task
4805 static void rt2860_periodic_task(void *context, int pending)
4807 struct rt2860_softc *sc;
4808 struct ifnet *ifp;
4809 struct ieee80211com *ic;
4810 struct ieee80211vap *vap;
4812 sc = context;
4813 ifp = sc->ifp;
4814 ic = ifp->if_l2com;
4816 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4817 "%s: periodic task: round=%lu\n",
4818 device_get_nameunit(sc->dev), sc->periodic_round);
4820 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4821 return;
4823 RT2860_SOFTC_LOCK(sc);
4825 sc->periodic_round++;
4827 rt2860_update_stats(sc);
4829 if ((sc->periodic_round % 10) == 0)
4831 rt2860_update_raw_counters(sc);
4833 rt2860_watchdog(sc);
4835 vap = TAILQ_FIRST(&ic->ic_vaps);
4836 if (vap != NULL)
4838 if (vap->iv_opmode == IEEE80211_M_STA)
4839 rt2860_amrr_update_iter_func(vap, vap->iv_bss);
4840 else
4841 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, vap);
4845 RT2860_SOFTC_UNLOCK(sc);
4847 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4851 * rt2860_rx_eof
4853 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4855 struct ifnet *ifp;
4856 struct ieee80211com *ic;
4857 struct ieee80211_frame *wh;
4858 struct ieee80211_node *ni;
4859 struct rt2860_softc_rx_radiotap_header *tap;
4860 struct rt2860_softc_rx_ring *ring;
4861 struct rt2860_rxdesc *desc;
4862 struct rt2860_softc_rx_data *data;
4863 struct rt2860_rxwi *rxwi;
4864 struct mbuf *m, *mnew;
4865 bus_dma_segment_t segs[1];
4866 uint32_t index, desc_flags;
4867 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4868 int8_t rssi_dbm;
4869 int error, nsegs, len, ampdu, amsdu, nframes;
4871 ifp = sc->ifp;
4872 ic = ifp->if_l2com;
4873 ring = &sc->rx_ring;
4875 nframes = 0;
4877 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4878 BUS_DMASYNC_POSTREAD);
4880 while (limit != 0)
4882 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4883 if (ring->cur == index)
4884 break;
4886 desc = &ring->desc[ring->cur];
4887 desc_flags = le32toh(desc->flags);
4888 data = &ring->data[ring->cur];
4890 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4891 break;
4893 nframes++;
4895 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4896 if (mnew == NULL)
4898 sc->rx_mbuf_alloc_errors++;
4899 ifp->if_ierrors++;
4900 goto skip;
4903 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4905 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4906 BUS_DMASYNC_POSTREAD);
4907 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4909 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4910 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4911 if (error != 0)
4913 m_freem(mnew);
4915 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4916 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
4917 if (error != 0)
4918 panic("%s: could not load old Rx mbuf: error=%d\n",
4919 device_get_name(sc->dev), error);
4921 sc->rx_mbuf_dmamap_errors++;
4922 ifp->if_ierrors++;
4924 goto skip;
4927 KASSERT(nsegs == 1, ("%s: too many DMA segments",
4928 device_get_nameunit(sc->dev)));
4930 m = data->m;
4932 data->m = mnew;
4933 desc->sdp0 = htole32(segs[0].ds_addr);
4935 /* get Rx wireless info */
4937 rxwi = mtod(m, struct rt2860_rxwi *);
4938 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4939 RT2860_RXWI_SIZE_MASK;
4941 /* check for L2 padding between IEEE 802.11 frame header and body */
4943 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4945 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4946 "%s: L2 padding: len=%d\n",
4947 device_get_nameunit(sc->dev), len);
4949 len += 2;
4952 m->m_pkthdr.rcvif = ifp;
4953 m->m_data = (caddr_t) (rxwi + 1);
4954 m->m_pkthdr.len = m->m_len = len;
4956 /* check for crc errors */
4958 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4960 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4961 "%s: rxdesc: crc error\n",
4962 device_get_nameunit(sc->dev));
4964 ifp->if_ierrors++;
4966 if (!(ifp->if_flags & IFF_PROMISC))
4968 m_freem(m);
4969 goto skip;
4973 wh = (struct ieee80211_frame *) (rxwi + 1);
4975 /* check for cipher errors */
4977 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4979 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4980 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4981 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4983 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4984 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4986 m->m_flags |= M_WEP;
4988 else
4990 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4991 "%s: rxdesc: cipher error=0x%02x\n",
4992 device_get_nameunit(sc->dev), cipher_err);
4994 ifp->if_ierrors++;
4996 if (!(ifp->if_flags & IFF_PROMISC))
4998 m_free(m);
4999 goto skip;
5003 else
5005 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
5007 ifp->if_ierrors++;
5009 if (!(ifp->if_flags & IFF_PROMISC))
5011 m_free(m);
5012 goto skip;
5017 /* check for A-MPDU */
5019 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
5021 m->m_flags |= M_AMPDU;
5022 ampdu = 1;
5024 else
5026 ampdu = 0;
5029 ant = rt2860_maxrssi_rxpath(sc, rxwi);
5030 rssi = rxwi->rssi[ant];
5031 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
5032 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
5033 RT2860_RXWI_PHYMODE_MASK);
5034 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
5035 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
5036 RT2860_RXWI_SHORTGI_MASK);
5037 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
5038 amsdu = (desc_flags & RT2860_RXDESC_FLAGS_AMSDU);
5040 if (ieee80211_radiotap_active(ic))
5042 tap = &sc->rxtap;
5044 tap->flags = (desc_flags & RT2860_RXDESC_FLAGS_L2PAD) ? IEEE80211_RADIOTAP_F_DATAPAD : 0;
5045 tap->dbm_antsignal = rssi_dbm;
5046 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
5047 tap->antenna = ant;
5048 tap->antsignal = rssi;
5049 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
5050 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
5051 tap->chan_ieee = ic->ic_curchan->ic_ieee;
5052 tap->chan_maxpow = 0;
5054 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
5055 tap->rate = mcs | IEEE80211_RATE_MCS;
5056 else
5057 tap->rate = rt2860_rxrate(rxwi);
5059 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
5060 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5062 if (shortgi)
5063 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
5066 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5067 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
5068 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
5069 device_get_nameunit(sc->dev),
5070 len, phymode, bw, shortgi, mcs,
5071 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
5072 rxwi->snr[0], rxwi->snr[1],
5073 rxwi->wcid, ampdu, amsdu);
5075 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
5076 if (ni != NULL)
5078 ieee80211_input(ni, m, rssi_dbm - RT2860_NOISE_FLOOR, RT2860_NOISE_FLOOR);
5079 ieee80211_free_node(ni);
5081 else
5083 ieee80211_input_all(ic, m, rssi_dbm - RT2860_NOISE_FLOOR, RT2860_NOISE_FLOOR);
5086 skip:
5088 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5090 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5091 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5093 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
5095 limit--;
5098 if (ring->cur == 0)
5099 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5100 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
5101 else
5102 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5103 ring->cur - 1);
5105 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5106 "%s: Rx eof: nframes=%d\n",
5107 device_get_nameunit(sc->dev), nframes);
5109 sc->rx_packets += nframes;
5111 return (limit == 0);
5115 * rt2860_tx_eof
5117 static void rt2860_tx_eof(struct rt2860_softc *sc,
5118 struct rt2860_softc_tx_ring *ring)
5120 struct ifnet *ifp;
5121 struct rt2860_txdesc *desc;
5122 struct rt2860_softc_tx_data *data;
5123 uint32_t index;
5124 int ndescs, nframes;
5126 ifp = sc->ifp;
5128 ndescs = 0;
5129 nframes = 0;
5131 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5132 BUS_DMASYNC_POSTREAD);
5134 for (;;)
5136 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
5137 if (ring->desc_next == index)
5138 break;
5140 ndescs++;
5142 rt2860_drain_fifo_stats(sc);
5144 desc = &ring->desc[ring->desc_next];
5146 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
5147 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
5149 nframes++;
5151 data = &ring->data[ring->data_next];
5153 if (data->m->m_flags & M_TXCB)
5154 ieee80211_process_callback(data->ni, data->m, 0);
5156 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5157 BUS_DMASYNC_POSTWRITE);
5158 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5160 m_freem(data->m);
5162 ieee80211_free_node(data->ni);
5164 data->m = NULL;
5165 data->ni = NULL;
5167 ifp->if_opackets++;
5169 ring->data_queued--;
5170 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
5173 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
5175 ring->desc_queued--;
5176 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
5179 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5180 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5182 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
5183 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
5184 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
5188 * rt2860_update_stats
5190 static void rt2860_update_stats(struct rt2860_softc *sc)
5192 struct ifnet *ifp;
5193 struct ieee80211com *ic;
5194 struct ieee80211vap *vap;
5195 struct ieee80211_node *ni;
5196 uint16_t associd;
5197 uint8_t wcid;
5198 uint32_t stacnt[3];
5199 int txcnt, retrycnt, failcnt;
5201 ifp = sc->ifp;
5202 ic = ifp->if_l2com;
5204 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5205 "%s: update statistic\n",
5206 device_get_nameunit(sc->dev));
5208 rt2860_drain_fifo_stats(sc);
5210 if (ic->ic_opmode == IEEE80211_M_STA)
5212 vap = TAILQ_FIRST(&ic->ic_vaps);
5213 ni = vap->iv_bss;
5215 associd = (ni != NULL) ? ni->ni_associd : 0;
5216 wcid = RT2860_AID2WCID(associd);
5218 /* read and clear Tx statistic registers */
5220 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
5221 stacnt, sizeof(stacnt));
5223 txcnt = le32toh(stacnt[1]) & 0xffff;
5224 retrycnt = le32toh(stacnt[1]) >> 16;
5225 failcnt = le32toh(stacnt[0]) & 0xffff;
5227 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5228 "%s: update statistic: associd=0x%04x, txcnt=%d, retrycnt=%d, failcnt=%d\n",
5229 device_get_nameunit(sc->dev),
5230 associd, txcnt, retrycnt, failcnt);
5232 ifp->if_oerrors += failcnt;
5234 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
5235 txcnt + retrycnt + failcnt, txcnt + retrycnt, retrycnt + failcnt);
5240 * rt2860_watchdog
5242 static void rt2860_watchdog(struct rt2860_softc *sc)
5244 uint32_t tmp;
5245 int ntries;
5247 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5249 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
5250 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
5251 device_get_nameunit(sc->dev), tmp);
5253 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
5255 sc->tx_queue_not_empty[0]++;
5257 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
5259 for (ntries = 0; ntries < 10; ntries++)
5261 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5262 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
5263 break;
5265 DELAY(1);
5268 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5271 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
5273 sc->tx_queue_not_empty[1]++;
5275 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
5277 for (ntries = 0; ntries < 10; ntries++)
5279 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5280 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
5281 break;
5283 DELAY(1);
5286 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5291 * rt2860_drain_fifo_stats
5293 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
5295 struct ifnet *ifp;
5296 uint32_t stats;
5297 uint8_t wcid, mcs, pid;
5298 int ok, retrycnt;
5300 ifp = sc->ifp;
5302 /* drain Tx status FIFO (maxsize = 16) */
5304 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
5305 RT2860_REG_TX_STA_FIFO_VALID)
5307 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
5308 RT2860_REG_TX_STA_FIFO_WCID_MASK;
5310 /* if no ACK was requested, no feedback is available */
5312 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
5313 continue;
5315 /* update AMRR statistic */
5317 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
5318 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
5319 RT2860_REG_TX_STA_FIFO_MCS_MASK;
5320 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
5321 RT2860_REG_TX_STA_FIFO_PID_MASK;
5322 retrycnt = pid - mcs;
5324 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5325 "%s: FIFO statistic: wcid=0x%02x, ok=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
5326 device_get_nameunit(sc->dev),
5327 wcid, ok, mcs, pid, retrycnt);
5329 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
5331 if (!ok)
5332 ifp->if_oerrors++;
5337 * rt2860_update_raw_counters
5339 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
5341 uint32_t tmp;
5343 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
5345 sc->rx_crc_errors += tmp & 0xffff;
5346 sc->rx_phy_errors += tmp >> 16;
5348 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
5350 sc->rx_false_ccas += tmp & 0xffff;
5351 sc->rx_plcp_errors += tmp >> 16;
5353 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
5355 sc->rx_dup_packets += tmp & 0xffff;
5356 sc->rx_fifo_overflows += tmp >> 16;
5360 * rt2860_intr_enable
5362 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
5364 uint32_t tmp;
5366 sc->intr_disable_mask &= ~intr_mask;
5368 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5370 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5374 * rt2860_intr_disable
5376 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
5378 uint32_t tmp;
5380 sc->intr_disable_mask |= intr_mask;
5382 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5384 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5388 * rt2860_txrx_enable
5390 static int rt2860_txrx_enable(struct rt2860_softc *sc)
5392 struct ieee80211com *ic;
5393 struct ifnet *ifp;
5394 uint32_t tmp;
5395 int ntries;
5397 ifp = sc->ifp;
5398 ic = ifp->if_l2com;
5400 /* enable Tx/Rx DMA engine */
5402 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
5404 for (ntries = 0; ntries < 200; ntries++)
5406 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
5407 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
5408 break;
5410 DELAY(1000);
5413 if (ntries == 200)
5415 printf("%s: timeout waiting for DMA engine\n",
5416 device_get_nameunit(sc->dev));
5417 return -1;
5420 DELAY(50);
5422 tmp |= RT2860_REG_TX_WB_DDONE |
5423 RT2860_REG_RX_DMA_ENABLE |
5424 RT2860_REG_TX_DMA_ENABLE |
5425 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
5427 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5429 /* set Rx filter */
5431 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
5432 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
5434 if (ic->ic_opmode != IEEE80211_M_MONITOR)
5436 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
5437 RT2860_REG_RX_FILTER_DROP_CTS |
5438 RT2860_REG_RX_FILTER_DROP_BA |
5439 RT2860_REG_RX_FILTER_DROP_ACK |
5440 RT2860_REG_RX_FILTER_DROP_VER_ERR |
5441 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
5442 RT2860_REG_RX_FILTER_DROP_CFACK |
5443 RT2860_REG_RX_FILTER_DROP_CFEND;
5445 if (ic->ic_opmode == IEEE80211_M_STA)
5446 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
5447 RT2860_REG_RX_FILTER_DROP_PSPOLL;
5449 if (!(ifp->if_flags & IFF_PROMISC))
5450 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
5453 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
5455 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
5456 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
5458 return 0;
5462 * rt2860_alloc_rx_ring
5464 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
5465 struct rt2860_softc_rx_ring *ring)
5467 struct rt2860_rxdesc *desc;
5468 struct rt2860_softc_rx_data *data;
5469 bus_dma_segment_t segs[1];
5470 int i, nsegs, error;
5472 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5473 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5474 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
5475 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5476 0, NULL, NULL, &ring->desc_dma_tag);
5477 if (error != 0)
5479 printf("%s: could not create Rx desc DMA tag\n",
5480 device_get_nameunit(sc->dev));
5481 goto fail;
5484 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5485 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5486 if (error != 0)
5488 printf("%s: could not allocate Rx desc DMA memory\n",
5489 device_get_nameunit(sc->dev));
5490 goto fail;
5493 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5494 ring->desc,
5495 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5496 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5497 if (error != 0)
5499 printf("%s: could not load Rx desc DMA map\n",
5500 device_get_nameunit(sc->dev));
5501 goto fail;
5504 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5505 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5506 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
5507 &ring->data_dma_tag);
5508 if (error != 0)
5510 printf("%s: could not create Rx data DMA tag\n",
5511 device_get_nameunit(sc->dev));
5512 goto fail;
5515 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5517 desc = &ring->desc[i];
5518 data = &ring->data[i];
5520 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5521 if (error != 0)
5523 printf("%s: could not create Rx data DMA map\n",
5524 device_get_nameunit(sc->dev));
5525 goto fail;
5528 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5529 if (data->m == NULL)
5531 printf("%s: could not allocate Rx mbuf\n",
5532 device_get_nameunit(sc->dev));
5533 error = ENOMEM;
5534 goto fail;
5537 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5539 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5540 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5541 if (error != 0)
5543 printf("%s: could not load Rx mbuf DMA map\n",
5544 device_get_nameunit(sc->dev));
5545 goto fail;
5548 KASSERT(nsegs == 1, ("%s: too many DMA segments",
5549 device_get_nameunit(sc->dev)));
5551 desc->sdp0 = htole32(segs[0].ds_addr);
5552 desc->sdl0 = htole16(MJUMPAGESIZE);
5555 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5556 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5558 return 0;
5560 fail:
5562 rt2860_free_rx_ring(sc, ring);
5564 return error;
5568 * rt2860_reset_rx_ring
5570 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5571 struct rt2860_softc_rx_ring *ring)
5573 struct rt2860_rxdesc *desc;
5574 int i;
5576 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5578 desc = &ring->desc[i];
5580 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5583 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5584 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5586 ring->cur = 0;
5590 * rt2860_free_rx_ring
5592 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5593 struct rt2860_softc_rx_ring *ring)
5595 struct rt2860_softc_rx_data *data;
5596 int i;
5598 if (ring->desc != NULL)
5600 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5601 BUS_DMASYNC_POSTWRITE);
5602 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5603 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5604 ring->desc_dma_map);
5607 if (ring->desc_dma_tag != NULL)
5608 bus_dma_tag_destroy(ring->desc_dma_tag);
5610 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5612 data = &ring->data[i];
5614 if (data->m != NULL)
5616 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5617 BUS_DMASYNC_POSTREAD);
5618 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5619 m_freem(data->m);
5622 if (data->dma_map != NULL)
5623 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5626 if (ring->data_dma_tag != NULL)
5627 bus_dma_tag_destroy(ring->data_dma_tag);
5631 * rt2860_alloc_tx_ring
5633 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5634 struct rt2860_softc_tx_ring *ring, int qid)
5636 struct rt2860_softc_tx_data *data;
5637 int error, i;
5639 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5640 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5641 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5642 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5643 0, NULL, NULL, &ring->desc_dma_tag);
5644 if (error != 0)
5646 printf("%s: could not create Tx desc DMA tag\n",
5647 device_get_nameunit(sc->dev));
5648 goto fail;
5651 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5652 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5653 if (error != 0)
5655 printf("%s: could not allocate Tx desc DMA memory\n",
5656 device_get_nameunit(sc->dev));
5657 goto fail;
5660 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5661 ring->desc,
5662 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5663 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5664 if (error != 0)
5666 printf("%s: could not load Tx desc DMA map\n",
5667 device_get_nameunit(sc->dev));
5668 goto fail;
5671 ring->desc_queued = 0;
5672 ring->desc_cur = 0;
5673 ring->desc_next = 0;
5675 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5676 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5677 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5678 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5679 0, NULL, NULL, &ring->seg0_dma_tag);
5680 if (error != 0)
5682 printf("%s: could not create Tx seg0 DMA tag\n",
5683 device_get_nameunit(sc->dev));
5684 goto fail;
5687 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5688 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5689 if (error != 0)
5691 printf("%s: could not allocate Tx seg0 DMA memory\n",
5692 device_get_nameunit(sc->dev));
5693 goto fail;
5696 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5697 ring->seg0,
5698 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5699 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5700 if (error != 0)
5702 printf("%s: could not load Tx seg0 DMA map\n",
5703 device_get_nameunit(sc->dev));
5704 goto fail;
5707 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5708 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5709 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5710 &ring->data_dma_tag);
5711 if (error != 0)
5713 printf("%s: could not create Tx data DMA tag\n",
5714 device_get_nameunit(sc->dev));
5715 goto fail;
5718 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5720 data = &ring->data[i];
5722 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5723 if (error != 0)
5725 printf("%s: could not create Tx data DMA map\n",
5726 device_get_nameunit(sc->dev));
5727 goto fail;
5731 ring->data_queued = 0;
5732 ring->data_cur = 0;
5733 ring->data_next = 0;
5735 ring->qid = qid;
5737 return 0;
5739 fail:
5741 rt2860_free_tx_ring(sc, ring);
5743 return error;
5747 * rt2860_reset_tx_ring
5749 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5750 struct rt2860_softc_tx_ring *ring)
5752 struct rt2860_softc_tx_data *data;
5753 struct rt2860_txdesc *desc;
5754 int i;
5756 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5758 desc = &ring->desc[i];
5760 desc->sdl0 = 0;
5761 desc->sdl1 = 0;
5764 ring->desc_queued = 0;
5765 ring->desc_cur = 0;
5766 ring->desc_next = 0;
5768 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5769 BUS_DMASYNC_PREWRITE);
5771 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5772 BUS_DMASYNC_PREWRITE);
5774 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5776 data = &ring->data[i];
5778 if (data->m != NULL)
5780 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5781 BUS_DMASYNC_POSTWRITE);
5782 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5783 m_freem(data->m);
5784 data->m = NULL;
5787 if (data->ni != NULL)
5789 ieee80211_free_node(data->ni);
5790 data->ni = NULL;
5794 ring->data_queued = 0;
5795 ring->data_cur = 0;
5796 ring->data_next = 0;
5800 * rt2860_free_tx_ring
5802 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5803 struct rt2860_softc_tx_ring *ring)
5805 struct rt2860_softc_tx_data *data;
5806 int i;
5808 if (ring->desc != NULL)
5810 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5811 BUS_DMASYNC_POSTWRITE);
5812 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5813 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5814 ring->desc_dma_map);
5817 if (ring->desc_dma_tag != NULL)
5818 bus_dma_tag_destroy(ring->desc_dma_tag);
5820 if (ring->seg0 != NULL)
5822 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5823 BUS_DMASYNC_POSTWRITE);
5824 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5825 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5826 ring->seg0_dma_map);
5829 if (ring->seg0_dma_tag != NULL)
5830 bus_dma_tag_destroy(ring->seg0_dma_tag);
5832 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5834 data = &ring->data[i];
5836 if (data->m != NULL)
5838 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5839 BUS_DMASYNC_POSTWRITE);
5840 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5841 m_freem(data->m);
5844 if (data->ni != NULL)
5845 ieee80211_free_node(data->ni);
5847 if (data->dma_map != NULL)
5848 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5851 if (ring->data_dma_tag != NULL)
5852 bus_dma_tag_destroy(ring->data_dma_tag);
5856 * rt2860_dma_map_addr
5858 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5859 int nseg, int error)
5861 if (error != 0)
5862 return;
5864 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5866 *(bus_addr_t *) arg = segs[0].ds_addr;
5870 * rt2860_sysctl_attach
5872 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5874 struct sysctl_ctx_list *ctx;
5875 struct sysctl_oid *tree;
5876 struct sysctl_oid *stats;
5878 ctx = device_get_sysctl_ctx(sc->dev);
5879 tree = device_get_sysctl_tree(sc->dev);
5881 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5882 "stats", CTLFLAG_RD, 0, "statistic");
5884 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5885 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5886 "all interrupts");
5888 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5889 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
5890 "Tx coherent interrupts");
5892 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5893 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
5894 "Rx coherent interrupts");
5896 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5897 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
5898 "Tx/Rx coherent interrupts");
5900 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5901 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
5902 "FIFO statistic full interrupts");
5904 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5905 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5906 "Rx interrupts");
5908 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5909 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
5910 "Rx delay interrupts");
5912 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5913 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5914 "Tx MGMT interrupts");
5916 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5917 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5918 "Tx HCCA interrupts");
5920 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5921 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5922 "Tx AC3 interrupts");
5924 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5925 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5926 "Tx AC2 interrupts");
5928 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5929 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5930 "Tx AC1 interrupts");
5932 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5933 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5934 "Tx AC0 interrupts");
5936 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5937 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
5938 "Tx delay interrupts");
5940 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5941 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
5942 "Pre-TBTT interrupts");
5944 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5945 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
5946 "TBTT interrupts");
5948 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5949 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
5950 "MCU command interrupts");
5952 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5953 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
5954 "auto wakeup interrupts");
5956 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5957 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
5958 "GP timer interrupts");
5960 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5961 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
5962 "Tx MGMT descriptors queued");
5964 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5965 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
5966 "Tx MGMT data queued");
5968 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5969 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
5970 "Tx HCCA descriptors queued");
5972 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5973 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
5974 "Tx HCCA data queued");
5976 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5977 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
5978 "Tx AC3 descriptors queued");
5980 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5981 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
5982 "Tx AC3 data queued");
5984 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5985 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
5986 "Tx AC2 descriptors queued");
5988 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5989 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
5990 "Tx AC2 data queued");
5992 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5993 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
5994 "Tx AC1 descriptors queued");
5996 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5997 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
5998 "Tx AC1 data queued");
6000 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6001 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
6002 "Tx AC0 descriptors queued");
6004 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6005 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
6006 "Tx AC0 data queued");
6008 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6009 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
6010 "Tx watchdog timeouts");
6012 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6013 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
6014 "Tx defragmented packets");
6016 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6017 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
6018 "no Tx descriptors available");
6020 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6021 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
6022 "Rx mbuf allocation errors");
6024 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6025 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
6026 "Rx mbuf DMA mapping errors");
6028 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6029 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
6030 "Tx queue 0 not empty");
6032 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6033 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
6034 "Tx queue 1 not empty");
6036 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6037 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
6038 "Rx packets");
6040 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6041 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
6042 "Rx CRC errors");
6044 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6045 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
6046 "Rx PHY errors");
6048 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6049 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
6050 "Rx false CCAs");
6052 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6053 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
6054 "Rx PLCP errors");
6056 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6057 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
6058 "Rx duplicate packets");
6060 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6061 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
6062 "Rx FIFO overflows");
6065 static device_method_t rt2860_dev_methods[] =
6067 DEVMETHOD(device_probe, rt2860_probe),
6068 DEVMETHOD(device_attach, rt2860_attach),
6069 DEVMETHOD(device_detach, rt2860_detach),
6070 DEVMETHOD(device_shutdown, rt2860_shutdown),
6071 DEVMETHOD(device_suspend, rt2860_suspend),
6072 DEVMETHOD(device_resume, rt2860_resume),
6073 { 0, 0 }
6076 static driver_t rt2860_driver =
6078 "rt2860",
6079 rt2860_dev_methods,
6080 sizeof(struct rt2860_softc)
6083 static devclass_t rt2860_dev_class;
6085 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
6087 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
6088 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);