xref: /wlan-dirver/qca-wifi-host-cmn/umac/cmn_services/sm_engine/src/wlan_sm_engine_dbg.c (revision f28396d060cff5c6519f883cb28ae0116ce479f1)
1 /*
2  * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /**
18  * DOC: Implements general SM debug framework
19  */
20 #include <wlan_sm_engine.h>
21 #include <wlan_sm_engine_dbg.h>
22 
23 void wlan_sm_save_history(struct wlan_sm *sm,
24 			  enum wlan_sm_trace_type trace_type,
25 			  uint8_t initial_state, uint8_t final_state,
26 			  uint16_t event_type)
27 {
28 	struct wlan_sm_history *p_sm_history = &sm->history;
29 	struct wlan_sm_history_info *p_memento;
30 
31 	/*
32 	 * History saved in circular buffer.
33 	 * Save a pointer to next write location and increment pointer.
34 	 */
35 	qdf_spin_lock_bh(&p_sm_history->sm_history_lock);
36 	p_memento = &p_sm_history->data[p_sm_history->index];
37 	p_sm_history->index++;
38 
39 	p_sm_history->index %= WLAN_SM_ENGINE_HISTORY_SIZE;
40 
41 	qdf_spin_unlock_bh(&p_sm_history->sm_history_lock);
42 
43 	qdf_mem_zero(p_memento, sizeof(*p_memento));
44 	p_memento->trace_type = trace_type;
45 	p_memento->initial_state = initial_state;
46 	p_memento->final_state = final_state;
47 	p_memento->event_type = event_type;
48 }
49 
50 void wlan_sm_history_init(struct wlan_sm *sm)
51 {
52 	qdf_spinlock_create(&sm->history.sm_history_lock);
53 	qdf_mem_zero(&sm->history, sizeof(struct wlan_sm_history));
54 }
55 
56 void wlan_sm_history_delete(struct wlan_sm *sm)
57 {
58 	qdf_spinlock_destroy(&sm->history.sm_history_lock);
59 }
60 
61 static void wlan_sm_print_history_entry(struct wlan_sm *sm,
62 					struct wlan_sm_history_info *ent,
63 					uint16_t i)
64 {
65 	const char *event_name = NULL;
66 
67 	if (sm->event_names) {
68 		if (ent->event_type < sm->num_event_names)
69 			event_name = sm->event_names[ent->event_type];
70 
71 		if (!ent->trace_type)
72 			return;
73 
74 		sm_engine_nofl_err(
75 				"|%6d |%11d |%23s[%3d] |%19s[%2d] |%19s[%2d] |",
76 				i, ent->trace_type,
77 				event_name ? event_name : "UNKNOWN_EVENT",
78 				ent->event_type,
79 				sm->state_info[ent->initial_state].name,
80 				ent->initial_state,
81 				sm->state_info[ent->final_state].name,
82 				ent->final_state);
83 	} else {
84 		sm_engine_nofl_err(
85 				"|%6d |%11d |%28d |%19s[%2d] |%19s[%2d] |",
86 				i, ent->trace_type,
87 				ent->event_type,
88 				sm->state_info[ent->initial_state].name,
89 				ent->initial_state,
90 				sm->state_info[ent->final_state].name,
91 				ent->final_state);
92 	}
93 }
94 
95 void wlan_sm_print_history(struct wlan_sm *sm)
96 {
97 	struct wlan_sm_history *p_sm_history = &sm->history;
98 	uint8_t i;
99 	uint8_t idx;
100 
101 	/*
102 	 * History saved in circular buffer.
103 	 * Save a pointer to next write location and increment pointer.
104 	 */
105 	qdf_spin_lock_bh(&p_sm_history->sm_history_lock);
106 
107 	sm_engine_nofl_err("|%6s |%11s |%28s |%23s |%23s |", "Index",
108 			   "Trace Type", "Event",
109 			   "Initial State", "Final State");
110 
111 	for (i = 0; i < WLAN_SM_ENGINE_HISTORY_SIZE; i++) {
112 		idx = (p_sm_history->index + i) % WLAN_SM_ENGINE_HISTORY_SIZE;
113 		wlan_sm_print_history_entry(
114 			sm, &p_sm_history->data[idx], idx);
115 	}
116 
117 	qdf_spin_unlock_bh(&p_sm_history->sm_history_lock);
118 }
119 
120 #if SM_HIST_DEBUGFS_SUPPORT
121 static void wlan_sm_print_fs_history_entry(struct wlan_sm *sm,
122 					   struct wlan_sm_history_info *ent,
123 					   uint16_t i, qdf_debugfs_file_t m)
124 {
125 	const char *event_name = NULL;
126 
127 	if (sm->event_names) {
128 		if (ent->event_type < sm->num_event_names)
129 			event_name = sm->event_names[ent->event_type];
130 
131 		if (!ent->trace_type)
132 			return;
133 
134 		qdf_debugfs_printf(m,
135 				   "|%6d |%11d |%23s[%3d] |%19s[%2d] |%19s[%2d] |\n",
136 				   i, ent->trace_type,
137 				   event_name ? event_name : "UNKNOWN_EVENT",
138 				   ent->event_type,
139 				   sm->state_info[ent->initial_state].name,
140 				   ent->initial_state,
141 				   sm->state_info[ent->final_state].name,
142 				   ent->final_state);
143 	} else {
144 		qdf_debugfs_printf(m,
145 				   "|%6d |%11d |%28d |%19s[%2d] |%19s[%2d] |\n",
146 				   i, ent->trace_type,
147 				   ent->event_type,
148 				   sm->state_info[ent->initial_state].name,
149 				   ent->initial_state,
150 				   sm->state_info[ent->final_state].name,
151 				   ent->final_state);
152 	}
153 }
154 
155 void wlan_sm_print_fs_history(struct wlan_sm *sm, qdf_debugfs_file_t m)
156 {
157 	struct wlan_sm_history *p_sm_history = &sm->history;
158 	uint8_t i;
159 	uint8_t idx;
160 
161 	/*
162 	 * History saved in circular buffer.
163 	 * Save a pointer to next write location and increment pointer.
164 	 */
165 	qdf_spin_lock_bh(&p_sm_history->sm_history_lock);
166 	qdf_debugfs_printf(m, "|%6s |%11s |%28s |%23s |%23s |\n", "Index",
167 			   "Trace Type", "Event",
168 			   "Initial State", "Final State");
169 
170 	for (i = 0; i < WLAN_SM_ENGINE_HISTORY_SIZE; i++) {
171 		idx = (p_sm_history->index + i) % WLAN_SM_ENGINE_HISTORY_SIZE;
172 		wlan_sm_print_fs_history_entry(sm, &p_sm_history->data[idx],
173 					       idx, m);
174 	}
175 
176 	qdf_spin_unlock_bh(&p_sm_history->sm_history_lock);
177 }
178 #endif
179