18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Atheros CARL9170 driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * firmware parser
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/firmware.h>
128c2ecf20Sopenharmony_ci#include <linux/crc32.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include "carl9170.h"
158c2ecf20Sopenharmony_ci#include "fwcmd.h"
168c2ecf20Sopenharmony_ci#include "version.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic const u8 otus_magic[4] = { OTUS_MAGIC };
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic const void *carl9170_fw_find_desc(struct ar9170 *ar, const u8 descid[4],
218c2ecf20Sopenharmony_ci	const unsigned int len, const u8 compatible_revision)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	const struct carl9170fw_desc_head *iter;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	carl9170fw_for_each_hdr(iter, ar->fw.desc) {
268c2ecf20Sopenharmony_ci		if (carl9170fw_desc_cmp(iter, descid, len,
278c2ecf20Sopenharmony_ci					compatible_revision))
288c2ecf20Sopenharmony_ci			return (void *)iter;
298c2ecf20Sopenharmony_ci	}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	/* needed to find the LAST desc */
328c2ecf20Sopenharmony_ci	if (carl9170fw_desc_cmp(iter, descid, len,
338c2ecf20Sopenharmony_ci				compatible_revision))
348c2ecf20Sopenharmony_ci		return (void *)iter;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	return NULL;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int carl9170_fw_verify_descs(struct ar9170 *ar,
408c2ecf20Sopenharmony_ci	const struct carl9170fw_desc_head *head, unsigned int max_len)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	const struct carl9170fw_desc_head *pos;
438c2ecf20Sopenharmony_ci	unsigned long pos_addr, end_addr;
448c2ecf20Sopenharmony_ci	unsigned int pos_length;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (max_len < sizeof(*pos))
478c2ecf20Sopenharmony_ci		return -ENODATA;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	max_len = min_t(unsigned int, CARL9170FW_DESC_MAX_LENGTH, max_len);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	pos = head;
528c2ecf20Sopenharmony_ci	pos_addr = (unsigned long) pos;
538c2ecf20Sopenharmony_ci	end_addr = pos_addr + max_len;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	while (pos_addr < end_addr) {
568c2ecf20Sopenharmony_ci		if (pos_addr + sizeof(*head) > end_addr)
578c2ecf20Sopenharmony_ci			return -E2BIG;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci		pos_length = le16_to_cpu(pos->length);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci		if (pos_length < sizeof(*head))
628c2ecf20Sopenharmony_ci			return -EBADMSG;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		if (pos_length > max_len)
658c2ecf20Sopenharmony_ci			return -EOVERFLOW;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci		if (pos_addr + pos_length > end_addr)
688c2ecf20Sopenharmony_ci			return -EMSGSIZE;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci		if (carl9170fw_desc_cmp(pos, LAST_MAGIC,
718c2ecf20Sopenharmony_ci					CARL9170FW_LAST_DESC_SIZE,
728c2ecf20Sopenharmony_ci					CARL9170FW_LAST_DESC_CUR_VER))
738c2ecf20Sopenharmony_ci			return 0;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		pos_addr += pos_length;
768c2ecf20Sopenharmony_ci		pos = (void *)pos_addr;
778c2ecf20Sopenharmony_ci		max_len -= pos_length;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	return -EINVAL;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void carl9170_fw_info(struct ar9170 *ar)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	const struct carl9170fw_motd_desc *motd_desc;
858c2ecf20Sopenharmony_ci	unsigned int str_ver_len;
868c2ecf20Sopenharmony_ci	u32 fw_date;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	dev_info(&ar->udev->dev, "driver   API: %s 2%03d-%02d-%02d [%d-%d]\n",
898c2ecf20Sopenharmony_ci		CARL9170FW_VERSION_GIT, CARL9170FW_VERSION_YEAR,
908c2ecf20Sopenharmony_ci		CARL9170FW_VERSION_MONTH, CARL9170FW_VERSION_DAY,
918c2ecf20Sopenharmony_ci		CARL9170FW_API_MIN_VER, CARL9170FW_API_MAX_VER);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	motd_desc = carl9170_fw_find_desc(ar, MOTD_MAGIC,
948c2ecf20Sopenharmony_ci		sizeof(*motd_desc), CARL9170FW_MOTD_DESC_CUR_VER);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (motd_desc) {
978c2ecf20Sopenharmony_ci		str_ver_len = strnlen(motd_desc->release,
988c2ecf20Sopenharmony_ci			CARL9170FW_MOTD_RELEASE_LEN);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		fw_date = le32_to_cpu(motd_desc->fw_year_month_day);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		dev_info(&ar->udev->dev, "firmware API: %.*s 2%03d-%02d-%02d\n",
1038c2ecf20Sopenharmony_ci			 str_ver_len, motd_desc->release,
1048c2ecf20Sopenharmony_ci			 CARL9170FW_GET_YEAR(fw_date),
1058c2ecf20Sopenharmony_ci			 CARL9170FW_GET_MONTH(fw_date),
1068c2ecf20Sopenharmony_ci			 CARL9170FW_GET_DAY(fw_date));
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		strlcpy(ar->hw->wiphy->fw_version, motd_desc->release,
1098c2ecf20Sopenharmony_ci			sizeof(ar->hw->wiphy->fw_version));
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic bool valid_dma_addr(const u32 address)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	if (address >= AR9170_SRAM_OFFSET &&
1168c2ecf20Sopenharmony_ci	    address < (AR9170_SRAM_OFFSET + AR9170_SRAM_SIZE))
1178c2ecf20Sopenharmony_ci		return true;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return false;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic bool valid_cpu_addr(const u32 address)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	if (valid_dma_addr(address) || (address >= AR9170_PRAM_OFFSET &&
1258c2ecf20Sopenharmony_ci	    address < (AR9170_PRAM_OFFSET + AR9170_PRAM_SIZE)))
1268c2ecf20Sopenharmony_ci		return true;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return false;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic int carl9170_fw_checksum(struct ar9170 *ar, const __u8 *data,
1328c2ecf20Sopenharmony_ci				size_t len)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	const struct carl9170fw_otus_desc *otus_desc;
1358c2ecf20Sopenharmony_ci	const struct carl9170fw_last_desc *last_desc;
1368c2ecf20Sopenharmony_ci	const struct carl9170fw_chk_desc *chk_desc;
1378c2ecf20Sopenharmony_ci	unsigned long fin, diff;
1388c2ecf20Sopenharmony_ci	unsigned int dsc_len;
1398c2ecf20Sopenharmony_ci	u32 crc32;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	last_desc = carl9170_fw_find_desc(ar, LAST_MAGIC,
1428c2ecf20Sopenharmony_ci		sizeof(*last_desc), CARL9170FW_LAST_DESC_CUR_VER);
1438c2ecf20Sopenharmony_ci	if (!last_desc)
1448c2ecf20Sopenharmony_ci		return -EINVAL;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	otus_desc = carl9170_fw_find_desc(ar, OTUS_MAGIC,
1478c2ecf20Sopenharmony_ci		sizeof(*otus_desc), CARL9170FW_OTUS_DESC_CUR_VER);
1488c2ecf20Sopenharmony_ci	if (!otus_desc) {
1498c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "failed to find compatible firmware "
1508c2ecf20Sopenharmony_ci			"descriptor.\n");
1518c2ecf20Sopenharmony_ci		return -ENODATA;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	chk_desc = carl9170_fw_find_desc(ar, CHK_MAGIC,
1558c2ecf20Sopenharmony_ci		sizeof(*chk_desc), CARL9170FW_CHK_DESC_CUR_VER);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	if (!chk_desc) {
1588c2ecf20Sopenharmony_ci		dev_warn(&ar->udev->dev, "Unprotected firmware image.\n");
1598c2ecf20Sopenharmony_ci		return 0;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	dsc_len = min_t(unsigned int, len,
1638c2ecf20Sopenharmony_ci			(unsigned long)chk_desc - (unsigned long)otus_desc);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	fin = (unsigned long) last_desc + sizeof(*last_desc);
1668c2ecf20Sopenharmony_ci	diff = fin - (unsigned long) otus_desc;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	if (diff < len)
1698c2ecf20Sopenharmony_ci		len -= diff;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (len < 256)
1728c2ecf20Sopenharmony_ci		return -EIO;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	crc32 = crc32_le(~0, data, len);
1758c2ecf20Sopenharmony_ci	if (cpu_to_le32(crc32) != chk_desc->fw_crc32) {
1768c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "fw checksum test failed.\n");
1778c2ecf20Sopenharmony_ci		return -ENOEXEC;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	crc32 = crc32_le(crc32, (void *)otus_desc, dsc_len);
1818c2ecf20Sopenharmony_ci	if (cpu_to_le32(crc32) != chk_desc->hdr_crc32) {
1828c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "descriptor check failed.\n");
1838c2ecf20Sopenharmony_ci		return -EINVAL;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci	return 0;
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic int carl9170_fw_tx_sequence(struct ar9170 *ar)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	const struct carl9170fw_txsq_desc *txsq_desc;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	txsq_desc = carl9170_fw_find_desc(ar, TXSQ_MAGIC, sizeof(*txsq_desc),
1938c2ecf20Sopenharmony_ci					  CARL9170FW_TXSQ_DESC_CUR_VER);
1948c2ecf20Sopenharmony_ci	if (txsq_desc) {
1958c2ecf20Sopenharmony_ci		ar->fw.tx_seq_table = le32_to_cpu(txsq_desc->seq_table_addr);
1968c2ecf20Sopenharmony_ci		if (!valid_cpu_addr(ar->fw.tx_seq_table))
1978c2ecf20Sopenharmony_ci			return -EINVAL;
1988c2ecf20Sopenharmony_ci	} else {
1998c2ecf20Sopenharmony_ci		ar->fw.tx_seq_table = 0;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	return 0;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic void carl9170_fw_set_if_combinations(struct ar9170 *ar,
2068c2ecf20Sopenharmony_ci					    u16 if_comb_types)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	if (ar->fw.vif_num < 2)
2098c2ecf20Sopenharmony_ci		return;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	ar->if_comb_limits[0].max = ar->fw.vif_num;
2128c2ecf20Sopenharmony_ci	ar->if_comb_limits[0].types = if_comb_types;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	ar->if_combs[0].num_different_channels = 1;
2158c2ecf20Sopenharmony_ci	ar->if_combs[0].max_interfaces = ar->fw.vif_num;
2168c2ecf20Sopenharmony_ci	ar->if_combs[0].limits = ar->if_comb_limits;
2178c2ecf20Sopenharmony_ci	ar->if_combs[0].n_limits = ARRAY_SIZE(ar->if_comb_limits);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	ar->hw->wiphy->iface_combinations = ar->if_combs;
2208c2ecf20Sopenharmony_ci	ar->hw->wiphy->n_iface_combinations = ARRAY_SIZE(ar->if_combs);
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic int carl9170_fw(struct ar9170 *ar, const __u8 *data, size_t len)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	const struct carl9170fw_otus_desc *otus_desc;
2268c2ecf20Sopenharmony_ci	int err;
2278c2ecf20Sopenharmony_ci	u16 if_comb_types;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	err = carl9170_fw_checksum(ar, data, len);
2308c2ecf20Sopenharmony_ci	if (err)
2318c2ecf20Sopenharmony_ci		return err;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	otus_desc = carl9170_fw_find_desc(ar, OTUS_MAGIC,
2348c2ecf20Sopenharmony_ci		sizeof(*otus_desc), CARL9170FW_OTUS_DESC_CUR_VER);
2358c2ecf20Sopenharmony_ci	if (!otus_desc) {
2368c2ecf20Sopenharmony_ci		return -ENODATA;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci#define SUPP(feat)						\
2408c2ecf20Sopenharmony_ci	(carl9170fw_supports(otus_desc->feature_set, feat))
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	if (!SUPP(CARL9170FW_DUMMY_FEATURE)) {
2438c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "invalid firmware descriptor "
2448c2ecf20Sopenharmony_ci			"format detected.\n");
2458c2ecf20Sopenharmony_ci		return -EINVAL;
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	ar->fw.api_version = otus_desc->api_ver;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (ar->fw.api_version < CARL9170FW_API_MIN_VER ||
2518c2ecf20Sopenharmony_ci	    ar->fw.api_version > CARL9170FW_API_MAX_VER) {
2528c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "unsupported firmware api version.\n");
2538c2ecf20Sopenharmony_ci		return -EINVAL;
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (!SUPP(CARL9170FW_COMMAND_PHY) || SUPP(CARL9170FW_UNUSABLE) ||
2578c2ecf20Sopenharmony_ci	    !SUPP(CARL9170FW_HANDLE_BACK_REQ)) {
2588c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "firmware does support "
2598c2ecf20Sopenharmony_ci			"mandatory features.\n");
2608c2ecf20Sopenharmony_ci		return -ECANCELED;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (ilog2(le32_to_cpu(otus_desc->feature_set)) >=
2648c2ecf20Sopenharmony_ci		__CARL9170FW_FEATURE_NUM) {
2658c2ecf20Sopenharmony_ci		dev_warn(&ar->udev->dev, "driver does not support all "
2668c2ecf20Sopenharmony_ci			 "firmware features.\n");
2678c2ecf20Sopenharmony_ci	}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if (!SUPP(CARL9170FW_COMMAND_CAM)) {
2708c2ecf20Sopenharmony_ci		dev_info(&ar->udev->dev, "crypto offloading is disabled "
2718c2ecf20Sopenharmony_ci			 "by firmware.\n");
2728c2ecf20Sopenharmony_ci		ar->fw.disable_offload_fw = true;
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_PSM) && SUPP(CARL9170FW_FIXED_5GHZ_PSM))
2768c2ecf20Sopenharmony_ci		ieee80211_hw_set(ar->hw, SUPPORTS_PS);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (!SUPP(CARL9170FW_USB_INIT_FIRMWARE)) {
2798c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "firmware does not provide "
2808c2ecf20Sopenharmony_ci			"mandatory interfaces.\n");
2818c2ecf20Sopenharmony_ci		return -EINVAL;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_MINIBOOT))
2858c2ecf20Sopenharmony_ci		ar->fw.offset = le16_to_cpu(otus_desc->miniboot_size);
2868c2ecf20Sopenharmony_ci	else
2878c2ecf20Sopenharmony_ci		ar->fw.offset = 0;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_USB_DOWN_STREAM)) {
2908c2ecf20Sopenharmony_ci		ar->hw->extra_tx_headroom += sizeof(struct ar9170_stream);
2918c2ecf20Sopenharmony_ci		ar->fw.tx_stream = true;
2928c2ecf20Sopenharmony_ci	}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_USB_UP_STREAM))
2958c2ecf20Sopenharmony_ci		ar->fw.rx_stream = true;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_RX_FILTER)) {
2988c2ecf20Sopenharmony_ci		ar->fw.rx_filter = true;
2998c2ecf20Sopenharmony_ci		ar->rx_filter_caps = FIF_FCSFAIL | FIF_PLCPFAIL |
3008c2ecf20Sopenharmony_ci			FIF_CONTROL | FIF_PSPOLL | FIF_OTHER_BSS;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_HW_COUNTERS))
3048c2ecf20Sopenharmony_ci		ar->fw.hw_counters = true;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_WOL))
3078c2ecf20Sopenharmony_ci		device_set_wakeup_enable(&ar->udev->dev, true);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	if (SUPP(CARL9170FW_RX_BA_FILTER))
3108c2ecf20Sopenharmony_ci		ar->fw.ba_filter = true;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if_comb_types = BIT(NL80211_IFTYPE_STATION) |
3138c2ecf20Sopenharmony_ci			BIT(NL80211_IFTYPE_P2P_CLIENT);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	ar->fw.vif_num = otus_desc->vif_num;
3168c2ecf20Sopenharmony_ci	ar->fw.cmd_bufs = otus_desc->cmd_bufs;
3178c2ecf20Sopenharmony_ci	ar->fw.address = le32_to_cpu(otus_desc->fw_address);
3188c2ecf20Sopenharmony_ci	ar->fw.rx_size = le16_to_cpu(otus_desc->rx_max_frame_len);
3198c2ecf20Sopenharmony_ci	ar->fw.mem_blocks = min_t(unsigned int, otus_desc->tx_descs, 0xfe);
3208c2ecf20Sopenharmony_ci	atomic_set(&ar->mem_free_blocks, ar->fw.mem_blocks);
3218c2ecf20Sopenharmony_ci	ar->fw.mem_block_size = le16_to_cpu(otus_desc->tx_frag_len);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (ar->fw.vif_num >= AR9170_MAX_VIRTUAL_MAC || !ar->fw.vif_num ||
3248c2ecf20Sopenharmony_ci	    ar->fw.mem_blocks < 16 || !ar->fw.cmd_bufs ||
3258c2ecf20Sopenharmony_ci	    ar->fw.mem_block_size < 64 || ar->fw.mem_block_size > 512 ||
3268c2ecf20Sopenharmony_ci	    ar->fw.rx_size > 32768 || ar->fw.rx_size < 4096 ||
3278c2ecf20Sopenharmony_ci	    !valid_cpu_addr(ar->fw.address)) {
3288c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "firmware shows obvious signs of "
3298c2ecf20Sopenharmony_ci			"malicious tampering.\n");
3308c2ecf20Sopenharmony_ci		return -EINVAL;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	ar->fw.beacon_addr = le32_to_cpu(otus_desc->bcn_addr);
3348c2ecf20Sopenharmony_ci	ar->fw.beacon_max_len = le16_to_cpu(otus_desc->bcn_len);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (valid_dma_addr(ar->fw.beacon_addr) && ar->fw.beacon_max_len >=
3378c2ecf20Sopenharmony_ci	    AR9170_MAC_BCN_LENGTH_MAX) {
3388c2ecf20Sopenharmony_ci		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci		if (SUPP(CARL9170FW_WLANTX_CAB)) {
3418c2ecf20Sopenharmony_ci			if_comb_types |= BIT(NL80211_IFTYPE_AP);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_MESH
3448c2ecf20Sopenharmony_ci			if_comb_types |=
3458c2ecf20Sopenharmony_ci				BIT(NL80211_IFTYPE_MESH_POINT);
3468c2ecf20Sopenharmony_ci#endif /* CONFIG_MAC80211_MESH */
3478c2ecf20Sopenharmony_ci		}
3488c2ecf20Sopenharmony_ci	}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	carl9170_fw_set_if_combinations(ar, if_comb_types);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	ar->hw->wiphy->interface_modes |= if_comb_types;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	ar->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/* As IBSS Encryption is software-based, IBSS RSN is supported. */
3578c2ecf20Sopenharmony_ci	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3588c2ecf20Sopenharmony_ci		 WIPHY_FLAG_IBSS_RSN | WIPHY_FLAG_SUPPORTS_TDLS;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci#undef SUPPORTED
3618c2ecf20Sopenharmony_ci	return carl9170_fw_tx_sequence(ar);
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic struct carl9170fw_desc_head *
3658c2ecf20Sopenharmony_cicarl9170_find_fw_desc(struct ar9170 *ar, const __u8 *fw_data, const size_t len)
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	int scan = 0, found = 0;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (!carl9170fw_size_check(len)) {
3718c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "firmware size is out of bound.\n");
3728c2ecf20Sopenharmony_ci		return NULL;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	while (scan < len - sizeof(struct carl9170fw_desc_head)) {
3768c2ecf20Sopenharmony_ci		if (fw_data[scan++] == otus_magic[found])
3778c2ecf20Sopenharmony_ci			found++;
3788c2ecf20Sopenharmony_ci		else
3798c2ecf20Sopenharmony_ci			found = 0;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci		if (scan >= len)
3828c2ecf20Sopenharmony_ci			break;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci		if (found == sizeof(otus_magic))
3858c2ecf20Sopenharmony_ci			break;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	if (found != sizeof(otus_magic))
3898c2ecf20Sopenharmony_ci		return NULL;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	return (void *)&fw_data[scan - found];
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ciint carl9170_parse_firmware(struct ar9170 *ar)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	const struct carl9170fw_desc_head *fw_desc = NULL;
3978c2ecf20Sopenharmony_ci	const struct firmware *fw = ar->fw.fw;
3988c2ecf20Sopenharmony_ci	unsigned long header_offset = 0;
3998c2ecf20Sopenharmony_ci	int err;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	if (WARN_ON(!fw))
4028c2ecf20Sopenharmony_ci		return -EINVAL;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	fw_desc = carl9170_find_fw_desc(ar, fw->data, fw->size);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	if (!fw_desc) {
4078c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "unsupported firmware.\n");
4088c2ecf20Sopenharmony_ci		return -ENODATA;
4098c2ecf20Sopenharmony_ci	}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	header_offset = (unsigned long)fw_desc - (unsigned long)fw->data;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	err = carl9170_fw_verify_descs(ar, fw_desc, fw->size - header_offset);
4148c2ecf20Sopenharmony_ci	if (err) {
4158c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "damaged firmware (%d).\n", err);
4168c2ecf20Sopenharmony_ci		return err;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	ar->fw.desc = fw_desc;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	carl9170_fw_info(ar);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	err = carl9170_fw(ar, fw->data, fw->size);
4248c2ecf20Sopenharmony_ci	if (err) {
4258c2ecf20Sopenharmony_ci		dev_err(&ar->udev->dev, "failed to parse firmware (%d).\n",
4268c2ecf20Sopenharmony_ci			err);
4278c2ecf20Sopenharmony_ci		return err;
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	return 0;
4318c2ecf20Sopenharmony_ci}
432