1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #define _RTL8188E_CMD_C_
8 
9 #include <osdep_service.h>
10 #include <drv_types.h>
11 #include <recv_osdep.h>
12 #include <mlme_osdep.h>
13 #include <rtw_ioctl_set.h>
14 
15 #include <rtl8188e_hal.h>
16 
17 #define RTL88E_MAX_H2C_BOX_NUMS		4
18 #define RTL88E_MAX_CMD_LEN		7
19 #define RTL88E_MESSAGE_BOX_SIZE		4
20 #define RTL88E_EX_MESSAGE_BOX_SIZE	4
21 
_is_fw_read_cmd_down(struct adapter * adapt,u8 msgbox_num)22 static u8 _is_fw_read_cmd_down(struct adapter *adapt, u8 msgbox_num)
23 {
24 	u8 read_down = false;
25 	int retry_cnts = 100;
26 
27 	u8 valid;
28 
29 	do {
30 		valid = usb_read8(adapt, REG_HMETFR) & BIT(msgbox_num);
31 		if (valid == 0)
32 			read_down = true;
33 	} while ((!read_down) && (retry_cnts--));
34 
35 	return read_down;
36 }
37 
38 /*****************************************
39 * H2C Msg format :
40 * 0x1DF - 0x1D0
41 *| 31 - 8	| 7-5	 4 - 0	|
42 *| h2c_msg	|Class_ID CMD_ID	|
43 *
44 * Extend 0x1FF - 0x1F0
45 *|31 - 0	  |
46 *|ext_msg|
47 ******************************************/
FillH2CCmd_88E(struct adapter * adapt,u8 ElementID,u32 CmdLen,u8 * pCmdBuffer)48 static s32 FillH2CCmd_88E(struct adapter *adapt, u8 ElementID, u32 CmdLen, u8 *pCmdBuffer)
49 {
50 	u8 h2c_box_num;
51 	u32 msgbox_addr;
52 	u32 msgbox_ex_addr;
53 	u8 cmd_idx, ext_cmd_len;
54 	u32 h2c_cmd = 0;
55 	u32 h2c_cmd_ex = 0;
56 	s32 ret = _FAIL;
57 
58 	if (!adapt->bFWReady) {
59 		DBG_88E("%s(): return H2C cmd because fw is not ready\n",
60 			__func__);
61 		return ret;
62 	}
63 
64 	if (!pCmdBuffer)
65 		goto exit;
66 	if (CmdLen > RTL88E_MAX_CMD_LEN)
67 		goto exit;
68 	if (adapt->bSurpriseRemoved)
69 		goto exit;
70 
71 	/* pay attention to if  race condition happened in  H2C cmd setting. */
72 	h2c_box_num = adapt->HalData->LastHMEBoxNum;
73 
74 	if (!_is_fw_read_cmd_down(adapt, h2c_box_num)) {
75 		DBG_88E(" fw read cmd failed...\n");
76 		goto exit;
77 	}
78 
79 	*(u8 *)(&h2c_cmd) = ElementID;
80 
81 	if (CmdLen <= 3) {
82 		memcpy((u8 *)(&h2c_cmd) + 1, pCmdBuffer, CmdLen);
83 	} else {
84 		memcpy((u8 *)(&h2c_cmd) + 1, pCmdBuffer, 3);
85 		ext_cmd_len = CmdLen - 3;
86 		memcpy((u8 *)(&h2c_cmd_ex), pCmdBuffer + 3, ext_cmd_len);
87 
88 		/* Write Ext command */
89 		msgbox_ex_addr = REG_HMEBOX_EXT_0 + (h2c_box_num * RTL88E_EX_MESSAGE_BOX_SIZE);
90 		for (cmd_idx = 0; cmd_idx < ext_cmd_len; cmd_idx++)
91 			usb_write8(adapt, msgbox_ex_addr + cmd_idx, *((u8 *)(&h2c_cmd_ex) + cmd_idx));
92 	}
93 	/*  Write command */
94 	msgbox_addr = REG_HMEBOX_0 + (h2c_box_num * RTL88E_MESSAGE_BOX_SIZE);
95 	for (cmd_idx = 0; cmd_idx < RTL88E_MESSAGE_BOX_SIZE; cmd_idx++)
96 		usb_write8(adapt, msgbox_addr + cmd_idx, *((u8 *)(&h2c_cmd) + cmd_idx));
97 
98 	adapt->HalData->LastHMEBoxNum =
99 		(h2c_box_num + 1) % RTL88E_MAX_H2C_BOX_NUMS;
100 
101 	ret = _SUCCESS;
102 
103 exit:
104 	return ret;
105 }
106 
107 /* bitmap[0:27] = tx_rate_bitmap */
108 /* bitmap[28:31]= Rate Adaptive id */
109 /* arg[0:4] = macid */
110 /* arg[5] = Short GI */
rtw_hal_add_ra_tid(struct adapter * pAdapter,u32 bitmap,u8 arg,u8 rssi_level)111 void rtw_hal_add_ra_tid(struct adapter *pAdapter, u32 bitmap, u8 arg, u8 rssi_level)
112 {
113 	struct odm_dm_struct *odmpriv = &pAdapter->HalData->odmpriv;
114 	u8 macid, init_rate, raid, shortGIrate = false;
115 
116 	macid = arg & 0x1f;
117 
118 	raid = (bitmap >> 28) & 0x0f;
119 	bitmap &= 0x0fffffff;
120 
121 	if (rssi_level != DM_RATR_STA_INIT)
122 		bitmap = ODM_Get_Rate_Bitmap(odmpriv, macid, bitmap, rssi_level);
123 
124 	bitmap |= ((raid << 28) & 0xf0000000);
125 
126 	init_rate = get_highest_rate_idx(bitmap & 0x0fffffff) & 0x3f;
127 
128 	shortGIrate = (arg & BIT(5)) ? true : false;
129 
130 	if (shortGIrate)
131 		init_rate |= BIT(6);
132 
133 	raid = (bitmap >> 28) & 0x0f;
134 
135 	bitmap &= 0x0fffffff;
136 
137 	DBG_88E("%s=> mac_id:%d, raid:%d, ra_bitmap=0x%x, shortGIrate=0x%02x\n",
138 		__func__, macid, raid, bitmap, shortGIrate);
139 
140 	ODM_RA_UpdateRateInfo_8188E(odmpriv, macid, raid, bitmap, shortGIrate);
141 }
142 
rtl8188e_set_FwPwrMode_cmd(struct adapter * adapt,u8 Mode)143 void rtl8188e_set_FwPwrMode_cmd(struct adapter *adapt, u8 Mode)
144 {
145 	struct setpwrmode_parm H2CSetPwrMode;
146 	struct pwrctrl_priv *pwrpriv = &adapt->pwrctrlpriv;
147 	u8 RLBM = 0; /*  0:Min, 1:Max, 2:User define */
148 
149 	DBG_88E("%s: Mode=%d SmartPS=%d UAPSD=%d\n", __func__,
150 		Mode, pwrpriv->smart_ps, adapt->registrypriv.uapsd_enable);
151 
152 	switch (Mode) {
153 	case PS_MODE_ACTIVE:
154 		H2CSetPwrMode.Mode = 0;
155 		break;
156 	case PS_MODE_MIN:
157 		H2CSetPwrMode.Mode = 1;
158 		break;
159 	case PS_MODE_MAX:
160 		RLBM = 1;
161 		H2CSetPwrMode.Mode = 1;
162 		break;
163 	case PS_MODE_DTIM:
164 		RLBM = 2;
165 		H2CSetPwrMode.Mode = 1;
166 		break;
167 	case PS_MODE_UAPSD_WMM:
168 		H2CSetPwrMode.Mode = 2;
169 		break;
170 	default:
171 		H2CSetPwrMode.Mode = 0;
172 		break;
173 	}
174 
175 	H2CSetPwrMode.SmartPS_RLBM = (((pwrpriv->smart_ps << 4) & 0xf0) | (RLBM & 0x0f));
176 
177 	H2CSetPwrMode.AwakeInterval = 1;
178 
179 	H2CSetPwrMode.bAllQueueUAPSD = adapt->registrypriv.uapsd_enable;
180 
181 	if (Mode > 0)
182 		H2CSetPwrMode.PwrState = 0x00;/*  AllON(0x0C), RFON(0x04), RFOFF(0x00) */
183 	else
184 		H2CSetPwrMode.PwrState = 0x0C;/*  AllON(0x0C), RFON(0x04), RFOFF(0x00) */
185 
186 	FillH2CCmd_88E(adapt, H2C_PS_PWR_MODE, sizeof(H2CSetPwrMode), (u8 *)&H2CSetPwrMode);
187 }
188 
rtl8188e_set_FwMediaStatus_cmd(struct adapter * adapt,__le16 mstatus_rpt)189 void rtl8188e_set_FwMediaStatus_cmd(struct adapter *adapt, __le16 mstatus_rpt)
190 {
191 	u8 opmode, macid;
192 	u16 mst_rpt = le16_to_cpu(mstatus_rpt);
193 
194 	opmode = (u8)mst_rpt;
195 	macid = (u8)(mst_rpt >> 8);
196 
197 	DBG_88E("### %s: MStatus=%x MACID=%d\n", __func__, opmode, macid);
198 	FillH2CCmd_88E(adapt, H2C_COM_MEDIA_STATUS_RPT, sizeof(mst_rpt), (u8 *)&mst_rpt);
199 }
200 
ConstructBeacon(struct adapter * adapt,u8 * pframe,u32 * pLength)201 static void ConstructBeacon(struct adapter *adapt, u8 *pframe, u32 *pLength)
202 {
203 	struct ieee80211_hdr *pwlanhdr;
204 	__le16 *fctrl;
205 	u32 rate_len, pktlen;
206 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
207 	struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
208 	struct wlan_bssid_ex *cur_network = &pmlmeinfo->network;
209 	u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
210 
211 	pwlanhdr = (struct ieee80211_hdr *)pframe;
212 
213 	fctrl = &pwlanhdr->frame_control;
214 	*(fctrl) = 0;
215 
216 	ether_addr_copy(pwlanhdr->addr1, bc_addr);
217 	ether_addr_copy(pwlanhdr->addr2, myid(&adapt->eeprompriv));
218 	ether_addr_copy(pwlanhdr->addr3, cur_network->MacAddress);
219 
220 	SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
221 	SetFrameSubType(pframe, WIFI_BEACON);
222 
223 	pframe += sizeof(struct ieee80211_hdr_3addr);
224 	pktlen = sizeof(struct ieee80211_hdr_3addr);
225 
226 	/* timestamp will be inserted by hardware */
227 	pframe += 8;
228 	pktlen += 8;
229 
230 	/*  beacon interval: 2 bytes */
231 	memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->ies)), 2);
232 
233 	pframe += 2;
234 	pktlen += 2;
235 
236 	/*  capability info: 2 bytes */
237 	memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->ies)), 2);
238 
239 	pframe += 2;
240 	pktlen += 2;
241 
242 	if ((pmlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) {
243 		pktlen += cur_network->ie_length - sizeof(struct ndis_802_11_fixed_ie);
244 		memcpy(pframe, cur_network->ies + sizeof(struct ndis_802_11_fixed_ie), pktlen);
245 
246 		goto _ConstructBeacon;
247 	}
248 
249 	/* below for ad-hoc mode */
250 
251 	/*  SSID */
252 	pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->ssid.ssid_length, cur_network->ssid.ssid, &pktlen);
253 
254 	/*  supported rates... */
255 	rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
256 	pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, min_t(u32, rate_len, 8), cur_network->SupportedRates, &pktlen);
257 
258 	/*  DS parameter set */
259 	pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&cur_network->Configuration.DSConfig, &pktlen);
260 
261 	if ((pmlmeinfo->state & 0x03) == WIFI_FW_ADHOC_STATE) {
262 		u32 ATIMWindow;
263 		/*  IBSS Parameter Set... */
264 		ATIMWindow = 0;
265 		pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pktlen);
266 	}
267 
268 	/* todo: ERP IE */
269 
270 	/*  EXTERNDED SUPPORTED RATE */
271 	if (rate_len > 8)
272 		pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pktlen);
273 
274 	/* todo:HT for adhoc */
275 
276 _ConstructBeacon:
277 
278 	if ((pktlen + TXDESC_SIZE) > 512) {
279 		DBG_88E("beacon frame too large\n");
280 		return;
281 	}
282 
283 	*pLength = pktlen;
284 }
285 
ConstructPSPoll(struct adapter * adapt,u8 * pframe,u32 * pLength)286 static void ConstructPSPoll(struct adapter *adapt, u8 *pframe, u32 *pLength)
287 {
288 	struct ieee80211_hdr *pwlanhdr;
289 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
290 	struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
291 	__le16 *fctrl;
292 	struct wlan_bssid_ex *pnetwork = &pmlmeinfo->network;
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 	ether_addr_copy(pwlanhdr->addr1, pnetwork->MacAddress);
307 
308 	/*  TA. */
309 	ether_addr_copy(pwlanhdr->addr2, myid(&adapt->eeprompriv));
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 	struct wlan_bssid_ex *pnetwork = &pmlmeinfo->network;
330 
331 	pwlanhdr = (struct ieee80211_hdr *)pframe;
332 
333 	fctrl = &pwlanhdr->frame_control;
334 	*(fctrl) = 0;
335 	if (bForcePowerSave)
336 		SetPwrMgt(fctrl);
337 
338 	switch (cur_network->network.InfrastructureMode) {
339 	case Ndis802_11Infrastructure:
340 		SetToDs(fctrl);
341 		ether_addr_copy(pwlanhdr->addr1, pnetwork->MacAddress);
342 		ether_addr_copy(pwlanhdr->addr2, myid(&adapt->eeprompriv));
343 		ether_addr_copy(pwlanhdr->addr3, StaAddr);
344 		break;
345 	case Ndis802_11APMode:
346 		SetFrDs(fctrl);
347 		ether_addr_copy(pwlanhdr->addr1, StaAddr);
348 		ether_addr_copy(pwlanhdr->addr2, pnetwork->MacAddress);
349 		ether_addr_copy(pwlanhdr->addr3, myid(&adapt->eeprompriv));
350 		break;
351 	case Ndis802_11IBSS:
352 	default:
353 		ether_addr_copy(pwlanhdr->addr1, StaAddr);
354 		ether_addr_copy(pwlanhdr->addr2, myid(&adapt->eeprompriv));
355 		ether_addr_copy(pwlanhdr->addr3, pnetwork->MacAddress);
356 		break;
357 	}
358 
359 	SetSeqNum(pwlanhdr, 0);
360 
361 	if (bQoS) {
362 		struct ieee80211_qos_hdr *pwlanqoshdr;
363 
364 		SetFrameSubType(pframe, WIFI_QOS_DATA_NULL);
365 
366 		pwlanqoshdr = (struct ieee80211_qos_hdr *)pframe;
367 		SetPriority(&pwlanqoshdr->qos_ctrl, AC);
368 		SetEOSP(&pwlanqoshdr->qos_ctrl, bEosp);
369 
370 		pktlen = sizeof(struct ieee80211_qos_hdr);
371 	} else {
372 		SetFrameSubType(pframe, WIFI_DATA_NULL);
373 
374 		pktlen = sizeof(struct ieee80211_hdr_3addr);
375 	}
376 
377 	*pLength = pktlen;
378 }
379 
ConstructProbeRsp(struct adapter * adapt,u8 * pframe,u32 * pLength,u8 * StaAddr,bool bHideSSID)380 static void ConstructProbeRsp(struct adapter *adapt, u8 *pframe, u32 *pLength, u8 *StaAddr, bool bHideSSID)
381 {
382 	struct ieee80211_hdr *pwlanhdr;
383 	__le16 *fctrl;
384 	u8 *mac, *bssid;
385 	u32 pktlen;
386 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
387 	struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
388 	struct wlan_bssid_ex *cur_network = &pmlmeinfo->network;
389 
390 	pwlanhdr = (struct ieee80211_hdr *)pframe;
391 
392 	mac = myid(&adapt->eeprompriv);
393 	bssid = cur_network->MacAddress;
394 
395 	fctrl = &pwlanhdr->frame_control;
396 	*(fctrl) = 0;
397 	ether_addr_copy(pwlanhdr->addr1, StaAddr);
398 	ether_addr_copy(pwlanhdr->addr2, mac);
399 	ether_addr_copy(pwlanhdr->addr3, bssid);
400 
401 	SetSeqNum(pwlanhdr, 0);
402 	SetFrameSubType(fctrl, WIFI_PROBERSP);
403 
404 	pktlen = sizeof(struct ieee80211_hdr_3addr);
405 	pframe += pktlen;
406 
407 	if (cur_network->ie_length > MAX_IE_SZ)
408 		return;
409 
410 	memcpy(pframe, cur_network->ies, cur_network->ie_length);
411 	pframe += cur_network->ie_length;
412 	pktlen += cur_network->ie_length;
413 
414 	*pLength = pktlen;
415 }
416 
417 /*  */
418 /*  Description: Fill the reserved packets that FW will use to RSVD page. */
419 /*			Now we just send 4 types packet to rsvd page. */
420 /*			(1)Beacon, (2)Ps-poll, (3)Null data, (4)ProbeRsp. */
421 /*	Input: */
422 /*	    bDLFinished - false: At the first time we will send all the packets as a large packet to Hw, */
423 /*						so we need to set the packet length to total length. */
424 /*			      true: At the second time, we should send the first packet (default:beacon) */
425 /*						to Hw again and set the length in descriptor to the real beacon length. */
426 /*  2009.10.15 by tynli. */
SetFwRsvdPagePkt(struct adapter * adapt,bool bDLFinished)427 static void SetFwRsvdPagePkt(struct adapter *adapt, bool bDLFinished)
428 {
429 	struct xmit_frame *pmgntframe;
430 	struct pkt_attrib *pattrib;
431 	struct xmit_priv *pxmitpriv;
432 	struct mlme_ext_priv *pmlmeext;
433 	struct mlme_ext_info *pmlmeinfo;
434 	u32 BeaconLength = 0, ProbeRspLength = 0, PSPollLength;
435 	u32 NullDataLength, QosNullLength;
436 	u8 *ReservedPagePacket;
437 	u8 PageNum, PageNeed, TxDescLen;
438 	u16 BufIndex;
439 	u32 TotalPacketLen;
440 	struct rsvdpage_loc RsvdPageLoc;
441 	struct wlan_bssid_ex *pnetwork;
442 
443 	DBG_88E("%s\n", __func__);
444 	ReservedPagePacket = kzalloc(1000, GFP_KERNEL);
445 	if (!ReservedPagePacket) {
446 		DBG_88E("%s: alloc ReservedPagePacket fail!\n", __func__);
447 		return;
448 	}
449 
450 	pxmitpriv = &adapt->xmitpriv;
451 	pmlmeext = &adapt->mlmeextpriv;
452 	pmlmeinfo = &pmlmeext->mlmext_info;
453 	pnetwork = &pmlmeinfo->network;
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 	adapt->HalData->FwRsvdPageStartOffset = PageNum;
470 
471 	BufIndex += PageNeed * 128;
472 
473 	/* 3 (2) ps-poll *1 page */
474 	RsvdPageLoc.LocPsPoll = PageNum;
475 	ConstructPSPoll(adapt, &ReservedPagePacket[BufIndex], &PSPollLength);
476 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], PSPollLength, true, false);
477 
478 	PageNeed = (u8)PageNum_128(TxDescLen + PSPollLength);
479 	PageNum += PageNeed;
480 
481 	BufIndex += PageNeed * 128;
482 
483 	/* 3 (3) null data * 1 page */
484 	RsvdPageLoc.LocNullData = PageNum;
485 	ConstructNullFunctionData(adapt, &ReservedPagePacket[BufIndex], &NullDataLength, pnetwork->MacAddress, false, 0, 0, false);
486 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], NullDataLength, false, false);
487 
488 	PageNeed = (u8)PageNum_128(TxDescLen + NullDataLength);
489 	PageNum += PageNeed;
490 
491 	BufIndex += PageNeed * 128;
492 
493 	/* 3 (4) probe response * 1page */
494 	RsvdPageLoc.LocProbeRsp = PageNum;
495 	ConstructProbeRsp(adapt, &ReservedPagePacket[BufIndex], &ProbeRspLength, pnetwork->MacAddress, false);
496 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], ProbeRspLength, false, false);
497 
498 	PageNeed = (u8)PageNum_128(TxDescLen + ProbeRspLength);
499 	PageNum += PageNeed;
500 
501 	BufIndex += PageNeed * 128;
502 
503 	/* 3 (5) Qos null data */
504 	RsvdPageLoc.LocQosNull = PageNum;
505 	ConstructNullFunctionData(adapt, &ReservedPagePacket[BufIndex],
506 				  &QosNullLength, pnetwork->MacAddress, true, 0, 0, false);
507 	rtl8188e_fill_fake_txdesc(adapt, &ReservedPagePacket[BufIndex - TxDescLen], QosNullLength, false, false);
508 
509 	PageNeed = (u8)PageNum_128(TxDescLen + QosNullLength);
510 	PageNum += PageNeed;
511 
512 	TotalPacketLen = BufIndex + QosNullLength;
513 	pmgntframe = alloc_mgtxmitframe(pxmitpriv);
514 	if (!pmgntframe)
515 		goto exit;
516 
517 	/*  update attribute */
518 	pattrib = &pmgntframe->attrib;
519 	update_mgntframe_attrib(adapt, pattrib);
520 	pattrib->qsel = 0x10;
521 	pattrib->last_txcmdsz = TotalPacketLen - TXDESC_OFFSET;
522 	pattrib->pktlen = pattrib->last_txcmdsz;
523 	memcpy(pmgntframe->buf_addr, ReservedPagePacket, TotalPacketLen);
524 
525 	rtw_hal_mgnt_xmit(adapt, pmgntframe);
526 
527 	DBG_88E("%s: Set RSVD page location to Fw\n", __func__);
528 	FillH2CCmd_88E(adapt, H2C_COM_RSVD_PAGE, sizeof(RsvdPageLoc), (u8 *)&RsvdPageLoc);
529 
530 exit:
531 	kfree(ReservedPagePacket);
532 }
533 
rtl8188e_set_FwJoinBssReport_cmd(struct adapter * adapt,u8 mstatus)534 void rtl8188e_set_FwJoinBssReport_cmd(struct adapter *adapt, u8 mstatus)
535 {
536 	struct hal_data_8188e *haldata = adapt->HalData;
537 	struct mlme_ext_priv *pmlmeext = &adapt->mlmeextpriv;
538 	struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
539 	bool bSendBeacon = false;
540 	bool bcn_valid = false;
541 	u8 DLBcnCount = 0;
542 	u32 poll = 0;
543 
544 	DBG_88E("%s mstatus(%x)\n", __func__, mstatus);
545 
546 	if (mstatus == 1) {
547 		/*  We should set AID, correct TSF, HW seq enable before set JoinBssReport to Fw in 88/92C. */
548 		/*  Suggested by filen. Added by tynli. */
549 		usb_write16(adapt, REG_BCN_PSR_RPT, (0xC000 | pmlmeinfo->aid));
550 		/*  Do not set TSF again here or vWiFi beacon DMA INT will not work. */
551 
552 		/* Set REG_CR bit 8. DMA beacon by SW. */
553 		haldata->RegCR_1 |= BIT(0);
554 		usb_write8(adapt,  REG_CR + 1, haldata->RegCR_1);
555 
556 		/*  Disable Hw protection for a time which revserd for Hw sending beacon. */
557 		/*  Fix download reserved page packet fail that access collision with the protection time. */
558 		/*  2010.05.11. Added by tynli. */
559 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL) & (~BIT(3)));
560 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL) | BIT(4));
561 
562 		if (haldata->RegFwHwTxQCtrl & BIT(6)) {
563 			DBG_88E("HalDownloadRSVDPage(): There is an Adapter is sending beacon.\n");
564 			bSendBeacon = true;
565 		}
566 
567 		/*  Set FWHW_TXQ_CTRL 0x422[6]=0 to tell Hw the packet is not a real beacon frame. */
568 		usb_write8(adapt, REG_FWHW_TXQ_CTRL + 2, (haldata->RegFwHwTxQCtrl & (~BIT(6))));
569 		haldata->RegFwHwTxQCtrl &= (~BIT(6));
570 
571 		/*  Clear beacon valid check bit. */
572 		rtw_hal_set_hwreg(adapt, HW_VAR_BCN_VALID, NULL);
573 		DLBcnCount = 0;
574 		poll = 0;
575 		do {
576 			/*  download rsvd page. */
577 			SetFwRsvdPagePkt(adapt, false);
578 			DLBcnCount++;
579 			do {
580 				yield();
581 				/* mdelay(10); */
582 				/*  check rsvd page download OK. */
583 				rtw_hal_get_hwreg(adapt, HW_VAR_BCN_VALID, (u8 *)(&bcn_valid));
584 				poll++;
585 			} while (!bcn_valid && (poll % 10) != 0 && !adapt->bSurpriseRemoved && !adapt->bDriverStopped);
586 		} while (!bcn_valid && DLBcnCount <= 100 && !adapt->bSurpriseRemoved && !adapt->bDriverStopped);
587 
588 		if (adapt->bSurpriseRemoved || adapt->bDriverStopped)
589 			;
590 		else if (!bcn_valid)
591 			DBG_88E("%s: 1 Download RSVD page failed! DLBcnCount:%u, poll:%u\n", __func__, DLBcnCount, poll);
592 		else
593 			DBG_88E("%s: 1 Download RSVD success! DLBcnCount:%u, poll:%u\n", __func__, DLBcnCount, poll);
594 		/*  */
595 		/*  We just can send the reserved page twice during the time that Tx thread is stopped (e.g. pnpsetpower) */
596 		/*  because we need to free the Tx BCN Desc which is used by the first reserved page packet. */
597 		/*  At run time, we cannot get the Tx Desc until it is released in TxHandleInterrupt() so we will return */
598 		/*  the beacon TCB in the following code. 2011.11.23. by tynli. */
599 		/*  */
600 
601 		/*  Enable Bcn */
602 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL) | BIT(3));
603 		usb_write8(adapt, REG_BCN_CTRL, usb_read8(adapt, REG_BCN_CTRL) & (~BIT(4)));
604 
605 		/*  To make sure that if there exists an adapter which would like to send beacon. */
606 		/*  If exists, the origianl value of 0x422[6] will be 1, we should check this to */
607 		/*  prevent from setting 0x422[6] to 0 after download reserved page, or it will cause */
608 		/*  the beacon cannot be sent by HW. */
609 		/*  2010.06.23. Added by tynli. */
610 		if (bSendBeacon) {
611 			usb_write8(adapt, REG_FWHW_TXQ_CTRL + 2, (haldata->RegFwHwTxQCtrl | BIT(6)));
612 			haldata->RegFwHwTxQCtrl |= BIT(6);
613 		}
614 
615 		/*  Update RSVD page location H2C to Fw. */
616 		if (bcn_valid) {
617 			rtw_hal_set_hwreg(adapt, HW_VAR_BCN_VALID, NULL);
618 			DBG_88E("Set RSVD page location to Fw.\n");
619 		}
620 
621 		/*  Do not enable HW DMA BCN or it will cause Pcie interface hang by timing issue. 2011.11.24. by tynli. */
622 		/*  Clear CR[8] or beacon packet will not be send to TxBuf anymore. */
623 		haldata->RegCR_1 &= (~BIT(0));
624 		usb_write8(adapt,  REG_CR + 1, haldata->RegCR_1);
625 	}
626 }
627