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 *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 
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 
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 
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 
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 
210 static u8 HTIOTActIsDisableMCS14(struct rtllib_device *ieee, u8 *PeerMacAddr)
211 {
212 	return 0;
213 }
214 
215 static bool HTIOTActIsDisableMCS15(struct rtllib_device *ieee)
216 {
217 	return false;
218 }
219 
220 static bool HTIOTActIsDisableMCSTwoSpatialStream(struct rtllib_device *ieee)
221 {
222 	return false;
223 }
224 
225 static u8 HTIOTActIsDisableEDCATurbo(struct rtllib_device *ieee,
226 				     u8 *PeerMacAddr)
227 {
228 	return false;
229 }
230 
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 
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 
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 
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 
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 
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 
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 
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 WIRELESS_MODE_B:
428 	case WIRELESS_MODE_G:
429 		for (i = 0; i <= 15; i++)
430 			pOperateMCS[i] = 0;
431 		break;
432 	case WIRELESS_MODE_N_24G:
433 		pOperateMCS[0] &= RATE_ADPT_1SS_MASK;
434 		pOperateMCS[1] &= RATE_ADPT_2SS_MASK;
435 		pOperateMCS[3] &= RATE_ADPT_MCS32_MASK;
436 		break;
437 	default:
438 		break;
439 	}
440 
441 	return true;
442 }
443 
444 u8 HTGetHighestMCSRate(struct rtllib_device *ieee, u8 *pMCSRateSet,
445 		       u8 *pMCSFilter)
446 {
447 	u8		i, j;
448 	u8		bitMap;
449 	u8		mcsRate = 0;
450 	u8		availableMcsRate[16];
451 
452 	if (!pMCSRateSet || !pMCSFilter) {
453 		netdev_warn(ieee->dev,
454 			    "%s(): pMCSRateSet and pMCSFilter are null\n",
455 			    __func__);
456 		return false;
457 	}
458 	for (i = 0; i < 16; i++)
459 		availableMcsRate[i] = pMCSRateSet[i] & pMCSFilter[i];
460 
461 	for (i = 0; i < 16; i++) {
462 		if (availableMcsRate[i] != 0)
463 			break;
464 	}
465 	if (i == 16)
466 		return false;
467 
468 	for (i = 0; i < 16; i++) {
469 		if (availableMcsRate[i] != 0) {
470 			bitMap = availableMcsRate[i];
471 			for (j = 0; j < 8; j++) {
472 				if ((bitMap % 2) != 0) {
473 					if (HTMcsToDataRate(ieee, (8 * i + j)) >
474 					    HTMcsToDataRate(ieee, mcsRate))
475 						mcsRate = 8 * i + j;
476 				}
477 				bitMap >>= 1;
478 			}
479 		}
480 	}
481 	return mcsRate | 0x80;
482 }
483 
484 static u8 HTFilterMCSRate(struct rtllib_device *ieee, u8 *pSupportMCS,
485 			  u8 *pOperateMCS)
486 {
487 	u8 i;
488 
489 	for (i = 0; i <= 15; i++)
490 		pOperateMCS[i] = ieee->reg_dot11tx_ht_oper_rate_set[i] &
491 				 pSupportMCS[i];
492 
493 	HT_PickMCSRate(ieee, pOperateMCS);
494 
495 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
496 		pOperateMCS[1] = 0;
497 
498 	for (i = 2; i <= 15; i++)
499 		pOperateMCS[i] = 0;
500 
501 	return true;
502 }
503 
504 void HTSetConnectBwMode(struct rtllib_device *ieee,
505 			enum ht_channel_width bandwidth,
506 			enum ht_extchnl_offset Offset);
507 
508 void HTOnAssocRsp(struct rtllib_device *ieee)
509 {
510 	struct rt_hi_throughput *ht_info = ieee->ht_info;
511 	struct ht_capab_ele *pPeerHTCap = NULL;
512 	struct ht_info_ele *pPeerHTInfo = NULL;
513 	u16 nMaxAMSDUSize = 0;
514 	u8 *pMcsFilter = NULL;
515 
516 	static const u8 EWC11NHTCap[] = { 0x00, 0x90, 0x4c, 0x33 };
517 	static const u8 EWC11NHTInfo[] = { 0x00, 0x90, 0x4c, 0x34 };
518 
519 	if (!ht_info->bCurrentHTSupport) {
520 		netdev_warn(ieee->dev, "%s(): HT_DISABLE\n", __func__);
521 		return;
522 	}
523 	netdev_dbg(ieee->dev, "%s(): HT_ENABLE\n", __func__);
524 
525 	if (!memcmp(ht_info->PeerHTCapBuf, EWC11NHTCap, sizeof(EWC11NHTCap)))
526 		pPeerHTCap = (struct ht_capab_ele *)(&ht_info->PeerHTCapBuf[4]);
527 	else
528 		pPeerHTCap = (struct ht_capab_ele *)(ht_info->PeerHTCapBuf);
529 
530 	if (!memcmp(ht_info->PeerHTInfoBuf, EWC11NHTInfo, sizeof(EWC11NHTInfo)))
531 		pPeerHTInfo = (struct ht_info_ele *)
532 			     (&ht_info->PeerHTInfoBuf[4]);
533 	else
534 		pPeerHTInfo = (struct ht_info_ele *)(ht_info->PeerHTInfoBuf);
535 
536 #ifdef VERBOSE_DEBUG
537 	print_hex_dump_bytes("%s: ", __func__, DUMP_PREFIX_NONE,
538 			     pPeerHTCap, sizeof(struct ht_capab_ele));
539 #endif
540 	HTSetConnectBwMode(ieee, (enum ht_channel_width)(pPeerHTCap->ChlWidth),
541 			   (enum ht_extchnl_offset)(pPeerHTInfo->ExtChlOffset));
542 	ht_info->cur_tx_bw40mhz = ((pPeerHTInfo->RecommemdedTxWidth == 1) ?
543 				 true : false);
544 
545 	ht_info->bCurShortGI20MHz = ((ht_info->bRegShortGI20MHz) ?
546 				    ((pPeerHTCap->ShortGI20Mhz == 1) ?
547 				    true : false) : false);
548 	ht_info->bCurShortGI40MHz = ((ht_info->bRegShortGI40MHz) ?
549 				     ((pPeerHTCap->ShortGI40Mhz == 1) ?
550 				     true : false) : false);
551 
552 	ht_info->bCurSuppCCK = ((ht_info->bRegSuppCCK) ?
553 			       ((pPeerHTCap->DssCCk == 1) ? true :
554 			       false) : false);
555 
556 	ht_info->bCurrent_AMSDU_Support = ht_info->bAMSDU_Support;
557 
558 	nMaxAMSDUSize = (pPeerHTCap->MaxAMSDUSize == 0) ? 3839 : 7935;
559 
560 	if (ht_info->nAMSDU_MaxSize > nMaxAMSDUSize)
561 		ht_info->nCurrent_AMSDU_MaxSize = nMaxAMSDUSize;
562 	else
563 		ht_info->nCurrent_AMSDU_MaxSize = ht_info->nAMSDU_MaxSize;
564 
565 	ht_info->bCurrentAMPDUEnable = ht_info->bAMPDUEnable;
566 	if (ieee->rtllib_ap_sec_type &&
567 	    (ieee->rtllib_ap_sec_type(ieee) & (SEC_ALG_WEP | SEC_ALG_TKIP))) {
568 		if ((ht_info->IOTPeer == HT_IOT_PEER_ATHEROS) ||
569 		    (ht_info->IOTPeer == HT_IOT_PEER_UNKNOWN))
570 			ht_info->bCurrentAMPDUEnable = false;
571 	}
572 
573 	if (!ht_info->reg_rt2rt_aggregation) {
574 		if (ht_info->AMPDU_Factor > pPeerHTCap->MaxRxAMPDUFactor)
575 			ht_info->CurrentAMPDUFactor =
576 						 pPeerHTCap->MaxRxAMPDUFactor;
577 		else
578 			ht_info->CurrentAMPDUFactor = ht_info->AMPDU_Factor;
579 
580 	} else {
581 		if (ieee->current_network.bssht.bd_rt2rt_aggregation) {
582 			if (ieee->pairwise_key_type != KEY_TYPE_NA)
583 				ht_info->CurrentAMPDUFactor =
584 						 pPeerHTCap->MaxRxAMPDUFactor;
585 			else
586 				ht_info->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
587 		} else {
588 			ht_info->CurrentAMPDUFactor = min_t(u32, pPeerHTCap->MaxRxAMPDUFactor,
589 							    HT_AGG_SIZE_32K);
590 		}
591 	}
592 	ht_info->current_mpdu_density = max_t(u8, ht_info->MPDU_Density,
593 					      pPeerHTCap->MPDUDensity);
594 	if (ht_info->iot_action & HT_IOT_ACT_TX_USE_AMSDU_8K) {
595 		ht_info->bCurrentAMPDUEnable = false;
596 		ht_info->ForcedAMSDUMode = HT_AGG_FORCE_ENABLE;
597 	}
598 	ht_info->cur_rx_reorder_enable = ht_info->reg_rx_reorder_enable;
599 
600 	if (pPeerHTCap->MCS[0] == 0)
601 		pPeerHTCap->MCS[0] = 0xff;
602 
603 	HTIOTActDetermineRaFunc(ieee, ((pPeerHTCap->MCS[1]) != 0));
604 
605 	HTFilterMCSRate(ieee, pPeerHTCap->MCS, ieee->dot11ht_oper_rate_set);
606 
607 	ht_info->peer_mimo_ps = pPeerHTCap->MimoPwrSave;
608 	if (ht_info->peer_mimo_ps == MIMO_PS_STATIC)
609 		pMcsFilter = MCS_FILTER_1SS;
610 	else
611 		pMcsFilter = MCS_FILTER_ALL;
612 	ieee->HTHighestOperaRate = HTGetHighestMCSRate(ieee,
613 						       ieee->dot11ht_oper_rate_set,
614 						       pMcsFilter);
615 	ieee->HTCurrentOperaRate = ieee->HTHighestOperaRate;
616 
617 	ht_info->current_op_mode = pPeerHTInfo->OptMode;
618 }
619 
620 void HTInitializeHTInfo(struct rtllib_device *ieee)
621 {
622 	struct rt_hi_throughput *ht_info = ieee->ht_info;
623 
624 	ht_info->bCurrentHTSupport = false;
625 
626 	ht_info->bCurBW40MHz = false;
627 	ht_info->cur_tx_bw40mhz = false;
628 
629 	ht_info->bCurShortGI20MHz = false;
630 	ht_info->bCurShortGI40MHz = false;
631 	ht_info->forced_short_gi = false;
632 
633 	ht_info->bCurSuppCCK = true;
634 
635 	ht_info->bCurrent_AMSDU_Support = false;
636 	ht_info->nCurrent_AMSDU_MaxSize = ht_info->nAMSDU_MaxSize;
637 	ht_info->current_mpdu_density = ht_info->MPDU_Density;
638 	ht_info->CurrentAMPDUFactor = ht_info->AMPDU_Factor;
639 
640 	memset((void *)(&ht_info->SelfHTCap), 0,
641 	       sizeof(ht_info->SelfHTCap));
642 	memset((void *)(&ht_info->SelfHTInfo), 0,
643 	       sizeof(ht_info->SelfHTInfo));
644 	memset((void *)(&ht_info->PeerHTCapBuf), 0,
645 	       sizeof(ht_info->PeerHTCapBuf));
646 	memset((void *)(&ht_info->PeerHTInfoBuf), 0,
647 	       sizeof(ht_info->PeerHTInfoBuf));
648 
649 	ht_info->sw_bw_in_progress = false;
650 
651 	ht_info->ePeerHTSpecVer = HT_SPEC_VER_IEEE;
652 
653 	ht_info->current_rt2rt_aggregation = false;
654 	ht_info->current_rt2rt_long_slot_time = false;
655 	ht_info->RT2RT_HT_Mode = (enum rt_ht_capability)0;
656 
657 	ht_info->IOTPeer = 0;
658 	ht_info->iot_action = 0;
659 	ht_info->iot_ra_func = 0;
660 
661 	{
662 		u8 *RegHTSuppRateSets = &ieee->reg_ht_supp_rate_set[0];
663 
664 		RegHTSuppRateSets[0] = 0xFF;
665 		RegHTSuppRateSets[1] = 0xFF;
666 		RegHTSuppRateSets[4] = 0x01;
667 	}
668 }
669 
670 void HTInitializeBssDesc(struct bss_ht *pBssHT)
671 {
672 	pBssHT->bd_support_ht = false;
673 	memset(pBssHT->bd_ht_cap_buf, 0, sizeof(pBssHT->bd_ht_cap_buf));
674 	pBssHT->bd_ht_cap_len = 0;
675 	memset(pBssHT->bd_ht_info_buf, 0, sizeof(pBssHT->bd_ht_info_buf));
676 	pBssHT->bd_ht_info_len = 0;
677 
678 	pBssHT->bd_ht_spec_ver = HT_SPEC_VER_IEEE;
679 
680 	pBssHT->bd_rt2rt_aggregation = false;
681 	pBssHT->bd_rt2rt_long_slot_time = false;
682 	pBssHT->rt2rt_ht_mode = (enum rt_ht_capability)0;
683 }
684 
685 void HTResetSelfAndSavePeerSetting(struct rtllib_device *ieee,
686 				   struct rtllib_network *pNetwork)
687 {
688 	struct rt_hi_throughput *ht_info = ieee->ht_info;
689 	u8	bIOTAction = 0;
690 
691 	/* unmark enable_ht flag here is the same reason why unmarked in
692 	 * function rtllib_softmac_new_net. WB 2008.09.10
693 	 */
694 	if (pNetwork->bssht.bd_support_ht) {
695 		ht_info->bCurrentHTSupport = true;
696 		ht_info->ePeerHTSpecVer = pNetwork->bssht.bd_ht_spec_ver;
697 
698 		if (pNetwork->bssht.bd_ht_cap_len > 0 &&
699 		    pNetwork->bssht.bd_ht_cap_len <= sizeof(ht_info->PeerHTCapBuf))
700 			memcpy(ht_info->PeerHTCapBuf,
701 			       pNetwork->bssht.bd_ht_cap_buf,
702 			       pNetwork->bssht.bd_ht_cap_len);
703 
704 		if (pNetwork->bssht.bd_ht_info_len > 0 &&
705 		    pNetwork->bssht.bd_ht_info_len <=
706 		    sizeof(ht_info->PeerHTInfoBuf))
707 			memcpy(ht_info->PeerHTInfoBuf,
708 			       pNetwork->bssht.bd_ht_info_buf,
709 			       pNetwork->bssht.bd_ht_info_len);
710 
711 		if (ht_info->reg_rt2rt_aggregation) {
712 			ht_info->current_rt2rt_aggregation =
713 				 pNetwork->bssht.bd_rt2rt_aggregation;
714 			ht_info->current_rt2rt_long_slot_time =
715 				 pNetwork->bssht.bd_rt2rt_long_slot_time;
716 			ht_info->RT2RT_HT_Mode = pNetwork->bssht.rt2rt_ht_mode;
717 		} else {
718 			ht_info->current_rt2rt_aggregation = false;
719 			ht_info->current_rt2rt_long_slot_time = false;
720 			ht_info->RT2RT_HT_Mode = (enum rt_ht_capability)0;
721 		}
722 
723 		HTIOTPeerDetermine(ieee);
724 
725 		ht_info->iot_action = 0;
726 		bIOTAction = HTIOTActIsDisableMCS14(ieee, pNetwork->bssid);
727 		if (bIOTAction)
728 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_MCS14;
729 
730 		bIOTAction = HTIOTActIsDisableMCS15(ieee);
731 		if (bIOTAction)
732 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_MCS15;
733 
734 		bIOTAction = HTIOTActIsDisableMCSTwoSpatialStream(ieee);
735 		if (bIOTAction)
736 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_ALL_2SS;
737 
738 		bIOTAction = HTIOTActIsDisableEDCATurbo(ieee, pNetwork->bssid);
739 		if (bIOTAction)
740 			ht_info->iot_action |= HT_IOT_ACT_DISABLE_EDCA_TURBO;
741 
742 		bIOTAction = HTIOTActIsMgntUseCCK6M(ieee, pNetwork);
743 		if (bIOTAction)
744 			ht_info->iot_action |= HT_IOT_ACT_MGNT_USE_CCK_6M;
745 		bIOTAction = HTIOTActIsCCDFsync(ieee);
746 		if (bIOTAction)
747 			ht_info->iot_action |= HT_IOT_ACT_CDD_FSYNC;
748 	} else {
749 		ht_info->bCurrentHTSupport = false;
750 		ht_info->current_rt2rt_aggregation = false;
751 		ht_info->current_rt2rt_long_slot_time = false;
752 		ht_info->RT2RT_HT_Mode = (enum rt_ht_capability)0;
753 
754 		ht_info->iot_action = 0;
755 		ht_info->iot_ra_func = 0;
756 	}
757 }
758 
759 void HT_update_self_and_peer_setting(struct rtllib_device *ieee,
760 				     struct rtllib_network *pNetwork)
761 {
762 	struct rt_hi_throughput *ht_info = ieee->ht_info;
763 	struct ht_info_ele *pPeerHTInfo =
764 		 (struct ht_info_ele *)pNetwork->bssht.bd_ht_info_buf;
765 
766 	if (ht_info->bCurrentHTSupport) {
767 		if (pNetwork->bssht.bd_ht_info_len != 0)
768 			ht_info->current_op_mode = pPeerHTInfo->OptMode;
769 	}
770 }
771 EXPORT_SYMBOL(HT_update_self_and_peer_setting);
772 
773 void HTUseDefaultSetting(struct rtllib_device *ieee)
774 {
775 	struct rt_hi_throughput *ht_info = ieee->ht_info;
776 
777 	if (ht_info->enable_ht) {
778 		ht_info->bCurrentHTSupport = true;
779 		ht_info->bCurSuppCCK = ht_info->bRegSuppCCK;
780 
781 		ht_info->bCurBW40MHz = ht_info->bRegBW40MHz;
782 		ht_info->bCurShortGI20MHz = ht_info->bRegShortGI20MHz;
783 
784 		ht_info->bCurShortGI40MHz = ht_info->bRegShortGI40MHz;
785 
786 		if (ieee->iw_mode == IW_MODE_ADHOC)
787 			ieee->current_network.qos_data.active =
788 				 ieee->current_network.qos_data.supported;
789 		ht_info->bCurrent_AMSDU_Support = ht_info->bAMSDU_Support;
790 		ht_info->nCurrent_AMSDU_MaxSize = ht_info->nAMSDU_MaxSize;
791 
792 		ht_info->bCurrentAMPDUEnable = ht_info->bAMPDUEnable;
793 		ht_info->CurrentAMPDUFactor = ht_info->AMPDU_Factor;
794 
795 		ht_info->current_mpdu_density = ht_info->current_mpdu_density;
796 
797 		HTFilterMCSRate(ieee, ieee->reg_dot11tx_ht_oper_rate_set,
798 				ieee->dot11ht_oper_rate_set);
799 		ieee->HTHighestOperaRate = HTGetHighestMCSRate(ieee,
800 							       ieee->dot11ht_oper_rate_set,
801 							       MCS_FILTER_ALL);
802 		ieee->HTCurrentOperaRate = ieee->HTHighestOperaRate;
803 
804 	} else {
805 		ht_info->bCurrentHTSupport = false;
806 	}
807 }
808 
809 u8 HTCCheck(struct rtllib_device *ieee, u8 *pFrame)
810 {
811 	if (ieee->ht_info->bCurrentHTSupport) {
812 		if ((IsQoSDataFrame(pFrame) && Frame_Order(pFrame)) == 1) {
813 			netdev_dbg(ieee->dev, "HT CONTROL FILED EXIST!!\n");
814 			return true;
815 		}
816 	}
817 	return false;
818 }
819 
820 static void HTSetConnectBwModeCallback(struct rtllib_device *ieee)
821 {
822 	struct rt_hi_throughput *ht_info = ieee->ht_info;
823 
824 	if (ht_info->bCurBW40MHz) {
825 		if (ht_info->CurSTAExtChnlOffset == HT_EXTCHNL_OFFSET_UPPER)
826 			ieee->set_chan(ieee->dev,
827 				       ieee->current_network.channel + 2);
828 		else if (ht_info->CurSTAExtChnlOffset ==
829 			 HT_EXTCHNL_OFFSET_LOWER)
830 			ieee->set_chan(ieee->dev,
831 				       ieee->current_network.channel - 2);
832 		else
833 			ieee->set_chan(ieee->dev,
834 				       ieee->current_network.channel);
835 
836 		ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20_40,
837 				       ht_info->CurSTAExtChnlOffset);
838 	} else {
839 		ieee->set_chan(ieee->dev, ieee->current_network.channel);
840 		ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20,
841 				       HT_EXTCHNL_OFFSET_NO_EXT);
842 	}
843 
844 	ht_info->sw_bw_in_progress = false;
845 }
846 
847 void HTSetConnectBwMode(struct rtllib_device *ieee,
848 			enum ht_channel_width bandwidth,
849 			enum ht_extchnl_offset Offset)
850 {
851 	struct rt_hi_throughput *ht_info = ieee->ht_info;
852 
853 	if (!ht_info->bRegBW40MHz)
854 		return;
855 
856 	if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
857 		bandwidth = HT_CHANNEL_WIDTH_20;
858 
859 	if (ht_info->sw_bw_in_progress) {
860 		pr_info("%s: sw_bw_in_progress!!\n", __func__);
861 		return;
862 	}
863 	if (bandwidth == HT_CHANNEL_WIDTH_20_40) {
864 		if (ieee->current_network.channel < 2 &&
865 		    Offset == HT_EXTCHNL_OFFSET_LOWER)
866 			Offset = HT_EXTCHNL_OFFSET_NO_EXT;
867 		if (Offset == HT_EXTCHNL_OFFSET_UPPER ||
868 		    Offset == HT_EXTCHNL_OFFSET_LOWER) {
869 			ht_info->bCurBW40MHz = true;
870 			ht_info->CurSTAExtChnlOffset = Offset;
871 		} else {
872 			ht_info->bCurBW40MHz = false;
873 			ht_info->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
874 		}
875 	} else {
876 		ht_info->bCurBW40MHz = false;
877 		ht_info->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
878 	}
879 
880 	netdev_dbg(ieee->dev, "%s():ht_info->bCurBW40MHz:%x\n", __func__,
881 		   ht_info->bCurBW40MHz);
882 
883 	ht_info->sw_bw_in_progress = true;
884 
885 	HTSetConnectBwModeCallback(ieee);
886 }
887