1 /*
2  * Copyright (c) 2020-2021, 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 #include <qdf_hang_event_notifier.h>
17 #include <qdf_notifier.h>
18 #include <wmi_hang_event.h>
19 #include <wmi_unified_priv.h>
20 #include <qdf_trace.h>
21 
22 struct wmi_hang_data_fixed_param {
23 	uint16_t tlv_header; /* tlv tag and length */
24 	uint32_t event;
25 	uint32_t data;
26 	uint64_t time;
27 } qdf_packed;
28 
29 #define WMI_EVT_HIST 0
30 #define WMI_CMD_HIST 1
31 #define NUM_HANG_WMI_HISTORY 1
32 
wmi_log_history(struct notifier_block * block,void * data,uint8_t wmi_history)33 static void wmi_log_history(struct notifier_block *block, void *data,
34 			    uint8_t wmi_history)
35 {
36 	qdf_notif_block *notif_block = qdf_container_of(block, qdf_notif_block,
37 							notif_block);
38 	struct qdf_notifer_data *wmi_hang_data = data;
39 	int nread, pos, total_len;
40 	unsigned int wmi_ring_size = NUM_HANG_WMI_HISTORY;
41 	uint64_t secs, usecs;
42 	struct wmi_event_debug *wmi_evt;
43 	struct wmi_unified *wmi_handle;
44 	struct wmi_log_buf_t *wmi_log;
45 	struct wmi_hang_data_fixed_param *cmd;
46 	struct wmi_command_debug *wmi_cmd;
47 	uint8_t *wmi_buf_ptr;
48 
49 	if (!wmi_hang_data)
50 		return;
51 
52 	wmi_handle = notif_block->priv_data;
53 	if (!wmi_handle)
54 		return;
55 
56 	if (wmi_history == WMI_EVT_HIST)
57 		wmi_log = &wmi_handle->log_info.wmi_event_log_buf_info;
58 	else
59 		wmi_log = &wmi_handle->log_info.wmi_command_log_buf_info;
60 
61 	total_len = sizeof(struct wmi_hang_data_fixed_param);
62 
63 	if (wmi_log->length <= wmi_ring_size)
64 		nread = wmi_log->length;
65 	else
66 		nread = wmi_ring_size;
67 
68 	if (*wmi_log->p_buf_tail_idx == 0)
69 		/* tail can be 0 after wrap-around */
70 		pos = wmi_ring_size - 1;
71 	else
72 		pos = *wmi_log->p_buf_tail_idx - 1;
73 
74 	while (nread--) {
75 		if (wmi_hang_data->offset + total_len > QDF_WLAN_HANG_FW_OFFSET)
76 			return;
77 
78 		switch (wmi_history) {
79 		case WMI_EVT_HIST:
80 			wmi_buf_ptr = (wmi_hang_data->hang_data +
81 				       wmi_hang_data->offset);
82 			cmd = ((struct wmi_hang_data_fixed_param *)wmi_buf_ptr);
83 			QDF_HANG_EVT_SET_HDR(&cmd->tlv_header,
84 					     HANG_EVT_TAG_WMI_EVT_HIST,
85 		  QDF_HANG_GET_STRUCT_TLVLEN(struct wmi_hang_data_fixed_param));
86 		     wmi_evt = &(((struct wmi_event_debug *)wmi_log->buf)[pos]);
87 			cmd->event = wmi_evt->event;
88 			qdf_log_timestamp_to_secs(wmi_evt->time, &secs, &usecs);
89 			cmd->time = secs;
90 			cmd->data = wmi_evt->data[0];
91 			break;
92 		case WMI_CMD_HIST:
93 			wmi_buf_ptr = (wmi_hang_data->hang_data +
94 				       wmi_hang_data->offset);
95 			cmd = ((struct wmi_hang_data_fixed_param *)wmi_buf_ptr);
96 			QDF_HANG_EVT_SET_HDR(&cmd->tlv_header,
97 					     HANG_EVT_TAG_WMI_CMD_HIST,
98 		 QDF_HANG_GET_STRUCT_TLVLEN(struct wmi_hang_data_fixed_param));
99 		   wmi_cmd = &(((struct wmi_command_debug *)wmi_log->buf)[pos]);
100 			cmd->event = wmi_cmd->command;
101 			qdf_log_timestamp_to_secs(wmi_cmd->time, &secs, &usecs);
102 			cmd->time = secs;
103 			cmd->data = wmi_cmd->data[0];
104 			break;
105 		}
106 		if (pos == 0)
107 			pos = wmi_ring_size - 1;
108 		else
109 			pos--;
110 		wmi_hang_data->offset += total_len;
111 	}
112 }
113 
wmi_recovery_notifier_call(struct notifier_block * block,unsigned long state,void * data)114 static int wmi_recovery_notifier_call(struct notifier_block *block,
115 				      unsigned long state,
116 				      void *data)
117 {
118 	wmi_log_history(block, data, WMI_EVT_HIST);
119 	wmi_log_history(block, data, WMI_CMD_HIST);
120 
121 	return NOTIFY_OK;
122 }
123 
124 static qdf_notif_block wmi_recovery_notifier = {
125 	.notif_block.notifier_call = wmi_recovery_notifier_call,
126 };
127 
wmi_hang_event_notifier_register(struct wmi_unified * wmi_hdl)128 QDF_STATUS wmi_hang_event_notifier_register(struct wmi_unified *wmi_hdl)
129 {
130 	wmi_recovery_notifier.priv_data = wmi_hdl;
131 	return qdf_hang_event_register_notifier(&wmi_recovery_notifier);
132 }
133 
wmi_hang_event_notifier_unregister(void)134 QDF_STATUS wmi_hang_event_notifier_unregister(void)
135 {
136 	return qdf_hang_event_unregister_notifier(&wmi_recovery_notifier);
137 }
138