18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci	Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
48c2ecf20Sopenharmony_ci	<http://rt2x00.serialmonkey.com>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci	Module: rt2x00mac
108c2ecf20Sopenharmony_ci	Abstract: rt2x00 generic mac80211 routines.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "rt2x00.h"
178c2ecf20Sopenharmony_ci#include "rt2x00lib.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
208c2ecf20Sopenharmony_ci				struct data_queue *queue,
218c2ecf20Sopenharmony_ci				struct sk_buff *frag_skb)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(frag_skb);
248c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *rts_info;
258c2ecf20Sopenharmony_ci	struct sk_buff *skb;
268c2ecf20Sopenharmony_ci	unsigned int data_length;
278c2ecf20Sopenharmony_ci	int retval = 0;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
308c2ecf20Sopenharmony_ci		data_length = sizeof(struct ieee80211_cts);
318c2ecf20Sopenharmony_ci	else
328c2ecf20Sopenharmony_ci		data_length = sizeof(struct ieee80211_rts);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(data_length + rt2x00dev->hw->extra_tx_headroom);
358c2ecf20Sopenharmony_ci	if (unlikely(!skb)) {
368c2ecf20Sopenharmony_ci		rt2x00_warn(rt2x00dev, "Failed to create RTS/CTS frame\n");
378c2ecf20Sopenharmony_ci		return -ENOMEM;
388c2ecf20Sopenharmony_ci	}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
418c2ecf20Sopenharmony_ci	skb_put(skb, data_length);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/*
448c2ecf20Sopenharmony_ci	 * Copy TX information over from original frame to
458c2ecf20Sopenharmony_ci	 * RTS/CTS frame. Note that we set the no encryption flag
468c2ecf20Sopenharmony_ci	 * since we don't want this frame to be encrypted.
478c2ecf20Sopenharmony_ci	 * RTS frames should be acked, while CTS-to-self frames
488c2ecf20Sopenharmony_ci	 * should not. The ready for TX flag is cleared to prevent
498c2ecf20Sopenharmony_ci	 * it being automatically send when the descriptor is
508c2ecf20Sopenharmony_ci	 * written to the hardware.
518c2ecf20Sopenharmony_ci	 */
528c2ecf20Sopenharmony_ci	memcpy(skb->cb, frag_skb->cb, sizeof(skb->cb));
538c2ecf20Sopenharmony_ci	rts_info = IEEE80211_SKB_CB(skb);
548c2ecf20Sopenharmony_ci	rts_info->control.rates[0].flags &= ~IEEE80211_TX_RC_USE_RTS_CTS;
558c2ecf20Sopenharmony_ci	rts_info->control.rates[0].flags &= ~IEEE80211_TX_RC_USE_CTS_PROTECT;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
588c2ecf20Sopenharmony_ci		rts_info->flags |= IEEE80211_TX_CTL_NO_ACK;
598c2ecf20Sopenharmony_ci	else
608c2ecf20Sopenharmony_ci		rts_info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/* Disable hardware encryption */
638c2ecf20Sopenharmony_ci	rts_info->control.hw_key = NULL;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/*
668c2ecf20Sopenharmony_ci	 * RTS/CTS frame should use the length of the frame plus any
678c2ecf20Sopenharmony_ci	 * encryption overhead that will be added by the hardware.
688c2ecf20Sopenharmony_ci	 */
698c2ecf20Sopenharmony_ci	data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
728c2ecf20Sopenharmony_ci		ieee80211_ctstoself_get(rt2x00dev->hw, tx_info->control.vif,
738c2ecf20Sopenharmony_ci					frag_skb->data, data_length, tx_info,
748c2ecf20Sopenharmony_ci					(struct ieee80211_cts *)(skb->data));
758c2ecf20Sopenharmony_ci	else
768c2ecf20Sopenharmony_ci		ieee80211_rts_get(rt2x00dev->hw, tx_info->control.vif,
778c2ecf20Sopenharmony_ci				  frag_skb->data, data_length, tx_info,
788c2ecf20Sopenharmony_ci				  (struct ieee80211_rts *)(skb->data));
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	retval = rt2x00queue_write_tx_frame(queue, skb, NULL, true);
818c2ecf20Sopenharmony_ci	if (retval) {
828c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
838c2ecf20Sopenharmony_ci		rt2x00_warn(rt2x00dev, "Failed to send RTS/CTS frame\n");
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return retval;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_civoid rt2x00mac_tx(struct ieee80211_hw *hw,
908c2ecf20Sopenharmony_ci		  struct ieee80211_tx_control *control,
918c2ecf20Sopenharmony_ci		  struct sk_buff *skb)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
948c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
958c2ecf20Sopenharmony_ci	enum data_queue_qid qid = skb_get_queue_mapping(skb);
968c2ecf20Sopenharmony_ci	struct data_queue *queue = NULL;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/*
998c2ecf20Sopenharmony_ci	 * Mac80211 might be calling this function while we are trying
1008c2ecf20Sopenharmony_ci	 * to remove the device or perhaps suspending it.
1018c2ecf20Sopenharmony_ci	 * Note that we can only stop the TX queues inside the TX path
1028c2ecf20Sopenharmony_ci	 * due to possible race conditions in mac80211.
1038c2ecf20Sopenharmony_ci	 */
1048c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1058c2ecf20Sopenharmony_ci		goto exit_free_skb;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/*
1088c2ecf20Sopenharmony_ci	 * Use the ATIM queue if appropriate and present.
1098c2ecf20Sopenharmony_ci	 */
1108c2ecf20Sopenharmony_ci	if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
1118c2ecf20Sopenharmony_ci	    rt2x00_has_cap_flag(rt2x00dev, REQUIRE_ATIM_QUEUE))
1128c2ecf20Sopenharmony_ci		qid = QID_ATIM;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	queue = rt2x00queue_get_tx_queue(rt2x00dev, qid);
1158c2ecf20Sopenharmony_ci	if (unlikely(!queue)) {
1168c2ecf20Sopenharmony_ci		rt2x00_err(rt2x00dev,
1178c2ecf20Sopenharmony_ci			   "Attempt to send packet over invalid queue %d\n"
1188c2ecf20Sopenharmony_ci			   "Please file bug report to %s\n", qid, DRV_PROJECT);
1198c2ecf20Sopenharmony_ci		goto exit_free_skb;
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/*
1238c2ecf20Sopenharmony_ci	 * If CTS/RTS is required. create and queue that frame first.
1248c2ecf20Sopenharmony_ci	 * Make sure we have at least enough entries available to send
1258c2ecf20Sopenharmony_ci	 * this CTS/RTS frame as well as the data frame.
1268c2ecf20Sopenharmony_ci	 * Note that when the driver has set the set_rts_threshold()
1278c2ecf20Sopenharmony_ci	 * callback function it doesn't need software generation of
1288c2ecf20Sopenharmony_ci	 * either RTS or CTS-to-self frame and handles everything
1298c2ecf20Sopenharmony_ci	 * inside the hardware.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	if (!rt2x00dev->ops->hw->set_rts_threshold &&
1328c2ecf20Sopenharmony_ci	    (tx_info->control.rates[0].flags & (IEEE80211_TX_RC_USE_RTS_CTS |
1338c2ecf20Sopenharmony_ci						IEEE80211_TX_RC_USE_CTS_PROTECT))) {
1348c2ecf20Sopenharmony_ci		if (rt2x00queue_available(queue) <= 1) {
1358c2ecf20Sopenharmony_ci			/*
1368c2ecf20Sopenharmony_ci			 * Recheck for full queue under lock to avoid race
1378c2ecf20Sopenharmony_ci			 * conditions with rt2x00lib_txdone().
1388c2ecf20Sopenharmony_ci			 */
1398c2ecf20Sopenharmony_ci			spin_lock(&queue->tx_lock);
1408c2ecf20Sopenharmony_ci			if (rt2x00queue_threshold(queue))
1418c2ecf20Sopenharmony_ci				rt2x00queue_pause_queue(queue);
1428c2ecf20Sopenharmony_ci			spin_unlock(&queue->tx_lock);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci			goto exit_free_skb;
1458c2ecf20Sopenharmony_ci		}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb))
1488c2ecf20Sopenharmony_ci			goto exit_free_skb;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	if (unlikely(rt2x00queue_write_tx_frame(queue, skb, control->sta, false)))
1528c2ecf20Sopenharmony_ci		goto exit_free_skb;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	return;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci exit_free_skb:
1578c2ecf20Sopenharmony_ci	ieee80211_free_txskb(hw, skb);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_tx);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ciint rt2x00mac_start(struct ieee80211_hw *hw)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1668c2ecf20Sopenharmony_ci		return 0;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags)) {
1698c2ecf20Sopenharmony_ci		/*
1708c2ecf20Sopenharmony_ci		 * This is special case for ieee80211_restart_hw(), otherwise
1718c2ecf20Sopenharmony_ci		 * mac80211 never call start() two times in row without stop();
1728c2ecf20Sopenharmony_ci		 */
1738c2ecf20Sopenharmony_ci		set_bit(DEVICE_STATE_RESET, &rt2x00dev->flags);
1748c2ecf20Sopenharmony_ci		rt2x00dev->ops->lib->pre_reset_hw(rt2x00dev);
1758c2ecf20Sopenharmony_ci		rt2x00lib_stop(rt2x00dev);
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci	return rt2x00lib_start(rt2x00dev);
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_start);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_civoid rt2x00mac_stop(struct ieee80211_hw *hw)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1868c2ecf20Sopenharmony_ci		return;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	rt2x00lib_stop(rt2x00dev);
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_stop);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_civoid
1938c2ecf20Sopenharmony_cirt2x00mac_reconfig_complete(struct ieee80211_hw *hw,
1948c2ecf20Sopenharmony_ci			    enum ieee80211_reconfig_type reconfig_type)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
1998c2ecf20Sopenharmony_ci		clear_bit(DEVICE_STATE_RESET, &rt2x00dev->flags);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_reconfig_complete);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciint rt2x00mac_add_interface(struct ieee80211_hw *hw,
2048c2ecf20Sopenharmony_ci			    struct ieee80211_vif *vif)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
2078c2ecf20Sopenharmony_ci	struct rt2x00_intf *intf = vif_to_intf(vif);
2088c2ecf20Sopenharmony_ci	struct data_queue *queue = rt2x00dev->bcn;
2098c2ecf20Sopenharmony_ci	struct queue_entry *entry = NULL;
2108c2ecf20Sopenharmony_ci	unsigned int i;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	/*
2138c2ecf20Sopenharmony_ci	 * Don't allow interfaces to be added
2148c2ecf20Sopenharmony_ci	 * the device has disappeared.
2158c2ecf20Sopenharmony_ci	 */
2168c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
2178c2ecf20Sopenharmony_ci	    !test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
2188c2ecf20Sopenharmony_ci		return -ENODEV;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/*
2218c2ecf20Sopenharmony_ci	 * Loop through all beacon queues to find a free
2228c2ecf20Sopenharmony_ci	 * entry. Since there are as much beacon entries
2238c2ecf20Sopenharmony_ci	 * as the maximum interfaces, this search shouldn't
2248c2ecf20Sopenharmony_ci	 * fail.
2258c2ecf20Sopenharmony_ci	 */
2268c2ecf20Sopenharmony_ci	for (i = 0; i < queue->limit; i++) {
2278c2ecf20Sopenharmony_ci		entry = &queue->entries[i];
2288c2ecf20Sopenharmony_ci		if (!test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
2298c2ecf20Sopenharmony_ci			break;
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	if (unlikely(i == queue->limit))
2338c2ecf20Sopenharmony_ci		return -ENOBUFS;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/*
2368c2ecf20Sopenharmony_ci	 * We are now absolutely sure the interface can be created,
2378c2ecf20Sopenharmony_ci	 * increase interface count and start initialization.
2388c2ecf20Sopenharmony_ci	 */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP)
2418c2ecf20Sopenharmony_ci		rt2x00dev->intf_ap_count++;
2428c2ecf20Sopenharmony_ci	else
2438c2ecf20Sopenharmony_ci		rt2x00dev->intf_sta_count++;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	mutex_init(&intf->beacon_skb_mutex);
2468c2ecf20Sopenharmony_ci	intf->beacon = entry;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	/*
2498c2ecf20Sopenharmony_ci	 * The MAC address must be configured after the device
2508c2ecf20Sopenharmony_ci	 * has been initialized. Otherwise the device can reset
2518c2ecf20Sopenharmony_ci	 * the MAC registers.
2528c2ecf20Sopenharmony_ci	 * The BSSID address must only be configured in AP mode,
2538c2ecf20Sopenharmony_ci	 * however we should not send an empty BSSID address for
2548c2ecf20Sopenharmony_ci	 * STA interfaces at this time, since this can cause
2558c2ecf20Sopenharmony_ci	 * invalid behavior in the device.
2568c2ecf20Sopenharmony_ci	 */
2578c2ecf20Sopenharmony_ci	rt2x00lib_config_intf(rt2x00dev, intf, vif->type,
2588c2ecf20Sopenharmony_ci			      vif->addr, NULL);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/*
2618c2ecf20Sopenharmony_ci	 * Some filters depend on the current working mode. We can force
2628c2ecf20Sopenharmony_ci	 * an update during the next configure_filter() run by mac80211 by
2638c2ecf20Sopenharmony_ci	 * resetting the current packet_filter state.
2648c2ecf20Sopenharmony_ci	 */
2658c2ecf20Sopenharmony_ci	rt2x00dev->packet_filter = 0;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return 0;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_civoid rt2x00mac_remove_interface(struct ieee80211_hw *hw,
2728c2ecf20Sopenharmony_ci				struct ieee80211_vif *vif)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
2758c2ecf20Sopenharmony_ci	struct rt2x00_intf *intf = vif_to_intf(vif);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/*
2788c2ecf20Sopenharmony_ci	 * Don't allow interfaces to be remove while
2798c2ecf20Sopenharmony_ci	 * either the device has disappeared or when
2808c2ecf20Sopenharmony_ci	 * no interface is present.
2818c2ecf20Sopenharmony_ci	 */
2828c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
2838c2ecf20Sopenharmony_ci	    (vif->type == NL80211_IFTYPE_AP && !rt2x00dev->intf_ap_count) ||
2848c2ecf20Sopenharmony_ci	    (vif->type != NL80211_IFTYPE_AP && !rt2x00dev->intf_sta_count))
2858c2ecf20Sopenharmony_ci		return;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP)
2888c2ecf20Sopenharmony_ci		rt2x00dev->intf_ap_count--;
2898c2ecf20Sopenharmony_ci	else
2908c2ecf20Sopenharmony_ci		rt2x00dev->intf_sta_count--;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/*
2938c2ecf20Sopenharmony_ci	 * Release beacon entry so it is available for
2948c2ecf20Sopenharmony_ci	 * new interfaces again.
2958c2ecf20Sopenharmony_ci	 */
2968c2ecf20Sopenharmony_ci	clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/*
2998c2ecf20Sopenharmony_ci	 * Make sure the bssid and mac address registers
3008c2ecf20Sopenharmony_ci	 * are cleared to prevent false ACKing of frames.
3018c2ecf20Sopenharmony_ci	 */
3028c2ecf20Sopenharmony_ci	rt2x00lib_config_intf(rt2x00dev, intf,
3038c2ecf20Sopenharmony_ci			      NL80211_IFTYPE_UNSPECIFIED, NULL, NULL);
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ciint rt2x00mac_config(struct ieee80211_hw *hw, u32 changed)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
3108c2ecf20Sopenharmony_ci	struct ieee80211_conf *conf = &hw->conf;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/*
3138c2ecf20Sopenharmony_ci	 * mac80211 might be calling this function while we are trying
3148c2ecf20Sopenharmony_ci	 * to remove the device or perhaps suspending it.
3158c2ecf20Sopenharmony_ci	 */
3168c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
3178c2ecf20Sopenharmony_ci		return 0;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/*
3208c2ecf20Sopenharmony_ci	 * Some configuration parameters (e.g. channel and antenna values) can
3218c2ecf20Sopenharmony_ci	 * only be set when the radio is enabled, but do require the RX to
3228c2ecf20Sopenharmony_ci	 * be off. During this period we should keep link tuning enabled,
3238c2ecf20Sopenharmony_ci	 * if for any reason the link tuner must be reset, this will be
3248c2ecf20Sopenharmony_ci	 * handled by rt2x00lib_config().
3258c2ecf20Sopenharmony_ci	 */
3268c2ecf20Sopenharmony_ci	rt2x00queue_stop_queue(rt2x00dev->rx);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* Do not race with with link tuner. */
3298c2ecf20Sopenharmony_ci	mutex_lock(&rt2x00dev->conf_mutex);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	/*
3328c2ecf20Sopenharmony_ci	 * When we've just turned on the radio, we want to reprogram
3338c2ecf20Sopenharmony_ci	 * everything to ensure a consistent state
3348c2ecf20Sopenharmony_ci	 */
3358c2ecf20Sopenharmony_ci	rt2x00lib_config(rt2x00dev, conf, changed);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/*
3388c2ecf20Sopenharmony_ci	 * After the radio has been enabled we need to configure
3398c2ecf20Sopenharmony_ci	 * the antenna to the default settings. rt2x00lib_config_antenna()
3408c2ecf20Sopenharmony_ci	 * should determine if any action should be taken based on
3418c2ecf20Sopenharmony_ci	 * checking if diversity has been enabled or no antenna changes
3428c2ecf20Sopenharmony_ci	 * have been made since the last configuration change.
3438c2ecf20Sopenharmony_ci	 */
3448c2ecf20Sopenharmony_ci	rt2x00lib_config_antenna(rt2x00dev, rt2x00dev->default_ant);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	mutex_unlock(&rt2x00dev->conf_mutex);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/* Turn RX back on */
3498c2ecf20Sopenharmony_ci	rt2x00queue_start_queue(rt2x00dev->rx);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return 0;
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_config);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_civoid rt2x00mac_configure_filter(struct ieee80211_hw *hw,
3568c2ecf20Sopenharmony_ci				unsigned int changed_flags,
3578c2ecf20Sopenharmony_ci				unsigned int *total_flags,
3588c2ecf20Sopenharmony_ci				u64 multicast)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	/*
3638c2ecf20Sopenharmony_ci	 * Mask off any flags we are going to ignore
3648c2ecf20Sopenharmony_ci	 * from the total_flags field.
3658c2ecf20Sopenharmony_ci	 */
3668c2ecf20Sopenharmony_ci	*total_flags &=
3678c2ecf20Sopenharmony_ci	    FIF_ALLMULTI |
3688c2ecf20Sopenharmony_ci	    FIF_FCSFAIL |
3698c2ecf20Sopenharmony_ci	    FIF_PLCPFAIL |
3708c2ecf20Sopenharmony_ci	    FIF_CONTROL |
3718c2ecf20Sopenharmony_ci	    FIF_PSPOLL |
3728c2ecf20Sopenharmony_ci	    FIF_OTHER_BSS;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	/*
3758c2ecf20Sopenharmony_ci	 * Apply some rules to the filters:
3768c2ecf20Sopenharmony_ci	 * - Some filters imply different filters to be set.
3778c2ecf20Sopenharmony_ci	 * - Some things we can't filter out at all.
3788c2ecf20Sopenharmony_ci	 * - Multicast filter seems to kill broadcast traffic so never use it.
3798c2ecf20Sopenharmony_ci	 */
3808c2ecf20Sopenharmony_ci	*total_flags |= FIF_ALLMULTI;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/*
3838c2ecf20Sopenharmony_ci	 * If the device has a single filter for all control frames,
3848c2ecf20Sopenharmony_ci	 * FIF_CONTROL and FIF_PSPOLL flags imply each other.
3858c2ecf20Sopenharmony_ci	 * And if the device has more than one filter for control frames
3868c2ecf20Sopenharmony_ci	 * of different types, but has no a separate filter for PS Poll frames,
3878c2ecf20Sopenharmony_ci	 * FIF_CONTROL flag implies FIF_PSPOLL.
3888c2ecf20Sopenharmony_ci	 */
3898c2ecf20Sopenharmony_ci	if (!rt2x00_has_cap_control_filters(rt2x00dev)) {
3908c2ecf20Sopenharmony_ci		if (*total_flags & FIF_CONTROL || *total_flags & FIF_PSPOLL)
3918c2ecf20Sopenharmony_ci			*total_flags |= FIF_CONTROL | FIF_PSPOLL;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci	if (!rt2x00_has_cap_control_filter_pspoll(rt2x00dev)) {
3948c2ecf20Sopenharmony_ci		if (*total_flags & FIF_CONTROL)
3958c2ecf20Sopenharmony_ci			*total_flags |= FIF_PSPOLL;
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	rt2x00dev->packet_filter = *total_flags;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic void rt2x00mac_set_tim_iter(void *data, u8 *mac,
4058c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct rt2x00_intf *intf = vif_to_intf(vif);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	if (vif->type != NL80211_IFTYPE_AP &&
4108c2ecf20Sopenharmony_ci	    vif->type != NL80211_IFTYPE_ADHOC &&
4118c2ecf20Sopenharmony_ci	    vif->type != NL80211_IFTYPE_MESH_POINT &&
4128c2ecf20Sopenharmony_ci	    vif->type != NL80211_IFTYPE_WDS)
4138c2ecf20Sopenharmony_ci		return;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	set_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags);
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ciint rt2x00mac_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
4198c2ecf20Sopenharmony_ci		      bool set)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
4248c2ecf20Sopenharmony_ci		return 0;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
4278c2ecf20Sopenharmony_ci		rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
4288c2ecf20Sopenharmony_ci		rt2x00mac_set_tim_iter, rt2x00dev);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	/* queue work to upodate the beacon template */
4318c2ecf20Sopenharmony_ci	ieee80211_queue_work(rt2x00dev->hw, &rt2x00dev->intf_work);
4328c2ecf20Sopenharmony_ci	return 0;
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_set_tim);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci#ifdef CONFIG_RT2X00_LIB_CRYPTO
4378c2ecf20Sopenharmony_cistatic void memcpy_tkip(struct rt2x00lib_crypto *crypto, u8 *key, u8 key_len)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	if (key_len > NL80211_TKIP_DATA_OFFSET_ENCR_KEY)
4408c2ecf20Sopenharmony_ci		memcpy(crypto->key,
4418c2ecf20Sopenharmony_ci		       &key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY],
4428c2ecf20Sopenharmony_ci		       sizeof(crypto->key));
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	if (key_len > NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
4458c2ecf20Sopenharmony_ci		memcpy(crypto->tx_mic,
4468c2ecf20Sopenharmony_ci		       &key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
4478c2ecf20Sopenharmony_ci		       sizeof(crypto->tx_mic));
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	if (key_len > NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY)
4508c2ecf20Sopenharmony_ci		memcpy(crypto->rx_mic,
4518c2ecf20Sopenharmony_ci		       &key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
4528c2ecf20Sopenharmony_ci		       sizeof(crypto->rx_mic));
4538c2ecf20Sopenharmony_ci}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ciint rt2x00mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4568c2ecf20Sopenharmony_ci		      struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4578c2ecf20Sopenharmony_ci		      struct ieee80211_key_conf *key)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
4608c2ecf20Sopenharmony_ci	int (*set_key) (struct rt2x00_dev *rt2x00dev,
4618c2ecf20Sopenharmony_ci			struct rt2x00lib_crypto *crypto,
4628c2ecf20Sopenharmony_ci			struct ieee80211_key_conf *key);
4638c2ecf20Sopenharmony_ci	struct rt2x00lib_crypto crypto;
4648c2ecf20Sopenharmony_ci	static const u8 bcast_addr[ETH_ALEN] =
4658c2ecf20Sopenharmony_ci		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
4668c2ecf20Sopenharmony_ci	struct rt2x00_sta *sta_priv = NULL;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
4698c2ecf20Sopenharmony_ci		return 0;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/* The hardware can't do MFP */
4728c2ecf20Sopenharmony_ci	if (!rt2x00_has_cap_hw_crypto(rt2x00dev) || (sta && sta->mfp))
4738c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	/*
4768c2ecf20Sopenharmony_ci	 * To support IBSS RSN, don't program group keys in IBSS, the
4778c2ecf20Sopenharmony_ci	 * hardware will then not attempt to decrypt the frames.
4788c2ecf20Sopenharmony_ci	 */
4798c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_ADHOC &&
4808c2ecf20Sopenharmony_ci	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
4818c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	if (key->keylen > 32)
4848c2ecf20Sopenharmony_ci		return -ENOSPC;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	memset(&crypto, 0, sizeof(crypto));
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	crypto.bssidx = rt2x00lib_get_bssidx(rt2x00dev, vif);
4898c2ecf20Sopenharmony_ci	crypto.cipher = rt2x00crypto_key_to_cipher(key);
4908c2ecf20Sopenharmony_ci	if (crypto.cipher == CIPHER_NONE)
4918c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4928c2ecf20Sopenharmony_ci	if (crypto.cipher == CIPHER_TKIP && rt2x00_is_usb(rt2x00dev))
4938c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	crypto.cmd = cmd;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	if (sta) {
4988c2ecf20Sopenharmony_ci		crypto.address = sta->addr;
4998c2ecf20Sopenharmony_ci		sta_priv = sta_to_rt2x00_sta(sta);
5008c2ecf20Sopenharmony_ci		crypto.wcid = sta_priv->wcid;
5018c2ecf20Sopenharmony_ci	} else
5028c2ecf20Sopenharmony_ci		crypto.address = bcast_addr;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (crypto.cipher == CIPHER_TKIP)
5058c2ecf20Sopenharmony_ci		memcpy_tkip(&crypto, &key->key[0], key->keylen);
5068c2ecf20Sopenharmony_ci	else
5078c2ecf20Sopenharmony_ci		memcpy(crypto.key, &key->key[0], key->keylen);
5088c2ecf20Sopenharmony_ci	/*
5098c2ecf20Sopenharmony_ci	 * Each BSS has a maximum of 4 shared keys.
5108c2ecf20Sopenharmony_ci	 * Shared key index values:
5118c2ecf20Sopenharmony_ci	 *	0) BSS0 key0
5128c2ecf20Sopenharmony_ci	 *	1) BSS0 key1
5138c2ecf20Sopenharmony_ci	 *	...
5148c2ecf20Sopenharmony_ci	 *	4) BSS1 key0
5158c2ecf20Sopenharmony_ci	 *	...
5168c2ecf20Sopenharmony_ci	 *	8) BSS2 key0
5178c2ecf20Sopenharmony_ci	 *	...
5188c2ecf20Sopenharmony_ci	 * Both pairwise as shared key indeces are determined by
5198c2ecf20Sopenharmony_ci	 * driver. This is required because the hardware requires
5208c2ecf20Sopenharmony_ci	 * keys to be assigned in correct order (When key 1 is
5218c2ecf20Sopenharmony_ci	 * provided but key 0 is not, then the key is not found
5228c2ecf20Sopenharmony_ci	 * by the hardware during RX).
5238c2ecf20Sopenharmony_ci	 */
5248c2ecf20Sopenharmony_ci	if (cmd == SET_KEY)
5258c2ecf20Sopenharmony_ci		key->hw_key_idx = 0;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5288c2ecf20Sopenharmony_ci		set_key = rt2x00dev->ops->lib->config_pairwise_key;
5298c2ecf20Sopenharmony_ci	else
5308c2ecf20Sopenharmony_ci		set_key = rt2x00dev->ops->lib->config_shared_key;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	if (!set_key)
5338c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return set_key(rt2x00dev, &crypto, key);
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_set_key);
5388c2ecf20Sopenharmony_ci#endif /* CONFIG_RT2X00_LIB_CRYPTO */
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_civoid rt2x00mac_sw_scan_start(struct ieee80211_hw *hw,
5418c2ecf20Sopenharmony_ci			     struct ieee80211_vif *vif,
5428c2ecf20Sopenharmony_ci			     const u8 *mac_addr)
5438c2ecf20Sopenharmony_ci{
5448c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
5458c2ecf20Sopenharmony_ci	set_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags);
5468c2ecf20Sopenharmony_ci	rt2x00link_stop_tuner(rt2x00dev);
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_sw_scan_start);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_civoid rt2x00mac_sw_scan_complete(struct ieee80211_hw *hw,
5518c2ecf20Sopenharmony_ci				struct ieee80211_vif *vif)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
5548c2ecf20Sopenharmony_ci	clear_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags);
5558c2ecf20Sopenharmony_ci	rt2x00link_start_tuner(rt2x00dev);
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_sw_scan_complete);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ciint rt2x00mac_get_stats(struct ieee80211_hw *hw,
5608c2ecf20Sopenharmony_ci			struct ieee80211_low_level_stats *stats)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/*
5658c2ecf20Sopenharmony_ci	 * The dot11ACKFailureCount, dot11RTSFailureCount and
5668c2ecf20Sopenharmony_ci	 * dot11RTSSuccessCount are updated in interrupt time.
5678c2ecf20Sopenharmony_ci	 * dot11FCSErrorCount is updated in the link tuner.
5688c2ecf20Sopenharmony_ci	 */
5698c2ecf20Sopenharmony_ci	memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	return 0;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_civoid rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
5768c2ecf20Sopenharmony_ci				struct ieee80211_vif *vif,
5778c2ecf20Sopenharmony_ci				struct ieee80211_bss_conf *bss_conf,
5788c2ecf20Sopenharmony_ci				u32 changes)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
5818c2ecf20Sopenharmony_ci	struct rt2x00_intf *intf = vif_to_intf(vif);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	/*
5848c2ecf20Sopenharmony_ci	 * mac80211 might be calling this function while we are trying
5858c2ecf20Sopenharmony_ci	 * to remove the device or perhaps suspending it.
5868c2ecf20Sopenharmony_ci	 */
5878c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
5888c2ecf20Sopenharmony_ci		return;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	/*
5918c2ecf20Sopenharmony_ci	 * Update the BSSID.
5928c2ecf20Sopenharmony_ci	 */
5938c2ecf20Sopenharmony_ci	if (changes & BSS_CHANGED_BSSID)
5948c2ecf20Sopenharmony_ci		rt2x00lib_config_intf(rt2x00dev, intf, vif->type, NULL,
5958c2ecf20Sopenharmony_ci				      bss_conf->bssid);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	/*
5988c2ecf20Sopenharmony_ci	 * Start/stop beaconing.
5998c2ecf20Sopenharmony_ci	 */
6008c2ecf20Sopenharmony_ci	if (changes & BSS_CHANGED_BEACON_ENABLED) {
6018c2ecf20Sopenharmony_ci		mutex_lock(&intf->beacon_skb_mutex);
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		/*
6048c2ecf20Sopenharmony_ci		 * Clear the 'enable_beacon' flag and clear beacon because
6058c2ecf20Sopenharmony_ci		 * the beacon queue has been stopped after hardware reset.
6068c2ecf20Sopenharmony_ci		 */
6078c2ecf20Sopenharmony_ci		if (test_bit(DEVICE_STATE_RESET, &rt2x00dev->flags) &&
6088c2ecf20Sopenharmony_ci		    intf->enable_beacon) {
6098c2ecf20Sopenharmony_ci			intf->enable_beacon = false;
6108c2ecf20Sopenharmony_ci			rt2x00queue_clear_beacon(rt2x00dev, vif);
6118c2ecf20Sopenharmony_ci		}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci		if (!bss_conf->enable_beacon && intf->enable_beacon) {
6148c2ecf20Sopenharmony_ci			rt2x00dev->intf_beaconing--;
6158c2ecf20Sopenharmony_ci			intf->enable_beacon = false;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci			if (rt2x00dev->intf_beaconing == 0) {
6188c2ecf20Sopenharmony_ci				/*
6198c2ecf20Sopenharmony_ci				 * Last beaconing interface disabled
6208c2ecf20Sopenharmony_ci				 * -> stop beacon queue.
6218c2ecf20Sopenharmony_ci				 */
6228c2ecf20Sopenharmony_ci				rt2x00queue_stop_queue(rt2x00dev->bcn);
6238c2ecf20Sopenharmony_ci			}
6248c2ecf20Sopenharmony_ci			/*
6258c2ecf20Sopenharmony_ci			 * Clear beacon in the H/W for this vif. This is needed
6268c2ecf20Sopenharmony_ci			 * to disable beaconing on this particular interface
6278c2ecf20Sopenharmony_ci			 * and keep it running on other interfaces.
6288c2ecf20Sopenharmony_ci			 */
6298c2ecf20Sopenharmony_ci			rt2x00queue_clear_beacon(rt2x00dev, vif);
6308c2ecf20Sopenharmony_ci		} else if (bss_conf->enable_beacon && !intf->enable_beacon) {
6318c2ecf20Sopenharmony_ci			rt2x00dev->intf_beaconing++;
6328c2ecf20Sopenharmony_ci			intf->enable_beacon = true;
6338c2ecf20Sopenharmony_ci			/*
6348c2ecf20Sopenharmony_ci			 * Upload beacon to the H/W. This is only required on
6358c2ecf20Sopenharmony_ci			 * USB devices. PCI devices fetch beacons periodically.
6368c2ecf20Sopenharmony_ci			 */
6378c2ecf20Sopenharmony_ci			if (rt2x00_is_usb(rt2x00dev))
6388c2ecf20Sopenharmony_ci				rt2x00queue_update_beacon(rt2x00dev, vif);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci			if (rt2x00dev->intf_beaconing == 1) {
6418c2ecf20Sopenharmony_ci				/*
6428c2ecf20Sopenharmony_ci				 * First beaconing interface enabled
6438c2ecf20Sopenharmony_ci				 * -> start beacon queue.
6448c2ecf20Sopenharmony_ci				 */
6458c2ecf20Sopenharmony_ci				rt2x00queue_start_queue(rt2x00dev->bcn);
6468c2ecf20Sopenharmony_ci			}
6478c2ecf20Sopenharmony_ci		}
6488c2ecf20Sopenharmony_ci		mutex_unlock(&intf->beacon_skb_mutex);
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	/*
6528c2ecf20Sopenharmony_ci	 * When the association status has changed we must reset the link
6538c2ecf20Sopenharmony_ci	 * tuner counter. This is because some drivers determine if they
6548c2ecf20Sopenharmony_ci	 * should perform link tuning based on the number of seconds
6558c2ecf20Sopenharmony_ci	 * while associated or not associated.
6568c2ecf20Sopenharmony_ci	 */
6578c2ecf20Sopenharmony_ci	if (changes & BSS_CHANGED_ASSOC) {
6588c2ecf20Sopenharmony_ci		rt2x00dev->link.count = 0;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci		if (bss_conf->assoc)
6618c2ecf20Sopenharmony_ci			rt2x00dev->intf_associated++;
6628c2ecf20Sopenharmony_ci		else
6638c2ecf20Sopenharmony_ci			rt2x00dev->intf_associated--;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci		rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	/*
6698c2ecf20Sopenharmony_ci	 * When the erp information has changed, we should perform
6708c2ecf20Sopenharmony_ci	 * additional configuration steps. For all other changes we are done.
6718c2ecf20Sopenharmony_ci	 */
6728c2ecf20Sopenharmony_ci	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_PREAMBLE |
6738c2ecf20Sopenharmony_ci		       BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BASIC_RATES |
6748c2ecf20Sopenharmony_ci		       BSS_CHANGED_BEACON_INT | BSS_CHANGED_HT))
6758c2ecf20Sopenharmony_ci		rt2x00lib_config_erp(rt2x00dev, intf, bss_conf, changes);
6768c2ecf20Sopenharmony_ci}
6778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ciint rt2x00mac_conf_tx(struct ieee80211_hw *hw,
6808c2ecf20Sopenharmony_ci		      struct ieee80211_vif *vif, u16 queue_idx,
6818c2ecf20Sopenharmony_ci		      const struct ieee80211_tx_queue_params *params)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
6848c2ecf20Sopenharmony_ci	struct data_queue *queue;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
6878c2ecf20Sopenharmony_ci	if (unlikely(!queue))
6888c2ecf20Sopenharmony_ci		return -EINVAL;
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/*
6918c2ecf20Sopenharmony_ci	 * The passed variables are stored as real value ((2^n)-1).
6928c2ecf20Sopenharmony_ci	 * Ralink registers require to know the bit number 'n'.
6938c2ecf20Sopenharmony_ci	 */
6948c2ecf20Sopenharmony_ci	if (params->cw_min > 0)
6958c2ecf20Sopenharmony_ci		queue->cw_min = fls(params->cw_min);
6968c2ecf20Sopenharmony_ci	else
6978c2ecf20Sopenharmony_ci		queue->cw_min = 5; /* cw_min: 2^5 = 32. */
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	if (params->cw_max > 0)
7008c2ecf20Sopenharmony_ci		queue->cw_max = fls(params->cw_max);
7018c2ecf20Sopenharmony_ci	else
7028c2ecf20Sopenharmony_ci		queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	queue->aifs = params->aifs;
7058c2ecf20Sopenharmony_ci	queue->txop = params->txop;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	rt2x00_dbg(rt2x00dev,
7088c2ecf20Sopenharmony_ci		   "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d, TXop: %d\n",
7098c2ecf20Sopenharmony_ci		   queue_idx, queue->cw_min, queue->cw_max, queue->aifs,
7108c2ecf20Sopenharmony_ci		   queue->txop);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	return 0;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_civoid rt2x00mac_rfkill_poll(struct ieee80211_hw *hw)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
7198c2ecf20Sopenharmony_ci	bool active = !!rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	wiphy_rfkill_set_hw_state(hw->wiphy, !active);
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_rfkill_poll);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_civoid rt2x00mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7268c2ecf20Sopenharmony_ci		     u32 queues, bool drop)
7278c2ecf20Sopenharmony_ci{
7288c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
7298c2ecf20Sopenharmony_ci	struct data_queue *queue;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
7328c2ecf20Sopenharmony_ci		return;
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	set_bit(DEVICE_STATE_FLUSHING, &rt2x00dev->flags);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	tx_queue_for_each(rt2x00dev, queue)
7378c2ecf20Sopenharmony_ci		rt2x00queue_flush_queue(queue, drop);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	clear_bit(DEVICE_STATE_FLUSHING, &rt2x00dev->flags);
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_flush);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ciint rt2x00mac_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
7448c2ecf20Sopenharmony_ci{
7458c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
7468c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
7478c2ecf20Sopenharmony_ci	struct antenna_setup *def = &rt2x00dev->default_ant;
7488c2ecf20Sopenharmony_ci	struct antenna_setup setup;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	// The antenna value is not supposed to be 0,
7518c2ecf20Sopenharmony_ci	// or exceed the maximum number of antenna's.
7528c2ecf20Sopenharmony_ci	if (!tx_ant || (tx_ant & ~3) || !rx_ant || (rx_ant & ~3))
7538c2ecf20Sopenharmony_ci		return -EINVAL;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	// When the client tried to configure the antenna to or from
7568c2ecf20Sopenharmony_ci	// diversity mode, we must reset the default antenna as well
7578c2ecf20Sopenharmony_ci	// as that controls the diversity switch.
7588c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_TX_DIVERSITY && tx_ant != 3)
7598c2ecf20Sopenharmony_ci		ant->flags &= ~ANTENNA_TX_DIVERSITY;
7608c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_RX_DIVERSITY && rx_ant != 3)
7618c2ecf20Sopenharmony_ci		ant->flags &= ~ANTENNA_RX_DIVERSITY;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	// If diversity is being enabled, check if we need hardware
7648c2ecf20Sopenharmony_ci	// or software diversity. In the latter case, reset the value,
7658c2ecf20Sopenharmony_ci	// and make sure we update the antenna flags to have the
7668c2ecf20Sopenharmony_ci	// link tuner pick up the diversity tuning.
7678c2ecf20Sopenharmony_ci	if (tx_ant == 3 && def->tx == ANTENNA_SW_DIVERSITY) {
7688c2ecf20Sopenharmony_ci		tx_ant = ANTENNA_SW_DIVERSITY;
7698c2ecf20Sopenharmony_ci		ant->flags |= ANTENNA_TX_DIVERSITY;
7708c2ecf20Sopenharmony_ci	}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	if (rx_ant == 3 && def->rx == ANTENNA_SW_DIVERSITY) {
7738c2ecf20Sopenharmony_ci		rx_ant = ANTENNA_SW_DIVERSITY;
7748c2ecf20Sopenharmony_ci		ant->flags |= ANTENNA_RX_DIVERSITY;
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	setup.tx = tx_ant;
7788c2ecf20Sopenharmony_ci	setup.rx = rx_ant;
7798c2ecf20Sopenharmony_ci	setup.rx_chain_num = 0;
7808c2ecf20Sopenharmony_ci	setup.tx_chain_num = 0;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	rt2x00lib_config_antenna(rt2x00dev, setup);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	return 0;
7858c2ecf20Sopenharmony_ci}
7868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_set_antenna);
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ciint rt2x00mac_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
7918c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
7928c2ecf20Sopenharmony_ci	struct antenna_setup *active = &rt2x00dev->link.ant.active;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	// When software diversity is active, we must report this to the
7958c2ecf20Sopenharmony_ci	// client and not the current active antenna state.
7968c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_TX_DIVERSITY)
7978c2ecf20Sopenharmony_ci		*tx_ant = ANTENNA_HW_DIVERSITY;
7988c2ecf20Sopenharmony_ci	else
7998c2ecf20Sopenharmony_ci		*tx_ant = active->tx;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_RX_DIVERSITY)
8028c2ecf20Sopenharmony_ci		*rx_ant = ANTENNA_HW_DIVERSITY;
8038c2ecf20Sopenharmony_ci	else
8048c2ecf20Sopenharmony_ci		*rx_ant = active->rx;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	return 0;
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_get_antenna);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_civoid rt2x00mac_get_ringparam(struct ieee80211_hw *hw,
8118c2ecf20Sopenharmony_ci			     u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
8148c2ecf20Sopenharmony_ci	struct data_queue *queue;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	tx_queue_for_each(rt2x00dev, queue) {
8178c2ecf20Sopenharmony_ci		*tx += queue->length;
8188c2ecf20Sopenharmony_ci		*tx_max += queue->limit;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	*rx = rt2x00dev->rx->length;
8228c2ecf20Sopenharmony_ci	*rx_max = rt2x00dev->rx->limit;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_get_ringparam);
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_cibool rt2x00mac_tx_frames_pending(struct ieee80211_hw *hw)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev = hw->priv;
8298c2ecf20Sopenharmony_ci	struct data_queue *queue;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	tx_queue_for_each(rt2x00dev, queue) {
8328c2ecf20Sopenharmony_ci		if (!rt2x00queue_empty(queue))
8338c2ecf20Sopenharmony_ci			return true;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	return false;
8378c2ecf20Sopenharmony_ci}
8388c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rt2x00mac_tx_frames_pending);
839