1b3e4c0f3SLuca Coelho // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2cdda18fbSLuca Coelho /*
3b99e32cbSAlon Giladi  * Copyright(c) 2020-2023 Intel Corporation
4cdda18fbSLuca Coelho  */
5b3e4c0f3SLuca Coelho 
6b3e4c0f3SLuca Coelho #include "iwl-drv.h"
7b3e4c0f3SLuca Coelho #include "pnvm.h"
8b3e4c0f3SLuca Coelho #include "iwl-prph.h"
9b3e4c0f3SLuca Coelho #include "iwl-io.h"
10b3e4c0f3SLuca Coelho #include "fw/api/commands.h"
11b3e4c0f3SLuca Coelho #include "fw/api/nvm-reg.h"
1269725928SLuca Coelho #include "fw/api/alive.h"
1384c3c995SLuca Coelho #include "fw/uefi.h"
1469725928SLuca Coelho 
1569725928SLuca Coelho struct iwl_pnvm_section {
1669725928SLuca Coelho 	__le32 offset;
1769725928SLuca Coelho 	const u8 data[];
1869725928SLuca Coelho } __packed;
19b3e4c0f3SLuca Coelho 
iwl_pnvm_complete_fn(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)20b3e4c0f3SLuca Coelho static bool iwl_pnvm_complete_fn(struct iwl_notif_wait_data *notif_wait,
21b3e4c0f3SLuca Coelho 				 struct iwl_rx_packet *pkt, void *data)
22b3e4c0f3SLuca Coelho {
23b3e4c0f3SLuca Coelho 	struct iwl_trans *trans = (struct iwl_trans *)data;
24b3e4c0f3SLuca Coelho 	struct iwl_pnvm_init_complete_ntfy *pnvm_ntf = (void *)pkt->data;
25b3e4c0f3SLuca Coelho 
26b3e4c0f3SLuca Coelho 	IWL_DEBUG_FW(trans,
272a1d2fcfSLuca Coelho 		     "PNVM complete notification received with status 0x%0x\n",
28b3e4c0f3SLuca Coelho 		     le32_to_cpu(pnvm_ntf->status));
29b3e4c0f3SLuca Coelho 
30b3e4c0f3SLuca Coelho 	return true;
31b3e4c0f3SLuca Coelho }
32b3e4c0f3SLuca Coelho 
iwl_pnvm_handle_section(struct iwl_trans * trans,const u8 * data,size_t len,struct iwl_pnvm_image * pnvm_data)3369725928SLuca Coelho static int iwl_pnvm_handle_section(struct iwl_trans *trans, const u8 *data,
34b99e32cbSAlon Giladi 				   size_t len,
35b99e32cbSAlon Giladi 				   struct iwl_pnvm_image *pnvm_data)
3669725928SLuca Coelho {
3773c289baSBjoern A. Zeeb 	const struct iwl_ucode_tlv *tlv;
3869725928SLuca Coelho 	u32 sha1 = 0;
3969725928SLuca Coelho 	u16 mac_type = 0, rf_id = 0;
400f673c16SJohannes Berg 	bool hw_match = false;
4169725928SLuca Coelho 
4269725928SLuca Coelho 	IWL_DEBUG_FW(trans, "Handling PNVM section\n");
4369725928SLuca Coelho 
44b99e32cbSAlon Giladi 	memset(pnvm_data, 0, sizeof(*pnvm_data));
45b99e32cbSAlon Giladi 
4669725928SLuca Coelho 	while (len >= sizeof(*tlv)) {
4769725928SLuca Coelho 		u32 tlv_len, tlv_type;
4869725928SLuca Coelho 
4969725928SLuca Coelho 		len -= sizeof(*tlv);
5073c289baSBjoern A. Zeeb 		tlv = (const void *)data;
5169725928SLuca Coelho 
5269725928SLuca Coelho 		tlv_len = le32_to_cpu(tlv->length);
5369725928SLuca Coelho 		tlv_type = le32_to_cpu(tlv->type);
5469725928SLuca Coelho 
5569725928SLuca Coelho 		if (len < tlv_len) {
5669725928SLuca Coelho 			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
5769725928SLuca Coelho 				len, tlv_len);
585f408503SAlon Giladi 			return -EINVAL;
5969725928SLuca Coelho 		}
6069725928SLuca Coelho 
6169725928SLuca Coelho 		data += sizeof(*tlv);
6269725928SLuca Coelho 
6369725928SLuca Coelho 		switch (tlv_type) {
6469725928SLuca Coelho 		case IWL_UCODE_TLV_PNVM_VERSION:
6569725928SLuca Coelho 			if (tlv_len < sizeof(__le32)) {
6669725928SLuca Coelho 				IWL_DEBUG_FW(trans,
6769725928SLuca Coelho 					     "Invalid size for IWL_UCODE_TLV_PNVM_VERSION (expected %zd, got %d)\n",
6869725928SLuca Coelho 					     sizeof(__le32), tlv_len);
6969725928SLuca Coelho 				break;
7069725928SLuca Coelho 			}
7169725928SLuca Coelho 
7273c289baSBjoern A. Zeeb 			sha1 = le32_to_cpup((const __le32 *)data);
7369725928SLuca Coelho 
7469725928SLuca Coelho 			IWL_DEBUG_FW(trans,
7569725928SLuca Coelho 				     "Got IWL_UCODE_TLV_PNVM_VERSION %0x\n",
7669725928SLuca Coelho 				     sha1);
77b99e32cbSAlon Giladi 			pnvm_data->version = sha1;
7869725928SLuca Coelho 			break;
7969725928SLuca Coelho 		case IWL_UCODE_TLV_HW_TYPE:
8069725928SLuca Coelho 			if (tlv_len < 2 * sizeof(__le16)) {
8169725928SLuca Coelho 				IWL_DEBUG_FW(trans,
8269725928SLuca Coelho 					     "Invalid size for IWL_UCODE_TLV_HW_TYPE (expected %zd, got %d)\n",
8369725928SLuca Coelho 					     2 * sizeof(__le16), tlv_len);
8469725928SLuca Coelho 				break;
8569725928SLuca Coelho 			}
8669725928SLuca Coelho 
870f673c16SJohannes Berg 			if (hw_match)
880f673c16SJohannes Berg 				break;
890f673c16SJohannes Berg 
9073c289baSBjoern A. Zeeb 			mac_type = le16_to_cpup((const __le16 *)data);
9173c289baSBjoern A. Zeeb 			rf_id = le16_to_cpup((const __le16 *)(data + sizeof(__le16)));
9269725928SLuca Coelho 
9369725928SLuca Coelho 			IWL_DEBUG_FW(trans,
9469725928SLuca Coelho 				     "Got IWL_UCODE_TLV_HW_TYPE mac_type 0x%0x rf_id 0x%0x\n",
9569725928SLuca Coelho 				     mac_type, rf_id);
9669725928SLuca Coelho 
970f673c16SJohannes Berg 			if (mac_type == CSR_HW_REV_TYPE(trans->hw_rev) &&
980f673c16SJohannes Berg 			    rf_id == CSR_HW_RFID_TYPE(trans->hw_rf_id))
990f673c16SJohannes Berg 				hw_match = true;
10069725928SLuca Coelho 			break;
10169725928SLuca Coelho 		case IWL_UCODE_TLV_SEC_RT: {
10273c289baSBjoern A. Zeeb 			const struct iwl_pnvm_section *section = (const void *)data;
10369725928SLuca Coelho 			u32 data_len = tlv_len - sizeof(*section);
10469725928SLuca Coelho 
10569725928SLuca Coelho 			IWL_DEBUG_FW(trans,
10669725928SLuca Coelho 				     "Got IWL_UCODE_TLV_SEC_RT len %d\n",
10769725928SLuca Coelho 				     tlv_len);
10869725928SLuca Coelho 
10969725928SLuca Coelho 			/* TODO: remove, this is a deprecated separator */
11073c289baSBjoern A. Zeeb 			if (le32_to_cpup((const __le32 *)data) == 0xddddeeee) {
11169725928SLuca Coelho 				IWL_DEBUG_FW(trans, "Ignoring separator.\n");
11269725928SLuca Coelho 				break;
11369725928SLuca Coelho 			}
11469725928SLuca Coelho 
115b99e32cbSAlon Giladi 			if (pnvm_data->n_chunks == IPC_DRAM_MAP_ENTRY_NUM_MAX) {
1165f408503SAlon Giladi 				IWL_DEBUG_FW(trans,
1175f408503SAlon Giladi 					     "too many payloads to allocate in DRAM.\n");
1185f408503SAlon Giladi 				return -EINVAL;
1195f408503SAlon Giladi 			}
1205f408503SAlon Giladi 
12169725928SLuca Coelho 			IWL_DEBUG_FW(trans, "Adding data (size %d)\n",
12269725928SLuca Coelho 				     data_len);
12369725928SLuca Coelho 
124b99e32cbSAlon Giladi 			pnvm_data->chunks[pnvm_data->n_chunks].data = section->data;
125b99e32cbSAlon Giladi 			pnvm_data->chunks[pnvm_data->n_chunks].len = data_len;
126b99e32cbSAlon Giladi 			pnvm_data->n_chunks++;
12769725928SLuca Coelho 
12869725928SLuca Coelho 			break;
12969725928SLuca Coelho 		}
130372a7148SGregory Greenman 		case IWL_UCODE_TLV_MEM_DESC:
131372a7148SGregory Greenman 			if (iwl_uefi_handle_tlv_mem_desc(trans, data, tlv_len,
132372a7148SGregory Greenman 							 pnvm_data))
133372a7148SGregory Greenman 				return -EINVAL;
134372a7148SGregory Greenman 			break;
13569725928SLuca Coelho 		case IWL_UCODE_TLV_PNVM_SKU:
13669725928SLuca Coelho 			IWL_DEBUG_FW(trans,
13769725928SLuca Coelho 				     "New PNVM section started, stop parsing.\n");
13869725928SLuca Coelho 			goto done;
13969725928SLuca Coelho 		default:
14069725928SLuca Coelho 			IWL_DEBUG_FW(trans, "Found TLV 0x%0x, len %d\n",
14169725928SLuca Coelho 				     tlv_type, tlv_len);
14269725928SLuca Coelho 			break;
14369725928SLuca Coelho 		}
14469725928SLuca Coelho 
14569725928SLuca Coelho 		len -= ALIGN(tlv_len, 4);
14669725928SLuca Coelho 		data += ALIGN(tlv_len, 4);
14769725928SLuca Coelho 	}
14869725928SLuca Coelho 
14969725928SLuca Coelho done:
1500f673c16SJohannes Berg 	if (!hw_match) {
1510f673c16SJohannes Berg 		IWL_DEBUG_FW(trans,
1520f673c16SJohannes Berg 			     "HW mismatch, skipping PNVM section (need mac_type 0x%x rf_id 0x%x)\n",
1530f673c16SJohannes Berg 			     CSR_HW_REV_TYPE(trans->hw_rev),
1540f673c16SJohannes Berg 			     CSR_HW_RFID_TYPE(trans->hw_rf_id));
1555f408503SAlon Giladi 		return -ENOENT;
1560f673c16SJohannes Berg 	}
1570f673c16SJohannes Berg 
158b99e32cbSAlon Giladi 	if (!pnvm_data->n_chunks) {
15969725928SLuca Coelho 		IWL_DEBUG_FW(trans, "Empty PNVM, skipping.\n");
1605f408503SAlon Giladi 		return -ENOENT;
16169725928SLuca Coelho 	}
16269725928SLuca Coelho 
163194d1f84SAlon Giladi 	return 0;
16469725928SLuca Coelho }
16569725928SLuca Coelho 
iwl_pnvm_parse(struct iwl_trans * trans,const u8 * data,size_t len,struct iwl_pnvm_image * pnvm_data)16669725928SLuca Coelho static int iwl_pnvm_parse(struct iwl_trans *trans, const u8 *data,
167b99e32cbSAlon Giladi 			  size_t len,
168b99e32cbSAlon Giladi 			  struct iwl_pnvm_image *pnvm_data)
16969725928SLuca Coelho {
17073c289baSBjoern A. Zeeb 	const struct iwl_ucode_tlv *tlv;
17169725928SLuca Coelho 
17269725928SLuca Coelho 	IWL_DEBUG_FW(trans, "Parsing PNVM file\n");
17369725928SLuca Coelho 
17469725928SLuca Coelho 	while (len >= sizeof(*tlv)) {
17569725928SLuca Coelho 		u32 tlv_len, tlv_type;
17669725928SLuca Coelho 
17769725928SLuca Coelho 		len -= sizeof(*tlv);
17886e8e657SJohannes Berg 		tlv = (const void *)data;
17969725928SLuca Coelho 
18069725928SLuca Coelho 		tlv_len = le32_to_cpu(tlv->length);
18169725928SLuca Coelho 		tlv_type = le32_to_cpu(tlv->type);
18269725928SLuca Coelho 
18369725928SLuca Coelho 		if (len < tlv_len) {
18469725928SLuca Coelho 			IWL_ERR(trans, "invalid TLV len: %zd/%u\n",
18569725928SLuca Coelho 				len, tlv_len);
18669725928SLuca Coelho 			return -EINVAL;
18769725928SLuca Coelho 		}
18869725928SLuca Coelho 
18969725928SLuca Coelho 		if (tlv_type == IWL_UCODE_TLV_PNVM_SKU) {
19073c289baSBjoern A. Zeeb 			const struct iwl_sku_id *sku_id =
19173c289baSBjoern A. Zeeb 				(const void *)(data + sizeof(*tlv));
19269725928SLuca Coelho 
19369725928SLuca Coelho 			IWL_DEBUG_FW(trans,
19469725928SLuca Coelho 				     "Got IWL_UCODE_TLV_PNVM_SKU len %d\n",
19569725928SLuca Coelho 				     tlv_len);
19669725928SLuca Coelho 			IWL_DEBUG_FW(trans, "sku_id 0x%0x 0x%0x 0x%0x\n",
19769725928SLuca Coelho 				     le32_to_cpu(sku_id->data[0]),
19869725928SLuca Coelho 				     le32_to_cpu(sku_id->data[1]),
19969725928SLuca Coelho 				     le32_to_cpu(sku_id->data[2]));
20069725928SLuca Coelho 
201ff11a8eeSLuca Coelho 			data += sizeof(*tlv) + ALIGN(tlv_len, 4);
202ff11a8eeSLuca Coelho 			len -= ALIGN(tlv_len, 4);
203ff11a8eeSLuca Coelho 
20469725928SLuca Coelho 			if (trans->sku_id[0] == le32_to_cpu(sku_id->data[0]) &&
20569725928SLuca Coelho 			    trans->sku_id[1] == le32_to_cpu(sku_id->data[1]) &&
20669725928SLuca Coelho 			    trans->sku_id[2] == le32_to_cpu(sku_id->data[2])) {
20769725928SLuca Coelho 				int ret;
20869725928SLuca Coelho 
209b99e32cbSAlon Giladi 				ret = iwl_pnvm_handle_section(trans, data, len,
210b99e32cbSAlon Giladi 							      pnvm_data);
21169725928SLuca Coelho 				if (!ret)
21269725928SLuca Coelho 					return 0;
21369725928SLuca Coelho 			} else {
21469725928SLuca Coelho 				IWL_DEBUG_FW(trans, "SKU ID didn't match!\n");
21569725928SLuca Coelho 			}
21669725928SLuca Coelho 		} else {
21769725928SLuca Coelho 			data += sizeof(*tlv) + ALIGN(tlv_len, 4);
21869725928SLuca Coelho 			len -= ALIGN(tlv_len, 4);
21969725928SLuca Coelho 		}
22069725928SLuca Coelho 	}
22169725928SLuca Coelho 
22269725928SLuca Coelho 	return -ENOENT;
22369725928SLuca Coelho }
22469725928SLuca Coelho 
iwl_pnvm_get_from_fs(struct iwl_trans * trans,u8 ** data,size_t * len)225cdda18fbSLuca Coelho static int iwl_pnvm_get_from_fs(struct iwl_trans *trans, u8 **data, size_t *len)
226b3e4c0f3SLuca Coelho {
2271c58bed4SJohannes Berg 	const struct firmware *pnvm;
228b05c1d14SDror Moshe 	char pnvm_name[MAX_PNVM_NAME];
22945010c08SChristophe JAILLET 	size_t new_len;
230cdda18fbSLuca Coelho 	int ret;
23169725928SLuca Coelho 
232b05c1d14SDror Moshe 	iwl_pnvm_get_fs_name(trans, pnvm_name, sizeof(pnvm_name));
23369725928SLuca Coelho 
23469725928SLuca Coelho 	ret = firmware_request_nowarn(&pnvm, pnvm_name, trans->dev);
23569725928SLuca Coelho 	if (ret) {
23669725928SLuca Coelho 		IWL_DEBUG_FW(trans, "PNVM file %s not found %d\n",
23769725928SLuca Coelho 			     pnvm_name, ret);
238cdda18fbSLuca Coelho 		return ret;
239cdda18fbSLuca Coelho 	}
240cdda18fbSLuca Coelho 
24145010c08SChristophe JAILLET 	new_len = pnvm->size;
242cdda18fbSLuca Coelho 	*data = kmemdup(pnvm->data, pnvm->size, GFP_KERNEL);
24345010c08SChristophe JAILLET 	release_firmware(pnvm);
24445010c08SChristophe JAILLET 
245cdda18fbSLuca Coelho 	if (!*data)
246cdda18fbSLuca Coelho 		return -ENOMEM;
247cdda18fbSLuca Coelho 
24845010c08SChristophe JAILLET 	*len = new_len;
249cdda18fbSLuca Coelho 
250cdda18fbSLuca Coelho 	return 0;
251cdda18fbSLuca Coelho }
252cdda18fbSLuca Coelho 
iwl_get_pnvm_image(struct iwl_trans * trans_p,size_t * len)253b99e32cbSAlon Giladi static u8 *iwl_get_pnvm_image(struct iwl_trans *trans_p, size_t *len)
254b99e32cbSAlon Giladi {
255b99e32cbSAlon Giladi 	struct pnvm_sku_package *package;
256b99e32cbSAlon Giladi 	u8 *image = NULL;
257b99e32cbSAlon Giladi 
258*1cabe943SMiri Korenblit 	/* Get PNVM from BIOS for non-Intel SKU */
259*1cabe943SMiri Korenblit 	if (trans_p->sku_id[2]) {
260b99e32cbSAlon Giladi 		package = iwl_uefi_get_pnvm(trans_p, len);
261b99e32cbSAlon Giladi 		if (!IS_ERR_OR_NULL(package)) {
262b99e32cbSAlon Giladi 			if (*len >= sizeof(*package)) {
263b99e32cbSAlon Giladi 				/* we need only the data */
264b99e32cbSAlon Giladi 				*len -= sizeof(*package);
265*1cabe943SMiri Korenblit 				image = kmemdup(package->data,
266*1cabe943SMiri Korenblit 						*len, GFP_KERNEL);
267b99e32cbSAlon Giladi 			}
268*1cabe943SMiri Korenblit 			/*
269*1cabe943SMiri Korenblit 			 * free package regardless of whether kmemdup
270*1cabe943SMiri Korenblit 			 * succeeded
271*1cabe943SMiri Korenblit 			 */
272b99e32cbSAlon Giladi 			kfree(package);
273b99e32cbSAlon Giladi 			if (image)
274b99e32cbSAlon Giladi 				return image;
275b99e32cbSAlon Giladi 		}
276*1cabe943SMiri Korenblit 	}
277b99e32cbSAlon Giladi 
278*1cabe943SMiri Korenblit 	/* If it's not available, or for Intel SKU, try from the filesystem */
279b99e32cbSAlon Giladi 	if (iwl_pnvm_get_from_fs(trans_p, &image, len))
280b99e32cbSAlon Giladi 		return NULL;
281b99e32cbSAlon Giladi 	return image;
282b99e32cbSAlon Giladi }
283b99e32cbSAlon Giladi 
iwl_pnvm_load_pnvm_to_trans(struct iwl_trans * trans,const struct iwl_ucode_capabilities * capa)284875d035fSJohannes Berg static void iwl_pnvm_load_pnvm_to_trans(struct iwl_trans *trans,
28533182810SAlon Giladi 					const struct iwl_ucode_capabilities *capa)
286cdda18fbSLuca Coelho {
287875d035fSJohannes Berg 	struct iwl_pnvm_image *pnvm_data = NULL;
288875d035fSJohannes Berg 	u8 *data = NULL;
289b99e32cbSAlon Giladi 	size_t length;
290cdda18fbSLuca Coelho 	int ret;
291cdda18fbSLuca Coelho 
292875d035fSJohannes Berg 	/* failed to get/parse the image in the past, no use trying again */
293b99e32cbSAlon Giladi 	if (trans->fail_to_parse_pnvm_image)
294875d035fSJohannes Berg 		return;
295b99e32cbSAlon Giladi 
296875d035fSJohannes Berg 	if (trans->pnvm_loaded)
297875d035fSJohannes Berg 		goto set;
298875d035fSJohannes Berg 
299b99e32cbSAlon Giladi 	data = iwl_get_pnvm_image(trans, &length);
300b99e32cbSAlon Giladi 	if (!data) {
301b99e32cbSAlon Giladi 		trans->fail_to_parse_pnvm_image = true;
302875d035fSJohannes Berg 		return;
303cdda18fbSLuca Coelho 	}
304875d035fSJohannes Berg 
305875d035fSJohannes Berg 	pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);
306875d035fSJohannes Berg 	if (!pnvm_data)
307875d035fSJohannes Berg 		goto free;
308875d035fSJohannes Berg 
309875d035fSJohannes Berg 	ret = iwl_pnvm_parse(trans, data, length, pnvm_data);
310cdda18fbSLuca Coelho 	if (ret) {
311b99e32cbSAlon Giladi 		trans->fail_to_parse_pnvm_image = true;
312875d035fSJohannes Berg 		goto free;
3131c58bed4SJohannes Berg 	}
314b3e4c0f3SLuca Coelho 
315875d035fSJohannes Berg 	ret = iwl_trans_load_pnvm(trans, pnvm_data, capa);
316b99e32cbSAlon Giladi 	if (ret)
317875d035fSJohannes Berg 		goto free;
318875d035fSJohannes Berg 	IWL_INFO(trans, "loaded PNVM version %08x\n", pnvm_data->version);
319875d035fSJohannes Berg 
320875d035fSJohannes Berg set:
321875d035fSJohannes Berg 	iwl_trans_set_pnvm(trans, capa);
322875d035fSJohannes Berg free:
323875d035fSJohannes Berg 	kfree(data);
324875d035fSJohannes Berg 	kfree(pnvm_data);
325b99e32cbSAlon Giladi }
326cdda18fbSLuca Coelho 
327875d035fSJohannes Berg static void
iwl_pnvm_load_reduce_power_to_trans(struct iwl_trans * trans,const struct iwl_ucode_capabilities * capa)328875d035fSJohannes Berg iwl_pnvm_load_reduce_power_to_trans(struct iwl_trans *trans,
329875d035fSJohannes Berg 				    const struct iwl_ucode_capabilities *capa)
330875d035fSJohannes Berg {
331875d035fSJohannes Berg 	struct iwl_pnvm_image *pnvm_data = NULL;
332875d035fSJohannes Berg 	u8 *data = NULL;
333875d035fSJohannes Berg 	size_t length;
334875d035fSJohannes Berg 	int ret;
335b99e32cbSAlon Giladi 
336380bf72dSAlon Giladi 	if (trans->failed_to_load_reduce_power_image)
337875d035fSJohannes Berg 		return;
338380bf72dSAlon Giladi 
339875d035fSJohannes Berg 	if (trans->reduce_power_loaded)
340875d035fSJohannes Berg 		goto set;
341875d035fSJohannes Berg 
342380bf72dSAlon Giladi 	data = iwl_uefi_get_reduced_power(trans, &length);
343380bf72dSAlon Giladi 	if (IS_ERR(data)) {
344380bf72dSAlon Giladi 		trans->failed_to_load_reduce_power_image = true;
345875d035fSJohannes Berg 		return;
346380bf72dSAlon Giladi 	}
347380bf72dSAlon Giladi 
348875d035fSJohannes Berg 	pnvm_data = kzalloc(sizeof(*pnvm_data), GFP_KERNEL);
349875d035fSJohannes Berg 	if (!pnvm_data)
350875d035fSJohannes Berg 		goto free;
351875d035fSJohannes Berg 
352875d035fSJohannes Berg 	ret = iwl_uefi_reduce_power_parse(trans, data, length, pnvm_data);
353ea3571f4SAlon Giladi 	if (ret) {
354380bf72dSAlon Giladi 		trans->failed_to_load_reduce_power_image = true;
355875d035fSJohannes Berg 		goto free;
356380bf72dSAlon Giladi 	}
357380bf72dSAlon Giladi 
358875d035fSJohannes Berg 	ret = iwl_trans_load_reduce_power(trans, pnvm_data, capa);
359c738fb61SAlon Giladi 	if (ret) {
3609dad325fSLuca Coelho 		IWL_DEBUG_FW(trans,
361c738fb61SAlon Giladi 			     "Failed to load reduce power table %d\n",
3629dad325fSLuca Coelho 			     ret);
363380bf72dSAlon Giladi 		trans->failed_to_load_reduce_power_image = true;
364875d035fSJohannes Berg 		goto free;
3658f55564cSJohannes Berg 	}
3669dad325fSLuca Coelho 
367875d035fSJohannes Berg set:
368875d035fSJohannes Berg 	iwl_trans_set_reduce_power(trans, capa);
369875d035fSJohannes Berg free:
370875d035fSJohannes Berg 	kfree(data);
371875d035fSJohannes Berg 	kfree(pnvm_data);
372875d035fSJohannes Berg }
373875d035fSJohannes Berg 
iwl_pnvm_load(struct iwl_trans * trans,struct iwl_notif_wait_data * notif_wait,const struct iwl_ucode_capabilities * capa)374875d035fSJohannes Berg int iwl_pnvm_load(struct iwl_trans *trans,
375875d035fSJohannes Berg 		  struct iwl_notif_wait_data *notif_wait,
376875d035fSJohannes Berg 		  const struct iwl_ucode_capabilities *capa)
377875d035fSJohannes Berg {
378875d035fSJohannes Berg 	struct iwl_notification_wait pnvm_wait;
379875d035fSJohannes Berg 	static const u16 ntf_cmds[] = { WIDE_ID(REGULATORY_AND_NVM_GROUP,
380875d035fSJohannes Berg 						PNVM_INIT_COMPLETE_NTFY) };
381875d035fSJohannes Berg 
382875d035fSJohannes Berg 	/* if the SKU_ID is empty, there's nothing to do */
383875d035fSJohannes Berg 	if (!trans->sku_id[0] && !trans->sku_id[1] && !trans->sku_id[2])
384875d035fSJohannes Berg 		return 0;
385875d035fSJohannes Berg 
386875d035fSJohannes Berg 	iwl_pnvm_load_pnvm_to_trans(trans, capa);
387875d035fSJohannes Berg 	iwl_pnvm_load_reduce_power_to_trans(trans, capa);
388875d035fSJohannes Berg 
389b3e4c0f3SLuca Coelho 	iwl_init_notification_wait(notif_wait, &pnvm_wait,
390b3e4c0f3SLuca Coelho 				   ntf_cmds, ARRAY_SIZE(ntf_cmds),
391b3e4c0f3SLuca Coelho 				   iwl_pnvm_complete_fn, trans);
392b3e4c0f3SLuca Coelho 
393b3e4c0f3SLuca Coelho 	/* kick the doorbell */
394b3e4c0f3SLuca Coelho 	iwl_write_umac_prph(trans, UREG_DOORBELL_TO_ISR6,
395b3e4c0f3SLuca Coelho 			    UREG_DOORBELL_TO_ISR6_PNVM);
396b3e4c0f3SLuca Coelho 
397b3e4c0f3SLuca Coelho 	return iwl_wait_notification(notif_wait, &pnvm_wait,
398b3e4c0f3SLuca Coelho 				     MVM_UCODE_PNVM_TIMEOUT);
399b3e4c0f3SLuca Coelho }
400b3e4c0f3SLuca Coelho IWL_EXPORT_SYMBOL(iwl_pnvm_load);
401