18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2013  Realtek Corporation.*/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "../wifi.h"
58c2ecf20Sopenharmony_ci#include "../pci.h"
68c2ecf20Sopenharmony_ci#include "../base.h"
78c2ecf20Sopenharmony_ci#include "../core.h"
88c2ecf20Sopenharmony_ci#include "../efuse.h"
98c2ecf20Sopenharmony_ci#include "reg.h"
108c2ecf20Sopenharmony_ci#include "def.h"
118c2ecf20Sopenharmony_ci#include "fw.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic void _rtl88e_enable_fw_download(struct ieee80211_hw *hw, bool enable)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
168c2ecf20Sopenharmony_ci	u8 tmp;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	if (enable) {
198c2ecf20Sopenharmony_ci		tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN + 1);
208c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN + 1, tmp | 0x04);
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci		tmp = rtl_read_byte(rtlpriv, REG_MCUFWDL);
238c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MCUFWDL, tmp | 0x01);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci		tmp = rtl_read_byte(rtlpriv, REG_MCUFWDL + 2);
268c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MCUFWDL + 2, tmp & 0xf7);
278c2ecf20Sopenharmony_ci	} else {
288c2ecf20Sopenharmony_ci		tmp = rtl_read_byte(rtlpriv, REG_MCUFWDL);
298c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MCUFWDL, tmp & 0xfe);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MCUFWDL + 1, 0x00);
328c2ecf20Sopenharmony_ci	}
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic void _rtl88e_write_fw(struct ieee80211_hw *hw,
368c2ecf20Sopenharmony_ci			     enum version_8188e version, u8 *buffer, u32 size)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
398c2ecf20Sopenharmony_ci	u8 *bufferptr = (u8 *)buffer;
408c2ecf20Sopenharmony_ci	u32 pagenums, remainsize;
418c2ecf20Sopenharmony_ci	u32 page, offset;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "FW size is %d bytes,\n", size);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	rtl_fill_dummy(bufferptr, &size);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	pagenums = size / FW_8192C_PAGE_SIZE;
488c2ecf20Sopenharmony_ci	remainsize = size % FW_8192C_PAGE_SIZE;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (pagenums > 8)
518c2ecf20Sopenharmony_ci		pr_err("Page numbers should not greater then 8\n");
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	for (page = 0; page < pagenums; page++) {
548c2ecf20Sopenharmony_ci		offset = page * FW_8192C_PAGE_SIZE;
558c2ecf20Sopenharmony_ci		rtl_fw_page_write(hw, page, (bufferptr + offset),
568c2ecf20Sopenharmony_ci				  FW_8192C_PAGE_SIZE);
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (remainsize) {
608c2ecf20Sopenharmony_ci		offset = pagenums * FW_8192C_PAGE_SIZE;
618c2ecf20Sopenharmony_ci		page = pagenums;
628c2ecf20Sopenharmony_ci		rtl_fw_page_write(hw, page, (bufferptr + offset), remainsize);
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int _rtl88e_fw_free_to_go(struct ieee80211_hw *hw)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
698c2ecf20Sopenharmony_ci	int err = -EIO;
708c2ecf20Sopenharmony_ci	u32 counter = 0;
718c2ecf20Sopenharmony_ci	u32 value32;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	do {
748c2ecf20Sopenharmony_ci		value32 = rtl_read_dword(rtlpriv, REG_MCUFWDL);
758c2ecf20Sopenharmony_ci	} while ((counter++ < FW_8192C_POLLING_TIMEOUT_COUNT) &&
768c2ecf20Sopenharmony_ci		 (!(value32 & FWDL_CHKSUM_RPT)));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (counter >= FW_8192C_POLLING_TIMEOUT_COUNT) {
798c2ecf20Sopenharmony_ci		pr_err("chksum report fail! REG_MCUFWDL:0x%08x .\n",
808c2ecf20Sopenharmony_ci		       value32);
818c2ecf20Sopenharmony_ci		goto exit;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci	value32 = rtl_read_dword(rtlpriv, REG_MCUFWDL);
848c2ecf20Sopenharmony_ci	value32 |= MCUFWDL_RDY;
858c2ecf20Sopenharmony_ci	value32 &= ~WINTINI_RDY;
868c2ecf20Sopenharmony_ci	rtl_write_dword(rtlpriv, REG_MCUFWDL, value32);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	rtl88e_firmware_selfreset(hw);
898c2ecf20Sopenharmony_ci	counter = 0;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	do {
928c2ecf20Sopenharmony_ci		value32 = rtl_read_dword(rtlpriv, REG_MCUFWDL);
938c2ecf20Sopenharmony_ci		if (value32 & WINTINI_RDY)
948c2ecf20Sopenharmony_ci			return 0;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		udelay(FW_8192C_POLLING_DELAY);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	} while (counter++ < FW_8192C_POLLING_TIMEOUT_COUNT);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	pr_err("Polling FW ready fail!! REG_MCUFWDL:0x%08x .\n",
1018c2ecf20Sopenharmony_ci	       value32);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciexit:
1048c2ecf20Sopenharmony_ci	return err;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciint rtl88e_download_fw(struct ieee80211_hw *hw,
1088c2ecf20Sopenharmony_ci		       bool buse_wake_on_wlan_fw)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1118c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1128c2ecf20Sopenharmony_ci	struct rtlwifi_firmware_header *pfwheader;
1138c2ecf20Sopenharmony_ci	u8 *pfwdata;
1148c2ecf20Sopenharmony_ci	u32 fwsize;
1158c2ecf20Sopenharmony_ci	int err;
1168c2ecf20Sopenharmony_ci	enum version_8188e version = rtlhal->version;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (!rtlhal->pfirmware)
1198c2ecf20Sopenharmony_ci		return 1;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	pfwheader = (struct rtlwifi_firmware_header *)rtlhal->pfirmware;
1228c2ecf20Sopenharmony_ci	rtlhal->fw_version = le16_to_cpu(pfwheader->version);
1238c2ecf20Sopenharmony_ci	rtlhal->fw_subversion = pfwheader->subversion;
1248c2ecf20Sopenharmony_ci	pfwdata = rtlhal->pfirmware;
1258c2ecf20Sopenharmony_ci	fwsize = rtlhal->fwsize;
1268c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_FW, DBG_DMESG,
1278c2ecf20Sopenharmony_ci		"normal Firmware SIZE %d\n", fwsize);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	if (IS_FW_HEADER_EXIST(pfwheader)) {
1308c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_FW, DBG_DMESG,
1318c2ecf20Sopenharmony_ci			"Firmware Version(%d), Signature(%#x), Size(%d)\n",
1328c2ecf20Sopenharmony_ci			pfwheader->version, pfwheader->signature,
1338c2ecf20Sopenharmony_ci			(int)sizeof(struct rtlwifi_firmware_header));
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci		pfwdata = pfwdata + sizeof(struct rtlwifi_firmware_header);
1368c2ecf20Sopenharmony_ci		fwsize = fwsize - sizeof(struct rtlwifi_firmware_header);
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (rtl_read_byte(rtlpriv, REG_MCUFWDL) & BIT(7)) {
1408c2ecf20Sopenharmony_ci		rtl_write_byte(rtlpriv, REG_MCUFWDL, 0);
1418c2ecf20Sopenharmony_ci		rtl88e_firmware_selfreset(hw);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci	_rtl88e_enable_fw_download(hw, true);
1448c2ecf20Sopenharmony_ci	_rtl88e_write_fw(hw, version, pfwdata, fwsize);
1458c2ecf20Sopenharmony_ci	_rtl88e_enable_fw_download(hw, false);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	err = _rtl88e_fw_free_to_go(hw);
1488c2ecf20Sopenharmony_ci	if (err)
1498c2ecf20Sopenharmony_ci		pr_err("Firmware is not ready to run!\n");
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	return 0;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic bool _rtl88e_check_fw_read_last_h2c(struct ieee80211_hw *hw, u8 boxnum)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1578c2ecf20Sopenharmony_ci	u8 val_hmetfr;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	val_hmetfr = rtl_read_byte(rtlpriv, REG_HMETFR);
1608c2ecf20Sopenharmony_ci	if (((val_hmetfr >> boxnum) & BIT(0)) == 0)
1618c2ecf20Sopenharmony_ci		return true;
1628c2ecf20Sopenharmony_ci	return false;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic void _rtl88e_fill_h2c_command(struct ieee80211_hw *hw,
1668c2ecf20Sopenharmony_ci				     u8 element_id, u32 cmd_len,
1678c2ecf20Sopenharmony_ci				     u8 *cmd_b)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1708c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1718c2ecf20Sopenharmony_ci	u8 boxnum;
1728c2ecf20Sopenharmony_ci	u16 box_reg = 0, box_extreg = 0;
1738c2ecf20Sopenharmony_ci	u8 u1b_tmp;
1748c2ecf20Sopenharmony_ci	bool isfw_read = false;
1758c2ecf20Sopenharmony_ci	u8 buf_index = 0;
1768c2ecf20Sopenharmony_ci	bool write_sucess = false;
1778c2ecf20Sopenharmony_ci	u8 wait_h2c_limmit = 100;
1788c2ecf20Sopenharmony_ci	u8 wait_writeh2c_limit = 100;
1798c2ecf20Sopenharmony_ci	u8 boxcontent[4], boxextcontent[4];
1808c2ecf20Sopenharmony_ci	u32 h2c_waitcounter = 0;
1818c2ecf20Sopenharmony_ci	unsigned long flag;
1828c2ecf20Sopenharmony_ci	u8 idx;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "come in\n");
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	while (true) {
1878c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rtlpriv->locks.h2c_lock, flag);
1888c2ecf20Sopenharmony_ci		if (rtlhal->h2c_setinprogress) {
1898c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD,
1908c2ecf20Sopenharmony_ci				"H2C set in progress! Wait to set..element_id(%d).\n",
1918c2ecf20Sopenharmony_ci				element_id);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci			while (rtlhal->h2c_setinprogress) {
1948c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&rtlpriv->locks.h2c_lock,
1958c2ecf20Sopenharmony_ci						       flag);
1968c2ecf20Sopenharmony_ci				h2c_waitcounter++;
1978c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD,
1988c2ecf20Sopenharmony_ci					"Wait 100 us (%d times)...\n",
1998c2ecf20Sopenharmony_ci					h2c_waitcounter);
2008c2ecf20Sopenharmony_ci				udelay(100);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci				if (h2c_waitcounter > 1000)
2038c2ecf20Sopenharmony_ci					return;
2048c2ecf20Sopenharmony_ci				spin_lock_irqsave(&rtlpriv->locks.h2c_lock,
2058c2ecf20Sopenharmony_ci						  flag);
2068c2ecf20Sopenharmony_ci			}
2078c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&rtlpriv->locks.h2c_lock, flag);
2088c2ecf20Sopenharmony_ci		} else {
2098c2ecf20Sopenharmony_ci			rtlhal->h2c_setinprogress = true;
2108c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&rtlpriv->locks.h2c_lock, flag);
2118c2ecf20Sopenharmony_ci			break;
2128c2ecf20Sopenharmony_ci		}
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	while (!write_sucess) {
2168c2ecf20Sopenharmony_ci		wait_writeh2c_limit--;
2178c2ecf20Sopenharmony_ci		if (wait_writeh2c_limit == 0) {
2188c2ecf20Sopenharmony_ci			pr_err("Write H2C fail because no trigger for FW INT!\n");
2198c2ecf20Sopenharmony_ci			break;
2208c2ecf20Sopenharmony_ci		}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		boxnum = rtlhal->last_hmeboxnum;
2238c2ecf20Sopenharmony_ci		switch (boxnum) {
2248c2ecf20Sopenharmony_ci		case 0:
2258c2ecf20Sopenharmony_ci			box_reg = REG_HMEBOX_0;
2268c2ecf20Sopenharmony_ci			box_extreg = REG_HMEBOX_EXT_0;
2278c2ecf20Sopenharmony_ci			break;
2288c2ecf20Sopenharmony_ci		case 1:
2298c2ecf20Sopenharmony_ci			box_reg = REG_HMEBOX_1;
2308c2ecf20Sopenharmony_ci			box_extreg = REG_HMEBOX_EXT_1;
2318c2ecf20Sopenharmony_ci			break;
2328c2ecf20Sopenharmony_ci		case 2:
2338c2ecf20Sopenharmony_ci			box_reg = REG_HMEBOX_2;
2348c2ecf20Sopenharmony_ci			box_extreg = REG_HMEBOX_EXT_2;
2358c2ecf20Sopenharmony_ci			break;
2368c2ecf20Sopenharmony_ci		case 3:
2378c2ecf20Sopenharmony_ci			box_reg = REG_HMEBOX_3;
2388c2ecf20Sopenharmony_ci			box_extreg = REG_HMEBOX_EXT_3;
2398c2ecf20Sopenharmony_ci			break;
2408c2ecf20Sopenharmony_ci		default:
2418c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
2428c2ecf20Sopenharmony_ci				"switch case %#x not processed\n", boxnum);
2438c2ecf20Sopenharmony_ci			break;
2448c2ecf20Sopenharmony_ci		}
2458c2ecf20Sopenharmony_ci		isfw_read = _rtl88e_check_fw_read_last_h2c(hw, boxnum);
2468c2ecf20Sopenharmony_ci		while (!isfw_read) {
2478c2ecf20Sopenharmony_ci			wait_h2c_limmit--;
2488c2ecf20Sopenharmony_ci			if (wait_h2c_limmit == 0) {
2498c2ecf20Sopenharmony_ci				rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD,
2508c2ecf20Sopenharmony_ci					"Waiting too long for FW read clear HMEBox(%d)!\n",
2518c2ecf20Sopenharmony_ci					boxnum);
2528c2ecf20Sopenharmony_ci				break;
2538c2ecf20Sopenharmony_ci			}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci			udelay(10);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci			isfw_read = _rtl88e_check_fw_read_last_h2c(hw, boxnum);
2588c2ecf20Sopenharmony_ci			u1b_tmp = rtl_read_byte(rtlpriv, 0x130);
2598c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD,
2608c2ecf20Sopenharmony_ci				"Waiting for FW read clear HMEBox(%d)!!! 0x130 = %2x\n",
2618c2ecf20Sopenharmony_ci				boxnum, u1b_tmp);
2628c2ecf20Sopenharmony_ci		}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		if (!isfw_read) {
2658c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD,
2668c2ecf20Sopenharmony_ci				"Write H2C register BOX[%d] fail!!!!! Fw do not read.\n",
2678c2ecf20Sopenharmony_ci				boxnum);
2688c2ecf20Sopenharmony_ci			break;
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		memset(boxcontent, 0, sizeof(boxcontent));
2728c2ecf20Sopenharmony_ci		memset(boxextcontent, 0, sizeof(boxextcontent));
2738c2ecf20Sopenharmony_ci		boxcontent[0] = element_id;
2748c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD,
2758c2ecf20Sopenharmony_ci			"Write element_id box_reg(%4x) = %2x\n",
2768c2ecf20Sopenharmony_ci			box_reg, element_id);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci		switch (cmd_len) {
2798c2ecf20Sopenharmony_ci		case 1:
2808c2ecf20Sopenharmony_ci		case 2:
2818c2ecf20Sopenharmony_ci		case 3:
2828c2ecf20Sopenharmony_ci			/*boxcontent[0] &= ~(BIT(7));*/
2838c2ecf20Sopenharmony_ci			memcpy((u8 *)(boxcontent) + 1,
2848c2ecf20Sopenharmony_ci			       cmd_b + buf_index, cmd_len);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci			for (idx = 0; idx < 4; idx++) {
2878c2ecf20Sopenharmony_ci				rtl_write_byte(rtlpriv, box_reg + idx,
2888c2ecf20Sopenharmony_ci					       boxcontent[idx]);
2898c2ecf20Sopenharmony_ci			}
2908c2ecf20Sopenharmony_ci			break;
2918c2ecf20Sopenharmony_ci		case 4:
2928c2ecf20Sopenharmony_ci		case 5:
2938c2ecf20Sopenharmony_ci		case 6:
2948c2ecf20Sopenharmony_ci		case 7:
2958c2ecf20Sopenharmony_ci			/*boxcontent[0] |= (BIT(7));*/
2968c2ecf20Sopenharmony_ci			memcpy((u8 *)(boxextcontent),
2978c2ecf20Sopenharmony_ci			       cmd_b + buf_index+3, cmd_len-3);
2988c2ecf20Sopenharmony_ci			memcpy((u8 *)(boxcontent) + 1,
2998c2ecf20Sopenharmony_ci			       cmd_b + buf_index, 3);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci			for (idx = 0; idx < 2; idx++) {
3028c2ecf20Sopenharmony_ci				rtl_write_byte(rtlpriv, box_extreg + idx,
3038c2ecf20Sopenharmony_ci					       boxextcontent[idx]);
3048c2ecf20Sopenharmony_ci			}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci			for (idx = 0; idx < 4; idx++) {
3078c2ecf20Sopenharmony_ci				rtl_write_byte(rtlpriv, box_reg + idx,
3088c2ecf20Sopenharmony_ci					       boxcontent[idx]);
3098c2ecf20Sopenharmony_ci			}
3108c2ecf20Sopenharmony_ci			break;
3118c2ecf20Sopenharmony_ci		default:
3128c2ecf20Sopenharmony_ci			rtl_dbg(rtlpriv, COMP_ERR, DBG_LOUD,
3138c2ecf20Sopenharmony_ci				"switch case %#x not processed\n", cmd_len);
3148c2ecf20Sopenharmony_ci			break;
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		write_sucess = true;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci		rtlhal->last_hmeboxnum = boxnum + 1;
3208c2ecf20Sopenharmony_ci		if (rtlhal->last_hmeboxnum == 4)
3218c2ecf20Sopenharmony_ci			rtlhal->last_hmeboxnum = 0;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD,
3248c2ecf20Sopenharmony_ci			"pHalData->last_hmeboxnum  = %d\n",
3258c2ecf20Sopenharmony_ci			rtlhal->last_hmeboxnum);
3268c2ecf20Sopenharmony_ci	}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtlpriv->locks.h2c_lock, flag);
3298c2ecf20Sopenharmony_ci	rtlhal->h2c_setinprogress = false;
3308c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtlpriv->locks.h2c_lock, flag);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_CMD, DBG_LOUD, "go out\n");
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_civoid rtl88e_fill_h2c_cmd(struct ieee80211_hw *hw,
3368c2ecf20Sopenharmony_ci			 u8 element_id, u32 cmd_len, u8 *cmdbuffer)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3398c2ecf20Sopenharmony_ci	u32 tmp_cmdbuf[2];
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (!rtlhal->fw_ready) {
3428c2ecf20Sopenharmony_ci		WARN_ONCE(true,
3438c2ecf20Sopenharmony_ci			  "rtl8188ee: error H2C cmd because of Fw download fail!!!\n");
3448c2ecf20Sopenharmony_ci		return;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	memset(tmp_cmdbuf, 0, 8);
3488c2ecf20Sopenharmony_ci	memcpy(tmp_cmdbuf, cmdbuffer, cmd_len);
3498c2ecf20Sopenharmony_ci	_rtl88e_fill_h2c_command(hw, element_id, cmd_len, (u8 *)&tmp_cmdbuf);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return;
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_civoid rtl88e_firmware_selfreset(struct ieee80211_hw *hw)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	u8 u1b_tmp;
3578c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	u1b_tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN+1);
3608c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN+1, (u1b_tmp & (~BIT(2))));
3618c2ecf20Sopenharmony_ci	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN+1, (u1b_tmp | BIT(2)));
3628c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
3638c2ecf20Sopenharmony_ci		"8051Reset88E(): 8051 reset success\n");
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_civoid rtl88e_set_fw_pwrmode_cmd(struct ieee80211_hw *hw, u8 mode)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
3708c2ecf20Sopenharmony_ci	u8 u1_h2c_set_pwrmode[H2C_88E_PWEMODE_LENGTH] = { 0 };
3718c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
3728c2ecf20Sopenharmony_ci	u8 rlbm, power_state = 0;
3738c2ecf20Sopenharmony_ci	rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD, "FW LPS mode = %d\n", mode);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	set_h2ccmd_pwrmode_parm_mode(u1_h2c_set_pwrmode, ((mode) ? 1 : 0));
3768c2ecf20Sopenharmony_ci	rlbm = 0;/*YJ, temp, 120316. FW now not support RLBM=2.*/
3778c2ecf20Sopenharmony_ci	set_h2ccmd_pwrmode_parm_rlbm(u1_h2c_set_pwrmode, rlbm);
3788c2ecf20Sopenharmony_ci	set_h2ccmd_pwrmode_parm_smart_ps(u1_h2c_set_pwrmode,
3798c2ecf20Sopenharmony_ci		(rtlpriv->mac80211.p2p) ? ppsc->smart_ps : 1);
3808c2ecf20Sopenharmony_ci	set_h2ccmd_pwrmode_parm_awake_interval(u1_h2c_set_pwrmode,
3818c2ecf20Sopenharmony_ci		ppsc->reg_max_lps_awakeintvl);
3828c2ecf20Sopenharmony_ci	set_h2ccmd_pwrmode_parm_all_queue_uapsd(u1_h2c_set_pwrmode, 0);
3838c2ecf20Sopenharmony_ci	if (mode == FW_PS_ACTIVE_MODE)
3848c2ecf20Sopenharmony_ci		power_state |= FW_PWR_STATE_ACTIVE;
3858c2ecf20Sopenharmony_ci	else
3868c2ecf20Sopenharmony_ci		power_state |= FW_PWR_STATE_RF_OFF;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	set_h2ccmd_pwrmode_parm_pwr_state(u1_h2c_set_pwrmode, power_state);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_DMESG,
3918c2ecf20Sopenharmony_ci		      "rtl92c_set_fw_pwrmode(): u1_h2c_set_pwrmode\n",
3928c2ecf20Sopenharmony_ci		      u1_h2c_set_pwrmode, H2C_88E_PWEMODE_LENGTH);
3938c2ecf20Sopenharmony_ci	rtl88e_fill_h2c_cmd(hw, H2C_88E_SETPWRMODE,
3948c2ecf20Sopenharmony_ci			    H2C_88E_PWEMODE_LENGTH, u1_h2c_set_pwrmode);
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_civoid rtl88e_set_fw_joinbss_report_cmd(struct ieee80211_hw *hw, u8 mstatus)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	u8 u1_joinbssrpt_parm[1] = { 0 };
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	SET_H2CCMD_JOINBSSRPT_PARM_OPMODE(u1_joinbssrpt_parm, mstatus);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	rtl88e_fill_h2c_cmd(hw, H2C_88E_JOINBSSRPT, 1, u1_joinbssrpt_parm);
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_civoid rtl88e_set_fw_ap_off_load_cmd(struct ieee80211_hw *hw,
4078c2ecf20Sopenharmony_ci				   u8 ap_offload_enable)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
4108c2ecf20Sopenharmony_ci	u8 u1_apoffload_parm[H2C_88E_AP_OFFLOAD_LENGTH] = { 0 };
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	SET_H2CCMD_AP_OFFLOAD_ON(u1_apoffload_parm, ap_offload_enable);
4138c2ecf20Sopenharmony_ci	SET_H2CCMD_AP_OFFLOAD_HIDDEN(u1_apoffload_parm, mac->hiddenssid);
4148c2ecf20Sopenharmony_ci	SET_H2CCMD_AP_OFFLOAD_DENYANY(u1_apoffload_parm, 0);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	rtl88e_fill_h2c_cmd(hw, H2C_88E_AP_OFFLOAD,
4178c2ecf20Sopenharmony_ci			    H2C_88E_AP_OFFLOAD_LENGTH, u1_apoffload_parm);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci#define BEACON_PG		0 /* ->1 */
4228c2ecf20Sopenharmony_ci#define PSPOLL_PG		2
4238c2ecf20Sopenharmony_ci#define NULL_PG			3
4248c2ecf20Sopenharmony_ci#define PROBERSP_PG		4 /* ->5 */
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci#define TOTAL_RESERVED_PKT_LEN	768
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic u8 reserved_page_packet[TOTAL_RESERVED_PKT_LEN] = {
4298c2ecf20Sopenharmony_ci	/* page 0 beacon */
4308c2ecf20Sopenharmony_ci	0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
4318c2ecf20Sopenharmony_ci	0xFF, 0xFF, 0x00, 0xE0, 0x4C, 0x76, 0x00, 0x42,
4328c2ecf20Sopenharmony_ci	0x00, 0x40, 0x10, 0x10, 0x00, 0x03, 0x50, 0x08,
4338c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4348c2ecf20Sopenharmony_ci	0x64, 0x00, 0x00, 0x04, 0x00, 0x0C, 0x6C, 0x69,
4358c2ecf20Sopenharmony_ci	0x6E, 0x6B, 0x73, 0x79, 0x73, 0x5F, 0x77, 0x6C,
4368c2ecf20Sopenharmony_ci	0x61, 0x6E, 0x01, 0x04, 0x82, 0x84, 0x8B, 0x96,
4378c2ecf20Sopenharmony_ci	0x03, 0x01, 0x01, 0x06, 0x02, 0x00, 0x00, 0x2A,
4388c2ecf20Sopenharmony_ci	0x01, 0x00, 0x32, 0x08, 0x24, 0x30, 0x48, 0x6C,
4398c2ecf20Sopenharmony_ci	0x0C, 0x12, 0x18, 0x60, 0x2D, 0x1A, 0x6C, 0x18,
4408c2ecf20Sopenharmony_ci	0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4418c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4428c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4438c2ecf20Sopenharmony_ci	0x3D, 0x00, 0xDD, 0x06, 0x00, 0xE0, 0x4C, 0x02,
4448c2ecf20Sopenharmony_ci	0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4458c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	/* page 1 beacon */
4488c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4498c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4508c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4518c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4528c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4538c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4548c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4558c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4568c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4578c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4588c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4598c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4608c2ecf20Sopenharmony_ci	0x10, 0x00, 0x20, 0x8C, 0x00, 0x12, 0x10, 0x00,
4618c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4628c2ecf20Sopenharmony_ci	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4638c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	/* page 2  ps-poll */
4668c2ecf20Sopenharmony_ci	0xA4, 0x10, 0x01, 0xC0, 0x00, 0x40, 0x10, 0x10,
4678c2ecf20Sopenharmony_ci	0x00, 0x03, 0x00, 0xE0, 0x4C, 0x76, 0x00, 0x42,
4688c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4698c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4708c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4718c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4728c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4738c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4748c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4758c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4768c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4778c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4788c2ecf20Sopenharmony_ci	0x18, 0x00, 0x20, 0x8C, 0x00, 0x12, 0x00, 0x00,
4798c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
4808c2ecf20Sopenharmony_ci	0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4818c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/* page 3  null */
4848c2ecf20Sopenharmony_ci	0x48, 0x01, 0x00, 0x00, 0x00, 0x40, 0x10, 0x10,
4858c2ecf20Sopenharmony_ci	0x00, 0x03, 0x00, 0xE0, 0x4C, 0x76, 0x00, 0x42,
4868c2ecf20Sopenharmony_ci	0x00, 0x40, 0x10, 0x10, 0x00, 0x03, 0x00, 0x00,
4878c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4888c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4898c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4908c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4918c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4928c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4938c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4948c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4958c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4968c2ecf20Sopenharmony_ci	0x72, 0x00, 0x20, 0x8C, 0x00, 0x12, 0x00, 0x00,
4978c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
4988c2ecf20Sopenharmony_ci	0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4998c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* page 4  probe_resp */
5028c2ecf20Sopenharmony_ci	0x50, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x10,
5038c2ecf20Sopenharmony_ci	0x00, 0x03, 0x00, 0xE0, 0x4C, 0x76, 0x00, 0x42,
5048c2ecf20Sopenharmony_ci	0x00, 0x40, 0x10, 0x10, 0x00, 0x03, 0x00, 0x00,
5058c2ecf20Sopenharmony_ci	0x9E, 0x46, 0x15, 0x32, 0x27, 0xF2, 0x2D, 0x00,
5068c2ecf20Sopenharmony_ci	0x64, 0x00, 0x00, 0x04, 0x00, 0x0C, 0x6C, 0x69,
5078c2ecf20Sopenharmony_ci	0x6E, 0x6B, 0x73, 0x79, 0x73, 0x5F, 0x77, 0x6C,
5088c2ecf20Sopenharmony_ci	0x61, 0x6E, 0x01, 0x04, 0x82, 0x84, 0x8B, 0x96,
5098c2ecf20Sopenharmony_ci	0x03, 0x01, 0x01, 0x06, 0x02, 0x00, 0x00, 0x2A,
5108c2ecf20Sopenharmony_ci	0x01, 0x00, 0x32, 0x08, 0x24, 0x30, 0x48, 0x6C,
5118c2ecf20Sopenharmony_ci	0x0C, 0x12, 0x18, 0x60, 0x2D, 0x1A, 0x6C, 0x18,
5128c2ecf20Sopenharmony_ci	0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5138c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5148c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5158c2ecf20Sopenharmony_ci	0x3D, 0x00, 0xDD, 0x06, 0x00, 0xE0, 0x4C, 0x02,
5168c2ecf20Sopenharmony_ci	0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5178c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	/* page 5  probe_resp */
5208c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5218c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5228c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5238c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5248c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5258c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5268c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5278c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5288c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5298c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5308c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5318c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5328c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5338c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5348c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5358c2ecf20Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5368c2ecf20Sopenharmony_ci};
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_civoid rtl88e_set_fw_rsvdpagepkt(struct ieee80211_hw *hw, bool b_dl_finished)
5398c2ecf20Sopenharmony_ci{
5408c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
5418c2ecf20Sopenharmony_ci	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
5428c2ecf20Sopenharmony_ci	struct sk_buff *skb = NULL;
5438c2ecf20Sopenharmony_ci	u32 totalpacketlen;
5448c2ecf20Sopenharmony_ci	bool rtstatus;
5458c2ecf20Sopenharmony_ci	u8 u1rsvdpageloc[5] = { 0 };
5468c2ecf20Sopenharmony_ci	bool b_dlok = false;
5478c2ecf20Sopenharmony_ci	u8 *beacon;
5488c2ecf20Sopenharmony_ci	u8 *p_pspoll;
5498c2ecf20Sopenharmony_ci	u8 *nullfunc;
5508c2ecf20Sopenharmony_ci	u8 *p_probersp;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/*---------------------------------------------------------
5538c2ecf20Sopenharmony_ci	 *			(1) beacon
5548c2ecf20Sopenharmony_ci	 *---------------------------------------------------------
5558c2ecf20Sopenharmony_ci	 */
5568c2ecf20Sopenharmony_ci	beacon = &reserved_page_packet[BEACON_PG * 128];
5578c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS2(beacon, mac->mac_addr);
5588c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS3(beacon, mac->bssid);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	/*-------------------------------------------------------
5618c2ecf20Sopenharmony_ci	 *			(2) ps-poll
5628c2ecf20Sopenharmony_ci	 *--------------------------------------------------------
5638c2ecf20Sopenharmony_ci	 */
5648c2ecf20Sopenharmony_ci	p_pspoll = &reserved_page_packet[PSPOLL_PG * 128];
5658c2ecf20Sopenharmony_ci	SET_80211_PS_POLL_AID(p_pspoll, (mac->assoc_id | 0xc000));
5668c2ecf20Sopenharmony_ci	SET_80211_PS_POLL_BSSID(p_pspoll, mac->bssid);
5678c2ecf20Sopenharmony_ci	SET_80211_PS_POLL_TA(p_pspoll, mac->mac_addr);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	SET_H2CCMD_RSVDPAGE_LOC_PSPOLL(u1rsvdpageloc, PSPOLL_PG);
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	/*--------------------------------------------------------
5728c2ecf20Sopenharmony_ci	 *			(3) null data
5738c2ecf20Sopenharmony_ci	 *---------------------------------------------------------
5748c2ecf20Sopenharmony_ci	 */
5758c2ecf20Sopenharmony_ci	nullfunc = &reserved_page_packet[NULL_PG * 128];
5768c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS1(nullfunc, mac->bssid);
5778c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS2(nullfunc, mac->mac_addr);
5788c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS3(nullfunc, mac->bssid);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	SET_H2CCMD_RSVDPAGE_LOC_NULL_DATA(u1rsvdpageloc, NULL_PG);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	/*---------------------------------------------------------
5838c2ecf20Sopenharmony_ci	 *			(4) probe response
5848c2ecf20Sopenharmony_ci	 *----------------------------------------------------------
5858c2ecf20Sopenharmony_ci	 */
5868c2ecf20Sopenharmony_ci	p_probersp = &reserved_page_packet[PROBERSP_PG * 128];
5878c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS1(p_probersp, mac->bssid);
5888c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS2(p_probersp, mac->mac_addr);
5898c2ecf20Sopenharmony_ci	SET_80211_HDR_ADDRESS3(p_probersp, mac->bssid);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	SET_H2CCMD_RSVDPAGE_LOC_PROBE_RSP(u1rsvdpageloc, PROBERSP_PG);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	totalpacketlen = TOTAL_RESERVED_PKT_LEN;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_LOUD,
5968c2ecf20Sopenharmony_ci		      "rtl88e_set_fw_rsvdpagepkt(): HW_VAR_SET_TX_CMD: ALL\n",
5978c2ecf20Sopenharmony_ci		      &reserved_page_packet[0], totalpacketlen);
5988c2ecf20Sopenharmony_ci	RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_DMESG,
5998c2ecf20Sopenharmony_ci		      "rtl88e_set_fw_rsvdpagepkt(): HW_VAR_SET_TX_CMD: ALL\n",
6008c2ecf20Sopenharmony_ci		      u1rsvdpageloc, 3);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(totalpacketlen);
6038c2ecf20Sopenharmony_ci	if (!skb)
6048c2ecf20Sopenharmony_ci		return;
6058c2ecf20Sopenharmony_ci	skb_put_data(skb, &reserved_page_packet, totalpacketlen);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	rtstatus = rtl_cmd_send_packet(hw, skb);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	if (rtstatus)
6108c2ecf20Sopenharmony_ci		b_dlok = true;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	if (b_dlok) {
6138c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_POWER, DBG_LOUD,
6148c2ecf20Sopenharmony_ci			"Set RSVD page location to Fw.\n");
6158c2ecf20Sopenharmony_ci		RT_PRINT_DATA(rtlpriv, COMP_CMD, DBG_DMESG,
6168c2ecf20Sopenharmony_ci			      "H2C_RSVDPAGE:\n", u1rsvdpageloc, 3);
6178c2ecf20Sopenharmony_ci		rtl88e_fill_h2c_cmd(hw, H2C_88E_RSVDPAGE,
6188c2ecf20Sopenharmony_ci				    sizeof(u1rsvdpageloc), u1rsvdpageloc);
6198c2ecf20Sopenharmony_ci	} else
6208c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_ERR, DBG_WARNING,
6218c2ecf20Sopenharmony_ci			"Set RSVD page location to Fw FAIL!!!!!!.\n");
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci/*Should check FW support p2p or not.*/
6258c2ecf20Sopenharmony_cistatic void rtl88e_set_p2p_ctw_period_cmd(struct ieee80211_hw *hw, u8 ctwindow)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	u8 u1_ctwindow_period[1] = { ctwindow};
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	rtl88e_fill_h2c_cmd(hw, H2C_88E_P2P_PS_CTW_CMD, 1, u1_ctwindow_period);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_civoid rtl88e_set_p2p_ps_offload_cmd(struct ieee80211_hw *hw, u8 p2p_ps_state)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
6368c2ecf20Sopenharmony_ci	struct rtl_ps_ctl *rtlps = rtl_psc(rtl_priv(hw));
6378c2ecf20Sopenharmony_ci	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
6388c2ecf20Sopenharmony_ci	struct rtl_p2p_ps_info *p2pinfo = &(rtlps->p2p_ps_info);
6398c2ecf20Sopenharmony_ci	struct p2p_ps_offload_t *p2p_ps_offload = &rtlhal->p2p_ps_offload;
6408c2ecf20Sopenharmony_ci	u8	i;
6418c2ecf20Sopenharmony_ci	u16	ctwindow;
6428c2ecf20Sopenharmony_ci	u32	start_time, tsf_low;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	switch (p2p_ps_state) {
6458c2ecf20Sopenharmony_ci	case P2P_PS_DISABLE:
6468c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "P2P_PS_DISABLE\n");
6478c2ecf20Sopenharmony_ci		memset(p2p_ps_offload, 0, sizeof(*p2p_ps_offload));
6488c2ecf20Sopenharmony_ci		break;
6498c2ecf20Sopenharmony_ci	case P2P_PS_ENABLE:
6508c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "P2P_PS_ENABLE\n");
6518c2ecf20Sopenharmony_ci		/* update CTWindow value. */
6528c2ecf20Sopenharmony_ci		if (p2pinfo->ctwindow > 0) {
6538c2ecf20Sopenharmony_ci			p2p_ps_offload->ctwindow_en = 1;
6548c2ecf20Sopenharmony_ci			ctwindow = p2pinfo->ctwindow;
6558c2ecf20Sopenharmony_ci			rtl88e_set_p2p_ctw_period_cmd(hw, ctwindow);
6568c2ecf20Sopenharmony_ci		}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci		/* hw only support 2 set of NoA */
6598c2ecf20Sopenharmony_ci		for (i = 0 ; i < p2pinfo->noa_num; i++) {
6608c2ecf20Sopenharmony_ci			/* To control the register setting for which NOA*/
6618c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, 0x5cf, (i << 4));
6628c2ecf20Sopenharmony_ci			if (i == 0)
6638c2ecf20Sopenharmony_ci				p2p_ps_offload->noa0_en = 1;
6648c2ecf20Sopenharmony_ci			else
6658c2ecf20Sopenharmony_ci				p2p_ps_offload->noa1_en = 1;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci			/* config P2P NoA Descriptor Register */
6688c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, 0x5E0,
6698c2ecf20Sopenharmony_ci					p2pinfo->noa_duration[i]);
6708c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, 0x5E4,
6718c2ecf20Sopenharmony_ci					p2pinfo->noa_interval[i]);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci			/*Get Current TSF value */
6748c2ecf20Sopenharmony_ci			tsf_low = rtl_read_dword(rtlpriv, REG_TSFTR);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci			start_time = p2pinfo->noa_start_time[i];
6778c2ecf20Sopenharmony_ci			if (p2pinfo->noa_count_type[i] != 1) {
6788c2ecf20Sopenharmony_ci				while (start_time <= (tsf_low+(50*1024))) {
6798c2ecf20Sopenharmony_ci					start_time += p2pinfo->noa_interval[i];
6808c2ecf20Sopenharmony_ci					if (p2pinfo->noa_count_type[i] != 255)
6818c2ecf20Sopenharmony_ci						p2pinfo->noa_count_type[i]--;
6828c2ecf20Sopenharmony_ci				}
6838c2ecf20Sopenharmony_ci			}
6848c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, 0x5E8, start_time);
6858c2ecf20Sopenharmony_ci			rtl_write_dword(rtlpriv, 0x5EC,
6868c2ecf20Sopenharmony_ci					p2pinfo->noa_count_type[i]);
6878c2ecf20Sopenharmony_ci		}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci		if ((p2pinfo->opp_ps == 1) || (p2pinfo->noa_num > 0)) {
6908c2ecf20Sopenharmony_ci			/* rst p2p circuit */
6918c2ecf20Sopenharmony_ci			rtl_write_byte(rtlpriv, REG_DUAL_TSF_RST, BIT(4));
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci			p2p_ps_offload->offload_en = 1;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci			if (P2P_ROLE_GO == rtlpriv->mac80211.p2p) {
6968c2ecf20Sopenharmony_ci				p2p_ps_offload->role = 1;
6978c2ecf20Sopenharmony_ci				p2p_ps_offload->allstasleep = -1;
6988c2ecf20Sopenharmony_ci			} else {
6998c2ecf20Sopenharmony_ci				p2p_ps_offload->role = 0;
7008c2ecf20Sopenharmony_ci			}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci			p2p_ps_offload->discovery = 0;
7038c2ecf20Sopenharmony_ci		}
7048c2ecf20Sopenharmony_ci		break;
7058c2ecf20Sopenharmony_ci	case P2P_PS_SCAN:
7068c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "P2P_PS_SCAN\n");
7078c2ecf20Sopenharmony_ci		p2p_ps_offload->discovery = 1;
7088c2ecf20Sopenharmony_ci		break;
7098c2ecf20Sopenharmony_ci	case P2P_PS_SCAN_DONE:
7108c2ecf20Sopenharmony_ci		rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "P2P_PS_SCAN_DONE\n");
7118c2ecf20Sopenharmony_ci		p2p_ps_offload->discovery = 0;
7128c2ecf20Sopenharmony_ci		p2pinfo->p2p_ps_state = P2P_PS_ENABLE;
7138c2ecf20Sopenharmony_ci		break;
7148c2ecf20Sopenharmony_ci	default:
7158c2ecf20Sopenharmony_ci		break;
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	rtl88e_fill_h2c_cmd(hw, H2C_88E_P2P_PS_OFFLOAD, 1,
7198c2ecf20Sopenharmony_ci			    (u8 *)p2p_ps_offload);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci}
722