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 
269 void HTResetIOTSetting(struct rt_hi_throughput *pHTInfo)
270 {
271 	pHTInfo->IOTAction = 0;
272 	pHTInfo->IOTPeer = HT_IOT_PEER_UNKNOWN;
273 	pHTInfo->IOTRaFunc = 0;
274 }
275 
276 void HTConstructCapabilityElement(struct rtllib_device *ieee, u8 *posHTCap,
277 				  u8 *len, u8 IsEncrypt, bool bAssoc)
278 {
279 	struct rt_hi_throughput *pHT = ieee->pHTInfo;
280 	struct ht_capab_ele *pCapELE = NULL;
281 
282 	if ((posHTCap == NULL) || (pHT == NULL)) {
283 		netdev_warn(ieee->dev,
284 			    "%s(): posHTCap and pHTInfo are null\n", __func__);
285 		return;
286 	}
287 	memset(posHTCap, 0, *len);
288 
289 	if ((bAssoc) && (pHT->ePeerHTSpecVer == HT_SPEC_VER_EWC)) {
290 		u8	EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};
291 
292 		memcpy(posHTCap, EWC11NHTCap, sizeof(EWC11NHTCap));
293 		pCapELE = (struct ht_capab_ele *)&(posHTCap[4]);
294 		*len = 30 + 2;
295 	} else {
296 		pCapELE = (struct ht_capab_ele *)posHTCap;
297 		*len = 26 + 2;
298 	}
299 
300 	pCapELE->AdvCoding		= 0;
301 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
302 		pCapELE->ChlWidth = 0;
303 	else
304 		pCapELE->ChlWidth = (pHT->bRegBW40MHz ? 1 : 0);
305 
306 	pCapELE->MimoPwrSave		= pHT->SelfMimoPs;
307 	pCapELE->GreenField		= 0;
308 	pCapELE->ShortGI20Mhz		= 1;
309 	pCapELE->ShortGI40Mhz		= 1;
310 
311 	pCapELE->TxSTBC			= 1;
312 	pCapELE->RxSTBC			= 0;
313 	pCapELE->DelayBA		= 0;
314 	pCapELE->MaxAMSDUSize = (MAX_RECEIVE_BUFFER_SIZE >= 7935) ? 1 : 0;
315 	pCapELE->DssCCk = ((pHT->bRegBW40MHz) ? (pHT->bRegSuppCCK ? 1 : 0) : 0);
316 	pCapELE->PSMP = 0;
317 	pCapELE->LSigTxopProtect = 0;
318 
319 
320 	netdev_dbg(ieee->dev,
321 		   "TX HT cap/info ele BW=%d MaxAMSDUSize:%d DssCCk:%d\n",
322 		   pCapELE->ChlWidth, pCapELE->MaxAMSDUSize, pCapELE->DssCCk);
323 
324 	if (IsEncrypt) {
325 		pCapELE->MPDUDensity	= 7;
326 		pCapELE->MaxRxAMPDUFactor	= 2;
327 	} else {
328 		pCapELE->MaxRxAMPDUFactor	= 3;
329 		pCapELE->MPDUDensity	= 0;
330 	}
331 
332 	memcpy(pCapELE->MCS, ieee->Regdot11HTOperationalRateSet, 16);
333 	memset(&pCapELE->ExtHTCapInfo, 0, 2);
334 	memset(pCapELE->TxBFCap, 0, 4);
335 
336 	pCapELE->ASCap = 0;
337 
338 	if (bAssoc) {
339 		if (pHT->IOTAction & HT_IOT_ACT_DISABLE_MCS15)
340 			pCapELE->MCS[1] &= 0x7f;
341 
342 		if (pHT->IOTAction & HT_IOT_ACT_DISABLE_MCS14)
343 			pCapELE->MCS[1] &= 0xbf;
344 
345 		if (pHT->IOTAction & HT_IOT_ACT_DISABLE_ALL_2SS)
346 			pCapELE->MCS[1] &= 0x00;
347 
348 		if (pHT->IOTAction & HT_IOT_ACT_DISABLE_RX_40MHZ_SHORT_GI)
349 			pCapELE->ShortGI40Mhz		= 0;
350 
351 		if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev)) {
352 			pCapELE->ChlWidth = 0;
353 			pCapELE->MCS[1] = 0;
354 		}
355 	}
356 }
357 
358 void HTConstructInfoElement(struct rtllib_device *ieee, u8 *posHTInfo,
359 			    u8 *len, u8 IsEncrypt)
360 {
361 	struct rt_hi_throughput *pHT = ieee->pHTInfo;
362 	struct ht_info_ele *pHTInfoEle = (struct ht_info_ele *)posHTInfo;
363 
364 	if ((posHTInfo == NULL) || (pHTInfoEle == NULL)) {
365 		netdev_warn(ieee->dev,
366 			    "%s(): posHTInfo and pHTInfoEle are null\n",
367 			    __func__);
368 		return;
369 	}
370 
371 	memset(posHTInfo, 0, *len);
372 	if ((ieee->iw_mode == IW_MODE_ADHOC) ||
373 	    (ieee->iw_mode == IW_MODE_MASTER)) {
374 		pHTInfoEle->ControlChl	= ieee->current_network.channel;
375 		pHTInfoEle->ExtChlOffset = ((!pHT->bRegBW40MHz) ?
376 					    HT_EXTCHNL_OFFSET_NO_EXT :
377 					    (ieee->current_network.channel <= 6)
378 					    ? HT_EXTCHNL_OFFSET_UPPER :
379 					    HT_EXTCHNL_OFFSET_LOWER);
380 		pHTInfoEle->RecommemdedTxWidth	= pHT->bRegBW40MHz;
381 		pHTInfoEle->RIFS			= 0;
382 		pHTInfoEle->PSMPAccessOnly		= 0;
383 		pHTInfoEle->SrvIntGranularity		= 0;
384 		pHTInfoEle->OptMode			= pHT->CurrentOpMode;
385 		pHTInfoEle->NonGFDevPresent		= 0;
386 		pHTInfoEle->DualBeacon			= 0;
387 		pHTInfoEle->SecondaryBeacon		= 0;
388 		pHTInfoEle->LSigTxopProtectFull		= 0;
389 		pHTInfoEle->PcoActive			= 0;
390 		pHTInfoEle->PcoPhase			= 0;
391 
392 		memset(pHTInfoEle->BasicMSC, 0, 16);
393 
394 
395 		*len = 22 + 2;
396 
397 	} else {
398 		*len = 0;
399 	}
400 }
401 
402 void HTConstructRT2RTAggElement(struct rtllib_device *ieee, u8 *posRT2RTAgg,
403 				u8 *len)
404 {
405 	if (posRT2RTAgg == NULL) {
406 		netdev_warn(ieee->dev, "%s(): posRT2RTAgg is null\n", __func__);
407 		return;
408 	}
409 	memset(posRT2RTAgg, 0, *len);
410 	*posRT2RTAgg++ = 0x00;
411 	*posRT2RTAgg++ = 0xe0;
412 	*posRT2RTAgg++ = 0x4c;
413 	*posRT2RTAgg++ = 0x02;
414 	*posRT2RTAgg++ = 0x01;
415 
416 	*posRT2RTAgg = 0x30;
417 
418 	if (ieee->bSupportRemoteWakeUp)
419 		*posRT2RTAgg |= RT_HT_CAP_USE_WOW;
420 
421 	*len = 6 + 2;
422 }
423 
424 static u8 HT_PickMCSRate(struct rtllib_device *ieee, u8 *pOperateMCS)
425 {
426 	u8 i;
427 
428 	if (pOperateMCS == NULL) {
429 		netdev_warn(ieee->dev, "%s(): pOperateMCS is null\n", __func__);
430 		return false;
431 	}
432 
433 	switch (ieee->mode) {
434 	case IEEE_A:
435 	case IEEE_B:
436 	case IEEE_G:
437 		for (i = 0; i <= 15; i++)
438 			pOperateMCS[i] = 0;
439 		break;
440 	case IEEE_N_24G:
441 	case IEEE_N_5G:
442 		pOperateMCS[0] &= RATE_ADPT_1SS_MASK;
443 		pOperateMCS[1] &= RATE_ADPT_2SS_MASK;
444 		pOperateMCS[3] &= RATE_ADPT_MCS32_MASK;
445 		break;
446 	default:
447 		break;
448 
449 	}
450 
451 	return true;
452 }
453 
454 u8 HTGetHighestMCSRate(struct rtllib_device *ieee, u8 *pMCSRateSet,
455 		       u8 *pMCSFilter)
456 {
457 	u8		i, j;
458 	u8		bitMap;
459 	u8		mcsRate = 0;
460 	u8		availableMcsRate[16];
461 
462 	if (pMCSRateSet == NULL || pMCSFilter == NULL) {
463 		netdev_warn(ieee->dev,
464 			    "%s(): pMCSRateSet and pMCSFilter are null\n",
465 			    __func__);
466 		return false;
467 	}
468 	for (i = 0; i < 16; i++)
469 		availableMcsRate[i] = pMCSRateSet[i] & pMCSFilter[i];
470 
471 	for (i = 0; i < 16; i++) {
472 		if (availableMcsRate[i] != 0)
473 			break;
474 	}
475 	if (i == 16)
476 		return false;
477 
478 	for (i = 0; i < 16; i++) {
479 		if (availableMcsRate[i] != 0) {
480 			bitMap = availableMcsRate[i];
481 			for (j = 0; j < 8; j++) {
482 				if ((bitMap%2) != 0) {
483 					if (HTMcsToDataRate(ieee, (8*i+j)) >
484 					    HTMcsToDataRate(ieee, mcsRate))
485 						mcsRate = 8 * i + j;
486 				}
487 				bitMap >>= 1;
488 			}
489 		}
490 	}
491 	return mcsRate | 0x80;
492 }
493 
494 static u8 HTFilterMCSRate(struct rtllib_device *ieee, u8 *pSupportMCS,
495 			  u8 *pOperateMCS)
496 {
497 
498 	u8 i;
499 
500 	for (i = 0; i <= 15; i++)
501 		pOperateMCS[i] = ieee->Regdot11TxHTOperationalRateSet[i] &
502 				 pSupportMCS[i];
503 
504 	HT_PickMCSRate(ieee, pOperateMCS);
505 
506 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
507 		pOperateMCS[1] = 0;
508 
509 	for (i = 2; i <= 15; i++)
510 		pOperateMCS[i] = 0;
511 
512 	return true;
513 }
514 
515 void HTSetConnectBwMode(struct rtllib_device *ieee,
516 			enum ht_channel_width Bandwidth,
517 			enum ht_extchnl_offset Offset);
518 
519 void HTOnAssocRsp(struct rtllib_device *ieee)
520 {
521 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
522 	struct ht_capab_ele *pPeerHTCap = NULL;
523 	struct ht_info_ele *pPeerHTInfo = NULL;
524 	u16 nMaxAMSDUSize = 0;
525 	u8 *pMcsFilter = NULL;
526 
527 	static u8 EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};
528 	static u8 EWC11NHTInfo[] = {0x00, 0x90, 0x4c, 0x34};
529 
530 	if (!pHTInfo->bCurrentHTSupport) {
531 		netdev_warn(ieee->dev, "%s(): HT_DISABLE\n", __func__);
532 		return;
533 	}
534 	netdev_dbg(ieee->dev, "%s(): HT_ENABLE\n", __func__);
535 
536 	if (!memcmp(pHTInfo->PeerHTCapBuf, EWC11NHTCap, sizeof(EWC11NHTCap)))
537 		pPeerHTCap = (struct ht_capab_ele *)(&pHTInfo->PeerHTCapBuf[4]);
538 	else
539 		pPeerHTCap = (struct ht_capab_ele *)(pHTInfo->PeerHTCapBuf);
540 
541 	if (!memcmp(pHTInfo->PeerHTInfoBuf, EWC11NHTInfo, sizeof(EWC11NHTInfo)))
542 		pPeerHTInfo = (struct ht_info_ele *)
543 			     (&pHTInfo->PeerHTInfoBuf[4]);
544 	else
545 		pPeerHTInfo = (struct ht_info_ele *)(pHTInfo->PeerHTInfoBuf);
546 
547 
548 #ifdef VERBOSE_DEBUG
549 	print_hex_dump_bytes("%s: ", __func__, DUMP_PREFIX_NONE,
550 			     pPeerHTCap, sizeof(struct ht_capab_ele));
551 #endif
552 	HTSetConnectBwMode(ieee, (enum ht_channel_width)(pPeerHTCap->ChlWidth),
553 			  (enum ht_extchnl_offset)(pPeerHTInfo->ExtChlOffset));
554 	pHTInfo->bCurTxBW40MHz = ((pPeerHTInfo->RecommemdedTxWidth == 1) ?
555 				 true : false);
556 
557 	pHTInfo->bCurShortGI20MHz = ((pHTInfo->bRegShortGI20MHz) ?
558 				    ((pPeerHTCap->ShortGI20Mhz == 1) ?
559 				    true : false) : false);
560 	pHTInfo->bCurShortGI40MHz = ((pHTInfo->bRegShortGI40MHz) ?
561 				     ((pPeerHTCap->ShortGI40Mhz == 1) ?
562 				     true : false) : false);
563 
564 	pHTInfo->bCurSuppCCK = ((pHTInfo->bRegSuppCCK) ?
565 			       ((pPeerHTCap->DssCCk == 1) ? true :
566 			       false) : false);
567 
568 
569 	pHTInfo->bCurrent_AMSDU_Support = pHTInfo->bAMSDU_Support;
570 
571 	nMaxAMSDUSize = (pPeerHTCap->MaxAMSDUSize == 0) ? 3839 : 7935;
572 
573 	if (pHTInfo->nAMSDU_MaxSize > nMaxAMSDUSize)
574 		pHTInfo->nCurrent_AMSDU_MaxSize = nMaxAMSDUSize;
575 	else
576 		pHTInfo->nCurrent_AMSDU_MaxSize = pHTInfo->nAMSDU_MaxSize;
577 
578 	pHTInfo->bCurrentAMPDUEnable = pHTInfo->bAMPDUEnable;
579 	if (ieee->rtllib_ap_sec_type &&
580 	   (ieee->rtllib_ap_sec_type(ieee)&(SEC_ALG_WEP|SEC_ALG_TKIP))) {
581 		if ((pHTInfo->IOTPeer == HT_IOT_PEER_ATHEROS) ||
582 				(pHTInfo->IOTPeer == HT_IOT_PEER_UNKNOWN))
583 			pHTInfo->bCurrentAMPDUEnable = false;
584 	}
585 
586 	if (!pHTInfo->bRegRT2RTAggregation) {
587 		if (pHTInfo->AMPDU_Factor > pPeerHTCap->MaxRxAMPDUFactor)
588 			pHTInfo->CurrentAMPDUFactor =
589 						 pPeerHTCap->MaxRxAMPDUFactor;
590 		else
591 			pHTInfo->CurrentAMPDUFactor = pHTInfo->AMPDU_Factor;
592 
593 	} else {
594 		if (ieee->current_network.bssht.bd_rt2rt_aggregation) {
595 			if (ieee->pairwise_key_type != KEY_TYPE_NA)
596 				pHTInfo->CurrentAMPDUFactor =
597 						 pPeerHTCap->MaxRxAMPDUFactor;
598 			else
599 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
600 		} else {
601 			if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
602 				pHTInfo->CurrentAMPDUFactor =
603 						 pPeerHTCap->MaxRxAMPDUFactor;
604 			else
605 				pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
606 		}
607 	}
608 	if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
609 		pHTInfo->CurrentMPDUDensity = pHTInfo->MPDU_Density;
610 	else
611 		pHTInfo->CurrentMPDUDensity = pPeerHTCap->MPDUDensity;
612 	if (pHTInfo->IOTAction & HT_IOT_ACT_TX_USE_AMSDU_8K) {
613 		pHTInfo->bCurrentAMPDUEnable = false;
614 		pHTInfo->ForcedAMSDUMode = HT_AGG_FORCE_ENABLE;
615 		pHTInfo->ForcedAMSDUMaxSize = 7935;
616 	}
617 	pHTInfo->bCurRxReorderEnable = pHTInfo->bRegRxReorderEnable;
618 
619 	if (pPeerHTCap->MCS[0] == 0)
620 		pPeerHTCap->MCS[0] = 0xff;
621 
622 	HTIOTActDetermineRaFunc(ieee, ((pPeerHTCap->MCS[1]) != 0));
623 
624 	HTFilterMCSRate(ieee, pPeerHTCap->MCS, ieee->dot11HTOperationalRateSet);
625 
626 	pHTInfo->PeerMimoPs = pPeerHTCap->MimoPwrSave;
627 	if (pHTInfo->PeerMimoPs == MIMO_PS_STATIC)
628 		pMcsFilter = MCS_FILTER_1SS;
629 	else
630 		pMcsFilter = MCS_FILTER_ALL;
631 	ieee->HTHighestOperaRate = HTGetHighestMCSRate(ieee,
632 				   ieee->dot11HTOperationalRateSet, pMcsFilter);
633 	ieee->HTCurrentOperaRate = ieee->HTHighestOperaRate;
634 
635 	pHTInfo->CurrentOpMode = pPeerHTInfo->OptMode;
636 }
637 
638 void HTInitializeHTInfo(struct rtllib_device *ieee)
639 {
640 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
641 
642 	pHTInfo->bCurrentHTSupport = false;
643 
644 	pHTInfo->bCurBW40MHz = false;
645 	pHTInfo->bCurTxBW40MHz = false;
646 
647 	pHTInfo->bCurShortGI20MHz = false;
648 	pHTInfo->bCurShortGI40MHz = false;
649 	pHTInfo->bForcedShortGI = false;
650 
651 	pHTInfo->bCurSuppCCK = true;
652 
653 	pHTInfo->bCurrent_AMSDU_Support = false;
654 	pHTInfo->nCurrent_AMSDU_MaxSize = pHTInfo->nAMSDU_MaxSize;
655 	pHTInfo->CurrentMPDUDensity = pHTInfo->MPDU_Density;
656 	pHTInfo->CurrentAMPDUFactor = pHTInfo->AMPDU_Factor;
657 
658 	memset((void *)(&(pHTInfo->SelfHTCap)), 0,
659 		sizeof(pHTInfo->SelfHTCap));
660 	memset((void *)(&(pHTInfo->SelfHTInfo)), 0,
661 		sizeof(pHTInfo->SelfHTInfo));
662 	memset((void *)(&(pHTInfo->PeerHTCapBuf)), 0,
663 		sizeof(pHTInfo->PeerHTCapBuf));
664 	memset((void *)(&(pHTInfo->PeerHTInfoBuf)), 0,
665 		sizeof(pHTInfo->PeerHTInfoBuf));
666 
667 	pHTInfo->bSwBwInProgress = false;
668 
669 	pHTInfo->ePeerHTSpecVer = HT_SPEC_VER_IEEE;
670 
671 	pHTInfo->bCurrentRT2RTAggregation = false;
672 	pHTInfo->bCurrentRT2RTLongSlotTime = false;
673 	pHTInfo->RT2RT_HT_Mode = (enum rt_ht_capability)0;
674 
675 	pHTInfo->IOTPeer = 0;
676 	pHTInfo->IOTAction = 0;
677 	pHTInfo->IOTRaFunc = 0;
678 
679 	{
680 		u8 *RegHTSuppRateSets = &(ieee->RegHTSuppRateSet[0]);
681 
682 		RegHTSuppRateSets[0] = 0xFF;
683 		RegHTSuppRateSets[1] = 0xFF;
684 		RegHTSuppRateSets[4] = 0x01;
685 	}
686 }
687 
688 void HTInitializeBssDesc(struct bss_ht *pBssHT)
689 {
690 
691 	pBssHT->bd_support_ht = false;
692 	memset(pBssHT->bd_ht_cap_buf, 0, sizeof(pBssHT->bd_ht_cap_buf));
693 	pBssHT->bd_ht_cap_len = 0;
694 	memset(pBssHT->bd_ht_info_buf, 0, sizeof(pBssHT->bd_ht_info_buf));
695 	pBssHT->bd_ht_info_len = 0;
696 
697 	pBssHT->bd_ht_spec_ver = HT_SPEC_VER_IEEE;
698 
699 	pBssHT->bd_rt2rt_aggregation = false;
700 	pBssHT->bd_rt2rt_long_slot_time = false;
701 	pBssHT->rt2rt_ht_mode = (enum rt_ht_capability)0;
702 }
703 
704 void HTResetSelfAndSavePeerSetting(struct rtllib_device *ieee,
705 				   struct rtllib_network *pNetwork)
706 {
707 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
708 	u8	bIOTAction = 0;
709 
710 	/* unmark bEnableHT flag here is the same reason why unmarked in
711 	 * function rtllib_softmac_new_net. WB 2008.09.10
712 	 */
713 	if (pNetwork->bssht.bd_support_ht) {
714 		pHTInfo->bCurrentHTSupport = true;
715 		pHTInfo->ePeerHTSpecVer = pNetwork->bssht.bd_ht_spec_ver;
716 
717 		if (pNetwork->bssht.bd_ht_cap_len > 0 &&
718 		    pNetwork->bssht.bd_ht_cap_len <= sizeof(pHTInfo->PeerHTCapBuf))
719 			memcpy(pHTInfo->PeerHTCapBuf,
720 			       pNetwork->bssht.bd_ht_cap_buf,
721 			       pNetwork->bssht.bd_ht_cap_len);
722 
723 		if (pNetwork->bssht.bd_ht_info_len > 0 &&
724 		    pNetwork->bssht.bd_ht_info_len <=
725 		    sizeof(pHTInfo->PeerHTInfoBuf))
726 			memcpy(pHTInfo->PeerHTInfoBuf,
727 			       pNetwork->bssht.bd_ht_info_buf,
728 			       pNetwork->bssht.bd_ht_info_len);
729 
730 		if (pHTInfo->bRegRT2RTAggregation) {
731 			pHTInfo->bCurrentRT2RTAggregation =
732 				 pNetwork->bssht.bd_rt2rt_aggregation;
733 			pHTInfo->bCurrentRT2RTLongSlotTime =
734 				 pNetwork->bssht.bd_rt2rt_long_slot_time;
735 			pHTInfo->RT2RT_HT_Mode = pNetwork->bssht.rt2rt_ht_mode;
736 		} else {
737 			pHTInfo->bCurrentRT2RTAggregation = false;
738 			pHTInfo->bCurrentRT2RTLongSlotTime = false;
739 			pHTInfo->RT2RT_HT_Mode = (enum rt_ht_capability)0;
740 		}
741 
742 		HTIOTPeerDetermine(ieee);
743 
744 		pHTInfo->IOTAction = 0;
745 		bIOTAction = HTIOTActIsDisableMCS14(ieee, pNetwork->bssid);
746 		if (bIOTAction)
747 			pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_MCS14;
748 
749 		bIOTAction = HTIOTActIsDisableMCS15(ieee);
750 		if (bIOTAction)
751 			pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_MCS15;
752 
753 		bIOTAction = HTIOTActIsDisableMCSTwoSpatialStream(ieee);
754 		if (bIOTAction)
755 			pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_ALL_2SS;
756 
757 
758 		bIOTAction = HTIOTActIsDisableEDCATurbo(ieee, pNetwork->bssid);
759 		if (bIOTAction)
760 			pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_EDCA_TURBO;
761 
762 		bIOTAction = HTIOTActIsMgntUseCCK6M(ieee, pNetwork);
763 		if (bIOTAction)
764 			pHTInfo->IOTAction |= HT_IOT_ACT_MGNT_USE_CCK_6M;
765 		bIOTAction = HTIOTActIsCCDFsync(ieee);
766 		if (bIOTAction)
767 			pHTInfo->IOTAction |= HT_IOT_ACT_CDD_FSYNC;
768 	} else {
769 		pHTInfo->bCurrentHTSupport = false;
770 		pHTInfo->bCurrentRT2RTAggregation = false;
771 		pHTInfo->bCurrentRT2RTLongSlotTime = false;
772 		pHTInfo->RT2RT_HT_Mode = (enum rt_ht_capability)0;
773 
774 		pHTInfo->IOTAction = 0;
775 		pHTInfo->IOTRaFunc = 0;
776 	}
777 }
778 
779 void HT_update_self_and_peer_setting(struct rtllib_device *ieee,
780 				     struct rtllib_network *pNetwork)
781 {
782 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
783 	struct ht_info_ele *pPeerHTInfo =
784 		 (struct ht_info_ele *)pNetwork->bssht.bd_ht_info_buf;
785 
786 	if (pHTInfo->bCurrentHTSupport) {
787 		if (pNetwork->bssht.bd_ht_info_len != 0)
788 			pHTInfo->CurrentOpMode = pPeerHTInfo->OptMode;
789 	}
790 }
791 EXPORT_SYMBOL(HT_update_self_and_peer_setting);
792 
793 void HTUseDefaultSetting(struct rtllib_device *ieee)
794 {
795 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
796 
797 	if (pHTInfo->bEnableHT) {
798 		pHTInfo->bCurrentHTSupport = true;
799 		pHTInfo->bCurSuppCCK = pHTInfo->bRegSuppCCK;
800 
801 		pHTInfo->bCurBW40MHz = pHTInfo->bRegBW40MHz;
802 		pHTInfo->bCurShortGI20MHz = pHTInfo->bRegShortGI20MHz;
803 
804 		pHTInfo->bCurShortGI40MHz = pHTInfo->bRegShortGI40MHz;
805 
806 		if (ieee->iw_mode == IW_MODE_ADHOC)
807 			ieee->current_network.qos_data.active =
808 				 ieee->current_network.qos_data.supported;
809 		pHTInfo->bCurrent_AMSDU_Support = pHTInfo->bAMSDU_Support;
810 		pHTInfo->nCurrent_AMSDU_MaxSize = pHTInfo->nAMSDU_MaxSize;
811 
812 		pHTInfo->bCurrentAMPDUEnable = pHTInfo->bAMPDUEnable;
813 		pHTInfo->CurrentAMPDUFactor = pHTInfo->AMPDU_Factor;
814 
815 		pHTInfo->CurrentMPDUDensity = pHTInfo->CurrentMPDUDensity;
816 
817 		HTFilterMCSRate(ieee, ieee->Regdot11TxHTOperationalRateSet,
818 				ieee->dot11HTOperationalRateSet);
819 		ieee->HTHighestOperaRate = HTGetHighestMCSRate(ieee,
820 					   ieee->dot11HTOperationalRateSet,
821 					   MCS_FILTER_ALL);
822 		ieee->HTCurrentOperaRate = ieee->HTHighestOperaRate;
823 
824 	} else {
825 		pHTInfo->bCurrentHTSupport = false;
826 	}
827 }
828 
829 u8 HTCCheck(struct rtllib_device *ieee, u8 *pFrame)
830 {
831 	if (ieee->pHTInfo->bCurrentHTSupport) {
832 		if ((IsQoSDataFrame(pFrame) && Frame_Order(pFrame)) == 1) {
833 			netdev_dbg(ieee->dev, "HT CONTROL FILED EXIST!!\n");
834 			return true;
835 		}
836 	}
837 	return false;
838 }
839 
840 static void HTSetConnectBwModeCallback(struct rtllib_device *ieee)
841 {
842 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
843 
844 	if (pHTInfo->bCurBW40MHz) {
845 		if (pHTInfo->CurSTAExtChnlOffset == HT_EXTCHNL_OFFSET_UPPER)
846 			ieee->set_chan(ieee->dev,
847 				       ieee->current_network.channel + 2);
848 		else if (pHTInfo->CurSTAExtChnlOffset ==
849 			 HT_EXTCHNL_OFFSET_LOWER)
850 			ieee->set_chan(ieee->dev,
851 				       ieee->current_network.channel - 2);
852 		else
853 			ieee->set_chan(ieee->dev,
854 				       ieee->current_network.channel);
855 
856 		ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20_40,
857 				       pHTInfo->CurSTAExtChnlOffset);
858 	} else {
859 		ieee->set_chan(ieee->dev, ieee->current_network.channel);
860 		ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20,
861 				       HT_EXTCHNL_OFFSET_NO_EXT);
862 	}
863 
864 	pHTInfo->bSwBwInProgress = false;
865 }
866 
867 void HTSetConnectBwMode(struct rtllib_device *ieee,
868 			enum ht_channel_width Bandwidth,
869 			enum ht_extchnl_offset Offset)
870 {
871 	struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
872 
873 	if (!pHTInfo->bRegBW40MHz)
874 		return;
875 
876 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
877 		Bandwidth = HT_CHANNEL_WIDTH_20;
878 
879 	if (pHTInfo->bSwBwInProgress) {
880 		pr_info("%s: bSwBwInProgress!!\n", __func__);
881 		return;
882 	}
883 	if (Bandwidth == HT_CHANNEL_WIDTH_20_40) {
884 		if (ieee->current_network.channel < 2 &&
885 		    Offset == HT_EXTCHNL_OFFSET_LOWER)
886 			Offset = HT_EXTCHNL_OFFSET_NO_EXT;
887 		if (Offset == HT_EXTCHNL_OFFSET_UPPER ||
888 		    Offset == HT_EXTCHNL_OFFSET_LOWER) {
889 			pHTInfo->bCurBW40MHz = true;
890 			pHTInfo->CurSTAExtChnlOffset = Offset;
891 		} else {
892 			pHTInfo->bCurBW40MHz = false;
893 			pHTInfo->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
894 		}
895 	} else {
896 		pHTInfo->bCurBW40MHz = false;
897 		pHTInfo->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
898 	}
899 
900 	netdev_dbg(ieee->dev, "%s():pHTInfo->bCurBW40MHz:%x\n", __func__,
901 		   pHTInfo->bCurBW40MHz);
902 
903 	pHTInfo->bSwBwInProgress = true;
904 
905 	HTSetConnectBwModeCallback(ieee);
906 }
907