fw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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) 2012 - 2013 Intel Corporation. All rights reserved.
  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. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <net/mac80211.h>
  64. #include "iwl-trans.h"
  65. #include "iwl-op-mode.h"
  66. #include "iwl-fw.h"
  67. #include "iwl-debug.h"
  68. #include "iwl-csr.h" /* for iwl_mvm_rx_card_state_notif */
  69. #include "iwl-io.h" /* for iwl_mvm_rx_card_state_notif */
  70. #include "iwl-eeprom-parse.h"
  71. #include "mvm.h"
  72. #include "iwl-phy-db.h"
  73. #define MVM_UCODE_ALIVE_TIMEOUT HZ
  74. #define MVM_UCODE_CALIB_TIMEOUT (2*HZ)
  75. #define UCODE_VALID_OK cpu_to_le32(0x1)
  76. struct iwl_mvm_alive_data {
  77. bool valid;
  78. u32 scd_base_addr;
  79. };
  80. static inline const struct fw_img *
  81. iwl_get_ucode_image(struct iwl_mvm *mvm, enum iwl_ucode_type ucode_type)
  82. {
  83. if (ucode_type >= IWL_UCODE_TYPE_MAX)
  84. return NULL;
  85. return &mvm->fw->img[ucode_type];
  86. }
  87. static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
  88. {
  89. struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
  90. .valid = cpu_to_le32(valid_tx_ant),
  91. };
  92. IWL_DEBUG_FW(mvm, "select valid tx ant: %u\n", valid_tx_ant);
  93. return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, CMD_SYNC,
  94. sizeof(tx_ant_cmd), &tx_ant_cmd);
  95. }
  96. static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
  97. struct iwl_rx_packet *pkt, void *data)
  98. {
  99. struct iwl_mvm *mvm =
  100. container_of(notif_wait, struct iwl_mvm, notif_wait);
  101. struct iwl_mvm_alive_data *alive_data = data;
  102. struct mvm_alive_resp *palive;
  103. palive = (void *)pkt->data;
  104. mvm->error_event_table = le32_to_cpu(palive->error_event_table_ptr);
  105. mvm->log_event_table = le32_to_cpu(palive->log_event_table_ptr);
  106. alive_data->scd_base_addr = le32_to_cpu(palive->scd_base_ptr);
  107. alive_data->valid = le16_to_cpu(palive->status) == IWL_ALIVE_STATUS_OK;
  108. IWL_DEBUG_FW(mvm,
  109. "Alive ucode status 0x%04x revision 0x%01X 0x%01X flags 0x%01X\n",
  110. le16_to_cpu(palive->status), palive->ver_type,
  111. palive->ver_subtype, palive->flags);
  112. return true;
  113. }
  114. static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
  115. struct iwl_rx_packet *pkt, void *data)
  116. {
  117. struct iwl_phy_db *phy_db = data;
  118. if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
  119. WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
  120. return true;
  121. }
  122. WARN_ON(iwl_phy_db_set_section(phy_db, pkt, GFP_ATOMIC));
  123. return false;
  124. }
  125. static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
  126. enum iwl_ucode_type ucode_type)
  127. {
  128. struct iwl_notification_wait alive_wait;
  129. struct iwl_mvm_alive_data alive_data;
  130. const struct fw_img *fw;
  131. int ret, i;
  132. enum iwl_ucode_type old_type = mvm->cur_ucode;
  133. static const u8 alive_cmd[] = { MVM_ALIVE };
  134. mvm->cur_ucode = ucode_type;
  135. fw = iwl_get_ucode_image(mvm, ucode_type);
  136. mvm->ucode_loaded = false;
  137. if (!fw)
  138. return -EINVAL;
  139. iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
  140. alive_cmd, ARRAY_SIZE(alive_cmd),
  141. iwl_alive_fn, &alive_data);
  142. ret = iwl_trans_start_fw(mvm->trans, fw, ucode_type == IWL_UCODE_INIT);
  143. if (ret) {
  144. mvm->cur_ucode = old_type;
  145. iwl_remove_notification(&mvm->notif_wait, &alive_wait);
  146. return ret;
  147. }
  148. /*
  149. * Some things may run in the background now, but we
  150. * just wait for the ALIVE notification here.
  151. */
  152. ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
  153. MVM_UCODE_ALIVE_TIMEOUT);
  154. if (ret) {
  155. mvm->cur_ucode = old_type;
  156. return ret;
  157. }
  158. if (!alive_data.valid) {
  159. IWL_ERR(mvm, "Loaded ucode is not valid!\n");
  160. mvm->cur_ucode = old_type;
  161. return -EIO;
  162. }
  163. iwl_trans_fw_alive(mvm->trans, alive_data.scd_base_addr);
  164. /*
  165. * Note: all the queues are enabled as part of the interface
  166. * initialization, but in firmware restart scenarios they
  167. * could be stopped, so wake them up. In firmware restart,
  168. * mac80211 will have the queues stopped as well until the
  169. * reconfiguration completes. During normal startup, they
  170. * will be empty.
  171. */
  172. for (i = 0; i < IWL_MAX_HW_QUEUES; i++) {
  173. if (i < mvm->first_agg_queue && i != IWL_MVM_CMD_QUEUE)
  174. mvm->queue_to_mac80211[i] = i;
  175. else
  176. mvm->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
  177. atomic_set(&mvm->queue_stop_count[i], 0);
  178. }
  179. mvm->transport_queue_stop = 0;
  180. mvm->ucode_loaded = true;
  181. return 0;
  182. }
  183. static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
  184. {
  185. struct iwl_phy_cfg_cmd phy_cfg_cmd;
  186. enum iwl_ucode_type ucode_type = mvm->cur_ucode;
  187. /* Set parameters */
  188. phy_cfg_cmd.phy_cfg = cpu_to_le32(mvm->fw->phy_config);
  189. phy_cfg_cmd.calib_control.event_trigger =
  190. mvm->fw->default_calib[ucode_type].event_trigger;
  191. phy_cfg_cmd.calib_control.flow_trigger =
  192. mvm->fw->default_calib[ucode_type].flow_trigger;
  193. IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
  194. phy_cfg_cmd.phy_cfg);
  195. return iwl_mvm_send_cmd_pdu(mvm, PHY_CONFIGURATION_CMD, CMD_SYNC,
  196. sizeof(phy_cfg_cmd), &phy_cfg_cmd);
  197. }
  198. int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
  199. {
  200. struct iwl_notification_wait calib_wait;
  201. static const u8 init_complete[] = {
  202. INIT_COMPLETE_NOTIF,
  203. CALIB_RES_NOTIF_PHY_DB
  204. };
  205. int ret;
  206. lockdep_assert_held(&mvm->mutex);
  207. if (mvm->init_ucode_complete)
  208. return 0;
  209. iwl_init_notification_wait(&mvm->notif_wait,
  210. &calib_wait,
  211. init_complete,
  212. ARRAY_SIZE(init_complete),
  213. iwl_wait_phy_db_entry,
  214. mvm->phy_db);
  215. /* Will also start the device */
  216. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
  217. if (ret) {
  218. IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
  219. goto error;
  220. }
  221. ret = iwl_send_bt_prio_tbl(mvm);
  222. if (ret)
  223. goto error;
  224. /* Read the NVM only at driver load time, no need to do this twice */
  225. if (read_nvm) {
  226. /* Read nvm */
  227. ret = iwl_nvm_init(mvm);
  228. if (ret) {
  229. IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
  230. goto error;
  231. }
  232. }
  233. /* In case we read the NVM from external file, load it to the NIC */
  234. if (iwlwifi_mod_params.nvm_file)
  235. iwl_mvm_load_nvm_to_nic(mvm);
  236. ret = iwl_nvm_check_version(mvm->nvm_data, mvm->trans);
  237. WARN_ON(ret);
  238. /*
  239. * abort after reading the nvm in case RF Kill is on, we will complete
  240. * the init seq later when RF kill will switch to off
  241. */
  242. if (iwl_mvm_is_radio_killed(mvm)) {
  243. IWL_DEBUG_RF_KILL(mvm,
  244. "jump over all phy activities due to RF kill\n");
  245. iwl_remove_notification(&mvm->notif_wait, &calib_wait);
  246. return 1;
  247. }
  248. /* Send TX valid antennas before triggering calibrations */
  249. ret = iwl_send_tx_ant_cfg(mvm, iwl_fw_valid_tx_ant(mvm->fw));
  250. if (ret)
  251. goto error;
  252. /*
  253. * Send phy configurations command to init uCode
  254. * to start the 16.0 uCode init image internal calibrations.
  255. */
  256. ret = iwl_send_phy_cfg_cmd(mvm);
  257. if (ret) {
  258. IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
  259. ret);
  260. goto error;
  261. }
  262. /*
  263. * Some things may run in the background now, but we
  264. * just wait for the calibration complete notification.
  265. */
  266. ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
  267. MVM_UCODE_CALIB_TIMEOUT);
  268. if (!ret)
  269. mvm->init_ucode_complete = true;
  270. goto out;
  271. error:
  272. iwl_remove_notification(&mvm->notif_wait, &calib_wait);
  273. out:
  274. if (!iwlmvm_mod_params.init_dbg) {
  275. iwl_trans_stop_device(mvm->trans);
  276. } else if (!mvm->nvm_data) {
  277. /* we want to debug INIT and we have no NVM - fake */
  278. mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
  279. sizeof(struct ieee80211_channel) +
  280. sizeof(struct ieee80211_rate),
  281. GFP_KERNEL);
  282. if (!mvm->nvm_data)
  283. return -ENOMEM;
  284. mvm->nvm_data->valid_rx_ant = 1;
  285. mvm->nvm_data->valid_tx_ant = 1;
  286. mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
  287. mvm->nvm_data->bands[0].n_channels = 1;
  288. mvm->nvm_data->bands[0].n_bitrates = 1;
  289. mvm->nvm_data->bands[0].bitrates =
  290. (void *)mvm->nvm_data->channels + 1;
  291. mvm->nvm_data->bands[0].bitrates->hw_value = 10;
  292. }
  293. return ret;
  294. }
  295. #define UCODE_CALIB_TIMEOUT (2*HZ)
  296. int iwl_mvm_up(struct iwl_mvm *mvm)
  297. {
  298. int ret, i;
  299. struct ieee80211_channel *chan;
  300. struct cfg80211_chan_def chandef;
  301. lockdep_assert_held(&mvm->mutex);
  302. ret = iwl_trans_start_hw(mvm->trans);
  303. if (ret)
  304. return ret;
  305. /*
  306. * If we haven't completed the run of the init ucode during
  307. * module loading, load init ucode now
  308. * (for example, if we were in RFKILL)
  309. */
  310. if (!mvm->init_ucode_complete) {
  311. ret = iwl_run_init_mvm_ucode(mvm, false);
  312. if (ret && !iwlmvm_mod_params.init_dbg) {
  313. IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
  314. /* this can't happen */
  315. if (WARN_ON(ret > 0))
  316. ret = -ERFKILL;
  317. goto error;
  318. }
  319. /* should stop & start HW since that INIT image just loaded */
  320. iwl_trans_stop_hw(mvm->trans, false);
  321. ret = iwl_trans_start_hw(mvm->trans);
  322. if (ret)
  323. return ret;
  324. }
  325. if (iwlmvm_mod_params.init_dbg)
  326. return 0;
  327. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
  328. if (ret) {
  329. IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
  330. goto error;
  331. }
  332. ret = iwl_send_tx_ant_cfg(mvm, iwl_fw_valid_tx_ant(mvm->fw));
  333. if (ret)
  334. goto error;
  335. ret = iwl_send_bt_prio_tbl(mvm);
  336. if (ret)
  337. goto error;
  338. ret = iwl_send_bt_init_conf(mvm);
  339. if (ret)
  340. goto error;
  341. /* Send phy db control command and then phy db calibration*/
  342. ret = iwl_send_phy_db_data(mvm->phy_db);
  343. if (ret)
  344. goto error;
  345. ret = iwl_send_phy_cfg_cmd(mvm);
  346. if (ret)
  347. goto error;
  348. /* init the fw <-> mac80211 STA mapping */
  349. for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
  350. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
  351. /* Add auxiliary station for scanning */
  352. ret = iwl_mvm_add_aux_sta(mvm);
  353. if (ret)
  354. goto error;
  355. /* Add all the PHY contexts */
  356. chan = &mvm->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels[0];
  357. cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
  358. for (i = 0; i < NUM_PHY_CTX; i++) {
  359. /*
  360. * The channel used here isn't relevant as it's
  361. * going to be overwritten in the other flows.
  362. * For now use the first channel we have.
  363. */
  364. ret = iwl_mvm_phy_ctxt_add(mvm, &mvm->phy_ctxts[i],
  365. &chandef, 1, 1);
  366. if (ret)
  367. goto error;
  368. }
  369. ret = iwl_mvm_power_update_device_mode(mvm);
  370. if (ret)
  371. goto error;
  372. IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
  373. return 0;
  374. error:
  375. iwl_trans_stop_device(mvm->trans);
  376. return ret;
  377. }
  378. int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
  379. {
  380. int ret, i;
  381. lockdep_assert_held(&mvm->mutex);
  382. ret = iwl_trans_start_hw(mvm->trans);
  383. if (ret)
  384. return ret;
  385. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
  386. if (ret) {
  387. IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
  388. goto error;
  389. }
  390. ret = iwl_send_tx_ant_cfg(mvm, iwl_fw_valid_tx_ant(mvm->fw));
  391. if (ret)
  392. goto error;
  393. /* Send phy db control command and then phy db calibration*/
  394. ret = iwl_send_phy_db_data(mvm->phy_db);
  395. if (ret)
  396. goto error;
  397. ret = iwl_send_phy_cfg_cmd(mvm);
  398. if (ret)
  399. goto error;
  400. /* init the fw <-> mac80211 STA mapping */
  401. for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
  402. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
  403. /* Add auxiliary station for scanning */
  404. ret = iwl_mvm_add_aux_sta(mvm);
  405. if (ret)
  406. goto error;
  407. return 0;
  408. error:
  409. iwl_trans_stop_device(mvm->trans);
  410. return ret;
  411. }
  412. int iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
  413. struct iwl_rx_cmd_buffer *rxb,
  414. struct iwl_device_cmd *cmd)
  415. {
  416. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  417. struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
  418. u32 flags = le32_to_cpu(card_state_notif->flags);
  419. IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
  420. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  421. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  422. (flags & CT_KILL_CARD_DISABLED) ?
  423. "Reached" : "Not reached");
  424. return 0;
  425. }
  426. int iwl_mvm_rx_radio_ver(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  427. struct iwl_device_cmd *cmd)
  428. {
  429. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  430. struct iwl_radio_version_notif *radio_version = (void *)pkt->data;
  431. /* TODO: what to do with that? */
  432. IWL_DEBUG_INFO(mvm,
  433. "Radio version: flavor: 0x%08x, step 0x%08x, dash 0x%08x\n",
  434. le32_to_cpu(radio_version->radio_flavor),
  435. le32_to_cpu(radio_version->radio_step),
  436. le32_to_cpu(radio_version->radio_dash));
  437. return 0;
  438. }