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_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
64 #define RT2870_ACK_SIZE 14
66 #define IEEE80211_HAS_ADDR4(wh) \
67 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
69 #define RT2870_MS(_v, _f) (((_v) & _f) >> _f##_S)
70 #define RT2870_SM(_v, _f) (((_v) << _f##_S) & _f)
72 #define RT2870_USB_XFER_TIMEOUT 5000
74 #define RT2870_TX_WATCHDOG_TIMEOUT 5
76 #define RT2870_WCID_RESERVED 0xff
77 #define RT2870_WCID_MCAST 0xf7
80 * Data structures and types
83 struct rt2870_cmd_argv_newstate
85 enum ieee80211_state nstate
;
89 struct rt2870_cmd_argv_nodecleanup
94 struct rt2870_cmd_argv_newassoc
97 uint8_t macaddr
[IEEE80211_ADDR_LEN
];
100 struct rt2870_cmd_argv_updatebeacon
105 struct rt2870_cmd_argv_keyset
107 enum ieee80211_opmode opmode
;
108 struct ieee80211_key key
;
112 struct rt2870_cmd_argv_keydelete
114 enum ieee80211_opmode opmode
;
115 struct ieee80211_key key
;
119 * Static function prototypes
122 static int rt2870_find_usb_endpoints(struct rt2870_softc
*sc
);
124 static void rt2870_init_channels(struct rt2870_softc
*sc
);
126 static void rt2870_init_channels_ht40(struct rt2870_softc
*sc
);
128 static void rt2870_init_locked(void *priv
);
130 static void rt2870_init(void *priv
);
132 static int rt2870_init_bbp(struct rt2870_softc
*sc
);
134 static void rt2870_stop_locked(void *priv
);
136 static void rt2870_stop(void *priv
);
138 static void rt2870_start(struct ifnet
*ifp
);
140 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
);
142 static int rt2870_reset(struct ifnet
*ifp
);
144 static int rt2870_newstate(struct ieee80211com
*ic
,
145 enum ieee80211_state nstate
, int arg
);
147 static void rt2870_scan_start(struct ieee80211com
*ic
);
149 static void rt2870_scan_end(struct ieee80211com
*ic
);
151 static void rt2870_set_channel(struct ieee80211com
*ic
);
153 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
);
155 static void rt2870_updateslot(struct ifnet
*ifp
);
157 static int rt2870_wme_update(struct ieee80211com
*ic
);
159 static void rt2870_update_beacon(struct ieee80211com
*ic
, int what
);
161 static void rt2870_key_update_begin(struct ieee80211com
*ic
);
163 static void rt2870_key_update_end(struct ieee80211com
*ic
);
165 static int rt2870_key_set(struct ieee80211com
*ic
,
166 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
]);
168 static int rt2870_key_delete(struct ieee80211com
*ic
,
169 const struct ieee80211_key
*k
);
171 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
172 const struct ieee80211_bpf_params
*params
);
174 static int rt2870_media_change(struct ifnet
*ifp
);
176 static struct ieee80211_node
*rt2870_node_alloc(struct ieee80211_node_table
*nt
);
178 static void rt2870_node_cleanup(struct ieee80211_node
*ni
);
180 static void rt2870_recv_action(struct ieee80211_node
*ni
,
181 const uint8_t *frm
, const uint8_t *efrm
);
183 static int rt2870_send_action(struct ieee80211_node
*ni
,
184 int category
, int action
, uint16_t args
[4]);
186 static int rt2870_addba_response(struct ieee80211_node
*ni
,
187 struct ieee80211_tx_ampdu
*tap
,
188 int status
, int baparamset
, int batimeout
);
190 static void rt2870_addba_stop(struct ieee80211_node
*ni
,
191 struct ieee80211_tx_ampdu
*tap
);
193 static int rt2870_send_bar(struct ieee80211_node
*ni
,
194 struct ieee80211_tx_ampdu
*tap
, ieee80211_seq seqno
);
196 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
);
198 static void rt2870_periodic(void *arg
);
200 static void rt2870_tx_watchdog(void *arg
);
202 static int rt2870_staid_alloc(struct rt2870_softc
*sc
, int aid
);
204 static void rt2870_staid_delete(struct rt2870_softc
*sc
, int staid
);
206 static int rt2870_do_async(struct rt2870_softc
*sc
,
207 void (*cb
)(struct rt2870_softc
*sc
, void *priv
),
210 static void rt2870_newstate_cb(struct rt2870_softc
*sc
, void *arg
);
212 static void rt2870_node_cleanup_cb(struct rt2870_softc
*sc
, void *arg
);
214 static void rt2870_scan_start_cb(struct rt2870_softc
*sc
, void *arg
);
216 static void rt2870_scan_end_cb(struct rt2870_softc
*sc
, void *arg
);
218 static void rt2870_set_channel_cb(struct rt2870_softc
*sc
, void *arg
);
220 static void rt2870_newassoc_cb(struct rt2870_softc
*sc
, void *arg
);
222 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
);
224 static void rt2870_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
);
226 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
);
228 static void rt2870_key_update_begin_cb(struct rt2870_softc
*sc
, void *arg
);
230 static void rt2870_key_update_end_cb(struct rt2870_softc
*sc
, void *arg
);
232 static void rt2870_key_set_cb(struct rt2870_softc
*sc
, void *arg
);
234 static void rt2870_key_delete_cb(struct rt2870_softc
*sc
, void *arg
);
236 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
237 const uint8_t *bssid
);
239 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
240 const uint8_t *addr
);
242 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
);
244 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
);
246 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
);
248 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
);
250 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
);
252 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
);
254 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
);
256 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
);
258 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
);
260 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
);
262 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
);
264 static void rt2870_asic_update_beacon(struct rt2870_softc
*sc
);
266 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
);
268 static void rt2870_asic_add_ba_session(struct rt2870_softc
*sc
,
269 uint8_t wcid
, int tid
);
271 static void rt2870_asic_del_ba_session(struct rt2870_softc
*sc
,
272 uint8_t wcid
, int tid
);
274 static int rt2870_beacon_alloc(struct rt2870_softc
*sc
);
276 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
);
278 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
279 const struct rt2870_rxwi
*rxwi
);
281 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
282 uint8_t rssi
, uint8_t rxpath
);
284 static uint8_t rt2870_rate2mcs(uint8_t rate
);
286 static int rt2870_ackrate(struct ieee80211com
*ic
, int rate
);
288 static uint16_t rt2870_txtime(int len
, int rate
, uint32_t flags
);
290 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
291 uint8_t *buf
, uint32_t dmalen
);
293 static int rt2870_tx_frame(struct rt2870_softc
*sc
,
294 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
);
296 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
297 struct mbuf
*m
, struct ieee80211_node
*ni
,
298 const struct ieee80211_bpf_params
*params
);
300 static void rt2870_rx_intr(usbd_xfer_handle xfer
,
301 usbd_private_handle priv
, usbd_status status
);
303 static void rt2870_tx_intr(usbd_xfer_handle xfer
,
304 usbd_private_handle priv
, usbd_status status
);
306 static void rt2870_rx_done_task(void *context
, int pending
);
308 static void rt2870_tx_done_task(void *context
, int pending
);
310 static void rt2870_periodic_task(void *context
, int pending
);
312 static void rt2870_cmd_task(void *context
, int pending
);
314 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
);
316 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
317 struct rt2870_softc_tx_ring
*ring
);
319 static void rt2870_update_stats(struct rt2870_softc
*sc
);
321 static void rt2870_bbp_tuning(struct rt2870_softc
*sc
);
323 static void rt2870_watchdog(struct rt2870_softc
*sc
);
325 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
);
327 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
);
329 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
330 struct rt2870_softc_rx_ring
*ring
);
332 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
333 struct rt2870_softc_rx_ring
*ring
);
335 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
336 struct rt2870_softc_rx_ring
*ring
);
338 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
339 struct rt2870_softc_tx_ring
*ring
, int qid
);
341 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
342 struct rt2870_softc_tx_ring
*ring
);
344 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
345 struct rt2870_softc_tx_ring
*ring
);
347 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
348 struct rt2870_softc_cmd_ring
*ring
);
350 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
);
356 static const struct usb_devno rt2870_usb_devno
[] =
358 { USB_VENDOR_LINKSYS4
, USB_PRODUCT_LINKSYS4_WUSB600N
}, /* Linksys WUSB600N */
359 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA140
}, /* D-Link DWA-140 */
360 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA160AREVB
}, /* D-Link DWA-160A Rev. B */
361 { USB_VENDOR_ASUS
, USB_PRODUCT_ASUS_RT2770F
}, /* Asus RT2770F */
362 { USB_VENDOR_RALINK
, USB_PRODUCT_RALINK_RT2770
}, /* Ralink RT2770 */
371 { RT2870_REG_PBF_BCN_OFFSET0
, 0xf8f0e8e0 },
372 { RT2870_REG_PBF_BCN_OFFSET1
, 0x6f77d0c8 },
373 { RT2870_REG_LEGACY_BASIC_RATE
, 0x0000013f },
374 { RT2870_REG_HT_BASIC_RATE
, 0x00008003 },
375 { RT2870_REG_SYS_CTRL
, 0x00000000 },
376 { RT2870_REG_RX_FILTER_CFG
, 0x00017f97 },
377 { RT2870_REG_BKOFF_SLOT_CFG
, 0x00000209 },
378 { RT2870_REG_TX_SW_CFG0
, 0x00000000 },
379 { RT2870_REG_TX_SW_CFG1
, 0x00080606 },
380 { RT2870_REG_TX_LINK_CFG
, 0x00001020 },
381 { RT2870_REG_TX_TIMEOUT_CFG
, 0x000a2090 },
382 { RT2870_REG_MAX_LEN_CFG
, (1 << 12) | RT2870_MAX_AGG_SIZE
},
383 { RT2870_REG_LED_CFG
, 0x7f031e46 },
384 { RT2870_REG_PBF_MAX_PCNT
, 0x1f3fbf9f },
385 { RT2870_REG_TX_RTY_CFG
, 0x47d01f0f },
386 { RT2870_REG_AUTO_RSP_CFG
, 0x00000013 },
387 { RT2870_REG_TX_CCK_PROT_CFG
, 0x05740003 },
388 { RT2870_REG_TX_OFDM_PROT_CFG
, 0x05740003 },
389 { RT2870_REG_PBF_CFG
, 0x00f40006 },
390 { RT2870_REG_TX_MM40_PROT_CFG
, 0x03f44084 },
391 { RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, 0x00000030 },
392 { RT2870_REG_TX_GF20_PROT_CFG
, 0x01744004 },
393 { RT2870_REG_TX_GF40_PROT_CFG
, 0x03f44084 },
394 { RT2870_REG_TX_MM20_PROT_CFG
, 0x01744004 },
395 { RT2870_REG_TX_TXOP_CTRL_CFG
, 0x0000583f },
396 { RT2870_REG_TX_RTS_CFG
, 0x00092b20 },
397 { RT2870_REG_TX_EXP_ACK_TIME
, 0x002400ca },
398 { RT2870_REG_HCCAPSMP_TXOP_HLDR_ET
, 0x00000002 },
399 { RT2870_REG_XIFS_TIME_CFG
, 0x33a41010 },
400 { RT2870_REG_PWR_PIN_CFG
, 0x00000003 },
401 { RT2870_REG_SCHDMA_WMM_AIFSN_CFG
, 0x00002273 },
402 { RT2870_REG_SCHDMA_WMM_CWMIN_CFG
, 0x00002344 },
403 { RT2870_REG_SCHDMA_WMM_CWMAX_CFG
, 0x000034aa },
406 #define RT2870_DEF_MAC_SIZE (sizeof(rt2870_def_mac) / sizeof(rt2870_def_mac[0]))
430 #define RT2870_DEF_BBP_SIZE (sizeof(rt2870_def_bbp) / sizeof(rt2870_def_bbp[0]))
432 SYSCTL_NODE(_hw
, OID_AUTO
, rt2870
, CTLFLAG_RD
, 0, "RT2870 driver parameters");
435 static int rt2870_debug
= 0;
436 SYSCTL_INT(_hw_rt2870
, OID_AUTO
, debug
, CTLFLAG_RW
, &rt2870_debug
, 0, "rt2870 debug level");
437 TUNABLE_INT("hw.rt2870.debug", &rt2870_debug
);
443 static int rt2870_probe(device_t dev
)
445 struct usb_attach_arg
*uaa
;
447 uaa
= device_get_ivars(dev
);
449 if (uaa
->iface
!= NULL
)
452 return (usb_lookup(rt2870_usb_devno
, uaa
->vendor
, uaa
->product
) != NULL
) ?
453 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
;
459 static int rt2870_attach(device_t dev
)
461 struct rt2870_softc
*sc
;
462 struct usb_attach_arg
*uaa
;
463 usbd_status usb_error
;
464 struct ieee80211com
*ic
;
466 int error
, ntries
, i
;
468 sc
= device_get_softc(dev
);
469 uaa
= device_get_ivars(dev
);
472 sc
->usb_dev
= uaa
->device
;
475 sc
->debug
= rt2870_debug
;
477 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev
),
478 SYSCTL_CHILDREN(device_get_sysctl_tree(dev
)), OID_AUTO
,
479 "debug", CTLFLAG_RW
, &sc
->debug
, 0, "rt2870 debug level");
482 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
484 device_get_nameunit(sc
->dev
));
486 if (usbd_set_config_no(sc
->usb_dev
, RT2870_USB_CONFIG_NO
, 0) != 0)
488 printf("%s: could not set USB configuration no\n",
489 device_get_nameunit(sc
->dev
));
493 usb_error
= usbd_device2interface_handle(sc
->usb_dev
,
494 RT2870_USB_IFACE_INDEX
, &sc
->usb_iface
);
497 printf("%s: could not get USB interface handle\n",
498 device_get_nameunit(sc
->dev
));
502 error
= rt2870_find_usb_endpoints(sc
);
506 for (ntries
= 0; ntries
< 100; ntries
++)
508 sc
->mac_rev
= rt2870_io_mac_read(sc
, RT2870_REG_MAC_CSR0
);
509 if (sc
->mac_rev
!= 0x00000000 && sc
->mac_rev
!= 0xffffffff)
517 printf("%s: timeout waiting for NIC to initialize\n",
518 device_get_nameunit(sc
->dev
));
522 rt2870_read_eeprom(sc
);
524 printf("%s: MAC/BBP RT2870 (rev 0x%08x), RF %s\n",
525 device_get_nameunit(sc
->dev
), sc
->mac_rev
,
526 rt2870_rf_name(sc
->rf_rev
));
528 sc
->flags
|= RT2870_SOFTC_FLAGS_VALID
;
530 /* clear key tables */
532 rt2870_asic_clear_keytables(sc
);
534 /* open Tx and Rx USB bulk pipes */
536 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
538 error
= usbd_open_pipe(sc
->usb_iface
, sc
->tx_ring
[i
].usb_ep
,
539 USBD_EXCLUSIVE_USE
, &sc
->tx_ring
[i
].usb_pipe
);
542 printf("%s: could not open Tx pipe #%d: %s\n",
543 device_get_nameunit(sc
->dev
), i
, usbd_errstr(error
));
548 error
= usbd_open_pipe(sc
->usb_iface
, sc
->rx_ring
.usb_ep
,
549 USBD_EXCLUSIVE_USE
, &sc
->rx_ring
.usb_pipe
);
552 printf("%s: could not open Rx pipe: %s\n",
553 device_get_nameunit(sc
->dev
), usbd_errstr(error
));
557 /* allocate Tx and Rx rings */
559 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
561 error
= rt2870_alloc_tx_ring(sc
, &sc
->tx_ring
[i
], i
);
564 printf("%s: could not allocate Tx ring #%d\n",
565 device_get_nameunit(sc
->dev
), i
);
570 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
571 sc
->tx_ring_mgtqid
= 5;
573 sc
->tx_ring_mgtqid
= 0;
575 error
= rt2870_alloc_rx_ring(sc
, &sc
->rx_ring
);
578 printf("%s: could not allocate Rx ring\n",
579 device_get_nameunit(sc
->dev
));
583 rt2870_reset_cmd_ring(sc
, &sc
->cmd_ring
);
585 callout_init(&sc
->periodic_ch
, 0);
586 callout_init(&sc
->tx_watchdog_ch
, 0);
588 ifp
= sc
->ifp
= if_alloc(IFT_ETHER
);
591 printf("%s: could not if_alloc()\n",
592 device_get_nameunit(sc
->dev
));
599 if_initname(ifp
, "rt2870", device_get_unit(sc
->dev
));
601 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
|
604 ifp
->if_init
= rt2870_init
;
605 ifp
->if_ioctl
= rt2870_ioctl
;
606 ifp
->if_start
= rt2870_start
;
608 IFQ_SET_MAXLEN(&ifp
->if_snd
, IFQ_MAXLEN
);
609 ifp
->if_snd
.ifq_drv_maxlen
= IFQ_MAXLEN
;
610 IFQ_SET_READY(&ifp
->if_snd
);
616 ic
->ic_phytype
= IEEE80211_T_HT
;
617 ic
->ic_opmode
= IEEE80211_M_STA
;
618 ic
->ic_state
= IEEE80211_S_INIT
;
620 ic
->ic_caps
= IEEE80211_C_MONITOR
|
627 IEEE80211_C_SHPREAMBLE
|
634 IEEE80211_C_AES_CCM
|
637 ic
->ic_htcaps
= IEEE80211_HTC_HT
|
638 IEEE80211_HTC_AMSDU
| /* A-MSDU Tx */
639 IEEE80211_HTC_AMPDU
| /* A-MPDU Tx */
640 IEEE80211_HTCAP_MAXAMSDU_3839
| /* max. A-MSDU Rx length */
641 IEEE80211_HTCAP_CHWIDTH40
| /* HT 40MHz channel width */
642 IEEE80211_HTCAP_GREENFIELD
| /* HT greenfield */
643 IEEE80211_HTCAP_SHORTGI20
| /* HT 20MHz short GI */
644 IEEE80211_HTCAP_SHORTGI40
| /* HT 40MHz short GI */
645 IEEE80211_HTCAP_SMPS_OFF
; /* MIMO power save disabled */
647 /* spatial streams */
649 if (sc
->nrxpath
== 2)
650 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_2STREAM
;
651 else if (sc
->nrxpath
== 3)
652 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_3STREAM
;
654 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_1STREAM
;
657 ic
->ic_htcaps
|= IEEE80211_HTCAP_TXSTBC
;
661 if (sc
->mac_rev
!= 0x28600100)
662 ic
->ic_htcaps
|= IEEE80211_HTCAP_DELBA
;
667 ic
->ic_regdomain
= 0;
668 ic
->ic_countrycode
= CTRY_DEFAULT
;
671 rt2870_init_channels(sc
);
673 rt2870_init_channels_ht40(sc
);
675 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, sc
->mac_addr
);
677 ieee80211_ifattach(ic
);
679 sc
->newstate
= ic
->ic_newstate
;
680 ic
->ic_newstate
= rt2870_newstate
;
682 ic
->ic_reset
= rt2870_reset
;
683 ic
->ic_node_alloc
= rt2870_node_alloc
;
685 sc
->node_cleanup
= ic
->ic_node_cleanup
;
686 ic
->ic_node_cleanup
= rt2870_node_cleanup
;
688 ic
->ic_scan_start
= rt2870_scan_start
;
689 ic
->ic_scan_end
= rt2870_scan_end
;
690 ic
->ic_set_channel
= rt2870_set_channel
;
691 ic
->ic_newassoc
= rt2870_newassoc
;
692 ic
->ic_updateslot
= rt2870_updateslot
;
693 ic
->ic_wme
.wme_update
= rt2870_wme_update
;
694 ic
->ic_update_beacon
= rt2870_update_beacon
;
695 ic
->ic_crypto
.cs_key_update_begin
= rt2870_key_update_begin
;
696 ic
->ic_crypto
.cs_key_update_end
= rt2870_key_update_end
;
697 ic
->ic_crypto
.cs_key_set
= rt2870_key_set
;
698 ic
->ic_crypto
.cs_key_delete
= rt2870_key_delete
;
699 ic
->ic_raw_xmit
= rt2870_raw_xmit
;
701 sc
->recv_action
= ic
->ic_recv_action
;
702 ic
->ic_recv_action
= rt2870_recv_action
;
704 sc
->send_action
= ic
->ic_send_action
;
705 ic
->ic_send_action
= rt2870_send_action
;
707 sc
->addba_response
= ic
->ic_addba_response
;
708 ic
->ic_addba_response
= rt2870_addba_response
;
710 sc
->addba_stop
= ic
->ic_addba_stop
;
711 ic
->ic_addba_stop
= rt2870_addba_stop
;
713 /* override Rx A-MPDU factor */
715 ic
->ic_ampdu_rxmax
= IEEE80211_HTCAP_MAXRXAMPDU_32K
;
716 ic
->ic_ampdu_density
= IEEE80211_HTCAP_MPDUDENSITY_NA
;
717 ic
->ic_ampdu_limit
= ic
->ic_ampdu_rxmax
;
719 /* hardware requires padding between 802.11 frame header and body */
721 ic
->ic_flags
|= IEEE80211_F_WME
| IEEE80211_F_DATAPAD
| IEEE80211_F_DOTH
;
723 ic
->ic_flags_ext
|= IEEE80211_FEXT_SWBMISS
;
725 ic
->ic_max_aid
= RT2870_SOFTC_STAID_COUNT
;
727 rt2870_amrr_init(&sc
->amrr
, ic
,
729 RT2870_AMRR_MIN_SUCCESS_THRESHOLD
,
730 RT2870_AMRR_MAX_SUCCESS_THRESHOLD
,
733 bpfattach2(ifp
, DLT_IEEE802_11_RADIO
,
734 sizeof(struct ieee80211_frame
) + IEEE80211_RADIOTAP_HDRLEN
,
737 sc
->rxtap_len
= sizeof(sc
->rxtapu
);
738 sc
->rxtap
.ihdr
.it_len
= htole16(sc
->rxtap_len
);
739 sc
->rxtap
.ihdr
.it_present
= htole32(RT2870_SOFTC_RX_RADIOTAP_PRESENT
);
741 sc
->txtap_len
= sizeof(sc
->txtapu
);
742 sc
->txtap
.ihdr
.it_len
= htole16(sc
->txtap_len
);
743 sc
->txtap
.ihdr
.it_present
= htole32(RT2870_SOFTC_TX_RADIOTAP_PRESENT
);
745 /* init task queue */
747 TASK_INIT(&sc
->rx_done_task
, 0, rt2870_rx_done_task
, sc
);
748 TASK_INIT(&sc
->tx_done_task
, 0, rt2870_tx_done_task
, sc
);
749 TASK_INIT(&sc
->periodic_task
, 0, rt2870_periodic_task
, sc
);
750 TASK_INIT(&sc
->cmd_task
, 0, rt2870_cmd_task
, sc
);
752 sc
->rx_process_limit
= 100;
754 sc
->taskqueue
= taskqueue_create("rt2870_taskq", M_NOWAIT
,
755 taskqueue_thread_enqueue
, &sc
->taskqueue
);
757 taskqueue_start_threads(&sc
->taskqueue
, 1, PI_NET
, "%s taskq",
758 device_get_nameunit(sc
->dev
));
760 ieee80211_media_init(ic
, rt2870_media_change
, ieee80211_media_status
);
762 rt2870_sysctl_attach(sc
);
765 ieee80211_announce(ic
);
767 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, sc
->usb_dev
, sc
->dev
);
773 /* close Tx and Rx USB bulk pipes */
775 if (sc
->rx_ring
.usb_pipe
!= NULL
)
777 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
778 usbd_close_pipe(sc
->rx_ring
.usb_pipe
);
779 sc
->rx_ring
.usb_pipe
= NULL
;
782 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
784 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
786 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
787 usbd_close_pipe(sc
->tx_ring
[i
].usb_pipe
);
788 sc
->tx_ring
[i
].usb_pipe
= NULL
;
792 /* free Tx and Rx rings */
794 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
795 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
797 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
805 static int rt2870_detach(device_t dev
)
807 struct rt2870_softc
*sc
;
808 struct ieee80211com
*ic
;
812 if (!device_is_attached(dev
))
815 sc
= device_get_softc(dev
);
819 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
821 device_get_nameunit(sc
->dev
));
823 sc
->flags
&= ~RT2870_SOFTC_FLAGS_VALID
;
827 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
829 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
831 callout_stop(&sc
->periodic_ch
);
832 callout_stop(&sc
->tx_watchdog_ch
);
834 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
835 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
836 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
837 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
839 /* close Tx and Rx USB bulk pipes */
841 if (sc
->rx_ring
.usb_pipe
!= NULL
)
843 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
844 usbd_close_pipe(sc
->rx_ring
.usb_pipe
);
845 sc
->rx_ring
.usb_pipe
= NULL
;
848 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
850 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
852 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
853 usbd_close_pipe(sc
->tx_ring
[i
].usb_pipe
);
854 sc
->tx_ring
[i
].usb_pipe
= NULL
;
858 /* free Tx and Rx rings */
860 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
861 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
863 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
867 ieee80211_ifdetach(ic
);
871 taskqueue_free(sc
->taskqueue
);
873 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->usb_dev
, sc
->dev
);
879 * rt2870_find_usb_endpoints
881 static int rt2870_find_usb_endpoints(struct rt2870_softc
*sc
)
883 usb_interface_descriptor_t
*id
;
884 usb_endpoint_descriptor_t
*ed
;
887 id
= usbd_get_interface_descriptor(sc
->usb_iface
);
889 sc
->usb_endpoints
= id
->bNumEndpoints
;
890 if ((sc
->usb_endpoints
!= (RT2870_SOFTC_TX_RING_COUNT
+ 1)) &&
891 (sc
->usb_endpoints
!= (RT2870_SOFTC_TX_RING_COUNT
- 2 + 1)))
893 printf("%s: wrong number of USB endpoints=%d\n",
894 device_get_nameunit(sc
->dev
), sc
->usb_endpoints
);
898 sc
->rx_ring
.usb_ep
= -1;
900 for (i
= 0; i
< RT2870_SOFTC_TX_RING_COUNT
; i
++)
901 sc
->tx_ring
[i
].usb_ep
= -1;
903 for (i
= 0, j
= 0; i
< id
->bNumEndpoints
; i
++)
905 ed
= usbd_interface2endpoint_descriptor(sc
->usb_iface
, i
);
908 printf("%s: no endpoint descriptor #%d for USB interface\n",
909 device_get_nameunit(sc
->dev
), i
);
913 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
914 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
916 sc
->rx_ring
.usb_ep
= ed
->bEndpointAddress
;
918 else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
919 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
921 if (j
< RT2870_SOFTC_TX_RING_COUNT
)
922 sc
->tx_ring
[j
++].usb_ep
= ed
->bEndpointAddress
;
924 sc
->tx_ring
[RT2870_SOFTC_TX_RING_COUNT
- 1].usb_ep
=
925 ed
->bEndpointAddress
;
929 if ((sc
->rx_ring
.usb_ep
== -1) ||
930 ((j
!= RT2870_SOFTC_TX_RING_COUNT
) && (j
!= RT2870_SOFTC_TX_RING_COUNT
- 2)))
932 printf("%s: missing USB endpoints\n",
933 device_get_nameunit(sc
->dev
));
941 * rt2870_init_channels
943 static void rt2870_init_channels(struct rt2870_softc
*sc
)
945 struct ieee80211com
*ic
;
946 struct ieee80211_channel
*c
;
951 /* set supported channels for 2GHz band */
953 for (i
= 1; i
<= 14; i
++)
955 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
956 flags
= IEEE80211_CHAN_B
;
958 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
962 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
963 flags
= IEEE80211_CHAN_B
| IEEE80211_CHAN_HT20
;
965 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
969 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
970 flags
= IEEE80211_CHAN_G
;
972 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
976 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
977 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT20
;
979 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
984 /* set supported channels for 5GHz band */
986 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
987 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
989 for (i
= 36; i
<= 64; i
+= 4)
991 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
992 flags
= IEEE80211_CHAN_A
;
994 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
998 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
999 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
1001 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
1003 c
->ic_flags
= flags
;
1006 for (i
= 100; i
<= 140; i
+= 4)
1008 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1009 flags
= IEEE80211_CHAN_A
;
1011 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
1013 c
->ic_flags
= flags
;
1015 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1016 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
1018 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
1020 c
->ic_flags
= flags
;
1023 for (i
= 149; i
<= 165; i
+= 4)
1025 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1026 flags
= IEEE80211_CHAN_A
;
1028 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
1030 c
->ic_flags
= flags
;
1032 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1033 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
1035 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
1037 c
->ic_flags
= flags
;
1043 * rt2870_init_channels_ht40
1045 static void rt2870_init_channels_ht40(struct rt2870_softc
*sc
)
1047 struct ieee80211com
*ic
;
1048 struct ieee80211_channel
*c
, *cent
, *ext
;
1053 /* set supported channels for 2GHz band */
1055 for (i
= 1; i
<= 14; i
++)
1057 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT40
;
1059 /* find the center channel */
1061 cent
= ieee80211_find_channel_byieee(ic
, i
,
1062 flags
& ~IEEE80211_CHAN_HT
);
1065 printf("%s: skip channel %d, could not find center channel\n",
1066 device_get_nameunit(sc
->dev
), i
);
1070 /* find the extension channel */
1072 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1073 flags
& ~IEEE80211_CHAN_HT
);
1076 printf("%s: skip channel %d, could not find extension channel\n",
1077 device_get_nameunit(sc
->dev
), i
);
1081 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1084 c
->ic_extieee
= ext
->ic_ieee
;
1085 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1086 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1088 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1091 c
->ic_extieee
= cent
->ic_ieee
;
1092 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1093 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1096 /* set supported channels for 5GHz band */
1098 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
1099 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
1101 for (i
= 36; i
<= 64; i
+= 4)
1103 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1105 /* find the center channel */
1107 cent
= ieee80211_find_channel_byieee(ic
, i
,
1108 flags
& ~IEEE80211_CHAN_HT
);
1111 printf("%s: skip channel %d, could not find center channel\n",
1112 device_get_nameunit(sc
->dev
), i
);
1116 /* find the extension channel */
1118 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1119 flags
& ~IEEE80211_CHAN_HT
);
1122 printf("%s: skip channel %d, could not find extension channel\n",
1123 device_get_nameunit(sc
->dev
), i
);
1127 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1130 c
->ic_extieee
= ext
->ic_ieee
;
1131 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1132 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1134 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1137 c
->ic_extieee
= cent
->ic_ieee
;
1138 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1139 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1142 for (i
= 100; i
<= 140; i
+= 4)
1144 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1146 /* find the center channel */
1148 cent
= ieee80211_find_channel_byieee(ic
, i
,
1149 flags
& ~IEEE80211_CHAN_HT
);
1152 printf("%s: skip channel %d, could not find center channel\n",
1153 device_get_nameunit(sc
->dev
), i
);
1157 /* find the extension channel */
1159 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1160 flags
& ~IEEE80211_CHAN_HT
);
1163 printf("%s: skip channel %d, could not find extension channel\n",
1164 device_get_nameunit(sc
->dev
), i
);
1168 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1171 c
->ic_extieee
= ext
->ic_ieee
;
1172 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1173 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1175 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1178 c
->ic_extieee
= cent
->ic_ieee
;
1179 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1180 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1183 for (i
= 149; i
<= 165; i
+= 4)
1185 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1187 /* find the center channel */
1189 cent
= ieee80211_find_channel_byieee(ic
, i
,
1190 flags
& ~IEEE80211_CHAN_HT
);
1193 printf("%s: skip channel %d, could not find center channel\n",
1194 device_get_nameunit(sc
->dev
), i
);
1198 /* find the extension channel */
1200 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1201 flags
& ~IEEE80211_CHAN_HT
);
1204 printf("%s: skip channel %d, could not find extension channel\n",
1205 device_get_nameunit(sc
->dev
), i
);
1209 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1212 c
->ic_extieee
= ext
->ic_ieee
;
1213 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1214 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1216 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1219 c
->ic_extieee
= cent
->ic_ieee
;
1220 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1221 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1227 * rt2870_init_locked
1229 static void rt2870_init_locked(void *priv
)
1231 struct rt2870_softc
*sc
;
1232 struct ieee80211com
*ic
;
1234 struct rt2870_softc_rx_data
*data
;
1235 int ntries
, error
, i
;
1236 uint32_t tmp
, stacnt
[6];
1242 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1243 "%s: initializing\n",
1244 device_get_nameunit(sc
->dev
));
1246 if (!(sc
->flags
& RT2870_SOFTC_FLAGS_UCODE_LOADED
))
1248 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1249 "%s: loading 8051 microcode\n",
1250 device_get_nameunit(sc
->dev
));
1252 error
= rt2870_io_mcu_load_ucode(sc
, rt2870_ucode
, sizeof(rt2870_ucode
));
1255 printf("%s: could not load 8051 microcode\n",
1256 device_get_nameunit(sc
->dev
));
1260 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1261 "%s: 8051 microcode was successfully loaded\n",
1262 device_get_nameunit(sc
->dev
));
1264 sc
->flags
|= RT2870_SOFTC_FLAGS_UCODE_LOADED
;
1267 /* wait while DMA engine is busy */
1269 for (ntries
= 0; ntries
< 100; ntries
++)
1271 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1272 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1280 printf("%s: timeout waiting for DMA engine\n",
1281 device_get_nameunit(sc
->dev
));
1286 tmp
|= RT2870_REG_TX_WB_DDONE
;
1288 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1290 /* PBF hardware reset */
1292 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_SYS_CTRL
);
1296 rt2870_io_mac_write(sc
, RT2870_REG_PBF_SYS_CTRL
, tmp
);
1298 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1299 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1301 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1303 rt2870_io_mcu_reset(sc
);
1305 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1307 /* init Tx power per rate */
1309 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
1311 if (sc
->txpow_rate_20mhz
[i
] == 0xffffffff)
1314 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
),
1315 sc
->txpow_rate_20mhz
[i
]);
1318 for (i
= 0; i
< RT2870_DEF_MAC_SIZE
; i
++)
1319 rt2870_io_mac_write(sc
, rt2870_def_mac
[i
].reg
,
1320 rt2870_def_mac
[i
].val
);
1322 /* wait while MAC is busy */
1324 for (ntries
= 0; ntries
< 100; ntries
++)
1326 if (!(rt2870_io_mac_read(sc
, RT2870_REG_STATUS_CFG
) &
1327 (RT2870_REG_STATUS_TX_BUSY
| RT2870_REG_STATUS_RX_BUSY
)))
1335 printf("%s: timeout waiting for MAC\n",
1336 device_get_nameunit(sc
->dev
));
1340 /* clear Host to MCU mailbox */
1342 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX_BBP_AGENT
, 0);
1343 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX
, 0);
1347 error
= rt2870_init_bbp(sc
);
1351 /* set up maximum buffer sizes */
1353 tmp
= (1 << 12) | RT2870_MAX_AGG_SIZE
;
1355 if (sc
->mac_rev
>= 0x28720200 && sc
->mac_rev
< 0x30700200)
1361 rt2870_io_mac_write(sc
, RT2870_REG_MAX_LEN_CFG
, tmp
);
1363 /* set mac address */
1365 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, IF_LLADDR(ifp
));
1367 rt2870_asic_set_macaddr(sc
, ic
->ic_myaddr
);
1369 /* clear statistic registers */
1371 rt2870_io_mac_read_multi(sc
, RT2870_REG_RX_STA_CNT0
,
1372 stacnt
, sizeof(stacnt
));
1374 /* send LEDs operating mode to microcontroller */
1376 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED1
,
1377 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[0]);
1378 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED2
,
1379 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[1]);
1380 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED3
,
1381 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[2]);
1383 /* write vendor-specific BBP values (from EEPROM) */
1385 for (i
= 0; i
< RT2870_SOFTC_BBP_EEPROM_COUNT
; i
++)
1387 if (sc
->bbp_eeprom
[i
].reg
== 0x00 ||
1388 sc
->bbp_eeprom
[i
].reg
== 0xff)
1391 rt2870_io_bbp_write(sc
, sc
->bbp_eeprom
[i
].reg
,
1392 sc
->bbp_eeprom
[i
].val
);
1395 /* disable non-existing Rx chains */
1397 tmp
= rt2870_io_bbp_read(sc
, 3);
1399 tmp
&= ~((1 << 4) | (1 << 3));
1401 if (sc
->nrxpath
== 3)
1403 else if (sc
->nrxpath
== 2)
1406 rt2870_io_bbp_write(sc
, 3, tmp
);
1408 /* disable non-existing Tx chains */
1410 tmp
= rt2870_io_bbp_read(sc
, 1);
1412 tmp
&= ~((1 << 4) | (1 << 3));
1414 if (sc
->ntxpath
== 2)
1417 rt2870_io_bbp_write(sc
, 1, tmp
);
1419 /* set current channel */
1421 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1423 /* turn radio LED on */
1425 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
);
1427 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_BOOT
,
1428 RT2870_REG_H2M_TOKEN_NO_INTR
, 0);
1430 /* set RTS threshold */
1432 rt2870_asic_update_rtsthreshold(sc
);
1436 rt2870_asic_update_txpower(sc
);
1438 /* set up protection mode */
1440 sc
->tx_ampdu_sessions
= 0;
1442 rt2870_asic_updateprot(sc
);
1444 /* clear beacon frame space (entries = 8, entry size = 512) */
1446 rt2870_io_mac_set_region_4(sc
, RT2870_REG_BEACON_BASE(0), 0, 1024);
1448 /* enable Tx/Rx DMA engine */
1450 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
);
1455 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
, tmp
);
1457 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1458 rt2870_io_mac_write(sc
, RT2870_REG_TX_TXOP_CTRL_CFG
, 0x583f);
1460 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, RT2870_REG_TX_ENABLE
);
1462 for (ntries
= 0; ntries
< 200; ntries
++)
1464 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1465 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1473 printf("%s: timeout waiting for DMA engine\n",
1474 device_get_nameunit(sc
->dev
));
1480 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
, 0);
1481 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
1484 tmp
|= RT2870_REG_TX_WB_DDONE
|
1485 RT2870_REG_RX_DMA_ENABLE
|
1486 RT2870_REG_TX_DMA_ENABLE
;
1488 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1490 tmp
= RT2870_REG_USB_DMA_TX_ENABLE
|
1491 RT2870_REG_USB_DMA_RX_ENABLE
|
1492 RT2870_REG_USB_DMA_RX_AGG_ENABLE
|
1493 /* Rx agg limit in unit of 1024 byte */
1494 ((RT2870_USB_RX_BULK_BUFLEN
/ 1024 - 3) << RT2870_REG_USB_DMA_RX_AGG_LIMIT_SHIFT
) |
1495 /* Rx agg timeout in unit of 33ns */
1498 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, tmp
);
1502 tmp
= RT2870_REG_RX_FILTER_DROP_CRC_ERR
|
1503 RT2870_REG_RX_FILTER_DROP_PHY_ERR
;
1505 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1507 tmp
|= RT2870_REG_RX_FILTER_DROP_DUPL
|
1508 RT2870_REG_RX_FILTER_DROP_CTS
|
1509 RT2870_REG_RX_FILTER_DROP_BA
|
1510 RT2870_REG_RX_FILTER_DROP_ACK
|
1511 RT2870_REG_RX_FILTER_DROP_VER_ERR
|
1512 RT2870_REG_RX_FILTER_DROP_CTRL_RSV
|
1513 RT2870_REG_RX_FILTER_DROP_CFACK
|
1514 RT2870_REG_RX_FILTER_DROP_CFEND
;
1516 if (ic
->ic_opmode
== IEEE80211_M_STA
)
1517 tmp
|= RT2870_REG_RX_FILTER_DROP_RTS
|
1518 RT2870_REG_RX_FILTER_DROP_PSPOLL
;
1520 if (!(ifp
->if_flags
& IFF_PROMISC
))
1521 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
1524 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
1526 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1527 RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1529 /* clear garbage interrupts */
1531 tmp
= rt2870_io_mac_read(sc
, 0x1300);
1533 taskqueue_unblock(sc
->taskqueue
);
1535 /* init Tx and Rx rings */
1537 for(i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1538 rt2870_reset_tx_ring(sc
, &sc
->tx_ring
[i
]);
1540 rt2870_reset_rx_ring(sc
, &sc
->rx_ring
);
1542 /* start up the receive pipe */
1544 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
1546 data
= &sc
->rx_ring
.data
[i
];
1548 STAILQ_INSERT_TAIL(&sc
->rx_ring
.active
, data
, next
);
1550 usbd_setup_xfer(data
->xfer
, sc
->rx_ring
.usb_pipe
, sc
, data
->buf
,
1551 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
1552 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
1554 usbd_transfer(data
->xfer
);
1557 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
1558 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
1560 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1562 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
1563 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
1567 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
1570 sc
->periodic_round
= 0;
1572 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
1578 rt2870_stop_locked(sc
);
1584 static void rt2870_init(void *priv
)
1586 struct rt2870_softc
*sc
;
1590 rt2870_init_locked(sc
);
1596 static int rt2870_init_bbp(struct rt2870_softc
*sc
)
1601 for (ntries
= 0; ntries
< 20; ntries
++)
1603 tmp
= rt2870_io_bbp_read(sc
, 0);
1604 if (tmp
!= 0x00 && tmp
!= 0xff)
1608 if (tmp
== 0x00 || tmp
== 0xff)
1610 printf("%s: timeout waiting for BBP to wakeup\n",
1611 device_get_nameunit(sc
->dev
));
1615 for (i
= 0; i
< RT2870_DEF_BBP_SIZE
; i
++)
1616 rt2870_io_bbp_write(sc
, rt2870_def_bbp
[i
].reg
,
1617 rt2870_def_bbp
[i
].val
);
1619 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1620 rt2870_io_bbp_write(sc
, 84, 0x19);
1628 static void rt2870_stop_locked(void *priv
)
1630 struct rt2870_softc
*sc
;
1631 struct ieee80211com
*ic
;
1640 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1642 device_get_nameunit(sc
->dev
));
1646 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
1648 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
1650 callout_stop(&sc
->periodic_ch
);
1651 callout_stop(&sc
->tx_watchdog_ch
);
1653 taskqueue_block(sc
->taskqueue
);
1655 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
1656 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
1657 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
1658 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
1660 /* clear key tables */
1662 rt2870_asic_clear_keytables(sc
);
1666 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SYS_CTRL
);
1668 tmp
&= ~(RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1670 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, tmp
);
1672 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1676 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1677 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1678 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1680 /* abort any pending transfers */
1682 if (sc
->rx_ring
.usb_pipe
!= NULL
)
1683 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
1685 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1686 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
1687 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
1689 if (sc
->beacon_mbuf
!= NULL
)
1691 m_free(sc
->beacon_mbuf
);
1692 sc
->beacon_mbuf
= NULL
;
1695 rt2870_reset_cmd_ring(sc
, &sc
->cmd_ring
);
1701 static void rt2870_stop(void *priv
)
1703 struct rt2870_softc
*sc
;
1707 rt2870_stop_locked(sc
);
1713 static void rt2870_start(struct ifnet
*ifp
)
1715 struct rt2870_softc
*sc
;
1716 struct ieee80211com
*ic
;
1717 struct ieee80211_node
*ni
;
1718 struct ether_header
*eh
;
1725 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
1730 IF_POLL(&ic
->ic_mgtq
, m
);
1733 if (sc
->tx_ring
[sc
->tx_ring_mgtqid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1735 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1736 "%s: if_start: Tx ring with qid=%d is full\n",
1737 device_get_nameunit(sc
->dev
), sc
->tx_ring_mgtqid
);
1739 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1741 sc
->tx_data_queue_full
[sc
->tx_ring_mgtqid
]++;
1746 IF_DEQUEUE(&ic
->ic_mgtq
, m
);
1750 ni
= (struct ieee80211_node
*) m
->m_pkthdr
.rcvif
;
1752 m
->m_pkthdr
.rcvif
= NULL
;
1754 if (bpf_peers_present(ic
->ic_rawbpf
))
1755 bpf_mtap(ic
->ic_rawbpf
, m
);
1757 if (rt2870_tx_frame(sc
, m
, ni
, sc
->tx_ring_mgtqid
) != 0)
1762 if (ic
->ic_state
!= IEEE80211_S_RUN
)
1765 IFQ_DRV_DEQUEUE(&ifp
->if_snd
, m
);
1769 if (ic
->ic_flags
& IEEE80211_F_SCAN
)
1770 ieee80211_cancel_scan(ic
);
1772 if (m
->m_len
< sizeof(struct ether_header
) &&
1773 !(m
= m_pullup(m
, sizeof (struct ether_header
))))
1776 eh
= mtod(m
, struct ether_header
*);
1778 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1781 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1782 "%s: if_start: could not find Tx node\n",
1783 device_get_nameunit(sc
->dev
));
1790 ieee80211_classify(ic
, m
, ni
);
1792 qid
= M_WME_GETAC(m
);
1794 if (sc
->tx_ring
[qid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1796 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1797 "%s: if_start: Tx ring with qid=%d is full\n",
1798 device_get_nameunit(sc
->dev
), qid
);
1801 ieee80211_free_node(ni
);
1803 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1806 sc
->tx_data_queue_full
[qid
]++;
1813 m
= ieee80211_encap(ic
, m
, ni
);
1816 ieee80211_free_node(ni
);
1823 if (bpf_peers_present(ic
->ic_rawbpf
))
1824 bpf_mtap(ic
->ic_rawbpf
, m
);
1826 if (rt2870_tx_frame(sc
, m
, ni
, qid
) != 0)
1828 ieee80211_free_node(ni
);
1830 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1837 sc
->tx_timer
= RT2870_TX_WATCHDOG_TIMEOUT
;
1839 ic
->ic_lastdata
= ticks
;
1841 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
1848 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1850 struct rt2870_softc
*sc
;
1851 struct ieee80211com
*ic
;
1862 if (ifp
->if_flags
& IFF_UP
)
1864 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1866 if ((ifp
->if_flags
^ sc
->if_flags
) & IFF_PROMISC
)
1867 rt2870_asic_update_promisc(sc
);
1871 rt2870_init_locked(sc
);
1876 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1877 rt2870_stop_locked(sc
);
1880 sc
->if_flags
= ifp
->if_flags
;
1884 error
= ieee80211_ioctl(ic
, cmd
, data
);
1887 if (error
== ENETRESET
)
1889 if ((ifp
->if_flags
& IFF_UP
) &&
1890 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
) &&
1891 (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
))
1893 rt2870_stop_locked(sc
);
1894 rt2870_init_locked(sc
);
1906 static int rt2870_reset(struct ifnet
*ifp
)
1908 struct rt2870_softc
*sc
;
1909 struct ieee80211com
*ic
;
1914 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1917 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1925 static int rt2870_newstate(struct ieee80211com
*ic
,
1926 enum ieee80211_state nstate
, int arg
)
1928 struct rt2870_softc
*sc
;
1930 struct rt2870_cmd_argv_newstate cmd_argv
;
1935 cmd_argv
.nstate
= nstate
;
1938 rt2870_do_async(sc
, rt2870_newstate_cb
, &cmd_argv
,
1939 sizeof(struct rt2870_cmd_argv_newstate
));
1947 static void rt2870_scan_start(struct ieee80211com
*ic
)
1949 struct rt2870_softc
*sc
;
1955 rt2870_do_async(sc
, rt2870_scan_start_cb
, NULL
, 0);
1961 static void rt2870_scan_end(struct ieee80211com
*ic
)
1963 struct rt2870_softc
*sc
;
1969 rt2870_do_async(sc
, rt2870_scan_end_cb
, NULL
, 0);
1973 * rt2870_set_channel
1975 static void rt2870_set_channel(struct ieee80211com
*ic
)
1977 struct rt2870_softc
*sc
;
1983 rt2870_do_async(sc
, rt2870_set_channel_cb
, NULL
, 0);
1989 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
)
1991 struct rt2870_softc
*sc
;
1992 struct ieee80211com
*ic
;
1994 struct rt2870_cmd_argv_newassoc cmd_argv
;
2000 cmd_argv
.isnew
= isnew
;
2001 memcpy(cmd_argv
.macaddr
, ni
->ni_macaddr
, IEEE80211_ADDR_LEN
);
2003 rt2870_do_async(sc
, rt2870_newassoc_cb
, &cmd_argv
,
2004 sizeof(struct rt2870_cmd_argv_newassoc
));
2010 static void rt2870_updateslot(struct ifnet
*ifp
)
2012 struct rt2870_softc
*sc
;
2016 rt2870_do_async(sc
, rt2870_updateslot_cb
, NULL
, 0);
2022 static int rt2870_wme_update(struct ieee80211com
*ic
)
2024 struct rt2870_softc
*sc
;
2030 rt2870_do_async(sc
, rt2870_wme_update_cb
, NULL
, 0);
2036 * rt2870_update_beacon
2038 static void rt2870_update_beacon(struct ieee80211com
*ic
, int what
)
2040 struct rt2870_softc
*sc
;
2042 struct rt2870_cmd_argv_updatebeacon cmd_argv
;
2047 cmd_argv
.what
= what
;
2049 rt2870_do_async(sc
, rt2870_update_beacon_cb
, &cmd_argv
,
2050 sizeof(struct rt2870_cmd_argv_updatebeacon
));
2054 * rt2870_key_update_begin
2056 static void rt2870_key_update_begin(struct ieee80211com
*ic
)
2058 struct rt2870_softc
*sc
;
2064 rt2870_do_async(sc
, rt2870_key_update_begin_cb
, NULL
, 0);
2068 * rt2870_key_update_end
2070 static void rt2870_key_update_end(struct ieee80211com
*ic
)
2072 struct rt2870_softc
*sc
;
2078 rt2870_do_async(sc
, rt2870_key_update_end_cb
, NULL
, 0);
2084 static int rt2870_key_set(struct ieee80211com
*ic
,
2085 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
2087 struct rt2870_softc
*sc
;
2089 struct ieee80211_node
*ni
;
2090 struct rt2870_softc_node
*rni
;
2091 struct rt2870_cmd_argv_keyset cmd_argv
;
2093 if (k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_WEP
&&
2094 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_TKIP
&&
2095 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_AES_CCM
)
2101 cmd_argv
.opmode
= ic
->ic_opmode
;
2103 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2105 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2107 ni
= ieee80211_find_node(&ic
->ic_sta
, mac
);
2108 rni
= (struct rt2870_softc_node
*) ni
;
2110 cmd_argv
.staid
= rni
->staid
;
2112 ieee80211_free_node(ni
);
2115 rt2870_do_async(sc
, rt2870_key_set_cb
, &cmd_argv
,
2116 sizeof(struct rt2870_cmd_argv_keyset
));
2124 static int rt2870_key_delete(struct ieee80211com
*ic
,
2125 const struct ieee80211_key
*k
)
2127 struct rt2870_softc
*sc
;
2129 struct rt2870_cmd_argv_keydelete cmd_argv
;
2134 cmd_argv
.opmode
= ic
->ic_opmode
;
2136 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2138 rt2870_do_async(sc
, rt2870_key_delete_cb
, &cmd_argv
,
2139 sizeof(struct rt2870_cmd_argv_keydelete
));
2147 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
2148 const struct ieee80211_bpf_params
*params
)
2150 struct ieee80211com
*ic
;
2152 struct rt2870_softc
*sc
;
2162 * rt2870_media_change
2164 static int rt2870_media_change(struct ifnet
*ifp
)
2166 struct rt2870_softc
*sc
;
2171 error
= ieee80211_media_change(ifp
);
2172 if (error
!= ENETRESET
)
2175 if ((ifp
->if_flags
& IFF_UP
) && (ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2187 static struct ieee80211_node
*rt2870_node_alloc(struct ieee80211_node_table
*nt
)
2189 return malloc(sizeof(struct rt2870_softc_node
),
2190 M_80211_NODE
, M_NOWAIT
| M_ZERO
);
2194 * rt2870_node_cleanup
2196 static void rt2870_node_cleanup(struct ieee80211_node
*ni
)
2198 struct rt2870_softc
*sc
;
2199 struct ieee80211com
*ic
;
2201 struct ieee80211_rx_ampdu
*rx_ampdu
;
2202 struct rt2870_softc_node
*rni
;
2203 struct rt2870_cmd_argv_nodecleanup cmd_argv
;
2209 rni
= (struct rt2870_softc_node
*) ni
;
2211 RT2870_DPRINTF(sc
, RT2870_DEBUG_NODE
,
2212 "%s: node cleanup: macaddr=%s, associd=0x%04x, staid=0x%02x\n",
2213 device_get_nameunit(sc
->dev
), ether_sprintf(ni
->ni_macaddr
),
2214 ni
->ni_associd
, rni
->staid
);
2216 if (rni
->staid
!= 0)
2218 cmd_argv
.staid
= rni
->staid
;
2222 for (i
= 0; i
< WME_NUM_TID
; i
++)
2224 rx_ampdu
= &ni
->ni_rx_ampdu
[i
];
2226 if (rx_ampdu
->rxa_flags
& IEEE80211_AGGR_XCHGPEND
)
2228 if (sc
->rx_ampdu_sessions
> 0)
2229 sc
->rx_ampdu_sessions
--;
2231 printf("%s: number of A-MPDU Rx sessions cannot be negative\n",
2232 device_get_nameunit(sc
->dev
));
2236 rt2870_do_async(sc
, rt2870_node_cleanup_cb
,
2237 &cmd_argv
, sizeof(struct rt2870_cmd_argv_nodecleanup
));
2240 sc
->node_cleanup(ni
);
2244 * rt2870_recv_action
2246 static void rt2870_recv_action(struct ieee80211_node
*ni
,
2247 const uint8_t *frm
, const uint8_t *efrm
)
2249 struct rt2870_softc
*sc
;
2250 struct ieee80211com
*ic
;
2252 struct ieee80211_rx_ampdu
*rx_ampdu
;
2253 struct rt2870_softc_node
*rni
;
2254 const struct ieee80211_action
*ia
;
2255 uint16_t baparamset
;
2262 rni
= (struct rt2870_softc_node
*) ni
;
2264 ia
= (const struct ieee80211_action
*) frm
;
2266 if (ia
->ia_category
== IEEE80211_ACTION_CAT_BA
)
2268 switch (ia
->ia_action
)
2270 /* IEEE80211_ACTION_BA_DELBA */
2271 case IEEE80211_ACTION_BA_DELBA
:
2272 baparamset
= LE_READ_2(frm
+ 2);
2273 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2274 rx_ampdu
= &ni
->ni_rx_ampdu
[tid
];
2277 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2278 "%s: received DELBA request: associd=0x%04x, staid=0x%02x, tid=%d\n",
2279 device_get_nameunit(sc
->dev
), ni
->ni_associd
, rni
->staid
, tid
);
2281 if (rni
->staid
!= 0)
2283 rt2870_asic_del_ba_session(sc
, wcid
, tid
);
2285 if (rx_ampdu
->rxa_flags
& IEEE80211_AGGR_XCHGPEND
)
2287 if (sc
->rx_ampdu_sessions
> 0)
2288 sc
->rx_ampdu_sessions
--;
2290 printf("%s: number of A-MPDU Rx sessions cannot be negative\n",
2291 device_get_nameunit(sc
->dev
));
2298 sc
->recv_action(ni
, frm
, efrm
);
2302 * rt2870_send_action
2304 static int rt2870_send_action(struct ieee80211_node
*ni
,
2305 int category
, int action
, uint16_t args
[4])
2307 struct rt2870_softc
*sc
;
2308 struct ieee80211com
*ic
;
2310 struct ieee80211_rx_ampdu
*rx_ampdu
;
2311 struct rt2870_softc_node
*rni
;
2312 uint16_t status
, baparamset
;
2319 rni
= (struct rt2870_softc_node
*) ni
;
2323 if (category
== IEEE80211_ACTION_CAT_BA
)
2327 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2328 case IEEE80211_ACTION_BA_ADDBA_RESPONSE
:
2330 baparamset
= args
[2];
2331 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2332 bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2333 rx_ampdu
= &ni
->ni_rx_ampdu
[tid
];
2335 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2336 "%s: sending ADDBA response: associd=0x%04x, staid=0x%02x, status=%d, tid=%d, bufsize=%d\n",
2337 device_get_nameunit(sc
->dev
), ni
->ni_associd
, rni
->staid
, status
, tid
, bufsize
);
2339 if (status
== IEEE80211_STATUS_SUCCESS
)
2341 rt2870_asic_add_ba_session(sc
, wcid
, tid
);
2343 sc
->rx_ampdu_sessions
++;
2347 /* IEEE80211_ACTION_BA_DELBA */
2348 case IEEE80211_ACTION_BA_DELBA
:
2349 baparamset
= RT2870_SM(args
[0], IEEE80211_DELBAPS_TID
) | args
[1];
2350 tid
= RT2870_MS(baparamset
, IEEE80211_DELBAPS_TID
);
2351 rx_ampdu
= &ni
->ni_rx_ampdu
[tid
];
2353 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2354 "%s: sending DELBA request: associd=0x%04x, staid=0x%02x, tid=%d\n",
2355 device_get_nameunit(sc
->dev
), ni
->ni_associd
, rni
->staid
, tid
);
2357 if (RT2870_MS(baparamset
, IEEE80211_DELBAPS_INIT
) != IEEE80211_DELBAPS_INIT
)
2359 rt2870_asic_del_ba_session(sc
, wcid
, tid
);
2361 if (rx_ampdu
->rxa_flags
& IEEE80211_AGGR_XCHGPEND
)
2363 if (sc
->rx_ampdu_sessions
> 0)
2364 sc
->rx_ampdu_sessions
--;
2366 printf("%s: number of A-MPDU Rx sessions cannot be negative\n",
2367 device_get_nameunit(sc
->dev
));
2374 return sc
->send_action(ni
, category
, action
, args
);
2378 * rt2870_addba_response
2380 static int rt2870_addba_response(struct ieee80211_node
*ni
,
2381 struct ieee80211_tx_ampdu
*tap
,
2382 int status
, int baparamset
, int batimeout
)
2384 struct rt2870_softc
*sc
;
2385 struct ieee80211com
*ic
;
2387 struct rt2870_softc_node
*rni
;
2388 ieee80211_seq seqno
;
2389 int ret
, tid
, old_bufsize
, new_bufsize
;
2394 rni
= (struct rt2870_softc_node
*) ni
;
2396 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2397 old_bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2398 new_bufsize
= old_bufsize
;
2400 if (status
== IEEE80211_STATUS_SUCCESS
)
2402 if (sc
->mac_rev
>= 0x28830300)
2404 if (sc
->mac_rev
>= 0x30700200)
2409 else if (sc
->mac_rev
>= 0x28720200)
2418 if (old_bufsize
> new_bufsize
)
2420 baparamset
&= ~IEEE80211_BAPS_BUFSIZ
;
2421 baparamset
= RT2870_SM(new_bufsize
, IEEE80211_BAPS_BUFSIZ
);
2424 if (!(tap
->txa_flags
& IEEE80211_AGGR_RUNNING
))
2426 sc
->tx_ampdu_sessions
++;
2428 if (sc
->tx_ampdu_sessions
== 1)
2429 rt2870_asic_updateprot(sc
);
2433 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2434 "%s: received ADDBA response: associd=0x%04x, staid=0x%02x, status=%d, tid=%d, "
2435 "old bufsize=%d, new bufsize=%d\n",
2436 device_get_nameunit(sc
->dev
), ni
->ni_associd
, rni
->staid
, status
, tid
,
2437 old_bufsize
, new_bufsize
);
2439 ret
= sc
->addba_response(ni
, tap
, status
, baparamset
, batimeout
);
2441 if (status
== IEEE80211_STATUS_SUCCESS
)
2443 seqno
= ni
->ni_txseqs
[tid
];
2445 rt2870_send_bar(ni
, tap
, seqno
);
2454 static void rt2870_addba_stop(struct ieee80211_node
*ni
,
2455 struct ieee80211_tx_ampdu
*tap
)
2457 struct rt2870_softc
*sc
;
2458 struct ieee80211com
*ic
;
2460 struct rt2870_softc_node
*rni
;
2466 rni
= (struct rt2870_softc_node
*) ni
;
2468 tid
= WME_AC_TO_TID(tap
->txa_ac
);
2470 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2471 "%s: stopping A-MPDU Tx: associd=0x%04x, staid=0x%02x, tid=%d\n",
2472 device_get_nameunit(sc
->dev
), ni
->ni_associd
, rni
->staid
, tid
);
2474 if (tap
->txa_flags
& IEEE80211_AGGR_RUNNING
)
2476 if (sc
->tx_ampdu_sessions
> 0)
2478 sc
->tx_ampdu_sessions
--;
2480 if (sc
->tx_ampdu_sessions
== 0)
2481 rt2870_asic_updateprot(sc
);
2485 printf("%s: number of A-MPDU Tx sessions cannot be negative\n",
2486 device_get_nameunit(sc
->dev
));
2490 sc
->addba_stop(ni
, tap
);
2496 static int rt2870_send_bar(struct ieee80211_node
*ni
,
2497 struct ieee80211_tx_ampdu
*tap
, ieee80211_seq seqno
)
2499 struct rt2870_softc
*sc
;
2500 struct ieee80211com
*ic
;
2502 struct ieee80211_frame_bar
*bar
;
2503 struct rt2870_softc_node
*rni
;
2505 uint16_t barctl
, barseqctl
;
2512 rni
= (struct rt2870_softc_node
*) ni
;
2514 if (!(tap
->txa_flags
& IEEE80211_AGGR_RUNNING
))
2517 m
= ieee80211_getmgtframe(&frm
, ic
->ic_headroom
, sizeof(struct ieee80211_frame_bar
));
2521 bar
= mtod(m
, struct ieee80211_frame_bar
*);
2523 bar
->i_fc
[0] = IEEE80211_FC0_VERSION_0
| IEEE80211_FC0_TYPE_CTL
| IEEE80211_FC0_SUBTYPE_BAR
;
2526 IEEE80211_ADDR_COPY(bar
->i_ra
, ni
->ni_macaddr
);
2527 IEEE80211_ADDR_COPY(bar
->i_ta
, ic
->ic_myaddr
);
2529 tid
= WME_AC_TO_TID(tap
->txa_ac
);
2531 barctl
= (tap
->txa_flags
& IEEE80211_AGGR_IMMEDIATE
? 0 : IEEE80211_BAR_NOACK
) |
2532 IEEE80211_BAR_COMP
|
2533 RT2870_SM(tid
, IEEE80211_BAR_TID
);
2534 barseqctl
= RT2870_SM(seqno
, IEEE80211_BASEQ_START
);
2536 bar
->i_ctl
= htole16(barctl
);
2537 bar
->i_seq
= htole16(barseqctl
);
2539 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ieee80211_frame_bar
);
2540 m
->m_pkthdr
.rcvif
= (void *) ni
;
2542 tap
->txa_start
= seqno
;
2544 ieee80211_ref_node(ni
);
2546 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2547 "%s: sending BAR: associd=0x%04x, staid=0x%02x, tid=%d, seqno=%d\n",
2548 device_get_nameunit(sc
->dev
), ni
->ni_associd
, rni
->staid
, tid
, seqno
);
2550 IF_ENQUEUE(&ic
->ic_mgtq
, m
);
2558 * rt2870_amrr_update_iter_func
2560 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
)
2562 struct rt2870_softc
*sc
;
2563 struct ieee80211com
*ic
;
2564 struct rt2870_softc_node
*rni
;
2569 rni
= (struct rt2870_softc_node
*) ni
;
2571 /* only associated stations */
2573 if (rni
->staid
!= 0)
2577 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2578 "%s: AMRR node: staid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2579 device_get_nameunit(sc
->dev
),
2580 rni
->staid
, sc
->amrr_node
[wcid
].txcnt
, sc
->amrr_node
[wcid
].success
, sc
->amrr_node
[wcid
].retrycnt
);
2582 rt2870_amrr_choose(ni
, &sc
->amrr_node
[wcid
]);
2584 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2585 "%s:%s node Tx rate: associd=0x%04x, staid=0x%02x, rate=0x%02x, max rate=0x%02x\n",
2586 device_get_nameunit(sc
->dev
),
2587 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2588 ni
->ni_associd
, rni
->staid
,
2589 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2590 (ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
] | IEEE80211_RATE_MCS
) :
2591 (ni
->ni_rates
.rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
),
2592 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2593 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2594 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2601 static void rt2870_periodic(void *arg
)
2603 struct rt2870_softc
*sc
;
2609 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
2611 device_get_nameunit(sc
->dev
));
2613 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2616 taskqueue_enqueue(sc
->taskqueue
, &sc
->periodic_task
);
2620 * rt2870_tx_watchdog
2622 static void rt2870_tx_watchdog(void *arg
)
2624 struct rt2870_softc
*sc
;
2630 if (sc
->tx_timer
== 0)
2633 if (--sc
->tx_timer
== 0)
2635 printf("%s: Tx watchdog timeout: resetting\n",
2636 device_get_nameunit(sc
->dev
));
2638 rt2870_stop_locked(sc
);
2639 rt2870_init_locked(sc
);
2643 sc
->tx_watchdog_timeouts
++;
2646 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
2650 * rt2870_staid_alloc
2652 static int rt2870_staid_alloc(struct rt2870_softc
*sc
, int aid
)
2656 if ((aid
> 0 && aid
< RT2870_SOFTC_STAID_COUNT
) && isclr(sc
->staid_mask
, aid
))
2662 for (staid
= 1; staid
< RT2870_SOFTC_STAID_COUNT
; staid
++)
2664 if (isclr(sc
->staid_mask
, staid
))
2669 setbit(sc
->staid_mask
, staid
);
2675 * rt2870_staid_delete
2677 static void rt2870_staid_delete(struct rt2870_softc
*sc
, int staid
)
2679 clrbit(sc
->staid_mask
, staid
);
2685 static int rt2870_do_async(struct rt2870_softc
*sc
,
2686 void (*cb
)(struct rt2870_softc
*sc
, void *arg
),
2690 struct rt2870_softc_cmd_ring
*ring
;
2691 struct rt2870_softc_cmd
*cmd
;
2695 ring
= &sc
->cmd_ring
;
2697 if (!(sc
->flags
& RT2870_SOFTC_FLAGS_VALID
))
2700 RT2870_SOFTC_LOCK(sc
);
2702 if (ring
->queued
>= RT2870_SOFTC_CMD_RING_CMD_COUNT
)
2704 RT2870_SOFTC_UNLOCK(sc
);
2708 cmd
= STAILQ_FIRST(&ring
->inactive
);
2709 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
2714 memcpy(cmd
->data
, arg
, len
);
2716 STAILQ_INSERT_TAIL(&ring
->active
, cmd
, next
);
2719 run_cmd_task
= (ring
->queued
== 1);
2721 RT2870_SOFTC_UNLOCK(sc
);
2724 taskqueue_enqueue(sc
->taskqueue
, &sc
->cmd_task
);
2730 * rt2870_newstate_cb
2732 static void rt2870_newstate_cb(struct rt2870_softc
*sc
, void *arg
)
2734 struct ieee80211com
*ic
;
2735 enum ieee80211_state nstate
;
2736 struct ieee80211_node
*ni
;
2737 struct rt2870_cmd_argv_newstate
*cmd_argv
;
2742 nstate
= cmd_argv
->nstate
;
2744 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2745 "%s: newstate: %s -> %s\n",
2746 device_get_nameunit(sc
->dev
),
2747 ieee80211_state_name
[ic
->ic_state
], ieee80211_state_name
[nstate
]);
2749 error
= sc
->newstate(ic
, nstate
, cmd_argv
->arg
);
2753 /* turn link LED off */
2755 if (nstate
!= IEEE80211_S_RUN
)
2756 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
2760 case IEEE80211_S_INIT
:
2761 rt2870_asic_disable_tsf_sync(sc
);
2764 case IEEE80211_S_RUN
:
2767 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2768 rt2870_rf_set_chan(sc
, ni
->ni_chan
);
2770 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2772 rt2870_asic_enable_mrr(sc
);
2773 rt2870_asic_set_txpreamble(sc
);
2774 rt2870_asic_set_basicrates(sc
);
2775 rt2870_asic_set_bssid(sc
, ni
->ni_bssid
);
2778 if (ic
->ic_opmode
== IEEE80211_M_STA
)
2779 rt2870_newassoc(ni
, 1);
2781 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
||
2782 ic
->ic_opmode
== IEEE80211_M_IBSS
)
2784 error
= rt2870_beacon_alloc(sc
);
2788 rt2870_asic_update_beacon(sc
);
2791 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2792 rt2870_asic_enable_tsf_sync(sc
);
2794 /* turn link LED on */
2796 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2798 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
|
2799 (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
) ?
2800 RT2870_LED_CMD_LINK_2GHZ
: RT2870_LED_CMD_LINK_5GHZ
));
2810 * rt2870_node_cleanup_cb
2812 static void rt2870_node_cleanup_cb(struct rt2870_softc
*sc
, void *arg
)
2814 struct ieee80211com
*ic
;
2815 struct rt2870_cmd_argv_nodecleanup
*cmd_argv
;
2816 uint8_t staid
, vapid
, wcid
;
2821 staid
= cmd_argv
->staid
;
2826 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2827 (RT2870_REG_CIPHER_MODE_NONE
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2829 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2831 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
), 0x00000000);
2832 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, 0x00000000);
2834 rt2870_staid_delete(sc
, staid
);
2838 * rt2870_scan_start_cb
2840 static void rt2870_scan_start_cb(struct rt2870_softc
*sc
, void *arg
)
2842 struct ieee80211com
*ic
;
2848 rt2870_asic_disable_tsf_sync(sc
);
2852 * rt2870_scan_end_cb
2854 static void rt2870_scan_end_cb(struct rt2870_softc
*sc
, void *arg
)
2856 struct ieee80211com
*ic
;
2857 struct ieee80211_node
*ni
;
2862 rt2870_asic_enable_tsf_sync(sc
);
2866 * rt2870_set_channel_cb
2868 static void rt2870_set_channel_cb(struct rt2870_softc
*sc
, void *arg
)
2870 struct ieee80211com
*ic
;
2874 RT2870_DPRINTF(sc
, RT2870_DEBUG_CHAN
,
2875 "%s: set channel: channel=%u, HT%s%s\n",
2876 device_get_nameunit(sc
->dev
),
2877 ieee80211_chan2ieee(ic
, ic
->ic_curchan
),
2878 !IEEE80211_IS_CHAN_HT(ic
->ic_curchan
) ? " disabled" :
2879 IEEE80211_IS_CHAN_HT20(ic
->ic_curchan
) ? "20":
2880 IEEE80211_IS_CHAN_HT40U(ic
->ic_curchan
) ? "40U" : "40D",
2881 (ic
->ic_flags
& IEEE80211_F_SCAN
) ? ", scanning" : "");
2883 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
2887 * rt2870_newassoc_cb
2889 static void rt2870_newassoc_cb(struct rt2870_softc
*sc
, void *arg
)
2891 struct ieee80211com
*ic
;
2892 struct ieee80211_node
*ni
;
2893 struct rt2870_softc_node
*rni
;
2894 struct rt2870_cmd_argv_newassoc
*cmd_argv
;
2896 uint8_t *macaddr
, wcid
;
2902 isnew
= cmd_argv
->isnew
;
2903 macaddr
= cmd_argv
->macaddr
;
2904 ni
= ieee80211_find_node(&ic
->ic_sta
, macaddr
);
2905 rni
= (struct rt2870_softc_node
*) ni
;
2909 aid
= IEEE80211_AID(ni
->ni_associd
);
2910 rni
->staid
= rt2870_staid_alloc(sc
, aid
);
2913 tmp
= (ni
->ni_macaddr
[3] << 24) |
2914 (ni
->ni_macaddr
[2] << 16) |
2915 (ni
->ni_macaddr
[1] << 8) |
2918 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
), tmp
);
2920 tmp
= (ni
->ni_macaddr
[5] << 8) |
2923 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2925 rt2870_amrr_node_init(&sc
->amrr
, &sc
->amrr_node
[wcid
], ni
);
2927 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2928 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2929 device_get_nameunit(sc
->dev
),
2930 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2931 ni
->ni_associd
, ni
->ni_txrate
,
2932 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2933 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2934 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2936 rt2870_asic_updateprot(sc
);
2937 rt2870_asic_updateslot(sc
);
2938 rt2870_asic_set_txpreamble(sc
);
2941 RT2870_DPRINTF(sc
, RT2870_DEBUG_NODE
,
2942 "%s: new association: isnew=%d, macaddr=%s, associd=0x%04x, staid=0x%02x, QoS %s, ERP %s, HT %s\n",
2943 device_get_nameunit(sc
->dev
), isnew
, ether_sprintf(ni
->ni_macaddr
),
2944 ni
->ni_associd
, rni
->staid
,
2945 (ni
->ni_flags
& IEEE80211_NODE_QOS
) ? "enabled" : "disabled",
2946 (ni
->ni_flags
& IEEE80211_NODE_ERP
) ? "enabled" : "disabled",
2947 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? "enabled" : "disabled");
2951 * rt2870_updateslot_cb
2953 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
)
2955 rt2870_asic_updateslot(sc
);
2959 * rt2870_wme_update_cb
2961 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
)
2963 rt2870_asic_wme_update(sc
);
2967 * rt2870_update_beacon_cb
2969 static void rt2870_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
)
2971 struct ieee80211com
*ic
;
2973 struct ieee80211_beacon_offsets
*bo
;
2974 struct rt2870_cmd_argv_updatebeacon
*cmd_argv
;
2977 m
= sc
->beacon_mbuf
;
2978 bo
= &sc
->beacon_offsets
;
2981 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2982 "%s: update beacon: what=%d\n",
2983 device_get_nameunit(sc
->dev
), cmd_argv
->what
);
2985 setbit(bo
->bo_flags
, cmd_argv
->what
);
2987 ieee80211_beacon_update(ic
->ic_bss
, bo
, m
, 0);
2989 rt2870_asic_update_beacon(sc
);
2993 * rt2870_key_update_begin_cb
2995 static void rt2870_key_update_begin_cb(struct rt2870_softc
*sc
, void *arg
)
3001 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
3002 "%s: key update begin\n",
3003 device_get_nameunit(sc
->dev
));
3005 taskqueue_block(sc
->taskqueue
);
3007 IF_LOCK(&ifp
->if_snd
);
3011 * rt2870_key_update_end_cb
3013 static void rt2870_key_update_end_cb(struct rt2870_softc
*sc
, void *arg
)
3019 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
3020 "%s: key update end\n",
3021 device_get_nameunit(sc
->dev
));
3023 IF_UNLOCK(&ifp
->if_snd
);
3025 taskqueue_unblock(sc
->taskqueue
);
3031 static void rt2870_key_set_cb(struct rt2870_softc
*sc
, void *arg
)
3033 struct ieee80211com
*ic
;
3034 struct ieee80211_key
*k
;
3035 enum ieee80211_opmode opmode
;
3036 struct rt2870_cmd_argv_keyset
*cmd_argv
;
3037 uint16_t key_base
, keymode_base
;
3038 uint8_t mode
, vapid
, wcid
, iv
[8];
3044 opmode
= cmd_argv
->opmode
;
3046 switch (k
->wk_cipher
->ic_cipher
)
3048 case IEEE80211_CIPHER_WEP
:
3049 if(k
->wk_keylen
< 8)
3050 mode
= RT2870_REG_CIPHER_MODE_WEP40
;
3052 mode
= RT2870_REG_CIPHER_MODE_WEP104
;
3055 case IEEE80211_CIPHER_TKIP
:
3056 mode
= RT2870_REG_CIPHER_MODE_TKIP
;
3059 case IEEE80211_CIPHER_AES_CCM
:
3060 mode
= RT2870_REG_CIPHER_MODE_AES_CCMP
;
3067 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
3068 "%s: key set: keyix=%d, keylen=%d, mode=%d, group=%d\n",
3069 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
,
3070 mode
, (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
3072 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
3074 /* install pairwise key */
3077 wcid
= cmd_argv
->staid
;
3078 key_base
= RT2870_REG_PKEY(wcid
);
3080 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
)
3084 iv
[3] = (k
->wk_keyix
<< 6);
3088 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
3090 iv
[0] = (k
->wk_keytsc
>> 8);
3091 iv
[1] = ((iv
[0] | 0x20) & 0x7f);
3092 iv
[2] = k
->wk_keytsc
;
3098 iv
[0] = k
->wk_keytsc
;
3099 iv
[1] = k
->wk_keytsc
>> 8;
3103 iv
[3] = ((k
->wk_keyix
<< 6) | IEEE80211_WEP_EXTIV
);
3104 iv
[4] = (k
->wk_keytsc
>> 16);
3105 iv
[5] = (k
->wk_keytsc
>> 24);
3106 iv
[6] = (k
->wk_keytsc
>> 32);
3107 iv
[7] = (k
->wk_keytsc
>> 40);
3109 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
3110 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
3111 device_get_nameunit(sc
->dev
),
3112 iv
[0], iv
[1], iv
[2], iv
[3], iv
[4], iv
[5], iv
[6], iv
[7]);
3115 rt2870_io_mac_write_multi(sc
, RT2870_REG_IVEIV(wcid
), iv
, 8);
3117 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
3119 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
3121 if (opmode
!= IEEE80211_M_HOSTAP
)
3123 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
3124 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
3128 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
3129 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
3134 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
3137 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
3138 (mode
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
3140 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
3143 if ((k
->wk_flags
& IEEE80211_KEY_GROUP
) ||
3144 (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
))
3146 /* install group key */
3149 wcid
= RT2870_WCID_MCAST
;
3150 key_base
= RT2870_REG_SKEY(vapid
, k
->wk_keyix
);
3151 keymode_base
= RT2870_REG_SKEY_MODE(vapid
);
3153 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
3155 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
3157 if (opmode
!= IEEE80211_M_HOSTAP
)
3159 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
3160 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
3164 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
3165 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
3170 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
3173 tmp
= rt2870_io_mac_read(sc
, keymode_base
);
3175 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
3176 tmp
|= (mode
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
3178 rt2870_io_mac_write(sc
, keymode_base
, tmp
);
3180 if (opmode
== IEEE80211_M_HOSTAP
)
3182 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
)
3186 iv
[3] = (k
->wk_keyix
<< 6);
3190 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
3192 iv
[0] = (k
->wk_keytsc
>> 8);
3193 iv
[1] = ((iv
[0] | 0x20) & 0x7f);
3194 iv
[2] = k
->wk_keytsc
;
3200 iv
[0] = k
->wk_keytsc
;
3201 iv
[1] = k
->wk_keytsc
>> 8;
3205 iv
[3] = ((k
->wk_keyix
<< 6) | IEEE80211_WEP_EXTIV
);
3206 iv
[4] = (k
->wk_keytsc
>> 16);
3207 iv
[5] = (k
->wk_keytsc
>> 24);
3208 iv
[6] = (k
->wk_keytsc
>> 32);
3209 iv
[7] = (k
->wk_keytsc
>> 40);
3211 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
3212 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
3213 device_get_nameunit(sc
->dev
),
3214 iv
[0], iv
[1], iv
[2], iv
[3], iv
[4], iv
[5], iv
[6], iv
[7]);
3217 rt2870_io_mac_write_multi(sc
, RT2870_REG_IVEIV(wcid
), iv
, 8);
3219 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
3220 (mode
<< RT2870_REG_CIPHER_MODE_SHIFT
);
3222 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
3228 * rt2870_key_delete_cb
3230 static void rt2870_key_delete_cb(struct rt2870_softc
*sc
, void *arg
)
3232 struct ieee80211com
*ic
;
3233 struct ieee80211_key
*k
;
3234 enum ieee80211_opmode opmode
;
3235 struct rt2870_cmd_argv_keydelete
*cmd_argv
;
3236 uint8_t vapid
, wcid
;
3242 opmode
= cmd_argv
->opmode
;
3244 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
3245 "%s: key delete: keyix=%d, keylen=%d, group=%d\n",
3246 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
,
3247 (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
3249 if (k
->wk_flags
& IEEE80211_KEY_GROUP
)
3251 /* remove group key */
3254 wcid
= RT2870_WCID_MCAST
;
3256 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SKEY_MODE(vapid
));
3258 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
3259 tmp
|= (RT2870_REG_CIPHER_MODE_NONE
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
3261 rt2870_io_mac_write(sc
, RT2870_REG_SKEY_MODE(vapid
), tmp
);
3263 if (opmode
== IEEE80211_M_HOSTAP
)
3265 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
3266 (RT2870_REG_CIPHER_MODE_NONE
<< RT2870_REG_CIPHER_MODE_SHIFT
);
3268 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
3274 * rt2870_asic_set_bssid
3276 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
3277 const uint8_t *bssid
)
3281 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3282 "%s: set bssid: bssid=%s\n",
3283 device_get_nameunit(sc
->dev
), ether_sprintf(bssid
));
3285 tmp
= bssid
[0] | (bssid
[1]) << 8 | (bssid
[2] << 16) | (bssid
[3] << 24);
3287 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW0
, tmp
);
3289 tmp
= bssid
[4] | (bssid
[5] << 8);
3291 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW1
, tmp
);
3295 * rt2870_asic_set_macaddr
3297 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
3298 const uint8_t *addr
)
3302 tmp
= addr
[0] | (addr
[1] << 8) | (addr
[2] << 16) | (addr
[3] << 24);
3304 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW0
, tmp
);
3306 tmp
= addr
[4] | (addr
[5] << 8);
3308 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW1
, tmp
);
3312 * rt2870_asic_enable_tsf_sync
3314 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
)
3316 struct ieee80211com
*ic
;
3321 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
3322 "%s: enabling TSF\n",
3323 device_get_nameunit(sc
->dev
));
3325 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3328 tmp
|= ic
->ic_bss
->ni_intval
* 16;
3329 tmp
|= (RT2870_REG_TSF_TIMER_ENABLE
| RT2870_REG_TBTT_TIMER_ENABLE
);
3331 if (ic
->ic_opmode
== IEEE80211_M_STA
)
3333 tmp
|= (RT2870_REG_TSF_SYNC_MODE_STA
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3335 else if (ic
->ic_opmode
== IEEE80211_M_IBSS
)
3337 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
3338 tmp
|= (RT2870_REG_TSF_SYNC_MODE_IBSS
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3340 else if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
)
3342 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
3343 tmp
|= (RT2870_REG_TSF_SYNC_MODE_HOSTAP
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3346 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3350 * rt2870_asic_disable_tsf_sync
3352 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
)
3356 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
3357 "%s: disabling TSF\n",
3358 device_get_nameunit(sc
->dev
));
3360 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3362 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
3363 RT2870_REG_TSF_TIMER_ENABLE
|
3364 RT2870_REG_TBTT_TIMER_ENABLE
);
3366 tmp
&= ~(RT2870_REG_TSF_SYNC_MODE_MASK
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3367 tmp
|= (RT2870_REG_TSF_SYNC_MODE_DISABLE
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3369 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3373 * rt2870_asic_enable_mrr
3375 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
)
3377 #define CCK(mcs) (mcs)
3378 #define OFDM(mcs) ((1 << 3) | (mcs))
3379 #define HT(mcs) (mcs)
3381 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG0
,
3382 (OFDM(6) << 28) | /* 54 -> 48 */
3383 (OFDM(5) << 24) | /* 48 -> 36 */
3384 (OFDM(4) << 20) | /* 36 -> 24 */
3385 (OFDM(3) << 16) | /* 24 -> 18 */
3386 (OFDM(2) << 12) | /* 18 -> 12 */
3387 (OFDM(1) << 8) | /* 12 -> 9 */
3388 (OFDM(0) << 4) | /* 9 -> 6 */
3389 OFDM(0)); /* 6 -> 6 */
3391 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG1
,
3392 (CCK(2) << 12) | /* 11 -> 5.5 */
3393 (CCK(1) << 8) | /* 5.5 -> 2 */
3394 (CCK(0) << 4) | /* 2 -> 1 */
3395 CCK(0)); /* 1 -> 1 */
3397 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG0
,
3407 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG1
,
3423 * rt2870_asic_set_txpreamble
3425 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
)
3427 struct ieee80211com
*ic
;
3432 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3433 "%s: %s short Tx preamble\n",
3434 device_get_nameunit(sc
->dev
),
3435 (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) ? "enabling" : "disabling");
3437 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_AUTO_RSP_CFG
);
3439 tmp
&= ~RT2870_REG_CCK_SHORT_ENABLE
;
3441 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
3442 tmp
|= RT2870_REG_CCK_SHORT_ENABLE
;
3444 rt2870_io_mac_write(sc
, RT2870_REG_AUTO_RSP_CFG
, tmp
);
3448 * rt2870_asic_set_basicrates
3450 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
)
3452 struct ieee80211com
*ic
;
3456 if (ic
->ic_curmode
== IEEE80211_MODE_11B
)
3457 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0xf);
3458 else if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3459 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x150);
3461 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x15f);
3465 * rt2870_asic_update_rtsthreshold
3467 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
)
3469 struct ieee80211com
*ic
;
3475 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3476 "%s: updating RTS threshold: %d\n",
3477 device_get_nameunit(sc
->dev
), ic
->ic_rtsthreshold
);
3479 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_RTS_CFG
);
3481 tmp
&= ~(RT2870_REG_TX_RTS_THRESHOLD_MASK
<< RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3483 if (ic
->ic_flags_ext
& IEEE80211_FEXT_AMSDU_TX
)
3486 threshold
= ic
->ic_rtsthreshold
;
3488 tmp
|= ((threshold
& RT2870_REG_TX_RTS_THRESHOLD_MASK
) <<
3489 RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3491 rt2870_io_mac_write(sc
, RT2870_REG_TX_RTS_CFG
, tmp
);
3495 * rt2870_asic_update_txpower
3497 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
)
3499 struct ieee80211com
*ic
;
3500 uint32_t *txpow_rate
;
3508 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3509 "%s: updating Tx power: %d\n",
3510 device_get_nameunit(sc
->dev
), ic
->ic_txpowlimit
);
3512 if (!IEEE80211_IS_CHAN_HT40(ic
->ic_curchan
))
3514 txpow_rate
= sc
->txpow_rate_20mhz
;
3518 if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_curchan
))
3519 txpow_rate
= sc
->txpow_rate_40mhz_2ghz
;
3521 txpow_rate
= sc
->txpow_rate_40mhz_5ghz
;
3526 val
= rt2870_io_bbp_read(sc
, 1);
3529 if (ic
->ic_txpowlimit
> 90)
3533 else if (ic
->ic_txpowlimit
> 60)
3537 else if (ic
->ic_txpowlimit
> 30)
3541 else if (ic
->ic_txpowlimit
> 15)
3545 else if (ic
->ic_txpowlimit
> 9)
3555 rt2870_io_bbp_write(sc
, 1, val
);
3557 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
3559 if (txpow_rate
[i
] == 0xffffffff)
3562 tmp
= rt2870_read_eeprom_txpow_rate_add_delta(txpow_rate
[i
], delta
);
3564 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
), tmp
);
3569 * rt2870_asic_update_promisc
3571 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
)
3576 ifp
= sc
->ic
.ic_ifp
;
3578 printf("%s: %s promiscuous mode\n",
3579 device_get_nameunit(sc
->dev
),
3580 (ifp
->if_flags
& IFF_PROMISC
) ? "entering" : "leaving");
3582 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_FILTER_CFG
);
3584 tmp
&= ~RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3586 if (!(ifp
->if_flags
& IFF_PROMISC
))
3587 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3589 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
3593 * rt2870_asic_updateprot
3595 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
)
3597 struct ieee80211com
*ic
;
3598 uint32_t cck_prot
, ofdm_prot
, mm20_prot
, mm40_prot
, gf20_prot
, gf40_prot
;
3600 enum ieee80211_protmode htprotmode
;
3604 if (!(sc
->flags
& RT2870_SOFTC_FLAGS_VALID
))
3607 /* CCK frame protection */
3609 cck_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3610 RT2870_REG_TXOP_ALLOW_ALL
| RT2870_REG_PROT_CTRL_NONE
;
3612 /* set up protection frame phy mode and rate (MCS code) */
3614 if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3615 cck_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3616 (0 << RT2870_REG_PROT_MCS_SHIFT
);
3618 cck_prot
|= ((RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3619 (3 << RT2870_REG_PROT_MCS_SHIFT
));
3621 rt2870_io_mac_write(sc
, RT2870_REG_TX_CCK_PROT_CFG
, cck_prot
);
3623 /* OFDM frame protection */
3625 ofdm_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3626 RT2870_REG_TXOP_ALLOW_ALL
;
3628 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3630 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3631 "%s: updating protection mode: b/g protection mode=%s\n",
3632 device_get_nameunit(sc
->dev
),
3633 (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3634 ((ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3636 if (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
)
3637 ofdm_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3638 else if (ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
)
3639 ofdm_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3641 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3645 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3646 "%s: updating protection mode: b/g protection mode=%s\n",
3647 device_get_nameunit(sc
->dev
), "none");
3649 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3652 rt2870_io_mac_write(sc
, RT2870_REG_TX_OFDM_PROT_CFG
, ofdm_prot
);
3654 /* HT frame protection */
3656 if ((ic
->ic_opmode
== IEEE80211_M_STA
) && (ic
->ic_state
== IEEE80211_S_RUN
))
3657 htopmode
= ic
->ic_bss
->ni_htopmode
;
3659 htopmode
= ic
->ic_curhtprotmode
;
3661 htprotmode
= ic
->ic_htprotmode
;
3663 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3665 if (sc
->tx_ampdu_sessions
> 0)
3667 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3668 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3669 device_get_nameunit(sc
->dev
));
3671 htopmode
= IEEE80211_HTINFO_OPMODE_MIXED
;
3672 htprotmode
= IEEE80211_PROT_RTSCTS
;
3675 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3676 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3677 device_get_nameunit(sc
->dev
),
3678 htopmode
& IEEE80211_HTINFO_OPMODE
,
3679 (htprotmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3680 ((htprotmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3682 switch (htopmode
& IEEE80211_HTINFO_OPMODE
)
3684 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3685 case IEEE80211_HTINFO_OPMODE_HT20PR
:
3686 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3687 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3688 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3689 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3690 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3692 gf20_prot
= mm20_prot
;
3694 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3695 RT2870_REG_TXOP_ALLOW_ALL
|
3696 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3697 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3699 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3700 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3701 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3702 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3704 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3706 gf40_prot
= mm40_prot
;
3709 /* IEEE80211_HTINFO_OPMODE_MIXED */
3710 case IEEE80211_HTINFO_OPMODE_MIXED
:
3711 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
|
3712 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3713 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
;
3715 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3716 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3717 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3719 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3720 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3722 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3723 mm20_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3724 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3725 mm20_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3727 mm20_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3729 gf20_prot
= mm20_prot
;
3731 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_TXOP_ALLOW_ALL
;
3733 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3734 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3735 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3737 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3738 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3740 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3741 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3742 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3743 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3745 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3747 gf40_prot
= mm40_prot
;
3751 * IEEE80211_HTINFO_OPMODE_PURE
3752 * IEEE80211_HTINFO_OPMODE_PROTOPT
3754 case IEEE80211_HTINFO_OPMODE_PURE
:
3755 case IEEE80211_HTINFO_OPMODE_PROTOPT
:
3757 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3758 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3759 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3760 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3761 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3763 gf20_prot
= mm20_prot
;
3765 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3766 RT2870_REG_TXOP_ALLOW_ALL
|
3767 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3768 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3770 gf40_prot
= mm40_prot
;
3774 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM20_PROT_CFG
, mm20_prot
);
3775 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM40_PROT_CFG
, mm40_prot
);
3776 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF20_PROT_CFG
, gf20_prot
);
3777 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF40_PROT_CFG
, gf40_prot
);
3781 * rt2870_asic_updateslot
3783 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
)
3785 struct ieee80211com
*ic
;
3790 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3791 "%s: %s short slot time\n",
3792 device_get_nameunit(sc
->dev
),
3793 (ic
->ic_flags
& (IEEE80211_F_SHSLOT
| IEEE80211_F_BURST
)) ? "enabling" : "disabling");
3795 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BKOFF_SLOT_CFG
);
3798 tmp
|= (ic
->ic_flags
& (IEEE80211_F_SHSLOT
| IEEE80211_F_BURST
)) ? 9 : 20;
3800 rt2870_io_mac_write(sc
, RT2870_REG_BKOFF_SLOT_CFG
, tmp
);
3804 * rt2870_asic_wme_update
3806 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
)
3808 struct ieee80211com
*ic
;
3809 struct ieee80211_wme_state
*wme
;
3810 const struct wmeParams
*wmep
;
3815 wmep
= wme
->wme_chanParams
.cap_wmeParams
;
3817 RT2870_DPRINTF(sc
, RT2870_DEBUG_WME
,
3818 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3819 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3820 device_get_nameunit(sc
->dev
),
3821 wmep
[WME_AC_VO
].wmep_aifsn
,
3822 wmep
[WME_AC_VO
].wmep_logcwmin
, wmep
[WME_AC_VO
].wmep_logcwmax
,
3823 wmep
[WME_AC_VO
].wmep_txopLimit
,
3824 wmep
[WME_AC_VI
].wmep_aifsn
,
3825 wmep
[WME_AC_VI
].wmep_logcwmin
, wmep
[WME_AC_VI
].wmep_logcwmax
,
3826 wmep
[WME_AC_VI
].wmep_txopLimit
,
3827 wmep
[WME_AC_BK
].wmep_aifsn
,
3828 wmep
[WME_AC_BK
].wmep_logcwmin
, wmep
[WME_AC_BK
].wmep_logcwmax
,
3829 wmep
[WME_AC_BK
].wmep_txopLimit
,
3830 wmep
[WME_AC_BE
].wmep_aifsn
,
3831 wmep
[WME_AC_BE
].wmep_logcwmin
, wmep
[WME_AC_BE
].wmep_logcwmax
,
3832 wmep
[WME_AC_BE
].wmep_txopLimit
);
3834 for (i
= 0; i
< WME_NUM_AC
; i
++)
3835 rt2870_io_mac_write(sc
, RT2870_REG_TX_EDCA_AC_CFG(i
),
3836 (wmep
[i
].wmep_logcwmax
<< 16) | (wmep
[i
].wmep_logcwmin
<< 12) |
3837 (wmep
[i
].wmep_aifsn
<< 8) | wmep
[i
].wmep_txopLimit
);
3839 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_AIFSN_CFG
,
3840 (wmep
[WME_AC_VO
].wmep_aifsn
<< 12) | (wmep
[WME_AC_VI
].wmep_aifsn
<< 8) |
3841 (wmep
[WME_AC_BK
].wmep_aifsn
<< 4) | wmep
[WME_AC_BE
].wmep_aifsn
);
3843 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMIN_CFG
,
3844 (wmep
[WME_AC_VO
].wmep_logcwmin
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmin
<< 8) |
3845 (wmep
[WME_AC_BK
].wmep_logcwmin
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmin
);
3847 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMAX_CFG
,
3848 (wmep
[WME_AC_VO
].wmep_logcwmax
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmax
<< 8) |
3849 (wmep
[WME_AC_BK
].wmep_logcwmax
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmax
);
3851 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
,
3852 (wmep
[WME_AC_BK
].wmep_txopLimit
<< 16) | wmep
[WME_AC_BE
].wmep_txopLimit
);
3854 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
3855 (wmep
[WME_AC_VO
].wmep_txopLimit
<< 16) | wmep
[WME_AC_VI
].wmep_txopLimit
);
3859 * rt2870_asic_update_beacon
3861 static void rt2870_asic_update_beacon(struct rt2870_softc
*sc
)
3863 struct ieee80211com
*ic
;
3865 struct rt2870_txwi
*txwi
;
3870 m
= sc
->beacon_mbuf
;
3871 txwi
= &sc
->beacon_txwi
;
3873 /* disable temporarily TSF sync */
3875 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3877 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
3878 RT2870_REG_TSF_TIMER_ENABLE
|
3879 RT2870_REG_TBTT_TIMER_ENABLE
);
3881 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3883 /* write Tx wireless info and beacon frame to on-chip memory */
3885 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0),
3886 txwi
, sizeof(struct rt2870_txwi
));
3888 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0) + sizeof(struct rt2870_txwi
),
3889 mtod(m
, uint8_t *), m
->m_pkthdr
.len
);
3891 /* enable again TSF sync */
3893 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3895 tmp
|= (RT2870_REG_BCN_TX_ENABLE
|
3896 RT2870_REG_TSF_TIMER_ENABLE
|
3897 RT2870_REG_TBTT_TIMER_ENABLE
);
3899 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3903 * rt2870_asic_clear_keytables
3905 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
)
3909 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3911 for (i
= 0; i
< 256; i
++)
3913 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
), 0xffffffff);
3914 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
) + 4, 0x0000ffff);
3917 /* clear WCID attribute table (entries = 256, entry size = 4) */
3919 rt2870_io_mac_set_region_4(sc
, RT2870_REG_WCID_ATTR(0), 0, 256);
3921 /* clear IV/EIV table (entries = 256, entry size = 8) */
3923 rt2870_io_mac_set_region_4(sc
, RT2870_REG_IVEIV(0), 0, 2 * 256);
3925 /* clear pairwise key table (entries = 256, entry size = 32) */
3927 rt2870_io_mac_set_region_4(sc
, RT2870_REG_PKEY(0), 0, 8 * 256);
3929 /* clear shared key table (entries = 32, entry size = 32) */
3931 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY(0, 0), 0, 8 * 32);
3933 /* clear shared key mode (entries = 32, entry size = 2) */
3935 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY_MODE(0), 0, 16);
3939 * rt2870_asic_add_ba_session
3941 static void rt2870_asic_add_ba_session(struct rt2870_softc
*sc
,
3942 uint8_t wcid
, int tid
)
3946 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
3948 tmp
|= (0x10000 << tid
);
3950 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
3954 * rt2860_asic_del_ba_session
3956 static void rt2870_asic_del_ba_session(struct rt2870_softc
*sc
,
3957 uint8_t wcid
, int tid
)
3961 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
3963 tmp
&= ~(0x10000 << tid
);
3965 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
3969 * rt2870_beacon_alloc
3971 static int rt2870_beacon_alloc(struct rt2870_softc
*sc
)
3973 struct ieee80211com
*ic
;
3975 struct rt2870_txwi txwi
;
3980 m
= ieee80211_beacon_alloc(ic
->ic_bss
, &sc
->beacon_offsets
);
3984 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
3985 mcs
= rt2870_rate2mcs(rate
);
3987 memset(&txwi
, 0, sizeof(struct rt2870_txwi
));
3990 txwi
.pid_mpdu_len
= ((htole16(m
->m_pkthdr
.len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
3991 RT2870_TXWI_MPDU_LEN_SHIFT
);
3992 txwi
.txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
3993 txwi
.mpdu_density_flags
|=
3994 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
3995 txwi
.bawin_size_xflags
|=
3996 (RT2870_TXWI_XFLAGS_NSEQ
<< RT2870_TXWI_XFLAGS_SHIFT
);
4000 txwi
.phymode_ifs_stbc_shortgi
=
4001 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
4003 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
4004 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
4008 txwi
.phymode_ifs_stbc_shortgi
=
4009 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
4012 txwi
.bw_mcs
= (RT2870_TXWI_BW_20
<< RT2870_TXWI_BW_SHIFT
) |
4013 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
4015 if (sc
->beacon_mbuf
!= NULL
)
4017 m_free(sc
->beacon_mbuf
);
4018 sc
->beacon_mbuf
= NULL
;
4021 sc
->beacon_mbuf
= m
;
4022 sc
->beacon_txwi
= txwi
;
4030 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
)
4032 uint8_t mcs
, phymode
;
4035 mcs
= (rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
;
4036 phymode
= (rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
4037 RT2870_RXWI_PHYMODE_MASK
;
4043 case RT2870_RXWI_PHYMODE_CCK
:
4044 switch (mcs
& ~RT2870_RXWI_MCS_SHOTPRE
)
4046 case 0: rate
= 2; break; /* 1 Mbps */
4047 case 1: rate
= 4; break; /* 2 MBps */
4048 case 2: rate
= 11; break; /* 5.5 Mbps */
4049 case 3: rate
= 22; break; /* 11 Mbps */
4053 case RT2870_RXWI_PHYMODE_OFDM
:
4056 case 0: rate
= 12; break; /* 6 Mbps */
4057 case 1: rate
= 18; break; /* 9 Mbps */
4058 case 2: rate
= 24; break; /* 12 Mbps */
4059 case 3: rate
= 36; break; /* 18 Mbps */
4060 case 4: rate
= 48; break; /* 24 Mbps */
4061 case 5: rate
= 72; break; /* 36 Mbps */
4062 case 6: rate
= 96; break; /* 48 Mbps */
4063 case 7: rate
= 108; break; /* 54 Mbps */
4067 case RT2870_RXWI_PHYMODE_HT_MIXED
:
4068 case RT2870_RXWI_PHYMODE_HT_GF
:
4076 * rt2870_maxrssi_rxpath
4078 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
4079 const struct rt2870_rxwi
*rxwi
)
4085 if (sc
->nrxpath
> 1)
4086 if (rxwi
->rssi
[1] > rxwi
->rssi
[rxpath
])
4089 if (sc
->nrxpath
> 2)
4090 if (rxwi
->rssi
[2] > rxwi
->rssi
[rxpath
])
4099 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
4100 uint8_t rssi
, uint8_t rxpath
)
4102 struct ieee80211com
*ic
;
4103 struct ieee80211_channel
*c
;
4105 int8_t rssi_off
, lna_gain
;
4112 chan
= ieee80211_chan2ieee(ic
, c
);
4114 if (IEEE80211_IS_CHAN_5GHZ(c
))
4116 rssi_off
= sc
->rssi_off_5ghz
[rxpath
];
4119 lna_gain
= sc
->lna_gain
[1];
4120 else if (chan
<= 128)
4121 lna_gain
= sc
->lna_gain
[2];
4123 lna_gain
= sc
->lna_gain
[3];
4127 rssi_off
= sc
->rssi_off_2ghz
[rxpath
];
4128 lna_gain
= sc
->lna_gain
[0];
4131 return (-12 - rssi_off
- lna_gain
- rssi
);
4137 static uint8_t rt2870_rate2mcs(uint8_t rate
)
4164 static int rt2870_ackrate(struct ieee80211com
*ic
, int rate
)
4176 return (ic
->ic_curmode
== IEEE80211_MODE_11B
) ? 4 : rate
;
4195 /* default to 1Mbps */
4202 static uint16_t rt2870_txtime(int len
, int rate
, uint32_t flags
)
4206 if (RT2870_RATE_IS_OFDM(rate
))
4208 txtime
= (8 + 4 * len
+ 3 + rate
- 1) / rate
;
4209 txtime
= 16 + 4 + 4 * txtime
+ 6;
4213 txtime
= (16 * len
+ rate
- 1) / rate
;
4215 if (rate
!= 2 && (flags
& IEEE80211_F_SHPREAMBLE
))
4227 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
4228 uint8_t *buf
, uint32_t dmalen
)
4230 struct ieee80211com
*ic
;
4232 struct ieee80211_frame
*wh
;
4233 struct ieee80211_node
*ni
;
4234 struct rt2870_softc_node
*rni
;
4235 struct rt2870_softc_rx_radiotap_header
*tap
;
4236 struct rt2870_rxwi
*rxwi
;
4237 struct rt2870_rxinfo
*rxinfo
;
4239 uint32_t rxinfo_flags
;
4240 uint8_t cipher_err
, rssi
, ant
, phymode
, bw
, shortgi
, mcs
, keyidx
;
4242 int len
, ampdu
, amsdu
, i
;
4247 /* get Rx wireless info */
4249 rxwi
= (struct rt2870_rxwi
*) buf
;
4250 len
= (le16toh(rxwi
->tid_size
) >> RT2870_RXWI_SIZE_SHIFT
) &
4251 RT2870_RXWI_SIZE_MASK
;
4255 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4256 "%s: bad rxwi len: DMA len=%d, rxwi len=%d\n",
4257 device_get_nameunit(sc
->dev
), dmalen
, len
);
4263 rxinfo
= (struct rt2870_rxinfo
*) ((caddr_t
) rxwi
+ dmalen
);
4264 rxinfo_flags
= le32toh(rxinfo
->flags
);
4266 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4267 "%s: Rx frame: DMA len=%d, len=%d, rxinfo flags=0x%08x\n",
4268 device_get_nameunit(sc
->dev
), dmalen
, len
, rxinfo_flags
);
4270 /* check for crc errors */
4272 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_CRC_ERR
)
4274 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4275 "%s: rxinfo: crc error\n",
4276 device_get_nameunit(sc
->dev
));
4280 if (!(ifp
->if_flags
& IFF_PROMISC
))
4284 wh
= (struct ieee80211_frame
*) (rxwi
+ 1);
4286 /* check for L2 padding between IEEE 802.11 frame header and body */
4288 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_L2PAD
)
4290 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4291 "%s: L2 padding: DMA len=%d, len=%d\n",
4292 device_get_nameunit(sc
->dev
), dmalen
, len
);
4297 m
= m_getjcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
, MJUMPAGESIZE
);
4300 sc
->rx_mbuf_alloc_errors
++;
4305 m
->m_pkthdr
.rcvif
= ifp
;
4306 m
->m_pkthdr
.len
= m
->m_len
= len
;
4308 m_copyback(m
, 0, len
, (caddr_t
) wh
);
4310 wh
= mtod(m
, struct ieee80211_frame
*);
4312 /* check for cipher errors */
4314 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_DECRYPTED
)
4316 cipher_err
= ((rxinfo_flags
>> RT2870_RXINFO_FLAGS_CIPHER_ERR_SHIFT
) &
4317 RT2870_RXINFO_FLAGS_CIPHER_ERR_MASK
);
4318 if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_NONE
)
4320 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4321 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4323 m
->m_flags
|= M_WEP
;
4325 sc
->rx_cipher_no_errors
++;
4329 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4330 "%s: rxinfo: cipher error=0x%02x\n",
4331 device_get_nameunit(sc
->dev
), cipher_err
);
4333 if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_ICV
)
4334 sc
->rx_cipher_icv_errors
++;
4335 else if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_MIC
)
4336 sc
->rx_cipher_mic_errors
++;
4337 else if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_INVALID_KEY
)
4338 sc
->rx_cipher_invalid_key_errors
++;
4340 if ((cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_MIC
) &&
4341 (rxinfo_flags
& RT2870_RXINFO_FLAGS_MYBSS
))
4343 keyidx
= (rxwi
->udf_bssidx_keyidx
>> RT2870_RXWI_KEYIDX_SHIFT
) &
4344 RT2870_RXWI_KEYIDX_MASK
;
4346 ieee80211_notify_michael_failure(ic
, wh
, keyidx
);
4351 if (!(ifp
->if_flags
& IFF_PROMISC
))
4360 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4362 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4363 "%s: rxinfo: not decrypted but protected flag set\n",
4364 device_get_nameunit(sc
->dev
));
4368 if (!(ifp
->if_flags
& IFF_PROMISC
))
4376 /* check for A-MPDU */
4378 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMPDU
)
4380 m
->m_flags
|= M_AMPDU
;
4391 /* check for A-MSDU */
4393 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMSDU
)
4404 ant
= rt2870_maxrssi_rxpath(sc
, rxwi
);
4405 rssi
= rxwi
->rssi
[ant
];
4406 rssi_dbm
= rt2870_rssi2dbm(sc
, rssi
, ant
);
4407 phymode
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
4408 RT2870_RXWI_PHYMODE_MASK
);
4409 bw
= ((rxwi
->bw_mcs
>> RT2870_RXWI_BW_SHIFT
) & RT2870_RXWI_BW_MASK
);
4410 shortgi
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_SHORTGI_SHIFT
) &
4411 RT2870_RXWI_SHORTGI_MASK
);
4412 mcs
= ((rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
);
4414 if (bpf_peers_present(sc
->drvbpf
))
4418 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
4419 tap
->dbm_antsignal
= rssi_dbm
;
4420 tap
->dbm_antnoise
= RT2870_NOISE_FLOOR
;
4422 tap
->antsignal
= rssi
;
4423 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4424 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4425 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4426 tap
->chan_maxpow
= 0;
4428 if (phymode
== RT2870_RXWI_PHYMODE_CCK
|| phymode
== RT2870_RXWI_PHYMODE_OFDM
)
4429 tap
->rate
= rt2870_rxrate(rxwi
);
4431 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4433 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_CRC_ERR
)
4434 tap
->flags
|= IEEE80211_RADIOTAP_F_BADFCS
;
4436 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_FRAG
)
4437 tap
->flags
|= IEEE80211_RADIOTAP_F_FRAG
;
4439 if (rxwi
->bw_mcs
& RT2870_RXWI_MCS_SHOTPRE
)
4440 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4443 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4445 bpf_mtap2(sc
->drvbpf
, tap
, sc
->rxtap_len
, m
);
4448 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4449 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4450 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
4451 device_get_nameunit(sc
->dev
),
4452 len
, phymode
, bw
, shortgi
, mcs
,
4453 ant
, rxwi
->rssi
[0], rxwi
->rssi
[1], rxwi
->rssi
[2],
4454 rxwi
->snr
[0], rxwi
->snr
[1],
4455 rxwi
->wcid
, ampdu
, amsdu
);
4457 ni
= ieee80211_find_rxnode(ic
, (struct ieee80211_frame_min
*) wh
);
4461 rni
= (struct rt2870_softc_node
*) ni
;
4463 for (i
= 0; i
< RT2870_SOFTC_RSSI_DBM_COUNT
; i
++)
4464 rni
->last_rssi_dbm
[i
] = rt2870_rssi2dbm(sc
, rxwi
->rssi
[i
], i
);
4467 ieee80211_input(ic
, m
, ni
, rssi_dbm
, RT2870_NOISE_FLOOR
, 0);
4469 ieee80211_free_node(ni
);
4475 static int rt2870_tx_frame(struct rt2870_softc
*sc
,
4476 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
)
4478 struct ieee80211com
*ic
;
4479 struct rt2870_softc_node
*rni
;
4480 struct rt2870_softc_tx_ring
*ring
;
4481 struct rt2870_softc_tx_data
*data
;
4482 struct rt2870_txinfo
*txinfo
;
4483 struct rt2870_txwi
*txwi
;
4484 struct ieee80211_frame
*wh
;
4485 struct ieee80211_tx_ampdu
*tx_ampdu
;
4486 struct rt2870_softc_tx_radiotap_header
*tap
;
4487 u_int hdrsize
, hdrspace
;
4488 uint8_t type
, rate
, bw
, stbc
, shortgi
, mcs
, pid
, wcid
, mpdu_density
, bawin_size
;
4489 uint16_t qos
, len
, dmalen
, mpdu_len
, dur
;
4490 int hasqos
, ac
, ampdu
, mimops
, ackrate
;
4493 rni
= (struct rt2870_softc_node
*) ni
;
4495 ring
= &sc
->tx_ring
[qid
];
4496 data
= STAILQ_FIRST(&ring
->inactive
);
4497 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
4498 txinfo
= (struct rt2870_txinfo
*) data
->buf
;
4499 txwi
= (struct rt2870_txwi
*) (txinfo
+ 1);
4501 wh
= mtod(m
, struct ieee80211_frame
*);
4503 type
= wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
;
4505 hasqos
= IEEE80211_QOS_HAS_SEQ(wh
);
4508 if (IEEE80211_HAS_ADDR4(wh
))
4509 qos
= le16toh(*(const uint16_t *)
4510 (((struct ieee80211_qosframe_addr4
*) wh
)->i_qos
));
4512 qos
= le16toh(*(const uint16_t *)
4513 (((struct ieee80211_qosframe
*) wh
)->i_qos
));
4520 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4522 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
4524 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
4525 rate
= ic
->ic_fixed_rate
;
4527 rate
= ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
];
4531 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
4532 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
4533 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
4534 rate
= ic
->ic_fixed_rate
;
4536 rate
= ni
->ni_rates
.rs_rates
[ni
->ni_txrate
];
4539 rate
&= IEEE80211_RATE_VAL
;
4541 len
= sizeof(struct rt2870_txinfo
) + sizeof(struct rt2870_txwi
) + m
->m_pkthdr
.len
;
4543 /* align end on a 4-bytes boundary */
4545 dmalen
= (len
+ 3) & ~3;
4549 memset(txinfo
, 0, sizeof(struct rt2870_txinfo
));
4551 txinfo
->len
= htole16(dmalen
);
4553 txinfo
->qsel_flags
= (RT2870_TXINFO_QSEL_EDCA
<< RT2870_TXINFO_QSEL_SHIFT
);
4555 /* fill Tx wireless info */
4557 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4560 mcs
= rt2870_rate2mcs(rate
);
4562 if (type
== IEEE80211_FC0_TYPE_DATA
)
4563 wcid
= !IEEE80211_IS_MULTICAST(wh
->i_addr1
) ? rni
->staid
: RT2870_WCID_MCAST
;
4565 wcid
= RT2870_WCID_RESERVED
;
4567 /* calculate MPDU length without padding */
4569 hdrsize
= ieee80211_anyhdrsize(wh
);
4570 hdrspace
= ieee80211_anyhdrspace(ic
, wh
);
4571 mpdu_len
= m
->m_pkthdr
.len
- hdrspace
+ hdrsize
;
4573 memset(txwi
, 0, sizeof(struct rt2870_txwi
));
4577 /* MIMO power save */
4579 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) &&
4580 ((ni
->ni_htcap
& IEEE80211_HTCAP_SMPS
) != IEEE80211_HTCAP_SMPS_OFF
))
4584 if ((ni
->ni_htcap
& IEEE80211_HTCAP_SMPS
) == IEEE80211_HTCAP_SMPS_DYNAMIC
)
4586 /* dynamic MIMO power save */
4588 txwi
->mpdu_density_flags
|=
4589 (RT2870_TXWI_FLAGS_MIMOPS
<< RT2870_TXWI_FLAGS_SHIFT
);
4593 /* static MIMO power save */
4608 txwi
->pid_mpdu_len
= ((htole16(pid
) & RT2870_TXWI_PID_MASK
) <<
4609 RT2870_TXWI_PID_SHIFT
) | ((htole16(mpdu_len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
4610 RT2870_TXWI_MPDU_LEN_SHIFT
);
4612 stbc
= (ic
->ic_htcaps
& IEEE80211_HTCAP_TXSTBC
) &&
4613 (ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_htcap
& IEEE80211_HTCAP_RXSTBC
);
4615 shortgi
= (ic
->ic_flags_ext
& (IEEE80211_FEXT_SHORTGI20
| IEEE80211_FEXT_SHORTGI40
)) &&
4616 (ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_htcap
& (IEEE80211_HTCAP_SHORTGI20
| IEEE80211_HTCAP_SHORTGI40
));
4618 txwi
->phymode_ifs_stbc_shortgi
|=
4619 ((stbc
& RT2870_TXWI_STBC_MASK
) << RT2870_TXWI_STBC_SHIFT
) |
4620 ((shortgi
& RT2870_TXWI_SHORTGI_MASK
) << RT2870_TXWI_SHORTGI_SHIFT
);
4622 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4624 txwi
->phymode_ifs_stbc_shortgi
|=
4625 (RT2870_TXWI_PHYMODE_HT_MIXED
<< RT2870_TXWI_PHYMODE_SHIFT
);
4629 if (!RT2870_RATE_IS_OFDM(rate
))
4631 txwi
->phymode_ifs_stbc_shortgi
|=
4632 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
4634 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
4635 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
4639 txwi
->phymode_ifs_stbc_shortgi
|=
4640 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
4644 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_chw
== 40))
4645 bw
= RT2870_TXWI_BW_40
;
4647 bw
= RT2870_TXWI_BW_20
;
4649 txwi
->bw_mcs
= ((bw
& RT2870_TXWI_BW_MASK
) << RT2870_TXWI_BW_SHIFT
) |
4650 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
4652 if (type
!= IEEE80211_FC0_TYPE_DATA
)
4653 txwi
->txop
= (RT2870_TXWI_TXOP_BACKOFF
<< RT2870_TXWI_TXOP_SHIFT
);
4655 txwi
->txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
4657 /* skip ACKs for multicast frames */
4659 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
4660 (!hasqos
|| (qos
& IEEE80211_QOS_ACKPOLICY
) != IEEE80211_QOS_ACKPOLICY_NOACK
))
4662 txwi
->bawin_size_xflags
|=
4663 (RT2870_TXWI_XFLAGS_ACK
<< RT2870_TXWI_XFLAGS_SHIFT
);
4665 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4667 /* preamble + plcp + signal extension + SIFS */
4669 dur
= 16 + 4 + 6 + 10;
4673 ackrate
= rt2870_ackrate(ic
, rate
);
4675 dur
= rt2870_txtime(RT2870_ACK_SIZE
, ackrate
, ic
->ic_flags
) +
4679 *(uint16_t *) wh
->i_dur
= htole16(dur
);
4682 /* check fo A-MPDU */
4684 if ((qos
& IEEE80211_QOS_ACKPOLICY
) == IEEE80211_QOS_ACKPOLICY_BA
)
4686 ac
= M_WME_GETAC(m
);
4687 tx_ampdu
= &ni
->ni_tx_ampdu
[ac
];
4689 mpdu_density
= RT2870_MS(ni
->ni_htparam
, IEEE80211_HTCAP_MPDUDENSITY
);
4690 bawin_size
= tx_ampdu
->txa_wnd
;
4692 txwi
->mpdu_density_flags
|=
4693 ((mpdu_density
& RT2870_TXWI_MPDU_DENSITY_MASK
) << RT2870_TXWI_MPDU_DENSITY_SHIFT
) |
4694 (RT2870_TXWI_FLAGS_AMPDU
<< RT2870_TXWI_FLAGS_SHIFT
);
4696 txwi
->bawin_size_xflags
|=
4697 ((bawin_size
& RT2870_TXWI_BAWIN_SIZE_MASK
) << RT2870_TXWI_BAWIN_SIZE_SHIFT
);
4699 if (IEEE80211_HAS_ADDR4(wh
))
4700 ((struct ieee80211_qosframe_addr4
*) wh
)->i_qos
[0] &= ~IEEE80211_QOS_ACKPOLICY
;
4702 ((struct ieee80211_qosframe
*) wh
)->i_qos
[0] &= ~IEEE80211_QOS_ACKPOLICY
;
4711 /* ask MAC to insert timestamp into probe responses */
4713 if ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
4714 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
4715 txwi
->mpdu_density_flags
|=
4716 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
4718 if (bpf_peers_present(sc
->drvbpf
))
4722 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
4723 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4724 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4725 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4726 tap
->chan_maxpow
= 0;
4728 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4729 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4733 if (mcs
& RT2870_TXWI_MCS_SHOTPRE
)
4734 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4737 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4739 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4740 tap
->flags
|= IEEE80211_RADIOTAP_F_WEP
;
4742 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4744 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4746 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4748 wh
->i_fc
[1] |= IEEE80211_FC1_WEP
;
4752 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4756 m_copydata(m
, 0, m
->m_pkthdr
.len
, (caddr_t
) (txwi
+ 1));
4758 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4759 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4760 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, ampdu=%d, mimops=%d, DMA len=%d\n",
4761 device_get_nameunit(sc
->dev
),
4762 qid
, hdrsize
, hdrspace
, m
->m_pkthdr
.len
, bw
, stbc
, shortgi
, mcs
, wcid
, ampdu
, mimops
, dmalen
);
4767 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4770 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, ring
, data
->buf
, len
,
4771 USBD_FORCE_SHORT_XFER
| USBD_NO_COPY
, RT2870_USB_XFER_TIMEOUT
, rt2870_tx_intr
);
4773 usbd_transfer(data
->xfer
);
4781 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
4782 struct mbuf
*m
, struct ieee80211_node
*ni
,
4783 const struct ieee80211_bpf_params
*params
)
4785 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4787 device_get_nameunit(sc
->dev
));
4795 static void rt2870_rx_intr(usbd_xfer_handle xfer
,
4796 usbd_private_handle priv
, usbd_status status
)
4798 struct rt2870_softc
*sc
;
4800 struct rt2870_softc_rx_ring
*ring
;
4801 struct rt2870_softc_rx_data
*data
;
4806 ring
= &sc
->rx_ring
;
4808 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4811 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4812 "%s: Rx interrupt: %s\n",
4813 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4816 sc
->rx_interrupts
++;
4820 case USBD_NORMAL_COMPLETION
:
4821 data
= STAILQ_FIRST(&ring
->active
);
4825 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4827 KASSERT(data
->xfer
== xfer
,
4828 ("%s: Rx interrupt: invalid USB xfer\n",
4829 device_get_nameunit(sc
->dev
)));
4831 usbd_get_xfer_status(xfer
, NULL
, NULL
, &len
, NULL
);
4835 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4837 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4840 case USBD_CANCELLED
:
4841 case USBD_NOT_STARTED
:
4845 if (status
== USBD_STALLED
)
4846 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4848 data
= STAILQ_FIRST(&ring
->active
);
4851 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4853 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4855 usbd_setup_xfer(xfer
, ring
->usb_pipe
, sc
, data
->buf
,
4856 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
4857 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
4859 usbd_transfer(xfer
);
4868 static void rt2870_tx_intr(usbd_xfer_handle xfer
,
4869 usbd_private_handle priv
, usbd_status status
)
4871 struct rt2870_softc
*sc
;
4873 struct rt2870_softc_tx_ring
*ring
;
4874 struct rt2870_softc_tx_data
*data
;
4880 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4883 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4884 "%s: Tx interrupt: %s\n",
4885 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4888 sc
->tx_interrupts
[ring
->qid
]++;
4892 case USBD_NORMAL_COMPLETION
:
4893 data
= STAILQ_FIRST(&ring
->active
);
4897 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4899 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4901 sc
->tx_qid_pending_mask
|= (1 << ring
->qid
);
4903 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4906 case USBD_CANCELLED
:
4907 case USBD_NOT_STARTED
:
4911 data
= STAILQ_FIRST(&ring
->active
);
4914 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4916 if (data
->m
!= NULL
)
4922 if (data
->ni
!= NULL
)
4924 ieee80211_free_node(data
->ni
);
4928 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4933 printf("%s: could not transmit buffer: qid=%d, status=%s\n",
4934 device_get_nameunit(sc
->dev
), ring
->qid
, usbd_errstr(status
));
4937 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4939 if (status
== USBD_STALLED
)
4940 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4946 * rt2870_rx_done_task
4948 static void rt2870_rx_done_task(void *context
, int pending
)
4950 struct rt2870_softc
*sc
;
4956 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4957 "%s: Rx done task\n",
4958 device_get_nameunit(sc
->dev
));
4960 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4963 if (rt2870_rx_eof(sc
, sc
->rx_process_limit
) != 0)
4965 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4966 "%s: Rx done task: scheduling again\n",
4967 device_get_nameunit(sc
->dev
));
4969 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4974 * rt2870_tx_done_task
4976 static void rt2870_tx_done_task(void *context
, int pending
)
4978 struct rt2870_softc
*sc
;
4985 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4986 "%s: Tx done task\n",
4987 device_get_nameunit(sc
->dev
));
4989 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4992 for (i
= sc
->usb_endpoints
- 2; i
>= 0; i
--)
4994 if (sc
->tx_qid_pending_mask
& (1 << i
))
4996 sc
->tx_qid_pending_mask
&= ~(1 << i
);
4998 rt2870_tx_eof(sc
, &sc
->tx_ring
[i
]);
5002 if (sc
->tx_qid_pending_mask
!= 0)
5004 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
5005 "%s: Tx done task: scheduling again\n",
5006 device_get_nameunit(sc
->dev
));
5008 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
5013 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
5017 * rt2870_periodic_task
5019 static void rt2870_periodic_task(void *context
, int pending
)
5021 struct rt2870_softc
*sc
;
5023 struct ieee80211com
*ic
;
5029 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
5030 "%s: periodic task: round=%lu\n",
5031 device_get_nameunit(sc
->dev
), sc
->periodic_round
);
5033 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
5036 sc
->periodic_round
++;
5038 rt2870_update_stats(sc
);
5040 if ((sc
->periodic_round
% 10) == 0)
5042 rt2870_bbp_tuning(sc
);
5044 rt2870_update_raw_counters(sc
);
5046 rt2870_watchdog(sc
);
5048 if (ic
->ic_opmode
== IEEE80211_M_STA
)
5049 rt2870_amrr_update_iter_func(sc
, ic
->ic_bss
);
5051 ieee80211_iterate_nodes(&ic
->ic_sta
, rt2870_amrr_update_iter_func
, sc
);
5054 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
5060 static void rt2870_cmd_task(void *context
, int pending
)
5062 struct rt2870_softc
*sc
;
5063 struct rt2870_softc_cmd_ring
*ring
;
5064 struct rt2870_softc_cmd
*cmd
;
5067 ring
= &sc
->cmd_ring
;
5071 if (!(sc
->flags
& RT2870_SOFTC_FLAGS_VALID
))
5074 RT2870_SOFTC_LOCK(sc
);
5076 cmd
= STAILQ_FIRST(&ring
->active
);
5079 RT2870_SOFTC_UNLOCK(sc
);
5083 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
5085 RT2870_SOFTC_UNLOCK(sc
);
5087 cmd
->cb(sc
, cmd
->data
);
5089 RT2870_SOFTC_LOCK(sc
);
5091 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
5094 RT2870_SOFTC_UNLOCK(sc
);
5101 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
)
5104 struct rt2870_softc_rx_ring
*ring
;
5105 struct rt2870_softc_rx_data
*data
;
5111 ring
= &sc
->rx_ring
;
5117 RT2870_SOFTC_LOCK(sc
);
5119 data
= STAILQ_FIRST(&ring
->done
);
5122 RT2870_SOFTC_UNLOCK(sc
);
5126 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
5128 RT2870_SOFTC_UNLOCK(sc
);
5133 if (len
< RT2870_RX_DESC_SIZE
)
5139 while (len
> (sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)))
5141 dmalen
= (le32toh(*(uint32_t *) buf
) & 0xffff);
5143 if (dmalen
== 0 || (dmalen
& 3) != 0)
5145 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
5146 "%s: bad DMA len=%u\n",
5147 device_get_nameunit(sc
->dev
), dmalen
);
5151 if ((dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)) > len
)
5153 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
5154 "%s: bad DMA len: DMA len=%u, USB len=%d\n",
5155 device_get_nameunit(sc
->dev
),
5156 (unsigned int) (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)), len
);
5162 rt2870_rx_frame(sc
, buf
+ sizeof(uint32_t), dmalen
);
5164 buf
+= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
5165 len
-= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
5170 RT2870_SOFTC_LOCK(sc
);
5172 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
5174 RT2870_SOFTC_UNLOCK(sc
);
5176 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, sc
, data
->buf
,
5177 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
5178 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
5180 usbd_transfer(data
->xfer
);
5185 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
5186 "%s: Rx eof: nframes=%d\n",
5187 device_get_nameunit(sc
->dev
), nframes
);
5189 sc
->rx_packets
+= nframes
;
5191 return (limit
== 0);
5197 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
5198 struct rt2870_softc_tx_ring
*ring
)
5201 struct rt2870_softc_tx_data
*data
;
5210 RT2870_SOFTC_LOCK(sc
);
5212 data
= STAILQ_FIRST(&ring
->done
);
5215 RT2870_SOFTC_UNLOCK(sc
);
5219 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
5221 RT2870_SOFTC_UNLOCK(sc
);
5223 rt2870_drain_fifo_stats(sc
);
5227 if (data
->m
->m_flags
& M_TXCB
)
5228 ieee80211_process_callback(data
->ni
, data
->m
, 0);
5232 ieee80211_free_node(data
->ni
);
5237 RT2870_SOFTC_LOCK(sc
);
5239 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5243 RT2870_SOFTC_UNLOCK(sc
);
5248 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
5249 "%s: Tx eof: qid=%d, nframes=%d\n",
5250 device_get_nameunit(sc
->dev
), ring
->qid
, nframes
);
5254 * rt2870_update_stats
5256 static void rt2870_update_stats(struct rt2870_softc
*sc
)
5259 struct ieee80211com
*ic
;
5261 int beacons
, noretryok
, retryok
, failed
, underflows
, zerolen
;
5266 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
5267 "%s: update statistic\n",
5268 device_get_nameunit(sc
->dev
));
5270 rt2870_drain_fifo_stats(sc
);
5272 /* read and clear Tx statistic registers */
5274 rt2870_io_mac_read_multi(sc
, RT2870_REG_TX_STA_CNT0
,
5275 stacnt
, sizeof(stacnt
));
5277 stacnt
[0] = le32toh(stacnt
[0]);
5278 stacnt
[1] = le32toh(stacnt
[1]);
5279 stacnt
[2] = le32toh(stacnt
[2]);
5281 beacons
= stacnt
[0] >> 16;
5282 noretryok
= stacnt
[1] & 0xffff;
5283 retryok
= stacnt
[1] >> 16;
5284 failed
= stacnt
[0] & 0xffff;
5285 underflows
= stacnt
[2] >> 16;
5286 zerolen
= stacnt
[2] & 0xffff;
5288 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
5289 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
5290 device_get_nameunit(sc
->dev
),
5291 beacons
, noretryok
, retryok
, failed
, underflows
, zerolen
);
5293 ifp
->if_oerrors
+= failed
;
5295 sc
->tx_beacons
+= beacons
;
5296 sc
->tx_noretryok
+= noretryok
;
5297 sc
->tx_retryok
+= retryok
;
5298 sc
->tx_failed
+= failed
;
5299 sc
->tx_underflows
+= underflows
;
5300 sc
->tx_zerolen
+= zerolen
;
5306 static void rt2870_bbp_tuning(struct rt2870_softc
*sc
)
5308 struct ieee80211com
*ic
;
5309 struct ieee80211_node
*ni
;
5311 int8_t rssi
, old
, new;
5313 /* RT2860C does not support BBP tuning */
5315 if (sc
->mac_rev
== 0x28600100)
5320 if ((ic
->ic_flags
& IEEE80211_F_SCAN
) ||
5321 ic
->ic_opmode
!= IEEE80211_M_STA
|| ic
->ic_state
!= IEEE80211_S_RUN
)
5326 chan
= ieee80211_chan2ieee(ic
, ni
->ni_chan
);
5330 else if (chan
<= 64)
5332 else if (chan
<= 128)
5337 rssi
= ieee80211_getrssi(ic
);
5339 if (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
))
5341 new = 0x2e + sc
->lna_gain
[group
];
5345 if (!IEEE80211_IS_CHAN_HT40(ni
->ni_chan
))
5346 new = 0x32 + sc
->lna_gain
[group
] * 5 / 3;
5348 new = 0x3a + sc
->lna_gain
[group
] * 5 / 3;
5351 /* Tune if absolute average RSSI is greater than -80 */
5356 old
= rt2870_io_bbp_read(sc
, 66);
5359 rt2870_io_bbp_write(sc
, 66, new);
5365 static void rt2870_watchdog(struct rt2870_softc
*sc
)
5370 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
5372 RT2870_DPRINTF(sc
, RT2870_DEBUG_WATCHDOG
,
5373 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
5374 device_get_nameunit(sc
->dev
), tmp
);
5376 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) != 0)
5378 sc
->tx_queue_not_empty
[0]++;
5380 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40012);
5382 for (ntries
= 0; ntries
< 10; ntries
++)
5384 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
5385 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) == 0)
5391 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
5394 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) != 0)
5396 sc
->tx_queue_not_empty
[1]++;
5398 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf4000a);
5400 for (ntries
= 0; ntries
< 10; ntries
++)
5402 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
5403 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) == 0)
5409 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
5414 * rt2870_drain_fifo_stats
5416 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
)
5420 uint8_t wcid
, mcs
, pid
;
5421 int ok
, agg
, retrycnt
;
5423 ifp
= sc
->ic
.ic_ifp
;
5425 /* drain Tx status FIFO (maxsize = 16) */
5427 while ((sc
->flags
& RT2870_SOFTC_FLAGS_VALID
) &&
5428 (stats
= rt2870_io_mac_read(sc
, RT2870_REG_TX_STA_FIFO
)) &
5429 RT2870_REG_TX_STA_FIFO_VALID
)
5431 wcid
= (stats
>> RT2870_REG_TX_STA_FIFO_WCID_SHIFT
) &
5432 RT2870_REG_TX_STA_FIFO_WCID_MASK
;
5434 /* if no ACK was requested, no feedback is available */
5436 if (!(stats
& RT2870_REG_TX_STA_FIFO_ACK_REQ
) || wcid
== 0xff)
5439 /* update AMRR statistic */
5441 ok
= (stats
& RT2870_REG_TX_STA_FIFO_TX_OK
) ? 1 : 0;
5442 agg
= (stats
& RT2870_REG_TX_STA_FIFO_AGG
) ? 1 : 0;
5443 mcs
= (stats
>> RT2870_REG_TX_STA_FIFO_MCS_SHIFT
) &
5444 RT2870_REG_TX_STA_FIFO_MCS_MASK
;
5445 pid
= (stats
>> RT2870_REG_TX_STA_FIFO_PID_SHIFT
) &
5446 RT2870_REG_TX_STA_FIFO_PID_MASK
;
5447 retrycnt
= pid
- mcs
;
5449 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
5450 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
5451 device_get_nameunit(sc
->dev
),
5452 wcid
, ok
, agg
, mcs
, pid
, retrycnt
);
5454 rt2870_amrr_tx_complete(&sc
->amrr_node
[wcid
], ok
, retrycnt
);
5462 * rt2870_update_raw_counters
5464 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
)
5468 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT
);
5470 sc
->tx_nonagg
+= tmp
& 0xffff;
5471 sc
->tx_agg
+= tmp
>> 16;
5473 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT0
);
5475 sc
->tx_ampdu
+= (tmp
& 0xffff) / 1 + (tmp
>> 16) / 2;
5477 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT1
);
5479 sc
->tx_ampdu
+= (tmp
& 0xffff) / 3 + (tmp
>> 16) / 4;
5481 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT2
);
5483 sc
->tx_ampdu
+= (tmp
& 0xffff) / 5 + (tmp
>> 16) / 6;
5485 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT3
);
5487 sc
->tx_ampdu
+= (tmp
& 0xffff) / 7 + (tmp
>> 16) / 8;
5489 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT4
);
5491 sc
->tx_ampdu
+= (tmp
& 0xffff) / 9 + (tmp
>> 16) / 10;
5493 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT5
);
5495 sc
->tx_ampdu
+= (tmp
& 0xffff) / 11 + (tmp
>> 16) / 12;
5497 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT6
);
5499 sc
->tx_ampdu
+= (tmp
& 0xffff) / 13 + (tmp
>> 16) / 14;
5501 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT7
);
5503 sc
->tx_ampdu
+= (tmp
& 0xffff) / 15 + (tmp
>> 16) / 16;
5505 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT0
);
5507 sc
->rx_crc_errors
+= tmp
& 0xffff;
5508 sc
->rx_phy_errors
+= tmp
>> 16;
5510 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT1
);
5512 sc
->rx_false_ccas
+= tmp
& 0xffff;
5513 sc
->rx_plcp_errors
+= tmp
>> 16;
5515 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT2
);
5517 sc
->rx_dup_packets
+= tmp
& 0xffff;
5518 sc
->rx_fifo_overflows
+= tmp
>> 16;
5520 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TXRX_MPDU_DEN_CNT
);
5522 sc
->tx_mpdu_zero_density
+= tmp
& 0xffff;
5523 sc
->rx_mpdu_zero_density
+= tmp
>> 16;
5527 * rt2870_alloc_rx_ring
5529 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
5530 struct rt2870_softc_rx_ring
*ring
)
5532 struct rt2870_softc_rx_data
*data
;
5535 STAILQ_INIT(&ring
->active
);
5536 STAILQ_INIT(&ring
->done
);
5538 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
5540 data
= &ring
->data
[i
];
5542 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
5543 if (data
->xfer
== NULL
)
5545 printf("%s: could not allocate Rx xfer\n",
5546 device_get_nameunit(sc
->dev
));
5551 data
->buf
= usbd_alloc_buffer(data
->xfer
, RT2870_USB_RX_BULK_BUFLEN
);
5552 if (data
->buf
== NULL
)
5554 printf("%s: could not allocate Rx buffer\n",
5555 device_get_nameunit(sc
->dev
));
5565 rt2870_free_rx_ring(sc
, ring
);
5571 * rt2870_reset_rx_ring
5573 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
5574 struct rt2870_softc_rx_ring
*ring
)
5576 STAILQ_INIT(&ring
->active
);
5577 STAILQ_INIT(&ring
->done
);
5581 * rt2870_free_rx_ring
5583 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
5584 struct rt2870_softc_rx_ring
*ring
)
5586 struct rt2870_softc_rx_data
*data
;
5589 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
5591 data
= &ring
->data
[i
];
5593 if ((data
->buf
!= NULL
) && (data
->xfer
!= NULL
))
5595 usbd_free_buffer(data
->xfer
);
5599 if (data
->xfer
!= NULL
)
5601 usbd_free_xfer(data
->xfer
);
5608 * rt2870_alloc_tx_ring
5610 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
5611 struct rt2870_softc_tx_ring
*ring
, int qid
)
5613 struct rt2870_softc_tx_data
*data
;
5616 STAILQ_INIT(&ring
->inactive
);
5617 STAILQ_INIT(&ring
->active
);
5618 STAILQ_INIT(&ring
->done
);
5624 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5626 data
= &ring
->data
[i
];
5628 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
5629 if (data
->xfer
== NULL
)
5631 printf("%s: could not allocate Tx xfer\n",
5632 device_get_nameunit(sc
->dev
));
5637 data
->buf
= usbd_alloc_buffer(data
->xfer
,
5638 RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
);
5639 if (data
->buf
== NULL
)
5641 printf("%s: could not allocate Tx buffer\n",
5642 device_get_nameunit(sc
->dev
));
5647 memset(data
->buf
, 0, RT2870_TX_DESC_SIZE
);
5649 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5656 rt2870_free_tx_ring(sc
, ring
);
5662 * rt2870_reset_tx_ring
5664 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
5665 struct rt2870_softc_tx_ring
*ring
)
5667 struct rt2870_softc_tx_data
*data
;
5670 STAILQ_INIT(&ring
->inactive
);
5671 STAILQ_INIT(&ring
->active
);
5672 STAILQ_INIT(&ring
->done
);
5676 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5678 data
= &ring
->data
[i
];
5680 if (data
->m
!= NULL
)
5686 if (data
->ni
!= NULL
)
5688 ieee80211_free_node(data
->ni
);
5692 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5697 * rt2870_free_tx_ring
5699 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
5700 struct rt2870_softc_tx_ring
*ring
)
5702 struct rt2870_softc_tx_data
*data
;
5705 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5707 data
= &ring
->data
[i
];
5709 if (data
->xfer
!= NULL
)
5711 usbd_free_xfer(data
->xfer
);
5715 if (data
->m
!= NULL
)
5721 if (data
->ni
!= NULL
)
5723 ieee80211_free_node(data
->ni
);
5730 * rt2870_reset_cmd_ring
5732 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
5733 struct rt2870_softc_cmd_ring
*ring
)
5735 struct rt2870_softc_cmd
*cmd
;
5738 STAILQ_INIT(&ring
->inactive
);
5739 STAILQ_INIT(&ring
->active
);
5743 for (i
= 0; i
< RT2870_SOFTC_CMD_RING_CMD_COUNT
; i
++)
5745 cmd
= &ring
->cmd
[i
];
5747 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
5752 * rt2870_sysctl_attach
5754 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
)
5756 struct sysctl_ctx_list
*ctx
;
5757 struct sysctl_oid
*tree
;
5758 struct sysctl_oid
*stats
;
5760 ctx
= device_get_sysctl_ctx(sc
->dev
);
5761 tree
= device_get_sysctl_tree(sc
->dev
);
5763 stats
= SYSCTL_ADD_NODE(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
5764 "stats", CTLFLAG_RD
, 0, "statistic");
5766 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5767 "interrupts", CTLFLAG_RD
, &sc
->interrupts
, 0,
5770 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5771 "rx_interrupts", CTLFLAG_RD
, &sc
->rx_interrupts
, 0,
5774 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5776 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5777 "tx_mgmt_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[5], 0,
5778 "Tx MGMT interrupts");
5780 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5781 "tx_hcca_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[4], 0,
5782 "Tx HCCA interrupts");
5785 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5786 "tx_ac3_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[3], 0,
5787 "Tx AC3 interrupts");
5789 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5790 "tx_ac2_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[2], 0,
5791 "Tx AC2 interrupts");
5793 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5794 "tx_ac1_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[1], 0,
5795 "Tx AC1 interrupts");
5797 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5798 "tx_ac0_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[0], 0,
5799 "Tx AC0 interrupts");
5801 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5803 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5804 "tx_mgmt_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[5].queued
, 0,
5805 "Tx MGMT data queued");
5807 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5808 "tx_hcca_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[4].queued
, 0,
5809 "Tx HCCA data queued");
5812 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5813 "tx_ac3_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[3].queued
, 0,
5814 "Tx AC3 data queued");
5816 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5817 "tx_ac2_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[2].queued
, 0,
5818 "Tx AC2 data queued");
5820 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5821 "tx_ac1_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[1].queued
, 0,
5822 "Tx AC1 data queued");
5824 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5825 "tx_ac0_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[0].queued
, 0,
5826 "Tx AC0 data queued");
5828 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5830 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5831 "tx_mgmt_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[5], 0,
5832 "Tx MGMT data queue full");
5834 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5835 "tx_hcca_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[4], 0,
5836 "Tx HCCA data queue full");
5839 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5840 "tx_ac3_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[3], 0,
5841 "Tx AC3 data queue full");
5843 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5844 "tx_ac2_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[2], 0,
5845 "Tx AC2 data queue full");
5847 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5848 "tx_ac1_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[1], 0,
5849 "Tx AC1 data queue full");
5851 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5852 "tx_ac0_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[0], 0,
5853 "Tx AC0 data queue full");
5855 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5856 "tx_watchdog_timeouts", CTLFLAG_RD
, &sc
->tx_watchdog_timeouts
, 0,
5857 "Tx watchdog timeouts");
5859 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5860 "rx_mbuf_alloc_errors", CTLFLAG_RD
, &sc
->rx_mbuf_alloc_errors
, 0,
5861 "Rx mbuf allocation errors");
5863 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5864 "tx_queue_0_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[0], 0,
5865 "Tx queue 0 not empty");
5867 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5868 "tx_queue_1_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[1], 0,
5869 "Tx queue 1 not empty");
5871 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5872 "tx_beacons", CTLFLAG_RD
, &sc
->tx_beacons
, 0,
5875 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5876 "tx_noretryok", CTLFLAG_RD
, &sc
->tx_noretryok
, 0,
5877 "Tx successfull without retries");
5879 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5880 "tx_retryok", CTLFLAG_RD
, &sc
->tx_retryok
, 0,
5881 "Tx successfull with retries");
5883 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5884 "tx_failed", CTLFLAG_RD
, &sc
->tx_failed
, 0,
5887 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5888 "tx_underflows", CTLFLAG_RD
, &sc
->tx_underflows
, 0,
5891 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5892 "tx_zerolen", CTLFLAG_RD
, &sc
->tx_zerolen
, 0,
5895 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5896 "tx_nonagg", CTLFLAG_RD
, &sc
->tx_nonagg
, 0,
5897 "Tx non-aggregated");
5899 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5900 "tx_agg", CTLFLAG_RD
, &sc
->tx_agg
, 0,
5903 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5904 "tx_ampdu", CTLFLAG_RD
, &sc
->tx_ampdu
, 0,
5907 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5908 "tx_mpdu_zero_density", CTLFLAG_RD
, &sc
->tx_mpdu_zero_density
, 0,
5909 "Tx MPDU with zero density");
5911 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5912 "tx_ampdu_sessions", CTLFLAG_RD
, &sc
->tx_ampdu_sessions
, 0,
5913 "Tx A-MPDU sessions");
5915 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5916 "rx_packets", CTLFLAG_RD
, &sc
->rx_packets
, 0,
5919 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5920 "rx_ampdu", CTLFLAG_RD
, &sc
->rx_ampdu
, 0,
5923 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5924 "rx_mpdu_zero_density", CTLFLAG_RD
, &sc
->rx_mpdu_zero_density
, 0,
5925 "Rx MPDU with zero density");
5927 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5928 "rx_ampdu_sessions", CTLFLAG_RD
, &sc
->rx_ampdu_sessions
, 0,
5929 "Rx A-MPDU sessions");
5931 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5932 "rx_amsdu", CTLFLAG_RD
, &sc
->rx_amsdu
, 0,
5935 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5936 "rx_crc_errors", CTLFLAG_RD
, &sc
->rx_crc_errors
, 0,
5939 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5940 "rx_phy_errors", CTLFLAG_RD
, &sc
->rx_phy_errors
, 0,
5943 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5944 "rx_false_ccas", CTLFLAG_RD
, &sc
->rx_false_ccas
, 0,
5947 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5948 "rx_plcp_errors", CTLFLAG_RD
, &sc
->rx_plcp_errors
, 0,
5951 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5952 "rx_dup_packets", CTLFLAG_RD
, &sc
->rx_dup_packets
, 0,
5953 "Rx duplicate packets");
5955 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5956 "rx_fifo_overflows", CTLFLAG_RD
, &sc
->rx_fifo_overflows
, 0,
5957 "Rx FIFO overflows");
5959 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5960 "rx_cipher_no_errors", CTLFLAG_RD
, &sc
->rx_cipher_no_errors
, 0,
5961 "Rx cipher no errors");
5963 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5964 "rx_cipher_icv_errors", CTLFLAG_RD
, &sc
->rx_cipher_icv_errors
, 0,
5965 "Rx cipher ICV errors");
5967 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5968 "rx_cipher_mic_errors", CTLFLAG_RD
, &sc
->rx_cipher_mic_errors
, 0,
5969 "Rx cipher MIC errors");
5971 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5972 "rx_cipher_invalid_key_errors", CTLFLAG_RD
, &sc
->rx_cipher_invalid_key_errors
, 0,
5973 "Rx cipher invalid key errors");
5976 static device_method_t rt2870_dev_methods
[] =
5978 DEVMETHOD(device_probe
, rt2870_probe
),
5979 DEVMETHOD(device_attach
, rt2870_attach
),
5980 DEVMETHOD(device_detach
, rt2870_detach
),
5984 static driver_t rt2870_driver
=
5988 sizeof(struct rt2870_softc
),
5991 static devclass_t rt2870_dev_class
;
5993 DRIVER_MODULE(rt2870
, uhub
, rt2870_driver
, rt2870_dev_class
,
5994 usbd_driver_load
, 0);
5996 MODULE_DEPEND(rt2870
, usb
, 1, 1, 1);
5997 MODULE_DEPEND(rt2870
, wlan
, 1, 1, 1);