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