xref: /openbmc/linux/drivers/net/wireless/intel/iwlwifi/mvm/sf.c (revision b0bc615df488abd0e95107e4a9ecefb9bf8c250a)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2013-2014, 2018-2019, 2022 Intel Corporation
4  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5  */
6 #include "mvm.h"
7 
8 /* For counting bound interfaces */
9 struct iwl_mvm_active_iface_iterator_data {
10 	struct ieee80211_vif *ignore_vif;
11 	u8 sta_vif_ap_sta_id;
12 	enum iwl_sf_state sta_vif_state;
13 	u32 num_active_macs;
14 };
15 
16 /*
17  * Count bound interfaces which are not p2p, besides data->ignore_vif.
18  * data->station_vif will point to one bound vif of type station, if exists.
19  */
20 static void iwl_mvm_bound_iface_iterator(void *_data, u8 *mac,
21 					 struct ieee80211_vif *vif)
22 {
23 	struct iwl_mvm_active_iface_iterator_data *data = _data;
24 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
25 
26 	if (vif == data->ignore_vif || !mvmvif->deflink.phy_ctxt ||
27 	    vif->type == NL80211_IFTYPE_P2P_DEVICE)
28 		return;
29 
30 	data->num_active_macs++;
31 
32 	if (vif->type == NL80211_IFTYPE_STATION) {
33 		data->sta_vif_ap_sta_id = mvmvif->deflink.ap_sta_id;
34 		if (vif->cfg.assoc)
35 			data->sta_vif_state = SF_FULL_ON;
36 		else
37 			data->sta_vif_state = SF_INIT_OFF;
38 	}
39 }
40 
41 /*
42  * Aging and idle timeouts for the different possible scenarios
43  * in default configuration
44  */
45 static const
46 __le32 sf_full_timeout_def[SF_NUM_SCENARIO][SF_NUM_TIMEOUT_TYPES] = {
47 	{
48 		cpu_to_le32(SF_SINGLE_UNICAST_AGING_TIMER_DEF),
49 		cpu_to_le32(SF_SINGLE_UNICAST_IDLE_TIMER_DEF)
50 	},
51 	{
52 		cpu_to_le32(SF_AGG_UNICAST_AGING_TIMER_DEF),
53 		cpu_to_le32(SF_AGG_UNICAST_IDLE_TIMER_DEF)
54 	},
55 	{
56 		cpu_to_le32(SF_MCAST_AGING_TIMER_DEF),
57 		cpu_to_le32(SF_MCAST_IDLE_TIMER_DEF)
58 	},
59 	{
60 		cpu_to_le32(SF_BA_AGING_TIMER_DEF),
61 		cpu_to_le32(SF_BA_IDLE_TIMER_DEF)
62 	},
63 	{
64 		cpu_to_le32(SF_TX_RE_AGING_TIMER_DEF),
65 		cpu_to_le32(SF_TX_RE_IDLE_TIMER_DEF)
66 	},
67 };
68 
69 /*
70  * Aging and idle timeouts for the different possible scenarios
71  * in single BSS MAC configuration.
72  */
73 static const __le32 sf_full_timeout[SF_NUM_SCENARIO][SF_NUM_TIMEOUT_TYPES] = {
74 	{
75 		cpu_to_le32(SF_SINGLE_UNICAST_AGING_TIMER),
76 		cpu_to_le32(SF_SINGLE_UNICAST_IDLE_TIMER)
77 	},
78 	{
79 		cpu_to_le32(SF_AGG_UNICAST_AGING_TIMER),
80 		cpu_to_le32(SF_AGG_UNICAST_IDLE_TIMER)
81 	},
82 	{
83 		cpu_to_le32(SF_MCAST_AGING_TIMER),
84 		cpu_to_le32(SF_MCAST_IDLE_TIMER)
85 	},
86 	{
87 		cpu_to_le32(SF_BA_AGING_TIMER),
88 		cpu_to_le32(SF_BA_IDLE_TIMER)
89 	},
90 	{
91 		cpu_to_le32(SF_TX_RE_AGING_TIMER),
92 		cpu_to_le32(SF_TX_RE_IDLE_TIMER)
93 	},
94 };
95 
96 static void iwl_mvm_fill_sf_command(struct iwl_mvm *mvm,
97 				    struct iwl_sf_cfg_cmd *sf_cmd,
98 				    struct ieee80211_sta *sta)
99 {
100 	int i, j, watermark;
101 	u8 max_rx_nss = 0;
102 	bool is_legacy = true;
103 	struct ieee80211_link_sta *link_sta;
104 	unsigned int link_id;
105 
106 	sf_cmd->watermark[SF_LONG_DELAY_ON] = cpu_to_le32(SF_W_MARK_SCAN);
107 
108 	/*
109 	 * If we are in association flow - check antenna configuration
110 	 * capabilities of the AP station, and choose the watermark accordingly.
111 	 */
112 	if (sta) {
113 		/* find the maximal NSS number among all links (if relevant) */
114 		rcu_read_lock();
115 		for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) {
116 			link_sta = rcu_dereference(sta->link[link_id]);
117 			if (!link_sta)
118 				continue;
119 
120 			if (link_sta->ht_cap.ht_supported ||
121 			    link_sta->vht_cap.vht_supported ||
122 			    link_sta->eht_cap.has_eht ||
123 			    link_sta->he_cap.has_he) {
124 				is_legacy = false;
125 				max_rx_nss = max(max_rx_nss, link_sta->rx_nss);
126 			}
127 		}
128 		rcu_read_unlock();
129 
130 		if (!is_legacy) {
131 			switch (max_rx_nss) {
132 			case 1:
133 				watermark = SF_W_MARK_SISO;
134 				break;
135 			case 2:
136 				watermark = SF_W_MARK_MIMO2;
137 				break;
138 			default:
139 				watermark = SF_W_MARK_MIMO3;
140 				break;
141 			}
142 		} else {
143 			watermark = SF_W_MARK_LEGACY;
144 		}
145 	/* default watermark value for unassociated mode. */
146 	} else {
147 		watermark = SF_W_MARK_MIMO2;
148 	}
149 	sf_cmd->watermark[SF_FULL_ON] = cpu_to_le32(watermark);
150 
151 	for (i = 0; i < SF_NUM_SCENARIO; i++) {
152 		for (j = 0; j < SF_NUM_TIMEOUT_TYPES; j++) {
153 			sf_cmd->long_delay_timeouts[i][j] =
154 					cpu_to_le32(SF_LONG_DELAY_AGING_TIMER);
155 		}
156 	}
157 
158 	if (sta) {
159 		BUILD_BUG_ON(sizeof(sf_full_timeout) !=
160 			     sizeof(__le32) * SF_NUM_SCENARIO *
161 			     SF_NUM_TIMEOUT_TYPES);
162 
163 		memcpy(sf_cmd->full_on_timeouts, sf_full_timeout,
164 		       sizeof(sf_full_timeout));
165 	} else {
166 		BUILD_BUG_ON(sizeof(sf_full_timeout_def) !=
167 			     sizeof(__le32) * SF_NUM_SCENARIO *
168 			     SF_NUM_TIMEOUT_TYPES);
169 
170 		memcpy(sf_cmd->full_on_timeouts, sf_full_timeout_def,
171 		       sizeof(sf_full_timeout_def));
172 	}
173 }
174 
175 static int iwl_mvm_sf_config(struct iwl_mvm *mvm, u8 sta_id,
176 			     enum iwl_sf_state new_state)
177 {
178 	struct iwl_sf_cfg_cmd sf_cmd = {
179 		.state = cpu_to_le32(new_state),
180 	};
181 	struct ieee80211_sta *sta;
182 	int ret = 0;
183 
184 	if (mvm->cfg->disable_dummy_notification)
185 		sf_cmd.state |= cpu_to_le32(SF_CFG_DUMMY_NOTIF_OFF);
186 
187 	/*
188 	 * If an associated AP sta changed its antenna configuration, the state
189 	 * will remain FULL_ON but SF parameters need to be reconsidered.
190 	 */
191 	if (new_state != SF_FULL_ON && mvm->sf_state == new_state)
192 		return 0;
193 
194 	switch (new_state) {
195 	case SF_UNINIT:
196 		iwl_mvm_fill_sf_command(mvm, &sf_cmd, NULL);
197 		break;
198 	case SF_FULL_ON:
199 		if (sta_id == IWL_MVM_INVALID_STA) {
200 			IWL_ERR(mvm,
201 				"No station: Cannot switch SF to FULL_ON\n");
202 			return -EINVAL;
203 		}
204 		rcu_read_lock();
205 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
206 		if (IS_ERR_OR_NULL(sta)) {
207 			IWL_ERR(mvm, "Invalid station id\n");
208 			rcu_read_unlock();
209 			return -EINVAL;
210 		}
211 		iwl_mvm_fill_sf_command(mvm, &sf_cmd, sta);
212 		rcu_read_unlock();
213 		break;
214 	case SF_INIT_OFF:
215 		iwl_mvm_fill_sf_command(mvm, &sf_cmd, NULL);
216 		break;
217 	default:
218 		WARN_ONCE(1, "Invalid state: %d. not sending Smart Fifo cmd\n",
219 			  new_state);
220 		return -EINVAL;
221 	}
222 
223 	ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_SF_CFG_CMD, CMD_ASYNC,
224 				   sizeof(sf_cmd), &sf_cmd);
225 	if (!ret)
226 		mvm->sf_state = new_state;
227 
228 	return ret;
229 }
230 
231 /*
232  * Update Smart fifo:
233  * Count bound interfaces that are not to be removed, ignoring p2p devices,
234  * and set new state accordingly.
235  */
236 int iwl_mvm_sf_update(struct iwl_mvm *mvm, struct ieee80211_vif *changed_vif,
237 		      bool remove_vif)
238 {
239 	enum iwl_sf_state new_state;
240 	u8 sta_id = IWL_MVM_INVALID_STA;
241 	struct iwl_mvm_vif *mvmvif = NULL;
242 	struct iwl_mvm_active_iface_iterator_data data = {
243 		.ignore_vif = changed_vif,
244 		.sta_vif_state = SF_UNINIT,
245 		.sta_vif_ap_sta_id = IWL_MVM_INVALID_STA,
246 	};
247 
248 	/*
249 	 * Ignore the call if we are in HW Restart flow, or if the handled
250 	 * vif is a p2p device.
251 	 */
252 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
253 	    (changed_vif && changed_vif->type == NL80211_IFTYPE_P2P_DEVICE))
254 		return 0;
255 
256 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
257 						   IEEE80211_IFACE_ITER_NORMAL,
258 						   iwl_mvm_bound_iface_iterator,
259 						   &data);
260 
261 	/* If changed_vif exists and is not to be removed, add to the count */
262 	if (changed_vif && !remove_vif)
263 		data.num_active_macs++;
264 
265 	switch (data.num_active_macs) {
266 	case 0:
267 		/* If there are no active macs - change state to SF_INIT_OFF */
268 		new_state = SF_INIT_OFF;
269 		break;
270 	case 1:
271 		if (remove_vif) {
272 			/* The one active mac left is of type station
273 			 * and we filled the relevant data during iteration
274 			 */
275 			new_state = data.sta_vif_state;
276 			sta_id = data.sta_vif_ap_sta_id;
277 		} else {
278 			if (WARN_ON(!changed_vif))
279 				return -EINVAL;
280 			if (changed_vif->type != NL80211_IFTYPE_STATION) {
281 				new_state = SF_UNINIT;
282 			} else if (changed_vif->cfg.assoc &&
283 				   changed_vif->bss_conf.dtim_period) {
284 				mvmvif = iwl_mvm_vif_from_mac80211(changed_vif);
285 				sta_id = mvmvif->deflink.ap_sta_id;
286 				new_state = SF_FULL_ON;
287 			} else {
288 				new_state = SF_INIT_OFF;
289 			}
290 		}
291 		break;
292 	default:
293 		/* If there are multiple active macs - change to SF_UNINIT */
294 		new_state = SF_UNINIT;
295 	}
296 
297 	/* For MLO it's ok to use deflink->sta_id as it's needed only to get
298 	 * a pointer to mac80211 sta
299 	 */
300 	return iwl_mvm_sf_config(mvm, sta_id, new_state);
301 }
302