xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/dhd_debug_linux.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * DHD debugability Linux os layer
3  *
4  * <<Broadcom-WL-IPTag/Open:>>
5  *
6  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
7  *
8  * Copyright (C) 1999-2017, Broadcom Corporation
9  *
10  *      Unless you and Broadcom execute a separate written software license
11  * agreement governing use of this software, this software is licensed to you
12  * under the terms of the GNU General Public License version 2 (the "GPL"),
13  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
14  * following added to such license:
15  *
16  *      As a special exception, the copyright holders of this software give you
17  * permission to link this software with independent modules, and to copy and
18  * distribute the resulting executable under terms of your choice, provided that
19  * you also meet, for each linked independent module, the terms and conditions of
20  * the license of that module.  An independent module is a module which is not
21  * derived from this software.  The special exception does not apply to any
22  * modifications of the software.
23  *
24  *      Notwithstanding the above, under no circumstances may you combine this
25  * software in any way with any other Broadcom software provided under a license
26  * other than the GPL, without Broadcom's express prior written consent.
27  *
28  * $Id: dhd_debug_linux.c 696754 2017-04-28 00:21:58Z $
29  */
30 
31 #include <typedefs.h>
32 #include <osl.h>
33 #include <bcmutils.h>
34 #include <bcmendian.h>
35 #include <dngl_stats.h>
36 #include <dhd.h>
37 #include <dhd_dbg.h>
38 #include <dhd_debug.h>
39 
40 #include <net/cfg80211.h>
41 #include <wl_cfgvendor.h>
42 
43 typedef void (*dbg_ring_send_sub_t)(void *ctx, const int ring_id, const void *data,
44 	const uint32 len, const dhd_dbg_ring_status_t ring_status);
45 typedef void (*dbg_urgent_noti_sub_t)(void *ctx, const void *data,
46 	const uint32 len, const uint32 fw_len);
47 
48 static dbg_ring_send_sub_t ring_send_sub_cb[DEBUG_RING_ID_MAX];
49 static dbg_urgent_noti_sub_t urgent_noti_sub_cb;
50 typedef struct dhd_dbg_os_ring_info {
51 	dhd_pub_t *dhdp;
52 	int ring_id;
53 	int log_level;
54 	unsigned long interval;
55 	struct delayed_work work;
56 	uint64 tsoffset;
57 } linux_dbgring_info_t;
58 
59 struct log_level_table dhd_event_map[] = {
60 	{1, WIFI_EVENT_DRIVER_EAPOL_FRAME_TRANSMIT_REQUESTED, "DRIVER EAPOL TX REQ"},
61 	{1, WIFI_EVENT_DRIVER_EAPOL_FRAME_RECEIVED, "DRIVER EAPOL RX"},
62 	{2, WIFI_EVENT_DRIVER_SCAN_REQUESTED, "SCAN_REQUESTED"},
63 	{2, WIFI_EVENT_DRIVER_SCAN_COMPLETE, "SCAN COMPELETE"},
64 	{3, WIFI_EVENT_DRIVER_SCAN_RESULT_FOUND, "SCAN RESULT FOUND"},
65 	{2, WIFI_EVENT_DRIVER_PNO_ADD, "PNO ADD"},
66 	{2, WIFI_EVENT_DRIVER_PNO_REMOVE, "PNO REMOVE"},
67 	{2, WIFI_EVENT_DRIVER_PNO_NETWORK_FOUND, "PNO NETWORK FOUND"},
68 	{2, WIFI_EVENT_DRIVER_PNO_SCAN_REQUESTED, "PNO SCAN_REQUESTED"},
69 	{1, WIFI_EVENT_DRIVER_PNO_SCAN_RESULT_FOUND, "PNO SCAN RESULT FOUND"},
70 	{1, WIFI_EVENT_DRIVER_PNO_SCAN_COMPLETE, "PNO SCAN COMPELETE"}
71 };
72 
73 static void
debug_data_send(dhd_pub_t * dhdp,int ring_id,const void * data,const uint32 len,const dhd_dbg_ring_status_t ring_status)74 debug_data_send(dhd_pub_t *dhdp, int ring_id, const void *data, const uint32 len,
75 	const dhd_dbg_ring_status_t ring_status)
76 {
77 	struct net_device *ndev;
78 	dbg_ring_send_sub_t ring_sub_send;
79 	ndev = dhd_linux_get_primary_netdev(dhdp);
80 	if (!ndev)
81 		return;
82 	if (!VALID_RING(ring_id))
83 		return;
84 	if (ring_send_sub_cb[ring_id]) {
85 		ring_sub_send = ring_send_sub_cb[ring_id];
86 		ring_sub_send(ndev, ring_id, data, len, ring_status);
87 	}
88 }
89 
90 static void
dhd_os_dbg_urgent_notifier(dhd_pub_t * dhdp,const void * data,const uint32 len)91 dhd_os_dbg_urgent_notifier(dhd_pub_t *dhdp, const void *data, const uint32 len)
92 {
93 	struct net_device *ndev;
94 	ndev = dhd_linux_get_primary_netdev(dhdp);
95 	if (!ndev)
96 		return;
97 	if (urgent_noti_sub_cb) {
98 		urgent_noti_sub_cb(ndev, data, len, dhdp->soc_ram_length);
99 	}
100 }
101 
102 static void
dbg_ring_poll_worker(struct work_struct * work)103 dbg_ring_poll_worker(struct work_struct *work)
104 {
105 	struct delayed_work *d_work = to_delayed_work(work);
106 	bool sched = TRUE;
107 	dhd_dbg_ring_t *ring;
108 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
109 #pragma GCC diagnostic push
110 #pragma GCC diagnostic ignored "-Wcast-qual"
111 #endif // endif
112 	linux_dbgring_info_t *ring_info =
113 		container_of(d_work, linux_dbgring_info_t, work);
114 #if defined(STRICT_GCC_WARNINGS) && defined(__GNUC__)
115 #pragma GCC diagnostic pop
116 #endif // endif
117 	dhd_pub_t *dhdp = ring_info->dhdp;
118 	int ringid = ring_info->ring_id;
119 	dhd_dbg_ring_status_t ring_status;
120 	void *buf;
121 	dhd_dbg_ring_entry_t *hdr;
122 	uint32 buflen, rlen;
123 	unsigned long flags;
124 
125 	ring = &dhdp->dbg->dbg_rings[ringid];
126 	DHD_DBG_RING_LOCK(ring->lock, flags);
127 	dhd_dbg_get_ring_status(dhdp, ringid, &ring_status);
128 
129 	if (ring->wp > ring->rp) {
130 		buflen = ring->wp - ring->rp;
131 	} else if (ring->wp < ring->rp) {
132 		buflen = ring->ring_size - ring->rp + ring->wp;
133 	} else {
134 		goto exit;
135 	}
136 
137 	if (buflen > ring->ring_size) {
138 		goto exit;
139 	}
140 
141 	buf = MALLOCZ(dhdp->osh, buflen);
142 	if (!buf) {
143 		DHD_ERROR(("%s failed to allocate read buf\n", __FUNCTION__));
144 		sched = FALSE;
145 		goto exit;
146 	}
147 
148 	rlen = dhd_dbg_pull_from_ring(dhdp, ringid, buf, buflen);
149 
150 	if (!ring->sched_pull) {
151 		ring->sched_pull = TRUE;
152 	}
153 
154 	hdr = (dhd_dbg_ring_entry_t *)buf;
155 	while (rlen > 0) {
156 		ring_status.read_bytes += ENTRY_LENGTH(hdr);
157 		/* offset fw ts to host ts */
158 		hdr->timestamp += ring_info->tsoffset;
159 		debug_data_send(dhdp, ringid, hdr, ENTRY_LENGTH(hdr),
160 			ring_status);
161 		rlen -= ENTRY_LENGTH(hdr);
162 		hdr = (dhd_dbg_ring_entry_t *)((char *)hdr + ENTRY_LENGTH(hdr));
163 	}
164 	MFREE(dhdp->osh, buf, buflen);
165 
166 exit:
167 	if (sched) {
168 		/* retrigger the work at same interval */
169 		if ((ring_status.written_bytes == ring_status.read_bytes) &&
170 				(ring_info->interval)) {
171 			schedule_delayed_work(d_work, ring_info->interval);
172 		}
173 	}
174 
175 	DHD_DBG_RING_UNLOCK(ring->lock, flags);
176 
177 	return;
178 }
179 
180 int
dhd_os_dbg_register_callback(int ring_id,dbg_ring_send_sub_t callback)181 dhd_os_dbg_register_callback(int ring_id, dbg_ring_send_sub_t callback)
182 {
183 	if (!VALID_RING(ring_id))
184 		return BCME_RANGE;
185 
186 	ring_send_sub_cb[ring_id] = callback;
187 	return BCME_OK;
188 }
189 
190 int
dhd_os_dbg_register_urgent_notifier(dhd_pub_t * dhdp,dbg_urgent_noti_sub_t urgent_noti_sub)191 dhd_os_dbg_register_urgent_notifier(dhd_pub_t *dhdp, dbg_urgent_noti_sub_t urgent_noti_sub)
192 {
193 	if (!dhdp || !urgent_noti_sub)
194 		return BCME_BADARG;
195 	urgent_noti_sub_cb = urgent_noti_sub;
196 
197 	return BCME_OK;
198 }
199 
200 int
dhd_os_start_logging(dhd_pub_t * dhdp,char * ring_name,int log_level,int flags,int time_intval,int threshold)201 dhd_os_start_logging(dhd_pub_t *dhdp, char *ring_name, int log_level,
202 	int flags, int time_intval, int threshold)
203 {
204 	int ret = BCME_OK;
205 	int ring_id;
206 	linux_dbgring_info_t *os_priv, *ring_info;
207 
208 	ring_id = dhd_dbg_find_ring_id(dhdp, ring_name);
209 	if (!VALID_RING(ring_id))
210 		return BCME_UNSUPPORTED;
211 
212 	DHD_DBGIF(("%s , log_level : %d, time_intval : %d, threshod %d Bytes\n",
213 		__FUNCTION__, log_level, time_intval, threshold));
214 
215 	/* change the configuration */
216 	ret = dhd_dbg_set_configuration(dhdp, ring_id, log_level, flags, threshold);
217 	if (ret) {
218 		DHD_ERROR(("dhd_set_configuration is failed : %d\n", ret));
219 		return ret;
220 	}
221 
222 	os_priv = dhd_dbg_get_priv(dhdp);
223 	if (!os_priv)
224 		return BCME_ERROR;
225 	ring_info = &os_priv[ring_id];
226 	ring_info->log_level = log_level;
227 
228 	if (time_intval == 0 || log_level == 0) {
229 		ring_info->interval = 0;
230 		cancel_delayed_work_sync(&ring_info->work);
231 	} else {
232 		ring_info->interval = msecs_to_jiffies(time_intval * MSEC_PER_SEC);
233 		cancel_delayed_work_sync(&ring_info->work);
234 		schedule_delayed_work(&ring_info->work, ring_info->interval);
235 	}
236 
237 	return ret;
238 }
239 
240 int
dhd_os_reset_logging(dhd_pub_t * dhdp)241 dhd_os_reset_logging(dhd_pub_t *dhdp)
242 {
243 	int ret = BCME_OK;
244 	int ring_id;
245 	linux_dbgring_info_t *os_priv, *ring_info;
246 
247 	os_priv = dhd_dbg_get_priv(dhdp);
248 	if (!os_priv)
249 		return BCME_ERROR;
250 
251 	/* Stop all rings */
252 	for (ring_id = DEBUG_RING_ID_INVALID + 1; ring_id < DEBUG_RING_ID_MAX; ring_id++) {
253 		DHD_DBGIF(("%s: Stop ring buffer %d\n", __FUNCTION__, ring_id));
254 
255 		ring_info = &os_priv[ring_id];
256 		/* cancel any pending work */
257 		cancel_delayed_work_sync(&ring_info->work);
258 		/* log level zero makes stop logging on that ring */
259 		ring_info->log_level = 0;
260 		ring_info->interval = 0;
261 		/* change the configuration */
262 		ret = dhd_dbg_set_configuration(dhdp, ring_id, 0, 0, 0);
263 		if (ret) {
264 			DHD_ERROR(("dhd_set_configuration is failed : %d\n", ret));
265 			return ret;
266 		}
267 	}
268 	return ret;
269 }
270 
271 #define SUPPRESS_LOG_LEVEL 1
272 int
dhd_os_suppress_logging(dhd_pub_t * dhdp,bool suppress)273 dhd_os_suppress_logging(dhd_pub_t *dhdp, bool suppress)
274 {
275 	int ret = BCME_OK;
276 	int max_log_level;
277 	int enable = (suppress) ? 0 : 1;
278 	linux_dbgring_info_t *os_priv;
279 
280 	os_priv = dhd_dbg_get_priv(dhdp);
281 	if (!os_priv)
282 		return BCME_ERROR;
283 
284 	max_log_level = os_priv[FW_VERBOSE_RING_ID].log_level;
285 
286 	if (max_log_level == SUPPRESS_LOG_LEVEL) {
287 		/* suppress the logging in FW not to wake up host while device in suspend mode */
288 		ret = dhd_iovar(dhdp, 0, "logtrace", (char *)&enable, sizeof(enable), NULL, 0,
289 				TRUE);
290 		if (ret < 0 && (ret != BCME_UNSUPPORTED)) {
291 			DHD_ERROR(("logtrace is failed : %d\n", ret));
292 		}
293 	}
294 
295 	return ret;
296 }
297 
298 int
dhd_os_get_ring_status(dhd_pub_t * dhdp,int ring_id,dhd_dbg_ring_status_t * dbg_ring_status)299 dhd_os_get_ring_status(dhd_pub_t *dhdp, int ring_id, dhd_dbg_ring_status_t *dbg_ring_status)
300 {
301 	return dhd_dbg_get_ring_status(dhdp, ring_id, dbg_ring_status);
302 }
303 
304 int
dhd_os_trigger_get_ring_data(dhd_pub_t * dhdp,char * ring_name)305 dhd_os_trigger_get_ring_data(dhd_pub_t *dhdp, char *ring_name)
306 {
307 	int ret = BCME_OK;
308 	int ring_id;
309 	linux_dbgring_info_t *os_priv, *ring_info;
310 	ring_id = dhd_dbg_find_ring_id(dhdp, ring_name);
311 	if (!VALID_RING(ring_id))
312 		return BCME_UNSUPPORTED;
313 	os_priv = dhd_dbg_get_priv(dhdp);
314 	if (os_priv) {
315 		ring_info = &os_priv[ring_id];
316 		if (ring_info->interval) {
317 			cancel_delayed_work_sync(&ring_info->work);
318 		}
319 		schedule_delayed_work(&ring_info->work, 0);
320 	} else {
321 		DHD_ERROR(("%s : os_priv is NULL\n", __FUNCTION__));
322 		ret = BCME_ERROR;
323 	}
324 	return ret;
325 }
326 
327 int
dhd_os_push_push_ring_data(dhd_pub_t * dhdp,int ring_id,void * data,int32 data_len)328 dhd_os_push_push_ring_data(dhd_pub_t *dhdp, int ring_id, void *data, int32 data_len)
329 {
330 	int ret = BCME_OK, i;
331 	dhd_dbg_ring_entry_t msg_hdr;
332 	log_conn_event_t *event_data = (log_conn_event_t *)data;
333 	linux_dbgring_info_t *os_priv, *ring_info = NULL;
334 
335 	if (!VALID_RING(ring_id))
336 		return BCME_UNSUPPORTED;
337 	os_priv = dhd_dbg_get_priv(dhdp);
338 
339 	if (os_priv) {
340 		ring_info = &os_priv[ring_id];
341 	} else
342 		return BCME_NORESOURCE;
343 
344 	memset(&msg_hdr, 0, sizeof(dhd_dbg_ring_entry_t));
345 
346 	if (ring_id == DHD_EVENT_RING_ID) {
347 		msg_hdr.type = DBG_RING_ENTRY_EVENT_TYPE;
348 		msg_hdr.flags |= DBG_RING_ENTRY_FLAGS_HAS_TIMESTAMP;
349 		msg_hdr.flags |= DBG_RING_ENTRY_FLAGS_HAS_BINARY;
350 		msg_hdr.timestamp = local_clock();
351 		/* convert to ms */
352 		msg_hdr.timestamp = DIV_U64_BY_U32(msg_hdr.timestamp, NSEC_PER_MSEC);
353 		msg_hdr.len = data_len;
354 		/* filter the event for higher log level with current log level */
355 		for (i = 0; i < ARRAYSIZE(dhd_event_map); i++) {
356 			if ((dhd_event_map[i].tag == event_data->event) &&
357 				dhd_event_map[i].log_level > ring_info->log_level) {
358 				return ret;
359 			}
360 		}
361 	}
362 	ret = dhd_dbg_push_to_ring(dhdp, ring_id, &msg_hdr, event_data);
363 	if (ret) {
364 		DHD_ERROR(("%s : failed to push data into the ring (%d) with ret(%d)\n",
365 			__FUNCTION__, ring_id, ret));
366 	}
367 
368 	return ret;
369 }
370 
371 #ifdef DBG_PKT_MON
372 int
dhd_os_dbg_attach_pkt_monitor(dhd_pub_t * dhdp)373 dhd_os_dbg_attach_pkt_monitor(dhd_pub_t *dhdp)
374 {
375 	return dhd_dbg_attach_pkt_monitor(dhdp, dhd_os_dbg_monitor_tx_pkts,
376 		dhd_os_dbg_monitor_tx_status, dhd_os_dbg_monitor_rx_pkts);
377 }
378 
379 int
dhd_os_dbg_start_pkt_monitor(dhd_pub_t * dhdp)380 dhd_os_dbg_start_pkt_monitor(dhd_pub_t *dhdp)
381 {
382 	return dhd_dbg_start_pkt_monitor(dhdp);
383 }
384 
385 int
dhd_os_dbg_monitor_tx_pkts(dhd_pub_t * dhdp,void * pkt,uint32 pktid)386 dhd_os_dbg_monitor_tx_pkts(dhd_pub_t *dhdp, void *pkt, uint32 pktid)
387 {
388 	return dhd_dbg_monitor_tx_pkts(dhdp, pkt, pktid);
389 }
390 
391 int
dhd_os_dbg_monitor_tx_status(dhd_pub_t * dhdp,void * pkt,uint32 pktid,uint16 status)392 dhd_os_dbg_monitor_tx_status(dhd_pub_t *dhdp, void *pkt, uint32 pktid,
393 	uint16 status)
394 {
395 	return dhd_dbg_monitor_tx_status(dhdp, pkt, pktid, status);
396 }
397 
398 int
dhd_os_dbg_monitor_rx_pkts(dhd_pub_t * dhdp,void * pkt)399 dhd_os_dbg_monitor_rx_pkts(dhd_pub_t *dhdp, void *pkt)
400 {
401 	return dhd_dbg_monitor_rx_pkts(dhdp, pkt);
402 }
403 
404 int
dhd_os_dbg_stop_pkt_monitor(dhd_pub_t * dhdp)405 dhd_os_dbg_stop_pkt_monitor(dhd_pub_t *dhdp)
406 {
407 	return dhd_dbg_stop_pkt_monitor(dhdp);
408 }
409 
410 int
dhd_os_dbg_monitor_get_tx_pkts(dhd_pub_t * dhdp,void __user * user_buf,uint16 req_count,uint16 * resp_count)411 dhd_os_dbg_monitor_get_tx_pkts(dhd_pub_t *dhdp, void __user *user_buf,
412 	uint16 req_count, uint16 *resp_count)
413 {
414 	return dhd_dbg_monitor_get_tx_pkts(dhdp, user_buf, req_count, resp_count);
415 }
416 
417 int
dhd_os_dbg_monitor_get_rx_pkts(dhd_pub_t * dhdp,void __user * user_buf,uint16 req_count,uint16 * resp_count)418 dhd_os_dbg_monitor_get_rx_pkts(dhd_pub_t *dhdp, void __user *user_buf,
419 	uint16 req_count, uint16 *resp_count)
420 {
421 	return dhd_dbg_monitor_get_rx_pkts(dhdp, user_buf, req_count, resp_count);
422 }
423 
424 int
dhd_os_dbg_detach_pkt_monitor(dhd_pub_t * dhdp)425 dhd_os_dbg_detach_pkt_monitor(dhd_pub_t *dhdp)
426 {
427 	return dhd_dbg_detach_pkt_monitor(dhdp);
428 }
429 #endif /* DBG_PKT_MON */
430 
431 int
dhd_os_dbg_get_feature(dhd_pub_t * dhdp,int32 * features)432 dhd_os_dbg_get_feature(dhd_pub_t *dhdp, int32 *features)
433 {
434 	int ret = BCME_OK;
435 	*features = 0;
436 #ifdef DEBUGABILITY
437 	*features |= DBG_MEMORY_DUMP_SUPPORTED;
438 	if (FW_SUPPORTED(dhdp, logtrace)) {
439 		*features |= DBG_CONNECT_EVENT_SUPPORTED;
440 		*features |= DBG_VERBOSE_LOG_SUPPORTED;
441 	}
442 	if (FW_SUPPORTED(dhdp, hchk)) {
443 		*features |= DBG_HEALTH_CHECK_SUPPORTED;
444 	}
445 #ifdef DBG_PKT_MON
446 	if (FW_SUPPORTED(dhdp, d11status)) {
447 		*features |= DBG_PACKET_FATE_SUPPORTED;
448 	}
449 #endif /* DBG_PKT_MON */
450 #endif /* DEBUGABILITY */
451 	return ret;
452 }
453 
454 static void
dhd_os_dbg_pullreq(void * os_priv,int ring_id)455 dhd_os_dbg_pullreq(void *os_priv, int ring_id)
456 {
457 	linux_dbgring_info_t *ring_info;
458 
459 	ring_info = &((linux_dbgring_info_t *)os_priv)[ring_id];
460 	cancel_delayed_work(&ring_info->work);
461 	schedule_delayed_work(&ring_info->work, 0);
462 }
463 
464 int
dhd_os_dbg_attach(dhd_pub_t * dhdp)465 dhd_os_dbg_attach(dhd_pub_t *dhdp)
466 {
467 	int ret = BCME_OK;
468 	linux_dbgring_info_t *os_priv, *ring_info;
469 	int ring_id;
470 
471 	/* os_dbg data */
472 	os_priv = MALLOCZ(dhdp->osh, sizeof(*os_priv) * DEBUG_RING_ID_MAX);
473 	if (!os_priv)
474 		return BCME_NOMEM;
475 
476 	for (ring_id = DEBUG_RING_ID_INVALID + 1; ring_id < DEBUG_RING_ID_MAX;
477 	     ring_id++) {
478 		ring_info = &os_priv[ring_id];
479 		INIT_DELAYED_WORK(&ring_info->work, dbg_ring_poll_worker);
480 		ring_info->dhdp = dhdp;
481 		ring_info->ring_id = ring_id;
482 	}
483 
484 	ret = dhd_dbg_attach(dhdp, dhd_os_dbg_pullreq, dhd_os_dbg_urgent_notifier, os_priv);
485 	if (ret)
486 		MFREE(dhdp->osh, os_priv, sizeof(*os_priv) * DEBUG_RING_ID_MAX);
487 
488 	return ret;
489 }
490 
491 void
dhd_os_dbg_detach(dhd_pub_t * dhdp)492 dhd_os_dbg_detach(dhd_pub_t *dhdp)
493 {
494 	linux_dbgring_info_t *os_priv, *ring_info;
495 	int ring_id;
496 	/* free os_dbg data */
497 	os_priv = dhd_dbg_get_priv(dhdp);
498 	if (!os_priv)
499 		return;
500 	/* abort pending any job */
501 	for (ring_id = DEBUG_RING_ID_INVALID + 1; ring_id < DEBUG_RING_ID_MAX; ring_id++) {
502 		ring_info = &os_priv[ring_id];
503 		if (ring_info->interval) {
504 			ring_info->interval = 0;
505 			cancel_delayed_work_sync(&ring_info->work);
506 		}
507 	}
508 	MFREE(dhdp->osh, os_priv, sizeof(*os_priv) * DEBUG_RING_ID_MAX);
509 
510 	return dhd_dbg_detach(dhdp);
511 }
512