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