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