1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright(c) 2020-2021 Intel Corporation
4  */
5 
6 #include "iwl-drv.h"
7 #include "pnvm.h"
8 #include "iwl-prph.h"
9 #include "iwl-io.h"
10 #include "fw/api/commands.h"
11 #include "fw/api/nvm-reg.h"
12 #include "fw/api/alive.h"
13 #include "fw/uefi.h"
14 
15 struct iwl_pnvm_section {
16 	__le32 offset;
17 	const u8 data[];
18 } __packed;
19 
20 static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait,
21 				 struct iwl_rx_packet *pkt, void *data)
22 {
23 	struct iwl_trans *trans = (struct iwl_trans *)data;
24 	struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data;
25 
26 	IWL_DEBUG_FW(trans,
27 		     "PNVM complete notification received with status %d\n",
28 		     le32_to_cpu(pnvm_ntf->status));
29 
30 	return true;
31 }
32 
33 static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data,
34 				   size_t len)
35 {
36 	struct iwl_ucode_tlv *tlv;
37 	u32 sha1 = 0;
38 	u16 mac_type = 0, rf_id = 0;
39 	u8 *pnvm_data = NULL, *tmp;
40 	u32 size = 0;
41 	int ret;
42 
43 	IWL_DEBUG_FW(trans, "Handling PNVM section\n");
44 
45 	while (len >= sizeof(*tlv)) {
46 		u32 tlv_len, tlv_type;
47 
48 		len -= sizeof(*tlv);
49 		tlv = (void *)data;
50 
51 		tlv_len = le32_to_cpu(tlv->length);
52 		tlv_type = le32_to_cpu(tlv->type);
53 
54 		if (len < tlv_len) {
55 			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
56 				len, tlv_len);
57 			ret = -EINVAL;
58 			goto out;
59 		}
60 
61 		data += sizeof(*tlv);
62 
63 		switch (tlv_type) {
64 		case IWL_UCODE_TLV_PNVM_VERSION:
65 			if (tlv_len < sizeof(__le32)) {
66 				IWL_DEBUG_FW(trans,
67 					     "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n",
68 					     sizeof(__le32), tlv_len);
69 				break;
70 			}
71 
72 			sha1 = le32_to_cpup((__le32 *)data);
73 
74 			IWL_DEBUG_FW(trans,
75 				     "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n",
76 				     sha1);
77 			break;
78 		case IWL_UCODE_TLV_HW_TYPE:
79 			if (tlv_len < 2 * sizeof(__le16)) {
80 				IWL_DEBUG_FW(trans,
81 					     "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n",
82 					     2 * sizeof(__le16), tlv_len);
83 				break;
84 			}
85 
86 			mac_type = le16_to_cpup((__le16 *)data);
87 			rf_id = le16_to_cpup((__le16 *)(data + sizeof(__le16)));
88 
89 			IWL_DEBUG_FW(trans,
90 				     "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n",
91 				     mac_type, rf_id);
92 
93 			if (mac_type != CSR_HW_REV_TYPE(trans->hw_rev) ||
94 			    rf_id != CSR_HW_RFID_TYPE(trans->hw_rf_id)) {
95 				IWL_DEBUG_FW(trans,
96 					     "HW mismatch, skipping PNVM section, mac_type 0x%0x, rf_id 0x%0x.\n",
97 					     CSR_HW_REV_TYPE(trans->hw_rev), trans->hw_rf_id);
98 				ret = -ENOENT;
99 				goto out;
100 			}
101 
102 			break;
103 		case IWL_UCODE_TLV_SEC_RT: {
104 			struct iwl_pnvm_section *section = (void *)data;
105 			u32 data_len = tlv_len - sizeof(*section);
106 
107 			IWL_DEBUG_FW(trans,
108 				     "Got IWL_UCODE_TLV_SEC_RT len %d\n",
109 				     tlv_len);
110 
111 			/* TODO: remove, this is a deprecated separator */
112 			if (le32_to_cpup((__le32 *)data) == 0xddddeeee) {
113 				IWL_DEBUG_FW(trans, "Ignoring separator.\n");
114 				break;
115 			}
116 
117 			IWL_DEBUG_FW(trans, "Adding data (size %d)\n",
118 				     data_len);
119 
120 			tmp = krealloc(pnvm_data, size + data_len, GFP_KERNEL);
121 			if (!tmp) {
122 				IWL_DEBUG_FW(trans,
123 					     "Couldn't allocate (more) pnvm_data\n");
124 
125 				ret = -ENOMEM;
126 				goto out;
127 			}
128 
129 			pnvm_data = tmp;
130 
131 			memcpy(pnvm_data + size, section->data, data_len);
132 
133 			size += data_len;
134 
135 			break;
136 		}
137 		case IWL_UCODE_TLV_PNVM_SKU:
138 			IWL_DEBUG_FW(trans,
139 				     "New PNVM section started, stop parsing.\n");
140 			goto done;
141 		default:
142 			IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",
143 				     tlv_type, tlv_len);
144 			break;
145 		}
146 
147 		len -= ALIGN(tlv_len, 4);
148 		data += ALIGN(tlv_len, 4);
149 	}
150 
151 done:
152 	if (!size) {
153 		IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n");
154 		ret = -ENOENT;
155 		goto out;
156 	}
157 
158 	IWL_INFO(trans, "loaded PNVM version 0x%0x\n", sha1);
159 
160 	ret = iwl_trans_set_pnvm(trans, pnvm_data, size);
161 out:
162 	kfree(pnvm_data);
163 	return ret;
164 }
165 
166 static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data,
167 			  size_t len)
168 {
169 	struct iwl_ucode_tlv *tlv;
170 
171 	IWL_DEBUG_FW(trans, "Parsing PNVM file\n");
172 
173 	while (len >= sizeof(*tlv)) {
174 		u32 tlv_len, tlv_type;
175 
176 		len -= sizeof(*tlv);
177 		tlv = (void *)data;
178 
179 		tlv_len = le32_to_cpu(tlv->length);
180 		tlv_type = le32_to_cpu(tlv->type);
181 
182 		if (len < tlv_len) {
183 			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
184 				len, tlv_len);
185 			return -EINVAL;
186 		}
187 
188 		if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) {
189 			struct iwl_sku_id *sku_id =
190 				(void *)(data + sizeof(*tlv));
191 
192 			IWL_DEBUG_FW(trans,
193 				     "Got IWL_UCODE_TLV_PNVM_SKU len %d\n",
194 				     tlv_len);
195 			IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n",
196 				     le32_to_cpu(sku_id->data[0]),
197 				     le32_to_cpu(sku_id->data[1]),
198 				     le32_to_cpu(sku_id->data[2]));
199 
200 			data += sizeof(*tlv) + ALIGN(tlv_len, 4);
201 			len -= ALIGN(tlv_len, 4);
202 
203 			if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) &&
204 			    trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) &&
205 			    trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) {
206 				int ret;
207 
208 				ret = iwl_pnvm_handle_section(trans, data, len);
209 				if (!ret)
210 					return 0;
211 			} else {
212 				IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");
213 			}
214 		} else {
215 			data += sizeof(*tlv) + ALIGN(tlv_len, 4);
216 			len -= ALIGN(tlv_len, 4);
217 		}
218 	}
219 
220 	return -ENOENT;
221 }
222 
223 static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len)
224 {
225 	const struct firmware *pnvm;
226 	char pnvm_name[64];
227 	int ret;
228 
229 	/*
230 	 * The prefix unfortunately includes a hyphen at the end, so
231 	 * don't add the dot here...
232 	 */
233 	snprintf(pnvm_name, sizeof(pnvm_name), "%spnvm",
234 		 trans->cfg->fw_name_pre);
235 
236 	/* ...but replace the hyphen with the dot here. */
237 	if (strlen(trans->cfg->fw_name_pre) < sizeof(pnvm_name))
238 		pnvm_name[strlen(trans->cfg->fw_name_pre) - 1] = '.';
239 
240 	ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev);
241 	if (ret) {
242 		IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n",
243 			     pnvm_name, ret);
244 		return ret;
245 	}
246 
247 	*data = kmemdup(pnvm->data, pnvm->size, GFP_KERNEL);
248 	if (!*data)
249 		return -ENOMEM;
250 
251 	*len = pnvm->size;
252 
253 	return 0;
254 }
255 
256 int iwl_pnvm_load(struct iwl_trans *trans,
257 		  struct iwl_notif_wait_data *notif_wait)
258 {
259 	u8 *data;
260 	size_t len;
261 	struct pnvm_sku_package *package;
262 	struct iwl_notification_wait pnvm_wait;
263 	static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP,
264 						PNVM_INIT_COMPLETE_NTFY) };
265 	int ret;
266 
267 	/* if the SKU_ID is empty, there's nothing to do */
268 	if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2])
269 		return 0;
270 
271 	/*
272 	 * If we already loaded (or tried to load) it before, we just
273 	 * need to set it again.
274 	 */
275 	if (trans->pnvm_loaded) {
276 		ret = iwl_trans_set_pnvm(trans, NULL, 0);
277 		if (ret)
278 			return ret;
279 		goto skip_parse;
280 	}
281 
282 	/* First attempt to get the PNVM from BIOS */
283 	package = iwl_uefi_get_pnvm(trans, &len);
284 	if (!IS_ERR_OR_NULL(package)) {
285 		data = kmemdup(package->data, len, GFP_KERNEL);
286 
287 		/* free package regardless of whether kmemdup succeeded */
288 		kfree(package);
289 
290 		if (data) {
291 			/* we need only the data size */
292 			len -= sizeof(*package);
293 			goto parse;
294 		}
295 	}
296 
297 	/* If it's not available, try from the filesystem */
298 	ret = iwl_pnvm_get_from_fs(trans, &data, &len);
299 	if (ret) {
300 		/*
301 		 * Pretend we've loaded it - at least we've tried and
302 		 * couldn't load it at all, so there's no point in
303 		 * trying again over and over.
304 		 */
305 		trans->pnvm_loaded = true;
306 
307 		goto skip_parse;
308 	}
309 
310 parse:
311 	iwl_pnvm_parse(trans, data, len);
312 
313 	kfree(data);
314 
315 skip_parse:
316 	data = NULL;
317 	/* now try to get the reduce power table, if not loaded yet */
318 	if (!trans->reduce_power_loaded) {
319 		data = iwl_uefi_get_reduced_power(trans, &len);
320 		if (IS_ERR_OR_NULL(data)) {
321 			/*
322 			 * Pretend we've loaded it - at least we've tried and
323 			 * couldn't load it at all, so there's no point in
324 			 * trying again over and over.
325 			 */
326 			trans->reduce_power_loaded = true;
327 
328 			goto skip_reduce_power;
329 		}
330 	}
331 
332 	ret = iwl_trans_set_reduce_power(trans, data, len);
333 	if (ret)
334 		IWL_DEBUG_FW(trans,
335 			     "Failed to set reduce power table %d\n",
336 			     ret);
337 	kfree(data);
338 
339 skip_reduce_power:
340 	iwl_init_notification_wait(notif_wait, &pnvm_wait,
341 				   ntf_cmds, ARRAY_SIZE(ntf_cmds),
342 				   iwl_pnvm_complete_fn, trans);
343 
344 	/* kick the doorbell */
345 	iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
346 			    UREG_DOORBELL_TO_ISR6_PNVM);
347 
348 	return iwl_wait_notification(notif_wait, &pnvm_wait,
349 				     MVM_UCODE_PNVM_TIMEOUT);
350 }
351 IWL_EXPORT_SYMBOL(iwl_pnvm_load);
352