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
5*12a89f01SJohannes Berg * Copyright (C) 2019-2021, 2023 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>
16781b9ae4SMukesh Sisodiya #include "fw/api/commands.h"
17e705c121SKalle Valo
iwl_trans_alloc(unsigned int priv_size,struct device * dev,const struct iwl_trans_ops * ops,const struct iwl_cfg_trans_params * cfg_trans)18e705c121SKalle Valo struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
19e705c121SKalle Valo struct device *dev,
20a89c72ffSJohannes Berg const struct iwl_trans_ops *ops,
21fda1bd0dSMordechay Goodstein const struct iwl_cfg_trans_params *cfg_trans)
22e705c121SKalle Valo {
23e705c121SKalle Valo struct iwl_trans *trans;
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
34e705c121SKalle Valo #ifdef CONFIG_LOCKDEP
35e705c121SKalle Valo lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
36e705c121SKalle Valo &__key, 0);
37e705c121SKalle Valo #endif
38e705c121SKalle Valo
39e705c121SKalle Valo trans->dev = dev;
40e705c121SKalle Valo trans->ops = ops;
41e705c121SKalle Valo trans->num_rx_queues = 1;
42e705c121SKalle Valo
43d12455fdSJohannes Berg WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
44885375d0SMordechay Goodstein
45*12a89f01SJohannes Berg if (trans->trans_cfg->gen2) {
46885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 64;
47885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
48885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
49885375d0SMordechay Goodstein } else {
50885375d0SMordechay Goodstein trans->txqs.tfd.addr_size = 36;
51885375d0SMordechay Goodstein trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
52885375d0SMordechay Goodstein trans->txqs.tfd.size = sizeof(struct iwl_tfd);
53885375d0SMordechay Goodstein }
54885375d0SMordechay Goodstein trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans);
55885375d0SMordechay Goodstein
56d12455fdSJohannes Berg return trans;
57d12455fdSJohannes Berg }
58d12455fdSJohannes Berg
iwl_trans_init(struct iwl_trans * trans)59d12455fdSJohannes Berg int iwl_trans_init(struct iwl_trans *trans)
60d12455fdSJohannes Berg {
61d12455fdSJohannes Berg int txcmd_size, txcmd_align;
62d12455fdSJohannes Berg
63d12455fdSJohannes Berg if (!trans->trans_cfg->gen2) {
64d12455fdSJohannes Berg txcmd_size = sizeof(struct iwl_tx_cmd);
65d12455fdSJohannes Berg txcmd_align = sizeof(void *);
66d12455fdSJohannes Berg } else if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210) {
67d12455fdSJohannes Berg txcmd_size = sizeof(struct iwl_tx_cmd_gen2);
68d12455fdSJohannes Berg txcmd_align = 64;
69d12455fdSJohannes Berg } else {
70d12455fdSJohannes Berg txcmd_size = sizeof(struct iwl_tx_cmd_gen3);
71d12455fdSJohannes Berg txcmd_align = 128;
72d12455fdSJohannes Berg }
73d12455fdSJohannes Berg
74d12455fdSJohannes Berg txcmd_size += sizeof(struct iwl_cmd_header);
75d12455fdSJohannes Berg txcmd_size += 36; /* biggest possible 802.11 header */
76d12455fdSJohannes Berg
77d12455fdSJohannes Berg /* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
78d12455fdSJohannes Berg if (WARN_ON(trans->trans_cfg->gen2 && txcmd_size >= txcmd_align))
79d12455fdSJohannes Berg return -EINVAL;
80d12455fdSJohannes Berg
81d5399f11SMordechay Goodstein if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
82d5399f11SMordechay Goodstein trans->txqs.bc_tbl_size =
83d5399f11SMordechay Goodstein sizeof(struct iwl_gen3_bc_tbl_entry) * TFD_QUEUE_BC_SIZE_GEN3_BZ;
84d5399f11SMordechay Goodstein else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
85d5399f11SMordechay Goodstein trans->txqs.bc_tbl_size =
86d5399f11SMordechay Goodstein sizeof(struct iwl_gen3_bc_tbl_entry) * TFD_QUEUE_BC_SIZE_GEN3_AX210;
87d12455fdSJohannes Berg else
88d12455fdSJohannes Berg trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl);
89d12455fdSJohannes Berg /*
90d12455fdSJohannes Berg * For gen2 devices, we use a single allocation for each byte-count
91d12455fdSJohannes Berg * table, but they're pretty small (1k) so use a DMA pool that we
92d12455fdSJohannes Berg * allocate here.
93d12455fdSJohannes Berg */
94d12455fdSJohannes Berg if (trans->trans_cfg->gen2) {
95d12455fdSJohannes Berg trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", trans->dev,
96d12455fdSJohannes Berg trans->txqs.bc_tbl_size,
97d12455fdSJohannes Berg 256, 0);
98d12455fdSJohannes Berg if (!trans->txqs.bc_pool)
99d12455fdSJohannes Berg return -ENOMEM;
100d12455fdSJohannes Berg }
101d12455fdSJohannes Berg
102d12455fdSJohannes Berg /* Some things must not change even if the config does */
103d12455fdSJohannes Berg WARN_ON(trans->txqs.tfd.addr_size !=
104*12a89f01SJohannes Berg (trans->trans_cfg->gen2 ? 64 : 36));
105d12455fdSJohannes Berg
106e705c121SKalle Valo snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
107e705c121SKalle Valo "iwl_cmd_pool:%s", dev_name(trans->dev));
108e705c121SKalle Valo trans->dev_cmd_pool =
109e705c121SKalle Valo kmem_cache_create(trans->dev_cmd_pool_name,
110fda1bd0dSMordechay Goodstein txcmd_size, txcmd_align,
111a89c72ffSJohannes Berg SLAB_HWCACHE_ALIGN, NULL);
112e705c121SKalle Valo if (!trans->dev_cmd_pool)
113d12455fdSJohannes Berg return -ENOMEM;
114d6d517b7SSara Sharon
1150cd1ad2dSMordechay Goodstein trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
1160cd1ad2dSMordechay Goodstein if (!trans->txqs.tso_hdr_page) {
1170cd1ad2dSMordechay Goodstein kmem_cache_destroy(trans->dev_cmd_pool);
118d12455fdSJohannes Berg return -ENOMEM;
1190cd1ad2dSMordechay Goodstein }
1200cd1ad2dSMordechay Goodstein
12113f028b4SMordechay Goodstein /* Initialize the wait queue for commands */
12213f028b4SMordechay Goodstein init_waitqueue_head(&trans->wait_command_queue);
12313f028b4SMordechay Goodstein
124d12455fdSJohannes Berg return 0;
125e705c121SKalle Valo }
126e705c121SKalle Valo
iwl_trans_free(struct iwl_trans * trans)127e705c121SKalle Valo void iwl_trans_free(struct iwl_trans *trans)
128e705c121SKalle Valo {
1290cd1ad2dSMordechay Goodstein int i;
1300cd1ad2dSMordechay Goodstein
131d12455fdSJohannes Berg if (trans->txqs.tso_hdr_page) {
1320cd1ad2dSMordechay Goodstein for_each_possible_cpu(i) {
1330cd1ad2dSMordechay Goodstein struct iwl_tso_hdr_page *p =
1340cd1ad2dSMordechay Goodstein per_cpu_ptr(trans->txqs.tso_hdr_page, i);
1350cd1ad2dSMordechay Goodstein
136d12455fdSJohannes Berg if (p && p->page)
1370cd1ad2dSMordechay Goodstein __free_page(p->page);
1380cd1ad2dSMordechay Goodstein }
1390cd1ad2dSMordechay Goodstein
1400cd1ad2dSMordechay Goodstein free_percpu(trans->txqs.tso_hdr_page);
141d12455fdSJohannes Berg }
1420cd1ad2dSMordechay Goodstein
143e705c121SKalle Valo kmem_cache_destroy(trans->dev_cmd_pool);
144e705c121SKalle Valo }
14592fe8343SEmmanuel Grumbach
iwl_trans_send_cmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)14692fe8343SEmmanuel Grumbach int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
14792fe8343SEmmanuel Grumbach {
14892fe8343SEmmanuel Grumbach int ret;
14992fe8343SEmmanuel Grumbach
15092fe8343SEmmanuel Grumbach if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
151326477e4SJohannes Berg test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
15292fe8343SEmmanuel Grumbach return -ERFKILL;
15392fe8343SEmmanuel Grumbach
154708a39aaSHaim Dreyfuss /*
155708a39aaSHaim Dreyfuss * We can't test IWL_MVM_STATUS_IN_D3 in mvm->status because this
156708a39aaSHaim Dreyfuss * bit is set early in the D3 flow, before we send all the commands
157708a39aaSHaim Dreyfuss * that configure the firmware for D3 operation (power, patterns, ...)
158708a39aaSHaim Dreyfuss * and we don't want to flag all those with CMD_SEND_IN_D3.
159708a39aaSHaim Dreyfuss * So use the system_pm_mode instead. The only command sent after
160708a39aaSHaim Dreyfuss * we set system_pm_mode is D3_CONFIG_CMD, which we now flag with
161708a39aaSHaim Dreyfuss * CMD_SEND_IN_D3.
162708a39aaSHaim Dreyfuss */
163708a39aaSHaim Dreyfuss if (unlikely(trans->system_pm_mode == IWL_PLAT_PM_MODE_D3 &&
164708a39aaSHaim Dreyfuss !(cmd->flags & CMD_SEND_IN_D3)))
165708a39aaSHaim Dreyfuss return -EHOSTDOWN;
166708a39aaSHaim Dreyfuss
16792fe8343SEmmanuel Grumbach if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
16892fe8343SEmmanuel Grumbach return -EIO;
16992fe8343SEmmanuel Grumbach
17092fe8343SEmmanuel Grumbach if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
17192fe8343SEmmanuel Grumbach IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
17292fe8343SEmmanuel Grumbach return -EIO;
17392fe8343SEmmanuel Grumbach }
17492fe8343SEmmanuel Grumbach
17592fe8343SEmmanuel Grumbach if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
17692fe8343SEmmanuel Grumbach !(cmd->flags & CMD_ASYNC)))
17792fe8343SEmmanuel Grumbach return -EINVAL;
17892fe8343SEmmanuel Grumbach
17992fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC))
18092fe8343SEmmanuel Grumbach lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
18192fe8343SEmmanuel Grumbach
182781b9ae4SMukesh Sisodiya if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id)) {
183781b9ae4SMukesh Sisodiya if (cmd->id != REPLY_ERROR)
1845b88792cSSara Sharon cmd->id = DEF_ID(cmd->id);
185781b9ae4SMukesh Sisodiya }
1865b88792cSSara Sharon
18713f028b4SMordechay Goodstein ret = iwl_trans_txq_send_hcmd(trans, cmd);
18892fe8343SEmmanuel Grumbach
18992fe8343SEmmanuel Grumbach if (!(cmd->flags & CMD_ASYNC))
19092fe8343SEmmanuel Grumbach lock_map_release(&trans->sync_cmd_lockdep_map);
19192fe8343SEmmanuel Grumbach
1920ec971fdSJohannes Berg if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
1930ec971fdSJohannes Berg return -EIO;
1940ec971fdSJohannes Berg
19592fe8343SEmmanuel Grumbach return ret;
19692fe8343SEmmanuel Grumbach }
19792fe8343SEmmanuel Grumbach IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
19839bdb17eSSharon Dvir
19939bdb17eSSharon Dvir /* Comparator for struct iwl_hcmd_names.
20039bdb17eSSharon Dvir * Used in the binary search over a list of host commands.
20139bdb17eSSharon Dvir *
20239bdb17eSSharon Dvir * @key: command_id that we're looking for.
20339bdb17eSSharon Dvir * @elt: struct iwl_hcmd_names candidate for match.
20439bdb17eSSharon Dvir *
20539bdb17eSSharon Dvir * @return 0 iff equal.
20639bdb17eSSharon Dvir */
iwl_hcmd_names_cmp(const void * key,const void * elt)20739bdb17eSSharon Dvir static int iwl_hcmd_names_cmp(const void *key, const void *elt)
20839bdb17eSSharon Dvir {
20939bdb17eSSharon Dvir const struct iwl_hcmd_names *name = elt;
21073c289baSBjoern A. Zeeb const u8 *cmd1 = key;
21139bdb17eSSharon Dvir u8 cmd2 = name->cmd_id;
21239bdb17eSSharon Dvir
21373c289baSBjoern A. Zeeb return (*cmd1 - cmd2);
21439bdb17eSSharon Dvir }
21539bdb17eSSharon Dvir
iwl_get_cmd_string(struct iwl_trans * trans,u32 id)21639bdb17eSSharon Dvir const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
21739bdb17eSSharon Dvir {
21839bdb17eSSharon Dvir u8 grp, cmd;
21939bdb17eSSharon Dvir struct iwl_hcmd_names *ret;
22039bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr;
22139bdb17eSSharon Dvir size_t size = sizeof(struct iwl_hcmd_names);
22239bdb17eSSharon Dvir
22339bdb17eSSharon Dvir grp = iwl_cmd_groupid(id);
22439bdb17eSSharon Dvir cmd = iwl_cmd_opcode(id);
22539bdb17eSSharon Dvir
22639bdb17eSSharon Dvir if (!trans->command_groups || grp >= trans->command_groups_size ||
22739bdb17eSSharon Dvir !trans->command_groups[grp].arr)
22839bdb17eSSharon Dvir return "UNKNOWN";
22939bdb17eSSharon Dvir
23039bdb17eSSharon Dvir arr = &trans->command_groups[grp];
23139bdb17eSSharon Dvir ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
23239bdb17eSSharon Dvir if (!ret)
23339bdb17eSSharon Dvir return "UNKNOWN";
23439bdb17eSSharon Dvir return ret->cmd_name;
23539bdb17eSSharon Dvir }
23639bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
23739bdb17eSSharon Dvir
iwl_cmd_groups_verify_sorted(const struct iwl_trans_config * trans)23839bdb17eSSharon Dvir int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
23939bdb17eSSharon Dvir {
24039bdb17eSSharon Dvir int i, j;
24139bdb17eSSharon Dvir const struct iwl_hcmd_arr *arr;
24239bdb17eSSharon Dvir
24339bdb17eSSharon Dvir for (i = 0; i < trans->command_groups_size; i++) {
24439bdb17eSSharon Dvir arr = &trans->command_groups[i];
24539bdb17eSSharon Dvir if (!arr->arr)
24639bdb17eSSharon Dvir continue;
24739bdb17eSSharon Dvir for (j = 0; j < arr->size - 1; j++)
24839bdb17eSSharon Dvir if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
24939bdb17eSSharon Dvir return -1;
25039bdb17eSSharon Dvir }
25139bdb17eSSharon Dvir return 0;
25239bdb17eSSharon Dvir }
25339bdb17eSSharon Dvir IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);
254