18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
38e99ea8dSJohannes Berg  * Copyright (C) 2015 Intel Mobile Communications GmbH
48e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2019-2020 Intel Corporation
68e99ea8dSJohannes Berg  */
7e705c121SKalle Valo #include <linux/kernel.h>
839bdb17eSSharon Dvir #include <linux/bsearch.h>
939bdb17eSSharon Dvir 
10fda1bd0dSMordechay Goodstein #include "fw/api/tx.h"
11e705c121SKalle Valo #include "iwl-trans.h"
1239bdb17eSSharon Dvir #include "iwl-drv.h"
133cd1980bSSara Sharon #include "iwl-fh.h"
140cd1ad2dSMordechay Goodstein #include "queue/tx.h"
15a26014e2SMordechay Goodstein #include <linux/dmapool.h>
16e705c121SKalle Valo 
17e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
18e705c121SKalle Valo 				  struct device *dev,
19a89c72ffSJohannes Berg 				  const struct iwl_trans_ops *ops,
20fda1bd0dSMordechay Goodstein 				  const struct iwl_cfg_trans_params *cfg_trans)
21e705c121SKalle Valo {
22e705c121SKalle Valo 	struct iwl_trans *trans;
23fda1bd0dSMordechay Goodstein 	int txcmd_size, txcmd_align;
24e705c121SKalle Valo #ifdef CONFIG_LOCKDEP
25e705c121SKalle Valo 	static struct lock_class_key __key;
26e705c121SKalle Valo #endif
27e705c121SKalle Valo 
285a41a86cSSharon Dvir 	trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL);
29e705c121SKalle Valo 	if (!trans)
30e705c121SKalle Valo 		return NULL;
31e705c121SKalle Valo 
32fda1bd0dSMordechay Goodstein 	trans->trans_cfg = cfg_trans;
33fda1bd0dSMordechay Goodstein 	if (!cfg_trans->gen2) {
34fda1bd0dSMordechay Goodstein 		txcmd_size = sizeof(struct iwl_tx_cmd);
35fda1bd0dSMordechay Goodstein 		txcmd_align = sizeof(void *);
36fda1bd0dSMordechay Goodstein 	} else if (cfg_trans->device_family < IWL_DEVICE_FAMILY_AX210) {
37fda1bd0dSMordechay Goodstein 		txcmd_size = sizeof(struct iwl_tx_cmd_gen2);
38fda1bd0dSMordechay Goodstein 		txcmd_align = 64;
39fda1bd0dSMordechay Goodstein 	} else {
40fda1bd0dSMordechay Goodstein 		txcmd_size = sizeof(struct iwl_tx_cmd_gen3);
41fda1bd0dSMordechay Goodstein 		txcmd_align = 128;
42fda1bd0dSMordechay Goodstein 	}
43fda1bd0dSMordechay Goodstein 
44fda1bd0dSMordechay Goodstein 	txcmd_size += sizeof(struct iwl_cmd_header);
45fda1bd0dSMordechay Goodstein 	txcmd_size += 36; /* biggest possible 802.11 header */
46fda1bd0dSMordechay Goodstein 
47fda1bd0dSMordechay Goodstein 	/* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
48fda1bd0dSMordechay Goodstein 	if (WARN_ON(cfg_trans->gen2 && txcmd_size >= txcmd_align))
49fda1bd0dSMordechay Goodstein 		return ERR_PTR(-EINVAL);
50fda1bd0dSMordechay Goodstein 
51e705c121SKalle Valo #ifdef CONFIG_LOCKDEP
52e705c121SKalle Valo 	lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
53e705c121SKalle Valo 			 &__key, 0);
54e705c121SKalle Valo #endif
55e705c121SKalle Valo 
56e705c121SKalle Valo 	trans->dev = dev;
57e705c121SKalle Valo 	trans->ops = ops;
58e705c121SKalle Valo 	trans->num_rx_queues = 1;
59e705c121SKalle Valo 
60a26014e2SMordechay Goodstein 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
61a26014e2SMordechay Goodstein 		trans->txqs.bc_tbl_size = sizeof(struct iwl_gen3_bc_tbl);
62a26014e2SMordechay Goodstein 	else
63a26014e2SMordechay Goodstein 		trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl);
64a26014e2SMordechay Goodstein 	/*
65a26014e2SMordechay Goodstein 	 * For gen2 devices, we use a single allocation for each byte-count
66a26014e2SMordechay Goodstein 	 * table, but they're pretty small (1k) so use a DMA pool that we
67a26014e2SMordechay Goodstein 	 * allocate here.
68a26014e2SMordechay Goodstein 	 */
69a26014e2SMordechay Goodstein 	if (trans->trans_cfg->gen2) {
70a26014e2SMordechay Goodstein 		trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", dev,
71a26014e2SMordechay Goodstein 						       trans->txqs.bc_tbl_size,
72a26014e2SMordechay Goodstein 						       256, 0);
73a26014e2SMordechay Goodstein 		if (!trans->txqs.bc_pool)
74a26014e2SMordechay Goodstein 			return NULL;
75a26014e2SMordechay Goodstein 	}
76885375d0SMordechay Goodstein 
77885375d0SMordechay Goodstein 	if (trans->trans_cfg->use_tfh) {
78885375d0SMordechay Goodstein 		trans->txqs.tfd.addr_size = 64;
79885375d0SMordechay Goodstein 		trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
80885375d0SMordechay Goodstein 		trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
81885375d0SMordechay Goodstein 	} else {
82885375d0SMordechay Goodstein 		trans->txqs.tfd.addr_size = 36;
83885375d0SMordechay Goodstein 		trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
84885375d0SMordechay Goodstein 		trans->txqs.tfd.size = sizeof(struct iwl_tfd);
85885375d0SMordechay Goodstein 	}
86885375d0SMordechay Goodstein 	trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans);
87885375d0SMordechay Goodstein 
88e705c121SKalle Valo 	snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
89e705c121SKalle Valo 		 "iwl_cmd_pool:%s", dev_name(trans->dev));
90e705c121SKalle Valo 	trans->dev_cmd_pool =
91e705c121SKalle Valo 		kmem_cache_create(trans->dev_cmd_pool_name,
92fda1bd0dSMordechay Goodstein 				  txcmd_size, txcmd_align,
93a89c72ffSJohannes Berg 				  SLAB_HWCACHE_ALIGN, NULL);
94e705c121SKalle Valo 	if (!trans->dev_cmd_pool)
955a41a86cSSharon Dvir 		return NULL;
96e705c121SKalle Valo 
97d6d517b7SSara Sharon 	WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
98d6d517b7SSara Sharon 
990cd1ad2dSMordechay Goodstein 	trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
1000cd1ad2dSMordechay Goodstein 	if (!trans->txqs.tso_hdr_page) {
1010cd1ad2dSMordechay Goodstein 		kmem_cache_destroy(trans->dev_cmd_pool);
1020cd1ad2dSMordechay Goodstein 		return NULL;
1030cd1ad2dSMordechay Goodstein 	}
1040cd1ad2dSMordechay Goodstein 
105e705c121SKalle Valo 	return trans;
106e705c121SKalle Valo }
107e705c121SKalle Valo 
108e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans)
109e705c121SKalle Valo {
1100cd1ad2dSMordechay Goodstein 	int i;
1110cd1ad2dSMordechay Goodstein 
1120cd1ad2dSMordechay Goodstein 	for_each_possible_cpu(i) {
1130cd1ad2dSMordechay Goodstein 		struct iwl_tso_hdr_page *p =
1140cd1ad2dSMordechay Goodstein 			per_cpu_ptr(trans->txqs.tso_hdr_page, i);
1150cd1ad2dSMordechay Goodstein 
1160cd1ad2dSMordechay Goodstein 		if (p->page)
1170cd1ad2dSMordechay Goodstein 			__free_page(p->page);
1180cd1ad2dSMordechay Goodstein 	}
1190cd1ad2dSMordechay Goodstein 
1200cd1ad2dSMordechay Goodstein 	free_percpu(trans->txqs.tso_hdr_page);
1210cd1ad2dSMordechay Goodstein 
122e705c121SKalle Valo 	kmem_cache_destroy(trans->dev_cmd_pool);
123e705c121SKalle Valo }
12492fe8343SEmmanuel Grumbach 
12592fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
12692fe8343SEmmanuel Grumbach {
12792fe8343SEmmanuel Grumbach 	int ret;
12892fe8343SEmmanuel Grumbach 
12992fe8343SEmmanuel Grumbach 	if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
130326477e4SJohannes Berg 		     test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
13192fe8343SEmmanuel Grumbach 		return -ERFKILL;
13292fe8343SEmmanuel Grumbach 
133*708a39aaSHaim Dreyfuss 	/*
134*708a39aaSHaim Dreyfuss 	 * We can't test IWL_MVM_STATUS_IN_D3 in mvm->status because this
135*708a39aaSHaim Dreyfuss 	 * bit is set early in the D3 flow, before we send all the commands
136*708a39aaSHaim Dreyfuss 	 * that configure the firmware for D3 operation (power, patterns, ...)
137*708a39aaSHaim Dreyfuss 	 * and we don't want to flag all those with CMD_SEND_IN_D3.
138*708a39aaSHaim Dreyfuss 	 * So use the system_pm_mode instead. The only command sent after
139*708a39aaSHaim Dreyfuss 	 * we set system_pm_mode is D3_CONFIG_CMD, which we now flag with
140*708a39aaSHaim Dreyfuss 	 * CMD_SEND_IN_D3.
141*708a39aaSHaim Dreyfuss 	 */
142*708a39aaSHaim Dreyfuss 	if (unlikely(trans->system_pm_mode == IWL_PLAT_PM_MODE_D3 &&
143*708a39aaSHaim Dreyfuss 		     !(cmd->flags & CMD_SEND_IN_D3)))
144*708a39aaSHaim Dreyfuss 		return -EHOSTDOWN;
145*708a39aaSHaim Dreyfuss 
14692fe8343SEmmanuel Grumbach 	if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
14792fe8343SEmmanuel Grumbach 		return -EIO;
14892fe8343SEmmanuel Grumbach 
14992fe8343SEmmanuel Grumbach 	if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
15092fe8343SEmmanuel Grumbach 		IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
15192fe8343SEmmanuel Grumbach 		return -EIO;
15292fe8343SEmmanuel Grumbach 	}
15392fe8343SEmmanuel Grumbach 
15492fe8343SEmmanuel Grumbach 	if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
15592fe8343SEmmanuel Grumbach 		    !(cmd->flags & CMD_ASYNC)))
15692fe8343SEmmanuel Grumbach 		return -EINVAL;
15792fe8343SEmmanuel Grumbach 
15892fe8343SEmmanuel Grumbach 	if (!(cmd->flags & CMD_ASYNC))
15992fe8343SEmmanuel Grumbach 		lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
16092fe8343SEmmanuel Grumbach 
161b7d96bcaSLuca Coelho 	if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id))
1625b88792cSSara Sharon 		cmd->id = DEF_ID(cmd->id);
1635b88792cSSara Sharon 
16492fe8343SEmmanuel Grumbach 	ret = trans->ops->send_cmd(trans, cmd);
16592fe8343SEmmanuel Grumbach 
16692fe8343SEmmanuel Grumbach 	if (!(cmd->flags & CMD_ASYNC))
16792fe8343SEmmanuel Grumbach 		lock_map_release(&trans->sync_cmd_lockdep_map);
16892fe8343SEmmanuel Grumbach 
1690ec971fdSJohannes Berg 	if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
1700ec971fdSJohannes Berg 		return -EIO;
1710ec971fdSJohannes Berg 
17292fe8343SEmmanuel Grumbach 	return ret;
17392fe8343SEmmanuel Grumbach }
17492fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
17539bdb17eSSharon Dvir 
17639bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names.
17739bdb17eSSharon Dvir  * Used in the binary search over a list of host commands.
17839bdb17eSSharon Dvir  *
17939bdb17eSSharon Dvir  * @key: command_id that we're looking for.
18039bdb17eSSharon Dvir  * @elt: struct iwl_hcmd_names candidate for match.
18139bdb17eSSharon Dvir  *
18239bdb17eSSharon Dvir  * @return 0 iff equal.
18339bdb17eSSharon Dvir  */
18439bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt)
18539bdb17eSSharon Dvir {
18639bdb17eSSharon Dvir 	const struct iwl_hcmd_names *name = elt;
18739bdb17eSSharon Dvir 	u8 cmd1 = *(u8 *)key;
18839bdb17eSSharon Dvir 	u8 cmd2 = name->cmd_id;
18939bdb17eSSharon Dvir 
19039bdb17eSSharon Dvir 	return (cmd1 - cmd2);
19139bdb17eSSharon Dvir }
19239bdb17eSSharon Dvir 
19339bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
19439bdb17eSSharon Dvir {
19539bdb17eSSharon Dvir 	u8 grp, cmd;
19639bdb17eSSharon Dvir 	struct iwl_hcmd_names *ret;
19739bdb17eSSharon Dvir 	const struct iwl_hcmd_arr *arr;
19839bdb17eSSharon Dvir 	size_t size = sizeof(struct iwl_hcmd_names);
19939bdb17eSSharon Dvir 
20039bdb17eSSharon Dvir 	grp = iwl_cmd_groupid(id);
20139bdb17eSSharon Dvir 	cmd = iwl_cmd_opcode(id);
20239bdb17eSSharon Dvir 
20339bdb17eSSharon Dvir 	if (!trans->command_groups || grp >= trans->command_groups_size ||
20439bdb17eSSharon Dvir 	    !trans->command_groups[grp].arr)
20539bdb17eSSharon Dvir 		return "UNKNOWN";
20639bdb17eSSharon Dvir 
20739bdb17eSSharon Dvir 	arr = &trans->command_groups[grp];
20839bdb17eSSharon Dvir 	ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
20939bdb17eSSharon Dvir 	if (!ret)
21039bdb17eSSharon Dvir 		return "UNKNOWN";
21139bdb17eSSharon Dvir 	return ret->cmd_name;
21239bdb17eSSharon Dvir }
21339bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
21439bdb17eSSharon Dvir 
21539bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
21639bdb17eSSharon Dvir {
21739bdb17eSSharon Dvir 	int i, j;
21839bdb17eSSharon Dvir 	const struct iwl_hcmd_arr *arr;
21939bdb17eSSharon Dvir 
22039bdb17eSSharon Dvir 	for (i = 0; i < trans->command_groups_size; i++) {
22139bdb17eSSharon Dvir 		arr = &trans->command_groups[i];
22239bdb17eSSharon Dvir 		if (!arr->arr)
22339bdb17eSSharon Dvir 			continue;
22439bdb17eSSharon Dvir 		for (j = 0; j < arr->size - 1; j++)
22539bdb17eSSharon Dvir 			if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
22639bdb17eSSharon Dvir 				return -1;
22739bdb17eSSharon Dvir 	}
22839bdb17eSSharon Dvir 	return 0;
22939bdb17eSSharon Dvir }
23039bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);
231