PageRenderTime 71ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/intel/iwlwifi/pcie/ctxt-info.c

https://gitlab.com/oyvholm/linux
C | 288 lines | 172 code | 42 blank | 74 comment | 21 complexity | be4b59d70463605b754db94863b60606 MD5 | raw file
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2017 Intel Deutschland GmbH
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * BSD LICENSE
  20. *
  21. * Copyright(c) 2017 Intel Deutschland GmbH
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. *
  28. * * Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * * Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in
  32. * the documentation and/or other materials provided with the
  33. * distribution.
  34. * * Neither the name Intel Corporation nor the names of its
  35. * contributors may be used to endorse or promote products derived
  36. * from this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. *****************************************************************************/
  51. #include "iwl-trans.h"
  52. #include "iwl-fh.h"
  53. #include "iwl-context-info.h"
  54. #include "internal.h"
  55. #include "iwl-prph.h"
  56. static int iwl_pcie_get_num_sections(const struct fw_img *fw,
  57. int start)
  58. {
  59. int i = 0;
  60. while (start < fw->num_sec &&
  61. fw->sec[start].offset != CPU1_CPU2_SEPARATOR_SECTION &&
  62. fw->sec[start].offset != PAGING_SEPARATOR_SECTION) {
  63. start++;
  64. i++;
  65. }
  66. return i;
  67. }
  68. static int iwl_pcie_ctxt_info_alloc_dma(struct iwl_trans *trans,
  69. const struct fw_desc *sec,
  70. struct iwl_dram_data *dram)
  71. {
  72. dram->block = dma_alloc_coherent(trans->dev, sec->len,
  73. &dram->physical,
  74. GFP_KERNEL);
  75. if (!dram->block)
  76. return -ENOMEM;
  77. dram->size = sec->len;
  78. memcpy(dram->block, sec->data, sec->len);
  79. return 0;
  80. }
  81. static void iwl_pcie_ctxt_info_free_fw_img(struct iwl_trans *trans)
  82. {
  83. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  84. struct iwl_self_init_dram *dram = &trans_pcie->init_dram;
  85. int i;
  86. if (!dram->fw) {
  87. WARN_ON(dram->fw_cnt);
  88. return;
  89. }
  90. for (i = 0; i < dram->fw_cnt; i++)
  91. dma_free_coherent(trans->dev, dram->fw[i].size,
  92. dram->fw[i].block, dram->fw[i].physical);
  93. kfree(dram->fw);
  94. dram->fw_cnt = 0;
  95. dram->fw = NULL;
  96. }
  97. void iwl_pcie_ctxt_info_free_paging(struct iwl_trans *trans)
  98. {
  99. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  100. struct iwl_self_init_dram *dram = &trans_pcie->init_dram;
  101. int i;
  102. if (!dram->paging) {
  103. WARN_ON(dram->paging_cnt);
  104. return;
  105. }
  106. /* free paging*/
  107. for (i = 0; i < dram->paging_cnt; i++)
  108. dma_free_coherent(trans->dev, dram->paging[i].size,
  109. dram->paging[i].block,
  110. dram->paging[i].physical);
  111. kfree(dram->paging);
  112. dram->paging_cnt = 0;
  113. dram->paging = NULL;
  114. }
  115. static int iwl_pcie_ctxt_info_init_fw_sec(struct iwl_trans *trans,
  116. const struct fw_img *fw,
  117. struct iwl_context_info *ctxt_info)
  118. {
  119. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  120. struct iwl_self_init_dram *dram = &trans_pcie->init_dram;
  121. struct iwl_context_info_dram *ctxt_dram = &ctxt_info->dram;
  122. int i, ret, lmac_cnt, umac_cnt, paging_cnt;
  123. if (WARN(dram->paging,
  124. "paging shouldn't already be initialized (%d pages)\n",
  125. dram->paging_cnt))
  126. iwl_pcie_ctxt_info_free_paging(trans);
  127. lmac_cnt = iwl_pcie_get_num_sections(fw, 0);
  128. /* add 1 due to separator */
  129. umac_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + 1);
  130. /* add 2 due to separators */
  131. paging_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + umac_cnt + 2);
  132. dram->fw = kcalloc(umac_cnt + lmac_cnt, sizeof(*dram->fw), GFP_KERNEL);
  133. if (!dram->fw)
  134. return -ENOMEM;
  135. dram->paging = kcalloc(paging_cnt, sizeof(*dram->paging), GFP_KERNEL);
  136. if (!dram->paging)
  137. return -ENOMEM;
  138. /* initialize lmac sections */
  139. for (i = 0; i < lmac_cnt; i++) {
  140. ret = iwl_pcie_ctxt_info_alloc_dma(trans, &fw->sec[i],
  141. &dram->fw[dram->fw_cnt]);
  142. if (ret)
  143. return ret;
  144. ctxt_dram->lmac_img[i] =
  145. cpu_to_le64(dram->fw[dram->fw_cnt].physical);
  146. dram->fw_cnt++;
  147. }
  148. /* initialize umac sections */
  149. for (i = 0; i < umac_cnt; i++) {
  150. /* access FW with +1 to make up for lmac separator */
  151. ret = iwl_pcie_ctxt_info_alloc_dma(trans,
  152. &fw->sec[dram->fw_cnt + 1],
  153. &dram->fw[dram->fw_cnt]);
  154. if (ret)
  155. return ret;
  156. ctxt_dram->umac_img[i] =
  157. cpu_to_le64(dram->fw[dram->fw_cnt].physical);
  158. dram->fw_cnt++;
  159. }
  160. /*
  161. * Initialize paging.
  162. * Paging memory isn't stored in dram->fw as the umac and lmac - it is
  163. * stored separately.
  164. * This is since the timing of its release is different -
  165. * while fw memory can be released on alive, the paging memory can be
  166. * freed only when the device goes down.
  167. * Given that, the logic here in accessing the fw image is a bit
  168. * different - fw_cnt isn't changing so loop counter is added to it.
  169. */
  170. for (i = 0; i < paging_cnt; i++) {
  171. /* access FW with +2 to make up for lmac & umac separators */
  172. int fw_idx = dram->fw_cnt + i + 2;
  173. ret = iwl_pcie_ctxt_info_alloc_dma(trans, &fw->sec[fw_idx],
  174. &dram->paging[i]);
  175. if (ret)
  176. return ret;
  177. ctxt_dram->virtual_img[i] =
  178. cpu_to_le64(dram->paging[i].physical);
  179. dram->paging_cnt++;
  180. }
  181. return 0;
  182. }
  183. int iwl_pcie_ctxt_info_init(struct iwl_trans *trans,
  184. const struct fw_img *fw)
  185. {
  186. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  187. struct iwl_context_info *ctxt_info;
  188. struct iwl_context_info_rbd_cfg *rx_cfg;
  189. u32 control_flags = 0;
  190. int ret;
  191. ctxt_info = dma_alloc_coherent(trans->dev, sizeof(*ctxt_info),
  192. &trans_pcie->ctxt_info_dma_addr,
  193. GFP_KERNEL);
  194. if (!ctxt_info)
  195. return -ENOMEM;
  196. ctxt_info->version.version = 0;
  197. ctxt_info->version.mac_id =
  198. cpu_to_le16((u16)iwl_read32(trans, CSR_HW_REV));
  199. /* size is in DWs */
  200. ctxt_info->version.size = cpu_to_le16(sizeof(*ctxt_info) / 4);
  201. BUILD_BUG_ON(RX_QUEUE_CB_SIZE(MQ_RX_TABLE_SIZE) > 0xF);
  202. control_flags = IWL_CTXT_INFO_RB_SIZE_4K |
  203. IWL_CTXT_INFO_TFD_FORMAT_LONG |
  204. RX_QUEUE_CB_SIZE(MQ_RX_TABLE_SIZE) <<
  205. IWL_CTXT_INFO_RB_CB_SIZE_POS;
  206. ctxt_info->control.control_flags = cpu_to_le32(control_flags);
  207. /* initialize RX default queue */
  208. rx_cfg = &ctxt_info->rbd_cfg;
  209. rx_cfg->free_rbd_addr = cpu_to_le64(trans_pcie->rxq->bd_dma);
  210. rx_cfg->used_rbd_addr = cpu_to_le64(trans_pcie->rxq->used_bd_dma);
  211. rx_cfg->status_wr_ptr = cpu_to_le64(trans_pcie->rxq->rb_stts_dma);
  212. /* initialize TX command queue */
  213. ctxt_info->hcmd_cfg.cmd_queue_addr =
  214. cpu_to_le64(trans_pcie->txq[trans_pcie->cmd_queue]->dma_addr);
  215. ctxt_info->hcmd_cfg.cmd_queue_size =
  216. TFD_QUEUE_CB_SIZE(trans_pcie->tx_cmd_queue_size);
  217. /* allocate ucode sections in dram and set addresses */
  218. ret = iwl_pcie_ctxt_info_init_fw_sec(trans, fw, ctxt_info);
  219. if (ret) {
  220. dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),
  221. ctxt_info, trans_pcie->ctxt_info_dma_addr);
  222. return ret;
  223. }
  224. trans_pcie->ctxt_info = ctxt_info;
  225. iwl_enable_interrupts(trans);
  226. /* Configure debug, if exists */
  227. if (trans->dbg_dest_tlv)
  228. iwl_pcie_apply_destination(trans);
  229. /* kick FW self load */
  230. iwl_write64(trans, CSR_CTXT_INFO_BA, trans_pcie->ctxt_info_dma_addr);
  231. iwl_write_prph(trans, UREG_CPU_INIT_RUN, 1);
  232. /* Context info will be released upon alive or failure to get one */
  233. return 0;
  234. }
  235. void iwl_pcie_ctxt_info_free(struct iwl_trans *trans)
  236. {
  237. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  238. if (!trans_pcie->ctxt_info)
  239. return;
  240. dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),
  241. trans_pcie->ctxt_info,
  242. trans_pcie->ctxt_info_dma_addr);
  243. trans_pcie->ctxt_info_dma_addr = 0;
  244. trans_pcie->ctxt_info = NULL;
  245. iwl_pcie_ctxt_info_free_fw_img(trans);
  246. }