Reset command ring during device attaching because in HOSTAP mode key setting/deletin...
[ralink_drivers/rt2870_fbsd8.git] / rt2870.c
blob44c9e1c84a1310377d8e22bc5b974cbe8af17640
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 "rt2870_softc.h"
20 #include "rt2870_reg.h"
21 #include "rt2870_eeprom.h"
22 #include "rt2870_ucode.h"
23 #include "rt2870_rxwi.h"
24 #include "rt2870_rxinfo.h"
25 #include "rt2870_txinfo.h"
26 #include "rt2870_txwi.h"
27 #include "rt2870_read_eeprom.h"
28 #include "rt2870_io.h"
29 #include "rt2870_rf.h"
30 #include "rt2870_led.h"
31 #include "rt2870_debug.h"
34 * Defines and macros
37 #define USB_PRODUCT_LINKSYS4_WUSB600N 0x0071
38 #define USB_PRODUCT_DLINK2_DWA140 0x3c09
39 #define USB_PRODUCT_DLINK2_DWA160AREVB 0x3c11
40 #define USB_PRODUCT_ASUS_RT2770F 0x1742
41 #define USB_PRODUCT_RALINK_RT2770 0x2770
43 #define RT2870_USB_CONFIG_INDEX 0
44 #define RT2870_USB_IFACE_INDEX 0
46 #define RT2870_USB_REQ_MAC_READ_MULTI 0x07
48 /* packet length + Rx wireless info + Rx info */
49 #define RT2870_RX_DESC_SIZE \
50 (sizeof(uint32_t) + sizeof(struct rt2870_rxwi) + sizeof(struct rt2870_rxinfo))
52 /* Tx info + Tx wireless info + max padding */
53 #define RT2870_TX_DESC_SIZE \
54 (sizeof(struct rt2870_txinfo) + sizeof(struct rt2870_txwi) + 11)
56 #define RT2870_MAX_AGG_SIZE 3840
58 #define RT2870_USB_RX_BULK_BUFLEN (2048 * 12)
60 #define RT2870_NOISE_FLOOR -95
62 #define RT2870_AID2WCID(aid) ((aid) & 0xff)
64 #define RT2870_ACK_SIZE 14
66 #define IEEE80211_HAS_ADDR4(wh) \
67 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
69 #define RT2870_MS(_v, _f) (((_v) & _f) >> _f##_S)
70 #define RT2870_SM(_v, _f) (((_v) << _f##_S) & _f)
72 #define RT2870_USB_XFER_TIMEOUT 5000
74 #define RT2870_TX_WATCHDOG_TIMEOUT 5
77 * Data structures and types
80 struct rt2870_cmd_argv_keyset
82 struct ieee80211_key key;
83 uint16_t associd;
86 struct rt2870_cmd_argv_keydelete
88 struct ieee80211_key key;
89 uint16_t associd;
93 * Static function prototypes
96 static void rt2870_init_channels(struct rt2870_softc *sc);
98 static void rt2870_init_channels_ht40(struct rt2870_softc *sc);
100 static void rt2870_init_locked(void *priv);
102 static void rt2870_init(void *priv);
104 static int rt2870_init_bbp(struct rt2870_softc *sc);
106 static void rt2870_stop_locked(void *priv);
108 static void rt2870_stop(void *priv);
110 static void rt2870_start(struct ifnet *ifp);
112 static int rt2870_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
114 static struct ieee80211vap *rt2870_vap_create(struct ieee80211com *ic,
115 const char name[IFNAMSIZ], int unit, int opmode, int flags,
116 const uint8_t bssid[IEEE80211_ADDR_LEN],
117 const uint8_t mac[IEEE80211_ADDR_LEN]);
119 static void rt2870_vap_delete(struct ieee80211vap *vap);
121 static int rt2870_vap_reset(struct ieee80211vap *vap, u_long cmd);
123 static int rt2870_vap_newstate(struct ieee80211vap *vap,
124 enum ieee80211_state nstate, int arg);
126 static void rt2870_vap_key_update_begin(struct ieee80211vap *vap);
128 static void rt2870_vap_key_update_end(struct ieee80211vap *vap);
130 static int rt2870_vap_key_set(struct ieee80211vap *vap,
131 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN]);
133 static int rt2870_vap_key_delete(struct ieee80211vap *vap,
134 const struct ieee80211_key *k);
136 static void rt2870_vap_update_beacon(struct ieee80211vap *vap, int what);
138 static struct ieee80211_node *rt2870_node_alloc(struct ieee80211vap *vap,
139 const uint8_t mac[IEEE80211_ADDR_LEN]);
141 static void rt2870_node_getmimoinfo(const struct ieee80211_node *ni,
142 struct ieee80211_mimo_info *mi);
144 static int rt2870_setregdomain(struct ieee80211com *ic,
145 struct ieee80211_regdomain *reg,
146 int nchans, struct ieee80211_channel chans[]);
148 static void rt2870_getradiocaps(struct ieee80211com *ic,
149 int maxchans, int *nchans, struct ieee80211_channel chans[]);
151 static void rt2870_scan_start(struct ieee80211com *ic);
153 static void rt2870_scan_end(struct ieee80211com *ic);
155 static void rt2870_set_channel(struct ieee80211com *ic);
157 static void rt2870_newassoc(struct ieee80211_node *ni, int isnew);
159 static void rt2870_updateslot(struct ifnet *ifp);
161 static void rt2870_update_promisc(struct ifnet *ifp);
163 static void rt2870_update_mcast(struct ifnet *ifp);
165 static int rt2870_wme_update(struct ieee80211com *ic);
167 static int rt2870_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
168 const struct ieee80211_bpf_params *params);
170 static int rt2870_recv_action(struct ieee80211_node *ni,
171 const struct ieee80211_frame *wh,
172 const uint8_t *frm, const uint8_t *efrm);
174 static int rt2870_send_action(struct ieee80211_node *ni,
175 int cat, int act, void *sa);
177 static void rt2870_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
179 static void rt2870_periodic(void *arg);
181 static void rt2870_tx_watchdog(void *arg);
183 static int rt2870_do_async(struct rt2870_softc *sc,
184 void (*cb)(struct rt2870_softc *sc, void *arg),
185 void *arg, int len);
187 static void rt2870_updateslot_cb(struct rt2870_softc *sc, void *arg);
189 static void rt2870_wme_update_cb(struct rt2870_softc *sc, void *arg);
191 static void rt2870_vap_key_set_cb(struct rt2870_softc *sc, void *arg);
193 static void rt2870_vap_key_delete_cb(struct rt2870_softc *sc, void *arg);
195 static void rt2870_vap_update_beacon_cb(struct rt2870_softc *sc, void *arg);
197 static void rt2870_asic_set_bssid(struct rt2870_softc *sc,
198 const uint8_t *bssid);
200 static void rt2870_asic_set_macaddr(struct rt2870_softc *sc,
201 const uint8_t *addr);
203 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc *sc);
205 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc *sc);
207 static void rt2870_asic_enable_mrr(struct rt2870_softc *sc);
209 static void rt2870_asic_set_txpreamble(struct rt2870_softc *sc);
211 static void rt2870_asic_set_basicrates(struct rt2870_softc *sc);
213 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc *sc);
215 static void rt2870_asic_update_txpower(struct rt2870_softc *sc);
217 static void rt2870_asic_update_promisc(struct rt2870_softc *sc);
219 static void rt2870_asic_updateprot(struct rt2870_softc *sc);
221 static void rt2870_asic_updateslot(struct rt2870_softc *sc);
223 static void rt2870_asic_wme_update(struct rt2870_softc *sc);
225 static void rt2870_asic_update_beacon(struct rt2870_softc *sc,
226 struct ieee80211vap *vap);
228 static void rt2870_asic_clear_keytables(struct rt2870_softc *sc);
230 static int rt2870_beacon_alloc(struct rt2870_softc *sc,
231 struct ieee80211vap *vap);
233 static uint8_t rt2870_rxrate(struct rt2870_rxwi *rxwi);
235 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc *sc,
236 const struct rt2870_rxwi *rxwi);
238 static int8_t rt2870_rssi2dbm(struct rt2870_softc *sc,
239 uint8_t rssi, uint8_t rxpath);
241 static uint8_t rt2870_rate2mcs(uint8_t rate);
243 static void rt2870_rx_frame(struct rt2870_softc *sc,
244 uint8_t *buf, uint32_t dmalen);
246 static int rt2870_tx_mgmt(struct rt2870_softc *sc,
247 struct mbuf *m, struct ieee80211_node *ni, int qid);
249 static int rt2870_tx_data(struct rt2870_softc *sc,
250 struct mbuf *m, struct ieee80211_node *ni, int qid);
252 static int rt2870_tx_raw(struct rt2870_softc *sc,
253 struct mbuf *m, struct ieee80211_node *ni,
254 const struct ieee80211_bpf_params *params);
256 static void rt2870_rx_intr(struct usb_xfer *xfer, usb_error_t error);
258 static void rt2870_tx_intr(struct usb_xfer *xfer, usb_error_t error);
260 static void rt2870_rx_done_task(void *context, int pending);
262 static void rt2870_tx_done_task(void *context, int pending);
264 static void rt2870_periodic_task(void *context, int pending);
266 static void rt2870_cmd_task(void *context, int pending);
268 static int rt2870_rx_eof(struct rt2870_softc *sc, int limit);
270 static void rt2870_tx_eof(struct rt2870_softc *sc,
271 struct rt2870_softc_tx_ring *ring);
273 static void rt2870_update_stats(struct rt2870_softc *sc);
275 static void rt2870_bbp_tuning(struct rt2870_softc *sc);
277 static void rt2870_watchdog(struct rt2870_softc *sc);
279 static void rt2870_drain_fifo_stats(struct rt2870_softc *sc);
281 static void rt2870_update_raw_counters(struct rt2870_softc *sc);
283 static int rt2870_txrx_enable(struct rt2870_softc *sc);
285 static int rt2870_alloc_rx_ring(struct rt2870_softc *sc,
286 struct rt2870_softc_rx_ring *ring);
288 static void rt2870_reset_rx_ring(struct rt2870_softc *sc,
289 struct rt2870_softc_rx_ring *ring);
291 static void rt2870_free_rx_ring(struct rt2870_softc *sc,
292 struct rt2870_softc_rx_ring *ring);
294 static int rt2870_alloc_tx_ring(struct rt2870_softc *sc,
295 struct rt2870_softc_tx_ring *ring, int qid);
297 static void rt2870_reset_tx_ring(struct rt2870_softc *sc,
298 struct rt2870_softc_tx_ring *ring);
300 static void rt2870_free_tx_ring(struct rt2870_softc *sc,
301 struct rt2870_softc_tx_ring *ring);
303 static void rt2870_reset_cmd_ring(struct rt2870_softc *sc,
304 struct rt2870_softc_cmd_ring *ring);
306 static void rt2870_sysctl_attach(struct rt2870_softc *sc);
309 * Static variables
312 static const struct usb_device_id rt2870_usb_devid[] =
314 { USB_VP(USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB600N) }, /* Linksys WUSB600N */
315 { USB_VP(USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA140) }, /* D-Link DWA-140 */
316 { USB_VP(USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA160AREVB) }, /* D-Link DWA-160A Rev. B */
317 { USB_VP(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2770F) }, /* Asus RT2770F */
318 { USB_VP(USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2770) }, /* Ralink RT2770 */
321 static const struct usb_config rt2870_usb_config[RT2870_SOFTC_USB_XFER_COUNT] =
324 .type = UE_BULK,
325 .endpoint = 0x01,
326 .direction = UE_DIR_IN,
327 .bufsize = RT2870_USB_RX_BULK_BUFLEN,
328 .flags = { .ext_buffer = 1, .pipe_bof = 1, .short_xfer_ok = 1, },
329 .callback = rt2870_rx_intr,
332 .type = UE_BULK,
333 .endpoint = 0x01,
334 .direction = UE_DIR_OUT,
335 .bufsize = (RT2870_TX_DESC_SIZE + MJUMPAGESIZE),
336 .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, },
337 .callback = rt2870_tx_intr,
338 .timeout = RT2870_USB_XFER_TIMEOUT,
341 .type = UE_BULK,
342 .endpoint = 0x02,
343 .direction = UE_DIR_OUT,
344 .bufsize = (RT2870_TX_DESC_SIZE + MJUMPAGESIZE),
345 .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, },
346 .callback = rt2870_tx_intr,
347 .timeout = RT2870_USB_XFER_TIMEOUT,
350 .type = UE_BULK,
351 .endpoint = 0x03,
352 .direction = UE_DIR_OUT,
353 .bufsize = (RT2870_TX_DESC_SIZE + MJUMPAGESIZE),
354 .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, },
355 .callback = rt2870_tx_intr,
356 .timeout = RT2870_USB_XFER_TIMEOUT,
359 .type = UE_BULK,
360 .endpoint = 0x04,
361 .direction = UE_DIR_OUT,
362 .bufsize = (RT2870_TX_DESC_SIZE + MJUMPAGESIZE),
363 .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, },
364 .callback = rt2870_tx_intr,
365 .timeout = RT2870_USB_XFER_TIMEOUT,
368 .type = UE_BULK,
369 .endpoint = 0x05,
370 .direction = UE_DIR_OUT,
371 .bufsize = (RT2870_TX_DESC_SIZE + MJUMPAGESIZE),
372 .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, },
373 .callback = rt2870_tx_intr,
374 .timeout = RT2870_USB_XFER_TIMEOUT,
377 .type = UE_BULK,
378 .endpoint = 0x06,
379 .direction = UE_DIR_OUT,
380 .bufsize = (RT2870_TX_DESC_SIZE + MJUMPAGESIZE),
381 .flags = { .ext_buffer = 1, .pipe_bof = 1, .force_short_xfer = 1, },
382 .callback = rt2870_tx_intr,
383 .timeout = RT2870_USB_XFER_TIMEOUT,
387 static const struct
389 uint32_t reg;
390 uint32_t val;
391 } rt2870_def_mac[] =
393 { RT2870_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
394 { RT2870_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
395 { RT2870_REG_LEGACY_BASIC_RATE, 0x0000013f },
396 { RT2870_REG_HT_BASIC_RATE, 0x00008003 },
397 { RT2870_REG_SYS_CTRL, 0x00000000 },
398 { RT2870_REG_RX_FILTER_CFG, 0x00017f97 },
399 { RT2870_REG_BKOFF_SLOT_CFG, 0x00000209 },
400 { RT2870_REG_TX_SW_CFG0, 0x00000000 },
401 { RT2870_REG_TX_SW_CFG1, 0x00080606 },
402 { RT2870_REG_TX_LINK_CFG, 0x00001020 },
403 { RT2870_REG_TX_TIMEOUT_CFG, 0x000a2090 },
404 { RT2870_REG_MAX_LEN_CFG, (1 << 12) | RT2870_MAX_AGG_SIZE },
405 { RT2870_REG_LED_CFG, 0x7f031e46 },
406 { RT2870_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
407 { RT2870_REG_TX_RTY_CFG, 0x47d01f0f },
408 { RT2870_REG_AUTO_RSP_CFG, 0x00000013 },
409 { RT2870_REG_TX_CCK_PROT_CFG, 0x05740003 },
410 { RT2870_REG_TX_OFDM_PROT_CFG, 0x05740003 },
411 { RT2870_REG_PBF_CFG, 0x00f40006 },
412 { RT2870_REG_TX_MM40_PROT_CFG, 0x03f44084 },
413 { RT2870_REG_SCHDMA_WPDMA_GLO_CFG, 0x00000030 },
414 { RT2870_REG_TX_GF20_PROT_CFG, 0x01744004 },
415 { RT2870_REG_TX_GF40_PROT_CFG, 0x03f44084 },
416 { RT2870_REG_TX_MM20_PROT_CFG, 0x01744004 },
417 { RT2870_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
418 { RT2870_REG_TX_RTS_CFG, 0x00092b20 },
419 { RT2870_REG_TX_EXP_ACK_TIME, 0x002400ca },
420 { RT2870_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
421 { RT2870_REG_XIFS_TIME_CFG, 0x33a41010 },
422 { RT2870_REG_PWR_PIN_CFG, 0x00000003 },
423 { RT2870_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
424 { RT2870_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
425 { RT2870_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
428 #define RT2870_DEF_MAC_SIZE (sizeof(rt2870_def_mac) / sizeof(rt2870_def_mac[0]))
430 static const struct
432 uint8_t reg;
433 uint8_t val;
434 } rt2870_def_bbp[] =
436 { 65, 0x2c },
437 { 66, 0x38 },
438 { 69, 0x12 },
439 { 70, 0x0a },
440 { 73, 0x10 },
441 { 81, 0x37 },
442 { 82, 0x62 },
443 { 83, 0x6a },
444 { 84, 0x99 },
445 { 86, 0x00 },
446 { 91, 0x04 },
447 { 92, 0x00 },
448 { 103, 0x00 },
449 { 105, 0x05 },
450 { 106, 0x35 },
453 #define RT2870_DEF_BBP_SIZE (sizeof(rt2870_def_bbp) / sizeof(rt2870_def_bbp[0]))
455 SYSCTL_NODE(_hw, OID_AUTO, rt2870, CTLFLAG_RD, 0, "RT2870 driver parameters");
457 #ifdef RT2870_DEBUG
458 static int rt2870_debug = 0;
459 SYSCTL_INT(_hw_rt2870, OID_AUTO, debug, CTLFLAG_RW, &rt2870_debug, 0, "rt2870 debug level");
460 TUNABLE_INT("hw.rt2870.debug", &rt2870_debug);
461 #endif
464 * rt2870_probe
466 static int rt2870_probe(device_t dev)
468 struct usb_attach_arg *uaa;
470 uaa = device_get_ivars(dev);
472 if (uaa->usb_mode != USB_MODE_HOST)
473 return ENXIO;
475 if (uaa->info.bConfigIndex != RT2870_USB_CONFIG_INDEX)
476 return ENXIO;
478 if (uaa->info.bIfaceIndex != RT2870_USB_IFACE_INDEX)
479 return ENXIO;
481 return (usbd_lookup_id_by_uaa(rt2870_usb_devid,
482 sizeof(rt2870_usb_devid), uaa));
486 * rt2870_attach
488 static int rt2870_attach(device_t dev)
490 struct rt2870_softc *sc;
491 struct usb_attach_arg *uaa;
492 usb_error_t usb_error;
493 struct ieee80211com *ic;
494 struct ifnet *ifp;
495 struct usb_interface *iface;
496 struct usb_interface_descriptor *iface_desc;
497 uint8_t iface_index;
498 int error, ntries, i;
500 sc = device_get_softc(dev);
501 uaa = device_get_ivars(dev);
503 sc->dev = dev;
504 sc->usb_dev = uaa->device;
506 device_set_usb_desc(dev);
508 #ifdef RT2870_DEBUG
509 sc->debug = rt2870_debug;
511 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
512 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
513 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2870 debug level");
514 #endif
516 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
517 "%s: attaching\n",
518 device_get_nameunit(sc->dev));
520 iface_index = RT2870_USB_IFACE_INDEX;
522 iface = usbd_get_iface(uaa->device, iface_index);
523 if (iface == NULL)
525 printf("%s: could not get USB interface\n",
526 device_get_nameunit(sc->dev));
527 return ENXIO;
530 iface_desc = usbd_get_interface_descriptor(iface);
531 if (iface_desc == NULL)
533 printf("%s: could not get USB interface descriptor\n",
534 device_get_nameunit(sc->dev));
535 return ENXIO;
538 sc->usb_endpoints = iface_desc->bNumEndpoints;
539 if ((sc->usb_endpoints != RT2870_SOFTC_USB_XFER_COUNT) &&
540 (sc->usb_endpoints != RT2870_SOFTC_USB_XFER_COUNT - 2))
542 printf("%s: wrong number of USB endpoints=%d\n",
543 device_get_nameunit(sc->dev), sc->usb_endpoints);
544 return ENXIO;
547 mtx_init(&sc->lock, device_get_nameunit(dev),
548 MTX_NETWORK_LOCK, MTX_DEF);
550 usb_error = usbd_transfer_setup(uaa->device, &iface_index,
551 sc->usb_xfer, rt2870_usb_config, sc->usb_endpoints,
552 sc, &sc->lock);
553 if (usb_error != 0)
555 printf("%s: could not allocate USB transfers: error=%s\n",
556 device_get_nameunit(sc->dev), usbd_errstr(usb_error));
557 error = ENXIO;
558 goto fail;
561 RT2870_SOFTC_LOCK(sc);
563 for (ntries = 0; ntries < 100; ntries++)
565 sc->mac_rev = rt2870_io_mac_read(sc, RT2870_REG_MAC_CSR0);
566 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
567 break;
569 DELAY(10);
572 if (ntries == 100)
574 printf("%s: timeout waiting for NIC to initialize\n",
575 device_get_nameunit(sc->dev));
576 error = ENXIO;
577 goto fail;
580 rt2870_read_eeprom(sc);
582 RT2870_SOFTC_UNLOCK(sc);
584 printf("%s: MAC/BBP RT2870 (rev 0x%08x), RF %s\n",
585 device_get_nameunit(sc->dev), sc->mac_rev,
586 rt2870_rf_name(sc->rf_rev));
588 /* allocate Tx and Rx rings */
590 for (i = 0; i < sc->usb_endpoints - 1; i++)
592 error = rt2870_alloc_tx_ring(sc, &sc->tx_ring[i], i);
593 if (error != 0)
595 printf("%s: could not allocate Tx ring #%d\n",
596 device_get_nameunit(sc->dev), i);
597 goto fail;
601 if (sc->usb_endpoints == RT2870_SOFTC_USB_XFER_COUNT)
602 sc->tx_ring_mgtqid = 5;
603 else
604 sc->tx_ring_mgtqid = 0;
606 error = rt2870_alloc_rx_ring(sc, &sc->rx_ring);
607 if (error != 0)
609 printf("%s: could not allocate Rx ring\n",
610 device_get_nameunit(sc->dev));
611 goto fail;
614 callout_init(&sc->periodic_ch, 0);
615 callout_init_mtx(&sc->tx_watchdog_ch, &sc->lock, 0);
617 ifp = sc->ifp = if_alloc(IFT_IEEE80211);
618 if (ifp == NULL)
620 printf("%s: could not if_alloc()\n",
621 device_get_nameunit(sc->dev));
622 error = ENOMEM;
623 goto fail;
626 ifp->if_softc = sc;
628 if_initname(ifp, "rt2870", device_get_unit(sc->dev));
630 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
632 ifp->if_init = rt2870_init;
633 ifp->if_ioctl = rt2870_ioctl;
634 ifp->if_start = rt2870_start;
636 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
637 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
638 IFQ_SET_READY(&ifp->if_snd);
640 ic = ifp->if_l2com;
642 ic->ic_ifp = ifp;
644 ic->ic_phytype = IEEE80211_T_HT;
645 ic->ic_opmode = IEEE80211_M_STA;
647 ic->ic_caps = IEEE80211_C_MONITOR |
648 IEEE80211_C_IBSS |
649 IEEE80211_C_STA |
650 IEEE80211_C_AHDEMO |
651 IEEE80211_C_HOSTAP |
652 IEEE80211_C_WDS |
653 IEEE80211_C_MBSS |
654 IEEE80211_C_BGSCAN |
655 IEEE80211_C_TXPMGT |
656 IEEE80211_C_SHPREAMBLE |
657 IEEE80211_C_SHSLOT |
658 IEEE80211_C_TXFRAG |
659 IEEE80211_C_BURST |
660 IEEE80211_C_WME |
661 IEEE80211_C_WPA;
663 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP |
664 IEEE80211_CRYPTO_TKIP |
665 IEEE80211_CRYPTO_TKIPMIC |
666 IEEE80211_CRYPTO_AES_CCM;
668 ic->ic_htcaps = IEEE80211_HTC_HT |
669 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
670 IEEE80211_HTC_AMPDU | /* A-MPDU Tx */
671 IEEE80211_HTC_SMPS | /* MIMO power save */
672 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
673 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
674 IEEE80211_HTCAP_GREENFIELD | /* HT greenfield */
675 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
676 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
677 IEEE80211_HTCAP_SMPS_OFF; /* MIMO power save disabled */
679 /* spatial streams */
681 if (sc->nrxpath == 2)
682 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
683 else if (sc->nrxpath == 3)
684 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
685 else
686 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
688 if (sc->ntxpath > 1)
689 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
691 /* delayed BA */
693 if (sc->mac_rev != 0x28600100)
694 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
696 /* init channels */
698 ic->ic_nchans = 0;
700 rt2870_init_channels(sc);
702 rt2870_init_channels_ht40(sc);
704 ieee80211_ifattach(ic, sc->mac_addr);
706 ic->ic_vap_create = rt2870_vap_create;
707 ic->ic_vap_delete = rt2870_vap_delete;
709 ic->ic_node_alloc = rt2870_node_alloc;
710 ic->ic_node_getmimoinfo = rt2870_node_getmimoinfo;
711 ic->ic_setregdomain = rt2870_setregdomain;
712 ic->ic_getradiocaps = rt2870_getradiocaps;
713 ic->ic_scan_start = rt2870_scan_start;
714 ic->ic_scan_end = rt2870_scan_end;
715 ic->ic_set_channel = rt2870_set_channel;
716 ic->ic_newassoc = rt2870_newassoc;
717 ic->ic_updateslot = rt2870_updateslot;
718 ic->ic_update_promisc = rt2870_update_promisc;
719 ic->ic_update_mcast = rt2870_update_mcast;
720 ic->ic_wme.wme_update = rt2870_wme_update;
721 ic->ic_raw_xmit = rt2870_raw_xmit;
723 sc->recv_action = ic->ic_recv_action;
724 ic->ic_recv_action = rt2870_recv_action;
726 sc->send_action = ic->ic_send_action;
727 ic->ic_send_action = rt2870_send_action;
729 /* hardware requires padding between 802.11 frame header and body */
731 ic->ic_flags |= IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
733 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
735 ieee80211_radiotap_attach(ic,
736 &sc->txtap.ihdr, sizeof(sc->txtap),
737 RT2870_SOFTC_TX_RADIOTAP_PRESENT,
738 &sc->rxtap.ihdr, sizeof(sc->rxtap),
739 RT2870_SOFTC_RX_RADIOTAP_PRESENT);
741 /* init task queue */
743 TASK_INIT(&sc->rx_done_task, 0, rt2870_rx_done_task, sc);
744 TASK_INIT(&sc->tx_done_task, 0, rt2870_tx_done_task, sc);
745 TASK_INIT(&sc->periodic_task, 0, rt2870_periodic_task, sc);
746 TASK_INIT(&sc->cmd_task, 0, rt2870_cmd_task, sc);
748 sc->rx_process_limit = 100;
750 sc->taskqueue = taskqueue_create("rt2870_taskq", M_NOWAIT,
751 taskqueue_thread_enqueue, &sc->taskqueue);
753 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
754 device_get_nameunit(sc->dev));
756 rt2870_reset_cmd_ring(sc, &sc->cmd_ring);
758 rt2870_sysctl_attach(sc);
760 if (bootverbose)
761 ieee80211_announce(ic);
763 return 0;
765 fail:
767 /* stop all USB transfers */
769 usbd_transfer_unsetup(sc->usb_xfer, sc->usb_endpoints);
771 /* free Tx and Rx rings */
773 for (i = 0; i < sc->usb_endpoints - 1; i++)
774 rt2870_free_tx_ring(sc, &sc->tx_ring[i]);
776 rt2870_free_rx_ring(sc, &sc->rx_ring);
778 mtx_destroy(&sc->lock);
780 return error;
784 * rt2870_detach
786 static int rt2870_detach(device_t dev)
788 struct rt2870_softc *sc;
789 struct ieee80211com *ic;
790 struct ifnet *ifp;
791 int i;
793 if (!device_is_attached(dev))
794 return 0;
796 sc = device_get_softc(dev);
797 ifp = sc->ifp;
798 ic = ifp->if_l2com;
800 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
801 "%s: detaching\n",
802 device_get_nameunit(sc->dev));
804 RT2870_SOFTC_LOCK(sc);
806 sc->tx_timer = 0;
808 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
810 callout_stop(&sc->periodic_ch);
811 callout_stop(&sc->tx_watchdog_ch);
813 RT2870_SOFTC_UNLOCK(sc);
815 taskqueue_block(sc->taskqueue);
817 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
818 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
819 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
820 taskqueue_drain(sc->taskqueue, &sc->cmd_task);
822 /* stop all USB transfers */
824 usbd_transfer_unsetup(sc->usb_xfer, sc->usb_endpoints);
826 RT2870_SOFTC_LOCK(sc);
828 /* free Tx and Rx rings */
830 for (i = 0; i < sc->usb_endpoints - 1; i++)
831 rt2870_free_tx_ring(sc, &sc->tx_ring[i]);
833 rt2870_free_rx_ring(sc, &sc->rx_ring);
835 RT2870_SOFTC_UNLOCK(sc);
837 ieee80211_ifdetach(ic);
839 if_free(ifp);
841 taskqueue_free(sc->taskqueue);
843 mtx_destroy(&sc->lock);
845 return 0;
849 * rt2870_init_channels
851 static void rt2870_init_channels(struct rt2870_softc *sc)
853 struct ifnet *ifp;
854 struct ieee80211com *ic;
855 struct ieee80211_channel *c;
856 int i, flags;
858 ifp = sc->ifp;
859 ic = ifp->if_l2com;
861 /* set supported channels for 2GHz band */
863 for (i = 1; i <= 14; i++)
865 c = &ic->ic_channels[ic->ic_nchans++];
866 flags = IEEE80211_CHAN_B;
868 c->ic_freq = ieee80211_ieee2mhz(i, flags);
869 c->ic_ieee = i;
870 c->ic_flags = flags;
872 c = &ic->ic_channels[ic->ic_nchans++];
873 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
875 c->ic_freq = ieee80211_ieee2mhz(i, flags);
876 c->ic_ieee = i;
877 c->ic_flags = flags;
879 c = &ic->ic_channels[ic->ic_nchans++];
880 flags = IEEE80211_CHAN_G;
882 c->ic_freq = ieee80211_ieee2mhz(i, flags);
883 c->ic_ieee = i;
884 c->ic_flags = flags;
886 c = &ic->ic_channels[ic->ic_nchans++];
887 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
889 c->ic_freq = ieee80211_ieee2mhz(i, flags);
890 c->ic_ieee = i;
891 c->ic_flags = flags;
894 /* set supported channels for 5GHz band */
896 if (sc->rf_rev == RT2870_EEPROM_RF_2850 ||
897 sc->rf_rev == RT2870_EEPROM_RF_2750)
899 for (i = 36; i <= 64; i += 4)
901 c = &ic->ic_channels[ic->ic_nchans++];
902 flags = IEEE80211_CHAN_A;
904 c->ic_freq = ieee80211_ieee2mhz(i, flags);
905 c->ic_ieee = i;
906 c->ic_flags = flags;
908 c = &ic->ic_channels[ic->ic_nchans++];
909 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
911 c->ic_freq = ieee80211_ieee2mhz(i, flags);
912 c->ic_ieee = i;
913 c->ic_flags = flags;
916 for (i = 100; i <= 140; i += 4)
918 c = &ic->ic_channels[ic->ic_nchans++];
919 flags = IEEE80211_CHAN_A;
921 c->ic_freq = ieee80211_ieee2mhz(i, flags);
922 c->ic_ieee = i;
923 c->ic_flags = flags;
925 c = &ic->ic_channels[ic->ic_nchans++];
926 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
928 c->ic_freq = ieee80211_ieee2mhz(i, flags);
929 c->ic_ieee = i;
930 c->ic_flags = flags;
933 for (i = 149; i <= 165; i += 4)
935 c = &ic->ic_channels[ic->ic_nchans++];
936 flags = IEEE80211_CHAN_A;
938 c->ic_freq = ieee80211_ieee2mhz(i, flags);
939 c->ic_ieee = i;
940 c->ic_flags = flags;
942 c = &ic->ic_channels[ic->ic_nchans++];
943 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
945 c->ic_freq = ieee80211_ieee2mhz(i, flags);
946 c->ic_ieee = i;
947 c->ic_flags = flags;
953 * rt2870_init_channels_ht40
955 static void rt2870_init_channels_ht40(struct rt2870_softc *sc)
957 struct ifnet *ifp;
958 struct ieee80211com *ic;
959 struct ieee80211_channel *c, *cent, *ext;
960 int i, flags;
962 ifp = sc->ifp;
963 ic = ifp->if_l2com;
965 /* set supported channels for 2GHz band */
967 for (i = 1; i <= 14; i++)
969 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
971 /* find the center channel */
973 cent = ieee80211_find_channel_byieee(ic, i,
974 flags & ~IEEE80211_CHAN_HT);
975 if (cent == NULL)
977 printf("%s: skip channel %d, could not find center channel\n",
978 device_get_nameunit(sc->dev), i);
979 continue;
982 /* find the extension channel */
984 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
985 flags & ~IEEE80211_CHAN_HT);
986 if (ext == NULL)
988 printf("%s: skip channel %d, could not find extension channel\n",
989 device_get_nameunit(sc->dev), i);
990 continue;
993 c = &ic->ic_channels[ic->ic_nchans++];
995 *c = *cent;
996 c->ic_extieee = ext->ic_ieee;
997 c->ic_flags &= ~IEEE80211_CHAN_HT;
998 c->ic_flags |= IEEE80211_CHAN_HT40U;
1000 c = &ic->ic_channels[ic->ic_nchans++];
1002 *c = *ext;
1003 c->ic_extieee = cent->ic_ieee;
1004 c->ic_flags &= ~IEEE80211_CHAN_HT;
1005 c->ic_flags |= IEEE80211_CHAN_HT40D;
1008 /* set supported channels for 5GHz band */
1010 if (sc->rf_rev == RT2870_EEPROM_RF_2850 ||
1011 sc->rf_rev == RT2870_EEPROM_RF_2750)
1013 for (i = 36; i <= 64; i += 4)
1015 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1017 /* find the center channel */
1019 cent = ieee80211_find_channel_byieee(ic, i,
1020 flags & ~IEEE80211_CHAN_HT);
1021 if (cent == NULL)
1023 printf("%s: skip channel %d, could not find center channel\n",
1024 device_get_nameunit(sc->dev), i);
1025 continue;
1028 /* find the extension channel */
1030 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1031 flags & ~IEEE80211_CHAN_HT);
1032 if (ext == NULL)
1034 printf("%s: skip channel %d, could not find extension channel\n",
1035 device_get_nameunit(sc->dev), i);
1036 continue;
1039 c = &ic->ic_channels[ic->ic_nchans++];
1041 *c = *cent;
1042 c->ic_extieee = ext->ic_ieee;
1043 c->ic_flags &= ~IEEE80211_CHAN_HT;
1044 c->ic_flags |= IEEE80211_CHAN_HT40U;
1046 c = &ic->ic_channels[ic->ic_nchans++];
1048 *c = *ext;
1049 c->ic_extieee = cent->ic_ieee;
1050 c->ic_flags &= ~IEEE80211_CHAN_HT;
1051 c->ic_flags |= IEEE80211_CHAN_HT40D;
1054 for (i = 100; i <= 140; i += 4)
1056 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1058 /* find the center channel */
1060 cent = ieee80211_find_channel_byieee(ic, i,
1061 flags & ~IEEE80211_CHAN_HT);
1062 if (cent == NULL)
1064 printf("%s: skip channel %d, could not find center channel\n",
1065 device_get_nameunit(sc->dev), i);
1066 continue;
1069 /* find the extension channel */
1071 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1072 flags & ~IEEE80211_CHAN_HT);
1073 if (ext == NULL)
1075 printf("%s: skip channel %d, could not find extension channel\n",
1076 device_get_nameunit(sc->dev), i);
1077 continue;
1080 c = &ic->ic_channels[ic->ic_nchans++];
1082 *c = *cent;
1083 c->ic_extieee = ext->ic_ieee;
1084 c->ic_flags &= ~IEEE80211_CHAN_HT;
1085 c->ic_flags |= IEEE80211_CHAN_HT40U;
1087 c = &ic->ic_channels[ic->ic_nchans++];
1089 *c = *ext;
1090 c->ic_extieee = cent->ic_ieee;
1091 c->ic_flags &= ~IEEE80211_CHAN_HT;
1092 c->ic_flags |= IEEE80211_CHAN_HT40D;
1095 for (i = 149; i <= 165; i += 4)
1097 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1099 /* find the center channel */
1101 cent = ieee80211_find_channel_byieee(ic, i,
1102 flags & ~IEEE80211_CHAN_HT);
1103 if (cent == NULL)
1105 printf("%s: skip channel %d, could not find center channel\n",
1106 device_get_nameunit(sc->dev), i);
1107 continue;
1110 /* find the extension channel */
1112 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1113 flags & ~IEEE80211_CHAN_HT);
1114 if (ext == NULL)
1116 printf("%s: skip channel %d, could not find extension channel\n",
1117 device_get_nameunit(sc->dev), i);
1118 continue;
1121 c = &ic->ic_channels[ic->ic_nchans++];
1123 *c = *cent;
1124 c->ic_extieee = ext->ic_ieee;
1125 c->ic_flags &= ~IEEE80211_CHAN_HT;
1126 c->ic_flags |= IEEE80211_CHAN_HT40U;
1128 c = &ic->ic_channels[ic->ic_nchans++];
1130 *c = *ext;
1131 c->ic_extieee = cent->ic_ieee;
1132 c->ic_flags &= ~IEEE80211_CHAN_HT;
1133 c->ic_flags |= IEEE80211_CHAN_HT40D;
1139 * rt2870_init_locked
1141 static void rt2870_init_locked(void *priv)
1143 struct rt2870_softc *sc;
1144 struct ifnet *ifp;
1145 struct ieee80211com *ic;
1146 int ntries, error, i;
1147 uint32_t tmp, stacnt[6];
1149 sc = priv;
1150 ifp = sc->ifp;
1151 ic = ifp->if_l2com;
1153 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1154 "%s: initializing\n",
1155 device_get_nameunit(sc->dev));
1157 RT2870_SOFTC_ASSERT_LOCKED(sc);
1159 if (!(sc->flags & RT2870_SOFTC_FLAGS_UCODE_LOADED))
1161 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1162 "%s: loading 8051 microcode\n",
1163 device_get_nameunit(sc->dev));
1165 error = rt2870_io_mcu_load_ucode(sc, rt2870_ucode, sizeof(rt2870_ucode));
1166 if (error != 0)
1168 printf("%s: could not load 8051 microcode\n",
1169 device_get_nameunit(sc->dev));
1170 goto fail;
1173 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1174 "%s: 8051 microcode was successfully loaded\n",
1175 device_get_nameunit(sc->dev));
1177 sc->flags |= RT2870_SOFTC_FLAGS_UCODE_LOADED;
1180 /* wait while DMA engine is busy */
1182 for (ntries = 0; ntries < 100; ntries++)
1184 tmp = rt2870_io_mac_read(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG);
1185 if (!(tmp & (RT2870_REG_TX_DMA_BUSY | RT2870_REG_RX_DMA_BUSY)))
1186 break;
1188 DELAY(1000);
1191 if (ntries == 100)
1193 printf("%s: timeout waiting for DMA engine\n",
1194 device_get_nameunit(sc->dev));
1195 goto fail;
1198 tmp &= 0xff0;
1199 tmp |= RT2870_REG_TX_WB_DDONE;
1201 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1203 /* PBF hardware reset */
1205 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_SYS_CTRL);
1207 tmp &= ~(1 << 13);
1209 rt2870_io_mac_write(sc, RT2870_REG_PBF_SYS_CTRL, tmp);
1211 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL,
1212 RT2870_REG_MAC_SRST | RT2870_REG_BBP_HRST);
1214 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_DMA_CFG, 0);
1216 rt2870_io_mcu_reset(sc);
1218 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, 0);
1220 /* init Tx power per rate */
1222 for (i = 0; i < RT2870_SOFTC_TXPOW_RATE_COUNT; i++)
1224 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1225 continue;
1227 rt2870_io_mac_write(sc, RT2870_REG_TX_PWR_CFG(i),
1228 sc->txpow_rate_20mhz[i]);
1231 for (i = 0; i < RT2870_DEF_MAC_SIZE; i++)
1232 rt2870_io_mac_write(sc, rt2870_def_mac[i].reg,
1233 rt2870_def_mac[i].val);
1235 /* wait while MAC is busy */
1237 for (ntries = 0; ntries < 100; ntries++)
1239 if (!(rt2870_io_mac_read(sc, RT2870_REG_STATUS_CFG) &
1240 (RT2870_REG_STATUS_TX_BUSY | RT2870_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 rt2870_io_mac_write(sc, RT2870_REG_H2M_MAILBOX_BBP_AGENT, 0);
1256 rt2870_io_mac_write(sc, RT2870_REG_H2M_MAILBOX, 0);
1258 DELAY(1000);
1260 error = rt2870_init_bbp(sc);
1261 if (error != 0)
1262 goto fail;
1264 /* set up maximum buffer sizes */
1266 tmp = (1 << 12) | RT2870_MAX_AGG_SIZE;
1268 rt2870_io_mac_write(sc, RT2870_REG_MAX_LEN_CFG, tmp);
1270 if (sc->mac_rev == 0x28720200)
1272 /* set max. PSDU length from 16K to 32K bytes */
1274 tmp = rt2870_io_mac_read(sc, RT2870_REG_MAX_LEN_CFG);
1276 tmp &= ~(3 << 12);
1277 tmp |= (2 << 12);
1279 rt2870_io_mac_write(sc, RT2870_REG_MAX_LEN_CFG, tmp);
1282 if (sc->mac_rev >= 0x28720200 && sc->mac_rev < 0x30700200)
1284 tmp = rt2870_io_mac_read(sc, RT2870_REG_MAX_LEN_CFG);
1286 tmp &= 0xfff;
1287 tmp |= 0x2000;
1289 rt2870_io_mac_write(sc, RT2870_REG_MAX_LEN_CFG, tmp);
1292 /* set mac address */
1294 rt2870_asic_set_macaddr(sc, IF_LLADDR(ifp));
1296 /* clear statistic registers */
1298 rt2870_io_mac_read_multi(sc, RT2870_REG_RX_STA_CNT0,
1299 stacnt, sizeof(stacnt));
1301 /* send LEDs operating mode to microcontroller */
1303 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_LED1,
1304 RT2870_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1305 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_LED2,
1306 RT2870_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1307 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_LED3,
1308 RT2870_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1310 /* write vendor-specific BBP values (from EEPROM) */
1312 for (i = 0; i < RT2870_SOFTC_BBP_EEPROM_COUNT; i++)
1314 if (sc->bbp_eeprom[i].reg == 0x00 ||
1315 sc->bbp_eeprom[i].reg == 0xff)
1316 continue;
1318 rt2870_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1319 sc->bbp_eeprom[i].val);
1322 /* disable non-existing Rx chains */
1324 tmp = rt2870_io_bbp_read(sc, 3);
1326 tmp &= ~((1 << 4) | (1 << 3));
1328 if (sc->nrxpath == 3)
1329 tmp |= (1 << 4);
1330 else if (sc->nrxpath == 2)
1331 tmp |= (1 << 3);
1333 rt2870_io_bbp_write(sc, 3, tmp);
1335 /* disable non-existing Tx chains */
1337 tmp = rt2870_io_bbp_read(sc, 1);
1339 tmp &= ~((1 << 4) | (1 << 3));
1341 if (sc->ntxpath == 2)
1342 tmp |= (1 << 4);
1344 rt2870_io_bbp_write(sc, 1, tmp);
1346 /* set current channel */
1348 rt2870_rf_set_chan(sc, ic->ic_curchan);
1350 /* turn radio LED on */
1352 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_ON);
1354 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_BOOT,
1355 RT2870_REG_H2M_TOKEN_NO_INTR, 0);
1357 /* set RTS threshold */
1359 rt2870_asic_update_rtsthreshold(sc);
1361 /* set Tx power */
1363 rt2870_asic_update_txpower(sc);
1365 /* set up protection mode */
1367 sc->tx_ampdu_sessions = 0;
1369 rt2870_asic_updateprot(sc);
1371 /* clear beacon frame space (entries = 8, entry size = 512) */
1373 rt2870_io_mac_set_region_4(sc, RT2870_REG_BEACON_BASE(0), 0, 1024);
1375 if (rt2870_txrx_enable(sc) != 0)
1376 goto fail;
1378 /* clear garbage interrupts */
1380 tmp = rt2870_io_mac_read(sc, 0x1300);
1382 taskqueue_unblock(sc->taskqueue);
1384 /* init Tx and Rx rings */
1386 for(i = 0; i < sc->usb_endpoints - 1; i++)
1387 rt2870_reset_tx_ring(sc, &sc->tx_ring[i]);
1389 rt2870_reset_rx_ring(sc, &sc->rx_ring);
1391 for(i = 0; i < sc->usb_endpoints - 1; i++)
1393 usbd_xfer_set_priv(sc->usb_xfer[i + 1], &sc->tx_ring[i]);
1394 usbd_xfer_set_stall(sc->usb_xfer[i + 1]);
1397 /* start up the receive pipe */
1399 for(i = 0; i < RT2870_SOFTC_RX_RING_DATA_COUNT; i++)
1400 usbd_transfer_start(sc->usb_xfer[0]);
1402 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1403 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1405 sc->periodic_round = 0;
1407 callout_reset(&sc->periodic_ch, hz / 10, rt2870_periodic, sc);
1409 return;
1411 fail:
1413 rt2870_stop_locked(sc);
1417 * rt2870_init
1419 static void rt2870_init(void *priv)
1421 struct rt2870_softc *sc;
1423 sc = priv;
1425 RT2870_SOFTC_LOCK(sc);
1427 rt2870_init_locked(sc);
1429 RT2870_SOFTC_UNLOCK(sc);
1433 * rt2870_init_bbp
1435 static int rt2870_init_bbp(struct rt2870_softc *sc)
1437 int ntries, i;
1438 uint8_t tmp;
1440 for (ntries = 0; ntries < 20; ntries++)
1442 tmp = rt2870_io_bbp_read(sc, 0);
1443 if (tmp != 0x00 && tmp != 0xff)
1444 break;
1447 if (tmp == 0x00 || tmp == 0xff)
1449 printf("%s: timeout waiting for BBP to wakeup\n",
1450 device_get_nameunit(sc->dev));
1451 return ETIMEDOUT;
1454 for (i = 0; i < RT2870_DEF_BBP_SIZE; i++)
1455 rt2870_io_bbp_write(sc, rt2870_def_bbp[i].reg,
1456 rt2870_def_bbp[i].val);
1458 if ((sc->mac_rev & 0xffff) != 0x0101)
1459 rt2870_io_bbp_write(sc, 84, 0x19);
1461 if (sc->mac_rev == 0x28600100)
1463 rt2870_io_bbp_write(sc, 69, 0x16);
1464 rt2870_io_bbp_write(sc, 73, 0x12);
1467 return 0;
1471 * rt2870_stop_locked
1473 static void rt2870_stop_locked(void *priv)
1475 struct rt2870_softc *sc;
1476 struct ifnet *ifp;
1477 struct ieee80211com *ic;
1478 uint32_t tmp;
1479 int i;
1481 sc = priv;
1482 ifp = sc->ifp;
1483 ic = ifp->if_l2com;
1485 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1486 "%s: stopping\n",
1487 device_get_nameunit(sc->dev));
1489 RT2870_SOFTC_ASSERT_LOCKED(sc);
1491 sc->tx_timer = 0;
1493 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1494 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_OFF);
1496 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1498 callout_stop(&sc->periodic_ch);
1499 callout_stop(&sc->tx_watchdog_ch);
1501 RT2870_SOFTC_UNLOCK(sc);
1503 taskqueue_block(sc->taskqueue);
1505 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1506 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1507 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1508 taskqueue_drain(sc->taskqueue, &sc->cmd_task);
1510 RT2870_SOFTC_LOCK(sc);
1512 /* clear key tables */
1514 rt2870_asic_clear_keytables(sc);
1516 /* disable Tx/Rx */
1518 tmp = rt2870_io_mac_read(sc, RT2870_REG_SYS_CTRL);
1520 tmp &= ~(RT2870_REG_RX_ENABLE | RT2870_REG_TX_ENABLE);
1522 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, tmp);
1524 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_DMA_CFG, 0);
1526 /* reset adapter */
1528 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL,
1529 RT2870_REG_MAC_SRST | RT2870_REG_BBP_HRST);
1530 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, 0);
1532 /* abort any pending transfers */
1534 for (i = 0; i < sc->usb_endpoints; i++)
1535 usbd_transfer_stop(sc->usb_xfer[i]);
1539 * rt2870_stop
1541 static void rt2870_stop(void *priv)
1543 struct rt2870_softc *sc;
1545 sc = priv;
1547 RT2870_SOFTC_LOCK(sc);
1549 rt2870_stop_locked(sc);
1551 RT2870_SOFTC_UNLOCK(sc);
1555 * rt2870_start
1557 static void rt2870_start(struct ifnet *ifp)
1559 struct rt2870_softc *sc;
1560 struct ieee80211_node *ni;
1561 struct mbuf *m;
1562 int qid;
1564 sc = ifp->if_softc;
1566 RT2870_SOFTC_LOCK(sc);
1568 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1570 RT2870_SOFTC_UNLOCK(sc);
1571 return;
1574 for (;;)
1576 IF_POLL(&ifp->if_snd, m);
1577 if (m == NULL)
1578 break;
1580 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1582 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1584 qid = M_WME_GETAC(m);
1586 if (sc->tx_ring[qid].queued >= RT2870_SOFTC_TX_RING_DATA_COUNT)
1588 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
1589 "%s: if_start: Tx ring with qid=%d is full\n",
1590 device_get_nameunit(sc->dev), qid);
1592 m_freem(m);
1593 ieee80211_free_node(ni);
1595 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1596 ifp->if_oerrors++;
1598 sc->tx_data_queue_full[qid]++;
1600 break;
1603 if (rt2870_tx_data(sc, m, ni, qid) != 0)
1605 ieee80211_free_node(ni);
1606 ifp->if_oerrors++;
1607 break;
1610 sc->tx_timer = RT2870_TX_WATCHDOG_TIMEOUT;
1612 callout_reset(&sc->tx_watchdog_ch, hz, rt2870_tx_watchdog, sc);
1615 RT2870_SOFTC_UNLOCK(sc);
1619 * rt2870_ioctl
1621 static int rt2870_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1623 struct rt2870_softc *sc;
1624 struct ieee80211com *ic;
1625 struct ifreq *ifr;
1626 int error, startall;
1628 sc = ifp->if_softc;
1629 ic = ifp->if_l2com;
1630 ifr = (struct ifreq *) data;
1632 error = 0;
1634 switch (cmd)
1636 case SIOCSIFFLAGS:
1637 startall = 0;
1639 RT2870_SOFTC_LOCK(sc);
1641 if (ifp->if_flags & IFF_UP)
1643 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1645 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1646 rt2870_asic_update_promisc(sc);
1648 else
1650 rt2870_init_locked(sc);
1651 startall = 1;
1654 else
1656 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1657 rt2870_stop_locked(sc);
1660 sc->if_flags = ifp->if_flags;
1662 RT2870_SOFTC_UNLOCK(sc);
1664 if (startall)
1665 ieee80211_start_all(ic);
1666 break;
1668 case SIOCGIFMEDIA:
1669 case SIOCSIFMEDIA:
1670 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1671 break;
1673 case SIOCGIFADDR:
1674 error = ether_ioctl(ifp, cmd, data);
1675 break;
1677 default:
1678 error = EINVAL;
1679 break;
1682 return error;
1686 * rt2870_vap_create
1688 static struct ieee80211vap *rt2870_vap_create(struct ieee80211com *ic,
1689 const char name[IFNAMSIZ], int unit, int opmode, int flags,
1690 const uint8_t bssid[IEEE80211_ADDR_LEN],
1691 const uint8_t mac[IEEE80211_ADDR_LEN])
1693 struct rt2870_softc *sc;
1694 struct ifnet *ifp;
1695 struct rt2870_softc_vap *rvap;
1696 struct ieee80211vap *vap;
1698 ifp = ic->ic_ifp;
1699 sc = ifp->if_softc;
1701 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
1702 "%s: VAP create: opmode=%s\n",
1703 device_get_nameunit(sc->dev),
1704 ieee80211_opmode_name[opmode]);
1706 switch (opmode)
1708 case IEEE80211_M_MONITOR:
1709 case IEEE80211_M_IBSS:
1710 case IEEE80211_M_STA:
1711 case IEEE80211_M_AHDEMO:
1712 case IEEE80211_M_HOSTAP:
1713 case IEEE80211_M_MBSS:
1714 if (!TAILQ_EMPTY(&ic->ic_vaps))
1716 if_printf(ifp, "only 1 VAP supported\n");
1717 return NULL;
1720 if (opmode == IEEE80211_M_STA)
1721 flags |= IEEE80211_CLONE_NOBEACONS;
1722 break;
1724 case IEEE80211_M_WDS:
1725 if (TAILQ_EMPTY(&ic->ic_vaps) || ic->ic_opmode != IEEE80211_M_HOSTAP)
1727 if_printf(ifp, "WDS only supported in AP mode\n");
1728 return NULL;
1731 flags &= ~IEEE80211_CLONE_BSSID;
1732 break;
1734 default:
1735 if_printf(ifp, "unknown opmode %d\n", opmode);
1736 return NULL;
1739 rvap = (struct rt2870_softc_vap *) malloc(sizeof(struct rt2870_softc_vap),
1740 M_80211_VAP, M_NOWAIT | M_ZERO);
1741 if (rvap == NULL)
1742 return NULL;
1744 vap = &rvap->vap;
1746 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
1748 rvap->newstate = vap->iv_newstate;
1749 vap->iv_newstate = rt2870_vap_newstate;
1751 vap->iv_reset = rt2870_vap_reset;
1752 vap->iv_key_update_begin = rt2870_vap_key_update_begin;
1753 vap->iv_key_update_end = rt2870_vap_key_update_end;
1754 vap->iv_key_set = rt2870_vap_key_set;
1755 vap->iv_key_delete = rt2870_vap_key_delete;
1756 vap->iv_update_beacon = rt2870_vap_update_beacon;
1758 rt2870_amrr_init(&rvap->amrr, vap,
1759 sc->ntxpath,
1760 RT2870_AMRR_MIN_SUCCESS_THRESHOLD,
1761 RT2870_AMRR_MAX_SUCCESS_THRESHOLD,
1762 500);
1764 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
1766 /* override default Rx A-MPDU factor */
1768 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
1769 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
1770 vap->iv_ampdu_limit = vap->iv_ampdu_rxmax;
1772 ic->ic_opmode = opmode;
1774 return vap;
1778 * rt2870_vap_delete
1780 static void rt2870_vap_delete(struct ieee80211vap *vap)
1782 struct rt2870_softc *sc;
1783 struct ieee80211com *ic;
1784 struct ifnet *ifp;
1785 struct rt2870_softc_vap *rvap;
1787 ic = vap->iv_ic;
1788 ifp = ic->ic_ifp;
1789 sc = ifp->if_softc;
1790 rvap = (struct rt2870_softc_vap *) vap;
1792 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
1793 "%s: VAP delete: opmode=%s\n",
1794 device_get_nameunit(sc->dev),
1795 ieee80211_opmode_name[vap->iv_opmode]);
1797 rt2870_amrr_cleanup(&rvap->amrr);
1799 ieee80211_vap_detach(vap);
1801 if (rvap->beacon_mbuf != NULL)
1803 m_free(rvap->beacon_mbuf);
1804 rvap->beacon_mbuf = NULL;
1807 free(rvap, M_80211_VAP);
1811 * rt2870_reset_vap
1813 static int rt2870_vap_reset(struct ieee80211vap *vap, u_long cmd)
1815 struct rt2870_softc *sc;
1816 struct ieee80211com *ic;
1817 struct ifnet *ifp;
1818 struct rt2870_softc_vap *rvap;
1819 int error;
1821 ic = vap->iv_ic;
1822 ifp = ic->ic_ifp;
1823 sc = ifp->if_softc;
1824 rvap = (struct rt2870_softc_vap *) vap;
1826 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
1827 "%s: VAP reset: cmd=%lu\n",
1828 device_get_nameunit(sc->dev), cmd);
1830 error = 0;
1832 RT2870_SOFTC_LOCK(sc);
1834 switch (cmd)
1836 case IEEE80211_IOC_RTSTHRESHOLD:
1837 case IEEE80211_IOC_AMSDU:
1838 rt2870_asic_update_rtsthreshold(sc);
1839 break;
1841 case IEEE80211_IOC_PROTMODE:
1842 case IEEE80211_IOC_HTPROTMODE:
1843 rt2870_asic_updateprot(sc);
1844 break;
1846 case IEEE80211_IOC_TXPOWER:
1847 rt2870_asic_update_txpower(sc);
1848 break;
1850 case IEEE80211_IOC_BURST:
1851 rt2870_asic_updateslot(sc);
1852 break;
1854 case IEEE80211_IOC_SHORTGI:
1855 case IEEE80211_IOC_AMPDU_DENSITY:
1856 case IEEE80211_IOC_SMPS:
1857 break;
1859 default:
1860 error = ENETRESET;
1861 break;
1864 RT2870_SOFTC_UNLOCK(sc);
1866 return error;
1870 * rt2870_vap_newstate
1872 static int rt2870_vap_newstate(struct ieee80211vap *vap,
1873 enum ieee80211_state nstate, int arg)
1875 struct rt2870_softc *sc;
1876 struct ieee80211com *ic;
1877 struct ifnet *ifp;
1878 struct rt2870_softc_vap *rvap;
1879 struct ieee80211_node *ni;
1880 int error;
1882 ic = vap->iv_ic;
1883 ifp = ic->ic_ifp;
1884 sc = ifp->if_softc;
1885 rvap = (struct rt2870_softc_vap *) vap;
1887 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
1888 "%s: VAP newstate: %s -> %s\n",
1889 device_get_nameunit(sc->dev),
1890 ieee80211_state_name[vap->iv_state], ieee80211_state_name[nstate]);
1892 error = rvap->newstate(vap, nstate, arg);
1893 if (error != 0)
1894 return error;
1896 IEEE80211_UNLOCK(ic);
1898 RT2870_SOFTC_LOCK(sc);
1900 /* turn link LED off */
1902 if (nstate != IEEE80211_S_RUN)
1903 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_OFF);
1905 switch (nstate)
1907 case IEEE80211_S_INIT:
1908 rt2870_asic_disable_tsf_sync(sc);
1909 break;
1911 case IEEE80211_S_RUN:
1912 ni = vap->iv_bss;
1914 rt2870_rf_set_chan(sc, ni->ni_chan);
1916 if (vap->iv_opmode != IEEE80211_M_MONITOR)
1918 rt2870_asic_enable_mrr(sc);
1919 rt2870_asic_set_txpreamble(sc);
1920 rt2870_asic_set_basicrates(sc);
1921 rt2870_asic_update_txpower(sc);
1922 rt2870_asic_set_bssid(sc, ni->ni_bssid);
1925 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1926 vap->iv_opmode == IEEE80211_M_IBSS ||
1927 vap->iv_opmode == IEEE80211_M_MBSS)
1929 error = rt2870_beacon_alloc(sc, vap);
1930 if (error != 0)
1931 break;
1933 rt2870_asic_update_beacon(sc, vap);
1936 if (vap->iv_opmode != IEEE80211_M_MONITOR)
1937 rt2870_asic_enable_tsf_sync(sc);
1939 /* turn link LED on */
1941 if (vap->iv_opmode != IEEE80211_M_MONITOR)
1943 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_ON |
1944 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
1945 RT2870_LED_CMD_LINK_2GHZ : RT2870_LED_CMD_LINK_5GHZ));
1947 break;
1949 default:
1950 break;
1953 RT2870_SOFTC_UNLOCK(sc);
1955 IEEE80211_LOCK(ic);
1957 return error;
1961 * rt2870_vap_key_update_begin
1963 static void rt2870_vap_key_update_begin(struct ieee80211vap *vap)
1965 struct rt2870_softc *sc;
1966 struct ieee80211com *ic;
1967 struct ifnet *ifp;
1969 ic = vap->iv_ic;
1970 ifp = ic->ic_ifp;
1971 sc = ifp->if_softc;
1973 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
1974 "%s: VAP key update begin\n",
1975 device_get_nameunit(sc->dev));
1977 taskqueue_block(sc->taskqueue);
1979 IF_LOCK(&ifp->if_snd);
1983 * rt2870_vap_key_update_end
1985 static void rt2870_vap_key_update_end(struct ieee80211vap *vap)
1987 struct rt2870_softc *sc;
1988 struct ieee80211com *ic;
1989 struct ifnet *ifp;
1991 ic = vap->iv_ic;
1992 ifp = ic->ic_ifp;
1993 sc = ifp->if_softc;
1995 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
1996 "%s: VAP key update end\n",
1997 device_get_nameunit(sc->dev));
1999 IF_UNLOCK(&ifp->if_snd);
2001 taskqueue_unblock(sc->taskqueue);
2005 * rt2870_vap_key_set
2007 static int rt2870_vap_key_set(struct ieee80211vap *vap,
2008 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2010 struct rt2870_softc *sc;
2011 struct ieee80211com *ic;
2012 struct ifnet *ifp;
2013 struct ieee80211_node *ni;
2014 struct rt2870_cmd_argv_keyset cmd_argv;
2015 uint16_t associd;
2016 uint8_t wcid;
2018 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2019 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2020 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2021 return 0;
2023 ic = vap->iv_ic;
2024 ifp = ic->ic_ifp;
2025 sc = ifp->if_softc;
2026 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
2027 associd = (ni != NULL) ? ni->ni_associd : 0;
2029 if (ni != NULL)
2030 ieee80211_free_node(ni);
2032 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2034 wcid = RT2870_AID2WCID(associd);
2035 if (wcid == 0)
2036 return 0;
2039 memcpy(&cmd_argv.key, k, sizeof(struct ieee80211_key));
2040 cmd_argv.associd = associd;
2042 rt2870_do_async(sc, rt2870_vap_key_set_cb,
2043 &cmd_argv, sizeof(struct rt2870_cmd_argv_keyset));
2045 return 1;
2049 * rt2870_vap_key_delete
2051 static int rt2870_vap_key_delete(struct ieee80211vap *vap,
2052 const struct ieee80211_key *k)
2054 struct rt2870_softc *sc;
2055 struct ieee80211com *ic;
2056 struct ifnet *ifp;
2057 struct ieee80211_node *ni;
2058 struct rt2870_cmd_argv_keydelete cmd_argv;
2059 uint16_t associd;
2060 uint8_t wcid;
2062 ic = vap->iv_ic;
2063 ifp = ic->ic_ifp;
2064 sc = ifp->if_softc;
2065 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
2066 associd = (ni != NULL) ? ni->ni_associd : 0;
2068 if (ni != NULL)
2069 ieee80211_free_node(ni);
2071 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2073 wcid = RT2870_AID2WCID(associd);
2074 if (wcid == 0)
2075 return 0;
2078 memcpy(&cmd_argv.key, k, sizeof(struct ieee80211_key));
2079 cmd_argv.associd = associd;
2081 rt2870_do_async(sc, rt2870_vap_key_delete_cb,
2082 &cmd_argv, sizeof(struct rt2870_cmd_argv_keydelete));
2084 return 1;
2088 * rt2870_vap_update_beacon
2090 static void rt2870_vap_update_beacon(struct ieee80211vap *vap, int what)
2092 struct rt2870_softc *sc;
2093 struct ieee80211com *ic;
2094 struct ifnet *ifp;
2095 struct rt2870_softc_vap *rvap;
2096 struct mbuf *m;
2097 struct ieee80211_beacon_offsets *bo;
2099 ic = vap->iv_ic;
2100 ifp = ic->ic_ifp;
2101 sc = ifp->if_softc;
2102 rvap = (struct rt2870_softc_vap *) vap;
2103 m = rvap->beacon_mbuf;
2104 bo = &rvap->beacon_offsets;
2106 RT2870_DPRINTF(sc, RT2870_DEBUG_BEACON,
2107 "%s: VAP update beacon: what=%d\n",
2108 device_get_nameunit(sc->dev), what);
2110 setbit(bo->bo_flags, what);
2112 ieee80211_beacon_update(vap->iv_bss, bo, m, 0);
2114 rt2870_do_async(sc, rt2870_vap_update_beacon_cb, NULL, 0);
2118 * rt2870_node_alloc
2120 static struct ieee80211_node *rt2870_node_alloc(struct ieee80211vap *vap,
2121 const uint8_t mac[IEEE80211_ADDR_LEN])
2123 return malloc(sizeof(struct rt2870_softc_node),
2124 M_80211_NODE, M_NOWAIT | M_ZERO);
2128 * rt2870_node_getmimoinfo
2130 static void rt2870_node_getmimoinfo(const struct ieee80211_node *ni,
2131 struct ieee80211_mimo_info *mi)
2133 const struct rt2870_softc_node *rni;
2134 int i;
2136 rni = (const struct rt2870_softc_node *) ni;
2138 for (i = 0; i < RT2870_SOFTC_RSSI_COUNT; i++)
2140 mi->rssi[i] = rni->last_rssi_dbm[i];
2141 mi->noise[i] = RT2870_NOISE_FLOOR;
2146 * rt2870_setregdomain
2148 static int rt2870_setregdomain(struct ieee80211com *ic,
2149 struct ieee80211_regdomain *reg,
2150 int nchans, struct ieee80211_channel chans[])
2152 struct rt2870_softc *sc;
2153 struct ifnet *ifp;
2155 ifp = ic->ic_ifp;
2156 sc = ifp->if_softc;
2158 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
2159 "%s: set regulatory domain: country=%d, country code string=%c%c, location=%c\n",
2160 device_get_nameunit(sc->dev),
2161 reg->country, reg->isocc[0], reg->isocc[1], reg->location);
2163 return 0;
2167 * rt2870_getradiocaps
2169 static void rt2870_getradiocaps(struct ieee80211com *ic,
2170 int maxchans, int *nchans, struct ieee80211_channel chans[])
2172 *nchans = (ic->ic_nchans >= maxchans) ? maxchans : ic->ic_nchans;
2174 memcpy(chans, ic->ic_channels, (*nchans) * sizeof(struct ieee80211_channel));
2178 * rt2870_scan_start
2180 static void rt2870_scan_start(struct ieee80211com *ic)
2182 struct rt2870_softc *sc;
2183 struct ifnet *ifp;
2185 ifp = ic->ic_ifp;
2186 sc = ifp->if_softc;
2188 RT2870_SOFTC_LOCK(sc);
2190 rt2870_asic_disable_tsf_sync(sc);
2192 RT2870_SOFTC_UNLOCK(sc);
2196 * rt2870_scan_end
2198 static void rt2870_scan_end(struct ieee80211com *ic)
2200 struct rt2870_softc *sc;
2201 struct ifnet *ifp;
2203 ifp = ic->ic_ifp;
2204 sc = ifp->if_softc;
2206 RT2870_SOFTC_LOCK(sc);
2208 rt2870_asic_enable_tsf_sync(sc);
2210 RT2870_SOFTC_UNLOCK(sc);
2214 * rt2870_set_channel
2216 static void rt2870_set_channel(struct ieee80211com *ic)
2218 struct rt2870_softc *sc;
2219 struct ifnet *ifp;
2221 ifp = ic->ic_ifp;
2222 sc = ifp->if_softc;
2224 RT2870_DPRINTF(sc, RT2870_DEBUG_CHAN,
2225 "%s: set channel: channel=%u, HT%s%s\n",
2226 device_get_nameunit(sc->dev),
2227 ieee80211_chan2ieee(ic, ic->ic_curchan),
2228 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
2229 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
2230 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
2231 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
2233 RT2870_SOFTC_LOCK(sc);
2235 rt2870_rf_set_chan(sc, ic->ic_curchan);
2237 RT2870_SOFTC_UNLOCK(sc);
2241 * rt2870_newassoc
2243 static void rt2870_newassoc(struct ieee80211_node *ni, int isnew)
2245 struct rt2870_softc *sc;
2246 struct ieee80211com *ic;
2247 struct ifnet *ifp;
2248 struct ieee80211vap *vap;
2249 struct rt2870_softc_vap *rvap;
2250 uint8_t wcid;
2252 vap = ni->ni_vap;
2253 ic = vap->iv_ic;
2254 ifp = ic->ic_ifp;
2255 sc = ifp->if_softc;
2256 rvap = (struct rt2870_softc_vap *) vap;
2258 RT2870_DPRINTF(sc, RT2870_DEBUG_NODE,
2259 "%s: new association: associd=0x%04x, "
2260 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2261 device_get_nameunit(sc->dev), ni->ni_associd,
2262 ether_sprintf(ni->ni_macaddr),
2263 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
2264 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
2265 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
2267 wcid = RT2870_AID2WCID(ni->ni_associd);
2268 if (wcid == 0)
2269 return;
2271 RT2870_SOFTC_LOCK(sc);
2273 rt2870_io_mac_write_multi(sc, RT2870_REG_WCID(wcid),
2274 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2276 rt2870_amrr_node_init(&rvap->amrr, &sc->amrr_node[wcid], ni);
2278 RT2870_DPRINTF(sc, RT2870_DEBUG_RATE,
2279 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2280 device_get_nameunit(sc->dev),
2281 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2282 ni->ni_associd, ni->ni_txrate,
2283 (ni->ni_flags & IEEE80211_NODE_HT) ?
2284 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2285 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2287 rt2870_asic_updateprot(sc);
2288 rt2870_asic_updateslot(sc);
2289 rt2870_asic_set_txpreamble(sc);
2291 RT2870_SOFTC_UNLOCK(sc);
2295 * rt2870_updateslot
2297 static void rt2870_updateslot(struct ifnet *ifp)
2299 struct rt2870_softc *sc;
2301 sc = ifp->if_softc;
2303 rt2870_do_async(sc, rt2870_updateslot_cb, NULL, 0);
2307 * rt2870_update_promisc
2309 static void rt2870_update_promisc(struct ifnet *ifp)
2311 struct rt2870_softc *sc;
2313 sc = ifp->if_softc;
2315 RT2870_SOFTC_LOCK(sc);
2317 rt2870_asic_update_promisc(sc);
2319 RT2870_SOFTC_UNLOCK(sc);
2323 * rt2870_update_mcast
2325 static void rt2870_update_mcast(struct ifnet *ifp)
2327 struct rt2870_softc *sc;
2329 sc = ifp->if_softc;
2333 * rt2870_wme_update
2335 static int rt2870_wme_update(struct ieee80211com *ic)
2337 struct rt2870_softc *sc;
2338 struct ifnet *ifp;
2340 ifp = ic->ic_ifp;
2341 sc = ifp->if_softc;
2343 rt2870_do_async(sc, rt2870_wme_update_cb, NULL, 0);
2345 return 0;
2349 * rt2870_raw_xmit
2351 static int rt2870_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2352 const struct ieee80211_bpf_params *params)
2354 struct rt2870_softc *sc;
2355 struct ieee80211com *ic;
2356 struct ifnet *ifp;
2358 ic = ni->ni_ic;
2359 ifp = ic->ic_ifp;
2360 sc = ifp->if_softc;
2362 RT2870_SOFTC_LOCK(sc);
2364 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2366 RT2870_SOFTC_UNLOCK(sc);
2368 m_freem(m);
2369 ieee80211_free_node(ni);
2371 return ENETDOWN;
2374 if (sc->tx_ring[sc->tx_ring_mgtqid].queued >= RT2870_SOFTC_TX_RING_DATA_COUNT)
2376 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
2377 "%s: raw xmit: Tx ring with qid=%d is full\n",
2378 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
2380 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2382 RT2870_SOFTC_UNLOCK(sc);
2384 m_freem(m);
2385 ieee80211_free_node(ni);
2387 sc->tx_data_queue_full[sc->tx_ring_mgtqid]++;
2389 return ENOBUFS;
2392 if (rt2870_tx_mgmt(sc, m, ni, sc->tx_ring_mgtqid) != 0)
2394 ifp->if_oerrors++;
2396 RT2870_SOFTC_UNLOCK(sc);
2398 ieee80211_free_node(ni);
2400 return EIO;
2403 sc->tx_timer = RT2870_TX_WATCHDOG_TIMEOUT;
2405 RT2870_SOFTC_UNLOCK(sc);
2407 return 0;
2411 * rt2870_recv_action
2413 static int rt2870_recv_action(struct ieee80211_node *ni,
2414 const struct ieee80211_frame *wh,
2415 const uint8_t *frm, const uint8_t *efrm)
2417 struct rt2870_softc *sc;
2418 struct ieee80211com *ic;
2419 struct ifnet *ifp;
2420 const struct ieee80211_action *ia;
2421 struct ieee80211_tx_ampdu *tx_ampdu;
2422 uint16_t associd, status, baparamset;
2423 uint8_t wcid;
2424 int ret, tid, bufsize, ac;
2425 uint32_t tmp;
2427 ic = ni->ni_ic;
2428 ifp = ic->ic_ifp;
2429 sc = ifp->if_softc;
2431 ia = (const struct ieee80211_action *) frm;
2433 ret = sc->recv_action(ni, wh, frm, efrm);
2435 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2436 return ret;
2438 associd = (ni != NULL) ? ni->ni_associd : 0;
2439 wcid = RT2870_AID2WCID(associd);
2441 switch (ia->ia_action)
2443 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2444 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2445 baparamset = LE_READ_2(frm + 3);
2446 tid = RT2870_MS(baparamset, IEEE80211_BAPS_TID);
2447 bufsize = RT2870_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2449 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2450 "%s: received ADDBA request: associd=0x%04x, tid=%d, bufsize=%d\n",
2451 device_get_nameunit(sc->dev), associd, tid, bufsize);
2453 RT2870_SOFTC_LOCK(sc);
2455 tmp = rt2870_io_mac_read(sc, RT2870_REG_WCID(wcid) + 4);
2457 tmp |= (0x10000 << tid);
2459 rt2870_io_mac_write(sc, RT2870_REG_WCID(wcid) + 4, tmp);
2461 RT2870_SOFTC_UNLOCK(sc);
2462 break;
2464 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2465 case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
2466 status = LE_READ_2(frm + 3);
2467 baparamset = LE_READ_2(frm + 4);
2468 tid = RT2870_MS(baparamset, IEEE80211_BAPS_TID);
2469 bufsize = RT2870_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2470 ac = TID_TO_WME_AC(tid);
2471 tx_ampdu = &ni->ni_tx_ampdu[ac];
2473 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2474 "%s: received ADDBA response: associd=0x%04x, status=%d, tid=%d, bufsize=%d\n",
2475 device_get_nameunit(sc->dev), associd, status, tid, bufsize);
2477 if (sc->mac_rev >= 0x28720200)
2479 if (tx_ampdu->txa_wnd > 13)
2480 tx_ampdu->txa_wnd = 13;
2482 else
2484 if (tx_ampdu->txa_wnd > 7)
2485 tx_ampdu->txa_wnd = 7;
2488 if (status == IEEE80211_STATUS_SUCCESS)
2490 sc->tx_ampdu_sessions++;
2492 RT2870_SOFTC_LOCK(sc);
2494 rt2870_asic_updateprot(sc);
2496 RT2870_SOFTC_UNLOCK(sc);
2498 break;
2500 /* IEEE80211_ACTION_BA_DELBA */
2501 case IEEE80211_ACTION_BA_DELBA:
2502 baparamset = LE_READ_2(frm + 2);
2503 tid = RT2870_MS(baparamset, IEEE80211_BAPS_TID);
2505 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2506 "%s: received DELBA request: associd=0x%04x, tid=%d\n",
2507 device_get_nameunit(sc->dev), associd, tid);
2509 RT2870_SOFTC_LOCK(sc);
2511 tmp = rt2870_io_mac_read(sc, RT2870_REG_WCID(wcid) + 4);
2513 tmp &= ~(0x10000 << tid);
2515 rt2870_io_mac_write(sc, RT2870_REG_WCID(wcid) + 4, tmp);
2517 RT2870_SOFTC_UNLOCK(sc);
2518 break;
2521 return ret;
2525 * rt2870_send_action
2527 static int rt2870_send_action(struct ieee80211_node *ni,
2528 int cat, int act, void *sa)
2530 struct rt2870_softc *sc;
2531 struct ieee80211com *ic;
2532 struct ifnet *ifp;
2533 uint16_t associd, *args, baparamset;
2534 uint8_t wcid;
2535 int ret, tid;
2536 uint32_t tmp;
2538 ic = ni->ni_ic;
2539 ifp = ic->ic_ifp;
2540 sc = ifp->if_softc;
2542 ret = sc->send_action(ni, cat, act, sa);
2544 if (cat != IEEE80211_ACTION_CAT_BA)
2545 return ret;
2547 associd = (ni != NULL) ? ni->ni_associd : 0;
2548 wcid = RT2870_AID2WCID(associd);
2549 args = sa;
2551 switch (act)
2553 /* IEEE80211_ACTION_BA_DELBA */
2554 case IEEE80211_ACTION_BA_DELBA:
2555 baparamset = RT2870_SM(args[0], IEEE80211_DELBAPS_TID) | args[1];
2557 tid = RT2870_MS(baparamset, IEEE80211_DELBAPS_TID);
2559 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2560 "%s: sending DELBA request: associd=0x%04x, tid=%d\n",
2561 device_get_nameunit(sc->dev), associd, tid);
2563 RT2870_SOFTC_LOCK(sc);
2565 if (RT2870_MS(baparamset, IEEE80211_DELBAPS_INIT) != IEEE80211_DELBAPS_INIT)
2567 tmp = rt2870_io_mac_read(sc, RT2870_REG_WCID(wcid) + 4);
2569 tmp &= ~(0x10000 << tid);
2571 rt2870_io_mac_write(sc, RT2870_REG_WCID(wcid) + 4, tmp);
2573 else
2575 sc->tx_ampdu_sessions--;
2577 rt2870_asic_updateprot(sc);
2580 RT2870_SOFTC_UNLOCK(sc);
2581 break;
2584 return ret;
2588 * rt2870_amrr_update_iter_func
2590 static void rt2870_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2592 struct rt2870_softc *sc;
2593 struct ieee80211com *ic;
2594 struct ifnet *ifp;
2595 struct ieee80211vap *vap;
2596 struct rt2870_softc_vap *rvap;
2597 uint8_t wcid;
2599 vap = arg;
2600 ic = vap->iv_ic;
2601 ifp = ic->ic_ifp;
2602 sc = ifp->if_softc;
2603 rvap = (struct rt2870_softc_vap *) vap;
2605 /* only associated stations */
2607 if ((ni->ni_vap == vap) && (ni->ni_associd != 0))
2609 wcid = RT2870_AID2WCID(ni->ni_associd);
2611 RT2870_DPRINTF(sc, RT2870_DEBUG_RATE,
2612 "%s: AMRR node: wcid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2613 device_get_nameunit(sc->dev),
2614 wcid, sc->amrr_node[wcid].txcnt, sc->amrr_node[wcid].success, sc->amrr_node[wcid].retrycnt);
2616 rt2870_amrr_choose(ni, &sc->amrr_node[wcid]);
2618 RT2870_DPRINTF(sc, RT2870_DEBUG_RATE,
2619 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2620 device_get_nameunit(sc->dev),
2621 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2622 ni->ni_associd, ni->ni_txrate,
2623 (ni->ni_flags & IEEE80211_NODE_HT) ?
2624 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2625 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2630 * rt2870_periodic
2632 static void rt2870_periodic(void *arg)
2634 struct rt2870_softc *sc;
2635 struct ifnet *ifp;
2637 sc = arg;
2638 ifp = sc->ifp;
2640 RT2870_DPRINTF(sc, RT2870_DEBUG_PERIODIC,
2641 "%s: periodic\n",
2642 device_get_nameunit(sc->dev));
2644 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2645 return;
2647 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2651 * rt2870_tx_watchdog
2653 static void rt2870_tx_watchdog(void *arg)
2655 struct rt2870_softc *sc;
2656 struct ifnet *ifp;
2658 sc = arg;
2659 ifp = sc->ifp;
2661 if (sc->tx_timer == 0)
2662 return;
2664 if (--sc->tx_timer == 0)
2666 printf("%s: Tx watchdog timeout: resetting\n",
2667 device_get_nameunit(sc->dev));
2669 rt2870_stop_locked(sc);
2670 rt2870_init_locked(sc);
2672 ifp->if_oerrors++;
2674 sc->tx_watchdog_timeouts++;
2677 callout_reset(&sc->tx_watchdog_ch, hz, rt2870_tx_watchdog, sc);
2681 * rt2870_do_async
2683 static int rt2870_do_async(struct rt2870_softc *sc,
2684 void (*cb)(struct rt2870_softc *sc, void *arg),
2685 void *arg, int len)
2687 struct ifnet *ifp;
2688 struct rt2870_softc_cmd_ring *ring;
2689 struct rt2870_softc_cmd *cmd;
2690 int run_cmd_task;
2692 ifp = sc->ifp;
2693 ring = &sc->cmd_ring;
2695 RT2870_SOFTC_LOCK(sc);
2697 if (ring->queued >= RT2870_SOFTC_CMD_RING_CMD_COUNT)
2699 RT2870_SOFTC_UNLOCK(sc);
2700 return -1;
2703 cmd = STAILQ_FIRST(&ring->inactive);
2704 STAILQ_REMOVE_HEAD(&ring->inactive, next);
2706 cmd->cb = cb;
2708 if(arg != NULL)
2709 memcpy(cmd->data, arg, len);
2711 STAILQ_INSERT_TAIL(&ring->active, cmd, next);
2712 ring->queued++;
2714 run_cmd_task = (ring->queued == 1);
2716 RT2870_SOFTC_UNLOCK(sc);
2718 if (run_cmd_task)
2719 taskqueue_enqueue(sc->taskqueue, &sc->cmd_task);
2721 return 0;
2725 * rt2870_updateslot_cb
2727 static void rt2870_updateslot_cb(struct rt2870_softc *sc, void *arg)
2729 RT2870_SOFTC_LOCK(sc);
2731 rt2870_asic_updateslot(sc);
2733 RT2870_SOFTC_UNLOCK(sc);
2737 * rt2870_wme_update_cb
2739 static void rt2870_wme_update_cb(struct rt2870_softc *sc, void *arg)
2741 RT2870_SOFTC_LOCK(sc);
2743 rt2870_asic_wme_update(sc);
2745 RT2870_SOFTC_UNLOCK(sc);
2749 * rt2870_vap_key_set_cb
2751 static void rt2870_vap_key_set_cb(struct rt2870_softc *sc, void *arg)
2753 struct ieee80211com *ic;
2754 struct ifnet *ifp;
2755 struct ieee80211vap *vap;
2756 struct ieee80211_key *k;
2757 struct rt2870_cmd_argv_keyset *cmd_argv;
2758 uint16_t associd, key_base, keymode_base;
2759 uint8_t mode, vapid, wcid, iv[8];
2760 uint32_t tmp;
2762 ifp = sc->ifp;
2763 ic = ifp->if_l2com;
2764 vap = TAILQ_FIRST(&ic->ic_vaps);
2765 cmd_argv = arg;
2766 k = &cmd_argv->key;
2767 associd = cmd_argv->associd;
2769 switch (k->wk_cipher->ic_cipher)
2771 case IEEE80211_CIPHER_WEP:
2772 if(k->wk_keylen < 8)
2773 mode = RT2870_REG_CIPHER_MODE_WEP40;
2774 else
2775 mode = RT2870_REG_CIPHER_MODE_WEP104;
2776 break;
2778 case IEEE80211_CIPHER_TKIP:
2779 mode = RT2870_REG_CIPHER_MODE_TKIP;
2780 break;
2782 case IEEE80211_CIPHER_AES_CCM:
2783 mode = RT2870_REG_CIPHER_MODE_AES_CCMP;
2784 break;
2786 default:
2787 return;
2790 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2791 "%s: VAP key set: keyix=%d, keylen=%d, macaddr=%s, associd=0x%04x, mode=%d, group=%d\n",
2792 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, ether_sprintf(k->wk_macaddr),
2793 associd, mode, (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2795 RT2870_SOFTC_LOCK(sc);
2797 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2799 /* install pairwise key */
2801 vapid = 0;
2803 wcid = RT2870_AID2WCID(associd);
2804 if (wcid == 0)
2805 return;
2807 key_base = RT2870_REG_PKEY(wcid);
2809 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2811 memset(iv, 0, 8);
2813 iv[3] = (k->wk_keyix << 6);
2815 else
2817 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2819 iv[0] = (k->wk_keytsc >> 8);
2820 iv[1] = ((iv[0] | 0x20) & 0x7f);
2821 iv[2] = k->wk_keytsc;
2823 else
2825 /* AES CCMP */
2827 iv[0] = k->wk_keytsc;
2828 iv[1] = k->wk_keytsc >> 8;
2829 iv[2] = 0;
2832 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2833 iv[4] = (k->wk_keytsc >> 16);
2834 iv[5] = (k->wk_keytsc >> 24);
2835 iv[6] = (k->wk_keytsc >> 32);
2836 iv[7] = (k->wk_keytsc >> 40);
2838 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2839 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2840 device_get_nameunit(sc->dev),
2841 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2844 rt2870_io_mac_write_multi(sc, RT2870_REG_IVEIV(wcid), iv, 8);
2846 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2848 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2850 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2852 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2853 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2855 else
2857 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2858 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2861 else
2863 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2866 tmp = ((vapid & RT2870_REG_VAP_MASK) << RT2870_REG_VAP_SHIFT) |
2867 (mode << RT2870_REG_CIPHER_MODE_SHIFT) | RT2870_REG_PKEY_ENABLE;
2869 rt2870_io_mac_write(sc, RT2870_REG_WCID_ATTR(wcid), tmp);
2872 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2873 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2875 /* install group key */
2877 vapid = 0;
2878 wcid = 0; /* mcast/bcast WCID */
2879 key_base = RT2870_REG_SKEY(vapid, k->wk_keyix);
2880 keymode_base = RT2870_REG_SKEY_MODE(vapid);
2882 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2884 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2886 if (vap->iv_opmode != IEEE80211_M_HOSTAP)
2888 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2889 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2891 else
2893 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2894 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2897 else
2899 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2902 tmp = rt2870_io_mac_read(sc, keymode_base);
2904 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2905 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2907 rt2870_io_mac_write(sc, keymode_base, tmp);
2909 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2911 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2913 memset(iv, 0, 8);
2915 iv[3] = (k->wk_keyix << 6);
2917 else
2919 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2921 iv[0] = (k->wk_keytsc >> 8);
2922 iv[1] = ((iv[0] | 0x20) & 0x7f);
2923 iv[2] = k->wk_keytsc;
2925 else
2927 /* AES CCMP */
2929 iv[0] = k->wk_keytsc;
2930 iv[1] = k->wk_keytsc >> 8;
2931 iv[2] = 0;
2934 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2935 iv[4] = (k->wk_keytsc >> 16);
2936 iv[5] = (k->wk_keytsc >> 24);
2937 iv[6] = (k->wk_keytsc >> 32);
2938 iv[7] = (k->wk_keytsc >> 40);
2940 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2941 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2942 device_get_nameunit(sc->dev),
2943 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2946 rt2870_io_mac_write_multi(sc, RT2870_REG_IVEIV(wcid), iv, 8);
2948 tmp = ((vapid & RT2870_REG_VAP_MASK) << RT2870_REG_VAP_SHIFT) |
2949 (mode << RT2870_REG_CIPHER_MODE_SHIFT);
2951 rt2870_io_mac_write(sc, RT2870_REG_WCID_ATTR(wcid), tmp);
2955 RT2870_SOFTC_UNLOCK(sc);
2959 * rt2870_vap_key_delete_cb
2961 static void rt2870_vap_key_delete_cb(struct rt2870_softc *sc, void *arg)
2963 struct ieee80211com *ic;
2964 struct ifnet *ifp;
2965 struct ieee80211vap *vap;
2966 struct ieee80211_key *k;
2967 struct rt2870_cmd_argv_keydelete *cmd_argv;
2968 uint16_t associd;
2969 uint8_t vapid, wcid;
2970 uint32_t tmp;
2972 ifp = sc->ifp;
2973 ic = ifp->if_l2com;
2974 vap = TAILQ_FIRST(&ic->ic_vaps);
2975 cmd_argv = arg;
2976 k = &cmd_argv->key;
2977 associd = cmd_argv->associd;
2979 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2980 "%s: VAP key delete: keyix=%d, keylen=%d, macaddr=%s, associd=0x%04x, group=%d\n",
2981 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, ether_sprintf(k->wk_macaddr),
2982 associd, (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2984 RT2870_SOFTC_LOCK(sc);
2986 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2988 /* remove pairwise key */
2990 vapid = 0;
2992 wcid = RT2870_AID2WCID(associd);
2993 if (wcid == 0)
2994 return;
2996 tmp = ((vapid & RT2870_REG_VAP_MASK) << RT2870_REG_VAP_SHIFT) |
2997 (RT2870_REG_CIPHER_MODE_NONE << RT2870_REG_CIPHER_MODE_SHIFT) | RT2870_REG_PKEY_ENABLE;
2999 rt2870_io_mac_write(sc, RT2870_REG_WCID_ATTR(wcid), tmp);
3001 else
3003 /* remove group key */
3005 vapid = 0;
3006 wcid = 0; /* mcast/bcast WCID */
3008 tmp = rt2870_io_mac_read(sc, RT2870_REG_SKEY_MODE(vapid));
3010 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
3011 tmp |= (RT2870_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
3013 rt2870_io_mac_write(sc, RT2870_REG_SKEY_MODE(vapid), tmp);
3015 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3017 tmp = ((vapid & RT2870_REG_VAP_MASK) << RT2870_REG_VAP_SHIFT) |
3018 (RT2870_REG_CIPHER_MODE_NONE << RT2870_REG_CIPHER_MODE_SHIFT);
3020 rt2870_io_mac_write(sc, RT2870_REG_WCID_ATTR(wcid), tmp);
3024 RT2870_SOFTC_UNLOCK(sc);
3028 * rt2870_vap_update_beacon_cb
3030 static void rt2870_vap_update_beacon_cb(struct rt2870_softc *sc, void *arg)
3032 struct ifnet *ifp;
3033 struct ieee80211com *ic;
3034 struct ieee80211vap *vap;
3036 ifp = sc->ifp;
3037 ic = ifp->if_l2com;
3038 vap = TAILQ_FIRST(&ic->ic_vaps);
3040 if (vap == NULL)
3041 return;
3043 RT2870_SOFTC_LOCK(sc);
3045 rt2870_asic_update_beacon(sc, vap);
3047 RT2870_SOFTC_UNLOCK(sc);
3051 * rt2870_asic_set_bssid
3053 static void rt2870_asic_set_bssid(struct rt2870_softc *sc,
3054 const uint8_t *bssid)
3056 uint32_t tmp;
3058 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
3059 "%s: set bssid: bssid=%s\n",
3060 device_get_nameunit(sc->dev),
3061 ether_sprintf(bssid));
3063 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
3065 rt2870_io_mac_write(sc, RT2870_REG_BSSID_DW0, tmp);
3067 tmp = bssid[4] | (bssid[5] << 8);
3069 rt2870_io_mac_write(sc, RT2870_REG_BSSID_DW1, tmp);
3073 * rt2870_asic_set_macaddr
3075 static void rt2870_asic_set_macaddr(struct rt2870_softc *sc,
3076 const uint8_t *addr)
3078 uint32_t tmp;
3080 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3082 rt2870_io_mac_write(sc, RT2870_REG_ADDR_DW0, tmp);
3084 tmp = addr[4] | (addr[5] << 8);
3086 rt2870_io_mac_write(sc, RT2870_REG_ADDR_DW1, tmp);
3090 * rt2870_asic_enable_tsf_sync
3092 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc *sc)
3094 struct ifnet *ifp;
3095 struct ieee80211com *ic;
3096 struct ieee80211vap *vap;
3097 uint32_t tmp;
3099 ifp = sc->ifp;
3100 ic = ifp->if_l2com;
3101 vap = TAILQ_FIRST(&ic->ic_vaps);
3103 RT2870_DPRINTF(sc, RT2870_DEBUG_BEACON,
3104 "%s: enabling TSF\n",
3105 device_get_nameunit(sc->dev));
3107 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
3109 tmp &= ~0x1fffff;
3110 tmp |= vap->iv_bss->ni_intval * 16;
3111 tmp |= (RT2870_REG_TSF_TIMER_ENABLE | RT2870_REG_TBTT_TIMER_ENABLE);
3113 if (vap->iv_opmode == IEEE80211_M_STA)
3115 tmp |= (RT2870_REG_TSF_SYNC_MODE_STA << RT2870_REG_TSF_SYNC_MODE_SHIFT);
3117 else if (vap->iv_opmode == IEEE80211_M_IBSS)
3119 tmp |= RT2870_REG_BCN_TX_ENABLE;
3120 tmp |= (RT2870_REG_TSF_SYNC_MODE_IBSS << RT2870_REG_TSF_SYNC_MODE_SHIFT);
3122 else if (vap->iv_opmode == IEEE80211_M_HOSTAP)
3124 tmp |= RT2870_REG_BCN_TX_ENABLE;
3125 tmp |= (RT2870_REG_TSF_SYNC_MODE_HOSTAP << RT2870_REG_TSF_SYNC_MODE_SHIFT);
3128 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
3132 * rt2870_asic_disable_tsf_sync
3134 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc *sc)
3136 uint32_t tmp;
3138 RT2870_DPRINTF(sc, RT2870_DEBUG_BEACON,
3139 "%s: disabling TSF\n",
3140 device_get_nameunit(sc->dev));
3142 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
3144 tmp &= ~(RT2870_REG_BCN_TX_ENABLE |
3145 RT2870_REG_TSF_TIMER_ENABLE |
3146 RT2870_REG_TBTT_TIMER_ENABLE);
3148 tmp &= ~(RT2870_REG_TSF_SYNC_MODE_MASK << RT2870_REG_TSF_SYNC_MODE_SHIFT);
3149 tmp |= (RT2870_REG_TSF_SYNC_MODE_DISABLE << RT2870_REG_TSF_SYNC_MODE_SHIFT);
3151 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
3155 * rt2870_asic_enable_mrr
3157 static void rt2870_asic_enable_mrr(struct rt2870_softc *sc)
3159 #define CCK(mcs) (mcs)
3160 #define OFDM(mcs) ((1 << 3) | (mcs))
3161 #define HT(mcs) (mcs)
3163 rt2870_io_mac_write(sc, RT2870_REG_TX_LG_FBK_CFG0,
3164 (OFDM(6) << 28) | /* 54 -> 48 */
3165 (OFDM(5) << 24) | /* 48 -> 36 */
3166 (OFDM(4) << 20) | /* 36 -> 24 */
3167 (OFDM(3) << 16) | /* 24 -> 18 */
3168 (OFDM(2) << 12) | /* 18 -> 12 */
3169 (OFDM(1) << 8) | /* 12 -> 9 */
3170 (OFDM(0) << 4) | /* 9 -> 6 */
3171 OFDM(0)); /* 6 -> 6 */
3173 rt2870_io_mac_write(sc, RT2870_REG_TX_LG_FBK_CFG1,
3174 (CCK(2) << 12) | /* 11 -> 5.5 */
3175 (CCK(1) << 8) | /* 5.5 -> 2 */
3176 (CCK(0) << 4) | /* 2 -> 1 */
3177 CCK(0)); /* 1 -> 1 */
3179 rt2870_io_mac_write(sc, RT2870_REG_TX_HT_FBK_CFG0,
3180 (HT(6) << 28) |
3181 (HT(5) << 24) |
3182 (HT(4) << 20) |
3183 (HT(3) << 16) |
3184 (HT(2) << 12) |
3185 (HT(1) << 8) |
3186 (HT(0) << 4) |
3187 HT(0));
3189 rt2870_io_mac_write(sc, RT2870_REG_TX_HT_FBK_CFG1,
3190 (HT(14) << 28) |
3191 (HT(13) << 24) |
3192 (HT(12) << 20) |
3193 (HT(11) << 16) |
3194 (HT(10) << 12) |
3195 (HT(9) << 8) |
3196 (HT(8) << 4) |
3197 HT(7));
3199 #undef HT
3200 #undef OFDM
3201 #undef CCK
3205 * rt2870_asic_set_txpreamble
3207 static void rt2870_asic_set_txpreamble(struct rt2870_softc *sc)
3209 struct ifnet *ifp;
3210 struct ieee80211com *ic;
3211 uint32_t tmp;
3213 ifp = sc->ifp;
3214 ic = ifp->if_l2com;
3216 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
3217 "%s: %s short Tx preamble\n",
3218 device_get_nameunit(sc->dev),
3219 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
3221 tmp = rt2870_io_mac_read(sc, RT2870_REG_AUTO_RSP_CFG);
3223 tmp &= ~RT2870_REG_CCK_SHORT_ENABLE;
3225 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3226 tmp |= RT2870_REG_CCK_SHORT_ENABLE;
3228 rt2870_io_mac_write(sc, RT2870_REG_AUTO_RSP_CFG, tmp);
3232 * rt2870_asic_set_basicrates
3234 static void rt2870_asic_set_basicrates(struct rt2870_softc *sc)
3236 struct ifnet *ifp;
3237 struct ieee80211com *ic;
3239 ifp = sc->ifp;
3240 ic = ifp->if_l2com;
3242 if (ic->ic_curmode == IEEE80211_MODE_11B)
3243 rt2870_io_mac_write(sc, RT2870_REG_LEGACY_BASIC_RATE, 0x3);
3244 else if (ic->ic_curmode == IEEE80211_MODE_11A)
3245 rt2870_io_mac_write(sc, RT2870_REG_LEGACY_BASIC_RATE, 0x150);
3246 else
3247 rt2870_io_mac_write(sc, RT2870_REG_LEGACY_BASIC_RATE, 0x15f);
3251 * rt2870_asic_update_rtsthreshold
3253 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc *sc)
3255 struct ifnet *ifp;
3256 struct ieee80211com *ic;
3257 struct ieee80211vap *vap;
3258 uint32_t tmp;
3259 uint16_t threshold;
3261 ifp = sc->ifp;
3262 ic = ifp->if_l2com;
3263 vap = TAILQ_FIRST(&ic->ic_vaps);
3265 if (vap == NULL)
3266 threshold = IEEE80211_RTS_MAX;
3267 else if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
3268 threshold = 0x1000;
3269 else
3270 threshold = vap->iv_rtsthreshold;
3272 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3273 "%s: updating RTS threshold: %d\n",
3274 device_get_nameunit(sc->dev), threshold);
3276 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_RTS_CFG);
3278 tmp &= ~(RT2870_REG_TX_RTS_THRESHOLD_MASK << RT2870_REG_TX_RTS_THRESHOLD_SHIFT);
3280 tmp |= ((threshold & RT2870_REG_TX_RTS_THRESHOLD_MASK) <<
3281 RT2870_REG_TX_RTS_THRESHOLD_SHIFT);
3283 rt2870_io_mac_write(sc, RT2870_REG_TX_RTS_CFG, tmp);
3287 * rt2870_asic_update_txpower
3289 static void rt2870_asic_update_txpower(struct rt2870_softc *sc)
3291 struct ifnet *ifp;
3292 struct ieee80211com *ic;
3293 uint32_t *txpow_rate;
3294 int8_t delta;
3295 uint8_t val;
3296 uint32_t tmp;
3297 int i;
3299 ifp = sc->ifp;
3300 ic = ifp->if_l2com;
3302 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
3303 "%s: updating Tx power: %d\n",
3304 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
3306 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
3308 txpow_rate = sc->txpow_rate_20mhz;
3310 else
3312 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3313 txpow_rate = sc->txpow_rate_40mhz_2ghz;
3314 else
3315 txpow_rate = sc->txpow_rate_40mhz_5ghz;
3318 delta = 0;
3320 val = rt2870_io_bbp_read(sc, 1);
3321 val &= 0xfc;
3323 if (ic->ic_txpowlimit > 90)
3325 /* do nothing */
3327 else if (ic->ic_txpowlimit > 60)
3329 delta -= 1;
3331 else if (ic->ic_txpowlimit > 30)
3333 delta -= 3;
3335 else if (ic->ic_txpowlimit > 15)
3337 val |= 0x1;
3339 else if (ic->ic_txpowlimit > 9)
3341 val |= 0x1;
3342 delta -= 3;
3344 else
3346 val |= 0x2;
3349 rt2870_io_bbp_write(sc, 1, val);
3351 for (i = 0; i < RT2870_SOFTC_TXPOW_RATE_COUNT; i++)
3353 if (txpow_rate[i] == 0xffffffff)
3354 continue;
3356 tmp = rt2870_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
3358 rt2870_io_mac_write(sc, RT2870_REG_TX_PWR_CFG(i), tmp);
3363 * rt2870_asic_update_promisc
3365 static void rt2870_asic_update_promisc(struct rt2870_softc *sc)
3367 struct ifnet *ifp;
3368 uint32_t tmp;
3370 ifp = sc->ifp;
3372 printf("%s: %s promiscuous mode\n",
3373 device_get_nameunit(sc->dev),
3374 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
3376 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_FILTER_CFG);
3378 tmp &= ~RT2870_REG_RX_FILTER_DROP_UC_NOME;
3380 if (!(ifp->if_flags & IFF_PROMISC))
3381 tmp |= RT2870_REG_RX_FILTER_DROP_UC_NOME;
3383 rt2870_io_mac_write(sc, RT2870_REG_RX_FILTER_CFG, tmp);
3387 * rt2870_asic_updateprot
3389 static void rt2870_asic_updateprot(struct rt2870_softc *sc)
3391 struct ifnet *ifp;
3392 struct ieee80211com *ic;
3393 struct ieee80211vap *vap;
3394 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
3395 uint8_t htopmode;
3396 enum ieee80211_protmode htprotmode;
3398 ifp = sc->ifp;
3399 ic = ifp->if_l2com;
3400 vap = TAILQ_FIRST(&ic->ic_vaps);
3402 /* CCK frame protection */
3404 cck_prot = RT2870_REG_RTSTH_ENABLE | RT2870_REG_PROT_NAV_SHORT |
3405 RT2870_REG_TXOP_ALLOW_ALL | RT2870_REG_PROT_CTRL_NONE;
3407 /* set up protection frame phy mode and rate (MCS code) */
3409 if (ic->ic_curmode == IEEE80211_MODE_11A)
3410 cck_prot |= (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3411 (0 << RT2870_REG_PROT_MCS_SHIFT);
3412 else
3413 cck_prot |= ((RT2870_REG_PROT_PHYMODE_CCK << RT2870_REG_PROT_PHYMODE_SHIFT) |
3414 (3 << RT2870_REG_PROT_MCS_SHIFT));
3416 rt2870_io_mac_write(sc, RT2870_REG_TX_CCK_PROT_CFG, cck_prot);
3418 /* OFDM frame protection */
3420 ofdm_prot = RT2870_REG_RTSTH_ENABLE | RT2870_REG_PROT_NAV_SHORT |
3421 RT2870_REG_TXOP_ALLOW_ALL;
3423 if (ic->ic_flags & IEEE80211_F_USEPROT)
3425 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3426 "%s: updating protection mode: b/g protection mode=%s\n",
3427 device_get_nameunit(sc->dev),
3428 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3429 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3431 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3432 ofdm_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3433 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3434 ofdm_prot |= RT2870_REG_PROT_CTRL_CTS;
3435 else
3436 ofdm_prot |= RT2870_REG_PROT_CTRL_NONE;
3438 else
3440 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3441 "%s: updating protection mode: b/g protection mode=%s\n",
3442 device_get_nameunit(sc->dev), "none");
3444 ofdm_prot |= RT2870_REG_PROT_CTRL_NONE;
3447 rt2870_io_mac_write(sc, RT2870_REG_TX_OFDM_PROT_CFG, ofdm_prot);
3449 /* HT frame protection */
3451 if ((vap != NULL) && (vap->iv_opmode == IEEE80211_M_STA) && (vap->iv_state == IEEE80211_S_RUN))
3452 htopmode = vap->iv_bss->ni_htopmode;
3453 else
3454 htopmode = ic->ic_curhtprotmode;
3456 htprotmode = ic->ic_htprotmode;
3458 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3460 if (sc->tx_ampdu_sessions > 0)
3462 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3463 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3464 device_get_nameunit(sc->dev));
3466 htopmode = IEEE80211_HTINFO_OPMODE_MIXED;
3467 htprotmode = IEEE80211_PROT_RTSCTS;
3470 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3471 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3472 device_get_nameunit(sc->dev),
3473 htopmode & IEEE80211_HTINFO_OPMODE,
3474 (htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3475 ((htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3477 switch (htopmode & IEEE80211_HTINFO_OPMODE)
3479 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3480 case IEEE80211_HTINFO_OPMODE_HT20PR:
3481 mm20_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3482 RT2870_REG_TXOP_ALLOW_CCK | RT2870_REG_TXOP_ALLOW_OFDM |
3483 RT2870_REG_TXOP_ALLOW_MM20 | RT2870_REG_TXOP_ALLOW_GF20 |
3484 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3485 (4 << RT2870_REG_PROT_MCS_SHIFT);
3487 gf20_prot = mm20_prot;
3489 mm40_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3490 RT2870_REG_TXOP_ALLOW_ALL |
3491 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3492 (0x84 << RT2870_REG_PROT_MCS_SHIFT);
3494 if (htprotmode == IEEE80211_PROT_RTSCTS)
3495 mm40_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3496 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3497 mm40_prot |= RT2870_REG_PROT_CTRL_CTS;
3498 else
3499 mm40_prot |= RT2870_REG_PROT_CTRL_NONE;
3501 gf40_prot = mm40_prot;
3502 break;
3504 /* IEEE80211_HTINFO_OPMODE_MIXED */
3505 case IEEE80211_HTINFO_OPMODE_MIXED:
3506 mm20_prot = RT2870_REG_PROT_NAV_SHORT |
3507 RT2870_REG_TXOP_ALLOW_CCK | RT2870_REG_TXOP_ALLOW_OFDM |
3508 RT2870_REG_TXOP_ALLOW_MM20 | RT2870_REG_TXOP_ALLOW_GF20;
3510 if (ic->ic_flags & IEEE80211_F_USEPROT)
3511 mm20_prot |= (RT2870_REG_PROT_PHYMODE_CCK << RT2870_REG_PROT_PHYMODE_SHIFT) |
3512 (3 << RT2870_REG_PROT_MCS_SHIFT);
3513 else
3514 mm20_prot |= (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3515 (4 << RT2870_REG_PROT_MCS_SHIFT);
3517 if (htprotmode == IEEE80211_PROT_RTSCTS)
3518 mm20_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3519 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3520 mm20_prot |= RT2870_REG_PROT_CTRL_CTS;
3521 else
3522 mm20_prot |= RT2870_REG_PROT_CTRL_NONE;
3524 gf20_prot = mm20_prot;
3526 mm40_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_TXOP_ALLOW_ALL;
3528 if (ic->ic_flags & IEEE80211_F_USEPROT)
3529 mm40_prot |= (RT2870_REG_PROT_PHYMODE_CCK << RT2870_REG_PROT_PHYMODE_SHIFT) |
3530 (3 << RT2870_REG_PROT_MCS_SHIFT);
3531 else
3532 mm40_prot |= (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3533 (0x84 << RT2870_REG_PROT_MCS_SHIFT);
3535 if (htprotmode == IEEE80211_PROT_RTSCTS)
3536 mm40_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3537 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3538 mm40_prot |= RT2870_REG_PROT_CTRL_CTS;
3539 else
3540 mm40_prot |= RT2870_REG_PROT_CTRL_NONE;
3542 gf40_prot = mm40_prot;
3543 break;
3546 * IEEE80211_HTINFO_OPMODE_PURE
3547 * IEEE80211_HTINFO_OPMODE_PROTOPT
3549 case IEEE80211_HTINFO_OPMODE_PURE:
3550 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3551 default:
3552 mm20_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3553 RT2870_REG_TXOP_ALLOW_CCK | RT2870_REG_TXOP_ALLOW_OFDM |
3554 RT2870_REG_TXOP_ALLOW_MM20 | RT2870_REG_TXOP_ALLOW_GF20 |
3555 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3556 (4 << RT2870_REG_PROT_MCS_SHIFT);
3558 gf20_prot = mm20_prot;
3560 mm40_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3561 RT2870_REG_TXOP_ALLOW_ALL |
3562 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3563 (0x84 << RT2870_REG_PROT_MCS_SHIFT);
3565 gf40_prot = mm40_prot;
3566 break;
3569 rt2870_io_mac_write(sc, RT2870_REG_TX_MM20_PROT_CFG, mm20_prot);
3570 rt2870_io_mac_write(sc, RT2870_REG_TX_MM40_PROT_CFG, mm40_prot);
3571 rt2870_io_mac_write(sc, RT2870_REG_TX_GF20_PROT_CFG, gf20_prot);
3572 rt2870_io_mac_write(sc, RT2870_REG_TX_GF40_PROT_CFG, gf40_prot);
3576 * rt2870_asic_updateslot
3578 static void rt2870_asic_updateslot(struct rt2870_softc *sc)
3580 struct ifnet *ifp;
3581 struct ieee80211com *ic;
3582 struct ieee80211vap *vap;
3583 uint32_t tmp;
3585 ifp = sc->ifp;
3586 ic = ifp->if_l2com;
3587 vap = TAILQ_FIRST(&ic->ic_vaps);
3589 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
3590 "%s: %s short slot time\n",
3591 device_get_nameunit(sc->dev),
3592 ((ic->ic_flags & IEEE80211_F_SHSLOT) ||
3593 ((vap != NULL) && (vap->iv_flags & IEEE80211_F_BURST))) ? "enabling" : "disabling");
3595 tmp = rt2870_io_mac_read(sc, RT2870_REG_BKOFF_SLOT_CFG);
3597 tmp &= ~0xff;
3599 if ((ic->ic_flags & IEEE80211_F_SHSLOT) ||
3600 ((vap != NULL) && (vap->iv_flags & IEEE80211_F_BURST)))
3601 tmp |= IEEE80211_DUR_SHSLOT;
3602 else
3603 tmp |= IEEE80211_DUR_SLOT;
3605 rt2870_io_mac_write(sc, RT2870_REG_BKOFF_SLOT_CFG, tmp);
3609 * rt2870_asic_wme_update
3611 static void rt2870_asic_wme_update(struct rt2870_softc *sc)
3613 struct ifnet *ifp;
3614 struct ieee80211com *ic;
3615 struct ieee80211_wme_state *wme;
3616 const struct wmeParams *wmep;
3617 int i;
3619 ifp = sc->ifp;
3620 ic = ifp->if_l2com;
3621 wme = &ic->ic_wme;
3622 wmep = wme->wme_chanParams.cap_wmeParams;
3624 RT2870_DPRINTF(sc, RT2870_DEBUG_WME,
3625 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3626 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3627 device_get_nameunit(sc->dev),
3628 wmep[WME_AC_VO].wmep_aifsn,
3629 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3630 wmep[WME_AC_VO].wmep_txopLimit,
3631 wmep[WME_AC_VI].wmep_aifsn,
3632 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3633 wmep[WME_AC_VI].wmep_txopLimit,
3634 wmep[WME_AC_BK].wmep_aifsn,
3635 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3636 wmep[WME_AC_BK].wmep_txopLimit,
3637 wmep[WME_AC_BE].wmep_aifsn,
3638 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3639 wmep[WME_AC_BE].wmep_txopLimit);
3641 for (i = 0; i < WME_NUM_AC; i++)
3642 rt2870_io_mac_write(sc, RT2870_REG_TX_EDCA_AC_CFG(i),
3643 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3644 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3646 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_AIFSN_CFG,
3647 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3648 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3650 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_CWMIN_CFG,
3651 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3652 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3654 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_CWMAX_CFG,
3655 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3656 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3658 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP0_CFG,
3659 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3661 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP1_CFG,
3662 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3666 * rt2870_asic_update_beacon
3668 static void rt2870_asic_update_beacon(struct rt2870_softc *sc,
3669 struct ieee80211vap *vap)
3671 struct rt2870_softc_vap *rvap;
3672 struct mbuf *m;
3673 struct rt2870_txwi *txwi;
3674 uint32_t tmp;
3676 rvap = (struct rt2870_softc_vap *) vap;
3678 m = rvap->beacon_mbuf;
3679 txwi = &rvap->beacon_txwi;
3681 /* disable temporarily TSF sync */
3683 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
3685 tmp &= ~(RT2870_REG_BCN_TX_ENABLE |
3686 RT2870_REG_TSF_TIMER_ENABLE |
3687 RT2870_REG_TBTT_TIMER_ENABLE);
3689 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
3691 /* write Tx wireless info and beacon frame to on-chip memory */
3693 rt2870_io_mac_write_multi(sc, RT2870_REG_BEACON_BASE(0),
3694 txwi, sizeof(struct rt2870_txwi));
3696 rt2870_io_mac_write_multi(sc, RT2870_REG_BEACON_BASE(0) + sizeof(struct rt2870_txwi),
3697 mtod(m, uint8_t *), m->m_pkthdr.len);
3699 /* enable again TSF sync */
3701 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
3703 tmp |= (RT2870_REG_BCN_TX_ENABLE |
3704 RT2870_REG_TSF_TIMER_ENABLE |
3705 RT2870_REG_TBTT_TIMER_ENABLE);
3707 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
3711 * rt2870_asic_clear_keytables
3713 static void rt2870_asic_clear_keytables(struct rt2870_softc *sc)
3715 int i;
3717 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3719 for (i = 0; i < 256; i++)
3721 rt2870_io_mac_write(sc, RT2870_REG_WCID(i), 0xffffffff);
3722 rt2870_io_mac_write(sc, RT2870_REG_WCID(i) + 4, 0x0000ffff);
3725 /* clear WCID attribute table (entries = 256, entry size = 4) */
3727 rt2870_io_mac_set_region_4(sc, RT2870_REG_WCID_ATTR(0), 0, 256);
3729 /* clear IV/EIV table (entries = 256, entry size = 8) */
3731 rt2870_io_mac_set_region_4(sc, RT2870_REG_IVEIV(0), 0, 2 * 256);
3733 /* clear pairwise key table (entries = 256, entry size = 32) */
3735 rt2870_io_mac_set_region_4(sc, RT2870_REG_PKEY(0), 0, 8 * 256);
3737 /* clear shared key table (entries = 32, entry size = 32) */
3739 rt2870_io_mac_set_region_4(sc, RT2870_REG_SKEY(0, 0), 0, 8 * 32);
3741 /* clear shared key mode (entries = 32, entry size = 2) */
3743 rt2870_io_mac_set_region_4(sc, RT2870_REG_SKEY_MODE(0), 0, 16);
3747 * rt2870_beacon_alloc
3749 static int rt2870_beacon_alloc(struct rt2870_softc *sc,
3750 struct ieee80211vap *vap)
3752 struct ieee80211com *ic;
3753 struct rt2870_softc_vap *rvap;
3754 const struct ieee80211_txparam *tp;
3755 struct mbuf *m;
3756 struct rt2870_txwi txwi;
3757 uint8_t rate, mcs;
3759 ic = vap->iv_ic;
3760 rvap = (struct rt2870_softc_vap *) vap;
3761 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
3763 m = ieee80211_beacon_alloc(vap->iv_bss, &rvap->beacon_offsets);
3764 if (m == NULL)
3765 return ENOMEM;
3767 rate = tp->mgmtrate;
3768 mcs = rt2870_rate2mcs(rate);
3770 memset(&txwi, 0, sizeof(struct rt2870_txwi));
3772 txwi.wcid = 0xff;
3773 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2870_TXWI_MPDU_LEN_MASK) <<
3774 RT2870_TXWI_MPDU_LEN_SHIFT);
3775 txwi.txop = (RT2870_TXWI_TXOP_HT << RT2870_TXWI_TXOP_SHIFT);
3776 txwi.mpdu_density_flags |=
3777 (RT2870_TXWI_FLAGS_TS << RT2870_TXWI_FLAGS_SHIFT);
3778 txwi.bawin_size_xflags |=
3779 (RT2870_TXWI_XFLAGS_NSEQ << RT2870_TXWI_XFLAGS_SHIFT);
3781 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
3783 txwi.phymode_ifs_stbc_shortgi =
3784 (RT2870_TXWI_PHYMODE_CCK << RT2870_TXWI_PHYMODE_SHIFT);
3786 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3787 mcs |= RT2870_TXWI_MCS_SHOTPRE;
3789 else
3791 txwi.phymode_ifs_stbc_shortgi =
3792 (RT2870_TXWI_PHYMODE_OFDM << RT2870_TXWI_PHYMODE_SHIFT);
3795 txwi.bw_mcs = (RT2870_TXWI_BW_20 << RT2870_TXWI_BW_SHIFT) |
3796 ((mcs & RT2870_TXWI_MCS_MASK) << RT2870_TXWI_MCS_SHIFT);
3798 if (rvap->beacon_mbuf != NULL)
3800 m_free(rvap->beacon_mbuf);
3801 rvap->beacon_mbuf = NULL;
3804 rvap->beacon_mbuf = m;
3805 rvap->beacon_txwi = txwi;
3807 return 0;
3811 * rt2870_rxrate
3813 static uint8_t rt2870_rxrate(struct rt2870_rxwi *rxwi)
3815 uint8_t mcs, phymode;
3816 uint8_t rate;
3818 mcs = (rxwi->bw_mcs >> RT2870_RXWI_MCS_SHIFT) & RT2870_RXWI_MCS_MASK;
3819 phymode = (rxwi->phymode_stbc_shortgi >> RT2870_RXWI_PHYMODE_SHIFT) &
3820 RT2870_RXWI_PHYMODE_MASK;
3822 rate = 2;
3824 switch (phymode)
3826 case RT2870_RXWI_PHYMODE_CCK:
3827 switch (mcs & ~RT2870_RXWI_MCS_SHOTPRE)
3829 case 0: rate = 2; break; /* 1 Mbps */
3830 case 1: rate = 4; break; /* 2 MBps */
3831 case 2: rate = 11; break; /* 5.5 Mbps */
3832 case 3: rate = 22; break; /* 11 Mbps */
3834 break;
3836 case RT2870_RXWI_PHYMODE_OFDM:
3837 switch (mcs)
3839 case 0: rate = 12; break; /* 6 Mbps */
3840 case 1: rate = 18; break; /* 9 Mbps */
3841 case 2: rate = 24; break; /* 12 Mbps */
3842 case 3: rate = 36; break; /* 18 Mbps */
3843 case 4: rate = 48; break; /* 24 Mbps */
3844 case 5: rate = 72; break; /* 36 Mbps */
3845 case 6: rate = 96; break; /* 48 Mbps */
3846 case 7: rate = 108; break; /* 54 Mbps */
3848 break;
3850 case RT2870_RXWI_PHYMODE_HT_MIXED:
3851 case RT2870_RXWI_PHYMODE_HT_GF:
3852 break;
3855 return rate;
3859 * rt2870_maxrssi_rxpath
3861 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc *sc,
3862 const struct rt2870_rxwi *rxwi)
3864 uint8_t rxpath;
3866 rxpath = 0;
3868 if (sc->nrxpath > 1)
3869 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3870 rxpath = 1;
3872 if (sc->nrxpath > 2)
3873 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3874 rxpath = 2;
3876 return rxpath;
3880 * rt2870_rssi2dbm
3882 static int8_t rt2870_rssi2dbm(struct rt2870_softc *sc,
3883 uint8_t rssi, uint8_t rxpath)
3885 struct ifnet *ifp;
3886 struct ieee80211com *ic;
3887 struct ieee80211_channel *c;
3888 int chan;
3889 int8_t rssi_off, lna_gain;
3891 if (rssi == 0)
3892 return -99;
3894 ifp = sc->ifp;
3895 ic = ifp->if_l2com;
3896 c = ic->ic_curchan;
3897 chan = ieee80211_chan2ieee(ic, c);
3899 if (IEEE80211_IS_CHAN_5GHZ(c))
3901 rssi_off = sc->rssi_off_5ghz[rxpath];
3903 if (chan <= 64)
3904 lna_gain = sc->lna_gain[1];
3905 else if (chan <= 128)
3906 lna_gain = sc->lna_gain[2];
3907 else
3908 lna_gain = sc->lna_gain[3];
3910 else
3912 rssi_off = sc->rssi_off_2ghz[rxpath];
3913 lna_gain = sc->lna_gain[0];
3916 return (-12 - rssi_off - lna_gain - rssi);
3920 * rt2870_rate2mcs
3922 static uint8_t rt2870_rate2mcs(uint8_t rate)
3924 switch (rate)
3926 /* CCK rates */
3927 case 2: return 0;
3928 case 4: return 1;
3929 case 11: return 2;
3930 case 22: return 3;
3932 /* OFDM rates */
3933 case 12: return 0;
3934 case 18: return 1;
3935 case 24: return 2;
3936 case 36: return 3;
3937 case 48: return 4;
3938 case 72: return 5;
3939 case 96: return 6;
3940 case 108: return 7;
3943 return 0;
3947 * rt2870_rx_frame
3949 static void rt2870_rx_frame(struct rt2870_softc *sc,
3950 uint8_t *buf, uint32_t dmalen)
3952 struct ifnet *ifp;
3953 struct ieee80211com *ic;
3954 struct ieee80211_frame *wh;
3955 struct ieee80211_node *ni;
3956 struct rt2870_softc_node *rni;
3957 struct rt2870_softc_rx_radiotap_header *tap;
3958 struct rt2870_rxwi *rxwi;
3959 struct rt2870_rxinfo *rxinfo;
3960 struct mbuf *m;
3961 uint32_t rxinfo_flags;
3962 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs, keyidx;
3963 int8_t rssi_dbm;
3964 int len, ampdu, amsdu, rssi_dbm_rel, i;
3966 ifp = sc->ifp;
3967 ic = ifp->if_l2com;
3969 /* get Rx wireless info */
3971 rxwi = (struct rt2870_rxwi *) buf;
3972 len = (le16toh(rxwi->tid_size) >> RT2870_RXWI_SIZE_SHIFT) &
3973 RT2870_RXWI_SIZE_MASK;
3975 if (len > dmalen)
3977 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3978 "%s: bad rxwi len: DMA len=%d, rxwi len=%d\n",
3979 device_get_nameunit(sc->dev), dmalen, len);
3980 return;
3983 /* get Rx info */
3985 rxinfo = (struct rt2870_rxinfo *) ((caddr_t) rxwi + dmalen);
3986 rxinfo_flags = le32toh(rxinfo->flags);
3988 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3989 "%s: Rx frame: DMA len=%d, len=%d, rxinfo flags=0x%08x\n",
3990 device_get_nameunit(sc->dev), dmalen, len, rxinfo_flags);
3992 /* check for crc errors */
3994 if (rxinfo_flags & RT2870_RXINFO_FLAGS_CRC_ERR)
3996 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3997 "%s: rxinfo: crc error\n",
3998 device_get_nameunit(sc->dev));
4000 ifp->if_ierrors++;
4002 if (!(ifp->if_flags & IFF_PROMISC))
4003 return;
4006 wh = (struct ieee80211_frame *) (rxwi + 1);
4008 /* check for L2 padding between IEEE 802.11 frame header and body */
4010 if (rxinfo_flags & RT2870_RXINFO_FLAGS_L2PAD)
4012 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4013 "%s: L2 padding: DMA len=%d, len=%d\n",
4014 device_get_nameunit(sc->dev), dmalen, len);
4016 len += 2;
4019 m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
4020 if (m == NULL)
4022 sc->rx_mbuf_alloc_errors++;
4023 ifp->if_ierrors++;
4024 return;
4027 m->m_pkthdr.rcvif = ifp;
4028 m->m_pkthdr.len = m->m_len = len;
4030 m_copyback(m, 0, len, (caddr_t) wh);
4032 wh = mtod(m, struct ieee80211_frame *);
4034 /* check for cipher errors */
4036 if (rxinfo_flags & RT2870_RXINFO_FLAGS_DECRYPTED)
4038 cipher_err = ((rxinfo_flags >> RT2870_RXINFO_FLAGS_CIPHER_ERR_SHIFT) &
4039 RT2870_RXINFO_FLAGS_CIPHER_ERR_MASK);
4040 if (cipher_err == RT2870_RXINFO_FLAGS_CIPHER_ERR_NONE)
4042 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4043 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4045 m->m_flags |= M_WEP;
4047 sc->rx_cipher_no_errors++;
4049 else
4051 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4052 "%s: rxinfo: cipher error=0x%02x\n",
4053 device_get_nameunit(sc->dev), cipher_err);
4055 if (cipher_err == RT2870_RXINFO_FLAGS_CIPHER_ERR_ICV)
4056 sc->rx_cipher_icv_errors++;
4057 else if (cipher_err == RT2870_RXINFO_FLAGS_CIPHER_ERR_MIC)
4058 sc->rx_cipher_mic_errors++;
4059 else if (cipher_err == RT2870_RXINFO_FLAGS_CIPHER_ERR_INVALID_KEY)
4060 sc->rx_cipher_invalid_key_errors++;
4062 if ((cipher_err == RT2870_RXINFO_FLAGS_CIPHER_ERR_MIC) &&
4063 (rxinfo_flags & RT2870_RXINFO_FLAGS_MYBSS))
4065 ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
4066 if (ni != NULL)
4068 keyidx = (rxwi->udf_bssidx_keyidx >> RT2870_RXWI_KEYIDX_SHIFT) &
4069 RT2870_RXWI_KEYIDX_MASK;
4071 ieee80211_notify_michael_failure(ni->ni_vap, wh, keyidx);
4073 ieee80211_free_node(ni);
4077 ifp->if_ierrors++;
4079 if (!(ifp->if_flags & IFF_PROMISC))
4081 m_free(m);
4082 return;
4086 else
4088 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4090 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4091 "%s: rxinfo: not decrypted but protected flag set\n",
4092 device_get_nameunit(sc->dev));
4094 ifp->if_ierrors++;
4096 if (!(ifp->if_flags & IFF_PROMISC))
4098 m_free(m);
4099 return;
4104 /* check for A-MPDU */
4106 if (rxinfo_flags & RT2870_RXINFO_FLAGS_AMPDU)
4108 m->m_flags |= M_AMPDU;
4110 sc->rx_ampdu++;
4112 ampdu = 1;
4114 else
4116 ampdu = 0;
4119 /* check for A-MSDU */
4121 if (rxinfo_flags & RT2870_RXINFO_FLAGS_AMSDU)
4123 sc->rx_amsdu++;
4125 amsdu = 1;
4127 else
4129 amsdu = 0;
4132 ant = rt2870_maxrssi_rxpath(sc, rxwi);
4133 rssi = rxwi->rssi[ant];
4134 rssi_dbm = rt2870_rssi2dbm(sc, rssi, ant);
4135 phymode = ((rxwi->phymode_stbc_shortgi >> RT2870_RXWI_PHYMODE_SHIFT) &
4136 RT2870_RXWI_PHYMODE_MASK);
4137 bw = ((rxwi->bw_mcs >> RT2870_RXWI_BW_SHIFT) & RT2870_RXWI_BW_MASK);
4138 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2870_RXWI_SHORTGI_SHIFT) &
4139 RT2870_RXWI_SHORTGI_MASK);
4140 mcs = ((rxwi->bw_mcs >> RT2870_RXWI_MCS_SHIFT) & RT2870_RXWI_MCS_MASK);
4142 if (ieee80211_radiotap_active(ic))
4144 tap = &sc->rxtap;
4146 tap->flags = (rxinfo_flags & RT2870_RXINFO_FLAGS_L2PAD) ? IEEE80211_RADIOTAP_F_DATAPAD : 0;
4147 tap->dbm_antsignal = rssi_dbm;
4148 tap->dbm_antnoise = RT2870_NOISE_FLOOR;
4149 tap->antenna = ant;
4150 tap->antsignal = rssi;
4151 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4152 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4153 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4154 tap->chan_maxpow = 0;
4156 if (phymode == RT2870_RXWI_PHYMODE_CCK || phymode == RT2870_RXWI_PHYMODE_OFDM)
4157 tap->rate = rt2870_rxrate(rxwi);
4158 else
4159 tap->rate = mcs | IEEE80211_RATE_MCS;
4161 if (rxinfo_flags & RT2870_RXINFO_FLAGS_CRC_ERR)
4162 tap->flags |= IEEE80211_RADIOTAP_F_BADFCS;
4164 if (rxinfo_flags & RT2870_RXINFO_FLAGS_FRAG)
4165 tap->flags |= IEEE80211_RADIOTAP_F_FRAG;
4167 if (rxwi->bw_mcs & RT2870_RXWI_MCS_SHOTPRE)
4168 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4170 if ((rxinfo_flags & RT2870_RXINFO_FLAGS_DECRYPTED) ||
4171 (wh->i_fc[1] & IEEE80211_FC1_WEP))
4172 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4174 if (shortgi)
4175 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4179 * net80211 assumes that RSSI data are in the range [-127..127] and
4180 * in .5 dBm units relative to the current noise floor
4183 rssi_dbm_rel = (rssi_dbm - RT2870_NOISE_FLOOR) * 2;
4184 if (rssi_dbm_rel > 127)
4185 rssi_dbm_rel = 127;
4187 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4188 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4189 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d, "
4190 "rssi_dbm=%d, rssi_dbm_rel=%d\n",
4191 device_get_nameunit(sc->dev),
4192 len, phymode, bw, shortgi, mcs,
4193 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
4194 rxwi->snr[0], rxwi->snr[1],
4195 rxwi->wcid, ampdu, amsdu, rssi_dbm, rssi_dbm_rel);
4197 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
4198 if (ni != NULL)
4200 rni = (struct rt2870_softc_node *) ni;
4202 for (i = 0; i < RT2870_SOFTC_RSSI_COUNT; i++)
4204 rni->last_rssi[i] = rxwi->rssi[i];
4205 rni->last_rssi_dbm[i] = rt2870_rssi2dbm(sc, rxwi->rssi[i], i);
4208 ieee80211_input(ni, m, rssi_dbm_rel, RT2870_NOISE_FLOOR);
4209 ieee80211_free_node(ni);
4211 else
4213 ieee80211_input_all(ic, m, rssi_dbm_rel, RT2870_NOISE_FLOOR);
4218 * rt2870_tx_mgmt
4220 static int rt2870_tx_mgmt(struct rt2870_softc *sc,
4221 struct mbuf *m, struct ieee80211_node *ni, int qid)
4223 struct ifnet *ifp;
4224 struct ieee80211com *ic;
4225 struct ieee80211vap *vap;
4226 const struct ieee80211_txparam *tp;
4227 struct rt2870_softc_node *rni;
4228 struct rt2870_softc_tx_ring *ring;
4229 struct rt2870_softc_tx_data *data;
4230 struct rt2870_txinfo *txinfo;
4231 struct rt2870_txwi *txwi;
4232 struct ieee80211_frame *wh;
4233 struct rt2870_softc_tx_radiotap_header *tap;
4234 u_int hdrsize, hdrspace;
4235 uint8_t rate, stbc, shortgi, mcs, pid;
4236 uint16_t len, dmalen, mpdu_len, dur;
4237 int mimops;
4239 KASSERT(qid >= 0 && qid < (sc->usb_endpoints - 1),
4240 ("%s: Tx MGMT: invalid qid=%d\n",
4241 device_get_nameunit(sc->dev), qid));
4243 ifp = sc->ifp;
4244 ic = ifp->if_l2com;
4245 vap = ni->ni_vap;
4246 rni = (struct rt2870_softc_node *) ni;
4247 tp = ni->ni_txparms;
4249 ring = &sc->tx_ring[qid];
4250 data = STAILQ_FIRST(&ring->inactive);
4251 STAILQ_REMOVE_HEAD(&ring->inactive, next);
4252 txinfo = (struct rt2870_txinfo *) data->buf;
4253 txwi = (struct rt2870_txwi *) (txinfo + 1);
4255 wh = mtod(m, struct ieee80211_frame *);
4257 rate = tp->mgmtrate & IEEE80211_RATE_VAL;
4259 len = sizeof(struct rt2870_txinfo) + sizeof(struct rt2870_txwi) + m->m_pkthdr.len;
4261 /* align end on a 4-bytes boundary */
4263 dmalen = (len + 3) & ~3;
4265 /* fill Tx info */
4267 memset(txinfo, 0, sizeof(struct rt2870_txinfo));
4269 txinfo->len = htole16(dmalen - sizeof(struct rt2870_txinfo));
4271 txinfo->qsel_flags = (RT2870_TXINFO_QSEL_EDCA << RT2870_TXINFO_QSEL_SHIFT);
4273 dmalen += 4;
4275 memset((caddr_t) txinfo + len, 0, dmalen - len);
4277 /* fill Tx wireless info */
4279 if (ni->ni_flags & IEEE80211_NODE_HT)
4280 mcs = rate;
4281 else
4282 mcs = rt2870_rate2mcs(rate);
4284 /* calculate MPDU length without padding */
4286 hdrsize = ieee80211_hdrsize(wh);
4287 hdrspace = ieee80211_hdrspace(ic, wh);
4288 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
4290 memset(txwi, 0, sizeof(struct rt2870_txwi));
4292 /* management frames do not need encryption */
4294 txwi->wcid = 0xff;
4296 /* MIMO power save */
4298 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_flags & IEEE80211_NODE_MIMO_PS))
4300 if (mcs > 7)
4302 if (ni->ni_flags & IEEE80211_NODE_MIMO_RTS)
4304 /* dynamic MIMO power save */
4306 txwi->mpdu_density_flags |=
4307 (RT2870_TXWI_FLAGS_MIMOPS << RT2870_TXWI_FLAGS_SHIFT);
4309 else
4311 /* static MIMO power save */
4313 mcs = 7;
4317 mimops = 1;
4319 else
4321 mimops = 0;
4324 pid = mcs;
4326 txwi->pid_mpdu_len = ((htole16(pid) & RT2870_TXWI_PID_MASK) <<
4327 RT2870_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2870_TXWI_MPDU_LEN_MASK) <<
4328 RT2870_TXWI_MPDU_LEN_SHIFT);
4330 stbc = (ic->ic_htcaps & IEEE80211_HTCAP_TXSTBC) &&
4331 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC);
4333 shortgi = (vap->iv_flags_ht & (IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40)) &&
4334 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_flags & (IEEE80211_NODE_SGI20 | IEEE80211_NODE_SGI40));
4336 txwi->phymode_ifs_stbc_shortgi |=
4337 ((stbc & RT2870_TXWI_STBC_MASK) << RT2870_TXWI_STBC_SHIFT) |
4338 ((shortgi & RT2870_TXWI_SHORTGI_MASK) << RT2870_TXWI_SHORTGI_SHIFT);
4340 txwi->phymode_ifs_stbc_shortgi |=
4341 ((shortgi & RT2870_TXWI_SHORTGI_MASK) << RT2870_TXWI_SHORTGI_SHIFT);
4343 if (ni->ni_flags & IEEE80211_NODE_HT)
4345 txwi->phymode_ifs_stbc_shortgi |=
4346 (RT2870_TXWI_PHYMODE_HT_MIXED << RT2870_TXWI_PHYMODE_SHIFT);
4348 else
4350 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
4352 txwi->phymode_ifs_stbc_shortgi |=
4353 (RT2870_TXWI_PHYMODE_CCK << RT2870_TXWI_PHYMODE_SHIFT);
4355 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4356 mcs |= RT2870_TXWI_MCS_SHOTPRE;
4358 else
4360 txwi->phymode_ifs_stbc_shortgi |=
4361 (RT2870_TXWI_PHYMODE_OFDM << RT2870_TXWI_PHYMODE_SHIFT);
4365 txwi->bw_mcs = (RT2870_TXWI_BW_20 << RT2870_TXWI_BW_SHIFT) |
4366 ((mcs & RT2870_TXWI_MCS_MASK) << RT2870_TXWI_MCS_SHIFT);
4368 txwi->txop = (RT2870_TXWI_TXOP_BACKOFF << RT2870_TXWI_TXOP_SHIFT);
4370 /* skip ACKs for multicast frames */
4372 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
4374 txwi->bawin_size_xflags |=
4375 (RT2870_TXWI_XFLAGS_ACK << RT2870_TXWI_XFLAGS_SHIFT);
4377 if (ni->ni_flags & IEEE80211_NODE_HT)
4379 /* preamble + plcp + signal extension + SIFS */
4381 dur = 16 + 4 + 6 + 10;
4383 else
4385 dur = ieee80211_ack_duration(ic->ic_rt, rate,
4386 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
4389 *(uint16_t *) wh->i_dur = htole16(dur);
4392 /* ask MAC to insert timestamp into probe responses */
4394 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
4395 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
4396 txwi->mpdu_density_flags |=
4397 (RT2870_TXWI_FLAGS_TS << RT2870_TXWI_FLAGS_SHIFT);
4399 if (ieee80211_radiotap_active_vap(vap))
4401 tap = &sc->txtap;
4403 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4404 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4405 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4406 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4407 tap->chan_maxpow = 0;
4409 if (ni->ni_flags & IEEE80211_NODE_HT)
4410 tap->rate = mcs | IEEE80211_RATE_MCS;
4411 else
4412 tap->rate = rate;
4414 if (mcs & RT2870_TXWI_MCS_SHOTPRE)
4415 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4417 if (shortgi)
4418 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4420 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4421 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4423 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4425 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4427 ieee80211_radiotap_tx(vap, m);
4429 wh->i_fc[1] |= IEEE80211_FC1_WEP;
4431 else
4433 ieee80211_radiotap_tx(vap, m);
4437 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t) (txwi + 1));
4439 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4440 "%s: sending MGMT frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4441 "stbc=%d, shortgi=%d, mcs=%d, mimops=%d, DMA len=%d\n",
4442 device_get_nameunit(sc->dev),
4443 qid, hdrsize, hdrspace, m->m_pkthdr.len, stbc, shortgi, mcs, mimops, dmalen);
4445 data->len = dmalen;
4446 data->m = m;
4447 data->ni = ni;
4449 STAILQ_INSERT_TAIL(&ring->pending, data, next);
4450 ring->queued++;
4452 usbd_transfer_start(sc->usb_xfer[1 + qid]);
4454 return 0;
4458 * rt2870_tx_data
4460 static int rt2870_tx_data(struct rt2870_softc *sc,
4461 struct mbuf *m, struct ieee80211_node *ni, int qid)
4463 struct ifnet *ifp;
4464 struct ieee80211com *ic;
4465 struct ieee80211vap *vap;
4466 const struct ieee80211_txparam *tp;
4467 struct rt2870_softc_node *rni;
4468 struct rt2870_softc_tx_ring *ring;
4469 struct rt2870_softc_tx_data *data;
4470 struct rt2870_txinfo *txinfo;
4471 struct rt2870_txwi *txwi;
4472 struct ieee80211_frame *wh;
4473 struct ieee80211_tx_ampdu *tx_ampdu;
4474 ieee80211_seq seqno;
4475 struct rt2870_softc_tx_radiotap_header *tap;
4476 u_int hdrsize, hdrspace;
4477 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, mpdu_density, bawin_size;
4478 uint16_t qos, len, dmalen, mpdu_len, dur;
4479 int hasqos, ac, tid, ampdu, mimops;
4481 KASSERT(qid >= 0 && qid < (sc->usb_endpoints - 1),
4482 ("%s: Tx data: invalid qid=%d\n",
4483 device_get_nameunit(sc->dev), qid));
4485 ifp = sc->ifp;
4486 ic = ifp->if_l2com;
4487 vap = ni->ni_vap;
4488 rni = (struct rt2870_softc_node *) ni;
4489 tp = ni->ni_txparms;
4491 ring = &sc->tx_ring[qid];
4492 data = STAILQ_FIRST(&ring->inactive);
4493 STAILQ_REMOVE_HEAD(&ring->inactive, next);
4494 txinfo = (struct rt2870_txinfo *) data->buf;
4495 txwi = (struct rt2870_txwi *) (txinfo + 1);
4497 wh = mtod(m, struct ieee80211_frame *);
4499 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
4501 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
4502 if (hasqos)
4504 if (IEEE80211_HAS_ADDR4(wh))
4505 qos = le16toh(*(const uint16_t *)
4506 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
4507 else
4508 qos = le16toh(*(const uint16_t *)
4509 (((struct ieee80211_qosframe *) wh)->i_qos));
4511 else
4513 qos = 0;
4516 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
4517 rate = tp->mcastrate;
4518 else if (m->m_flags & M_EAPOL)
4519 rate = tp->mgmtrate;
4520 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
4521 rate = tp->ucastrate;
4522 else
4523 rate = ni->ni_txrate;
4525 rate &= IEEE80211_RATE_VAL;
4527 len = sizeof(struct rt2870_txinfo) + sizeof(struct rt2870_txwi) + m->m_pkthdr.len;
4529 /* align end on a 4-bytes boundary */
4531 dmalen = (len + 3) & ~3;
4533 /* fill Tx info */
4535 memset(txinfo, 0, sizeof(struct rt2870_txinfo));
4537 txinfo->len = htole16(dmalen - sizeof(struct rt2870_txinfo));
4539 txinfo->qsel_flags = (RT2870_TXINFO_QSEL_EDCA << RT2870_TXINFO_QSEL_SHIFT);
4541 dmalen += 4;
4543 memset((caddr_t) txinfo + len, 0, dmalen - len);
4545 /* fill Tx wireless info */
4547 if (ni->ni_flags & IEEE80211_NODE_HT)
4548 mcs = rate;
4549 else
4550 mcs = rt2870_rate2mcs(rate);
4552 if (type == IEEE80211_FC0_TYPE_DATA)
4553 wcid = !IEEE80211_IS_MULTICAST(wh->i_addr1) ? RT2870_AID2WCID(ni->ni_associd) : 0;
4554 else
4555 wcid = 0xff;
4557 /* calculate MPDU length without padding */
4559 hdrsize = ieee80211_hdrsize(wh);
4560 hdrspace = ieee80211_hdrspace(ic, wh);
4561 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
4563 memset(txwi, 0, sizeof(struct rt2870_txwi));
4565 txwi->wcid = wcid;
4567 /* MIMO power save */
4569 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_flags & IEEE80211_NODE_MIMO_PS))
4571 if (mcs > 7)
4573 if (ni->ni_flags & IEEE80211_NODE_MIMO_RTS)
4575 /* dynamic MIMO power save */
4577 txwi->mpdu_density_flags |=
4578 (RT2870_TXWI_FLAGS_MIMOPS << RT2870_TXWI_FLAGS_SHIFT);
4580 else
4582 /* static MIMO power save */
4584 mcs = 7;
4588 mimops = 1;
4590 else
4592 mimops = 0;
4595 pid = mcs;
4597 txwi->pid_mpdu_len = ((htole16(pid) & RT2870_TXWI_PID_MASK) <<
4598 RT2870_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2870_TXWI_MPDU_LEN_MASK) <<
4599 RT2870_TXWI_MPDU_LEN_SHIFT);
4601 stbc = (ic->ic_htcaps & IEEE80211_HTCAP_TXSTBC) &&
4602 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC);
4604 shortgi = (vap->iv_flags_ht & (IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40)) &&
4605 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_flags & (IEEE80211_NODE_SGI20 | IEEE80211_NODE_SGI40));
4607 txwi->phymode_ifs_stbc_shortgi |=
4608 ((stbc & RT2870_TXWI_STBC_MASK) << RT2870_TXWI_STBC_SHIFT) |
4609 ((shortgi & RT2870_TXWI_SHORTGI_MASK) << RT2870_TXWI_SHORTGI_SHIFT);
4611 if (ni->ni_flags & IEEE80211_NODE_HT)
4613 txwi->phymode_ifs_stbc_shortgi |=
4614 (RT2870_TXWI_PHYMODE_HT_MIXED << RT2870_TXWI_PHYMODE_SHIFT);
4616 else
4618 if (ieee80211_rate2phytype(ic->ic_rt, rate) != IEEE80211_T_OFDM)
4620 txwi->phymode_ifs_stbc_shortgi |=
4621 (RT2870_TXWI_PHYMODE_CCK << RT2870_TXWI_PHYMODE_SHIFT);
4623 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4624 mcs |= RT2870_TXWI_MCS_SHOTPRE;
4626 else
4628 txwi->phymode_ifs_stbc_shortgi |=
4629 (RT2870_TXWI_PHYMODE_OFDM << RT2870_TXWI_PHYMODE_SHIFT);
4633 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_chw == 40))
4634 bw = RT2870_TXWI_BW_40;
4635 else
4636 bw = RT2870_TXWI_BW_20;
4638 txwi->bw_mcs = ((bw & RT2870_TXWI_BW_MASK) << RT2870_TXWI_BW_SHIFT) |
4639 ((mcs & RT2870_TXWI_MCS_MASK) << RT2870_TXWI_MCS_SHIFT);
4641 txwi->txop = (RT2870_TXWI_TXOP_HT << RT2870_TXWI_TXOP_SHIFT);
4643 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
4644 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
4646 txwi->bawin_size_xflags |=
4647 (RT2870_TXWI_XFLAGS_ACK << RT2870_TXWI_XFLAGS_SHIFT);
4649 if (ni->ni_flags & IEEE80211_NODE_HT)
4651 /* preamble + plcp + signal extension + SIFS */
4653 dur = 16 + 4 + 6 + 10;
4655 else
4657 dur = ieee80211_ack_duration(ic->ic_rt, rate,
4658 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
4661 *(uint16_t *) wh->i_dur = htole16(dur);
4664 /* check fo A-MPDU */
4666 if (m->m_flags & M_AMPDU_MPDU)
4668 ac = M_WME_GETAC(m);
4669 tid = WME_AC_TO_TID(ac);
4670 tx_ampdu = &ni->ni_tx_ampdu[ac];
4672 mpdu_density = RT2870_MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4673 bawin_size = tx_ampdu->txa_wnd;
4675 txwi->mpdu_density_flags |=
4676 ((mpdu_density & RT2870_TXWI_MPDU_DENSITY_MASK) << RT2870_TXWI_MPDU_DENSITY_SHIFT) |
4677 (RT2870_TXWI_FLAGS_AMPDU << RT2870_TXWI_FLAGS_SHIFT);
4679 txwi->bawin_size_xflags |=
4680 ((bawin_size & RT2870_TXWI_BAWIN_SIZE_MASK) << RT2870_TXWI_BAWIN_SIZE_SHIFT);
4682 seqno = ni->ni_txseqs[tid]++;
4684 *(uint16_t *) &wh->i_seq[0] = htole16(seqno << IEEE80211_SEQ_SEQ_SHIFT);
4686 ampdu = 1;
4688 else
4690 ampdu = 0;
4693 /* ask MAC to insert timestamp into probe responses */
4695 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
4696 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
4697 txwi->mpdu_density_flags |=
4698 (RT2870_TXWI_FLAGS_TS << RT2870_TXWI_FLAGS_SHIFT);
4700 if (ieee80211_radiotap_active_vap(vap))
4702 tap = &sc->txtap;
4704 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4705 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4706 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4707 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4708 tap->chan_maxpow = 0;
4710 if (ni->ni_flags & IEEE80211_NODE_HT)
4711 tap->rate = mcs | IEEE80211_RATE_MCS;
4712 else
4713 tap->rate = rate;
4715 if (mcs & RT2870_TXWI_MCS_SHOTPRE)
4716 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4718 if (shortgi)
4719 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4721 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4722 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4724 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4726 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4728 ieee80211_radiotap_tx(vap, m);
4730 wh->i_fc[1] |= IEEE80211_FC1_WEP;
4732 else
4734 ieee80211_radiotap_tx(vap, m);
4738 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t) (txwi + 1));
4740 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4741 "%s: sending data: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4742 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, ampdu=%d, mimops=%d, DMA len=%d\n",
4743 device_get_nameunit(sc->dev),
4744 qid, hdrsize, hdrspace, m->m_pkthdr.len,
4745 bw, stbc, shortgi, mcs, wcid, ampdu, mimops, dmalen);
4747 data->len = dmalen;
4748 data->m = m;
4749 data->ni = ni;
4751 STAILQ_INSERT_TAIL(&ring->pending, data, next);
4752 ring->queued++;
4754 usbd_transfer_start(sc->usb_xfer[1 + qid]);
4756 return 0;
4760 * rt2870_tx_raw
4762 static int rt2870_tx_raw(struct rt2870_softc *sc,
4763 struct mbuf *m, struct ieee80211_node *ni,
4764 const struct ieee80211_bpf_params *params)
4766 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4767 "%s: Tx raw\n",
4768 device_get_nameunit(sc->dev));
4770 return 0;
4774 * rt2870_rx_intr
4776 static void rt2870_rx_intr(struct usb_xfer *xfer, usb_error_t error)
4778 struct rt2870_softc *sc;
4779 struct ifnet *ifp;
4780 struct rt2870_softc_rx_ring *ring;
4781 struct rt2870_softc_rx_data *data;
4782 int len;
4784 sc = usbd_xfer_softc(xfer);
4785 ifp = sc->ifp;
4786 ring = &sc->rx_ring;
4788 RT2870_DPRINTF(sc, RT2870_DEBUG_INTR,
4789 "%s: Rx interrupt: state=%d, error=%s\n",
4790 device_get_nameunit(sc->dev),
4791 USB_GET_STATE(xfer), usbd_errstr(error));
4793 switch (USB_GET_STATE(xfer))
4795 case USB_ST_TRANSFERRED:
4796 sc->interrupts++;
4797 sc->rx_interrupts++;
4799 data = STAILQ_FIRST(&ring->active);
4800 if (data == NULL)
4801 goto setup;
4803 STAILQ_REMOVE_HEAD(&ring->active, next);
4805 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
4807 data->len = len;
4809 STAILQ_INSERT_TAIL(&ring->done, data, next);
4811 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4813 /* FALLTHROUGH */
4815 setup:
4817 case USB_ST_SETUP:
4818 data = STAILQ_FIRST(&ring->inactive);
4819 if (data == NULL)
4820 break;
4822 STAILQ_REMOVE_HEAD(&ring->inactive, next);
4824 STAILQ_INSERT_TAIL(&ring->active, data, next);
4826 usbd_xfer_set_frame_data(xfer, 0, data->buf, usbd_xfer_max_len(xfer));
4827 usbd_transfer_submit(xfer);
4828 break;
4830 default:
4831 sc->interrupts++;
4832 sc->rx_interrupts++;
4834 data = STAILQ_FIRST(&ring->active);
4835 if (data != NULL)
4837 STAILQ_REMOVE_HEAD(&ring->active, next);
4838 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
4841 if (error != USB_ERR_CANCELLED)
4843 ifp->if_ierrors++;
4845 usbd_xfer_set_stall(xfer);
4847 goto setup;
4849 break;
4854 * rt2870_tx_intr
4856 static void rt2870_tx_intr(struct usb_xfer *xfer, usb_error_t error)
4858 struct rt2870_softc *sc;
4859 struct ifnet *ifp;
4860 struct rt2870_softc_tx_ring *ring;
4861 struct rt2870_softc_tx_data *data;
4863 sc = usbd_xfer_softc(xfer);
4864 ifp = sc->ifp;
4865 ring = usbd_xfer_get_priv(xfer);
4867 usbd_xfer_status(xfer, NULL, NULL, NULL, NULL);
4869 RT2870_DPRINTF(sc, RT2870_DEBUG_INTR,
4870 "%s: Tx interrupt: state=%d, error=%s\n",
4871 device_get_nameunit(sc->dev),
4872 USB_GET_STATE(xfer), usbd_errstr(error));
4874 switch (USB_GET_STATE(xfer))
4876 case USB_ST_TRANSFERRED:
4877 sc->interrupts++;
4878 sc->tx_interrupts[ring->qid]++;
4880 data = STAILQ_FIRST(&ring->active);
4881 if (data == NULL)
4882 goto setup;
4884 STAILQ_REMOVE_HEAD(&ring->active, next);
4886 STAILQ_INSERT_TAIL(&ring->done, data, next);
4888 sc->tx_qid_pending_mask |= (1 << ring->qid);
4890 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4892 /* FALLTHROUGH */
4894 setup:
4896 case USB_ST_SETUP:
4897 data = STAILQ_FIRST(&ring->pending);
4898 if (data == NULL)
4899 break;
4901 STAILQ_REMOVE_HEAD(&ring->pending, next);
4902 STAILQ_INSERT_TAIL(&ring->active, data, next);
4904 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->len);
4905 usbd_transfer_submit(xfer);
4907 RT2870_SOFTC_UNLOCK(sc);
4909 rt2870_start(ifp);
4911 RT2870_SOFTC_LOCK(sc);
4912 break;
4914 default:
4915 sc->interrupts++;
4916 sc->tx_interrupts[ring->qid]++;
4918 data = STAILQ_FIRST(&ring->active);
4919 if (data != NULL)
4921 STAILQ_REMOVE_HEAD(&ring->active, next);
4923 printf("%s: could not transmit buffer: qid=%d, error=%s\n",
4924 device_get_nameunit(sc->dev), ring->qid, usbd_errstr(error));
4926 if (data->m != NULL)
4928 m_freem(data->m);
4929 data->m = NULL;
4932 if (data->ni != NULL)
4934 ieee80211_free_node(data->ni);
4935 data->ni = NULL;
4938 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
4940 ring->queued--;
4943 if (error != USB_ERR_CANCELLED)
4945 ifp->if_oerrors++;
4946 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4948 usbd_xfer_set_stall(xfer);
4950 goto setup;
4952 break;
4957 * rt2870_rx_done_task
4959 static void rt2870_rx_done_task(void *context, int pending)
4961 struct rt2870_softc *sc;
4962 struct ifnet *ifp;
4964 sc = context;
4965 ifp = sc->ifp;
4967 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4968 "%s: Rx done task\n",
4969 device_get_nameunit(sc->dev));
4971 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4972 return;
4974 if (rt2870_rx_eof(sc, sc->rx_process_limit) != 0)
4976 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4977 "%s: Rx done task: scheduling again\n",
4978 device_get_nameunit(sc->dev));
4980 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4985 * rt2870_tx_done_task
4987 static void rt2870_tx_done_task(void *context, int pending)
4989 struct rt2870_softc *sc;
4990 struct ifnet *ifp;
4991 int i;
4993 sc = context;
4994 ifp = sc->ifp;
4996 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4997 "%s: Tx done task\n",
4998 device_get_nameunit(sc->dev));
5000 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
5001 return;
5003 for (i = sc->usb_endpoints - 2; i >= 0; i--)
5005 if (sc->tx_qid_pending_mask & (1 << i))
5007 sc->tx_qid_pending_mask &= ~(1 << i);
5009 rt2870_tx_eof(sc, &sc->tx_ring[i]);
5013 if (sc->tx_qid_pending_mask != 0)
5015 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
5016 "%s: Tx done task: scheduling again\n",
5017 device_get_nameunit(sc->dev));
5019 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
5022 sc->tx_timer = 0;
5024 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5028 * rt2870_periodic_task
5030 static void rt2870_periodic_task(void *context, int pending)
5032 struct rt2870_softc *sc;
5033 struct ifnet *ifp;
5034 struct ieee80211com *ic;
5035 struct ieee80211vap *vap;
5037 sc = context;
5038 ifp = sc->ifp;
5039 ic = ifp->if_l2com;
5040 vap = TAILQ_FIRST(&ic->ic_vaps);
5042 RT2870_DPRINTF(sc, RT2870_DEBUG_PERIODIC,
5043 "%s: periodic task: round=%lu\n",
5044 device_get_nameunit(sc->dev), sc->periodic_round);
5046 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
5047 return;
5049 RT2870_SOFTC_LOCK(sc);
5051 sc->periodic_round++;
5053 rt2870_update_stats(sc);
5055 if ((sc->periodic_round % 10) == 0)
5057 rt2870_bbp_tuning(sc);
5059 rt2870_update_raw_counters(sc);
5061 rt2870_watchdog(sc);
5063 if (vap != NULL && vap->iv_opmode != IEEE80211_M_MONITOR && vap->iv_state == IEEE80211_S_RUN)
5065 if (vap->iv_opmode == IEEE80211_M_STA)
5066 rt2870_amrr_update_iter_func(vap, vap->iv_bss);
5067 else
5068 ieee80211_iterate_nodes(&ic->ic_sta, rt2870_amrr_update_iter_func, vap);
5072 RT2870_SOFTC_UNLOCK(sc);
5074 callout_reset(&sc->periodic_ch, hz / 10, rt2870_periodic, sc);
5078 * rt2870_cmd_task
5080 static void rt2870_cmd_task(void *context, int pending)
5082 struct rt2870_softc *sc;
5083 struct rt2870_softc_cmd_ring *ring;
5084 struct rt2870_softc_cmd *cmd;
5086 sc = context;
5087 ring = &sc->cmd_ring;
5089 while (1)
5091 RT2870_SOFTC_LOCK(sc);
5093 cmd = STAILQ_FIRST(&ring->active);
5094 if (cmd == NULL)
5096 RT2870_SOFTC_UNLOCK(sc);
5097 break;
5100 STAILQ_REMOVE_HEAD(&ring->active, next);
5102 RT2870_SOFTC_UNLOCK(sc);
5104 cmd->cb(sc, cmd->data);
5106 RT2870_SOFTC_LOCK(sc);
5108 STAILQ_INSERT_TAIL(&ring->inactive, cmd, next);
5109 ring->queued--;
5111 RT2870_SOFTC_UNLOCK(sc);
5116 * rt2870_rx_eof
5118 static int rt2870_rx_eof(struct rt2870_softc *sc, int limit)
5120 struct ifnet *ifp;
5121 struct rt2870_softc_rx_ring *ring;
5122 struct rt2870_softc_rx_data *data;
5123 uint32_t dmalen;
5124 uint8_t *buf;
5125 int nframes, len;
5127 ifp = sc->ifp;
5128 ring = &sc->rx_ring;
5130 nframes = 0;
5132 while (limit != 0)
5134 RT2870_SOFTC_LOCK(sc);
5136 data = STAILQ_FIRST(&ring->done);
5137 if (data == NULL)
5139 RT2870_SOFTC_UNLOCK(sc);
5140 break;
5143 STAILQ_REMOVE_HEAD(&ring->done, next);
5145 RT2870_SOFTC_UNLOCK(sc);
5147 buf = data->buf;
5148 len = data->len;
5150 if (len < RT2870_RX_DESC_SIZE)
5152 ifp->if_ierrors++;
5153 goto skip;
5156 while (len > (sizeof(uint32_t) + sizeof(struct rt2870_rxinfo)))
5158 dmalen = (le32toh(*(uint32_t *) buf) & 0xffff);
5160 if (dmalen == 0 || (dmalen & 3) != 0)
5162 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
5163 "%s: bad DMA len=%u\n",
5164 device_get_nameunit(sc->dev), dmalen);
5165 goto skip;
5168 if ((dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo)) > len)
5170 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
5171 "%s: bad DMA len: DMA len=%u, USB len=%d\n",
5172 device_get_nameunit(sc->dev),
5173 (unsigned int) (dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo)), len);
5174 goto skip;
5177 nframes++;
5179 rt2870_rx_frame(sc, buf + sizeof(uint32_t), dmalen);
5181 buf += (dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo));
5182 len -= (dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo));
5185 skip:
5187 RT2870_SOFTC_LOCK(sc);
5189 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5191 usbd_transfer_start(sc->usb_xfer[0]);
5193 RT2870_SOFTC_UNLOCK(sc);
5195 limit--;
5198 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
5199 "%s: Rx eof: nframes=%d\n",
5200 device_get_nameunit(sc->dev), nframes);
5202 sc->rx_packets += nframes;
5204 return (limit == 0);
5208 * rt2870_tx_eof
5210 static void rt2870_tx_eof(struct rt2870_softc *sc,
5211 struct rt2870_softc_tx_ring *ring)
5213 struct ifnet *ifp;
5214 struct rt2870_softc_tx_data *data;
5215 int nframes;
5217 ifp = sc->ifp;
5219 nframes = 0;
5221 while (1)
5223 RT2870_SOFTC_LOCK(sc);
5225 data = STAILQ_FIRST(&ring->done);
5226 if (data == NULL)
5228 RT2870_SOFTC_UNLOCK(sc);
5229 break;
5232 STAILQ_REMOVE_HEAD(&ring->done, next);
5234 rt2870_drain_fifo_stats(sc);
5236 RT2870_SOFTC_UNLOCK(sc);
5238 nframes++;
5240 if (data->m->m_flags & M_TXCB)
5241 ieee80211_process_callback(data->ni, data->m, 0);
5243 m_freem(data->m);
5245 ieee80211_free_node(data->ni);
5247 data->m = NULL;
5248 data->ni = NULL;
5250 RT2870_SOFTC_LOCK(sc);
5252 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5254 ring->queued--;
5255 ifp->if_opackets++;
5257 RT2870_SOFTC_UNLOCK(sc);
5260 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
5261 "%s: Tx eof: qid=%d, nframes=%d\n",
5262 device_get_nameunit(sc->dev), ring->qid, nframes);
5266 * rt2870_update_stats
5268 static void rt2870_update_stats(struct rt2870_softc *sc)
5270 struct ifnet *ifp;
5271 struct ieee80211com *ic;
5272 struct ieee80211vap *vap;
5273 struct ieee80211_node *ni;
5274 uint32_t stacnt[3];
5275 int beacons, noretryok, retryok, failed, underflows, zerolen;
5276 uint16_t associd;
5277 uint8_t wcid;
5279 ifp = sc->ifp;
5280 ic = ifp->if_l2com;
5281 vap = TAILQ_FIRST(&ic->ic_vaps);
5283 RT2870_DPRINTF(sc, RT2870_DEBUG_STATS,
5284 "%s: update statistic\n",
5285 device_get_nameunit(sc->dev));
5287 rt2870_drain_fifo_stats(sc);
5289 /* read and clear Tx statistic registers */
5291 rt2870_io_mac_read_multi(sc, RT2870_REG_TX_STA_CNT0,
5292 stacnt, sizeof(stacnt));
5294 stacnt[0] = le32toh(stacnt[0]);
5295 stacnt[1] = le32toh(stacnt[1]);
5296 stacnt[2] = le32toh(stacnt[2]);
5298 beacons = stacnt[0] >> 16;
5299 noretryok = stacnt[1] & 0xffff;
5300 retryok = stacnt[1] >> 16;
5301 failed = stacnt[0] & 0xffff;
5302 underflows = stacnt[2] >> 16;
5303 zerolen = stacnt[2] & 0xffff;
5305 RT2870_DPRINTF(sc, RT2870_DEBUG_STATS,
5306 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
5307 device_get_nameunit(sc->dev),
5308 beacons, noretryok, retryok, failed, underflows, zerolen);
5310 ifp->if_oerrors += failed;
5312 sc->tx_beacons += beacons;
5313 sc->tx_noretryok += noretryok;
5314 sc->tx_retryok += retryok;
5315 sc->tx_failed += failed;
5316 sc->tx_underflows += underflows;
5317 sc->tx_zerolen += zerolen;
5319 if (vap != NULL && vap->iv_opmode == IEEE80211_M_STA && vap->iv_state == IEEE80211_S_RUN)
5321 ni = vap->iv_bss;
5323 associd = (ni != NULL) ? ni->ni_associd : 0;
5324 wcid = RT2870_AID2WCID(associd);
5326 rt2870_amrr_tx_update(&sc->amrr_node[wcid],
5327 noretryok + retryok + failed, noretryok + retryok, retryok + failed);
5332 * rt2870_bbp_tuning
5334 static void rt2870_bbp_tuning(struct rt2870_softc *sc)
5336 struct ifnet *ifp;
5337 struct ieee80211com *ic;
5338 struct ieee80211vap *vap;
5339 struct ieee80211_node *ni;
5340 int chan, group;
5341 int8_t rssi, old, new;
5343 /* RT2860C does not support BBP tuning */
5345 if (sc->mac_rev == 0x28600100)
5346 return;
5348 ifp = sc->ifp;
5349 ic = ifp->if_l2com;
5350 vap = TAILQ_FIRST(&ic->ic_vaps);
5352 if ((ic->ic_flags & IEEE80211_F_SCAN) || vap == NULL ||
5353 vap->iv_opmode != IEEE80211_M_STA || vap->iv_state != IEEE80211_S_RUN)
5354 return;
5356 ni = vap->iv_bss;
5358 chan = ieee80211_chan2ieee(ic, ni->ni_chan);
5360 if (chan <= 14)
5361 group = 0;
5362 else if (chan <= 64)
5363 group = 1;
5364 else if (chan <= 128)
5365 group = 2;
5366 else
5367 group = 3;
5369 rssi = ieee80211_getrssi(vap);
5371 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
5373 new = 0x2e + sc->lna_gain[group];
5375 else
5377 if (!IEEE80211_IS_CHAN_HT40(ni->ni_chan))
5378 new = 0x32 + sc->lna_gain[group] * 5 / 3;
5379 else
5380 new = 0x3a + sc->lna_gain[group] * 5 / 3;
5383 /* Tune if absolute average RSSI is greater than -80 */
5385 if (rssi > 30)
5386 new += 0x10;
5388 old = rt2870_io_bbp_read(sc, 66);
5390 if (old != new)
5391 rt2870_io_bbp_write(sc, 66, new);
5395 * rt2870_watchdog
5397 static void rt2870_watchdog(struct rt2870_softc *sc)
5399 uint32_t tmp;
5400 int ntries;
5402 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_TXRXQ_PCNT);
5404 RT2870_DPRINTF(sc, RT2870_DEBUG_WATCHDOG,
5405 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
5406 device_get_nameunit(sc->dev), tmp);
5408 if (((tmp >> RT2870_REG_TX0Q_PCNT_SHIFT) & RT2870_REG_TX0Q_PCNT_MASK) != 0)
5410 sc->tx_queue_not_empty[0]++;
5412 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf40012);
5414 for (ntries = 0; ntries < 10; ntries++)
5416 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_TXRXQ_PCNT);
5417 if (((tmp >> RT2870_REG_TX0Q_PCNT_SHIFT) & RT2870_REG_TX0Q_PCNT_MASK) == 0)
5418 break;
5420 DELAY(1);
5423 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf40006);
5426 if (((tmp >> RT2870_REG_TX1Q_PCNT_SHIFT) & RT2870_REG_TX1Q_PCNT_MASK) != 0)
5428 sc->tx_queue_not_empty[1]++;
5430 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf4000a);
5432 for (ntries = 0; ntries < 10; ntries++)
5434 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_TXRXQ_PCNT);
5435 if (((tmp >> RT2870_REG_TX1Q_PCNT_SHIFT) & RT2870_REG_TX1Q_PCNT_MASK) == 0)
5436 break;
5438 DELAY(1);
5441 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf40006);
5446 * rt2870_drain_fifo_stats
5448 static void rt2870_drain_fifo_stats(struct rt2870_softc *sc)
5450 struct ifnet *ifp;
5451 uint32_t stats;
5452 uint8_t wcid, mcs, pid;
5453 int ok, agg, retrycnt;
5455 ifp = sc->ifp;
5457 /* drain Tx status FIFO (maxsize = 16) */
5459 while ((stats = rt2870_io_mac_read(sc, RT2870_REG_TX_STA_FIFO)) &
5460 RT2870_REG_TX_STA_FIFO_VALID)
5462 wcid = (stats >> RT2870_REG_TX_STA_FIFO_WCID_SHIFT) &
5463 RT2870_REG_TX_STA_FIFO_WCID_MASK;
5465 /* if no ACK was requested, no feedback is available */
5467 if (!(stats & RT2870_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
5468 continue;
5470 /* update AMRR statistic */
5472 ok = (stats & RT2870_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
5473 agg = (stats & RT2870_REG_TX_STA_FIFO_AGG) ? 1 : 0;
5474 mcs = (stats >> RT2870_REG_TX_STA_FIFO_MCS_SHIFT) &
5475 RT2870_REG_TX_STA_FIFO_MCS_MASK;
5476 pid = (stats >> RT2870_REG_TX_STA_FIFO_PID_SHIFT) &
5477 RT2870_REG_TX_STA_FIFO_PID_MASK;
5478 retrycnt = pid - mcs;
5480 RT2870_DPRINTF(sc, RT2870_DEBUG_STATS,
5481 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
5482 device_get_nameunit(sc->dev),
5483 wcid, ok, agg, mcs, pid, retrycnt);
5485 rt2870_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
5487 if (!ok)
5488 ifp->if_oerrors++;
5493 * rt2870_update_raw_counters
5495 static void rt2870_update_raw_counters(struct rt2870_softc *sc)
5497 uint32_t tmp;
5499 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT);
5501 sc->tx_nonagg += tmp & 0xffff;
5502 sc->tx_agg += tmp >> 16;
5504 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT0);
5506 sc->tx_ampdu += (tmp & 0xffff) / 1 + (tmp >> 16) / 2;
5508 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT1);
5510 sc->tx_ampdu += (tmp & 0xffff) / 3 + (tmp >> 16) / 4;
5512 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT2);
5514 sc->tx_ampdu += (tmp & 0xffff) / 5 + (tmp >> 16) / 6;
5516 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT3);
5518 sc->tx_ampdu += (tmp & 0xffff) / 7 + (tmp >> 16) / 8;
5520 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT4);
5522 sc->tx_ampdu += (tmp & 0xffff) / 9 + (tmp >> 16) / 10;
5524 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT5);
5526 sc->tx_ampdu += (tmp & 0xffff) / 11 + (tmp >> 16) / 12;
5528 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT6);
5530 sc->tx_ampdu += (tmp & 0xffff) / 13 + (tmp >> 16) / 14;
5532 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT7);
5534 sc->tx_ampdu += (tmp & 0xffff) / 15 + (tmp >> 16) / 16;
5536 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_STA_CNT0);
5538 sc->rx_crc_errors += tmp & 0xffff;
5539 sc->rx_phy_errors += tmp >> 16;
5541 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_STA_CNT1);
5543 sc->rx_false_ccas += tmp & 0xffff;
5544 sc->rx_plcp_errors += tmp >> 16;
5546 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_STA_CNT2);
5548 sc->rx_dup_packets += tmp & 0xffff;
5549 sc->rx_fifo_overflows += tmp >> 16;
5551 tmp = rt2870_io_mac_read(sc, RT2870_REG_TXRX_MPDU_DEN_CNT);
5553 sc->tx_mpdu_zero_density += tmp & 0xffff;
5554 sc->rx_mpdu_zero_density += tmp >> 16;
5558 * rt2870_txrx_enable
5560 static int rt2870_txrx_enable(struct rt2870_softc *sc)
5562 struct ieee80211com *ic;
5563 struct ifnet *ifp;
5564 uint32_t tmp;
5565 int ntries;
5567 ifp = sc->ifp;
5568 ic = ifp->if_l2com;
5570 /* enable Tx/Rx DMA engine */
5572 tmp = rt2870_io_mac_read(sc, RT2870_REG_SCHDMA_USB_CYC_CFG);
5574 tmp &= 0xffffff00;
5575 tmp |= 0x1e;
5577 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_CYC_CFG, tmp);
5579 if ((sc->mac_rev & 0xffff) != 0x0101)
5580 rt2870_io_mac_write(sc, RT2870_REG_TX_TXOP_CTRL_CFG, 0x583f);
5582 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, RT2870_REG_TX_ENABLE);
5584 for (ntries = 0; ntries < 200; ntries++)
5586 tmp = rt2870_io_mac_read(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG);
5587 if (!(tmp & (RT2870_REG_TX_DMA_BUSY | RT2870_REG_RX_DMA_BUSY)))
5588 break;
5590 DELAY(1000);
5593 if (ntries == 200)
5595 printf("%s: timeout waiting for DMA engine\n",
5596 device_get_nameunit(sc->dev));
5597 return -1;
5600 DELAY(50);
5602 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP0_CFG, 0);
5603 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP1_CFG,
5604 (48 << 16) | 96);
5606 tmp |= RT2870_REG_TX_WB_DDONE |
5607 RT2870_REG_RX_DMA_ENABLE |
5608 RT2870_REG_TX_DMA_ENABLE;
5610 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
5612 tmp = RT2870_REG_USB_DMA_TX_ENABLE |
5613 RT2870_REG_USB_DMA_RX_ENABLE |
5614 RT2870_REG_USB_DMA_RX_AGG_ENABLE |
5615 /* Rx agg limit in unit of 1024 byte */
5616 ((RT2870_USB_RX_BULK_BUFLEN / 1024 - 3) << RT2870_REG_USB_DMA_RX_AGG_LIMIT_SHIFT) |
5617 /* Rx agg timeout in unit of 33ns */
5618 0x80;
5620 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_DMA_CFG, tmp);
5622 /* set Rx filter */
5624 tmp = RT2870_REG_RX_FILTER_DROP_CRC_ERR |
5625 RT2870_REG_RX_FILTER_DROP_PHY_ERR;
5627 if (ic->ic_opmode != IEEE80211_M_MONITOR)
5629 tmp |= RT2870_REG_RX_FILTER_DROP_DUPL |
5630 RT2870_REG_RX_FILTER_DROP_CTS |
5631 RT2870_REG_RX_FILTER_DROP_BA |
5632 RT2870_REG_RX_FILTER_DROP_ACK |
5633 RT2870_REG_RX_FILTER_DROP_VER_ERR |
5634 RT2870_REG_RX_FILTER_DROP_CTRL_RSV |
5635 RT2870_REG_RX_FILTER_DROP_CFACK |
5636 RT2870_REG_RX_FILTER_DROP_CFEND;
5638 if (ic->ic_opmode == IEEE80211_M_STA)
5639 tmp |= RT2870_REG_RX_FILTER_DROP_RTS |
5640 RT2870_REG_RX_FILTER_DROP_PSPOLL;
5642 if (!(ifp->if_flags & IFF_PROMISC))
5643 tmp |= RT2870_REG_RX_FILTER_DROP_UC_NOME;
5646 rt2870_io_mac_write(sc, RT2870_REG_RX_FILTER_CFG, tmp);
5648 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL,
5649 RT2870_REG_RX_ENABLE | RT2870_REG_TX_ENABLE);
5651 return 0;
5655 * rt2870_alloc_rx_ring
5657 static int rt2870_alloc_rx_ring(struct rt2870_softc *sc,
5658 struct rt2870_softc_rx_ring *ring)
5660 struct rt2870_softc_rx_data *data;
5661 int i, error;
5663 STAILQ_INIT(&ring->inactive);
5664 STAILQ_INIT(&ring->active);
5665 STAILQ_INIT(&ring->done);
5667 for (i = 0; i < RT2870_SOFTC_RX_RING_DATA_COUNT; i++)
5669 data = &ring->data[i];
5671 data->buf = malloc(RT2870_USB_RX_BULK_BUFLEN, M_USBDEV, M_NOWAIT);
5672 if (data->buf == NULL)
5674 printf("%s: could not allocate Rx buffer\n",
5675 device_get_nameunit(sc->dev));
5676 error = ENOMEM;
5677 goto fail;
5680 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5683 return 0;
5685 fail:
5687 rt2870_free_rx_ring(sc, ring);
5689 return error;
5693 * rt2870_reset_rx_ring
5695 static void rt2870_reset_rx_ring(struct rt2870_softc *sc,
5696 struct rt2870_softc_rx_ring *ring)
5698 struct rt2870_softc_rx_data *data;
5699 int i;
5701 STAILQ_INIT(&ring->inactive);
5702 STAILQ_INIT(&ring->active);
5703 STAILQ_INIT(&ring->done);
5705 for (i = 0; i < RT2870_SOFTC_RX_RING_DATA_COUNT; i++)
5707 data = &ring->data[i];
5709 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5714 * rt2870_free_rx_ring
5716 static void rt2870_free_rx_ring(struct rt2870_softc *sc,
5717 struct rt2870_softc_rx_ring *ring)
5719 struct rt2870_softc_rx_data *data;
5720 int i;
5722 for (i = 0; i < RT2870_SOFTC_RX_RING_DATA_COUNT; i++)
5724 data = &ring->data[i];
5726 if (data->buf != NULL)
5728 free(data->buf, M_USBDEV);
5729 data->buf = NULL;
5735 * rt2870_alloc_tx_ring
5737 static int rt2870_alloc_tx_ring(struct rt2870_softc *sc,
5738 struct rt2870_softc_tx_ring *ring, int qid)
5740 struct rt2870_softc_tx_data *data;
5741 int i, error;
5743 STAILQ_INIT(&ring->inactive);
5744 STAILQ_INIT(&ring->pending);
5745 STAILQ_INIT(&ring->active);
5746 STAILQ_INIT(&ring->done);
5748 ring->queued = 0;
5749 ring->qid = qid;
5751 for (i = 0; i < RT2870_SOFTC_TX_RING_DATA_COUNT; i++)
5753 data = &ring->data[i];
5755 data->buf = malloc(RT2870_TX_DESC_SIZE + MJUMPAGESIZE, M_USBDEV, M_NOWAIT);
5756 if (data->buf == NULL)
5758 printf("%s: could not allocate Tx buffer\n",
5759 device_get_nameunit(sc->dev));
5760 error = ENOMEM;
5761 goto fail;
5764 memset(data->buf, 0, RT2870_TX_DESC_SIZE);
5766 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5769 return 0;
5771 fail:
5773 rt2870_free_tx_ring(sc, ring);
5775 return error;
5779 * rt2870_reset_tx_ring
5781 static void rt2870_reset_tx_ring(struct rt2870_softc *sc,
5782 struct rt2870_softc_tx_ring *ring)
5784 struct rt2870_softc_tx_data *data;
5785 int i;
5787 STAILQ_INIT(&ring->inactive);
5788 STAILQ_INIT(&ring->pending);
5789 STAILQ_INIT(&ring->active);
5790 STAILQ_INIT(&ring->done);
5792 ring->queued = 0;
5794 for (i = 0; i < RT2870_SOFTC_TX_RING_DATA_COUNT; i++)
5796 data = &ring->data[i];
5798 if (data->m != NULL)
5800 m_free(data->m);
5801 data->m = NULL;
5804 if (data->ni != NULL)
5806 ieee80211_free_node(data->ni);
5807 data->ni = NULL;
5810 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5815 * rt2870_free_tx_ring
5817 static void rt2870_free_tx_ring(struct rt2870_softc *sc,
5818 struct rt2870_softc_tx_ring *ring)
5820 struct rt2870_softc_tx_data *data;
5821 int i;
5823 for (i = 0; i < RT2870_SOFTC_TX_RING_DATA_COUNT; i++)
5825 data = &ring->data[i];
5827 if (data->buf != NULL)
5829 free(data->buf, M_USBDEV);
5830 data->buf = NULL;
5833 if (data->m != NULL)
5835 m_free(data->m);
5836 data->m = NULL;
5839 if (data->ni != NULL)
5841 ieee80211_free_node(data->ni);
5842 data->ni = NULL;
5848 * rt2870_reset_cmd_ring
5850 static void rt2870_reset_cmd_ring(struct rt2870_softc *sc,
5851 struct rt2870_softc_cmd_ring *ring)
5853 struct rt2870_softc_cmd *cmd;
5854 int i;
5856 STAILQ_INIT(&ring->inactive);
5857 STAILQ_INIT(&ring->active);
5859 ring->queued = 0;
5861 for (i = 0; i < RT2870_SOFTC_CMD_RING_CMD_COUNT; i++)
5863 cmd = &ring->cmd[i];
5865 STAILQ_INSERT_TAIL(&ring->inactive, cmd, next);
5870 * rt2870_sysctl_attach
5872 static void rt2870_sysctl_attach(struct rt2870_softc *sc)
5874 struct sysctl_ctx_list *ctx;
5875 struct sysctl_oid *tree;
5876 struct sysctl_oid *stats;
5878 ctx = device_get_sysctl_ctx(sc->dev);
5879 tree = device_get_sysctl_tree(sc->dev);
5881 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5882 "stats", CTLFLAG_RD, 0, "statistic");
5884 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5885 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5886 "all interrupts");
5888 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5889 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5890 "Rx interrupts");
5892 if (sc->usb_endpoints == RT2870_SOFTC_USB_XFER_COUNT)
5894 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5895 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5896 "Tx MGMT interrupts");
5898 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5899 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5900 "Tx HCCA interrupts");
5903 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5904 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5905 "Tx AC3 interrupts");
5907 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5908 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5909 "Tx AC2 interrupts");
5911 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5912 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5913 "Tx AC1 interrupts");
5915 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5916 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5917 "Tx AC0 interrupts");
5919 if (sc->usb_endpoints == RT2870_SOFTC_USB_XFER_COUNT)
5921 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5922 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].queued, 0,
5923 "Tx MGMT data queued");
5925 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5926 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].queued, 0,
5927 "Tx HCCA data queued");
5930 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5931 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].queued, 0,
5932 "Tx AC3 data queued");
5934 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5935 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].queued, 0,
5936 "Tx AC2 data queued");
5938 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5939 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].queued, 0,
5940 "Tx AC1 data queued");
5942 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5943 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].queued, 0,
5944 "Tx AC0 data queued");
5946 if (sc->usb_endpoints == RT2870_SOFTC_USB_XFER_COUNT)
5948 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5949 "tx_mgmt_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[5], 0,
5950 "Tx MGMT data queue full");
5952 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5953 "tx_hcca_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[4], 0,
5954 "Tx HCCA data queue full");
5957 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5958 "tx_ac3_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[3], 0,
5959 "Tx AC3 data queue full");
5961 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5962 "tx_ac2_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[2], 0,
5963 "Tx AC2 data queue full");
5965 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5966 "tx_ac1_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[1], 0,
5967 "Tx AC1 data queue full");
5969 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5970 "tx_ac0_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[0], 0,
5971 "Tx AC0 data queue full");
5973 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5974 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5975 "Tx watchdog timeouts");
5977 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5978 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
5979 "Rx mbuf allocation errors");
5981 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5982 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5983 "Tx queue 0 not empty");
5985 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5986 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5987 "Tx queue 1 not empty");
5989 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5990 "tx_beacons", CTLFLAG_RD, &sc->tx_beacons, 0,
5991 "Tx beacons");
5993 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5994 "tx_noretryok", CTLFLAG_RD, &sc->tx_noretryok, 0,
5995 "Tx successfull without retries");
5997 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5998 "tx_retryok", CTLFLAG_RD, &sc->tx_retryok, 0,
5999 "Tx successfull with retries");
6001 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6002 "tx_failed", CTLFLAG_RD, &sc->tx_failed, 0,
6003 "Tx failed");
6005 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6006 "tx_underflows", CTLFLAG_RD, &sc->tx_underflows, 0,
6007 "Tx underflows");
6009 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6010 "tx_zerolen", CTLFLAG_RD, &sc->tx_zerolen, 0,
6011 "Tx zero length");
6013 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6014 "tx_nonagg", CTLFLAG_RD, &sc->tx_nonagg, 0,
6015 "Tx non-aggregated");
6017 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6018 "tx_agg", CTLFLAG_RD, &sc->tx_agg, 0,
6019 "Tx aggregated");
6021 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6022 "tx_ampdu", CTLFLAG_RD, &sc->tx_ampdu, 0,
6023 "Tx A-MPDU");
6025 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6026 "tx_mpdu_zero_density", CTLFLAG_RD, &sc->tx_mpdu_zero_density, 0,
6027 "Tx MPDU with zero density");
6029 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6030 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
6031 "Rx packets");
6033 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6034 "rx_ampdu", CTLFLAG_RD, &sc->rx_ampdu, 0,
6035 "Rx A-MPDU");
6037 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6038 "rx_mpdu_zero_density", CTLFLAG_RD, &sc->rx_mpdu_zero_density, 0,
6039 "Rx MPDU with zero density");
6041 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6042 "rx_amsdu", CTLFLAG_RD, &sc->rx_amsdu, 0,
6043 "Rx A-MSDU");
6045 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6046 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
6047 "Rx CRC errors");
6049 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6050 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
6051 "Rx PHY errors");
6053 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6054 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
6055 "Rx false CCAs");
6057 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6058 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
6059 "Rx PLCP errors");
6061 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6062 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
6063 "Rx duplicate packets");
6065 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6066 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
6067 "Rx FIFO overflows");
6069 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6070 "rx_cipher_no_errors", CTLFLAG_RD, &sc->rx_cipher_no_errors, 0,
6071 "Rx cipher no errors");
6073 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6074 "rx_cipher_icv_errors", CTLFLAG_RD, &sc->rx_cipher_icv_errors, 0,
6075 "Rx cipher ICV errors");
6077 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6078 "rx_cipher_mic_errors", CTLFLAG_RD, &sc->rx_cipher_mic_errors, 0,
6079 "Rx cipher MIC errors");
6081 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
6082 "rx_cipher_invalid_key_errors", CTLFLAG_RD, &sc->rx_cipher_invalid_key_errors, 0,
6083 "Rx cipher invalid key errors");
6086 static device_method_t rt2870_dev_methods[] =
6088 DEVMETHOD(device_probe, rt2870_probe),
6089 DEVMETHOD(device_attach, rt2870_attach),
6090 DEVMETHOD(device_detach, rt2870_detach),
6091 { 0, 0 }
6094 static driver_t rt2870_driver =
6096 "rt2870",
6097 rt2870_dev_methods,
6098 sizeof(struct rt2870_softc),
6101 static devclass_t rt2870_dev_class;
6103 DRIVER_MODULE(rt2870, uhub, rt2870_driver, rt2870_dev_class,
6104 NULL, 0);
6106 MODULE_DEPEND(rt2870, usb, 1, 1, 1);
6107 MODULE_DEPEND(rt2870, wlan, 1, 1, 1);