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_txwi.h"
26 #include "rt2870_txinfo.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"
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_NO 1
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_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
66 #define RT2870_ACK_SIZE 14
68 #define IEEE80211_HAS_ADDR4(wh) \
69 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
71 #define RT2870_MS(_v, _f) (((_v) & _f) >> _f##_S)
72 #define RT2870_SM(_v, _f) (((_v) << _f##_S) & _f)
74 #define RT2870_USB_XFER_TIMEOUT 5000
76 #define RT2870_TX_WATCHDOG_TIMEOUT 5
79 * Data structures and types
82 struct rt2870_cmd_argv_newstate
84 enum ieee80211_state nstate
;
88 struct rt2870_cmd_argv_newassoc
92 uint8_t macaddr
[IEEE80211_ADDR_LEN
];
95 struct rt2870_cmd_argv_updatebeacon
100 struct rt2870_cmd_argv_keyset
102 struct ieee80211_key key
;
106 struct rt2870_cmd_argv_keydelete
108 struct ieee80211_key key
;
113 * Static function prototypes
116 static int rt2870_find_usb_endpoints(struct rt2870_softc
*sc
);
118 static void rt2870_init_channels(struct rt2870_softc
*sc
);
120 static void rt2870_init_channels_ht40(struct rt2870_softc
*sc
);
122 static void rt2870_init_locked(void *priv
);
124 static void rt2870_init(void *priv
);
126 static int rt2870_init_bbp(struct rt2870_softc
*sc
);
128 static void rt2870_stop_locked(void *priv
);
130 static void rt2870_stop(void *priv
);
132 static void rt2870_start(struct ifnet
*ifp
);
134 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
);
136 static int rt2870_reset(struct ifnet
*ifp
);
138 static int rt2870_newstate(struct ieee80211com
*ic
,
139 enum ieee80211_state nstate
, int arg
);
141 static void rt2870_scan_start(struct ieee80211com
*ic
);
143 static void rt2870_scan_end(struct ieee80211com
*ic
);
145 static void rt2870_set_channel(struct ieee80211com
*ic
);
147 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
);
149 static void rt2870_updateslot(struct ifnet
*ifp
);
151 static int rt2870_wme_update(struct ieee80211com
*ic
);
153 static void rt2870_update_beacon(struct ieee80211com
*ic
, int what
);
155 static void rt2870_key_update_begin(struct ieee80211com
*ic
);
157 static void rt2870_key_update_end(struct ieee80211com
*ic
);
159 static int rt2870_key_set(struct ieee80211com
*ic
,
160 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
]);
162 static int rt2870_key_delete(struct ieee80211com
*ic
,
163 const struct ieee80211_key
*k
);
165 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
166 const struct ieee80211_bpf_params
*params
);
168 static int rt2870_media_change(struct ifnet
*ifp
);
170 static struct ieee80211_node
*rt2870_node_alloc(struct ieee80211_node_table
*nt
);
172 static void rt2870_recv_action(struct ieee80211_node
*ni
,
173 const uint8_t *frm
, const uint8_t *efrm
);
175 static int rt2870_send_action(struct ieee80211_node
*ni
,
176 int category
, int action
, uint16_t args
[4]);
178 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
);
180 static void rt2870_periodic(void *arg
);
182 static void rt2870_tx_watchdog(void *arg
);
184 static int rt2870_do_async(struct rt2870_softc
*sc
,
185 void (*cb
)(struct rt2870_softc
*sc
, void *priv
),
188 static void rt2870_newstate_cb(struct rt2870_softc
*sc
, void *arg
);
190 static void rt2870_scan_start_cb(struct rt2870_softc
*sc
, void *arg
);
192 static void rt2870_scan_end_cb(struct rt2870_softc
*sc
, void *arg
);
194 static void rt2870_set_channel_cb(struct rt2870_softc
*sc
, void *arg
);
196 static void rt2870_newassoc_cb(struct rt2870_softc
*sc
, void *arg
);
198 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
);
200 static void rt2870_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
);
202 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
);
204 static void rt2870_key_update_begin_cb(struct rt2870_softc
*sc
, void *arg
);
206 static void rt2870_key_update_end_cb(struct rt2870_softc
*sc
, void *arg
);
208 static void rt2870_key_set_cb(struct rt2870_softc
*sc
, void *arg
);
210 static void rt2870_key_delete_cb(struct rt2870_softc
*sc
, void *arg
);
212 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
213 const uint8_t *bssid
);
215 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
216 const uint8_t *addr
);
218 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
);
220 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
);
222 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
);
224 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
);
226 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
);
228 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
);
230 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
);
232 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
);
234 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
);
236 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
);
238 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
);
240 static int rt2870_asic_update_beacon(struct rt2870_softc
*sc
);
242 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
);
244 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
);
246 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
247 const struct rt2870_rxwi
*rxwi
);
249 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
250 uint8_t rssi
, uint8_t rxpath
);
252 static uint8_t rt2870_rate2mcs(uint8_t rate
);
254 static int rt2870_ackrate(struct ieee80211com
*ic
, int rate
);
256 static uint16_t rt2870_txtime(int len
, int rate
, uint32_t flags
);
258 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
259 uint8_t *buf
, uint32_t dmalen
);
261 static int rt2870_tx_frame(struct rt2870_softc
*sc
,
262 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
);
264 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
265 struct mbuf
*m
, struct ieee80211_node
*ni
,
266 const struct ieee80211_bpf_params
*params
);
268 static void rt2870_rx_intr(usbd_xfer_handle xfer
,
269 usbd_private_handle priv
, usbd_status status
);
271 static void rt2870_tx_intr(usbd_xfer_handle xfer
,
272 usbd_private_handle priv
, usbd_status status
);
274 static void rt2870_rx_done_task(void *context
, int pending
);
276 static void rt2870_tx_done_task(void *context
, int pending
);
278 static void rt2870_periodic_task(void *context
, int pending
);
280 static void rt2870_cmd_task(void *context
, int pending
);
282 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
);
284 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
285 struct rt2870_softc_tx_ring
*ring
);
287 static void rt2870_update_stats(struct rt2870_softc
*sc
);
289 static void rt2870_bbp_tuning(struct rt2870_softc
*sc
);
291 static void rt2870_watchdog(struct rt2870_softc
*sc
);
293 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
);
295 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
);
297 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
298 struct rt2870_softc_rx_ring
*ring
);
300 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
301 struct rt2870_softc_rx_ring
*ring
);
303 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
304 struct rt2870_softc_rx_ring
*ring
);
306 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
307 struct rt2870_softc_tx_ring
*ring
, int qid
);
309 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
310 struct rt2870_softc_tx_ring
*ring
);
312 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
313 struct rt2870_softc_tx_ring
*ring
);
315 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
316 struct rt2870_softc_cmd_ring
*ring
);
318 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
);
324 static const struct usb_devno rt2870_usb_devno
[] =
326 { USB_VENDOR_LINKSYS4
, USB_PRODUCT_LINKSYS4_WUSB600N
}, /* Linksys WUSB600N */
327 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA140
}, /* D-Link DWA-140 */
328 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA160AREVB
}, /* D-Link DWA-160A Rev. B */
329 { USB_VENDOR_ASUS
, USB_PRODUCT_ASUS_RT2770F
}, /* Asus RT2770F */
330 { USB_VENDOR_RALINK
, USB_PRODUCT_RALINK_RT2770
}, /* Ralink RT2770 */
339 { RT2870_REG_PBF_BCN_OFFSET0
, 0xf8f0e8e0 },
340 { RT2870_REG_PBF_BCN_OFFSET1
, 0x6f77d0c8 },
341 { RT2870_REG_LEGACY_BASIC_RATE
, 0x0000013f },
342 { RT2870_REG_HT_BASIC_RATE
, 0x00008003 },
343 { RT2870_REG_SYS_CTRL
, 0x00000000 },
344 { RT2870_REG_RX_FILTER_CFG
, 0x00017f97 },
345 { RT2870_REG_BKOFF_SLOT_CFG
, 0x00000209 },
346 { RT2870_REG_TX_SW_CFG0
, 0x00000000 },
347 { RT2870_REG_TX_SW_CFG1
, 0x00080606 },
348 { RT2870_REG_TX_LINK_CFG
, 0x00001020 },
349 { RT2870_REG_TX_TIMEOUT_CFG
, 0x000a2090 },
350 { RT2870_REG_MAX_LEN_CFG
, (1 << 12) | RT2870_MAX_AGG_SIZE
},
351 { RT2870_REG_LED_CFG
, 0x7f031e46 },
352 { RT2870_REG_PBF_MAX_PCNT
, 0x1f3fbf9f },
353 { RT2870_REG_TX_RTY_CFG
, 0x47d01f0f },
354 { RT2870_REG_AUTO_RSP_CFG
, 0x00000013 },
355 { RT2870_REG_TX_CCK_PROT_CFG
, 0x05740003 },
356 { RT2870_REG_TX_OFDM_PROT_CFG
, 0x05740003 },
357 { RT2870_REG_PBF_CFG
, 0x00f40006 },
358 { RT2870_REG_TX_MM40_PROT_CFG
, 0x03f44084 },
359 { RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, 0x00000030 },
360 { RT2870_REG_TX_GF20_PROT_CFG
, 0x01744004 },
361 { RT2870_REG_TX_GF40_PROT_CFG
, 0x03f44084 },
362 { RT2870_REG_TX_MM20_PROT_CFG
, 0x01744004 },
363 { RT2870_REG_TX_TXOP_CTRL_CFG
, 0x0000583f },
364 { RT2870_REG_TX_RTS_CFG
, 0x00092b20 },
365 { RT2870_REG_TX_EXP_ACK_TIME
, 0x002400ca },
366 { RT2870_REG_HCCAPSMP_TXOP_HLDR_ET
, 0x00000002 },
367 { RT2870_REG_XIFS_TIME_CFG
, 0x33a41010 },
368 { RT2870_REG_PWR_PIN_CFG
, 0x00000003 },
369 { RT2870_REG_SCHDMA_WMM_AIFSN_CFG
, 0x00002273 },
370 { RT2870_REG_SCHDMA_WMM_CWMIN_CFG
, 0x00002344 },
371 { RT2870_REG_SCHDMA_WMM_CWMAX_CFG
, 0x000034aa },
374 #define RT2870_DEF_MAC_SIZE (sizeof(rt2870_def_mac) / sizeof(rt2870_def_mac[0]))
398 #define RT2870_DEF_BBP_SIZE (sizeof(rt2870_def_bbp) / sizeof(rt2870_def_bbp[0]))
400 SYSCTL_NODE(_hw
, OID_AUTO
, rt2870
, CTLFLAG_RD
, 0, "RT2870 driver parameters");
403 static int rt2870_debug
= 0;
404 SYSCTL_INT(_hw_rt2870
, OID_AUTO
, debug
, CTLFLAG_RW
, &rt2870_debug
, 0, "rt2870 debug level");
405 TUNABLE_INT("hw.rt2870.debug", &rt2870_debug
);
411 static int rt2870_probe(device_t dev
)
413 struct usb_attach_arg
*uaa
;
415 uaa
= device_get_ivars(dev
);
417 if (uaa
->iface
!= NULL
)
420 return (usb_lookup(rt2870_usb_devno
, uaa
->vendor
, uaa
->product
) != NULL
) ?
421 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
;
427 static int rt2870_attach(device_t dev
)
429 struct rt2870_softc
*sc
;
430 struct usb_attach_arg
*uaa
;
431 usbd_status usb_error
;
432 struct ieee80211com
*ic
;
434 int error
, ntries
, i
;
436 sc
= device_get_softc(dev
);
437 uaa
= device_get_ivars(dev
);
440 sc
->usb_dev
= uaa
->device
;
443 sc
->debug
= rt2870_debug
;
445 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev
),
446 SYSCTL_CHILDREN(device_get_sysctl_tree(dev
)), OID_AUTO
,
447 "debug", CTLFLAG_RW
, &sc
->debug
, 0, "rt2870 debug level");
450 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
452 device_get_nameunit(sc
->dev
));
454 if (usbd_set_config_no(sc
->usb_dev
, RT2870_USB_CONFIG_NO
, 0) != 0)
456 printf("%s: could not set USB configuration no\n",
457 device_get_nameunit(sc
->dev
));
461 usb_error
= usbd_device2interface_handle(sc
->usb_dev
,
462 RT2870_USB_IFACE_INDEX
, &sc
->usb_iface
);
465 printf("%s: could not get USB interface handle\n",
466 device_get_nameunit(sc
->dev
));
470 error
= rt2870_find_usb_endpoints(sc
);
474 for (ntries
= 0; ntries
< 100; ntries
++)
476 sc
->mac_rev
= rt2870_io_mac_read(sc
, RT2870_REG_MAC_CSR0
);
477 if (sc
->mac_rev
!= 0x00000000 && sc
->mac_rev
!= 0xffffffff)
485 printf("%s: timeout waiting for NIC to initialize\n",
486 device_get_nameunit(sc
->dev
));
490 rt2870_read_eeprom(sc
);
492 printf("%s: MAC/BBP RT2870 (rev 0x%08x), RF %s\n",
493 device_get_nameunit(sc
->dev
), sc
->mac_rev
,
494 rt2870_rf_name(sc
->rf_rev
));
496 /* open Tx and Rx USB bulk pipes */
498 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
500 error
= usbd_open_pipe(sc
->usb_iface
, sc
->tx_ring
[i
].usb_ep
,
501 USBD_EXCLUSIVE_USE
, &sc
->tx_ring
[i
].usb_pipe
);
504 printf("%s: could not open Tx pipe #%d: %s\n",
505 device_get_nameunit(sc
->dev
), i
, usbd_errstr(error
));
510 error
= usbd_open_pipe(sc
->usb_iface
, sc
->rx_ring
.usb_ep
,
511 USBD_EXCLUSIVE_USE
, &sc
->rx_ring
.usb_pipe
);
514 printf("%s: could not open Rx pipe: %s\n",
515 device_get_nameunit(sc
->dev
), usbd_errstr(error
));
519 /* allocate Tx and Rx rings */
521 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
523 error
= rt2870_alloc_tx_ring(sc
, &sc
->tx_ring
[i
], i
);
526 printf("%s: could not allocate Tx ring #%d\n",
527 device_get_nameunit(sc
->dev
), i
);
532 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
533 sc
->tx_ring_mgtqid
= 5;
535 sc
->tx_ring_mgtqid
= 0;
537 error
= rt2870_alloc_rx_ring(sc
, &sc
->rx_ring
);
540 printf("%s: could not allocate Rx ring\n",
541 device_get_nameunit(sc
->dev
));
545 callout_init(&sc
->periodic_ch
, 0);
546 callout_init(&sc
->tx_watchdog_ch
, 0);
548 ifp
= sc
->ifp
= if_alloc(IFT_ETHER
);
551 printf("%s: could not if_alloc()\n",
552 device_get_nameunit(sc
->dev
));
559 if_initname(ifp
, "rt2870", device_get_unit(sc
->dev
));
561 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
|
564 ifp
->if_init
= rt2870_init
;
565 ifp
->if_ioctl
= rt2870_ioctl
;
566 ifp
->if_start
= rt2870_start
;
568 IFQ_SET_MAXLEN(&ifp
->if_snd
, IFQ_MAXLEN
);
569 ifp
->if_snd
.ifq_drv_maxlen
= IFQ_MAXLEN
;
570 IFQ_SET_READY(&ifp
->if_snd
);
576 ic
->ic_phytype
= IEEE80211_T_HT
;
577 ic
->ic_opmode
= IEEE80211_M_STA
;
578 ic
->ic_state
= IEEE80211_S_INIT
;
580 ic
->ic_caps
= IEEE80211_C_MONITOR
|
587 IEEE80211_C_SHPREAMBLE
|
594 IEEE80211_C_AES_CCM
|
597 ic
->ic_htcaps
= IEEE80211_HTC_HT
|
598 IEEE80211_HTC_AMSDU
| /* A-MSDU Tx */
599 IEEE80211_HTC_AMPDU
| /* A-MPDU Tx */
600 IEEE80211_HTCAP_MAXAMSDU_3839
| /* max. A-MSDU Rx length */
601 IEEE80211_HTCAP_CHWIDTH40
| /* HT 40MHz channel width */
602 IEEE80211_HTCAP_GREENFIELD
| /* HT greenfield */
603 IEEE80211_HTCAP_SHORTGI20
| /* HT 20MHz short GI */
604 IEEE80211_HTCAP_SHORTGI40
| /* HT 40MHz short GI */
605 IEEE80211_HTCAP_SMPS_OFF
| /* SM power save mode */
606 IEEE80211_HTCAP_MCSFBACK_UNSOL
| /* unsolicited MCS feedback */
607 IEEE80211_HTCAP_HTC
; /* HTC+ */
609 /* spatial streams */
611 if (sc
->nrxpath
== 2)
612 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_2STREAM
;
613 else if (sc
->nrxpath
== 3)
614 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_3STREAM
;
616 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_1STREAM
;
619 ic
->ic_htcaps
|= IEEE80211_HTCAP_TXSTBC
;
623 if (sc
->mac_rev
!= 0x28600100)
624 ic
->ic_htcaps
|= IEEE80211_HTCAP_DELBA
;
629 ic
->ic_regdomain
= 0;
630 ic
->ic_countrycode
= CTRY_DEFAULT
;
633 rt2870_init_channels(sc
);
635 rt2870_init_channels_ht40(sc
);
637 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, sc
->mac_addr
);
639 ieee80211_ifattach(ic
);
641 sc
->newstate
= ic
->ic_newstate
;
642 ic
->ic_newstate
= rt2870_newstate
;
644 ic
->ic_reset
= rt2870_reset
;
645 ic
->ic_node_alloc
= rt2870_node_alloc
;
646 ic
->ic_scan_start
= rt2870_scan_start
;
647 ic
->ic_scan_end
= rt2870_scan_end
;
648 ic
->ic_set_channel
= rt2870_set_channel
;
649 ic
->ic_newassoc
= rt2870_newassoc
;
650 ic
->ic_updateslot
= rt2870_updateslot
;
651 ic
->ic_wme
.wme_update
= rt2870_wme_update
;
652 ic
->ic_update_beacon
= rt2870_update_beacon
;
653 ic
->ic_crypto
.cs_key_update_begin
= rt2870_key_update_begin
;
654 ic
->ic_crypto
.cs_key_update_end
= rt2870_key_update_end
;
655 ic
->ic_crypto
.cs_key_set
= rt2870_key_set
;
656 ic
->ic_crypto
.cs_key_delete
= rt2870_key_delete
;
657 ic
->ic_raw_xmit
= rt2870_raw_xmit
;
659 sc
->recv_action
= ic
->ic_recv_action
;
660 ic
->ic_recv_action
= rt2870_recv_action
;
662 sc
->send_action
= ic
->ic_send_action
;
663 ic
->ic_send_action
= rt2870_send_action
;
665 /* override Rx A-MPDU factor */
667 ic
->ic_ampdu_rxmax
= IEEE80211_HTCAP_MAXRXAMPDU_32K
;
668 ic
->ic_ampdu_density
= IEEE80211_HTCAP_MPDUDENSITY_NA
;
669 ic
->ic_ampdu_limit
= ic
->ic_ampdu_rxmax
;
671 /* hardware requires padding between 802.11 frame header and body */
673 ic
->ic_flags
|= IEEE80211_F_WME
| IEEE80211_F_DATAPAD
| IEEE80211_F_DOTH
;
675 ic
->ic_flags_ext
|= IEEE80211_FEXT_SWBMISS
;
677 ieee80211_media_init(ic
, rt2870_media_change
, ieee80211_media_status
);
679 rt2870_amrr_init(&sc
->amrr
, ic
,
681 RT2870_AMRR_MIN_SUCCESS_THRESHOLD
,
682 RT2870_AMRR_MAX_SUCCESS_THRESHOLD
,
685 bpfattach2(ifp
, DLT_IEEE802_11_RADIO
,
686 sizeof(struct ieee80211_frame
) + IEEE80211_RADIOTAP_HDRLEN
,
689 sc
->rxtap_len
= sizeof(sc
->rxtapu
);
690 sc
->rxtap
.ihdr
.it_len
= htole16(sc
->rxtap_len
);
691 sc
->rxtap
.ihdr
.it_present
= htole32(RT2870_SOFTC_RX_RADIOTAP_PRESENT
);
693 sc
->txtap_len
= sizeof(sc
->txtapu
);
694 sc
->txtap
.ihdr
.it_len
= htole16(sc
->txtap_len
);
695 sc
->txtap
.ihdr
.it_present
= htole32(RT2870_SOFTC_TX_RADIOTAP_PRESENT
);
697 /* init task queue */
699 TASK_INIT(&sc
->rx_done_task
, 0, rt2870_rx_done_task
, sc
);
700 TASK_INIT(&sc
->tx_done_task
, 0, rt2870_tx_done_task
, sc
);
701 TASK_INIT(&sc
->periodic_task
, 0, rt2870_periodic_task
, sc
);
702 TASK_INIT(&sc
->cmd_task
, 0, rt2870_cmd_task
, sc
);
704 sc
->rx_process_limit
= 100;
706 sc
->taskqueue
= taskqueue_create("rt2870_taskq", M_NOWAIT
,
707 taskqueue_thread_enqueue
, &sc
->taskqueue
);
709 taskqueue_start_threads(&sc
->taskqueue
, 1, PI_NET
, "%s taskq",
710 device_get_nameunit(sc
->dev
));
712 rt2870_sysctl_attach(sc
);
715 ieee80211_announce(ic
);
717 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, sc
->usb_dev
, sc
->dev
);
723 /* close Tx and Rx USB bulk pipes */
725 if (sc
->rx_ring
.usb_pipe
!= NULL
)
727 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
728 usbd_close_pipe(sc
->rx_ring
.usb_pipe
);
729 sc
->rx_ring
.usb_pipe
= NULL
;
732 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
734 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
736 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
737 usbd_close_pipe(sc
->tx_ring
[i
].usb_pipe
);
738 sc
->tx_ring
[i
].usb_pipe
= NULL
;
742 /* free Tx and Rx rings */
744 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
745 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
747 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
755 static int rt2870_detach(device_t dev
)
757 struct rt2870_softc
*sc
;
758 struct ieee80211com
*ic
;
762 if (!device_is_attached(dev
))
765 sc
= device_get_softc(dev
);
769 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
771 device_get_nameunit(sc
->dev
));
775 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
777 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
779 callout_stop(&sc
->periodic_ch
);
780 callout_stop(&sc
->tx_watchdog_ch
);
782 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
783 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
784 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
785 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
787 /* close Tx and Rx USB bulk pipes */
789 if (sc
->rx_ring
.usb_pipe
!= NULL
)
791 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
792 usbd_close_pipe(sc
->rx_ring
.usb_pipe
);
793 sc
->rx_ring
.usb_pipe
= NULL
;
796 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
798 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
800 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
801 usbd_close_pipe(sc
->tx_ring
[i
].usb_pipe
);
802 sc
->tx_ring
[i
].usb_pipe
= NULL
;
806 /* free Tx and Rx rings */
808 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
809 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
811 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
815 ieee80211_ifdetach(ic
);
819 taskqueue_free(sc
->taskqueue
);
821 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->usb_dev
, sc
->dev
);
827 * rt2870_find_usb_endpoints
829 static int rt2870_find_usb_endpoints(struct rt2870_softc
*sc
)
831 usb_interface_descriptor_t
*id
;
832 usb_endpoint_descriptor_t
*ed
;
835 id
= usbd_get_interface_descriptor(sc
->usb_iface
);
837 sc
->usb_endpoints
= id
->bNumEndpoints
;
838 if ((sc
->usb_endpoints
!= (RT2870_SOFTC_TX_RING_COUNT
+ 1)) &&
839 (sc
->usb_endpoints
!= (RT2870_SOFTC_TX_RING_COUNT
- 2 + 1)))
841 printf("%s: wrong number of USB endpoints=%d\n",
842 device_get_nameunit(sc
->dev
), sc
->usb_endpoints
);
846 sc
->rx_ring
.usb_ep
= -1;
848 for (i
= 0; i
< RT2870_SOFTC_TX_RING_COUNT
; i
++)
849 sc
->tx_ring
[i
].usb_ep
= -1;
851 for (i
= 0, j
= 0; i
< id
->bNumEndpoints
; i
++)
853 ed
= usbd_interface2endpoint_descriptor(sc
->usb_iface
, i
);
856 printf("%s: no endpoint descriptor #%d for USB interface\n",
857 device_get_nameunit(sc
->dev
), i
);
861 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
862 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
864 sc
->rx_ring
.usb_ep
= ed
->bEndpointAddress
;
866 else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
867 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
869 if (j
< RT2870_SOFTC_TX_RING_COUNT
)
870 sc
->tx_ring
[j
++].usb_ep
= ed
->bEndpointAddress
;
872 sc
->tx_ring
[RT2870_SOFTC_TX_RING_COUNT
- 1].usb_ep
=
873 ed
->bEndpointAddress
;
877 if ((sc
->rx_ring
.usb_ep
== -1) ||
878 ((j
!= RT2870_SOFTC_TX_RING_COUNT
) && (j
!= RT2870_SOFTC_TX_RING_COUNT
- 2)))
880 printf("%s: missing USB endpoints\n",
881 device_get_nameunit(sc
->dev
));
889 * rt2870_init_channels
891 static void rt2870_init_channels(struct rt2870_softc
*sc
)
893 struct ieee80211com
*ic
;
894 struct ieee80211_channel
*c
;
899 /* set supported channels for 2GHz band */
901 for (i
= 1; i
<= 14; i
++)
903 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
904 flags
= IEEE80211_CHAN_B
;
906 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
910 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
911 flags
= IEEE80211_CHAN_B
| IEEE80211_CHAN_HT20
;
913 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
917 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
918 flags
= IEEE80211_CHAN_G
;
920 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
924 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
925 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT20
;
927 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
932 /* set supported channels for 5GHz band */
934 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
935 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
937 for (i
= 36; i
<= 64; i
+= 4)
939 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
940 flags
= IEEE80211_CHAN_A
;
942 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
946 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
947 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
949 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
954 for (i
= 100; i
<= 140; i
+= 4)
956 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
957 flags
= IEEE80211_CHAN_A
;
959 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
963 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
964 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
966 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
971 for (i
= 149; i
<= 165; i
+= 4)
973 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
974 flags
= IEEE80211_CHAN_A
;
976 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
980 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
981 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
983 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
991 * rt2870_init_channels_ht40
993 static void rt2870_init_channels_ht40(struct rt2870_softc
*sc
)
995 struct ieee80211com
*ic
;
996 struct ieee80211_channel
*c
, *cent
, *ext
;
1001 /* set supported channels for 2GHz band */
1003 for (i
= 1; i
<= 14; i
++)
1005 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT40
;
1007 /* find the center channel */
1009 cent
= ieee80211_find_channel_byieee(ic
, i
,
1010 flags
& ~IEEE80211_CHAN_HT
);
1013 printf("%s: skip channel %d, could not find center channel\n",
1014 device_get_nameunit(sc
->dev
), i
);
1018 /* find the extension channel */
1020 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1021 flags
& ~IEEE80211_CHAN_HT
);
1024 printf("%s: skip channel %d, could not find extension channel\n",
1025 device_get_nameunit(sc
->dev
), i
);
1029 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1032 c
->ic_extieee
= ext
->ic_ieee
;
1033 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1034 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1036 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1039 c
->ic_extieee
= cent
->ic_ieee
;
1040 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1041 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1044 /* set supported channels for 5GHz band */
1046 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
1047 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
1049 for (i
= 36; i
<= 64; i
+= 4)
1051 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1053 /* find the center channel */
1055 cent
= ieee80211_find_channel_byieee(ic
, i
,
1056 flags
& ~IEEE80211_CHAN_HT
);
1059 printf("%s: skip channel %d, could not find center channel\n",
1060 device_get_nameunit(sc
->dev
), i
);
1064 /* find the extension channel */
1066 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1067 flags
& ~IEEE80211_CHAN_HT
);
1070 printf("%s: skip channel %d, could not find extension channel\n",
1071 device_get_nameunit(sc
->dev
), i
);
1075 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1078 c
->ic_extieee
= ext
->ic_ieee
;
1079 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1080 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1082 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1085 c
->ic_extieee
= cent
->ic_ieee
;
1086 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1087 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1090 for (i
= 100; i
<= 140; i
+= 4)
1092 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1094 /* find the center channel */
1096 cent
= ieee80211_find_channel_byieee(ic
, i
,
1097 flags
& ~IEEE80211_CHAN_HT
);
1100 printf("%s: skip channel %d, could not find center channel\n",
1101 device_get_nameunit(sc
->dev
), i
);
1105 /* find the extension channel */
1107 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1108 flags
& ~IEEE80211_CHAN_HT
);
1111 printf("%s: skip channel %d, could not find extension channel\n",
1112 device_get_nameunit(sc
->dev
), i
);
1116 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1119 c
->ic_extieee
= ext
->ic_ieee
;
1120 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1121 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1123 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1126 c
->ic_extieee
= cent
->ic_ieee
;
1127 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1128 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1131 for (i
= 149; i
<= 165; i
+= 4)
1133 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1135 /* find the center channel */
1137 cent
= ieee80211_find_channel_byieee(ic
, i
,
1138 flags
& ~IEEE80211_CHAN_HT
);
1141 printf("%s: skip channel %d, could not find center channel\n",
1142 device_get_nameunit(sc
->dev
), i
);
1146 /* find the extension channel */
1148 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1149 flags
& ~IEEE80211_CHAN_HT
);
1152 printf("%s: skip channel %d, could not find extension channel\n",
1153 device_get_nameunit(sc
->dev
), i
);
1157 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1160 c
->ic_extieee
= ext
->ic_ieee
;
1161 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1162 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1164 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1167 c
->ic_extieee
= cent
->ic_ieee
;
1168 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1169 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1175 * rt2870_init_locked
1177 static void rt2870_init_locked(void *priv
)
1179 struct rt2870_softc
*sc
;
1180 struct ieee80211com
*ic
;
1182 struct rt2870_softc_rx_data
*data
;
1183 int ntries
, error
, i
;
1184 uint32_t tmp
, stacnt
[6];
1190 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1191 "%s: initializing\n",
1192 device_get_nameunit(sc
->dev
));
1194 if (!(sc
->flags
& RT2870_SOFTC_FLAGS_UCODE_LOADED
))
1196 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1197 "%s: loading 8051 microcode\n",
1198 device_get_nameunit(sc
->dev
));
1200 error
= rt2870_io_mcu_load_ucode(sc
, rt2870_ucode
, sizeof(rt2870_ucode
));
1203 printf("%s: could not load 8051 microcode\n",
1204 device_get_nameunit(sc
->dev
));
1208 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1209 "%s: 8051 microcode was successfully loaded\n",
1210 device_get_nameunit(sc
->dev
));
1212 sc
->flags
|= RT2870_SOFTC_FLAGS_UCODE_LOADED
;
1215 /* wait while DMA engine is busy */
1217 for (ntries
= 0; ntries
< 100; ntries
++)
1219 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1220 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1228 printf("%s: timeout waiting for DMA engine\n",
1229 device_get_nameunit(sc
->dev
));
1234 tmp
|= RT2870_REG_TX_WB_DDONE
;
1236 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1238 /* PBF hardware reset */
1240 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_SYS_CTRL
);
1244 rt2870_io_mac_write(sc
, RT2870_REG_PBF_SYS_CTRL
, tmp
);
1246 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1247 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1249 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1251 rt2870_io_mcu_reset(sc
);
1253 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1255 /* init Tx power per rate */
1257 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
1259 if (sc
->txpow_rate_20mhz
[i
] == 0xffffffff)
1262 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
),
1263 sc
->txpow_rate_20mhz
[i
]);
1266 for (i
= 0; i
< RT2870_DEF_MAC_SIZE
; i
++)
1267 rt2870_io_mac_write(sc
, rt2870_def_mac
[i
].reg
,
1268 rt2870_def_mac
[i
].val
);
1270 /* wait while MAC is busy */
1272 for (ntries
= 0; ntries
< 100; ntries
++)
1274 if (!(rt2870_io_mac_read(sc
, RT2870_REG_STATUS_CFG
) &
1275 (RT2870_REG_STATUS_TX_BUSY
| RT2870_REG_STATUS_RX_BUSY
)))
1283 printf("%s: timeout waiting for MAC\n",
1284 device_get_nameunit(sc
->dev
));
1288 /* clear Host to MCU mailbox */
1290 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX_BBP_AGENT
, 0);
1291 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX
, 0);
1295 error
= rt2870_init_bbp(sc
);
1299 /* set up maximum buffer sizes */
1301 tmp
= (1 << 12) | RT2870_MAX_AGG_SIZE
;
1303 if (sc
->mac_rev
>= 0x28720200 && sc
->mac_rev
< 0x30700200)
1309 rt2870_io_mac_write(sc
, RT2870_REG_MAX_LEN_CFG
, tmp
);
1311 /* set mac address */
1313 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, IF_LLADDR(ifp
));
1315 rt2870_asic_set_macaddr(sc
, ic
->ic_myaddr
);
1317 /* clear statistic registers */
1319 rt2870_io_mac_read_multi(sc
, RT2870_REG_RX_STA_CNT0
,
1320 stacnt
, sizeof(stacnt
));
1322 /* send LEDs operating mode to microcontroller */
1324 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED1
,
1325 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[0]);
1326 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED2
,
1327 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[1]);
1328 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED3
,
1329 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[2]);
1331 /* write vendor-specific BBP values (from EEPROM) */
1333 for (i
= 0; i
< RT2870_SOFTC_BBP_EEPROM_COUNT
; i
++)
1335 if (sc
->bbp_eeprom
[i
].reg
== 0x00 ||
1336 sc
->bbp_eeprom
[i
].reg
== 0xff)
1339 rt2870_io_bbp_write(sc
, sc
->bbp_eeprom
[i
].reg
,
1340 sc
->bbp_eeprom
[i
].val
);
1343 /* disable non-existing Rx chains */
1345 tmp
= rt2870_io_bbp_read(sc
, 3);
1347 tmp
&= ~((1 << 4) | (1 << 3));
1349 if (sc
->nrxpath
== 3)
1351 else if (sc
->nrxpath
== 2)
1354 rt2870_io_bbp_write(sc
, 3, tmp
);
1356 /* disable non-existing Tx chains */
1358 tmp
= rt2870_io_bbp_read(sc
, 1);
1360 tmp
&= ~((1 << 4) | (1 << 3));
1362 if (sc
->ntxpath
== 2)
1365 rt2870_io_bbp_write(sc
, 1, tmp
);
1367 /* set current channel */
1369 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1371 /* turn radio LED on */
1373 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
);
1375 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_BOOT
,
1376 RT2870_REG_H2M_TOKEN_NO_INTR
, 0);
1378 /* set RTS threshold */
1380 rt2870_asic_update_rtsthreshold(sc
);
1384 rt2870_asic_update_txpower(sc
);
1386 /* set up protection mode */
1388 rt2870_asic_updateprot(sc
);
1390 /* clear key tables */
1392 rt2870_asic_clear_keytables(sc
);
1394 /* clear beacon frame space (entries = 8, entry size = 512) */
1396 rt2870_io_mac_set_region_4(sc
, RT2870_REG_BEACON_BASE(0), 0, 1024);
1398 /* enable Tx/Rx DMA engine */
1400 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
);
1405 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
, tmp
);
1407 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1408 rt2870_io_mac_write(sc
, RT2870_REG_TX_TXOP_CTRL_CFG
, 0x583f);
1410 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, RT2870_REG_TX_ENABLE
);
1412 for (ntries
= 0; ntries
< 200; ntries
++)
1414 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1415 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1423 printf("%s: timeout waiting for DMA engine\n",
1424 device_get_nameunit(sc
->dev
));
1430 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
, 0);
1431 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
1434 tmp
|= RT2870_REG_TX_WB_DDONE
|
1435 RT2870_REG_RX_DMA_ENABLE
|
1436 RT2870_REG_TX_DMA_ENABLE
;
1438 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1440 tmp
= RT2870_REG_USB_DMA_TX_ENABLE
|
1441 RT2870_REG_USB_DMA_RX_ENABLE
|
1442 RT2870_REG_USB_DMA_RX_AGG_ENABLE
|
1443 /* Rx agg limit in unit of 1024 byte */
1444 ((RT2870_USB_RX_BULK_BUFLEN
/ 1024 - 3) << RT2870_REG_USB_DMA_RX_AGG_LIMIT_SHIFT
) |
1445 /* Rx agg timeout in unit of 33ns */
1448 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, tmp
);
1452 tmp
= RT2870_REG_RX_FILTER_DROP_CRC_ERR
|
1453 RT2870_REG_RX_FILTER_DROP_PHY_ERR
;
1455 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1457 tmp
|= RT2870_REG_RX_FILTER_DROP_DUPL
|
1458 RT2870_REG_RX_FILTER_DROP_CTS
|
1459 RT2870_REG_RX_FILTER_DROP_BA
|
1460 RT2870_REG_RX_FILTER_DROP_ACK
|
1461 RT2870_REG_RX_FILTER_DROP_VER_ERR
|
1462 RT2870_REG_RX_FILTER_DROP_CTRL_RSV
|
1463 RT2870_REG_RX_FILTER_DROP_CFACK
|
1464 RT2870_REG_RX_FILTER_DROP_CFEND
;
1466 if (ic
->ic_opmode
== IEEE80211_M_STA
)
1467 tmp
|= RT2870_REG_RX_FILTER_DROP_RTS
|
1468 RT2870_REG_RX_FILTER_DROP_PSPOLL
;
1470 if (!(ifp
->if_flags
& IFF_PROMISC
))
1471 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
1474 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
1476 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1477 RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1479 /* clear garbage interrupts */
1481 tmp
= rt2870_io_mac_read(sc
, 0x1300);
1483 taskqueue_unblock(sc
->taskqueue
);
1485 /* init Tx and Rx rings */
1487 for(i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1488 rt2870_reset_tx_ring(sc
, &sc
->tx_ring
[i
]);
1490 rt2870_reset_rx_ring(sc
, &sc
->rx_ring
);
1492 rt2870_reset_cmd_ring(sc
, &sc
->cmd_ring
);
1494 /* start up the receive pipe */
1496 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
1498 data
= &sc
->rx_ring
.data
[i
];
1500 STAILQ_INSERT_TAIL(&sc
->rx_ring
.active
, data
, next
);
1502 usbd_setup_xfer(data
->xfer
, sc
->rx_ring
.usb_pipe
, sc
, data
->buf
,
1503 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
1504 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
1506 usbd_transfer(data
->xfer
);
1509 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
1510 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
1512 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1514 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
1515 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
1519 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
1522 sc
->periodic_round
= 0;
1524 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
1530 rt2870_stop_locked(sc
);
1536 static void rt2870_init(void *priv
)
1538 struct rt2870_softc
*sc
;
1542 rt2870_init_locked(sc
);
1548 static int rt2870_init_bbp(struct rt2870_softc
*sc
)
1553 for (ntries
= 0; ntries
< 20; ntries
++)
1555 tmp
= rt2870_io_bbp_read(sc
, 0);
1556 if (tmp
!= 0x00 && tmp
!= 0xff)
1560 if (tmp
== 0x00 || tmp
== 0xff)
1562 printf("%s: timeout waiting for BBP to wakeup\n",
1563 device_get_nameunit(sc
->dev
));
1567 for (i
= 0; i
< RT2870_DEF_BBP_SIZE
; i
++)
1568 rt2870_io_bbp_write(sc
, rt2870_def_bbp
[i
].reg
,
1569 rt2870_def_bbp
[i
].val
);
1571 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1572 rt2870_io_bbp_write(sc
, 84, 0x19);
1580 static void rt2870_stop_locked(void *priv
)
1582 struct rt2870_softc
*sc
;
1583 struct ieee80211com
*ic
;
1592 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1594 device_get_nameunit(sc
->dev
));
1598 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1599 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
1601 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
1603 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
1605 callout_stop(&sc
->periodic_ch
);
1606 callout_stop(&sc
->tx_watchdog_ch
);
1608 taskqueue_block(sc
->taskqueue
);
1610 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
1611 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
1612 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
1613 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
1615 /* clear key tables */
1617 rt2870_asic_clear_keytables(sc
);
1621 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SYS_CTRL
);
1623 tmp
&= ~(RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1625 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, tmp
);
1627 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1631 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1632 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1633 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1635 /* abort any pending transfers */
1637 if (sc
->rx_ring
.usb_pipe
!= NULL
)
1638 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
1640 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1641 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
1642 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
1648 static void rt2870_stop(void *priv
)
1650 struct rt2870_softc
*sc
;
1654 rt2870_stop_locked(sc
);
1660 static void rt2870_start(struct ifnet
*ifp
)
1662 struct rt2870_softc
*sc
;
1663 struct ieee80211com
*ic
;
1664 struct ieee80211_node
*ni
;
1665 struct ether_header
*eh
;
1672 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
1677 IF_POLL(&ic
->ic_mgtq
, m
);
1680 if (sc
->tx_ring
[sc
->tx_ring_mgtqid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1682 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1683 "%s: if_start: Tx ring with qid=%d is full\n",
1684 device_get_nameunit(sc
->dev
), sc
->tx_ring_mgtqid
);
1686 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1688 sc
->tx_data_queue_full
[sc
->tx_ring_mgtqid
]++;
1693 IF_DEQUEUE(&ic
->ic_mgtq
, m
);
1695 ni
= (struct ieee80211_node
*) m
->m_pkthdr
.rcvif
;
1696 m
->m_pkthdr
.rcvif
= NULL
;
1698 if (bpf_peers_present(ic
->ic_rawbpf
))
1699 bpf_mtap(ic
->ic_rawbpf
, m
);
1701 if (rt2870_tx_frame(sc
, m
, ni
, sc
->tx_ring_mgtqid
) != 0)
1706 if (ic
->ic_state
!= IEEE80211_S_RUN
)
1709 IF_POLL(&ifp
->if_snd
, m
);
1713 IFQ_DRV_DEQUEUE(&ifp
->if_snd
, m
);
1715 if (ic
->ic_flags
& IEEE80211_F_SCAN
)
1716 ieee80211_cancel_scan(ic
);
1718 if (m
->m_len
< sizeof(struct ether_header
) &&
1719 !(m
= m_pullup(m
, sizeof (struct ether_header
))))
1722 eh
= mtod(m
, struct ether_header
*);
1724 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1727 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1728 "%s: if_start: could not find Tx node\n",
1729 device_get_nameunit(sc
->dev
));
1736 ieee80211_classify(ic
, m
, ni
);
1738 qid
= M_WME_GETAC(m
);
1740 if (sc
->tx_ring
[qid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1742 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1743 "%s: if_start: Tx ring with qid=%d is full\n",
1744 device_get_nameunit(sc
->dev
), qid
);
1747 ieee80211_free_node(ni
);
1749 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1752 sc
->tx_data_queue_full
[qid
]++;
1759 m
= ieee80211_encap(ic
, m
, ni
);
1762 ieee80211_free_node(ni
);
1769 if (bpf_peers_present(ic
->ic_rawbpf
))
1770 bpf_mtap(ic
->ic_rawbpf
, m
);
1772 if (rt2870_tx_frame(sc
, m
, ni
, qid
) != 0)
1774 ieee80211_free_node(ni
);
1776 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1783 sc
->tx_timer
= RT2870_TX_WATCHDOG_TIMEOUT
;
1785 ic
->ic_lastdata
= ticks
;
1787 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
1794 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1796 struct rt2870_softc
*sc
;
1797 struct ieee80211com
*ic
;
1808 if (ifp
->if_flags
& IFF_UP
)
1810 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1812 if ((ifp
->if_flags
^ sc
->if_flags
) & IFF_PROMISC
)
1813 rt2870_asic_update_promisc(sc
);
1817 rt2870_init_locked(sc
);
1822 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1823 rt2870_stop_locked(sc
);
1826 sc
->if_flags
= ifp
->if_flags
;
1830 error
= ieee80211_ioctl(ic
, cmd
, data
);
1833 if (error
== ENETRESET
)
1835 if ((ifp
->if_flags
& IFF_UP
) &&
1836 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
) &&
1837 (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
))
1839 rt2870_stop_locked(sc
);
1840 rt2870_init_locked(sc
);
1852 static int rt2870_reset(struct ifnet
*ifp
)
1854 struct rt2870_softc
*sc
;
1855 struct ieee80211com
*ic
;
1860 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1863 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1871 static int rt2870_newstate(struct ieee80211com
*ic
,
1872 enum ieee80211_state nstate
, int arg
)
1874 struct rt2870_softc
*sc
;
1876 struct rt2870_cmd_argv_newstate cmd_argv
;
1881 cmd_argv
.nstate
= nstate
;
1884 rt2870_do_async(sc
, rt2870_newstate_cb
, &cmd_argv
,
1885 sizeof(struct rt2870_cmd_argv_newstate
));
1893 static void rt2870_scan_start(struct ieee80211com
*ic
)
1895 struct rt2870_softc
*sc
;
1901 rt2870_do_async(sc
, rt2870_scan_start_cb
, NULL
, 0);
1907 static void rt2870_scan_end(struct ieee80211com
*ic
)
1909 struct rt2870_softc
*sc
;
1915 rt2870_do_async(sc
, rt2870_scan_end_cb
, NULL
, 0);
1919 * rt2870_set_channel
1921 static void rt2870_set_channel(struct ieee80211com
*ic
)
1923 struct rt2870_softc
*sc
;
1929 rt2870_do_async(sc
, rt2870_set_channel_cb
, NULL
, 0);
1935 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
)
1937 struct rt2870_softc
*sc
;
1938 struct ieee80211com
*ic
;
1940 struct rt2870_cmd_argv_newassoc cmd_argv
;
1946 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
1947 cmd_argv
.isnew
= isnew
;
1948 memcpy(cmd_argv
.macaddr
, ni
->ni_macaddr
, IEEE80211_ADDR_LEN
);
1950 rt2870_do_async(sc
, rt2870_newassoc_cb
, &cmd_argv
,
1951 sizeof(struct rt2870_cmd_argv_newassoc
));
1957 static void rt2870_updateslot(struct ifnet
*ifp
)
1959 struct rt2870_softc
*sc
;
1963 rt2870_do_async(sc
, rt2870_updateslot_cb
, NULL
, 0);
1969 static int rt2870_wme_update(struct ieee80211com
*ic
)
1971 struct rt2870_softc
*sc
;
1977 rt2870_do_async(sc
, rt2870_wme_update_cb
, NULL
, 0);
1983 * rt2870_update_beacon
1985 static void rt2870_update_beacon(struct ieee80211com
*ic
, int what
)
1987 struct rt2870_softc
*sc
;
1989 struct rt2870_cmd_argv_updatebeacon cmd_argv
;
1994 cmd_argv
.what
= what
;
1996 rt2870_do_async(sc
, rt2870_update_beacon_cb
, &cmd_argv
,
1997 sizeof(struct rt2870_cmd_argv_updatebeacon
));
2001 * rt2870_key_update_begin
2003 static void rt2870_key_update_begin(struct ieee80211com
*ic
)
2005 struct rt2870_softc
*sc
;
2011 rt2870_do_async(sc
, rt2870_key_update_begin_cb
, NULL
, 0);
2015 * rt2870_key_update_end
2017 static void rt2870_key_update_end(struct ieee80211com
*ic
)
2019 struct rt2870_softc
*sc
;
2025 rt2870_do_async(sc
, rt2870_key_update_end_cb
, NULL
, 0);
2031 static int rt2870_key_set(struct ieee80211com
*ic
,
2032 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
2034 struct rt2870_softc
*sc
;
2036 struct ieee80211_node
*ni
;
2037 struct rt2870_cmd_argv_keyset cmd_argv
;
2039 if (k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_WEP
&&
2040 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_TKIP
&&
2041 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_AES_CCM
)
2047 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2050 ni
= ieee80211_find_node(&ic
->ic_sta
, mac
);
2052 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2053 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2055 if ((ic
->ic_opmode
== IEEE80211_M_HOSTAP
) && (ni
!= NULL
))
2056 ieee80211_free_node(ni
);
2058 rt2870_do_async(sc
, rt2870_key_set_cb
, &cmd_argv
,
2059 sizeof(struct rt2870_cmd_argv_keyset
));
2067 static int rt2870_key_delete(struct ieee80211com
*ic
,
2068 const struct ieee80211_key
*k
)
2070 struct rt2870_softc
*sc
;
2072 struct ieee80211_node
*ni
;
2073 struct rt2870_cmd_argv_keydelete cmd_argv
;
2079 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2080 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2082 rt2870_do_async(sc
, rt2870_key_delete_cb
, &cmd_argv
,
2083 sizeof(struct rt2870_cmd_argv_keydelete
));
2091 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
2092 const struct ieee80211_bpf_params
*params
)
2094 struct ieee80211com
*ic
;
2096 struct rt2870_softc
*sc
;
2106 * rt2870_media_change
2108 static int rt2870_media_change(struct ifnet
*ifp
)
2110 struct rt2870_softc
*sc
;
2115 error
= ieee80211_media_change(ifp
);
2116 if (error
!= ENETRESET
)
2119 if ((ifp
->if_flags
& IFF_UP
) && (ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2131 static struct ieee80211_node
*rt2870_node_alloc(struct ieee80211_node_table
*nt
)
2133 return malloc(sizeof(struct rt2870_softc_node
),
2134 M_80211_NODE
, M_NOWAIT
| M_ZERO
);
2138 * rt2870_recv_action
2140 static void rt2870_recv_action(struct ieee80211_node
*ni
,
2141 const uint8_t *frm
, const uint8_t *efrm
)
2143 struct rt2870_softc
*sc
;
2144 struct ieee80211com
*ic
;
2146 const struct ieee80211_action
*ia
;
2147 struct ieee80211_tx_ampdu
*tx_ampdu
;
2148 uint16_t associd
, status
, baparamset
;
2150 int tid
, bufsize
, ac
;
2157 ia
= (const struct ieee80211_action
*) frm
;
2159 sc
->recv_action(ni
, frm
, efrm
);
2161 if (ia
->ia_category
!= IEEE80211_ACTION_CAT_BA
)
2164 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2165 wcid
= RT2870_AID2WCID(associd
);
2167 switch (ia
->ia_action
)
2169 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2170 case IEEE80211_ACTION_BA_ADDBA_REQUEST
:
2171 baparamset
= LE_READ_2(frm
+ 3);
2172 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2173 bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2175 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2176 "%s: received ADDBA request: associd=0x%04x, tid=%d, bufsize=%d\n",
2177 device_get_nameunit(sc
->dev
), associd
, tid
, bufsize
);
2179 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2181 tmp
|= (0x10000 << tid
);
2183 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2186 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2187 case IEEE80211_ACTION_BA_ADDBA_RESPONSE
:
2188 status
= LE_READ_2(frm
+ 3);
2189 baparamset
= LE_READ_2(frm
+ 4);
2190 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2191 bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2192 ac
= TID_TO_WME_AC(tid
);
2193 tx_ampdu
= &ni
->ni_tx_ampdu
[ac
];
2195 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2196 "%s: received ADDBA response: associd=0x%04x, status=%d, tid=%d, bufsize=%d\n",
2197 device_get_nameunit(sc
->dev
), associd
, status
, tid
, bufsize
);
2199 if (sc
->mac_rev
>= 0x28720200)
2201 if (tx_ampdu
->txa_wnd
> 13)
2202 tx_ampdu
->txa_wnd
= 13;
2206 if (tx_ampdu
->txa_wnd
> 7)
2207 tx_ampdu
->txa_wnd
= 7;
2210 if (status
== IEEE80211_STATUS_SUCCESS
)
2212 sc
->tx_ampdu_sessions
++;
2214 rt2870_asic_updateprot(sc
);
2218 /* IEEE80211_ACTION_BA_DELBA */
2219 case IEEE80211_ACTION_BA_DELBA
:
2220 baparamset
= LE_READ_2(frm
+ 2);
2221 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2223 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2224 "%s: received DELBA request: associd=0x%04x, tid=%d\n",
2225 device_get_nameunit(sc
->dev
), associd
, tid
);
2227 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2229 tmp
&= ~(0x10000 << tid
);
2231 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2237 * rt2870_send_action
2239 static int rt2870_send_action(struct ieee80211_node
*ni
,
2240 int category
, int action
, uint16_t args
[4])
2242 struct rt2870_softc
*sc
;
2243 struct ieee80211com
*ic
;
2245 uint16_t associd
, baparamset
;
2254 ret
= sc
->send_action(ni
, category
, action
, args
);
2256 if (category
!= IEEE80211_ACTION_CAT_BA
)
2259 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2260 wcid
= RT2870_AID2WCID(associd
);
2264 /* IEEE80211_ACTION_BA_DELBA */
2265 case IEEE80211_ACTION_BA_DELBA
:
2266 baparamset
= RT2870_SM(args
[0], IEEE80211_DELBAPS_TID
) | args
[1];
2268 tid
= RT2870_MS(baparamset
, IEEE80211_DELBAPS_TID
);
2270 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2271 "%s: sending DELBA request: associd=0x%04x, tid=%d\n",
2272 device_get_nameunit(sc
->dev
), associd
, tid
);
2274 if (RT2870_MS(baparamset
, IEEE80211_DELBAPS_INIT
) != IEEE80211_DELBAPS_INIT
)
2276 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2278 tmp
&= ~(0x10000 << tid
);
2280 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2284 sc
->tx_ampdu_sessions
--;
2286 rt2870_asic_updateprot(sc
);
2295 * rt2870_amrr_update_iter_func
2297 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
)
2299 struct rt2870_softc
*sc
;
2300 struct ieee80211com
*ic
;
2306 /* only associated stations */
2308 if (ni
->ni_associd
!= 0)
2310 wcid
= RT2870_AID2WCID(ni
->ni_associd
);
2312 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2313 "%s: AMRR node: wcid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2314 device_get_nameunit(sc
->dev
),
2315 wcid
, sc
->amrr_node
[wcid
].txcnt
, sc
->amrr_node
[wcid
].success
, sc
->amrr_node
[wcid
].retrycnt
);
2317 rt2870_amrr_choose(ni
, &sc
->amrr_node
[wcid
]);
2319 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2320 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2321 device_get_nameunit(sc
->dev
),
2322 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2324 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2325 (ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
] | IEEE80211_RATE_MCS
) :
2326 (ni
->ni_rates
.rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
),
2327 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2328 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2329 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2336 static void rt2870_periodic(void *arg
)
2338 struct rt2870_softc
*sc
;
2344 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
2346 device_get_nameunit(sc
->dev
));
2348 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2351 taskqueue_enqueue(sc
->taskqueue
, &sc
->periodic_task
);
2355 * rt2870_tx_watchdog
2357 static void rt2870_tx_watchdog(void *arg
)
2359 struct rt2870_softc
*sc
;
2365 if (sc
->tx_timer
== 0)
2368 if (--sc
->tx_timer
== 0)
2370 printf("%s: Tx watchdog timeout: resetting\n",
2371 device_get_nameunit(sc
->dev
));
2373 rt2870_stop_locked(sc
);
2374 rt2870_init_locked(sc
);
2378 sc
->tx_watchdog_timeouts
++;
2381 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
2387 static int rt2870_do_async(struct rt2870_softc
*sc
,
2388 void (*cb
)(struct rt2870_softc
*sc
, void *arg
),
2392 struct rt2870_softc_cmd_ring
*ring
;
2393 struct rt2870_softc_cmd
*cmd
;
2397 ring
= &sc
->cmd_ring
;
2399 RT2870_SOFTC_LOCK(sc
);
2401 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2403 RT2870_SOFTC_UNLOCK(sc
);
2407 if (ring
->queued
>= RT2870_SOFTC_CMD_RING_CMD_COUNT
)
2409 RT2870_SOFTC_UNLOCK(sc
);
2413 cmd
= STAILQ_FIRST(&ring
->inactive
);
2414 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
2419 memcpy(cmd
->data
, arg
, len
);
2421 STAILQ_INSERT_TAIL(&ring
->active
, cmd
, next
);
2424 run_cmd_task
= (ring
->queued
== 1);
2426 RT2870_SOFTC_UNLOCK(sc
);
2429 taskqueue_enqueue(sc
->taskqueue
, &sc
->cmd_task
);
2435 * rt2870_newstate_cb
2437 static void rt2870_newstate_cb(struct rt2870_softc
*sc
, void *arg
)
2439 struct ieee80211com
*ic
;
2440 enum ieee80211_state nstate
;
2441 struct ieee80211_node
*ni
;
2442 struct rt2870_cmd_argv_newstate
*cmd_argv
;
2447 nstate
= cmd_argv
->nstate
;
2449 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2450 "%s: newstate: %s -> %s\n",
2451 device_get_nameunit(sc
->dev
),
2452 ieee80211_state_name
[ic
->ic_state
], ieee80211_state_name
[nstate
]);
2454 error
= sc
->newstate(ic
, nstate
, cmd_argv
->arg
);
2458 /* turn link LED off */
2460 if (nstate
!= IEEE80211_S_RUN
)
2461 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
2465 case IEEE80211_S_INIT
:
2466 rt2870_asic_disable_tsf_sync(sc
);
2469 case IEEE80211_S_RUN
:
2472 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2473 rt2870_rf_set_chan(sc
, ni
->ni_chan
);
2475 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2477 rt2870_asic_enable_mrr(sc
);
2478 rt2870_asic_set_txpreamble(sc
);
2479 rt2870_asic_set_basicrates(sc
);
2480 rt2870_asic_set_bssid(sc
, ni
->ni_bssid
);
2483 if (ic
->ic_opmode
== IEEE80211_M_STA
)
2484 rt2870_newassoc(ni
, 1);
2486 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
||
2487 ic
->ic_opmode
== IEEE80211_M_IBSS
)
2489 error
= rt2870_asic_update_beacon(sc
);
2494 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2495 rt2870_asic_enable_tsf_sync(sc
);
2497 /* turn link LED on */
2499 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2501 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
|
2502 (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
) ?
2503 RT2870_LED_CMD_LINK_2GHZ
: RT2870_LED_CMD_LINK_5GHZ
));
2513 * rt2870_scan_start_cb
2515 static void rt2870_scan_start_cb(struct rt2870_softc
*sc
, void *arg
)
2517 struct ieee80211com
*ic
;
2523 rt2870_asic_disable_tsf_sync(sc
);
2527 * rt2870_scan_end_cb
2529 static void rt2870_scan_end_cb(struct rt2870_softc
*sc
, void *arg
)
2531 struct ieee80211com
*ic
;
2532 struct ieee80211_node
*ni
;
2537 rt2870_asic_enable_tsf_sync(sc
);
2541 * rt2870_set_channel_cb
2543 static void rt2870_set_channel_cb(struct rt2870_softc
*sc
, void *arg
)
2545 struct ieee80211com
*ic
;
2549 RT2870_DPRINTF(sc
, RT2870_DEBUG_CHAN
,
2550 "%s: set channel: channel=%u, HT%s%s\n",
2551 device_get_nameunit(sc
->dev
),
2552 ieee80211_chan2ieee(ic
, ic
->ic_curchan
),
2553 !IEEE80211_IS_CHAN_HT(ic
->ic_curchan
) ? " disabled" :
2554 IEEE80211_IS_CHAN_HT20(ic
->ic_curchan
) ? "20":
2555 IEEE80211_IS_CHAN_HT40U(ic
->ic_curchan
) ? "40U" : "40D",
2556 (ic
->ic_flags
& IEEE80211_F_SCAN
) ? ", scanning" : "");
2558 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
2562 * rt2870_newassoc_cb
2564 static void rt2870_newassoc_cb(struct rt2870_softc
*sc
, void *arg
)
2566 struct ieee80211com
*ic
;
2567 struct ieee80211_node
*ni
;
2568 struct rt2870_cmd_argv_newassoc
*cmd_argv
;
2571 uint8_t *macaddr
, wcid
;
2576 associd
= cmd_argv
->associd
;
2577 isnew
= cmd_argv
->isnew
;
2578 macaddr
= cmd_argv
->macaddr
;
2580 wcid
= RT2870_AID2WCID(associd
);
2582 RT2870_DPRINTF(sc
, RT2870_DEBUG_NODE
,
2583 "%s: new association: wcid=0x%02x, "
2584 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2585 device_get_nameunit(sc
->dev
), wcid
,
2586 ether_sprintf(macaddr
),
2587 (ni
->ni_flags
& IEEE80211_NODE_QOS
) ? "enabled" : "disabled",
2588 (ni
->ni_flags
& IEEE80211_NODE_ERP
) ? "enabled" : "disabled",
2589 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? "enabled" : "disabled");
2591 rt2870_io_mac_write_multi(sc
, RT2870_REG_WCID(wcid
),
2592 macaddr
, IEEE80211_ADDR_LEN
);
2594 rt2870_amrr_node_init(&sc
->amrr
, &sc
->amrr_node
[wcid
], ni
);
2596 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2597 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2598 device_get_nameunit(sc
->dev
),
2599 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2601 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2602 (ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
] | IEEE80211_RATE_MCS
) :
2603 (ni
->ni_rates
.rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
),
2604 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2605 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2606 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2608 rt2870_asic_updateprot(sc
);
2609 rt2870_asic_updateslot(sc
);
2610 rt2870_asic_set_txpreamble(sc
);
2614 * rt2870_updateslot_cb
2616 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
)
2618 rt2870_asic_updateslot(sc
);
2622 * rt2870_wme_update_cb
2624 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
)
2626 rt2870_asic_wme_update(sc
);
2630 * rt2870_update_beacon_cb
2632 static void rt2870_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
)
2634 struct rt2870_cmd_argv_updatebeacon
*cmd_argv
;
2638 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2639 "%s: update beacon\n",
2640 device_get_nameunit(sc
->dev
));
2642 rt2870_asic_update_beacon(sc
);
2646 * rt2870_key_update_begin_cb
2648 static void rt2870_key_update_begin_cb(struct rt2870_softc
*sc
, void *arg
)
2654 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2655 "%s: key update begin\n",
2656 device_get_nameunit(sc
->dev
));
2658 taskqueue_block(sc
->taskqueue
);
2660 IF_LOCK(&ifp
->if_snd
);
2664 * rt2870_key_update_end_cb
2666 static void rt2870_key_update_end_cb(struct rt2870_softc
*sc
, void *arg
)
2672 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2673 "%s: key update end\n",
2674 device_get_nameunit(sc
->dev
));
2676 IF_UNLOCK(&ifp
->if_snd
);
2678 taskqueue_unblock(sc
->taskqueue
);
2684 static void rt2870_key_set_cb(struct rt2870_softc
*sc
, void *arg
)
2686 struct ieee80211com
*ic
;
2687 struct ieee80211_key
*k
;
2688 struct rt2870_cmd_argv_keyset
*cmd_argv
;
2689 uint16_t associd
, key_base
, keymode_base
;
2690 uint8_t mode
, vapid
, wcid
, iv
[8];
2696 associd
= cmd_argv
->associd
;
2698 switch (k
->wk_cipher
->ic_cipher
)
2700 case IEEE80211_CIPHER_WEP
:
2701 if(k
->wk_keylen
< 8)
2702 mode
= RT2870_REG_CIPHER_MODE_WEP40
;
2704 mode
= RT2870_REG_CIPHER_MODE_WEP104
;
2707 case IEEE80211_CIPHER_TKIP
:
2708 mode
= RT2870_REG_CIPHER_MODE_TKIP
;
2711 case IEEE80211_CIPHER_AES_CCM
:
2712 mode
= RT2870_REG_CIPHER_MODE_AES_CCMP
;
2719 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2720 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2721 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, associd
, mode
,
2722 (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2724 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2726 /* install pairwise key */
2729 wcid
= RT2870_AID2WCID(associd
);
2730 key_base
= RT2870_REG_PKEY(wcid
);
2732 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
)
2736 iv
[3] = (k
->wk_keyix
<< 6);
2740 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2742 iv
[0] = (k
->wk_keytsc
>> 8);
2743 iv
[1] = ((iv
[0] | 0x20) & 0x7f);
2744 iv
[2] = k
->wk_keytsc
;
2750 iv
[0] = k
->wk_keytsc
;
2751 iv
[1] = k
->wk_keytsc
>> 8;
2755 iv
[3] = ((k
->wk_keyix
<< 6) | IEEE80211_WEP_EXTIV
);
2756 iv
[4] = (k
->wk_keytsc
>> 16);
2757 iv
[5] = (k
->wk_keytsc
>> 24);
2758 iv
[6] = (k
->wk_keytsc
>> 32);
2759 iv
[7] = (k
->wk_keytsc
>> 40);
2761 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2762 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2763 device_get_nameunit(sc
->dev
),
2764 iv
[0], iv
[1], iv
[2], iv
[3], iv
[4], iv
[5], iv
[6], iv
[7]);
2767 rt2870_io_mac_write_multi(sc
, RT2870_REG_IVEIV(wcid
), iv
, 8);
2769 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2771 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2773 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2775 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2776 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2780 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2781 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2786 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2789 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2790 (mode
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2792 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2795 if ((k
->wk_flags
& IEEE80211_KEY_GROUP
) ||
2796 (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
))
2798 /* install group key */
2801 key_base
= RT2870_REG_SKEY(vapid
, k
->wk_keyix
);
2802 keymode_base
= RT2870_REG_SKEY_MODE(vapid
);
2804 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2806 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2808 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2810 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2811 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2815 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2816 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2821 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2824 tmp
= rt2870_io_mac_read(sc
, keymode_base
);
2826 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2827 tmp
|= (mode
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2829 rt2870_io_mac_write(sc
, keymode_base
, tmp
);
2834 * rt2870_key_delete_cb
2836 static void rt2870_key_delete_cb(struct rt2870_softc
*sc
, void *arg
)
2838 struct ieee80211com
*ic
;
2839 struct ieee80211_key
*k
;
2840 struct rt2870_cmd_argv_keydelete
*cmd_argv
;
2842 uint8_t vapid
, wcid
;
2848 associd
= cmd_argv
->associd
;
2850 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2851 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2852 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, associd
,
2853 (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2855 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2857 /* remove pairwise key */
2860 wcid
= RT2870_AID2WCID(associd
);
2862 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2863 (RT2870_REG_CIPHER_MODE_NONE
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2865 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2869 /* remove group key */
2873 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SKEY_MODE(vapid
));
2875 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2876 tmp
|= (RT2870_REG_CIPHER_MODE_NONE
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2878 rt2870_io_mac_write(sc
, RT2870_REG_SKEY_MODE(vapid
), tmp
);
2883 * rt2870_asic_set_bssid
2885 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
2886 const uint8_t *bssid
)
2890 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2891 "%s: set bssid: bssid=%s\n",
2892 device_get_nameunit(sc
->dev
), ether_sprintf(bssid
));
2894 tmp
= bssid
[0] | (bssid
[1]) << 8 | (bssid
[2] << 16) | (bssid
[3] << 24);
2896 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW0
, tmp
);
2898 tmp
= bssid
[4] | (bssid
[5] << 8);
2900 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW1
, tmp
);
2904 * rt2870_asic_set_macaddr
2906 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
2907 const uint8_t *addr
)
2911 tmp
= addr
[0] | (addr
[1] << 8) | (addr
[2] << 16) | (addr
[3] << 24);
2913 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW0
, tmp
);
2915 tmp
= addr
[4] | (addr
[5] << 8);
2917 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW1
, tmp
);
2921 * rt2870_asic_enable_tsf_sync
2923 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
)
2925 struct ieee80211com
*ic
;
2930 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2931 "%s: enabling TSF\n",
2932 device_get_nameunit(sc
->dev
));
2934 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
2937 tmp
|= ic
->ic_bss
->ni_intval
* 16;
2938 tmp
|= (RT2870_REG_TSF_TIMER_ENABLE
| RT2870_REG_TBTT_TIMER_ENABLE
);
2940 if (ic
->ic_opmode
== IEEE80211_M_STA
)
2942 tmp
|= (RT2870_REG_TSF_SYNC_MODE_STA
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2944 else if (ic
->ic_opmode
== IEEE80211_M_IBSS
)
2946 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
2947 tmp
|= (RT2870_REG_TSF_SYNC_MODE_IBSS
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2949 else if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
)
2951 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
2952 tmp
|= (RT2870_REG_TSF_SYNC_MODE_HOSTAP
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2955 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
2959 * rt2870_asic_disable_tsf_sync
2961 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
)
2965 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2966 "%s: disabling TSF\n",
2967 device_get_nameunit(sc
->dev
));
2969 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
2971 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
2972 RT2870_REG_TSF_TIMER_ENABLE
|
2973 RT2870_REG_TBTT_TIMER_ENABLE
);
2975 tmp
&= ~(RT2870_REG_TSF_SYNC_MODE_MASK
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2976 tmp
|= (RT2870_REG_TSF_SYNC_MODE_DISABLE
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2978 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
2982 * rt2870_asic_enable_mrr
2984 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
)
2986 #define CCK(mcs) (mcs)
2987 #define OFDM(mcs) ((1 << 3) | (mcs))
2988 #define HT(mcs) (mcs)
2990 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG0
,
2991 (OFDM(6) << 28) | /* 54 -> 48 */
2992 (OFDM(5) << 24) | /* 48 -> 36 */
2993 (OFDM(4) << 20) | /* 36 -> 24 */
2994 (OFDM(3) << 16) | /* 24 -> 18 */
2995 (OFDM(2) << 12) | /* 18 -> 12 */
2996 (OFDM(1) << 8) | /* 12 -> 9 */
2997 (OFDM(0) << 4) | /* 9 -> 6 */
2998 OFDM(0)); /* 6 -> 6 */
3000 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG1
,
3001 (CCK(2) << 12) | /* 11 -> 5.5 */
3002 (CCK(1) << 8) | /* 5.5 -> 2 */
3003 (CCK(0) << 4) | /* 2 -> 1 */
3004 CCK(0)); /* 1 -> 1 */
3006 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG0
,
3016 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG1
,
3032 * rt2870_asic_set_txpreamble
3034 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
)
3036 struct ieee80211com
*ic
;
3041 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3042 "%s: %s short Tx preamble\n",
3043 device_get_nameunit(sc
->dev
),
3044 (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) ? "enabling" : "disabling");
3046 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_AUTO_RSP_CFG
);
3048 tmp
&= ~RT2870_REG_CCK_SHORT_ENABLE
;
3050 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
3051 tmp
|= RT2870_REG_CCK_SHORT_ENABLE
;
3053 rt2870_io_mac_write(sc
, RT2870_REG_AUTO_RSP_CFG
, tmp
);
3057 * rt2870_asic_set_basicrates
3059 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
)
3061 struct ieee80211com
*ic
;
3065 if (ic
->ic_curmode
== IEEE80211_MODE_11B
)
3066 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x3);
3067 else if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3068 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x150);
3070 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x15f);
3074 * rt2870_asic_update_rtsthreshold
3076 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
)
3078 struct ieee80211com
*ic
;
3084 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3085 "%s: updating RTS threshold: %d\n",
3086 device_get_nameunit(sc
->dev
), ic
->ic_rtsthreshold
);
3088 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_RTS_CFG
);
3090 tmp
&= ~(RT2870_REG_TX_RTS_THRESHOLD_MASK
<< RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3092 if (ic
->ic_flags_ext
& IEEE80211_FEXT_AMSDU_TX
)
3095 threshold
= ic
->ic_rtsthreshold
;
3097 tmp
|= ((threshold
& RT2870_REG_TX_RTS_THRESHOLD_MASK
) <<
3098 RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3100 rt2870_io_mac_write(sc
, RT2870_REG_TX_RTS_CFG
, tmp
);
3104 * rt2870_asic_update_txpower
3106 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
)
3108 struct ieee80211com
*ic
;
3109 uint32_t *txpow_rate
;
3117 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3118 "%s: updating Tx power: %d\n",
3119 device_get_nameunit(sc
->dev
), ic
->ic_txpowlimit
);
3121 if (!IEEE80211_IS_CHAN_HT40(ic
->ic_curchan
))
3123 txpow_rate
= sc
->txpow_rate_20mhz
;
3127 if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_curchan
))
3128 txpow_rate
= sc
->txpow_rate_40mhz_2ghz
;
3130 txpow_rate
= sc
->txpow_rate_40mhz_5ghz
;
3135 val
= rt2870_io_bbp_read(sc
, 1);
3138 if (ic
->ic_txpowlimit
> 90)
3142 else if (ic
->ic_txpowlimit
> 60)
3146 else if (ic
->ic_txpowlimit
> 30)
3150 else if (ic
->ic_txpowlimit
> 15)
3154 else if (ic
->ic_txpowlimit
> 9)
3164 rt2870_io_bbp_write(sc
, 1, val
);
3166 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
3168 if (txpow_rate
[i
] == 0xffffffff)
3171 tmp
= rt2870_read_eeprom_txpow_rate_add_delta(txpow_rate
[i
], delta
);
3173 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
), tmp
);
3178 * rt2870_asic_update_promisc
3180 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
)
3185 ifp
= sc
->ic
.ic_ifp
;
3187 printf("%s: %s promiscuous mode\n",
3188 device_get_nameunit(sc
->dev
),
3189 (ifp
->if_flags
& IFF_PROMISC
) ? "entering" : "leaving");
3191 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_FILTER_CFG
);
3193 tmp
&= ~RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3195 if (!(ifp
->if_flags
& IFF_PROMISC
))
3196 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3198 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
3202 * rt2870_asic_updateprot
3204 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
)
3206 struct ieee80211com
*ic
;
3207 uint32_t cck_prot
, ofdm_prot
, mm20_prot
, mm40_prot
, gf20_prot
, gf40_prot
;
3209 enum ieee80211_protmode htprotmode
;
3213 /* CCK frame protection */
3215 cck_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3216 RT2870_REG_TXOP_ALLOW_ALL
| RT2870_REG_PROT_CTRL_NONE
;
3218 /* set up protection frame phy mode and rate (MCS code) */
3220 if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3221 cck_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3222 (0 << RT2870_REG_PROT_MCS_SHIFT
);
3224 cck_prot
|= ((RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3225 (3 << RT2870_REG_PROT_MCS_SHIFT
));
3227 rt2870_io_mac_write(sc
, RT2870_REG_TX_CCK_PROT_CFG
, cck_prot
);
3229 /* OFDM frame protection */
3231 ofdm_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3232 RT2870_REG_TXOP_ALLOW_ALL
;
3234 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3236 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3237 "%s: updating protection mode: b/g protection mode=%s\n",
3238 device_get_nameunit(sc
->dev
),
3239 (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3240 ((ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3242 if (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
)
3243 ofdm_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3244 else if (ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
)
3245 ofdm_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3247 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3251 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3252 "%s: updating protection mode: b/g protection mode=%s\n",
3253 device_get_nameunit(sc
->dev
), "none");
3255 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3258 rt2870_io_mac_write(sc
, RT2870_REG_TX_OFDM_PROT_CFG
, ofdm_prot
);
3260 /* HT frame protection */
3262 if ((ic
->ic_opmode
== IEEE80211_M_STA
) && (ic
->ic_state
== IEEE80211_S_RUN
))
3263 htopmode
= ic
->ic_bss
->ni_htopmode
;
3265 htopmode
= ic
->ic_curhtprotmode
;
3267 htprotmode
= ic
->ic_htprotmode
;
3269 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3271 if (sc
->tx_ampdu_sessions
> 0)
3273 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3274 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3275 device_get_nameunit(sc
->dev
));
3277 htopmode
= IEEE80211_HTINFO_OPMODE_MIXED
;
3278 htprotmode
= IEEE80211_PROT_RTSCTS
;
3281 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3282 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3283 device_get_nameunit(sc
->dev
),
3284 htopmode
& IEEE80211_HTINFO_OPMODE
,
3285 (htprotmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3286 ((htprotmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3288 switch (htopmode
& IEEE80211_HTINFO_OPMODE
)
3290 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3291 case IEEE80211_HTINFO_OPMODE_HT20PR
:
3292 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3293 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3294 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3295 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3296 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3298 gf20_prot
= mm20_prot
;
3300 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3301 RT2870_REG_TXOP_ALLOW_ALL
|
3302 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3303 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3305 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3306 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3307 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3308 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3310 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3312 gf40_prot
= mm40_prot
;
3315 /* IEEE80211_HTINFO_OPMODE_MIXED */
3316 case IEEE80211_HTINFO_OPMODE_MIXED
:
3317 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
|
3318 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3319 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
;
3321 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3322 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3323 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3325 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3326 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3328 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3329 mm20_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3330 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3331 mm20_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3333 mm20_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3335 gf20_prot
= mm20_prot
;
3337 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_TXOP_ALLOW_ALL
;
3339 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3340 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3341 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3343 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3344 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3346 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3347 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3348 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3349 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3351 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3353 gf40_prot
= mm40_prot
;
3357 * IEEE80211_HTINFO_OPMODE_PURE
3358 * IEEE80211_HTINFO_OPMODE_PROTOPT
3360 case IEEE80211_HTINFO_OPMODE_PURE
:
3361 case IEEE80211_HTINFO_OPMODE_PROTOPT
:
3363 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3364 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3365 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3366 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3367 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3369 gf20_prot
= mm20_prot
;
3371 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3372 RT2870_REG_TXOP_ALLOW_ALL
|
3373 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3374 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3376 gf40_prot
= mm40_prot
;
3380 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM20_PROT_CFG
, mm20_prot
);
3381 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM40_PROT_CFG
, mm40_prot
);
3382 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF20_PROT_CFG
, gf20_prot
);
3383 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF40_PROT_CFG
, gf40_prot
);
3387 * rt2870_asic_updateslot
3389 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
)
3391 struct ieee80211com
*ic
;
3396 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3397 "%s: %s short slot time\n",
3398 device_get_nameunit(sc
->dev
),
3399 (ic
->ic_flags
& (IEEE80211_F_SHSLOT
| IEEE80211_F_BURST
)) ? "enabling" : "disabling");
3401 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BKOFF_SLOT_CFG
);
3404 tmp
|= (ic
->ic_flags
& (IEEE80211_F_SHSLOT
| IEEE80211_F_BURST
)) ? 9 : 20;
3406 rt2870_io_mac_write(sc
, RT2870_REG_BKOFF_SLOT_CFG
, tmp
);
3410 * rt2870_asic_wme_update
3412 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
)
3414 struct ieee80211com
*ic
;
3415 struct ieee80211_wme_state
*wme
;
3416 const struct wmeParams
*wmep
;
3421 wmep
= wme
->wme_chanParams
.cap_wmeParams
;
3423 RT2870_DPRINTF(sc
, RT2870_DEBUG_WME
,
3424 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3425 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3426 device_get_nameunit(sc
->dev
),
3427 wmep
[WME_AC_VO
].wmep_aifsn
,
3428 wmep
[WME_AC_VO
].wmep_logcwmin
, wmep
[WME_AC_VO
].wmep_logcwmax
,
3429 wmep
[WME_AC_VO
].wmep_txopLimit
,
3430 wmep
[WME_AC_VI
].wmep_aifsn
,
3431 wmep
[WME_AC_VI
].wmep_logcwmin
, wmep
[WME_AC_VI
].wmep_logcwmax
,
3432 wmep
[WME_AC_VI
].wmep_txopLimit
,
3433 wmep
[WME_AC_BK
].wmep_aifsn
,
3434 wmep
[WME_AC_BK
].wmep_logcwmin
, wmep
[WME_AC_BK
].wmep_logcwmax
,
3435 wmep
[WME_AC_BK
].wmep_txopLimit
,
3436 wmep
[WME_AC_BE
].wmep_aifsn
,
3437 wmep
[WME_AC_BE
].wmep_logcwmin
, wmep
[WME_AC_BE
].wmep_logcwmax
,
3438 wmep
[WME_AC_BE
].wmep_txopLimit
);
3440 for (i
= 0; i
< WME_NUM_AC
; i
++)
3441 rt2870_io_mac_write(sc
, RT2870_REG_TX_EDCA_AC_CFG(i
),
3442 (wmep
[i
].wmep_logcwmax
<< 16) | (wmep
[i
].wmep_logcwmin
<< 12) |
3443 (wmep
[i
].wmep_aifsn
<< 8) | wmep
[i
].wmep_txopLimit
);
3445 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_AIFSN_CFG
,
3446 (wmep
[WME_AC_VO
].wmep_aifsn
<< 12) | (wmep
[WME_AC_VI
].wmep_aifsn
<< 8) |
3447 (wmep
[WME_AC_BK
].wmep_aifsn
<< 4) | wmep
[WME_AC_BE
].wmep_aifsn
);
3449 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMIN_CFG
,
3450 (wmep
[WME_AC_VO
].wmep_logcwmin
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmin
<< 8) |
3451 (wmep
[WME_AC_BK
].wmep_logcwmin
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmin
);
3453 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMAX_CFG
,
3454 (wmep
[WME_AC_VO
].wmep_logcwmax
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmax
<< 8) |
3455 (wmep
[WME_AC_BK
].wmep_logcwmax
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmax
);
3457 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
,
3458 (wmep
[WME_AC_BK
].wmep_txopLimit
<< 16) | wmep
[WME_AC_BE
].wmep_txopLimit
);
3460 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
3461 (wmep
[WME_AC_VO
].wmep_txopLimit
<< 16) | wmep
[WME_AC_VI
].wmep_txopLimit
);
3465 * rt2870_asic_update_beacon
3467 static int rt2870_asic_update_beacon(struct rt2870_softc
*sc
)
3469 struct ieee80211com
*ic
;
3471 struct rt2870_txwi txwi
;
3477 m
= ieee80211_beacon_alloc(ic
->ic_bss
, &sc
->beacon_offsets
);
3481 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
3482 mcs
= rt2870_rate2mcs(rate
);
3484 memset(&txwi
, 0, sizeof(struct rt2870_txwi
));
3487 txwi
.pid_mpdu_len
= ((htole16(m
->m_pkthdr
.len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
3488 RT2870_TXWI_MPDU_LEN_SHIFT
);
3489 txwi
.txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
3490 txwi
.mpdu_density_flags
|=
3491 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
3492 txwi
.bawin_size_xflags
|=
3493 (RT2870_TXWI_XFLAGS_NSEQ
<< RT2870_TXWI_XFLAGS_SHIFT
);
3497 txwi
.phymode_ifs_stbc_shortgi
=
3498 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
3500 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
3501 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
3505 txwi
.phymode_ifs_stbc_shortgi
=
3506 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
3509 txwi
.bw_mcs
= (RT2870_TXWI_BW_20
<< RT2870_TXWI_BW_SHIFT
) |
3510 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
3512 /* disable temporarily TSF sync */
3514 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3516 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
3517 RT2870_REG_TSF_TIMER_ENABLE
|
3518 RT2870_REG_TBTT_TIMER_ENABLE
);
3520 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3522 /* write Tx wireless info and beacon frame to on-chip memory */
3524 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0),
3525 &txwi
, sizeof(struct rt2870_txwi
));
3527 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0) + sizeof(struct rt2870_txwi
),
3528 mtod(m
, uint8_t *), m
->m_pkthdr
.len
);
3530 /* enable again TSF sync */
3532 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3534 tmp
|= (RT2870_REG_BCN_TX_ENABLE
|
3535 RT2870_REG_TSF_TIMER_ENABLE
|
3536 RT2870_REG_TBTT_TIMER_ENABLE
);
3538 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3546 * rt2870_asic_clear_keytables
3548 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
)
3552 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3554 for (i
= 0; i
< 256; i
++)
3556 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
), 0xffffffff);
3557 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
) + 4, 0x0000ffff);
3560 /* clear WCID attribute table (entries = 256, entry size = 4) */
3562 rt2870_io_mac_set_region_4(sc
, RT2870_REG_WCID_ATTR(0), 0, 256);
3564 /* clear IV/EIV table (entries = 256, entry size = 8) */
3566 rt2870_io_mac_set_region_4(sc
, RT2870_REG_IVEIV(0), 0, 2 * 256);
3568 /* clear pairwise key table (entries = 256, entry size = 32) */
3570 rt2870_io_mac_set_region_4(sc
, RT2870_REG_PKEY(0), 0, 8 * 256);
3572 /* clear shared key table (entries = 32, entry size = 32) */
3574 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY(0, 0), 0, 8 * 32);
3576 /* clear shared key mode (entries = 32, entry size = 2) */
3578 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY_MODE(0), 0, 16);
3584 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
)
3586 uint8_t mcs
, phymode
;
3589 mcs
= (rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
;
3590 phymode
= (rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
3591 RT2870_RXWI_PHYMODE_MASK
;
3597 case RT2870_RXWI_PHYMODE_CCK
:
3598 switch (mcs
& ~RT2870_RXWI_MCS_SHOTPRE
)
3600 case 0: rate
= 2; break; /* 1 Mbps */
3601 case 1: rate
= 4; break; /* 2 MBps */
3602 case 2: rate
= 11; break; /* 5.5 Mbps */
3603 case 3: rate
= 22; break; /* 11 Mbps */
3607 case RT2870_RXWI_PHYMODE_OFDM
:
3610 case 0: rate
= 12; break; /* 6 Mbps */
3611 case 1: rate
= 18; break; /* 9 Mbps */
3612 case 2: rate
= 24; break; /* 12 Mbps */
3613 case 3: rate
= 36; break; /* 18 Mbps */
3614 case 4: rate
= 48; break; /* 24 Mbps */
3615 case 5: rate
= 72; break; /* 36 Mbps */
3616 case 6: rate
= 96; break; /* 48 Mbps */
3617 case 7: rate
= 108; break; /* 54 Mbps */
3621 case RT2870_RXWI_PHYMODE_HT_MIXED
:
3622 case RT2870_RXWI_PHYMODE_HT_GF
:
3630 * rt2870_maxrssi_rxpath
3632 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
3633 const struct rt2870_rxwi
*rxwi
)
3639 if (sc
->nrxpath
> 1)
3640 if (rxwi
->rssi
[1] > rxwi
->rssi
[rxpath
])
3643 if (sc
->nrxpath
> 2)
3644 if (rxwi
->rssi
[2] > rxwi
->rssi
[rxpath
])
3653 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
3654 uint8_t rssi
, uint8_t rxpath
)
3656 struct ieee80211com
*ic
;
3657 struct ieee80211_channel
*c
;
3659 int8_t rssi_off
, lna_gain
;
3666 chan
= ieee80211_chan2ieee(ic
, c
);
3668 if (IEEE80211_IS_CHAN_5GHZ(c
))
3670 rssi_off
= sc
->rssi_off_5ghz
[rxpath
];
3673 lna_gain
= sc
->lna_gain
[1];
3674 else if (chan
<= 128)
3675 lna_gain
= sc
->lna_gain
[2];
3677 lna_gain
= sc
->lna_gain
[3];
3681 rssi_off
= sc
->rssi_off_2ghz
[rxpath
];
3682 lna_gain
= sc
->lna_gain
[0];
3685 return (-12 - rssi_off
- lna_gain
- rssi
);
3691 static uint8_t rt2870_rate2mcs(uint8_t rate
)
3718 static int rt2870_ackrate(struct ieee80211com
*ic
, int rate
)
3730 return (ic
->ic_curmode
== IEEE80211_MODE_11B
) ? 4 : rate
;
3749 /* default to 1Mbps */
3756 static uint16_t rt2870_txtime(int len
, int rate
, uint32_t flags
)
3760 if (RT2870_RATE_IS_OFDM(rate
))
3762 txtime
= (8 + 4 * len
+ 3 + rate
- 1) / rate
;
3763 txtime
= 16 + 4 + 4 * txtime
+ 6;
3767 txtime
= (16 * len
+ rate
- 1) / rate
;
3769 if (rate
!= 2 && (flags
& IEEE80211_F_SHPREAMBLE
))
3781 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
3782 uint8_t *buf
, uint32_t dmalen
)
3784 struct ieee80211com
*ic
;
3786 struct ieee80211_frame
*wh
;
3787 struct ieee80211_node
*ni
;
3788 struct rt2870_softc_node
*rni
;
3789 struct rt2870_softc_rx_radiotap_header
*tap
;
3790 struct rt2870_rxwi
*rxwi
;
3791 struct rt2870_rxinfo
*rxinfo
;
3793 uint32_t rxinfo_flags
;
3794 uint8_t cipher_err
, rssi
, ant
, phymode
, bw
, shortgi
, mcs
;
3796 int len
, ampdu
, amsdu
, i
;
3801 /* get Rx wireless info */
3803 rxwi
= (struct rt2870_rxwi
*) buf
;
3804 len
= (le16toh(rxwi
->tid_size
) >> RT2870_RXWI_SIZE_SHIFT
) &
3805 RT2870_RXWI_SIZE_MASK
;
3809 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3810 "%s: bad rxwi len: DMA len=%d, rxwi len=%d\n",
3811 device_get_nameunit(sc
->dev
), dmalen
, len
);
3817 rxinfo
= (struct rt2870_rxinfo
*) ((caddr_t
) rxwi
+ dmalen
);
3818 rxinfo_flags
= le32toh(rxinfo
->flags
);
3820 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3821 "%s: Rx frame: DMA len=%d, len=%d, rxinfo flags=0x%08x\n",
3822 device_get_nameunit(sc
->dev
), dmalen
, len
, rxinfo_flags
);
3824 /* check for crc errors */
3826 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_CRC_ERR
)
3828 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3829 "%s: rxinfo: crc error\n",
3830 device_get_nameunit(sc
->dev
));
3834 if (!(ifp
->if_flags
& IFF_PROMISC
))
3838 wh
= (struct ieee80211_frame
*) (rxwi
+ 1);
3840 /* check for L2 padding between IEEE 802.11 frame header and body */
3842 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_L2PAD
)
3844 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3845 "%s: L2 padding: DMA len=%d, len=%d\n",
3846 device_get_nameunit(sc
->dev
), dmalen
, len
);
3851 m
= m_getjcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
, MJUMPAGESIZE
);
3854 sc
->rx_mbuf_alloc_errors
++;
3859 m
->m_pkthdr
.rcvif
= ifp
;
3860 m
->m_pkthdr
.len
= m
->m_len
= len
;
3862 m_copyback(m
, 0, len
, (caddr_t
) wh
);
3864 wh
= mtod(m
, struct ieee80211_frame
*);
3866 /* check for cipher errors */
3868 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_DECRYPTED
)
3870 cipher_err
= ((rxinfo_flags
>> RT2870_RXINFO_FLAGS_CIPHER_ERR_SHIFT
) &
3871 RT2870_RXINFO_FLAGS_CIPHER_ERR_MASK
);
3872 if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_NONE
)
3874 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
3875 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
3877 m
->m_flags
|= M_WEP
;
3881 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3882 "%s: rxinfo: cipher error=0x%02x\n",
3883 device_get_nameunit(sc
->dev
), cipher_err
);
3887 if (!(ifp
->if_flags
& IFF_PROMISC
))
3896 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
3900 if (!(ifp
->if_flags
& IFF_PROMISC
))
3908 /* check for A-MPDU */
3910 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMPDU
)
3912 m
->m_flags
|= M_AMPDU
;
3923 /* check for A-MSDU */
3925 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMSDU
)
3936 ant
= rt2870_maxrssi_rxpath(sc
, rxwi
);
3937 rssi
= rxwi
->rssi
[ant
];
3938 rssi_dbm
= rt2870_rssi2dbm(sc
, rssi
, ant
);
3939 phymode
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
3940 RT2870_RXWI_PHYMODE_MASK
);
3941 bw
= ((rxwi
->bw_mcs
>> RT2870_RXWI_BW_SHIFT
) & RT2870_RXWI_BW_MASK
);
3942 shortgi
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_SHORTGI_SHIFT
) &
3943 RT2870_RXWI_SHORTGI_MASK
);
3944 mcs
= ((rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
);
3946 if (bpf_peers_present(sc
->drvbpf
))
3950 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
3951 tap
->dbm_antsignal
= rssi_dbm
;
3952 tap
->dbm_antnoise
= RT2870_NOISE_FLOOR
;
3954 tap
->antsignal
= rssi
;
3955 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
3956 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
3957 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
3958 tap
->chan_maxpow
= 0;
3960 if (phymode
== RT2870_RXWI_PHYMODE_CCK
|| phymode
== RT2870_RXWI_PHYMODE_OFDM
)
3961 tap
->rate
= rt2870_rxrate(rxwi
);
3963 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
3965 if (rxwi
->bw_mcs
& RT2870_RXWI_MCS_SHOTPRE
)
3966 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
3969 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
3971 bpf_mtap2(sc
->drvbpf
, tap
, sc
->rxtap_len
, m
);
3974 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3975 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
3976 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
3977 device_get_nameunit(sc
->dev
),
3978 len
, phymode
, bw
, shortgi
, mcs
,
3979 ant
, rxwi
->rssi
[0], rxwi
->rssi
[1], rxwi
->rssi
[2],
3980 rxwi
->snr
[0], rxwi
->snr
[1],
3981 rxwi
->wcid
, ampdu
, amsdu
);
3983 ni
= ieee80211_find_rxnode(ic
, (struct ieee80211_frame_min
*) wh
);
3987 rni
= (struct rt2870_softc_node
*) ni
;
3989 for (i
= 0; i
< RT2870_SOFTC_RSSI_DBM_COUNT
; i
++)
3990 rni
->last_rssi_dbm
[i
] = rt2870_rssi2dbm(sc
, rxwi
->rssi
[i
], i
);
3993 ieee80211_input(ic
, m
, ni
, rssi_dbm
, RT2870_NOISE_FLOOR
, 0);
3995 ieee80211_free_node(ni
);
4001 static int rt2870_tx_frame(struct rt2870_softc
*sc
,
4002 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
)
4004 struct ieee80211com
*ic
;
4005 struct rt2870_softc_tx_ring
*ring
;
4006 struct rt2870_softc_tx_data
*data
;
4007 struct rt2870_txinfo
*txinfo
;
4008 struct rt2870_txwi
*txwi
;
4009 struct ieee80211_frame
*wh
;
4010 struct ieee80211_tx_ampdu
*tx_ampdu
;
4011 struct rt2870_softc_tx_radiotap_header
*tap
;
4012 u_int hdrsize
, hdrspace
;
4013 uint8_t type
, rate
, bw
, stbc
, shortgi
, mcs
, pid
, wcid
, mpdu_density
, bawin_size
;
4014 uint16_t qos
, len
, dmalen
, mpdu_len
, dur
;
4015 int hasqos
, ac
, ampdu
, ackrate
;
4019 ring
= &sc
->tx_ring
[qid
];
4020 data
= STAILQ_FIRST(&ring
->inactive
);
4021 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
4022 txinfo
= (struct rt2870_txinfo
*) data
->buf
;
4023 txwi
= (struct rt2870_txwi
*) (txinfo
+ 1);
4025 wh
= mtod(m
, struct ieee80211_frame
*);
4027 type
= wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
;
4029 hasqos
= IEEE80211_QOS_HAS_SEQ(wh
);
4032 if (IEEE80211_HAS_ADDR4(wh
))
4033 qos
= le16toh(*(const uint16_t *)
4034 (((struct ieee80211_qosframe_addr4
*) wh
)->i_qos
));
4036 qos
= le16toh(*(const uint16_t *)
4037 (((struct ieee80211_qosframe
*) wh
)->i_qos
));
4044 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4046 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
4048 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
4049 rate
= ic
->ic_fixed_rate
;
4051 rate
= ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
];
4055 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
4056 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
4057 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
4058 rate
= ic
->ic_fixed_rate
;
4060 rate
= ni
->ni_rates
.rs_rates
[ni
->ni_txrate
];
4063 rate
&= IEEE80211_RATE_VAL
;
4065 len
= sizeof(struct rt2870_txinfo
) + sizeof(struct rt2870_txwi
) + m
->m_pkthdr
.len
;
4067 /* align end on a 4-bytes boundary */
4069 dmalen
= (len
+ 3) & ~3;
4073 memset(txinfo
, 0, sizeof(struct rt2870_txinfo
));
4075 txinfo
->len
= htole16(dmalen
);
4077 txinfo
->qsel_flags
= (RT2870_TXINFO_QSEL_EDCA
<< RT2870_TXINFO_QSEL_SHIFT
);
4079 /* fill Tx wireless info */
4081 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4084 mcs
= rt2870_rate2mcs(rate
);
4088 wcid
= (type
== IEEE80211_FC0_TYPE_DATA
) ?
4089 RT2870_AID2WCID(ni
->ni_associd
) : 0xff;
4091 /* calculate MPDU length without padding */
4093 hdrsize
= ieee80211_hdrsize(wh
);
4094 hdrspace
= ieee80211_hdrspace(ic
, wh
);
4095 mpdu_len
= m
->m_pkthdr
.len
- hdrspace
+ hdrsize
;
4097 memset(txwi
, 0, sizeof(struct rt2870_txwi
));
4101 txwi
->pid_mpdu_len
= ((htole16(pid
) & RT2870_TXWI_PID_MASK
) <<
4102 RT2870_TXWI_PID_SHIFT
) | ((htole16(mpdu_len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
4103 RT2870_TXWI_MPDU_LEN_SHIFT
);
4105 stbc
= (ni
->ni_htcap
& IEEE80211_HTCAP_RXSTBC
) ? 1 : 0;
4107 shortgi
= (ic
->ic_flags_ext
& (IEEE80211_FEXT_SHORTGI20
| IEEE80211_FEXT_SHORTGI40
)) &&
4108 (ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_htcap
& (IEEE80211_HTCAP_SHORTGI20
| IEEE80211_HTCAP_SHORTGI40
));
4110 txwi
->phymode_ifs_stbc_shortgi
|=
4111 ((stbc
& RT2870_TXWI_STBC_MASK
) << RT2870_TXWI_STBC_SHIFT
) |
4112 ((shortgi
& RT2870_TXWI_SHORTGI_MASK
) << RT2870_TXWI_SHORTGI_SHIFT
);
4114 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4116 txwi
->phymode_ifs_stbc_shortgi
|=
4117 (RT2870_TXWI_PHYMODE_HT_MIXED
<< RT2870_TXWI_PHYMODE_SHIFT
);
4121 if (!RT2870_RATE_IS_OFDM(rate
))
4123 txwi
->phymode_ifs_stbc_shortgi
|=
4124 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
4126 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
4127 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
4131 txwi
->phymode_ifs_stbc_shortgi
|=
4132 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
4136 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_chw
== 40))
4137 bw
= RT2870_TXWI_BW_40
;
4139 bw
= RT2870_TXWI_BW_20
;
4141 txwi
->bw_mcs
= ((bw
& RT2870_TXWI_BW_MASK
) << RT2870_TXWI_BW_SHIFT
) |
4142 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
4144 if (type
!= IEEE80211_FC0_TYPE_DATA
)
4145 txwi
->txop
= (RT2870_TXWI_TXOP_BACKOFF
<< RT2870_TXWI_TXOP_SHIFT
);
4147 txwi
->txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
4149 /* skip ACKs for multicast frames */
4151 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
4152 (!hasqos
|| (qos
& IEEE80211_QOS_ACKPOLICY
) != IEEE80211_QOS_ACKPOLICY_NOACK
))
4154 txwi
->bawin_size_xflags
|=
4155 (RT2870_TXWI_XFLAGS_ACK
<< RT2870_TXWI_XFLAGS_SHIFT
);
4157 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4159 /* preamble + plcp + signal extension + SIFS */
4161 dur
= 16 + 4 + 6 + 10;
4165 ackrate
= rt2870_ackrate(ic
, rate
);
4167 dur
= rt2870_txtime(RT2870_ACK_SIZE
, ackrate
, ic
->ic_flags
) +
4171 *(uint16_t *) wh
->i_dur
= htole16(dur
);
4174 /* check fo A-MPDU */
4176 if ((qos
& IEEE80211_QOS_ACKPOLICY
) == IEEE80211_QOS_ACKPOLICY_BA
)
4178 ac
= M_WME_GETAC(m
);
4179 tx_ampdu
= &ni
->ni_tx_ampdu
[ac
];
4181 mpdu_density
= RT2870_MS(ni
->ni_htparam
, IEEE80211_HTCAP_MPDUDENSITY
);
4182 bawin_size
= tx_ampdu
->txa_wnd
;
4184 txwi
->mpdu_density_flags
|=
4185 ((mpdu_density
& RT2870_TXWI_MPDU_DENSITY_MASK
) << RT2870_TXWI_MPDU_DENSITY_SHIFT
) |
4186 (RT2870_TXWI_FLAGS_AMPDU
<< RT2870_TXWI_FLAGS_SHIFT
);
4188 txwi
->bawin_size_xflags
|=
4189 ((bawin_size
& RT2870_TXWI_BAWIN_SIZE_MASK
) << RT2870_TXWI_BAWIN_SIZE_SHIFT
);
4191 if (IEEE80211_HAS_ADDR4(wh
))
4192 ((struct ieee80211_qosframe_addr4
*) wh
)->i_qos
[0] &= ~IEEE80211_QOS_ACKPOLICY
;
4194 ((struct ieee80211_qosframe
*) wh
)->i_qos
[0] &= ~IEEE80211_QOS_ACKPOLICY
;
4203 /* ask MAC to insert timestamp into probe responses */
4205 if ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
4206 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
4207 txwi
->mpdu_density_flags
|=
4208 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
4210 if (bpf_peers_present(sc
->drvbpf
))
4214 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
4215 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4216 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4217 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4218 tap
->chan_maxpow
= 0;
4220 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4221 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4225 if (mcs
& RT2870_TXWI_MCS_SHOTPRE
)
4226 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4229 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4231 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4232 tap
->flags
|= IEEE80211_RADIOTAP_F_WEP
;
4234 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4236 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4238 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4240 wh
->i_fc
[1] |= IEEE80211_FC1_WEP
;
4244 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4248 m_copydata(m
, 0, m
->m_pkthdr
.len
, (caddr_t
) (txwi
+ 1));
4250 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4251 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4252 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, ampdu=%d, DMA len=%d\n",
4253 device_get_nameunit(sc
->dev
),
4254 qid
, hdrsize
, hdrspace
, m
->m_pkthdr
.len
, bw
, stbc
, shortgi
, mcs
, wcid
, ampdu
, dmalen
);
4259 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4262 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, ring
, data
->buf
, len
,
4263 USBD_FORCE_SHORT_XFER
| USBD_NO_COPY
, RT2870_USB_XFER_TIMEOUT
, rt2870_tx_intr
);
4265 usbd_transfer(data
->xfer
);
4273 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
4274 struct mbuf
*m
, struct ieee80211_node
*ni
,
4275 const struct ieee80211_bpf_params
*params
)
4277 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4279 device_get_nameunit(sc
->dev
));
4287 static void rt2870_rx_intr(usbd_xfer_handle xfer
,
4288 usbd_private_handle priv
, usbd_status status
)
4290 struct rt2870_softc
*sc
;
4292 struct rt2870_softc_rx_ring
*ring
;
4293 struct rt2870_softc_rx_data
*data
;
4298 ring
= &sc
->rx_ring
;
4300 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4303 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4304 "%s: Rx interrupt: %s\n",
4305 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4308 sc
->rx_interrupts
++;
4312 case USBD_NORMAL_COMPLETION
:
4313 data
= STAILQ_FIRST(&ring
->active
);
4317 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4319 KASSERT(data
->xfer
== xfer
,
4320 ("%s: Rx interrupt: invalid USB xfer\n",
4321 device_get_nameunit(sc
->dev
)));
4323 usbd_get_xfer_status(xfer
, NULL
, NULL
, &len
, NULL
);
4327 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4329 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4332 case USBD_CANCELLED
:
4333 case USBD_NOT_STARTED
:
4337 if (status
== USBD_STALLED
)
4338 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4340 data
= STAILQ_FIRST(&ring
->active
);
4343 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4345 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4347 usbd_setup_xfer(xfer
, ring
->usb_pipe
, sc
, data
->buf
,
4348 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
4349 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
4351 usbd_transfer(xfer
);
4360 static void rt2870_tx_intr(usbd_xfer_handle xfer
,
4361 usbd_private_handle priv
, usbd_status status
)
4363 struct rt2870_softc
*sc
;
4365 struct rt2870_softc_tx_ring
*ring
;
4366 struct rt2870_softc_tx_data
*data
;
4372 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4375 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4376 "%s: Tx interrupt: %s\n",
4377 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4380 sc
->tx_interrupts
[ring
->qid
]++;
4384 case USBD_NORMAL_COMPLETION
:
4385 data
= STAILQ_FIRST(&ring
->active
);
4389 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4391 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4393 sc
->tx_qid_pending_mask
|= (1 << ring
->qid
);
4395 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4398 case USBD_CANCELLED
:
4399 case USBD_NOT_STARTED
:
4403 data
= STAILQ_FIRST(&ring
->active
);
4406 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4408 if (data
->m
!= NULL
)
4414 if (data
->ni
!= NULL
)
4416 ieee80211_free_node(data
->ni
);
4420 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4425 printf("%s: could not transmit buffer: qid=%d, status=%s\n",
4426 device_get_nameunit(sc
->dev
), ring
->qid
, usbd_errstr(status
));
4429 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4431 if (status
== USBD_STALLED
)
4432 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4438 * rt2870_rx_done_task
4440 static void rt2870_rx_done_task(void *context
, int pending
)
4442 struct rt2870_softc
*sc
;
4448 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4449 "%s: Rx done task\n",
4450 device_get_nameunit(sc
->dev
));
4452 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4455 if (rt2870_rx_eof(sc
, sc
->rx_process_limit
) != 0)
4457 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4458 "%s: Rx done task: scheduling again\n",
4459 device_get_nameunit(sc
->dev
));
4461 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4466 * rt2870_tx_done_task
4468 static void rt2870_tx_done_task(void *context
, int pending
)
4470 struct rt2870_softc
*sc
;
4477 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4478 "%s: Tx done task\n",
4479 device_get_nameunit(sc
->dev
));
4481 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4484 for (i
= sc
->usb_endpoints
- 2; i
>= 0; i
--)
4486 if (sc
->tx_qid_pending_mask
& (1 << i
))
4488 sc
->tx_qid_pending_mask
&= ~(1 << i
);
4490 rt2870_tx_eof(sc
, &sc
->tx_ring
[i
]);
4494 if (sc
->tx_qid_pending_mask
!= 0)
4496 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4497 "%s: Tx done task: scheduling again\n",
4498 device_get_nameunit(sc
->dev
));
4500 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4505 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4509 * rt2870_periodic_task
4511 static void rt2870_periodic_task(void *context
, int pending
)
4513 struct rt2870_softc
*sc
;
4515 struct ieee80211com
*ic
;
4521 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
4522 "%s: periodic task: round=%lu\n",
4523 device_get_nameunit(sc
->dev
), sc
->periodic_round
);
4525 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4528 sc
->periodic_round
++;
4530 rt2870_update_stats(sc
);
4532 if ((sc
->periodic_round
% 10) == 0)
4534 rt2870_bbp_tuning(sc
);
4536 rt2870_update_raw_counters(sc
);
4538 rt2870_watchdog(sc
);
4540 if (ic
->ic_opmode
== IEEE80211_M_STA
)
4541 rt2870_amrr_update_iter_func(sc
, ic
->ic_bss
);
4543 ieee80211_iterate_nodes(&ic
->ic_sta
, rt2870_amrr_update_iter_func
, sc
);
4546 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
4552 static void rt2870_cmd_task(void *context
, int pending
)
4554 struct rt2870_softc
*sc
;
4555 struct rt2870_softc_cmd_ring
*ring
;
4556 struct rt2870_softc_cmd
*cmd
;
4559 ring
= &sc
->cmd_ring
;
4563 RT2870_SOFTC_LOCK(sc
);
4565 cmd
= STAILQ_FIRST(&ring
->active
);
4568 RT2870_SOFTC_UNLOCK(sc
);
4572 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4574 RT2870_SOFTC_UNLOCK(sc
);
4576 cmd
->cb(sc
, cmd
->data
);
4578 RT2870_SOFTC_LOCK(sc
);
4580 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
4583 RT2870_SOFTC_UNLOCK(sc
);
4590 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
)
4593 struct rt2870_softc_rx_ring
*ring
;
4594 struct rt2870_softc_rx_data
*data
;
4600 ring
= &sc
->rx_ring
;
4606 RT2870_SOFTC_LOCK(sc
);
4608 data
= STAILQ_FIRST(&ring
->done
);
4611 RT2870_SOFTC_UNLOCK(sc
);
4615 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
4617 RT2870_SOFTC_UNLOCK(sc
);
4622 if (len
< RT2870_RX_DESC_SIZE
)
4628 while (len
> (sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)))
4630 dmalen
= (le32toh(*(uint32_t *) buf
) & 0xffff);
4632 if (dmalen
== 0 || (dmalen
& 3) != 0)
4634 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4635 "%s: bad DMA len=%u\n",
4636 device_get_nameunit(sc
->dev
), dmalen
);
4640 if ((dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)) > len
)
4642 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4643 "%s: bad DMA len: DMA len=%u, USB len=%d\n",
4644 device_get_nameunit(sc
->dev
),
4645 (unsigned int) (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)), len
);
4651 rt2870_rx_frame(sc
, buf
+ sizeof(uint32_t), dmalen
);
4653 buf
+= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
4654 len
-= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
4659 RT2870_SOFTC_LOCK(sc
);
4661 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4663 RT2870_SOFTC_UNLOCK(sc
);
4665 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, sc
, data
->buf
,
4666 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
4667 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
4669 usbd_transfer(data
->xfer
);
4674 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4675 "%s: Rx eof: nframes=%d\n",
4676 device_get_nameunit(sc
->dev
), nframes
);
4678 sc
->rx_packets
+= nframes
;
4680 return (limit
== 0);
4686 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
4687 struct rt2870_softc_tx_ring
*ring
)
4690 struct rt2870_softc_tx_data
*data
;
4699 RT2870_SOFTC_LOCK(sc
);
4701 data
= STAILQ_FIRST(&ring
->done
);
4704 RT2870_SOFTC_UNLOCK(sc
);
4708 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
4710 RT2870_SOFTC_UNLOCK(sc
);
4712 rt2870_drain_fifo_stats(sc
);
4716 if (data
->m
->m_flags
& M_TXCB
)
4717 ieee80211_process_callback(data
->ni
, data
->m
, 0);
4721 ieee80211_free_node(data
->ni
);
4726 RT2870_SOFTC_LOCK(sc
);
4728 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4732 RT2870_SOFTC_UNLOCK(sc
);
4737 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4738 "%s: Tx eof: qid=%d, nframes=%d\n",
4739 device_get_nameunit(sc
->dev
), ring
->qid
, nframes
);
4743 * rt2870_update_stats
4745 static void rt2870_update_stats(struct rt2870_softc
*sc
)
4748 struct ieee80211com
*ic
;
4749 struct ieee80211_node
*ni
;
4751 int beacons
, noretryok
, retryok
, failed
, underflows
, zerolen
;
4758 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4759 "%s: update statistic\n",
4760 device_get_nameunit(sc
->dev
));
4762 rt2870_drain_fifo_stats(sc
);
4764 /* read and clear Tx statistic registers */
4766 rt2870_io_mac_read_multi(sc
, RT2870_REG_TX_STA_CNT0
,
4767 stacnt
, sizeof(stacnt
));
4769 stacnt
[0] = le32toh(stacnt
[0]);
4770 stacnt
[1] = le32toh(stacnt
[1]);
4771 stacnt
[2] = le32toh(stacnt
[2]);
4773 beacons
= stacnt
[0] >> 16;
4774 noretryok
= stacnt
[1] & 0xffff;
4775 retryok
= stacnt
[1] >> 16;
4776 failed
= stacnt
[0] & 0xffff;
4777 underflows
= stacnt
[2] >> 16;
4778 zerolen
= stacnt
[2] & 0xffff;
4780 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4781 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
4782 device_get_nameunit(sc
->dev
),
4783 beacons
, noretryok
, retryok
, failed
, underflows
, zerolen
);
4785 ifp
->if_oerrors
+= failed
;
4787 sc
->tx_beacons
+= beacons
;
4788 sc
->tx_noretryok
+= noretryok
;
4789 sc
->tx_retryok
+= retryok
;
4790 sc
->tx_failed
+= failed
;
4791 sc
->tx_underflows
+= underflows
;
4792 sc
->tx_zerolen
+= zerolen
;
4794 if (ic
->ic_opmode
== IEEE80211_M_STA
&& ic
->ic_state
== IEEE80211_S_RUN
)
4798 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
4799 wcid
= RT2870_AID2WCID(associd
);
4801 rt2870_amrr_tx_update(&sc
->amrr_node
[wcid
],
4802 noretryok
+ retryok
+ failed
, noretryok
+ retryok
, retryok
+ failed
);
4809 static void rt2870_bbp_tuning(struct rt2870_softc
*sc
)
4811 struct ieee80211com
*ic
;
4812 struct ieee80211_node
*ni
;
4814 int8_t rssi
, old
, new;
4816 /* RT2860C does not support BBP tuning */
4818 if (sc
->mac_rev
== 0x28600100)
4823 if ((ic
->ic_flags
& IEEE80211_F_SCAN
) ||
4824 ic
->ic_opmode
!= IEEE80211_M_STA
|| ic
->ic_state
!= IEEE80211_S_RUN
)
4829 chan
= ieee80211_chan2ieee(ic
, ni
->ni_chan
);
4833 else if (chan
<= 64)
4835 else if (chan
<= 128)
4840 rssi
= ieee80211_getrssi(ic
);
4842 if (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
))
4844 new = 0x2e + sc
->lna_gain
[group
];
4848 if (!IEEE80211_IS_CHAN_HT40(ni
->ni_chan
))
4849 new = 0x32 + sc
->lna_gain
[group
] * 5 / 3;
4851 new = 0x3a + sc
->lna_gain
[group
] * 5 / 3;
4854 /* Tune if absolute average RSSI is greater than -80 */
4859 old
= rt2870_io_bbp_read(sc
, 66);
4862 rt2870_io_bbp_write(sc
, 66, new);
4868 static void rt2870_watchdog(struct rt2870_softc
*sc
)
4873 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4875 RT2870_DPRINTF(sc
, RT2870_DEBUG_WATCHDOG
,
4876 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4877 device_get_nameunit(sc
->dev
), tmp
);
4879 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) != 0)
4881 sc
->tx_queue_not_empty
[0]++;
4883 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40012);
4885 for (ntries
= 0; ntries
< 10; ntries
++)
4887 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4888 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) == 0)
4894 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
4897 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) != 0)
4899 sc
->tx_queue_not_empty
[1]++;
4901 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf4000a);
4903 for (ntries
= 0; ntries
< 10; ntries
++)
4905 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4906 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) == 0)
4912 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
4917 * rt2870_drain_fifo_stats
4919 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
)
4923 uint8_t wcid
, mcs
, pid
;
4924 int ok
, agg
, retrycnt
;
4926 ifp
= sc
->ic
.ic_ifp
;
4928 /* drain Tx status FIFO (maxsize = 16) */
4930 while ((stats
= rt2870_io_mac_read(sc
, RT2870_REG_TX_STA_FIFO
)) &
4931 RT2870_REG_TX_STA_FIFO_VALID
)
4933 wcid
= (stats
>> RT2870_REG_TX_STA_FIFO_WCID_SHIFT
) &
4934 RT2870_REG_TX_STA_FIFO_WCID_MASK
;
4936 /* if no ACK was requested, no feedback is available */
4938 if (!(stats
& RT2870_REG_TX_STA_FIFO_ACK_REQ
) || wcid
== 0xff)
4941 /* update AMRR statistic */
4943 ok
= (stats
& RT2870_REG_TX_STA_FIFO_TX_OK
) ? 1 : 0;
4944 agg
= (stats
& RT2870_REG_TX_STA_FIFO_AGG
) ? 1 : 0;
4945 mcs
= (stats
>> RT2870_REG_TX_STA_FIFO_MCS_SHIFT
) &
4946 RT2870_REG_TX_STA_FIFO_MCS_MASK
;
4947 pid
= (stats
>> RT2870_REG_TX_STA_FIFO_PID_SHIFT
) &
4948 RT2870_REG_TX_STA_FIFO_PID_MASK
;
4949 retrycnt
= pid
- mcs
;
4951 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4952 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
4953 device_get_nameunit(sc
->dev
),
4954 wcid
, ok
, agg
, mcs
, pid
, retrycnt
);
4956 rt2870_amrr_tx_complete(&sc
->amrr_node
[wcid
], ok
, retrycnt
);
4964 * rt2870_update_raw_counters
4966 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
)
4970 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT
);
4972 sc
->tx_nonagg
+= tmp
& 0xffff;
4973 sc
->tx_agg
+= tmp
>> 16;
4975 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT0
);
4977 sc
->tx_ampdu
+= (tmp
& 0xffff) / 1 + (tmp
>> 16) / 2;
4979 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT1
);
4981 sc
->tx_ampdu
+= (tmp
& 0xffff) / 3 + (tmp
>> 16) / 4;
4983 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT2
);
4985 sc
->tx_ampdu
+= (tmp
& 0xffff) / 5 + (tmp
>> 16) / 6;
4987 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT3
);
4989 sc
->tx_ampdu
+= (tmp
& 0xffff) / 7 + (tmp
>> 16) / 8;
4991 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT4
);
4993 sc
->tx_ampdu
+= (tmp
& 0xffff) / 9 + (tmp
>> 16) / 10;
4995 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT5
);
4997 sc
->tx_ampdu
+= (tmp
& 0xffff) / 11 + (tmp
>> 16) / 12;
4999 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT6
);
5001 sc
->tx_ampdu
+= (tmp
& 0xffff) / 13 + (tmp
>> 16) / 14;
5003 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT7
);
5005 sc
->tx_ampdu
+= (tmp
& 0xffff) / 15 + (tmp
>> 16) / 16;
5007 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT0
);
5009 sc
->rx_crc_errors
+= tmp
& 0xffff;
5010 sc
->rx_phy_errors
+= tmp
>> 16;
5012 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT1
);
5014 sc
->rx_false_ccas
+= tmp
& 0xffff;
5015 sc
->rx_plcp_errors
+= tmp
>> 16;
5017 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT2
);
5019 sc
->rx_dup_packets
+= tmp
& 0xffff;
5020 sc
->rx_fifo_overflows
+= tmp
>> 16;
5022 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TXRX_MPDU_DEN_CNT
);
5024 sc
->tx_mpdu_zero_density
+= tmp
& 0xffff;
5025 sc
->rx_mpdu_zero_density
+= tmp
>> 16;
5029 * rt2870_alloc_rx_ring
5031 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
5032 struct rt2870_softc_rx_ring
*ring
)
5034 struct rt2870_softc_rx_data
*data
;
5037 STAILQ_INIT(&ring
->active
);
5038 STAILQ_INIT(&ring
->done
);
5040 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
5042 data
= &ring
->data
[i
];
5044 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
5045 if (data
->xfer
== NULL
)
5047 printf("%s: could not allocate Rx xfer\n",
5048 device_get_nameunit(sc
->dev
));
5053 data
->buf
= usbd_alloc_buffer(data
->xfer
, RT2870_USB_RX_BULK_BUFLEN
);
5054 if (data
->buf
== NULL
)
5056 printf("%s: could not allocate Rx buffer\n",
5057 device_get_nameunit(sc
->dev
));
5067 rt2870_free_rx_ring(sc
, ring
);
5073 * rt2870_reset_rx_ring
5075 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
5076 struct rt2870_softc_rx_ring
*ring
)
5078 STAILQ_INIT(&ring
->active
);
5079 STAILQ_INIT(&ring
->done
);
5083 * rt2870_free_rx_ring
5085 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
5086 struct rt2870_softc_rx_ring
*ring
)
5088 struct rt2870_softc_rx_data
*data
;
5091 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
5093 data
= &ring
->data
[i
];
5095 if (data
->xfer
!= NULL
)
5097 usbd_free_xfer(data
->xfer
);
5104 * rt2870_alloc_tx_ring
5106 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
5107 struct rt2870_softc_tx_ring
*ring
, int qid
)
5109 struct rt2870_softc_tx_data
*data
;
5112 STAILQ_INIT(&ring
->inactive
);
5113 STAILQ_INIT(&ring
->active
);
5114 STAILQ_INIT(&ring
->done
);
5120 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5122 data
= &ring
->data
[i
];
5124 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
5125 if (data
->xfer
== NULL
)
5127 printf("%s: could not allocate Tx xfer\n",
5128 device_get_nameunit(sc
->dev
));
5133 data
->buf
= usbd_alloc_buffer(data
->xfer
,
5134 RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
);
5135 if (data
->buf
== NULL
)
5137 printf("%s: could not allocate Tx buffer\n",
5138 device_get_nameunit(sc
->dev
));
5143 memset(data
->buf
, 0, RT2870_TX_DESC_SIZE
);
5145 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5152 rt2870_free_tx_ring(sc
, ring
);
5158 * rt2870_reset_tx_ring
5160 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
5161 struct rt2870_softc_tx_ring
*ring
)
5163 struct rt2870_softc_tx_data
*data
;
5166 STAILQ_INIT(&ring
->inactive
);
5167 STAILQ_INIT(&ring
->active
);
5168 STAILQ_INIT(&ring
->done
);
5172 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5174 data
= &ring
->data
[i
];
5176 if (data
->m
!= NULL
)
5182 if (data
->ni
!= NULL
)
5184 ieee80211_free_node(data
->ni
);
5188 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5193 * rt2870_free_tx_ring
5195 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
5196 struct rt2870_softc_tx_ring
*ring
)
5198 struct rt2870_softc_tx_data
*data
;
5201 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5203 data
= &ring
->data
[i
];
5205 if (data
->xfer
!= NULL
)
5207 usbd_free_xfer(data
->xfer
);
5211 if (data
->m
!= NULL
)
5217 if (data
->ni
!= NULL
)
5219 ieee80211_free_node(data
->ni
);
5226 * rt2870_reset_cmd_ring
5228 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
5229 struct rt2870_softc_cmd_ring
*ring
)
5231 struct rt2870_softc_cmd
*cmd
;
5234 STAILQ_INIT(&ring
->inactive
);
5235 STAILQ_INIT(&ring
->active
);
5239 for (i
= 0; i
< RT2870_SOFTC_CMD_RING_CMD_COUNT
; i
++)
5241 cmd
= &ring
->cmd
[i
];
5243 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
5248 * rt2870_sysctl_attach
5250 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
)
5252 struct sysctl_ctx_list
*ctx
;
5253 struct sysctl_oid
*tree
;
5254 struct sysctl_oid
*stats
;
5256 ctx
= device_get_sysctl_ctx(sc
->dev
);
5257 tree
= device_get_sysctl_tree(sc
->dev
);
5259 stats
= SYSCTL_ADD_NODE(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
5260 "stats", CTLFLAG_RD
, 0, "statistic");
5262 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5263 "interrupts", CTLFLAG_RD
, &sc
->interrupts
, 0,
5266 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5267 "rx_interrupts", CTLFLAG_RD
, &sc
->rx_interrupts
, 0,
5270 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5272 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5273 "tx_mgmt_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[5], 0,
5274 "Tx MGMT interrupts");
5276 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5277 "tx_hcca_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[4], 0,
5278 "Tx HCCA interrupts");
5281 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5282 "tx_ac3_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[3], 0,
5283 "Tx AC3 interrupts");
5285 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5286 "tx_ac2_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[2], 0,
5287 "Tx AC2 interrupts");
5289 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5290 "tx_ac1_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[1], 0,
5291 "Tx AC1 interrupts");
5293 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5294 "tx_ac0_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[0], 0,
5295 "Tx AC0 interrupts");
5297 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5299 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5300 "tx_mgmt_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[5].queued
, 0,
5301 "Tx MGMT data queued");
5303 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5304 "tx_hcca_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[4].queued
, 0,
5305 "Tx HCCA data queued");
5308 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5309 "tx_ac3_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[3].queued
, 0,
5310 "Tx AC3 data queued");
5312 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5313 "tx_ac2_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[2].queued
, 0,
5314 "Tx AC2 data queued");
5316 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5317 "tx_ac1_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[1].queued
, 0,
5318 "Tx AC1 data queued");
5320 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5321 "tx_ac0_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[0].queued
, 0,
5322 "Tx AC0 data queued");
5324 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5326 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5327 "tx_mgmt_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[5], 0,
5328 "Tx MGMT data queue full");
5330 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5331 "tx_hcca_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[4], 0,
5332 "Tx HCCA data queue full");
5335 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5336 "tx_ac3_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[3], 0,
5337 "Tx AC3 data queue full");
5339 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5340 "tx_ac2_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[2], 0,
5341 "Tx AC2 data queue full");
5343 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5344 "tx_ac1_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[1], 0,
5345 "Tx AC1 data queue full");
5347 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5348 "tx_ac0_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[0], 0,
5349 "Tx AC0 data queue full");
5351 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5352 "tx_watchdog_timeouts", CTLFLAG_RD
, &sc
->tx_watchdog_timeouts
, 0,
5353 "Tx watchdog timeouts");
5355 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5356 "rx_mbuf_alloc_errors", CTLFLAG_RD
, &sc
->rx_mbuf_alloc_errors
, 0,
5357 "Rx mbuf allocation errors");
5359 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5360 "tx_queue_0_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[0], 0,
5361 "Tx queue 0 not empty");
5363 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5364 "tx_queue_1_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[1], 0,
5365 "Tx queue 1 not empty");
5367 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5368 "tx_beacons", CTLFLAG_RD
, &sc
->tx_beacons
, 0,
5371 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5372 "tx_noretryok", CTLFLAG_RD
, &sc
->tx_noretryok
, 0,
5373 "Tx successfull without retries");
5375 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5376 "tx_retryok", CTLFLAG_RD
, &sc
->tx_retryok
, 0,
5377 "Tx successfull with retries");
5379 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5380 "tx_failed", CTLFLAG_RD
, &sc
->tx_failed
, 0,
5383 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5384 "tx_underflows", CTLFLAG_RD
, &sc
->tx_underflows
, 0,
5387 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5388 "tx_zerolen", CTLFLAG_RD
, &sc
->tx_zerolen
, 0,
5391 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5392 "tx_nonagg", CTLFLAG_RD
, &sc
->tx_nonagg
, 0,
5393 "Tx non-aggregated");
5395 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5396 "tx_agg", CTLFLAG_RD
, &sc
->tx_agg
, 0,
5399 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5400 "tx_ampdu", CTLFLAG_RD
, &sc
->tx_ampdu
, 0,
5403 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5404 "tx_mpdu_zero_density", CTLFLAG_RD
, &sc
->tx_mpdu_zero_density
, 0,
5405 "Tx MPDU with zero density");
5407 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5408 "rx_packets", CTLFLAG_RD
, &sc
->rx_packets
, 0,
5411 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5412 "rx_ampdu", CTLFLAG_RD
, &sc
->rx_ampdu
, 0,
5415 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5416 "rx_mpdu_zero_density", CTLFLAG_RD
, &sc
->rx_mpdu_zero_density
, 0,
5417 "Rx MPDU with zero density");
5419 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5420 "rx_amsdu", CTLFLAG_RD
, &sc
->rx_amsdu
, 0,
5423 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5424 "rx_crc_errors", CTLFLAG_RD
, &sc
->rx_crc_errors
, 0,
5427 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5428 "rx_phy_errors", CTLFLAG_RD
, &sc
->rx_phy_errors
, 0,
5431 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5432 "rx_false_ccas", CTLFLAG_RD
, &sc
->rx_false_ccas
, 0,
5435 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5436 "rx_plcp_errors", CTLFLAG_RD
, &sc
->rx_plcp_errors
, 0,
5439 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5440 "rx_dup_packets", CTLFLAG_RD
, &sc
->rx_dup_packets
, 0,
5441 "Rx duplicate packets");
5443 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5444 "rx_fifo_overflows", CTLFLAG_RD
, &sc
->rx_fifo_overflows
, 0,
5445 "Rx FIFO overflows");
5448 static device_method_t rt2870_dev_methods
[] =
5450 DEVMETHOD(device_probe
, rt2870_probe
),
5451 DEVMETHOD(device_attach
, rt2870_attach
),
5452 DEVMETHOD(device_detach
, rt2870_detach
),
5456 static driver_t rt2870_driver
=
5460 sizeof(struct rt2870_softc
),
5463 static devclass_t rt2870_dev_class
;
5465 DRIVER_MODULE(rt2870
, uhub
, rt2870_driver
, rt2870_dev_class
,
5466 usbd_driver_load
, 0);
5468 MODULE_DEPEND(rt2870
, usb
, 1, 1, 1);
5469 MODULE_DEPEND(rt2870
, wlan
, 1, 1, 1);