3 * Copyright (c) 2009-2010 Alexander Egorenkov <egorenar@gmail.com>
4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "rt2870_softc.h"
20 #include "rt2870_reg.h"
21 #include "rt2870_eeprom.h"
22 #include "rt2870_ucode.h"
23 #include "rt2870_rxwi.h"
24 #include "rt2870_rxinfo.h"
25 #include "rt2870_txinfo.h"
26 #include "rt2870_txwi.h"
27 #include "rt2870_read_eeprom.h"
28 #include "rt2870_io.h"
29 #include "rt2870_rf.h"
30 #include "rt2870_led.h"
31 #include "rt2870_debug.h"
37 #define USB_PRODUCT_LINKSYS4_WUSB600N 0x0071
38 #define USB_PRODUCT_DLINK2_DWA140 0x3c09
39 #define USB_PRODUCT_DLINK2_DWA160AREVB 0x3c11
40 #define USB_PRODUCT_ASUS_RT2770F 0x1742
41 #define USB_PRODUCT_RALINK_RT2770 0x2770
43 #define RT2870_USB_CONFIG_INDEX 0
44 #define RT2870_USB_IFACE_INDEX 0
46 #define RT2870_USB_REQ_MAC_READ_MULTI 0x07
48 /* packet length + Rx wireless info + Rx info */
49 #define RT2870_RX_DESC_SIZE \
50 (sizeof(uint32_t) + sizeof(struct rt2870_rxwi) + sizeof(struct rt2870_rxinfo))
52 /* Tx info + Tx wireless info + max padding */
53 #define RT2870_TX_DESC_SIZE \
54 (sizeof(struct rt2870_txinfo) + sizeof(struct rt2870_txwi) + 11)
56 #define RT2870_MAX_AGG_SIZE 3840
58 #define RT2870_USB_RX_BULK_BUFLEN (2048 * 12)
60 #define RT2870_NOISE_FLOOR -95
62 #define RT2870_AID2WCID(aid) ((aid) & 0xff)
64 #define RT2870_ACK_SIZE 14
66 #define IEEE80211_HAS_ADDR4(wh) \
67 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
69 #define RT2870_MS(_v, _f) (((_v) & _f) >> _f##_S)
70 #define RT2870_SM(_v, _f) (((_v) << _f##_S) & _f)
72 #define RT2870_USB_XFER_TIMEOUT 5000
74 #define RT2870_TX_WATCHDOG_TIMEOUT 5
77 * Data structures and types
80 struct rt2870_cmd_argv_keyset
82 struct ieee80211_key key
;
86 struct rt2870_cmd_argv_keydelete
88 struct ieee80211_key key
;
93 * Static function prototypes
96 static void rt2870_init_channels(struct rt2870_softc
*sc
);
98 static void rt2870_init_channels_ht40(struct rt2870_softc
*sc
);
100 static void rt2870_init_locked(void *priv
);
102 static void rt2870_init(void *priv
);
104 static int rt2870_init_bbp(struct rt2870_softc
*sc
);
106 static void rt2870_stop_locked(void *priv
);
108 static void rt2870_stop(void *priv
);
110 static void rt2870_start(struct ifnet
*ifp
);
112 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
);
114 static struct ieee80211vap
*rt2870_vap_create(struct ieee80211com
*ic
,
115 const char name
[IFNAMSIZ
], int unit
, int opmode
, int flags
,
116 const uint8_t bssid
[IEEE80211_ADDR_LEN
],
117 const uint8_t mac
[IEEE80211_ADDR_LEN
]);
119 static void rt2870_vap_delete(struct ieee80211vap
*vap
);
121 static int rt2870_vap_reset(struct ieee80211vap
*vap
, u_long cmd
);
123 static int rt2870_vap_newstate(struct ieee80211vap
*vap
,
124 enum ieee80211_state nstate
, int arg
);
126 static void rt2870_vap_key_update_begin(struct ieee80211vap
*vap
);
128 static void rt2870_vap_key_update_end(struct ieee80211vap
*vap
);
130 static int rt2870_vap_key_set(struct ieee80211vap
*vap
,
131 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
]);
133 static int rt2870_vap_key_delete(struct ieee80211vap
*vap
,
134 const struct ieee80211_key
*k
);
136 static void rt2870_vap_update_beacon(struct ieee80211vap
*vap
, int what
);
138 static struct ieee80211_node
*rt2870_node_alloc(struct ieee80211vap
*vap
,
139 const uint8_t mac
[IEEE80211_ADDR_LEN
]);
141 static void rt2870_node_getmimoinfo(const struct ieee80211_node
*ni
,
142 struct ieee80211_mimo_info
*mi
);
144 static int rt2870_setregdomain(struct ieee80211com
*ic
,
145 struct ieee80211_regdomain
*reg
,
146 int nchans
, struct ieee80211_channel chans
[]);
148 static void rt2870_getradiocaps(struct ieee80211com
*ic
,
149 int maxchans
, int *nchans
, struct ieee80211_channel chans
[]);
151 static void rt2870_scan_start(struct ieee80211com
*ic
);
153 static void rt2870_scan_end(struct ieee80211com
*ic
);
155 static void rt2870_set_channel(struct ieee80211com
*ic
);
157 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
);
159 static void rt2870_updateslot(struct ifnet
*ifp
);
161 static void rt2870_update_promisc(struct ifnet
*ifp
);
163 static void rt2870_update_mcast(struct ifnet
*ifp
);
165 static int rt2870_wme_update(struct ieee80211com
*ic
);
167 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
168 const struct ieee80211_bpf_params
*params
);
170 static int rt2870_recv_action(struct ieee80211_node
*ni
,
171 const struct ieee80211_frame
*wh
,
172 const uint8_t *frm
, const uint8_t *efrm
);
174 static int rt2870_send_action(struct ieee80211_node
*ni
,
175 int cat
, int act
, void *sa
);
177 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
);
179 static void rt2870_periodic(void *arg
);
181 static void rt2870_tx_watchdog(void *arg
);
183 static int rt2870_do_async(struct rt2870_softc
*sc
,
184 void (*cb
)(struct rt2870_softc
*sc
, void *arg
),
187 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
);
189 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
);
191 static void rt2870_vap_key_set_cb(struct rt2870_softc
*sc
, void *arg
);
193 static void rt2870_vap_key_delete_cb(struct rt2870_softc
*sc
, void *arg
);
195 static void rt2870_vap_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
);
197 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
198 const uint8_t *bssid
);
200 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
201 const uint8_t *addr
);
203 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
);
205 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
);
207 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
);
209 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
);
211 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
);
213 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
);
215 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
);
217 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
);
219 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
);
221 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
);
223 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
);
225 static void rt2870_asic_update_beacon(struct rt2870_softc
*sc
,
226 struct ieee80211vap
*vap
);
228 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
);
230 static int rt2870_beacon_alloc(struct rt2870_softc
*sc
,
231 struct ieee80211vap
*vap
);
233 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
);
235 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
236 const struct rt2870_rxwi
*rxwi
);
238 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
239 uint8_t rssi
, uint8_t rxpath
);
241 static uint8_t rt2870_rate2mcs(uint8_t rate
);
243 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
244 uint8_t *buf
, uint32_t dmalen
);
246 static int rt2870_tx_mgmt(struct rt2870_softc
*sc
,
247 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
);
249 static int rt2870_tx_data(struct rt2870_softc
*sc
,
250 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
);
252 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
253 struct mbuf
*m
, struct ieee80211_node
*ni
,
254 const struct ieee80211_bpf_params
*params
);
256 static void rt2870_rx_intr(struct usb_xfer
*xfer
, usb_error_t error
);
258 static void rt2870_tx_intr(struct usb_xfer
*xfer
, usb_error_t error
);
260 static void rt2870_rx_done_task(void *context
, int pending
);
262 static void rt2870_tx_done_task(void *context
, int pending
);
264 static void rt2870_periodic_task(void *context
, int pending
);
266 static void rt2870_cmd_task(void *context
, int pending
);
268 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
);
270 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
271 struct rt2870_softc_tx_ring
*ring
);
273 static void rt2870_update_stats(struct rt2870_softc
*sc
);
275 static void rt2870_bbp_tuning(struct rt2870_softc
*sc
);
277 static void rt2870_watchdog(struct rt2870_softc
*sc
);
279 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
);
281 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
);
283 static int rt2870_txrx_enable(struct rt2870_softc
*sc
);
285 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
286 struct rt2870_softc_rx_ring
*ring
);
288 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
289 struct rt2870_softc_rx_ring
*ring
);
291 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
292 struct rt2870_softc_rx_ring
*ring
);
294 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
295 struct rt2870_softc_tx_ring
*ring
, int qid
);
297 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
298 struct rt2870_softc_tx_ring
*ring
);
300 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
301 struct rt2870_softc_tx_ring
*ring
);
303 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
304 struct rt2870_softc_cmd_ring
*ring
);
306 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
);
312 static const struct usb_device_id rt2870_usb_devid
[] =
314 { USB_VP(USB_VENDOR_LINKSYS4
, USB_PRODUCT_LINKSYS4_WUSB600N
) }, /* Linksys WUSB600N */
315 { USB_VP(USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA140
) }, /* D-Link DWA-140 */
316 { USB_VP(USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA160AREVB
) }, /* D-Link DWA-160A Rev. B */
317 { USB_VP(USB_VENDOR_ASUS
, USB_PRODUCT_ASUS_RT2770F
) }, /* Asus RT2770F */
318 { USB_VP(USB_VENDOR_RALINK
, USB_PRODUCT_RALINK_RT2770
) }, /* Ralink RT2770 */
321 static const struct usb_config rt2870_usb_config
[RT2870_SOFTC_USB_XFER_COUNT
] =
326 .direction
= UE_DIR_IN
,
327 .bufsize
= RT2870_USB_RX_BULK_BUFLEN
,
328 .flags
= { .ext_buffer
= 1, .pipe_bof
= 1, .short_xfer_ok
= 1, },
329 .callback
= rt2870_rx_intr
,
334 .direction
= UE_DIR_OUT
,
335 .bufsize
= (RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
),
336 .flags
= { .ext_buffer
= 1, .pipe_bof
= 1, .force_short_xfer
= 1, },
337 .callback
= rt2870_tx_intr
,
338 .timeout
= RT2870_USB_XFER_TIMEOUT
,
343 .direction
= UE_DIR_OUT
,
344 .bufsize
= (RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
),
345 .flags
= { .ext_buffer
= 1, .pipe_bof
= 1, .force_short_xfer
= 1, },
346 .callback
= rt2870_tx_intr
,
347 .timeout
= RT2870_USB_XFER_TIMEOUT
,
352 .direction
= UE_DIR_OUT
,
353 .bufsize
= (RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
),
354 .flags
= { .ext_buffer
= 1, .pipe_bof
= 1, .force_short_xfer
= 1, },
355 .callback
= rt2870_tx_intr
,
356 .timeout
= RT2870_USB_XFER_TIMEOUT
,
361 .direction
= UE_DIR_OUT
,
362 .bufsize
= (RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
),
363 .flags
= { .ext_buffer
= 1, .pipe_bof
= 1, .force_short_xfer
= 1, },
364 .callback
= rt2870_tx_intr
,
365 .timeout
= RT2870_USB_XFER_TIMEOUT
,
370 .direction
= UE_DIR_OUT
,
371 .bufsize
= (RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
),
372 .flags
= { .ext_buffer
= 1, .pipe_bof
= 1, .force_short_xfer
= 1, },
373 .callback
= rt2870_tx_intr
,
374 .timeout
= RT2870_USB_XFER_TIMEOUT
,
379 .direction
= UE_DIR_OUT
,
380 .bufsize
= (RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
),
381 .flags
= { .ext_buffer
= 1, .pipe_bof
= 1, .force_short_xfer
= 1, },
382 .callback
= rt2870_tx_intr
,
383 .timeout
= RT2870_USB_XFER_TIMEOUT
,
393 { RT2870_REG_PBF_BCN_OFFSET0
, 0xf8f0e8e0 },
394 { RT2870_REG_PBF_BCN_OFFSET1
, 0x6f77d0c8 },
395 { RT2870_REG_LEGACY_BASIC_RATE
, 0x0000013f },
396 { RT2870_REG_HT_BASIC_RATE
, 0x00008003 },
397 { RT2870_REG_SYS_CTRL
, 0x00000000 },
398 { RT2870_REG_RX_FILTER_CFG
, 0x00017f97 },
399 { RT2870_REG_BKOFF_SLOT_CFG
, 0x00000209 },
400 { RT2870_REG_TX_SW_CFG0
, 0x00000000 },
401 { RT2870_REG_TX_SW_CFG1
, 0x00080606 },
402 { RT2870_REG_TX_LINK_CFG
, 0x00001020 },
403 { RT2870_REG_TX_TIMEOUT_CFG
, 0x000a2090 },
404 { RT2870_REG_MAX_LEN_CFG
, (1 << 12) | RT2870_MAX_AGG_SIZE
},
405 { RT2870_REG_LED_CFG
, 0x7f031e46 },
406 { RT2870_REG_PBF_MAX_PCNT
, 0x1f3fbf9f },
407 { RT2870_REG_TX_RTY_CFG
, 0x47d01f0f },
408 { RT2870_REG_AUTO_RSP_CFG
, 0x00000013 },
409 { RT2870_REG_TX_CCK_PROT_CFG
, 0x05740003 },
410 { RT2870_REG_TX_OFDM_PROT_CFG
, 0x05740003 },
411 { RT2870_REG_PBF_CFG
, 0x00f40006 },
412 { RT2870_REG_TX_MM40_PROT_CFG
, 0x03f44084 },
413 { RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, 0x00000030 },
414 { RT2870_REG_TX_GF20_PROT_CFG
, 0x01744004 },
415 { RT2870_REG_TX_GF40_PROT_CFG
, 0x03f44084 },
416 { RT2870_REG_TX_MM20_PROT_CFG
, 0x01744004 },
417 { RT2870_REG_TX_TXOP_CTRL_CFG
, 0x0000583f },
418 { RT2870_REG_TX_RTS_CFG
, 0x00092b20 },
419 { RT2870_REG_TX_EXP_ACK_TIME
, 0x002400ca },
420 { RT2870_REG_HCCAPSMP_TXOP_HLDR_ET
, 0x00000002 },
421 { RT2870_REG_XIFS_TIME_CFG
, 0x33a41010 },
422 { RT2870_REG_PWR_PIN_CFG
, 0x00000003 },
423 { RT2870_REG_SCHDMA_WMM_AIFSN_CFG
, 0x00002273 },
424 { RT2870_REG_SCHDMA_WMM_CWMIN_CFG
, 0x00002344 },
425 { RT2870_REG_SCHDMA_WMM_CWMAX_CFG
, 0x000034aa },
428 #define RT2870_DEF_MAC_SIZE (sizeof(rt2870_def_mac) / sizeof(rt2870_def_mac[0]))
453 #define RT2870_DEF_BBP_SIZE (sizeof(rt2870_def_bbp) / sizeof(rt2870_def_bbp[0]))
455 SYSCTL_NODE(_hw
, OID_AUTO
, rt2870
, CTLFLAG_RD
, 0, "RT2870 driver parameters");
458 static int rt2870_debug
= 0;
459 SYSCTL_INT(_hw_rt2870
, OID_AUTO
, debug
, CTLFLAG_RW
, &rt2870_debug
, 0, "rt2870 debug level");
460 TUNABLE_INT("hw.rt2870.debug", &rt2870_debug
);
466 static int rt2870_probe(device_t dev
)
468 struct usb_attach_arg
*uaa
;
470 uaa
= device_get_ivars(dev
);
472 if (uaa
->usb_mode
!= USB_MODE_HOST
)
475 if (uaa
->info
.bConfigIndex
!= RT2870_USB_CONFIG_INDEX
)
478 if (uaa
->info
.bIfaceIndex
!= RT2870_USB_IFACE_INDEX
)
481 return (usbd_lookup_id_by_uaa(rt2870_usb_devid
,
482 sizeof(rt2870_usb_devid
), uaa
));
488 static int rt2870_attach(device_t dev
)
490 struct rt2870_softc
*sc
;
491 struct usb_attach_arg
*uaa
;
492 usb_error_t usb_error
;
493 struct ieee80211com
*ic
;
495 struct usb_interface
*iface
;
496 struct usb_interface_descriptor
*iface_desc
;
498 int error
, ntries
, i
;
500 sc
= device_get_softc(dev
);
501 uaa
= device_get_ivars(dev
);
504 sc
->usb_dev
= uaa
->device
;
506 device_set_usb_desc(dev
);
509 sc
->debug
= rt2870_debug
;
511 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev
),
512 SYSCTL_CHILDREN(device_get_sysctl_tree(dev
)), OID_AUTO
,
513 "debug", CTLFLAG_RW
, &sc
->debug
, 0, "rt2870 debug level");
516 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
518 device_get_nameunit(sc
->dev
));
520 iface_index
= RT2870_USB_IFACE_INDEX
;
522 iface
= usbd_get_iface(uaa
->device
, iface_index
);
525 printf("%s: could not get USB interface\n",
526 device_get_nameunit(sc
->dev
));
530 iface_desc
= usbd_get_interface_descriptor(iface
);
531 if (iface_desc
== NULL
)
533 printf("%s: could not get USB interface descriptor\n",
534 device_get_nameunit(sc
->dev
));
538 sc
->usb_endpoints
= iface_desc
->bNumEndpoints
;
539 if ((sc
->usb_endpoints
!= RT2870_SOFTC_USB_XFER_COUNT
) &&
540 (sc
->usb_endpoints
!= RT2870_SOFTC_USB_XFER_COUNT
- 2))
542 printf("%s: wrong number of USB endpoints=%d\n",
543 device_get_nameunit(sc
->dev
), sc
->usb_endpoints
);
547 mtx_init(&sc
->lock
, device_get_nameunit(dev
),
548 MTX_NETWORK_LOCK
, MTX_DEF
);
550 usb_error
= usbd_transfer_setup(uaa
->device
, &iface_index
,
551 sc
->usb_xfer
, rt2870_usb_config
, sc
->usb_endpoints
,
555 printf("%s: could not allocate USB transfers: error=%s\n",
556 device_get_nameunit(sc
->dev
), usbd_errstr(usb_error
));
561 RT2870_SOFTC_LOCK(sc
);
563 for (ntries
= 0; ntries
< 100; ntries
++)
565 sc
->mac_rev
= rt2870_io_mac_read(sc
, RT2870_REG_MAC_CSR0
);
566 if (sc
->mac_rev
!= 0x00000000 && sc
->mac_rev
!= 0xffffffff)
574 printf("%s: timeout waiting for NIC to initialize\n",
575 device_get_nameunit(sc
->dev
));
580 rt2870_read_eeprom(sc
);
582 RT2870_SOFTC_UNLOCK(sc
);
584 printf("%s: MAC/BBP RT2870 (rev 0x%08x), RF %s\n",
585 device_get_nameunit(sc
->dev
), sc
->mac_rev
,
586 rt2870_rf_name(sc
->rf_rev
));
588 /* allocate Tx and Rx rings */
590 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
592 error
= rt2870_alloc_tx_ring(sc
, &sc
->tx_ring
[i
], i
);
595 printf("%s: could not allocate Tx ring #%d\n",
596 device_get_nameunit(sc
->dev
), i
);
601 if (sc
->usb_endpoints
== RT2870_SOFTC_USB_XFER_COUNT
)
602 sc
->tx_ring_mgtqid
= 5;
604 sc
->tx_ring_mgtqid
= 0;
606 error
= rt2870_alloc_rx_ring(sc
, &sc
->rx_ring
);
609 printf("%s: could not allocate Rx ring\n",
610 device_get_nameunit(sc
->dev
));
614 callout_init(&sc
->periodic_ch
, 0);
615 callout_init_mtx(&sc
->tx_watchdog_ch
, &sc
->lock
, 0);
617 ifp
= sc
->ifp
= if_alloc(IFT_IEEE80211
);
620 printf("%s: could not if_alloc()\n",
621 device_get_nameunit(sc
->dev
));
628 if_initname(ifp
, "rt2870", device_get_unit(sc
->dev
));
630 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
632 ifp
->if_init
= rt2870_init
;
633 ifp
->if_ioctl
= rt2870_ioctl
;
634 ifp
->if_start
= rt2870_start
;
636 IFQ_SET_MAXLEN(&ifp
->if_snd
, IFQ_MAXLEN
);
637 ifp
->if_snd
.ifq_drv_maxlen
= IFQ_MAXLEN
;
638 IFQ_SET_READY(&ifp
->if_snd
);
644 ic
->ic_phytype
= IEEE80211_T_HT
;
645 ic
->ic_opmode
= IEEE80211_M_STA
;
647 ic
->ic_caps
= IEEE80211_C_MONITOR
|
656 IEEE80211_C_SHPREAMBLE
|
663 ic
->ic_cryptocaps
|= IEEE80211_CRYPTO_WEP
|
664 IEEE80211_CRYPTO_TKIP
|
665 IEEE80211_CRYPTO_TKIPMIC
|
666 IEEE80211_CRYPTO_AES_CCM
;
668 ic
->ic_htcaps
= IEEE80211_HTC_HT
|
669 IEEE80211_HTC_AMSDU
| /* A-MSDU Tx */
670 IEEE80211_HTC_AMPDU
| /* A-MPDU Tx */
671 IEEE80211_HTC_SMPS
| /* MIMO power save */
672 IEEE80211_HTCAP_MAXAMSDU_3839
| /* max. A-MSDU Rx length */
673 IEEE80211_HTCAP_CHWIDTH40
| /* HT 40MHz channel width */
674 IEEE80211_HTCAP_GREENFIELD
| /* HT greenfield */
675 IEEE80211_HTCAP_SHORTGI20
| /* HT 20MHz short GI */
676 IEEE80211_HTCAP_SHORTGI40
| /* HT 40MHz short GI */
677 IEEE80211_HTCAP_SMPS_OFF
; /* MIMO power save disabled */
679 /* spatial streams */
681 if (sc
->nrxpath
== 2)
682 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_2STREAM
;
683 else if (sc
->nrxpath
== 3)
684 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_3STREAM
;
686 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_1STREAM
;
689 ic
->ic_htcaps
|= IEEE80211_HTCAP_TXSTBC
;
693 if (sc
->mac_rev
!= 0x28600100)
694 ic
->ic_htcaps
|= IEEE80211_HTCAP_DELBA
;
700 rt2870_init_channels(sc
);
702 rt2870_init_channels_ht40(sc
);
704 ieee80211_ifattach(ic
, sc
->mac_addr
);
706 ic
->ic_vap_create
= rt2870_vap_create
;
707 ic
->ic_vap_delete
= rt2870_vap_delete
;
709 ic
->ic_node_alloc
= rt2870_node_alloc
;
710 ic
->ic_node_getmimoinfo
= rt2870_node_getmimoinfo
;
711 ic
->ic_setregdomain
= rt2870_setregdomain
;
712 ic
->ic_getradiocaps
= rt2870_getradiocaps
;
713 ic
->ic_scan_start
= rt2870_scan_start
;
714 ic
->ic_scan_end
= rt2870_scan_end
;
715 ic
->ic_set_channel
= rt2870_set_channel
;
716 ic
->ic_newassoc
= rt2870_newassoc
;
717 ic
->ic_updateslot
= rt2870_updateslot
;
718 ic
->ic_update_promisc
= rt2870_update_promisc
;
719 ic
->ic_update_mcast
= rt2870_update_mcast
;
720 ic
->ic_wme
.wme_update
= rt2870_wme_update
;
721 ic
->ic_raw_xmit
= rt2870_raw_xmit
;
723 sc
->recv_action
= ic
->ic_recv_action
;
724 ic
->ic_recv_action
= rt2870_recv_action
;
726 sc
->send_action
= ic
->ic_send_action
;
727 ic
->ic_send_action
= rt2870_send_action
;
729 /* hardware requires padding between 802.11 frame header and body */
731 ic
->ic_flags
|= IEEE80211_F_DATAPAD
| IEEE80211_F_DOTH
;
733 ic
->ic_flags_ext
|= IEEE80211_FEXT_SWBMISS
;
735 ieee80211_radiotap_attach(ic
,
736 &sc
->txtap
.ihdr
, sizeof(sc
->txtap
),
737 RT2870_SOFTC_TX_RADIOTAP_PRESENT
,
738 &sc
->rxtap
.ihdr
, sizeof(sc
->rxtap
),
739 RT2870_SOFTC_RX_RADIOTAP_PRESENT
);
741 /* init task queue */
743 TASK_INIT(&sc
->rx_done_task
, 0, rt2870_rx_done_task
, sc
);
744 TASK_INIT(&sc
->tx_done_task
, 0, rt2870_tx_done_task
, sc
);
745 TASK_INIT(&sc
->periodic_task
, 0, rt2870_periodic_task
, sc
);
746 TASK_INIT(&sc
->cmd_task
, 0, rt2870_cmd_task
, sc
);
748 sc
->rx_process_limit
= 100;
750 sc
->taskqueue
= taskqueue_create("rt2870_taskq", M_NOWAIT
,
751 taskqueue_thread_enqueue
, &sc
->taskqueue
);
753 taskqueue_start_threads(&sc
->taskqueue
, 1, PI_NET
, "%s taskq",
754 device_get_nameunit(sc
->dev
));
756 rt2870_reset_cmd_ring(sc
, &sc
->cmd_ring
);
758 rt2870_sysctl_attach(sc
);
761 ieee80211_announce(ic
);
767 /* stop all USB transfers */
769 usbd_transfer_unsetup(sc
->usb_xfer
, sc
->usb_endpoints
);
771 /* free Tx and Rx rings */
773 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
774 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
776 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
778 mtx_destroy(&sc
->lock
);
786 static int rt2870_detach(device_t dev
)
788 struct rt2870_softc
*sc
;
789 struct ieee80211com
*ic
;
793 if (!device_is_attached(dev
))
796 sc
= device_get_softc(dev
);
800 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
802 device_get_nameunit(sc
->dev
));
804 RT2870_SOFTC_LOCK(sc
);
808 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
810 callout_stop(&sc
->periodic_ch
);
811 callout_stop(&sc
->tx_watchdog_ch
);
813 RT2870_SOFTC_UNLOCK(sc
);
815 taskqueue_block(sc
->taskqueue
);
817 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
818 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
819 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
820 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
822 /* stop all USB transfers */
824 usbd_transfer_unsetup(sc
->usb_xfer
, sc
->usb_endpoints
);
826 RT2870_SOFTC_LOCK(sc
);
828 /* free Tx and Rx rings */
830 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
831 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
833 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
835 RT2870_SOFTC_UNLOCK(sc
);
837 ieee80211_ifdetach(ic
);
841 taskqueue_free(sc
->taskqueue
);
843 mtx_destroy(&sc
->lock
);
849 * rt2870_init_channels
851 static void rt2870_init_channels(struct rt2870_softc
*sc
)
854 struct ieee80211com
*ic
;
855 struct ieee80211_channel
*c
;
861 /* set supported channels for 2GHz band */
863 for (i
= 1; i
<= 14; i
++)
865 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
866 flags
= IEEE80211_CHAN_B
;
868 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
872 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
873 flags
= IEEE80211_CHAN_B
| IEEE80211_CHAN_HT20
;
875 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
879 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
880 flags
= IEEE80211_CHAN_G
;
882 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
886 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
887 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT20
;
889 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
894 /* set supported channels for 5GHz band */
896 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
897 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
899 for (i
= 36; i
<= 64; i
+= 4)
901 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
902 flags
= IEEE80211_CHAN_A
;
904 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
908 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
909 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
911 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
916 for (i
= 100; i
<= 140; i
+= 4)
918 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
919 flags
= IEEE80211_CHAN_A
;
921 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
925 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
926 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
928 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
933 for (i
= 149; i
<= 165; i
+= 4)
935 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
936 flags
= IEEE80211_CHAN_A
;
938 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
942 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
943 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
945 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
953 * rt2870_init_channels_ht40
955 static void rt2870_init_channels_ht40(struct rt2870_softc
*sc
)
958 struct ieee80211com
*ic
;
959 struct ieee80211_channel
*c
, *cent
, *ext
;
965 /* set supported channels for 2GHz band */
967 for (i
= 1; i
<= 14; i
++)
969 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT40
;
971 /* find the center channel */
973 cent
= ieee80211_find_channel_byieee(ic
, i
,
974 flags
& ~IEEE80211_CHAN_HT
);
977 printf("%s: skip channel %d, could not find center channel\n",
978 device_get_nameunit(sc
->dev
), i
);
982 /* find the extension channel */
984 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
985 flags
& ~IEEE80211_CHAN_HT
);
988 printf("%s: skip channel %d, could not find extension channel\n",
989 device_get_nameunit(sc
->dev
), i
);
993 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
996 c
->ic_extieee
= ext
->ic_ieee
;
997 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
998 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1000 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1003 c
->ic_extieee
= cent
->ic_ieee
;
1004 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1005 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1008 /* set supported channels for 5GHz band */
1010 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
1011 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
1013 for (i
= 36; i
<= 64; i
+= 4)
1015 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1017 /* find the center channel */
1019 cent
= ieee80211_find_channel_byieee(ic
, i
,
1020 flags
& ~IEEE80211_CHAN_HT
);
1023 printf("%s: skip channel %d, could not find center channel\n",
1024 device_get_nameunit(sc
->dev
), i
);
1028 /* find the extension channel */
1030 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1031 flags
& ~IEEE80211_CHAN_HT
);
1034 printf("%s: skip channel %d, could not find extension channel\n",
1035 device_get_nameunit(sc
->dev
), i
);
1039 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1042 c
->ic_extieee
= ext
->ic_ieee
;
1043 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1044 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1046 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1049 c
->ic_extieee
= cent
->ic_ieee
;
1050 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1051 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1054 for (i
= 100; i
<= 140; i
+= 4)
1056 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1058 /* find the center channel */
1060 cent
= ieee80211_find_channel_byieee(ic
, i
,
1061 flags
& ~IEEE80211_CHAN_HT
);
1064 printf("%s: skip channel %d, could not find center channel\n",
1065 device_get_nameunit(sc
->dev
), i
);
1069 /* find the extension channel */
1071 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1072 flags
& ~IEEE80211_CHAN_HT
);
1075 printf("%s: skip channel %d, could not find extension channel\n",
1076 device_get_nameunit(sc
->dev
), i
);
1080 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1083 c
->ic_extieee
= ext
->ic_ieee
;
1084 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1085 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1087 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1090 c
->ic_extieee
= cent
->ic_ieee
;
1091 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1092 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1095 for (i
= 149; i
<= 165; i
+= 4)
1097 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1099 /* find the center channel */
1101 cent
= ieee80211_find_channel_byieee(ic
, i
,
1102 flags
& ~IEEE80211_CHAN_HT
);
1105 printf("%s: skip channel %d, could not find center channel\n",
1106 device_get_nameunit(sc
->dev
), i
);
1110 /* find the extension channel */
1112 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1113 flags
& ~IEEE80211_CHAN_HT
);
1116 printf("%s: skip channel %d, could not find extension channel\n",
1117 device_get_nameunit(sc
->dev
), i
);
1121 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1124 c
->ic_extieee
= ext
->ic_ieee
;
1125 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1126 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1128 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1131 c
->ic_extieee
= cent
->ic_ieee
;
1132 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1133 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1139 * rt2870_init_locked
1141 static void rt2870_init_locked(void *priv
)
1143 struct rt2870_softc
*sc
;
1145 struct ieee80211com
*ic
;
1146 int ntries
, error
, i
;
1147 uint32_t tmp
, stacnt
[6];
1153 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1154 "%s: initializing\n",
1155 device_get_nameunit(sc
->dev
));
1157 RT2870_SOFTC_ASSERT_LOCKED(sc
);
1159 if (!(sc
->flags
& RT2870_SOFTC_FLAGS_UCODE_LOADED
))
1161 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1162 "%s: loading 8051 microcode\n",
1163 device_get_nameunit(sc
->dev
));
1165 error
= rt2870_io_mcu_load_ucode(sc
, rt2870_ucode
, sizeof(rt2870_ucode
));
1168 printf("%s: could not load 8051 microcode\n",
1169 device_get_nameunit(sc
->dev
));
1173 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1174 "%s: 8051 microcode was successfully loaded\n",
1175 device_get_nameunit(sc
->dev
));
1177 sc
->flags
|= RT2870_SOFTC_FLAGS_UCODE_LOADED
;
1180 /* wait while DMA engine is busy */
1182 for (ntries
= 0; ntries
< 100; ntries
++)
1184 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1185 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1193 printf("%s: timeout waiting for DMA engine\n",
1194 device_get_nameunit(sc
->dev
));
1199 tmp
|= RT2870_REG_TX_WB_DDONE
;
1201 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1203 /* PBF hardware reset */
1205 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_SYS_CTRL
);
1209 rt2870_io_mac_write(sc
, RT2870_REG_PBF_SYS_CTRL
, tmp
);
1211 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1212 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1214 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1216 rt2870_io_mcu_reset(sc
);
1218 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1220 /* init Tx power per rate */
1222 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
1224 if (sc
->txpow_rate_20mhz
[i
] == 0xffffffff)
1227 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
),
1228 sc
->txpow_rate_20mhz
[i
]);
1231 for (i
= 0; i
< RT2870_DEF_MAC_SIZE
; i
++)
1232 rt2870_io_mac_write(sc
, rt2870_def_mac
[i
].reg
,
1233 rt2870_def_mac
[i
].val
);
1235 /* wait while MAC is busy */
1237 for (ntries
= 0; ntries
< 100; ntries
++)
1239 if (!(rt2870_io_mac_read(sc
, RT2870_REG_STATUS_CFG
) &
1240 (RT2870_REG_STATUS_TX_BUSY
| RT2870_REG_STATUS_RX_BUSY
)))
1248 printf("%s: timeout waiting for MAC\n",
1249 device_get_nameunit(sc
->dev
));
1253 /* clear Host to MCU mailbox */
1255 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX_BBP_AGENT
, 0);
1256 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX
, 0);
1260 error
= rt2870_init_bbp(sc
);
1264 /* set up maximum buffer sizes */
1266 tmp
= (1 << 12) | RT2870_MAX_AGG_SIZE
;
1268 rt2870_io_mac_write(sc
, RT2870_REG_MAX_LEN_CFG
, tmp
);
1270 if (sc
->mac_rev
== 0x28720200)
1272 /* set max. PSDU length from 16K to 32K bytes */
1274 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_MAX_LEN_CFG
);
1279 rt2870_io_mac_write(sc
, RT2870_REG_MAX_LEN_CFG
, tmp
);
1282 if (sc
->mac_rev
>= 0x28720200 && sc
->mac_rev
< 0x30700200)
1284 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_MAX_LEN_CFG
);
1289 rt2870_io_mac_write(sc
, RT2870_REG_MAX_LEN_CFG
, tmp
);
1292 /* set mac address */
1294 rt2870_asic_set_macaddr(sc
, IF_LLADDR(ifp
));
1296 /* clear statistic registers */
1298 rt2870_io_mac_read_multi(sc
, RT2870_REG_RX_STA_CNT0
,
1299 stacnt
, sizeof(stacnt
));
1301 /* send LEDs operating mode to microcontroller */
1303 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED1
,
1304 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[0]);
1305 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED2
,
1306 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[1]);
1307 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED3
,
1308 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[2]);
1310 /* write vendor-specific BBP values (from EEPROM) */
1312 for (i
= 0; i
< RT2870_SOFTC_BBP_EEPROM_COUNT
; i
++)
1314 if (sc
->bbp_eeprom
[i
].reg
== 0x00 ||
1315 sc
->bbp_eeprom
[i
].reg
== 0xff)
1318 rt2870_io_bbp_write(sc
, sc
->bbp_eeprom
[i
].reg
,
1319 sc
->bbp_eeprom
[i
].val
);
1322 /* disable non-existing Rx chains */
1324 tmp
= rt2870_io_bbp_read(sc
, 3);
1326 tmp
&= ~((1 << 4) | (1 << 3));
1328 if (sc
->nrxpath
== 3)
1330 else if (sc
->nrxpath
== 2)
1333 rt2870_io_bbp_write(sc
, 3, tmp
);
1335 /* disable non-existing Tx chains */
1337 tmp
= rt2870_io_bbp_read(sc
, 1);
1339 tmp
&= ~((1 << 4) | (1 << 3));
1341 if (sc
->ntxpath
== 2)
1344 rt2870_io_bbp_write(sc
, 1, tmp
);
1346 /* set current channel */
1348 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1350 /* turn radio LED on */
1352 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
);
1354 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_BOOT
,
1355 RT2870_REG_H2M_TOKEN_NO_INTR
, 0);
1357 /* set RTS threshold */
1359 rt2870_asic_update_rtsthreshold(sc
);
1363 rt2870_asic_update_txpower(sc
);
1365 /* set up protection mode */
1367 sc
->tx_ampdu_sessions
= 0;
1369 rt2870_asic_updateprot(sc
);
1371 /* clear beacon frame space (entries = 8, entry size = 512) */
1373 rt2870_io_mac_set_region_4(sc
, RT2870_REG_BEACON_BASE(0), 0, 1024);
1375 if (rt2870_txrx_enable(sc
) != 0)
1378 /* clear garbage interrupts */
1380 tmp
= rt2870_io_mac_read(sc
, 0x1300);
1382 taskqueue_unblock(sc
->taskqueue
);
1384 /* init Tx and Rx rings */
1386 for(i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1387 rt2870_reset_tx_ring(sc
, &sc
->tx_ring
[i
]);
1389 rt2870_reset_rx_ring(sc
, &sc
->rx_ring
);
1391 for(i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1393 usbd_xfer_set_priv(sc
->usb_xfer
[i
+ 1], &sc
->tx_ring
[i
]);
1394 usbd_xfer_set_stall(sc
->usb_xfer
[i
+ 1]);
1397 /* start up the receive pipe */
1399 for(i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
1400 usbd_transfer_start(sc
->usb_xfer
[0]);
1402 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
1403 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
1405 sc
->periodic_round
= 0;
1407 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
1413 rt2870_stop_locked(sc
);
1419 static void rt2870_init(void *priv
)
1421 struct rt2870_softc
*sc
;
1425 RT2870_SOFTC_LOCK(sc
);
1427 rt2870_init_locked(sc
);
1429 RT2870_SOFTC_UNLOCK(sc
);
1435 static int rt2870_init_bbp(struct rt2870_softc
*sc
)
1440 for (ntries
= 0; ntries
< 20; ntries
++)
1442 tmp
= rt2870_io_bbp_read(sc
, 0);
1443 if (tmp
!= 0x00 && tmp
!= 0xff)
1447 if (tmp
== 0x00 || tmp
== 0xff)
1449 printf("%s: timeout waiting for BBP to wakeup\n",
1450 device_get_nameunit(sc
->dev
));
1454 for (i
= 0; i
< RT2870_DEF_BBP_SIZE
; i
++)
1455 rt2870_io_bbp_write(sc
, rt2870_def_bbp
[i
].reg
,
1456 rt2870_def_bbp
[i
].val
);
1458 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1459 rt2870_io_bbp_write(sc
, 84, 0x19);
1461 if (sc
->mac_rev
== 0x28600100)
1463 rt2870_io_bbp_write(sc
, 69, 0x16);
1464 rt2870_io_bbp_write(sc
, 73, 0x12);
1471 * rt2870_stop_locked
1473 static void rt2870_stop_locked(void *priv
)
1475 struct rt2870_softc
*sc
;
1477 struct ieee80211com
*ic
;
1485 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1487 device_get_nameunit(sc
->dev
));
1489 RT2870_SOFTC_ASSERT_LOCKED(sc
);
1493 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1494 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
1496 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
1498 callout_stop(&sc
->periodic_ch
);
1499 callout_stop(&sc
->tx_watchdog_ch
);
1501 RT2870_SOFTC_UNLOCK(sc
);
1503 taskqueue_block(sc
->taskqueue
);
1505 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
1506 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
1507 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
1508 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
1510 RT2870_SOFTC_LOCK(sc
);
1512 /* clear key tables */
1514 rt2870_asic_clear_keytables(sc
);
1518 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SYS_CTRL
);
1520 tmp
&= ~(RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1522 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, tmp
);
1524 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1528 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1529 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1530 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1532 /* abort any pending transfers */
1534 for (i
= 0; i
< sc
->usb_endpoints
; i
++)
1535 usbd_transfer_stop(sc
->usb_xfer
[i
]);
1541 static void rt2870_stop(void *priv
)
1543 struct rt2870_softc
*sc
;
1547 RT2870_SOFTC_LOCK(sc
);
1549 rt2870_stop_locked(sc
);
1551 RT2870_SOFTC_UNLOCK(sc
);
1557 static void rt2870_start(struct ifnet
*ifp
)
1559 struct rt2870_softc
*sc
;
1560 struct ieee80211_node
*ni
;
1566 RT2870_SOFTC_LOCK(sc
);
1568 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
1570 RT2870_SOFTC_UNLOCK(sc
);
1576 IF_POLL(&ifp
->if_snd
, m
);
1580 IFQ_DRV_DEQUEUE(&ifp
->if_snd
, m
);
1582 ni
= (struct ieee80211_node
*) m
->m_pkthdr
.rcvif
;
1584 qid
= M_WME_GETAC(m
);
1586 if (sc
->tx_ring
[qid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1588 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1589 "%s: if_start: Tx ring with qid=%d is full\n",
1590 device_get_nameunit(sc
->dev
), qid
);
1593 ieee80211_free_node(ni
);
1595 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1598 sc
->tx_data_queue_full
[qid
]++;
1603 if (rt2870_tx_data(sc
, m
, ni
, qid
) != 0)
1605 ieee80211_free_node(ni
);
1610 sc
->tx_timer
= RT2870_TX_WATCHDOG_TIMEOUT
;
1612 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
1615 RT2870_SOFTC_UNLOCK(sc
);
1621 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1623 struct rt2870_softc
*sc
;
1624 struct ieee80211com
*ic
;
1626 int error
, startall
;
1630 ifr
= (struct ifreq
*) data
;
1639 RT2870_SOFTC_LOCK(sc
);
1641 if (ifp
->if_flags
& IFF_UP
)
1643 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1645 if ((ifp
->if_flags
^ sc
->if_flags
) & IFF_PROMISC
)
1646 rt2870_asic_update_promisc(sc
);
1650 rt2870_init_locked(sc
);
1656 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1657 rt2870_stop_locked(sc
);
1660 sc
->if_flags
= ifp
->if_flags
;
1662 RT2870_SOFTC_UNLOCK(sc
);
1665 ieee80211_start_all(ic
);
1670 error
= ifmedia_ioctl(ifp
, ifr
, &ic
->ic_media
, cmd
);
1674 error
= ether_ioctl(ifp
, cmd
, data
);
1688 static struct ieee80211vap
*rt2870_vap_create(struct ieee80211com
*ic
,
1689 const char name
[IFNAMSIZ
], int unit
, int opmode
, int flags
,
1690 const uint8_t bssid
[IEEE80211_ADDR_LEN
],
1691 const uint8_t mac
[IEEE80211_ADDR_LEN
])
1693 struct rt2870_softc
*sc
;
1695 struct rt2870_softc_vap
*rvap
;
1696 struct ieee80211vap
*vap
;
1701 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
1702 "%s: VAP create: opmode=%s\n",
1703 device_get_nameunit(sc
->dev
),
1704 ieee80211_opmode_name
[opmode
]);
1708 case IEEE80211_M_MONITOR
:
1709 case IEEE80211_M_IBSS
:
1710 case IEEE80211_M_STA
:
1711 case IEEE80211_M_AHDEMO
:
1712 case IEEE80211_M_HOSTAP
:
1713 case IEEE80211_M_MBSS
:
1714 if (!TAILQ_EMPTY(&ic
->ic_vaps
))
1716 if_printf(ifp
, "only 1 VAP supported\n");
1720 if (opmode
== IEEE80211_M_STA
)
1721 flags
|= IEEE80211_CLONE_NOBEACONS
;
1724 case IEEE80211_M_WDS
:
1725 if (TAILQ_EMPTY(&ic
->ic_vaps
) || ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
1727 if_printf(ifp
, "WDS only supported in AP mode\n");
1731 flags
&= ~IEEE80211_CLONE_BSSID
;
1735 if_printf(ifp
, "unknown opmode %d\n", opmode
);
1739 rvap
= (struct rt2870_softc_vap
*) malloc(sizeof(struct rt2870_softc_vap
),
1740 M_80211_VAP
, M_NOWAIT
| M_ZERO
);
1746 ieee80211_vap_setup(ic
, vap
, name
, unit
, opmode
, flags
, bssid
, mac
);
1748 rvap
->newstate
= vap
->iv_newstate
;
1749 vap
->iv_newstate
= rt2870_vap_newstate
;
1751 vap
->iv_reset
= rt2870_vap_reset
;
1752 vap
->iv_key_update_begin
= rt2870_vap_key_update_begin
;
1753 vap
->iv_key_update_end
= rt2870_vap_key_update_end
;
1754 vap
->iv_key_set
= rt2870_vap_key_set
;
1755 vap
->iv_key_delete
= rt2870_vap_key_delete
;
1756 vap
->iv_update_beacon
= rt2870_vap_update_beacon
;
1758 rt2870_amrr_init(&rvap
->amrr
, vap
,
1760 RT2870_AMRR_MIN_SUCCESS_THRESHOLD
,
1761 RT2870_AMRR_MAX_SUCCESS_THRESHOLD
,
1764 ieee80211_vap_attach(vap
, ieee80211_media_change
, ieee80211_media_status
);
1766 /* override default Rx A-MPDU factor */
1768 vap
->iv_ampdu_rxmax
= IEEE80211_HTCAP_MAXRXAMPDU_32K
;
1769 vap
->iv_ampdu_density
= IEEE80211_HTCAP_MPDUDENSITY_NA
;
1770 vap
->iv_ampdu_limit
= vap
->iv_ampdu_rxmax
;
1772 ic
->ic_opmode
= opmode
;
1780 static void rt2870_vap_delete(struct ieee80211vap
*vap
)
1782 struct rt2870_softc
*sc
;
1783 struct ieee80211com
*ic
;
1785 struct rt2870_softc_vap
*rvap
;
1790 rvap
= (struct rt2870_softc_vap
*) vap
;
1792 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
1793 "%s: VAP delete: opmode=%s\n",
1794 device_get_nameunit(sc
->dev
),
1795 ieee80211_opmode_name
[vap
->iv_opmode
]);
1797 rt2870_amrr_cleanup(&rvap
->amrr
);
1799 ieee80211_vap_detach(vap
);
1801 if (rvap
->beacon_mbuf
!= NULL
)
1803 m_free(rvap
->beacon_mbuf
);
1804 rvap
->beacon_mbuf
= NULL
;
1807 free(rvap
, M_80211_VAP
);
1813 static int rt2870_vap_reset(struct ieee80211vap
*vap
, u_long cmd
)
1815 struct rt2870_softc
*sc
;
1816 struct ieee80211com
*ic
;
1818 struct rt2870_softc_vap
*rvap
;
1824 rvap
= (struct rt2870_softc_vap
*) vap
;
1826 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
1827 "%s: VAP reset: cmd=%lu\n",
1828 device_get_nameunit(sc
->dev
), cmd
);
1832 RT2870_SOFTC_LOCK(sc
);
1836 case IEEE80211_IOC_RTSTHRESHOLD
:
1837 case IEEE80211_IOC_AMSDU
:
1838 rt2870_asic_update_rtsthreshold(sc
);
1841 case IEEE80211_IOC_PROTMODE
:
1842 case IEEE80211_IOC_HTPROTMODE
:
1843 rt2870_asic_updateprot(sc
);
1846 case IEEE80211_IOC_TXPOWER
:
1847 rt2870_asic_update_txpower(sc
);
1850 case IEEE80211_IOC_BURST
:
1851 rt2870_asic_updateslot(sc
);
1854 case IEEE80211_IOC_SHORTGI
:
1855 case IEEE80211_IOC_AMPDU_DENSITY
:
1856 case IEEE80211_IOC_SMPS
:
1864 RT2870_SOFTC_UNLOCK(sc
);
1870 * rt2870_vap_newstate
1872 static int rt2870_vap_newstate(struct ieee80211vap
*vap
,
1873 enum ieee80211_state nstate
, int arg
)
1875 struct rt2870_softc
*sc
;
1876 struct ieee80211com
*ic
;
1878 struct rt2870_softc_vap
*rvap
;
1879 struct ieee80211_node
*ni
;
1885 rvap
= (struct rt2870_softc_vap
*) vap
;
1887 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
1888 "%s: VAP newstate: %s -> %s\n",
1889 device_get_nameunit(sc
->dev
),
1890 ieee80211_state_name
[vap
->iv_state
], ieee80211_state_name
[nstate
]);
1892 error
= rvap
->newstate(vap
, nstate
, arg
);
1896 IEEE80211_UNLOCK(ic
);
1898 RT2870_SOFTC_LOCK(sc
);
1900 /* turn link LED off */
1902 if (nstate
!= IEEE80211_S_RUN
)
1903 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
1907 case IEEE80211_S_INIT
:
1908 rt2870_asic_disable_tsf_sync(sc
);
1911 case IEEE80211_S_RUN
:
1914 rt2870_rf_set_chan(sc
, ni
->ni_chan
);
1916 if (vap
->iv_opmode
!= IEEE80211_M_MONITOR
)
1918 rt2870_asic_enable_mrr(sc
);
1919 rt2870_asic_set_txpreamble(sc
);
1920 rt2870_asic_set_basicrates(sc
);
1921 rt2870_asic_update_txpower(sc
);
1922 rt2870_asic_set_bssid(sc
, ni
->ni_bssid
);
1925 if (vap
->iv_opmode
== IEEE80211_M_HOSTAP
||
1926 vap
->iv_opmode
== IEEE80211_M_IBSS
||
1927 vap
->iv_opmode
== IEEE80211_M_MBSS
)
1929 error
= rt2870_beacon_alloc(sc
, vap
);
1933 rt2870_asic_update_beacon(sc
, vap
);
1936 if (vap
->iv_opmode
!= IEEE80211_M_MONITOR
)
1937 rt2870_asic_enable_tsf_sync(sc
);
1939 /* turn link LED on */
1941 if (vap
->iv_opmode
!= IEEE80211_M_MONITOR
)
1943 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
|
1944 (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
) ?
1945 RT2870_LED_CMD_LINK_2GHZ
: RT2870_LED_CMD_LINK_5GHZ
));
1953 RT2870_SOFTC_UNLOCK(sc
);
1961 * rt2870_vap_key_update_begin
1963 static void rt2870_vap_key_update_begin(struct ieee80211vap
*vap
)
1965 struct rt2870_softc
*sc
;
1966 struct ieee80211com
*ic
;
1973 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
1974 "%s: VAP key update begin\n",
1975 device_get_nameunit(sc
->dev
));
1977 taskqueue_block(sc
->taskqueue
);
1979 IF_LOCK(&ifp
->if_snd
);
1983 * rt2870_vap_key_update_end
1985 static void rt2870_vap_key_update_end(struct ieee80211vap
*vap
)
1987 struct rt2870_softc
*sc
;
1988 struct ieee80211com
*ic
;
1995 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
1996 "%s: VAP key update end\n",
1997 device_get_nameunit(sc
->dev
));
1999 IF_UNLOCK(&ifp
->if_snd
);
2001 taskqueue_unblock(sc
->taskqueue
);
2005 * rt2870_vap_key_set
2007 static int rt2870_vap_key_set(struct ieee80211vap
*vap
,
2008 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
2010 struct rt2870_softc
*sc
;
2011 struct ieee80211com
*ic
;
2013 struct ieee80211_node
*ni
;
2014 struct rt2870_cmd_argv_keyset cmd_argv
;
2018 if (k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_WEP
&&
2019 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_TKIP
&&
2020 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_AES_CCM
)
2026 ni
= ieee80211_find_vap_node(&ic
->ic_sta
, vap
, k
->wk_macaddr
);
2027 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2030 ieee80211_free_node(ni
);
2032 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2034 wcid
= RT2870_AID2WCID(associd
);
2039 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2040 cmd_argv
.associd
= associd
;
2042 rt2870_do_async(sc
, rt2870_vap_key_set_cb
,
2043 &cmd_argv
, sizeof(struct rt2870_cmd_argv_keyset
));
2049 * rt2870_vap_key_delete
2051 static int rt2870_vap_key_delete(struct ieee80211vap
*vap
,
2052 const struct ieee80211_key
*k
)
2054 struct rt2870_softc
*sc
;
2055 struct ieee80211com
*ic
;
2057 struct ieee80211_node
*ni
;
2058 struct rt2870_cmd_argv_keydelete cmd_argv
;
2065 ni
= ieee80211_find_vap_node(&ic
->ic_sta
, vap
, k
->wk_macaddr
);
2066 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2069 ieee80211_free_node(ni
);
2071 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2073 wcid
= RT2870_AID2WCID(associd
);
2078 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2079 cmd_argv
.associd
= associd
;
2081 rt2870_do_async(sc
, rt2870_vap_key_delete_cb
,
2082 &cmd_argv
, sizeof(struct rt2870_cmd_argv_keydelete
));
2088 * rt2870_vap_update_beacon
2090 static void rt2870_vap_update_beacon(struct ieee80211vap
*vap
, int what
)
2092 struct rt2870_softc
*sc
;
2093 struct ieee80211com
*ic
;
2095 struct rt2870_softc_vap
*rvap
;
2097 struct ieee80211_beacon_offsets
*bo
;
2102 rvap
= (struct rt2870_softc_vap
*) vap
;
2103 m
= rvap
->beacon_mbuf
;
2104 bo
= &rvap
->beacon_offsets
;
2106 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2107 "%s: VAP update beacon: what=%d\n",
2108 device_get_nameunit(sc
->dev
), what
);
2110 setbit(bo
->bo_flags
, what
);
2112 ieee80211_beacon_update(vap
->iv_bss
, bo
, m
, 0);
2114 rt2870_do_async(sc
, rt2870_vap_update_beacon_cb
, NULL
, 0);
2120 static struct ieee80211_node
*rt2870_node_alloc(struct ieee80211vap
*vap
,
2121 const uint8_t mac
[IEEE80211_ADDR_LEN
])
2123 return malloc(sizeof(struct rt2870_softc_node
),
2124 M_80211_NODE
, M_NOWAIT
| M_ZERO
);
2128 * rt2870_node_getmimoinfo
2130 static void rt2870_node_getmimoinfo(const struct ieee80211_node
*ni
,
2131 struct ieee80211_mimo_info
*mi
)
2133 const struct rt2870_softc_node
*rni
;
2136 rni
= (const struct rt2870_softc_node
*) ni
;
2138 for (i
= 0; i
< RT2870_SOFTC_RSSI_COUNT
; i
++)
2140 mi
->rssi
[i
] = rni
->last_rssi_dbm
[i
];
2141 mi
->noise
[i
] = RT2870_NOISE_FLOOR
;
2146 * rt2870_setregdomain
2148 static int rt2870_setregdomain(struct ieee80211com
*ic
,
2149 struct ieee80211_regdomain
*reg
,
2150 int nchans
, struct ieee80211_channel chans
[])
2152 struct rt2870_softc
*sc
;
2158 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2159 "%s: set regulatory domain: country=%d, country code string=%c%c, location=%c\n",
2160 device_get_nameunit(sc
->dev
),
2161 reg
->country
, reg
->isocc
[0], reg
->isocc
[1], reg
->location
);
2167 * rt2870_getradiocaps
2169 static void rt2870_getradiocaps(struct ieee80211com
*ic
,
2170 int maxchans
, int *nchans
, struct ieee80211_channel chans
[])
2172 *nchans
= (ic
->ic_nchans
>= maxchans
) ? maxchans
: ic
->ic_nchans
;
2174 memcpy(chans
, ic
->ic_channels
, (*nchans
) * sizeof(struct ieee80211_channel
));
2180 static void rt2870_scan_start(struct ieee80211com
*ic
)
2182 struct rt2870_softc
*sc
;
2188 RT2870_SOFTC_LOCK(sc
);
2190 rt2870_asic_disable_tsf_sync(sc
);
2192 RT2870_SOFTC_UNLOCK(sc
);
2198 static void rt2870_scan_end(struct ieee80211com
*ic
)
2200 struct rt2870_softc
*sc
;
2206 RT2870_SOFTC_LOCK(sc
);
2208 rt2870_asic_enable_tsf_sync(sc
);
2210 RT2870_SOFTC_UNLOCK(sc
);
2214 * rt2870_set_channel
2216 static void rt2870_set_channel(struct ieee80211com
*ic
)
2218 struct rt2870_softc
*sc
;
2224 RT2870_DPRINTF(sc
, RT2870_DEBUG_CHAN
,
2225 "%s: set channel: channel=%u, HT%s%s\n",
2226 device_get_nameunit(sc
->dev
),
2227 ieee80211_chan2ieee(ic
, ic
->ic_curchan
),
2228 !IEEE80211_IS_CHAN_HT(ic
->ic_curchan
) ? " disabled" :
2229 IEEE80211_IS_CHAN_HT20(ic
->ic_curchan
) ? "20":
2230 IEEE80211_IS_CHAN_HT40U(ic
->ic_curchan
) ? "40U" : "40D",
2231 (ic
->ic_flags
& IEEE80211_F_SCAN
) ? ", scanning" : "");
2233 RT2870_SOFTC_LOCK(sc
);
2235 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
2237 RT2870_SOFTC_UNLOCK(sc
);
2243 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
)
2245 struct rt2870_softc
*sc
;
2246 struct ieee80211com
*ic
;
2248 struct ieee80211vap
*vap
;
2249 struct rt2870_softc_vap
*rvap
;
2256 rvap
= (struct rt2870_softc_vap
*) vap
;
2258 RT2870_DPRINTF(sc
, RT2870_DEBUG_NODE
,
2259 "%s: new association: associd=0x%04x, "
2260 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2261 device_get_nameunit(sc
->dev
), ni
->ni_associd
,
2262 ether_sprintf(ni
->ni_macaddr
),
2263 (ni
->ni_flags
& IEEE80211_NODE_QOS
) ? "enabled" : "disabled",
2264 (ni
->ni_flags
& IEEE80211_NODE_ERP
) ? "enabled" : "disabled",
2265 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? "enabled" : "disabled");
2267 wcid
= RT2870_AID2WCID(ni
->ni_associd
);
2271 RT2870_SOFTC_LOCK(sc
);
2273 rt2870_io_mac_write_multi(sc
, RT2870_REG_WCID(wcid
),
2274 ni
->ni_macaddr
, IEEE80211_ADDR_LEN
);
2276 rt2870_amrr_node_init(&rvap
->amrr
, &sc
->amrr_node
[wcid
], ni
);
2278 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2279 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2280 device_get_nameunit(sc
->dev
),
2281 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2282 ni
->ni_associd
, ni
->ni_txrate
,
2283 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2284 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2285 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2287 rt2870_asic_updateprot(sc
);
2288 rt2870_asic_updateslot(sc
);
2289 rt2870_asic_set_txpreamble(sc
);
2291 RT2870_SOFTC_UNLOCK(sc
);
2297 static void rt2870_updateslot(struct ifnet
*ifp
)
2299 struct rt2870_softc
*sc
;
2303 rt2870_do_async(sc
, rt2870_updateslot_cb
, NULL
, 0);
2307 * rt2870_update_promisc
2309 static void rt2870_update_promisc(struct ifnet
*ifp
)
2311 struct rt2870_softc
*sc
;
2315 RT2870_SOFTC_LOCK(sc
);
2317 rt2870_asic_update_promisc(sc
);
2319 RT2870_SOFTC_UNLOCK(sc
);
2323 * rt2870_update_mcast
2325 static void rt2870_update_mcast(struct ifnet
*ifp
)
2327 struct rt2870_softc
*sc
;
2335 static int rt2870_wme_update(struct ieee80211com
*ic
)
2337 struct rt2870_softc
*sc
;
2343 rt2870_do_async(sc
, rt2870_wme_update_cb
, NULL
, 0);
2351 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
2352 const struct ieee80211_bpf_params
*params
)
2354 struct rt2870_softc
*sc
;
2355 struct ieee80211com
*ic
;
2362 RT2870_SOFTC_LOCK(sc
);
2364 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2366 RT2870_SOFTC_UNLOCK(sc
);
2369 ieee80211_free_node(ni
);
2374 if (sc
->tx_ring
[sc
->tx_ring_mgtqid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
2376 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
2377 "%s: raw xmit: Tx ring with qid=%d is full\n",
2378 device_get_nameunit(sc
->dev
), sc
->tx_ring_mgtqid
);
2380 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
2382 RT2870_SOFTC_UNLOCK(sc
);
2385 ieee80211_free_node(ni
);
2387 sc
->tx_data_queue_full
[sc
->tx_ring_mgtqid
]++;
2392 if (rt2870_tx_mgmt(sc
, m
, ni
, sc
->tx_ring_mgtqid
) != 0)
2396 RT2870_SOFTC_UNLOCK(sc
);
2398 ieee80211_free_node(ni
);
2403 sc
->tx_timer
= RT2870_TX_WATCHDOG_TIMEOUT
;
2405 RT2870_SOFTC_UNLOCK(sc
);
2411 * rt2870_recv_action
2413 static int rt2870_recv_action(struct ieee80211_node
*ni
,
2414 const struct ieee80211_frame
*wh
,
2415 const uint8_t *frm
, const uint8_t *efrm
)
2417 struct rt2870_softc
*sc
;
2418 struct ieee80211com
*ic
;
2420 const struct ieee80211_action
*ia
;
2421 struct ieee80211_tx_ampdu
*tx_ampdu
;
2422 uint16_t associd
, status
, baparamset
;
2424 int ret
, tid
, bufsize
, ac
;
2431 ia
= (const struct ieee80211_action
*) frm
;
2433 ret
= sc
->recv_action(ni
, wh
, frm
, efrm
);
2435 if (ia
->ia_category
!= IEEE80211_ACTION_CAT_BA
)
2438 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2439 wcid
= RT2870_AID2WCID(associd
);
2441 switch (ia
->ia_action
)
2443 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2444 case IEEE80211_ACTION_BA_ADDBA_REQUEST
:
2445 baparamset
= LE_READ_2(frm
+ 3);
2446 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2447 bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2449 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2450 "%s: received ADDBA request: associd=0x%04x, tid=%d, bufsize=%d\n",
2451 device_get_nameunit(sc
->dev
), associd
, tid
, bufsize
);
2453 RT2870_SOFTC_LOCK(sc
);
2455 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2457 tmp
|= (0x10000 << tid
);
2459 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2461 RT2870_SOFTC_UNLOCK(sc
);
2464 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2465 case IEEE80211_ACTION_BA_ADDBA_RESPONSE
:
2466 status
= LE_READ_2(frm
+ 3);
2467 baparamset
= LE_READ_2(frm
+ 4);
2468 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2469 bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2470 ac
= TID_TO_WME_AC(tid
);
2471 tx_ampdu
= &ni
->ni_tx_ampdu
[ac
];
2473 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2474 "%s: received ADDBA response: associd=0x%04x, status=%d, tid=%d, bufsize=%d\n",
2475 device_get_nameunit(sc
->dev
), associd
, status
, tid
, bufsize
);
2477 if (sc
->mac_rev
>= 0x28720200)
2479 if (tx_ampdu
->txa_wnd
> 13)
2480 tx_ampdu
->txa_wnd
= 13;
2484 if (tx_ampdu
->txa_wnd
> 7)
2485 tx_ampdu
->txa_wnd
= 7;
2488 if (status
== IEEE80211_STATUS_SUCCESS
)
2490 sc
->tx_ampdu_sessions
++;
2492 RT2870_SOFTC_LOCK(sc
);
2494 rt2870_asic_updateprot(sc
);
2496 RT2870_SOFTC_UNLOCK(sc
);
2500 /* IEEE80211_ACTION_BA_DELBA */
2501 case IEEE80211_ACTION_BA_DELBA
:
2502 baparamset
= LE_READ_2(frm
+ 2);
2503 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2505 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2506 "%s: received DELBA request: associd=0x%04x, tid=%d\n",
2507 device_get_nameunit(sc
->dev
), associd
, tid
);
2509 RT2870_SOFTC_LOCK(sc
);
2511 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2513 tmp
&= ~(0x10000 << tid
);
2515 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2517 RT2870_SOFTC_UNLOCK(sc
);
2525 * rt2870_send_action
2527 static int rt2870_send_action(struct ieee80211_node
*ni
,
2528 int cat
, int act
, void *sa
)
2530 struct rt2870_softc
*sc
;
2531 struct ieee80211com
*ic
;
2533 uint16_t associd
, *args
, baparamset
;
2542 ret
= sc
->send_action(ni
, cat
, act
, sa
);
2544 if (cat
!= IEEE80211_ACTION_CAT_BA
)
2547 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2548 wcid
= RT2870_AID2WCID(associd
);
2553 /* IEEE80211_ACTION_BA_DELBA */
2554 case IEEE80211_ACTION_BA_DELBA
:
2555 baparamset
= RT2870_SM(args
[0], IEEE80211_DELBAPS_TID
) | args
[1];
2557 tid
= RT2870_MS(baparamset
, IEEE80211_DELBAPS_TID
);
2559 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2560 "%s: sending DELBA request: associd=0x%04x, tid=%d\n",
2561 device_get_nameunit(sc
->dev
), associd
, tid
);
2563 RT2870_SOFTC_LOCK(sc
);
2565 if (RT2870_MS(baparamset
, IEEE80211_DELBAPS_INIT
) != IEEE80211_DELBAPS_INIT
)
2567 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2569 tmp
&= ~(0x10000 << tid
);
2571 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2575 sc
->tx_ampdu_sessions
--;
2577 rt2870_asic_updateprot(sc
);
2580 RT2870_SOFTC_UNLOCK(sc
);
2588 * rt2870_amrr_update_iter_func
2590 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
)
2592 struct rt2870_softc
*sc
;
2593 struct ieee80211com
*ic
;
2595 struct ieee80211vap
*vap
;
2596 struct rt2870_softc_vap
*rvap
;
2603 rvap
= (struct rt2870_softc_vap
*) vap
;
2605 /* only associated stations */
2607 if ((ni
->ni_vap
== vap
) && (ni
->ni_associd
!= 0))
2609 wcid
= RT2870_AID2WCID(ni
->ni_associd
);
2611 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2612 "%s: AMRR node: wcid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2613 device_get_nameunit(sc
->dev
),
2614 wcid
, sc
->amrr_node
[wcid
].txcnt
, sc
->amrr_node
[wcid
].success
, sc
->amrr_node
[wcid
].retrycnt
);
2616 rt2870_amrr_choose(ni
, &sc
->amrr_node
[wcid
]);
2618 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2619 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2620 device_get_nameunit(sc
->dev
),
2621 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2622 ni
->ni_associd
, ni
->ni_txrate
,
2623 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2624 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2625 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2632 static void rt2870_periodic(void *arg
)
2634 struct rt2870_softc
*sc
;
2640 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
2642 device_get_nameunit(sc
->dev
));
2644 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2647 taskqueue_enqueue(sc
->taskqueue
, &sc
->periodic_task
);
2651 * rt2870_tx_watchdog
2653 static void rt2870_tx_watchdog(void *arg
)
2655 struct rt2870_softc
*sc
;
2661 if (sc
->tx_timer
== 0)
2664 if (--sc
->tx_timer
== 0)
2666 printf("%s: Tx watchdog timeout: resetting\n",
2667 device_get_nameunit(sc
->dev
));
2669 rt2870_stop_locked(sc
);
2670 rt2870_init_locked(sc
);
2674 sc
->tx_watchdog_timeouts
++;
2677 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
2683 static int rt2870_do_async(struct rt2870_softc
*sc
,
2684 void (*cb
)(struct rt2870_softc
*sc
, void *arg
),
2688 struct rt2870_softc_cmd_ring
*ring
;
2689 struct rt2870_softc_cmd
*cmd
;
2693 ring
= &sc
->cmd_ring
;
2695 RT2870_SOFTC_LOCK(sc
);
2697 if (ring
->queued
>= RT2870_SOFTC_CMD_RING_CMD_COUNT
)
2699 RT2870_SOFTC_UNLOCK(sc
);
2703 cmd
= STAILQ_FIRST(&ring
->inactive
);
2704 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
2709 memcpy(cmd
->data
, arg
, len
);
2711 STAILQ_INSERT_TAIL(&ring
->active
, cmd
, next
);
2714 run_cmd_task
= (ring
->queued
== 1);
2716 RT2870_SOFTC_UNLOCK(sc
);
2719 taskqueue_enqueue(sc
->taskqueue
, &sc
->cmd_task
);
2725 * rt2870_updateslot_cb
2727 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
)
2729 RT2870_SOFTC_LOCK(sc
);
2731 rt2870_asic_updateslot(sc
);
2733 RT2870_SOFTC_UNLOCK(sc
);
2737 * rt2870_wme_update_cb
2739 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
)
2741 RT2870_SOFTC_LOCK(sc
);
2743 rt2870_asic_wme_update(sc
);
2745 RT2870_SOFTC_UNLOCK(sc
);
2749 * rt2870_vap_key_set_cb
2751 static void rt2870_vap_key_set_cb(struct rt2870_softc
*sc
, void *arg
)
2753 struct ieee80211com
*ic
;
2755 struct ieee80211vap
*vap
;
2756 struct ieee80211_key
*k
;
2757 struct rt2870_cmd_argv_keyset
*cmd_argv
;
2758 uint16_t associd
, key_base
, keymode_base
;
2759 uint8_t mode
, vapid
, wcid
, iv
[8];
2764 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
2767 associd
= cmd_argv
->associd
;
2769 switch (k
->wk_cipher
->ic_cipher
)
2771 case IEEE80211_CIPHER_WEP
:
2772 if(k
->wk_keylen
< 8)
2773 mode
= RT2870_REG_CIPHER_MODE_WEP40
;
2775 mode
= RT2870_REG_CIPHER_MODE_WEP104
;
2778 case IEEE80211_CIPHER_TKIP
:
2779 mode
= RT2870_REG_CIPHER_MODE_TKIP
;
2782 case IEEE80211_CIPHER_AES_CCM
:
2783 mode
= RT2870_REG_CIPHER_MODE_AES_CCMP
;
2790 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2791 "%s: VAP key set: keyix=%d, keylen=%d, macaddr=%s, associd=0x%04x, mode=%d, group=%d\n",
2792 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, ether_sprintf(k
->wk_macaddr
),
2793 associd
, mode
, (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2795 RT2870_SOFTC_LOCK(sc
);
2797 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2799 /* install pairwise key */
2803 wcid
= RT2870_AID2WCID(associd
);
2807 key_base
= RT2870_REG_PKEY(wcid
);
2809 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
)
2813 iv
[3] = (k
->wk_keyix
<< 6);
2817 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2819 iv
[0] = (k
->wk_keytsc
>> 8);
2820 iv
[1] = ((iv
[0] | 0x20) & 0x7f);
2821 iv
[2] = k
->wk_keytsc
;
2827 iv
[0] = k
->wk_keytsc
;
2828 iv
[1] = k
->wk_keytsc
>> 8;
2832 iv
[3] = ((k
->wk_keyix
<< 6) | IEEE80211_WEP_EXTIV
);
2833 iv
[4] = (k
->wk_keytsc
>> 16);
2834 iv
[5] = (k
->wk_keytsc
>> 24);
2835 iv
[6] = (k
->wk_keytsc
>> 32);
2836 iv
[7] = (k
->wk_keytsc
>> 40);
2838 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2839 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2840 device_get_nameunit(sc
->dev
),
2841 iv
[0], iv
[1], iv
[2], iv
[3], iv
[4], iv
[5], iv
[6], iv
[7]);
2844 rt2870_io_mac_write_multi(sc
, RT2870_REG_IVEIV(wcid
), iv
, 8);
2846 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2848 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2850 if (vap
->iv_opmode
!= IEEE80211_M_HOSTAP
)
2852 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2853 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2857 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2858 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2863 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2866 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2867 (mode
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2869 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2872 if ((k
->wk_flags
& IEEE80211_KEY_GROUP
) ||
2873 (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
))
2875 /* install group key */
2878 wcid
= 0; /* mcast/bcast WCID */
2879 key_base
= RT2870_REG_SKEY(vapid
, k
->wk_keyix
);
2880 keymode_base
= RT2870_REG_SKEY_MODE(vapid
);
2882 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2884 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2886 if (vap
->iv_opmode
!= IEEE80211_M_HOSTAP
)
2888 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2889 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2893 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2894 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2899 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2902 tmp
= rt2870_io_mac_read(sc
, keymode_base
);
2904 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2905 tmp
|= (mode
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2907 rt2870_io_mac_write(sc
, keymode_base
, tmp
);
2909 if (vap
->iv_opmode
== IEEE80211_M_HOSTAP
)
2911 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
)
2915 iv
[3] = (k
->wk_keyix
<< 6);
2919 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2921 iv
[0] = (k
->wk_keytsc
>> 8);
2922 iv
[1] = ((iv
[0] | 0x20) & 0x7f);
2923 iv
[2] = k
->wk_keytsc
;
2929 iv
[0] = k
->wk_keytsc
;
2930 iv
[1] = k
->wk_keytsc
>> 8;
2934 iv
[3] = ((k
->wk_keyix
<< 6) | IEEE80211_WEP_EXTIV
);
2935 iv
[4] = (k
->wk_keytsc
>> 16);
2936 iv
[5] = (k
->wk_keytsc
>> 24);
2937 iv
[6] = (k
->wk_keytsc
>> 32);
2938 iv
[7] = (k
->wk_keytsc
>> 40);
2940 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2941 "%s: VAP key set: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2942 device_get_nameunit(sc
->dev
),
2943 iv
[0], iv
[1], iv
[2], iv
[3], iv
[4], iv
[5], iv
[6], iv
[7]);
2946 rt2870_io_mac_write_multi(sc
, RT2870_REG_IVEIV(wcid
), iv
, 8);
2948 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2949 (mode
<< RT2870_REG_CIPHER_MODE_SHIFT
);
2951 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2955 RT2870_SOFTC_UNLOCK(sc
);
2959 * rt2870_vap_key_delete_cb
2961 static void rt2870_vap_key_delete_cb(struct rt2870_softc
*sc
, void *arg
)
2963 struct ieee80211com
*ic
;
2965 struct ieee80211vap
*vap
;
2966 struct ieee80211_key
*k
;
2967 struct rt2870_cmd_argv_keydelete
*cmd_argv
;
2969 uint8_t vapid
, wcid
;
2974 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
2977 associd
= cmd_argv
->associd
;
2979 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2980 "%s: VAP key delete: keyix=%d, keylen=%d, macaddr=%s, associd=0x%04x, group=%d\n",
2981 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, ether_sprintf(k
->wk_macaddr
),
2982 associd
, (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2984 RT2870_SOFTC_LOCK(sc
);
2986 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2988 /* remove pairwise key */
2992 wcid
= RT2870_AID2WCID(associd
);
2996 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2997 (RT2870_REG_CIPHER_MODE_NONE
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2999 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
3003 /* remove group key */
3006 wcid
= 0; /* mcast/bcast WCID */
3008 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SKEY_MODE(vapid
));
3010 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
3011 tmp
|= (RT2870_REG_CIPHER_MODE_NONE
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
3013 rt2870_io_mac_write(sc
, RT2870_REG_SKEY_MODE(vapid
), tmp
);
3015 if (vap
->iv_opmode
== IEEE80211_M_HOSTAP
)
3017 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
3018 (RT2870_REG_CIPHER_MODE_NONE
<< RT2870_REG_CIPHER_MODE_SHIFT
);
3020 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
3024 RT2870_SOFTC_UNLOCK(sc
);
3028 * rt2870_vap_update_beacon_cb
3030 static void rt2870_vap_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
)
3033 struct ieee80211com
*ic
;
3034 struct ieee80211vap
*vap
;
3038 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
3043 RT2870_SOFTC_LOCK(sc
);
3045 rt2870_asic_update_beacon(sc
, vap
);
3047 RT2870_SOFTC_UNLOCK(sc
);
3051 * rt2870_asic_set_bssid
3053 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
3054 const uint8_t *bssid
)
3058 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3059 "%s: set bssid: bssid=%s\n",
3060 device_get_nameunit(sc
->dev
),
3061 ether_sprintf(bssid
));
3063 tmp
= bssid
[0] | (bssid
[1]) << 8 | (bssid
[2] << 16) | (bssid
[3] << 24);
3065 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW0
, tmp
);
3067 tmp
= bssid
[4] | (bssid
[5] << 8);
3069 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW1
, tmp
);
3073 * rt2870_asic_set_macaddr
3075 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
3076 const uint8_t *addr
)
3080 tmp
= addr
[0] | (addr
[1] << 8) | (addr
[2] << 16) | (addr
[3] << 24);
3082 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW0
, tmp
);
3084 tmp
= addr
[4] | (addr
[5] << 8);
3086 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW1
, tmp
);
3090 * rt2870_asic_enable_tsf_sync
3092 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
)
3095 struct ieee80211com
*ic
;
3096 struct ieee80211vap
*vap
;
3101 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
3103 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
3104 "%s: enabling TSF\n",
3105 device_get_nameunit(sc
->dev
));
3107 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3110 tmp
|= vap
->iv_bss
->ni_intval
* 16;
3111 tmp
|= (RT2870_REG_TSF_TIMER_ENABLE
| RT2870_REG_TBTT_TIMER_ENABLE
);
3113 if (vap
->iv_opmode
== IEEE80211_M_STA
)
3115 tmp
|= (RT2870_REG_TSF_SYNC_MODE_STA
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3117 else if (vap
->iv_opmode
== IEEE80211_M_IBSS
)
3119 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
3120 tmp
|= (RT2870_REG_TSF_SYNC_MODE_IBSS
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3122 else if (vap
->iv_opmode
== IEEE80211_M_HOSTAP
)
3124 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
3125 tmp
|= (RT2870_REG_TSF_SYNC_MODE_HOSTAP
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3128 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3132 * rt2870_asic_disable_tsf_sync
3134 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
)
3138 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
3139 "%s: disabling TSF\n",
3140 device_get_nameunit(sc
->dev
));
3142 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3144 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
3145 RT2870_REG_TSF_TIMER_ENABLE
|
3146 RT2870_REG_TBTT_TIMER_ENABLE
);
3148 tmp
&= ~(RT2870_REG_TSF_SYNC_MODE_MASK
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3149 tmp
|= (RT2870_REG_TSF_SYNC_MODE_DISABLE
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
3151 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3155 * rt2870_asic_enable_mrr
3157 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
)
3159 #define CCK(mcs) (mcs)
3160 #define OFDM(mcs) ((1 << 3) | (mcs))
3161 #define HT(mcs) (mcs)
3163 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG0
,
3164 (OFDM(6) << 28) | /* 54 -> 48 */
3165 (OFDM(5) << 24) | /* 48 -> 36 */
3166 (OFDM(4) << 20) | /* 36 -> 24 */
3167 (OFDM(3) << 16) | /* 24 -> 18 */
3168 (OFDM(2) << 12) | /* 18 -> 12 */
3169 (OFDM(1) << 8) | /* 12 -> 9 */
3170 (OFDM(0) << 4) | /* 9 -> 6 */
3171 OFDM(0)); /* 6 -> 6 */
3173 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG1
,
3174 (CCK(2) << 12) | /* 11 -> 5.5 */
3175 (CCK(1) << 8) | /* 5.5 -> 2 */
3176 (CCK(0) << 4) | /* 2 -> 1 */
3177 CCK(0)); /* 1 -> 1 */
3179 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG0
,
3189 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG1
,
3205 * rt2870_asic_set_txpreamble
3207 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
)
3210 struct ieee80211com
*ic
;
3216 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3217 "%s: %s short Tx preamble\n",
3218 device_get_nameunit(sc
->dev
),
3219 (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) ? "enabling" : "disabling");
3221 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_AUTO_RSP_CFG
);
3223 tmp
&= ~RT2870_REG_CCK_SHORT_ENABLE
;
3225 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
3226 tmp
|= RT2870_REG_CCK_SHORT_ENABLE
;
3228 rt2870_io_mac_write(sc
, RT2870_REG_AUTO_RSP_CFG
, tmp
);
3232 * rt2870_asic_set_basicrates
3234 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
)
3237 struct ieee80211com
*ic
;
3242 if (ic
->ic_curmode
== IEEE80211_MODE_11B
)
3243 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x3);
3244 else if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3245 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x150);
3247 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x15f);
3251 * rt2870_asic_update_rtsthreshold
3253 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
)
3256 struct ieee80211com
*ic
;
3257 struct ieee80211vap
*vap
;
3263 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
3266 threshold
= IEEE80211_RTS_MAX
;
3267 else if (vap
->iv_flags_ht
& IEEE80211_FHT_AMSDU_TX
)
3270 threshold
= vap
->iv_rtsthreshold
;
3272 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3273 "%s: updating RTS threshold: %d\n",
3274 device_get_nameunit(sc
->dev
), threshold
);
3276 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_RTS_CFG
);
3278 tmp
&= ~(RT2870_REG_TX_RTS_THRESHOLD_MASK
<< RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3280 tmp
|= ((threshold
& RT2870_REG_TX_RTS_THRESHOLD_MASK
) <<
3281 RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3283 rt2870_io_mac_write(sc
, RT2870_REG_TX_RTS_CFG
, tmp
);
3287 * rt2870_asic_update_txpower
3289 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
)
3292 struct ieee80211com
*ic
;
3293 uint32_t *txpow_rate
;
3302 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3303 "%s: updating Tx power: %d\n",
3304 device_get_nameunit(sc
->dev
), ic
->ic_txpowlimit
);
3306 if (!IEEE80211_IS_CHAN_HT40(ic
->ic_curchan
))
3308 txpow_rate
= sc
->txpow_rate_20mhz
;
3312 if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_curchan
))
3313 txpow_rate
= sc
->txpow_rate_40mhz_2ghz
;
3315 txpow_rate
= sc
->txpow_rate_40mhz_5ghz
;
3320 val
= rt2870_io_bbp_read(sc
, 1);
3323 if (ic
->ic_txpowlimit
> 90)
3327 else if (ic
->ic_txpowlimit
> 60)
3331 else if (ic
->ic_txpowlimit
> 30)
3335 else if (ic
->ic_txpowlimit
> 15)
3339 else if (ic
->ic_txpowlimit
> 9)
3349 rt2870_io_bbp_write(sc
, 1, val
);
3351 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
3353 if (txpow_rate
[i
] == 0xffffffff)
3356 tmp
= rt2870_read_eeprom_txpow_rate_add_delta(txpow_rate
[i
], delta
);
3358 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
), tmp
);
3363 * rt2870_asic_update_promisc
3365 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
)
3372 printf("%s: %s promiscuous mode\n",
3373 device_get_nameunit(sc
->dev
),
3374 (ifp
->if_flags
& IFF_PROMISC
) ? "entering" : "leaving");
3376 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_FILTER_CFG
);
3378 tmp
&= ~RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3380 if (!(ifp
->if_flags
& IFF_PROMISC
))
3381 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3383 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
3387 * rt2870_asic_updateprot
3389 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
)
3392 struct ieee80211com
*ic
;
3393 struct ieee80211vap
*vap
;
3394 uint32_t cck_prot
, ofdm_prot
, mm20_prot
, mm40_prot
, gf20_prot
, gf40_prot
;
3396 enum ieee80211_protmode htprotmode
;
3400 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
3402 /* CCK frame protection */
3404 cck_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3405 RT2870_REG_TXOP_ALLOW_ALL
| RT2870_REG_PROT_CTRL_NONE
;
3407 /* set up protection frame phy mode and rate (MCS code) */
3409 if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3410 cck_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3411 (0 << RT2870_REG_PROT_MCS_SHIFT
);
3413 cck_prot
|= ((RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3414 (3 << RT2870_REG_PROT_MCS_SHIFT
));
3416 rt2870_io_mac_write(sc
, RT2870_REG_TX_CCK_PROT_CFG
, cck_prot
);
3418 /* OFDM frame protection */
3420 ofdm_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3421 RT2870_REG_TXOP_ALLOW_ALL
;
3423 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3425 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3426 "%s: updating protection mode: b/g protection mode=%s\n",
3427 device_get_nameunit(sc
->dev
),
3428 (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3429 ((ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3431 if (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
)
3432 ofdm_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3433 else if (ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
)
3434 ofdm_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3436 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3440 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3441 "%s: updating protection mode: b/g protection mode=%s\n",
3442 device_get_nameunit(sc
->dev
), "none");
3444 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3447 rt2870_io_mac_write(sc
, RT2870_REG_TX_OFDM_PROT_CFG
, ofdm_prot
);
3449 /* HT frame protection */
3451 if ((vap
!= NULL
) && (vap
->iv_opmode
== IEEE80211_M_STA
) && (vap
->iv_state
== IEEE80211_S_RUN
))
3452 htopmode
= vap
->iv_bss
->ni_htopmode
;
3454 htopmode
= ic
->ic_curhtprotmode
;
3456 htprotmode
= ic
->ic_htprotmode
;
3458 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3460 if (sc
->tx_ampdu_sessions
> 0)
3462 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3463 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3464 device_get_nameunit(sc
->dev
));
3466 htopmode
= IEEE80211_HTINFO_OPMODE_MIXED
;
3467 htprotmode
= IEEE80211_PROT_RTSCTS
;
3470 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3471 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3472 device_get_nameunit(sc
->dev
),
3473 htopmode
& IEEE80211_HTINFO_OPMODE
,
3474 (htprotmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3475 ((htprotmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3477 switch (htopmode
& IEEE80211_HTINFO_OPMODE
)
3479 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3480 case IEEE80211_HTINFO_OPMODE_HT20PR
:
3481 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3482 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3483 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3484 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3485 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3487 gf20_prot
= mm20_prot
;
3489 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3490 RT2870_REG_TXOP_ALLOW_ALL
|
3491 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3492 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3494 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3495 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3496 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3497 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3499 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3501 gf40_prot
= mm40_prot
;
3504 /* IEEE80211_HTINFO_OPMODE_MIXED */
3505 case IEEE80211_HTINFO_OPMODE_MIXED
:
3506 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
|
3507 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3508 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
;
3510 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3511 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3512 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3514 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3515 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3517 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3518 mm20_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3519 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3520 mm20_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3522 mm20_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3524 gf20_prot
= mm20_prot
;
3526 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_TXOP_ALLOW_ALL
;
3528 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3529 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3530 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3532 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3533 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3535 if (htprotmode
== IEEE80211_PROT_RTSCTS
)
3536 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3537 else if (htprotmode
== IEEE80211_PROT_CTSONLY
)
3538 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3540 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3542 gf40_prot
= mm40_prot
;
3546 * IEEE80211_HTINFO_OPMODE_PURE
3547 * IEEE80211_HTINFO_OPMODE_PROTOPT
3549 case IEEE80211_HTINFO_OPMODE_PURE
:
3550 case IEEE80211_HTINFO_OPMODE_PROTOPT
:
3552 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3553 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3554 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3555 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3556 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3558 gf20_prot
= mm20_prot
;
3560 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3561 RT2870_REG_TXOP_ALLOW_ALL
|
3562 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3563 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3565 gf40_prot
= mm40_prot
;
3569 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM20_PROT_CFG
, mm20_prot
);
3570 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM40_PROT_CFG
, mm40_prot
);
3571 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF20_PROT_CFG
, gf20_prot
);
3572 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF40_PROT_CFG
, gf40_prot
);
3576 * rt2870_asic_updateslot
3578 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
)
3581 struct ieee80211com
*ic
;
3582 struct ieee80211vap
*vap
;
3587 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
3589 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3590 "%s: %s short slot time\n",
3591 device_get_nameunit(sc
->dev
),
3592 ((ic
->ic_flags
& IEEE80211_F_SHSLOT
) ||
3593 ((vap
!= NULL
) && (vap
->iv_flags
& IEEE80211_F_BURST
))) ? "enabling" : "disabling");
3595 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BKOFF_SLOT_CFG
);
3599 if ((ic
->ic_flags
& IEEE80211_F_SHSLOT
) ||
3600 ((vap
!= NULL
) && (vap
->iv_flags
& IEEE80211_F_BURST
)))
3601 tmp
|= IEEE80211_DUR_SHSLOT
;
3603 tmp
|= IEEE80211_DUR_SLOT
;
3605 rt2870_io_mac_write(sc
, RT2870_REG_BKOFF_SLOT_CFG
, tmp
);
3609 * rt2870_asic_wme_update
3611 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
)
3614 struct ieee80211com
*ic
;
3615 struct ieee80211_wme_state
*wme
;
3616 const struct wmeParams
*wmep
;
3622 wmep
= wme
->wme_chanParams
.cap_wmeParams
;
3624 RT2870_DPRINTF(sc
, RT2870_DEBUG_WME
,
3625 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3626 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3627 device_get_nameunit(sc
->dev
),
3628 wmep
[WME_AC_VO
].wmep_aifsn
,
3629 wmep
[WME_AC_VO
].wmep_logcwmin
, wmep
[WME_AC_VO
].wmep_logcwmax
,
3630 wmep
[WME_AC_VO
].wmep_txopLimit
,
3631 wmep
[WME_AC_VI
].wmep_aifsn
,
3632 wmep
[WME_AC_VI
].wmep_logcwmin
, wmep
[WME_AC_VI
].wmep_logcwmax
,
3633 wmep
[WME_AC_VI
].wmep_txopLimit
,
3634 wmep
[WME_AC_BK
].wmep_aifsn
,
3635 wmep
[WME_AC_BK
].wmep_logcwmin
, wmep
[WME_AC_BK
].wmep_logcwmax
,
3636 wmep
[WME_AC_BK
].wmep_txopLimit
,
3637 wmep
[WME_AC_BE
].wmep_aifsn
,
3638 wmep
[WME_AC_BE
].wmep_logcwmin
, wmep
[WME_AC_BE
].wmep_logcwmax
,
3639 wmep
[WME_AC_BE
].wmep_txopLimit
);
3641 for (i
= 0; i
< WME_NUM_AC
; i
++)
3642 rt2870_io_mac_write(sc
, RT2870_REG_TX_EDCA_AC_CFG(i
),
3643 (wmep
[i
].wmep_logcwmax
<< 16) | (wmep
[i
].wmep_logcwmin
<< 12) |
3644 (wmep
[i
].wmep_aifsn
<< 8) | wmep
[i
].wmep_txopLimit
);
3646 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_AIFSN_CFG
,
3647 (wmep
[WME_AC_VO
].wmep_aifsn
<< 12) | (wmep
[WME_AC_VI
].wmep_aifsn
<< 8) |
3648 (wmep
[WME_AC_BK
].wmep_aifsn
<< 4) | wmep
[WME_AC_BE
].wmep_aifsn
);
3650 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMIN_CFG
,
3651 (wmep
[WME_AC_VO
].wmep_logcwmin
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmin
<< 8) |
3652 (wmep
[WME_AC_BK
].wmep_logcwmin
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmin
);
3654 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMAX_CFG
,
3655 (wmep
[WME_AC_VO
].wmep_logcwmax
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmax
<< 8) |
3656 (wmep
[WME_AC_BK
].wmep_logcwmax
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmax
);
3658 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
,
3659 (wmep
[WME_AC_BK
].wmep_txopLimit
<< 16) | wmep
[WME_AC_BE
].wmep_txopLimit
);
3661 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
3662 (wmep
[WME_AC_VO
].wmep_txopLimit
<< 16) | wmep
[WME_AC_VI
].wmep_txopLimit
);
3666 * rt2870_asic_update_beacon
3668 static void rt2870_asic_update_beacon(struct rt2870_softc
*sc
,
3669 struct ieee80211vap
*vap
)
3671 struct rt2870_softc_vap
*rvap
;
3673 struct rt2870_txwi
*txwi
;
3676 rvap
= (struct rt2870_softc_vap
*) vap
;
3678 m
= rvap
->beacon_mbuf
;
3679 txwi
= &rvap
->beacon_txwi
;
3681 /* disable temporarily TSF sync */
3683 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3685 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
3686 RT2870_REG_TSF_TIMER_ENABLE
|
3687 RT2870_REG_TBTT_TIMER_ENABLE
);
3689 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3691 /* write Tx wireless info and beacon frame to on-chip memory */
3693 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0),
3694 txwi
, sizeof(struct rt2870_txwi
));
3696 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0) + sizeof(struct rt2870_txwi
),
3697 mtod(m
, uint8_t *), m
->m_pkthdr
.len
);
3699 /* enable again TSF sync */
3701 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3703 tmp
|= (RT2870_REG_BCN_TX_ENABLE
|
3704 RT2870_REG_TSF_TIMER_ENABLE
|
3705 RT2870_REG_TBTT_TIMER_ENABLE
);
3707 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3711 * rt2870_asic_clear_keytables
3713 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
)
3717 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3719 for (i
= 0; i
< 256; i
++)
3721 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
), 0xffffffff);
3722 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
) + 4, 0x0000ffff);
3725 /* clear WCID attribute table (entries = 256, entry size = 4) */
3727 rt2870_io_mac_set_region_4(sc
, RT2870_REG_WCID_ATTR(0), 0, 256);
3729 /* clear IV/EIV table (entries = 256, entry size = 8) */
3731 rt2870_io_mac_set_region_4(sc
, RT2870_REG_IVEIV(0), 0, 2 * 256);
3733 /* clear pairwise key table (entries = 256, entry size = 32) */
3735 rt2870_io_mac_set_region_4(sc
, RT2870_REG_PKEY(0), 0, 8 * 256);
3737 /* clear shared key table (entries = 32, entry size = 32) */
3739 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY(0, 0), 0, 8 * 32);
3741 /* clear shared key mode (entries = 32, entry size = 2) */
3743 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY_MODE(0), 0, 16);
3747 * rt2870_beacon_alloc
3749 static int rt2870_beacon_alloc(struct rt2870_softc
*sc
,
3750 struct ieee80211vap
*vap
)
3752 struct ieee80211com
*ic
;
3753 struct rt2870_softc_vap
*rvap
;
3754 const struct ieee80211_txparam
*tp
;
3756 struct rt2870_txwi txwi
;
3760 rvap
= (struct rt2870_softc_vap
*) vap
;
3761 tp
= &vap
->iv_txparms
[ieee80211_chan2mode(ic
->ic_bsschan
)];
3763 m
= ieee80211_beacon_alloc(vap
->iv_bss
, &rvap
->beacon_offsets
);
3767 rate
= tp
->mgmtrate
;
3768 mcs
= rt2870_rate2mcs(rate
);
3770 memset(&txwi
, 0, sizeof(struct rt2870_txwi
));
3773 txwi
.pid_mpdu_len
= ((htole16(m
->m_pkthdr
.len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
3774 RT2870_TXWI_MPDU_LEN_SHIFT
);
3775 txwi
.txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
3776 txwi
.mpdu_density_flags
|=
3777 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
3778 txwi
.bawin_size_xflags
|=
3779 (RT2870_TXWI_XFLAGS_NSEQ
<< RT2870_TXWI_XFLAGS_SHIFT
);
3781 if (ieee80211_rate2phytype(ic
->ic_rt
, rate
) != IEEE80211_T_OFDM
)
3783 txwi
.phymode_ifs_stbc_shortgi
=
3784 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
3786 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
3787 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
3791 txwi
.phymode_ifs_stbc_shortgi
=
3792 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
3795 txwi
.bw_mcs
= (RT2870_TXWI_BW_20
<< RT2870_TXWI_BW_SHIFT
) |
3796 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
3798 if (rvap
->beacon_mbuf
!= NULL
)
3800 m_free(rvap
->beacon_mbuf
);
3801 rvap
->beacon_mbuf
= NULL
;
3804 rvap
->beacon_mbuf
= m
;
3805 rvap
->beacon_txwi
= txwi
;
3813 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
)
3815 uint8_t mcs
, phymode
;
3818 mcs
= (rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
;
3819 phymode
= (rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
3820 RT2870_RXWI_PHYMODE_MASK
;
3826 case RT2870_RXWI_PHYMODE_CCK
:
3827 switch (mcs
& ~RT2870_RXWI_MCS_SHOTPRE
)
3829 case 0: rate
= 2; break; /* 1 Mbps */
3830 case 1: rate
= 4; break; /* 2 MBps */
3831 case 2: rate
= 11; break; /* 5.5 Mbps */
3832 case 3: rate
= 22; break; /* 11 Mbps */
3836 case RT2870_RXWI_PHYMODE_OFDM
:
3839 case 0: rate
= 12; break; /* 6 Mbps */
3840 case 1: rate
= 18; break; /* 9 Mbps */
3841 case 2: rate
= 24; break; /* 12 Mbps */
3842 case 3: rate
= 36; break; /* 18 Mbps */
3843 case 4: rate
= 48; break; /* 24 Mbps */
3844 case 5: rate
= 72; break; /* 36 Mbps */
3845 case 6: rate
= 96; break; /* 48 Mbps */
3846 case 7: rate
= 108; break; /* 54 Mbps */
3850 case RT2870_RXWI_PHYMODE_HT_MIXED
:
3851 case RT2870_RXWI_PHYMODE_HT_GF
:
3859 * rt2870_maxrssi_rxpath
3861 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
3862 const struct rt2870_rxwi
*rxwi
)
3868 if (sc
->nrxpath
> 1)
3869 if (rxwi
->rssi
[1] > rxwi
->rssi
[rxpath
])
3872 if (sc
->nrxpath
> 2)
3873 if (rxwi
->rssi
[2] > rxwi
->rssi
[rxpath
])
3882 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
3883 uint8_t rssi
, uint8_t rxpath
)
3886 struct ieee80211com
*ic
;
3887 struct ieee80211_channel
*c
;
3889 int8_t rssi_off
, lna_gain
;
3897 chan
= ieee80211_chan2ieee(ic
, c
);
3899 if (IEEE80211_IS_CHAN_5GHZ(c
))
3901 rssi_off
= sc
->rssi_off_5ghz
[rxpath
];
3904 lna_gain
= sc
->lna_gain
[1];
3905 else if (chan
<= 128)
3906 lna_gain
= sc
->lna_gain
[2];
3908 lna_gain
= sc
->lna_gain
[3];
3912 rssi_off
= sc
->rssi_off_2ghz
[rxpath
];
3913 lna_gain
= sc
->lna_gain
[0];
3916 return (-12 - rssi_off
- lna_gain
- rssi
);
3922 static uint8_t rt2870_rate2mcs(uint8_t rate
)
3949 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
3950 uint8_t *buf
, uint32_t dmalen
)
3953 struct ieee80211com
*ic
;
3954 struct ieee80211_frame
*wh
;
3955 struct ieee80211_node
*ni
;
3956 struct rt2870_softc_node
*rni
;
3957 struct rt2870_softc_rx_radiotap_header
*tap
;
3958 struct rt2870_rxwi
*rxwi
;
3959 struct rt2870_rxinfo
*rxinfo
;
3961 uint32_t rxinfo_flags
;
3962 uint8_t cipher_err
, rssi
, ant
, phymode
, bw
, shortgi
, mcs
, keyidx
;
3964 int len
, ampdu
, amsdu
, rssi_dbm_rel
, i
;
3969 /* get Rx wireless info */
3971 rxwi
= (struct rt2870_rxwi
*) buf
;
3972 len
= (le16toh(rxwi
->tid_size
) >> RT2870_RXWI_SIZE_SHIFT
) &
3973 RT2870_RXWI_SIZE_MASK
;
3977 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3978 "%s: bad rxwi len: DMA len=%d, rxwi len=%d\n",
3979 device_get_nameunit(sc
->dev
), dmalen
, len
);
3985 rxinfo
= (struct rt2870_rxinfo
*) ((caddr_t
) rxwi
+ dmalen
);
3986 rxinfo_flags
= le32toh(rxinfo
->flags
);
3988 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3989 "%s: Rx frame: DMA len=%d, len=%d, rxinfo flags=0x%08x\n",
3990 device_get_nameunit(sc
->dev
), dmalen
, len
, rxinfo_flags
);
3992 /* check for crc errors */
3994 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_CRC_ERR
)
3996 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3997 "%s: rxinfo: crc error\n",
3998 device_get_nameunit(sc
->dev
));
4002 if (!(ifp
->if_flags
& IFF_PROMISC
))
4006 wh
= (struct ieee80211_frame
*) (rxwi
+ 1);
4008 /* check for L2 padding between IEEE 802.11 frame header and body */
4010 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_L2PAD
)
4012 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4013 "%s: L2 padding: DMA len=%d, len=%d\n",
4014 device_get_nameunit(sc
->dev
), dmalen
, len
);
4019 m
= m_getjcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
, MJUMPAGESIZE
);
4022 sc
->rx_mbuf_alloc_errors
++;
4027 m
->m_pkthdr
.rcvif
= ifp
;
4028 m
->m_pkthdr
.len
= m
->m_len
= len
;
4030 m_copyback(m
, 0, len
, (caddr_t
) wh
);
4032 wh
= mtod(m
, struct ieee80211_frame
*);
4034 /* check for cipher errors */
4036 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_DECRYPTED
)
4038 cipher_err
= ((rxinfo_flags
>> RT2870_RXINFO_FLAGS_CIPHER_ERR_SHIFT
) &
4039 RT2870_RXINFO_FLAGS_CIPHER_ERR_MASK
);
4040 if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_NONE
)
4042 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4043 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4045 m
->m_flags
|= M_WEP
;
4047 sc
->rx_cipher_no_errors
++;
4051 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4052 "%s: rxinfo: cipher error=0x%02x\n",
4053 device_get_nameunit(sc
->dev
), cipher_err
);
4055 if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_ICV
)
4056 sc
->rx_cipher_icv_errors
++;
4057 else if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_MIC
)
4058 sc
->rx_cipher_mic_errors
++;
4059 else if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_INVALID_KEY
)
4060 sc
->rx_cipher_invalid_key_errors
++;
4062 if ((cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_MIC
) &&
4063 (rxinfo_flags
& RT2870_RXINFO_FLAGS_MYBSS
))
4065 ni
= ieee80211_find_rxnode(ic
, (const struct ieee80211_frame_min
*) wh
);
4068 keyidx
= (rxwi
->udf_bssidx_keyidx
>> RT2870_RXWI_KEYIDX_SHIFT
) &
4069 RT2870_RXWI_KEYIDX_MASK
;
4071 ieee80211_notify_michael_failure(ni
->ni_vap
, wh
, keyidx
);
4073 ieee80211_free_node(ni
);
4079 if (!(ifp
->if_flags
& IFF_PROMISC
))
4088 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4090 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4091 "%s: rxinfo: not decrypted but protected flag set\n",
4092 device_get_nameunit(sc
->dev
));
4096 if (!(ifp
->if_flags
& IFF_PROMISC
))
4104 /* check for A-MPDU */
4106 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMPDU
)
4108 m
->m_flags
|= M_AMPDU
;
4119 /* check for A-MSDU */
4121 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMSDU
)
4132 ant
= rt2870_maxrssi_rxpath(sc
, rxwi
);
4133 rssi
= rxwi
->rssi
[ant
];
4134 rssi_dbm
= rt2870_rssi2dbm(sc
, rssi
, ant
);
4135 phymode
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
4136 RT2870_RXWI_PHYMODE_MASK
);
4137 bw
= ((rxwi
->bw_mcs
>> RT2870_RXWI_BW_SHIFT
) & RT2870_RXWI_BW_MASK
);
4138 shortgi
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_SHORTGI_SHIFT
) &
4139 RT2870_RXWI_SHORTGI_MASK
);
4140 mcs
= ((rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
);
4142 if (ieee80211_radiotap_active(ic
))
4146 tap
->flags
= (rxinfo_flags
& RT2870_RXINFO_FLAGS_L2PAD
) ? IEEE80211_RADIOTAP_F_DATAPAD
: 0;
4147 tap
->dbm_antsignal
= rssi_dbm
;
4148 tap
->dbm_antnoise
= RT2870_NOISE_FLOOR
;
4150 tap
->antsignal
= rssi
;
4151 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4152 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4153 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4154 tap
->chan_maxpow
= 0;
4156 if (phymode
== RT2870_RXWI_PHYMODE_CCK
|| phymode
== RT2870_RXWI_PHYMODE_OFDM
)
4157 tap
->rate
= rt2870_rxrate(rxwi
);
4159 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4161 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_CRC_ERR
)
4162 tap
->flags
|= IEEE80211_RADIOTAP_F_BADFCS
;
4164 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_FRAG
)
4165 tap
->flags
|= IEEE80211_RADIOTAP_F_FRAG
;
4167 if (rxwi
->bw_mcs
& RT2870_RXWI_MCS_SHOTPRE
)
4168 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4170 if ((rxinfo_flags
& RT2870_RXINFO_FLAGS_DECRYPTED
) ||
4171 (wh
->i_fc
[1] & IEEE80211_FC1_WEP
))
4172 tap
->flags
|= IEEE80211_RADIOTAP_F_WEP
;
4175 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4179 * net80211 assumes that RSSI data are in the range [-127..127] and
4180 * in .5 dBm units relative to the current noise floor
4183 rssi_dbm_rel
= (rssi_dbm
- RT2870_NOISE_FLOOR
) * 2;
4184 if (rssi_dbm_rel
> 127)
4187 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4188 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
4189 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d, "
4190 "rssi_dbm=%d, rssi_dbm_rel=%d\n",
4191 device_get_nameunit(sc
->dev
),
4192 len
, phymode
, bw
, shortgi
, mcs
,
4193 ant
, rxwi
->rssi
[0], rxwi
->rssi
[1], rxwi
->rssi
[2],
4194 rxwi
->snr
[0], rxwi
->snr
[1],
4195 rxwi
->wcid
, ampdu
, amsdu
, rssi_dbm
, rssi_dbm_rel
);
4197 ni
= ieee80211_find_rxnode(ic
, (struct ieee80211_frame_min
*) wh
);
4200 rni
= (struct rt2870_softc_node
*) ni
;
4202 for (i
= 0; i
< RT2870_SOFTC_RSSI_COUNT
; i
++)
4204 rni
->last_rssi
[i
] = rxwi
->rssi
[i
];
4205 rni
->last_rssi_dbm
[i
] = rt2870_rssi2dbm(sc
, rxwi
->rssi
[i
], i
);
4208 ieee80211_input(ni
, m
, rssi_dbm_rel
, RT2870_NOISE_FLOOR
);
4209 ieee80211_free_node(ni
);
4213 ieee80211_input_all(ic
, m
, rssi_dbm_rel
, RT2870_NOISE_FLOOR
);
4220 static int rt2870_tx_mgmt(struct rt2870_softc
*sc
,
4221 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
)
4224 struct ieee80211com
*ic
;
4225 struct ieee80211vap
*vap
;
4226 const struct ieee80211_txparam
*tp
;
4227 struct rt2870_softc_node
*rni
;
4228 struct rt2870_softc_tx_ring
*ring
;
4229 struct rt2870_softc_tx_data
*data
;
4230 struct rt2870_txinfo
*txinfo
;
4231 struct rt2870_txwi
*txwi
;
4232 struct ieee80211_frame
*wh
;
4233 struct rt2870_softc_tx_radiotap_header
*tap
;
4234 u_int hdrsize
, hdrspace
;
4235 uint8_t rate
, stbc
, shortgi
, mcs
, pid
;
4236 uint16_t len
, dmalen
, mpdu_len
, dur
;
4239 KASSERT(qid
>= 0 && qid
< (sc
->usb_endpoints
- 1),
4240 ("%s: Tx MGMT: invalid qid=%d\n",
4241 device_get_nameunit(sc
->dev
), qid
));
4246 rni
= (struct rt2870_softc_node
*) ni
;
4247 tp
= ni
->ni_txparms
;
4249 ring
= &sc
->tx_ring
[qid
];
4250 data
= STAILQ_FIRST(&ring
->inactive
);
4251 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
4252 txinfo
= (struct rt2870_txinfo
*) data
->buf
;
4253 txwi
= (struct rt2870_txwi
*) (txinfo
+ 1);
4255 wh
= mtod(m
, struct ieee80211_frame
*);
4257 rate
= tp
->mgmtrate
& IEEE80211_RATE_VAL
;
4259 len
= sizeof(struct rt2870_txinfo
) + sizeof(struct rt2870_txwi
) + m
->m_pkthdr
.len
;
4261 /* align end on a 4-bytes boundary */
4263 dmalen
= (len
+ 3) & ~3;
4267 memset(txinfo
, 0, sizeof(struct rt2870_txinfo
));
4269 txinfo
->len
= htole16(dmalen
- sizeof(struct rt2870_txinfo
));
4271 txinfo
->qsel_flags
= (RT2870_TXINFO_QSEL_EDCA
<< RT2870_TXINFO_QSEL_SHIFT
);
4275 memset((caddr_t
) txinfo
+ len
, 0, dmalen
- len
);
4277 /* fill Tx wireless info */
4279 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4282 mcs
= rt2870_rate2mcs(rate
);
4284 /* calculate MPDU length without padding */
4286 hdrsize
= ieee80211_hdrsize(wh
);
4287 hdrspace
= ieee80211_hdrspace(ic
, wh
);
4288 mpdu_len
= m
->m_pkthdr
.len
- hdrspace
+ hdrsize
;
4290 memset(txwi
, 0, sizeof(struct rt2870_txwi
));
4292 /* management frames do not need encryption */
4296 /* MIMO power save */
4298 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_flags
& IEEE80211_NODE_MIMO_PS
))
4302 if (ni
->ni_flags
& IEEE80211_NODE_MIMO_RTS
)
4304 /* dynamic MIMO power save */
4306 txwi
->mpdu_density_flags
|=
4307 (RT2870_TXWI_FLAGS_MIMOPS
<< RT2870_TXWI_FLAGS_SHIFT
);
4311 /* static MIMO power save */
4326 txwi
->pid_mpdu_len
= ((htole16(pid
) & RT2870_TXWI_PID_MASK
) <<
4327 RT2870_TXWI_PID_SHIFT
) | ((htole16(mpdu_len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
4328 RT2870_TXWI_MPDU_LEN_SHIFT
);
4330 stbc
= (ic
->ic_htcaps
& IEEE80211_HTCAP_TXSTBC
) &&
4331 (ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_htcap
& IEEE80211_HTCAP_RXSTBC
);
4333 shortgi
= (vap
->iv_flags_ht
& (IEEE80211_FHT_SHORTGI20
| IEEE80211_FHT_SHORTGI40
)) &&
4334 (ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_flags
& (IEEE80211_NODE_SGI20
| IEEE80211_NODE_SGI40
));
4336 txwi
->phymode_ifs_stbc_shortgi
|=
4337 ((stbc
& RT2870_TXWI_STBC_MASK
) << RT2870_TXWI_STBC_SHIFT
) |
4338 ((shortgi
& RT2870_TXWI_SHORTGI_MASK
) << RT2870_TXWI_SHORTGI_SHIFT
);
4340 txwi
->phymode_ifs_stbc_shortgi
|=
4341 ((shortgi
& RT2870_TXWI_SHORTGI_MASK
) << RT2870_TXWI_SHORTGI_SHIFT
);
4343 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4345 txwi
->phymode_ifs_stbc_shortgi
|=
4346 (RT2870_TXWI_PHYMODE_HT_MIXED
<< RT2870_TXWI_PHYMODE_SHIFT
);
4350 if (ieee80211_rate2phytype(ic
->ic_rt
, rate
) != IEEE80211_T_OFDM
)
4352 txwi
->phymode_ifs_stbc_shortgi
|=
4353 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
4355 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
4356 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
4360 txwi
->phymode_ifs_stbc_shortgi
|=
4361 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
4365 txwi
->bw_mcs
= (RT2870_TXWI_BW_20
<< RT2870_TXWI_BW_SHIFT
) |
4366 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
4368 txwi
->txop
= (RT2870_TXWI_TXOP_BACKOFF
<< RT2870_TXWI_TXOP_SHIFT
);
4370 /* skip ACKs for multicast frames */
4372 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
))
4374 txwi
->bawin_size_xflags
|=
4375 (RT2870_TXWI_XFLAGS_ACK
<< RT2870_TXWI_XFLAGS_SHIFT
);
4377 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4379 /* preamble + plcp + signal extension + SIFS */
4381 dur
= 16 + 4 + 6 + 10;
4385 dur
= ieee80211_ack_duration(ic
->ic_rt
, rate
,
4386 ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
);
4389 *(uint16_t *) wh
->i_dur
= htole16(dur
);
4392 /* ask MAC to insert timestamp into probe responses */
4394 if ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
4395 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
4396 txwi
->mpdu_density_flags
|=
4397 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
4399 if (ieee80211_radiotap_active_vap(vap
))
4403 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
4404 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4405 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4406 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4407 tap
->chan_maxpow
= 0;
4409 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4410 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4414 if (mcs
& RT2870_TXWI_MCS_SHOTPRE
)
4415 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4418 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4420 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4421 tap
->flags
|= IEEE80211_RADIOTAP_F_WEP
;
4423 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4425 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4427 ieee80211_radiotap_tx(vap
, m
);
4429 wh
->i_fc
[1] |= IEEE80211_FC1_WEP
;
4433 ieee80211_radiotap_tx(vap
, m
);
4437 m_copydata(m
, 0, m
->m_pkthdr
.len
, (caddr_t
) (txwi
+ 1));
4439 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4440 "%s: sending MGMT frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4441 "stbc=%d, shortgi=%d, mcs=%d, mimops=%d, DMA len=%d\n",
4442 device_get_nameunit(sc
->dev
),
4443 qid
, hdrsize
, hdrspace
, m
->m_pkthdr
.len
, stbc
, shortgi
, mcs
, mimops
, dmalen
);
4449 STAILQ_INSERT_TAIL(&ring
->pending
, data
, next
);
4452 usbd_transfer_start(sc
->usb_xfer
[1 + qid
]);
4460 static int rt2870_tx_data(struct rt2870_softc
*sc
,
4461 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
)
4464 struct ieee80211com
*ic
;
4465 struct ieee80211vap
*vap
;
4466 const struct ieee80211_txparam
*tp
;
4467 struct rt2870_softc_node
*rni
;
4468 struct rt2870_softc_tx_ring
*ring
;
4469 struct rt2870_softc_tx_data
*data
;
4470 struct rt2870_txinfo
*txinfo
;
4471 struct rt2870_txwi
*txwi
;
4472 struct ieee80211_frame
*wh
;
4473 struct ieee80211_tx_ampdu
*tx_ampdu
;
4474 ieee80211_seq seqno
;
4475 struct rt2870_softc_tx_radiotap_header
*tap
;
4476 u_int hdrsize
, hdrspace
;
4477 uint8_t type
, rate
, bw
, stbc
, shortgi
, mcs
, pid
, wcid
, mpdu_density
, bawin_size
;
4478 uint16_t qos
, len
, dmalen
, mpdu_len
, dur
;
4479 int hasqos
, ac
, tid
, ampdu
, mimops
;
4481 KASSERT(qid
>= 0 && qid
< (sc
->usb_endpoints
- 1),
4482 ("%s: Tx data: invalid qid=%d\n",
4483 device_get_nameunit(sc
->dev
), qid
));
4488 rni
= (struct rt2870_softc_node
*) ni
;
4489 tp
= ni
->ni_txparms
;
4491 ring
= &sc
->tx_ring
[qid
];
4492 data
= STAILQ_FIRST(&ring
->inactive
);
4493 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
4494 txinfo
= (struct rt2870_txinfo
*) data
->buf
;
4495 txwi
= (struct rt2870_txwi
*) (txinfo
+ 1);
4497 wh
= mtod(m
, struct ieee80211_frame
*);
4499 type
= wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
;
4501 hasqos
= IEEE80211_QOS_HAS_SEQ(wh
);
4504 if (IEEE80211_HAS_ADDR4(wh
))
4505 qos
= le16toh(*(const uint16_t *)
4506 (((struct ieee80211_qosframe_addr4
*) wh
)->i_qos
));
4508 qos
= le16toh(*(const uint16_t *)
4509 (((struct ieee80211_qosframe
*) wh
)->i_qos
));
4516 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
))
4517 rate
= tp
->mcastrate
;
4518 else if (m
->m_flags
& M_EAPOL
)
4519 rate
= tp
->mgmtrate
;
4520 else if (tp
->ucastrate
!= IEEE80211_FIXED_RATE_NONE
)
4521 rate
= tp
->ucastrate
;
4523 rate
= ni
->ni_txrate
;
4525 rate
&= IEEE80211_RATE_VAL
;
4527 len
= sizeof(struct rt2870_txinfo
) + sizeof(struct rt2870_txwi
) + m
->m_pkthdr
.len
;
4529 /* align end on a 4-bytes boundary */
4531 dmalen
= (len
+ 3) & ~3;
4535 memset(txinfo
, 0, sizeof(struct rt2870_txinfo
));
4537 txinfo
->len
= htole16(dmalen
- sizeof(struct rt2870_txinfo
));
4539 txinfo
->qsel_flags
= (RT2870_TXINFO_QSEL_EDCA
<< RT2870_TXINFO_QSEL_SHIFT
);
4543 memset((caddr_t
) txinfo
+ len
, 0, dmalen
- len
);
4545 /* fill Tx wireless info */
4547 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4550 mcs
= rt2870_rate2mcs(rate
);
4552 if (type
== IEEE80211_FC0_TYPE_DATA
)
4553 wcid
= !IEEE80211_IS_MULTICAST(wh
->i_addr1
) ? RT2870_AID2WCID(ni
->ni_associd
) : 0;
4557 /* calculate MPDU length without padding */
4559 hdrsize
= ieee80211_hdrsize(wh
);
4560 hdrspace
= ieee80211_hdrspace(ic
, wh
);
4561 mpdu_len
= m
->m_pkthdr
.len
- hdrspace
+ hdrsize
;
4563 memset(txwi
, 0, sizeof(struct rt2870_txwi
));
4567 /* MIMO power save */
4569 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_flags
& IEEE80211_NODE_MIMO_PS
))
4573 if (ni
->ni_flags
& IEEE80211_NODE_MIMO_RTS
)
4575 /* dynamic MIMO power save */
4577 txwi
->mpdu_density_flags
|=
4578 (RT2870_TXWI_FLAGS_MIMOPS
<< RT2870_TXWI_FLAGS_SHIFT
);
4582 /* static MIMO power save */
4597 txwi
->pid_mpdu_len
= ((htole16(pid
) & RT2870_TXWI_PID_MASK
) <<
4598 RT2870_TXWI_PID_SHIFT
) | ((htole16(mpdu_len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
4599 RT2870_TXWI_MPDU_LEN_SHIFT
);
4601 stbc
= (ic
->ic_htcaps
& IEEE80211_HTCAP_TXSTBC
) &&
4602 (ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_htcap
& IEEE80211_HTCAP_RXSTBC
);
4604 shortgi
= (vap
->iv_flags_ht
& (IEEE80211_FHT_SHORTGI20
| IEEE80211_FHT_SHORTGI40
)) &&
4605 (ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_flags
& (IEEE80211_NODE_SGI20
| IEEE80211_NODE_SGI40
));
4607 txwi
->phymode_ifs_stbc_shortgi
|=
4608 ((stbc
& RT2870_TXWI_STBC_MASK
) << RT2870_TXWI_STBC_SHIFT
) |
4609 ((shortgi
& RT2870_TXWI_SHORTGI_MASK
) << RT2870_TXWI_SHORTGI_SHIFT
);
4611 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4613 txwi
->phymode_ifs_stbc_shortgi
|=
4614 (RT2870_TXWI_PHYMODE_HT_MIXED
<< RT2870_TXWI_PHYMODE_SHIFT
);
4618 if (ieee80211_rate2phytype(ic
->ic_rt
, rate
) != IEEE80211_T_OFDM
)
4620 txwi
->phymode_ifs_stbc_shortgi
|=
4621 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
4623 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
4624 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
4628 txwi
->phymode_ifs_stbc_shortgi
|=
4629 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
4633 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) && (ni
->ni_chw
== 40))
4634 bw
= RT2870_TXWI_BW_40
;
4636 bw
= RT2870_TXWI_BW_20
;
4638 txwi
->bw_mcs
= ((bw
& RT2870_TXWI_BW_MASK
) << RT2870_TXWI_BW_SHIFT
) |
4639 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
4641 txwi
->txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
4643 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
4644 (!hasqos
|| (qos
& IEEE80211_QOS_ACKPOLICY
) != IEEE80211_QOS_ACKPOLICY_NOACK
))
4646 txwi
->bawin_size_xflags
|=
4647 (RT2870_TXWI_XFLAGS_ACK
<< RT2870_TXWI_XFLAGS_SHIFT
);
4649 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4651 /* preamble + plcp + signal extension + SIFS */
4653 dur
= 16 + 4 + 6 + 10;
4657 dur
= ieee80211_ack_duration(ic
->ic_rt
, rate
,
4658 ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
);
4661 *(uint16_t *) wh
->i_dur
= htole16(dur
);
4664 /* check fo A-MPDU */
4666 if (m
->m_flags
& M_AMPDU_MPDU
)
4668 ac
= M_WME_GETAC(m
);
4669 tid
= WME_AC_TO_TID(ac
);
4670 tx_ampdu
= &ni
->ni_tx_ampdu
[ac
];
4672 mpdu_density
= RT2870_MS(ni
->ni_htparam
, IEEE80211_HTCAP_MPDUDENSITY
);
4673 bawin_size
= tx_ampdu
->txa_wnd
;
4675 txwi
->mpdu_density_flags
|=
4676 ((mpdu_density
& RT2870_TXWI_MPDU_DENSITY_MASK
) << RT2870_TXWI_MPDU_DENSITY_SHIFT
) |
4677 (RT2870_TXWI_FLAGS_AMPDU
<< RT2870_TXWI_FLAGS_SHIFT
);
4679 txwi
->bawin_size_xflags
|=
4680 ((bawin_size
& RT2870_TXWI_BAWIN_SIZE_MASK
) << RT2870_TXWI_BAWIN_SIZE_SHIFT
);
4682 seqno
= ni
->ni_txseqs
[tid
]++;
4684 *(uint16_t *) &wh
->i_seq
[0] = htole16(seqno
<< IEEE80211_SEQ_SEQ_SHIFT
);
4693 /* ask MAC to insert timestamp into probe responses */
4695 if ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
4696 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
4697 txwi
->mpdu_density_flags
|=
4698 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
4700 if (ieee80211_radiotap_active_vap(vap
))
4704 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
4705 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4706 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4707 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4708 tap
->chan_maxpow
= 0;
4710 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4711 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4715 if (mcs
& RT2870_TXWI_MCS_SHOTPRE
)
4716 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4719 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4721 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4722 tap
->flags
|= IEEE80211_RADIOTAP_F_WEP
;
4724 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4726 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4728 ieee80211_radiotap_tx(vap
, m
);
4730 wh
->i_fc
[1] |= IEEE80211_FC1_WEP
;
4734 ieee80211_radiotap_tx(vap
, m
);
4738 m_copydata(m
, 0, m
->m_pkthdr
.len
, (caddr_t
) (txwi
+ 1));
4740 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4741 "%s: sending data: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4742 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, ampdu=%d, mimops=%d, DMA len=%d\n",
4743 device_get_nameunit(sc
->dev
),
4744 qid
, hdrsize
, hdrspace
, m
->m_pkthdr
.len
,
4745 bw
, stbc
, shortgi
, mcs
, wcid
, ampdu
, mimops
, dmalen
);
4751 STAILQ_INSERT_TAIL(&ring
->pending
, data
, next
);
4754 usbd_transfer_start(sc
->usb_xfer
[1 + qid
]);
4762 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
4763 struct mbuf
*m
, struct ieee80211_node
*ni
,
4764 const struct ieee80211_bpf_params
*params
)
4766 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4768 device_get_nameunit(sc
->dev
));
4776 static void rt2870_rx_intr(struct usb_xfer
*xfer
, usb_error_t error
)
4778 struct rt2870_softc
*sc
;
4780 struct rt2870_softc_rx_ring
*ring
;
4781 struct rt2870_softc_rx_data
*data
;
4784 sc
= usbd_xfer_softc(xfer
);
4786 ring
= &sc
->rx_ring
;
4788 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4789 "%s: Rx interrupt: state=%d, error=%s\n",
4790 device_get_nameunit(sc
->dev
),
4791 USB_GET_STATE(xfer
), usbd_errstr(error
));
4793 switch (USB_GET_STATE(xfer
))
4795 case USB_ST_TRANSFERRED
:
4797 sc
->rx_interrupts
++;
4799 data
= STAILQ_FIRST(&ring
->active
);
4803 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4805 usbd_xfer_status(xfer
, &len
, NULL
, NULL
, NULL
);
4809 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4811 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4818 data
= STAILQ_FIRST(&ring
->inactive
);
4822 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
4824 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4826 usbd_xfer_set_frame_data(xfer
, 0, data
->buf
, usbd_xfer_max_len(xfer
));
4827 usbd_transfer_submit(xfer
);
4832 sc
->rx_interrupts
++;
4834 data
= STAILQ_FIRST(&ring
->active
);
4837 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4838 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4841 if (error
!= USB_ERR_CANCELLED
)
4845 usbd_xfer_set_stall(xfer
);
4856 static void rt2870_tx_intr(struct usb_xfer
*xfer
, usb_error_t error
)
4858 struct rt2870_softc
*sc
;
4860 struct rt2870_softc_tx_ring
*ring
;
4861 struct rt2870_softc_tx_data
*data
;
4863 sc
= usbd_xfer_softc(xfer
);
4865 ring
= usbd_xfer_get_priv(xfer
);
4867 usbd_xfer_status(xfer
, NULL
, NULL
, NULL
, NULL
);
4869 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4870 "%s: Tx interrupt: state=%d, error=%s\n",
4871 device_get_nameunit(sc
->dev
),
4872 USB_GET_STATE(xfer
), usbd_errstr(error
));
4874 switch (USB_GET_STATE(xfer
))
4876 case USB_ST_TRANSFERRED
:
4878 sc
->tx_interrupts
[ring
->qid
]++;
4880 data
= STAILQ_FIRST(&ring
->active
);
4884 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4886 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4888 sc
->tx_qid_pending_mask
|= (1 << ring
->qid
);
4890 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4897 data
= STAILQ_FIRST(&ring
->pending
);
4901 STAILQ_REMOVE_HEAD(&ring
->pending
, next
);
4902 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4904 usbd_xfer_set_frame_data(xfer
, 0, data
->buf
, data
->len
);
4905 usbd_transfer_submit(xfer
);
4907 RT2870_SOFTC_UNLOCK(sc
);
4911 RT2870_SOFTC_LOCK(sc
);
4916 sc
->tx_interrupts
[ring
->qid
]++;
4918 data
= STAILQ_FIRST(&ring
->active
);
4921 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4923 printf("%s: could not transmit buffer: qid=%d, error=%s\n",
4924 device_get_nameunit(sc
->dev
), ring
->qid
, usbd_errstr(error
));
4926 if (data
->m
!= NULL
)
4932 if (data
->ni
!= NULL
)
4934 ieee80211_free_node(data
->ni
);
4938 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4943 if (error
!= USB_ERR_CANCELLED
)
4946 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4948 usbd_xfer_set_stall(xfer
);
4957 * rt2870_rx_done_task
4959 static void rt2870_rx_done_task(void *context
, int pending
)
4961 struct rt2870_softc
*sc
;
4967 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4968 "%s: Rx done task\n",
4969 device_get_nameunit(sc
->dev
));
4971 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4974 if (rt2870_rx_eof(sc
, sc
->rx_process_limit
) != 0)
4976 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4977 "%s: Rx done task: scheduling again\n",
4978 device_get_nameunit(sc
->dev
));
4980 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4985 * rt2870_tx_done_task
4987 static void rt2870_tx_done_task(void *context
, int pending
)
4989 struct rt2870_softc
*sc
;
4996 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4997 "%s: Tx done task\n",
4998 device_get_nameunit(sc
->dev
));
5000 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
5003 for (i
= sc
->usb_endpoints
- 2; i
>= 0; i
--)
5005 if (sc
->tx_qid_pending_mask
& (1 << i
))
5007 sc
->tx_qid_pending_mask
&= ~(1 << i
);
5009 rt2870_tx_eof(sc
, &sc
->tx_ring
[i
]);
5013 if (sc
->tx_qid_pending_mask
!= 0)
5015 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
5016 "%s: Tx done task: scheduling again\n",
5017 device_get_nameunit(sc
->dev
));
5019 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
5024 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
5028 * rt2870_periodic_task
5030 static void rt2870_periodic_task(void *context
, int pending
)
5032 struct rt2870_softc
*sc
;
5034 struct ieee80211com
*ic
;
5035 struct ieee80211vap
*vap
;
5040 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
5042 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
5043 "%s: periodic task: round=%lu\n",
5044 device_get_nameunit(sc
->dev
), sc
->periodic_round
);
5046 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
5049 RT2870_SOFTC_LOCK(sc
);
5051 sc
->periodic_round
++;
5053 rt2870_update_stats(sc
);
5055 if ((sc
->periodic_round
% 10) == 0)
5057 rt2870_bbp_tuning(sc
);
5059 rt2870_update_raw_counters(sc
);
5061 rt2870_watchdog(sc
);
5063 if (vap
!= NULL
&& vap
->iv_opmode
!= IEEE80211_M_MONITOR
&& vap
->iv_state
== IEEE80211_S_RUN
)
5065 if (vap
->iv_opmode
== IEEE80211_M_STA
)
5066 rt2870_amrr_update_iter_func(vap
, vap
->iv_bss
);
5068 ieee80211_iterate_nodes(&ic
->ic_sta
, rt2870_amrr_update_iter_func
, vap
);
5072 RT2870_SOFTC_UNLOCK(sc
);
5074 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
5080 static void rt2870_cmd_task(void *context
, int pending
)
5082 struct rt2870_softc
*sc
;
5083 struct rt2870_softc_cmd_ring
*ring
;
5084 struct rt2870_softc_cmd
*cmd
;
5087 ring
= &sc
->cmd_ring
;
5091 RT2870_SOFTC_LOCK(sc
);
5093 cmd
= STAILQ_FIRST(&ring
->active
);
5096 RT2870_SOFTC_UNLOCK(sc
);
5100 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
5102 RT2870_SOFTC_UNLOCK(sc
);
5104 cmd
->cb(sc
, cmd
->data
);
5106 RT2870_SOFTC_LOCK(sc
);
5108 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
5111 RT2870_SOFTC_UNLOCK(sc
);
5118 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
)
5121 struct rt2870_softc_rx_ring
*ring
;
5122 struct rt2870_softc_rx_data
*data
;
5128 ring
= &sc
->rx_ring
;
5134 RT2870_SOFTC_LOCK(sc
);
5136 data
= STAILQ_FIRST(&ring
->done
);
5139 RT2870_SOFTC_UNLOCK(sc
);
5143 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
5145 RT2870_SOFTC_UNLOCK(sc
);
5150 if (len
< RT2870_RX_DESC_SIZE
)
5156 while (len
> (sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)))
5158 dmalen
= (le32toh(*(uint32_t *) buf
) & 0xffff);
5160 if (dmalen
== 0 || (dmalen
& 3) != 0)
5162 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
5163 "%s: bad DMA len=%u\n",
5164 device_get_nameunit(sc
->dev
), dmalen
);
5168 if ((dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)) > len
)
5170 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
5171 "%s: bad DMA len: DMA len=%u, USB len=%d\n",
5172 device_get_nameunit(sc
->dev
),
5173 (unsigned int) (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)), len
);
5179 rt2870_rx_frame(sc
, buf
+ sizeof(uint32_t), dmalen
);
5181 buf
+= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
5182 len
-= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
5187 RT2870_SOFTC_LOCK(sc
);
5189 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5191 usbd_transfer_start(sc
->usb_xfer
[0]);
5193 RT2870_SOFTC_UNLOCK(sc
);
5198 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
5199 "%s: Rx eof: nframes=%d\n",
5200 device_get_nameunit(sc
->dev
), nframes
);
5202 sc
->rx_packets
+= nframes
;
5204 return (limit
== 0);
5210 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
5211 struct rt2870_softc_tx_ring
*ring
)
5214 struct rt2870_softc_tx_data
*data
;
5223 RT2870_SOFTC_LOCK(sc
);
5225 data
= STAILQ_FIRST(&ring
->done
);
5228 RT2870_SOFTC_UNLOCK(sc
);
5232 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
5234 rt2870_drain_fifo_stats(sc
);
5236 RT2870_SOFTC_UNLOCK(sc
);
5240 if (data
->m
->m_flags
& M_TXCB
)
5241 ieee80211_process_callback(data
->ni
, data
->m
, 0);
5245 ieee80211_free_node(data
->ni
);
5250 RT2870_SOFTC_LOCK(sc
);
5252 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5257 RT2870_SOFTC_UNLOCK(sc
);
5260 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
5261 "%s: Tx eof: qid=%d, nframes=%d\n",
5262 device_get_nameunit(sc
->dev
), ring
->qid
, nframes
);
5266 * rt2870_update_stats
5268 static void rt2870_update_stats(struct rt2870_softc
*sc
)
5271 struct ieee80211com
*ic
;
5272 struct ieee80211vap
*vap
;
5273 struct ieee80211_node
*ni
;
5275 int beacons
, noretryok
, retryok
, failed
, underflows
, zerolen
;
5281 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
5283 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
5284 "%s: update statistic\n",
5285 device_get_nameunit(sc
->dev
));
5287 rt2870_drain_fifo_stats(sc
);
5289 /* read and clear Tx statistic registers */
5291 rt2870_io_mac_read_multi(sc
, RT2870_REG_TX_STA_CNT0
,
5292 stacnt
, sizeof(stacnt
));
5294 stacnt
[0] = le32toh(stacnt
[0]);
5295 stacnt
[1] = le32toh(stacnt
[1]);
5296 stacnt
[2] = le32toh(stacnt
[2]);
5298 beacons
= stacnt
[0] >> 16;
5299 noretryok
= stacnt
[1] & 0xffff;
5300 retryok
= stacnt
[1] >> 16;
5301 failed
= stacnt
[0] & 0xffff;
5302 underflows
= stacnt
[2] >> 16;
5303 zerolen
= stacnt
[2] & 0xffff;
5305 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
5306 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
5307 device_get_nameunit(sc
->dev
),
5308 beacons
, noretryok
, retryok
, failed
, underflows
, zerolen
);
5310 ifp
->if_oerrors
+= failed
;
5312 sc
->tx_beacons
+= beacons
;
5313 sc
->tx_noretryok
+= noretryok
;
5314 sc
->tx_retryok
+= retryok
;
5315 sc
->tx_failed
+= failed
;
5316 sc
->tx_underflows
+= underflows
;
5317 sc
->tx_zerolen
+= zerolen
;
5319 if (vap
!= NULL
&& vap
->iv_opmode
== IEEE80211_M_STA
&& vap
->iv_state
== IEEE80211_S_RUN
)
5323 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
5324 wcid
= RT2870_AID2WCID(associd
);
5326 rt2870_amrr_tx_update(&sc
->amrr_node
[wcid
],
5327 noretryok
+ retryok
+ failed
, noretryok
+ retryok
, retryok
+ failed
);
5334 static void rt2870_bbp_tuning(struct rt2870_softc
*sc
)
5337 struct ieee80211com
*ic
;
5338 struct ieee80211vap
*vap
;
5339 struct ieee80211_node
*ni
;
5341 int8_t rssi
, old
, new;
5343 /* RT2860C does not support BBP tuning */
5345 if (sc
->mac_rev
== 0x28600100)
5350 vap
= TAILQ_FIRST(&ic
->ic_vaps
);
5352 if ((ic
->ic_flags
& IEEE80211_F_SCAN
) || vap
== NULL
||
5353 vap
->iv_opmode
!= IEEE80211_M_STA
|| vap
->iv_state
!= IEEE80211_S_RUN
)
5358 chan
= ieee80211_chan2ieee(ic
, ni
->ni_chan
);
5362 else if (chan
<= 64)
5364 else if (chan
<= 128)
5369 rssi
= ieee80211_getrssi(vap
);
5371 if (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
))
5373 new = 0x2e + sc
->lna_gain
[group
];
5377 if (!IEEE80211_IS_CHAN_HT40(ni
->ni_chan
))
5378 new = 0x32 + sc
->lna_gain
[group
] * 5 / 3;
5380 new = 0x3a + sc
->lna_gain
[group
] * 5 / 3;
5383 /* Tune if absolute average RSSI is greater than -80 */
5388 old
= rt2870_io_bbp_read(sc
, 66);
5391 rt2870_io_bbp_write(sc
, 66, new);
5397 static void rt2870_watchdog(struct rt2870_softc
*sc
)
5402 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
5404 RT2870_DPRINTF(sc
, RT2870_DEBUG_WATCHDOG
,
5405 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
5406 device_get_nameunit(sc
->dev
), tmp
);
5408 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) != 0)
5410 sc
->tx_queue_not_empty
[0]++;
5412 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40012);
5414 for (ntries
= 0; ntries
< 10; ntries
++)
5416 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
5417 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) == 0)
5423 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
5426 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) != 0)
5428 sc
->tx_queue_not_empty
[1]++;
5430 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf4000a);
5432 for (ntries
= 0; ntries
< 10; ntries
++)
5434 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
5435 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) == 0)
5441 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
5446 * rt2870_drain_fifo_stats
5448 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
)
5452 uint8_t wcid
, mcs
, pid
;
5453 int ok
, agg
, retrycnt
;
5457 /* drain Tx status FIFO (maxsize = 16) */
5459 while ((stats
= rt2870_io_mac_read(sc
, RT2870_REG_TX_STA_FIFO
)) &
5460 RT2870_REG_TX_STA_FIFO_VALID
)
5462 wcid
= (stats
>> RT2870_REG_TX_STA_FIFO_WCID_SHIFT
) &
5463 RT2870_REG_TX_STA_FIFO_WCID_MASK
;
5465 /* if no ACK was requested, no feedback is available */
5467 if (!(stats
& RT2870_REG_TX_STA_FIFO_ACK_REQ
) || wcid
== 0xff)
5470 /* update AMRR statistic */
5472 ok
= (stats
& RT2870_REG_TX_STA_FIFO_TX_OK
) ? 1 : 0;
5473 agg
= (stats
& RT2870_REG_TX_STA_FIFO_AGG
) ? 1 : 0;
5474 mcs
= (stats
>> RT2870_REG_TX_STA_FIFO_MCS_SHIFT
) &
5475 RT2870_REG_TX_STA_FIFO_MCS_MASK
;
5476 pid
= (stats
>> RT2870_REG_TX_STA_FIFO_PID_SHIFT
) &
5477 RT2870_REG_TX_STA_FIFO_PID_MASK
;
5478 retrycnt
= pid
- mcs
;
5480 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
5481 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
5482 device_get_nameunit(sc
->dev
),
5483 wcid
, ok
, agg
, mcs
, pid
, retrycnt
);
5485 rt2870_amrr_tx_complete(&sc
->amrr_node
[wcid
], ok
, retrycnt
);
5493 * rt2870_update_raw_counters
5495 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
)
5499 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT
);
5501 sc
->tx_nonagg
+= tmp
& 0xffff;
5502 sc
->tx_agg
+= tmp
>> 16;
5504 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT0
);
5506 sc
->tx_ampdu
+= (tmp
& 0xffff) / 1 + (tmp
>> 16) / 2;
5508 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT1
);
5510 sc
->tx_ampdu
+= (tmp
& 0xffff) / 3 + (tmp
>> 16) / 4;
5512 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT2
);
5514 sc
->tx_ampdu
+= (tmp
& 0xffff) / 5 + (tmp
>> 16) / 6;
5516 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT3
);
5518 sc
->tx_ampdu
+= (tmp
& 0xffff) / 7 + (tmp
>> 16) / 8;
5520 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT4
);
5522 sc
->tx_ampdu
+= (tmp
& 0xffff) / 9 + (tmp
>> 16) / 10;
5524 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT5
);
5526 sc
->tx_ampdu
+= (tmp
& 0xffff) / 11 + (tmp
>> 16) / 12;
5528 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT6
);
5530 sc
->tx_ampdu
+= (tmp
& 0xffff) / 13 + (tmp
>> 16) / 14;
5532 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT7
);
5534 sc
->tx_ampdu
+= (tmp
& 0xffff) / 15 + (tmp
>> 16) / 16;
5536 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT0
);
5538 sc
->rx_crc_errors
+= tmp
& 0xffff;
5539 sc
->rx_phy_errors
+= tmp
>> 16;
5541 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT1
);
5543 sc
->rx_false_ccas
+= tmp
& 0xffff;
5544 sc
->rx_plcp_errors
+= tmp
>> 16;
5546 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT2
);
5548 sc
->rx_dup_packets
+= tmp
& 0xffff;
5549 sc
->rx_fifo_overflows
+= tmp
>> 16;
5551 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TXRX_MPDU_DEN_CNT
);
5553 sc
->tx_mpdu_zero_density
+= tmp
& 0xffff;
5554 sc
->rx_mpdu_zero_density
+= tmp
>> 16;
5558 * rt2870_txrx_enable
5560 static int rt2870_txrx_enable(struct rt2870_softc
*sc
)
5562 struct ieee80211com
*ic
;
5570 /* enable Tx/Rx DMA engine */
5572 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
);
5577 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
, tmp
);
5579 if ((sc
->mac_rev
& 0xffff) != 0x0101)
5580 rt2870_io_mac_write(sc
, RT2870_REG_TX_TXOP_CTRL_CFG
, 0x583f);
5582 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, RT2870_REG_TX_ENABLE
);
5584 for (ntries
= 0; ntries
< 200; ntries
++)
5586 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
5587 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
5595 printf("%s: timeout waiting for DMA engine\n",
5596 device_get_nameunit(sc
->dev
));
5602 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
, 0);
5603 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
5606 tmp
|= RT2870_REG_TX_WB_DDONE
|
5607 RT2870_REG_RX_DMA_ENABLE
|
5608 RT2870_REG_TX_DMA_ENABLE
;
5610 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
5612 tmp
= RT2870_REG_USB_DMA_TX_ENABLE
|
5613 RT2870_REG_USB_DMA_RX_ENABLE
|
5614 RT2870_REG_USB_DMA_RX_AGG_ENABLE
|
5615 /* Rx agg limit in unit of 1024 byte */
5616 ((RT2870_USB_RX_BULK_BUFLEN
/ 1024 - 3) << RT2870_REG_USB_DMA_RX_AGG_LIMIT_SHIFT
) |
5617 /* Rx agg timeout in unit of 33ns */
5620 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, tmp
);
5624 tmp
= RT2870_REG_RX_FILTER_DROP_CRC_ERR
|
5625 RT2870_REG_RX_FILTER_DROP_PHY_ERR
;
5627 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
5629 tmp
|= RT2870_REG_RX_FILTER_DROP_DUPL
|
5630 RT2870_REG_RX_FILTER_DROP_CTS
|
5631 RT2870_REG_RX_FILTER_DROP_BA
|
5632 RT2870_REG_RX_FILTER_DROP_ACK
|
5633 RT2870_REG_RX_FILTER_DROP_VER_ERR
|
5634 RT2870_REG_RX_FILTER_DROP_CTRL_RSV
|
5635 RT2870_REG_RX_FILTER_DROP_CFACK
|
5636 RT2870_REG_RX_FILTER_DROP_CFEND
;
5638 if (ic
->ic_opmode
== IEEE80211_M_STA
)
5639 tmp
|= RT2870_REG_RX_FILTER_DROP_RTS
|
5640 RT2870_REG_RX_FILTER_DROP_PSPOLL
;
5642 if (!(ifp
->if_flags
& IFF_PROMISC
))
5643 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
5646 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
5648 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
5649 RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
5655 * rt2870_alloc_rx_ring
5657 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
5658 struct rt2870_softc_rx_ring
*ring
)
5660 struct rt2870_softc_rx_data
*data
;
5663 STAILQ_INIT(&ring
->inactive
);
5664 STAILQ_INIT(&ring
->active
);
5665 STAILQ_INIT(&ring
->done
);
5667 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
5669 data
= &ring
->data
[i
];
5671 data
->buf
= malloc(RT2870_USB_RX_BULK_BUFLEN
, M_USBDEV
, M_NOWAIT
);
5672 if (data
->buf
== NULL
)
5674 printf("%s: could not allocate Rx buffer\n",
5675 device_get_nameunit(sc
->dev
));
5680 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5687 rt2870_free_rx_ring(sc
, ring
);
5693 * rt2870_reset_rx_ring
5695 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
5696 struct rt2870_softc_rx_ring
*ring
)
5698 struct rt2870_softc_rx_data
*data
;
5701 STAILQ_INIT(&ring
->inactive
);
5702 STAILQ_INIT(&ring
->active
);
5703 STAILQ_INIT(&ring
->done
);
5705 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
5707 data
= &ring
->data
[i
];
5709 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5714 * rt2870_free_rx_ring
5716 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
5717 struct rt2870_softc_rx_ring
*ring
)
5719 struct rt2870_softc_rx_data
*data
;
5722 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
5724 data
= &ring
->data
[i
];
5726 if (data
->buf
!= NULL
)
5728 free(data
->buf
, M_USBDEV
);
5735 * rt2870_alloc_tx_ring
5737 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
5738 struct rt2870_softc_tx_ring
*ring
, int qid
)
5740 struct rt2870_softc_tx_data
*data
;
5743 STAILQ_INIT(&ring
->inactive
);
5744 STAILQ_INIT(&ring
->pending
);
5745 STAILQ_INIT(&ring
->active
);
5746 STAILQ_INIT(&ring
->done
);
5751 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5753 data
= &ring
->data
[i
];
5755 data
->buf
= malloc(RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
, M_USBDEV
, M_NOWAIT
);
5756 if (data
->buf
== NULL
)
5758 printf("%s: could not allocate Tx buffer\n",
5759 device_get_nameunit(sc
->dev
));
5764 memset(data
->buf
, 0, RT2870_TX_DESC_SIZE
);
5766 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5773 rt2870_free_tx_ring(sc
, ring
);
5779 * rt2870_reset_tx_ring
5781 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
5782 struct rt2870_softc_tx_ring
*ring
)
5784 struct rt2870_softc_tx_data
*data
;
5787 STAILQ_INIT(&ring
->inactive
);
5788 STAILQ_INIT(&ring
->pending
);
5789 STAILQ_INIT(&ring
->active
);
5790 STAILQ_INIT(&ring
->done
);
5794 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5796 data
= &ring
->data
[i
];
5798 if (data
->m
!= NULL
)
5804 if (data
->ni
!= NULL
)
5806 ieee80211_free_node(data
->ni
);
5810 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5815 * rt2870_free_tx_ring
5817 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
5818 struct rt2870_softc_tx_ring
*ring
)
5820 struct rt2870_softc_tx_data
*data
;
5823 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5825 data
= &ring
->data
[i
];
5827 if (data
->buf
!= NULL
)
5829 free(data
->buf
, M_USBDEV
);
5833 if (data
->m
!= NULL
)
5839 if (data
->ni
!= NULL
)
5841 ieee80211_free_node(data
->ni
);
5848 * rt2870_reset_cmd_ring
5850 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
5851 struct rt2870_softc_cmd_ring
*ring
)
5853 struct rt2870_softc_cmd
*cmd
;
5856 STAILQ_INIT(&ring
->inactive
);
5857 STAILQ_INIT(&ring
->active
);
5861 for (i
= 0; i
< RT2870_SOFTC_CMD_RING_CMD_COUNT
; i
++)
5863 cmd
= &ring
->cmd
[i
];
5865 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
5870 * rt2870_sysctl_attach
5872 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
)
5874 struct sysctl_ctx_list
*ctx
;
5875 struct sysctl_oid
*tree
;
5876 struct sysctl_oid
*stats
;
5878 ctx
= device_get_sysctl_ctx(sc
->dev
);
5879 tree
= device_get_sysctl_tree(sc
->dev
);
5881 stats
= SYSCTL_ADD_NODE(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
5882 "stats", CTLFLAG_RD
, 0, "statistic");
5884 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5885 "interrupts", CTLFLAG_RD
, &sc
->interrupts
, 0,
5888 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5889 "rx_interrupts", CTLFLAG_RD
, &sc
->rx_interrupts
, 0,
5892 if (sc
->usb_endpoints
== RT2870_SOFTC_USB_XFER_COUNT
)
5894 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5895 "tx_mgmt_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[5], 0,
5896 "Tx MGMT interrupts");
5898 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5899 "tx_hcca_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[4], 0,
5900 "Tx HCCA interrupts");
5903 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5904 "tx_ac3_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[3], 0,
5905 "Tx AC3 interrupts");
5907 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5908 "tx_ac2_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[2], 0,
5909 "Tx AC2 interrupts");
5911 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5912 "tx_ac1_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[1], 0,
5913 "Tx AC1 interrupts");
5915 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5916 "tx_ac0_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[0], 0,
5917 "Tx AC0 interrupts");
5919 if (sc
->usb_endpoints
== RT2870_SOFTC_USB_XFER_COUNT
)
5921 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5922 "tx_mgmt_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[5].queued
, 0,
5923 "Tx MGMT data queued");
5925 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5926 "tx_hcca_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[4].queued
, 0,
5927 "Tx HCCA data queued");
5930 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5931 "tx_ac3_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[3].queued
, 0,
5932 "Tx AC3 data queued");
5934 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5935 "tx_ac2_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[2].queued
, 0,
5936 "Tx AC2 data queued");
5938 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5939 "tx_ac1_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[1].queued
, 0,
5940 "Tx AC1 data queued");
5942 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5943 "tx_ac0_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[0].queued
, 0,
5944 "Tx AC0 data queued");
5946 if (sc
->usb_endpoints
== RT2870_SOFTC_USB_XFER_COUNT
)
5948 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5949 "tx_mgmt_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[5], 0,
5950 "Tx MGMT data queue full");
5952 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5953 "tx_hcca_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[4], 0,
5954 "Tx HCCA data queue full");
5957 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5958 "tx_ac3_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[3], 0,
5959 "Tx AC3 data queue full");
5961 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5962 "tx_ac2_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[2], 0,
5963 "Tx AC2 data queue full");
5965 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5966 "tx_ac1_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[1], 0,
5967 "Tx AC1 data queue full");
5969 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5970 "tx_ac0_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[0], 0,
5971 "Tx AC0 data queue full");
5973 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5974 "tx_watchdog_timeouts", CTLFLAG_RD
, &sc
->tx_watchdog_timeouts
, 0,
5975 "Tx watchdog timeouts");
5977 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5978 "rx_mbuf_alloc_errors", CTLFLAG_RD
, &sc
->rx_mbuf_alloc_errors
, 0,
5979 "Rx mbuf allocation errors");
5981 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5982 "tx_queue_0_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[0], 0,
5983 "Tx queue 0 not empty");
5985 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5986 "tx_queue_1_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[1], 0,
5987 "Tx queue 1 not empty");
5989 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5990 "tx_beacons", CTLFLAG_RD
, &sc
->tx_beacons
, 0,
5993 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5994 "tx_noretryok", CTLFLAG_RD
, &sc
->tx_noretryok
, 0,
5995 "Tx successfull without retries");
5997 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5998 "tx_retryok", CTLFLAG_RD
, &sc
->tx_retryok
, 0,
5999 "Tx successfull with retries");
6001 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6002 "tx_failed", CTLFLAG_RD
, &sc
->tx_failed
, 0,
6005 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6006 "tx_underflows", CTLFLAG_RD
, &sc
->tx_underflows
, 0,
6009 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6010 "tx_zerolen", CTLFLAG_RD
, &sc
->tx_zerolen
, 0,
6013 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6014 "tx_nonagg", CTLFLAG_RD
, &sc
->tx_nonagg
, 0,
6015 "Tx non-aggregated");
6017 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6018 "tx_agg", CTLFLAG_RD
, &sc
->tx_agg
, 0,
6021 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6022 "tx_ampdu", CTLFLAG_RD
, &sc
->tx_ampdu
, 0,
6025 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6026 "tx_mpdu_zero_density", CTLFLAG_RD
, &sc
->tx_mpdu_zero_density
, 0,
6027 "Tx MPDU with zero density");
6029 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6030 "rx_packets", CTLFLAG_RD
, &sc
->rx_packets
, 0,
6033 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6034 "rx_ampdu", CTLFLAG_RD
, &sc
->rx_ampdu
, 0,
6037 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6038 "rx_mpdu_zero_density", CTLFLAG_RD
, &sc
->rx_mpdu_zero_density
, 0,
6039 "Rx MPDU with zero density");
6041 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6042 "rx_amsdu", CTLFLAG_RD
, &sc
->rx_amsdu
, 0,
6045 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6046 "rx_crc_errors", CTLFLAG_RD
, &sc
->rx_crc_errors
, 0,
6049 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6050 "rx_phy_errors", CTLFLAG_RD
, &sc
->rx_phy_errors
, 0,
6053 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6054 "rx_false_ccas", CTLFLAG_RD
, &sc
->rx_false_ccas
, 0,
6057 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6058 "rx_plcp_errors", CTLFLAG_RD
, &sc
->rx_plcp_errors
, 0,
6061 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6062 "rx_dup_packets", CTLFLAG_RD
, &sc
->rx_dup_packets
, 0,
6063 "Rx duplicate packets");
6065 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6066 "rx_fifo_overflows", CTLFLAG_RD
, &sc
->rx_fifo_overflows
, 0,
6067 "Rx FIFO overflows");
6069 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6070 "rx_cipher_no_errors", CTLFLAG_RD
, &sc
->rx_cipher_no_errors
, 0,
6071 "Rx cipher no errors");
6073 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6074 "rx_cipher_icv_errors", CTLFLAG_RD
, &sc
->rx_cipher_icv_errors
, 0,
6075 "Rx cipher ICV errors");
6077 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6078 "rx_cipher_mic_errors", CTLFLAG_RD
, &sc
->rx_cipher_mic_errors
, 0,
6079 "Rx cipher MIC errors");
6081 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
6082 "rx_cipher_invalid_key_errors", CTLFLAG_RD
, &sc
->rx_cipher_invalid_key_errors
, 0,
6083 "Rx cipher invalid key errors");
6086 static device_method_t rt2870_dev_methods
[] =
6088 DEVMETHOD(device_probe
, rt2870_probe
),
6089 DEVMETHOD(device_attach
, rt2870_attach
),
6090 DEVMETHOD(device_detach
, rt2870_detach
),
6094 static driver_t rt2870_driver
=
6098 sizeof(struct rt2870_softc
),
6101 static devclass_t rt2870_dev_class
;
6103 DRIVER_MODULE(rt2870
, uhub
, rt2870_driver
, rt2870_dev_class
,
6106 MODULE_DEPEND(rt2870
, usb
, 1, 1, 1);
6107 MODULE_DEPEND(rt2870
, wlan
, 1, 1, 1);