1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NXP Wireless LAN device driver: 802.11ac
4  *
5  * Copyright 2011-2020 NXP
6  */
7 
8 #include "decl.h"
9 #include "ioctl.h"
10 #include "fw.h"
11 #include "main.h"
12 #include "11ac.h"
13 
14 /* Tables of the MCS map to the highest data rate (in Mbps) supported
15  * for long GI.
16  */
17 static const u16 max_rate_lgi_80MHZ[8][3] = {
18 	{0x124, 0x15F, 0x186},	/* NSS = 1 */
19 	{0x249, 0x2BE, 0x30C},  /* NSS = 2 */
20 	{0x36D, 0x41D, 0x492},  /* NSS = 3 */
21 	{0x492, 0x57C, 0x618},  /* NSS = 4 */
22 	{0x5B6, 0x6DB, 0x79E},  /* NSS = 5 */
23 	{0x6DB, 0x83A, 0x0},    /* NSS = 6 */
24 	{0x7FF, 0x999, 0xAAA},  /* NSS = 7 */
25 	{0x924, 0xAF8, 0xC30}   /* NSS = 8 */
26 };
27 
28 static const u16 max_rate_lgi_160MHZ[8][3] = {
29 	{0x249, 0x2BE, 0x30C},   /* NSS = 1 */
30 	{0x492, 0x57C, 0x618},   /* NSS = 2 */
31 	{0x6DB, 0x83A, 0x0},     /* NSS = 3 */
32 	{0x924, 0xAF8, 0xC30},   /* NSS = 4 */
33 	{0xB6D, 0xDB6, 0xF3C},   /* NSS = 5 */
34 	{0xDB6, 0x1074, 0x1248}, /* NSS = 6 */
35 	{0xFFF, 0x1332, 0x1554}, /* NSS = 7 */
36 	{0x1248, 0x15F0, 0x1860} /* NSS = 8 */
37 };
38 
39 /* This function converts the 2-bit MCS map to the highest long GI
40  * VHT data rate.
41  */
42 static u16
43 mwifiex_convert_mcsmap_to_maxrate(struct mwifiex_private *priv,
44 				  u8 bands, u16 mcs_map)
45 {
46 	u8 i, nss, mcs;
47 	u16 max_rate = 0;
48 	u32 usr_vht_cap_info = 0;
49 	struct mwifiex_adapter *adapter = priv->adapter;
50 
51 	if (bands & BAND_AAC)
52 		usr_vht_cap_info = adapter->usr_dot_11ac_dev_cap_a;
53 	else
54 		usr_vht_cap_info = adapter->usr_dot_11ac_dev_cap_bg;
55 
56 	/* find the max NSS supported */
57 	nss = 1;
58 	for (i = 1; i <= 8; i++) {
59 		mcs = GET_VHTNSSMCS(mcs_map, i);
60 		if (mcs < IEEE80211_VHT_MCS_NOT_SUPPORTED)
61 			nss = i;
62 	}
63 	mcs = GET_VHTNSSMCS(mcs_map, nss);
64 
65 	/* if mcs is 3, nss must be 1 (NSS = 1). Default mcs to MCS 0~9 */
66 	if (mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
67 		mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
68 
69 	if (GET_VHTCAP_CHWDSET(usr_vht_cap_info)) {
70 		/* support 160 MHz */
71 		max_rate = max_rate_lgi_160MHZ[nss - 1][mcs];
72 		if (!max_rate)
73 			/* MCS9 is not supported in NSS6 */
74 			max_rate = max_rate_lgi_160MHZ[nss - 1][mcs - 1];
75 	} else {
76 		max_rate = max_rate_lgi_80MHZ[nss - 1][mcs];
77 		if (!max_rate)
78 			/* MCS9 is not supported in NSS3 */
79 			max_rate = max_rate_lgi_80MHZ[nss - 1][mcs - 1];
80 	}
81 
82 	return max_rate;
83 }
84 
85 static void
86 mwifiex_fill_vht_cap_info(struct mwifiex_private *priv,
87 			  struct ieee80211_vht_cap *vht_cap, u8 bands)
88 {
89 	struct mwifiex_adapter *adapter = priv->adapter;
90 
91 	if (bands & BAND_A)
92 		vht_cap->vht_cap_info =
93 				cpu_to_le32(adapter->usr_dot_11ac_dev_cap_a);
94 	else
95 		vht_cap->vht_cap_info =
96 				cpu_to_le32(adapter->usr_dot_11ac_dev_cap_bg);
97 }
98 
99 void mwifiex_fill_vht_cap_tlv(struct mwifiex_private *priv,
100 			      struct ieee80211_vht_cap *vht_cap, u8 bands)
101 {
102 	struct mwifiex_adapter *adapter = priv->adapter;
103 	u16 mcs_map_user, mcs_map_resp, mcs_map_result;
104 	u16 mcs_user, mcs_resp, nss, tmp;
105 
106 	/* Fill VHT cap info */
107 	mwifiex_fill_vht_cap_info(priv, vht_cap, bands);
108 
109 	/* rx MCS Set: find the minimum of the user rx mcs and ap rx mcs */
110 	mcs_map_user = GET_DEVRXMCSMAP(adapter->usr_dot_11ac_mcs_support);
111 	mcs_map_resp = le16_to_cpu(vht_cap->supp_mcs.rx_mcs_map);
112 	mcs_map_result = 0;
113 
114 	for (nss = 1; nss <= 8; nss++) {
115 		mcs_user = GET_VHTNSSMCS(mcs_map_user, nss);
116 		mcs_resp = GET_VHTNSSMCS(mcs_map_resp, nss);
117 
118 		if ((mcs_user == IEEE80211_VHT_MCS_NOT_SUPPORTED) ||
119 		    (mcs_resp == IEEE80211_VHT_MCS_NOT_SUPPORTED))
120 			SET_VHTNSSMCS(mcs_map_result, nss,
121 				      IEEE80211_VHT_MCS_NOT_SUPPORTED);
122 		else
123 			SET_VHTNSSMCS(mcs_map_result, nss,
124 				      min(mcs_user, mcs_resp));
125 	}
126 
127 	vht_cap->supp_mcs.rx_mcs_map = cpu_to_le16(mcs_map_result);
128 
129 	tmp = mwifiex_convert_mcsmap_to_maxrate(priv, bands, mcs_map_result);
130 	vht_cap->supp_mcs.rx_highest = cpu_to_le16(tmp);
131 
132 	/* tx MCS Set: find the minimum of the user tx mcs and ap tx mcs */
133 	mcs_map_user = GET_DEVTXMCSMAP(adapter->usr_dot_11ac_mcs_support);
134 	mcs_map_resp = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
135 	mcs_map_result = 0;
136 
137 	for (nss = 1; nss <= 8; nss++) {
138 		mcs_user = GET_VHTNSSMCS(mcs_map_user, nss);
139 		mcs_resp = GET_VHTNSSMCS(mcs_map_resp, nss);
140 		if ((mcs_user == IEEE80211_VHT_MCS_NOT_SUPPORTED) ||
141 		    (mcs_resp == IEEE80211_VHT_MCS_NOT_SUPPORTED))
142 			SET_VHTNSSMCS(mcs_map_result, nss,
143 				      IEEE80211_VHT_MCS_NOT_SUPPORTED);
144 		else
145 			SET_VHTNSSMCS(mcs_map_result, nss,
146 				      min(mcs_user, mcs_resp));
147 	}
148 
149 	vht_cap->supp_mcs.tx_mcs_map = cpu_to_le16(mcs_map_result);
150 
151 	tmp = mwifiex_convert_mcsmap_to_maxrate(priv, bands, mcs_map_result);
152 	vht_cap->supp_mcs.tx_highest = cpu_to_le16(tmp);
153 
154 	return;
155 }
156 
157 int mwifiex_cmd_append_11ac_tlv(struct mwifiex_private *priv,
158 			     struct mwifiex_bssdescriptor *bss_desc,
159 			     u8 **buffer)
160 {
161 	struct mwifiex_ie_types_vhtcap *vht_cap;
162 	struct mwifiex_ie_types_oper_mode_ntf *oper_ntf;
163 	struct ieee_types_oper_mode_ntf *ieee_oper_ntf;
164 	struct mwifiex_ie_types_vht_oper *vht_op;
165 	struct mwifiex_adapter *adapter = priv->adapter;
166 	u8 supp_chwd_set;
167 	u32 usr_vht_cap_info;
168 	int ret_len = 0;
169 
170 	if (bss_desc->bss_band & BAND_A)
171 		usr_vht_cap_info = adapter->usr_dot_11ac_dev_cap_a;
172 	else
173 		usr_vht_cap_info = adapter->usr_dot_11ac_dev_cap_bg;
174 
175 	/* VHT Capabilities IE */
176 	if (bss_desc->bcn_vht_cap) {
177 		vht_cap = (struct mwifiex_ie_types_vhtcap *)*buffer;
178 		memset(vht_cap, 0, sizeof(*vht_cap));
179 		vht_cap->header.type = cpu_to_le16(WLAN_EID_VHT_CAPABILITY);
180 		vht_cap->header.len  =
181 				cpu_to_le16(sizeof(struct ieee80211_vht_cap));
182 		memcpy((u8 *)vht_cap + sizeof(struct mwifiex_ie_types_header),
183 		       (u8 *)bss_desc->bcn_vht_cap,
184 		       le16_to_cpu(vht_cap->header.len));
185 
186 		mwifiex_fill_vht_cap_tlv(priv, &vht_cap->vht_cap,
187 					 bss_desc->bss_band);
188 		*buffer += sizeof(*vht_cap);
189 		ret_len += sizeof(*vht_cap);
190 	}
191 
192 	/* VHT Operation IE */
193 	if (bss_desc->bcn_vht_oper) {
194 		if (priv->bss_mode == NL80211_IFTYPE_STATION) {
195 			vht_op = (struct mwifiex_ie_types_vht_oper *)*buffer;
196 			memset(vht_op, 0, sizeof(*vht_op));
197 			vht_op->header.type =
198 					cpu_to_le16(WLAN_EID_VHT_OPERATION);
199 			vht_op->header.len  = cpu_to_le16(sizeof(*vht_op) -
200 				      sizeof(struct mwifiex_ie_types_header));
201 			memcpy((u8 *)vht_op +
202 				sizeof(struct mwifiex_ie_types_header),
203 			       (u8 *)bss_desc->bcn_vht_oper,
204 			       le16_to_cpu(vht_op->header.len));
205 
206 			/* negotiate the channel width and central freq
207 			 * and keep the central freq as the peer suggests
208 			 */
209 			supp_chwd_set = GET_VHTCAP_CHWDSET(usr_vht_cap_info);
210 
211 			switch (supp_chwd_set) {
212 			case 0:
213 				vht_op->chan_width =
214 				     min_t(u8, IEEE80211_VHT_CHANWIDTH_80MHZ,
215 					   bss_desc->bcn_vht_oper->chan_width);
216 				break;
217 			case 1:
218 				vht_op->chan_width =
219 				     min_t(u8, IEEE80211_VHT_CHANWIDTH_160MHZ,
220 					   bss_desc->bcn_vht_oper->chan_width);
221 				break;
222 			case 2:
223 				vht_op->chan_width =
224 				     min_t(u8, IEEE80211_VHT_CHANWIDTH_80P80MHZ,
225 					   bss_desc->bcn_vht_oper->chan_width);
226 				break;
227 			default:
228 				vht_op->chan_width =
229 				     IEEE80211_VHT_CHANWIDTH_USE_HT;
230 				break;
231 			}
232 
233 			*buffer += sizeof(*vht_op);
234 			ret_len += sizeof(*vht_op);
235 		}
236 	}
237 
238 	/* Operating Mode Notification IE */
239 	if (bss_desc->oper_mode) {
240 		ieee_oper_ntf = bss_desc->oper_mode;
241 		oper_ntf = (void *)*buffer;
242 		memset(oper_ntf, 0, sizeof(*oper_ntf));
243 		oper_ntf->header.type = cpu_to_le16(WLAN_EID_OPMODE_NOTIF);
244 		oper_ntf->header.len = cpu_to_le16(sizeof(u8));
245 		oper_ntf->oper_mode = ieee_oper_ntf->oper_mode;
246 		*buffer += sizeof(*oper_ntf);
247 		ret_len += sizeof(*oper_ntf);
248 	}
249 
250 	return ret_len;
251 }
252 
253 int mwifiex_cmd_11ac_cfg(struct mwifiex_private *priv,
254 			 struct host_cmd_ds_command *cmd, u16 cmd_action,
255 			 struct mwifiex_11ac_vht_cfg *cfg)
256 {
257 	struct host_cmd_11ac_vht_cfg *vhtcfg = &cmd->params.vht_cfg;
258 
259 	cmd->command = cpu_to_le16(HostCmd_CMD_11AC_CFG);
260 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_11ac_vht_cfg) +
261 				S_DS_GEN);
262 	vhtcfg->action = cpu_to_le16(cmd_action);
263 	vhtcfg->band_config = cfg->band_config;
264 	vhtcfg->misc_config = cfg->misc_config;
265 	vhtcfg->cap_info = cpu_to_le32(cfg->cap_info);
266 	vhtcfg->mcs_tx_set = cpu_to_le32(cfg->mcs_tx_set);
267 	vhtcfg->mcs_rx_set = cpu_to_le32(cfg->mcs_rx_set);
268 
269 	return 0;
270 }
271 
272 /* This function initializes the BlockACK setup information for given
273  * mwifiex_private structure for 11ac enabled networks.
274  */
275 void mwifiex_set_11ac_ba_params(struct mwifiex_private *priv)
276 {
277 	priv->add_ba_param.timeout = MWIFIEX_DEFAULT_BLOCK_ACK_TIMEOUT;
278 
279 	if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
280 		priv->add_ba_param.tx_win_size =
281 					   MWIFIEX_11AC_UAP_AMPDU_DEF_TXWINSIZE;
282 		priv->add_ba_param.rx_win_size =
283 					   MWIFIEX_11AC_UAP_AMPDU_DEF_RXWINSIZE;
284 	} else {
285 		priv->add_ba_param.tx_win_size =
286 					   MWIFIEX_11AC_STA_AMPDU_DEF_TXWINSIZE;
287 		priv->add_ba_param.rx_win_size =
288 					   MWIFIEX_11AC_STA_AMPDU_DEF_RXWINSIZE;
289 	}
290 
291 	return;
292 }
293 
294 bool mwifiex_is_bss_in_11ac_mode(struct mwifiex_private *priv)
295 {
296 	struct mwifiex_bssdescriptor *bss_desc;
297 	struct ieee80211_vht_operation *vht_oper;
298 
299 	bss_desc = &priv->curr_bss_params.bss_descriptor;
300 	vht_oper = bss_desc->bcn_vht_oper;
301 
302 	if (!bss_desc->bcn_vht_cap || !vht_oper)
303 		return false;
304 
305 	if (vht_oper->chan_width == IEEE80211_VHT_CHANWIDTH_USE_HT)
306 		return false;
307 
308 	return true;
309 }
310 
311 u8 mwifiex_get_center_freq_index(struct mwifiex_private *priv, u8 band,
312 				 u32 pri_chan, u8 chan_bw)
313 {
314 	u8 center_freq_idx = 0;
315 
316 	if (band & BAND_AAC) {
317 		switch (pri_chan) {
318 		case 36:
319 		case 40:
320 		case 44:
321 		case 48:
322 			if (chan_bw == IEEE80211_VHT_CHANWIDTH_80MHZ)
323 				center_freq_idx = 42;
324 			break;
325 		case 52:
326 		case 56:
327 		case 60:
328 		case 64:
329 			if (chan_bw == IEEE80211_VHT_CHANWIDTH_80MHZ)
330 				center_freq_idx = 58;
331 			else if (chan_bw == IEEE80211_VHT_CHANWIDTH_160MHZ)
332 				center_freq_idx = 50;
333 			break;
334 		case 100:
335 		case 104:
336 		case 108:
337 		case 112:
338 			if (chan_bw == IEEE80211_VHT_CHANWIDTH_80MHZ)
339 				center_freq_idx = 106;
340 			break;
341 		case 116:
342 		case 120:
343 		case 124:
344 		case 128:
345 			if (chan_bw == IEEE80211_VHT_CHANWIDTH_80MHZ)
346 				center_freq_idx = 122;
347 			else if (chan_bw == IEEE80211_VHT_CHANWIDTH_160MHZ)
348 				center_freq_idx = 114;
349 			break;
350 		case 132:
351 		case 136:
352 		case 140:
353 		case 144:
354 			if (chan_bw == IEEE80211_VHT_CHANWIDTH_80MHZ)
355 				center_freq_idx = 138;
356 			break;
357 		case 149:
358 		case 153:
359 		case 157:
360 		case 161:
361 			if (chan_bw == IEEE80211_VHT_CHANWIDTH_80MHZ)
362 				center_freq_idx = 155;
363 			break;
364 		default:
365 			center_freq_idx = 42;
366 		}
367 	}
368 
369 	return center_freq_idx;
370 }
371