Resolved compiler warnings
[ralink_drivers/rt2860_fbsd8.git] / rt2860.c
blob73f5f7e96690a6813e3630ad1d6ae9c307feace0
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_bbp_tuning(struct rt2860_softc *sc);
271 static void rt2860_watchdog(struct rt2860_softc *sc);
273 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc);
275 static void rt2860_update_raw_counters(struct rt2860_softc *sc);
277 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask);
279 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask);
281 static int rt2860_txrx_enable(struct rt2860_softc *sc);
283 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
284 struct rt2860_softc_rx_ring *ring);
286 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
287 struct rt2860_softc_rx_ring *ring);
289 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
290 struct rt2860_softc_rx_ring *ring);
292 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
293 struct rt2860_softc_tx_ring *ring, int qid);
295 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
296 struct rt2860_softc_tx_ring *ring);
298 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
299 struct rt2860_softc_tx_ring *ring);
301 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
302 int nseg, int error);
304 static void rt2860_sysctl_attach(struct rt2860_softc *sc);
307 * Static variables
310 static const struct rt2860_pci_ident rt2860_pci_ids[] =
312 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCI, "Ralink RT2860 PCI" },
313 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860_PCIe, "Ralink RT2860 PCIe" },
314 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760_PCI, "Ralink RT2760 PCI" },
315 { PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790_PCIe, "Ralink RT2790 PCIe" },
316 { 0, 0, NULL }
319 static const struct
321 uint32_t reg;
322 uint32_t val;
323 } rt2860_def_mac[] =
325 { RT2860_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
326 { RT2860_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
327 { RT2860_REG_LEGACY_BASIC_RATE, 0x0000013f },
328 { RT2860_REG_HT_BASIC_RATE, 0x00008003 },
329 { RT2860_REG_SYS_CTRL, 0x00000000 },
330 { RT2860_REG_RX_FILTER_CFG, 0x00017f97 },
331 { RT2860_REG_BKOFF_SLOT_CFG, 0x00000209 },
332 { RT2860_REG_TX_SW_CFG0, 0x00000000 },
333 { RT2860_REG_TX_SW_CFG1, 0x00080606 },
334 { RT2860_REG_TX_LINK_CFG, 0x00001020 },
335 { RT2860_REG_TX_TIMEOUT_CFG, 0x000a2090 },
336 { RT2860_REG_MAX_LEN_CFG, (1 << 12) | RT2860_MAX_AGG_SIZE },
337 { RT2860_REG_LED_CFG, 0x7f031e46 },
338 { RT2860_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
339 { RT2860_REG_TX_RTY_CFG, 0x47d01f0f },
340 { RT2860_REG_AUTO_RSP_CFG, 0x00000013 },
341 { RT2860_REG_TX_CCK_PROT_CFG, 0x05740003 },
342 { RT2860_REG_TX_OFDM_PROT_CFG, 0x05740003 },
343 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f44084 },
344 { RT2860_REG_SCHDMA_WPDMA_GLO_CFG, 0x00000030 },
345 { RT2860_REG_TX_GF20_PROT_CFG, 0x01744004 },
346 { RT2860_REG_TX_GF40_PROT_CFG, 0x03f44084 },
347 { RT2860_REG_TX_MM20_PROT_CFG, 0x01744004 },
348 { RT2860_REG_TX_MM40_PROT_CFG, 0x03f54084 },
349 { RT2860_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
350 { RT2860_REG_TX_RTS_CFG, 0x00092b20 },
351 { RT2860_REG_TX_EXP_ACK_TIME, 0x002400ca },
352 { RT2860_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
353 { RT2860_REG_XIFS_TIME_CFG, 0x33a41010 },
354 { RT2860_REG_PWR_PIN_CFG, 0x00000003 },
355 { RT2860_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
356 { RT2860_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
357 { RT2860_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
360 #define RT2860_DEF_MAC_SIZE (sizeof(rt2860_def_mac) / sizeof(rt2860_def_mac[0]))
362 static const struct
364 uint8_t reg;
365 uint8_t val;
366 } rt2860_def_bbp[] =
368 { 65, 0x2c },
369 { 66, 0x38 },
370 { 69, 0x12 },
371 { 70, 0x0a },
372 { 73, 0x10 },
373 { 81, 0x37 },
374 { 82, 0x62 },
375 { 83, 0x6a },
376 { 84, 0x99 },
377 { 86, 0x00 },
378 { 91, 0x04 },
379 { 92, 0x00 },
380 { 103, 0x00 },
381 { 105, 0x05 },
382 { 106, 0x35 },
385 #define RT2860_DEF_BBP_SIZE (sizeof(rt2860_def_bbp) / sizeof(rt2860_def_bbp[0]))
387 SYSCTL_NODE(_hw, OID_AUTO, rt2860, CTLFLAG_RD, 0, "RT2860 driver parameters");
389 #ifdef RT2860_DEBUG
390 static int rt2860_debug = 0;
391 SYSCTL_INT(_hw_rt2860, OID_AUTO, debug, CTLFLAG_RW, &rt2860_debug, 0, "rt2860 debug level");
392 TUNABLE_INT("hw.rt2860.debug", &rt2860_debug);
393 #endif
396 * rt2860_probe
398 static int rt2860_probe(device_t dev)
400 const struct rt2860_pci_ident *ident;
402 for (ident = rt2860_pci_ids; ident->name != NULL; ident++)
404 if (pci_get_vendor(dev) == ident->vendor &&
405 pci_get_device(dev) == ident->device)
407 device_set_desc(dev, ident->name);
408 return 0;
412 return ENXIO;
416 * rt2860_attach
418 static int rt2860_attach(device_t dev)
420 struct rt2860_softc *sc;
421 struct ifnet *ifp;
422 struct ieee80211com *ic;
423 int error, ntries, i;
425 sc = device_get_softc(dev);
427 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
429 printf("%s: chip is in D%d power mode, setting to D0\n",
430 device_get_nameunit(dev), pci_get_powerstate(dev));
431 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
434 /* enable bus-mastering */
436 pci_enable_busmaster(dev);
438 sc->dev = dev;
440 mtx_init(&sc->lock, device_get_nameunit(dev),
441 MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
443 sc->mem_rid = PCIR_BAR(0);
444 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
445 &sc->mem_rid, RF_ACTIVE);
446 if (sc->mem == NULL)
448 printf("%s: could not allocate memory resource\n",
449 device_get_nameunit(dev));
450 error = ENXIO;
451 goto fail;
454 sc->bst = rman_get_bustag(sc->mem);
455 sc->bsh = rman_get_bushandle(sc->mem);
457 sc->irq_rid = 0;
458 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
459 &sc->irq_rid, RF_ACTIVE | RF_SHAREABLE);
460 if (sc->irq == NULL)
462 printf("%s: could not allocate interrupt resource\n",
463 device_get_nameunit(dev));
464 error = ENXIO;
465 goto fail;
468 #ifdef RT2860_DEBUG
469 sc->debug = rt2860_debug;
471 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
472 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
473 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2860 debug level");
474 #endif
476 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
477 "%s: attaching\n",
478 device_get_nameunit(sc->dev));
480 /* wait for NIC to initialize */
482 for (ntries = 0; ntries < 100; ntries++)
484 sc->mac_rev = rt2860_io_mac_read(sc, RT2860_REG_MAC_CSR0);
485 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
486 break;
488 DELAY(10);
491 if (ntries == 100)
493 printf("%s: timeout waiting for NIC to initialize\n",
494 device_get_nameunit(dev));
495 error = EIO;
496 goto fail;
499 rt2860_read_eeprom(sc);
501 printf("%s: MAC/BBP RT2860 (rev 0x%08x), RF %s\n",
502 device_get_nameunit(sc->dev), sc->mac_rev,
503 rt2860_rf_name(sc->rf_rev));
505 /* allocate Tx and Rx rings */
507 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
509 error = rt2860_alloc_tx_ring(sc, &sc->tx_ring[i], i);
510 if (error != 0)
512 printf("%s: could not allocate Tx ring #%d\n",
513 device_get_nameunit(sc->dev), i);
514 goto fail;
518 sc->tx_ring_mgtqid = 5;
520 error = rt2860_alloc_rx_ring(sc, &sc->rx_ring);
521 if (error != 0)
523 printf("%s: could not allocate Rx ring\n",
524 device_get_nameunit(sc->dev));
525 goto fail;
528 callout_init(&sc->periodic_ch, 0);
529 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
531 ifp = sc->ifp = if_alloc(IFT_IEEE80211);
532 if (ifp == NULL)
534 printf("%s: could not if_alloc()\n",
535 device_get_nameunit(sc->dev));
536 error = ENOMEM;
537 goto fail;
540 ifp->if_softc = sc;
542 if_initname(ifp, "rt2860", device_get_unit(sc->dev));
544 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
546 ifp->if_init = rt2860_init;
547 ifp->if_ioctl = rt2860_ioctl;
548 ifp->if_start = rt2860_start;
550 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
551 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
552 IFQ_SET_READY(&ifp->if_snd);
554 ic = ifp->if_l2com;
556 ic->ic_ifp = ifp;
558 ic->ic_phytype = IEEE80211_T_HT;
559 ic->ic_opmode = IEEE80211_M_STA;
561 ic->ic_caps = IEEE80211_C_MONITOR |
562 IEEE80211_C_IBSS |
563 IEEE80211_C_STA |
564 IEEE80211_C_AHDEMO |
565 IEEE80211_C_HOSTAP |
566 IEEE80211_C_WDS |
567 IEEE80211_C_MBSS |
568 /* IEEE80211_C_BGSCAN | */
569 IEEE80211_C_TXPMGT |
570 IEEE80211_C_SHPREAMBLE |
571 IEEE80211_C_SHSLOT |
572 IEEE80211_C_TXFRAG |
573 IEEE80211_C_BURST |
574 IEEE80211_C_WME |
575 IEEE80211_C_WPA;
577 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP |
578 IEEE80211_CRYPTO_TKIP |
579 IEEE80211_CRYPTO_TKIPMIC |
580 IEEE80211_CRYPTO_AES_CCM;
582 ic->ic_htcaps = IEEE80211_HTC_HT |
583 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
584 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
585 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
586 IEEE80211_HTCAP_GREENFIELD | /* HT greenfield */
587 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
588 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
589 IEEE80211_HTCAP_DSSSCCK40; /* HT 40MHz DSSS/CCK modulation */
591 /* spatial streams */
593 if (sc->nrxpath == 2)
594 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
595 else if (sc->nrxpath == 3)
596 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
597 else
598 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
600 if (sc->ntxpath > 1)
601 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
603 /* delayed BA */
605 if (sc->mac_rev != 0x28600100)
606 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
608 /* init channels */
610 ic->ic_nchans = 0;
612 rt2860_init_channels(sc);
614 rt2860_init_channels_ht40(sc);
616 ieee80211_ifattach(ic, sc->mac_addr);
618 ic->ic_vap_create = rt2860_vap_create;
619 ic->ic_vap_delete = rt2860_vap_delete;
621 ic->ic_node_alloc = rt2860_node_alloc;
622 ic->ic_setregdomain = rt2860_setregdomain;
623 ic->ic_getradiocaps = rt2860_getradiocaps;
624 ic->ic_scan_start = rt2860_scan_start;
625 ic->ic_scan_end = rt2860_scan_end;
626 ic->ic_set_channel = rt2860_set_channel;
627 ic->ic_newassoc = rt2860_newassoc;
628 ic->ic_updateslot = rt2860_updateslot;
629 ic->ic_update_promisc = rt2860_update_promisc;
630 ic->ic_update_mcast = rt2860_update_mcast;
631 ic->ic_wme.wme_update = rt2860_wme_update;
632 ic->ic_raw_xmit = rt2860_raw_xmit;
634 sc->recv_action = ic->ic_recv_action;
635 ic->ic_recv_action = rt2860_recv_action;
637 sc->send_action = ic->ic_send_action;
638 ic->ic_send_action = rt2860_send_action;
640 /* hardware requires padding between 802.11 frame header and body */
642 ic->ic_flags |= IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
644 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
646 ieee80211_radiotap_attach(ic,
647 &sc->txtap.ihdr, sizeof(sc->txtap),
648 RT2860_SOFTC_TX_RADIOTAP_PRESENT,
649 &sc->rxtap.ihdr, sizeof(sc->rxtap),
650 RT2860_SOFTC_RX_RADIOTAP_PRESENT);
652 /* init task queue */
654 TASK_INIT(&sc->rx_done_task, 0, rt2860_rx_done_task, sc);
655 TASK_INIT(&sc->tx_done_task, 0, rt2860_tx_done_task, sc);
656 TASK_INIT(&sc->fifo_sta_full_task, 0, rt2860_fifo_sta_full_task, sc);
657 TASK_INIT(&sc->periodic_task, 0, rt2860_periodic_task, sc);
659 sc->rx_process_limit = 100;
661 sc->taskqueue = taskqueue_create("rt2860_taskq", M_NOWAIT,
662 taskqueue_thread_enqueue, &sc->taskqueue);
664 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
665 device_get_nameunit(sc->dev));
667 rt2860_sysctl_attach(sc);
669 if (bootverbose)
670 ieee80211_announce(ic);
672 /* set up interrupt */
674 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
675 NULL, rt2860_intr, sc, &sc->irqh);
676 if (error != 0)
678 printf("%s: could not set up interrupt\n",
679 device_get_nameunit(dev));
680 goto fail;
683 return 0;
685 fail:
687 /* free Tx and Rx rings */
689 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
690 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
692 rt2860_free_rx_ring(sc, &sc->rx_ring);
694 mtx_destroy(&sc->lock);
696 if (sc->mem != NULL)
697 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
699 if (sc->irq != NULL)
700 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
702 return error;
706 * rt2860_detach
708 static int rt2860_detach(device_t dev)
710 struct rt2860_softc *sc;
711 struct ifnet *ifp;
712 struct ieee80211com *ic;
713 int i;
715 sc = device_get_softc(dev);
716 ifp = sc->ifp;
717 ic = ifp->if_l2com;
719 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
720 "%s: detaching\n",
721 device_get_nameunit(sc->dev));
723 RT2860_SOFTC_LOCK(sc);
725 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
727 callout_stop(&sc->periodic_ch);
728 callout_stop(&sc->tx_watchdog_ch);
730 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
731 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
732 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
733 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
735 /* free Tx and Rx rings */
737 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
738 rt2860_free_tx_ring(sc, &sc->tx_ring[i]);
740 rt2860_free_rx_ring(sc, &sc->rx_ring);
742 RT2860_SOFTC_UNLOCK(sc);
744 ieee80211_ifdetach(ic);
746 if_free(ifp);
748 taskqueue_free(sc->taskqueue);
750 mtx_destroy(&sc->lock);
752 bus_generic_detach(dev);
754 bus_teardown_intr(dev, sc->irq, sc->irqh);
756 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
758 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
760 return 0;
764 * rt2860_shutdown
766 static int rt2860_shutdown(device_t dev)
768 struct rt2860_softc *sc;
770 sc = device_get_softc(dev);
772 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
773 "%s: shutting down\n",
774 device_get_nameunit(sc->dev));
776 rt2860_stop(sc);
778 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
780 return 0;
784 * rt2860_suspend
786 static int rt2860_suspend(device_t dev)
788 struct rt2860_softc *sc;
790 sc = device_get_softc(dev);
792 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
793 "%s: suspending\n",
794 device_get_nameunit(sc->dev));
796 rt2860_stop(sc);
798 sc->flags &= ~RT2860_SOFTC_FLAGS_UCODE_LOADED;
800 return 0;
804 * rt2860_resume
806 static int rt2860_resume(device_t dev)
808 struct rt2860_softc *sc;
809 struct ifnet *ifp;
811 sc = device_get_softc(dev);
812 ifp = sc->ifp;
814 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
815 "%s: resuming\n",
816 device_get_nameunit(sc->dev));
818 if (ifp->if_flags & IFF_UP)
819 rt2860_init(sc);
821 return 0;
825 * rt2860_init_channels
827 static void rt2860_init_channels(struct rt2860_softc *sc)
829 struct ifnet *ifp;
830 struct ieee80211com *ic;
831 struct ieee80211_channel *c;
832 int i, flags;
834 ifp = sc->ifp;
835 ic = ifp->if_l2com;
837 /* set supported channels for 2GHz band */
839 for (i = 1; i <= 14; i++)
841 c = &ic->ic_channels[ic->ic_nchans++];
842 flags = IEEE80211_CHAN_B;
844 c->ic_freq = ieee80211_ieee2mhz(i, flags);
845 c->ic_ieee = i;
846 c->ic_flags = flags;
848 c = &ic->ic_channels[ic->ic_nchans++];
849 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
851 c->ic_freq = ieee80211_ieee2mhz(i, flags);
852 c->ic_ieee = i;
853 c->ic_flags = flags;
855 c = &ic->ic_channels[ic->ic_nchans++];
856 flags = IEEE80211_CHAN_G;
858 c->ic_freq = ieee80211_ieee2mhz(i, flags);
859 c->ic_ieee = i;
860 c->ic_flags = flags;
862 c = &ic->ic_channels[ic->ic_nchans++];
863 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
865 c->ic_freq = ieee80211_ieee2mhz(i, flags);
866 c->ic_ieee = i;
867 c->ic_flags = flags;
870 /* set supported channels for 5GHz band */
872 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
873 sc->rf_rev == RT2860_EEPROM_RF_2750)
875 for (i = 36; i <= 64; i += 4)
877 c = &ic->ic_channels[ic->ic_nchans++];
878 flags = IEEE80211_CHAN_A;
880 c->ic_freq = ieee80211_ieee2mhz(i, flags);
881 c->ic_ieee = i;
882 c->ic_flags = flags;
884 c = &ic->ic_channels[ic->ic_nchans++];
885 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
887 c->ic_freq = ieee80211_ieee2mhz(i, flags);
888 c->ic_ieee = i;
889 c->ic_flags = flags;
892 for (i = 100; i <= 140; i += 4)
894 c = &ic->ic_channels[ic->ic_nchans++];
895 flags = IEEE80211_CHAN_A;
897 c->ic_freq = ieee80211_ieee2mhz(i, flags);
898 c->ic_ieee = i;
899 c->ic_flags = flags;
901 c = &ic->ic_channels[ic->ic_nchans++];
902 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
904 c->ic_freq = ieee80211_ieee2mhz(i, flags);
905 c->ic_ieee = i;
906 c->ic_flags = flags;
909 for (i = 149; i <= 165; i += 4)
911 c = &ic->ic_channels[ic->ic_nchans++];
912 flags = IEEE80211_CHAN_A;
914 c->ic_freq = ieee80211_ieee2mhz(i, flags);
915 c->ic_ieee = i;
916 c->ic_flags = flags;
918 c = &ic->ic_channels[ic->ic_nchans++];
919 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
921 c->ic_freq = ieee80211_ieee2mhz(i, flags);
922 c->ic_ieee = i;
923 c->ic_flags = flags;
929 * rt2860_init_channels_ht40
931 static void rt2860_init_channels_ht40(struct rt2860_softc *sc)
933 struct ifnet *ifp;
934 struct ieee80211com *ic;
935 struct ieee80211_channel *c, *cent, *ext;
936 int i, flags;
938 ifp = sc->ifp;
939 ic = ifp->if_l2com;
941 /* set supported channels for 2GHz band */
943 for (i = 1; i <= 14; i++)
945 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
947 /* find the center channel */
949 cent = ieee80211_find_channel_byieee(ic, i,
950 flags & ~IEEE80211_CHAN_HT);
951 if (cent == NULL)
953 printf("%s: skip channel %d, could not find center channel\n",
954 device_get_nameunit(sc->dev), i);
955 continue;
958 /* find the extension channel */
960 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
961 flags & ~IEEE80211_CHAN_HT);
962 if (ext == NULL)
964 printf("%s: skip channel %d, could not find extension channel\n",
965 device_get_nameunit(sc->dev), i);
966 continue;
969 c = &ic->ic_channels[ic->ic_nchans++];
971 *c = *cent;
972 c->ic_extieee = ext->ic_ieee;
973 c->ic_flags &= ~IEEE80211_CHAN_HT;
974 c->ic_flags |= IEEE80211_CHAN_HT40U;
976 c = &ic->ic_channels[ic->ic_nchans++];
978 *c = *ext;
979 c->ic_extieee = cent->ic_ieee;
980 c->ic_flags &= ~IEEE80211_CHAN_HT;
981 c->ic_flags |= IEEE80211_CHAN_HT40D;
984 /* set supported channels for 5GHz band */
986 if (sc->rf_rev == RT2860_EEPROM_RF_2850 ||
987 sc->rf_rev == RT2860_EEPROM_RF_2750)
989 for (i = 36; i <= 64; i += 4)
991 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
993 /* find the center channel */
995 cent = ieee80211_find_channel_byieee(ic, i,
996 flags & ~IEEE80211_CHAN_HT);
997 if (cent == NULL)
999 printf("%s: skip channel %d, could not find center channel\n",
1000 device_get_nameunit(sc->dev), i);
1001 continue;
1004 /* find the extension channel */
1006 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1007 flags & ~IEEE80211_CHAN_HT);
1008 if (ext == NULL)
1010 printf("%s: skip channel %d, could not find extension channel\n",
1011 device_get_nameunit(sc->dev), i);
1012 continue;
1015 c = &ic->ic_channels[ic->ic_nchans++];
1017 *c = *cent;
1018 c->ic_extieee = ext->ic_ieee;
1019 c->ic_flags &= ~IEEE80211_CHAN_HT;
1020 c->ic_flags |= IEEE80211_CHAN_HT40U;
1022 c = &ic->ic_channels[ic->ic_nchans++];
1024 *c = *ext;
1025 c->ic_extieee = cent->ic_ieee;
1026 c->ic_flags &= ~IEEE80211_CHAN_HT;
1027 c->ic_flags |= IEEE80211_CHAN_HT40D;
1030 for (i = 100; i <= 140; i += 4)
1032 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1034 /* find the center channel */
1036 cent = ieee80211_find_channel_byieee(ic, i,
1037 flags & ~IEEE80211_CHAN_HT);
1038 if (cent == NULL)
1040 printf("%s: skip channel %d, could not find center channel\n",
1041 device_get_nameunit(sc->dev), i);
1042 continue;
1045 /* find the extension channel */
1047 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1048 flags & ~IEEE80211_CHAN_HT);
1049 if (ext == NULL)
1051 printf("%s: skip channel %d, could not find extension channel\n",
1052 device_get_nameunit(sc->dev), i);
1053 continue;
1056 c = &ic->ic_channels[ic->ic_nchans++];
1058 *c = *cent;
1059 c->ic_extieee = ext->ic_ieee;
1060 c->ic_flags &= ~IEEE80211_CHAN_HT;
1061 c->ic_flags |= IEEE80211_CHAN_HT40U;
1063 c = &ic->ic_channels[ic->ic_nchans++];
1065 *c = *ext;
1066 c->ic_extieee = cent->ic_ieee;
1067 c->ic_flags &= ~IEEE80211_CHAN_HT;
1068 c->ic_flags |= IEEE80211_CHAN_HT40D;
1071 for (i = 149; i <= 165; i += 4)
1073 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1075 /* find the center channel */
1077 cent = ieee80211_find_channel_byieee(ic, i,
1078 flags & ~IEEE80211_CHAN_HT);
1079 if (cent == NULL)
1081 printf("%s: skip channel %d, could not find center channel\n",
1082 device_get_nameunit(sc->dev), i);
1083 continue;
1086 /* find the extension channel */
1088 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1089 flags & ~IEEE80211_CHAN_HT);
1090 if (ext == NULL)
1092 printf("%s: skip channel %d, could not find extension channel\n",
1093 device_get_nameunit(sc->dev), i);
1094 continue;
1097 c = &ic->ic_channels[ic->ic_nchans++];
1099 *c = *cent;
1100 c->ic_extieee = ext->ic_ieee;
1101 c->ic_flags &= ~IEEE80211_CHAN_HT;
1102 c->ic_flags |= IEEE80211_CHAN_HT40U;
1104 c = &ic->ic_channels[ic->ic_nchans++];
1106 *c = *ext;
1107 c->ic_extieee = cent->ic_ieee;
1108 c->ic_flags &= ~IEEE80211_CHAN_HT;
1109 c->ic_flags |= IEEE80211_CHAN_HT40D;
1115 * rt2860_init_locked
1117 static void rt2860_init_locked(void *priv)
1119 struct rt2860_softc *sc;
1120 struct ifnet *ifp;
1121 struct ieee80211com *ic;
1122 int error, i, ntries;
1123 uint32_t tmp, stacnt[6];
1125 sc = priv;
1126 ifp = sc->ifp;
1127 ic = ifp->if_l2com;
1129 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1130 "%s: initializing\n",
1131 device_get_nameunit(sc->dev));
1133 RT2860_SOFTC_ASSERT_LOCKED(sc);
1135 if (!(sc->flags & RT2860_SOFTC_FLAGS_UCODE_LOADED))
1137 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1138 "%s: loading 8051 microcode\n",
1139 device_get_nameunit(sc->dev));
1141 error = rt2860_io_mcu_load_ucode(sc, rt2860_ucode, sizeof(rt2860_ucode));
1142 if (error != 0)
1144 printf("%s: could not load 8051 microcode\n",
1145 device_get_nameunit(sc->dev));
1146 goto fail;
1149 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1150 "%s: 8051 microcode was successfully loaded\n",
1151 device_get_nameunit(sc->dev));
1153 sc->flags |= RT2860_SOFTC_FLAGS_UCODE_LOADED;
1156 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x2);
1158 /* disable DMA engine */
1160 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1162 tmp &= 0xff0;
1163 tmp |= RT2860_REG_TX_WB_DDONE;
1165 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1167 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, 0xffffffff);
1169 /* PBF hardware reset */
1171 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1172 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1174 /* wait while DMA engine is busy */
1176 for (ntries = 0; ntries < 100; ntries++)
1178 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1179 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1180 break;
1182 DELAY(1000);
1185 if (ntries == 100)
1187 printf("%s: timeout waiting for DMA engine\n",
1188 device_get_nameunit(sc->dev));
1189 goto fail;
1192 tmp &= 0xff0;
1193 tmp |= RT2860_REG_TX_WB_DDONE;
1195 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1197 /* reset Rx and Tx rings */
1199 tmp = RT2860_REG_RST_IDX_RX |
1200 RT2860_REG_RST_IDX_TX_MGMT |
1201 RT2860_REG_RST_IDX_TX_HCCA |
1202 RT2860_REG_RST_IDX_TX_AC3 |
1203 RT2860_REG_RST_IDX_TX_AC2 |
1204 RT2860_REG_RST_IDX_TX_AC1 |
1205 RT2860_REG_RST_IDX_TX_AC0;
1207 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_RST_IDX, tmp);
1209 /* PBF hardware reset */
1211 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe1f);
1212 rt2860_io_mac_write(sc, RT2860_REG_PBF_SYS_CTRL, 0xe00);
1214 rt2860_io_mac_write(sc, RT2860_REG_PWR_PIN_CFG, 0x3);
1216 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1217 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1218 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1220 /* init Tx power per rate */
1222 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
1224 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1225 continue;
1227 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i),
1228 sc->txpow_rate_20mhz[i]);
1231 for (i = 0; i < RT2860_DEF_MAC_SIZE; i++)
1232 rt2860_io_mac_write(sc, rt2860_def_mac[i].reg,
1233 rt2860_def_mac[i].val);
1235 /* wait while MAC is busy */
1237 for (ntries = 0; ntries < 100; ntries++)
1239 if (!(rt2860_io_mac_read(sc, RT2860_REG_STATUS_CFG) &
1240 (RT2860_REG_STATUS_TX_BUSY | RT2860_REG_STATUS_RX_BUSY)))
1241 break;
1243 DELAY(1000);
1246 if (ntries == 100)
1248 printf("%s: timeout waiting for MAC\n",
1249 device_get_nameunit(sc->dev));
1250 goto fail;
1253 /* clear Host to MCU mailbox */
1255 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX_BBP_AGENT, 0);
1256 rt2860_io_mac_write(sc, RT2860_REG_H2M_MAILBOX, 0);
1258 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_BOOT,
1259 RT2860_REG_H2M_TOKEN_NO_INTR, 0);
1261 DELAY(1000);
1263 error = rt2860_init_bbp(sc);
1264 if (error != 0)
1265 goto fail;
1267 /* set up maximum buffer sizes */
1269 tmp = (1 << 12) | RT2860_MAX_AGG_SIZE;
1271 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1273 if (sc->mac_rev == 0x28720200)
1275 /* set max. PSDU length from 16K to 32K bytes */
1277 tmp = rt2860_io_mac_read(sc, RT2860_REG_MAX_LEN_CFG);
1279 tmp &= ~(3 << 12);
1280 tmp |= (2 << 12);
1282 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1285 if (sc->mac_rev >= 0x28720200 && sc->mac_rev < 0x30700200)
1287 tmp = rt2860_io_mac_read(sc, RT2860_REG_MAX_LEN_CFG);
1289 tmp &= 0xfff;
1290 tmp |= 0x2000;
1292 rt2860_io_mac_write(sc, RT2860_REG_MAX_LEN_CFG, tmp);
1295 /* set mac address */
1297 rt2860_asic_set_macaddr(sc, IF_LLADDR(ifp));
1299 /* clear statistic registers */
1301 rt2860_io_mac_read_multi(sc, RT2860_REG_RX_STA_CNT0,
1302 stacnt, sizeof(stacnt));
1304 /* set RTS threshold */
1306 rt2860_asic_update_rtsthreshold(sc);
1308 /* set Tx power */
1310 rt2860_asic_update_txpower(sc);
1312 /* set up protection mode */
1314 rt2860_asic_updateprot(sc);
1316 /* clear key tables */
1318 rt2860_asic_clear_keytables(sc);
1320 taskqueue_unblock(sc->taskqueue);
1322 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
1324 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1325 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
1327 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
1329 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
1330 sc->tx_ring[i].desc_phys_addr);
1331 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
1332 RT2860_SOFTC_TX_RING_DESC_COUNT);
1333 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
1336 /* init Rx ring */
1338 rt2860_reset_rx_ring(sc, &sc->rx_ring);
1340 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
1341 sc->rx_ring.desc_phys_addr);
1342 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
1343 RT2860_SOFTC_RX_RING_DATA_COUNT);
1344 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
1345 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
1347 /* wait while DMA engine is busy */
1349 for (ntries = 0; ntries < 100; ntries++)
1351 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
1352 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
1353 break;
1355 DELAY(1000);
1358 if (ntries == 100)
1360 printf("%s: timeout waiting for DMA engine\n",
1361 device_get_nameunit(sc->dev));
1362 goto fail;
1365 tmp &= 0xff0;
1366 tmp |= RT2860_REG_TX_WB_DDONE;
1368 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1370 /* disable interrupts mitigation */
1372 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_DELAY_INT_CFG, 0);
1374 /* send LEDs operating mode to microcontroller */
1376 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED1,
1377 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1378 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED2,
1379 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1380 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_LED3,
1381 RT2860_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1383 /* turn radio LED on */
1385 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON);
1387 /* write vendor-specific BBP values (from EEPROM) */
1389 for (i = 0; i < RT2860_SOFTC_BBP_EEPROM_COUNT; i++)
1391 if (sc->bbp_eeprom[i].reg == 0x00 ||
1392 sc->bbp_eeprom[i].reg == 0xff)
1393 continue;
1395 rt2860_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1396 sc->bbp_eeprom[i].val);
1400 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_GPIO_CTRL_CFG);
1401 if (tmp & (1 << 2))
1403 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_SLEEP,
1404 RT2860_REG_H2M_TOKEN_RADIOOFF, 0x02ff);
1405 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_RADIOOFF);
1407 rt2860_io_mcu_cmd(sc, RT2860_IO_MCU_CMD_WAKEUP,
1408 RT2860_REG_H2M_TOKEN_WAKEUP, 0);
1409 rt2860_io_mcu_cmd_check(sc, RT2860_REG_H2M_TOKEN_WAKEUP);
1413 /* disable non-existing Rx chains */
1415 tmp = rt2860_io_bbp_read(sc, 3);
1417 tmp &= ~((1 << 4) | (1 << 3));
1419 if (sc->nrxpath == 3)
1420 tmp |= (1 << 4);
1421 else if (sc->nrxpath == 2)
1422 tmp |= (1 << 3);
1424 rt2860_io_bbp_write(sc, 3, tmp);
1426 /* disable non-existing Tx chains */
1428 tmp = rt2860_io_bbp_read(sc, 1);
1430 tmp &= ~((1 << 4) | (1 << 3));
1432 if (sc->ntxpath == 2)
1433 tmp |= (1 << 4);
1435 rt2860_io_bbp_write(sc, 1, tmp);
1437 /* set current channel */
1439 rt2860_rf_set_chan(sc, ic->ic_curchan);
1441 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1442 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
1443 (48 << 16) | 96);
1445 if ((sc->mac_rev & 0xffff) != 0x0101)
1446 rt2860_io_mac_write(sc, RT2860_REG_TX_TXOP_CTRL_CFG, 0x583f);
1448 /* clear pending interrupts */
1450 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, 0xffffffff);
1452 /* enable interrupts */
1454 tmp = RT2860_REG_INT_TX_COHERENT |
1455 RT2860_REG_INT_RX_COHERENT |
1456 RT2860_REG_INT_GP_TIMER |
1457 RT2860_REG_INT_AUTO_WAKEUP |
1458 RT2860_REG_INT_FIFO_STA_FULL |
1460 RT2860_REG_INT_PRE_TBTT |
1461 RT2860_REG_INT_TBTT |
1463 RT2860_REG_INT_TXRX_COHERENT |
1464 RT2860_REG_INT_MCU_CMD |
1465 RT2860_REG_INT_TX_MGMT_DONE |
1466 RT2860_REG_INT_TX_HCCA_DONE |
1467 RT2860_REG_INT_TX_AC3_DONE |
1468 RT2860_REG_INT_TX_AC2_DONE |
1469 RT2860_REG_INT_TX_AC1_DONE |
1470 RT2860_REG_INT_TX_AC0_DONE |
1471 RT2860_REG_INT_RX_DONE;
1473 sc->intr_enable_mask = tmp;
1475 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
1477 if (rt2860_txrx_enable(sc) != 0)
1478 goto fail;
1480 /* clear garbage interrupts */
1482 tmp = rt2860_io_mac_read(sc, 0x1300);
1484 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1485 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1487 sc->periodic_round = 0;
1489 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
1491 return;
1493 fail:
1495 rt2860_stop_locked(sc);
1499 * rt2860_init
1501 static void rt2860_init(void *priv)
1503 struct rt2860_softc *sc;
1505 sc = priv;
1507 RT2860_SOFTC_LOCK(sc);
1509 rt2860_init_locked(sc);
1511 RT2860_SOFTC_UNLOCK(sc);
1515 * rt2860_init_bbp
1517 static int rt2860_init_bbp(struct rt2860_softc *sc)
1519 int ntries, i;
1520 uint8_t tmp;
1522 for (ntries = 0; ntries < 20; ntries++)
1524 tmp = rt2860_io_bbp_read(sc, 0);
1525 if (tmp != 0x00 && tmp != 0xff)
1526 break;
1529 if (tmp == 0x00 || tmp == 0xff)
1531 printf("%s: timeout waiting for BBP to wakeup\n",
1532 device_get_nameunit(sc->dev));
1533 return ETIMEDOUT;
1536 for (i = 0; i < RT2860_DEF_BBP_SIZE; i++)
1537 rt2860_io_bbp_write(sc, rt2860_def_bbp[i].reg,
1538 rt2860_def_bbp[i].val);
1540 if ((sc->mac_rev & 0xffff) != 0x0101)
1541 rt2860_io_bbp_write(sc, 84, 0x19);
1543 if (sc->mac_rev == 0x28600100)
1545 rt2860_io_bbp_write(sc, 69, 0x16);
1546 rt2860_io_bbp_write(sc, 73, 0x12);
1549 return 0;
1553 * rt2860_stop_locked
1555 static void rt2860_stop_locked(void *priv)
1557 struct rt2860_softc *sc;
1558 struct ifnet *ifp;
1559 struct ieee80211com *ic;
1560 uint32_t tmp;
1562 sc = priv;
1563 ifp = sc->ifp;
1564 ic = ifp->if_l2com;
1566 RT2860_DPRINTF(sc, RT2860_DEBUG_ANY,
1567 "%s: stopping\n",
1568 device_get_nameunit(sc->dev));
1570 RT2860_SOFTC_ASSERT_LOCKED(sc);
1572 sc->tx_timer = 0;
1574 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1575 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1577 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1579 callout_stop(&sc->periodic_ch);
1580 callout_stop(&sc->tx_watchdog_ch);
1582 RT2860_SOFTC_UNLOCK(sc);
1584 taskqueue_block(sc->taskqueue);
1586 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1587 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1588 taskqueue_drain(sc->taskqueue, &sc->fifo_sta_full_task);
1589 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1591 RT2860_SOFTC_LOCK(sc);
1593 /* clear key tables */
1595 rt2860_asic_clear_keytables(sc);
1597 /* disable interrupts */
1599 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, 0);
1601 /* disable Tx/Rx */
1603 tmp = rt2860_io_mac_read(sc, RT2860_REG_SYS_CTRL);
1605 tmp &= ~(RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
1607 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, tmp);
1609 /* reset adapter */
1611 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
1612 RT2860_REG_MAC_SRST | RT2860_REG_BBP_HRST);
1613 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, 0);
1617 * rt2860_stop
1619 static void rt2860_stop(void *priv)
1621 struct rt2860_softc *sc;
1623 sc = priv;
1625 RT2860_SOFTC_LOCK(sc);
1627 rt2860_stop_locked(sc);
1629 RT2860_SOFTC_UNLOCK(sc);
1633 * rt2860_start
1635 static void rt2860_start(struct ifnet *ifp)
1637 struct rt2860_softc *sc;
1638 struct ieee80211_node *ni;
1639 struct mbuf *m;
1640 int qid;
1642 sc = ifp->if_softc;
1644 RT2860_SOFTC_LOCK(sc);
1646 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1648 RT2860_SOFTC_UNLOCK(sc);
1649 return;
1652 for (;;)
1654 IF_POLL(&ifp->if_snd, m);
1655 if (m == NULL)
1656 break;
1658 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1660 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1662 qid = M_WME_GETAC(m);
1664 if (sc->tx_ring[qid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
1666 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
1667 "%s: if_start: Tx ring with qid=%d is full\n",
1668 device_get_nameunit(sc->dev), qid);
1670 m_freem(m);
1671 ieee80211_free_node(ni);
1672 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1673 ifp->if_oerrors++;
1674 break;
1677 if (rt2860_tx_data(sc, m, ni, qid) != 0)
1679 ieee80211_free_node(ni);
1680 ifp->if_oerrors++;
1681 break;
1684 rt2860_drain_fifo_stats(sc);
1686 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
1688 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
1691 RT2860_SOFTC_UNLOCK(sc);
1695 * rt2860_ioctl
1697 static int rt2860_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1699 struct rt2860_softc *sc;
1700 struct ieee80211com *ic;
1701 struct ifreq *ifr;
1702 int error, startall;
1704 sc = ifp->if_softc;
1705 ic = ifp->if_l2com;
1706 ifr = (struct ifreq *) data;
1708 error = 0;
1710 switch (cmd)
1712 case SIOCSIFFLAGS:
1713 startall = 0;
1715 RT2860_SOFTC_LOCK(sc);
1717 if (ifp->if_flags & IFF_UP)
1719 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1721 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1722 rt2860_asic_update_promisc(sc);
1724 else
1726 rt2860_init_locked(sc);
1727 startall = 1;
1730 else
1732 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1733 rt2860_stop_locked(sc);
1736 sc->if_flags = ifp->if_flags;
1738 RT2860_SOFTC_UNLOCK(sc);
1740 if (startall)
1741 ieee80211_start_all(ic);
1742 break;
1744 case SIOCGIFMEDIA:
1745 case SIOCSIFMEDIA:
1746 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1747 break;
1749 case SIOCGIFADDR:
1750 error = ether_ioctl(ifp, cmd, data);
1751 break;
1753 default:
1754 error = EINVAL;
1755 break;
1758 return error;
1762 * rt2860_vap_create
1764 static struct ieee80211vap *rt2860_vap_create(struct ieee80211com *ic,
1765 const char name[IFNAMSIZ], int unit, int opmode, int flags,
1766 const uint8_t bssid[IEEE80211_ADDR_LEN],
1767 const uint8_t mac[IEEE80211_ADDR_LEN])
1769 struct rt2860_softc *sc;
1770 struct ifnet *ifp;
1771 struct rt2860_softc_vap *rvap;
1772 struct ieee80211vap *vap;
1774 ifp = ic->ic_ifp;
1775 sc = ifp->if_softc;
1777 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1778 "%s: VAP create: opmode=%s\n",
1779 device_get_nameunit(sc->dev),
1780 ieee80211_opmode_name[opmode]);
1782 switch (opmode)
1784 case IEEE80211_M_MONITOR:
1785 case IEEE80211_M_IBSS:
1786 case IEEE80211_M_STA:
1787 case IEEE80211_M_AHDEMO:
1788 case IEEE80211_M_HOSTAP:
1789 case IEEE80211_M_MBSS:
1790 if (!TAILQ_EMPTY(&ic->ic_vaps))
1792 if_printf(ifp, "only 1 VAP supported\n");
1793 return NULL;
1796 if (opmode == IEEE80211_M_STA)
1797 flags |= IEEE80211_CLONE_NOBEACONS;
1798 break;
1800 case IEEE80211_M_WDS:
1801 if (TAILQ_EMPTY(&ic->ic_vaps) || ic->ic_opmode != IEEE80211_M_HOSTAP)
1803 if_printf(ifp, "WDS only supported in AP mode\n");
1804 return NULL;
1807 flags &= ~IEEE80211_CLONE_BSSID;
1808 break;
1810 default:
1811 if_printf(ifp, "unknown opmode %d\n", opmode);
1812 return NULL;
1815 rvap = (struct rt2860_softc_vap *) malloc(sizeof(struct rt2860_softc_vap),
1816 M_80211_VAP, M_NOWAIT | M_ZERO);
1817 if (rvap == NULL)
1818 return NULL;
1820 vap = &rvap->vap;
1822 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
1824 rvap->newstate = vap->iv_newstate;
1825 vap->iv_newstate = rt2860_vap_newstate;
1827 vap->iv_reset = rt2860_vap_reset;
1828 vap->iv_key_update_begin = rt2860_vap_key_update_begin;
1829 vap->iv_key_update_end = rt2860_vap_key_update_end;
1830 vap->iv_key_set = rt2860_vap_key_set;
1831 vap->iv_key_delete = rt2860_vap_key_delete;
1832 vap->iv_update_beacon = rt2860_vap_update_beacon;
1834 rt2860_amrr_init(&rvap->amrr, vap,
1835 RT2860_AMRR_MIN_SUCCESS_THRESHOLD,
1836 RT2860_AMRR_MAX_SUCCESS_THRESHOLD,
1837 500);
1839 ieee80211_vap_attach(vap, rt2860_media_change, ieee80211_media_status);
1841 ic->ic_opmode = opmode;
1843 return vap;
1847 * rt2860_vap_delete
1849 static void rt2860_vap_delete(struct ieee80211vap *vap)
1851 struct rt2860_softc *sc;
1852 struct ieee80211com *ic;
1853 struct ifnet *ifp;
1854 struct rt2860_softc_vap *rvap;
1856 ic = vap->iv_ic;
1857 ifp = ic->ic_ifp;
1858 sc = ifp->if_softc;
1859 rvap = (struct rt2860_softc_vap *) vap;
1861 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1862 "%s: VAP delete: opmode=%s\n",
1863 device_get_nameunit(sc->dev),
1864 ieee80211_opmode_name[vap->iv_opmode]);
1866 rt2860_amrr_cleanup(&rvap->amrr);
1868 ieee80211_vap_detach(vap);
1870 free(rvap, M_80211_VAP);
1874 * rt2860_reset_vap
1876 static int rt2860_vap_reset(struct ieee80211vap *vap, u_long cmd)
1878 struct rt2860_softc *sc;
1879 struct ieee80211com *ic;
1880 struct ifnet *ifp;
1881 struct rt2860_softc_vap *rvap;
1882 int error;
1884 ic = vap->iv_ic;
1885 ifp = ic->ic_ifp;
1886 sc = ifp->if_softc;
1887 rvap = (struct rt2860_softc_vap *) vap;
1889 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1890 "%s: VAP reset: cmd=%lu\n",
1891 device_get_nameunit(sc->dev), cmd);
1893 error = 0;
1895 switch (cmd)
1897 case IEEE80211_IOC_RTSTHRESHOLD:
1898 rt2860_asic_update_rtsthreshold(sc);
1899 break;
1901 case IEEE80211_IOC_PROTMODE:
1902 case IEEE80211_IOC_HTPROTMODE:
1903 rt2860_asic_updateprot(sc);
1904 break;
1906 case IEEE80211_IOC_TXPOWER:
1907 rt2860_asic_update_txpower(sc);
1908 break;
1910 case IEEE80211_IOC_SHORTGI:
1911 case IEEE80211_IOC_AMPDU_DENSITY:
1912 break;
1914 default:
1915 error = ENETRESET;
1916 break;
1919 return error;
1923 * rt2860_vap_newstate
1925 static int rt2860_vap_newstate(struct ieee80211vap *vap,
1926 enum ieee80211_state nstate, int arg)
1928 struct rt2860_softc *sc;
1929 struct ieee80211com *ic;
1930 struct ifnet *ifp;
1931 struct rt2860_softc_vap *rvap;
1932 struct ieee80211_node *ni;
1933 enum ieee80211_state ostate;
1934 int error;
1936 ic = vap->iv_ic;
1937 ifp = ic->ic_ifp;
1938 sc = ifp->if_softc;
1939 rvap = (struct rt2860_softc_vap *) vap;
1941 ostate = vap->iv_state;
1943 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
1944 "%s: VAP newstate: %s -> %s\n",
1945 device_get_nameunit(sc->dev),
1946 ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
1948 error = rvap->newstate(vap, nstate, arg);
1949 if (error != 0)
1950 return error;
1952 IEEE80211_UNLOCK(ic);
1954 RT2860_SOFTC_LOCK(sc);
1956 /* turn link LED off */
1958 if (nstate != IEEE80211_S_RUN)
1959 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_OFF);
1961 switch (nstate)
1963 case IEEE80211_S_INIT:
1964 rt2860_asic_disable_tsf_sync(sc);
1965 break;
1967 case IEEE80211_S_RUN:
1968 ni = vap->iv_bss;
1970 rt2860_rf_set_chan(sc, ni->ni_chan);
1972 if (vap->iv_opmode != IEEE80211_M_MONITOR)
1974 rt2860_asic_enable_mrr(sc);
1975 rt2860_asic_set_txpreamble(sc);
1976 rt2860_asic_set_basicrates(sc);
1977 rt2860_asic_update_txpower(sc);
1978 rt2860_asic_set_bssid(sc, ni->ni_bssid);
1981 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1982 vap->iv_opmode == IEEE80211_M_IBSS ||
1983 vap->iv_opmode == IEEE80211_M_MBSS)
1985 error = rt2860_asic_update_beacon(sc, vap);
1986 if (error != 0)
1987 break;
1990 if (vap->iv_opmode != IEEE80211_M_MONITOR)
1991 rt2860_asic_enable_tsf_sync(sc);
1993 /* turn link LED on */
1995 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1997 rt2860_led_cmd(sc, RT2860_LED_CMD_RADIO_ON |
1998 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1999 RT2860_LED_CMD_LINK_2GHZ : RT2860_LED_CMD_LINK_5GHZ));
2001 break;
2003 default:
2004 break;
2007 RT2860_SOFTC_UNLOCK(sc);
2009 IEEE80211_LOCK(ic);
2011 return error;
2015 * rt2860_vap_key_update_begin
2017 static void rt2860_vap_key_update_begin(struct ieee80211vap *vap)
2019 struct rt2860_softc *sc;
2020 struct ieee80211com *ic;
2021 struct ifnet *ifp;
2023 ic = vap->iv_ic;
2024 ifp = ic->ic_ifp;
2025 sc = ifp->if_softc;
2027 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2028 "%s: VAP key update begin\n",
2029 device_get_nameunit(sc->dev));
2031 taskqueue_block(sc->taskqueue);
2033 IF_LOCK(&ifp->if_snd);
2037 * rt2860_vap_key_update_end
2039 static void rt2860_vap_key_update_end(struct ieee80211vap *vap)
2041 struct rt2860_softc *sc;
2042 struct ieee80211com *ic;
2043 struct ifnet *ifp;
2045 ic = vap->iv_ic;
2046 ifp = ic->ic_ifp;
2047 sc = ifp->if_softc;
2049 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2050 "%s: VAP key update end\n",
2051 device_get_nameunit(sc->dev));
2053 IF_UNLOCK(&ifp->if_snd);
2055 taskqueue_unblock(sc->taskqueue);
2059 * rt2860_vap_key_set
2061 static int rt2860_vap_key_set(struct ieee80211vap *vap,
2062 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2064 struct rt2860_softc *sc;
2065 struct ieee80211com *ic;
2066 struct ifnet *ifp;
2067 struct ieee80211_node *ni;
2068 uint16_t associd, key_base, keymode_base;
2069 uint8_t mode, vapid, wcid, iv[8];
2070 uint32_t tmp;
2072 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2073 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2074 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2075 return EINVAL;
2077 ic = vap->iv_ic;
2078 ifp = ic->ic_ifp;
2079 sc = ifp->if_softc;
2081 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2082 ni = vap->iv_bss;
2083 else
2084 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, mac);
2086 associd = (ni != NULL) ? ni->ni_associd : 0;
2088 if ((vap->iv_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2089 ieee80211_free_node(ni);
2091 switch (k->wk_cipher->ic_cipher)
2093 case IEEE80211_CIPHER_WEP:
2094 if(k->wk_keylen < 8)
2095 mode = RT2860_REG_CIPHER_MODE_WEP40;
2096 else
2097 mode = RT2860_REG_CIPHER_MODE_WEP104;
2098 break;
2100 case IEEE80211_CIPHER_TKIP:
2101 mode = RT2860_REG_CIPHER_MODE_TKIP;
2102 break;
2104 case IEEE80211_CIPHER_AES_CCM:
2105 mode = RT2860_REG_CIPHER_MODE_AES_CCMP;
2106 break;
2108 default:
2109 return EINVAL;
2112 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2113 "%s: VAP key set: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2114 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2115 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2117 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2119 /* install pairwise key */
2121 vapid = 0;
2122 wcid = RT2860_AID2WCID(associd);
2123 key_base = RT2860_REG_PKEY(wcid);
2125 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2127 memset(iv, 0, 8);
2129 iv[3] = (k->wk_keyix << 6);
2131 else
2133 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2135 iv[0] = (k->wk_keytsc >> 8);
2136 iv[1] = ((iv[0] | 0x20) & 0x7f);
2137 iv[2] = k->wk_keytsc;
2139 else
2141 /* AES CCMP */
2143 iv[0] = k->wk_keytsc;
2144 iv[1] = k->wk_keytsc >> 8;
2145 iv[2] = 0;
2148 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2149 iv[4] = (k->wk_keytsc >> 16);
2150 iv[5] = (k->wk_keytsc >> 24);
2151 iv[6] = (k->wk_keytsc >> 32);
2152 iv[7] = (k->wk_keytsc >> 40);
2154 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2155 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2156 device_get_nameunit(sc->dev),
2157 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2160 rt2860_io_mac_write_multi(sc, RT2860_REG_IVEIV(wcid), iv, 8);
2162 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2164 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2166 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2168 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2169 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2171 else
2173 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2174 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2177 else
2179 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2182 tmp = ((vapid & RT2860_REG_VAP_MASK) << RT2860_REG_VAP_SHIFT) |
2183 (mode << RT2860_REG_CIPHER_MODE_SHIFT) | RT2860_REG_PKEY_ENABLE;
2185 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2188 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2189 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2191 /* install group key */
2193 vapid = 0;
2194 key_base = RT2860_REG_SKEY(vapid, k->wk_keyix);
2195 keymode_base = RT2860_REG_SKEY_MODE(vapid);
2197 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2199 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2201 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2203 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2204 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2206 else
2208 rt2860_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2209 rt2860_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2212 else
2214 rt2860_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2217 tmp = rt2860_io_mac_read(sc, keymode_base);
2219 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2220 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2222 rt2860_io_mac_write(sc, keymode_base, tmp);
2225 return 1;
2229 * rt2860_vap_key_delete
2231 static int rt2860_vap_key_delete(struct ieee80211vap *vap,
2232 const struct ieee80211_key *k)
2234 struct rt2860_softc *sc;
2235 struct ieee80211com *ic;
2236 struct ifnet *ifp;
2237 struct ieee80211_node *ni;
2238 uint16_t associd;
2239 uint8_t vapid, wcid;
2240 uint32_t tmp;
2242 ic = vap->iv_ic;
2243 ifp = ic->ic_ifp;
2244 sc = ifp->if_softc;
2245 ni = vap->iv_bss;
2246 associd = (ni != NULL) ? ni->ni_associd : 0;
2248 RT2860_DPRINTF(sc, RT2860_DEBUG_KEY,
2249 "%s: VAP key delete: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2250 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2251 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2253 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2255 /* remove pairwise key */
2257 wcid = RT2860_AID2WCID(associd);
2259 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID_ATTR(wcid));
2261 tmp &= ~0xf;
2262 tmp |= (RT2860_REG_CIPHER_MODE_NONE << RT2860_REG_CIPHER_MODE_SHIFT);
2264 rt2860_io_mac_write(sc, RT2860_REG_WCID_ATTR(wcid), tmp);
2266 else
2268 /* remove group key */
2270 vapid = 0;
2272 tmp = rt2860_io_mac_read(sc, RT2860_REG_SKEY_MODE(vapid));
2274 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2275 tmp |= (RT2860_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2277 rt2860_io_mac_write(sc, RT2860_REG_SKEY_MODE(vapid), tmp);
2280 return 1;
2284 * rt2860_vap_update_beacon
2286 static void rt2860_vap_update_beacon(struct ieee80211vap *vap, int what)
2288 struct rt2860_softc *sc;
2289 struct ieee80211com *ic;
2290 struct ifnet *ifp;
2292 ic = vap->iv_ic;
2293 ifp = ic->ic_ifp;
2294 sc = ifp->if_softc;
2296 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2297 "%s: VAP update beacon\n",
2298 device_get_nameunit(sc->dev));
2300 rt2860_asic_update_beacon(sc, vap);
2304 * rt2860_media_change
2306 static int rt2860_media_change(struct ifnet *ifp)
2308 int error;
2310 error = ieee80211_media_change(ifp);
2312 return (error == ENETRESET ? 0 : error);
2316 * rt2860_node_alloc
2318 static struct ieee80211_node *rt2860_node_alloc(struct ieee80211vap *vap,
2319 const uint8_t mac[IEEE80211_ADDR_LEN])
2321 return malloc(sizeof(struct rt2860_softc_node),
2322 M_80211_NODE, M_NOWAIT | M_ZERO);
2326 * rt2860_setregdomain
2328 static int rt2860_setregdomain(struct ieee80211com *ic,
2329 struct ieee80211_regdomain *reg,
2330 int nchans, struct ieee80211_channel chans[])
2332 struct rt2860_softc *sc;
2333 struct ifnet *ifp;
2335 ifp = ic->ic_ifp;
2336 sc = ifp->if_softc;
2338 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2339 "%s: set regulatory domain: country=%d, country code string=%c%c, location=%c\n",
2340 device_get_nameunit(sc->dev),
2341 reg->country, reg->isocc[0], reg->isocc[1], reg->location);
2343 return 0;
2347 * rt2860_getradiocaps
2349 static void rt2860_getradiocaps(struct ieee80211com *ic,
2350 int maxchans, int *nchans, struct ieee80211_channel chans[])
2352 *nchans = (ic->ic_nchans >= maxchans) ? maxchans : ic->ic_nchans;
2354 memcpy(chans, ic->ic_channels, (*nchans) * sizeof(struct ieee80211_channel));
2358 * rt2860_scan_start
2360 static void rt2860_scan_start(struct ieee80211com *ic)
2362 struct rt2860_softc *sc;
2363 struct ifnet *ifp;
2365 ifp = ic->ic_ifp;
2366 sc = ifp->if_softc;
2368 rt2860_asic_disable_tsf_sync(sc);
2372 * rt2860_scan_end
2374 static void rt2860_scan_end(struct ieee80211com *ic)
2376 struct rt2860_softc *sc;
2377 struct ifnet *ifp;
2379 ifp = ic->ic_ifp;
2380 sc = ifp->if_softc;
2382 rt2860_asic_enable_tsf_sync(sc);
2386 * rt2860_set_channel
2388 static void rt2860_set_channel(struct ieee80211com *ic)
2390 struct rt2860_softc *sc;
2391 struct ifnet *ifp;
2393 ifp = ic->ic_ifp;
2394 sc = ifp->if_softc;
2396 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
2397 "%s: set channel: channel=%u, HT%s%s\n",
2398 device_get_nameunit(sc->dev),
2399 ieee80211_chan2ieee(ic, ic->ic_curchan),
2400 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
2401 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
2402 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
2403 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
2405 RT2860_SOFTC_LOCK(sc);
2407 rt2860_rf_set_chan(sc, ic->ic_curchan);
2409 RT2860_SOFTC_UNLOCK(sc);
2413 * rt2860_newassoc
2415 static void rt2860_newassoc(struct ieee80211_node *ni, int isnew)
2417 struct rt2860_softc *sc;
2418 struct ieee80211com *ic;
2419 struct ifnet *ifp;
2420 struct ieee80211vap *vap;
2421 struct rt2860_softc_vap *rvap;
2422 uint16_t associd;
2423 uint8_t wcid;
2425 vap = ni->ni_vap;
2426 ic = vap->iv_ic;
2427 ifp = ic->ic_ifp;
2428 sc = ifp->if_softc;
2429 rvap = (struct rt2860_softc_vap *) vap;
2431 associd = (ni != NULL) ? ni->ni_associd : 0;
2432 wcid = RT2860_AID2WCID(associd);
2434 RT2860_DPRINTF(sc, RT2860_DEBUG_NODE,
2435 "%s: new association: wcid=0x%02x, "
2436 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2437 device_get_nameunit(sc->dev), wcid,
2438 ether_sprintf(ni->ni_macaddr),
2439 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
2440 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
2441 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
2443 rt2860_io_mac_write_multi(sc, RT2860_REG_WCID(wcid),
2444 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2446 rt2860_amrr_node_init(&rvap->amrr, &sc->amrr_node[wcid], ni);
2448 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2449 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2450 device_get_nameunit(sc->dev),
2451 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2452 ni->ni_associd, ni->ni_txrate,
2453 (ni->ni_flags & IEEE80211_NODE_HT) ?
2454 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2455 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2457 rt2860_asic_updateprot(sc);
2458 rt2860_asic_updateslot(sc);
2459 rt2860_asic_set_txpreamble(sc);
2463 * rt2860_updateslot
2465 static void rt2860_updateslot(struct ifnet *ifp)
2467 struct rt2860_softc *sc;
2469 sc = ifp->if_softc;
2471 rt2860_asic_updateslot(sc);
2475 * rt2860_update_promisc
2477 static void rt2860_update_promisc(struct ifnet *ifp)
2479 struct rt2860_softc *sc;
2481 sc = ifp->if_softc;
2483 rt2860_asic_update_promisc(sc);
2487 * rt2860_update_mcast
2489 static void rt2860_update_mcast(struct ifnet *ifp)
2491 struct rt2860_softc *sc;
2493 sc = ifp->if_softc;
2497 * rt2860_wme_update
2499 static int rt2860_wme_update(struct ieee80211com *ic)
2501 struct rt2860_softc *sc;
2502 struct ifnet *ifp;
2504 ifp = ic->ic_ifp;
2505 sc = ifp->if_softc;
2507 rt2860_asic_wme_update(sc);
2509 return 0;
2513 * rt2860_raw_xmit
2515 static int rt2860_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2516 const struct ieee80211_bpf_params *params)
2518 struct rt2860_softc *sc;
2519 struct ieee80211com *ic;
2520 struct ifnet *ifp;
2522 ic = ni->ni_ic;
2523 ifp = ic->ic_ifp;
2524 sc = ifp->if_softc;
2526 RT2860_SOFTC_LOCK(sc);
2528 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2530 RT2860_SOFTC_UNLOCK(sc);
2531 m_freem(m);
2532 ieee80211_free_node(ni);
2533 return ENETDOWN;
2536 if (sc->tx_ring[sc->tx_ring_mgtqid].data_queued >= RT2860_SOFTC_TX_RING_DATA_COUNT)
2538 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
2539 "%s: raw xmit: Tx ring with qid=%d is full\n",
2540 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
2542 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2543 RT2860_SOFTC_UNLOCK(sc);
2544 m_freem(m);
2545 ieee80211_free_node(ni);
2546 return ENOBUFS;
2549 if (rt2860_tx_mgmt(sc, m, ni, sc->tx_ring_mgtqid) != 0)
2551 ifp->if_oerrors++;
2552 RT2860_SOFTC_UNLOCK(sc);
2553 ieee80211_free_node(ni);
2554 return EIO;
2557 sc->tx_timer = RT2860_TX_WATCHDOG_TIMEOUT;
2559 RT2860_SOFTC_UNLOCK(sc);
2561 return 0;
2565 * rt2860_recv_action
2567 static int rt2860_recv_action(struct ieee80211_node *ni,
2568 const struct ieee80211_frame *wh,
2569 const uint8_t *frm, const uint8_t *efrm)
2571 struct rt2860_softc *sc;
2572 struct ieee80211com *ic;
2573 struct ifnet *ifp;
2574 const struct ieee80211_action *ia;
2575 uint16_t associd, baparamset;
2576 uint8_t wcid;
2577 int ret, tid, bufsize;
2578 uint32_t tmp;
2580 ic = ni->ni_ic;
2581 ifp = ic->ic_ifp;
2582 sc = ifp->if_softc;
2584 ia = (const struct ieee80211_action *) frm;
2586 ret = sc->recv_action(ni, wh, frm, efrm);
2588 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2589 return ret;
2591 associd = (ni != NULL) ? ni->ni_associd : 0;
2592 wcid = RT2860_AID2WCID(associd);
2594 switch (ia->ia_action)
2596 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2597 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2598 baparamset = LE_READ_2(frm + 3);
2599 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2600 bufsize = RT2860_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2602 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2603 "%s: received ADDBA: associd=0x%04x, tid=%d, bufsize=%d\n",
2604 device_get_nameunit(sc->dev), associd, tid, bufsize);
2606 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2608 tmp |= (0x10000 << tid);
2610 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2611 break;
2613 /* IEEE80211_ACTION_BA_DELBA */
2614 case IEEE80211_ACTION_BA_DELBA:
2615 baparamset = LE_READ_2(frm + 2);
2616 tid = RT2860_MS(baparamset, IEEE80211_BAPS_TID);
2618 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2619 "%s: received DELBA: associd=0x%04x, tid=%d\n",
2620 device_get_nameunit(sc->dev), associd, tid);
2622 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2624 tmp &= ~(0x10000 << tid);
2626 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2627 break;
2630 return ret;
2634 * rt2860_send_action
2636 static int rt2860_send_action(struct ieee80211_node *ni,
2637 int cat, int act, void *sa)
2639 struct rt2860_softc *sc;
2640 struct ieee80211com *ic;
2641 struct ifnet *ifp;
2642 uint16_t associd, *args, baparamset;
2643 uint8_t wcid;
2644 int ret, tid;
2645 uint32_t tmp;
2647 ic = ni->ni_ic;
2648 ifp = ic->ic_ifp;
2649 sc = ifp->if_softc;
2651 ret = sc->send_action(ni, cat, act, sa);
2653 if (cat != IEEE80211_ACTION_CAT_BA)
2654 return ret;
2656 associd = (ni != NULL) ? ni->ni_associd : 0;
2657 wcid = RT2860_AID2WCID(associd);
2658 args = sa;
2660 switch (act)
2662 /* IEEE80211_ACTION_BA_DELBA */
2663 case IEEE80211_ACTION_BA_DELBA:
2664 baparamset = RT2860_SM(args[0], IEEE80211_DELBAPS_TID) | args[1];
2666 if (RT2860_MS(baparamset, IEEE80211_DELBAPS_INIT) == IEEE80211_DELBAPS_INIT)
2667 break;
2669 tid = RT2860_MS(baparamset, IEEE80211_DELBAPS_TID);
2671 RT2860_DPRINTF(sc, RT2860_DEBUG_BA,
2672 "%s: sending DELBA: associd=0x%04x, tid=%d\n",
2673 device_get_nameunit(sc->dev), associd, tid);
2675 tmp = rt2860_io_mac_read(sc, RT2860_REG_WCID(wcid) + 4);
2677 tmp &= ~(0x10000 << tid);
2679 rt2860_io_mac_write(sc, RT2860_REG_WCID(wcid) + 4, tmp);
2680 break;
2683 return ret;
2687 * rt2860_amrr_update_iter_func
2689 static void rt2860_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2691 struct rt2860_softc *sc;
2692 struct ieee80211com *ic;
2693 struct ifnet *ifp;
2694 struct ieee80211vap *vap;
2695 struct rt2860_softc_vap *rvap;
2696 uint8_t wcid;
2698 vap = arg;
2699 ic = vap->iv_ic;
2700 ifp = ic->ic_ifp;
2701 sc = ifp->if_softc;
2702 rvap = (struct rt2860_softc_vap *) vap;
2704 /* only associated stations */
2706 if ((ni->ni_vap == vap) && (ni->ni_associd != 0))
2708 wcid = RT2860_AID2WCID(ni->ni_associd);
2710 rt2860_amrr_choose(ni, &sc->amrr_node[wcid]);
2712 RT2860_DPRINTF(sc, RT2860_DEBUG_RATE,
2713 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2714 device_get_nameunit(sc->dev),
2715 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2716 ni->ni_associd, ni->ni_txrate,
2717 (ni->ni_flags & IEEE80211_NODE_HT) ?
2718 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2719 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2724 * rt2860_periodic
2726 static void rt2860_periodic(void *arg)
2728 struct rt2860_softc *sc;
2730 sc = arg;
2732 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
2733 "%s: periodic\n",
2734 device_get_nameunit(sc->dev));
2736 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2740 * rt2860_tx_watchdog
2742 static void rt2860_tx_watchdog(void *arg)
2744 struct rt2860_softc *sc;
2745 struct ifnet *ifp;
2747 sc = arg;
2748 ifp = sc->ifp;
2750 if (sc->tx_timer == 0)
2751 return;
2753 if (--sc->tx_timer == 0)
2755 printf("%s: Tx watchdog timeout: resetting\n",
2756 device_get_nameunit(sc->dev));
2758 rt2860_stop_locked(sc);
2759 rt2860_init_locked(sc);
2761 ifp->if_oerrors++;
2763 sc->tx_watchdog_timeouts++;
2766 callout_reset(&sc->tx_watchdog_ch, hz, rt2860_tx_watchdog, sc);
2770 * rt2860_asic_set_bssid
2772 static void rt2860_asic_set_bssid(struct rt2860_softc *sc,
2773 const uint8_t *bssid)
2775 uint32_t tmp;
2777 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2778 "%s: set bssid: bssid=%s\n",
2779 device_get_nameunit(sc->dev),
2780 ether_sprintf(bssid));
2782 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2784 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW0, tmp);
2786 tmp = bssid[4] | (bssid[5] << 8);
2788 rt2860_io_mac_write(sc, RT2860_REG_BSSID_DW1, tmp);
2792 * rt2860_asic_set_macaddr
2794 static void rt2860_asic_set_macaddr(struct rt2860_softc *sc,
2795 const uint8_t *addr)
2797 uint32_t tmp;
2799 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2801 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW0, tmp);
2803 tmp = addr[4] | (addr[5] << 8);
2805 rt2860_io_mac_write(sc, RT2860_REG_ADDR_DW1, tmp);
2809 * rt2860_asic_enable_tsf_sync
2811 static void rt2860_asic_enable_tsf_sync(struct rt2860_softc *sc)
2813 struct ifnet *ifp;
2814 struct ieee80211com *ic;
2815 struct ieee80211vap *vap;
2816 uint32_t tmp;
2818 ifp = sc->ifp;
2819 ic = ifp->if_l2com;
2820 vap = TAILQ_FIRST(&ic->ic_vaps);
2822 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2823 "%s: enabling TSF\n",
2824 device_get_nameunit(sc->dev));
2826 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2828 tmp &= ~0x1fffff;
2829 tmp |= vap->iv_bss->ni_intval * 16;
2830 tmp |= (RT2860_REG_TSF_TIMER_ENABLE | RT2860_REG_TBTT_TIMER_ENABLE);
2832 if (vap->iv_opmode == IEEE80211_M_STA)
2834 tmp |= (RT2860_REG_TSF_SYNC_MODE_STA << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2836 else if (vap->iv_opmode == IEEE80211_M_IBSS)
2838 tmp |= RT2860_REG_BCN_TX_ENABLE;
2839 tmp |= (RT2860_REG_TSF_SYNC_MODE_IBSS << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2841 else if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2843 tmp |= RT2860_REG_BCN_TX_ENABLE;
2844 tmp |= (RT2860_REG_TSF_SYNC_MODE_HOSTAP << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2847 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2851 * rt2860_asic_disable_tsf_sync
2853 static void rt2860_asic_disable_tsf_sync(struct rt2860_softc *sc)
2855 uint32_t tmp;
2857 RT2860_DPRINTF(sc, RT2860_DEBUG_BEACON,
2858 "%s: disabling TSF\n",
2859 device_get_nameunit(sc->dev));
2861 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
2863 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
2864 RT2860_REG_TSF_TIMER_ENABLE |
2865 RT2860_REG_TBTT_TIMER_ENABLE);
2867 tmp &= ~(RT2860_REG_TSF_SYNC_MODE_MASK << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2868 tmp |= (RT2860_REG_TSF_SYNC_MODE_DISABLE << RT2860_REG_TSF_SYNC_MODE_SHIFT);
2870 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
2874 * rt2860_asic_enable_mrr
2876 static void rt2860_asic_enable_mrr(struct rt2860_softc *sc)
2878 #define CCK(mcs) (mcs)
2879 #define OFDM(mcs) ((1 << 3) | (mcs))
2880 #define HT(mcs) (mcs)
2882 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG0,
2883 (OFDM(6) << 28) | /* 54 -> 48 */
2884 (OFDM(5) << 24) | /* 48 -> 36 */
2885 (OFDM(4) << 20) | /* 36 -> 24 */
2886 (OFDM(3) << 16) | /* 24 -> 18 */
2887 (OFDM(2) << 12) | /* 18 -> 12 */
2888 (OFDM(1) << 8) | /* 12 -> 9 */
2889 (OFDM(0) << 4) | /* 9 -> 6 */
2890 OFDM(0)); /* 6 -> 6 */
2892 rt2860_io_mac_write(sc, RT2860_REG_TX_LG_FBK_CFG1,
2893 (CCK(2) << 12) | /* 11 -> 5.5 */
2894 (CCK(1) << 8) | /* 5.5 -> 2 */
2895 (CCK(0) << 4) | /* 2 -> 1 */
2896 CCK(0)); /* 1 -> 1 */
2898 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG0,
2899 (HT(6) << 28) |
2900 (HT(5) << 24) |
2901 (HT(4) << 20) |
2902 (HT(3) << 16) |
2903 (HT(2) << 12) |
2904 (HT(1) << 8) |
2905 (HT(0) << 4) |
2906 HT(0));
2908 rt2860_io_mac_write(sc, RT2860_REG_TX_HT_FBK_CFG1,
2909 (HT(14) << 28) |
2910 (HT(13) << 24) |
2911 (HT(12) << 20) |
2912 (HT(11) << 16) |
2913 (HT(10) << 12) |
2914 (HT(9) << 8) |
2915 (HT(8) << 4) |
2916 HT(8));
2918 #undef HT
2919 #undef OFDM
2920 #undef CCK
2924 * rt2860_asic_set_txpreamble
2926 static void rt2860_asic_set_txpreamble(struct rt2860_softc *sc)
2928 struct ifnet *ifp;
2929 struct ieee80211com *ic;
2930 uint32_t tmp;
2932 ifp = sc->ifp;
2933 ic = ifp->if_l2com;
2935 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
2936 "%s: %s short Tx preamble\n",
2937 device_get_nameunit(sc->dev),
2938 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
2940 tmp = rt2860_io_mac_read(sc, RT2860_REG_AUTO_RSP_CFG);
2942 tmp &= ~RT2860_REG_CCK_SHORT_ENABLE;
2944 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2945 tmp |= RT2860_REG_CCK_SHORT_ENABLE;
2947 rt2860_io_mac_write(sc, RT2860_REG_AUTO_RSP_CFG, tmp);
2951 * rt2860_asic_set_basicrates
2953 static void rt2860_asic_set_basicrates(struct rt2860_softc *sc)
2955 struct ifnet *ifp;
2956 struct ieee80211com *ic;
2958 ifp = sc->ifp;
2959 ic = ifp->if_l2com;
2961 if (ic->ic_curmode == IEEE80211_MODE_11B)
2962 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x3);
2963 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2964 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x150);
2965 else
2966 rt2860_io_mac_write(sc, RT2860_REG_LEGACY_BASIC_RATE, 0x15f);
2970 * rt2860_asic_update_rtsthreshold
2972 static void rt2860_asic_update_rtsthreshold(struct rt2860_softc *sc)
2974 struct ifnet *ifp;
2975 struct ieee80211com *ic;
2976 struct ieee80211vap *vap;
2977 uint32_t tmp;
2978 uint16_t threshold;
2980 ifp = sc->ifp;
2981 ic = ifp->if_l2com;
2982 vap = TAILQ_FIRST(&ic->ic_vaps);
2984 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
2985 "%s: updating RTS threshold: %d\n",
2986 device_get_nameunit(sc->dev), vap->iv_rtsthreshold);
2988 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_RTS_CFG);
2990 tmp &= ~(RT2860_REG_TX_RTS_THRESHOLD_MASK << RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2992 threshold = (vap->iv_rtsthreshold < IEEE80211_RTS_MAX) ?
2993 vap->iv_rtsthreshold : 0x1000;
2995 tmp |= ((threshold & RT2860_REG_TX_RTS_THRESHOLD_MASK) <<
2996 RT2860_REG_TX_RTS_THRESHOLD_SHIFT);
2998 rt2860_io_mac_write(sc, RT2860_REG_TX_RTS_CFG, tmp);
3002 * rt2860_asic_update_txpower
3004 static void rt2860_asic_update_txpower(struct rt2860_softc *sc)
3006 struct ifnet *ifp;
3007 struct ieee80211com *ic;
3008 uint32_t *txpow_rate;
3009 int8_t delta;
3010 uint8_t val;
3011 uint32_t tmp;
3012 int i;
3014 ifp = sc->ifp;
3015 ic = ifp->if_l2com;
3017 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3018 "%s: updating Tx power: %d\n",
3019 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
3021 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
3023 txpow_rate = sc->txpow_rate_20mhz;
3025 else
3027 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3028 txpow_rate = sc->txpow_rate_40mhz_2ghz;
3029 else
3030 txpow_rate = sc->txpow_rate_40mhz_5ghz;
3033 delta = 0;
3035 val = rt2860_io_bbp_read(sc, 1);
3036 val &= 0xfc;
3038 if (ic->ic_txpowlimit > 90)
3040 /* do nothing */
3042 else if (ic->ic_txpowlimit > 60)
3044 delta -= 1;
3046 else if (ic->ic_txpowlimit > 30)
3048 delta -= 3;
3050 else if (ic->ic_txpowlimit > 15)
3052 val |= 0x1;
3054 else if (ic->ic_txpowlimit > 9)
3056 val |= 0x1;
3057 delta -= 3;
3059 else
3061 val |= 0x2;
3064 rt2860_io_bbp_write(sc, 1, val);
3066 for (i = 0; i < RT2860_SOFTC_TXPOW_RATE_COUNT; i++)
3068 if (txpow_rate[i] == 0xffffffff)
3069 continue;
3071 tmp = rt2860_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
3073 rt2860_io_mac_write(sc, RT2860_REG_TX_PWR_CFG(i), tmp);
3078 * rt2860_asic_update_promisc
3080 static void rt2860_asic_update_promisc(struct rt2860_softc *sc)
3082 struct ifnet *ifp;
3083 uint32_t tmp;
3085 ifp = sc->ifp;
3087 printf("%s: %s promiscuous mode\n",
3088 device_get_nameunit(sc->dev),
3089 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
3091 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_FILTER_CFG);
3093 tmp &= ~RT2860_REG_RX_FILTER_DROP_UC_NOME;
3095 if (!(ifp->if_flags & IFF_PROMISC))
3096 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
3098 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
3102 * rt2860_asic_updateprot
3104 static void rt2860_asic_updateprot(struct rt2860_softc *sc)
3106 struct ifnet *ifp;
3107 struct ieee80211com *ic;
3108 struct ieee80211vap *vap;
3109 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
3110 uint8_t htopmode;
3112 ifp = sc->ifp;
3113 ic = ifp->if_l2com;
3114 vap = TAILQ_FIRST(&ic->ic_vaps);
3116 /* CCK frame protection */
3118 cck_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3119 RT2860_REG_TXOP_ALLOW_ALL | RT2860_REG_PROT_CTRL_NONE;
3121 /* set up protection frame phy mode and rate (MCS code) */
3123 if (ic->ic_curmode == IEEE80211_MODE_11A)
3124 cck_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3125 (0 << RT2860_REG_PROT_MCS_SHIFT);
3126 else
3127 cck_prot |= ((RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3128 (3 << RT2860_REG_PROT_MCS_SHIFT));
3130 rt2860_io_mac_write(sc, RT2860_REG_TX_CCK_PROT_CFG, cck_prot);
3132 /* OFDM frame protection */
3134 ofdm_prot = RT2860_REG_RTSTH_ENABLE | RT2860_REG_PROT_NAV_SHORT |
3135 RT2860_REG_TXOP_ALLOW_ALL;
3137 if (ic->ic_flags & IEEE80211_F_USEPROT)
3139 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3140 "%s: updating protection mode: b/g protection mode=%s\n",
3141 device_get_nameunit(sc->dev),
3142 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3143 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3145 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3146 ofdm_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3147 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3148 ofdm_prot |= RT2860_REG_PROT_CTRL_CTS;
3149 else
3150 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3152 else
3154 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3155 "%s: updating protection mode: b/g protection mode=%s\n",
3156 device_get_nameunit(sc->dev), "none");
3158 ofdm_prot |= RT2860_REG_PROT_CTRL_NONE;
3161 rt2860_io_mac_write(sc, RT2860_REG_TX_OFDM_PROT_CFG, ofdm_prot);
3163 /* HT frame protection */
3165 if ((vap->iv_opmode == IEEE80211_M_STA) && (vap->iv_state == IEEE80211_S_RUN))
3166 htopmode = vap->iv_bss->ni_htopmode;
3167 else
3168 htopmode = ic->ic_curhtprotmode;
3170 RT2860_DPRINTF(sc, RT2860_DEBUG_PROT,
3171 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3172 device_get_nameunit(sc->dev),
3173 htopmode & IEEE80211_HTINFO_OPMODE,
3174 (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3175 ((ic->ic_htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3177 switch (htopmode & IEEE80211_HTINFO_OPMODE)
3179 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3180 case IEEE80211_HTINFO_OPMODE_HT20PR:
3181 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3182 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3183 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3184 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3185 (4 << RT2860_REG_PROT_MCS_SHIFT);
3187 gf20_prot = mm20_prot;
3189 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL |
3190 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3191 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3193 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
3194 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3195 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
3196 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3197 else
3198 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3200 gf40_prot = mm40_prot;
3201 break;
3203 /* IEEE80211_HTINFO_OPMODE_MIXED */
3204 case IEEE80211_HTINFO_OPMODE_MIXED:
3205 mm20_prot = RT2860_REG_PROT_NAV_SHORT |
3206 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3207 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20;
3209 if (ic->ic_flags & IEEE80211_F_USEPROT)
3210 mm20_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3211 (3 << RT2860_REG_PROT_MCS_SHIFT);
3212 else
3213 mm20_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3214 (4 << RT2860_REG_PROT_MCS_SHIFT);
3216 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
3217 mm20_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3218 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
3219 mm20_prot |= RT2860_REG_PROT_CTRL_CTS;
3220 else
3221 mm20_prot |= RT2860_REG_PROT_CTRL_NONE;
3223 gf20_prot = mm20_prot;
3225 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_TXOP_ALLOW_ALL;
3227 if (ic->ic_flags & IEEE80211_F_USEPROT)
3228 mm40_prot |= (RT2860_REG_PROT_PHYMODE_CCK << RT2860_REG_PROT_PHYMODE_SHIFT) |
3229 (3 << RT2860_REG_PROT_MCS_SHIFT);
3230 else
3231 mm40_prot |= (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3232 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3234 if (ic->ic_htprotmode == IEEE80211_PROT_RTSCTS)
3235 mm40_prot |= RT2860_REG_PROT_CTRL_RTS_CTS;
3236 else if (ic->ic_htprotmode == IEEE80211_PROT_CTSONLY)
3237 mm40_prot |= RT2860_REG_PROT_CTRL_CTS;
3238 else
3239 mm40_prot |= RT2860_REG_PROT_CTRL_NONE;
3241 gf40_prot = mm40_prot;
3242 break;
3245 * IEEE80211_HTINFO_OPMODE_PURE
3246 * IEEE80211_HTINFO_OPMODE_PROTOPT
3248 case IEEE80211_HTINFO_OPMODE_PURE:
3249 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3250 default:
3251 mm20_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3252 RT2860_REG_TXOP_ALLOW_CCK | RT2860_REG_TXOP_ALLOW_OFDM |
3253 RT2860_REG_TXOP_ALLOW_MM20 | RT2860_REG_TXOP_ALLOW_GF20 |
3254 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3255 (4 << RT2860_REG_PROT_MCS_SHIFT);
3257 gf20_prot = mm20_prot;
3259 mm40_prot = RT2860_REG_PROT_NAV_SHORT | RT2860_REG_PROT_CTRL_NONE |
3260 RT2860_REG_TXOP_ALLOW_ALL |
3261 (RT2860_REG_PROT_PHYMODE_OFDM << RT2860_REG_PROT_PHYMODE_SHIFT) |
3262 (0x84 << RT2860_REG_PROT_MCS_SHIFT);
3264 gf40_prot = mm40_prot;
3265 break;
3268 rt2860_io_mac_write(sc, RT2860_REG_TX_MM20_PROT_CFG, mm20_prot);
3269 rt2860_io_mac_write(sc, RT2860_REG_TX_MM40_PROT_CFG, mm40_prot);
3270 rt2860_io_mac_write(sc, RT2860_REG_TX_GF20_PROT_CFG, gf20_prot);
3271 rt2860_io_mac_write(sc, RT2860_REG_TX_GF40_PROT_CFG, gf40_prot);
3275 * rt2860_asic_updateslot
3277 static void rt2860_asic_updateslot(struct rt2860_softc *sc)
3279 struct ifnet *ifp;
3280 struct ieee80211com *ic;
3281 uint32_t tmp;
3283 ifp = sc->ifp;
3284 ic = ifp->if_l2com;
3286 RT2860_DPRINTF(sc, RT2860_DEBUG_STATE,
3287 "%s: %s short slot time\n",
3288 device_get_nameunit(sc->dev),
3289 (ic->ic_flags & IEEE80211_F_SHSLOT) ? "enabling" : "disabling");
3291 tmp = rt2860_io_mac_read(sc, RT2860_REG_BKOFF_SLOT_CFG);
3293 tmp &= ~0xff;
3294 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
3296 rt2860_io_mac_write(sc, RT2860_REG_BKOFF_SLOT_CFG, tmp);
3300 * rt2860_asic_wme_update
3302 static void rt2860_asic_wme_update(struct rt2860_softc *sc)
3304 struct ifnet *ifp;
3305 struct ieee80211com *ic;
3306 struct ieee80211_wme_state *wme;
3307 const struct wmeParams *wmep;
3308 int i;
3310 ifp = sc->ifp;
3311 ic = ifp->if_l2com;
3312 wme = &ic->ic_wme;
3313 wmep = wme->wme_chanParams.cap_wmeParams;
3315 RT2860_DPRINTF(sc, RT2860_DEBUG_WME,
3316 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3317 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3318 device_get_nameunit(sc->dev),
3319 wmep[WME_AC_VO].wmep_aifsn,
3320 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3321 wmep[WME_AC_VO].wmep_txopLimit,
3322 wmep[WME_AC_VI].wmep_aifsn,
3323 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3324 wmep[WME_AC_VI].wmep_txopLimit,
3325 wmep[WME_AC_BK].wmep_aifsn,
3326 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3327 wmep[WME_AC_BK].wmep_txopLimit,
3328 wmep[WME_AC_BE].wmep_aifsn,
3329 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3330 wmep[WME_AC_BE].wmep_txopLimit);
3332 for (i = 0; i < WME_NUM_AC; i++)
3333 rt2860_io_mac_write(sc, RT2860_REG_TX_EDCA_AC_CFG(i),
3334 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3335 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3337 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_AIFSN_CFG,
3338 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3339 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3341 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMIN_CFG,
3342 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3343 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3345 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_CWMAX_CFG,
3346 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3347 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3349 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP0_CFG,
3350 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3352 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WMM_TXOP1_CFG,
3353 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3357 * rt2860_asic_update_beacon
3359 static int rt2860_asic_update_beacon(struct rt2860_softc *sc,
3360 struct ieee80211vap *vap)
3362 struct ifnet *ifp;
3363 struct ieee80211com *ic;
3364 struct rt2860_softc_vap *rvap;
3365 const struct ieee80211_txparam *tp;
3366 struct mbuf *m;
3367 struct rt2860_txwi txwi;
3368 uint8_t rate, mcs;
3369 uint32_t tmp;
3371 ifp = sc->ifp;
3372 ic = ifp->if_l2com;
3373 rvap = (struct rt2860_softc_vap *) vap;
3374 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
3376 m = ieee80211_beacon_alloc(vap->iv_bss, &rvap->beacon_offsets);
3377 if (m == NULL)
3378 return ENOMEM;
3380 rate = tp->mgmtrate;
3381 mcs = rt2860_rate2mcs(rate);
3383 memset(&txwi, 0, sizeof(struct rt2860_txwi));
3385 txwi.wcid = 0xff;
3386 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3387 RT2860_TXWI_MPDU_LEN_SHIFT);
3388 txwi.txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
3389 txwi.mpdu_density_flags |=
3390 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3391 txwi.bawin_size_xflags |=
3392 (RT2860_TXWI_XFLAGS_NSEQ << RT2860_TXWI_XFLAGS_SHIFT);
3394 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
3396 txwi.phymode_ifs_stbc_shortgi =
3397 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3399 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3400 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3402 else
3404 txwi.phymode_ifs_stbc_shortgi =
3405 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3408 txwi.bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3409 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3411 /* disable temporarily TSF sync */
3413 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3415 tmp &= ~(RT2860_REG_BCN_TX_ENABLE |
3416 RT2860_REG_TSF_TIMER_ENABLE |
3417 RT2860_REG_TBTT_TIMER_ENABLE);
3419 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3421 /* write Tx wireless info and beacon frame to on-chip memory */
3423 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0),
3424 &txwi, sizeof(struct rt2860_txwi));
3426 rt2860_io_mac_write_multi(sc, RT2860_REG_BEACON_BASE(0) + sizeof(struct rt2860_txwi),
3427 mtod(m, uint8_t *), m->m_pkthdr.len);
3429 /* enable again TSF sync */
3431 tmp = rt2860_io_mac_read(sc, RT2860_REG_BCN_TIME_CFG);
3433 tmp |= (RT2860_REG_BCN_TX_ENABLE |
3434 RT2860_REG_TSF_TIMER_ENABLE |
3435 RT2860_REG_TBTT_TIMER_ENABLE);
3437 rt2860_io_mac_write(sc, RT2860_REG_BCN_TIME_CFG, tmp);
3439 m_freem(m);
3441 return 0;
3445 * rt2860_asic_clear_keytables
3447 static void rt2860_asic_clear_keytables(struct rt2860_softc *sc)
3449 int i;
3451 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3453 for (i = 0; i < 256; i++)
3455 rt2860_io_mac_write(sc, RT2860_REG_WCID(i), 0xffffffff);
3456 rt2860_io_mac_write(sc, RT2860_REG_WCID(i) + 4, 0x0000ffff);
3459 /* clear WCID attribute table (entries = 256, entry size = 4) */
3461 rt2860_io_mac_set_region_4(sc, RT2860_REG_WCID_ATTR(0), 0, 256);
3463 /* clear IV/EIV table (entries = 256, entry size = 8) */
3465 rt2860_io_mac_set_region_4(sc, RT2860_REG_IVEIV(0), 0, 2 * 256);
3467 /* clear pairwise key table (entries = 256, entry size = 32) */
3469 rt2860_io_mac_set_region_4(sc, RT2860_REG_PKEY(0), 0, 8 * 256);
3471 /* clear shared key table (entries = 32, entry size = 32) */
3473 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY(0, 0), 0, 8 * 32);
3475 /* clear shared key mode (entries = 32, entry size = 2) */
3477 rt2860_io_mac_set_region_4(sc, RT2860_REG_SKEY_MODE(0), 0, 16);
3481 * rt2860_rxrate
3483 static uint8_t rt2860_rxrate(struct rt2860_rxwi *rxwi)
3485 uint8_t mcs, phymode;
3486 uint8_t rate;
3488 mcs = (rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK;
3489 phymode = (rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
3490 RT2860_RXWI_PHYMODE_MASK;
3492 rate = 2;
3494 switch (phymode)
3496 case RT2860_RXWI_PHYMODE_CCK:
3497 switch (mcs & ~RT2860_RXWI_MCS_SHOTPRE)
3499 case 0: rate = 2; break; /* 1 Mbps */
3500 case 1: rate = 4; break; /* 2 MBps */
3501 case 2: rate = 11; break; /* 5.5 Mbps */
3502 case 3: rate = 22; break; /* 11 Mbps */
3504 break;
3506 case RT2860_RXWI_PHYMODE_OFDM:
3507 switch (mcs)
3509 case 0: rate = 12; break; /* 6 Mbps */
3510 case 1: rate = 18; break; /* 9 Mbps */
3511 case 2: rate = 24; break; /* 12 Mbps */
3512 case 3: rate = 36; break; /* 18 Mbps */
3513 case 4: rate = 48; break; /* 24 Mbps */
3514 case 5: rate = 72; break; /* 36 Mbps */
3515 case 6: rate = 96; break; /* 48 Mbps */
3516 case 7: rate = 108; break; /* 54 Mbps */
3518 break;
3520 case RT2860_RXWI_PHYMODE_HT_MIXED:
3521 case RT2860_RXWI_PHYMODE_HT_GF:
3522 break;
3525 return rate;
3529 * rt2860_maxrssi_rxpath
3531 static uint8_t rt2860_maxrssi_rxpath(struct rt2860_softc *sc,
3532 const struct rt2860_rxwi *rxwi)
3534 uint8_t rxpath;
3536 rxpath = 0;
3538 if (sc->nrxpath > 1)
3539 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3540 rxpath = 1;
3542 if (sc->nrxpath > 2)
3543 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3544 rxpath = 2;
3546 return rxpath;
3550 * rt2860_rssi2dbm
3552 static int8_t rt2860_rssi2dbm(struct rt2860_softc *sc,
3553 uint8_t rssi, uint8_t rxpath)
3555 struct ifnet *ifp;
3556 struct ieee80211com *ic;
3557 struct ieee80211_channel *c;
3558 int chan;
3559 int8_t rssi_off, lna_gain;
3561 if (rssi == 0)
3562 return -99;
3564 ifp = sc->ifp;
3565 ic = ifp->if_l2com;
3566 c = ic->ic_curchan;
3567 chan = ieee80211_chan2ieee(ic, c);
3569 if (IEEE80211_IS_CHAN_5GHZ(c))
3571 rssi_off = sc->rssi_off_5ghz[rxpath];
3573 if (chan <= 64)
3574 lna_gain = sc->lna_gain[1];
3575 else if (chan <= 128)
3576 lna_gain = sc->lna_gain[2];
3577 else
3578 lna_gain = sc->lna_gain[3];
3580 else
3582 rssi_off = sc->rssi_off_2ghz[rxpath] - sc->lna_gain[0];
3583 lna_gain = sc->lna_gain[0];
3586 return (-12 - rssi_off - lna_gain - rssi);
3590 * rt2860_rate2mcs
3592 static uint8_t rt2860_rate2mcs(uint8_t rate)
3594 switch (rate)
3596 /* CCK rates */
3597 case 2: return 0;
3598 case 4: return 1;
3599 case 11: return 2;
3600 case 22: return 3;
3602 /* OFDM rates */
3603 case 12: return 0;
3604 case 18: return 1;
3605 case 24: return 2;
3606 case 36: return 3;
3607 case 48: return 4;
3608 case 72: return 5;
3609 case 96: return 6;
3610 case 108: return 7;
3613 return 0;
3617 * rt2860_tx_mgmt
3619 static int rt2860_tx_mgmt(struct rt2860_softc *sc,
3620 struct mbuf *m, struct ieee80211_node *ni, int qid)
3622 struct ifnet *ifp;
3623 struct ieee80211com *ic;
3624 struct ieee80211vap *vap;
3625 const struct ieee80211_txparam *tp;
3626 struct rt2860_softc_node *rni;
3627 struct rt2860_softc_tx_ring *ring;
3628 struct rt2860_softc_tx_data *data;
3629 struct rt2860_txdesc *desc;
3630 struct rt2860_txwi *txwi;
3631 struct ieee80211_frame *wh;
3632 struct rt2860_softc_tx_radiotap_header *tap;
3633 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3634 u_int hdrsize, hdrspace;
3635 uint8_t rate, stbc, shortgi, mcs, pid, qsel;
3636 uint16_t len, dmalen, mpdu_len, dur;
3637 int error, ndmasegs, ndescs, i, j;
3639 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3640 ("%s: Tx MGMT: invalid qid=%d\n",
3641 device_get_nameunit(sc->dev), qid));
3643 ifp = sc->ifp;
3644 ic = ifp->if_l2com;
3645 vap = ni->ni_vap;
3646 rni = (struct rt2860_softc_node *) ni;
3647 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
3649 ring = &sc->tx_ring[qid];
3650 desc = &ring->desc[ring->desc_cur];
3651 data = &ring->data[ring->data_cur];
3652 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3654 wh = mtod(m, struct ieee80211_frame *);
3656 rate = tp->mgmtrate & IEEE80211_RATE_VAL;
3658 /* fill Tx wireless info */
3660 if (ni->ni_flags & IEEE80211_NODE_HT)
3661 mcs = rate;
3662 else
3663 mcs = rt2860_rate2mcs(rate);
3665 pid = mcs;
3667 /* calculate MPDU length without padding */
3669 hdrsize = ieee80211_hdrsize(wh);
3670 hdrspace = ieee80211_hdrspace(ic, wh);
3671 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
3673 memset(txwi, 0, sizeof(struct rt2860_txwi));
3675 /* management frames do not need encryption */
3677 txwi->wcid = 0xff;
3679 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
3680 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
3681 RT2860_TXWI_MPDU_LEN_SHIFT);
3683 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
3685 shortgi = (vap->iv_flags_ht & (IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40)) &&
3686 (ni->ni_flags & IEEE80211_NODE_HT);
3688 txwi->phymode_ifs_stbc_shortgi |=
3689 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
3690 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
3692 if (ni->ni_flags & IEEE80211_NODE_HT)
3694 txwi->phymode_ifs_stbc_shortgi |=
3695 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
3697 else
3699 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
3701 txwi->phymode_ifs_stbc_shortgi |=
3702 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
3704 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3705 mcs |= RT2860_TXWI_MCS_SHOTPRE;
3707 else
3709 txwi->phymode_ifs_stbc_shortgi |=
3710 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
3714 txwi->bw_mcs = (RT2860_TXWI_BW_20 << RT2860_TXWI_BW_SHIFT) |
3715 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
3717 txwi->txop = (RT2860_TXWI_TXOP_BACKOFF << RT2860_TXWI_TXOP_SHIFT);
3719 /* skip ACKs for multicast frames and probe responses */
3721 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3722 ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) !=
3723 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP)))
3725 txwi->bawin_size_xflags |=
3726 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
3728 if (ni->ni_flags & IEEE80211_NODE_HT)
3730 /* preamble + plcp + signal extension */
3732 dur = 16 + 4 + 6;
3734 else
3736 dur = ieee80211_ack_duration(ic->ic_rt, rate,
3737 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3740 *(uint16_t *) wh->i_dur = htole16(dur);
3743 /* ask MAC to insert timestamp into probe responses */
3745 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3746 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3747 txwi->mpdu_density_flags |=
3748 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
3750 if (ieee80211_radiotap_active_vap(vap))
3752 tap = &sc->txtap;
3754 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3755 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3756 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3757 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3758 tap->chan_maxpow = 0;
3760 if (ni->ni_flags & IEEE80211_NODE_HT)
3761 tap->rate = mcs | IEEE80211_RATE_MCS;
3762 else
3763 tap->rate = rate;
3765 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
3766 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3768 if (shortgi)
3769 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3771 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3772 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
3774 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3776 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3778 ieee80211_radiotap_tx(vap, m);
3780 wh->i_fc[1] |= IEEE80211_FC1_WEP;
3782 else
3784 ieee80211_radiotap_tx(vap, m);
3788 /* copy and trim 802.11 header */
3790 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
3791 m_adj(m, hdrspace);
3793 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3794 dma_seg, &ndmasegs, 0);
3795 if (error != 0)
3797 /* too many fragments, linearize */
3799 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3800 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
3801 device_get_nameunit(sc->dev));
3803 m = m_defrag(m, M_DONTWAIT);
3804 if (m == NULL)
3805 return ENOMEM;
3807 sc->tx_defrag_packets++;
3809 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
3810 dma_seg, &ndmasegs, 0);
3811 if (error != 0)
3813 printf("%s: could not load mbuf DMA map: error=%d\n",
3814 device_get_nameunit(sc->dev), error);
3815 m_freem(m);
3816 return error;
3820 /* determine how many Tx descs are required */
3822 ndescs = 1 + ndmasegs / 2;
3823 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
3825 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3826 "%s: there are not enough Tx descs\n",
3827 device_get_nameunit(sc->dev));
3829 sc->no_tx_desc_avail++;
3831 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
3832 m_freem(m);
3833 return EFBIG;
3836 data->m = m;
3837 data->ni = ni;
3839 /* set up Tx descs */
3841 /* first segment is Tx wireless info and 802.11 header */
3843 len = sizeof(struct rt2860_txwi) + hdrsize;
3845 /* align end on a 4-bytes boundary */
3847 dmalen = (len + 3) & ~ 3;
3849 memset((caddr_t) txwi + len, 0, dmalen - len);
3851 qsel = RT2860_TXDESC_QSEL_EDCA;
3853 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3854 desc->sdl0 = htole16(dmalen);
3855 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3857 /* set up payload segments */
3859 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
3861 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3862 desc->sdl1 = htole16(dma_seg[j].ds_len);
3864 ring->desc_queued++;
3865 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3867 j++;
3869 desc = &ring->desc[ring->desc_cur];
3871 desc->sdp0 = htole32(dma_seg[j].ds_addr);
3872 desc->sdl0 = htole16(dma_seg[j].ds_len);
3873 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
3875 j++;
3878 /* finalize last payload segment */
3880 if (i > 0)
3882 desc->sdp1 = htole32(dma_seg[j].ds_addr);
3883 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
3885 else
3887 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
3888 desc->sdl1 = 0;
3891 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
3892 "%s: sending MGMT frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
3893 "stbc=%d, shortgi=%d, mcs=%d, DMA len=%d, ndmasegs=%d, DMA ds_len=%d/%d/%d/%d/%d\n",
3894 device_get_nameunit(sc->dev),
3895 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
3896 stbc, shortgi, mcs, dmalen, ndmasegs,
3897 (int) dma_seg[0].ds_len, (int) dma_seg[1].ds_len, (int) dma_seg[2].ds_len, (int) dma_seg[3].ds_len, (int) dma_seg[4].ds_len);
3899 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
3900 BUS_DMASYNC_PREWRITE);
3901 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
3902 BUS_DMASYNC_PREWRITE);
3903 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
3904 BUS_DMASYNC_PREWRITE);
3906 ring->desc_queued++;
3907 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
3909 ring->data_queued++;
3910 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
3912 /* kick Tx */
3914 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
3916 return 0;
3920 * rt2860_tx_data
3922 static int rt2860_tx_data(struct rt2860_softc *sc,
3923 struct mbuf *m, struct ieee80211_node *ni, int qid)
3925 struct ifnet *ifp;
3926 struct ieee80211com *ic;
3927 struct ieee80211vap *vap;
3928 const struct ieee80211_txparam *tp;
3929 struct rt2860_softc_node *rni;
3930 struct rt2860_softc_tx_ring *ring;
3931 struct rt2860_softc_tx_data *data;
3932 struct rt2860_txdesc *desc;
3933 struct rt2860_txwi *txwi;
3934 struct ieee80211_frame *wh;
3935 struct rt2860_softc_tx_radiotap_header *tap;
3936 bus_dma_segment_t dma_seg[RT2860_SOFTC_MAX_SCATTER];
3937 u_int hdrsize, hdrspace;
3938 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, qsel;
3939 uint16_t qos, len, dmalen, mpdu_len, dur;
3940 int error, hasqos, ndmasegs, ndescs, i, j;
3942 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
3943 ("%s: Tx data: invalid qid=%d\n",
3944 device_get_nameunit(sc->dev), qid));
3946 ifp = sc->ifp;
3947 ic = ifp->if_l2com;
3948 vap = ni->ni_vap;
3949 rni = (struct rt2860_softc_node *) ni;
3950 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
3952 ring = &sc->tx_ring[qid];
3953 desc = &ring->desc[ring->desc_cur];
3954 data = &ring->data[ring->data_cur];
3955 txwi = (struct rt2860_txwi *) (ring->seg0 + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
3957 wh = mtod(m, struct ieee80211_frame *);
3959 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3961 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
3962 if (hasqos)
3964 if (IEEE80211_HAS_ADDR4(wh))
3965 qos = le16toh(*(const uint16_t *)
3966 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
3967 else
3968 qos = le16toh(*(const uint16_t *)
3969 (((struct ieee80211_qosframe *) wh)->i_qos));
3971 else
3973 qos = 0;
3976 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
3977 rate = tp->mcastrate;
3978 else if (m->m_flags & M_EAPOL)
3979 rate = tp->mgmtrate;
3980 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3981 rate = tp->ucastrate;
3982 else
3983 rate = ni->ni_txrate;
3985 rate &= IEEE80211_RATE_VAL;
3987 /* fill Tx wireless info */
3989 if (ni->ni_flags & IEEE80211_NODE_HT)
3990 mcs = rate;
3991 else
3992 mcs = rt2860_rate2mcs(rate);
3994 pid = mcs;
3996 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
3997 RT2860_AID2WCID(ni->ni_associd) : 0xff;
3999 /* calculate MPDU length without padding */
4001 hdrsize = ieee80211_hdrsize(wh);
4002 hdrspace = ieee80211_hdrspace(ic, wh);
4003 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
4005 memset(txwi, 0, sizeof(struct rt2860_txwi));
4007 txwi->wcid = wcid;
4009 txwi->pid_mpdu_len = ((htole16(pid) & RT2860_TXWI_PID_MASK) <<
4010 RT2860_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2860_TXWI_MPDU_LEN_MASK) <<
4011 RT2860_TXWI_MPDU_LEN_SHIFT);
4013 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
4015 shortgi = (vap->iv_flags_ht & (IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40)) &&
4016 (ni->ni_flags & IEEE80211_NODE_HT);
4018 txwi->phymode_ifs_stbc_shortgi |=
4019 ((stbc & RT2860_TXWI_STBC_MASK) << RT2860_TXWI_STBC_SHIFT) |
4020 ((shortgi & RT2860_TXWI_SHORTGI_MASK) << RT2860_TXWI_SHORTGI_SHIFT);
4022 if (ni->ni_flags & IEEE80211_NODE_HT)
4024 txwi->phymode_ifs_stbc_shortgi |=
4025 (RT2860_TXWI_PHYMODE_HT_MIXED << RT2860_TXWI_PHYMODE_SHIFT);
4027 else
4029 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
4031 txwi->phymode_ifs_stbc_shortgi |=
4032 (RT2860_TXWI_PHYMODE_CCK << RT2860_TXWI_PHYMODE_SHIFT);
4034 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4035 mcs |= RT2860_TXWI_MCS_SHOTPRE;
4037 else
4039 txwi->phymode_ifs_stbc_shortgi |=
4040 (RT2860_TXWI_PHYMODE_OFDM << RT2860_TXWI_PHYMODE_SHIFT);
4044 if ((ni->ni_flags & IEEE80211_NODE_HT) &&
4045 (ni->ni_htcap & IEEE80211_HTCAP_CHWIDTH40))
4046 bw = RT2860_TXWI_BW_40;
4047 else
4048 bw = RT2860_TXWI_BW_20;
4050 txwi->bw_mcs = ((bw & RT2860_TXWI_BW_MASK) << RT2860_TXWI_BW_SHIFT) |
4051 ((mcs & RT2860_TXWI_MCS_MASK) << RT2860_TXWI_MCS_SHIFT);
4053 txwi->txop = (RT2860_TXWI_TXOP_HT << RT2860_TXWI_TXOP_SHIFT);
4055 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
4056 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
4058 txwi->bawin_size_xflags |=
4059 (RT2860_TXWI_XFLAGS_ACK << RT2860_TXWI_XFLAGS_SHIFT);
4061 if (ni->ni_flags & IEEE80211_NODE_HT)
4063 /* preamble + plcp + signal extension */
4065 dur = 16 + 4 + 6;
4067 else
4069 dur = ieee80211_ack_duration(ic->ic_rt, rate,
4070 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
4073 *(uint16_t *) wh->i_dur = htole16(dur);
4076 /* ask MAC to insert timestamp into probe responses */
4078 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
4079 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
4080 txwi->mpdu_density_flags |=
4081 (RT2860_TXWI_FLAGS_TS << RT2860_TXWI_FLAGS_SHIFT);
4083 if (ieee80211_radiotap_active_vap(vap))
4085 tap = &sc->txtap;
4087 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4088 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4089 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4090 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4091 tap->chan_maxpow = 0;
4093 if (ni->ni_flags & IEEE80211_NODE_HT)
4094 tap->rate = mcs | IEEE80211_RATE_MCS;
4095 else
4096 tap->rate = rate;
4098 if (mcs & RT2860_TXWI_MCS_SHOTPRE)
4099 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4101 if (shortgi)
4102 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4104 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4105 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4107 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4109 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4111 ieee80211_radiotap_tx(vap, m);
4113 wh->i_fc[1] |= IEEE80211_FC1_WEP;
4115 else
4117 ieee80211_radiotap_tx(vap, m);
4121 /* copy and trim 802.11 header */
4123 m_copydata(m, 0, hdrsize, (caddr_t) (txwi + 1));
4124 m_adj(m, hdrspace);
4126 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4127 dma_seg, &ndmasegs, 0);
4128 if (error != 0)
4130 /* too many fragments, linearize */
4132 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4133 "%s: could not load mbuf DMA map, trying to linearize mbuf\n",
4134 device_get_nameunit(sc->dev));
4136 m = m_defrag(m, M_DONTWAIT);
4137 if (m == NULL)
4138 return ENOMEM;
4140 sc->tx_defrag_packets++;
4142 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map, m,
4143 dma_seg, &ndmasegs, 0);
4144 if (error != 0)
4146 printf("%s: could not load mbuf DMA map: error=%d\n",
4147 device_get_nameunit(sc->dev), error);
4148 m_freem(m);
4149 return error;
4153 /* determine how many Tx descs are required */
4155 ndescs = 1 + ndmasegs / 2;
4156 if ((ring->desc_queued + ndescs) > (RT2860_SOFTC_TX_RING_DESC_COUNT - 2))
4158 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4159 "%s: there are not enough Tx descs\n",
4160 device_get_nameunit(sc->dev));
4162 sc->no_tx_desc_avail++;
4164 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4165 m_freem(m);
4166 return EFBIG;
4169 data->m = m;
4170 data->ni = ni;
4172 /* set up Tx descs */
4174 /* first segment is Tx wireless info and 802.11 header */
4176 len = sizeof(struct rt2860_txwi) + hdrsize;
4178 /* align end on a 4-bytes boundary */
4180 dmalen = (len + 3) & ~ 3;
4182 memset((caddr_t) txwi + len, 0, dmalen - len);
4184 qsel = RT2860_TXDESC_QSEL_EDCA;
4186 desc->sdp0 = htole32(ring->seg0_phys_addr + ring->data_cur * RT2860_TX_DATA_SEG0_SIZE);
4187 desc->sdl0 = htole16(dmalen);
4188 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4190 /* set up payload segments */
4192 for (i = ndmasegs, j = 0; i >= 2; i -= 2)
4194 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4195 desc->sdl1 = htole16(dma_seg[j].ds_len);
4197 ring->desc_queued++;
4198 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4200 j++;
4202 desc = &ring->desc[ring->desc_cur];
4204 desc->sdp0 = htole32(dma_seg[j].ds_addr);
4205 desc->sdl0 = htole16(dma_seg[j].ds_len);
4206 desc->qsel_flags = (qsel << RT2860_TXDESC_QSEL_SHIFT);
4208 j++;
4211 /* finalize last payload segment */
4213 if (i > 0)
4215 desc->sdp1 = htole32(dma_seg[j].ds_addr);
4216 desc->sdl1 = htole16(dma_seg[j].ds_len | RT2860_TXDESC_SDL1_LASTSEG);
4218 else
4220 desc->sdl0 |= htole16(RT2860_TXDESC_SDL0_LASTSEG);
4221 desc->sdl1 = 0;
4224 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4225 "%s: sending data: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4226 "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",
4227 device_get_nameunit(sc->dev),
4228 qid, hdrsize, hdrspace, m->m_pkthdr.len + hdrsize,
4229 bw, stbc, shortgi, mcs, wcid, dmalen, ndmasegs,
4230 (int) dma_seg[0].ds_len, (int) dma_seg[1].ds_len, (int) dma_seg[2].ds_len, (int) dma_seg[3].ds_len, (int) dma_seg[4].ds_len);
4232 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
4233 BUS_DMASYNC_PREWRITE);
4234 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4235 BUS_DMASYNC_PREWRITE);
4236 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4237 BUS_DMASYNC_PREWRITE);
4239 ring->desc_queued++;
4240 ring->desc_cur = (ring->desc_cur + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
4242 ring->data_queued++;
4243 ring->data_cur = (ring->data_cur + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
4245 /* kick Tx */
4247 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(qid), ring->desc_cur);
4249 return 0;
4253 * rt2860_tx_raw
4255 static int rt2860_tx_raw(struct rt2860_softc *sc,
4256 struct mbuf *m, struct ieee80211_node *ni,
4257 const struct ieee80211_bpf_params *params)
4259 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4260 "%s: Tx raw\n",
4261 device_get_nameunit(sc->dev));
4263 return 0;
4267 * rt2860_intr
4269 static void rt2860_intr(void *arg)
4271 struct rt2860_softc *sc;
4272 struct ifnet *ifp;
4273 uint32_t status;
4275 sc = arg;
4276 ifp = sc->ifp;
4278 /* acknowledge interrupts */
4280 status = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_INT_STATUS);
4281 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_STATUS, status);
4283 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4284 "%s: interrupt: status = 0x%08x\n",
4285 device_get_nameunit(sc->dev), status);
4287 if (status == 0xffffffff || /* device likely went away */
4288 status == 0) /* not for us */
4289 return;
4291 sc->interrupts++;
4293 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4294 return;
4296 if (status & RT2860_REG_INT_TX_COHERENT)
4297 rt2860_tx_coherent_intr(sc);
4299 if (status & RT2860_REG_INT_RX_COHERENT)
4300 rt2860_rx_coherent_intr(sc);
4302 if (status & RT2860_REG_INT_TXRX_COHERENT)
4303 rt2860_txrx_coherent_intr(sc);
4305 if (status & RT2860_REG_INT_FIFO_STA_FULL)
4306 rt2860_fifo_sta_full_intr(sc);
4308 if (status & RT2860_REG_INT_TX_MGMT_DONE)
4309 rt2860_tx_intr(sc, 5);
4311 if (status & RT2860_REG_INT_RX_DONE)
4312 rt2860_rx_intr(sc);
4314 if (status & RT2860_REG_INT_RX_DELAY_DONE)
4315 rt2860_rx_delay_intr(sc);
4317 if (status & RT2860_REG_INT_TX_HCCA_DONE)
4318 rt2860_tx_intr(sc, 4);
4320 if (status & RT2860_REG_INT_TX_AC3_DONE)
4321 rt2860_tx_intr(sc, 3);
4323 if (status & RT2860_REG_INT_TX_AC2_DONE)
4324 rt2860_tx_intr(sc, 2);
4326 if (status & RT2860_REG_INT_TX_AC1_DONE)
4327 rt2860_tx_intr(sc, 1);
4329 if (status & RT2860_REG_INT_TX_AC0_DONE)
4330 rt2860_tx_intr(sc, 0);
4332 if (status & RT2860_REG_INT_TX_DELAY_DONE)
4333 rt2860_tx_delay_intr(sc);
4335 if (status & RT2860_REG_INT_PRE_TBTT)
4336 rt2860_pre_tbtt_intr(sc);
4338 if (status & RT2860_REG_INT_TBTT)
4339 rt2860_tbtt_intr(sc);
4341 if (status & RT2860_REG_INT_MCU_CMD)
4342 rt2860_mcu_cmd_intr(sc);
4344 if (status & RT2860_REG_INT_AUTO_WAKEUP)
4345 rt2860_auto_wakeup_intr(sc);
4347 if (status & RT2860_REG_INT_GP_TIMER)
4348 rt2860_gp_timer_intr(sc);
4352 * rt2860_tx_coherent_intr
4354 static void rt2860_tx_coherent_intr(struct rt2860_softc *sc)
4356 uint32_t tmp;
4357 int i;
4359 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4360 "%s: Tx coherent interrupt\n",
4361 device_get_nameunit(sc->dev));
4363 sc->tx_coherent_interrupts++;
4365 /* restart DMA engine */
4367 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4369 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4370 RT2860_REG_RX_DMA_ENABLE |
4371 RT2860_REG_TX_DMA_ENABLE);
4373 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4375 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4377 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4378 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4380 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4382 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4383 sc->tx_ring[i].desc_phys_addr);
4384 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4385 RT2860_SOFTC_TX_RING_DESC_COUNT);
4386 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4389 /* init Rx ring */
4391 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4393 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4394 sc->rx_ring.desc_phys_addr);
4395 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4396 RT2860_SOFTC_RX_RING_DATA_COUNT);
4397 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4398 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4400 rt2860_txrx_enable(sc);
4404 * rt2860_rx_coherent_intr
4406 static void rt2860_rx_coherent_intr(struct rt2860_softc *sc)
4408 uint32_t tmp;
4409 int i;
4411 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4412 "%s: Rx coherent interrupt\n",
4413 device_get_nameunit(sc->dev));
4415 sc->rx_coherent_interrupts++;
4417 /* restart DMA engine */
4419 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4421 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4422 RT2860_REG_RX_DMA_ENABLE |
4423 RT2860_REG_TX_DMA_ENABLE);
4425 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4427 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4429 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4430 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4432 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4434 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4435 sc->tx_ring[i].desc_phys_addr);
4436 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4437 RT2860_SOFTC_TX_RING_DESC_COUNT);
4438 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4441 /* init Rx ring */
4443 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4445 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4446 sc->rx_ring.desc_phys_addr);
4447 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4448 RT2860_SOFTC_RX_RING_DATA_COUNT);
4449 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4450 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4452 rt2860_txrx_enable(sc);
4456 * rt2860_txrx_coherent_intr
4458 static void rt2860_txrx_coherent_intr(struct rt2860_softc *sc)
4460 uint32_t tmp;
4461 int i;
4463 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4464 "%s: Tx/Rx coherent interrupt\n",
4465 device_get_nameunit(sc->dev));
4467 sc->txrx_coherent_interrupts++;
4469 /* restart DMA engine */
4471 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
4473 tmp &= ~(RT2860_REG_TX_WB_DDONE |
4474 RT2860_REG_RX_DMA_ENABLE |
4475 RT2860_REG_TX_DMA_ENABLE);
4477 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
4479 /* init Tx rings (4 EDCAs + HCCA + MGMT) */
4481 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4482 rt2860_reset_tx_ring(sc, &sc->tx_ring[i]);
4484 for (i = 0; i < RT2860_SOFTC_TX_RING_COUNT; i++)
4486 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_BASE_PTR(i),
4487 sc->tx_ring[i].desc_phys_addr);
4488 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_MAX_CNT(i),
4489 RT2860_SOFTC_TX_RING_DESC_COUNT);
4490 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_TX_CTX_IDX(i), 0);
4493 /* init Rx ring */
4495 rt2860_reset_rx_ring(sc, &sc->rx_ring);
4497 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_BASE_PTR,
4498 sc->rx_ring.desc_phys_addr);
4499 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_MAX_CNT,
4500 RT2860_SOFTC_RX_RING_DATA_COUNT);
4501 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
4502 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
4504 rt2860_txrx_enable(sc);
4508 * rt2860_fifo_sta_full_intr
4510 static void rt2860_fifo_sta_full_intr(struct rt2860_softc *sc)
4512 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4513 "%s: FIFO statistic full interrupt\n",
4514 device_get_nameunit(sc->dev));
4516 sc->fifo_sta_full_interrupts++;
4518 RT2860_SOFTC_LOCK(sc);
4520 if (!(sc->intr_disable_mask & RT2860_REG_INT_FIFO_STA_FULL))
4522 rt2860_intr_disable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4524 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4527 sc->intr_pending_mask |= RT2860_REG_INT_FIFO_STA_FULL;
4529 RT2860_SOFTC_UNLOCK(sc);
4533 * rt2860_rx_intr
4535 static void rt2860_rx_intr(struct rt2860_softc *sc)
4537 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4538 "%s: Rx interrupt\n",
4539 device_get_nameunit(sc->dev));
4541 sc->rx_interrupts++;
4543 RT2860_SOFTC_LOCK(sc);
4545 if (!(sc->intr_disable_mask & RT2860_REG_INT_RX_DONE))
4547 rt2860_intr_disable(sc, RT2860_REG_INT_RX_DONE);
4549 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4552 sc->intr_pending_mask |= RT2860_REG_INT_RX_DONE;
4554 RT2860_SOFTC_UNLOCK(sc);
4558 * rt2860_rx_delay_intr
4560 static void rt2860_rx_delay_intr(struct rt2860_softc *sc)
4562 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4563 "%s: Rx delay interrupt\n",
4564 device_get_nameunit(sc->dev));
4566 sc->rx_delay_interrupts++;
4570 * rt2860_tx_intr
4572 static void rt2860_tx_intr(struct rt2860_softc *sc, int qid)
4574 KASSERT(qid >= 0 && qid < RT2860_SOFTC_TX_RING_COUNT,
4575 ("%s: Tx interrupt: invalid qid=%d\n",
4576 device_get_nameunit(sc->dev), qid));
4578 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4579 "%s: Tx interrupt: qid=%d\n",
4580 device_get_nameunit(sc->dev), qid);
4582 sc->tx_interrupts[qid]++;
4584 RT2860_SOFTC_LOCK(sc);
4586 if (!(sc->intr_disable_mask & (RT2860_REG_INT_TX_AC0_DONE << qid)))
4588 rt2860_intr_disable(sc, (RT2860_REG_INT_TX_AC0_DONE << qid));
4590 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4593 sc->intr_pending_mask |= (RT2860_REG_INT_TX_AC0_DONE << qid);
4595 RT2860_SOFTC_UNLOCK(sc);
4599 * rt2860_tx_delay_intr
4601 static void rt2860_tx_delay_intr(struct rt2860_softc *sc)
4603 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4604 "%s: Tx delay interrupt\n",
4605 device_get_nameunit(sc->dev));
4607 sc->tx_delay_interrupts++;
4611 * rt2860_pre_tbtt_intr
4613 static void rt2860_pre_tbtt_intr(struct rt2860_softc *sc)
4615 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4616 "%s: Pre-TBTT interrupt\n",
4617 device_get_nameunit(sc->dev));
4619 sc->pre_tbtt_interrupts++;
4623 * rt2860_tbtt_intr
4625 static void rt2860_tbtt_intr(struct rt2860_softc *sc)
4627 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4628 "%s: TBTT interrupt\n",
4629 device_get_nameunit(sc->dev));
4631 sc->tbtt_interrupts++;
4635 * rt2860_mcu_cmd_intr
4637 static void rt2860_mcu_cmd_intr(struct rt2860_softc *sc)
4639 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4640 "%s: MCU command interrupt\n",
4641 device_get_nameunit(sc->dev));
4643 sc->mcu_cmd_interrupts++;
4647 * rt2860_auto_wakeup_intr
4649 static void rt2860_auto_wakeup_intr(struct rt2860_softc *sc)
4651 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4652 "%s: auto wakeup interrupt\n",
4653 device_get_nameunit(sc->dev));
4655 sc->auto_wakeup_interrupts++;
4659 * rt2860_gp_timer_intr
4661 static void rt2860_gp_timer_intr(struct rt2860_softc *sc)
4663 RT2860_DPRINTF(sc, RT2860_DEBUG_INTR,
4664 "%s: GP timer interrupt\n",
4665 device_get_nameunit(sc->dev));
4667 sc->gp_timer_interrupts++;
4671 * rt2860_rx_done_task
4673 static void rt2860_rx_done_task(void *context, int pending)
4675 struct rt2860_softc *sc;
4676 struct ifnet *ifp;
4677 int again;
4679 sc = context;
4680 ifp = sc->ifp;
4682 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4683 "%s: Rx done task\n",
4684 device_get_nameunit(sc->dev));
4686 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4687 return;
4689 sc->intr_pending_mask &= ~RT2860_REG_INT_RX_DONE;
4691 again = rt2860_rx_eof(sc, sc->rx_process_limit);
4693 RT2860_SOFTC_LOCK(sc);
4695 if ((sc->intr_pending_mask & RT2860_REG_INT_RX_DONE) || again)
4697 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4698 "%s: Rx done task: scheduling again\n",
4699 device_get_nameunit(sc->dev));
4701 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4703 else
4705 rt2860_intr_enable(sc, RT2860_REG_INT_RX_DONE);
4708 RT2860_SOFTC_UNLOCK(sc);
4712 * rt2860_tx_done_task
4714 static void rt2860_tx_done_task(void *context, int pending)
4716 struct rt2860_softc *sc;
4717 struct ifnet *ifp;
4718 uint32_t intr_mask;
4719 int i;
4721 sc = context;
4722 ifp = sc->ifp;
4724 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4725 "%s: Tx done task\n",
4726 device_get_nameunit(sc->dev));
4728 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4729 return;
4731 intr_mask = (RT2860_REG_INT_TX_MGMT_DONE |
4732 RT2860_REG_INT_TX_HCCA_DONE |
4733 RT2860_REG_INT_TX_AC3_DONE |
4734 RT2860_REG_INT_TX_AC2_DONE |
4735 RT2860_REG_INT_TX_AC1_DONE |
4736 RT2860_REG_INT_TX_AC0_DONE);
4738 sc->intr_pending_mask &= ~intr_mask;
4740 for (i = RT2860_SOFTC_TX_RING_COUNT - 1; i >= 0; i--)
4741 rt2860_tx_eof(sc, &sc->tx_ring[i]);
4743 sc->tx_timer = 0;
4745 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4747 RT2860_SOFTC_LOCK(sc);
4749 rt2860_intr_enable(sc, ~sc->intr_pending_mask &
4750 (sc->intr_disable_mask & intr_mask));
4752 if (sc->intr_pending_mask & intr_mask)
4754 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
4755 "%s: Tx done task: scheduling again\n",
4756 device_get_nameunit(sc->dev));
4758 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4761 RT2860_SOFTC_UNLOCK(sc);
4765 * rt2860_fifo_sta_full_task
4767 static void rt2860_fifo_sta_full_task(void *context, int pending)
4769 struct rt2860_softc *sc;
4770 struct ifnet *ifp;
4772 sc = context;
4773 ifp = sc->ifp;
4775 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4776 "%s: FIFO statistic full task\n",
4777 device_get_nameunit(sc->dev));
4779 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4780 return;
4782 sc->intr_pending_mask &= ~RT2860_REG_INT_FIFO_STA_FULL;
4784 rt2860_drain_fifo_stats(sc);
4786 RT2860_SOFTC_LOCK(sc);
4788 if (sc->intr_pending_mask & RT2860_REG_INT_FIFO_STA_FULL)
4790 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
4791 "%s: FIFO statistic full task: scheduling again\n",
4792 device_get_nameunit(sc->dev));
4794 taskqueue_enqueue(sc->taskqueue, &sc->fifo_sta_full_task);
4796 else
4798 rt2860_intr_enable(sc, RT2860_REG_INT_FIFO_STA_FULL);
4801 RT2860_SOFTC_UNLOCK(sc);
4805 * rt2860_periodic_task
4807 static void rt2860_periodic_task(void *context, int pending)
4809 struct rt2860_softc *sc;
4810 struct ifnet *ifp;
4811 struct ieee80211com *ic;
4812 struct ieee80211vap *vap;
4814 sc = context;
4815 ifp = sc->ifp;
4816 ic = ifp->if_l2com;
4818 RT2860_DPRINTF(sc, RT2860_DEBUG_PERIODIC,
4819 "%s: periodic task: round=%lu\n",
4820 device_get_nameunit(sc->dev), sc->periodic_round);
4822 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4823 return;
4825 RT2860_SOFTC_LOCK(sc);
4827 sc->periodic_round++;
4829 rt2860_update_stats(sc);
4831 if ((sc->periodic_round % 10) == 0)
4833 rt2860_bbp_tuning(sc);
4835 rt2860_update_raw_counters(sc);
4837 rt2860_watchdog(sc);
4839 vap = TAILQ_FIRST(&ic->ic_vaps);
4840 if (vap != NULL)
4842 if (vap->iv_opmode == IEEE80211_M_STA)
4843 rt2860_amrr_update_iter_func(vap, vap->iv_bss);
4844 else
4845 ieee80211_iterate_nodes(&ic->ic_sta, rt2860_amrr_update_iter_func, vap);
4849 RT2860_SOFTC_UNLOCK(sc);
4851 callout_reset(&sc->periodic_ch, hz / 10, rt2860_periodic, sc);
4855 * rt2860_rx_eof
4857 static int rt2860_rx_eof(struct rt2860_softc *sc, int limit)
4859 struct ifnet *ifp;
4860 struct ieee80211com *ic;
4861 struct ieee80211_frame *wh;
4862 struct ieee80211_node *ni;
4863 struct rt2860_softc_rx_radiotap_header *tap;
4864 struct rt2860_softc_rx_ring *ring;
4865 struct rt2860_rxdesc *desc;
4866 struct rt2860_softc_rx_data *data;
4867 struct rt2860_rxwi *rxwi;
4868 struct mbuf *m, *mnew;
4869 bus_dma_segment_t segs[1];
4870 uint32_t index, desc_flags;
4871 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
4872 int8_t rssi_dbm;
4873 int error, nsegs, len, ampdu, amsdu, rssi_dbm_rel, nframes;
4875 ifp = sc->ifp;
4876 ic = ifp->if_l2com;
4877 ring = &sc->rx_ring;
4879 nframes = 0;
4881 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
4882 BUS_DMASYNC_POSTREAD);
4884 while (limit != 0)
4886 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_RX_DRX_IDX);
4887 if (ring->cur == index)
4888 break;
4890 desc = &ring->desc[ring->cur];
4891 desc_flags = le32toh(desc->flags);
4892 data = &ring->data[ring->cur];
4894 if (!(desc->sdl0 & htole16(RT2860_RXDESC_SDL0_DDONE)))
4895 break;
4897 nframes++;
4899 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4900 if (mnew == NULL)
4902 sc->rx_mbuf_alloc_errors++;
4903 ifp->if_ierrors++;
4904 goto skip;
4907 mnew->m_len = mnew->m_pkthdr.len = MJUMPAGESIZE;
4909 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
4910 BUS_DMASYNC_POSTREAD);
4911 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
4913 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4914 mnew, segs, &nsegs, BUS_DMA_NOWAIT);
4915 if (error != 0)
4917 m_freem(mnew);
4919 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
4920 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
4921 if (error != 0)
4922 panic("%s: could not load old Rx mbuf: error=%d\n",
4923 device_get_name(sc->dev), error);
4925 sc->rx_mbuf_dmamap_errors++;
4926 ifp->if_ierrors++;
4928 goto skip;
4931 KASSERT(nsegs == 1, ("%s: too many DMA segments",
4932 device_get_nameunit(sc->dev)));
4934 m = data->m;
4936 data->m = mnew;
4937 desc->sdp0 = htole32(segs[0].ds_addr);
4939 /* get Rx wireless info */
4941 rxwi = mtod(m, struct rt2860_rxwi *);
4942 len = (le16toh(rxwi->tid_size) >> RT2860_RXWI_SIZE_SHIFT) &
4943 RT2860_RXWI_SIZE_MASK;
4945 /* check for L2 padding between IEEE 802.11 frame header and body */
4947 if (desc_flags & RT2860_RXDESC_FLAGS_L2PAD)
4949 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4950 "%s: L2 padding: len=%d\n",
4951 device_get_nameunit(sc->dev), len);
4953 len += 2;
4956 m->m_pkthdr.rcvif = ifp;
4957 m->m_data = (caddr_t) (rxwi + 1);
4958 m->m_pkthdr.len = m->m_len = len;
4960 /* check for crc errors */
4962 if (desc_flags & RT2860_RXDESC_FLAGS_CRC_ERR)
4964 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4965 "%s: rxdesc: crc error\n",
4966 device_get_nameunit(sc->dev));
4968 ifp->if_ierrors++;
4970 if (!(ifp->if_flags & IFF_PROMISC))
4972 m_freem(m);
4973 goto skip;
4977 wh = (struct ieee80211_frame *) (rxwi + 1);
4979 /* check for cipher errors */
4981 if (desc_flags & RT2860_RXDESC_FLAGS_DECRYPTED)
4983 cipher_err = ((desc_flags >> RT2860_RXDESC_FLAGS_CIPHER_ERR_SHIFT) &
4984 RT2860_RXDESC_FLAGS_CIPHER_ERR_MASK);
4985 if (cipher_err == RT2860_RXDESC_FLAGS_CIPHER_ERR_NONE)
4987 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4988 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4990 m->m_flags |= M_WEP;
4992 else
4994 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
4995 "%s: rxdesc: cipher error=0x%02x\n",
4996 device_get_nameunit(sc->dev), cipher_err);
4998 ifp->if_ierrors++;
5000 if (!(ifp->if_flags & IFF_PROMISC))
5002 m_free(m);
5003 goto skip;
5007 else
5009 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
5011 ifp->if_ierrors++;
5013 if (!(ifp->if_flags & IFF_PROMISC))
5015 m_free(m);
5016 goto skip;
5021 /* check for A-MPDU */
5023 if (desc_flags & RT2860_RXDESC_FLAGS_AMPDU)
5025 m->m_flags |= M_AMPDU;
5026 ampdu = 1;
5028 else
5030 ampdu = 0;
5033 ant = rt2860_maxrssi_rxpath(sc, rxwi);
5034 rssi = rxwi->rssi[ant];
5035 rssi_dbm = rt2860_rssi2dbm(sc, rssi, ant);
5036 phymode = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_PHYMODE_SHIFT) &
5037 RT2860_RXWI_PHYMODE_MASK);
5038 bw = ((rxwi->bw_mcs >> RT2860_RXWI_BW_SHIFT) & RT2860_RXWI_BW_MASK);
5039 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2860_RXWI_SHORTGI_SHIFT) &
5040 RT2860_RXWI_SHORTGI_MASK);
5041 mcs = ((rxwi->bw_mcs >> RT2860_RXWI_MCS_SHIFT) & RT2860_RXWI_MCS_MASK);
5042 amsdu = (desc_flags & RT2860_RXDESC_FLAGS_AMSDU) ? 1 : 0;
5044 if (ieee80211_radiotap_active(ic))
5046 tap = &sc->rxtap;
5048 tap->flags = (desc_flags & RT2860_RXDESC_FLAGS_L2PAD) ? IEEE80211_RADIOTAP_F_DATAPAD : 0;
5049 tap->dbm_antsignal = rssi_dbm;
5050 tap->dbm_antnoise = RT2860_NOISE_FLOOR;
5051 tap->antenna = ant;
5052 tap->antsignal = rssi;
5053 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
5054 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
5055 tap->chan_ieee = ic->ic_curchan->ic_ieee;
5056 tap->chan_maxpow = 0;
5058 if (phymode == RT2860_TXWI_PHYMODE_HT_MIXED || phymode == RT2860_TXWI_PHYMODE_HT_GF)
5059 tap->rate = mcs | IEEE80211_RATE_MCS;
5060 else
5061 tap->rate = rt2860_rxrate(rxwi);
5063 if (rxwi->bw_mcs & RT2860_RXWI_MCS_SHOTPRE)
5064 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5066 if (shortgi)
5067 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
5071 * net80211 assumes that RSSI data are in the range [-127..127] and
5072 * in .5 dBm units relative to the current noise floor
5075 rssi_dbm_rel = (rssi_dbm - RT2860_NOISE_FLOOR) * 2;
5076 if (rssi_dbm_rel > 127)
5077 rssi_dbm_rel = 127;
5079 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5080 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
5081 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d, "
5082 "rssi_dbm=%d, rssi_dbm_rel=%d\n",
5083 device_get_nameunit(sc->dev),
5084 len, phymode, bw, shortgi, mcs,
5085 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
5086 rxwi->snr[0], rxwi->snr[1],
5087 rxwi->wcid, ampdu, amsdu, rssi_dbm, rssi_dbm_rel);
5089 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
5090 if (ni != NULL)
5092 ieee80211_input(ni, m, rssi_dbm_rel, RT2860_NOISE_FLOOR);
5093 ieee80211_free_node(ni);
5095 else
5097 ieee80211_input_all(ic, m, rssi_dbm_rel, RT2860_NOISE_FLOOR);
5100 skip:
5102 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5104 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5105 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5107 ring->cur = (ring->cur + 1) % RT2860_SOFTC_RX_RING_DATA_COUNT;
5109 limit--;
5112 if (ring->cur == 0)
5113 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5114 RT2860_SOFTC_RX_RING_DATA_COUNT - 1);
5115 else
5116 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_RX_CALC_IDX,
5117 ring->cur - 1);
5119 RT2860_DPRINTF(sc, RT2860_DEBUG_RX,
5120 "%s: Rx eof: nframes=%d\n",
5121 device_get_nameunit(sc->dev), nframes);
5123 sc->rx_packets += nframes;
5125 return (limit == 0);
5129 * rt2860_tx_eof
5131 static void rt2860_tx_eof(struct rt2860_softc *sc,
5132 struct rt2860_softc_tx_ring *ring)
5134 struct ifnet *ifp;
5135 struct rt2860_txdesc *desc;
5136 struct rt2860_softc_tx_data *data;
5137 uint32_t index;
5138 int ndescs, nframes;
5140 ifp = sc->ifp;
5142 ndescs = 0;
5143 nframes = 0;
5145 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5146 BUS_DMASYNC_POSTREAD);
5148 for (;;)
5150 index = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_TX_DTX_IDX(ring->qid));
5151 if (ring->desc_next == index)
5152 break;
5154 ndescs++;
5156 rt2860_drain_fifo_stats(sc);
5158 desc = &ring->desc[ring->desc_next];
5160 if (desc->sdl0 & htole16(RT2860_TXDESC_SDL0_LASTSEG) ||
5161 desc->sdl1 & htole16(RT2860_TXDESC_SDL1_LASTSEG))
5163 nframes++;
5165 data = &ring->data[ring->data_next];
5167 if (data->m->m_flags & M_TXCB)
5168 ieee80211_process_callback(data->ni, data->m, 0);
5170 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5171 BUS_DMASYNC_POSTWRITE);
5172 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5174 m_freem(data->m);
5176 ieee80211_free_node(data->ni);
5178 data->m = NULL;
5179 data->ni = NULL;
5181 ifp->if_opackets++;
5183 ring->data_queued--;
5184 ring->data_next = (ring->data_next + 1) % RT2860_SOFTC_TX_RING_DATA_COUNT;
5187 desc->sdl0 &= ~htole16(RT2860_TXDESC_SDL0_DDONE);
5189 ring->desc_queued--;
5190 ring->desc_next = (ring->desc_next + 1) % RT2860_SOFTC_TX_RING_DESC_COUNT;
5193 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5194 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5196 RT2860_DPRINTF(sc, RT2860_DEBUG_TX,
5197 "%s: Tx eof: qid=%d, ndescs=%d, nframes=%d\n",
5198 device_get_nameunit(sc->dev), ring->qid, ndescs, nframes);
5202 * rt2860_update_stats
5204 static void rt2860_update_stats(struct rt2860_softc *sc)
5206 struct ifnet *ifp;
5207 struct ieee80211com *ic;
5208 struct ieee80211vap *vap;
5209 struct ieee80211_node *ni;
5210 uint16_t associd;
5211 uint8_t wcid;
5212 uint32_t stacnt[3];
5213 int beacons, noretryok, retryok, failed;
5215 ifp = sc->ifp;
5216 ic = ifp->if_l2com;
5218 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5219 "%s: update statistic\n",
5220 device_get_nameunit(sc->dev));
5222 rt2860_drain_fifo_stats(sc);
5224 if (ic->ic_opmode == IEEE80211_M_STA)
5226 vap = TAILQ_FIRST(&ic->ic_vaps);
5227 ni = vap->iv_bss;
5229 associd = (ni != NULL) ? ni->ni_associd : 0;
5230 wcid = RT2860_AID2WCID(associd);
5232 /* read and clear Tx statistic registers */
5234 rt2860_io_mac_read_multi(sc, RT2860_REG_TX_STA_CNT0,
5235 stacnt, sizeof(stacnt));
5237 stacnt[0] = le32toh(stacnt[0]);
5238 stacnt[1] = le32toh(stacnt[1]);
5239 stacnt[2] = le32toh(stacnt[2]);
5241 beacons = stacnt[0] >> 16;
5242 noretryok = stacnt[1] & 0xffff;
5243 retryok = stacnt[1] >> 16;
5244 failed = stacnt[0] & 0xffff;
5246 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5247 "%s: update statistic: associd=0x%04x, beacons=%d, noretryok=%d, retryok=%d, failed=%d\n",
5248 device_get_nameunit(sc->dev),
5249 associd, beacons, noretryok, retryok, failed);
5251 ifp->if_oerrors += failed;
5253 rt2860_amrr_tx_update(&sc->amrr_node[wcid],
5254 noretryok + retryok + failed, noretryok + retryok, retryok + failed);
5256 sc->tx_beacons += beacons;
5257 sc->tx_noretryok += noretryok;
5258 sc->tx_retryok += retryok;
5259 sc->tx_failed += failed;
5264 * rt2860_bbp_tuning
5266 static void rt2860_bbp_tuning(struct rt2860_softc *sc)
5268 struct ifnet *ifp;
5269 struct ieee80211com *ic;
5270 struct ieee80211vap *vap;
5271 struct ieee80211_node *ni;
5272 int chan, group;
5273 int8_t rssi, old, new;
5275 /* RT2860C does not support BBP tuning */
5277 if (sc->mac_rev == 0x28600100)
5278 return;
5280 ifp = sc->ifp;
5281 ic = ifp->if_l2com;
5282 vap = TAILQ_FIRST(&ic->ic_vaps);
5284 if ((ic->ic_flags & IEEE80211_F_SCAN) || vap == NULL ||
5285 vap->iv_opmode != IEEE80211_M_STA || vap->iv_state != IEEE80211_S_RUN)
5286 return;
5288 ni = vap->iv_bss;
5290 chan = ieee80211_chan2ieee(ic, ni->ni_chan);
5292 if (chan <= 14)
5293 group = 0;
5294 else if (chan <= 64)
5295 group = 1;
5296 else if (chan <= 128)
5297 group = 2;
5298 else
5299 group = 3;
5301 rssi = ieee80211_getrssi(vap);
5303 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
5305 new = 0x2e + sc->lna_gain[group];
5307 else
5309 if (!IEEE80211_IS_CHAN_HT40(ni->ni_chan))
5310 new = 0x32 + sc->lna_gain[group] * 5 / 3;
5311 else
5312 new = 0x3a + sc->lna_gain[group] * 5 / 3;
5315 /* Tune if absolute average RSSI is greater than -80 */
5317 if (rssi > 30)
5318 new += 0x10;
5320 old = rt2860_io_bbp_read(sc, 66);
5322 if (old != new)
5323 rt2860_io_bbp_write(sc, 66, new);
5327 * rt2860_watchdog
5329 static void rt2860_watchdog(struct rt2860_softc *sc)
5331 uint32_t tmp;
5332 int ntries;
5334 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5336 RT2860_DPRINTF(sc, RT2860_DEBUG_WATCHDOG,
5337 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
5338 device_get_nameunit(sc->dev), tmp);
5340 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) != 0)
5342 sc->tx_queue_not_empty[0]++;
5344 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40012);
5346 for (ntries = 0; ntries < 10; ntries++)
5348 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5349 if (((tmp >> RT2860_REG_TX0Q_PCNT_SHIFT) & RT2860_REG_TX0Q_PCNT_MASK) == 0)
5350 break;
5352 DELAY(1);
5355 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5358 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) != 0)
5360 sc->tx_queue_not_empty[1]++;
5362 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf4000a);
5364 for (ntries = 0; ntries < 10; ntries++)
5366 tmp = rt2860_io_mac_read(sc, RT2860_REG_PBF_TXRXQ_PCNT);
5367 if (((tmp >> RT2860_REG_TX1Q_PCNT_SHIFT) & RT2860_REG_TX1Q_PCNT_MASK) == 0)
5368 break;
5370 DELAY(1);
5373 rt2860_io_mac_write(sc, RT2860_REG_PBF_CFG, 0xf40006);
5378 * rt2860_drain_fifo_stats
5380 static void rt2860_drain_fifo_stats(struct rt2860_softc *sc)
5382 struct ifnet *ifp;
5383 uint32_t stats;
5384 uint8_t wcid, mcs, pid;
5385 int ok, retrycnt;
5387 ifp = sc->ifp;
5389 /* drain Tx status FIFO (maxsize = 16) */
5391 while ((stats = rt2860_io_mac_read(sc, RT2860_REG_TX_STA_FIFO)) &
5392 RT2860_REG_TX_STA_FIFO_VALID)
5394 wcid = (stats >> RT2860_REG_TX_STA_FIFO_WCID_SHIFT) &
5395 RT2860_REG_TX_STA_FIFO_WCID_MASK;
5397 /* if no ACK was requested, no feedback is available */
5399 if (!(stats & RT2860_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
5400 continue;
5402 /* update AMRR statistic */
5404 ok = (stats & RT2860_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
5405 mcs = (stats >> RT2860_REG_TX_STA_FIFO_MCS_SHIFT) &
5406 RT2860_REG_TX_STA_FIFO_MCS_MASK;
5407 pid = (stats >> RT2860_REG_TX_STA_FIFO_PID_SHIFT) &
5408 RT2860_REG_TX_STA_FIFO_PID_MASK;
5409 retrycnt = pid - mcs;
5411 RT2860_DPRINTF(sc, RT2860_DEBUG_STATS,
5412 "%s: FIFO statistic: wcid=0x%02x, ok=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
5413 device_get_nameunit(sc->dev),
5414 wcid, ok, mcs, pid, retrycnt);
5416 rt2860_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
5418 if (!ok)
5419 ifp->if_oerrors++;
5424 * rt2860_update_raw_counters
5426 static void rt2860_update_raw_counters(struct rt2860_softc *sc)
5428 uint32_t tmp;
5430 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT);
5432 sc->tx_nonagg += tmp & 0xffff;
5433 sc->tx_agg += tmp >> 16;
5435 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT0);
5437 sc->tx_ampdu += (tmp & 0xffff) / 1 + (tmp >> 16) / 2;
5439 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT1);
5441 sc->tx_ampdu += (tmp & 0xffff) / 3 + (tmp >> 16) / 4;
5443 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT2);
5445 sc->tx_ampdu += (tmp & 0xffff) / 5 + (tmp >> 16) / 6;
5447 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT3);
5449 sc->tx_ampdu += (tmp & 0xffff) / 7 + (tmp >> 16) / 8;
5451 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT4);
5453 sc->tx_ampdu += (tmp & 0xffff) / 9 + (tmp >> 16) / 10;
5455 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT5);
5457 sc->tx_ampdu += (tmp & 0xffff) / 11 + (tmp >> 16) / 12;
5459 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT6);
5461 sc->tx_ampdu += (tmp & 0xffff) / 13 + (tmp >> 16) / 14;
5463 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_AGG_CNT7);
5465 sc->tx_ampdu += (tmp & 0xffff) / 15 + (tmp >> 16) / 16;
5467 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT0);
5469 sc->rx_crc_errors += tmp & 0xffff;
5470 sc->rx_phy_errors += tmp >> 16;
5472 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT1);
5474 sc->rx_false_ccas += tmp & 0xffff;
5475 sc->rx_plcp_errors += tmp >> 16;
5477 tmp = rt2860_io_mac_read(sc, RT2860_REG_RX_STA_CNT2);
5479 sc->rx_dup_packets += tmp & 0xffff;
5480 sc->rx_fifo_overflows += tmp >> 16;
5484 * rt2860_intr_enable
5486 static void rt2860_intr_enable(struct rt2860_softc *sc, uint32_t intr_mask)
5488 uint32_t tmp;
5490 sc->intr_disable_mask &= ~intr_mask;
5492 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5494 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5498 * rt2860_intr_disable
5500 static void rt2860_intr_disable(struct rt2860_softc *sc, uint32_t intr_mask)
5502 uint32_t tmp;
5504 sc->intr_disable_mask |= intr_mask;
5506 tmp = sc->intr_enable_mask & ~sc->intr_disable_mask;
5508 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_INT_MASK, tmp);
5512 * rt2860_txrx_enable
5514 static int rt2860_txrx_enable(struct rt2860_softc *sc)
5516 struct ieee80211com *ic;
5517 struct ifnet *ifp;
5518 uint32_t tmp;
5519 int ntries;
5521 ifp = sc->ifp;
5522 ic = ifp->if_l2com;
5524 /* enable Tx/Rx DMA engine */
5526 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL, RT2860_REG_TX_ENABLE);
5528 for (ntries = 0; ntries < 200; ntries++)
5530 tmp = rt2860_io_mac_read(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG);
5531 if (!(tmp & (RT2860_REG_TX_DMA_BUSY | RT2860_REG_RX_DMA_BUSY)))
5532 break;
5534 DELAY(1000);
5537 if (ntries == 200)
5539 printf("%s: timeout waiting for DMA engine\n",
5540 device_get_nameunit(sc->dev));
5541 return -1;
5544 DELAY(50);
5546 tmp |= RT2860_REG_TX_WB_DDONE |
5547 RT2860_REG_RX_DMA_ENABLE |
5548 RT2860_REG_TX_DMA_ENABLE |
5549 (RT2860_REG_WPDMA_BT_SIZE64 << RT2860_REG_WPDMA_BT_SIZE_SHIFT);
5551 rt2860_io_mac_write(sc, RT2860_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5553 /* set Rx filter */
5555 tmp = RT2860_REG_RX_FILTER_DROP_CRC_ERR |
5556 RT2860_REG_RX_FILTER_DROP_PHY_ERR;
5558 if (ic->ic_opmode != IEEE80211_M_MONITOR)
5560 tmp |= RT2860_REG_RX_FILTER_DROP_DUPL |
5561 RT2860_REG_RX_FILTER_DROP_CTS |
5562 RT2860_REG_RX_FILTER_DROP_BA |
5563 RT2860_REG_RX_FILTER_DROP_ACK |
5564 RT2860_REG_RX_FILTER_DROP_VER_ERR |
5565 RT2860_REG_RX_FILTER_DROP_CTRL_RSV |
5566 RT2860_REG_RX_FILTER_DROP_CFACK |
5567 RT2860_REG_RX_FILTER_DROP_CFEND;
5569 if (ic->ic_opmode == IEEE80211_M_STA)
5570 tmp |= RT2860_REG_RX_FILTER_DROP_RTS |
5571 RT2860_REG_RX_FILTER_DROP_PSPOLL;
5573 if (!(ifp->if_flags & IFF_PROMISC))
5574 tmp |= RT2860_REG_RX_FILTER_DROP_UC_NOME;
5577 rt2860_io_mac_write(sc, RT2860_REG_RX_FILTER_CFG, tmp);
5579 rt2860_io_mac_write(sc, RT2860_REG_SYS_CTRL,
5580 RT2860_REG_RX_ENABLE | RT2860_REG_TX_ENABLE);
5582 return 0;
5586 * rt2860_alloc_rx_ring
5588 static int rt2860_alloc_rx_ring(struct rt2860_softc *sc,
5589 struct rt2860_softc_rx_ring *ring)
5591 struct rt2860_rxdesc *desc;
5592 struct rt2860_softc_rx_data *data;
5593 bus_dma_segment_t segs[1];
5594 int i, nsegs, error;
5596 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5597 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5598 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc), 1,
5599 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5600 0, NULL, NULL, &ring->desc_dma_tag);
5601 if (error != 0)
5603 printf("%s: could not create Rx desc DMA tag\n",
5604 device_get_nameunit(sc->dev));
5605 goto fail;
5608 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5609 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5610 if (error != 0)
5612 printf("%s: could not allocate Rx desc DMA memory\n",
5613 device_get_nameunit(sc->dev));
5614 goto fail;
5617 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5618 ring->desc,
5619 RT2860_SOFTC_RX_RING_DATA_COUNT * sizeof(struct rt2860_rxdesc),
5620 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5621 if (error != 0)
5623 printf("%s: could not load Rx desc DMA map\n",
5624 device_get_nameunit(sc->dev));
5625 goto fail;
5628 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5629 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5630 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL,
5631 &ring->data_dma_tag);
5632 if (error != 0)
5634 printf("%s: could not create Rx data DMA tag\n",
5635 device_get_nameunit(sc->dev));
5636 goto fail;
5639 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5641 desc = &ring->desc[i];
5642 data = &ring->data[i];
5644 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5645 if (error != 0)
5647 printf("%s: could not create Rx data DMA map\n",
5648 device_get_nameunit(sc->dev));
5649 goto fail;
5652 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
5653 if (data->m == NULL)
5655 printf("%s: could not allocate Rx mbuf\n",
5656 device_get_nameunit(sc->dev));
5657 error = ENOMEM;
5658 goto fail;
5661 data->m->m_len = data->m->m_pkthdr.len = MJUMPAGESIZE;
5663 error = bus_dmamap_load_mbuf_sg(ring->data_dma_tag, data->dma_map,
5664 data->m, segs, &nsegs, BUS_DMA_NOWAIT);
5665 if (error != 0)
5667 printf("%s: could not load Rx mbuf DMA map\n",
5668 device_get_nameunit(sc->dev));
5669 goto fail;
5672 KASSERT(nsegs == 1, ("%s: too many DMA segments",
5673 device_get_nameunit(sc->dev)));
5675 desc->sdp0 = htole32(segs[0].ds_addr);
5676 desc->sdl0 = htole16(MJUMPAGESIZE);
5679 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5680 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5682 return 0;
5684 fail:
5686 rt2860_free_rx_ring(sc, ring);
5688 return error;
5692 * rt2860_reset_rx_ring
5694 static void rt2860_reset_rx_ring(struct rt2860_softc *sc,
5695 struct rt2860_softc_rx_ring *ring)
5697 struct rt2860_rxdesc *desc;
5698 int i;
5700 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5702 desc = &ring->desc[i];
5704 desc->sdl0 &= ~htole16(RT2860_RXDESC_SDL0_DDONE);
5707 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5708 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5710 ring->cur = 0;
5714 * rt2860_free_rx_ring
5716 static void rt2860_free_rx_ring(struct rt2860_softc *sc,
5717 struct rt2860_softc_rx_ring *ring)
5719 struct rt2860_softc_rx_data *data;
5720 int i;
5722 if (ring->desc != NULL)
5724 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5725 BUS_DMASYNC_POSTWRITE);
5726 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5727 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5728 ring->desc_dma_map);
5731 if (ring->desc_dma_tag != NULL)
5732 bus_dma_tag_destroy(ring->desc_dma_tag);
5734 for (i = 0; i < RT2860_SOFTC_RX_RING_DATA_COUNT; i++)
5736 data = &ring->data[i];
5738 if (data->m != NULL)
5740 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5741 BUS_DMASYNC_POSTREAD);
5742 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5743 m_freem(data->m);
5746 if (data->dma_map != NULL)
5747 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5750 if (ring->data_dma_tag != NULL)
5751 bus_dma_tag_destroy(ring->data_dma_tag);
5755 * rt2860_alloc_tx_ring
5757 static int rt2860_alloc_tx_ring(struct rt2860_softc *sc,
5758 struct rt2860_softc_tx_ring *ring, int qid)
5760 struct rt2860_softc_tx_data *data;
5761 int error, i;
5763 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5764 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5765 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc), 1,
5766 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5767 0, NULL, NULL, &ring->desc_dma_tag);
5768 if (error != 0)
5770 printf("%s: could not create Tx desc DMA tag\n",
5771 device_get_nameunit(sc->dev));
5772 goto fail;
5775 error = bus_dmamem_alloc(ring->desc_dma_tag, (void **) &ring->desc,
5776 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_dma_map);
5777 if (error != 0)
5779 printf("%s: could not allocate Tx desc DMA memory\n",
5780 device_get_nameunit(sc->dev));
5781 goto fail;
5784 error = bus_dmamap_load(ring->desc_dma_tag, ring->desc_dma_map,
5785 ring->desc,
5786 RT2860_SOFTC_TX_RING_DESC_COUNT * sizeof(struct rt2860_txdesc),
5787 rt2860_dma_map_addr, &ring->desc_phys_addr, 0);
5788 if (error != 0)
5790 printf("%s: could not load Tx desc DMA map\n",
5791 device_get_nameunit(sc->dev));
5792 goto fail;
5795 ring->desc_queued = 0;
5796 ring->desc_cur = 0;
5797 ring->desc_next = 0;
5799 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
5800 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5801 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE, 1,
5802 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5803 0, NULL, NULL, &ring->seg0_dma_tag);
5804 if (error != 0)
5806 printf("%s: could not create Tx seg0 DMA tag\n",
5807 device_get_nameunit(sc->dev));
5808 goto fail;
5811 error = bus_dmamem_alloc(ring->seg0_dma_tag, (void **) &ring->seg0,
5812 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->seg0_dma_map);
5813 if (error != 0)
5815 printf("%s: could not allocate Tx seg0 DMA memory\n",
5816 device_get_nameunit(sc->dev));
5817 goto fail;
5820 error = bus_dmamap_load(ring->seg0_dma_tag, ring->seg0_dma_map,
5821 ring->seg0,
5822 RT2860_SOFTC_TX_RING_DATA_COUNT * RT2860_TX_DATA_SEG0_SIZE,
5823 rt2860_dma_map_addr, &ring->seg0_phys_addr, 0);
5824 if (error != 0)
5826 printf("%s: could not load Tx seg0 DMA map\n",
5827 device_get_nameunit(sc->dev));
5828 goto fail;
5831 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
5832 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
5833 MJUMPAGESIZE, RT2860_SOFTC_MAX_SCATTER, MJUMPAGESIZE, 0, NULL, NULL,
5834 &ring->data_dma_tag);
5835 if (error != 0)
5837 printf("%s: could not create Tx data DMA tag\n",
5838 device_get_nameunit(sc->dev));
5839 goto fail;
5842 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5844 data = &ring->data[i];
5846 error = bus_dmamap_create(ring->data_dma_tag, 0, &data->dma_map);
5847 if (error != 0)
5849 printf("%s: could not create Tx data DMA map\n",
5850 device_get_nameunit(sc->dev));
5851 goto fail;
5855 ring->data_queued = 0;
5856 ring->data_cur = 0;
5857 ring->data_next = 0;
5859 ring->qid = qid;
5861 return 0;
5863 fail:
5865 rt2860_free_tx_ring(sc, ring);
5867 return error;
5871 * rt2860_reset_tx_ring
5873 static void rt2860_reset_tx_ring(struct rt2860_softc *sc,
5874 struct rt2860_softc_tx_ring *ring)
5876 struct rt2860_softc_tx_data *data;
5877 struct rt2860_txdesc *desc;
5878 int i;
5880 for (i = 0; i < RT2860_SOFTC_TX_RING_DESC_COUNT; i++)
5882 desc = &ring->desc[i];
5884 desc->sdl0 = 0;
5885 desc->sdl1 = 0;
5888 ring->desc_queued = 0;
5889 ring->desc_cur = 0;
5890 ring->desc_next = 0;
5892 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5893 BUS_DMASYNC_PREWRITE);
5895 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5896 BUS_DMASYNC_PREWRITE);
5898 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5900 data = &ring->data[i];
5902 if (data->m != NULL)
5904 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5905 BUS_DMASYNC_POSTWRITE);
5906 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5907 m_freem(data->m);
5908 data->m = NULL;
5911 if (data->ni != NULL)
5913 ieee80211_free_node(data->ni);
5914 data->ni = NULL;
5918 ring->data_queued = 0;
5919 ring->data_cur = 0;
5920 ring->data_next = 0;
5924 * rt2860_free_tx_ring
5926 static void rt2860_free_tx_ring(struct rt2860_softc *sc,
5927 struct rt2860_softc_tx_ring *ring)
5929 struct rt2860_softc_tx_data *data;
5930 int i;
5932 if (ring->desc != NULL)
5934 bus_dmamap_sync(ring->desc_dma_tag, ring->desc_dma_map,
5935 BUS_DMASYNC_POSTWRITE);
5936 bus_dmamap_unload(ring->desc_dma_tag, ring->desc_dma_map);
5937 bus_dmamem_free(ring->desc_dma_tag, ring->desc,
5938 ring->desc_dma_map);
5941 if (ring->desc_dma_tag != NULL)
5942 bus_dma_tag_destroy(ring->desc_dma_tag);
5944 if (ring->seg0 != NULL)
5946 bus_dmamap_sync(ring->seg0_dma_tag, ring->seg0_dma_map,
5947 BUS_DMASYNC_POSTWRITE);
5948 bus_dmamap_unload(ring->seg0_dma_tag, ring->seg0_dma_map);
5949 bus_dmamem_free(ring->seg0_dma_tag, ring->seg0,
5950 ring->seg0_dma_map);
5953 if (ring->seg0_dma_tag != NULL)
5954 bus_dma_tag_destroy(ring->seg0_dma_tag);
5956 for (i = 0; i < RT2860_SOFTC_TX_RING_DATA_COUNT; i++)
5958 data = &ring->data[i];
5960 if (data->m != NULL)
5962 bus_dmamap_sync(ring->data_dma_tag, data->dma_map,
5963 BUS_DMASYNC_POSTWRITE);
5964 bus_dmamap_unload(ring->data_dma_tag, data->dma_map);
5965 m_freem(data->m);
5968 if (data->ni != NULL)
5969 ieee80211_free_node(data->ni);
5971 if (data->dma_map != NULL)
5972 bus_dmamap_destroy(ring->data_dma_tag, data->dma_map);
5975 if (ring->data_dma_tag != NULL)
5976 bus_dma_tag_destroy(ring->data_dma_tag);
5980 * rt2860_dma_map_addr
5982 static void rt2860_dma_map_addr(void *arg, bus_dma_segment_t *segs,
5983 int nseg, int error)
5985 if (error != 0)
5986 return;
5988 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
5990 *(bus_addr_t *) arg = segs[0].ds_addr;
5994 * rt2860_sysctl_attach
5996 static void rt2860_sysctl_attach(struct rt2860_softc *sc)
5998 struct sysctl_ctx_list *ctx;
5999 struct sysctl_oid *tree;
6000 struct sysctl_oid *stats;
6002 ctx = device_get_sysctl_ctx(sc->dev);
6003 tree = device_get_sysctl_tree(sc->dev);
6005 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6006 "stats", CTLFLAG_RD, 0, "statistic");
6008 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6009 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
6010 "all interrupts");
6012 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6013 "tx_coherent_interrupts", CTLFLAG_RD, &sc->tx_coherent_interrupts, 0,
6014 "Tx coherent interrupts");
6016 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6017 "rx_coherent_interrupts", CTLFLAG_RD, &sc->rx_coherent_interrupts, 0,
6018 "Rx coherent interrupts");
6020 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6021 "txrx_coherent_interrupts", CTLFLAG_RD, &sc->txrx_coherent_interrupts, 0,
6022 "Tx/Rx coherent interrupts");
6024 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6025 "fifo_sta_full_interrupts", CTLFLAG_RD, &sc->fifo_sta_full_interrupts, 0,
6026 "FIFO statistic full interrupts");
6028 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6029 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
6030 "Rx interrupts");
6032 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6033 "rx_delay_interrupts", CTLFLAG_RD, &sc->rx_delay_interrupts, 0,
6034 "Rx delay interrupts");
6036 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6037 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
6038 "Tx MGMT interrupts");
6040 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6041 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
6042 "Tx HCCA interrupts");
6044 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6045 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
6046 "Tx AC3 interrupts");
6048 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6049 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
6050 "Tx AC2 interrupts");
6052 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6053 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
6054 "Tx AC1 interrupts");
6056 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6057 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
6058 "Tx AC0 interrupts");
6060 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6061 "tx_delay_interrupts", CTLFLAG_RD, &sc->tx_delay_interrupts, 0,
6062 "Tx delay interrupts");
6064 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6065 "pre_tbtt_interrupts", CTLFLAG_RD, &sc->pre_tbtt_interrupts, 0,
6066 "Pre-TBTT interrupts");
6068 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6069 "tbtt_interrupts", CTLFLAG_RD, &sc->tbtt_interrupts, 0,
6070 "TBTT interrupts");
6072 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6073 "mcu_cmd_interrupts", CTLFLAG_RD, &sc->mcu_cmd_interrupts, 0,
6074 "MCU command interrupts");
6076 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6077 "auto_wakeup_interrupts", CTLFLAG_RD, &sc->auto_wakeup_interrupts, 0,
6078 "auto wakeup interrupts");
6080 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6081 "gp_timer_interrupts", CTLFLAG_RD, &sc->gp_timer_interrupts, 0,
6082 "GP timer interrupts");
6084 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6085 "tx_mgmt_desc_queued", CTLFLAG_RD, &sc->tx_ring[5].desc_queued, 0,
6086 "Tx MGMT descriptors queued");
6088 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6089 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].data_queued, 0,
6090 "Tx MGMT data queued");
6092 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6093 "tx_hcca_desc_queued", CTLFLAG_RD, &sc->tx_ring[4].desc_queued, 0,
6094 "Tx HCCA descriptors queued");
6096 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6097 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].data_queued, 0,
6098 "Tx HCCA data queued");
6100 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6101 "tx_ac3_desc_queued", CTLFLAG_RD, &sc->tx_ring[3].desc_queued, 0,
6102 "Tx AC3 descriptors queued");
6104 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6105 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].data_queued, 0,
6106 "Tx AC3 data queued");
6108 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6109 "tx_ac2_desc_queued", CTLFLAG_RD, &sc->tx_ring[2].desc_queued, 0,
6110 "Tx AC2 descriptors queued");
6112 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6113 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].data_queued, 0,
6114 "Tx AC2 data queued");
6116 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6117 "tx_ac1_desc_queued", CTLFLAG_RD, &sc->tx_ring[1].desc_queued, 0,
6118 "Tx AC1 descriptors queued");
6120 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6121 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].data_queued, 0,
6122 "Tx AC1 data queued");
6124 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6125 "tx_ac0_desc_queued", CTLFLAG_RD, &sc->tx_ring[0].desc_queued, 0,
6126 "Tx AC0 descriptors queued");
6128 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6129 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].data_queued, 0,
6130 "Tx AC0 data queued");
6132 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6133 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
6134 "Tx watchdog timeouts");
6136 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6137 "tx_defrag_packets", CTLFLAG_RD, &sc->tx_defrag_packets, 0,
6138 "Tx defragmented packets");
6140 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6141 "no_tx_desc_avail", CTLFLAG_RD, &sc->no_tx_desc_avail, 0,
6142 "no Tx descriptors available");
6144 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6145 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
6146 "Rx mbuf allocation errors");
6148 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6149 "rx_mbuf_dmamap_errors", CTLFLAG_RD, &sc->rx_mbuf_dmamap_errors, 0,
6150 "Rx mbuf DMA mapping errors");
6152 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6153 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
6154 "Tx queue 0 not empty");
6156 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6157 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
6158 "Tx queue 1 not empty");
6160 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6161 "tx_beacons", CTLFLAG_RD, &sc->tx_beacons, 0,
6162 "Tx beacons");
6164 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6165 "tx_noretryok", CTLFLAG_RD, &sc->tx_noretryok, 0,
6166 "Tx successfull without retries");
6168 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6169 "tx_retryok", CTLFLAG_RD, &sc->tx_retryok, 0,
6170 "Tx successfull with retries");
6172 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6173 "tx_failed", CTLFLAG_RD, &sc->tx_failed, 0,
6174 "Tx failed");
6176 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6177 "tx_nonagg", CTLFLAG_RD, &sc->tx_nonagg, 0,
6178 "Tx non-aggregated");
6180 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6181 "tx_agg", CTLFLAG_RD, &sc->tx_agg, 0,
6182 "Tx aggregated");
6184 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6185 "tx_ampdu", CTLFLAG_RD, &sc->tx_ampdu, 0,
6186 "Tx A-MPDU");
6188 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6189 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
6190 "Rx packets");
6192 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6193 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
6194 "Rx CRC errors");
6196 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6197 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
6198 "Rx PHY errors");
6200 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6201 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
6202 "Rx false CCAs");
6204 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6205 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
6206 "Rx PLCP errors");
6208 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6209 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
6210 "Rx duplicate packets");
6212 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6213 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
6214 "Rx FIFO overflows");
6217 static device_method_t rt2860_dev_methods[] =
6219 DEVMETHOD(device_probe, rt2860_probe),
6220 DEVMETHOD(device_attach, rt2860_attach),
6221 DEVMETHOD(device_detach, rt2860_detach),
6222 DEVMETHOD(device_shutdown, rt2860_shutdown),
6223 DEVMETHOD(device_suspend, rt2860_suspend),
6224 DEVMETHOD(device_resume, rt2860_resume),
6225 { 0, 0 }
6228 static driver_t rt2860_driver =
6230 "rt2860",
6231 rt2860_dev_methods,
6232 sizeof(struct rt2860_softc)
6235 static devclass_t rt2860_dev_class;
6237 DRIVER_MODULE(rt2860, pci, rt2860_driver, rt2860_dev_class, 0, 0);
6239 MODULE_DEPEND(rt2860, pci, 1, 1, 1);
6240 MODULE_DEPEND(rt2860, wlan, 1, 1, 1);