From 193bfea2185a0ee976f54812e41ace77e6ee85e4 Mon Sep 17 00:00:00 2001
From: Sriram R <srirrama@codeaurora.org>
Date: Fri, 10 Jul 2020 12:46:12 +0530
Subject: [PATCH 1/3] mac80211: add nss support

Add Support for NSS Offload if the HW supports it.
New flag is introduced to indicate HW support for NSS
offload

Signed-off-by: Sriram R <srirrama@codeaurora.org>
---
 include/net/mac80211.h | 13 +++++++++++++
 net/mac80211/debugfs.c |  1 +
 net/mac80211/util.c    | 16 ++++++++++++++++
 3 files changed, 30 insertions(+)

--- a/include/net/mac80211.h
+++ b/include/net/mac80211.h
@@ -359,6 +359,20 @@ enum ieee80211_bss_change {
 	/* when adding here, make sure to change ieee80211_reconfig */
 };
 
+/**
+ * enum ieee80211_nss_bss_change - NSS BSS change notification flags
+ *
+ * These flags are used with the nss_bss_info_changed() callback
+ * to indicate which NSS BSS parameter changed.
+ *
+ * @BSS_CHANGED_NSS_AP_ISOLATE: AP Isolate feature in NSS mode
+ *
+ */
+
+enum ieee80211_nss_bss_change {
+	BSS_CHANGED_NSS_AP_ISOLATE  = BIT(0),
+};
+
 /*
  * The maximum number of IPv4 addresses listed for ARP filtering. If the number
  * of addresses for an interface increase beyond this value, hardware ARP
@@ -631,6 +645,7 @@ struct ieee80211_fils_discovery {
  * @s1g: BSS is S1G BSS (affects Association Request format).
  * @beacon_tx_rate: The configured beacon transmit rate that needs to be passed
  *	to driver when rate control is offloaded to firmware.
+ * @nss_ap_isolate: Used for notifying the NSS host about AP isolate feature
  */
 struct ieee80211_bss_conf {
 	const u8 *bssid;
@@ -701,6 +716,7 @@ struct ieee80211_bss_conf {
 	u32 unsol_bcast_probe_resp_interval;
 	bool s1g;
 	struct cfg80211_bitrate_mask beacon_tx_rate;
+	bool nss_ap_isolate;
 };
 
 /**
@@ -1781,6 +1797,16 @@ struct ieee80211_vif *wdev_to_ieee80211_
 struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif);
 
 /**
+ * ieee80211_vif_to_wdev_relaxed - return a wdev struct from a vif
+ * @vif: the vif to get the wdev for
+ *
+ * This function is similar to ieee80211_vif_to_wdev, but the wdev
+ * is returned even if sdata is not running.
+ *
+ */
+struct wireless_dev *ieee80211_vif_to_wdev_relaxed(struct ieee80211_vif *vif);
+
+/**
  * enum ieee80211_key_flags - key flags
  *
  * These flags are used for communication about keys between the driver
@@ -2407,6 +2433,8 @@ struct ieee80211_txq {
  * @IEEE80211_HW_SUPPORTS_CONC_MON_RX_DECAP: Hardware supports concurrent rx
  *	decapsulation offload and passing raw 802.11 frames for monitor iface.
  *
+ *  @IEEE80211_HW_SUPPORTS_NSS_OFFLOAD: Hardware/driver supports NSS offload
+ *
  * @NUM_IEEE80211_HW_FLAGS: number of hardware flags, used for sizing arrays
  */
 enum ieee80211_hw_flags {
@@ -2462,6 +2490,7 @@ enum ieee80211_hw_flags {
 	IEEE80211_HW_SUPPORTS_TX_ENCAP_OFFLOAD,
 	IEEE80211_HW_SUPPORTS_RX_DECAP_OFFLOAD,
 	IEEE80211_HW_SUPPORTS_CONC_MON_RX_DECAP,
+	IEEE80211_HW_SUPPORTS_NSS_OFFLOAD,
 
 	/* keep last, obviously */
 	NUM_IEEE80211_HW_FLAGS
@@ -3440,6 +3469,10 @@ enum ieee80211_reconfig_type {
  *	of the bss parameters has changed when a call is made. The callback
  *	can sleep.
  *
+ * @nss_bss_info_changed: Handler for configuration requests related to NSS BSS
+ * parameters that may vary during BSS's lifespan, and may affect low level
+ * driver.
+ *
  * @prepare_multicast: Prepare for multicast filter configuration.
  *	This callback is optional, and its return value is passed
  *	to configure_filter(). This callback must be atomic.
@@ -3927,6 +3960,9 @@ struct ieee80211_ops {
 				 struct ieee80211_vif *vif,
 				 struct ieee80211_bss_conf *info,
 				 u32 changed);
+	void (*nss_bss_info_changed)(struct ieee80211_hw *hw,
+				 struct ieee80211_vif *vif,
+				 u32 changed);
 
 	int (*start_ap)(struct ieee80211_hw *hw, struct ieee80211_vif *vif);
 	void (*stop_ap)(struct ieee80211_hw *hw, struct ieee80211_vif *vif);
--- a/net/mac80211/debugfs.c
+++ b/net/mac80211/debugfs.c
@@ -407,6 +407,7 @@ static const char *hw_flag_names[] = {
 	FLAG(SUPPORTS_TX_ENCAP_OFFLOAD),
 	FLAG(SUPPORTS_RX_DECAP_OFFLOAD),
 	FLAG(SUPPORTS_CONC_MON_RX_DECAP),
+	FLAG(SUPPORTS_NSS_OFFLOAD),
 #undef FLAG
 };
 
--- a/net/mac80211/util.c
+++ b/net/mac80211/util.c
@@ -881,6 +881,22 @@ struct wireless_dev *ieee80211_vif_to_wd
 }
 EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
 
+struct wireless_dev *ieee80211_vif_to_wdev_relaxed(struct ieee80211_vif *vif)
+{
+	struct ieee80211_sub_if_data *sdata;
+
+	if (!vif)
+		return NULL;
+
+	sdata = vif_to_sdata(vif);
+
+	if (sdata)
+		return &sdata->wdev;
+
+	return NULL;
+}
+EXPORT_SYMBOL(ieee80211_vif_to_wdev_relaxed);
+
 /*
  * Nothing should have been stuffed into the workqueue during
  * the suspend->resume cycle. Since we can't check each caller
--- a/net/mac80211/main.c
+++ b/net/mac80211/main.c
@@ -210,6 +210,17 @@ void ieee80211_bss_info_change_notify(st
 	drv_bss_info_changed(local, sdata, &sdata->vif.bss_conf, changed);
 }
 
+void ieee80211_nss_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
+				      u32 changed)
+{
+	struct ieee80211_local *local = sdata->local;
+
+	if (!changed || sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
+		return;
+
+	drv_nss_bss_info_changed(local, sdata, &sdata->vif.bss_conf, changed);
+}
+
 u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
 {
 	sdata->vif.bss_conf.use_cts_prot = false;
@@ -598,12 +609,6 @@ struct ieee80211_hw *ieee80211_alloc_hw_
 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_FILS_STA);
 #endif
 	wiphy_ext_feature_set(wiphy,
-			      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211);
-	wiphy_ext_feature_set(wiphy,
-			      NL80211_EXT_FEATURE_CONTROL_PORT_NO_PREAUTH);
-	wiphy_ext_feature_set(wiphy,
-			      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211_TX_STATUS);
-	wiphy_ext_feature_set(wiphy,
 			      NL80211_EXT_FEATURE_SCAN_FREQ_KHZ);
 
 	if (!ops->hw_scan) {
@@ -921,6 +926,18 @@ int ieee80211_register_hw(struct ieee802
 		    (!local->ops->start_nan || !local->ops->stop_nan)))
 		return -EINVAL;
 
+	/* Control port over nl80211 is disabled for nss offload as
+	 * sending per packet tx status is not supported and only
+	 * rx over netdev from driver is done currently */
+	if (!ieee80211_hw_check(hw, SUPPORTS_NSS_OFFLOAD)) {
+		wiphy_ext_feature_set(hw->wiphy,
+				      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211);
+		wiphy_ext_feature_set(hw->wiphy,
+				      NL80211_EXT_FEATURE_CONTROL_PORT_NO_PREAUTH);
+		wiphy_ext_feature_set(hw->wiphy,
+				      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211_TX_STATUS);
+	}
+
 #ifdef CONFIG_PM
 	if (hw->wiphy->wowlan && (!local->ops->suspend || !local->ops->resume))
 		return -EINVAL;
--- a/net/mac80211/sta_info.c
+++ b/net/mac80211/sta_info.c
@@ -2092,7 +2092,10 @@ sta_get_last_rx_stats(struct sta_info *s
 	struct ieee80211_local *local = sta->local;
 	int cpu;
 
-	if (!ieee80211_hw_check(&local->hw, USES_RSS))
+	if (ieee80211_hw_check(&local->hw, SUPPORTS_NSS_OFFLOAD))
+		return stats;
+
+ 	if (!ieee80211_hw_check(&local->hw, USES_RSS))
 		return stats;
 
 	for_each_possible_cpu(cpu) {
--- a/net/mac80211/tx.c
+++ b/net/mac80211/tx.c
@@ -994,11 +994,23 @@ ieee80211_tx_h_stats(struct ieee80211_tx
 {
 	struct sk_buff *skb;
 	int ac = -1;
+	struct ieee80211_hdr *hdr;
+	bool nss_offload;
 
 	if (!tx->sta)
 		return TX_CONTINUE;
 
+	nss_offload = ieee80211_hw_check(&tx->local->hw, SUPPORTS_NSS_OFFLOAD);
+
 	skb_queue_walk(&tx->skbs, skb) {
+		/* Do not increment stats for data packets if NSS offload is enabled.
+		 * As we use the stats from NSS, this will be a duplication
+		 */
+		if (nss_offload) {
+			hdr = (void *) skb->data;
+			if (ieee80211_is_data(hdr->frame_control))
+				continue;
+		}
 		ac = skb_get_queue_mapping(skb);
 		tx->sta->tx_stats.bytes[ac] += skb->len;
 	}
@@ -2751,7 +2763,9 @@ static struct sk_buff *ieee80211_build_h
 
 	if (unlikely(!multicast && ((skb->sk &&
 		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
-		     ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
+		     ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS) &&
+		     !(ieee80211_hw_check(&local->hw, SUPPORTS_NSS_OFFLOAD) &&
+		       ieee80211_is_data(fc) && !ieee80211_is_qos_nullfunc(fc))))
 		info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
 						  cookie);
 
@@ -4210,7 +4224,8 @@ static void ieee80211_8023_xmit(struct i
 	}
 
 	if (unlikely(skb->sk &&
-		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
+		     skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS &&
+		     !ieee80211_hw_check(&local->hw, SUPPORTS_NSS_OFFLOAD)))
 		info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
 							     &info->flags, NULL);
 
@@ -4218,8 +4233,10 @@ static void ieee80211_8023_xmit(struct i
 
 	dev_sw_netstats_tx_add(dev, 1, skb->len);
 
-	sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
-	sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
+	if (!ieee80211_hw_check(&local->hw, SUPPORTS_NSS_OFFLOAD)) {
+		sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
+		sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
+	}
 
 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
 		sdata = container_of(sdata->bss,
--- a/net/wireless/util.c
+++ b/net/wireless/util.c
@@ -2084,6 +2084,9 @@ bool cfg80211_does_bw_fit_range(const st
 
 int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
 {
+	if(sinfo->pertid)
+		return 0;
+
 	sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
 				sizeof(*(sinfo->pertid)),
 				gfp);
--- a/net/mac80211/cfg.c
+++ b/net/mac80211/cfg.c
@@ -2280,7 +2280,7 @@ static int ieee80211_change_bss(struct w
 {
 	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
 	struct ieee80211_supported_band *sband;
-	u32 changed = 0;
+	u32 changed = 0, nss_changed = 0;;
 
 	if (!sdata_dereference(sdata->u.ap.beacon, sdata))
 		return -ENOENT;
@@ -2327,6 +2327,8 @@ static int ieee80211_change_bss(struct w
 			sdata->flags |= IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
 		else
 			sdata->flags &= ~IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
+		sdata->vif.bss_conf.nss_ap_isolate = params->ap_isolate;
+		nss_changed |= BSS_CHANGED_NSS_AP_ISOLATE;
 		ieee80211_check_fast_rx_iface(sdata);
 	}
 
@@ -2356,6 +2358,8 @@ static int ieee80211_change_bss(struct w
 
 	ieee80211_bss_info_change_notify(sdata, changed);
 
+	ieee80211_nss_bss_info_change_notify(sdata, nss_changed);
+
 	return 0;
 }
 
--- a/net/mac80211/driver-ops.h
+++ b/net/mac80211/driver-ops.h
@@ -178,6 +178,23 @@ static inline void drv_bss_info_changed(
 	trace_drv_return_void(local);
 }
 
+static inline void drv_nss_bss_info_changed(struct ieee80211_local *local,
+					struct ieee80211_sub_if_data *sdata,
+					struct ieee80211_bss_conf *info,
+					u32 changed)
+{
+	might_sleep();
+
+	if (!check_sdata_in_driver(sdata))
+		return;
+
+	trace_drv_nss_bss_info_changed(local, sdata, info, changed);
+	if (local->ops->nss_bss_info_changed) {
+		local->ops->nss_bss_info_changed(&local->hw, &sdata->vif, changed);
+	}
+	trace_drv_nss_return_void(local);
+}
+
 static inline u64 drv_prepare_multicast(struct ieee80211_local *local,
 					struct netdev_hw_addr_list *mc_list)
 {
--- a/net/mac80211/ieee80211_i.h
+++ b/net/mac80211/ieee80211_i.h
@@ -1626,6 +1626,8 @@ int ieee80211_hw_config(struct ieee80211
 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx);
 void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
 				      u32 changed);
+void ieee80211_nss_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
+				      u32 changed);
 void ieee80211_configure_filter(struct ieee80211_local *local);
 u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata);
 
--- a/net/mac80211/trace.h
+++ b/net/mac80211/trace.h
@@ -389,6 +389,38 @@ TRACE_EVENT(drv_config,
 		LOCAL_PR_ARG, __entry->changed, CHANDEF_PR_ARG
 	)
 );
+TRACE_EVENT(drv_nss_bss_info_changed,
+	TP_PROTO(struct ieee80211_local *local,
+		 struct ieee80211_sub_if_data *sdata,
+		 struct ieee80211_bss_conf *info,
+		 u32 changed),
+
+	TP_ARGS(local, sdata, info, changed),
+
+	TP_STRUCT__entry(
+		LOCAL_ENTRY
+		VIF_ENTRY
+		__field(u32, changed)
+		__field(bool, nss_ap_isolate);
+	),
+
+	TP_fast_assign(
+		LOCAL_ASSIGN;
+		VIF_ASSIGN;
+		__entry->changed = changed;
+		__entry->nss_ap_isolate = info->nss_ap_isolate;
+	),
+
+	TP_printk(
+		LOCAL_PR_FMT  VIF_PR_FMT " changed:%#x",
+		LOCAL_PR_ARG, VIF_PR_ARG, __entry->changed
+	)
+);
+
+DEFINE_EVENT(local_only_evt, drv_nss_return_void,
+	TP_PROTO(struct ieee80211_local *local),
+	TP_ARGS(local)
+);
 
 TRACE_EVENT(drv_bss_info_changed,
 	TP_PROTO(struct ieee80211_local *local,
--- a/net/mac80211/iface.c
+++ b/net/mac80211/iface.c
@@ -863,7 +863,8 @@ static bool ieee80211_set_sdata_offload_
 		    local->hw.wiphy->frag_threshold != (u32)-1)
 			flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
 
-		if (local->monitors)
+		if (local->monitors &&
+		    !ieee80211_hw_check(&local->hw, SUPPORTS_NSS_OFFLOAD))
 			flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
 	} else {
 		flags &= ~IEEE80211_OFFLOAD_ENCAP_ENABLED;
