1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <drv_types.h>
8 #include <rtw_debug.h>
9 #include <hal_data.h>
10 
rtw_hal_chip_configure(struct adapter * padapter)11 void rtw_hal_chip_configure(struct adapter *padapter)
12 {
13 	if (padapter->HalFunc.intf_chip_configure)
14 		padapter->HalFunc.intf_chip_configure(padapter);
15 }
16 
rtw_hal_read_chip_info(struct adapter * padapter)17 void rtw_hal_read_chip_info(struct adapter *padapter)
18 {
19 	if (padapter->HalFunc.read_adapter_info)
20 		padapter->HalFunc.read_adapter_info(padapter);
21 }
22 
rtw_hal_read_chip_version(struct adapter * padapter)23 void rtw_hal_read_chip_version(struct adapter *padapter)
24 {
25 	if (padapter->HalFunc.read_chip_version)
26 		padapter->HalFunc.read_chip_version(padapter);
27 }
28 
rtw_hal_def_value_init(struct adapter * padapter)29 void rtw_hal_def_value_init(struct adapter *padapter)
30 {
31 	if (is_primary_adapter(padapter))
32 		if (padapter->HalFunc.init_default_value)
33 			padapter->HalFunc.init_default_value(padapter);
34 }
35 
rtw_hal_free_data(struct adapter * padapter)36 void rtw_hal_free_data(struct adapter *padapter)
37 {
38 	/* free HAL Data */
39 	rtw_hal_data_deinit(padapter);
40 
41 	if (is_primary_adapter(padapter))
42 		if (padapter->HalFunc.free_hal_data)
43 			padapter->HalFunc.free_hal_data(padapter);
44 }
45 
rtw_hal_dm_init(struct adapter * padapter)46 void rtw_hal_dm_init(struct adapter *padapter)
47 {
48 	if (is_primary_adapter(padapter))
49 		if (padapter->HalFunc.dm_init)
50 			padapter->HalFunc.dm_init(padapter);
51 }
52 
rtw_hal_dm_deinit(struct adapter * padapter)53 void rtw_hal_dm_deinit(struct adapter *padapter)
54 {
55 	/*  cancel dm  timer */
56 	if (is_primary_adapter(padapter))
57 		if (padapter->HalFunc.dm_deinit)
58 			padapter->HalFunc.dm_deinit(padapter);
59 }
60 
rtw_hal_init_opmode(struct adapter * padapter)61 static void rtw_hal_init_opmode(struct adapter *padapter)
62 {
63 	enum ndis_802_11_network_infrastructure networkType = Ndis802_11InfrastructureMax;
64 	struct  mlme_priv *pmlmepriv = &(padapter->mlmepriv);
65 	signed int fw_state;
66 
67 	fw_state = get_fwstate(pmlmepriv);
68 
69 	if (fw_state & WIFI_ADHOC_STATE)
70 		networkType = Ndis802_11IBSS;
71 	else if (fw_state & WIFI_STATION_STATE)
72 		networkType = Ndis802_11Infrastructure;
73 	else if (fw_state & WIFI_AP_STATE)
74 		networkType = Ndis802_11APMode;
75 	else
76 		return;
77 
78 	rtw_setopmode_cmd(padapter, networkType, false);
79 }
80 
rtw_hal_init(struct adapter * padapter)81 uint rtw_hal_init(struct adapter *padapter)
82 {
83 	uint status;
84 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
85 
86 	status = padapter->HalFunc.hal_init(padapter);
87 
88 	if (status == _SUCCESS) {
89 		rtw_hal_init_opmode(padapter);
90 
91 		dvobj->padapters->hw_init_completed = true;
92 
93 		if (padapter->registrypriv.notch_filter == 1)
94 			rtw_hal_notch_filter(padapter, 1);
95 
96 		rtw_hal_reset_security_engine(padapter);
97 
98 		rtw_sec_restore_wep_key(dvobj->padapters);
99 
100 		init_hw_mlme_ext(padapter);
101 
102 		rtw_bb_rf_gain_offset(padapter);
103 	} else {
104 		dvobj->padapters->hw_init_completed = false;
105 	}
106 
107 	return status;
108 }
109 
rtw_hal_deinit(struct adapter * padapter)110 uint rtw_hal_deinit(struct adapter *padapter)
111 {
112 	uint status = _SUCCESS;
113 	struct dvobj_priv *dvobj = adapter_to_dvobj(padapter);
114 
115 	status = padapter->HalFunc.hal_deinit(padapter);
116 
117 	if (status == _SUCCESS) {
118 		padapter = dvobj->padapters;
119 		padapter->hw_init_completed = false;
120 	}
121 
122 	return status;
123 }
124 
rtw_hal_set_hwreg(struct adapter * padapter,u8 variable,u8 * val)125 void rtw_hal_set_hwreg(struct adapter *padapter, u8 variable, u8 *val)
126 {
127 	if (padapter->HalFunc.SetHwRegHandler)
128 		padapter->HalFunc.SetHwRegHandler(padapter, variable, val);
129 }
130 
rtw_hal_get_hwreg(struct adapter * padapter,u8 variable,u8 * val)131 void rtw_hal_get_hwreg(struct adapter *padapter, u8 variable, u8 *val)
132 {
133 	if (padapter->HalFunc.GetHwRegHandler)
134 		padapter->HalFunc.GetHwRegHandler(padapter, variable, val);
135 }
136 
rtw_hal_set_hwreg_with_buf(struct adapter * padapter,u8 variable,u8 * pbuf,int len)137 void rtw_hal_set_hwreg_with_buf(struct adapter *padapter, u8 variable, u8 *pbuf, int len)
138 {
139 	if (padapter->HalFunc.SetHwRegHandlerWithBuf)
140 		padapter->HalFunc.SetHwRegHandlerWithBuf(padapter, variable, pbuf, len);
141 }
142 
rtw_hal_set_def_var(struct adapter * padapter,enum hal_def_variable eVariable,void * pValue)143 u8 rtw_hal_set_def_var(struct adapter *padapter, enum hal_def_variable eVariable, void *pValue)
144 {
145 	if (padapter->HalFunc.SetHalDefVarHandler)
146 		return padapter->HalFunc.SetHalDefVarHandler(padapter, eVariable, pValue);
147 	return _FAIL;
148 }
149 
rtw_hal_get_def_var(struct adapter * padapter,enum hal_def_variable eVariable,void * pValue)150 u8 rtw_hal_get_def_var(struct adapter *padapter, enum hal_def_variable eVariable, void *pValue)
151 {
152 	if (padapter->HalFunc.GetHalDefVarHandler)
153 		return padapter->HalFunc.GetHalDefVarHandler(padapter, eVariable, pValue);
154 	return _FAIL;
155 }
156 
rtw_hal_set_odm_var(struct adapter * padapter,enum hal_odm_variable eVariable,void * pValue1,bool bSet)157 void rtw_hal_set_odm_var(struct adapter *padapter, enum hal_odm_variable eVariable, void *pValue1, bool bSet)
158 {
159 	if (padapter->HalFunc.SetHalODMVarHandler)
160 		padapter->HalFunc.SetHalODMVarHandler(padapter, eVariable, pValue1, bSet);
161 }
162 
rtw_hal_get_odm_var(struct adapter * padapter,enum hal_odm_variable eVariable,void * pValue1,void * pValue2)163 void rtw_hal_get_odm_var(struct adapter *padapter, enum hal_odm_variable eVariable, void *pValue1, void *pValue2)
164 {
165 	if (padapter->HalFunc.GetHalODMVarHandler)
166 		padapter->HalFunc.GetHalODMVarHandler(padapter, eVariable, pValue1, pValue2);
167 }
168 
rtw_hal_enable_interrupt(struct adapter * padapter)169 void rtw_hal_enable_interrupt(struct adapter *padapter)
170 {
171 	if (padapter->HalFunc.enable_interrupt)
172 		padapter->HalFunc.enable_interrupt(padapter);
173 }
174 
rtw_hal_disable_interrupt(struct adapter * padapter)175 void rtw_hal_disable_interrupt(struct adapter *padapter)
176 {
177 	if (padapter->HalFunc.disable_interrupt)
178 		padapter->HalFunc.disable_interrupt(padapter);
179 }
180 
rtw_hal_check_ips_status(struct adapter * padapter)181 u8 rtw_hal_check_ips_status(struct adapter *padapter)
182 {
183 	u8 val = false;
184 	if (padapter->HalFunc.check_ips_status)
185 		val = padapter->HalFunc.check_ips_status(padapter);
186 
187 	return val;
188 }
189 
rtw_hal_xmitframe_enqueue(struct adapter * padapter,struct xmit_frame * pxmitframe)190 s32	rtw_hal_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
191 {
192 	if (padapter->HalFunc.hal_xmitframe_enqueue)
193 		return padapter->HalFunc.hal_xmitframe_enqueue(padapter, pxmitframe);
194 
195 	return false;
196 }
197 
rtw_hal_xmit(struct adapter * padapter,struct xmit_frame * pxmitframe)198 s32	rtw_hal_xmit(struct adapter *padapter, struct xmit_frame *pxmitframe)
199 {
200 	if (padapter->HalFunc.hal_xmit)
201 		return padapter->HalFunc.hal_xmit(padapter, pxmitframe);
202 
203 	return false;
204 }
205 
206 /*
207  * [IMPORTANT] This function would be run in interrupt context.
208  */
rtw_hal_mgnt_xmit(struct adapter * padapter,struct xmit_frame * pmgntframe)209 s32	rtw_hal_mgnt_xmit(struct adapter *padapter, struct xmit_frame *pmgntframe)
210 {
211 	s32 ret = _FAIL;
212 	update_mgntframe_attrib_addr(padapter, pmgntframe);
213 	/* pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; */
214 	/* pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; */
215 	/* memcpy(pmgntframe->attrib.ra, pwlanhdr->addr1, ETH_ALEN); */
216 
217 	if (padapter->securitypriv.binstallBIPkey == true) {
218 		if (IS_MCAST(pmgntframe->attrib.ra)) {
219 			pmgntframe->attrib.encrypt = _BIP_;
220 			/* pmgntframe->attrib.bswenc = true; */
221 		} else {
222 			pmgntframe->attrib.encrypt = _AES_;
223 			pmgntframe->attrib.bswenc = true;
224 		}
225 		rtw_mgmt_xmitframe_coalesce(padapter, pmgntframe->pkt, pmgntframe);
226 	}
227 
228 	if (padapter->HalFunc.mgnt_xmit)
229 		ret = padapter->HalFunc.mgnt_xmit(padapter, pmgntframe);
230 	return ret;
231 }
232 
rtw_hal_init_xmit_priv(struct adapter * padapter)233 s32	rtw_hal_init_xmit_priv(struct adapter *padapter)
234 {
235 	if (padapter->HalFunc.init_xmit_priv)
236 		return padapter->HalFunc.init_xmit_priv(padapter);
237 	return _FAIL;
238 }
239 
rtw_hal_free_xmit_priv(struct adapter * padapter)240 void rtw_hal_free_xmit_priv(struct adapter *padapter)
241 {
242 	if (padapter->HalFunc.free_xmit_priv)
243 		padapter->HalFunc.free_xmit_priv(padapter);
244 }
245 
rtw_hal_init_recv_priv(struct adapter * padapter)246 s32	rtw_hal_init_recv_priv(struct adapter *padapter)
247 {
248 	if (padapter->HalFunc.init_recv_priv)
249 		return padapter->HalFunc.init_recv_priv(padapter);
250 
251 	return _FAIL;
252 }
253 
rtw_hal_free_recv_priv(struct adapter * padapter)254 void rtw_hal_free_recv_priv(struct adapter *padapter)
255 {
256 	if (padapter->HalFunc.free_recv_priv)
257 		padapter->HalFunc.free_recv_priv(padapter);
258 }
259 
rtw_hal_update_ra_mask(struct sta_info * psta,u8 rssi_level)260 void rtw_hal_update_ra_mask(struct sta_info *psta, u8 rssi_level)
261 {
262 	struct adapter *padapter;
263 	struct mlme_priv *pmlmepriv;
264 
265 	if (!psta)
266 		return;
267 
268 	padapter = psta->padapter;
269 
270 	pmlmepriv = &(padapter->mlmepriv);
271 
272 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
273 		add_RATid(padapter, psta, rssi_level);
274 	else {
275 		if (padapter->HalFunc.UpdateRAMaskHandler)
276 			padapter->HalFunc.UpdateRAMaskHandler(padapter, psta->mac_id, rssi_level);
277 	}
278 }
279 
rtw_hal_add_ra_tid(struct adapter * padapter,u32 bitmap,u8 * arg,u8 rssi_level)280 void rtw_hal_add_ra_tid(struct adapter *padapter, u32 bitmap, u8 *arg, u8 rssi_level)
281 {
282 	if (padapter->HalFunc.Add_RateATid)
283 		padapter->HalFunc.Add_RateATid(padapter, bitmap, arg, rssi_level);
284 }
285 
286 /*Start specifical interface thread		*/
rtw_hal_start_thread(struct adapter * padapter)287 void rtw_hal_start_thread(struct adapter *padapter)
288 {
289 	if (padapter->HalFunc.run_thread)
290 		padapter->HalFunc.run_thread(padapter);
291 }
292 /*Start specifical interface thread		*/
rtw_hal_stop_thread(struct adapter * padapter)293 void rtw_hal_stop_thread(struct adapter *padapter)
294 {
295 	if (padapter->HalFunc.cancel_thread)
296 		padapter->HalFunc.cancel_thread(padapter);
297 }
298 
rtw_hal_read_bbreg(struct adapter * padapter,u32 RegAddr,u32 BitMask)299 u32 rtw_hal_read_bbreg(struct adapter *padapter, u32 RegAddr, u32 BitMask)
300 {
301 	u32 data = 0;
302 	if (padapter->HalFunc.read_bbreg)
303 		 data = padapter->HalFunc.read_bbreg(padapter, RegAddr, BitMask);
304 	return data;
305 }
rtw_hal_write_bbreg(struct adapter * padapter,u32 RegAddr,u32 BitMask,u32 Data)306 void rtw_hal_write_bbreg(struct adapter *padapter, u32 RegAddr, u32 BitMask, u32 Data)
307 {
308 	if (padapter->HalFunc.write_bbreg)
309 		padapter->HalFunc.write_bbreg(padapter, RegAddr, BitMask, Data);
310 }
311 
rtw_hal_read_rfreg(struct adapter * padapter,u32 eRFPath,u32 RegAddr,u32 BitMask)312 u32 rtw_hal_read_rfreg(struct adapter *padapter, u32 eRFPath, u32 RegAddr, u32 BitMask)
313 {
314 	u32 data = 0;
315 	if (padapter->HalFunc.read_rfreg)
316 		data = padapter->HalFunc.read_rfreg(padapter, eRFPath, RegAddr, BitMask);
317 	return data;
318 }
rtw_hal_write_rfreg(struct adapter * padapter,u32 eRFPath,u32 RegAddr,u32 BitMask,u32 Data)319 void rtw_hal_write_rfreg(struct adapter *padapter, u32 eRFPath, u32 RegAddr, u32 BitMask, u32 Data)
320 {
321 	if (padapter->HalFunc.write_rfreg)
322 		padapter->HalFunc.write_rfreg(padapter, eRFPath, RegAddr, BitMask, Data);
323 }
324 
rtw_hal_set_chan(struct adapter * padapter,u8 channel)325 void rtw_hal_set_chan(struct adapter *padapter, u8 channel)
326 {
327 	if (padapter->HalFunc.set_channel_handler)
328 		padapter->HalFunc.set_channel_handler(padapter, channel);
329 }
330 
rtw_hal_set_chnl_bw(struct adapter * padapter,u8 channel,enum channel_width Bandwidth,u8 Offset40,u8 Offset80)331 void rtw_hal_set_chnl_bw(struct adapter *padapter, u8 channel,
332 			 enum channel_width Bandwidth, u8 Offset40, u8 Offset80)
333 {
334 	if (padapter->HalFunc.set_chnl_bw_handler)
335 		padapter->HalFunc.set_chnl_bw_handler(padapter, channel,
336 						      Bandwidth, Offset40,
337 						      Offset80);
338 }
339 
rtw_hal_dm_watchdog(struct adapter * padapter)340 void rtw_hal_dm_watchdog(struct adapter *padapter)
341 {
342 	if (padapter->HalFunc.hal_dm_watchdog)
343 		padapter->HalFunc.hal_dm_watchdog(padapter);
344 }
345 
rtw_hal_dm_watchdog_in_lps(struct adapter * padapter)346 void rtw_hal_dm_watchdog_in_lps(struct adapter *padapter)
347 {
348 	if (adapter_to_pwrctl(padapter)->fw_current_in_ps_mode) {
349 		if (padapter->HalFunc.hal_dm_watchdog_in_lps)
350 			padapter->HalFunc.hal_dm_watchdog_in_lps(padapter); /* this function caller is in interrupt context */
351 	}
352 }
353 
beacon_timing_control(struct adapter * padapter)354 void beacon_timing_control(struct adapter *padapter)
355 {
356 	if (padapter->HalFunc.SetBeaconRelatedRegistersHandler)
357 		padapter->HalFunc.SetBeaconRelatedRegistersHandler(padapter);
358 }
359 
360 
rtw_hal_xmit_thread_handler(struct adapter * padapter)361 s32 rtw_hal_xmit_thread_handler(struct adapter *padapter)
362 {
363 	if (padapter->HalFunc.xmit_thread_handler)
364 		return padapter->HalFunc.xmit_thread_handler(padapter);
365 	return _FAIL;
366 }
367 
rtw_hal_notch_filter(struct adapter * adapter,bool enable)368 void rtw_hal_notch_filter(struct adapter *adapter, bool enable)
369 {
370 	if (adapter->HalFunc.hal_notch_filter)
371 		adapter->HalFunc.hal_notch_filter(adapter, enable);
372 }
373 
rtw_hal_reset_security_engine(struct adapter * adapter)374 void rtw_hal_reset_security_engine(struct adapter *adapter)
375 {
376 	if (adapter->HalFunc.hal_reset_security_engine)
377 		adapter->HalFunc.hal_reset_security_engine(adapter);
378 }
379 
rtw_hal_c2h_valid(struct adapter * adapter,u8 * buf)380 bool rtw_hal_c2h_valid(struct adapter *adapter, u8 *buf)
381 {
382 	return c2h_evt_valid((struct c2h_evt_hdr_88xx *)buf);
383 }
384 
rtw_hal_c2h_handler(struct adapter * adapter,u8 * c2h_evt)385 s32 rtw_hal_c2h_handler(struct adapter *adapter, u8 *c2h_evt)
386 {
387 	s32 ret = _FAIL;
388 	if (adapter->HalFunc.c2h_handler)
389 		ret = adapter->HalFunc.c2h_handler(adapter, c2h_evt);
390 	return ret;
391 }
392 
rtw_hal_c2h_id_filter_ccx(struct adapter * adapter)393 c2h_id_filter rtw_hal_c2h_id_filter_ccx(struct adapter *adapter)
394 {
395 	return adapter->HalFunc.c2h_id_filter_ccx;
396 }
397 
rtw_hal_is_disable_sw_channel_plan(struct adapter * padapter)398 s32 rtw_hal_is_disable_sw_channel_plan(struct adapter *padapter)
399 {
400 	return GET_HAL_DATA(padapter)->bDisableSWChannelPlan;
401 }
402 
rtw_hal_macid_sleep(struct adapter * padapter,u32 macid)403 s32 rtw_hal_macid_sleep(struct adapter *padapter, u32 macid)
404 {
405 	u8 support;
406 
407 	support = false;
408 	rtw_hal_get_def_var(padapter, HAL_DEF_MACID_SLEEP, &support);
409 	if (false == support)
410 		return _FAIL;
411 
412 	rtw_hal_set_hwreg(padapter, HW_VAR_MACID_SLEEP, (u8 *)&macid);
413 
414 	return _SUCCESS;
415 }
416 
rtw_hal_macid_wakeup(struct adapter * padapter,u32 macid)417 s32 rtw_hal_macid_wakeup(struct adapter *padapter, u32 macid)
418 {
419 	u8 support;
420 
421 	support = false;
422 	rtw_hal_get_def_var(padapter, HAL_DEF_MACID_SLEEP, &support);
423 	if (false == support)
424 		return _FAIL;
425 
426 	rtw_hal_set_hwreg(padapter, HW_VAR_MACID_WAKEUP, (u8 *)&macid);
427 
428 	return _SUCCESS;
429 }
430 
rtw_hal_fill_h2c_cmd(struct adapter * padapter,u8 ElementID,u32 CmdLen,u8 * pCmdBuffer)431 s32 rtw_hal_fill_h2c_cmd(struct adapter *padapter, u8 ElementID, u32 CmdLen, u8 *pCmdBuffer)
432 {
433 	s32 ret = _FAIL;
434 
435 	if (padapter->HalFunc.fill_h2c_cmd)
436 		ret = padapter->HalFunc.fill_h2c_cmd(padapter, ElementID, CmdLen, pCmdBuffer);
437 
438 	return ret;
439 }
440