iwl-trans.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2015 Intel Mobile Communications GmbH
  4. * Copyright (C) 2016-2017 Intel Deutschland GmbH
  5. * Copyright (C) 2019-2021 Intel Corporation
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/bsearch.h>
  9. #include "fw/api/tx.h"
  10. #include "iwl-trans.h"
  11. #include "iwl-drv.h"
  12. #include "iwl-fh.h"
  13. #include "queue/tx.h"
  14. #include <linux/dmapool.h>
  15. #include "fw/api/commands.h"
  16. struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
  17. struct device *dev,
  18. const struct iwl_trans_ops *ops,
  19. const struct iwl_cfg_trans_params *cfg_trans)
  20. {
  21. struct iwl_trans *trans;
  22. #ifdef CONFIG_LOCKDEP
  23. static struct lock_class_key __key;
  24. #endif
  25. trans = devm_kzalloc(dev, sizeof(*trans) + priv_size, GFP_KERNEL);
  26. if (!trans)
  27. return NULL;
  28. trans->trans_cfg = cfg_trans;
  29. #ifdef CONFIG_LOCKDEP
  30. lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
  31. &__key, 0);
  32. #endif
  33. trans->dev = dev;
  34. trans->ops = ops;
  35. trans->num_rx_queues = 1;
  36. WARN_ON(!ops->wait_txq_empty && !ops->wait_tx_queues_empty);
  37. if (trans->trans_cfg->use_tfh) {
  38. trans->txqs.tfd.addr_size = 64;
  39. trans->txqs.tfd.max_tbs = IWL_TFH_NUM_TBS;
  40. trans->txqs.tfd.size = sizeof(struct iwl_tfh_tfd);
  41. } else {
  42. trans->txqs.tfd.addr_size = 36;
  43. trans->txqs.tfd.max_tbs = IWL_NUM_OF_TBS;
  44. trans->txqs.tfd.size = sizeof(struct iwl_tfd);
  45. }
  46. trans->max_skb_frags = IWL_TRANS_MAX_FRAGS(trans);
  47. return trans;
  48. }
  49. int iwl_trans_init(struct iwl_trans *trans)
  50. {
  51. int txcmd_size, txcmd_align;
  52. if (!trans->trans_cfg->gen2) {
  53. txcmd_size = sizeof(struct iwl_tx_cmd);
  54. txcmd_align = sizeof(void *);
  55. } else if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210) {
  56. txcmd_size = sizeof(struct iwl_tx_cmd_gen2);
  57. txcmd_align = 64;
  58. } else {
  59. txcmd_size = sizeof(struct iwl_tx_cmd_gen3);
  60. txcmd_align = 128;
  61. }
  62. txcmd_size += sizeof(struct iwl_cmd_header);
  63. txcmd_size += 36; /* biggest possible 802.11 header */
  64. /* Ensure device TX cmd cannot reach/cross a page boundary in gen2 */
  65. if (WARN_ON(trans->trans_cfg->gen2 && txcmd_size >= txcmd_align))
  66. return -EINVAL;
  67. if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
  68. trans->txqs.bc_tbl_size =
  69. sizeof(struct iwl_gen3_bc_tbl_entry) * TFD_QUEUE_BC_SIZE_GEN3_BZ;
  70. else if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
  71. trans->txqs.bc_tbl_size =
  72. sizeof(struct iwl_gen3_bc_tbl_entry) * TFD_QUEUE_BC_SIZE_GEN3_AX210;
  73. else
  74. trans->txqs.bc_tbl_size = sizeof(struct iwlagn_scd_bc_tbl);
  75. /*
  76. * For gen2 devices, we use a single allocation for each byte-count
  77. * table, but they're pretty small (1k) so use a DMA pool that we
  78. * allocate here.
  79. */
  80. if (trans->trans_cfg->gen2) {
  81. trans->txqs.bc_pool = dmam_pool_create("iwlwifi:bc", trans->dev,
  82. trans->txqs.bc_tbl_size,
  83. 256, 0);
  84. if (!trans->txqs.bc_pool)
  85. return -ENOMEM;
  86. }
  87. /* Some things must not change even if the config does */
  88. WARN_ON(trans->txqs.tfd.addr_size !=
  89. (trans->trans_cfg->use_tfh ? 64 : 36));
  90. snprintf(trans->dev_cmd_pool_name, sizeof(trans->dev_cmd_pool_name),
  91. "iwl_cmd_pool:%s", dev_name(trans->dev));
  92. trans->dev_cmd_pool =
  93. kmem_cache_create(trans->dev_cmd_pool_name,
  94. txcmd_size, txcmd_align,
  95. SLAB_HWCACHE_ALIGN, NULL);
  96. if (!trans->dev_cmd_pool)
  97. return -ENOMEM;
  98. trans->txqs.tso_hdr_page = alloc_percpu(struct iwl_tso_hdr_page);
  99. if (!trans->txqs.tso_hdr_page) {
  100. kmem_cache_destroy(trans->dev_cmd_pool);
  101. return -ENOMEM;
  102. }
  103. /* Initialize the wait queue for commands */
  104. init_waitqueue_head(&trans->wait_command_queue);
  105. return 0;
  106. }
  107. void iwl_trans_free(struct iwl_trans *trans)
  108. {
  109. int i;
  110. if (trans->txqs.tso_hdr_page) {
  111. for_each_possible_cpu(i) {
  112. struct iwl_tso_hdr_page *p =
  113. per_cpu_ptr(trans->txqs.tso_hdr_page, i);
  114. if (p && p->page)
  115. __free_page(p->page);
  116. }
  117. free_percpu(trans->txqs.tso_hdr_page);
  118. }
  119. kmem_cache_destroy(trans->dev_cmd_pool);
  120. }
  121. int iwl_trans_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  122. {
  123. int ret;
  124. if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  125. test_bit(STATUS_RFKILL_OPMODE, &trans->status)))
  126. return -ERFKILL;
  127. /*
  128. * We can't test IWL_MVM_STATUS_IN_D3 in mvm->status because this
  129. * bit is set early in the D3 flow, before we send all the commands
  130. * that configure the firmware for D3 operation (power, patterns, ...)
  131. * and we don't want to flag all those with CMD_SEND_IN_D3.
  132. * So use the system_pm_mode instead. The only command sent after
  133. * we set system_pm_mode is D3_CONFIG_CMD, which we now flag with
  134. * CMD_SEND_IN_D3.
  135. */
  136. if (unlikely(trans->system_pm_mode == IWL_PLAT_PM_MODE_D3 &&
  137. !(cmd->flags & CMD_SEND_IN_D3)))
  138. return -EHOSTDOWN;
  139. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  140. return -EIO;
  141. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
  142. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  143. return -EIO;
  144. }
  145. if (WARN_ON((cmd->flags & CMD_WANT_ASYNC_CALLBACK) &&
  146. !(cmd->flags & CMD_ASYNC)))
  147. return -EINVAL;
  148. if (!(cmd->flags & CMD_ASYNC))
  149. lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
  150. if (trans->wide_cmd_header && !iwl_cmd_groupid(cmd->id)) {
  151. if (cmd->id != REPLY_ERROR)
  152. cmd->id = DEF_ID(cmd->id);
  153. }
  154. ret = iwl_trans_txq_send_hcmd(trans, cmd);
  155. if (!(cmd->flags & CMD_ASYNC))
  156. lock_map_release(&trans->sync_cmd_lockdep_map);
  157. if (WARN_ON((cmd->flags & CMD_WANT_SKB) && !ret && !cmd->resp_pkt))
  158. return -EIO;
  159. return ret;
  160. }
  161. IWL_EXPORT_SYMBOL(iwl_trans_send_cmd);
  162. /* Comparator for struct iwl_hcmd_names.
  163. * Used in the binary search over a list of host commands.
  164. *
  165. * @key: command_id that we're looking for.
  166. * @elt: struct iwl_hcmd_names candidate for match.
  167. *
  168. * @return 0 iff equal.
  169. */
  170. static int iwl_hcmd_names_cmp(const void *key, const void *elt)
  171. {
  172. const struct iwl_hcmd_names *name = elt;
  173. const u8 *cmd1 = key;
  174. u8 cmd2 = name->cmd_id;
  175. return (*cmd1 - cmd2);
  176. }
  177. const char *iwl_get_cmd_string(struct iwl_trans *trans, u32 id)
  178. {
  179. u8 grp, cmd;
  180. struct iwl_hcmd_names *ret;
  181. const struct iwl_hcmd_arr *arr;
  182. size_t size = sizeof(struct iwl_hcmd_names);
  183. grp = iwl_cmd_groupid(id);
  184. cmd = iwl_cmd_opcode(id);
  185. if (!trans->command_groups || grp >= trans->command_groups_size ||
  186. !trans->command_groups[grp].arr)
  187. return "UNKNOWN";
  188. arr = &trans->command_groups[grp];
  189. ret = bsearch(&cmd, arr->arr, arr->size, size, iwl_hcmd_names_cmp);
  190. if (!ret)
  191. return "UNKNOWN";
  192. return ret->cmd_name;
  193. }
  194. IWL_EXPORT_SYMBOL(iwl_get_cmd_string);
  195. int iwl_cmd_groups_verify_sorted(const struct iwl_trans_config *trans)
  196. {
  197. int i, j;
  198. const struct iwl_hcmd_arr *arr;
  199. for (i = 0; i < trans->command_groups_size; i++) {
  200. arr = &trans->command_groups[i];
  201. if (!arr->arr)
  202. continue;
  203. for (j = 0; j < arr->size - 1; j++)
  204. if (arr->arr[j].cmd_id > arr->arr[j + 1].cmd_id)
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. IWL_EXPORT_SYMBOL(iwl_cmd_groups_verify_sorted);