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