1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * DHD debugability Linux os layer
4 *
5 * <<Broadcom-WL-IPTag/Open:>>
6 *
7 * Copyright (C) 1999-2019, Broadcom.
8 *
9 * Unless you and Broadcom execute a separate written software license
10 * agreement governing use of this software, this software is licensed to you
11 * under the terms of the GNU General Public License version 2 (the "GPL"),
12 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
13 * following added to such license:
14 *
15 * As a special exception, the copyright holders of this software give you
16 * permission to link this software with independent modules, and to copy and
17 * distribute the resulting executable under terms of your choice, provided that
18 * you also meet, for each linked independent module, the terms and conditions of
19 * the license of that module. An independent module is a module which is not
20 * derived from this software. The special exception does not apply to any
21 * modifications of the software.
22 *
23 * Notwithstanding the above, under no circumstances may you combine this
24 * software in any way with any other Broadcom software provided under a license
25 * other than the GPL, without Broadcom's express prior written consent.
26 *
27 * $Id: dhd_debug_linux.c 769272 2018-06-25 09:23:27Z $
28 */
29
30 #include <typedefs.h>
31 #include <osl.h>
32 #include <bcmutils.h>
33 #include <bcmendian.h>
34 #include <dngl_stats.h>
35 #include <dhd.h>
36 #include <dhd_dbg.h>
37 #include <dhd_debug.h>
38
39 #include <net/cfg80211.h>
40 #include <wl_cfgvendor.h>
41 #include <dhd_config.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 DHD_DBG_RING_UNLOCK(ring->lock, flags);
149 rlen = dhd_dbg_pull_from_ring(dhdp, ringid, buf, buflen);
150 DHD_DBG_RING_LOCK(ring->lock, flags);
151
152 if (!ring->sched_pull) {
153 ring->sched_pull = TRUE;
154 }
155
156 hdr = (dhd_dbg_ring_entry_t *)buf;
157 while (rlen > 0) {
158 ring_status.read_bytes += ENTRY_LENGTH(hdr);
159 /* offset fw ts to host ts */
160 hdr->timestamp += ring_info->tsoffset;
161 debug_data_send(dhdp, ringid, hdr, ENTRY_LENGTH(hdr),
162 ring_status);
163 rlen -= ENTRY_LENGTH(hdr);
164 hdr = (dhd_dbg_ring_entry_t *)((char *)hdr + ENTRY_LENGTH(hdr));
165 }
166 MFREE(dhdp->osh, buf, buflen);
167
168 exit:
169 if (sched) {
170 /* retrigger the work at same interval */
171 if ((ring_status.written_bytes == ring_status.read_bytes) &&
172 (ring_info->interval)) {
173 schedule_delayed_work(d_work, ring_info->interval);
174 }
175 }
176
177 DHD_DBG_RING_UNLOCK(ring->lock, flags);
178
179 return;
180 }
181
182 int
dhd_os_dbg_register_callback(int ring_id,dbg_ring_send_sub_t callback)183 dhd_os_dbg_register_callback(int ring_id, dbg_ring_send_sub_t callback)
184 {
185 if (!VALID_RING(ring_id))
186 return BCME_RANGE;
187
188 ring_send_sub_cb[ring_id] = callback;
189 return BCME_OK;
190 }
191
192 int
dhd_os_dbg_register_urgent_notifier(dhd_pub_t * dhdp,dbg_urgent_noti_sub_t urgent_noti_sub)193 dhd_os_dbg_register_urgent_notifier(dhd_pub_t *dhdp, dbg_urgent_noti_sub_t urgent_noti_sub)
194 {
195 if (!dhdp || !urgent_noti_sub)
196 return BCME_BADARG;
197 urgent_noti_sub_cb = urgent_noti_sub;
198
199 return BCME_OK;
200 }
201
202 int
dhd_os_start_logging(dhd_pub_t * dhdp,char * ring_name,int log_level,int flags,int time_intval,int threshold)203 dhd_os_start_logging(dhd_pub_t *dhdp, char *ring_name, int log_level,
204 int flags, int time_intval, int threshold)
205 {
206 int ret = BCME_OK;
207 int ring_id;
208 linux_dbgring_info_t *os_priv, *ring_info;
209
210 ring_id = dhd_dbg_find_ring_id(dhdp, ring_name);
211 if (!VALID_RING(ring_id))
212 return BCME_UNSUPPORTED;
213
214 DHD_DBGIF(("%s , log_level : %d, time_intval : %d, threshod %d Bytes\n",
215 __FUNCTION__, log_level, time_intval, threshold));
216
217 /* change the configuration */
218 ret = dhd_dbg_set_configuration(dhdp, ring_id, log_level, flags, threshold);
219 if (ret) {
220 DHD_ERROR(("dhd_set_configuration is failed : %d\n", ret));
221 return ret;
222 }
223
224 os_priv = dhd_dbg_get_priv(dhdp);
225 if (!os_priv)
226 return BCME_ERROR;
227 ring_info = &os_priv[ring_id];
228 ring_info->log_level = log_level;
229
230 if (time_intval == 0 || log_level == 0) {
231 ring_info->interval = 0;
232 cancel_delayed_work_sync(&ring_info->work);
233 } else {
234 ring_info->interval = msecs_to_jiffies(time_intval * MSEC_PER_SEC);
235 cancel_delayed_work_sync(&ring_info->work);
236 schedule_delayed_work(&ring_info->work, ring_info->interval);
237 }
238
239 return ret;
240 }
241
242 int
dhd_os_reset_logging(dhd_pub_t * dhdp)243 dhd_os_reset_logging(dhd_pub_t *dhdp)
244 {
245 int ret = BCME_OK;
246 int ring_id;
247 linux_dbgring_info_t *os_priv, *ring_info;
248
249 os_priv = dhd_dbg_get_priv(dhdp);
250 if (!os_priv)
251 return BCME_ERROR;
252
253 /* Stop all rings */
254 for (ring_id = DEBUG_RING_ID_INVALID + 1; ring_id < DEBUG_RING_ID_MAX; ring_id++) {
255 DHD_DBGIF(("%s: Stop ring buffer %d\n", __FUNCTION__, ring_id));
256
257 ring_info = &os_priv[ring_id];
258 /* cancel any pending work */
259 cancel_delayed_work_sync(&ring_info->work);
260 /* log level zero makes stop logging on that ring */
261 ring_info->log_level = 0;
262 ring_info->interval = 0;
263 /* change the configuration */
264 ret = dhd_dbg_set_configuration(dhdp, ring_id, 0, 0, 0);
265 if (ret) {
266 DHD_ERROR(("dhd_set_configuration is failed : %d\n", ret));
267 return ret;
268 }
269 }
270 return ret;
271 }
272
273 #define SUPPRESS_LOG_LEVEL 1
274 int
dhd_os_suppress_logging(dhd_pub_t * dhdp,bool suppress)275 dhd_os_suppress_logging(dhd_pub_t *dhdp, bool suppress)
276 {
277 int ret = BCME_OK;
278 int max_log_level;
279 int enable = (suppress) ? 0 : 1;
280 linux_dbgring_info_t *os_priv;
281
282 os_priv = dhd_dbg_get_priv(dhdp);
283 if (!os_priv)
284 return BCME_ERROR;
285
286 max_log_level = os_priv[FW_VERBOSE_RING_ID].log_level;
287
288 if (max_log_level == SUPPRESS_LOG_LEVEL) {
289 /* suppress the logging in FW not to wake up host while device in suspend mode */
290 ret = dhd_iovar(dhdp, 0, "logtrace", (char *)&enable, sizeof(enable), NULL, 0,
291 TRUE);
292 if (ret < 0 && (ret != BCME_UNSUPPORTED)) {
293 DHD_ERROR(("logtrace is failed : %d\n", ret));
294 }
295 }
296
297 return ret;
298 }
299
300 int
dhd_os_get_ring_status(dhd_pub_t * dhdp,int ring_id,dhd_dbg_ring_status_t * dbg_ring_status)301 dhd_os_get_ring_status(dhd_pub_t *dhdp, int ring_id, dhd_dbg_ring_status_t *dbg_ring_status)
302 {
303 return dhd_dbg_get_ring_status(dhdp, ring_id, dbg_ring_status);
304 }
305
306 int
dhd_os_trigger_get_ring_data(dhd_pub_t * dhdp,char * ring_name)307 dhd_os_trigger_get_ring_data(dhd_pub_t *dhdp, char *ring_name)
308 {
309 int ret = BCME_OK;
310 int ring_id;
311 linux_dbgring_info_t *os_priv, *ring_info;
312 ring_id = dhd_dbg_find_ring_id(dhdp, ring_name);
313 if (!VALID_RING(ring_id))
314 return BCME_UNSUPPORTED;
315 os_priv = dhd_dbg_get_priv(dhdp);
316 if (os_priv) {
317 ring_info = &os_priv[ring_id];
318 if (ring_info->interval) {
319 cancel_delayed_work_sync(&ring_info->work);
320 }
321 schedule_delayed_work(&ring_info->work, 0);
322 } else {
323 DHD_ERROR(("%s : os_priv is NULL\n", __FUNCTION__));
324 ret = BCME_ERROR;
325 }
326 return ret;
327 }
328
329 int
dhd_os_push_push_ring_data(dhd_pub_t * dhdp,int ring_id,void * data,int32 data_len)330 dhd_os_push_push_ring_data(dhd_pub_t *dhdp, int ring_id, void *data, int32 data_len)
331 {
332 int ret = BCME_OK, i;
333 dhd_dbg_ring_entry_t msg_hdr;
334 log_conn_event_t *event_data = (log_conn_event_t *)data;
335 linux_dbgring_info_t *os_priv, *ring_info = NULL;
336
337 if (!VALID_RING(ring_id))
338 return BCME_UNSUPPORTED;
339 os_priv = dhd_dbg_get_priv(dhdp);
340
341 if (os_priv) {
342 ring_info = &os_priv[ring_id];
343 } else
344 return BCME_NORESOURCE;
345
346 memset(&msg_hdr, 0, sizeof(dhd_dbg_ring_entry_t));
347
348 if (ring_id == DHD_EVENT_RING_ID) {
349 msg_hdr.type = DBG_RING_ENTRY_EVENT_TYPE;
350 msg_hdr.flags |= DBG_RING_ENTRY_FLAGS_HAS_TIMESTAMP;
351 msg_hdr.flags |= DBG_RING_ENTRY_FLAGS_HAS_BINARY;
352 msg_hdr.timestamp = local_clock();
353 /* convert to ms */
354 msg_hdr.timestamp = DIV_U64_BY_U32(msg_hdr.timestamp, NSEC_PER_MSEC);
355 msg_hdr.len = data_len;
356 /* filter the event for higher log level with current log level */
357 for (i = 0; i < ARRAYSIZE(dhd_event_map); i++) {
358 if ((dhd_event_map[i].tag == event_data->event) &&
359 dhd_event_map[i].log_level > ring_info->log_level) {
360 return ret;
361 }
362 }
363 }
364 ret = dhd_dbg_push_to_ring(dhdp, ring_id, &msg_hdr, event_data);
365 if (ret) {
366 DHD_ERROR(("%s : failed to push data into the ring (%d) with ret(%d)\n",
367 __FUNCTION__, ring_id, ret));
368 }
369
370 return ret;
371 }
372
373 #ifdef DBG_PKT_MON
374 int
dhd_os_dbg_attach_pkt_monitor(dhd_pub_t * dhdp)375 dhd_os_dbg_attach_pkt_monitor(dhd_pub_t *dhdp)
376 {
377 return dhd_dbg_attach_pkt_monitor(dhdp, dhd_os_dbg_monitor_tx_pkts,
378 dhd_os_dbg_monitor_tx_status, dhd_os_dbg_monitor_rx_pkts);
379 }
380
381 int
dhd_os_dbg_start_pkt_monitor(dhd_pub_t * dhdp)382 dhd_os_dbg_start_pkt_monitor(dhd_pub_t *dhdp)
383 {
384 return dhd_dbg_start_pkt_monitor(dhdp);
385 }
386
387 int
dhd_os_dbg_monitor_tx_pkts(dhd_pub_t * dhdp,void * pkt,uint32 pktid)388 dhd_os_dbg_monitor_tx_pkts(dhd_pub_t *dhdp, void *pkt, uint32 pktid)
389 {
390 return dhd_dbg_monitor_tx_pkts(dhdp, pkt, pktid);
391 }
392
393 int
dhd_os_dbg_monitor_tx_status(dhd_pub_t * dhdp,void * pkt,uint32 pktid,uint16 status)394 dhd_os_dbg_monitor_tx_status(dhd_pub_t *dhdp, void *pkt, uint32 pktid,
395 uint16 status)
396 {
397 return dhd_dbg_monitor_tx_status(dhdp, pkt, pktid, status);
398 }
399
400 int
dhd_os_dbg_monitor_rx_pkts(dhd_pub_t * dhdp,void * pkt)401 dhd_os_dbg_monitor_rx_pkts(dhd_pub_t *dhdp, void *pkt)
402 {
403 return dhd_dbg_monitor_rx_pkts(dhdp, pkt);
404 }
405
406 int
dhd_os_dbg_stop_pkt_monitor(dhd_pub_t * dhdp)407 dhd_os_dbg_stop_pkt_monitor(dhd_pub_t *dhdp)
408 {
409 return dhd_dbg_stop_pkt_monitor(dhdp);
410 }
411
412 int
dhd_os_dbg_monitor_get_tx_pkts(dhd_pub_t * dhdp,void __user * user_buf,uint16 req_count,uint16 * resp_count)413 dhd_os_dbg_monitor_get_tx_pkts(dhd_pub_t *dhdp, void __user *user_buf,
414 uint16 req_count, uint16 *resp_count)
415 {
416 return dhd_dbg_monitor_get_tx_pkts(dhdp, user_buf, req_count, resp_count);
417 }
418
419 int
dhd_os_dbg_monitor_get_rx_pkts(dhd_pub_t * dhdp,void __user * user_buf,uint16 req_count,uint16 * resp_count)420 dhd_os_dbg_monitor_get_rx_pkts(dhd_pub_t *dhdp, void __user *user_buf,
421 uint16 req_count, uint16 *resp_count)
422 {
423 return dhd_dbg_monitor_get_rx_pkts(dhdp, user_buf, req_count, resp_count);
424 }
425
426 int
dhd_os_dbg_detach_pkt_monitor(dhd_pub_t * dhdp)427 dhd_os_dbg_detach_pkt_monitor(dhd_pub_t *dhdp)
428 {
429 return dhd_dbg_detach_pkt_monitor(dhdp);
430 }
431 #endif /* DBG_PKT_MON */
432
433 int
dhd_os_dbg_get_feature(dhd_pub_t * dhdp,int32 * features)434 dhd_os_dbg_get_feature(dhd_pub_t *dhdp, int32 *features)
435 {
436 int ret = BCME_OK;
437 *features = 0;
438 #ifdef DEBUGABILITY
439 // fix for RequestFirmwareDebugDump issue of VTS
440 if ((dhdp->conf->chip != BCM43751_CHIP_ID) && (dhdp->conf->chip != BCM43752_CHIP_ID) &&
441 (dhdp->conf->chip != BCM4375_CHIP_ID))
442 *features |= DBG_MEMORY_DUMP_SUPPORTED;
443 if (FW_SUPPORTED(dhdp, logtrace)) {
444 *features |= DBG_CONNECT_EVENT_SUPPORTED;
445 *features |= DBG_VERBOSE_LOG_SUPPORTED;
446 }
447 if (FW_SUPPORTED(dhdp, hchk)) {
448 *features |= DBG_HEALTH_CHECK_SUPPORTED;
449 }
450 #ifdef DBG_PKT_MON
451 if (FW_SUPPORTED(dhdp, d11status)) {
452 *features |= DBG_PACKET_FATE_SUPPORTED;
453 }
454 #endif /* DBG_PKT_MON */
455 #endif /* DEBUGABILITY */
456 return ret;
457 }
458
459 static void
dhd_os_dbg_pullreq(void * os_priv,int ring_id)460 dhd_os_dbg_pullreq(void *os_priv, int ring_id)
461 {
462 linux_dbgring_info_t *ring_info;
463
464 ring_info = &((linux_dbgring_info_t *)os_priv)[ring_id];
465 cancel_delayed_work(&ring_info->work);
466 schedule_delayed_work(&ring_info->work, 0);
467 }
468
469 int
dhd_os_dbg_attach(dhd_pub_t * dhdp)470 dhd_os_dbg_attach(dhd_pub_t *dhdp)
471 {
472 int ret = BCME_OK;
473 linux_dbgring_info_t *os_priv, *ring_info;
474 int ring_id;
475
476 /* os_dbg data */
477 os_priv = MALLOCZ(dhdp->osh, sizeof(*os_priv) * DEBUG_RING_ID_MAX);
478 if (!os_priv)
479 return BCME_NOMEM;
480
481 for (ring_id = DEBUG_RING_ID_INVALID + 1; ring_id < DEBUG_RING_ID_MAX;
482 ring_id++) {
483 ring_info = &os_priv[ring_id];
484 INIT_DELAYED_WORK(&ring_info->work, dbg_ring_poll_worker);
485 ring_info->dhdp = dhdp;
486 ring_info->ring_id = ring_id;
487 }
488
489 ret = dhd_dbg_attach(dhdp, dhd_os_dbg_pullreq, dhd_os_dbg_urgent_notifier, os_priv);
490 if (ret)
491 MFREE(dhdp->osh, os_priv, sizeof(*os_priv) * DEBUG_RING_ID_MAX);
492
493 return ret;
494 }
495
496 void
dhd_os_dbg_detach(dhd_pub_t * dhdp)497 dhd_os_dbg_detach(dhd_pub_t *dhdp)
498 {
499 linux_dbgring_info_t *os_priv, *ring_info;
500 int ring_id;
501 /* free os_dbg data */
502 os_priv = dhd_dbg_get_priv(dhdp);
503 if (!os_priv)
504 return;
505 /* abort pending any job */
506 for (ring_id = DEBUG_RING_ID_INVALID + 1; ring_id < DEBUG_RING_ID_MAX; ring_id++) {
507 ring_info = &os_priv[ring_id];
508 if (ring_info->interval) {
509 ring_info->interval = 0;
510 cancel_delayed_work_sync(&ring_info->work);
511 }
512 }
513 MFREE(dhdp->osh, os_priv, sizeof(*os_priv) * DEBUG_RING_ID_MAX);
514
515 return dhd_dbg_detach(dhdp);
516 }
517