xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/infineon/bcmdhd/bcmevent.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * bcmevent read-only data shared by kernel or app layers
3  *
4  * Portions of this code are copyright (c) 2022 Cypress Semiconductor Corporation
5  *
6  * Copyright (C) 1999-2017, Broadcom Corporation
7  *
8  *      Unless you and Broadcom execute a separate written software license
9  * agreement governing use of this software, this software is licensed to you
10  * under the terms of the GNU General Public License version 2 (the "GPL"),
11  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12  * following added to such license:
13  *
14  *      As a special exception, the copyright holders of this software give you
15  * permission to link this software with independent modules, and to copy and
16  * distribute the resulting executable under terms of your choice, provided that
17  * you also meet, for each linked independent module, the terms and conditions of
18  * the license of that module.  An independent module is a module which is not
19  * derived from this software.  The special exception does not apply to any
20  * modifications of the software.
21  *
22  *      Notwithstanding the above, under no circumstances may you combine this
23  * software in any way with any other Broadcom software provided under a license
24  * other than the GPL, without Broadcom's express prior written consent.
25  *
26  *
27  * <<Broadcom-WL-IPTag/Open:>>
28  *
29  * $Id: bcmevent.c 702756 2017-06-03 17:20:27Z $
30  */
31 
32 #include <typedefs.h>
33 #include <bcmutils.h>
34 #include <bcmendian.h>
35 #include <ethernet.h>
36 #include <bcmeth.h>
37 #include <bcmevent.h>
38 #include <802.11.h>
39 
40 /* Table of event name strings for UIs and debugging dumps */
41 typedef struct {
42 	uint event;
43 	const char *name;
44 } bcmevent_name_str_t;
45 
46 /* Use the actual name for event tracing */
47 #define BCMEVENT_NAME(_event) {(_event), #_event}
48 
49 static const bcmevent_name_str_t bcmevent_names[] = {
50 	BCMEVENT_NAME(WLC_E_SET_SSID),
51 	BCMEVENT_NAME(WLC_E_JOIN),
52 	BCMEVENT_NAME(WLC_E_START),
53 	BCMEVENT_NAME(WLC_E_AUTH),
54 	BCMEVENT_NAME(WLC_E_AUTH_IND),
55 	BCMEVENT_NAME(WLC_E_DEAUTH),
56 	BCMEVENT_NAME(WLC_E_DEAUTH_IND),
57 	BCMEVENT_NAME(WLC_E_ASSOC),
58 	BCMEVENT_NAME(WLC_E_ASSOC_IND),
59 	BCMEVENT_NAME(WLC_E_REASSOC),
60 	BCMEVENT_NAME(WLC_E_REASSOC_IND),
61 	BCMEVENT_NAME(WLC_E_DISASSOC),
62 	BCMEVENT_NAME(WLC_E_DISASSOC_IND),
63 	BCMEVENT_NAME(WLC_E_QUIET_START),
64 	BCMEVENT_NAME(WLC_E_QUIET_END),
65 	BCMEVENT_NAME(WLC_E_BEACON_RX),
66 	BCMEVENT_NAME(WLC_E_LINK),
67 	BCMEVENT_NAME(WLC_E_MIC_ERROR),
68 	BCMEVENT_NAME(WLC_E_NDIS_LINK),
69 	BCMEVENT_NAME(WLC_E_ROAM),
70 	BCMEVENT_NAME(WLC_E_TXFAIL),
71 	BCMEVENT_NAME(WLC_E_PMKID_CACHE),
72 	BCMEVENT_NAME(WLC_E_RETROGRADE_TSF),
73 	BCMEVENT_NAME(WLC_E_PRUNE),
74 	BCMEVENT_NAME(WLC_E_AUTOAUTH),
75 	BCMEVENT_NAME(WLC_E_EAPOL_MSG),
76 	BCMEVENT_NAME(WLC_E_SCAN_COMPLETE),
77 	BCMEVENT_NAME(WLC_E_IND_DOS_STATUS),
78 	BCMEVENT_NAME(WLC_E_ADDTS_IND),
79 	BCMEVENT_NAME(WLC_E_DELTS_IND),
80 	BCMEVENT_NAME(WLC_E_BCNSENT_IND),
81 	BCMEVENT_NAME(WLC_E_BCNRX_MSG),
82 	BCMEVENT_NAME(WLC_E_BCNLOST_MSG),
83 	BCMEVENT_NAME(WLC_E_ROAM_PREP),
84 	BCMEVENT_NAME(WLC_E_PFN_NET_FOUND),
85 	BCMEVENT_NAME(WLC_E_PFN_SCAN_ALLGONE),
86 	BCMEVENT_NAME(WLC_E_PFN_NET_LOST),
87 	BCMEVENT_NAME(WLC_E_JOIN_START),
88 	BCMEVENT_NAME(WLC_E_ROAM_START),
89 	BCMEVENT_NAME(WLC_E_ASSOC_START),
90 #if defined(IBSS_PEER_DISCOVERY_EVENT)
91 	BCMEVENT_NAME(WLC_E_IBSS_ASSOC),
92 #endif /* defined(IBSS_PEER_DISCOVERY_EVENT) */
93 	BCMEVENT_NAME(WLC_E_RADIO),
94 	BCMEVENT_NAME(WLC_E_PSM_WATCHDOG),
95 	BCMEVENT_NAME(WLC_E_PROBREQ_MSG),
96 	BCMEVENT_NAME(WLC_E_SCAN_CONFIRM_IND),
97 	BCMEVENT_NAME(WLC_E_PSK_SUP),
98 	BCMEVENT_NAME(WLC_E_COUNTRY_CODE_CHANGED),
99 	BCMEVENT_NAME(WLC_E_EXCEEDED_MEDIUM_TIME),
100 	BCMEVENT_NAME(WLC_E_ICV_ERROR),
101 	BCMEVENT_NAME(WLC_E_UNICAST_DECODE_ERROR),
102 	BCMEVENT_NAME(WLC_E_MULTICAST_DECODE_ERROR),
103 	BCMEVENT_NAME(WLC_E_TRACE),
104 	BCMEVENT_NAME(WLC_E_IF),
105 #ifdef WLP2P
106 	BCMEVENT_NAME(WLC_E_P2P_DISC_LISTEN_COMPLETE),
107 #endif // endif
108 	BCMEVENT_NAME(WLC_E_RSSI),
109 	BCMEVENT_NAME(WLC_E_PFN_SCAN_COMPLETE),
110 	BCMEVENT_NAME(WLC_E_ACTION_FRAME),
111 	BCMEVENT_NAME(WLC_E_ACTION_FRAME_RX),
112 	BCMEVENT_NAME(WLC_E_ACTION_FRAME_COMPLETE),
113 #ifdef BCMWAPI_WAI
114 	BCMEVENT_NAME(WLC_E_WAI_STA_EVENT),
115 	BCMEVENT_NAME(WLC_E_WAI_MSG),
116 #endif /* BCMWAPI_WAI */
117 	BCMEVENT_NAME(WLC_E_ESCAN_RESULT),
118 	BCMEVENT_NAME(WLC_E_ACTION_FRAME_OFF_CHAN_COMPLETE),
119 #ifdef WLP2P
120 	BCMEVENT_NAME(WLC_E_PROBRESP_MSG),
121 	BCMEVENT_NAME(WLC_E_P2P_PROBREQ_MSG),
122 #endif // endif
123 #ifdef PROP_TXSTATUS
124 	BCMEVENT_NAME(WLC_E_FIFO_CREDIT_MAP),
125 #endif // endif
126 	BCMEVENT_NAME(WLC_E_WAKE_EVENT),
127 	BCMEVENT_NAME(WLC_E_DCS_REQUEST),
128 	BCMEVENT_NAME(WLC_E_RM_COMPLETE),
129 	BCMEVENT_NAME(WLC_E_OVERLAY_REQ),
130 	BCMEVENT_NAME(WLC_E_CSA_COMPLETE_IND),
131 	BCMEVENT_NAME(WLC_E_EXCESS_PM_WAKE_EVENT),
132 	BCMEVENT_NAME(WLC_E_PFN_SCAN_NONE),
133 	BCMEVENT_NAME(WLC_E_PFN_SCAN_ALLGONE),
134 #ifdef SOFTAP
135 	BCMEVENT_NAME(WLC_E_GTK_PLUMBED),
136 #endif // endif
137 	BCMEVENT_NAME(WLC_E_ASSOC_REQ_IE),
138 	BCMEVENT_NAME(WLC_E_ASSOC_RESP_IE),
139 	BCMEVENT_NAME(WLC_E_BEACON_FRAME_RX),
140 #ifdef WLTDLS
141 	BCMEVENT_NAME(WLC_E_TDLS_PEER_EVENT),
142 #endif /* WLTDLS */
143 	BCMEVENT_NAME(WLC_E_NATIVE),
144 #ifdef WLPKTDLYSTAT
145 	BCMEVENT_NAME(WLC_E_PKTDELAY_IND),
146 #endif /* WLPKTDLYSTAT */
147 	BCMEVENT_NAME(WLC_E_SERVICE_FOUND),
148 	BCMEVENT_NAME(WLC_E_GAS_FRAGMENT_RX),
149 	BCMEVENT_NAME(WLC_E_GAS_COMPLETE),
150 	BCMEVENT_NAME(WLC_E_P2PO_ADD_DEVICE),
151 	BCMEVENT_NAME(WLC_E_P2PO_DEL_DEVICE),
152 #ifdef WLWNM
153 	BCMEVENT_NAME(WLC_E_WNM_STA_SLEEP),
154 #endif /* WLWNM */
155 #if defined(WL_PROXDETECT) || defined(RTT_SUPPORT)
156 	BCMEVENT_NAME(WLC_E_PROXD),
157 #endif // endif
158 	BCMEVENT_NAME(WLC_E_CCA_CHAN_QUAL),
159 	BCMEVENT_NAME(WLC_E_BSSID),
160 #ifdef PROP_TXSTATUS
161 	BCMEVENT_NAME(WLC_E_BCMC_CREDIT_SUPPORT),
162 #endif // endif
163 	BCMEVENT_NAME(WLC_E_PSTA_PRIMARY_INTF_IND),
164 	BCMEVENT_NAME(WLC_E_TXFAIL_THRESH),
165 #ifdef WLAIBSS
166 	BCMEVENT_NAME(WLC_E_AIBSS_TXFAIL),
167 #endif /* WLAIBSS */
168 #ifdef GSCAN_SUPPORT
169 	BCMEVENT_NAME(WLC_E_PFN_GSCAN_FULL_RESULT),
170 	BCMEVENT_NAME(WLC_E_PFN_SSID_EXT),
171 #endif /* GSCAN_SUPPORT */
172 #ifdef WLBSSLOAD_REPORT
173 	BCMEVENT_NAME(WLC_E_BSS_LOAD),
174 #endif // endif
175 #if defined(BT_WIFI_HANDOVER) || defined(WL_TBOW)
176 	BCMEVENT_NAME(WLC_E_BT_WIFI_HANDOVER_REQ),
177 #endif // endif
178 #ifdef WLFBT
179 	BCMEVENT_NAME(WLC_E_FBT),
180 #endif /* WLFBT */
181 	BCMEVENT_NAME(WLC_E_AUTHORIZED),
182 	BCMEVENT_NAME(WLC_E_PROBREQ_MSG_RX),
183 	BCMEVENT_NAME(WLC_E_CSA_START_IND),
184 	BCMEVENT_NAME(WLC_E_CSA_DONE_IND),
185 	BCMEVENT_NAME(WLC_E_CSA_FAILURE_IND),
186 	BCMEVENT_NAME(WLC_E_RMC_EVENT),
187 	BCMEVENT_NAME(WLC_E_DPSTA_INTF_IND),
188 	BCMEVENT_NAME(WLC_E_ALLOW_CREDIT_BORROW),
189 	BCMEVENT_NAME(WLC_E_MSCH),
190 	BCMEVENT_NAME(WLC_E_ULP),
191 	BCMEVENT_NAME(WLC_E_NAN),
192 	BCMEVENT_NAME(WLC_E_PKT_FILTER),
193 	BCMEVENT_NAME(WLC_E_DMA_TXFLUSH_COMPLETE),
194 	BCMEVENT_NAME(WLC_E_PSK_AUTH),
195 	BCMEVENT_NAME(WLC_E_SDB_TRANSITION),
196 	BCMEVENT_NAME(WLC_E_PFN_SCAN_BACKOFF),
197 	BCMEVENT_NAME(WLC_E_PFN_BSSID_SCAN_BACKOFF),
198 	BCMEVENT_NAME(WLC_E_AGGR_EVENT),
199 	BCMEVENT_NAME(WLC_E_TVPM_MITIGATION),
200 #ifdef WL_NAN
201 	BCMEVENT_NAME(WLC_E_NAN_CRITICAL),
202 	BCMEVENT_NAME(WLC_E_NAN_NON_CRITICAL),
203 	BCMEVENT_NAME(WLC_E_NAN),
204 #endif /* WL_NAN */
205 	BCMEVENT_NAME(WLC_E_RPSNOA),
206 	BCMEVENT_NAME(WLC_E_PHY_CAL),
207 	BCMEVENT_NAME(WLC_E_WA_LQM),
208 	BCMEVENT_NAME(WLC_E_OVERTEMP),
209 };
210 
bcmevent_get_name(uint event_type)211 const char *bcmevent_get_name(uint event_type)
212 {
213 	/* note:  first coded this as a static const but some
214 	 * ROMs already have something called event_name so
215 	 * changed it so we don't have a variable for the
216 	 * 'unknown string
217 	 */
218 	const char *event_name = NULL;
219 
220 	uint idx;
221 	for (idx = 0; idx < (uint)ARRAYSIZE(bcmevent_names); idx++) {
222 
223 		if (bcmevent_names[idx].event == event_type) {
224 			event_name = bcmevent_names[idx].name;
225 			break;
226 		}
227 	}
228 
229 	/* if we find an event name in the array, return it.
230 	 * otherwise return unknown string.
231 	 */
232 	return ((event_name) ? event_name : "Unknown Event");
233 }
234 
235 void
wl_event_to_host_order(wl_event_msg_t * evt)236 wl_event_to_host_order(wl_event_msg_t * evt)
237 {
238 	/* Event struct members passed from dongle to host are stored in network
239 	* byte order. Convert all members to host-order.
240 	*/
241 	evt->event_type = ntoh32(evt->event_type);
242 	evt->flags = ntoh16(evt->flags);
243 	evt->status = ntoh32(evt->status);
244 	evt->reason = ntoh32(evt->reason);
245 	evt->auth_type = ntoh32(evt->auth_type);
246 	evt->datalen = ntoh32(evt->datalen);
247 	evt->version = ntoh16(evt->version);
248 }
249 
250 void
wl_event_to_network_order(wl_event_msg_t * evt)251 wl_event_to_network_order(wl_event_msg_t * evt)
252 {
253 	/* Event struct members passed from dongle to host are stored in network
254 	* byte order. Convert all members to host-order.
255 	*/
256 	evt->event_type = hton32(evt->event_type);
257 	evt->flags = hton16(evt->flags);
258 	evt->status = hton32(evt->status);
259 	evt->reason = hton32(evt->reason);
260 	evt->auth_type = hton32(evt->auth_type);
261 	evt->datalen = hton32(evt->datalen);
262 	evt->version = hton16(evt->version);
263 }
264 
265 /*
266  * Validate if the event is proper and if valid copy event header to event.
267  * If proper event pointer is passed, to just validate, pass NULL to event.
268  *
269  * Return values are
270  *	BCME_OK - It is a BRCM event or BRCM dongle event
271  *	BCME_NOTFOUND - Not BRCM, not an event, may be okay
272  *	BCME_BADLEN - Bad length, should not process, just drop
273  */
274 int
is_wlc_event_frame(void * pktdata,uint pktlen,uint16 exp_usr_subtype,bcm_event_msg_u_t * out_event)275 is_wlc_event_frame(void *pktdata, uint pktlen, uint16 exp_usr_subtype,
276 	bcm_event_msg_u_t *out_event)
277 {
278 	uint16 evlen = 0;	/* length in bcmeth_hdr */
279 	uint16 subtype;
280 	uint16 usr_subtype;
281 	bcm_event_t *bcm_event;
282 	uint8 *pktend;
283 	uint8 *evend;
284 	int err = BCME_OK;
285 	uint32 data_len = 0; /* data length in bcm_event */
286 
287 	pktend = (uint8 *)pktdata + pktlen;
288 	bcm_event = (bcm_event_t *)pktdata;
289 
290 	/* only care about 16-bit subtype / length versions */
291 	if ((uint8 *)&bcm_event->bcm_hdr < pktend) {
292 		uint8 short_subtype = *(uint8 *)&bcm_event->bcm_hdr;
293 		if (!(short_subtype & 0x80)) {
294 			err = BCME_NOTFOUND;
295 			goto done;
296 		}
297 	}
298 
299 	/* must have both ether_header and bcmeth_hdr */
300 	if (pktlen < OFFSETOF(bcm_event_t, event)) {
301 		err = BCME_BADLEN;
302 		goto done;
303 	}
304 
305 	/* check length in bcmeth_hdr */
306 
307 	/* temporary - header length not always set properly. When the below
308 	 * !BCMDONGLEHOST is in all branches that use trunk DHD, the code
309 	 * under BCMDONGLEHOST can be removed.
310 	 */
311 	evlen = (uint16)(pktend - (uint8 *)&bcm_event->bcm_hdr.version);
312 	evend = (uint8 *)&bcm_event->bcm_hdr.version + evlen;
313 	if (evend != pktend) {
314 		err = BCME_BADLEN;
315 		goto done;
316 	}
317 
318 	/* match on subtype, oui and usr subtype for BRCM events */
319 	subtype = ntoh16_ua((void *)&bcm_event->bcm_hdr.subtype);
320 	if (subtype != BCMILCP_SUBTYPE_VENDOR_LONG) {
321 		err = BCME_NOTFOUND;
322 		goto done;
323 	}
324 
325 	if (bcmp(BRCM_OUI, &bcm_event->bcm_hdr.oui[0], DOT11_OUI_LEN)) {
326 		err = BCME_NOTFOUND;
327 		goto done;
328 	}
329 
330 	/* if it is a bcm_event or bcm_dngl_event_t, validate it */
331 	usr_subtype = ntoh16_ua((void *)&bcm_event->bcm_hdr.usr_subtype);
332 	switch (usr_subtype) {
333 	case BCMILCP_BCM_SUBTYPE_EVENT:
334 		/* check that header length and pkt length are sufficient */
335 		if ((pktlen < sizeof(bcm_event_t)) ||
336 			(evend < ((uint8 *)bcm_event + sizeof(bcm_event_t)))) {
337 			err = BCME_BADLEN;
338 			goto done;
339 		}
340 
341 		/* ensure data length in event is not beyond the packet. */
342 		data_len = ntoh32_ua((void *)&bcm_event->event.datalen);
343 		if ((sizeof(bcm_event_t) + data_len +
344 			BCMILCP_BCM_SUBTYPE_EVENT_DATA_PAD) != pktlen) {
345 			err = BCME_BADLEN;
346 			goto done;
347 		}
348 
349 		if (exp_usr_subtype && (exp_usr_subtype != usr_subtype)) {
350 			err = BCME_NOTFOUND;
351 			goto done;
352 		}
353 
354 		if (out_event) {
355 			/* ensure BRCM event pkt aligned */
356 			memcpy(&out_event->event, &bcm_event->event, sizeof(wl_event_msg_t));
357 		}
358 
359 		break;
360 
361 	case BCMILCP_BCM_SUBTYPE_DNGLEVENT:
362 #if defined(DNGL_EVENT_SUPPORT)
363 		if ((pktlen < sizeof(bcm_dngl_event_t)) ||
364 			(evend < ((uint8 *)bcm_event + sizeof(bcm_dngl_event_t)))) {
365 			err = BCME_BADLEN;
366 			goto done;
367 		}
368 
369 		/* ensure data length in event is not beyond the packet. */
370 		data_len = ntoh16_ua((void *)&((bcm_dngl_event_t *)pktdata)->dngl_event.datalen);
371 		if ((sizeof(bcm_dngl_event_t) + data_len +
372 			BCMILCP_BCM_SUBTYPE_EVENT_DATA_PAD) != pktlen) {
373 			err = BCME_BADLEN;
374 			goto done;
375 		}
376 
377 		if (exp_usr_subtype && (exp_usr_subtype != usr_subtype)) {
378 			err = BCME_NOTFOUND;
379 			goto done;
380 		}
381 
382 		if (out_event) {
383 			/* ensure BRCM dngl event pkt aligned */
384 			memcpy(&out_event->dngl_event, &((bcm_dngl_event_t *)pktdata)->dngl_event,
385 				sizeof(bcm_dngl_event_msg_t));
386 		}
387 
388 		break;
389 #else
390 		err = BCME_UNSUPPORTED;
391 		break;
392 #endif // endif
393 
394 	default:
395 		err = BCME_NOTFOUND;
396 		goto done;
397 	}
398 
399 	BCM_REFERENCE(data_len);
400 done:
401 	return err;
402 }
403