1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
4  *
5  * Contact Information: wlanfae <wlanfae@realtek.com>
6  */
7 #include "rtllib.h"
8 #include "rtl819x_HT.h"
9 u8 MCS_FILTER_ALL[16] = {
10 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
11 	0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
12 };
13 
14 u8 MCS_FILTER_1SS[16] = {
15 	0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
16 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
17 ;
18 
19 u16 MCS_DATA_RATE[2][2][77] = {
20 	{{13, 26, 39, 52, 78, 104, 117, 130, 26, 52, 78, 104, 156, 208, 234,
21 	 260, 39, 78, 117, 234, 312, 351, 390, 52, 104, 156, 208, 312, 416,
22 	 468, 520, 0, 78, 104, 130, 117, 156, 195, 104, 130, 130, 156, 182,
23 	 182, 208, 156, 195, 195, 234, 273, 273, 312, 130, 156, 181, 156,
24 	 181, 208, 234, 208, 234, 260, 260, 286, 195, 234, 273, 234, 273,
25 	 312, 351, 312, 351, 390, 390, 429},
26 	{14, 29, 43, 58, 87, 116, 130, 144, 29, 58, 87, 116, 173, 231, 260, 289,
27 	 43, 87, 130, 173, 260, 347, 390, 433, 58, 116, 173, 231, 347, 462, 520,
28 	 578, 0, 87, 116, 144, 130, 173, 217, 116, 144, 144, 173, 202, 202, 231,
29 	 173, 217, 217, 260, 303, 303, 347, 144, 173, 202, 173, 202, 231, 260,
30 	 231, 260, 289, 289, 318, 217, 260, 303, 260, 303, 347, 390, 347, 390,
31 	 433, 433, 477} },
32 	{{27, 54, 81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486,
33 	 540, 81, 162, 243, 324, 486, 648, 729, 810, 108, 216, 324, 432, 648,
34 	 864, 972, 1080, 12, 162, 216, 270, 243, 324, 405, 216, 270, 270, 324,
35 	 378, 378, 432, 324, 405, 405, 486, 567, 567, 648, 270, 324, 378, 324,
36 	 378, 432, 486, 432, 486, 540, 540, 594, 405, 486, 567, 486, 567, 648,
37 	 729, 648, 729, 810, 810, 891},
38 	{30, 60, 90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540,
39 	 600, 90, 180, 270, 360, 540, 720, 810, 900, 120, 240, 360, 480, 720,
40 	 960, 1080, 1200, 13, 180, 240, 300, 270, 360, 450, 240, 300, 300, 360,
41 	 420, 420, 480, 360, 450, 450, 540, 630, 630, 720, 300, 360, 420, 360,
42 	 420, 480, 540, 480, 540, 600, 600, 660, 450, 540, 630, 540, 630, 720,
43 	 810, 720, 810, 900, 900, 990} }
44 };
45 
46 static u8 UNKNOWN_BORADCOM[3] = {0x00, 0x14, 0xbf};
47 
48 static u8 LINKSYSWRT330_LINKSYSWRT300_BROADCOM[3] = {0x00, 0x1a, 0x70};
49 
50 static u8 LINKSYSWRT350_LINKSYSWRT150_BROADCOM[3] = {0x00, 0x1d, 0x7e};
51 
52 static u8 BELKINF5D8233V1_RALINK[3] = {0x00, 0x17, 0x3f};
53 
54 static u8 BELKINF5D82334V3_RALINK[3] = {0x00, 0x1c, 0xdf};
55 
56 static u8 PCI_RALINK[3] = {0x00, 0x90, 0xcc};
57 
58 static u8 EDIMAX_RALINK[3] = {0x00, 0x0e, 0x2e};
59 
60 static u8 AIRLINK_RALINK[3] = {0x00, 0x18, 0x02};
61 
62 static u8 DLINK_ATHEROS_1[3] = {0x00, 0x1c, 0xf0};
63 
64 static u8 DLINK_ATHEROS_2[3] = {0x00, 0x21, 0x91};
65 
66 static u8 CISCO_BROADCOM[3] = {0x00, 0x17, 0x94};
67 
68 static u8 LINKSYS_MARVELL_4400N[3] = {0x00, 0x14, 0xa4};
69 
HTUpdateDefaultSetting(struct rtllib_device * ieee)70 void HTUpdateDefaultSetting(struct rtllib_device *ieee)
71 {
72 	struct rt_hi_throughput *ht_info = ieee->ht_info;
73 
74 	ht_info->bRegShortGI20MHz = 1;
75 	ht_info->bRegShortGI40MHz = 1;
76 
77 	ht_info->bRegBW40MHz = 1;
78 
79 	if (ht_info->bRegBW40MHz)
80 		ht_info->bRegSuppCCK = 1;
81 	else
82 		ht_info->bRegSuppCCK = true;
83 
84 	ht_info->nAMSDU_MaxSize = 7935UL;
85 	ht_info->bAMSDU_Support = 0;
86 
87 	ht_info->bAMPDUEnable = 1;
88 	ht_info->AMPDU_Factor = 2;
89 	ht_info->MPDU_Density = 0;
90 
91 	ht_info->self_mimo_ps = 3;
92 	if (ht_info->self_mimo_ps == 2)
93 		ht_info->self_mimo_ps = 3;
94 	ieee->tx_dis_rate_fallback = 0;
95 	ieee->tx_use_drv_assinged_rate = 0;
96 
97 	ieee->bTxEnableFwCalcDur = 1;
98 
99 	ht_info->reg_rt2rt_aggregation = 1;
100 
101 	ht_info->reg_rx_reorder_enable = 1;
102 	ht_info->rx_reorder_win_size = 64;
103 	ht_info->rx_reorder_pending_time = 30;
104 }
105 
HTMcsToDataRate(struct rtllib_device * ieee,u8 nMcsRate)106 static u16 HTMcsToDataRate(struct rtllib_device *ieee, u8 nMcsRate)
107 {
108 	struct rt_hi_throughput *ht_info = ieee->ht_info;
109 
110 	u8	is40MHz = (ht_info->bCurBW40MHz) ? 1 : 0;
111 	u8	isShortGI = (ht_info->bCurBW40MHz) ?
112 			    ((ht_info->bCurShortGI40MHz) ? 1 : 0) :
113 			    ((ht_info->bCurShortGI20MHz) ? 1 : 0);
114 	return MCS_DATA_RATE[is40MHz][isShortGI][(nMcsRate & 0x7f)];
115 }
116 
TxCountToDataRate(struct rtllib_device * ieee,u8 nDataRate)117 u16  TxCountToDataRate(struct rtllib_device *ieee, u8 nDataRate)
118 {
119 	u16	CCKOFDMRate[12] = {0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18,
120 				   0x24, 0x30, 0x48, 0x60, 0x6c};
121 	u8	is40MHz = 0;
122 	u8	isShortGI = 0;
123 
124 	if (nDataRate < 12)
125 		return CCKOFDMRate[nDataRate];
126 	if (nDataRate >= 0x10 && nDataRate <= 0x1f) {
127 		is40MHz = 0;
128 		isShortGI = 0;
129 	} else if (nDataRate >= 0x20  && nDataRate <= 0x2f) {
130 		is40MHz = 1;
131 		isShortGI = 0;
132 	} else if (nDataRate >= 0x30  && nDataRate <= 0x3f) {
133 		is40MHz = 0;
134 		isShortGI = 1;
135 	} else if (nDataRate >= 0x40  && nDataRate <= 0x4f) {
136 		is40MHz = 1;
137 		isShortGI = 1;
138 	}
139 	return MCS_DATA_RATE[is40MHz][isShortGI][nDataRate & 0xf];
140 }
141 
IsHTHalfNmodeAPs(struct rtllib_device * ieee)142 bool IsHTHalfNmodeAPs(struct rtllib_device *ieee)
143 {
144 	bool			retValue = false;
145 	struct rtllib_network *net = &ieee->current_network;
146 
147 	if ((memcmp(net->bssid, BELKINF5D8233V1_RALINK, 3) == 0) ||
148 	    (memcmp(net->bssid, BELKINF5D82334V3_RALINK, 3) == 0) ||
149 	    (memcmp(net->bssid, PCI_RALINK, 3) == 0) ||
150 	    (memcmp(net->bssid, EDIMAX_RALINK, 3) == 0) ||
151 	    (memcmp(net->bssid, AIRLINK_RALINK, 3) == 0) ||
152 	    (net->ralink_cap_exist))
153 		retValue = true;
154 	else if (!memcmp(net->bssid, UNKNOWN_BORADCOM, 3) ||
155 		 !memcmp(net->bssid, LINKSYSWRT330_LINKSYSWRT300_BROADCOM, 3) ||
156 		 !memcmp(net->bssid, LINKSYSWRT350_LINKSYSWRT150_BROADCOM, 3) ||
157 		(net->broadcom_cap_exist))
158 		retValue = true;
159 	else if (net->bssht.bd_rt2rt_aggregation)
160 		retValue = true;
161 	else
162 		retValue = false;
163 
164 	return retValue;
165 }
166 
HTIOTPeerDetermine(struct rtllib_device * ieee)167 static void HTIOTPeerDetermine(struct rtllib_device *ieee)
168 {
169 	struct rt_hi_throughput *ht_info = ieee->ht_info;
170 	struct rtllib_network *net = &ieee->current_network;
171 
172 	if (net->bssht.bd_rt2rt_aggregation) {
173 		ht_info->IOTPeer = HT_IOT_PEER_REALTEK;
174 		if (net->bssht.rt2rt_ht_mode & RT_HT_CAP_USE_92SE)
175 			ht_info->IOTPeer = HT_IOT_PEER_REALTEK_92SE;
176 		if (net->bssht.rt2rt_ht_mode & RT_HT_CAP_USE_SOFTAP)
177 			ht_info->IOTPeer = HT_IOT_PEER_92U_SOFTAP;
178 	} else if (net->broadcom_cap_exist) {
179 		ht_info->IOTPeer = HT_IOT_PEER_BROADCOM;
180 	} else if (!memcmp(net->bssid, UNKNOWN_BORADCOM, 3) ||
181 		 !memcmp(net->bssid, LINKSYSWRT330_LINKSYSWRT300_BROADCOM, 3) ||
182 		 !memcmp(net->bssid, LINKSYSWRT350_LINKSYSWRT150_BROADCOM, 3)) {
183 		ht_info->IOTPeer = HT_IOT_PEER_BROADCOM;
184 	} else if ((memcmp(net->bssid, BELKINF5D8233V1_RALINK, 3) == 0) ||
185 		 (memcmp(net->bssid, BELKINF5D82334V3_RALINK, 3) == 0) ||
186 		 (memcmp(net->bssid, PCI_RALINK, 3) == 0) ||
187 		 (memcmp(net->bssid, EDIMAX_RALINK, 3) == 0) ||
188 		 (memcmp(net->bssid, AIRLINK_RALINK, 3) == 0) ||
189 		  net->ralink_cap_exist) {
190 		ht_info->IOTPeer = HT_IOT_PEER_RALINK;
191 	} else if ((net->atheros_cap_exist) ||
192 		(memcmp(net->bssid, DLINK_ATHEROS_1, 3) == 0) ||
193 		(memcmp(net->bssid, DLINK_ATHEROS_2, 3) == 0)) {
194 		ht_info->IOTPeer = HT_IOT_PEER_ATHEROS;
195 	} else if ((memcmp(net->bssid, CISCO_BROADCOM, 3) == 0) ||
196 		  net->cisco_cap_exist) {
197 		ht_info->IOTPeer = HT_IOT_PEER_CISCO;
198 	} else if ((memcmp(net->bssid, LINKSYS_MARVELL_4400N, 3) == 0) ||
199 		  net->marvell_cap_exist) {
200 		ht_info->IOTPeer = HT_IOT_PEER_MARVELL;
201 	} else if (net->airgo_cap_exist) {
202 		ht_info->IOTPeer = HT_IOT_PEER_AIRGO;
203 	} else {
204 		ht_info->IOTPeer = HT_IOT_PEER_UNKNOWN;
205 	}
206 
207 	netdev_dbg(ieee->dev, "IOTPEER: %x\n", ht_info->IOTPeer);
208 }
209 
HTIOTActIsDisableMCS14(struct rtllib_device * ieee,u8 * PeerMacAddr)210 static u8 HTIOTActIsDisableMCS14(struct rtllib_device *ieee, u8 *PeerMacAddr)
211 {
212 	return 0;
213 }
214 
HTIOTActIsDisableMCS15(struct rtllib_device * ieee)215 static bool HTIOTActIsDisableMCS15(struct rtllib_device *ieee)
216 {
217 	return false;
218 }
219 
HTIOTActIsDisableMCSTwoSpatialStream(struct rtllib_device * ieee)220 static bool HTIOTActIsDisableMCSTwoSpatialStream(struct rtllib_device *ieee)
221 {
222 	return false;
223 }
224 
HTIOTActIsDisableEDCATurbo(struct rtllib_device * ieee,u8 * PeerMacAddr)225 static u8 HTIOTActIsDisableEDCATurbo(struct rtllib_device *ieee,
226 				     u8 *PeerMacAddr)
227 {
228 	return false;
229 }
230 
HTIOTActIsMgntUseCCK6M(struct rtllib_device * ieee,struct rtllib_network * network)231 static u8 HTIOTActIsMgntUseCCK6M(struct rtllib_device *ieee,
232 				 struct rtllib_network *network)
233 {
234 	u8	retValue = 0;
235 
236 	if (ieee->ht_info->IOTPeer == HT_IOT_PEER_BROADCOM)
237 		retValue = 1;
238 
239 	return retValue;
240 }
241 
HTIOTActIsCCDFsync(struct rtllib_device * ieee)242 static u8 HTIOTActIsCCDFsync(struct rtllib_device *ieee)
243 {
244 	u8	retValue = 0;
245 
246 	if (ieee->ht_info->IOTPeer == HT_IOT_PEER_BROADCOM)
247 		retValue = 1;
248 	return retValue;
249 }
250 
HTIOTActDetermineRaFunc(struct rtllib_device * ieee,bool bPeerRx2ss)251 static void HTIOTActDetermineRaFunc(struct rtllib_device *ieee, bool bPeerRx2ss)
252 {
253 	struct rt_hi_throughput *ht_info = ieee->ht_info;
254 
255 	ht_info->iot_ra_func &= HT_IOT_RAFUNC_DISABLE_ALL;
256 
257 	if (ht_info->IOTPeer == HT_IOT_PEER_RALINK && !bPeerRx2ss)
258 		ht_info->iot_ra_func |= HT_IOT_RAFUNC_PEER_1R;
259 
260 	if (ht_info->iot_action & HT_IOT_ACT_AMSDU_ENABLE)
261 		ht_info->iot_ra_func |= HT_IOT_RAFUNC_TX_AMSDU;
262 }
263 
HTResetIOTSetting(struct rt_hi_throughput * ht_info)264 void HTResetIOTSetting(struct rt_hi_throughput *ht_info)
265 {
266 	ht_info->iot_action = 0;
267 	ht_info->IOTPeer = HT_IOT_PEER_UNKNOWN;
268 	ht_info->iot_ra_func = 0;
269 }
270 
HTConstructCapabilityElement(struct rtllib_device * ieee,u8 * posHTCap,u8 * len,u8 IsEncrypt,bool bAssoc)271 void HTConstructCapabilityElement(struct rtllib_device *ieee, u8 *posHTCap,
272 				  u8 *len, u8 IsEncrypt, bool bAssoc)
273 {
274 	struct rt_hi_throughput *pHT = ieee->ht_info;
275 	struct ht_capab_ele *pCapELE = NULL;
276 
277 	if (!posHTCap || !pHT) {
278 		netdev_warn(ieee->dev,
279 			    "%s(): posHTCap and ht_info are null\n", __func__);
280 		return;
281 	}
282 	memset(posHTCap, 0, *len);
283 
284 	if ((bAssoc) && (pHT->ePeerHTSpecVer == HT_SPEC_VER_EWC)) {
285 		static const u8	EWC11NHTCap[] = { 0x00, 0x90, 0x4c, 0x33 };
286 
287 		memcpy(posHTCap, EWC11NHTCap, sizeof(EWC11NHTCap));
288 		pCapELE = (struct ht_capab_ele *)&posHTCap[4];
289 		*len = 30 + 2;
290 	} else {
291 		pCapELE = (struct ht_capab_ele *)posHTCap;
292 		*len = 26 + 2;
293 	}
294 
295 	pCapELE->AdvCoding		= 0;
296 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
297 		pCapELE->ChlWidth = 0;
298 	else
299 		pCapELE->ChlWidth = (pHT->bRegBW40MHz ? 1 : 0);
300 
301 	pCapELE->MimoPwrSave		= pHT->self_mimo_ps;
302 	pCapELE->GreenField		= 0;
303 	pCapELE->ShortGI20Mhz		= 1;
304 	pCapELE->ShortGI40Mhz		= 1;
305 
306 	pCapELE->TxSTBC			= 1;
307 	pCapELE->RxSTBC			= 0;
308 	pCapELE->DelayBA		= 0;
309 	pCapELE->MaxAMSDUSize = (MAX_RECEIVE_BUFFER_SIZE >= 7935) ? 1 : 0;
310 	pCapELE->DssCCk = ((pHT->bRegBW40MHz) ? (pHT->bRegSuppCCK ? 1 : 0) : 0);
311 	pCapELE->PSMP = 0;
312 	pCapELE->LSigTxopProtect = 0;
313 
314 	netdev_dbg(ieee->dev,
315 		   "TX HT cap/info ele BW=%d MaxAMSDUSize:%d DssCCk:%d\n",
316 		   pCapELE->ChlWidth, pCapELE->MaxAMSDUSize, pCapELE->DssCCk);
317 
318 	if (IsEncrypt) {
319 		pCapELE->MPDUDensity	= 7;
320 		pCapELE->MaxRxAMPDUFactor	= 2;
321 	} else {
322 		pCapELE->MaxRxAMPDUFactor	= 3;
323 		pCapELE->MPDUDensity	= 0;
324 	}
325 
326 	memcpy(pCapELE->MCS, ieee->reg_dot11ht_oper_rate_set, 16);
327 	memset(&pCapELE->ExtHTCapInfo, 0, 2);
328 	memset(pCapELE->TxBFCap, 0, 4);
329 
330 	pCapELE->ASCap = 0;
331 
332 	if (bAssoc) {
333 		if (pHT->iot_action & HT_IOT_ACT_DISABLE_MCS15)
334 			pCapELE->MCS[1] &= 0x7f;
335 
336 		if (pHT->iot_action & HT_IOT_ACT_DISABLE_MCS14)
337 			pCapELE->MCS[1] &= 0xbf;
338 
339 		if (pHT->iot_action & HT_IOT_ACT_DISABLE_ALL_2SS)
340 			pCapELE->MCS[1] &= 0x00;
341 
342 		if (pHT->iot_action & HT_IOT_ACT_DISABLE_RX_40MHZ_SHORT_GI)
343 			pCapELE->ShortGI40Mhz		= 0;
344 
345 		if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev)) {
346 			pCapELE->ChlWidth = 0;
347 			pCapELE->MCS[1] = 0;
348 		}
349 	}
350 }
351 
HTConstructInfoElement(struct rtllib_device * ieee,u8 * posHTInfo,u8 * len,u8 IsEncrypt)352 void HTConstructInfoElement(struct rtllib_device *ieee, u8 *posHTInfo,
353 			    u8 *len, u8 IsEncrypt)
354 {
355 	struct rt_hi_throughput *pHT = ieee->ht_info;
356 	struct ht_info_ele *pHTInfoEle = (struct ht_info_ele *)posHTInfo;
357 
358 	if (!posHTInfo || !pHTInfoEle) {
359 		netdev_warn(ieee->dev,
360 			    "%s(): posHTInfo and pHTInfoEle are null\n",
361 			    __func__);
362 		return;
363 	}
364 
365 	memset(posHTInfo, 0, *len);
366 	if ((ieee->iw_mode == IW_MODE_ADHOC) ||
367 	    (ieee->iw_mode == IW_MODE_MASTER)) {
368 		pHTInfoEle->ControlChl	= ieee->current_network.channel;
369 		pHTInfoEle->ExtChlOffset = ((!pHT->bRegBW40MHz) ?
370 					    HT_EXTCHNL_OFFSET_NO_EXT :
371 					    (ieee->current_network.channel <= 6)
372 					    ? HT_EXTCHNL_OFFSET_UPPER :
373 					    HT_EXTCHNL_OFFSET_LOWER);
374 		pHTInfoEle->RecommemdedTxWidth	= pHT->bRegBW40MHz;
375 		pHTInfoEle->RIFS			= 0;
376 		pHTInfoEle->PSMPAccessOnly		= 0;
377 		pHTInfoEle->SrvIntGranularity		= 0;
378 		pHTInfoEle->OptMode			= pHT->current_op_mode;
379 		pHTInfoEle->NonGFDevPresent		= 0;
380 		pHTInfoEle->DualBeacon			= 0;
381 		pHTInfoEle->SecondaryBeacon		= 0;
382 		pHTInfoEle->LSigTxopProtectFull		= 0;
383 		pHTInfoEle->PcoActive			= 0;
384 		pHTInfoEle->PcoPhase			= 0;
385 
386 		memset(pHTInfoEle->BasicMSC, 0, 16);
387 
388 		*len = 22 + 2;
389 
390 	} else {
391 		*len = 0;
392 	}
393 }
394 
HTConstructRT2RTAggElement(struct rtllib_device * ieee,u8 * posRT2RTAgg,u8 * len)395 void HTConstructRT2RTAggElement(struct rtllib_device *ieee, u8 *posRT2RTAgg,
396 				u8 *len)
397 {
398 	if (!posRT2RTAgg) {
399 		netdev_warn(ieee->dev, "%s(): posRT2RTAgg is null\n", __func__);
400 		return;
401 	}
402 	memset(posRT2RTAgg, 0, *len);
403 	*posRT2RTAgg++ = 0x00;
404 	*posRT2RTAgg++ = 0xe0;
405 	*posRT2RTAgg++ = 0x4c;
406 	*posRT2RTAgg++ = 0x02;
407 	*posRT2RTAgg++ = 0x01;
408 
409 	*posRT2RTAgg = 0x30;
410 
411 	if (ieee->bSupportRemoteWakeUp)
412 		*posRT2RTAgg |= RT_HT_CAP_USE_WOW;
413 
414 	*len = 6 + 2;
415 }
416 
HT_PickMCSRate(struct rtllib_device * ieee,u8 * pOperateMCS)417 static u8 HT_PickMCSRate(struct rtllib_device *ieee, u8 *pOperateMCS)
418 {
419 	u8 i;
420 
421 	if (!pOperateMCS) {
422 		netdev_warn(ieee->dev, "%s(): pOperateMCS is null\n", __func__);
423 		return false;
424 	}
425 
426 	switch (ieee->mode) {
427 	case IEEE_A:
428 	case IEEE_B:
429 	case IEEE_G:
430 		for (i = 0; i <= 15; i++)
431 			pOperateMCS[i] = 0;
432 		break;
433 	case IEEE_N_24G:
434 	case IEEE_N_5G:
435 		pOperateMCS[0] &= RATE_ADPT_1SS_MASK;
436 		pOperateMCS[1] &= RATE_ADPT_2SS_MASK;
437 		pOperateMCS[3] &= RATE_ADPT_MCS32_MASK;
438 		break;
439 	default:
440 		break;
441 	}
442 
443 	return true;
444 }
445 
HTGetHighestMCSRate(struct rtllib_device * ieee,u8 * pMCSRateSet,u8 * pMCSFilter)446 u8 HTGetHighestMCSRate(struct rtllib_device *ieee, u8 *pMCSRateSet,
447 		       u8 *pMCSFilter)
448 {
449 	u8		i, j;
450 	u8		bitMap;
451 	u8		mcsRate = 0;
452 	u8		availableMcsRate[16];
453 
454 	if (!pMCSRateSet || !pMCSFilter) {
455 		netdev_warn(ieee->dev,
456 			    "%s(): pMCSRateSet and pMCSFilter are null\n",
457 			    __func__);
458 		return false;
459 	}
460 	for (i = 0; i < 16; i++)
461 		availableMcsRate[i] = pMCSRateSet[i] & pMCSFilter[i];
462 
463 	for (i = 0; i < 16; i++) {
464 		if (availableMcsRate[i] != 0)
465 			break;
466 	}
467 	if (i == 16)
468 		return false;
469 
470 	for (i = 0; i < 16; i++) {
471 		if (availableMcsRate[i] != 0) {
472 			bitMap = availableMcsRate[i];
473 			for (j = 0; j < 8; j++) {
474 				if ((bitMap % 2) != 0) {
475 					if (HTMcsToDataRate(ieee, (8 * i + j)) >
476 					    HTMcsToDataRate(ieee, mcsRate))
477 						mcsRate = 8 * i + j;
478 				}
479 				bitMap >>= 1;
480 			}
481 		}
482 	}
483 	return mcsRate | 0x80;
484 }
485 
HTFilterMCSRate(struct rtllib_device * ieee,u8 * pSupportMCS,u8 * pOperateMCS)486 static u8 HTFilterMCSRate(struct rtllib_device *ieee, u8 *pSupportMCS,
487 			  u8 *pOperateMCS)
488 {
489 	u8 i;
490 
491 	for (i = 0; i <= 15; i++)
492 		pOperateMCS[i] = ieee->reg_dot11tx_ht_oper_rate_set[i] &
493 				 pSupportMCS[i];
494 
495 	HT_PickMCSRate(ieee, pOperateMCS);
496 
497 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
498 		pOperateMCS[1] = 0;
499 
500 	for (i = 2; i <= 15; i++)
501 		pOperateMCS[i] = 0;
502 
503 	return true;
504 }
505 
506 void HTSetConnectBwMode(struct rtllib_device *ieee,
507 			enum ht_channel_width bandwidth,
508 			enum ht_extchnl_offset Offset);
509 
HTOnAssocRsp(struct rtllib_device * ieee)510 void HTOnAssocRsp(struct rtllib_device *ieee)
511 {
512 	struct rt_hi_throughput *ht_info = ieee->ht_info;
513 	struct ht_capab_ele *pPeerHTCap = NULL;
514 	struct ht_info_ele *pPeerHTInfo = NULL;
515 	u16 nMaxAMSDUSize = 0;
516 	u8 *pMcsFilter = NULL;
517 
518 	static const u8 EWC11NHTCap[] = { 0x00, 0x90, 0x4c, 0x33 };
519 	static const u8 EWC11NHTInfo[] = { 0x00, 0x90, 0x4c, 0x34 };
520 
521 	if (!ht_info->bCurrentHTSupport) {
522 		netdev_warn(ieee->dev, "%s(): HT_DISABLE\n", __func__);
523 		return;
524 	}
525 	netdev_dbg(ieee->dev, "%s(): HT_ENABLE\n", __func__);
526 
527 	if (!memcmp(ht_info->PeerHTCapBuf, EWC11NHTCap, sizeof(EWC11NHTCap)))
528 		pPeerHTCap = (struct ht_capab_ele *)(&ht_info->PeerHTCapBuf[4]);
529 	else
530 		pPeerHTCap = (struct ht_capab_ele *)(ht_info->PeerHTCapBuf);
531 
532 	if (!memcmp(ht_info->PeerHTInfoBuf, EWC11NHTInfo, sizeof(EWC11NHTInfo)))
533 		pPeerHTInfo = (struct ht_info_ele *)
534 			     (&ht_info->PeerHTInfoBuf[4]);
535 	else
536 		pPeerHTInfo = (struct ht_info_ele *)(ht_info->PeerHTInfoBuf);
537 
538 #ifdef VERBOSE_DEBUG
539 	print_hex_dump_bytes("%s: ", __func__, DUMP_PREFIX_NONE,
540 			     pPeerHTCap, sizeof(struct ht_capab_ele));
541 #endif
542 	HTSetConnectBwMode(ieee, (enum ht_channel_width)(pPeerHTCap->ChlWidth),
543 			   (enum ht_extchnl_offset)(pPeerHTInfo->ExtChlOffset));
544 	ht_info->cur_tx_bw40mhz = ((pPeerHTInfo->RecommemdedTxWidth == 1) ?
545 				 true : false);
546 
547 	ht_info->bCurShortGI20MHz = ((ht_info->bRegShortGI20MHz) ?
548 				    ((pPeerHTCap->ShortGI20Mhz == 1) ?
549 				    true : false) : false);
550 	ht_info->bCurShortGI40MHz = ((ht_info->bRegShortGI40MHz) ?
551 				     ((pPeerHTCap->ShortGI40Mhz == 1) ?
552 				     true : false) : false);
553 
554 	ht_info->bCurSuppCCK = ((ht_info->bRegSuppCCK) ?
555 			       ((pPeerHTCap->DssCCk == 1) ? true :
556 			       false) : false);
557 
558 	ht_info->bCurrent_AMSDU_Support = ht_info->bAMSDU_Support;
559 
560 	nMaxAMSDUSize = (pPeerHTCap->MaxAMSDUSize == 0) ? 3839 : 7935;
561 
562 	if (ht_info->nAMSDU_MaxSize > nMaxAMSDUSize)
563 		ht_info->nCurrent_AMSDU_MaxSize = nMaxAMSDUSize;
564 	else
565 		ht_info->nCurrent_AMSDU_MaxSize = ht_info->nAMSDU_MaxSize;
566 
567 	ht_info->bCurrentAMPDUEnable = ht_info->bAMPDUEnable;
568 	if (ieee->rtllib_ap_sec_type &&
569 	    (ieee->rtllib_ap_sec_type(ieee) & (SEC_ALG_WEP | SEC_ALG_TKIP))) {
570 		if ((ht_info->IOTPeer == HT_IOT_PEER_ATHEROS) ||
571 		    (ht_info->IOTPeer == HT_IOT_PEER_UNKNOWN))
572 			ht_info->bCurrentAMPDUEnable = false;
573 	}
574 
575 	if (!ht_info->reg_rt2rt_aggregation) {
576 		if (ht_info->AMPDU_Factor > pPeerHTCap->MaxRxAMPDUFactor)
577 			ht_info->CurrentAMPDUFactor =
578 						 pPeerHTCap->MaxRxAMPDUFactor;
579 		else
580 			ht_info->CurrentAMPDUFactor = ht_info->AMPDU_Factor;
581 
582 	} else {
583 		if (ieee->current_network.bssht.bd_rt2rt_aggregation) {
584 			if (ieee->pairwise_key_type != KEY_TYPE_NA)
585 				ht_info->CurrentAMPDUFactor =
586 						 pPeerHTCap->MaxRxAMPDUFactor;
587 			else
588 				ht_info->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
589 		} else {
590 			ht_info->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
591 							    HT_AGG_SIZE_32K);
592 		}
593 	}
594 	ht_info->current_mpdu_density = max_t(u8, ht_info->MPDU_Density,
595 					      pPeerHTCap->MPDUDensity);
596 	if (ht_info->iot_action & HT_IOT_ACT_TX_USE_AMSDU_8K) {
597 		ht_info->bCurrentAMPDUEnable = false;
598 		ht_info->ForcedAMSDUMode = HT_AGG_FORCE_ENABLE;
599 	}
600 	ht_info->cur_rx_reorder_enable = ht_info->reg_rx_reorder_enable;
601 
602 	if (pPeerHTCap->MCS[0] == 0)
603 		pPeerHTCap->MCS[0] = 0xff;
604 
605 	HTIOTActDetermineRaFunc(ieee, ((pPeerHTCap->MCS[1]) != 0));
606 
607 	HTFilterMCSRate(ieee, pPeerHTCap->MCS, ieee->dot11ht_oper_rate_set);
608 
609 	ht_info->peer_mimo_ps = pPeerHTCap->MimoPwrSave;
610 	if (ht_info->peer_mimo_ps == MIMO_PS_STATIC)
611 		pMcsFilter = MCS_FILTER_1SS;
612 	else
613 		pMcsFilter = MCS_FILTER_ALL;
614 	ieee->HTHighestOperaRate = HTGetHighestMCSRate(ieee,
615 						       ieee->dot11ht_oper_rate_set,
616 						       pMcsFilter);
617 	ieee->HTCurrentOperaRate = ieee->HTHighestOperaRate;
618 
619 	ht_info->current_op_mode = pPeerHTInfo->OptMode;
620 }
621 
HTInitializeHTInfo(struct rtllib_device * ieee)622 void HTInitializeHTInfo(struct rtllib_device *ieee)
623 {
624 	struct rt_hi_throughput *ht_info = ieee->ht_info;
625 
626 	ht_info->bCurrentHTSupport = false;
627 
628 	ht_info->bCurBW40MHz = false;
629 	ht_info->cur_tx_bw40mhz = false;
630 
631 	ht_info->bCurShortGI20MHz = false;
632 	ht_info->bCurShortGI40MHz = false;
633 	ht_info->forced_short_gi = false;
634 
635 	ht_info->bCurSuppCCK = true;
636 
637 	ht_info->bCurrent_AMSDU_Support = false;
638 	ht_info->nCurrent_AMSDU_MaxSize = ht_info->nAMSDU_MaxSize;
639 	ht_info->current_mpdu_density = ht_info->MPDU_Density;
640 	ht_info->CurrentAMPDUFactor = ht_info->AMPDU_Factor;
641 
642 	memset((void *)(&ht_info->SelfHTCap), 0,
643 	       sizeof(ht_info->SelfHTCap));
644 	memset((void *)(&ht_info->SelfHTInfo), 0,
645 	       sizeof(ht_info->SelfHTInfo));
646 	memset((void *)(&ht_info->PeerHTCapBuf), 0,
647 	       sizeof(ht_info->PeerHTCapBuf));
648 	memset((void *)(&ht_info->PeerHTInfoBuf), 0,
649 	       sizeof(ht_info->PeerHTInfoBuf));
650 
651 	ht_info->sw_bw_in_progress = false;
652 
653 	ht_info->ePeerHTSpecVer = HT_SPEC_VER_IEEE;
654 
655 	ht_info->current_rt2rt_aggregation = false;
656 	ht_info->current_rt2rt_long_slot_time = false;
657 	ht_info->RT2RT_HT_Mode = (enum rt_ht_capability)0;
658 
659 	ht_info->IOTPeer = 0;
660 	ht_info->iot_action = 0;
661 	ht_info->iot_ra_func = 0;
662 
663 	{
664 		u8 *RegHTSuppRateSets = &ieee->reg_ht_supp_rate_set[0];
665 
666 		RegHTSuppRateSets[0] = 0xFF;
667 		RegHTSuppRateSets[1] = 0xFF;
668 		RegHTSuppRateSets[4] = 0x01;
669 	}
670 }
671 
HTInitializeBssDesc(struct bss_ht * pBssHT)672 void HTInitializeBssDesc(struct bss_ht *pBssHT)
673 {
674 	pBssHT->bd_support_ht = false;
675 	memset(pBssHT->bd_ht_cap_buf, 0, sizeof(pBssHT->bd_ht_cap_buf));
676 	pBssHT->bd_ht_cap_len = 0;
677 	memset(pBssHT->bd_ht_info_buf, 0, sizeof(pBssHT->bd_ht_info_buf));
678 	pBssHT->bd_ht_info_len = 0;
679 
680 	pBssHT->bd_ht_spec_ver = HT_SPEC_VER_IEEE;
681 
682 	pBssHT->bd_rt2rt_aggregation = false;
683 	pBssHT->bd_rt2rt_long_slot_time = false;
684 	pBssHT->rt2rt_ht_mode = (enum rt_ht_capability)0;
685 }
686 
HTResetSelfAndSavePeerSetting(struct rtllib_device * ieee,struct rtllib_network * pNetwork)687 void HTResetSelfAndSavePeerSetting(struct rtllib_device *ieee,
688 				   struct rtllib_network *pNetwork)
689 {
690 	struct rt_hi_throughput *ht_info = ieee->ht_info;
691 	u8	bIOTAction = 0;
692 
693 	/* unmark enable_ht flag here is the same reason why unmarked in
694 	 * function rtllib_softmac_new_net. WB 2008.09.10
695 	 */
696 	if (pNetwork->bssht.bd_support_ht) {
697 		ht_info->bCurrentHTSupport = true;
698 		ht_info->ePeerHTSpecVer = pNetwork->bssht.bd_ht_spec_ver;
699 
700 		if (pNetwork->bssht.bd_ht_cap_len > 0 &&
701 		    pNetwork->bssht.bd_ht_cap_len <= sizeof(ht_info->PeerHTCapBuf))
702 			memcpy(ht_info->PeerHTCapBuf,
703 			       pNetwork->bssht.bd_ht_cap_buf,
704 			       pNetwork->bssht.bd_ht_cap_len);
705 
706 		if (pNetwork->bssht.bd_ht_info_len > 0 &&
707 		    pNetwork->bssht.bd_ht_info_len <=
708 		    sizeof(ht_info->PeerHTInfoBuf))
709 			memcpy(ht_info->PeerHTInfoBuf,
710 			       pNetwork->bssht.bd_ht_info_buf,
711 			       pNetwork->bssht.bd_ht_info_len);
712 
713 		if (ht_info->reg_rt2rt_aggregation) {
714 			ht_info->current_rt2rt_aggregation =
715 				 pNetwork->bssht.bd_rt2rt_aggregation;
716 			ht_info->current_rt2rt_long_slot_time =
717 				 pNetwork->bssht.bd_rt2rt_long_slot_time;
718 			ht_info->RT2RT_HT_Mode = pNetwork->bssht.rt2rt_ht_mode;
719 		} else {
720 			ht_info->current_rt2rt_aggregation = false;
721 			ht_info->current_rt2rt_long_slot_time = false;
722 			ht_info->RT2RT_HT_Mode = (enum rt_ht_capability)0;
723 		}
724 
725 		HTIOTPeerDetermine(ieee);
726 
727 		ht_info->iot_action = 0;
728 		bIOTAction = HTIOTActIsDisableMCS14(ieee, pNetwork->bssid);
729 		if (bIOTAction)
730 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_MCS14;
731 
732 		bIOTAction = HTIOTActIsDisableMCS15(ieee);
733 		if (bIOTAction)
734 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_MCS15;
735 
736 		bIOTAction = HTIOTActIsDisableMCSTwoSpatialStream(ieee);
737 		if (bIOTAction)
738 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_ALL_2SS;
739 
740 		bIOTAction = HTIOTActIsDisableEDCATurbo(ieee, pNetwork->bssid);
741 		if (bIOTAction)
742 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_EDCA_TURBO;
743 
744 		bIOTAction = HTIOTActIsMgntUseCCK6M(ieee, pNetwork);
745 		if (bIOTAction)
746 			ht_info->iot_action |= HT_IOT_ACT_MGNT_USE_CCK_6M;
747 		bIOTAction = HTIOTActIsCCDFsync(ieee);
748 		if (bIOTAction)
749 			ht_info->iot_action |= HT_IOT_ACT_CDD_FSYNC;
750 	} else {
751 		ht_info->bCurrentHTSupport = false;
752 		ht_info->current_rt2rt_aggregation = false;
753 		ht_info->current_rt2rt_long_slot_time = false;
754 		ht_info->RT2RT_HT_Mode = (enum rt_ht_capability)0;
755 
756 		ht_info->iot_action = 0;
757 		ht_info->iot_ra_func = 0;
758 	}
759 }
760 
HT_update_self_and_peer_setting(struct rtllib_device * ieee,struct rtllib_network * pNetwork)761 void HT_update_self_and_peer_setting(struct rtllib_device *ieee,
762 				     struct rtllib_network *pNetwork)
763 {
764 	struct rt_hi_throughput *ht_info = ieee->ht_info;
765 	struct ht_info_ele *pPeerHTInfo =
766 		 (struct ht_info_ele *)pNetwork->bssht.bd_ht_info_buf;
767 
768 	if (ht_info->bCurrentHTSupport) {
769 		if (pNetwork->bssht.bd_ht_info_len != 0)
770 			ht_info->current_op_mode = pPeerHTInfo->OptMode;
771 	}
772 }
773 EXPORT_SYMBOL(HT_update_self_and_peer_setting);
774 
HTUseDefaultSetting(struct rtllib_device * ieee)775 void HTUseDefaultSetting(struct rtllib_device *ieee)
776 {
777 	struct rt_hi_throughput *ht_info = ieee->ht_info;
778 
779 	if (ht_info->enable_ht) {
780 		ht_info->bCurrentHTSupport = true;
781 		ht_info->bCurSuppCCK = ht_info->bRegSuppCCK;
782 
783 		ht_info->bCurBW40MHz = ht_info->bRegBW40MHz;
784 		ht_info->bCurShortGI20MHz = ht_info->bRegShortGI20MHz;
785 
786 		ht_info->bCurShortGI40MHz = ht_info->bRegShortGI40MHz;
787 
788 		if (ieee->iw_mode == IW_MODE_ADHOC)
789 			ieee->current_network.qos_data.active =
790 				 ieee->current_network.qos_data.supported;
791 		ht_info->bCurrent_AMSDU_Support = ht_info->bAMSDU_Support;
792 		ht_info->nCurrent_AMSDU_MaxSize = ht_info->nAMSDU_MaxSize;
793 
794 		ht_info->bCurrentAMPDUEnable = ht_info->bAMPDUEnable;
795 		ht_info->CurrentAMPDUFactor = ht_info->AMPDU_Factor;
796 
797 		ht_info->current_mpdu_density = ht_info->current_mpdu_density;
798 
799 		HTFilterMCSRate(ieee, ieee->reg_dot11tx_ht_oper_rate_set,
800 				ieee->dot11ht_oper_rate_set);
801 		ieee->HTHighestOperaRate = HTGetHighestMCSRate(ieee,
802 							       ieee->dot11ht_oper_rate_set,
803 							       MCS_FILTER_ALL);
804 		ieee->HTCurrentOperaRate = ieee->HTHighestOperaRate;
805 
806 	} else {
807 		ht_info->bCurrentHTSupport = false;
808 	}
809 }
810 
HTCCheck(struct rtllib_device * ieee,u8 * pFrame)811 u8 HTCCheck(struct rtllib_device *ieee, u8 *pFrame)
812 {
813 	if (ieee->ht_info->bCurrentHTSupport) {
814 		if ((IsQoSDataFrame(pFrame) && Frame_Order(pFrame)) == 1) {
815 			netdev_dbg(ieee->dev, "HT CONTROL FILED EXIST!!\n");
816 			return true;
817 		}
818 	}
819 	return false;
820 }
821 
HTSetConnectBwModeCallback(struct rtllib_device * ieee)822 static void HTSetConnectBwModeCallback(struct rtllib_device *ieee)
823 {
824 	struct rt_hi_throughput *ht_info = ieee->ht_info;
825 
826 	if (ht_info->bCurBW40MHz) {
827 		if (ht_info->CurSTAExtChnlOffset == HT_EXTCHNL_OFFSET_UPPER)
828 			ieee->set_chan(ieee->dev,
829 				       ieee->current_network.channel + 2);
830 		else if (ht_info->CurSTAExtChnlOffset ==
831 			 HT_EXTCHNL_OFFSET_LOWER)
832 			ieee->set_chan(ieee->dev,
833 				       ieee->current_network.channel - 2);
834 		else
835 			ieee->set_chan(ieee->dev,
836 				       ieee->current_network.channel);
837 
838 		ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20_40,
839 				       ht_info->CurSTAExtChnlOffset);
840 	} else {
841 		ieee->set_chan(ieee->dev, ieee->current_network.channel);
842 		ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20,
843 				       HT_EXTCHNL_OFFSET_NO_EXT);
844 	}
845 
846 	ht_info->sw_bw_in_progress = false;
847 }
848 
HTSetConnectBwMode(struct rtllib_device * ieee,enum ht_channel_width bandwidth,enum ht_extchnl_offset Offset)849 void HTSetConnectBwMode(struct rtllib_device *ieee,
850 			enum ht_channel_width bandwidth,
851 			enum ht_extchnl_offset Offset)
852 {
853 	struct rt_hi_throughput *ht_info = ieee->ht_info;
854 
855 	if (!ht_info->bRegBW40MHz)
856 		return;
857 
858 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
859 		bandwidth = HT_CHANNEL_WIDTH_20;
860 
861 	if (ht_info->sw_bw_in_progress) {
862 		pr_info("%s: sw_bw_in_progress!!\n", __func__);
863 		return;
864 	}
865 	if (bandwidth == HT_CHANNEL_WIDTH_20_40) {
866 		if (ieee->current_network.channel < 2 &&
867 		    Offset == HT_EXTCHNL_OFFSET_LOWER)
868 			Offset = HT_EXTCHNL_OFFSET_NO_EXT;
869 		if (Offset == HT_EXTCHNL_OFFSET_UPPER ||
870 		    Offset == HT_EXTCHNL_OFFSET_LOWER) {
871 			ht_info->bCurBW40MHz = true;
872 			ht_info->CurSTAExtChnlOffset = Offset;
873 		} else {
874 			ht_info->bCurBW40MHz = false;
875 			ht_info->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
876 		}
877 	} else {
878 		ht_info->bCurBW40MHz = false;
879 		ht_info->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
880 	}
881 
882 	netdev_dbg(ieee->dev, "%s():ht_info->bCurBW40MHz:%x\n", __func__,
883 		   ht_info->bCurBW40MHz);
884 
885 	ht_info->sw_bw_in_progress = true;
886 
887 	HTSetConnectBwModeCallback(ieee);
888 }
889