1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright (C) 2018 - 2019 Intel Corporation
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright (C) 2018 - 2019 Intel Corporation
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  *
38  *  * Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  *  * Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in
42  *    the documentation and/or other materials provided with the
43  *    distribution.
44  *  * Neither the name Intel Corporation nor the names of its
45  *    contributors may be used to endorse or promote products derived
46  *    from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  *
60  *****************************************************************************/
61 
62 #include <linux/firmware.h>
63 #include "iwl-drv.h"
64 #include "iwl-trans.h"
65 #include "iwl-dbg-tlv.h"
66 #include "fw/dbg.h"
67 #include "fw/runtime.h"
68 
69 /**
70  * enum iwl_dbg_tlv_type - debug TLV types
71  * @IWL_DBG_TLV_TYPE_DEBUG_INFO: debug info TLV
72  * @IWL_DBG_TLV_TYPE_BUF_ALLOC: buffer allocation TLV
73  * @IWL_DBG_TLV_TYPE_HCMD: host command TLV
74  * @IWL_DBG_TLV_TYPE_REGION: region TLV
75  * @IWL_DBG_TLV_TYPE_TRIGGER: trigger TLV
76  * @IWL_DBG_TLV_TYPE_NUM: number of debug TLVs
77  */
78 enum iwl_dbg_tlv_type {
79 	IWL_DBG_TLV_TYPE_DEBUG_INFO =
80 		IWL_UCODE_TLV_TYPE_DEBUG_INFO - IWL_UCODE_TLV_DEBUG_BASE,
81 	IWL_DBG_TLV_TYPE_BUF_ALLOC,
82 	IWL_DBG_TLV_TYPE_HCMD,
83 	IWL_DBG_TLV_TYPE_REGION,
84 	IWL_DBG_TLV_TYPE_TRIGGER,
85 	IWL_DBG_TLV_TYPE_NUM,
86 };
87 
88 /**
89  * struct iwl_dbg_tlv_ver_data -  debug TLV version struct
90  * @min_ver: min version supported
91  * @max_ver: max version supported
92  */
93 struct iwl_dbg_tlv_ver_data {
94 	int min_ver;
95 	int max_ver;
96 };
97 
98 static const struct iwl_dbg_tlv_ver_data
99 dbg_ver_table[IWL_DBG_TLV_TYPE_NUM] = {
100 	[IWL_DBG_TLV_TYPE_DEBUG_INFO]	= {.min_ver = 1, .max_ver = 1,},
101 	[IWL_DBG_TLV_TYPE_BUF_ALLOC]	= {.min_ver = 1, .max_ver = 1,},
102 	[IWL_DBG_TLV_TYPE_HCMD]		= {.min_ver = 1, .max_ver = 1,},
103 	[IWL_DBG_TLV_TYPE_REGION]	= {.min_ver = 1, .max_ver = 1,},
104 	[IWL_DBG_TLV_TYPE_TRIGGER]	= {.min_ver = 1, .max_ver = 1,},
105 };
106 
107 static bool iwl_dbg_tlv_ver_support(struct iwl_ucode_tlv *tlv)
108 {
109 	struct iwl_fw_ini_header *hdr = (void *)&tlv->data[0];
110 	u32 type = le32_to_cpu(tlv->type);
111 	u32 tlv_idx = type - IWL_UCODE_TLV_DEBUG_BASE;
112 	u32 ver = le32_to_cpu(hdr->tlv_version);
113 
114 	if (ver < dbg_ver_table[tlv_idx].min_ver ||
115 	    ver > dbg_ver_table[tlv_idx].max_ver)
116 		return false;
117 
118 	return true;
119 }
120 
121 void iwl_dbg_tlv_alloc(struct iwl_trans *trans, struct iwl_ucode_tlv *tlv,
122 		       bool ext)
123 {
124 	struct iwl_fw_ini_header *hdr = (void *)&tlv->data[0];
125 	u32 type = le32_to_cpu(tlv->type);
126 	u32 pnt = le32_to_cpu(hdr->apply_point);
127 	u32 tlv_idx = type - IWL_UCODE_TLV_DEBUG_BASE;
128 	enum iwl_ini_cfg_state *cfg_state = ext ?
129 		&trans->dbg.external_ini_cfg : &trans->dbg.internal_ini_cfg;
130 
131 	IWL_DEBUG_FW(trans, "WRT: read TLV 0x%x, apply point %d\n",
132 		     type, pnt);
133 
134 	if (tlv_idx >= IWL_DBG_TLV_TYPE_NUM) {
135 		IWL_ERR(trans, "WRT: Unsupported TLV 0x%x\n", type);
136 		goto out_err;
137 	}
138 
139 	if (!iwl_dbg_tlv_ver_support(tlv)) {
140 		IWL_ERR(trans, "WRT: Unsupported TLV 0x%x version %u\n", type,
141 			le32_to_cpu(hdr->tlv_version));
142 		goto out_err;
143 	}
144 
145 	if (*cfg_state == IWL_INI_CFG_STATE_NOT_LOADED)
146 		*cfg_state = IWL_INI_CFG_STATE_LOADED;
147 
148 	return;
149 
150 out_err:
151 	*cfg_state = IWL_INI_CFG_STATE_CORRUPTED;
152 }
153 
154 void iwl_dbg_tlv_del_timers(struct iwl_trans *trans)
155 {
156 	/* will be used later */
157 }
158 IWL_EXPORT_SYMBOL(iwl_dbg_tlv_del_timers);
159 
160 void iwl_dbg_tlv_free(struct iwl_trans *trans)
161 {
162 	/* will be used again later */
163 }
164 
165 static int iwl_dbg_tlv_parse_bin(struct iwl_trans *trans, const u8 *data,
166 				 size_t len)
167 {
168 	struct iwl_ucode_tlv *tlv;
169 	u32 tlv_len;
170 
171 	while (len >= sizeof(*tlv)) {
172 		len -= sizeof(*tlv);
173 		tlv = (void *)data;
174 
175 		tlv_len = le32_to_cpu(tlv->length);
176 
177 		if (len < tlv_len) {
178 			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
179 				len, tlv_len);
180 			return -EINVAL;
181 		}
182 		len -= ALIGN(tlv_len, 4);
183 		data += sizeof(*tlv) + ALIGN(tlv_len, 4);
184 
185 		iwl_dbg_tlv_alloc(trans, tlv, true);
186 	}
187 
188 	return 0;
189 }
190 
191 void iwl_dbg_tlv_load_bin(struct device *dev, struct iwl_trans *trans)
192 {
193 	const struct firmware *fw;
194 	int res;
195 
196 	if (!iwlwifi_mod_params.enable_ini)
197 		return;
198 
199 	res = request_firmware(&fw, "iwl-dbg-tlv.ini", dev);
200 	if (res)
201 		return;
202 
203 	iwl_dbg_tlv_parse_bin(trans, fw->data, fw->size);
204 
205 	release_firmware(fw);
206 }
207 
208 void iwl_dbg_tlv_time_point(struct iwl_fw_runtime *fwrt,
209 			    enum iwl_fw_ini_time_point tp_id,
210 			    union iwl_dbg_tlv_tp_data *tp_data)
211 {
212 	/* will be used later */
213 }
214 IWL_EXPORT_SYMBOL(iwl_dbg_tlv_time_point);
215