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