1 /** @file wifi-internal.h
2  *
3  *  @brief WLAN Internal API
4  *
5  *  Copyright 2008-2024 NXP
6  *
7  *  SPDX-License-Identifier: BSD-3-Clause
8  *
9  */
10 
11 #ifndef __WIFI_INTERNAL_H__
12 #define __WIFI_INTERNAL_H__
13 
14 #include <limits.h>
15 
16 #if CONFIG_WPA_SUPP
17 #include "wifi_nxp_internal.h"
18 #endif
19 
20 #include <mlan_api.h>
21 
22 #include <osa.h>
23 #include <wifi_events.h>
24 #include <wifi-decl.h>
25 #if CONFIG_WPA_SUPP
26 #include <ieee802_11_defs.h>
27 #endif
28 
29 /* We don't see events coming in quick succession,
30  * MAX_EVENTS = 20 is fairly big value */
31 #define MAX_EVENTS 20
32 
33 struct bus_message
34 {
35     uint16_t event;
36     uint16_t reason;
37     void *data;
38 };
39 
40 typedef struct
41 {
42     int (*wifi_uap_downld_domain_params_p)(int band);
43 } wifi_uap_11d_apis_t;
44 
45 typedef struct mcast_filter
46 {
47     uint8_t mac_addr[MLAN_MAC_ADDR_LENGTH];
48     struct mcast_filter *next;
49 } mcast_filter;
50 
51 /* User response buffer parameters for hostcmd */
52 typedef struct _hostcmd_cfg
53 {
54     void *resp_buf;
55     uint32_t resp_buf_len;
56     uint32_t *reqd_resp_len;
57     bool is_hostcmd;
58 } hostcmd_cfg_t;
59 
60 /** This enum defines various thread events
61  * for which thread processing will occur */
62 enum wifi_thread_event_t
63 {
64     WIFI_EVENT_STA            = 1,
65     WIFI_EVENT_UAP            = 1 << 1,
66     WIFI_EVENT_SDIO           = 1 << 2,
67     WIFI_EVENT_SCAN           = 1 << 3,
68     WIFI_EVENT_TX_DATA        = 1 << 4,
69     WIFI_EVENT_TX_NULL_DATA   = 1 << 5,
70     WIFI_EVENT_TX_BYPASS_DATA = 1 << 6,
71 };
72 
73 typedef struct
74 {
75     const uint8_t *fw_start_addr;
76     size_t size;
77     t_u8 wifi_init_done;
78     t_u8 wifi_core_init_done;
79     OSA_TASK_HANDLE_DEFINE(wifi_drv_task_Handle);
80 
81 #ifndef RW610
82     OSA_TASK_HANDLE_DEFINE(wifi_core_task_Handle);
83 #endif
84     OSA_TASK_HANDLE_DEFINE(wifi_scan_task_Handle);
85 #if CONFIG_WMM
86     /** Thread handle for sending data */
87     OSA_TASK_HANDLE_DEFINE(wifi_drv_tx_task_Handle);
88 #endif
89     OSA_TASK_HANDLE_DEFINE(wifi_powersave_task_Handle);
90 
91     OSA_EVENT_HANDLE_DEFINE(wifi_event_Handle);
92 
93     osa_msgq_handle_t *wlc_mgr_event_queue;
94 
95 #if !CONFIG_WIFI_RX_REORDER
96     void (*data_input_callback)(const uint8_t interface, const uint8_t *buffer, const uint16_t len);
97 #endif
98 #if FSL_USDHC_ENABLE_SCATTER_GATHER_TRANSFER
99     void *(*wifi_get_rxbuf_desc)(t_u16 rx_len);
100 #endif
101     void (*amsdu_data_input_callback)(uint8_t interface, uint8_t *buffer, uint16_t len);
102     void (*deliver_packet_above_callback)(void *rxpd, t_u8 interface, t_void *lwip_pbuf);
103     bool (*wrapper_net_is_ip_or_ipv6_callback)(const t_u8 *buffer);
104 #if CONFIG_WIFI_RX_REORDER
105     void *(*gen_pbuf_from_data2)(t_u8 *payload, t_u16 datalen, void **p_payload);
106 #endif
107 
108 #if CONFIG_P2P
109     osa_msgq_handle_t *wfd_event_queue;
110 #endif
111     OSA_MUTEX_HANDLE_DEFINE(command_lock);
112 
113     OSA_SEMAPHORE_HANDLE_DEFINE(command_resp_sem);
114 
115     OSA_MUTEX_HANDLE_DEFINE(mcastf_mutex);
116 
117 #if CONFIG_WMM
118     /** Semaphore to protect data parameters */
119     OSA_SEMAPHORE_HANDLE_DEFINE(tx_data_sem);
120     /** Queue for sending data packets to fw */
121     OSA_MSGQ_HANDLE_DEFINE(tx_data, MAX_EVENTS, sizeof(struct bus_message));
122 #endif
123     unsigned last_sent_cmd_msec;
124 
125     /* Queue for events/data from low level interface driver */
126     OSA_MSGQ_HANDLE_DEFINE(io_events, MAX_EVENTS, sizeof(struct bus_message));
127     OSA_MSGQ_HANDLE_DEFINE(powersave_queue, MAX_EVENTS, sizeof(struct bus_message));
128 
129     mcast_filter *start_list;
130 
131     /*
132      * Usage note:
133      * There are a number of API's (for e.g. wifi_get_antenna()) which
134      * return some data in the buffer passed by the caller. Most of the
135      * time this data needs to be retrived from the firmware. This
136      * retrival happens in a different thread context. Hence, we need
137      * to store the buffer pointer passed by the user at a shared
138      * location. This pointer to used for this purpose.
139      *
140      * Note to the developer: Please ensure to set this to NULL after
141      * use in the wifi driver thread context.
142      */
143     void *cmd_resp_priv;
144     void *cmd_resp_ioctl;
145     /*
146      * In continuation with the description written for the
147      * cmd_resp_priv member above, the below member indicates the
148      * result of the retrieval operation from the firmware.
149      */
150     int cmd_resp_status;
151 
152     /*
153      * Store 11D support status in Wi-Fi driver.
154      */
155     bool enable_11d_support;
156     /*
157      * 11D support callback function
158      */
159     wifi_uap_11d_apis_t *uap_support_11d_apis;
160     /** Broadcast ssid control */
161     t_u8 hidden_ssid;
162     /** beacon period */
163     t_u16 beacon_period;
164     /** Wi-Fi Bandwidth */
165     t_u8 bandwidth;
166     /** channel switch time to send ECSA */
167     t_u8 chan_sw_count;
168     /** Sniffer channel number */
169     t_u8 chan_num;
170     /** HT Capability Info */
171     t_u16 ht_cap_info;
172     /** HTTX Cfg */
173     t_u16 ht_tx_cfg;
174 #if CONFIG_WIFI_FW_DEBUG
175     /** This function mount USB device.
176      *
177      * return WM_SUCCESS on success
178      * return -WM_FAIL on failure.
179      */
180     int (*wifi_usb_mount_cb)();
181     /** This function will open file for writing FW dump.
182      *
183      * \param[in] test_file_name Name of file to write FW dump data.
184      *
185      * \return WM_SUCCESS if opening of file is successful.
186      * \return -WM_FAIL in case of failure.
187      */
188     int (*wifi_usb_file_open_cb)(char *test_file_name);
189     /** This function will write data to file opened using wifi_usb_file_open_cb()
190      *
191      * \param[in] data Buffer containing FW dump data.
192      * \param[in] data_len Length of data that needs to be written.
193      *
194      * \return WM_SUCCESS if write is successful
195      * \return -WM_FAIL in case of failure.
196      */
197     int (*wifi_usb_file_write_cb)(uint8_t *data, size_t data_len);
198     /** This function will close the file on which FW dump is written.
199      *
200      * \note This will close file that is opened using wifi_usb_file_open_cb().
201      *
202      * \return WM_SUCCESS on success.
203      * \return -WM_FAIL on failure.
204      */
205     int (*wifi_usb_file_close_cb)();
206 #endif
207     /** Structure to store the response from WiFi firmware in
208      * response buffer provided by application layers
209      * structure also stores lengths for usage and validation internally*/
210     hostcmd_cfg_t hostcmd_cfg;
211     wlan_user_scan_cfg *g_user_scan_cfg;
212 
213     bool scan_stop;
214 #if CONFIG_WPA_SUPP
215     void *if_priv;
216     void *hapd_if_priv;
217     /** Supported bands info. @ref wifi_nxp_event_supported_band */
218     struct wifi_nxp_event_supported_band sband[WIFI_NXP_EVENT_GET_WIPHY_NUM_BANDS];
219     wifi_nxp_callbk_fns_t *supp_if_callbk_fns;
220     nxp_wifi_event_mlme_t mgmt_resp;
221     nxp_wifi_assoc_event_mlme_t assoc_resp;
222     nxp_wifi_event_mlme_t mgmt_rx;
223     nxp_wifi_event_eapol_mlme_t eapol_rx;
224     bool wpa_supp_scan;
225     bool external_scan;
226 #if CONFIG_WPA_SUPP_AP
227     bool hostapd_op;
228 #endif
229 #endif
230 } wm_wifi_t;
231 
232 extern wm_wifi_t wm_wifi;
233 extern bool split_scan_in_progress;
234 
235 /* fixme: This structure seems to have been removed from mlan. This was
236    copied from userif_ext.h file temporarily. Change the handling of events to
237    make it similar to what mlan does */
238 
239 /** Event structure */
240 typedef MLAN_PACK_START struct _Event_Ext_t
241 {
242     /** No of bytes in packet including this field */
243     uint16_t length;
244     /** Type: Event (3) */
245     uint16_t type;
246     /** Event ID */
247     uint16_t event_id;
248     /** BSS index number for multiple BSS support */
249     uint8_t bss_index;
250     /** BSS type */
251     uint8_t bss_type;
252     /** Reason code */
253     uint16_t reason_code;
254     /** Source MAC address */
255     uint8_t src_mac_addr[MLAN_MAC_ADDR_LENGTH];
256 } MLAN_PACK_END Event_Ext_t;
257 
258 typedef MLAN_PACK_START struct _mac_address
259 {
260     unsigned char addr[MLAN_MAC_ADDR_LENGTH];
261 } MLAN_PACK_END mac_address_t;
262 
263 typedef MLAN_PACK_START struct _nxp_wifi_acl_info
264 {
265     unsigned char acl_policy;
266     unsigned int num_mac_acl;
267     mac_address_t mac_acl[];
268 } MLAN_PACK_END nxp_wifi_acl_info_t;
269 
270 /* PACK_START struct ieee80211_hdr
271 {
272     t_u16 frame_control;
273     t_u16 duration_id;
274     t_u8 addr1[6];
275     t_u8 addr2[6];
276     t_u8 addr3[6];
277     t_u16 seq_ctrl;
278     t_u8 addr4[6];
279 } PACK_END; */
280 
281 /**
282  * This function handles events received from the firmware.
283  */
284 int wifi_handle_fw_event(struct bus_message *msg);
285 
286 /**
287  * This function is used to send events to the upper layer through the
288  * message queue registered by the upper layer.
289  */
290 int wifi_event_completion(enum wifi_event event, enum wifi_event_reason result, void *data);
291 
292 /**
293  * Use this function to know whether a split scan is in progress.
294  */
295 bool is_split_scan_complete(void);
296 
297 /**
298  * Waits for Command processing to complete and waits for command response
299  */
300 int wifi_wait_for_cmdresp(void *cmd_resp_priv);
301 #if CONFIG_FW_VDLL
302 /**
303  * Waits for Command processing to complete and waits for command response for VDLL
304  */
305 int wifi_wait_for_vdllcmdresp(void *cmd_resp_priv);
306 #endif
307 /**
308  * Register an event queue
309  *
310  * This queue is used to send events and command responses to the wifi
311  * driver from the stack dispatcher thread.
312  */
313 int bus_register_event_queue(osa_msgq_handle_t event_queue);
314 
315 /**
316  * De-register the event queue.
317  */
318 void bus_deregister_event_queue(void);
319 #if CONFIG_P2P
320 /**
321  * Register a special queue for WPS
322  */
323 int bus_register_special_queue(osa_msgq_handle_t special_queue);
324 
325 /**
326  * Deregister special queue
327  */
328 void bus_deregister_special_queue(void);
329 #endif
330 
331 /**
332  * Register DATA input function with SDIO driver.
333  *
334  * This queue is used to DATA frames to the wifi
335  * driver from the stack dispatcher thread.
336  */
337 int bus_register_data_input_function(int (*wifi_low_level_input)(const uint8_t interface,
338                                                                  const uint8_t *buffer,
339                                                                  const uint16_t len));
340 
341 /**
342  * De-register the DATA input function with SDIO driver.
343  */
344 void bus_deregister_data_input_funtion(void);
345 
346 /*
347  * @internal
348  *
349  *
350  */
351 int wifi_get_command_lock(void);
352 
353 int wifi_put_command_resp_sem(void);
354 
355 /*
356  * @internal
357  *
358  *
359  */
360 int wifi_put_command_lock(void);
361 
362 #if ((CONFIG_11MC) || (CONFIG_11AZ)) && (CONFIG_WLS_CSI_PROC)
363 /*
364  * @internal
365  *
366  *
367  */
368 int wifi_get_wls_csi_sem(void);
369 
370 /*
371  * @internal
372  *
373  *
374  */
375 int wifi_put_wls_csi_sem(void);
376 #endif
377 
378 /*
379  * Process the command reponse received from the firmware.
380  *
381  * Change the type of param below to HostCmd_DS_COMMAND after mlan
382  * integration complete and then move it to header file.
383  */
384 int wifi_process_cmd_response(HostCmd_DS_COMMAND *resp);
385 
386 /*
387  * @internal
388  *
389  *
390  */
391 void *wifi_mem_malloc_cmdrespbuf(void);
392 
393 /*
394  * @internal
395  *
396  *
397  */
398 void *wifi_malloc_eventbuf(size_t size);
399 void wifi_free_eventbuf(void *buffer);
400 
401 void wifi_uap_handle_cmd_resp(HostCmd_DS_COMMAND *resp);
402 
403 mlan_status wrapper_moal_malloc(t_void *pmoal_handle, t_u32 size, t_u32 flag, t_u8 **ppbuf);
404 mlan_status wrapper_moal_mfree(t_void *pmoal_handle, t_u8 *pbuf);
405 
406 #if defined(RW610)
407 int wifi_imu_lock(void);
408 void wifi_imu_unlock(void);
409 #else
410 int wifi_sdio_lock(void);
411 void wifi_sdio_unlock(void);
412 #endif
413 
414 #if CONFIG_WIFI_IND_RESET
415 bool wifi_ind_reset_in_progress(void);
416 void wifi_ind_reset_start(void);
417 void wifi_ind_reset_stop(void);
418 int wifi_ind_reset_lock(void);
419 void wifi_ind_reset_unlock(void);
420 #endif
421 
422 mlan_status wrapper_wlan_cmd_mgmt_ie(int bss_type, void *buffer, unsigned int len, t_u16 action);
423 
424 /**
425  * This function should be called when user scan is
426  * finished with success/failure.
427  *
428  */
429 void wifi_user_scan_config_cleanup(void);
430 
431 /**
432  * This function should be called to wait for scan task done before resetting.
433  *
434  */
435 void wifi_scan_stop(void);
436 #if CONFIG_WPA_SUPP
437 void wpa_supp_handle_link_lost(mlan_private *priv);
438 
439 int wifi_set_scan_ies(void *ie, size_t ie_len);
440 #if CONFIG_WPA_SUPP_WPS
441 bool wifi_nxp_wps_session_enable(void);
442 #endif
443 
444 int wifi_setup_ht_cap(t_u16 *ht_capab, t_u8 *mcs_set, t_u8 *a_mpdu_params, t_u8 band);
445 void wifi_setup_channel_info(void *channels, int num_channels, t_u8 band);
446 
447 #if CONFIG_11AC
448 int wifi_setup_vht_cap(t_u32 *vht_capab, t_u8 *vht_mcs_set, t_u8 band);
449 #endif
450 
451 #if CONFIG_11AX
452 int wifi_setup_he_cap(nxp_wifi_he_capabilities *he_cap, t_u8 band);
453 #endif
454 int wifi_nxp_send_assoc(nxp_wifi_assoc_info_t *assoc_info);
455 int wifi_nxp_init_ap(nxp_wifi_ap_info_t *params);
456 int wifi_nxp_send_mlme(unsigned int bss_type, int channel, unsigned int wait_time, const t_u8 *data, size_t data_len);
457 int wifi_remain_on_channel(const bool status, const uint8_t channel, const uint32_t duration);
458 int wifi_nxp_beacon_config(nxp_wifi_ap_info_t *params);
459 int wifi_set_uap_rts(int rts_threshold);
460 int wifi_set_uap_frag(int frag_threshold);
461 int wifi_nxp_sta_add(nxp_wifi_sta_info_t *params);
462 int wifi_nxp_sta_remove(const uint8_t *addr);
463 void wifi_nxp_uap_disconnect(mlan_private *priv, t_u16 reason_code, t_u8 *mac);
464 int wifi_nxp_stop_ap(void);
465 int wifi_nxp_set_acl(nxp_wifi_acl_info_t *acl_params);
466 int wifi_nxp_set_country(unsigned int bss_type, const char *alpha2);
467 int wifi_nxp_get_country(unsigned int bss_type, char *alpha2);
468 int wifi_nxp_get_signal(unsigned int bss_type, nxp_wifi_signal_info_t *signal_params);
469 int wifi_nxp_scan_res_num(void);
470 int wifi_nxp_scan_res_get2(t_u32 table_idx, nxp_wifi_event_new_scan_result_t *scan_res);
471 #endif /* CONFIG_WPA_SUPP */
472 
473 #if CONFIG_WIFI_RX_REORDER
474 int wrapper_wlan_handle_rx_packet(t_u16 datalen, RxPD *rxpd, void *p, void *payload);
475 #endif
476 
477 #if CONFIG_WMM
478 int send_wifi_driver_tx_data_event(t_u8 interface);
479 int send_wifi_driver_tx_null_data_event(t_u8 interface);
480 #endif
481 
482 #endif /* __WIFI_INTERNAL_H__ */
483