Path: blob/master/ALFA-W1F1/RTL8814AU/hal/hal_halmac.c
1307 views
/******************************************************************************1*2* Copyright(c) 2015 - 2019 Realtek Corporation.3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of version 2 of the GNU General Public License as6* published by the Free Software Foundation.7*8* This program is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for11* more details.12*13*****************************************************************************/14#define _HAL_HALMAC_C_1516#include <drv_types.h> /* PADAPTER, struct dvobj_priv, SDIO_ERR_VAL8 and etc. */17#include <hal_data.h> /* efuse, PHAL_DATA_TYPE and etc. */18#include "hal_halmac.h" /* dvobj_to_halmac() and ect. */1920/*21* HALMAC take return value 0 for fail and 1 for success to replace22* _FALSE/_TRUE after V1_04_0923*/24#define RTW_HALMAC_FAIL 025#define RTW_HALMAC_SUCCESS 12627#define DEFAULT_INDICATOR_TIMELMT 1000 /* ms */28#define MSG_PREFIX "[HALMAC]"2930#define RTW_HALMAC_DLFW_MEM_NO_STOP_TX3132/*33* Driver API for HALMAC operations34*/3536#ifdef CONFIG_SDIO_HCI37#include <rtw_sdio.h>3839static u8 _halmac_mac_reg_page0_chk(const char *func, struct dvobj_priv *dvobj, u32 offset)40{41#if defined(CONFIG_IO_CHECK_IN_ANA_LOW_CLK) && defined(CONFIG_LPS_LCLK)42struct pwrctrl_priv *pwrpriv = &dvobj->pwrctl_priv;43u32 mac_reg_offset = 0;4445if (pwrpriv->pwr_mode == PS_MODE_ACTIVE)46return _TRUE;4748if (pwrpriv->lps_level == LPS_NORMAL)49return _TRUE;5051if (pwrpriv->rpwm >= PS_STATE_S2)52return _TRUE;5354if (offset & (WLAN_IOREG_DEVICE_ID << 13)) { /*WLAN_IOREG_OFFSET*/55mac_reg_offset = offset & HALMAC_WLAN_MAC_REG_MSK;56if (mac_reg_offset < 0x100) {57RTW_ERR(FUNC_ADPT_FMT58"access MAC REG -0x%04x in PS-mode:0x%02x (rpwm:0x%02x, lps_level:0x%02x)\n",59FUNC_ADPT_ARG(dvobj_get_primary_adapter(dvobj)), mac_reg_offset,60pwrpriv->pwr_mode, pwrpriv->rpwm, pwrpriv->lps_level);61rtw_warn_on(1);62return _FALSE;63}64}65#endif66return _TRUE;67}6869static u8 _halmac_sdio_cmd52_read(void *p, u32 offset)70{71struct dvobj_priv *d;72u8 val;73u8 ret;747576d = (struct dvobj_priv *)p;77_halmac_mac_reg_page0_chk(__func__, d, offset);78ret = rtw_sdio_read_cmd52(d, offset, &val, 1);79if (_FAIL == ret) {80RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);81return SDIO_ERR_VAL8;82}8384return val;85}8687static void _halmac_sdio_cmd52_write(void *p, u32 offset, u8 val)88{89struct dvobj_priv *d;90u8 ret;919293d = (struct dvobj_priv *)p;94_halmac_mac_reg_page0_chk(__func__, d, offset);95ret = rtw_sdio_write_cmd52(d, offset, &val, 1);96if (_FAIL == ret)97RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);98}99100static u8 _halmac_sdio_reg_read_8(void *p, u32 offset)101{102struct dvobj_priv *d;103u8 *pbuf;104u8 val;105u8 ret;106107108d = (struct dvobj_priv *)p;109val = SDIO_ERR_VAL8;110_halmac_mac_reg_page0_chk(__func__, d, offset);111pbuf = rtw_zmalloc(1);112if (!pbuf)113return val;114115ret = rtw_sdio_read_cmd53(d, offset, pbuf, 1);116if (ret == _FAIL) {117RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);118goto exit;119}120121val = *pbuf;122123exit:124rtw_mfree(pbuf, 1);125126return val;127}128129static u16 _halmac_sdio_reg_read_16(void *p, u32 offset)130{131struct dvobj_priv *d;132u8 *pbuf;133u16 val;134u8 ret;135136137d = (struct dvobj_priv *)p;138val = SDIO_ERR_VAL16;139_halmac_mac_reg_page0_chk(__func__, d, offset);140pbuf = rtw_zmalloc(2);141if (!pbuf)142return val;143144ret = rtw_sdio_read_cmd53(d, offset, pbuf, 2);145if (ret == _FAIL) {146RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);147goto exit;148}149150val = le16_to_cpu(*(u16 *)pbuf);151152exit:153rtw_mfree(pbuf, 2);154155return val;156}157158static u32 _halmac_sdio_reg_read_32(void *p, u32 offset)159{160struct dvobj_priv *d;161u8 *pbuf;162u32 val;163u8 ret;164165166d = (struct dvobj_priv *)p;167val = SDIO_ERR_VAL32;168_halmac_mac_reg_page0_chk(__func__, d, offset);169pbuf = rtw_zmalloc(4);170if (!pbuf)171return val;172173ret = rtw_sdio_read_cmd53(d, offset, pbuf, 4);174if (ret == _FAIL) {175RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);176goto exit;177}178179val = le32_to_cpu(*(u32 *)pbuf);180181exit:182rtw_mfree(pbuf, 4);183184return val;185}186187static u8 _halmac_sdio_reg_read_n(void *p, u32 offset, u32 size, u8 *data)188{189struct dvobj_priv *d = (struct dvobj_priv *)p;190u8 *pbuf;191u8 ret;192u8 rst = RTW_HALMAC_FAIL;193u32 sdio_read_size;194195196if (!data)197return rst;198199sdio_read_size = RND4(size);200sdio_read_size = rtw_sdio_cmd53_align_size(d, sdio_read_size);201202pbuf = rtw_zmalloc(sdio_read_size);203if (!pbuf)204return rst;205206ret = rtw_sdio_read_cmd53(d, offset, pbuf, sdio_read_size);207if (ret == _FAIL) {208RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);209goto exit;210}211212_rtw_memcpy(data, pbuf, size);213rst = RTW_HALMAC_SUCCESS;214exit:215rtw_mfree(pbuf, sdio_read_size);216217return rst;218}219220static void _halmac_sdio_reg_write_8(void *p, u32 offset, u8 val)221{222struct dvobj_priv *d;223u8 *pbuf;224u8 ret;225226227d = (struct dvobj_priv *)p;228_halmac_mac_reg_page0_chk(__func__, d, offset);229pbuf = rtw_zmalloc(1);230if (!pbuf)231return;232_rtw_memcpy(pbuf, &val, 1);233234ret = rtw_sdio_write_cmd53(d, offset, pbuf, 1);235if (ret == _FAIL)236RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);237238rtw_mfree(pbuf, 1);239}240241static void _halmac_sdio_reg_write_16(void *p, u32 offset, u16 val)242{243struct dvobj_priv *d;244u8 *pbuf;245u8 ret;246247248d = (struct dvobj_priv *)p;249_halmac_mac_reg_page0_chk(__func__, d, offset);250val = cpu_to_le16(val);251pbuf = rtw_zmalloc(2);252if (!pbuf)253return;254_rtw_memcpy(pbuf, &val, 2);255256ret = rtw_sdio_write_cmd53(d, offset, pbuf, 2);257if (ret == _FAIL)258RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);259260rtw_mfree(pbuf, 2);261}262263static void _halmac_sdio_reg_write_32(void *p, u32 offset, u32 val)264{265struct dvobj_priv *d;266u8 *pbuf;267u8 ret;268269270d = (struct dvobj_priv *)p;271_halmac_mac_reg_page0_chk(__func__, d, offset);272val = cpu_to_le32(val);273pbuf = rtw_zmalloc(4);274if (!pbuf)275return;276_rtw_memcpy(pbuf, &val, 4);277278ret = rtw_sdio_write_cmd53(d, offset, pbuf, 4);279if (ret == _FAIL)280RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);281282rtw_mfree(pbuf, 4);283}284285static u8 _halmac_sdio_read_cia(void *p, u32 offset)286{287struct dvobj_priv *d;288u8 data = 0;289u8 ret;290291292d = (struct dvobj_priv *)p;293294ret = rtw_sdio_f0_read(d, offset, &data, 1);295if (ret == _FAIL)296RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);297298return data;299}300301#else /* !CONFIG_SDIO_HCI */302303static u8 _halmac_reg_read_8(void *p, u32 offset)304{305struct dvobj_priv *d;306PADAPTER adapter;307308309d = (struct dvobj_priv *)p;310adapter = dvobj_get_primary_adapter(d);311312return _rtw_read8(adapter, offset);313}314315static u16 _halmac_reg_read_16(void *p, u32 offset)316{317struct dvobj_priv *d;318PADAPTER adapter;319320321d = (struct dvobj_priv *)p;322adapter = dvobj_get_primary_adapter(d);323324return _rtw_read16(adapter, offset);325}326327static u32 _halmac_reg_read_32(void *p, u32 offset)328{329struct dvobj_priv *d;330PADAPTER adapter;331332333d = (struct dvobj_priv *)p;334adapter = dvobj_get_primary_adapter(d);335336return _rtw_read32(adapter, offset);337}338339static void _halmac_reg_write_8(void *p, u32 offset, u8 val)340{341struct dvobj_priv *d;342PADAPTER adapter;343int err;344345346d = (struct dvobj_priv *)p;347adapter = dvobj_get_primary_adapter(d);348349err = _rtw_write8(adapter, offset, val);350if (err == _FAIL)351RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);352}353354static void _halmac_reg_write_16(void *p, u32 offset, u16 val)355{356struct dvobj_priv *d;357PADAPTER adapter;358int err;359360361d = (struct dvobj_priv *)p;362adapter = dvobj_get_primary_adapter(d);363364err = _rtw_write16(adapter, offset, val);365if (err == _FAIL)366RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);367}368369static void _halmac_reg_write_32(void *p, u32 offset, u32 val)370{371struct dvobj_priv *d;372PADAPTER adapter;373int err;374375376d = (struct dvobj_priv *)p;377adapter = dvobj_get_primary_adapter(d);378379err = _rtw_write32(adapter, offset, val);380if (err == _FAIL)381RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__);382}383#endif /* !CONFIG_SDIO_HCI */384385#ifdef DBG_IO386static void _halmac_reg_read_monitor(void *p, u32 addr, u32 len, u32 val387, const char *caller, const u32 line)388{389struct dvobj_priv *d = (struct dvobj_priv *)p;390_adapter *adapter = dvobj_get_primary_adapter(d);391392dbg_rtw_reg_read_monitor(adapter, addr, len, val, caller, line);393}394395static void _halmac_reg_write_monitor(void *p, u32 addr, u32 len, u32 val396, const char *caller, const u32 line)397{398struct dvobj_priv *d = (struct dvobj_priv *)p;399_adapter *adapter = dvobj_get_primary_adapter(d);400401dbg_rtw_reg_write_monitor(adapter, addr, len, val, caller, line);402}403#endif404405static u8 _halmac_mfree(void *p, void *buffer, u32 size)406{407rtw_mfree(buffer, size);408return RTW_HALMAC_SUCCESS;409}410411static void *_halmac_malloc(void *p, u32 size)412{413return rtw_zmalloc(size);414}415416static u8 _halmac_memcpy(void *p, void *dest, void *src, u32 size)417{418_rtw_memcpy(dest, src, size);419return RTW_HALMAC_SUCCESS;420}421422static u8 _halmac_memset(void *p, void *addr, u8 value, u32 size)423{424_rtw_memset(addr, value, size);425return RTW_HALMAC_SUCCESS;426}427428static void _halmac_udelay(void *p, u32 us)429{430/* Most hardware polling wait time < 50us) */431if (us <= 50)432rtw_udelay_os(us);433else if (us <= 1000)434rtw_usleep_os(us);435else436rtw_msleep_os(RTW_DIV_ROUND_UP(us, 1000));437}438439static u8 _halmac_mutex_init(void *p, HALMAC_MUTEX *pMutex)440{441_rtw_mutex_init(pMutex);442return RTW_HALMAC_SUCCESS;443}444445static u8 _halmac_mutex_deinit(void *p, HALMAC_MUTEX *pMutex)446{447_rtw_mutex_free(pMutex);448return RTW_HALMAC_SUCCESS;449}450451static u8 _halmac_mutex_lock(void *p, HALMAC_MUTEX *pMutex)452{453int err;454455err = _enter_critical_mutex(pMutex, NULL);456if (err)457return RTW_HALMAC_FAIL;458459return RTW_HALMAC_SUCCESS;460}461462static u8 _halmac_mutex_unlock(void *p, HALMAC_MUTEX *pMutex)463{464_exit_critical_mutex(pMutex, NULL);465return RTW_HALMAC_SUCCESS;466}467468#ifndef CONFIG_SDIO_HCI469#define DBG_MSG_FILTER470#endif471472#ifdef DBG_MSG_FILTER473static u8 is_msg_allowed(uint drv_lv, u8 msg_lv)474{475switch (drv_lv) {476case _DRV_NONE_:477return _FALSE;478479case _DRV_ALWAYS_:480if (msg_lv > HALMAC_DBG_ALWAYS)481return _FALSE;482break;483case _DRV_ERR_:484if (msg_lv > HALMAC_DBG_ERR)485return _FALSE;486break;487case _DRV_WARNING_:488if (msg_lv > HALMAC_DBG_WARN)489return _FALSE;490break;491case _DRV_INFO_:492if (msg_lv >= HALMAC_DBG_TRACE)493return _FALSE;494break;495}496497return _TRUE;498}499#endif /* DBG_MSG_FILTER */500501static u8 _halmac_msg_print(void *p, u32 msg_type, u8 msg_level, s8 *fmt, ...)502{503#define MSG_LEN 100504va_list args;505u8 str[MSG_LEN] = {0};506#ifdef DBG_MSG_FILTER507uint drv_level = _DRV_NONE_;508#endif509int err;510u8 ret = RTW_HALMAC_SUCCESS;511512513#ifdef DBG_MSG_FILTER514#ifdef CONFIG_RTW_DEBUG515drv_level = rtw_drv_log_level;516#endif517if (is_msg_allowed(drv_level, msg_level) == _FALSE)518return ret;519#endif520521str[0] = '\n';522va_start(args, fmt);523err = vsnprintf(str, MSG_LEN, fmt, args);524va_end(args);525526/* An output error is encountered */527if (err < 0)528return RTW_HALMAC_FAIL;529/* Output may be truncated due to size limit */530if ((err == (MSG_LEN - 1)) && (str[MSG_LEN - 2] != '\n'))531ret = RTW_HALMAC_FAIL;532533if (msg_level == HALMAC_DBG_ALWAYS)534RTW_PRINT(MSG_PREFIX "%s", str);535else if (msg_level <= HALMAC_DBG_ERR)536RTW_ERR(MSG_PREFIX "%s", str);537else if (msg_level <= HALMAC_DBG_WARN)538RTW_WARN(MSG_PREFIX "%s", str);539else540RTW_DBG(MSG_PREFIX "%s", str);541542return ret;543}544545static u8 _halmac_buff_print(void *p, u32 msg_type, u8 msg_level, s8 *buf, u32 size)546{547if (msg_level <= HALMAC_DBG_WARN)548RTW_INFO_DUMP(MSG_PREFIX, buf, size);549else550RTW_DBG_DUMP(MSG_PREFIX, buf, size);551552return RTW_HALMAC_SUCCESS;553}554555556const char *const RTW_HALMAC_FEATURE_NAME[] = {557"HALMAC_FEATURE_CFG_PARA",558"HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE",559"HALMAC_FEATURE_DUMP_LOGICAL_EFUSE",560"HALMAC_FEATURE_DUMP_LOGICAL_EFUSE_MASK",561"HALMAC_FEATURE_UPDATE_PACKET",562"HALMAC_FEATURE_SEND_SCAN_PACKET",563"HALMAC_FEATURE_DROP_SCAN_PACKET",564"HALMAC_FEATURE_UPDATE_DATAPACK",565"HALMAC_FEATURE_RUN_DATAPACK",566"HALMAC_FEATURE_CHANNEL_SWITCH",567"HALMAC_FEATURE_IQK",568"HALMAC_FEATURE_POWER_TRACKING",569"HALMAC_FEATURE_PSD",570"HALMAC_FEATURE_FW_SNDING",571"HALMAC_FEATURE_ALL"572};573574static inline u8 is_valid_id_status(enum halmac_feature_id id, enum halmac_cmd_process_status status)575{576switch (id) {577case HALMAC_FEATURE_CFG_PARA:578RTW_DBG("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);579break;580case HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE:581RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);582if (HALMAC_CMD_PROCESS_DONE != status)583RTW_INFO("%s: id(%d) unspecified status(%d)!\n",584__FUNCTION__, id, status);585break;586case HALMAC_FEATURE_DUMP_LOGICAL_EFUSE:587RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);588if (HALMAC_CMD_PROCESS_DONE != status)589RTW_INFO("%s: id(%d) unspecified status(%d)!\n",590__FUNCTION__, id, status);591break;592case HALMAC_FEATURE_UPDATE_PACKET:593RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);594if (status != HALMAC_CMD_PROCESS_DONE)595RTW_INFO("%s: id(%d) unspecified status(%d)!\n",596__FUNCTION__, id, status);597break;598case HALMAC_FEATURE_UPDATE_DATAPACK:599RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);600break;601case HALMAC_FEATURE_RUN_DATAPACK:602RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);603break;604case HALMAC_FEATURE_CHANNEL_SWITCH:605RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);606if ((status != HALMAC_CMD_PROCESS_DONE) && (status != HALMAC_CMD_PROCESS_RCVD))607RTW_INFO("%s: id(%d) unspecified status(%d)!\n",608__FUNCTION__, id, status);609if (status == HALMAC_CMD_PROCESS_DONE)610return _FALSE;611break;612case HALMAC_FEATURE_IQK:613RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);614break;615case HALMAC_FEATURE_POWER_TRACKING:616RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);617break;618case HALMAC_FEATURE_PSD:619RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);620break;621case HALMAC_FEATURE_FW_SNDING:622RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);623break;624case HALMAC_FEATURE_ALL:625RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]);626break;627default:628RTW_ERR("%s: unknown feature id(%d)\n", __FUNCTION__, id);629return _FALSE;630}631632return _TRUE;633}634635static int init_halmac_event_with_waittime(struct dvobj_priv *d, enum halmac_feature_id id, u8 *buf, u32 size, u32 time)636{637struct submit_ctx *sctx;638639640if (!d->hmpriv.indicator[id].sctx) {641sctx = (struct submit_ctx *)rtw_zmalloc(sizeof(*sctx));642if (!sctx)643return -1;644} else {645RTW_WARN("%s: id(%d) sctx is not NULL!!\n", __FUNCTION__, id);646sctx = d->hmpriv.indicator[id].sctx;647d->hmpriv.indicator[id].sctx = NULL;648}649650rtw_sctx_init(sctx, time);651d->hmpriv.indicator[id].buffer = buf;652d->hmpriv.indicator[id].buf_size = size;653d->hmpriv.indicator[id].ret_size = 0;654d->hmpriv.indicator[id].status = 0;655/* fill sctx at least to sure other variables are all ready! */656d->hmpriv.indicator[id].sctx = sctx;657658return 0;659}660661static inline int init_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id, u8 *buf, u32 size)662{663return init_halmac_event_with_waittime(d, id, buf, size, DEFAULT_INDICATOR_TIMELMT);664}665666static void free_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id)667{668struct submit_ctx *sctx;669670671if (!d->hmpriv.indicator[id].sctx)672return;673674sctx = d->hmpriv.indicator[id].sctx;675d->hmpriv.indicator[id].sctx = NULL;676rtw_mfree((u8 *)sctx, sizeof(*sctx));677}678679static int wait_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id)680{681struct halmac_adapter *mac;682struct halmac_api *api;683struct submit_ctx *sctx;684int ret;685686687sctx = d->hmpriv.indicator[id].sctx;688if (!sctx)689return -1;690691ret = rtw_sctx_wait(sctx, RTW_HALMAC_FEATURE_NAME[id]);692free_halmac_event(d, id);693if (_SUCCESS == ret)694return 0;695696/* timeout! We have to reset halmac state */697RTW_ERR("%s: Wait id(%d, %s) TIMEOUT! Reset HALMAC state!\n",698__FUNCTION__, id, RTW_HALMAC_FEATURE_NAME[id]);699mac = dvobj_to_halmac(d);700api = HALMAC_GET_API(mac);701api->halmac_reset_feature(mac, id);702703return -1;704}705706/*707* Return:708* Always return RTW_HALMAC_SUCCESS, HALMAC don't care the return value.709*/710static u8 _halmac_event_indication(void *p, enum halmac_feature_id feature_id, enum halmac_cmd_process_status process_status, u8 *buf, u32 size)711{712struct dvobj_priv *d;713PADAPTER adapter;714PHAL_DATA_TYPE hal;715struct halmac_indicator *tbl, *indicator;716struct submit_ctx *sctx;717u32 cpsz;718u8 ret;719720721d = (struct dvobj_priv *)p;722adapter = dvobj_get_primary_adapter(d);723hal = GET_HAL_DATA(adapter);724tbl = d->hmpriv.indicator;725726/* Filter(Skip) middle status indication */727ret = is_valid_id_status(feature_id, process_status);728if (_FALSE == ret)729goto exit;730731indicator = &tbl[feature_id];732indicator->status = process_status;733indicator->ret_size = size;734if (!indicator->sctx) {735RTW_WARN("%s: No feature id(%d, %s) waiting!!\n", __FUNCTION__, feature_id, RTW_HALMAC_FEATURE_NAME[feature_id]);736goto exit;737}738sctx = indicator->sctx;739740if (HALMAC_CMD_PROCESS_ERROR == process_status) {741RTW_ERR("%s: Something wrong id(%d, %s)!!\n", __FUNCTION__, feature_id, RTW_HALMAC_FEATURE_NAME[feature_id]);742rtw_sctx_done_err(&sctx, RTW_SCTX_DONE_UNKNOWN);743goto exit;744}745746if (size > indicator->buf_size) {747RTW_WARN("%s: id(%d, %s) buffer is not enough(%d<%d), data will be truncated!\n",748__FUNCTION__, feature_id, RTW_HALMAC_FEATURE_NAME[feature_id], indicator->buf_size, size);749cpsz = indicator->buf_size;750} else {751cpsz = size;752}753if (cpsz && indicator->buffer)754_rtw_memcpy(indicator->buffer, buf, cpsz);755756rtw_sctx_done(&sctx);757758exit:759return RTW_HALMAC_SUCCESS;760}761762struct halmac_platform_api rtw_halmac_platform_api = {763/* R/W register */764#ifdef CONFIG_SDIO_HCI765.SDIO_CMD52_READ = _halmac_sdio_cmd52_read,766.SDIO_CMD53_READ_8 = _halmac_sdio_reg_read_8,767.SDIO_CMD53_READ_16 = _halmac_sdio_reg_read_16,768.SDIO_CMD53_READ_32 = _halmac_sdio_reg_read_32,769.SDIO_CMD53_READ_N = _halmac_sdio_reg_read_n,770.SDIO_CMD52_WRITE = _halmac_sdio_cmd52_write,771.SDIO_CMD53_WRITE_8 = _halmac_sdio_reg_write_8,772.SDIO_CMD53_WRITE_16 = _halmac_sdio_reg_write_16,773.SDIO_CMD53_WRITE_32 = _halmac_sdio_reg_write_32,774.SDIO_CMD52_CIA_READ = _halmac_sdio_read_cia,775#endif /* CONFIG_SDIO_HCI */776#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI)777.REG_READ_8 = _halmac_reg_read_8,778.REG_READ_16 = _halmac_reg_read_16,779.REG_READ_32 = _halmac_reg_read_32,780.REG_WRITE_8 = _halmac_reg_write_8,781.REG_WRITE_16 = _halmac_reg_write_16,782.REG_WRITE_32 = _halmac_reg_write_32,783#endif /* CONFIG_USB_HCI || CONFIG_PCI_HCI */784785#ifdef DBG_IO786.READ_MONITOR = _halmac_reg_read_monitor,787.WRITE_MONITOR = _halmac_reg_write_monitor,788#endif789790/* Write data */791#if 0792/* impletement in HAL-IC level */793.SEND_RSVD_PAGE = sdio_write_data_rsvd_page,794.SEND_H2C_PKT = sdio_write_data_h2c,795#endif796/* Memory allocate */797.RTL_FREE = _halmac_mfree,798.RTL_MALLOC = _halmac_malloc,799.RTL_MEMCPY = _halmac_memcpy,800.RTL_MEMSET = _halmac_memset,801802/* Sleep */803.RTL_DELAY_US = _halmac_udelay,804805/* Process Synchronization */806.MUTEX_INIT = _halmac_mutex_init,807.MUTEX_DEINIT = _halmac_mutex_deinit,808.MUTEX_LOCK = _halmac_mutex_lock,809.MUTEX_UNLOCK = _halmac_mutex_unlock,810811.MSG_PRINT = _halmac_msg_print,812.BUFF_PRINT = _halmac_buff_print,813.EVENT_INDICATION = _halmac_event_indication,814};815816u8 rtw_halmac_read8(struct intf_hdl *pintfhdl, u32 addr)817{818struct halmac_adapter *mac;819struct halmac_api *api;820821822/* WARNING: pintf_dev should not be null! */823mac = dvobj_to_halmac(pintfhdl->pintf_dev);824api = HALMAC_GET_API(mac);825826return api->halmac_reg_read_8(mac, addr);827}828829u16 rtw_halmac_read16(struct intf_hdl *pintfhdl, u32 addr)830{831struct halmac_adapter *mac;832struct halmac_api *api;833834835/* WARNING: pintf_dev should not be null! */836mac = dvobj_to_halmac(pintfhdl->pintf_dev);837api = HALMAC_GET_API(mac);838839return api->halmac_reg_read_16(mac, addr);840}841842u32 rtw_halmac_read32(struct intf_hdl *pintfhdl, u32 addr)843{844struct halmac_adapter *mac;845struct halmac_api *api;846847848/* WARNING: pintf_dev should not be null! */849mac = dvobj_to_halmac(pintfhdl->pintf_dev);850api = HALMAC_GET_API(mac);851852return api->halmac_reg_read_32(mac, addr);853}854855static void _read_register(struct dvobj_priv *d, u32 addr, u32 cnt, u8 *buf)856{857#if 1858struct _ADAPTER *a;859u32 i, n;860u16 val16;861u32 val32;862863864a = dvobj_get_primary_adapter(d);865866i = addr & 0x3;867/* Handle address not start from 4 bytes alignment case */868if (i) {869val32 = cpu_to_le32(rtw_read32(a, addr & ~0x3));870n = 4 - i;871_rtw_memcpy(buf, ((u8 *)&val32) + i, n);872i = n;873cnt -= n;874}875876while (cnt) {877if (cnt >= 4)878n = 4;879else if (cnt >= 2)880n = 2;881else882n = 1;883cnt -= n;884885switch (n) {886case 1:887buf[i] = rtw_read8(a, addr+i);888i++;889break;890case 2:891val16 = cpu_to_le16(rtw_read16(a, addr+i));892_rtw_memcpy(&buf[i], &val16, 2);893i += 2;894break;895case 4:896val32 = cpu_to_le32(rtw_read32(a, addr+i));897_rtw_memcpy(&buf[i], &val32, 4);898i += 4;899break;900}901}902#else903struct _ADAPTER *a;904u32 i;905906907a = dvobj_get_primary_adapter(d);908for (i = 0; i < cnt; i++)909buf[i] = rtw_read8(a, addr + i);910#endif911}912913#ifdef CONFIG_SDIO_HCI914static int _sdio_read_local(struct dvobj_priv *d, u32 addr, u32 cnt, u8 *buf)915{916struct halmac_adapter *mac;917struct halmac_api *api;918enum halmac_ret_status status;919920921if (buf == NULL)922return -1;923924mac = dvobj_to_halmac(d);925api = HALMAC_GET_API(mac);926927status = api->halmac_reg_sdio_cmd53_read_n(mac, addr, cnt, buf);928if (status != HALMAC_RET_SUCCESS) {929RTW_ERR("%s: addr=0x%08x cnt=%d err=%d\n",930__FUNCTION__, addr, cnt, status);931return -1;932}933934return 0;935}936#endif /* CONFIG_SDIO_HCI */937938void rtw_halmac_read_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem)939{940struct dvobj_priv *d;941942943if (pmem == NULL) {944RTW_ERR("pmem is NULL\n");945return;946}947948d = pintfhdl->pintf_dev;949950#ifdef CONFIG_SDIO_HCI951if (addr & 0xFFFF0000) {952int err = 0;953954err = _sdio_read_local(d, addr, cnt, pmem);955if (!err)956return;957}958#endif /* CONFIG_SDIO_HCI */959960_read_register(d, addr, cnt, pmem);961}962963#ifdef CONFIG_SDIO_INDIRECT_ACCESS964u8 rtw_halmac_iread8(struct intf_hdl *pintfhdl, u32 addr)965{966struct halmac_adapter *mac;967struct halmac_api *api;968969/* WARNING: pintf_dev should not be null! */970mac = dvobj_to_halmac(pintfhdl->pintf_dev);971api = HALMAC_GET_API(mac);972973/*return api->halmac_reg_read_indirect_8(mac, addr);*/974return api->halmac_reg_read_8(mac, addr);975}976977u16 rtw_halmac_iread16(struct intf_hdl *pintfhdl, u32 addr)978{979struct halmac_adapter *mac;980struct halmac_api *api;981u16 val16 = 0;982983/* WARNING: pintf_dev should not be null! */984mac = dvobj_to_halmac(pintfhdl->pintf_dev);985api = HALMAC_GET_API(mac);986987/*return api->halmac_reg_read_indirect_16(mac, addr);*/988return api->halmac_reg_read_16(mac, addr);989}990991u32 rtw_halmac_iread32(struct intf_hdl *pintfhdl, u32 addr)992{993struct halmac_adapter *mac;994struct halmac_api *api;995996997/* WARNING: pintf_dev should not be null! */998mac = dvobj_to_halmac(pintfhdl->pintf_dev);999api = HALMAC_GET_API(mac);10001001return api->halmac_reg_read_indirect_32(mac, addr);1002}1003#endif /* CONFIG_SDIO_INDIRECT_ACCESS */10041005int rtw_halmac_write8(struct intf_hdl *pintfhdl, u32 addr, u8 value)1006{1007struct halmac_adapter *mac;1008struct halmac_api *api;1009enum halmac_ret_status status;101010111012/* WARNING: pintf_dev should not be null! */1013mac = dvobj_to_halmac(pintfhdl->pintf_dev);1014api = HALMAC_GET_API(mac);10151016status = api->halmac_reg_write_8(mac, addr, value);10171018if (status == HALMAC_RET_SUCCESS)1019return 0;10201021return -1;1022}10231024int rtw_halmac_write16(struct intf_hdl *pintfhdl, u32 addr, u16 value)1025{1026struct halmac_adapter *mac;1027struct halmac_api *api;1028enum halmac_ret_status status;102910301031/* WARNING: pintf_dev should not be null! */1032mac = dvobj_to_halmac(pintfhdl->pintf_dev);1033api = HALMAC_GET_API(mac);10341035status = api->halmac_reg_write_16(mac, addr, value);10361037if (status == HALMAC_RET_SUCCESS)1038return 0;10391040return -1;1041}10421043int rtw_halmac_write32(struct intf_hdl *pintfhdl, u32 addr, u32 value)1044{1045struct halmac_adapter *mac;1046struct halmac_api *api;1047enum halmac_ret_status status;104810491050/* WARNING: pintf_dev should not be null! */1051mac = dvobj_to_halmac(pintfhdl->pintf_dev);1052api = HALMAC_GET_API(mac);10531054status = api->halmac_reg_write_32(mac, addr, value);10551056if (status == HALMAC_RET_SUCCESS)1057return 0;10581059return -1;1060}10611062static int init_write_rsvd_page_size(struct dvobj_priv *d)1063{1064struct halmac_adapter *mac;1065struct halmac_api *api;1066u32 size = 0;1067struct halmac_ofld_func_info ofld_info;1068enum halmac_ret_status status;1069int err = 0;107010711072#ifdef CONFIG_USB_HCI1073/* for USB do not exceed MAX_CMDBUF_SZ */1074size = 0x1000;1075#elif defined(CONFIG_PCI_HCI)1076size = MAX_CMDBUF_SZ - TXDESC_OFFSET;1077#elif defined(CONFIG_SDIO_HCI)1078size = 0x7000; /* 28KB */1079#else1080/* Use HALMAC default setting and don't call any function */1081return 0;1082#endif1083#if 0 /* Fail to pass coverity DEADCODE check */1084/* If size==0, use HALMAC default setting and don't call any function */1085if (!size)1086return 0;1087#endif1088err = rtw_halmac_set_max_dl_fw_size(d, size);1089if (err) {1090RTW_ERR("%s: Fail to set max download fw size!\n", __FUNCTION__);1091return -1;1092}10931094mac = dvobj_to_halmac(d);1095api = HALMAC_GET_API(mac);10961097_rtw_memset(&ofld_info, 0, sizeof(ofld_info));1098ofld_info.halmac_malloc_max_sz = 0xFFFFFFFF;1099ofld_info.rsvd_pg_drv_buf_max_sz = size;1100status = api->halmac_ofld_func_cfg(mac, &ofld_info);1101if (status != HALMAC_RET_SUCCESS) {1102RTW_ERR("%s: Fail to config offload parameters!\n", __FUNCTION__);1103return -1;1104}11051106return 0;1107}11081109static int init_priv(struct halmacpriv *priv)1110{1111struct halmac_indicator *indicator;1112u32 count, size;111311141115if (priv->indicator)1116RTW_WARN("%s: HALMAC private data is not CLEAR!\n", __FUNCTION__);1117count = HALMAC_FEATURE_ALL + 1;1118size = sizeof(*indicator) * count;1119indicator = (struct halmac_indicator *)rtw_zmalloc(size);1120if (!indicator)1121return -1;1122priv->indicator = indicator;11231124return 0;1125}11261127static void deinit_priv(struct halmacpriv *priv)1128{1129struct halmac_indicator *indicator;113011311132indicator = priv->indicator;1133priv->indicator = NULL;1134if (indicator) {1135u32 count, size;11361137count = HALMAC_FEATURE_ALL + 1;1138#ifdef CONFIG_RTW_DEBUG1139{1140struct submit_ctx *sctx;1141u32 i;11421143for (i = 0; i < count; i++) {1144if (!indicator[i].sctx)1145continue;11461147RTW_WARN("%s: %s id(%d) sctx still exist!!\n",1148__FUNCTION__, RTW_HALMAC_FEATURE_NAME[i], i);1149sctx = indicator[i].sctx;1150indicator[i].sctx = NULL;1151rtw_mfree((u8 *)sctx, sizeof(*sctx));1152}1153}1154#endif /* !CONFIG_RTW_DEBUG */1155size = sizeof(*indicator) * count;1156rtw_mfree((u8 *)indicator, size);1157}1158}11591160#ifdef CONFIG_SDIO_HCI1161static enum halmac_sdio_spec_ver _sdio_ver_drv2halmac(struct dvobj_priv *d)1162{1163bool v3;1164enum halmac_sdio_spec_ver ver;116511661167v3 = rtw_is_sdio30(dvobj_get_primary_adapter(d));1168if (v3)1169ver = HALMAC_SDIO_SPEC_VER_3_00;1170else1171ver = HALMAC_SDIO_SPEC_VER_2_00;11721173return ver;1174}1175#endif /* CONFIG_SDIO_HCI */11761177void rtw_halmac_get_version(char *str, u32 len)1178{1179enum halmac_ret_status status;1180struct halmac_ver ver;118111821183status = halmac_get_version(&ver);1184if (status != HALMAC_RET_SUCCESS)1185return;11861187rtw_sprintf(str, len, "V%d_%02d_%02d",1188ver.major_ver, ver.prototype_ver, ver.minor_ver);1189}11901191int rtw_halmac_init_adapter(struct dvobj_priv *d, struct halmac_platform_api *pf_api)1192{1193struct halmac_adapter *halmac;1194struct halmac_api *api;1195enum halmac_interface intf;1196enum halmac_ret_status status;1197int err = 0;1198#ifdef CONFIG_SDIO_HCI1199struct halmac_sdio_hw_info info;1200#endif /* CONFIG_SDIO_HCI */120112021203halmac = dvobj_to_halmac(d);1204if (halmac) {1205RTW_WARN("%s: initialize already completed!\n", __FUNCTION__);1206goto error;1207}12081209err = init_priv(&d->hmpriv);1210if (err)1211goto error;12121213#ifdef CONFIG_SDIO_HCI1214intf = HALMAC_INTERFACE_SDIO;1215#elif defined(CONFIG_USB_HCI)1216intf = HALMAC_INTERFACE_USB;1217#elif defined(CONFIG_PCI_HCI)1218intf = HALMAC_INTERFACE_PCIE;1219#else1220#warning "INTERFACE(CONFIG_XXX_HCI) not be defined!!"1221intf = HALMAC_INTERFACE_UNDEFINE;1222#endif1223status = halmac_init_adapter(d, pf_api, intf, &halmac, &api);1224if (HALMAC_RET_SUCCESS != status) {1225RTW_ERR("%s: halmac_init_adapter fail!(status=%d)\n", __FUNCTION__, status);1226err = -1;1227if (halmac)1228goto deinit;1229goto free;1230}12311232dvobj_set_halmac(d, halmac);12331234status = api->halmac_interface_integration_tuning(halmac);1235if (status != HALMAC_RET_SUCCESS) {1236RTW_ERR("%s: halmac_interface_integration_tuning fail!(status=%d)\n", __FUNCTION__, status);1237err = -1;1238goto deinit;1239}12401241status = api->halmac_phy_cfg(halmac, HALMAC_INTF_PHY_PLATFORM_ALL);1242if (status != HALMAC_RET_SUCCESS) {1243RTW_ERR("%s: halmac_phy_cfg fail!(status=%d)\n", __FUNCTION__, status);1244err = -1;1245goto deinit;1246}12471248init_write_rsvd_page_size(d);12491250#ifdef CONFIG_SDIO_HCI1251_rtw_memset(&info, 0, sizeof(info));1252info.spec_ver = _sdio_ver_drv2halmac(d);1253/* Convert clock speed unit to MHz from Hz */1254info.clock_speed = RTW_DIV_ROUND_UP(rtw_sdio_get_clock(d), 1000000);1255info.block_size = rtw_sdio_get_block_size(d);1256RTW_DBG("%s: SDIO ver=%u clock=%uMHz blk_size=%u bytes\n",1257__FUNCTION__, info.spec_ver+2, info.clock_speed,1258info.block_size);1259status = api->halmac_sdio_hw_info(halmac, &info);1260if (status != HALMAC_RET_SUCCESS) {1261RTW_ERR("%s: halmac_sdio_hw_info fail!(status=%d)\n",1262__FUNCTION__, status);1263err = -1;1264goto deinit;1265}1266#endif /* CONFIG_SDIO_HCI */12671268return 0;12691270deinit:1271status = halmac_deinit_adapter(halmac);1272dvobj_set_halmac(d, NULL);1273if (status != HALMAC_RET_SUCCESS)1274RTW_ERR("%s: halmac_deinit_adapter fail!(status=%d)\n",1275__FUNCTION__, status);12761277free:1278deinit_priv(&d->hmpriv);12791280error:1281return err;1282}12831284int rtw_halmac_deinit_adapter(struct dvobj_priv *d)1285{1286struct halmac_adapter *halmac;1287enum halmac_ret_status status;1288int err = 0;128912901291halmac = dvobj_to_halmac(d);1292if (halmac) {1293status = halmac_deinit_adapter(halmac);1294dvobj_set_halmac(d, NULL);1295if (status != HALMAC_RET_SUCCESS)1296err = -1;1297}12981299deinit_priv(&d->hmpriv);13001301return err;1302}13031304static inline enum halmac_portid _hw_port_drv2halmac(enum _hw_port hwport)1305{1306enum halmac_portid port = HALMAC_PORTID_NUM;130713081309switch (hwport) {1310case HW_PORT0:1311port = HALMAC_PORTID0;1312break;1313case HW_PORT1:1314port = HALMAC_PORTID1;1315break;1316case HW_PORT2:1317port = HALMAC_PORTID2;1318break;1319case HW_PORT3:1320port = HALMAC_PORTID3;1321break;1322case HW_PORT4:1323port = HALMAC_PORTID4;1324break;1325default:1326break;1327}13281329return port;1330}13311332static enum halmac_network_type_select _network_type_drv2halmac(u8 type)1333{1334enum halmac_network_type_select network = HALMAC_NETWORK_UNDEFINE;133513361337switch (type) {1338case _HW_STATE_NOLINK_:1339case _HW_STATE_MONITOR_:1340network = HALMAC_NETWORK_NO_LINK;1341break;13421343case _HW_STATE_ADHOC_:1344network = HALMAC_NETWORK_ADHOC;1345break;13461347case _HW_STATE_STATION_:1348network = HALMAC_NETWORK_INFRASTRUCTURE;1349break;13501351case _HW_STATE_AP_:1352network = HALMAC_NETWORK_AP;1353break;1354}13551356return network;1357}13581359static u8 _network_type_halmac2drv(enum halmac_network_type_select network)1360{1361u8 type = _HW_STATE_NOLINK_;136213631364switch (network) {1365case HALMAC_NETWORK_NO_LINK:1366case HALMAC_NETWORK_UNDEFINE:1367type = _HW_STATE_NOLINK_;1368break;13691370case HALMAC_NETWORK_ADHOC:1371type = _HW_STATE_ADHOC_;1372break;13731374case HALMAC_NETWORK_INFRASTRUCTURE:1375type = _HW_STATE_STATION_;1376break;13771378case HALMAC_NETWORK_AP:1379type = _HW_STATE_AP_;1380break;1381}13821383return type;1384}13851386static void _beacon_ctrl_halmac2drv(struct halmac_bcn_ctrl *ctrl,1387struct rtw_halmac_bcn_ctrl *drv_ctrl)1388{1389drv_ctrl->rx_bssid_fit = ctrl->dis_rx_bssid_fit ? 0 : 1;1390drv_ctrl->txbcn_rpt = ctrl->en_txbcn_rpt ? 1 : 0;1391drv_ctrl->tsf_update = ctrl->dis_tsf_udt ? 0 : 1;1392drv_ctrl->enable_bcn = ctrl->en_bcn ? 1 : 0;1393drv_ctrl->rxbcn_rpt = ctrl->en_rxbcn_rpt ? 1 : 0;1394drv_ctrl->p2p_ctwin = ctrl->en_p2p_ctwin ? 1 : 0;1395drv_ctrl->p2p_bcn_area = ctrl->en_p2p_bcn_area ? 1 : 0;1396}13971398static void _beacon_ctrl_drv2halmac(struct rtw_halmac_bcn_ctrl *drv_ctrl,1399struct halmac_bcn_ctrl *ctrl)1400{1401ctrl->dis_rx_bssid_fit = drv_ctrl->rx_bssid_fit ? 0 : 1;1402ctrl->en_txbcn_rpt = drv_ctrl->txbcn_rpt ? 1 : 0;1403ctrl->dis_tsf_udt = drv_ctrl->tsf_update ? 0 : 1;1404ctrl->en_bcn = drv_ctrl->enable_bcn ? 1 : 0;1405ctrl->en_rxbcn_rpt = drv_ctrl->rxbcn_rpt ? 1 : 0;1406ctrl->en_p2p_ctwin = drv_ctrl->p2p_ctwin ? 1 : 0;1407ctrl->en_p2p_bcn_area = drv_ctrl->p2p_bcn_area ? 1 : 0;1408}14091410int rtw_halmac_get_hw_value(struct dvobj_priv *d, enum halmac_hw_id hw_id, void *pvalue)1411{1412struct halmac_adapter *mac;1413struct halmac_api *api;1414enum halmac_ret_status status;141514161417mac = dvobj_to_halmac(d);1418api = HALMAC_GET_API(mac);14191420status = api->halmac_get_hw_value(mac, hw_id, pvalue);1421if (HALMAC_RET_SUCCESS != status)1422return -1;14231424return 0;1425}14261427/**1428* rtw_halmac_get_tx_fifo_size() - TX FIFO size1429* @d: struct dvobj_priv*1430* @size: TX FIFO size, unit is byte.1431*1432* Get TX FIFO size(byte) from HALMAC.1433*1434* Return 0 for OK, otherwise fail.1435*/1436int rtw_halmac_get_tx_fifo_size(struct dvobj_priv *d, u32 *size)1437{1438struct halmac_adapter *halmac;1439struct halmac_api *api;1440enum halmac_ret_status status;1441u32 val = 0;144214431444halmac = dvobj_to_halmac(d);1445api = HALMAC_GET_API(halmac);14461447status = api->halmac_get_hw_value(halmac, HALMAC_HW_TXFIFO_SIZE, &val);1448if (status != HALMAC_RET_SUCCESS)1449return -1;14501451*size = val;14521453return 0;1454}14551456/**1457* rtw_halmac_get_rx_fifo_size() - RX FIFO size1458* @d: struct dvobj_priv*1459* @size: RX FIFO size, unit is byte1460*1461* Get RX FIFO size(byte) from HALMAC.1462*1463* Return 0 for OK, otherwise fail.1464*/1465int rtw_halmac_get_rx_fifo_size(struct dvobj_priv *d, u32 *size)1466{1467struct halmac_adapter *halmac;1468struct halmac_api *api;1469enum halmac_ret_status status;1470u32 val = 0;147114721473halmac = dvobj_to_halmac(d);1474api = HALMAC_GET_API(halmac);14751476status = api->halmac_get_hw_value(halmac, HALMAC_HW_RXFIFO_SIZE, &val);1477if (status != HALMAC_RET_SUCCESS)1478return -1;14791480*size = val;14811482return 0;1483}14841485/**1486* rtw_halmac_get_rsvd_drv_pg_bndy() - Reserve page boundary of driver1487* @d: struct dvobj_priv*1488* @size: Page size, unit is byte1489*1490* Get reserve page boundary of driver from HALMAC.1491*1492* Return 0 for OK, otherwise fail.1493*/1494int rtw_halmac_get_rsvd_drv_pg_bndy(struct dvobj_priv *d, u16 *bndy)1495{1496struct halmac_adapter *halmac;1497struct halmac_api *api;1498enum halmac_ret_status status;1499u16 val = 0;150015011502halmac = dvobj_to_halmac(d);1503api = HALMAC_GET_API(halmac);15041505status = api->halmac_get_hw_value(halmac, HALMAC_HW_RSVD_PG_BNDY, &val);1506if (status != HALMAC_RET_SUCCESS)1507return -1;15081509*bndy = val;15101511return 0;1512}15131514/**1515* rtw_halmac_get_page_size() - Page size1516* @d: struct dvobj_priv*1517* @size: Page size, unit is byte1518*1519* Get TX/RX page size(byte) from HALMAC.1520*1521* Return 0 for OK, otherwise fail.1522*/1523int rtw_halmac_get_page_size(struct dvobj_priv *d, u32 *size)1524{1525struct halmac_adapter *halmac;1526struct halmac_api *api;1527enum halmac_ret_status status;1528u32 val = 0;152915301531halmac = dvobj_to_halmac(d);1532api = HALMAC_GET_API(halmac);15331534status = api->halmac_get_hw_value(halmac, HALMAC_HW_PAGE_SIZE, &val);1535if (status != HALMAC_RET_SUCCESS)1536return -1;15371538*size = val;15391540return 0;1541}15421543/**1544* rtw_halmac_get_tx_agg_align_size() - TX aggregation align size1545* @d: struct dvobj_priv*1546* @size: TX aggregation align size, unit is byte1547*1548* Get TX aggregation align size(byte) from HALMAC.1549*1550* Return 0 for OK, otherwise fail.1551*/1552int rtw_halmac_get_tx_agg_align_size(struct dvobj_priv *d, u16 *size)1553{1554struct halmac_adapter *halmac;1555struct halmac_api *api;1556enum halmac_ret_status status;1557u16 val = 0;155815591560halmac = dvobj_to_halmac(d);1561api = HALMAC_GET_API(halmac);15621563status = api->halmac_get_hw_value(halmac, HALMAC_HW_TX_AGG_ALIGN_SIZE, &val);1564if (status != HALMAC_RET_SUCCESS)1565return -1;15661567*size = val;15681569return 0;1570}15711572/**1573* rtw_halmac_get_rx_agg_align_size() - RX aggregation align size1574* @d: struct dvobj_priv*1575* @size: RX aggregation align size, unit is byte1576*1577* Get RX aggregation align size(byte) from HALMAC.1578*1579* Return 0 for OK, otherwise fail.1580*/1581int rtw_halmac_get_rx_agg_align_size(struct dvobj_priv *d, u8 *size)1582{1583struct halmac_adapter *halmac;1584struct halmac_api *api;1585enum halmac_ret_status status;1586u8 val = 0;158715881589halmac = dvobj_to_halmac(d);1590api = HALMAC_GET_API(halmac);15911592status = api->halmac_get_hw_value(halmac, HALMAC_HW_RX_AGG_ALIGN_SIZE, &val);1593if (status != HALMAC_RET_SUCCESS)1594return -1;15951596*size = val;15971598return 0;1599}16001601/*1602* Description:1603* Get RX driver info size. RX driver info is a small memory space between1604* scriptor and RX payload.1605*1606* +-------------------------+1607* | RX descriptor |1608* | usually 24 bytes |1609* +-------------------------+1610* | RX driver info |1611* | depends on driver cfg |1612* +-------------------------+1613* | RX paylad |1614* | |1615* +-------------------------+1616*1617* Parameter:1618* d pointer to struct dvobj_priv of driver1619* sz rx driver info size in bytes.1620*1621* Return:1622* 0 Success1623* other Fail1624*/1625int rtw_halmac_get_rx_drv_info_sz(struct dvobj_priv *d, u8 *sz)1626{1627enum halmac_ret_status status;1628struct halmac_adapter *halmac = dvobj_to_halmac(d);1629struct halmac_api *api = HALMAC_GET_API(halmac);1630u8 dw = 0;16311632status = api->halmac_get_hw_value(halmac, HALMAC_HW_DRV_INFO_SIZE, &dw);1633if (status != HALMAC_RET_SUCCESS)1634return -1;16351636*sz = dw * 8;1637return 0;1638}16391640/**1641* rtw_halmac_get_tx_desc_size() - TX descriptor size1642* @d: struct dvobj_priv*1643* @size: TX descriptor size, unit is byte.1644*1645* Get TX descriptor size(byte) from HALMAC.1646*1647* Return 0 for OK, otherwise fail.1648*/1649int rtw_halmac_get_tx_desc_size(struct dvobj_priv *d, u32 *size)1650{1651struct halmac_adapter *halmac;1652struct halmac_api *api;1653enum halmac_ret_status status;1654u32 val = 0;165516561657halmac = dvobj_to_halmac(d);1658api = HALMAC_GET_API(halmac);16591660status = api->halmac_get_hw_value(halmac, HALMAC_HW_TX_DESC_SIZE, &val);1661if (status != HALMAC_RET_SUCCESS)1662return -1;16631664*size = val;16651666return 0;1667}16681669/**1670* rtw_halmac_get_rx_desc_size() - RX descriptor size1671* @d: struct dvobj_priv*1672* @size: RX descriptor size, unit is byte.1673*1674* Get RX descriptor size(byte) from HALMAC.1675*1676* Return 0 for OK, otherwise fail.1677*/1678int rtw_halmac_get_rx_desc_size(struct dvobj_priv *d, u32 *size)1679{1680struct halmac_adapter *halmac;1681struct halmac_api *api;1682enum halmac_ret_status status;1683u32 val = 0;168416851686halmac = dvobj_to_halmac(d);1687api = HALMAC_GET_API(halmac);16881689status = api->halmac_get_hw_value(halmac, HALMAC_HW_RX_DESC_SIZE, &val);1690if (status != HALMAC_RET_SUCCESS)1691return -1;16921693*size = val;16941695return 0;1696}16971698/**1699* rtw_halmac_get_tx_dma_ch_map() - Get TX DMA channel Map for tx desc1700* @d: struct dvobj_priv*1701* @dma_ch_map: return map of QSEL to DMA channel1702* @map_size: size of dma_ch_map1703* Suggest size to be last valid QSEL(QSLT_CMD)+1 or full QSLT1704* size(0x20)1705*1706* 8814B would need this to get mapping of QSEL to DMA channel for TX desc.1707*1708* Return 0 for OK, otherwise fail.1709*/1710int rtw_halmac_get_tx_dma_ch_map(struct dvobj_priv *d, u8 *dma_ch_map, u8 map_size)1711{1712struct halmac_adapter *halmac;1713struct halmac_api *api;1714enum halmac_ret_status status;1715struct halmac_rqpn_ch_map map;1716enum halmac_dma_ch channel = HALMAC_DMA_CH_UNDEFINE;1717u8 qsel;171817191720halmac = dvobj_to_halmac(d);1721api = HALMAC_GET_API(halmac);17221723status = api->halmac_get_hw_value(halmac, HALMAC_HW_RQPN_CH_MAPPING, &map);1724if (status != HALMAC_RET_SUCCESS)1725return -1;17261727for (qsel = 0; qsel < map_size; qsel++) {1728switch (qsel) {1729/*case QSLT_VO:*/1730case 0x06:1731case 0x07:1732channel = map.dma_map_vo;1733break;1734/*case QSLT_VI:*/1735case 0x04:1736case 0x05:1737channel = map.dma_map_vi;1738break;1739/*case QSLT_BE:*/1740case 0x00:1741case 0x03:1742channel = map.dma_map_be;1743break;1744/*case QSLT_BK:*/1745case 0x01:1746case 0x02:1747channel = map.dma_map_bk;1748break;1749/*case QSLT_BEACON:*/1750case 0x10:1751channel = HALMAC_DMA_CH_BCN;1752break;1753/*case QSLT_HIGH:*/1754case 0x11:1755channel = map.dma_map_hi;1756break;1757/*case QSLT_MGNT:*/1758case 0x12:1759channel = map.dma_map_mg;1760break;1761/*case QSLT_CMD:*/1762case 0x13:1763channel = HALMAC_DMA_CH_H2C;1764break;1765default:1766/*RTW_ERR("%s: invalid qsel=0x%x\n", __FUNCTION__, qsel);*/1767channel = HALMAC_DMA_CH_UNDEFINE;1768break;1769}1770dma_ch_map[qsel] = (u8)channel;1771}17721773return 0;1774}17751776/**1777* rtw_halmac_get_fw_max_size() - Firmware MAX size1778* @d: struct dvobj_priv*1779* @size: MAX Firmware size, unit is byte.1780*1781* Get Firmware MAX size(byte) from HALMAC.1782*1783* Return 0 for OK, otherwise fail.1784*/1785static int rtw_halmac_get_fw_max_size(struct dvobj_priv *d, u32 *size)1786{1787struct halmac_adapter *halmac;1788struct halmac_api *api;1789enum halmac_ret_status status;1790u32 val = 0;179117921793halmac = dvobj_to_halmac(d);1794api = HALMAC_GET_API(halmac);17951796status = api->halmac_get_hw_value(halmac, HALMAC_HW_FW_MAX_SIZE, &val);1797if (status != HALMAC_RET_SUCCESS)1798return -1;17991800*size = val;18011802return 0;1803}18041805/**1806* rtw_halmac_get_ori_h2c_size() - Original H2C MAX size1807* @d: struct dvobj_priv*1808* @size: H2C MAX size, unit is byte.1809*1810* Get original H2C MAX size(byte) from HALMAC.1811*1812* Return 0 for OK, otherwise fail.1813*/1814int rtw_halmac_get_ori_h2c_size(struct dvobj_priv *d, u32 *size)1815{1816struct halmac_adapter *halmac;1817struct halmac_api *api;1818enum halmac_ret_status status;1819u32 val = 0;182018211822halmac = dvobj_to_halmac(d);1823api = HALMAC_GET_API(halmac);18241825status = api->halmac_get_hw_value(halmac, HALMAC_HW_ORI_H2C_SIZE, &val);1826if (status != HALMAC_RET_SUCCESS)1827return -1;18281829*size = val;18301831return 0;1832}18331834int rtw_halmac_get_oqt_size(struct dvobj_priv *d, u8 *size)1835{1836enum halmac_ret_status status;1837struct halmac_adapter *halmac;1838struct halmac_api *api;1839u8 val;184018411842if (!size)1843return -1;18441845halmac = dvobj_to_halmac(d);1846api = HALMAC_GET_API(halmac);18471848status = api->halmac_get_hw_value(halmac, HALMAC_HW_AC_OQT_SIZE, &val);1849if (status != HALMAC_RET_SUCCESS)1850return -1;18511852*size = val;1853return 0;1854}18551856int rtw_halmac_get_ac_queue_number(struct dvobj_priv *d, u8 *num)1857{1858enum halmac_ret_status status;1859struct halmac_adapter *halmac;1860struct halmac_api *api;1861u8 val;186218631864if (!num)1865return -1;18661867halmac = dvobj_to_halmac(d);1868api = HALMAC_GET_API(halmac);18691870status = api->halmac_get_hw_value(halmac, HALMAC_HW_AC_QUEUE_NUM, &val);1871if (status != HALMAC_RET_SUCCESS)1872return -1;18731874*num = val;1875return 0;1876}18771878/**1879* rtw_halmac_get_mac_address() - Get MAC address of specific port1880* @d: struct dvobj_priv*1881* @hwport: port1882* @addr: buffer for storing MAC address1883*1884* Get MAC address of specific port from HALMAC.1885*1886* Return 0 for OK, otherwise fail.1887*/1888int rtw_halmac_get_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)1889{1890struct halmac_adapter *halmac;1891struct halmac_api *api;1892enum halmac_portid port;1893union halmac_wlan_addr hwa;1894enum halmac_ret_status status;1895int err = -1;189618971898if (!addr)1899goto out;19001901halmac = dvobj_to_halmac(d);1902api = HALMAC_GET_API(halmac);1903port = _hw_port_drv2halmac(hwport);1904_rtw_memset(&hwa, 0, sizeof(hwa));19051906status = api->halmac_get_mac_addr(halmac, port, &hwa);1907if (status != HALMAC_RET_SUCCESS)1908goto out;19091910_rtw_memcpy(addr, hwa.addr, 6);19111912err = 0;1913out:1914return err;1915}19161917/**1918* rtw_halmac_get_network_type() - Get network type of specific port1919* @d: struct dvobj_priv*1920* @hwport: port1921* @type: buffer to put network type (_HW_STATE_*)1922*1923* Get network type of specific port from HALMAC.1924*1925* Return 0 for OK, otherwise fail.1926*/1927int rtw_halmac_get_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 *type)1928{1929#if 01930struct halmac_adapter *halmac;1931struct halmac_api *api;1932enum halmac_portid port;1933enum halmac_network_type_select network;1934enum halmac_ret_status status;1935int err = -1;193619371938halmac = dvobj_to_halmac(d);1939api = HALMAC_GET_API(halmac);1940port = _hw_port_drv2halmac(hwport);1941network = HALMAC_NETWORK_UNDEFINE;19421943status = api->halmac_get_net_type(halmac, port, &network);1944if (status != HALMAC_RET_SUCCESS)1945goto out;19461947*type = _network_type_halmac2drv(network);19481949err = 0;1950out:1951return err;1952#else1953struct _ADAPTER *a;1954enum halmac_portid port;1955enum halmac_network_type_select network;1956u32 val;1957int err = -1;195819591960a = dvobj_get_primary_adapter(d);1961port = _hw_port_drv2halmac(hwport);1962network = HALMAC_NETWORK_UNDEFINE;19631964switch (port) {1965case HALMAC_PORTID0:1966val = rtw_read32(a, REG_CR);1967network = BIT_GET_NETYPE0(val);1968break;19691970case HALMAC_PORTID1:1971val = rtw_read32(a, REG_CR);1972network = BIT_GET_NETYPE1(val);1973break;19741975case HALMAC_PORTID2:1976val = rtw_read32(a, REG_CR_EXT);1977network = BIT_GET_NETYPE2(val);1978break;19791980case HALMAC_PORTID3:1981val = rtw_read32(a, REG_CR_EXT);1982network = BIT_GET_NETYPE3(val);1983break;19841985case HALMAC_PORTID4:1986val = rtw_read32(a, REG_CR_EXT);1987network = BIT_GET_NETYPE4(val);1988break;19891990default:1991goto out;1992}19931994*type = _network_type_halmac2drv(network);19951996err = 0;1997out:1998return err;1999#endif2000}20012002/**2003* rtw_halmac_get_bcn_ctrl() - Get beacon control setting of specific port2004* @d: struct dvobj_priv*2005* @hwport: port2006* @bcn_ctrl: setting of beacon control2007*2008* Get beacon control setting of specific port from HALMAC.2009*2010* Return 0 for OK, otherwise fail.2011*/2012int rtw_halmac_get_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport,2013struct rtw_halmac_bcn_ctrl *bcn_ctrl)2014{2015struct halmac_adapter *halmac;2016struct halmac_api *api;2017enum halmac_portid port;2018struct halmac_bcn_ctrl ctrl;2019enum halmac_ret_status status;2020int err = -1;202120222023halmac = dvobj_to_halmac(d);2024api = HALMAC_GET_API(halmac);2025port = _hw_port_drv2halmac(hwport);2026_rtw_memset(&ctrl, 0, sizeof(ctrl));20272028status = api->halmac_rw_bcn_ctrl(halmac, port, 0, &ctrl);2029if (status != HALMAC_RET_SUCCESS)2030goto out;2031_beacon_ctrl_halmac2drv(&ctrl, bcn_ctrl);20322033err = 0;2034out:2035return err;2036}20372038/*2039* Note:2040* When this function return, the register REG_RCR may be changed.2041*/2042int rtw_halmac_config_rx_info(struct dvobj_priv *d, enum halmac_drv_info info)2043{2044struct halmac_adapter *halmac;2045struct halmac_api *api;2046enum halmac_ret_status status;2047int err = -1;204820492050halmac = dvobj_to_halmac(d);2051api = HALMAC_GET_API(halmac);20522053status = api->halmac_cfg_drv_info(halmac, info);2054if (status != HALMAC_RET_SUCCESS)2055goto out;20562057err = 0;2058out:2059return err;2060}20612062/**2063* rtw_halmac_set_max_dl_fw_size() - Set the MAX download firmware size2064* @d: struct dvobj_priv*2065* @size: the max download firmware size in one I/O2066*2067* Set the max download firmware size in one I/O.2068* Please also consider the max size of the callback function "SEND_RSVD_PAGE"2069* could accept, because download firmware would call "SEND_RSVD_PAGE" to send2070* firmware to IC.2071*2072* If the value of "size" is not even, it would be rounded down to nearest2073* even, and 0 and 1 are both invalid value.2074*2075* Return 0 for setting OK, otherwise fail.2076*/2077int rtw_halmac_set_max_dl_fw_size(struct dvobj_priv *d, u32 size)2078{2079struct halmac_adapter *mac;2080struct halmac_api *api;2081enum halmac_ret_status status;208220832084if (!size || (size == 1))2085return -1;20862087mac = dvobj_to_halmac(d);2088if (!mac) {2089RTW_ERR("%s: HALMAC is not ready!!\n", __FUNCTION__);2090return -1;2091}2092api = HALMAC_GET_API(mac);20932094size &= ~1; /* round down to even */2095status = api->halmac_cfg_max_dl_size(mac, size);2096if (status != HALMAC_RET_SUCCESS) {2097RTW_WARN("%s: Fail to cfg_max_dl_size(%d), err=%d!!\n",2098__FUNCTION__, size, status);2099return -1;2100}21012102return 0;2103}21042105/**2106* rtw_halmac_set_mac_address() - Set mac address of specific port2107* @d: struct dvobj_priv*2108* @hwport: port2109* @addr: mac address2110*2111* Set self mac address of specific port to HALMAC.2112*2113* Return 0 for OK, otherwise fail.2114*/2115int rtw_halmac_set_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)2116{2117struct halmac_adapter *halmac;2118struct halmac_api *api;2119enum halmac_portid port;2120union halmac_wlan_addr hwa;2121enum halmac_ret_status status;2122int err = -1;212321242125halmac = dvobj_to_halmac(d);2126api = HALMAC_GET_API(halmac);21272128port = _hw_port_drv2halmac(hwport);2129_rtw_memset(&hwa, 0, sizeof(hwa));2130_rtw_memcpy(hwa.addr, addr, 6);21312132status = api->halmac_cfg_mac_addr(halmac, port, &hwa);2133if (status != HALMAC_RET_SUCCESS)2134goto out;21352136err = 0;2137out:2138return err;2139}21402141/**2142* rtw_halmac_set_bssid() - Set BSSID of specific port2143* @d: struct dvobj_priv*2144* @hwport: port2145* @addr: BSSID, mac address of AP2146*2147* Set BSSID of specific port to HALMAC.2148*2149* Return 0 for OK, otherwise fail.2150*/2151int rtw_halmac_set_bssid(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)2152{2153struct halmac_adapter *halmac;2154struct halmac_api *api;2155enum halmac_portid port;2156union halmac_wlan_addr hwa;2157enum halmac_ret_status status;2158int err = -1;215921602161halmac = dvobj_to_halmac(d);2162api = HALMAC_GET_API(halmac);2163port = _hw_port_drv2halmac(hwport);21642165_rtw_memset(&hwa, 0, sizeof(hwa));2166_rtw_memcpy(hwa.addr, addr, 6);2167status = api->halmac_cfg_bssid(halmac, port, &hwa);2168if (status != HALMAC_RET_SUCCESS)2169goto out;21702171err = 0;2172out:2173return err;2174}21752176/**2177* rtw_halmac_set_tx_address() - Set transmitter address of specific port2178* @d: struct dvobj_priv*2179* @hwport: port2180* @addr: transmitter address2181*2182* Set transmitter address of specific port to HALMAC.2183*2184* Return 0 for OK, otherwise fail.2185*/2186int rtw_halmac_set_tx_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr)2187{2188struct halmac_adapter *halmac;2189struct halmac_api *api;2190enum halmac_portid port;2191union halmac_wlan_addr hwa;2192enum halmac_ret_status status;2193int err = -1;219421952196halmac = dvobj_to_halmac(d);2197api = HALMAC_GET_API(halmac);2198port = _hw_port_drv2halmac(hwport);2199_rtw_memset(&hwa, 0, sizeof(hwa));2200_rtw_memcpy(hwa.addr, addr, 6);22012202status = api->halmac_cfg_transmitter_addr(halmac, port, &hwa);2203if (status != HALMAC_RET_SUCCESS)2204goto out;22052206err = 0;2207out:2208return err;2209}22102211/**2212* rtw_halmac_set_network_type() - Set network type of specific port2213* @d: struct dvobj_priv*2214* @hwport: port2215* @type: network type (_HW_STATE_*)2216*2217* Set network type of specific port to HALMAC.2218*2219* Return 0 for OK, otherwise fail.2220*/2221int rtw_halmac_set_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 type)2222{2223struct halmac_adapter *halmac;2224struct halmac_api *api;2225enum halmac_portid port;2226enum halmac_network_type_select network;2227enum halmac_ret_status status;2228int err = -1;222922302231halmac = dvobj_to_halmac(d);2232api = HALMAC_GET_API(halmac);2233port = _hw_port_drv2halmac(hwport);2234network = _network_type_drv2halmac(type);22352236status = api->halmac_cfg_net_type(halmac, port, network);2237if (status != HALMAC_RET_SUCCESS)2238goto out;22392240err = 0;2241out:2242return err;2243}22442245/**2246* rtw_halmac_reset_tsf() - Reset TSF timer of specific port2247* @d: struct dvobj_priv*2248* @hwport: port2249*2250* Notice HALMAC to reset timing synchronization function(TSF) timer of2251* specific port.2252*2253* Return 0 for OK, otherwise fail.2254*/2255int rtw_halmac_reset_tsf(struct dvobj_priv *d, enum _hw_port hwport)2256{2257struct halmac_adapter *halmac;2258struct halmac_api *api;2259enum halmac_portid port;2260enum halmac_ret_status status;2261int err = -1;226222632264halmac = dvobj_to_halmac(d);2265api = HALMAC_GET_API(halmac);2266port = _hw_port_drv2halmac(hwport);22672268status = api->halmac_cfg_tsf_rst(halmac, port);2269if (status != HALMAC_RET_SUCCESS)2270goto out;22712272err = 0;2273out:2274return err;2275}22762277/**2278* rtw_halmac_set_bcn_interval() - Set beacon interval of each port2279* @d: struct dvobj_priv*2280* @hwport: port2281* @space: beacon interval, unit is ms2282*2283* Set beacon interval of specific port to HALMAC.2284*2285* Return 0 for OK, otherwise fail.2286*/2287int rtw_halmac_set_bcn_interval(struct dvobj_priv *d, enum _hw_port hwport,2288u32 interval)2289{2290struct halmac_adapter *halmac;2291struct halmac_api *api;2292enum halmac_portid port;2293enum halmac_ret_status status;2294int err = -1;229522962297halmac = dvobj_to_halmac(d);2298api = HALMAC_GET_API(halmac);2299port = _hw_port_drv2halmac(hwport);23002301status = api->halmac_cfg_bcn_space(halmac, port, interval);2302if (status != HALMAC_RET_SUCCESS)2303goto out;23042305err = 0;2306out:2307return err;2308}23092310/**2311* rtw_halmac_set_bcn_ctrl() - Set beacon control setting of each port2312* @d: struct dvobj_priv*2313* @hwport: port2314* @bcn_ctrl: setting of beacon control2315*2316* Set beacon control setting of specific port to HALMAC.2317*2318* Return 0 for OK, otherwise fail.2319*/2320int rtw_halmac_set_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport,2321struct rtw_halmac_bcn_ctrl *bcn_ctrl)2322{2323struct halmac_adapter *halmac;2324struct halmac_api *api;2325enum halmac_portid port;2326struct halmac_bcn_ctrl ctrl;2327enum halmac_ret_status status;2328int err = -1;232923302331halmac = dvobj_to_halmac(d);2332api = HALMAC_GET_API(halmac);2333port = _hw_port_drv2halmac(hwport);2334_rtw_memset(&ctrl, 0, sizeof(ctrl));2335_beacon_ctrl_drv2halmac(bcn_ctrl, &ctrl);23362337status = api->halmac_rw_bcn_ctrl(halmac, port, 1, &ctrl);2338if (status != HALMAC_RET_SUCCESS)2339goto out;23402341err = 0;2342out:2343return err;2344}23452346/**2347* rtw_halmac_set_aid() - Set association identifier(AID) of specific port2348* @d: struct dvobj_priv*2349* @hwport: port2350* @aid: Association identifier2351*2352* Set association identifier(AID) of specific port to HALMAC.2353*2354* Return 0 for OK, otherwise fail.2355*/2356int rtw_halmac_set_aid(struct dvobj_priv *d, enum _hw_port hwport, u16 aid)2357{2358struct halmac_adapter *halmac;2359struct halmac_api *api;2360enum halmac_portid port;2361enum halmac_ret_status status;2362int err = -1;236323642365halmac = dvobj_to_halmac(d);2366api = HALMAC_GET_API(halmac);2367port = _hw_port_drv2halmac(hwport);23682369#if 02370status = api->halmac_cfg_aid(halmac, port, aid);2371if (status != HALMAC_RET_SUCCESS)2372goto out;2373#else2374{2375struct _ADAPTER *a;2376u32 addr;2377u16 val;23782379a = dvobj_get_primary_adapter(d);23802381switch (port) {2382case 0:2383addr = REG_BCN_PSR_RPT;2384val = rtw_read16(a, addr);2385val = BIT_SET_PS_AID_0(val, aid);2386rtw_write16(a, addr, val);2387break;23882389case 1:2390addr = REG_BCN_PSR_RPT1;2391val = rtw_read16(a, addr);2392val = BIT_SET_PS_AID_1(val, aid);2393rtw_write16(a, addr, val);2394break;23952396case 2:2397addr = REG_BCN_PSR_RPT2;2398val = rtw_read16(a, addr);2399val = BIT_SET_PS_AID_2(val, aid);2400rtw_write16(a, addr, val);2401break;24022403case 3:2404addr = REG_BCN_PSR_RPT3;2405val = rtw_read16(a, addr);2406val = BIT_SET_PS_AID_3(val, aid);2407rtw_write16(a, addr, val);2408break;24092410case 4:2411addr = REG_BCN_PSR_RPT4;2412val = rtw_read16(a, addr);2413val = BIT_SET_PS_AID_4(val, aid);2414rtw_write16(a, addr, val);2415break;24162417default:2418goto out;2419}2420}2421#endif24222423err = 0;2424out:2425return err;2426}24272428int rtw_halmac_set_bandwidth(struct dvobj_priv *d, u8 channel, u8 pri_ch_idx, u8 bw)2429{2430struct halmac_adapter *mac;2431struct halmac_api *api;2432enum halmac_ret_status status;243324342435mac = dvobj_to_halmac(d);2436api = HALMAC_GET_API(mac);24372438status = api->halmac_cfg_ch_bw(mac, channel, pri_ch_idx, bw);2439if (HALMAC_RET_SUCCESS != status)2440return -1;24412442return 0;2443}24442445/**2446* rtw_halmac_set_edca() - config edca parameter2447* @d: struct dvobj_priv*2448* @queue: XMIT_[VO/VI/BE/BK]_QUEUE2449* @aifs: Arbitration inter-frame space(AIFS)2450* @cw: Contention window(CW)2451* @txop: MAX Transmit Opportunity(TXOP)2452*2453* Return: 0 if process OK, otherwise -1.2454*/2455int rtw_halmac_set_edca(struct dvobj_priv *d, u8 queue, u8 aifs, u8 cw, u16 txop)2456{2457struct halmac_adapter *mac;2458struct halmac_api *api;2459enum halmac_acq_id ac;2460struct halmac_edca_para edca;2461enum halmac_ret_status status;246224632464mac = dvobj_to_halmac(d);2465api = HALMAC_GET_API(mac);24662467switch (queue) {2468case XMIT_VO_QUEUE:2469ac = HALMAC_ACQ_ID_VO;2470break;2471case XMIT_VI_QUEUE:2472ac = HALMAC_ACQ_ID_VI;2473break;2474case XMIT_BE_QUEUE:2475ac = HALMAC_ACQ_ID_BE;2476break;2477case XMIT_BK_QUEUE:2478ac = HALMAC_ACQ_ID_BK;2479break;2480default:2481return -1;2482}24832484edca.aifs = aifs;2485edca.cw = cw;2486edca.txop_limit = txop;24872488status = api->halmac_cfg_edca_para(mac, ac, &edca);2489if (status != HALMAC_RET_SUCCESS)2490return -1;24912492return 0;2493}24942495/**2496* rtw_halmac_set_rts_full_bw() - Send RTS to all covered channels2497* @d: struct dvobj_priv*2498* @enable: _TRUE(enable), _FALSE(disable)2499*2500* Hradware will duplicate RTS packet to all channels which are covered in used2501* bandwidth.2502*2503* Return 0 if process OK, otherwise -1.2504*/2505int rtw_halmac_set_rts_full_bw(struct dvobj_priv *d, u8 enable)2506{2507struct halmac_adapter *mac;2508struct halmac_api *api;2509enum halmac_ret_status status;2510u8 full;251125122513mac = dvobj_to_halmac(d);2514api = HALMAC_GET_API(mac);2515full = (enable == _TRUE) ? 1 : 0;25162517status = api->halmac_set_hw_value(mac, HALMAC_HW_RTS_FULL_BW, &full);2518if (HALMAC_RET_SUCCESS != status)2519return -1;25202521return 0;2522}25232524#ifdef RTW_HALMAC_DBG_POWER_SWITCH2525static void _dump_mac_reg(struct dvobj_priv *d, u32 start, u32 end)2526{2527struct _ADAPTER *adapter;2528int i, j = 1;252925302531adapter = dvobj_get_primary_adapter(d);2532for (i = start; i < end; i += 4) {2533if (j % 4 == 1)2534RTW_PRINT("0x%04x", i);2535_RTW_PRINT(" 0x%08x ", rtw_read32(adapter, i));2536if ((j++) % 4 == 0)2537_RTW_PRINT("\n");2538}2539}25402541void dump_dbg_val(struct _ADAPTER *a, u32 reg)2542{2543u32 v32;254425452546rtw_write8(a, 0x3A, reg);2547v32 = rtw_read32(a, 0xC0);2548RTW_PRINT("0x3A = %02x, 0xC0 = 0x%08x\n",reg, v32);2549}25502551#ifdef CONFIG_PCI_HCI2552static void _dump_pcie_cfg_space(struct dvobj_priv *d)2553{2554struct _ADAPTER *padapter = dvobj_get_primary_adapter(d);2555struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);2556struct pci_dev *pdev = pdvobjpriv->ppcidev;2557struct pci_dev *bridge_pdev = pdev->bus->self;25582559u32 tmp[4] = { 0 };2560u32 i, j;25612562RTW_PRINT("\n***** PCI Device Configuration Space *****\n\n");25632564for(i = 0; i < 0x100; i += 0x10)2565{2566for (j = 0 ; j < 4 ; j++)2567pci_read_config_dword(pdev, i + j * 4, tmp+j);25682569RTW_PRINT("%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",2570i, tmp[0] & 0xFF, (tmp[0] >> 8) & 0xFF, (tmp[0] >> 16) & 0xFF, (tmp[0] >> 24) & 0xFF,2571tmp[1] & 0xFF, (tmp[1] >> 8) & 0xFF, (tmp[1] >> 16) & 0xFF, (tmp[1] >> 24) & 0xFF,2572tmp[2] & 0xFF, (tmp[2] >> 8) & 0xFF, (tmp[2] >> 16) & 0xFF, (tmp[2] >> 24) & 0xFF,2573tmp[3] & 0xFF, (tmp[3] >> 8) & 0xFF, (tmp[3] >> 16) & 0xFF, (tmp[3] >> 24) & 0xFF);2574}25752576RTW_PRINT("\n***** PCI Host Device Configuration Space*****\n\n");25772578for(i = 0; i < 0x100; i += 0x10)2579{2580for (j = 0 ; j < 4 ; j++)2581pci_read_config_dword(bridge_pdev, i + j * 4, tmp+j);25822583RTW_PRINT("%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",2584i, tmp[0] & 0xFF, (tmp[0] >> 8) & 0xFF, (tmp[0] >> 16) & 0xFF, (tmp[0] >> 24) & 0xFF,2585tmp[1] & 0xFF, (tmp[1] >> 8) & 0xFF, (tmp[1] >> 16) & 0xFF, (tmp[1] >> 24) & 0xFF,2586tmp[2] & 0xFF, (tmp[2] >> 8) & 0xFF, (tmp[2] >> 16) & 0xFF, (tmp[2] >> 24) & 0xFF,2587tmp[3] & 0xFF, (tmp[3] >> 8) & 0xFF, (tmp[3] >> 16) & 0xFF, (tmp[3] >> 24) & 0xFF);2588}2589}2590#endif25912592static void _dump_mac_reg_for_power_switch(struct dvobj_priv *d,2593const char* caller, char* desc)2594{2595struct _ADAPTER *a;2596u8 v8;259725982599RTW_PRINT("%s: %s\n", caller, desc);2600RTW_PRINT("======= MAC REG =======\n");2601/* page 0/1 */2602_dump_mac_reg(d, 0x0, 0x200);2603_dump_mac_reg(d, 0x300, 0x400); /* also dump page 3 */26042605/* dump debug register */2606a = dvobj_get_primary_adapter(d);26072608#ifdef CONFIG_PCI_HCI2609_dump_pcie_cfg_space(d);26102611v8 = rtw_read8(a, 0xF6) | 0x01;2612rtw_write8(a, 0xF6, v8);2613RTW_PRINT("0xF6 = %02x\n", v8);26142615dump_dbg_val(a, 0x63);2616dump_dbg_val(a, 0x64);2617dump_dbg_val(a, 0x68);2618dump_dbg_val(a, 0x69);2619dump_dbg_val(a, 0x6a);2620dump_dbg_val(a, 0x6b);2621dump_dbg_val(a, 0x71);2622dump_dbg_val(a, 0x72);2623#endif2624}26252626static enum halmac_ret_status _power_switch(struct halmac_adapter *halmac,2627struct halmac_api *api,2628enum halmac_mac_power pwr)2629{2630enum halmac_ret_status status;2631char desc[80] = {0};263226332634rtw_sprintf(desc, 80, "before calling power %s",2635(pwr==HALMAC_MAC_POWER_ON)?"on":"off");2636_dump_mac_reg_for_power_switch((struct dvobj_priv *)halmac->drv_adapter,2637__FUNCTION__, desc);26382639status = api->halmac_mac_power_switch(halmac, pwr);2640RTW_PRINT("%s: status=%d\n", __FUNCTION__, status);26412642rtw_sprintf(desc, 80, "after calling power %s",2643(pwr==HALMAC_MAC_POWER_ON)?"on":"off");2644_dump_mac_reg_for_power_switch((struct dvobj_priv *)halmac->drv_adapter,2645__FUNCTION__, desc);26462647return status;2648}2649#else /* !RTW_HALMAC_DBG_POWER_SWITCH */2650#define _power_switch(mac, api, pwr) (api)->halmac_mac_power_switch(mac, pwr)2651#endif /* !RTW_HALMAC_DBG_POWER_SWITCH */26522653/*2654* Description:2655* Power on device hardware.2656* [Notice!] If device's power state is on before,2657* it would be power off first and turn on power again.2658*2659* Return:2660* 0 power on success2661* -1 power on fail2662* -2 power state unchange2663*/2664int rtw_halmac_poweron(struct dvobj_priv *d)2665{2666struct halmac_adapter *halmac;2667struct halmac_api *api;2668enum halmac_ret_status status;2669int err = -1;2670#if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B)2671struct _ADAPTER *a;2672u8 v8;2673u32 addr;26742675a = dvobj_get_primary_adapter(d);2676#endif26772678halmac = dvobj_to_halmac(d);2679if (!halmac)2680goto out;26812682api = HALMAC_GET_API(halmac);26832684status = api->halmac_pre_init_system_cfg(halmac);2685if (status != HALMAC_RET_SUCCESS)2686goto out;26872688#ifdef CONFIG_SDIO_HCI2689status = api->halmac_sdio_cmd53_4byte(halmac, HALMAC_SDIO_CMD53_4BYTE_MODE_RW);2690if (status != HALMAC_RET_SUCCESS)2691goto out;2692#endif /* CONFIG_SDIO_HCI */26932694#if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B)2695addr = 0x3F3;2696v8 = rtw_read8(a, addr);2697RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);2698/* are we in pcie debug mode? */2699if (!(v8 & BIT(2))) {2700RTW_PRINT("%s: Enable pcie debug mode\n", __FUNCTION__);2701v8 |= BIT(2);2702v8 = rtw_write8(a, addr, v8);2703}2704#endif27052706status = _power_switch(halmac, api, HALMAC_MAC_POWER_ON);2707if (HALMAC_RET_PWR_UNCHANGE == status) {27082709#if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B)2710addr = 0x3F3;2711v8 = rtw_read8(a, addr);2712RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);27132714/* are we in pcie debug mode? */2715if (!(v8 & BIT(2))) {2716RTW_PRINT("%s: Enable pcie debug mode\n", __FUNCTION__);2717v8 |= BIT(2);2718v8 = rtw_write8(a, addr, v8);2719} else if (v8 & BIT(0)) {2720/* DMA stuck */2721addr = 0x1350;2722v8 = rtw_read8(a, addr);2723RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);2724RTW_PRINT("%s: recover DMA stuck\n", __FUNCTION__);2725v8 |= BIT(6);2726v8 = rtw_write8(a, addr, v8);2727RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8);2728}2729#endif2730/*2731* Work around for warm reboot but device not power off,2732* but it would also fall into this case when auto power on is enabled.2733*/2734_power_switch(halmac, api, HALMAC_MAC_POWER_OFF);2735status = _power_switch(halmac, api, HALMAC_MAC_POWER_ON);2736RTW_WARN("%s: Power state abnormal, try to recover...%s\n",2737__FUNCTION__, (HALMAC_RET_SUCCESS == status)?"OK":"FAIL!");2738}2739if (HALMAC_RET_SUCCESS != status) {2740if (HALMAC_RET_PWR_UNCHANGE == status)2741err = -2;2742goto out;2743}27442745status = api->halmac_init_system_cfg(halmac);2746if (status != HALMAC_RET_SUCCESS)2747goto out;27482749err = 0;2750out:2751return err;2752}27532754/*2755* Description:2756* Power off device hardware.2757*2758* Return:2759* 0 Power off success2760* -1 Power off fail2761*/2762int rtw_halmac_poweroff(struct dvobj_priv *d)2763{2764struct halmac_adapter *halmac;2765struct halmac_api *api;2766enum halmac_ret_status status;2767int err = -1;276827692770halmac = dvobj_to_halmac(d);2771if (!halmac)2772goto out;27732774api = HALMAC_GET_API(halmac);27752776status = _power_switch(halmac, api, HALMAC_MAC_POWER_OFF);2777if ((HALMAC_RET_SUCCESS != status)2778&& (HALMAC_RET_PWR_UNCHANGE != status))2779goto out;27802781err = 0;2782out:2783return err;2784}27852786#ifdef CONFIG_SUPPORT_TRX_SHARED2787static inline enum halmac_rx_fifo_expanding_mode _trx_share_mode_drv2halmac(u8 trx_share_mode)2788{2789if (0 == trx_share_mode)2790return HALMAC_RX_FIFO_EXPANDING_MODE_DISABLE;2791else if (1 == trx_share_mode)2792return HALMAC_RX_FIFO_EXPANDING_MODE_1_BLOCK;2793else if (2 == trx_share_mode)2794return HALMAC_RX_FIFO_EXPANDING_MODE_2_BLOCK;2795else if (3 == trx_share_mode)2796return HALMAC_RX_FIFO_EXPANDING_MODE_3_BLOCK;2797else2798return HALMAC_RX_FIFO_EXPANDING_MODE_DISABLE;2799}28002801static enum halmac_rx_fifo_expanding_mode _rtw_get_trx_share_mode(struct _ADAPTER *adapter)2802{2803struct registry_priv *registry_par = &adapter->registrypriv;28042805return _trx_share_mode_drv2halmac(registry_par->trx_share_mode);2806}28072808void dump_trx_share_mode(void *sel, struct _ADAPTER *adapter)2809{2810struct registry_priv *registry_par = &adapter->registrypriv;2811u8 mode = _trx_share_mode_drv2halmac(registry_par->trx_share_mode);28122813if (HALMAC_RX_FIFO_EXPANDING_MODE_1_BLOCK == mode)2814RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_1");2815else if (HALMAC_RX_FIFO_EXPANDING_MODE_2_BLOCK == mode)2816RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_2");2817else if (HALMAC_RX_FIFO_EXPANDING_MODE_3_BLOCK == mode)2818RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_3");2819else2820RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "DISABLE");2821}2822#endif28232824static enum halmac_drv_rsvd_pg_num _rsvd_page_num_drv2halmac(u16 num)2825{2826if (num <= 8)2827return HALMAC_RSVD_PG_NUM8;2828if (num <= 16)2829return HALMAC_RSVD_PG_NUM16;2830if (num <= 24)2831return HALMAC_RSVD_PG_NUM24;2832if (num <= 32)2833return HALMAC_RSVD_PG_NUM32;2834if (num <= 64)2835return HALMAC_RSVD_PG_NUM64;2836if (num <= 128)2837return HALMAC_RSVD_PG_NUM128;28382839if (num > 256)2840RTW_WARN("%s: Fail to allocate RSVD page(%d)!!"2841" The MAX RSVD page number is 256...\n",2842__FUNCTION__, num);28432844return HALMAC_RSVD_PG_NUM256;2845}28462847static u16 _rsvd_page_num_halmac2drv(enum halmac_drv_rsvd_pg_num rsvd_page_number)2848{2849u16 num = 0;285028512852switch (rsvd_page_number) {2853case HALMAC_RSVD_PG_NUM8:2854num = 8;2855break;28562857case HALMAC_RSVD_PG_NUM16:2858num = 16;2859break;28602861case HALMAC_RSVD_PG_NUM24:2862num = 24;2863break;28642865case HALMAC_RSVD_PG_NUM32:2866num = 32;2867break;28682869case HALMAC_RSVD_PG_NUM64:2870num = 64;2871break;28722873case HALMAC_RSVD_PG_NUM128:2874num = 128;2875break;28762877case HALMAC_RSVD_PG_NUM256:2878num = 256;2879break;2880}28812882return num;2883}28842885static enum halmac_trx_mode _choose_trx_mode(struct dvobj_priv *d)2886{2887PADAPTER p;288828892890p = dvobj_get_primary_adapter(d);28912892if (p->registrypriv.wifi_spec)2893return HALMAC_TRX_MODE_WMM;28942895#ifdef CONFIG_SUPPORT_TRX_SHARED2896if (_rtw_get_trx_share_mode(p))2897return HALMAC_TRX_MODE_TRXSHARE;2898#endif28992900return HALMAC_TRX_MODE_NORMAL;2901}29022903static inline enum halmac_rf_type _rf_type_drv2halmac(enum rf_type rf_drv)2904{2905enum halmac_rf_type rf_mac;290629072908switch (rf_drv) {2909case RF_1T1R:2910rf_mac = HALMAC_RF_1T1R;2911break;2912case RF_1T2R:2913rf_mac = HALMAC_RF_1T2R;2914break;2915case RF_2T2R:2916rf_mac = HALMAC_RF_2T2R;2917break;2918case RF_2T3R:2919rf_mac = HALMAC_RF_2T3R;2920break;2921case RF_2T4R:2922rf_mac = HALMAC_RF_2T4R;2923break;2924case RF_3T3R:2925rf_mac = HALMAC_RF_3T3R;2926break;2927case RF_3T4R:2928rf_mac = HALMAC_RF_3T4R;2929break;2930case RF_4T4R:2931rf_mac = HALMAC_RF_4T4R;2932break;2933default:2934rf_mac = HALMAC_RF_MAX_TYPE;2935RTW_ERR("%s: Invalid RF type(0x%x)!\n", __FUNCTION__, rf_drv);2936break;2937}29382939return rf_mac;2940}29412942static inline enum rf_type _rf_type_halmac2drv(enum halmac_rf_type rf_mac)2943{2944enum rf_type rf_drv;294529462947switch (rf_mac) {2948case HALMAC_RF_1T2R:2949rf_drv = RF_1T2R;2950break;2951case HALMAC_RF_2T4R:2952rf_drv = RF_2T4R;2953break;2954case HALMAC_RF_2T2R:2955case HALMAC_RF_2T2R_GREEN:2956rf_drv = RF_2T2R;2957break;2958case HALMAC_RF_2T3R:2959rf_drv = RF_2T3R;2960break;2961case HALMAC_RF_1T1R:2962rf_drv = RF_1T1R;2963break;2964case HALMAC_RF_3T3R:2965rf_drv = RF_3T3R;2966break;2967case HALMAC_RF_3T4R:2968rf_drv = RF_3T4R;2969break;2970case HALMAC_RF_4T4R:2971rf_drv = RF_4T4R;2972break;2973default:2974rf_drv = RF_TYPE_MAX;2975RTW_ERR("%s: Invalid RF type(0x%x)!\n", __FUNCTION__, rf_mac);2976break;2977}29782979return rf_drv;2980}29812982static enum odm_cut_version _cut_version_drv2phydm(2983enum tag_HAL_Cut_Version_Definition cut_drv)2984{2985enum odm_cut_version cut_phydm = ODM_CUT_A;2986u32 diff;298729882989if (cut_drv > K_CUT_VERSION)2990RTW_WARN("%s: unknown cut_ver=%d !!\n", __FUNCTION__, cut_drv);29912992diff = cut_drv - A_CUT_VERSION;2993cut_phydm += diff;29942995return cut_phydm;2996}29972998static int _send_general_info_by_reg(struct dvobj_priv *d,2999struct halmac_general_info *info)3000{3001struct _ADAPTER *a;3002struct hal_com_data *hal;3003enum tag_HAL_Cut_Version_Definition cut_drv;3004enum rf_type rftype;3005enum odm_cut_version cut_phydm;3006u8 h2c[RTW_HALMAC_H2C_MAX_SIZE] = {0};300730083009a = dvobj_get_primary_adapter(d);3010hal = GET_HAL_DATA(a);3011rftype = _rf_type_halmac2drv(info->rf_type);3012cut_drv = GET_CVID_CUT_VERSION(hal->version_id);3013cut_phydm = _cut_version_drv2phydm(cut_drv);30143015#define CLASS_GENERAL_INFO_REG 0x023016#define CMD_ID_GENERAL_INFO_REG 0x0C3017#define GENERAL_INFO_REG_SET_CMD_ID(buf, v) SET_BITS_TO_LE_4BYTE(buf, 0, 5, v)3018#define GENERAL_INFO_REG_SET_CLASS(buf, v) SET_BITS_TO_LE_4BYTE(buf, 5, 3, v)3019#define GENERAL_INFO_REG_SET_RFE_TYPE(buf, v) SET_BITS_TO_LE_4BYTE(buf, 8, 8, v)3020#define GENERAL_INFO_REG_SET_RF_TYPE(buf, v) SET_BITS_TO_LE_4BYTE(buf, 16, 8, v)3021#define GENERAL_INFO_REG_SET_CUT_VERSION(buf, v) SET_BITS_TO_LE_4BYTE(buf, 24, 8, v)3022#define GENERAL_INFO_REG_SET_RX_ANT_STATUS(buf, v) SET_BITS_TO_LE_1BYTE(buf+4, 0, 4, v)3023#define GENERAL_INFO_REG_SET_TX_ANT_STATUS(buf, v) SET_BITS_TO_LE_1BYTE(buf+4, 4, 4, v)30243025GENERAL_INFO_REG_SET_CMD_ID(h2c, CMD_ID_GENERAL_INFO_REG);3026GENERAL_INFO_REG_SET_CLASS(h2c, CLASS_GENERAL_INFO_REG);3027GENERAL_INFO_REG_SET_RFE_TYPE(h2c, info->rfe_type);3028GENERAL_INFO_REG_SET_RF_TYPE(h2c, rftype);3029GENERAL_INFO_REG_SET_CUT_VERSION(h2c, cut_phydm);3030GENERAL_INFO_REG_SET_RX_ANT_STATUS(h2c, info->rx_ant_status);3031GENERAL_INFO_REG_SET_TX_ANT_STATUS(h2c, info->tx_ant_status);30323033return rtw_halmac_send_h2c(d, h2c);3034}30353036static int _send_general_info(struct dvobj_priv *d)3037{3038struct _ADAPTER *adapter;3039struct hal_com_data *hal;3040struct halmac_adapter *halmac;3041struct halmac_api *api;3042struct halmac_general_info info;3043enum halmac_ret_status status;3044enum rf_type rf = RF_1T1R;3045enum bb_path txpath = BB_PATH_A;3046enum bb_path rxpath = BB_PATH_A;3047int err;304830493050adapter = dvobj_get_primary_adapter(d);3051hal = GET_HAL_DATA(adapter);3052halmac = dvobj_to_halmac(d);3053if (!halmac)3054return -1;3055api = HALMAC_GET_API(halmac);30563057_rtw_memset(&info, 0, sizeof(info));3058info.rfe_type = (u8)hal->rfe_type;3059rtw_hal_get_trx_path(d, &rf, &txpath, &rxpath);3060info.rf_type = _rf_type_drv2halmac(rf);3061info.tx_ant_status = (u8)txpath;3062info.rx_ant_status = (u8)rxpath;3063info.ext_pa = 0; /* 2.4G or 5G? format not known */3064info.package_type = hal->PackageType;3065info.mp_mode = adapter->registrypriv.mp_mode;30663067status = api->halmac_send_general_info(halmac, &info);3068switch (status) {3069case HALMAC_RET_SUCCESS:3070break;3071case HALMAC_RET_NO_DLFW:3072RTW_WARN("%s: halmac_send_general_info() fail because fw not dl!\n",3073__FUNCTION__);3074/* go through */3075default:3076return -1;3077}30783079err = _send_general_info_by_reg(d, &info);3080if (err) {3081RTW_ERR("%s: Fail to send general info by register!\n",3082__FUNCTION__);3083return -1;3084}30853086return 0;3087}30883089static int _cfg_drv_rsvd_pg_num(struct dvobj_priv *d)3090{3091struct _ADAPTER *a;3092struct hal_com_data *hal;3093struct halmac_adapter *halmac;3094struct halmac_api *api;3095enum halmac_drv_rsvd_pg_num rsvd_page_number;3096enum halmac_ret_status status;3097u16 drv_rsvd_num;3098int ret = 0;309931003101a = dvobj_get_primary_adapter(d);3102hal = GET_HAL_DATA(a);3103halmac = dvobj_to_halmac(d);3104api = HALMAC_GET_API(halmac);31053106drv_rsvd_num = rtw_hal_get_rsvd_page_num(a);3107rsvd_page_number = _rsvd_page_num_drv2halmac(drv_rsvd_num);3108status = api->halmac_cfg_drv_rsvd_pg_num(halmac, rsvd_page_number);3109if (status != HALMAC_RET_SUCCESS) {3110ret = -1;3111goto exit;3112}3113hal->drv_rsvd_page_number = _rsvd_page_num_halmac2drv(rsvd_page_number);31143115exit:3116#ifndef DBG_RSVD_PAGE_CFG3117if (drv_rsvd_num != _rsvd_page_num_halmac2drv(rsvd_page_number))3118#endif3119RTW_INFO("%s: request %d pages => halmac %d pages %s\n"3120, __FUNCTION__, drv_rsvd_num, _rsvd_page_num_halmac2drv(rsvd_page_number)3121, ret ? "fail" : "success");31223123return ret;3124}31253126static void _debug_dlfw_fail(struct dvobj_priv *d)3127{3128struct _ADAPTER *a;3129u32 addr;3130u32 v32, i, n;313131323133a = dvobj_get_primary_adapter(d);31343135/* read 0x80[15:0], 0x10F8[31:0] once */3136addr = 0x80;3137v32 = rtw_read16(a, addr);3138RTW_PRINT("%s: 0x%X = 0x%04x\n", __FUNCTION__, addr, v32);31393140addr = 0x10F8;3141v32 = rtw_read32(a, addr);3142RTW_PRINT("%s: 0x%X = 0x%08x\n", __FUNCTION__, addr, v32);31433144/* read 0x10FC[31:0], 5 times */3145addr = 0x10FC;3146n = 5;3147for (i = 0; i < n; i++) {3148v32 = rtw_read32(a, addr);3149RTW_PRINT("%s: 0x%X = 0x%08x (%u/%u)\n",3150__FUNCTION__, addr, v32, i, n);3151}31523153/*3154* write 0x3A[7:0]=0x28 and 0xF6[7:0]=0x013155* and then read 0xC0[31:0] 5 times3156*/3157addr = 0x3A;3158v32 = 0x28;3159rtw_write8(a, addr, (u8)v32);3160v32 = rtw_read8(a, addr);3161RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v32);31623163addr = 0xF6;3164v32 = 0x1;3165rtw_write8(a, addr, (u8)v32);3166v32 = rtw_read8(a, addr);3167RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v32);31683169addr = 0xC0;3170n = 5;3171for (i = 0; i < n; i++) {3172v32 = rtw_read32(a, addr);3173RTW_PRINT("%s: 0x%X = 0x%08x (%u/%u)\n",3174__FUNCTION__, addr, v32, i, n);3175}31763177mac_reg_dump(NULL, a);3178#ifdef CONFIG_SDIO_HCI3179RTW_PRINT("======= SDIO Local REG =======\n");3180sdio_local_reg_dump(NULL, a);3181RTW_PRINT("======= SDIO CCCR REG =======\n");3182sd_f0_reg_dump(NULL, a);3183#endif /* CONFIG_SDIO_HCI */31843185/* read 0x80 after 10 secs */3186rtw_msleep_os(10000);3187addr = 0x80;3188v32 = rtw_read16(a, addr);3189RTW_PRINT("%s: 0x%X = 0x%04x (after 10 secs)\n",3190__FUNCTION__, addr, v32);3191}31923193static enum halmac_ret_status _enter_cpu_sleep_mode(struct dvobj_priv *d)3194{3195struct hal_com_data *hal;3196struct halmac_adapter *mac;3197struct halmac_api *api;319831993200hal = GET_HAL_DATA(dvobj_get_primary_adapter(d));3201mac = dvobj_to_halmac(d);3202api = HALMAC_GET_API(mac);32033204#ifdef CONFIG_RTL8822B3205/* Support after firmware version 21 */3206if (hal->firmware_version < 21)3207return HALMAC_RET_NOT_SUPPORT;3208#elif defined(CONFIG_RTL8821C)3209/* Support after firmware version 13.6 or 16 */3210if (hal->firmware_version == 13) {3211if (hal->firmware_sub_version < 6)3212return HALMAC_RET_NOT_SUPPORT;3213} else if (hal->firmware_version < 16) {3214return HALMAC_RET_NOT_SUPPORT;3215}3216#endif32173218return api->halmac_enter_cpu_sleep_mode(mac);3219}32203221/*3222* _cpu_sleep() - Let IC CPU enter sleep mode3223* @d: struct dvobj_priv*3224* @timeout: time limit of wait, unit is ms3225* 0 for no limit3226*3227* Return 0 for CPU in sleep mode, otherwise fail to enter sleep mode.3228* Error codes definition are as follow:3229* -1 HALMAC enter sleep return fail3230* -2 HALMAC get CPU mode return fail3231* -110 timeout3232*/3233static int _cpu_sleep(struct dvobj_priv *d, u32 timeout)3234{3235struct halmac_adapter *mac;3236struct halmac_api *api;3237enum halmac_ret_status status;3238enum halmac_wlcpu_mode mode = HALMAC_WLCPU_UNDEFINE;3239systime start_t;3240s32 period = 0;3241u32 cnt = 0;3242int err = 0;324332443245mac = dvobj_to_halmac(d);3246api = HALMAC_GET_API(mac);32473248start_t = rtw_get_current_time();32493250status = _enter_cpu_sleep_mode(d);3251if (status != HALMAC_RET_SUCCESS) {3252if (status != HALMAC_RET_NOT_SUPPORT)3253err = -1;3254goto exit;3255}32563257do {3258cnt++;32593260mode = HALMAC_WLCPU_UNDEFINE;3261status = api->halmac_get_cpu_mode(mac, &mode);32623263period = rtw_get_passing_time_ms(start_t);32643265if (status != HALMAC_RET_SUCCESS) {3266err = -2;3267break;3268}3269if (mode == HALMAC_WLCPU_SLEEP)3270break;3271if (period > timeout) {3272err = -110;3273break;3274}32753276rtw_msleep_os(1);3277} while (1);32783279exit:3280if (err)3281RTW_ERR("%s: Fail to enter sleep mode! (%d, %d)\n",3282__FUNCTION__, status, mode);32833284RTW_INFO("%s: Cost %dms to polling %u times. (err=%d)\n",3285__FUNCTION__, period, cnt, err);32863287return err;3288}32893290static void _init_trx_cfg_drv(struct dvobj_priv *d)3291{3292#ifdef CONFIG_PCI_HCI3293rtw_hal_irp_reset(dvobj_get_primary_adapter(d));3294#endif3295}32963297/*3298* Description:3299* Downlaod Firmware Flow3300*3301* Parameters:3302* d pointer of struct dvobj_priv3303* fw firmware array3304* fwsize firmware size3305* re_dl re-download firmware or not3306* 0: run in init hal flow, not re-download3307* 1: it is a stand alone operation, not in init hal flow3308*3309* Return:3310* 0 Success3311* others Fail3312*/3313static int download_fw(struct dvobj_priv *d, u8 *fw, u32 fwsize, u8 re_dl)3314{3315PHAL_DATA_TYPE hal;3316struct halmac_adapter *mac;3317struct halmac_api *api;3318struct halmac_fw_version fw_vesion;3319enum halmac_ret_status status;3320int err = 0;332133223323hal = GET_HAL_DATA(dvobj_get_primary_adapter(d));3324mac = dvobj_to_halmac(d);3325api = HALMAC_GET_API(mac);33263327if ((!fw) || (!fwsize))3328return -1;33293330/* 1. Driver Stop Tx */3331/* ToDo */33323333/* 2. Driver Check Tx FIFO is empty */3334err = rtw_halmac_txfifo_wait_empty(d, 2000); /* wait 2s */3335if (err) {3336err = -1;3337goto resume_tx;3338}33393340/* 3. Config MAX download size */3341/*3342* Already done in rtw_halmac_init_adapter() or3343* somewhere calling rtw_halmac_set_max_dl_fw_size().3344*/33453346if (re_dl) {3347/* 4. Enter IC CPU sleep mode */3348err = _cpu_sleep(d, 2000);3349if (err) {3350RTW_ERR("%s: IC CPU fail to enter sleep mode!(%d)\n",3351__FUNCTION__, err);3352/* skip this error */3353err = 0;3354}3355}33563357/* 5. Download Firmware */3358status = api->halmac_download_firmware(mac, fw, fwsize);3359if (status != HALMAC_RET_SUCCESS) {3360RTW_ERR("%s: download firmware FAIL! status=0x%02x\n",3361__FUNCTION__, status);3362_debug_dlfw_fail(d);3363err = -1;3364goto resume_tx;3365}33663367/* 5.1. (Driver) Reset driver variables if needed */3368hal->LastHMEBoxNum = 0;33693370/* 5.2. (Driver) Get FW version */3371status = api->halmac_get_fw_version(mac, &fw_vesion);3372if (status == HALMAC_RET_SUCCESS) {3373hal->firmware_version = fw_vesion.version;3374hal->firmware_sub_version = fw_vesion.sub_version;3375hal->firmware_size = fwsize;3376}33773378resume_tx:3379/* 6. Driver resume TX if needed */3380/* ToDo */33813382if (err)3383goto exit;33843385if (re_dl) {3386enum halmac_trx_mode mode;33873388/* 7. Change reserved page size */3389err = _cfg_drv_rsvd_pg_num(d);3390if (err)3391return -1;33923393/* 8. Init TRX Configuration */3394mode = _choose_trx_mode(d);3395status = api->halmac_init_trx_cfg(mac, mode);3396if (HALMAC_RET_SUCCESS != status)3397return -1;3398_init_trx_cfg_drv(d);33993400/* 9. Config RX Aggregation */3401err = rtw_halmac_rx_agg_switch(d, _TRUE);3402if (err)3403return -1;34043405/* 10. Send General Info */3406err = _send_general_info(d);3407if (err)3408return -1;3409}34103411exit:3412return err;3413}34143415static int init_mac_flow(struct dvobj_priv *d)3416{3417PADAPTER p;3418struct hal_com_data *hal;3419struct halmac_adapter *halmac;3420struct halmac_api *api;3421enum halmac_drv_rsvd_pg_num rsvd_page_number;3422union halmac_wlan_addr hwa;3423enum halmac_trx_mode trx_mode;3424enum halmac_ret_status status;3425u8 drv_rsvd_num;3426u8 nettype;3427int err, err_ret = -1;342834293430p = dvobj_get_primary_adapter(d);3431hal = GET_HAL_DATA(p);3432halmac = dvobj_to_halmac(d);3433api = HALMAC_GET_API(halmac);34343435#ifdef CONFIG_SUPPORT_TRX_SHARED3436status = api->halmac_cfg_rxff_expand_mode(halmac,3437_rtw_get_trx_share_mode(p));3438if (status != HALMAC_RET_SUCCESS)3439goto out;3440#endif34413442#ifdef DBG_LA_MODE3443if (dvobj_to_regsty(d)->la_mode_en) {3444status = api->halmac_cfg_la_mode(halmac, HALMAC_LA_MODE_PARTIAL);3445if (status != HALMAC_RET_SUCCESS) {3446RTW_ERR("%s: Fail to enable LA mode!\n", __FUNCTION__);3447goto out;3448}3449RTW_PRINT("%s: Enable LA mode OK.\n", __FUNCTION__);3450}3451#endif34523453err = _cfg_drv_rsvd_pg_num(d);3454if (err)3455goto out;34563457#ifdef CONFIG_USB_HCI3458status = api->halmac_set_bulkout_num(halmac, d->RtNumOutPipes);3459if (status != HALMAC_RET_SUCCESS)3460goto out;3461#endif /* CONFIG_USB_HCI */34623463trx_mode = _choose_trx_mode(d);3464status = api->halmac_init_mac_cfg(halmac, trx_mode);3465if (status != HALMAC_RET_SUCCESS)3466goto out;3467_init_trx_cfg_drv(d);34683469err = rtw_halmac_rx_agg_switch(d, _TRUE);3470if (err)3471goto out;34723473nettype = dvobj_to_regsty(d)->wireless_mode;3474if (is_supported_vht(nettype) == _TRUE)3475status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_AC);3476else if (is_supported_ht(nettype) == _TRUE)3477status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_N);3478else if (IsSupportedTxOFDM(nettype) == _TRUE)3479status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_G);3480else3481status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_B);3482if (status != HALMAC_RET_SUCCESS)3483goto out;34843485err_ret = 0;3486out:3487return err_ret;3488}34893490static int _drv_enable_trx(struct dvobj_priv *d)3491{3492struct _ADAPTER *adapter;3493u32 status;349434953496adapter = dvobj_get_primary_adapter(d);3497if (adapter->bup == _FALSE) {3498#ifdef CONFIG_NEW_NETDEV_HDL3499status = rtw_mi_start_drv_threads(adapter);3500#else3501status = rtw_start_drv_threads(adapter);3502#endif3503if (status == _FAIL) {3504RTW_ERR("%s: Start threads Failed!\n", __FUNCTION__);3505return -1;3506}3507}35083509rtw_intf_start(adapter);35103511return 0;3512}35133514/*3515* Notices:3516* Make sure following information3517* 1. GET_HAL_RFPATH3518* 2. GET_HAL_DATA(dvobj_get_primary_adapter(d))->rfe_type3519* 3. GET_HAL_DATA(dvobj_get_primary_adapter(d))->PackageType3520* 4. dvobj_get_primary_adapter(d)->registrypriv.mp_mode3521* are all ready before calling this function.3522*/3523static int _halmac_init_hal(struct dvobj_priv *d, u8 *fw, u32 fwsize)3524{3525PADAPTER adapter;3526struct halmac_adapter *halmac;3527struct halmac_api *api;3528enum halmac_ret_status status;3529u32 ok;3530u8 fw_ok = _FALSE;3531int err, err_ret = -1;353235333534adapter = dvobj_get_primary_adapter(d);3535halmac = dvobj_to_halmac(d);3536if (!halmac)3537goto out;3538api = HALMAC_GET_API(halmac);35393540/* StatePowerOff */35413542/* SKIP: halmac_init_adapter (Already done before) */35433544/* halmac_pre_Init_system_cfg */3545/* halmac_mac_power_switch(on) */3546/* halmac_Init_system_cfg */3547ok = rtw_hal_power_on(adapter);3548if (_FAIL == ok)3549goto out;35503551/* StatePowerOn */35523553/* DownloadFW */3554if (fw && fwsize) {3555err = download_fw(d, fw, fwsize, 0);3556if (err)3557goto out;3558fw_ok = _TRUE;3559}35603561/* InitMACFlow */3562err = init_mac_flow(d);3563if (err)3564goto out;35653566/* Driver insert flow: Enable TR/RX */3567err = _drv_enable_trx(d);3568if (err)3569goto out;35703571/* halmac_send_general_info */3572if (_TRUE == fw_ok) {3573err = _send_general_info(d);3574if (err)3575goto out;3576}35773578/* Init Phy parameter-MAC */3579ok = rtw_hal_init_mac_register(adapter);3580if (_FALSE == ok)3581goto out;35823583/* StateMacInitialized */35843585/* halmac_cfg_drv_info */3586err = rtw_halmac_config_rx_info(d, HALMAC_DRV_INFO_PHY_STATUS);3587if (err)3588goto out;35893590/* halmac_set_hw_value(HALMAC_HW_EN_BB_RF) */3591/* Init BB, RF */3592ok = rtw_hal_init_phy(adapter);3593if (_FALSE == ok)3594goto out;35953596status = api->halmac_init_interface_cfg(halmac);3597if (status != HALMAC_RET_SUCCESS)3598goto out;35993600/* SKIP: halmac_verify_platform_api */3601/* SKIP: halmac_h2c_lb */36023603/* StateRxIdle */36043605err_ret = 0;3606out:3607return err_ret;3608}36093610int rtw_halmac_init_hal(struct dvobj_priv *d)3611{3612return _halmac_init_hal(d, NULL, 0);3613}36143615/*3616* Notices:3617* Make sure following information3618* 1. GET_HAL_RFPATH3619* 2. GET_HAL_DATA(dvobj_get_primary_adapter(d))->rfe_type3620* 3. GET_HAL_DATA(dvobj_get_primary_adapter(d))->PackageType3621* 4. dvobj_get_primary_adapter(d)->registrypriv.mp_mode3622* are all ready before calling this function.3623*/3624int rtw_halmac_init_hal_fw(struct dvobj_priv *d, u8 *fw, u32 fwsize)3625{3626return _halmac_init_hal(d, fw, fwsize);3627}36283629/*3630* Notices:3631* Make sure following information3632* 1. GET_HAL_RFPATH3633* 2. GET_HAL_DATA(dvobj_get_primary_adapter(d))->rfe_type3634* 3. GET_HAL_DATA(dvobj_get_primary_adapter(d))->PackageType3635* 4. dvobj_get_primary_adapter(d)->registrypriv.mp_mode3636* are all ready before calling this function.3637*/3638int rtw_halmac_init_hal_fw_file(struct dvobj_priv *d, u8 *fwpath)3639{3640u8 *fw = NULL;3641u32 fwmaxsize = 0, size = 0;3642int err = 0;364336443645err = rtw_halmac_get_fw_max_size(d, &fwmaxsize);3646if (err) {3647RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err);3648return -1;3649}36503651fw = rtw_zmalloc(fwmaxsize);3652if (!fw)3653return -1;36543655size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize);3656if (!size) {3657err = -1;3658goto exit;3659}36603661err = _halmac_init_hal(d, fw, size);36623663exit:3664rtw_mfree(fw, fwmaxsize);3665/*fw = NULL;*/36663667return err;3668}36693670int rtw_halmac_deinit_hal(struct dvobj_priv *d)3671{3672PADAPTER adapter;3673struct halmac_adapter *halmac;3674struct halmac_api *api;3675enum halmac_ret_status status;3676int err = -1;367736783679adapter = dvobj_get_primary_adapter(d);3680halmac = dvobj_to_halmac(d);3681if (!halmac)3682goto out;3683api = HALMAC_GET_API(halmac);36843685status = api->halmac_deinit_interface_cfg(halmac);3686if (status != HALMAC_RET_SUCCESS)3687goto out;36883689rtw_hal_power_off(adapter);36903691err = 0;3692out:3693return err;3694}36953696int rtw_halmac_self_verify(struct dvobj_priv *d)3697{3698struct halmac_adapter *mac;3699struct halmac_api *api;3700enum halmac_ret_status status;3701int err = -1;370237033704mac = dvobj_to_halmac(d);3705api = HALMAC_GET_API(mac);37063707status = api->halmac_verify_platform_api(mac);3708if (status != HALMAC_RET_SUCCESS)3709goto out;37103711status = api->halmac_h2c_lb(mac);3712if (status != HALMAC_RET_SUCCESS)3713goto out;37143715err = 0;3716out:3717return err;3718}37193720static u8 rtw_halmac_txfifo_is_empty(struct dvobj_priv *d)3721{3722struct halmac_adapter *mac;3723struct halmac_api *api;3724enum halmac_ret_status status;3725u32 chk_num = 10;3726u8 rst = _FALSE;372737283729mac = dvobj_to_halmac(d);3730api = HALMAC_GET_API(mac);37313732status = api->halmac_txfifo_is_empty(mac, chk_num);3733if (status == HALMAC_RET_SUCCESS)3734rst = _TRUE;37353736return rst;3737}37383739/**3740* rtw_halmac_txfifo_wait_empty() - Wait TX FIFO to be emtpy3741* @d: struct dvobj_priv*3742* @timeout: time limit of wait, unit is ms3743* 0 for no limit3744*3745* Wait TX FIFO to be emtpy.3746*3747* Return 0 for TX FIFO is empty, otherwise not empty.3748*/3749int rtw_halmac_txfifo_wait_empty(struct dvobj_priv *d, u32 timeout)3750{3751struct _ADAPTER *a;3752u8 empty = _FALSE;3753u32 cnt = 0;3754systime start_time = 0;3755u32 pass_time; /* ms */375637573758a = dvobj_get_primary_adapter(d);3759start_time = rtw_get_current_time();37603761do {3762cnt++;3763empty = rtw_halmac_txfifo_is_empty(d);3764if (empty == _TRUE)3765break;37663767if (timeout) {3768pass_time = rtw_get_passing_time_ms(start_time);3769if (pass_time > timeout)3770break;3771}3772if (RTW_CANNOT_IO(a)) {3773RTW_WARN("%s: Interrupted by I/O forbiden!\n", __FUNCTION__);3774break;3775}37763777rtw_msleep_os(2);3778} while (1);37793780if (empty == _FALSE) {3781#ifdef CONFIG_RTW_DEBUG3782u16 dbg_reg[] = {0x210, 0x230, 0x234, 0x238, 0x23C, 0x240,37830x418, 0x10FC, 0x10F8, 0x11F4, 0x11F8};3784u8 i;3785u32 val;37863787if (!RTW_CANNOT_IO(a)) {3788for (i = 0; i < ARRAY_SIZE(dbg_reg); i++) {3789val = rtw_read32(a, dbg_reg[i]);3790RTW_ERR("REG_%X:0x%08x\n", dbg_reg[i], val);3791}3792}3793#endif /* CONFIG_RTW_DEBUG */37943795RTW_ERR("%s: Fail to wait txfifo empty!(cnt=%d)\n",3796__FUNCTION__, cnt);3797return -1;3798}37993800return 0;3801}38023803static enum halmac_dlfw_mem _fw_mem_drv2halmac(enum fw_mem mem, u8 tx_stop)3804{3805enum halmac_dlfw_mem mem_halmac = HALMAC_DLFW_MEM_UNDEFINE;380638073808switch (mem) {3809case FW_EMEM:3810if (tx_stop == _FALSE)3811mem_halmac = HALMAC_DLFW_MEM_EMEM_RSVD_PG;3812else3813mem_halmac = HALMAC_DLFW_MEM_EMEM;3814break;38153816case FW_IMEM:3817case FW_DMEM:3818mem_halmac = HALMAC_DLFW_MEM_UNDEFINE;3819break;3820}38213822return mem_halmac;3823}38243825int rtw_halmac_dlfw_mem(struct dvobj_priv *d, u8 *fw, u32 fwsize, enum fw_mem mem)3826{3827struct halmac_adapter *mac;3828struct halmac_api *api;3829enum halmac_ret_status status;3830enum halmac_dlfw_mem dlfw_mem;3831u8 tx_stop = _FALSE;3832u32 chk_timeout = 2000; /* unit: ms */3833int err = 0;383438353836mac = dvobj_to_halmac(d);3837api = HALMAC_GET_API(mac);38383839if ((!fw) || (!fwsize))3840return -1;38413842#ifndef RTW_HALMAC_DLFW_MEM_NO_STOP_TX3843/* 1. Driver Stop Tx */3844/* ToDo */38453846/* 2. Driver Check Tx FIFO is empty */3847err = rtw_halmac_txfifo_wait_empty(d, chk_timeout);3848if (err)3849tx_stop = _FALSE;3850else3851tx_stop = _TRUE;3852#endif /* !RTW_HALMAC_DLFW_MEM_NO_STOP_TX */38533854/* 3. Download Firmware MEM */3855dlfw_mem = _fw_mem_drv2halmac(mem, tx_stop);3856if (dlfw_mem == HALMAC_DLFW_MEM_UNDEFINE) {3857err = -1;3858goto resume_tx;3859}3860status = api->halmac_free_download_firmware(mac, dlfw_mem, fw, fwsize);3861if (status != HALMAC_RET_SUCCESS) {3862RTW_ERR("%s: halmac_free_download_firmware fail(err=0x%x)\n",3863__FUNCTION__, status);3864err = -1;3865goto resume_tx;3866}38673868resume_tx:3869#ifndef RTW_HALMAC_DLFW_MEM_NO_STOP_TX3870/* 4. Driver resume TX if needed */3871/* ToDo */3872#endif /* !RTW_HALMAC_DLFW_MEM_NO_STOP_TX */38733874return err;3875}38763877int rtw_halmac_dlfw_mem_from_file(struct dvobj_priv *d, u8 *fwpath, enum fw_mem mem)3878{3879u8 *fw = NULL;3880u32 fwmaxsize = 0, size = 0;3881int err = 0;388238833884err = rtw_halmac_get_fw_max_size(d, &fwmaxsize);3885if (err) {3886RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err);3887return -1;3888}38893890fw = rtw_zmalloc(fwmaxsize);3891if (!fw)3892return -1;38933894size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize);3895if (size)3896err = rtw_halmac_dlfw_mem(d, fw, size, mem);3897else3898err = -1;38993900rtw_mfree(fw, fwmaxsize);3901/*fw = NULL;*/39023903return err;3904}39053906/*3907* Return:3908* 0 Success3909* -22 Invalid arguemnt3910*/3911int rtw_halmac_dlfw(struct dvobj_priv *d, u8 *fw, u32 fwsize)3912{3913PADAPTER adapter;3914enum halmac_ret_status status;3915u32 ok;3916int err, err_ret = -1;391739183919if (!fw || !fwsize)3920return -22;39213922adapter = dvobj_get_primary_adapter(d);39233924/* re-download firmware */3925if (rtw_is_hw_init_completed(adapter))3926return download_fw(d, fw, fwsize, 1);39273928/* Download firmware before hal init */3929/* Power on, download firmware and init mac */3930ok = rtw_hal_power_on(adapter);3931if (_FAIL == ok)3932goto out;39333934err = download_fw(d, fw, fwsize, 0);3935if (err) {3936err_ret = err;3937goto out;3938}39393940err = init_mac_flow(d);3941if (err)3942goto out;39433944err = _send_general_info(d);3945if (err)3946goto out;39473948err_ret = 0;39493950out:3951return err_ret;3952}39533954int rtw_halmac_dlfw_from_file(struct dvobj_priv *d, u8 *fwpath)3955{3956u8 *fw = NULL;3957u32 fwmaxsize = 0, size = 0;3958int err = 0;395939603961err = rtw_halmac_get_fw_max_size(d, &fwmaxsize);3962if (err) {3963RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err);3964return -1;3965}39663967fw = rtw_zmalloc(fwmaxsize);3968if (!fw)3969return -1;39703971size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize);3972if (size)3973err = rtw_halmac_dlfw(d, fw, size);3974else3975err = -1;39763977rtw_mfree(fw, fwmaxsize);3978/*fw = NULL;*/39793980return err;3981}39823983/*3984* Description:3985* Power on/off BB/RF domain.3986*3987* Parameters:3988* enable _TRUE/_FALSE for power on/off3989*3990* Return:3991* 0 Success3992* others Fail3993*/3994int rtw_halmac_phy_power_switch(struct dvobj_priv *d, u8 enable)3995{3996PADAPTER adapter;3997struct halmac_adapter *halmac;3998struct halmac_api *api;3999enum halmac_ret_status status;4000u8 on;400140024003adapter = dvobj_get_primary_adapter(d);4004halmac = dvobj_to_halmac(d);4005if (!halmac)4006return -1;4007api = HALMAC_GET_API(halmac);4008on = (enable == _TRUE) ? 1 : 0;40094010status = api->halmac_set_hw_value(halmac, HALMAC_HW_EN_BB_RF, &on);4011if (status != HALMAC_RET_SUCCESS)4012return -1;40134014return 0;4015}40164017static u8 _is_fw_read_cmd_down(PADAPTER adapter, u8 msgbox_num)4018{4019u8 read_down = _FALSE;4020int retry_cnts = 100;4021u8 valid;40224023do {4024valid = rtw_read8(adapter, REG_HMETFR) & BIT(msgbox_num);4025if (0 == valid)4026read_down = _TRUE;4027else4028rtw_msleep_os(1);4029} while ((!read_down) && (retry_cnts--));40304031if (_FALSE == read_down)4032RTW_WARN("%s, reg_1cc(%x), msg_box(%d)...\n", __func__, rtw_read8(adapter, REG_HMETFR), msgbox_num);40334034return read_down;4035}40364037/**4038* rtw_halmac_send_h2c() - Send H2C to firmware4039* @d: struct dvobj_priv*4040* @h2c: H2C data buffer, suppose to be 8 bytes4041*4042* Send H2C to firmware by message box register(0x1D0~0x1D3 & 0x1F0~0x1F3).4043*4044* Assume firmware be ready to accept H2C here, please check4045* (hal->bFWReady == _TRUE) before call this function or make sure firmware is4046* ready.4047*4048* Return: 0 if process OK, otherwise fail to send this H2C.4049*/4050int rtw_halmac_send_h2c(struct dvobj_priv *d, u8 *h2c)4051{4052PADAPTER adapter = dvobj_get_primary_adapter(d);4053PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter);4054u8 h2c_box_num = 0;4055u32 msgbox_addr = 0;4056u32 msgbox_ex_addr = 0;4057u32 h2c_cmd = 0;4058u32 h2c_cmd_ex = 0;4059int err = -1;406040614062if (!h2c) {4063RTW_WARN("%s: pbuf is NULL\n", __FUNCTION__);4064return err;4065}40664067if (rtw_is_surprise_removed(adapter)) {4068RTW_WARN("%s: surprise removed\n", __FUNCTION__);4069return err;4070}40714072_enter_critical_mutex(&d->h2c_fwcmd_mutex, NULL);40734074/* pay attention to if race condition happened in H2C cmd setting */4075h2c_box_num = hal->LastHMEBoxNum;40764077if (!_is_fw_read_cmd_down(adapter, h2c_box_num)) {4078RTW_WARN(" fw read cmd failed...\n");4079#ifdef DBG_CONFIG_ERROR_DETECT4080hal->srestpriv.self_dect_fw = _TRUE;4081hal->srestpriv.self_dect_fw_cnt++;4082#endif /* DBG_CONFIG_ERROR_DETECT */4083goto exit;4084}40854086/* Write Ext command (byte 4~7) */4087msgbox_ex_addr = REG_HMEBOX_E0 + (h2c_box_num * EX_MESSAGE_BOX_SIZE);4088_rtw_memcpy((u8 *)(&h2c_cmd_ex), h2c + 4, EX_MESSAGE_BOX_SIZE);4089h2c_cmd_ex = le32_to_cpu(h2c_cmd_ex);4090rtw_write32(adapter, msgbox_ex_addr, h2c_cmd_ex);40914092/* Write command (byte 0~3) */4093msgbox_addr = REG_HMEBOX0 + (h2c_box_num * MESSAGE_BOX_SIZE);4094_rtw_memcpy((u8 *)(&h2c_cmd), h2c, 4);4095h2c_cmd = le32_to_cpu(h2c_cmd);4096rtw_write32(adapter, msgbox_addr, h2c_cmd);40974098/* update last msg box number */4099hal->LastHMEBoxNum = (h2c_box_num + 1) % MAX_H2C_BOX_NUMS;4100err = 0;41014102#ifdef DBG_H2C_CONTENT4103RTW_INFO_DUMP("[H2C] - ", h2c, RTW_HALMAC_H2C_MAX_SIZE);4104#endif4105exit:4106_exit_critical_mutex(&d->h2c_fwcmd_mutex, NULL);4107return err;4108}41094110/**4111* rtw_halmac_c2h_handle() - Handle C2H for HALMAC4112* @d: struct dvobj_priv*4113* @c2h: Full C2H packet, including RX description and payload4114* @size: Size(byte) of c2h4115*4116* Send C2H packet to HALMAC to process C2H packets, and the expected C2H ID is4117* 0xFF. This function won't have any I/O, so caller doesn't have to call it in4118* I/O safe place(ex. command thread).4119*4120* Please sure doesn't call this function in the same thread as someone is4121* waiting HALMAC C2H ack, otherwise there is a deadlock happen.4122*4123* Return: 0 if process OK, otherwise no action for this C2H.4124*/4125int rtw_halmac_c2h_handle(struct dvobj_priv *d, u8 *c2h, u32 size)4126{4127struct halmac_adapter *mac;4128struct halmac_api *api;4129enum halmac_ret_status status;413041314132mac = dvobj_to_halmac(d);4133api = HALMAC_GET_API(mac);41344135status = api->halmac_get_c2h_info(mac, c2h, size);4136if (HALMAC_RET_SUCCESS != status)4137return -1;41384139return 0;4140}41414142int rtw_halmac_get_available_efuse_size(struct dvobj_priv *d, u32 *size)4143{4144struct halmac_adapter *mac;4145struct halmac_api *api;4146enum halmac_ret_status status;4147u32 val;414841494150mac = dvobj_to_halmac(d);4151api = HALMAC_GET_API(mac);41524153status = api->halmac_get_efuse_available_size(mac, &val);4154if (HALMAC_RET_SUCCESS != status)4155return -1;41564157*size = val;4158return 0;4159}41604161int rtw_halmac_get_physical_efuse_size(struct dvobj_priv *d, u32 *size)4162{4163struct halmac_adapter *mac;4164struct halmac_api *api;4165enum halmac_ret_status status;4166u32 val;416741684169mac = dvobj_to_halmac(d);4170api = HALMAC_GET_API(mac);41714172status = api->halmac_get_efuse_size(mac, &val);4173if (HALMAC_RET_SUCCESS != status)4174return -1;41754176*size = val;4177return 0;4178}41794180int rtw_halmac_read_physical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size)4181{4182struct halmac_adapter *mac;4183struct halmac_api *api;4184enum halmac_ret_status status;4185enum halmac_feature_id id;4186int ret;418741884189mac = dvobj_to_halmac(d);4190api = HALMAC_GET_API(mac);4191id = HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE;41924193ret = init_halmac_event(d, id, map, size);4194if (ret)4195return -1;41964197status = api->halmac_dump_efuse_map(mac, HALMAC_EFUSE_R_DRV);4198if (HALMAC_RET_SUCCESS != status) {4199free_halmac_event(d, id);4200return -1;4201}42024203ret = wait_halmac_event(d, id);4204if (ret)4205return -1;42064207return 0;4208}42094210int rtw_halmac_read_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)4211{4212struct halmac_adapter *mac;4213struct halmac_api *api;4214enum halmac_ret_status status;4215u8 v;4216u32 i;4217u8 *efuse = NULL;4218u32 size = 0;4219int err = 0;422042214222mac = dvobj_to_halmac(d);4223api = HALMAC_GET_API(mac);42244225if (api->halmac_read_efuse) {4226for (i = 0; i < cnt; i++) {4227status = api->halmac_read_efuse(mac, offset + i, &v);4228if (HALMAC_RET_SUCCESS != status)4229return -1;4230data[i] = v;4231}4232} else {4233err = rtw_halmac_get_physical_efuse_size(d, &size);4234if (err)4235return -1;42364237efuse = rtw_zmalloc(size);4238if (!efuse)4239return -1;42404241err = rtw_halmac_read_physical_efuse_map(d, efuse, size);4242if (err)4243err = -1;4244else4245_rtw_memcpy(data, efuse + offset, cnt);42464247rtw_mfree(efuse, size);4248}42494250return err;4251}42524253int rtw_halmac_write_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)4254{4255struct halmac_adapter *mac;4256struct halmac_api *api;4257enum halmac_ret_status status;4258u32 i;425942604261mac = dvobj_to_halmac(d);4262api = HALMAC_GET_API(mac);42634264if (api->halmac_write_efuse == NULL)4265return -1;42664267for (i = 0; i < cnt; i++) {4268status = api->halmac_write_efuse(mac, offset + i, data[i]);4269if (HALMAC_RET_SUCCESS != status)4270return -1;4271}42724273return 0;4274}42754276int rtw_halmac_get_logical_efuse_size(struct dvobj_priv *d, u32 *size)4277{4278struct halmac_adapter *mac;4279struct halmac_api *api;4280enum halmac_ret_status status;4281u32 val;428242834284mac = dvobj_to_halmac(d);4285api = HALMAC_GET_API(mac);42864287status = api->halmac_get_logical_efuse_size(mac, &val);4288if (HALMAC_RET_SUCCESS != status)4289return -1;42904291*size = val;4292return 0;4293}42944295int rtw_halmac_read_logical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size, u8 *maskmap, u32 masksize)4296{4297struct halmac_adapter *mac;4298struct halmac_api *api;4299enum halmac_ret_status status;4300enum halmac_feature_id id;4301int ret;430243034304mac = dvobj_to_halmac(d);4305api = HALMAC_GET_API(mac);4306id = HALMAC_FEATURE_DUMP_LOGICAL_EFUSE;43074308ret = init_halmac_event(d, id, map, size);4309if (ret)4310return -1;43114312status = api->halmac_dump_logical_efuse_map(mac, HALMAC_EFUSE_R_DRV);4313if (HALMAC_RET_SUCCESS != status) {4314free_halmac_event(d, id);4315return -1;4316}43174318ret = wait_halmac_event(d, id);4319if (ret)4320return -1;43214322if (maskmap && masksize) {4323struct halmac_pg_efuse_info pginfo;43244325pginfo.efuse_map = map;4326pginfo.efuse_map_size = size;4327pginfo.efuse_mask = maskmap;4328pginfo.efuse_mask_size = masksize;43294330status = api->halmac_mask_logical_efuse(mac, &pginfo);4331if (status != HALMAC_RET_SUCCESS)4332RTW_WARN("%s: mask logical efuse FAIL!\n", __FUNCTION__);4333}43344335return 0;4336}43374338int rtw_halmac_write_logical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size, u8 *maskmap, u32 masksize)4339{4340struct halmac_adapter *mac;4341struct halmac_api *api;4342struct halmac_pg_efuse_info pginfo;4343enum halmac_ret_status status;434443454346mac = dvobj_to_halmac(d);4347api = HALMAC_GET_API(mac);43484349pginfo.efuse_map = map;4350pginfo.efuse_map_size = size;4351pginfo.efuse_mask = maskmap;4352pginfo.efuse_mask_size = masksize;43534354status = api->halmac_pg_efuse_by_map(mac, &pginfo, HALMAC_EFUSE_R_AUTO);4355if (HALMAC_RET_SUCCESS != status)4356return -1;43574358return 0;4359}43604361int rtw_halmac_read_logical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)4362{4363struct halmac_adapter *mac;4364struct halmac_api *api;4365enum halmac_ret_status status;4366u8 v;4367u32 i;436843694370mac = dvobj_to_halmac(d);4371api = HALMAC_GET_API(mac);43724373for (i = 0; i < cnt; i++) {4374status = api->halmac_read_logical_efuse(mac, offset + i, &v);4375if (HALMAC_RET_SUCCESS != status)4376return -1;4377data[i] = v;4378}43794380return 0;4381}43824383int rtw_halmac_write_logical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)4384{4385struct halmac_adapter *mac;4386struct halmac_api *api;4387enum halmac_ret_status status;4388u32 i;438943904391mac = dvobj_to_halmac(d);4392api = HALMAC_GET_API(mac);43934394for (i = 0; i < cnt; i++) {4395status = api->halmac_write_logical_efuse(mac, offset + i, data[i]);4396if (HALMAC_RET_SUCCESS != status)4397return -1;4398}43994400return 0;4401}44024403int rtw_halmac_write_bt_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data)4404{4405struct halmac_adapter *mac;4406struct halmac_api *api;4407enum halmac_ret_status status;4408u32 i;4409u8 bank = 1;441044114412mac = dvobj_to_halmac(d);4413api = HALMAC_GET_API(mac);44144415for (i = 0; i < cnt; i++) {4416status = api->halmac_write_efuse_bt(mac, offset + i, data[i], bank);4417if (HALMAC_RET_SUCCESS != status) {4418printk("%s: halmac_write_efuse_bt status = %d\n", __FUNCTION__, status);4419return -1;4420}4421}4422printk("%s: halmac_write_efuse_bt status = HALMAC_RET_SUCCESS %d\n", __FUNCTION__, status);4423return 0;4424}442544264427int rtw_halmac_read_bt_physical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size)4428{4429struct halmac_adapter *mac;4430struct halmac_api *api;4431enum halmac_ret_status status;4432int bank = 1;443344344435mac = dvobj_to_halmac(d);4436api = HALMAC_GET_API(mac);44374438status = api->halmac_dump_efuse_map_bt(mac, bank, size, map);4439if (HALMAC_RET_SUCCESS != status) {4440printk("%s: halmac_dump_efuse_map_bt fail!\n", __FUNCTION__);4441return -1;4442}44434444printk("%s: OK!\n", __FUNCTION__);44454446return 0;4447}44484449static enum hal_fifo_sel _fifo_sel_drv2halmac(u8 fifo_sel)4450{4451switch (fifo_sel) {4452case 0:4453return HAL_FIFO_SEL_TX;4454case 1:4455return HAL_FIFO_SEL_RX;4456case 2:4457return HAL_FIFO_SEL_RSVD_PAGE;4458case 3:4459return HAL_FIFO_SEL_REPORT;4460case 4:4461return HAL_FIFO_SEL_LLT;4462case 5:4463return HAL_FIFO_SEL_RXBUF_FW;4464}44654466return HAL_FIFO_SEL_RSVD_PAGE;4467}44684469/*#define CONFIG_HALMAC_FIFO_DUMP*/4470int rtw_halmac_dump_fifo(struct dvobj_priv *d, u8 fifo_sel, u32 addr, u32 size, u8 *buffer)4471{4472struct halmac_adapter *mac;4473struct halmac_api *api;4474enum hal_fifo_sel halmac_fifo_sel;4475enum halmac_ret_status status;4476u8 *pfifo_map = NULL;4477u32 fifo_size = 0;4478s8 ret = 0;/* 0:success, -1:error */4479u8 mem_created = _FALSE;448044814482mac = dvobj_to_halmac(d);4483api = HALMAC_GET_API(mac);44844485if ((size != 0) && (buffer == NULL))4486return -1;44874488halmac_fifo_sel = _fifo_sel_drv2halmac(fifo_sel);44894490if ((size) && (buffer)) {4491pfifo_map = buffer;4492fifo_size = size;4493} else {4494fifo_size = api->halmac_get_fifo_size(mac, halmac_fifo_sel);44954496if (fifo_size)4497pfifo_map = rtw_zvmalloc(fifo_size);4498if (pfifo_map == NULL)4499return -1;4500mem_created = _TRUE;4501}45024503status = api->halmac_dump_fifo(mac, halmac_fifo_sel, addr, fifo_size, pfifo_map);4504if (HALMAC_RET_SUCCESS != status) {4505ret = -1;4506goto _exit;4507}45084509#ifdef CONFIG_HALMAC_FIFO_DUMP4510{4511static const char * const fifo_sel_str[] = {4512"TX", "RX", "RSVD_PAGE", "REPORT", "LLT", "RXBUF_FW"4513};45144515RTW_INFO("%s FIFO DUMP [start_addr:0x%04x , size:%d]\n", fifo_sel_str[halmac_fifo_sel], addr, fifo_size);4516RTW_INFO_DUMP("\n", pfifo_map, fifo_size);4517RTW_INFO(" ==================================================\n");4518}4519#endif /* CONFIG_HALMAC_FIFO_DUMP */45204521_exit:4522if ((mem_created == _TRUE) && pfifo_map)4523rtw_vmfree(pfifo_map, fifo_size);45244525return ret;4526}45274528/*4529* rtw_halmac_rx_agg_switch() - Switch RX aggregation function and setting4530* @d struct dvobj_priv *4531* @enable _FALSE/_TRUE for disable/enable RX aggregation function4532*4533* This function could help to on/off bus RX aggregation function, and is only4534* useful for SDIO and USB interface. Although only "enable" flag is brough in,4535* some setting would be taken from other places, and they are from:4536* [DMA aggregation]4537* struct hal_com_data.rxagg_dma_size4538* struct hal_com_data.rxagg_dma_timeout4539* [USB aggregation] (only use for USB interface)4540* struct hal_com_data.rxagg_usb_size4541* struct hal_com_data.rxagg_usb_timeout4542* If above values of size and timeout are both 0 means driver would not4543* control the threshold setting and leave it to HALMAC handle.4544*4545* From HALMAC V1_04_04, driver force the size threshold be hard limit, and the4546* rx size can not exceed the setting.4547*4548* Return 0 for success, otherwise fail.4549*/4550int rtw_halmac_rx_agg_switch(struct dvobj_priv *d, u8 enable)4551{4552struct _ADAPTER *adapter;4553struct hal_com_data *hal;4554struct halmac_adapter *halmac;4555struct halmac_api *api;4556struct halmac_rxagg_cfg rxaggcfg;4557enum halmac_ret_status status;455845594560adapter = dvobj_get_primary_adapter(d);4561hal = GET_HAL_DATA(adapter);4562halmac = dvobj_to_halmac(d);4563api = HALMAC_GET_API(halmac);4564_rtw_memset((void *)&rxaggcfg, 0, sizeof(rxaggcfg));4565rxaggcfg.mode = HALMAC_RX_AGG_MODE_NONE;4566/*4567* Always enable size limit to avoid rx size exceed4568* driver defined size.4569*/4570rxaggcfg.threshold.size_limit_en = 1;45714572#ifdef RTW_RX_AGGREGATION4573if (_TRUE == enable) {4574#ifdef CONFIG_SDIO_HCI4575rxaggcfg.mode = HALMAC_RX_AGG_MODE_DMA;4576rxaggcfg.threshold.drv_define = 0;4577if (hal->rxagg_dma_size || hal->rxagg_dma_timeout) {4578rxaggcfg.threshold.drv_define = 1;4579rxaggcfg.threshold.timeout = hal->rxagg_dma_timeout;4580rxaggcfg.threshold.size = hal->rxagg_dma_size;4581RTW_INFO("%s: RX aggregation threshold: "4582"timeout=%u size=%u\n",4583__FUNCTION__,4584hal->rxagg_dma_timeout,4585hal->rxagg_dma_size);4586}4587#elif defined(CONFIG_USB_HCI)4588switch (hal->rxagg_mode) {4589case RX_AGG_DISABLE:4590rxaggcfg.mode = HALMAC_RX_AGG_MODE_NONE;4591break;45924593case RX_AGG_DMA:4594rxaggcfg.mode = HALMAC_RX_AGG_MODE_DMA;4595if (hal->rxagg_dma_size || hal->rxagg_dma_timeout) {4596rxaggcfg.threshold.drv_define = 1;4597rxaggcfg.threshold.timeout = hal->rxagg_dma_timeout;4598rxaggcfg.threshold.size = hal->rxagg_dma_size;4599}4600break;46014602case RX_AGG_USB:4603case RX_AGG_MIX:4604rxaggcfg.mode = HALMAC_RX_AGG_MODE_USB;4605if (hal->rxagg_usb_size || hal->rxagg_usb_timeout) {4606rxaggcfg.threshold.drv_define = 1;4607rxaggcfg.threshold.timeout = hal->rxagg_usb_timeout;4608rxaggcfg.threshold.size = hal->rxagg_usb_size;4609}4610break;4611}4612#endif /* CONFIG_USB_HCI */4613}4614#endif /* RTW_RX_AGGREGATION */46154616status = api->halmac_cfg_rx_aggregation(halmac, &rxaggcfg);4617if (status != HALMAC_RET_SUCCESS)4618return -1;46194620return 0;4621}46224623int rtw_halmac_download_rsvd_page(struct dvobj_priv *dvobj, u8 pg_offset, u8 *pbuf, u32 size)4624{4625enum halmac_ret_status status = HALMAC_RET_SUCCESS;4626struct halmac_adapter *halmac = dvobj_to_halmac(dvobj);4627struct halmac_api *api = HALMAC_GET_API(halmac);46284629status = api->halmac_dl_drv_rsvd_page(halmac, pg_offset, pbuf, size);4630if (status != HALMAC_RET_SUCCESS)4631return -1;46324633return 0;4634}46354636/*4637* Description4638* Fill following spec info from HALMAC API:4639* sec_cam_ent_num4640*4641* Return4642* 0 Success4643* others Fail4644*/4645int rtw_halmac_fill_hal_spec(struct dvobj_priv *dvobj, struct hal_spec_t *spec)4646{4647enum halmac_ret_status status;4648struct halmac_adapter *halmac;4649struct halmac_api *api;4650u8 cam = 0; /* Security Cam Entry Number */465146524653halmac = dvobj_to_halmac(dvobj);4654api = HALMAC_GET_API(halmac);46554656/* Prepare data from HALMAC */4657status = api->halmac_get_hw_value(halmac, HALMAC_HW_CAM_ENTRY_NUM, &cam);4658if (status != HALMAC_RET_SUCCESS)4659return -1;46604661/* Fill data to hal_spec_t */4662spec->sec_cam_ent_num = cam;46634664return 0;4665}46664667int rtw_halmac_p2pps(struct dvobj_priv *dvobj, struct hal_p2p_ps_para *pp2p_ps_para)4668{4669enum halmac_ret_status status = HALMAC_RET_SUCCESS;4670struct halmac_adapter *halmac = dvobj_to_halmac(dvobj);4671struct halmac_api *api = HALMAC_GET_API(halmac);4672struct halmac_p2pps halmac_p2p_ps;46734674(&halmac_p2p_ps)->offload_en = pp2p_ps_para->offload_en;4675(&halmac_p2p_ps)->role = pp2p_ps_para->role;4676(&halmac_p2p_ps)->ctwindow_en = pp2p_ps_para->ctwindow_en;4677(&halmac_p2p_ps)->noa_en = pp2p_ps_para->noa_en;4678(&halmac_p2p_ps)->noa_sel = pp2p_ps_para->noa_sel;4679(&halmac_p2p_ps)->all_sta_sleep = pp2p_ps_para->all_sta_sleep;4680(&halmac_p2p_ps)->discovery = pp2p_ps_para->discovery;4681(&halmac_p2p_ps)->disable_close_rf = pp2p_ps_para->disable_close_rf;4682(&halmac_p2p_ps)->p2p_port_id = _hw_port_drv2halmac(pp2p_ps_para->p2p_port_id);4683(&halmac_p2p_ps)->p2p_group = pp2p_ps_para->p2p_group;4684(&halmac_p2p_ps)->p2p_macid = pp2p_ps_para->p2p_macid;4685(&halmac_p2p_ps)->ctwindow_length = pp2p_ps_para->ctwindow_length;4686(&halmac_p2p_ps)->noa_duration_para = pp2p_ps_para->noa_duration_para;4687(&halmac_p2p_ps)->noa_interval_para = pp2p_ps_para->noa_interval_para;4688(&halmac_p2p_ps)->noa_start_time_para = pp2p_ps_para->noa_start_time_para;4689(&halmac_p2p_ps)->noa_count_para = pp2p_ps_para->noa_count_para;46904691status = api->halmac_p2pps(halmac, (&halmac_p2p_ps));4692if (status != HALMAC_RET_SUCCESS)4693return -1;46944695return 0;46964697}46984699/**4700* rtw_halmac_iqk() - Run IQ Calibration4701* @d: struct dvobj_priv*4702* @clear: IQK parameters4703* @segment: IQK parameters4704*4705* Process IQ Calibration(IQK).4706*4707* Return 0 for OK, otherwise fail.4708*/4709int rtw_halmac_iqk(struct dvobj_priv *d, u8 clear, u8 segment)4710{4711struct halmac_adapter *mac;4712struct halmac_api *api;4713enum halmac_ret_status status;4714enum halmac_feature_id id;4715struct halmac_iqk_para para;4716int ret;4717u8 retry = 3;4718u8 delay = 1; /* ms */471947204721mac = dvobj_to_halmac(d);4722api = HALMAC_GET_API(mac);4723id = HALMAC_FEATURE_IQK;47244725ret = init_halmac_event(d, id, NULL, 0);4726if (ret)4727return -1;47284729para.clear = clear;4730para.segment_iqk = segment;47314732do {4733status = api->halmac_start_iqk(mac, ¶);4734if (status != HALMAC_RET_BUSY_STATE)4735break;4736RTW_WARN("%s: Fail to start IQK, status is BUSY! retry=%d\n", __FUNCTION__, retry);4737if (!retry)4738break;4739retry--;4740rtw_msleep_os(delay);4741} while (1);4742if (status != HALMAC_RET_SUCCESS) {4743free_halmac_event(d, id);4744return -1;4745}47464747ret = wait_halmac_event(d, id);4748if (ret)4749return -1;47504751return 0;4752}47534754static inline u32 _phy_parameter_val_drv2halmac(u32 val, u8 msk_en, u32 msk)4755{4756if (!msk_en)4757return val;47584759return (val << bitshift(msk));4760}47614762static int _phy_parameter_drv2halmac(struct rtw_phy_parameter *para, struct halmac_phy_parameter_info *info)4763{4764if (!para || !info)4765return -1;47664767_rtw_memset(info, 0, sizeof(*info));47684769switch (para->cmd) {4770case 0:4771/* MAC register */4772switch (para->data.mac.size) {4773case 1:4774info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W8;4775break;4776case 2:4777info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W16;4778break;4779default:4780info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W32;4781break;4782}4783info->content.MAC_REG_W.value = _phy_parameter_val_drv2halmac(4784para->data.mac.value,4785para->data.mac.msk_en,4786para->data.mac.msk);4787info->content.MAC_REG_W.msk = para->data.mac.msk;4788info->content.MAC_REG_W.offset = para->data.mac.offset;4789info->content.MAC_REG_W.msk_en = para->data.mac.msk_en;4790break;47914792case 1:4793/* BB register */4794switch (para->data.bb.size) {4795case 1:4796info->cmd_id = HALMAC_PARAMETER_CMD_BB_W8;4797break;4798case 2:4799info->cmd_id = HALMAC_PARAMETER_CMD_BB_W16;4800break;4801default:4802info->cmd_id = HALMAC_PARAMETER_CMD_BB_W32;4803break;4804}4805info->content.BB_REG_W.value = _phy_parameter_val_drv2halmac(4806para->data.bb.value,4807para->data.bb.msk_en,4808para->data.bb.msk);4809info->content.BB_REG_W.msk = para->data.bb.msk;4810info->content.BB_REG_W.offset = para->data.bb.offset;4811info->content.BB_REG_W.msk_en = para->data.bb.msk_en;4812break;48134814case 2:4815/* RF register */4816info->cmd_id = HALMAC_PARAMETER_CMD_RF_W;4817info->content.RF_REG_W.value = _phy_parameter_val_drv2halmac(4818para->data.rf.value,4819para->data.rf.msk_en,4820para->data.rf.msk);4821info->content.RF_REG_W.msk = para->data.rf.msk;4822info->content.RF_REG_W.offset = para->data.rf.offset;4823info->content.RF_REG_W.msk_en = para->data.rf.msk_en;4824info->content.RF_REG_W.rf_path = para->data.rf.path;4825break;48264827case 3:4828/* Delay register */4829if (para->data.delay.unit == 0)4830info->cmd_id = HALMAC_PARAMETER_CMD_DELAY_US;4831else4832info->cmd_id = HALMAC_PARAMETER_CMD_DELAY_MS;4833info->content.DELAY_TIME.delay_time = para->data.delay.value;4834break;48354836case 0xFF:4837/* Latest(End) command */4838info->cmd_id = HALMAC_PARAMETER_CMD_END;4839break;48404841default:4842return -1;4843}48444845return 0;4846}48474848/**4849* rtw_halmac_cfg_phy_para() - Register(Phy parameter) configuration4850* @d: struct dvobj_priv*4851* @para: phy parameter4852*4853* Configure registers by firmware using H2C/C2H mechanism.4854* The latest command should be para->cmd==0xFF(End command) to finish all4855* processes.4856*4857* Return: 0 for OK, otherwise fail.4858*/4859int rtw_halmac_cfg_phy_para(struct dvobj_priv *d, struct rtw_phy_parameter *para)4860{4861struct halmac_adapter *mac;4862struct halmac_api *api;4863enum halmac_ret_status status;4864enum halmac_feature_id id;4865struct halmac_phy_parameter_info info;4866u8 full_fifo;4867int err, ret;486848694870mac = dvobj_to_halmac(d);4871api = HALMAC_GET_API(mac);4872id = HALMAC_FEATURE_CFG_PARA;4873full_fifo = 1; /* ToDo: How to deciede? */4874ret = 0;48754876err = _phy_parameter_drv2halmac(para, &info);4877if (err)4878return -1;48794880err = init_halmac_event(d, id, NULL, 0);4881if (err)4882return -1;48834884status = api->halmac_cfg_parameter(mac, &info, full_fifo);4885if (info.cmd_id == HALMAC_PARAMETER_CMD_END) {4886if (status == HALMAC_RET_SUCCESS) {4887err = wait_halmac_event(d, id);4888if (err)4889ret = -1;4890} else {4891free_halmac_event(d, id);4892ret = -1;4893RTW_ERR("%s: Fail to send END of cfg parameter, status is 0x%x!\n", __FUNCTION__, status);4894}4895} else {4896if (status == HALMAC_RET_PARA_SENDING) {4897err = wait_halmac_event(d, id);4898if (err)4899ret = -1;4900} else {4901free_halmac_event(d, id);4902if (status != HALMAC_RET_SUCCESS) {4903ret = -1;4904RTW_ERR("%s: Fail to cfg parameter, status is 0x%x!\n", __FUNCTION__, status);4905}4906}4907}49084909return ret;4910}49114912static enum halmac_wlled_mode _led_mode_drv2halmac(u8 drv_mode)4913{4914enum halmac_wlled_mode halmac_mode;491549164917switch (drv_mode) {4918case 1:4919halmac_mode = HALMAC_WLLED_MODE_TX;4920break;4921case 2:4922halmac_mode = HALMAC_WLLED_MODE_RX;4923break;4924case 3:4925halmac_mode = HALMAC_WLLED_MODE_SW_CTRL;4926break;4927case 0:4928default:4929halmac_mode = HALMAC_WLLED_MODE_TRX;4930break;4931}49324933return halmac_mode;4934}49354936/**4937* rtw_halmac_led_cfg() - Configure Hardware LED Mode4938* @d: struct dvobj_priv*4939* @enable: enable or disable LED function4940* 0: disable4941* 1: enable4942* @mode: WLan LED mode (valid when enable==1)4943* 0: Blink when TX(transmit packet) and RX(receive packet)4944* 1: Blink when TX only4945* 2: Blink when RX only4946* 3: Software control4947*4948* Configure hardware WLan LED mode.4949* If want to change LED mode after enabled, need to disable LED first and4950* enable again to set new mode.4951*4952* Return 0 for OK, otherwise fail.4953*/4954int rtw_halmac_led_cfg(struct dvobj_priv *d, u8 enable, u8 mode)4955{4956struct halmac_adapter *halmac;4957struct halmac_api *api;4958enum halmac_wlled_mode led_mode;4959enum halmac_ret_status status;496049614962halmac = dvobj_to_halmac(d);4963api = HALMAC_GET_API(halmac);49644965if (enable) {4966status = api->halmac_pinmux_set_func(halmac,4967HALMAC_GPIO_FUNC_WL_LED);4968if (status != HALMAC_RET_SUCCESS) {4969RTW_ERR("%s: pinmux set fail!(0x%x)\n",4970__FUNCTION__, status);4971return -1;4972}49734974led_mode = _led_mode_drv2halmac(mode);4975status = api->halmac_pinmux_wl_led_mode(halmac, led_mode);4976if (status != HALMAC_RET_SUCCESS) {4977RTW_ERR("%s: mode set fail!(0x%x)\n",4978__FUNCTION__, status);4979return -1;4980}4981} else {4982/* Change LED to software control and turn off */4983api->halmac_pinmux_wl_led_mode(halmac,4984HALMAC_WLLED_MODE_SW_CTRL);4985api->halmac_pinmux_wl_led_sw_ctrl(halmac, 0);49864987status = api->halmac_pinmux_free_func(halmac,4988HALMAC_GPIO_FUNC_WL_LED);4989if (status != HALMAC_RET_SUCCESS) {4990RTW_ERR("%s: pinmux free fail!(0x%x)\n",4991__FUNCTION__, status);4992return -1;4993}4994}49954996return 0;4997}49984999/**5000* rtw_halmac_led_switch() - Turn Hardware LED on/off5001* @d: struct dvobj_priv*5002* @on: LED light or not5003* 0: Off5004* 1: On(Light)5005*5006* Turn Hardware WLan LED On/Off.5007* Before use this function, user should call rtw_halmac_led_ctrl() to switch5008* mode to "software control(3)" first, otherwise control would fail.5009* The interval between on and off must be longer than 1 ms, or the LED would5010* keep light or dark only.5011* Ex. Turn off LED at first, turn on after 0.5ms and turn off again after5012* 0.5ms. The LED during this flow will only keep dark, and miss the turn on5013* operation between two turn off operations.5014*/5015void rtw_halmac_led_switch(struct dvobj_priv *d, u8 on)5016{5017struct halmac_adapter *halmac;5018struct halmac_api *api;501950205021halmac = dvobj_to_halmac(d);5022api = HALMAC_GET_API(halmac);50235024api->halmac_pinmux_wl_led_sw_ctrl(halmac, on);5025}50265027/**5028* rtw_halmac_bt_wake_cfg() - Configure BT wake host function5029* @d: struct dvobj_priv*5030* @enable: enable or disable BT wake host function5031* 0: disable5032* 1: enable5033*5034* Configure pinmux to allow BT to control BT wake host pin.5035*5036* Return 0 for OK, otherwise fail.5037*/5038int rtw_halmac_bt_wake_cfg(struct dvobj_priv *d, u8 enable)5039{5040struct halmac_adapter *halmac;5041struct halmac_api *api;5042enum halmac_ret_status status;504350445045halmac = dvobj_to_halmac(d);5046api = HALMAC_GET_API(halmac);50475048if (enable) {5049status = api->halmac_pinmux_set_func(halmac,5050HALMAC_GPIO_FUNC_BT_HOST_WAKE1);5051if (status != HALMAC_RET_SUCCESS) {5052RTW_ERR("%s: pinmux set BT_HOST_WAKE1 fail!(0x%x)\n",5053__FUNCTION__, status);5054return -1;5055}5056} else {5057status = api->halmac_pinmux_free_func(halmac,5058HALMAC_GPIO_FUNC_BT_HOST_WAKE1);5059if (status != HALMAC_RET_SUCCESS) {5060RTW_ERR("%s: pinmux free BT_HOST_WAKE1 fail!(0x%x)\n",5061__FUNCTION__, status);5062return -1;5063}5064}50655066return 0;5067}50685069#ifdef CONFIG_PNO_SUPPORT5070/**5071* _halmac_scanoffload() - Switch channel by firmware during scanning5072* @d: struct dvobj_priv*5073* @enable: 1: enable, 0: disable5074* @nlo: 1: nlo mode (no c2h event), 0: normal mode5075* @ssid: ssid of probe request5076* @ssid_len: ssid length5077*5078* Switch Channel and Send Porbe Request Offloaded by FW5079*5080* Return 0 for OK, otherwise fail.5081*/5082static int _halmac_scanoffload(struct dvobj_priv *d, u32 enable, u8 nlo,5083u8 *ssid, u8 ssid_len)5084{5085struct _ADAPTER *adapter;5086struct halmac_adapter *mac;5087struct halmac_api *api;5088enum halmac_ret_status status;5089struct halmac_ch_info ch_info;5090struct halmac_ch_switch_option cs_option;5091struct mlme_ext_priv *pmlmeext;5092enum halmac_feature_id id_update, id_ch_sw;5093struct halmac_indicator *indicator, *tbl;50945095int err = 0;5096u8 probereq[64];5097u32 len = 0;5098int i = 0;5099struct pno_ssid pnossid;5100struct rf_ctl_t *rfctl = NULL;5101struct _RT_CHANNEL_INFO *ch_set;510251035104tbl = d->hmpriv.indicator;5105adapter = dvobj_get_primary_adapter(d);5106mac = dvobj_to_halmac(d);5107if (!mac)5108return -1;5109api = HALMAC_GET_API(mac);5110id_update = HALMAC_FEATURE_UPDATE_PACKET;5111id_ch_sw = HALMAC_FEATURE_CHANNEL_SWITCH;5112pmlmeext = &(adapter->mlmeextpriv);5113rfctl = adapter_to_rfctl(adapter);5114ch_set = rfctl->channel_set;51155116RTW_INFO("%s: %s scanoffload, mode: %s\n",5117__FUNCTION__, enable?"Enable":"Disable",5118nlo?"PNO/NLO":"Normal");51195120if (enable) {5121_rtw_memset(probereq, 0, sizeof(probereq));51225123_rtw_memset(&pnossid, 0, sizeof(pnossid));5124if (ssid) {5125if (ssid_len > sizeof(pnossid.SSID)) {5126RTW_ERR("%s: SSID length(%d) is too long(>%d)!!\n",5127__FUNCTION__, ssid_len, sizeof(pnossid.SSID));5128return -1;5129}51305131pnossid.SSID_len = ssid_len;5132_rtw_memcpy(pnossid.SSID, ssid, ssid_len);5133}51345135rtw_hal_construct_ProbeReq(adapter, probereq, &len, &pnossid);51365137if (!nlo) {5138err = init_halmac_event(d, id_update, NULL, 0);5139if (err)5140return -1;5141}51425143status = api->halmac_update_packet(mac, HALMAC_PACKET_PROBE_REQ,5144probereq, len);5145if (status != HALMAC_RET_SUCCESS) {5146if (!nlo)5147free_halmac_event(d, id_update);5148RTW_ERR("%s: halmac_update_packet FAIL(%d)!!\n",5149__FUNCTION__, status);5150return -1;5151}51525153if (!nlo) {5154err = wait_halmac_event(d, id_update);5155if (err)5156RTW_ERR("%s: wait update packet FAIL(%d)!!\n",5157__FUNCTION__, err);5158}51595160api->halmac_clear_ch_info(mac);51615162for (i = 0; i < rfctl->max_chan_nums && ch_set[i].ChannelNum != 0; i++) {5163_rtw_memset(&ch_info, 0, sizeof(ch_info));5164ch_info.extra_info = 0;5165ch_info.channel = ch_set[i].ChannelNum;5166ch_info.bw = HALMAC_BW_20;5167ch_info.pri_ch_idx = HALMAC_CH_IDX_1;5168ch_info.action_id = HALMAC_CS_ACTIVE_SCAN;5169ch_info.timeout = 1;5170status = api->halmac_add_ch_info(mac, &ch_info);5171if (status != HALMAC_RET_SUCCESS) {5172RTW_ERR("%s: add_ch_info FAIL(%d)!!\n",5173__FUNCTION__, status);5174return -1;5175}5176}51775178/* set channel switch option */5179_rtw_memset(&cs_option, 0, sizeof(cs_option));5180cs_option.dest_bw = HALMAC_BW_20;5181cs_option.periodic_option = HALMAC_CS_PERIODIC_2_PHASE;5182cs_option.dest_pri_ch_idx = HALMAC_CH_IDX_UNDEFINE;5183cs_option.tsf_low = 0;5184cs_option.switch_en = 1;5185cs_option.dest_ch_en = 1;5186cs_option.absolute_time_en = 0;5187cs_option.dest_ch = 1;51885189cs_option.normal_period = 5;5190cs_option.normal_period_sel = 0;5191cs_option.normal_cycle = 10;51925193cs_option.phase_2_period = 1;5194cs_option.phase_2_period_sel = 1;51955196/* nlo is for wow fw, 1: no c2h response */5197cs_option.nlo_en = nlo;51985199if (!nlo) {5200err = init_halmac_event(d, id_ch_sw, NULL, 0);5201if (err)5202return -1;5203}52045205status = api->halmac_ctrl_ch_switch(mac, &cs_option);5206if (status != HALMAC_RET_SUCCESS) {5207if (!nlo)5208free_halmac_event(d, id_ch_sw);5209RTW_ERR("%s: halmac_ctrl_ch_switch FAIL(%d)!!\n",5210__FUNCTION__, status);5211return -1;5212}52135214if (!nlo) {5215err = wait_halmac_event(d, id_ch_sw);5216if (err)5217RTW_ERR("%s: wait ctrl_ch_switch FAIL(%d)!!\n",5218__FUNCTION__, err);5219}5220} else {5221api->halmac_clear_ch_info(mac);52225223_rtw_memset(&cs_option, 0, sizeof(cs_option));5224cs_option.switch_en = 0;52255226if (!nlo) {5227err = init_halmac_event(d, id_ch_sw, NULL, 0);5228if (err)5229return -1;5230}52315232status = api->halmac_ctrl_ch_switch(mac, &cs_option);5233if (status != HALMAC_RET_SUCCESS) {5234if (!nlo)5235free_halmac_event(d, id_ch_sw);5236RTW_ERR("%s: halmac_ctrl_ch_switch FAIL(%d)!!\n",5237__FUNCTION__, status);5238return -1;5239}52405241if (!nlo) {5242err = wait_halmac_event(d, id_ch_sw);5243if (err)5244RTW_ERR("%s: wait ctrl_ch_switch FAIL(%d)!!\n",5245__FUNCTION__, err);5246}5247}52485249return 0;5250}52515252/**5253* rtw_halmac_pno_scanoffload() - Control firmware scan AP function for PNO5254* @d: struct dvobj_priv*5255* @enable: 1: enable, 0: disable5256*5257* Switch firmware scan AP function for PNO(prefer network offload) or5258* NLO(network list offload).5259*5260* Return 0 for OK, otherwise fail.5261*/5262int rtw_halmac_pno_scanoffload(struct dvobj_priv *d, u32 enable)5263{5264return _halmac_scanoffload(d, enable, 1, NULL, 0);5265}5266#endif /* CONFIG_PNO_SUPPORT */52675268#ifdef CONFIG_SDIO_HCI52695270/*5271* Description:5272* Update queue allocated page number to driver5273*5274* Parameter:5275* d pointer to struct dvobj_priv of driver5276*5277* Return:5278* 0 Success, "page" is valid.5279* others Fail, "page" is invalid.5280*/5281int rtw_halmac_query_tx_page_num(struct dvobj_priv *d)5282{5283PADAPTER adapter;5284struct halmacpriv *hmpriv;5285struct halmac_adapter *halmac;5286struct halmac_api *api;5287struct halmac_rqpn_map rqpn;5288enum halmac_dma_mapping dmaqueue;5289struct halmac_txff_allocation fifosize;5290enum halmac_ret_status status;5291u8 i;529252935294adapter = dvobj_get_primary_adapter(d);5295hmpriv = &d->hmpriv;5296halmac = dvobj_to_halmac(d);5297api = HALMAC_GET_API(halmac);5298_rtw_memset((void *)&rqpn, 0, sizeof(rqpn));5299_rtw_memset((void *)&fifosize, 0, sizeof(fifosize));53005301status = api->halmac_get_hw_value(halmac, HALMAC_HW_RQPN_MAPPING, &rqpn);5302if (status != HALMAC_RET_SUCCESS)5303return -1;5304status = api->halmac_get_hw_value(halmac, HALMAC_HW_TXFF_ALLOCATION, &fifosize);5305if (status != HALMAC_RET_SUCCESS)5306return -1;53075308for (i = 0; i < HW_QUEUE_ENTRY; i++) {5309hmpriv->txpage[i] = 0;53105311/* Driver index mapping to HALMAC DMA queue */5312dmaqueue = HALMAC_DMA_MAPPING_UNDEFINE;5313switch (i) {5314case VO_QUEUE_INX:5315dmaqueue = rqpn.dma_map_vo;5316break;5317case VI_QUEUE_INX:5318dmaqueue = rqpn.dma_map_vi;5319break;5320case BE_QUEUE_INX:5321dmaqueue = rqpn.dma_map_be;5322break;5323case BK_QUEUE_INX:5324dmaqueue = rqpn.dma_map_bk;5325break;5326case MGT_QUEUE_INX:5327dmaqueue = rqpn.dma_map_mg;5328break;5329case HIGH_QUEUE_INX:5330dmaqueue = rqpn.dma_map_hi;5331break;5332case BCN_QUEUE_INX:5333case TXCMD_QUEUE_INX:5334/* Unlimited */5335hmpriv->txpage[i] = 0xFFFF;5336continue;5337}53385339switch (dmaqueue) {5340case HALMAC_DMA_MAPPING_EXTRA:5341hmpriv->txpage[i] = fifosize.extra_queue_pg_num;5342break;5343case HALMAC_DMA_MAPPING_LOW:5344hmpriv->txpage[i] = fifosize.low_queue_pg_num;5345break;5346case HALMAC_DMA_MAPPING_NORMAL:5347hmpriv->txpage[i] = fifosize.normal_queue_pg_num;5348break;5349case HALMAC_DMA_MAPPING_HIGH:5350hmpriv->txpage[i] = fifosize.high_queue_pg_num;5351break;5352case HALMAC_DMA_MAPPING_UNDEFINE:5353break;5354}5355hmpriv->txpage[i] += fifosize.pub_queue_pg_num;5356}53575358return 0;5359}53605361/*5362* Description:5363* Get specific queue allocated page number5364*5365* Parameter:5366* d pointer to struct dvobj_priv of driver5367* queue target queue to query, VO/VI/BE/BK/.../TXCMD_QUEUE_INX5368* page return allocated page number5369*5370* Return:5371* 0 Success, "page" is valid.5372* others Fail, "page" is invalid.5373*/5374int rtw_halmac_get_tx_queue_page_num(struct dvobj_priv *d, u8 queue, u32 *page)5375{5376*page = 0;5377if (queue < HW_QUEUE_ENTRY)5378*page = d->hmpriv.txpage[queue];53795380return 0;5381}53825383/*5384* Return:5385* address for SDIO command5386*/5387u32 rtw_halmac_sdio_get_tx_addr(struct dvobj_priv *d, u8 *desc, u32 size)5388{5389struct halmac_adapter *mac;5390struct halmac_api *api;5391enum halmac_ret_status status;5392u32 addr;539353945395mac = dvobj_to_halmac(d);5396api = HALMAC_GET_API(mac);53975398status = api->halmac_get_sdio_tx_addr(mac, desc, size, &addr);5399if (HALMAC_RET_SUCCESS != status)5400return 0;54015402return addr;5403}54045405int rtw_halmac_sdio_tx_allowed(struct dvobj_priv *d, u8 *buf, u32 size)5406{5407struct halmac_adapter *mac;5408struct halmac_api *api;5409enum halmac_ret_status status;541054115412mac = dvobj_to_halmac(d);5413api = HALMAC_GET_API(mac);54145415status = api->halmac_tx_allowed_sdio(mac, buf, size);5416if (HALMAC_RET_SUCCESS != status)5417return -1;54185419return 0;5420}54215422u32 rtw_halmac_sdio_get_rx_addr(struct dvobj_priv *d, u8 *seq)5423{5424u8 id;54255426#define RTW_SDIO_ADDR_RX_RX0FF_PRFIX 0x0E0005427#define RTW_SDIO_ADDR_RX_RX0FF_GEN(a) (RTW_SDIO_ADDR_RX_RX0FF_PRFIX|(a&0x3))54285429id = *seq;5430(*seq)++;5431return RTW_SDIO_ADDR_RX_RX0FF_GEN(id);5432}54335434int rtw_halmac_sdio_set_tx_format(struct dvobj_priv *d, enum halmac_sdio_tx_format format)5435{5436struct halmac_adapter *mac;5437struct halmac_api *api;5438enum halmac_ret_status status;54395440mac = dvobj_to_halmac(d);5441api = HALMAC_GET_API(mac);54425443status = api->halmac_set_hw_value(mac, HALMAC_HW_SDIO_TX_FORMAT, &format);5444if (HALMAC_RET_SUCCESS != status)5445return -1;54465447return 0;5448}5449#endif /* CONFIG_SDIO_HCI */54505451#ifdef CONFIG_USB_HCI5452u8 rtw_halmac_usb_get_bulkout_id(struct dvobj_priv *d, u8 *buf, u32 size)5453{5454struct halmac_adapter *mac;5455struct halmac_api *api;5456enum halmac_ret_status status;5457u8 bulkout_id;545854595460mac = dvobj_to_halmac(d);5461api = HALMAC_GET_API(mac);54625463status = api->halmac_get_usb_bulkout_id(mac, buf, size, &bulkout_id);5464if (HALMAC_RET_SUCCESS != status)5465return 0;54665467return bulkout_id;5468}54695470/**5471* rtw_halmac_usb_get_txagg_desc_num() - MAX descriptor number in one bulk for TX5472* @d: struct dvobj_priv*5473* @size: TX FIFO size, unit is byte.5474*5475* Get MAX descriptor number in one bulk out from HALMAC.5476*5477* Return 0 for OK, otherwise fail.5478*/5479int rtw_halmac_usb_get_txagg_desc_num(struct dvobj_priv *d, u8 *num)5480{5481struct halmac_adapter *halmac;5482struct halmac_api *api;5483enum halmac_ret_status status;5484u8 val = 0;548554865487halmac = dvobj_to_halmac(d);5488api = HALMAC_GET_API(halmac);54895490status = api->halmac_get_hw_value(halmac, HALMAC_HW_USB_TXAGG_DESC_NUM, &val);5491if (status != HALMAC_RET_SUCCESS)5492return -1;54935494*num = val;54955496return 0;5497}54985499static inline enum halmac_usb_mode _usb_mode_drv2halmac(enum RTW_USB_SPEED usb_mode)5500{5501enum halmac_usb_mode halmac_usb_mode = HALMAC_USB_MODE_U2;55025503switch (usb_mode) {5504case RTW_USB_SPEED_2:5505halmac_usb_mode = HALMAC_USB_MODE_U2;5506break;5507case RTW_USB_SPEED_3:5508halmac_usb_mode = HALMAC_USB_MODE_U3;5509break;5510default:5511halmac_usb_mode = HALMAC_USB_MODE_U2;5512break;5513}55145515return halmac_usb_mode;5516}55175518u8 rtw_halmac_switch_usb_mode(struct dvobj_priv *d, enum RTW_USB_SPEED usb_mode)5519{5520PADAPTER adapter;5521struct halmac_adapter *mac;5522struct halmac_api *api;5523enum halmac_ret_status status;5524enum halmac_usb_mode halmac_usb_mode;55255526adapter = dvobj_get_primary_adapter(d);5527mac = dvobj_to_halmac(d);5528api = HALMAC_GET_API(mac);5529halmac_usb_mode = _usb_mode_drv2halmac(usb_mode);5530status = api->halmac_set_hw_value(mac, HALMAC_HW_USB_MODE, (void *)&halmac_usb_mode);55315532if (HALMAC_RET_SUCCESS != status)5533return _FAIL;55345535return _SUCCESS;5536}5537#endif /* CONFIG_USB_HCI */55385539#ifdef CONFIG_BEAMFORMING5540#ifdef RTW_BEAMFORMING_VERSION_25541int rtw_halmac_bf_add_mu_bfer(struct dvobj_priv *d, u16 paid, u16 csi_para,5542u16 my_aid, enum halmac_csi_seg_len sel, u8 *addr)5543{5544struct halmac_adapter *mac;5545struct halmac_api *api;5546enum halmac_ret_status status;5547struct halmac_mu_bfer_init_para param;554855495550mac = dvobj_to_halmac(d);5551api = HALMAC_GET_API(mac);55525553_rtw_memset(¶m, 0, sizeof(param));5554param.paid = paid;5555param.csi_para = csi_para;5556param.my_aid = my_aid;5557param.csi_length_sel = sel;5558_rtw_memcpy(param.bfer_address.addr, addr, 6);55595560status = api->halmac_mu_bfer_entry_init(mac, ¶m);5561if (status != HALMAC_RET_SUCCESS)5562return -1;55635564return 0;5565}55665567int rtw_halmac_bf_del_mu_bfer(struct dvobj_priv *d)5568{5569struct halmac_adapter *mac;5570struct halmac_api *api;5571enum halmac_ret_status status;557255735574mac = dvobj_to_halmac(d);5575api = HALMAC_GET_API(mac);55765577status = api->halmac_mu_bfer_entry_del(mac);5578if (status != HALMAC_RET_SUCCESS)5579return -1;55805581return 0;5582}558355845585int rtw_halmac_bf_cfg_sounding(struct dvobj_priv *d,5586enum halmac_snd_role role, enum halmac_data_rate rate)5587{5588struct halmac_adapter *mac;5589struct halmac_api *api;5590enum halmac_ret_status status;559155925593mac = dvobj_to_halmac(d);5594api = HALMAC_GET_API(mac);55955596status = api->halmac_cfg_sounding(mac, role, rate);5597if (status != HALMAC_RET_SUCCESS)5598return -1;55995600return 0;5601}56025603int rtw_halmac_bf_del_sounding(struct dvobj_priv *d,5604enum halmac_snd_role role)5605{5606struct halmac_adapter *mac;5607struct halmac_api *api;5608enum halmac_ret_status status;560956105611mac = dvobj_to_halmac(d);5612api = HALMAC_GET_API(mac);56135614status = api->halmac_del_sounding(mac, role);5615if (status != HALMAC_RET_SUCCESS)5616return -1;56175618return 0;5619}56205621/**5622* rtw_halmac_bf_cfg_csi_rate() - Config data rate for CSI report frame by RSSI5623* @d: struct dvobj_priv*5624* @rssi: RSSI vlaue, unit is percentage (0~100).5625* @current_rate: Current CSI frame rate5626* Valid value example5627* 0 CCK 1M5628* 3 CCK 11M5629* 4 OFDM 6M5630* and so on5631* @fixrate_en: Enable to fix CSI frame in VHT rate, otherwise legacy OFDM rate.5632* The value "0" for disable, otheriwse enable.5633* @new_rate: Return new data rate, and value range is the same as5634* current_rate5635* @bmp_ofdm54: Return to suggest enabling OFDM 54M for CSI report frame or not,5636* The valid values and meanings are:5637* 0x00 disable5638* 0x01 enable5639* 0xFF Keep current setting5640*5641* According RSSI to config data rate for CSI report frame of Beamforming.5642*5643* Return 0 for OK, otherwise fail.5644*/5645int rtw_halmac_bf_cfg_csi_rate(struct dvobj_priv *d, u8 rssi,5646u8 current_rate, u8 fixrate_en, u8 *new_rate,5647u8 *bmp_ofdm54)5648{5649struct halmac_adapter *mac;5650struct halmac_api *api;5651enum halmac_ret_status status;565256535654mac = dvobj_to_halmac(d);5655api = HALMAC_GET_API(mac);56565657status = api->halmac_cfg_csi_rate(mac,5658rssi, current_rate, fixrate_en, new_rate,5659bmp_ofdm54);5660if (status != HALMAC_RET_SUCCESS)5661return -1;56625663return 0;5664}56655666int rtw_halmac_bf_cfg_mu_mimo(struct dvobj_priv *d, enum halmac_snd_role role,5667u8 *sounding_sts, u16 grouping_bitmap, u8 mu_tx_en,5668u32 *given_gid_tab, u32 *given_user_pos)5669{5670struct halmac_adapter *mac;5671struct halmac_api *api;5672enum halmac_ret_status status;5673struct halmac_cfg_mumimo_para param;567456755676mac = dvobj_to_halmac(d);5677api = HALMAC_GET_API(mac);56785679_rtw_memset(¶m, 0, sizeof(param));56805681param.role = role;5682param.grouping_bitmap = grouping_bitmap;5683param.mu_tx_en = mu_tx_en;56845685if (sounding_sts)5686_rtw_memcpy(param.sounding_sts, sounding_sts, 6);56875688if (given_gid_tab)5689_rtw_memcpy(param.given_gid_tab, given_gid_tab, 8);56905691if (given_user_pos)5692_rtw_memcpy(param.given_user_pos, given_user_pos, 16);56935694status = api->halmac_cfg_mumimo(mac, ¶m);5695if (status != HALMAC_RET_SUCCESS)5696return -1;56975698return 0;5699}57005701#endif /* RTW_BEAMFORMING_VERSION_2 */5702#endif /* CONFIG_BEAMFORMING */570357045705