1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * The full GNU General Public License is included in this distribution
21  * in the file called COPYING.
22  *
23  * Contact Information:
24  *  Intel Linux Wireless <linuxwifi@intel.com>
25  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26  *
27  * BSD LICENSE
28  *
29  * Copyright(c) 2013 - 2014 Intel Corporation. All rights reserved.
30  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  *
37  *  * Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  *  * Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in
41  *    the documentation and/or other materials provided with the
42  *    distribution.
43  *  * Neither the name Intel Corporation nor the names of its
44  *    contributors may be used to endorse or promote products derived
45  *    from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59  *****************************************************************************/
60 #include "mvm.h"
61 
62 /* For counting bound interfaces */
63 struct iwl_mvm_active_iface_iterator_data {
64 	struct ieee80211_vif *ignore_vif;
65 	u8 sta_vif_ap_sta_id;
66 	enum iwl_sf_state sta_vif_state;
67 	int num_active_macs;
68 };
69 
70 /*
71  * Count bound interfaces which are not p2p, besides data->ignore_vif.
72  * data->station_vif will point to one bound vif of type station, if exists.
73  */
74 static void iwl_mvm_bound_iface_iterator(void *_data, u8 *mac,
75 					 struct ieee80211_vif *vif)
76 {
77 	struct iwl_mvm_active_iface_iterator_data *data = _data;
78 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
79 
80 	if (vif == data->ignore_vif || !mvmvif->phy_ctxt ||
81 	    vif->type == NL80211_IFTYPE_P2P_DEVICE)
82 		return;
83 
84 	data->num_active_macs++;
85 
86 	if (vif->type == NL80211_IFTYPE_STATION) {
87 		data->sta_vif_ap_sta_id = mvmvif->ap_sta_id;
88 		if (vif->bss_conf.assoc)
89 			data->sta_vif_state = SF_FULL_ON;
90 		else
91 			data->sta_vif_state = SF_INIT_OFF;
92 	}
93 }
94 
95 /*
96  * Aging and idle timeouts for the different possible scenarios
97  * in default configuration
98  */
99 static const
100 __le32 sf_full_timeout_def[SF_NUM_SCENARIO][SF_NUM_TIMEOUT_TYPES] = {
101 	{
102 		cpu_to_le32(SF_SINGLE_UNICAST_AGING_TIMER_DEF),
103 		cpu_to_le32(SF_SINGLE_UNICAST_IDLE_TIMER_DEF)
104 	},
105 	{
106 		cpu_to_le32(SF_AGG_UNICAST_AGING_TIMER_DEF),
107 		cpu_to_le32(SF_AGG_UNICAST_IDLE_TIMER_DEF)
108 	},
109 	{
110 		cpu_to_le32(SF_MCAST_AGING_TIMER_DEF),
111 		cpu_to_le32(SF_MCAST_IDLE_TIMER_DEF)
112 	},
113 	{
114 		cpu_to_le32(SF_BA_AGING_TIMER_DEF),
115 		cpu_to_le32(SF_BA_IDLE_TIMER_DEF)
116 	},
117 	{
118 		cpu_to_le32(SF_TX_RE_AGING_TIMER_DEF),
119 		cpu_to_le32(SF_TX_RE_IDLE_TIMER_DEF)
120 	},
121 };
122 
123 /*
124  * Aging and idle timeouts for the different possible scenarios
125  * in single BSS MAC configuration.
126  */
127 static const __le32 sf_full_timeout[SF_NUM_SCENARIO][SF_NUM_TIMEOUT_TYPES] = {
128 	{
129 		cpu_to_le32(SF_SINGLE_UNICAST_AGING_TIMER),
130 		cpu_to_le32(SF_SINGLE_UNICAST_IDLE_TIMER)
131 	},
132 	{
133 		cpu_to_le32(SF_AGG_UNICAST_AGING_TIMER),
134 		cpu_to_le32(SF_AGG_UNICAST_IDLE_TIMER)
135 	},
136 	{
137 		cpu_to_le32(SF_MCAST_AGING_TIMER),
138 		cpu_to_le32(SF_MCAST_IDLE_TIMER)
139 	},
140 	{
141 		cpu_to_le32(SF_BA_AGING_TIMER),
142 		cpu_to_le32(SF_BA_IDLE_TIMER)
143 	},
144 	{
145 		cpu_to_le32(SF_TX_RE_AGING_TIMER),
146 		cpu_to_le32(SF_TX_RE_IDLE_TIMER)
147 	},
148 };
149 
150 static void iwl_mvm_fill_sf_command(struct iwl_mvm *mvm,
151 				    struct iwl_sf_cfg_cmd *sf_cmd,
152 				    struct ieee80211_sta *sta)
153 {
154 	int i, j, watermark;
155 
156 	sf_cmd->watermark[SF_LONG_DELAY_ON] = cpu_to_le32(SF_W_MARK_SCAN);
157 
158 	/*
159 	 * If we are in association flow - check antenna configuration
160 	 * capabilities of the AP station, and choose the watermark accordingly.
161 	 */
162 	if (sta) {
163 		if (sta->ht_cap.ht_supported || sta->vht_cap.vht_supported) {
164 			switch (sta->rx_nss) {
165 			case 1:
166 				watermark = SF_W_MARK_SISO;
167 				break;
168 			case 2:
169 				watermark = SF_W_MARK_MIMO2;
170 				break;
171 			default:
172 				watermark = SF_W_MARK_MIMO3;
173 				break;
174 			}
175 		} else {
176 			watermark = SF_W_MARK_LEGACY;
177 		}
178 	/* default watermark value for unassociated mode. */
179 	} else {
180 		watermark = SF_W_MARK_MIMO2;
181 	}
182 	sf_cmd->watermark[SF_FULL_ON] = cpu_to_le32(watermark);
183 
184 	for (i = 0; i < SF_NUM_SCENARIO; i++) {
185 		for (j = 0; j < SF_NUM_TIMEOUT_TYPES; j++) {
186 			sf_cmd->long_delay_timeouts[i][j] =
187 					cpu_to_le32(SF_LONG_DELAY_AGING_TIMER);
188 		}
189 	}
190 
191 	if (sta) {
192 		BUILD_BUG_ON(sizeof(sf_full_timeout) !=
193 			     sizeof(__le32) * SF_NUM_SCENARIO *
194 			     SF_NUM_TIMEOUT_TYPES);
195 
196 		memcpy(sf_cmd->full_on_timeouts, sf_full_timeout,
197 		       sizeof(sf_full_timeout));
198 	} else {
199 		BUILD_BUG_ON(sizeof(sf_full_timeout_def) !=
200 			     sizeof(__le32) * SF_NUM_SCENARIO *
201 			     SF_NUM_TIMEOUT_TYPES);
202 
203 		memcpy(sf_cmd->full_on_timeouts, sf_full_timeout_def,
204 		       sizeof(sf_full_timeout_def));
205 	}
206 
207 }
208 
209 static int iwl_mvm_sf_config(struct iwl_mvm *mvm, u8 sta_id,
210 			     enum iwl_sf_state new_state)
211 {
212 	struct iwl_sf_cfg_cmd sf_cmd = {
213 		.state = cpu_to_le32(new_state),
214 	};
215 	struct ieee80211_sta *sta;
216 	int ret = 0;
217 
218 	if (mvm->cfg->disable_dummy_notification)
219 		sf_cmd.state |= cpu_to_le32(SF_CFG_DUMMY_NOTIF_OFF);
220 
221 	/*
222 	 * If an associated AP sta changed its antenna configuration, the state
223 	 * will remain FULL_ON but SF parameters need to be reconsidered.
224 	 */
225 	if (new_state != SF_FULL_ON && mvm->sf_state == new_state)
226 		return 0;
227 
228 	switch (new_state) {
229 	case SF_UNINIT:
230 		iwl_mvm_fill_sf_command(mvm, &sf_cmd, NULL);
231 		break;
232 	case SF_FULL_ON:
233 		if (sta_id == IWL_MVM_INVALID_STA) {
234 			IWL_ERR(mvm,
235 				"No station: Cannot switch SF to FULL_ON\n");
236 			return -EINVAL;
237 		}
238 		rcu_read_lock();
239 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
240 		if (IS_ERR_OR_NULL(sta)) {
241 			IWL_ERR(mvm, "Invalid station id\n");
242 			rcu_read_unlock();
243 			return -EINVAL;
244 		}
245 		iwl_mvm_fill_sf_command(mvm, &sf_cmd, sta);
246 		rcu_read_unlock();
247 		break;
248 	case SF_INIT_OFF:
249 		iwl_mvm_fill_sf_command(mvm, &sf_cmd, NULL);
250 		break;
251 	default:
252 		WARN_ONCE(1, "Invalid state: %d. not sending Smart Fifo cmd\n",
253 			  new_state);
254 		return -EINVAL;
255 	}
256 
257 	ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_SF_CFG_CMD, CMD_ASYNC,
258 				   sizeof(sf_cmd), &sf_cmd);
259 	if (!ret)
260 		mvm->sf_state = new_state;
261 
262 	return ret;
263 }
264 
265 /*
266  * Update Smart fifo:
267  * Count bound interfaces that are not to be removed, ignoring p2p devices,
268  * and set new state accordingly.
269  */
270 int iwl_mvm_sf_update(struct iwl_mvm *mvm, struct ieee80211_vif *changed_vif,
271 		      bool remove_vif)
272 {
273 	enum iwl_sf_state new_state;
274 	u8 sta_id = IWL_MVM_INVALID_STA;
275 	struct iwl_mvm_vif *mvmvif = NULL;
276 	struct iwl_mvm_active_iface_iterator_data data = {
277 		.ignore_vif = changed_vif,
278 		.sta_vif_state = SF_UNINIT,
279 		.sta_vif_ap_sta_id = IWL_MVM_INVALID_STA,
280 	};
281 
282 	/*
283 	 * Ignore the call if we are in HW Restart flow, or if the handled
284 	 * vif is a p2p device.
285 	 */
286 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
287 	    (changed_vif && changed_vif->type == NL80211_IFTYPE_P2P_DEVICE))
288 		return 0;
289 
290 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
291 						   IEEE80211_IFACE_ITER_NORMAL,
292 						   iwl_mvm_bound_iface_iterator,
293 						   &data);
294 
295 	/* If changed_vif exists and is not to be removed, add to the count */
296 	if (changed_vif && !remove_vif)
297 		data.num_active_macs++;
298 
299 	switch (data.num_active_macs) {
300 	case 0:
301 		/* If there are no active macs - change state to SF_INIT_OFF */
302 		new_state = SF_INIT_OFF;
303 		break;
304 	case 1:
305 		if (remove_vif) {
306 			/* The one active mac left is of type station
307 			 * and we filled the relevant data during iteration
308 			 */
309 			new_state = data.sta_vif_state;
310 			sta_id = data.sta_vif_ap_sta_id;
311 		} else {
312 			if (WARN_ON(!changed_vif))
313 				return -EINVAL;
314 			if (changed_vif->type != NL80211_IFTYPE_STATION) {
315 				new_state = SF_UNINIT;
316 			} else if (changed_vif->bss_conf.assoc &&
317 				   changed_vif->bss_conf.dtim_period) {
318 				mvmvif = iwl_mvm_vif_from_mac80211(changed_vif);
319 				sta_id = mvmvif->ap_sta_id;
320 				new_state = SF_FULL_ON;
321 			} else {
322 				new_state = SF_INIT_OFF;
323 			}
324 		}
325 		break;
326 	default:
327 		/* If there are multiple active macs - change to SF_UNINIT */
328 		new_state = SF_UNINIT;
329 	}
330 	return iwl_mvm_sf_config(mvm, sta_id, new_state);
331 }
332