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) 2017        Intel Deutschland GmbH
9  * Copyright(c) 2018 Intel Corporation
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) 2017        Intel Deutschland GmbH
30  * Copyright(c) 2018 Intel Corporation
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 "rs.h"
61 #include "fw-api.h"
62 #include "sta.h"
63 #include "iwl-op-mode.h"
64 #include "mvm.h"
65 
66 static u8 rs_fw_bw_from_sta_bw(struct ieee80211_sta *sta)
67 {
68 	switch (sta->bandwidth) {
69 	case IEEE80211_STA_RX_BW_160:
70 		return IWL_TLC_MNG_CH_WIDTH_160MHZ;
71 	case IEEE80211_STA_RX_BW_80:
72 		return IWL_TLC_MNG_CH_WIDTH_80MHZ;
73 	case IEEE80211_STA_RX_BW_40:
74 		return IWL_TLC_MNG_CH_WIDTH_40MHZ;
75 	case IEEE80211_STA_RX_BW_20:
76 	default:
77 		return IWL_TLC_MNG_CH_WIDTH_20MHZ;
78 	}
79 }
80 
81 static u8 rs_fw_set_active_chains(u8 chains)
82 {
83 	u8 fw_chains = 0;
84 
85 	if (chains & ANT_A)
86 		fw_chains |= IWL_TLC_MNG_CHAIN_A_MSK;
87 	if (chains & ANT_B)
88 		fw_chains |= IWL_TLC_MNG_CHAIN_B_MSK;
89 	if (chains & ANT_C)
90 		WARN(false,
91 		     "tlc offload doesn't support antenna C. chains: 0x%x\n",
92 		     chains);
93 
94 	return fw_chains;
95 }
96 
97 static u8 rs_fw_sgi_cw_support(struct ieee80211_sta *sta)
98 {
99 	struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
100 	struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
101 	u8 supp = 0;
102 
103 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
104 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_20MHZ);
105 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
106 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_40MHZ);
107 	if (vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_80)
108 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_80MHZ);
109 	if (vht_cap->cap & IEEE80211_VHT_CAP_SHORT_GI_160)
110 		supp |= BIT(IWL_TLC_MNG_CH_WIDTH_160MHZ);
111 
112 	return supp;
113 }
114 
115 static u16 rs_fw_set_config_flags(struct iwl_mvm *mvm,
116 				  struct ieee80211_sta *sta)
117 {
118 	struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
119 	struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
120 	bool vht_ena = vht_cap && vht_cap->vht_supported;
121 	u16 flags = 0;
122 
123 	if (mvm->cfg->ht_params->stbc &&
124 	    (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
125 	    ((ht_cap && (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC)) ||
126 	     (vht_ena && (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))))
127 		flags |= IWL_TLC_MNG_CFG_FLAGS_STBC_MSK;
128 
129 	if (mvm->cfg->ht_params->ldpc &&
130 	    ((ht_cap && (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)) ||
131 	     (vht_ena && (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))))
132 		flags |= IWL_TLC_MNG_CFG_FLAGS_LDPC_MSK;
133 
134 	return flags;
135 }
136 
137 static
138 int rs_fw_vht_highest_rx_mcs_index(const struct ieee80211_sta_vht_cap *vht_cap,
139 				   int nss)
140 {
141 	u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
142 		(0x3 << (2 * (nss - 1)));
143 	rx_mcs >>= (2 * (nss - 1));
144 
145 	switch (rx_mcs) {
146 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
147 		return IWL_TLC_MNG_HT_RATE_MCS7;
148 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
149 		return IWL_TLC_MNG_HT_RATE_MCS8;
150 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
151 		return IWL_TLC_MNG_HT_RATE_MCS9;
152 	default:
153 		WARN_ON_ONCE(1);
154 		break;
155 	}
156 
157 	return 0;
158 }
159 
160 static void
161 rs_fw_vht_set_enabled_rates(const struct ieee80211_sta *sta,
162 			    const struct ieee80211_sta_vht_cap *vht_cap,
163 			    struct iwl_tlc_config_cmd *cmd)
164 {
165 	u16 supp;
166 	int i, highest_mcs;
167 
168 	for (i = 0; i < sta->rx_nss; i++) {
169 		if (i == MAX_NSS)
170 			break;
171 
172 		highest_mcs = rs_fw_vht_highest_rx_mcs_index(vht_cap, i + 1);
173 		if (!highest_mcs)
174 			continue;
175 
176 		supp = BIT(highest_mcs + 1) - 1;
177 		if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
178 			supp &= ~BIT(IWL_TLC_MNG_HT_RATE_MCS9);
179 
180 		cmd->ht_rates[i][0] = cpu_to_le16(supp);
181 		if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
182 			cmd->ht_rates[i][1] = cmd->ht_rates[i][0];
183 	}
184 }
185 
186 static void rs_fw_set_supp_rates(struct ieee80211_sta *sta,
187 				 struct ieee80211_supported_band *sband,
188 				 struct iwl_tlc_config_cmd *cmd)
189 {
190 	int i;
191 	unsigned long tmp;
192 	unsigned long supp; /* must be unsigned long for for_each_set_bit */
193 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
194 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
195 
196 	/* non HT rates */
197 	supp = 0;
198 	tmp = sta->supp_rates[sband->band];
199 	for_each_set_bit(i, &tmp, BITS_PER_LONG)
200 		supp |= BIT(sband->bitrates[i].hw_value);
201 
202 	cmd->non_ht_rates = cpu_to_le16(supp);
203 	cmd->mode = IWL_TLC_MNG_MODE_NON_HT;
204 
205 	if (vht_cap && vht_cap->vht_supported) {
206 		cmd->mode = IWL_TLC_MNG_MODE_VHT;
207 		rs_fw_vht_set_enabled_rates(sta, vht_cap, cmd);
208 	} else if (ht_cap && ht_cap->ht_supported) {
209 		cmd->mode = IWL_TLC_MNG_MODE_HT;
210 		cmd->ht_rates[0][0] = cpu_to_le16(ht_cap->mcs.rx_mask[0]);
211 		cmd->ht_rates[1][0] = cpu_to_le16(ht_cap->mcs.rx_mask[1]);
212 	}
213 }
214 
215 void iwl_mvm_tlc_update_notif(struct iwl_mvm *mvm,
216 			      struct iwl_rx_cmd_buffer *rxb)
217 {
218 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
219 	struct iwl_tlc_update_notif *notif;
220 	struct ieee80211_sta *sta;
221 	struct iwl_mvm_sta *mvmsta;
222 	struct iwl_lq_sta_rs_fw *lq_sta;
223 	u32 flags;
224 
225 	rcu_read_lock();
226 
227 	notif = (void *)pkt->data;
228 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
229 	if (IS_ERR_OR_NULL(sta)) {
230 		IWL_ERR(mvm, "Invalid sta id (%d) in FW TLC notification\n",
231 			notif->sta_id);
232 		goto out;
233 	}
234 
235 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
236 
237 	if (!mvmsta) {
238 		IWL_ERR(mvm, "Invalid sta id (%d) in FW TLC notification\n",
239 			notif->sta_id);
240 		goto out;
241 	}
242 
243 	flags = le32_to_cpu(notif->flags);
244 
245 	lq_sta = &mvmsta->lq_sta.rs_fw;
246 
247 	if (flags & IWL_TLC_NOTIF_FLAG_RATE) {
248 		lq_sta->last_rate_n_flags = le32_to_cpu(notif->rate);
249 		IWL_DEBUG_RATE(mvm, "new rate_n_flags: 0x%X\n",
250 			       lq_sta->last_rate_n_flags);
251 	}
252 
253 	if (flags & IWL_TLC_NOTIF_FLAG_AMSDU) {
254 		u16 size = le32_to_cpu(notif->amsdu_size);
255 
256 		if (WARN_ON(sta->max_amsdu_len < size))
257 			goto out;
258 
259 		mvmsta->amsdu_enabled = le32_to_cpu(notif->amsdu_enabled);
260 		mvmsta->max_amsdu_len = size;
261 
262 		IWL_DEBUG_RATE(mvm,
263 			       "AMSDU update. AMSDU size: %d, AMSDU selected size: %d, AMSDU TID bitmap 0x%X\n",
264 			       le32_to_cpu(notif->amsdu_size), size,
265 			       mvmsta->amsdu_enabled);
266 	}
267 out:
268 	rcu_read_unlock();
269 }
270 
271 void rs_fw_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
272 		     enum nl80211_band band)
273 {
274 	struct ieee80211_hw *hw = mvm->hw;
275 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
276 	struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
277 	u32 cmd_id = iwl_cmd_id(TLC_MNG_CONFIG_CMD, DATA_PATH_GROUP, 0);
278 	struct ieee80211_supported_band *sband;
279 	struct iwl_tlc_config_cmd cfg_cmd = {
280 		.sta_id = mvmsta->sta_id,
281 		.max_ch_width = rs_fw_bw_from_sta_bw(sta),
282 		.flags = cpu_to_le16(rs_fw_set_config_flags(mvm, sta)),
283 		.chains = rs_fw_set_active_chains(iwl_mvm_get_valid_tx_ant(mvm)),
284 		.max_mpdu_len = cpu_to_le16(sta->max_amsdu_len),
285 		.sgi_ch_width_supp = rs_fw_sgi_cw_support(sta),
286 		.amsdu = iwl_mvm_is_csum_supported(mvm),
287 	};
288 	int ret;
289 
290 	memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
291 
292 #ifdef CONFIG_IWLWIFI_DEBUGFS
293 	iwl_mvm_reset_frame_stats(mvm);
294 #endif
295 	sband = hw->wiphy->bands[band];
296 	rs_fw_set_supp_rates(sta, sband, &cfg_cmd);
297 
298 	ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, sizeof(cfg_cmd), &cfg_cmd);
299 	if (ret)
300 		IWL_ERR(mvm, "Failed to send rate scale config (%d)\n", ret);
301 }
302 
303 int rs_fw_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
304 			bool enable)
305 {
306 	/* TODO: need to introduce a new FW cmd since LQ cmd is not relevant */
307 	IWL_DEBUG_RATE(mvm, "tx protection - not implemented yet.\n");
308 	return 0;
309 }
310 
311 void iwl_mvm_rs_add_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta)
312 {
313 	struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
314 
315 	IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
316 
317 	lq_sta->pers.drv = mvm;
318 	lq_sta->pers.sta_id = mvmsta->sta_id;
319 	lq_sta->pers.chains = 0;
320 	memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
321 	lq_sta->pers.last_rssi = S8_MIN;
322 	lq_sta->last_rate_n_flags = 0;
323 
324 #ifdef CONFIG_MAC80211_DEBUGFS
325 	lq_sta->pers.dbg_fixed_rate = 0;
326 #endif
327 }
328