trans-gen2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. * Copyright(c) 2018 Intel Corporation
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Copyright(c) 2017 Intel Deutschland GmbH
  23. * Copyright(c) 2018 Intel Corporation
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions
  28. * are met:
  29. *
  30. * * Redistributions of source code must retain the above copyright
  31. * notice, this list of conditions and the following disclaimer.
  32. * * Redistributions in binary form must reproduce the above copyright
  33. * notice, this list of conditions and the following disclaimer in
  34. * the documentation and/or other materials provided with the
  35. * distribution.
  36. * * Neither the name Intel Corporation nor the names of its
  37. * contributors may be used to endorse or promote products derived
  38. * from this software without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  41. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  42. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  43. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  44. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  46. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  47. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  48. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  50. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. *****************************************************************************/
  53. #include "iwl-trans.h"
  54. #include "iwl-prph.h"
  55. #include "iwl-context-info.h"
  56. #include "internal.h"
  57. /*
  58. * Start up NIC's basic functionality after it has been reset
  59. * (e.g. after platform boot, or shutdown via iwl_pcie_apm_stop())
  60. * NOTE: This does not load uCode nor start the embedded processor
  61. */
  62. static int iwl_pcie_gen2_apm_init(struct iwl_trans *trans)
  63. {
  64. int ret = 0;
  65. IWL_DEBUG_INFO(trans, "Init card's basic functions\n");
  66. /*
  67. * Use "set_bit" below rather than "write", to preserve any hardware
  68. * bits already set by default after reset.
  69. */
  70. /*
  71. * Disable L0s without affecting L1;
  72. * don't wait for ICH L0s (ICH bug W/A)
  73. */
  74. iwl_set_bit(trans, CSR_GIO_CHICKEN_BITS,
  75. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  76. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  77. iwl_set_bit(trans, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  78. /*
  79. * Enable HAP INTA (interrupt from management bus) to
  80. * wake device's PCI Express link L1a -> L0s
  81. */
  82. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  83. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  84. iwl_pcie_apm_config(trans);
  85. /*
  86. * Set "initialization complete" bit to move adapter from
  87. * D0U* --> D0A* (powered-up active) state.
  88. */
  89. iwl_set_bit(trans, CSR_GP_CNTRL,
  90. BIT(trans->cfg->csr->flag_init_done));
  91. /*
  92. * Wait for clock stabilization; once stabilized, access to
  93. * device-internal resources is supported, e.g. iwl_write_prph()
  94. * and accesses to uCode SRAM.
  95. */
  96. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  97. BIT(trans->cfg->csr->flag_mac_clock_ready),
  98. BIT(trans->cfg->csr->flag_mac_clock_ready),
  99. 25000);
  100. if (ret < 0) {
  101. IWL_DEBUG_INFO(trans, "Failed to init the card\n");
  102. return ret;
  103. }
  104. set_bit(STATUS_DEVICE_ENABLED, &trans->status);
  105. return 0;
  106. }
  107. static void iwl_pcie_gen2_apm_stop(struct iwl_trans *trans, bool op_mode_leave)
  108. {
  109. IWL_DEBUG_INFO(trans, "Stop card, put in low power state\n");
  110. if (op_mode_leave) {
  111. if (!test_bit(STATUS_DEVICE_ENABLED, &trans->status))
  112. iwl_pcie_gen2_apm_init(trans);
  113. /* inform ME that we are leaving */
  114. iwl_set_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  115. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  116. iwl_set_bit(trans, CSR_HW_IF_CONFIG_REG,
  117. CSR_HW_IF_CONFIG_REG_PREPARE |
  118. CSR_HW_IF_CONFIG_REG_ENABLE_PME);
  119. mdelay(1);
  120. iwl_clear_bit(trans, CSR_DBG_LINK_PWR_MGMT_REG,
  121. CSR_RESET_LINK_PWR_MGMT_DISABLED);
  122. mdelay(5);
  123. }
  124. clear_bit(STATUS_DEVICE_ENABLED, &trans->status);
  125. /* Stop device's DMA activity */
  126. iwl_pcie_apm_stop_master(trans);
  127. iwl_trans_sw_reset(trans);
  128. /*
  129. * Clear "initialization complete" bit to move adapter from
  130. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  131. */
  132. iwl_clear_bit(trans, CSR_GP_CNTRL,
  133. BIT(trans->cfg->csr->flag_init_done));
  134. }
  135. void _iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans, bool low_power)
  136. {
  137. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  138. lockdep_assert_held(&trans_pcie->mutex);
  139. if (trans_pcie->is_down)
  140. return;
  141. trans_pcie->is_down = true;
  142. /* Stop dbgc before stopping device */
  143. iwl_write_prph(trans, DBGC_IN_SAMPLE, 0);
  144. udelay(100);
  145. iwl_write_prph(trans, DBGC_OUT_CTRL, 0);
  146. /* tell the device to stop sending interrupts */
  147. iwl_disable_interrupts(trans);
  148. /* device going down, Stop using ICT table */
  149. iwl_pcie_disable_ict(trans);
  150. /*
  151. * If a HW restart happens during firmware loading,
  152. * then the firmware loading might call this function
  153. * and later it might be called again due to the
  154. * restart. So don't process again if the device is
  155. * already dead.
  156. */
  157. if (test_and_clear_bit(STATUS_DEVICE_ENABLED, &trans->status)) {
  158. IWL_DEBUG_INFO(trans,
  159. "DEVICE_ENABLED bit was set and is now cleared\n");
  160. iwl_pcie_gen2_tx_stop(trans);
  161. iwl_pcie_rx_stop(trans);
  162. }
  163. iwl_pcie_ctxt_info_free_paging(trans);
  164. iwl_pcie_ctxt_info_free(trans);
  165. /* Make sure (redundant) we've released our request to stay awake */
  166. iwl_clear_bit(trans, CSR_GP_CNTRL,
  167. BIT(trans->cfg->csr->flag_mac_access_req));
  168. /* Stop the device, and put it in low power state */
  169. iwl_pcie_gen2_apm_stop(trans, false);
  170. iwl_trans_sw_reset(trans);
  171. /*
  172. * Upon stop, the IVAR table gets erased, so msi-x won't
  173. * work. This causes a bug in RF-KILL flows, since the interrupt
  174. * that enables radio won't fire on the correct irq, and the
  175. * driver won't be able to handle the interrupt.
  176. * Configure the IVAR table again after reset.
  177. */
  178. iwl_pcie_conf_msix_hw(trans_pcie);
  179. /*
  180. * Upon stop, the APM issues an interrupt if HW RF kill is set.
  181. * This is a bug in certain verions of the hardware.
  182. * Certain devices also keep sending HW RF kill interrupt all
  183. * the time, unless the interrupt is ACKed even if the interrupt
  184. * should be masked. Re-ACK all the interrupts here.
  185. */
  186. iwl_disable_interrupts(trans);
  187. /* clear all status bits */
  188. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  189. clear_bit(STATUS_INT_ENABLED, &trans->status);
  190. clear_bit(STATUS_TPOWER_PMI, &trans->status);
  191. /*
  192. * Even if we stop the HW, we still want the RF kill
  193. * interrupt
  194. */
  195. iwl_enable_rfkill_int(trans);
  196. /* re-take ownership to prevent other users from stealing the device */
  197. iwl_pcie_prepare_card_hw(trans);
  198. }
  199. void iwl_trans_pcie_gen2_stop_device(struct iwl_trans *trans, bool low_power)
  200. {
  201. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  202. bool was_in_rfkill;
  203. mutex_lock(&trans_pcie->mutex);
  204. trans_pcie->opmode_down = true;
  205. was_in_rfkill = test_bit(STATUS_RFKILL_OPMODE, &trans->status);
  206. _iwl_trans_pcie_gen2_stop_device(trans, low_power);
  207. iwl_trans_pcie_handle_stop_rfkill(trans, was_in_rfkill);
  208. mutex_unlock(&trans_pcie->mutex);
  209. }
  210. static int iwl_pcie_gen2_nic_init(struct iwl_trans *trans)
  211. {
  212. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  213. /* TODO: most of the logic can be removed in A0 - but not in Z0 */
  214. spin_lock(&trans_pcie->irq_lock);
  215. iwl_pcie_gen2_apm_init(trans);
  216. spin_unlock(&trans_pcie->irq_lock);
  217. iwl_op_mode_nic_config(trans->op_mode);
  218. /* Allocate the RX queue, or reset if it is already allocated */
  219. if (iwl_pcie_gen2_rx_init(trans))
  220. return -ENOMEM;
  221. /* Allocate or reset and init all Tx and Command queues */
  222. if (iwl_pcie_gen2_tx_init(trans))
  223. return -ENOMEM;
  224. /* enable shadow regs in HW */
  225. iwl_set_bit(trans, CSR_MAC_SHADOW_REG_CTRL, 0x800FFFFF);
  226. IWL_DEBUG_INFO(trans, "Enabling shadow registers in device\n");
  227. return 0;
  228. }
  229. void iwl_trans_pcie_gen2_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  230. {
  231. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  232. iwl_pcie_reset_ict(trans);
  233. /* make sure all queue are not stopped/used */
  234. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  235. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  236. /* now that we got alive we can free the fw image & the context info.
  237. * paging memory cannot be freed included since FW will still use it
  238. */
  239. iwl_pcie_ctxt_info_free(trans);
  240. }
  241. int iwl_trans_pcie_gen2_start_fw(struct iwl_trans *trans,
  242. const struct fw_img *fw, bool run_in_rfkill)
  243. {
  244. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  245. bool hw_rfkill;
  246. int ret;
  247. /* This may fail if AMT took ownership of the device */
  248. if (iwl_pcie_prepare_card_hw(trans)) {
  249. IWL_WARN(trans, "Exit HW not ready\n");
  250. ret = -EIO;
  251. goto out;
  252. }
  253. iwl_enable_rfkill_int(trans);
  254. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  255. /*
  256. * We enabled the RF-Kill interrupt and the handler may very
  257. * well be running. Disable the interrupts to make sure no other
  258. * interrupt can be fired.
  259. */
  260. iwl_disable_interrupts(trans);
  261. /* Make sure it finished running */
  262. iwl_pcie_synchronize_irqs(trans);
  263. mutex_lock(&trans_pcie->mutex);
  264. /* If platform's RF_KILL switch is NOT set to KILL */
  265. hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
  266. if (hw_rfkill && !run_in_rfkill) {
  267. ret = -ERFKILL;
  268. goto out;
  269. }
  270. /* Someone called stop_device, don't try to start_fw */
  271. if (trans_pcie->is_down) {
  272. IWL_WARN(trans,
  273. "Can't start_fw since the HW hasn't been started\n");
  274. ret = -EIO;
  275. goto out;
  276. }
  277. /* make sure rfkill handshake bits are cleared */
  278. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  279. iwl_write32(trans, CSR_UCODE_DRV_GP1_CLR,
  280. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  281. /* clear (again), then enable host interrupts */
  282. iwl_write32(trans, CSR_INT, 0xFFFFFFFF);
  283. ret = iwl_pcie_gen2_nic_init(trans);
  284. if (ret) {
  285. IWL_ERR(trans, "Unable to init nic\n");
  286. goto out;
  287. }
  288. ret = iwl_pcie_ctxt_info_init(trans, fw);
  289. if (ret)
  290. goto out;
  291. /* re-check RF-Kill state since we may have missed the interrupt */
  292. hw_rfkill = iwl_pcie_check_hw_rf_kill(trans);
  293. if (hw_rfkill && !run_in_rfkill)
  294. ret = -ERFKILL;
  295. out:
  296. mutex_unlock(&trans_pcie->mutex);
  297. return ret;
  298. }