1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 #define _RTL8188E_CMD_C_
5 
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/recv_osdep.h"
9 #include "../include/mlme_osdep.h"
10 #include "../include/rtw_ioctl_set.h"
11 
12 #include "../include/rtl8188e_hal.h"
13 
14 #define RTL88E_MAX_H2C_BOX_NUMS		4
15 #define RTL88E_MAX_CMD_LEN		7
16 #define RTL88E_MESSAGE_BOX_SIZE		4
17 #define RTL88E_EX_MESSAGE_BOX_SIZE	4
18 
_is_fw_read_cmd_down(struct adapter * adapt,u8 msgbox_num)19 static u8 _is_fw_read_cmd_down(struct adapter *adapt, u8 msgbox_num)
20 {
21 	u8 read_down = false, reg;
22 	int	retry_cnts = 100;
23 	int res;
24 
25 	u8 valid;
26 
27 	do {
28 		res = rtw_read8(adapt, REG_HMETFR, &reg);
29 		if (res)
30 			continue;
31 
32 		valid = reg & BIT(msgbox_num);
33 		if (0 == valid)
34 			read_down = true;
35 	} while ((!read_down) && (retry_cnts--));
36 
37 	return read_down;
38 }
39 
40 /*****************************************
41 * H2C Msg format :
42 * 0x1DF - 0x1D0
43 *| 31 - 8	| 7-5	 4 - 0	|
44 *| h2c_msg	|Class_ID CMD_ID	|
45 *
46 * Extend 0x1FF - 0x1F0
47 *|31 - 0	  |
48 *|ext_msg|
49 ******************************************/
FillH2CCmd_88E(struct adapter * adapt,u8 ElementID,u32 CmdLen,u8 * pCmdBuffer)50 static s32 FillH2CCmd_88E(struct adapter *adapt, u8 ElementID, u32 CmdLen, u8 *pCmdBuffer)
51 {
52 	u8 bcmd_down = false;
53 	s32 retry_cnts = 100;
54 	u8 h2c_box_num;
55 	u32 msgbox_addr;
56 	u32 msgbox_ex_addr;
57 	struct hal_data_8188e *haldata = &adapt->haldata;
58 	u8 cmd_idx, ext_cmd_len;
59 	u32 h2c_cmd = 0;
60 	u32 h2c_cmd_ex = 0;
61 
62 	if (!adapt->bFWReady)
63 		return _FAIL;
64 
65 	if (!pCmdBuffer || CmdLen > RTL88E_MAX_CMD_LEN || adapt->bSurpriseRemoved)
66 		return _FAIL;
67 
68 	/* pay attention to if  race condition happened in  H2C cmd setting. */
69 	do {
70 		h2c_box_num = haldata->LastHMEBoxNum;
71 
72 		if (!_is_fw_read_cmd_down(adapt, h2c_box_num))
73 			return _FAIL;
74 
75 		*(u8 *)(&h2c_cmd) = ElementID;
76 
77 		if (CmdLen <= 3) {
78 			memcpy((u8 *)(&h2c_cmd) + 1, pCmdBuffer, CmdLen);
79 		} else {
80 			memcpy((u8 *)(&h2c_cmd) + 1, pCmdBuffer, 3);
81 			ext_cmd_len = CmdLen - 3;
82 			memcpy((u8 *)(&h2c_cmd_ex), pCmdBuffer + 3, ext_cmd_len);
83 
84 			/* Write Ext command */
85 			msgbox_ex_addr = REG_HMEBOX_EXT_0 + (h2c_box_num * RTL88E_EX_MESSAGE_BOX_SIZE);
86 			for (cmd_idx = 0; cmd_idx < ext_cmd_len; cmd_idx++) {
87 				rtw_write8(adapt, msgbox_ex_addr + cmd_idx, *((u8 *)(&h2c_cmd_ex) + cmd_idx));
88 			}
89 		}
90 		/*  Write command */
91 		msgbox_addr = REG_HMEBOX_0 + (h2c_box_num * RTL88E_MESSAGE_BOX_SIZE);
92 		for (cmd_idx = 0; cmd_idx < RTL88E_MESSAGE_BOX_SIZE; cmd_idx++) {
93 			rtw_write8(adapt, msgbox_addr + cmd_idx, *((u8 *)(&h2c_cmd) + cmd_idx));
94 		}
95 		bcmd_down = true;
96 
97 		haldata->LastHMEBoxNum = (h2c_box_num + 1) % RTL88E_MAX_H2C_BOX_NUMS;
98 
99 	} while ((!bcmd_down) && (retry_cnts--));
100 
101 	return _SUCCESS;
102 }
103 
rtl8188e_set_raid_cmd(struct adapter * adapt,u32 mask)104 u8 rtl8188e_set_raid_cmd(struct adapter *adapt, u32 mask)
105 {
106 	u8 buf[3];
107 	u8 res = _SUCCESS;
108 	struct hal_data_8188e *haldata = &adapt->haldata;
109 
110 	if (haldata->fw_ractrl) {
111 		__le32 lmask;
112 
113 		memset(buf, 0, 3);
114 		lmask = cpu_to_le32(mask);
115 		memcpy(buf, &lmask, 3);
116 
117 		FillH2CCmd_88E(adapt, H2C_DM_MACID_CFG, 3, buf);
118 	} else {
119 		res = _FAIL;
120 	}
121 
122 	return res;
123 }
124 
125 /* bitmap[0:27] = tx_rate_bitmap */
126 /* bitmap[28:31]= Rate Adaptive id */
127 /* arg[0:4] = macid */
128 /* arg[5] = Short GI */
rtl8188e_Add_RateATid(struct adapter * pAdapter,u32 bitmap,u8 arg,u8 rssi_level)129 void rtl8188e_Add_RateATid(struct adapter *pAdapter, u32 bitmap, u8 arg, u8 rssi_level)
130 {
131 	struct hal_data_8188e *haldata = &pAdapter->haldata;
132 
133 	u8 macid, raid, short_gi_rate = false;
134 
135 	macid = arg & 0x1f;
136 
137 	raid = (bitmap >> 28) & 0x0f;
138 	bitmap &= 0x0fffffff;
139 
140 	if (rssi_level != DM_RATR_STA_INIT)
141 		bitmap = ODM_Get_Rate_Bitmap(&haldata->odmpriv, macid, bitmap, rssi_level);
142 
143 	bitmap |= ((raid << 28) & 0xf0000000);
144 
145 	short_gi_rate = (arg & BIT(5)) ? true : false;
146 
147 	raid = (bitmap >> 28) & 0x0f;
148 
149 	bitmap &= 0x0fffffff;
150 
151 	ODM_RA_UpdateRateInfo_8188E(&haldata->odmpriv, macid, raid, bitmap, short_gi_rate);
152 }
153 
rtl8188e_set_FwPwrMode_cmd(struct adapter * adapt,u8 Mode)154 void rtl8188e_set_FwPwrMode_cmd(struct adapter *adapt, u8 Mode)
155 {
156 	struct setpwrmode_parm H2CSetPwrMode;
157 	struct pwrctrl_priv *pwrpriv = &adapt->pwrctrlpriv;
158 	u8 RLBM = 0; /*  0:Min, 1:Max, 2:User define */
159 
160 	switch (Mode) {
161 	case PS_MODE_ACTIVE:
162 		H2CSetPwrMode.Mode = 0;
163 		break;
164 	case PS_MODE_MIN:
165 		H2CSetPwrMode.Mode = 1;
166 		break;
167 	case PS_MODE_MAX:
168 		RLBM = 1;
169 		H2CSetPwrMode.Mode = 1;
170 		break;
171 	case PS_MODE_DTIM:
172 		RLBM = 2;
173 		H2CSetPwrMode.Mode = 1;
174 		break;
175 	case PS_MODE_UAPSD_WMM:
176 		H2CSetPwrMode.Mode = 2;
177 		break;
178 	default:
179 		H2CSetPwrMode.Mode = 0;
180 		break;
181 	}
182 
183 	H2CSetPwrMode.SmartPS_RLBM = (((pwrpriv->smart_ps << 4) & 0xf0) | (RLBM & 0x0f));
184 
185 	H2CSetPwrMode.AwakeInterval = 1;
186 
187 	H2CSetPwrMode.bAllQueueUAPSD = adapt->registrypriv.uapsd_enable;
188 
189 	if (Mode > 0)
190 		H2CSetPwrMode.PwrState = 0x00;/*  AllON(0x0C), RFON(0x04), RFOFF(0x00) */
191 	else
192 		H2CSetPwrMode.PwrState = 0x0C;/*  AllON(0x0C), RFON(0x04), RFOFF(0x00) */
193 
194 	FillH2CCmd_88E(adapt, H2C_PS_PWR_MODE, sizeof(H2CSetPwrMode), (u8 *)&H2CSetPwrMode);
195 
196 }
197 
rtl8188e_set_FwMediaStatus_cmd(struct adapter * adapt,__le16 mstatus_rpt)198 void rtl8188e_set_FwMediaStatus_cmd(struct adapter *adapt, __le16 mstatus_rpt)
199 {
200 	u16 mst_rpt = le16_to_cpu(mstatus_rpt);
201 
202 	FillH2CCmd_88E(adapt, H2C_COM_MEDIA_STATUS_RPT, sizeof(mst_rpt), (u8 *)&mst_rpt);
203 }
204 
ConstructBeacon(struct adapter * adapt,u8 * pframe,u32 * pLength)205 static void ConstructBeacon(struct adapter *adapt, u8 *pframe, u32 *pLength)
206 {
207 	struct ieee80211_hdr *pwlanhdr;
208 	__le16 *fctrl;
209 	u32 rate_len, pktlen;
210 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
211 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
212 	struct wlan_bssid_ex		*cur_network = &pmlmeinfo->network;
213 
214 	pwlanhdr = (struct ieee80211_hdr *)pframe;
215 
216 	fctrl = &pwlanhdr->frame_control;
217 	*(fctrl) = 0;
218 
219 	eth_broadcast_addr(pwlanhdr->addr1);
220 	memcpy(pwlanhdr->addr2, myid(&adapt->eeprompriv), ETH_ALEN);
221 	memcpy(pwlanhdr->addr3, get_my_bssid(cur_network), ETH_ALEN);
222 
223 	SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
224 	SetFrameSubType(pframe, WIFI_BEACON);
225 
226 	pframe += sizeof(struct ieee80211_hdr_3addr);
227 	pktlen = sizeof(struct ieee80211_hdr_3addr);
228 
229 	/* timestamp will be inserted by hardware */
230 	pframe += 8;
231 	pktlen += 8;
232 
233 	/*  beacon interval: 2 bytes */
234 	memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
235 
236 	pframe += 2;
237 	pktlen += 2;
238 
239 	/*  capability info: 2 bytes */
240 	memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
241 
242 	pframe += 2;
243 	pktlen += 2;
244 
245 	if ((pmlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) {
246 		pktlen += cur_network->IELength - sizeof(struct ndis_802_11_fixed_ie);
247 		memcpy(pframe, cur_network->IEs + sizeof(struct ndis_802_11_fixed_ie), pktlen);
248 
249 		goto _ConstructBeacon;
250 	}
251 
252 	/* below for ad-hoc mode */
253 
254 	/*  SSID */
255 	pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pktlen);
256 
257 	/*  supported rates... */
258 	rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
259 	pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, ((rate_len > 8) ? 8 : rate_len), cur_network->SupportedRates, &pktlen);
260 
261 	/*  DS parameter set */
262 	pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&cur_network->Configuration.DSConfig, &pktlen);
263 
264 	if ((pmlmeinfo->state & 0x03) == WIFI_FW_ADHOC_STATE) {
265 		u32 ATIMWindow;
266 		/*  IBSS Parameter Set... */
267 		ATIMWindow = 0;
268 		pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pktlen);
269 	}
270 
271 	/* todo: ERP IE */
272 
273 	/*  EXTERNDED SUPPORTED RATE */
274 	if (rate_len > 8)
275 		pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pktlen);
276 
277 	/* todo:HT for adhoc */
278 
279 _ConstructBeacon:
280 
281 	if ((pktlen + TXDESC_SIZE) > 512)
282 		return;
283 
284 	*pLength = pktlen;
285 }
286 
ConstructPSPoll(struct adapter * adapt,u8 * pframe,u32 * pLength)287 static void ConstructPSPoll(struct adapter *adapt, u8 *pframe, u32 *pLength)
288 {
289 	struct ieee80211_hdr *pwlanhdr;
290 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
291 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
292 	__le16 *fctrl;
293 
294 	pwlanhdr = (struct ieee80211_hdr *)pframe;
295 
296 	/*  Frame control. */
297 	fctrl = &pwlanhdr->frame_control;
298 	*(fctrl) = 0;
299 	SetPwrMgt(fctrl);
300 	SetFrameSubType(pframe, WIFI_PSPOLL);
301 
302 	/*  AID. */
303 	SetDuration(pframe, (pmlmeinfo->aid | 0xc000));
304 
305 	/*  BSSID. */
306 	memcpy(pwlanhdr->addr1, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
307 
308 	/*  TA. */
309 	memcpy(pwlanhdr->addr2, myid(&adapt->eeprompriv), ETH_ALEN);
310 
311 	*pLength = 16;
312 }
313 
ConstructNullFunctionData(struct adapter * adapt,u8 * pframe,u32 * pLength,u8 * StaAddr,u8 bQoS,u8 AC,u8 bEosp,u8 bForcePowerSave)314 static void ConstructNullFunctionData(struct adapter *adapt, u8 *pframe,
315 	u32 *pLength,
316 	u8 *StaAddr,
317 	u8 bQoS,
318 	u8 AC,
319 	u8 bEosp,
320 	u8 bForcePowerSave)
321 {
322 	struct ieee80211_hdr *pwlanhdr;
323 	__le16 *fctrl;
324 	u32 pktlen;
325 	struct mlme_priv *pmlmepriv = &adapt->mlmepriv;
326 	struct wlan_network		*cur_network = &pmlmepriv->cur_network;
327 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
328 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
329 
330 	pwlanhdr = (struct ieee80211_hdr *)pframe;
331 
332 	fctrl = &pwlanhdr->frame_control;
333 	*(fctrl) = 0;
334 	if (bForcePowerSave)
335 		SetPwrMgt(fctrl);
336 
337 	switch (cur_network->network.InfrastructureMode) {
338 	case Ndis802_11Infrastructure:
339 		SetToDs(fctrl);
340 		memcpy(pwlanhdr->addr1, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
341 		memcpy(pwlanhdr->addr2, myid(&adapt->eeprompriv), ETH_ALEN);
342 		memcpy(pwlanhdr->addr3, StaAddr, ETH_ALEN);
343 		break;
344 	case Ndis802_11APMode:
345 		SetFrDs(fctrl);
346 		memcpy(pwlanhdr->addr1, StaAddr, ETH_ALEN);
347 		memcpy(pwlanhdr->addr2, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
348 		memcpy(pwlanhdr->addr3, myid(&adapt->eeprompriv), ETH_ALEN);
349 		break;
350 	case Ndis802_11IBSS:
351 	default:
352 		memcpy(pwlanhdr->addr1, StaAddr, ETH_ALEN);
353 		memcpy(pwlanhdr->addr2, myid(&adapt->eeprompriv), ETH_ALEN);
354 		memcpy(pwlanhdr->addr3, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
355 		break;
356 	}
357 
358 	SetSeqNum(pwlanhdr, 0);
359 
360 	if (bQoS) {
361 		struct ieee80211_qos_hdr *pwlanqoshdr;
362 
363 		SetFrameSubType(pframe, WIFI_QOS_DATA_NULL);
364 
365 		pwlanqoshdr = (struct ieee80211_qos_hdr *)pframe;
366 		SetPriority(&pwlanqoshdr->qos_ctrl, AC);
367 		SetEOSP(&pwlanqoshdr->qos_ctrl, bEosp);
368 
369 		pktlen = sizeof(struct ieee80211_qos_hdr);
370 	} else {
371 		SetFrameSubType(pframe, WIFI_DATA_NULL);
372 
373 		pktlen = sizeof(struct ieee80211_qos_hdr);
374 	}
375 
376 	*pLength = pktlen;
377 }
378 
ConstructProbeRsp(struct adapter * adapt,u8 * pframe,u32 * pLength,u8 * StaAddr,bool bHideSSID)379 static void ConstructProbeRsp(struct adapter *adapt, u8 *pframe, u32 *pLength, u8 *StaAddr, bool bHideSSID)
380 {
381 	struct ieee80211_hdr *pwlanhdr;
382 	__le16 *fctrl;
383 	u8 *mac, *bssid;
384 	u32 pktlen;
385 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
386 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
387 	struct wlan_bssid_ex	*cur_network = &pmlmeinfo->network;
388 
389 	pwlanhdr = (struct ieee80211_hdr *)pframe;
390 
391 	mac = myid(&adapt->eeprompriv);
392 	bssid = cur_network->MacAddress;
393 
394 	fctrl = &pwlanhdr->frame_control;
395 	*(fctrl) = 0;
396 	memcpy(pwlanhdr->addr1, StaAddr, ETH_ALEN);
397 	memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
398 	memcpy(pwlanhdr->addr3, bssid, ETH_ALEN);
399 
400 	SetSeqNum(pwlanhdr, 0);
401 	SetFrameSubType(fctrl, WIFI_PROBERSP);
402 
403 	pktlen = sizeof(struct ieee80211_hdr_3addr);
404 	pframe += pktlen;
405 
406 	if (cur_network->IELength > MAX_IE_SZ)
407 		return;
408 
409 	memcpy(pframe, cur_network->IEs, cur_network->IELength);
410 	pframe += cur_network->IELength;
411 	pktlen += cur_network->IELength;
412 
413 	*pLength = pktlen;
414 }
415 
416 /*  To check if reserved page content is destroyed by beacon because beacon is too large. */
417 /*  2010.06.23. Added by tynli. */
CheckFwRsvdPageContent(struct adapter * Adapter)418 void CheckFwRsvdPageContent(struct adapter *Adapter)
419 {
420 }
421 
422 /*  */
423 /*  Description: Fill the reserved packets that FW will use to RSVD page. */
424 /*			Now we just send 4 types packet to rsvd page. */
425 /*			(1)Beacon, (2)Ps-poll, (3)Null data, (4)ProbeRsp. */
426 /*	Input: */
427 /*	    bDLFinished - false: At the first time we will send all the packets as a large packet to Hw, */
428 /*						so we need to set the packet length to total length. */
429 /*			      true: At the second time, we should send the first packet (default:beacon) */
430 /*						to Hw again and set the length in descriptor to the real beacon length. */
431 /*  2009.10.15 by tynli. */
SetFwRsvdPagePkt(struct adapter * adapt,bool bDLFinished)432 static void SetFwRsvdPagePkt(struct adapter *adapt, bool bDLFinished)
433 {
434 	struct xmit_frame	*pmgntframe;
435 	struct pkt_attrib	*pattrib;
436 	struct xmit_priv *pxmitpriv;
437 	struct mlme_ext_priv *pmlmeext;
438 	struct mlme_ext_info	*pmlmeinfo;
439 	u32 BeaconLength = 0, ProbeRspLength = 0, PSPollLength;
440 	u32 NullDataLength, QosNullLength;
441 	u8 *ReservedPagePacket;
442 	u8 PageNum, PageNeed, TxDescLen;
443 	u16 BufIndex;
444 	u32 TotalPacketLen;
445 	struct rsvdpage_loc RsvdPageLoc;
446 
447 	ReservedPagePacket = kzalloc(1000, GFP_KERNEL);
448 	if (!ReservedPagePacket)
449 		return;
450 
451 	pxmitpriv = &adapt->xmitpriv;
452 	pmlmeext = &adapt->mlmeextpriv;
453 	pmlmeinfo = &pmlmeext->mlmext_info;
454 
455 	TxDescLen = TXDESC_SIZE;
456 	PageNum = 0;
457 
458 	/* 3 (1) beacon * 2 pages */
459 	BufIndex = TXDESC_OFFSET;
460 	ConstructBeacon(adapt, &ReservedPagePacket[BufIndex], &BeaconLength);
461 
462 	/*  When we count the first page size, we need to reserve description size for the RSVD */
463 	/*  packet, it will be filled in front of the packet in TXPKTBUF. */
464 	PageNeed = (u8)PageNum_128(TxDescLen + BeaconLength);
465 	/*  To reserved 2 pages for beacon buffer. 2010.06.24. */
466 	if (PageNeed == 1)
467 		PageNeed += 1;
468 	PageNum += PageNeed;
469 
470 	BufIndex += PageNeed * 128;
471 
472 	/* 3 (2) ps-poll *1 page */
473 	RsvdPageLoc.LocPsPoll = PageNum;
474 	ConstructPSPoll(adapt, &ReservedPagePacket[BufIndex], &PSPollLength);
475 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], PSPollLength, true, false);
476 
477 	PageNeed = (u8)PageNum_128(TxDescLen + PSPollLength);
478 	PageNum += PageNeed;
479 
480 	BufIndex += PageNeed * 128;
481 
482 	/* 3 (3) null data * 1 page */
483 	RsvdPageLoc.LocNullData = PageNum;
484 	ConstructNullFunctionData(adapt, &ReservedPagePacket[BufIndex], &NullDataLength, get_my_bssid(&pmlmeinfo->network), false, 0, 0, false);
485 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], NullDataLength, false, false);
486 
487 	PageNeed = (u8)PageNum_128(TxDescLen + NullDataLength);
488 	PageNum += PageNeed;
489 
490 	BufIndex += PageNeed * 128;
491 
492 	/* 3 (4) probe response * 1page */
493 	RsvdPageLoc.LocProbeRsp = PageNum;
494 	ConstructProbeRsp(adapt, &ReservedPagePacket[BufIndex], &ProbeRspLength, get_my_bssid(&pmlmeinfo->network), false);
495 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], ProbeRspLength, false, false);
496 
497 	PageNeed = (u8)PageNum_128(TxDescLen + ProbeRspLength);
498 	PageNum += PageNeed;
499 
500 	BufIndex += PageNeed * 128;
501 
502 	/* 3 (5) Qos null data */
503 	RsvdPageLoc.LocQosNull = PageNum;
504 	ConstructNullFunctionData(adapt, &ReservedPagePacket[BufIndex],
505 				  &QosNullLength, get_my_bssid(&pmlmeinfo->network), true, 0, 0, false);
506 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], QosNullLength, false, false);
507 
508 	PageNeed = (u8)PageNum_128(TxDescLen + QosNullLength);
509 	PageNum += PageNeed;
510 
511 	TotalPacketLen = BufIndex + QosNullLength;
512 	pmgntframe = alloc_mgtxmitframe(pxmitpriv);
513 	if (!pmgntframe)
514 		goto exit;
515 
516 	/*  update attribute */
517 	pattrib = &pmgntframe->attrib;
518 	update_mgntframe_attrib(adapt, pattrib);
519 	pattrib->qsel = 0x10;
520 	pattrib->last_txcmdsz = TotalPacketLen - TXDESC_OFFSET;
521 	pattrib->pktlen = pattrib->last_txcmdsz;
522 	memcpy(pmgntframe->buf_addr, ReservedPagePacket, TotalPacketLen);
523 
524 	rtl8188eu_mgnt_xmit(adapt, pmgntframe);
525 
526 	FillH2CCmd_88E(adapt, H2C_COM_RSVD_PAGE, sizeof(RsvdPageLoc), (u8 *)&RsvdPageLoc);
527 
528 exit:
529 	kfree(ReservedPagePacket);
530 }
531 
rtl8188e_set_FwJoinBssReport_cmd(struct adapter * adapt,u8 mstatus)532 void rtl8188e_set_FwJoinBssReport_cmd(struct adapter *adapt, u8 mstatus)
533 {
534 	struct hal_data_8188e *haldata = &adapt->haldata;
535 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
536 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
537 	bool	bSendBeacon = false;
538 	bool	bcn_valid = false;
539 	u8 DLBcnCount = 0;
540 	u32 poll = 0;
541 	u8 reg;
542 	int res;
543 
544 	if (mstatus == 1) {
545 		/*  We should set AID, correct TSF, HW seq enable before set JoinBssReport to Fw in 88/92C. */
546 		/*  Suggested by filen. Added by tynli. */
547 		rtw_write16(adapt, REG_BCN_PSR_RPT, (0xC000 | pmlmeinfo->aid));
548 		/*  Do not set TSF again here or vWiFi beacon DMA INT will not work. */
549 
550 		/* Set REG_CR bit 8. DMA beacon by SW. */
551 		haldata->RegCR_1 |= BIT(0);
552 		rtw_write8(adapt,  REG_CR + 1, haldata->RegCR_1);
553 
554 		/*  Disable Hw protection for a time which revserd for Hw sending beacon. */
555 		/*  Fix download reserved page packet fail that access collision with the protection time. */
556 		/*  2010.05.11. Added by tynli. */
557 		res = rtw_read8(adapt, REG_BCN_CTRL, &reg);
558 		if (res)
559 			return;
560 
561 		rtw_write8(adapt, REG_BCN_CTRL, reg & (~BIT(3)));
562 
563 		res = rtw_read8(adapt, REG_BCN_CTRL, &reg);
564 		if (res)
565 			return;
566 
567 		rtw_write8(adapt, REG_BCN_CTRL, reg | BIT(4));
568 
569 		if (haldata->RegFwHwTxQCtrl & BIT(6))
570 			bSendBeacon = true;
571 
572 		/*  Set FWHW_TXQ_CTRL 0x422[6]=0 to tell Hw the packet is not a real beacon frame. */
573 		rtw_write8(adapt, REG_FWHW_TXQ_CTRL + 2, (haldata->RegFwHwTxQCtrl & (~BIT(6))));
574 		haldata->RegFwHwTxQCtrl &= (~BIT(6));
575 
576 		clear_beacon_valid_bit(adapt);
577 		DLBcnCount = 0;
578 		poll = 0;
579 		do {
580 			/*  download rsvd page. */
581 			SetFwRsvdPagePkt(adapt, false);
582 			DLBcnCount++;
583 			do {
584 				yield();
585 				/* mdelay(10); */
586 				/*  check rsvd page download OK. */
587 				bcn_valid = get_beacon_valid_bit(adapt);
588 				poll++;
589 			} while (!bcn_valid && (poll % 10) != 0 && !adapt->bSurpriseRemoved && !adapt->bDriverStopped);
590 		} while (!bcn_valid && DLBcnCount <= 100 && !adapt->bSurpriseRemoved && !adapt->bDriverStopped);
591 
592 		/*  */
593 		/*  We just can send the reserved page twice during the time that Tx thread is stopped (e.g. pnpsetpower) */
594 		/*  because we need to free the Tx BCN Desc which is used by the first reserved page packet. */
595 		/*  At run time, we cannot get the Tx Desc until it is released in TxHandleInterrupt() so we will return */
596 		/*  the beacon TCB in the following code. 2011.11.23. by tynli. */
597 		/*  */
598 
599 		/*  Enable Bcn */
600 		res = rtw_read8(adapt, REG_BCN_CTRL, &reg);
601 		if (res)
602 			return;
603 
604 		rtw_write8(adapt, REG_BCN_CTRL, reg | BIT(3));
605 
606 		res = rtw_read8(adapt, REG_BCN_CTRL, &reg);
607 		if (res)
608 			return;
609 
610 		rtw_write8(adapt, REG_BCN_CTRL, reg & (~BIT(4)));
611 
612 		/*  To make sure that if there exists an adapter which would like to send beacon. */
613 		/*  If exists, the origianl value of 0x422[6] will be 1, we should check this to */
614 		/*  prevent from setting 0x422[6] to 0 after download reserved page, or it will cause */
615 		/*  the beacon cannot be sent by HW. */
616 		/*  2010.06.23. Added by tynli. */
617 		if (bSendBeacon) {
618 			rtw_write8(adapt, REG_FWHW_TXQ_CTRL + 2, (haldata->RegFwHwTxQCtrl | BIT(6)));
619 			haldata->RegFwHwTxQCtrl |= BIT(6);
620 		}
621 
622 		/*  Update RSVD page location H2C to Fw. */
623 		if (bcn_valid)
624 			clear_beacon_valid_bit(adapt);
625 
626 		/*  Do not enable HW DMA BCN or it will cause Pcie interface hang by timing issue. 2011.11.24. by tynli. */
627 		/*  Clear CR[8] or beacon packet will not be send to TxBuf anymore. */
628 		haldata->RegCR_1 &= (~BIT(0));
629 		rtw_write8(adapt,  REG_CR + 1, haldata->RegCR_1);
630 	}
631 
632 }
633 
rtl8188e_set_p2p_ps_offload_cmd(struct adapter * adapt,u8 p2p_ps_state)634 void rtl8188e_set_p2p_ps_offload_cmd(struct adapter *adapt, u8 p2p_ps_state)
635 {
636 	struct hal_data_8188e *haldata = &adapt->haldata;
637 	struct wifidirect_info	*pwdinfo = &adapt->wdinfo;
638 	struct P2P_PS_Offload_t	*p2p_ps_offload = &haldata->p2p_ps_offload;
639 	u8 i;
640 
641 	switch (p2p_ps_state) {
642 	case P2P_PS_DISABLE:
643 		memset(p2p_ps_offload, 0, 1);
644 		break;
645 	case P2P_PS_ENABLE:
646 		/*  update CTWindow value. */
647 		if (pwdinfo->ctwindow > 0) {
648 			p2p_ps_offload->CTWindow_En = 1;
649 			rtw_write8(adapt, REG_P2P_CTWIN, pwdinfo->ctwindow);
650 		}
651 
652 		/*  hw only support 2 set of NoA */
653 		for (i = 0; i < pwdinfo->noa_num; i++) {
654 			/*  To control the register setting for which NOA */
655 			rtw_write8(adapt, REG_NOA_DESC_SEL, (i << 4));
656 			if (i == 0)
657 				p2p_ps_offload->NoA0_En = 1;
658 			else
659 				p2p_ps_offload->NoA1_En = 1;
660 
661 			/*  config P2P NoA Descriptor Register */
662 			rtw_write32(adapt, REG_NOA_DESC_DURATION, pwdinfo->noa_duration[i]);
663 			rtw_write32(adapt, REG_NOA_DESC_INTERVAL, pwdinfo->noa_interval[i]);
664 			rtw_write32(adapt, REG_NOA_DESC_START, pwdinfo->noa_start_time[i]);
665 			rtw_write8(adapt, REG_NOA_DESC_COUNT, pwdinfo->noa_count[i]);
666 		}
667 
668 		if ((pwdinfo->opp_ps == 1) || (pwdinfo->noa_num > 0)) {
669 			/*  rst p2p circuit */
670 			rtw_write8(adapt, REG_DUAL_TSF_RST, BIT(4));
671 
672 			p2p_ps_offload->Offload_En = 1;
673 
674 			if (pwdinfo->role == P2P_ROLE_GO) {
675 				p2p_ps_offload->role = 1;
676 				p2p_ps_offload->AllStaSleep = 0;
677 			} else {
678 				p2p_ps_offload->role = 0;
679 			}
680 
681 			p2p_ps_offload->discovery = 0;
682 		}
683 		break;
684 	case P2P_PS_SCAN:
685 		p2p_ps_offload->discovery = 1;
686 		break;
687 	case P2P_PS_SCAN_DONE:
688 		p2p_ps_offload->discovery = 0;
689 		pwdinfo->p2p_ps_state = P2P_PS_ENABLE;
690 		break;
691 	default:
692 		break;
693 	}
694 
695 	FillH2CCmd_88E(adapt, H2C_PS_P2P_OFFLOAD, 1, (u8 *)p2p_ps_offload);
696 }
697