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 
185 	if (padapter->HalFunc.check_ips_status)
186 		val = padapter->HalFunc.check_ips_status(padapter);
187 
188 	return val;
189 }
190 
rtw_hal_xmitframe_enqueue(struct adapter * padapter,struct xmit_frame * pxmitframe)191 s32	rtw_hal_xmitframe_enqueue(struct adapter *padapter, struct xmit_frame *pxmitframe)
192 {
193 	if (padapter->HalFunc.hal_xmitframe_enqueue)
194 		return padapter->HalFunc.hal_xmitframe_enqueue(padapter, pxmitframe);
195 
196 	return false;
197 }
198 
rtw_hal_xmit(struct adapter * padapter,struct xmit_frame * pxmitframe)199 s32	rtw_hal_xmit(struct adapter *padapter, struct xmit_frame *pxmitframe)
200 {
201 	if (padapter->HalFunc.hal_xmit)
202 		return padapter->HalFunc.hal_xmit(padapter, pxmitframe);
203 
204 	return false;
205 }
206 
207 /*
208  * [IMPORTANT] This function would be run in interrupt context.
209  */
rtw_hal_mgnt_xmit(struct adapter * padapter,struct xmit_frame * pmgntframe)210 s32	rtw_hal_mgnt_xmit(struct adapter *padapter, struct xmit_frame *pmgntframe)
211 {
212 	s32 ret = _FAIL;
213 
214 	update_mgntframe_attrib_addr(padapter, pmgntframe);
215 	/* pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET; */
216 	/* pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; */
217 	/* memcpy(pmgntframe->attrib.ra, pwlanhdr->addr1, ETH_ALEN); */
218 
219 	if (padapter->securitypriv.binstallBIPkey == true) {
220 		if (IS_MCAST(pmgntframe->attrib.ra)) {
221 			pmgntframe->attrib.encrypt = _BIP_;
222 			/* pmgntframe->attrib.bswenc = true; */
223 		} else {
224 			pmgntframe->attrib.encrypt = _AES_;
225 			pmgntframe->attrib.bswenc = true;
226 		}
227 		rtw_mgmt_xmitframe_coalesce(padapter, pmgntframe->pkt, pmgntframe);
228 	}
229 
230 	if (padapter->HalFunc.mgnt_xmit)
231 		ret = padapter->HalFunc.mgnt_xmit(padapter, pmgntframe);
232 	return ret;
233 }
234 
rtw_hal_init_xmit_priv(struct adapter * padapter)235 s32	rtw_hal_init_xmit_priv(struct adapter *padapter)
236 {
237 	if (padapter->HalFunc.init_xmit_priv)
238 		return padapter->HalFunc.init_xmit_priv(padapter);
239 	return _FAIL;
240 }
241 
rtw_hal_free_xmit_priv(struct adapter * padapter)242 void rtw_hal_free_xmit_priv(struct adapter *padapter)
243 {
244 	if (padapter->HalFunc.free_xmit_priv)
245 		padapter->HalFunc.free_xmit_priv(padapter);
246 }
247 
rtw_hal_init_recv_priv(struct adapter * padapter)248 s32	rtw_hal_init_recv_priv(struct adapter *padapter)
249 {
250 	if (padapter->HalFunc.init_recv_priv)
251 		return padapter->HalFunc.init_recv_priv(padapter);
252 
253 	return _FAIL;
254 }
255 
rtw_hal_free_recv_priv(struct adapter * padapter)256 void rtw_hal_free_recv_priv(struct adapter *padapter)
257 {
258 	if (padapter->HalFunc.free_recv_priv)
259 		padapter->HalFunc.free_recv_priv(padapter);
260 }
261 
rtw_hal_update_ra_mask(struct sta_info * psta,u8 rssi_level)262 void rtw_hal_update_ra_mask(struct sta_info *psta, u8 rssi_level)
263 {
264 	struct adapter *padapter;
265 	struct mlme_priv *pmlmepriv;
266 
267 	if (!psta)
268 		return;
269 
270 	padapter = psta->padapter;
271 
272 	pmlmepriv = &(padapter->mlmepriv);
273 
274 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
275 		add_RATid(padapter, psta, rssi_level);
276 	else {
277 		if (padapter->HalFunc.UpdateRAMaskHandler)
278 			padapter->HalFunc.UpdateRAMaskHandler(padapter, psta->mac_id, rssi_level);
279 	}
280 }
281 
rtw_hal_add_ra_tid(struct adapter * padapter,u32 bitmap,u8 * arg,u8 rssi_level)282 void rtw_hal_add_ra_tid(struct adapter *padapter, u32 bitmap, u8 *arg, u8 rssi_level)
283 {
284 	if (padapter->HalFunc.Add_RateATid)
285 		padapter->HalFunc.Add_RateATid(padapter, bitmap, arg, rssi_level);
286 }
287 
288 /*Start specifical interface thread		*/
rtw_hal_start_thread(struct adapter * padapter)289 void rtw_hal_start_thread(struct adapter *padapter)
290 {
291 	if (padapter->HalFunc.run_thread)
292 		padapter->HalFunc.run_thread(padapter);
293 }
294 /*Start specifical interface thread		*/
rtw_hal_stop_thread(struct adapter * padapter)295 void rtw_hal_stop_thread(struct adapter *padapter)
296 {
297 	if (padapter->HalFunc.cancel_thread)
298 		padapter->HalFunc.cancel_thread(padapter);
299 }
300 
rtw_hal_read_bbreg(struct adapter * padapter,u32 RegAddr,u32 BitMask)301 u32 rtw_hal_read_bbreg(struct adapter *padapter, u32 RegAddr, u32 BitMask)
302 {
303 	u32 data = 0;
304 
305 	if (padapter->HalFunc.read_bbreg)
306 		 data = padapter->HalFunc.read_bbreg(padapter, RegAddr, BitMask);
307 	return data;
308 }
rtw_hal_write_bbreg(struct adapter * padapter,u32 RegAddr,u32 BitMask,u32 Data)309 void rtw_hal_write_bbreg(struct adapter *padapter, u32 RegAddr, u32 BitMask, u32 Data)
310 {
311 	if (padapter->HalFunc.write_bbreg)
312 		padapter->HalFunc.write_bbreg(padapter, RegAddr, BitMask, Data);
313 }
314 
rtw_hal_read_rfreg(struct adapter * padapter,u32 eRFPath,u32 RegAddr,u32 BitMask)315 u32 rtw_hal_read_rfreg(struct adapter *padapter, u32 eRFPath, u32 RegAddr, u32 BitMask)
316 {
317 	u32 data = 0;
318 
319 	if (padapter->HalFunc.read_rfreg)
320 		data = padapter->HalFunc.read_rfreg(padapter, eRFPath, RegAddr, BitMask);
321 	return data;
322 }
rtw_hal_write_rfreg(struct adapter * padapter,u32 eRFPath,u32 RegAddr,u32 BitMask,u32 Data)323 void rtw_hal_write_rfreg(struct adapter *padapter, u32 eRFPath, u32 RegAddr, u32 BitMask, u32 Data)
324 {
325 	if (padapter->HalFunc.write_rfreg)
326 		padapter->HalFunc.write_rfreg(padapter, eRFPath, RegAddr, BitMask, Data);
327 }
328 
rtw_hal_set_chan(struct adapter * padapter,u8 channel)329 void rtw_hal_set_chan(struct adapter *padapter, u8 channel)
330 {
331 	if (padapter->HalFunc.set_channel_handler)
332 		padapter->HalFunc.set_channel_handler(padapter, channel);
333 }
334 
rtw_hal_set_chnl_bw(struct adapter * padapter,u8 channel,enum channel_width Bandwidth,u8 Offset40,u8 Offset80)335 void rtw_hal_set_chnl_bw(struct adapter *padapter, u8 channel,
336 			 enum channel_width Bandwidth, u8 Offset40, u8 Offset80)
337 {
338 	if (padapter->HalFunc.set_chnl_bw_handler)
339 		padapter->HalFunc.set_chnl_bw_handler(padapter, channel,
340 						      Bandwidth, Offset40,
341 						      Offset80);
342 }
343 
rtw_hal_dm_watchdog(struct adapter * padapter)344 void rtw_hal_dm_watchdog(struct adapter *padapter)
345 {
346 	if (padapter->HalFunc.hal_dm_watchdog)
347 		padapter->HalFunc.hal_dm_watchdog(padapter);
348 }
349 
rtw_hal_dm_watchdog_in_lps(struct adapter * padapter)350 void rtw_hal_dm_watchdog_in_lps(struct adapter *padapter)
351 {
352 	if (adapter_to_pwrctl(padapter)->fw_current_in_ps_mode) {
353 		if (padapter->HalFunc.hal_dm_watchdog_in_lps)
354 			padapter->HalFunc.hal_dm_watchdog_in_lps(padapter); /* this function caller is in interrupt context */
355 	}
356 }
357 
beacon_timing_control(struct adapter * padapter)358 void beacon_timing_control(struct adapter *padapter)
359 {
360 	if (padapter->HalFunc.SetBeaconRelatedRegistersHandler)
361 		padapter->HalFunc.SetBeaconRelatedRegistersHandler(padapter);
362 }
363 
364 
rtw_hal_xmit_thread_handler(struct adapter * padapter)365 s32 rtw_hal_xmit_thread_handler(struct adapter *padapter)
366 {
367 	if (padapter->HalFunc.xmit_thread_handler)
368 		return padapter->HalFunc.xmit_thread_handler(padapter);
369 	return _FAIL;
370 }
371 
rtw_hal_notch_filter(struct adapter * adapter,bool enable)372 void rtw_hal_notch_filter(struct adapter *adapter, bool enable)
373 {
374 	if (adapter->HalFunc.hal_notch_filter)
375 		adapter->HalFunc.hal_notch_filter(adapter, enable);
376 }
377 
rtw_hal_reset_security_engine(struct adapter * adapter)378 void rtw_hal_reset_security_engine(struct adapter *adapter)
379 {
380 	if (adapter->HalFunc.hal_reset_security_engine)
381 		adapter->HalFunc.hal_reset_security_engine(adapter);
382 }
383 
rtw_hal_c2h_valid(struct adapter * adapter,u8 * buf)384 bool rtw_hal_c2h_valid(struct adapter *adapter, u8 *buf)
385 {
386 	return c2h_evt_valid((struct c2h_evt_hdr_88xx *)buf);
387 }
388 
rtw_hal_c2h_handler(struct adapter * adapter,u8 * c2h_evt)389 s32 rtw_hal_c2h_handler(struct adapter *adapter, u8 *c2h_evt)
390 {
391 	s32 ret = _FAIL;
392 
393 	if (adapter->HalFunc.c2h_handler)
394 		ret = adapter->HalFunc.c2h_handler(adapter, c2h_evt);
395 	return ret;
396 }
397 
rtw_hal_c2h_id_filter_ccx(struct adapter * adapter)398 c2h_id_filter rtw_hal_c2h_id_filter_ccx(struct adapter *adapter)
399 {
400 	return adapter->HalFunc.c2h_id_filter_ccx;
401 }
402 
rtw_hal_is_disable_sw_channel_plan(struct adapter * padapter)403 s32 rtw_hal_is_disable_sw_channel_plan(struct adapter *padapter)
404 {
405 	return GET_HAL_DATA(padapter)->bDisableSWChannelPlan;
406 }
407 
rtw_hal_macid_sleep(struct adapter * padapter,u32 macid)408 s32 rtw_hal_macid_sleep(struct adapter *padapter, u32 macid)
409 {
410 	u8 support;
411 
412 	support = false;
413 	rtw_hal_get_def_var(padapter, HAL_DEF_MACID_SLEEP, &support);
414 	if (false == support)
415 		return _FAIL;
416 
417 	rtw_hal_set_hwreg(padapter, HW_VAR_MACID_SLEEP, (u8 *)&macid);
418 
419 	return _SUCCESS;
420 }
421 
rtw_hal_macid_wakeup(struct adapter * padapter,u32 macid)422 s32 rtw_hal_macid_wakeup(struct adapter *padapter, u32 macid)
423 {
424 	u8 support;
425 
426 	support = false;
427 	rtw_hal_get_def_var(padapter, HAL_DEF_MACID_SLEEP, &support);
428 	if (false == support)
429 		return _FAIL;
430 
431 	rtw_hal_set_hwreg(padapter, HW_VAR_MACID_WAKEUP, (u8 *)&macid);
432 
433 	return _SUCCESS;
434 }
435 
rtw_hal_fill_h2c_cmd(struct adapter * padapter,u8 ElementID,u32 CmdLen,u8 * pCmdBuffer)436 s32 rtw_hal_fill_h2c_cmd(struct adapter *padapter, u8 ElementID, u32 CmdLen, u8 *pCmdBuffer)
437 {
438 	s32 ret = _FAIL;
439 
440 	if (padapter->HalFunc.fill_h2c_cmd)
441 		ret = padapter->HalFunc.fill_h2c_cmd(padapter, ElementID, CmdLen, pCmdBuffer);
442 
443 	return ret;
444 }
445