1 /*
2  * Copyright (c) 2018, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #ifndef __LIB_TRACER_H__
34 #define __LIB_TRACER_H__
35 
36 #include <linux/mlx5/driver.h>
37 #include "mlx5_core.h"
38 
39 #define STRINGS_DB_SECTIONS_NUM 8
40 #define STRINGS_DB_READ_SIZE_BYTES 256
41 #define STRINGS_DB_LEFTOVER_SIZE_BYTES 64
42 #define TRACER_BUFFER_PAGE_NUM 64
43 #define TRACER_BUFFER_CHUNK 4096
44 #define TRACE_BUFFER_SIZE_BYTE (TRACER_BUFFER_PAGE_NUM * TRACER_BUFFER_CHUNK)
45 
46 #define TRACER_BLOCK_SIZE_BYTE 256
47 #define TRACES_PER_BLOCK 32
48 
49 #define TRACE_STR_MSG 256
50 #define SAVED_TRACES_NUM 8192
51 
52 #define TRACER_MAX_PARAMS 7
53 #define MESSAGE_HASH_BITS 6
54 #define MESSAGE_HASH_SIZE BIT(MESSAGE_HASH_BITS)
55 
56 #define MASK_52_7 (0x1FFFFFFFFFFF80)
57 #define MASK_6_0  (0x7F)
58 
59 struct mlx5_fw_trace_data {
60 	u64 timestamp;
61 	bool lost;
62 	u8 event_id;
63 	char msg[TRACE_STR_MSG];
64 };
65 
66 enum mlx5_fw_tracer_state {
67 	MLX5_TRACER_STATE_UP = BIT(0),
68 	MLX5_TRACER_RECREATE_DB = BIT(1),
69 };
70 
71 struct mlx5_fw_tracer {
72 	struct mlx5_core_dev *dev;
73 	struct mlx5_nb        nb;
74 	bool owner;
75 	u8   trc_ver;
76 	struct workqueue_struct *work_queue;
77 	struct work_struct ownership_change_work;
78 	struct work_struct read_fw_strings_work;
79 
80 	/* Strings DB */
81 	struct {
82 		u8 first_string_trace;
83 		u8 num_string_trace;
84 		u32 num_string_db;
85 		u32 base_address_out[STRINGS_DB_SECTIONS_NUM];
86 		u32 size_out[STRINGS_DB_SECTIONS_NUM];
87 		void *buffer[STRINGS_DB_SECTIONS_NUM];
88 		bool loaded;
89 	} str_db;
90 
91 	/* Log Buffer */
92 	struct {
93 		u32 pdn;
94 		void *log_buf;
95 		dma_addr_t dma;
96 		u32 size;
97 		u32 mkey;
98 		u32 consumer_index;
99 	} buff;
100 
101 	/* Saved Traces Array */
102 	struct {
103 		struct mlx5_fw_trace_data straces[SAVED_TRACES_NUM];
104 		u32 saved_traces_index;
105 		struct mutex lock; /* Protect st_arr access */
106 	} st_arr;
107 
108 	u64 last_timestamp;
109 	struct work_struct handle_traces_work;
110 	struct hlist_head hash[MESSAGE_HASH_SIZE];
111 	struct list_head ready_strings_list;
112 	struct work_struct update_db_work;
113 	struct mutex state_lock; /* Synchronize update work with reload flows */
114 	unsigned long state;
115 };
116 
117 struct tracer_string_format {
118 	char *string;
119 	int params[TRACER_MAX_PARAMS];
120 	int num_of_params;
121 	int last_param_num;
122 	u8 event_id;
123 	u32 tmsn;
124 	struct hlist_node hlist;
125 	struct list_head list;
126 	u32 timestamp;
127 	bool lost;
128 };
129 
130 enum mlx5_fw_tracer_ownership_state {
131 	MLX5_FW_TRACER_RELEASE_OWNERSHIP,
132 	MLX5_FW_TRACER_ACQUIRE_OWNERSHIP,
133 };
134 
135 enum tracer_ctrl_fields_select {
136 	TRACE_STATUS = 1 << 0,
137 };
138 
139 enum tracer_event_type {
140 	TRACER_EVENT_TYPE_STRING,
141 	TRACER_EVENT_TYPE_TIMESTAMP = 0xFF,
142 	TRACER_EVENT_TYPE_UNRECOGNIZED,
143 };
144 
145 enum tracing_mode {
146 	TRACE_TO_MEMORY = 1 << 0,
147 };
148 
149 struct tracer_timestamp_event {
150 	u64        timestamp;
151 	u8         unreliable;
152 };
153 
154 struct tracer_string_event {
155 	u32        timestamp;
156 	u32        tmsn;
157 	u32        tdsn;
158 	u32        string_param;
159 };
160 
161 struct tracer_event {
162 	bool      lost_event;
163 	u32       type;
164 	u8        event_id;
165 	union {
166 		struct tracer_string_event string_event;
167 		struct tracer_timestamp_event timestamp_event;
168 	};
169 	u64 *out;
170 };
171 
172 struct mlx5_ifc_tracer_event_bits {
173 	u8         lost[0x1];
174 	u8         timestamp[0x7];
175 	u8         event_id[0x8];
176 	u8         event_data[0x30];
177 };
178 
179 struct mlx5_ifc_tracer_string_event_bits {
180 	u8         lost[0x1];
181 	u8         timestamp[0x7];
182 	u8         event_id[0x8];
183 	u8         tmsn[0xd];
184 	u8         tdsn[0x3];
185 	u8         string_param[0x20];
186 };
187 
188 struct mlx5_ifc_tracer_timestamp_event_bits {
189 	u8         timestamp7_0[0x8];
190 	u8         event_id[0x8];
191 	u8         urts[0x3];
192 	u8         timestamp52_40[0xd];
193 	u8         timestamp39_8[0x20];
194 };
195 
196 struct mlx5_fw_tracer *mlx5_fw_tracer_create(struct mlx5_core_dev *dev);
197 int mlx5_fw_tracer_init(struct mlx5_fw_tracer *tracer);
198 void mlx5_fw_tracer_cleanup(struct mlx5_fw_tracer *tracer);
199 void mlx5_fw_tracer_destroy(struct mlx5_fw_tracer *tracer);
200 int mlx5_fw_tracer_trigger_core_dump_general(struct mlx5_core_dev *dev);
201 int mlx5_fw_tracer_get_saved_traces_objects(struct mlx5_fw_tracer *tracer,
202 					    struct devlink_fmsg *fmsg);
203 int mlx5_fw_tracer_reload(struct mlx5_fw_tracer *tracer);
204 
205 #endif
206