fw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 LICENSE.GPL.
  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. /* Default calibration values for WkP - set to INIT image w/o running */
  77. static const u8 wkp_calib_values_rx_iq_skew[] = { 0x00, 0x00, 0x01, 0x00 };
  78. static const u8 wkp_calib_values_tx_iq_skew[] = { 0x01, 0x00, 0x00, 0x00 };
  79. struct iwl_calib_default_data {
  80. u16 size;
  81. void *data;
  82. };
  83. #define CALIB_SIZE_N_DATA(_buf) {.size = sizeof(_buf), .data = &_buf}
  84. static const struct iwl_calib_default_data wkp_calib_default_data[12] = {
  85. [9] = CALIB_SIZE_N_DATA(wkp_calib_values_tx_iq_skew),
  86. [11] = CALIB_SIZE_N_DATA(wkp_calib_values_rx_iq_skew),
  87. };
  88. struct iwl_mvm_alive_data {
  89. bool valid;
  90. u32 scd_base_addr;
  91. };
  92. static inline const struct fw_img *
  93. iwl_get_ucode_image(struct iwl_mvm *mvm, enum iwl_ucode_type ucode_type)
  94. {
  95. if (ucode_type >= IWL_UCODE_TYPE_MAX)
  96. return NULL;
  97. return &mvm->fw->img[ucode_type];
  98. }
  99. static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
  100. {
  101. struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
  102. .valid = cpu_to_le32(valid_tx_ant),
  103. };
  104. IWL_DEBUG_HC(mvm, "select valid tx ant: %u\n", valid_tx_ant);
  105. return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, CMD_SYNC,
  106. sizeof(tx_ant_cmd), &tx_ant_cmd);
  107. }
  108. static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
  109. struct iwl_rx_packet *pkt, void *data)
  110. {
  111. struct iwl_mvm *mvm =
  112. container_of(notif_wait, struct iwl_mvm, notif_wait);
  113. struct iwl_mvm_alive_data *alive_data = data;
  114. struct mvm_alive_resp *palive;
  115. palive = (void *)pkt->data;
  116. mvm->error_event_table = le32_to_cpu(palive->error_event_table_ptr);
  117. mvm->log_event_table = le32_to_cpu(palive->log_event_table_ptr);
  118. alive_data->scd_base_addr = le32_to_cpu(palive->scd_base_ptr);
  119. alive_data->valid = le16_to_cpu(palive->status) == IWL_ALIVE_STATUS_OK;
  120. IWL_DEBUG_FW(mvm, "Alive ucode status 0x%04x revision 0x%01X 0x%01X\n",
  121. le16_to_cpu(palive->status), palive->ver_type,
  122. palive->ver_subtype);
  123. return true;
  124. }
  125. static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
  126. struct iwl_rx_packet *pkt, void *data)
  127. {
  128. struct iwl_phy_db *phy_db = data;
  129. if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
  130. WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
  131. return true;
  132. }
  133. WARN_ON(iwl_phy_db_set_section(phy_db, pkt, GFP_ATOMIC));
  134. return false;
  135. }
  136. static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
  137. enum iwl_ucode_type ucode_type)
  138. {
  139. struct iwl_notification_wait alive_wait;
  140. struct iwl_mvm_alive_data alive_data;
  141. const struct fw_img *fw;
  142. int ret, i;
  143. enum iwl_ucode_type old_type = mvm->cur_ucode;
  144. static const u8 alive_cmd[] = { MVM_ALIVE };
  145. mvm->cur_ucode = ucode_type;
  146. fw = iwl_get_ucode_image(mvm, ucode_type);
  147. mvm->ucode_loaded = false;
  148. if (!fw)
  149. return -EINVAL;
  150. iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
  151. alive_cmd, ARRAY_SIZE(alive_cmd),
  152. iwl_alive_fn, &alive_data);
  153. ret = iwl_trans_start_fw(mvm->trans, fw, ucode_type == IWL_UCODE_INIT);
  154. if (ret) {
  155. mvm->cur_ucode = old_type;
  156. iwl_remove_notification(&mvm->notif_wait, &alive_wait);
  157. return ret;
  158. }
  159. /*
  160. * Some things may run in the background now, but we
  161. * just wait for the ALIVE notification here.
  162. */
  163. ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
  164. MVM_UCODE_ALIVE_TIMEOUT);
  165. if (ret) {
  166. mvm->cur_ucode = old_type;
  167. return ret;
  168. }
  169. if (!alive_data.valid) {
  170. IWL_ERR(mvm, "Loaded ucode is not valid!\n");
  171. mvm->cur_ucode = old_type;
  172. return -EIO;
  173. }
  174. iwl_trans_fw_alive(mvm->trans, alive_data.scd_base_addr);
  175. /*
  176. * Note: all the queues are enabled as part of the interface
  177. * initialization, but in firmware restart scenarios they
  178. * could be stopped, so wake them up. In firmware restart,
  179. * mac80211 will have the queues stopped as well until the
  180. * reconfiguration completes. During normal startup, they
  181. * will be empty.
  182. */
  183. for (i = 0; i < IWL_MAX_HW_QUEUES; i++) {
  184. if (i < IWL_MVM_FIRST_AGG_QUEUE && i != IWL_MVM_CMD_QUEUE)
  185. mvm->queue_to_mac80211[i] = i;
  186. else
  187. mvm->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
  188. atomic_set(&mvm->queue_stop_count[i], 0);
  189. }
  190. mvm->transport_queue_stop = 0;
  191. mvm->ucode_loaded = true;
  192. return 0;
  193. }
  194. static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
  195. {
  196. struct iwl_phy_cfg_cmd phy_cfg_cmd;
  197. enum iwl_ucode_type ucode_type = mvm->cur_ucode;
  198. /* Set parameters */
  199. phy_cfg_cmd.phy_cfg = cpu_to_le32(mvm->fw->phy_config);
  200. phy_cfg_cmd.calib_control.event_trigger =
  201. mvm->fw->default_calib[ucode_type].event_trigger;
  202. phy_cfg_cmd.calib_control.flow_trigger =
  203. mvm->fw->default_calib[ucode_type].flow_trigger;
  204. IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
  205. phy_cfg_cmd.phy_cfg);
  206. return iwl_mvm_send_cmd_pdu(mvm, PHY_CONFIGURATION_CMD, CMD_SYNC,
  207. sizeof(phy_cfg_cmd), &phy_cfg_cmd);
  208. }
  209. static int iwl_set_default_calibrations(struct iwl_mvm *mvm)
  210. {
  211. u8 cmd_raw[16]; /* holds the variable size commands */
  212. struct iwl_set_calib_default_cmd *cmd =
  213. (struct iwl_set_calib_default_cmd *)cmd_raw;
  214. int ret, i;
  215. /* Setting default values for calibrations we don't run */
  216. for (i = 0; i < ARRAY_SIZE(wkp_calib_default_data); i++) {
  217. u16 cmd_len;
  218. if (wkp_calib_default_data[i].size == 0)
  219. continue;
  220. memset(cmd_raw, 0, sizeof(cmd_raw));
  221. cmd_len = wkp_calib_default_data[i].size + sizeof(cmd);
  222. cmd->calib_index = cpu_to_le16(i);
  223. cmd->length = cpu_to_le16(wkp_calib_default_data[i].size);
  224. if (WARN_ONCE(cmd_len > sizeof(cmd_raw),
  225. "Need to enlarge cmd_raw to %d\n", cmd_len))
  226. break;
  227. memcpy(cmd->data, wkp_calib_default_data[i].data,
  228. wkp_calib_default_data[i].size);
  229. ret = iwl_mvm_send_cmd_pdu(mvm, SET_CALIB_DEFAULT_CMD, 0,
  230. sizeof(*cmd) +
  231. wkp_calib_default_data[i].size,
  232. cmd);
  233. if (ret)
  234. return ret;
  235. }
  236. return 0;
  237. }
  238. int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
  239. {
  240. struct iwl_notification_wait calib_wait;
  241. static const u8 init_complete[] = {
  242. INIT_COMPLETE_NOTIF,
  243. CALIB_RES_NOTIF_PHY_DB
  244. };
  245. int ret;
  246. lockdep_assert_held(&mvm->mutex);
  247. if (mvm->init_ucode_run)
  248. return 0;
  249. iwl_init_notification_wait(&mvm->notif_wait,
  250. &calib_wait,
  251. init_complete,
  252. ARRAY_SIZE(init_complete),
  253. iwl_wait_phy_db_entry,
  254. mvm->phy_db);
  255. /* Will also start the device */
  256. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
  257. if (ret) {
  258. IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
  259. goto error;
  260. }
  261. if (read_nvm) {
  262. /* Read nvm */
  263. ret = iwl_nvm_init(mvm);
  264. if (ret) {
  265. IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
  266. goto error;
  267. }
  268. }
  269. ret = iwl_nvm_check_version(mvm->nvm_data, mvm->trans);
  270. WARN_ON(ret);
  271. /* Send TX valid antennas before triggering calibrations */
  272. ret = iwl_send_tx_ant_cfg(mvm, mvm->nvm_data->valid_tx_ant);
  273. if (ret)
  274. goto error;
  275. /* WkP doesn't have all calibrations, need to set default values */
  276. if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  277. ret = iwl_set_default_calibrations(mvm);
  278. if (ret)
  279. goto error;
  280. }
  281. /*
  282. * Send phy configurations command to init uCode
  283. * to start the 16.0 uCode init image internal calibrations.
  284. */
  285. ret = iwl_send_phy_cfg_cmd(mvm);
  286. if (ret) {
  287. IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
  288. ret);
  289. goto error;
  290. }
  291. /*
  292. * Some things may run in the background now, but we
  293. * just wait for the calibration complete notification.
  294. */
  295. ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
  296. MVM_UCODE_CALIB_TIMEOUT);
  297. if (!ret)
  298. mvm->init_ucode_run = true;
  299. goto out;
  300. error:
  301. iwl_remove_notification(&mvm->notif_wait, &calib_wait);
  302. out:
  303. if (!iwlmvm_mod_params.init_dbg) {
  304. iwl_trans_stop_device(mvm->trans);
  305. } else if (!mvm->nvm_data) {
  306. /* we want to debug INIT and we have no NVM - fake */
  307. mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
  308. sizeof(struct ieee80211_channel) +
  309. sizeof(struct ieee80211_rate),
  310. GFP_KERNEL);
  311. if (!mvm->nvm_data)
  312. return -ENOMEM;
  313. mvm->nvm_data->valid_rx_ant = 1;
  314. mvm->nvm_data->valid_tx_ant = 1;
  315. mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
  316. mvm->nvm_data->bands[0].n_channels = 1;
  317. mvm->nvm_data->bands[0].n_bitrates = 1;
  318. mvm->nvm_data->bands[0].bitrates =
  319. (void *)mvm->nvm_data->channels + 1;
  320. mvm->nvm_data->bands[0].bitrates->hw_value = 10;
  321. }
  322. return ret;
  323. }
  324. #define UCODE_CALIB_TIMEOUT (2*HZ)
  325. int iwl_mvm_up(struct iwl_mvm *mvm)
  326. {
  327. int ret, i;
  328. lockdep_assert_held(&mvm->mutex);
  329. ret = iwl_trans_start_hw(mvm->trans);
  330. if (ret)
  331. return ret;
  332. /* If we were in RFKILL during module loading, load init ucode now */
  333. if (!mvm->init_ucode_run) {
  334. ret = iwl_run_init_mvm_ucode(mvm, false);
  335. if (ret && !iwlmvm_mod_params.init_dbg) {
  336. IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
  337. goto error;
  338. }
  339. }
  340. if (iwlmvm_mod_params.init_dbg)
  341. return 0;
  342. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
  343. if (ret) {
  344. IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
  345. goto error;
  346. }
  347. ret = iwl_send_tx_ant_cfg(mvm, mvm->nvm_data->valid_tx_ant);
  348. if (ret)
  349. goto error;
  350. /* Send phy db control command and then phy db calibration*/
  351. ret = iwl_send_phy_db_data(mvm->phy_db);
  352. if (ret)
  353. goto error;
  354. ret = iwl_send_phy_cfg_cmd(mvm);
  355. if (ret)
  356. goto error;
  357. /* init the fw <-> mac80211 STA mapping */
  358. for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
  359. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
  360. /* Add auxiliary station for scanning */
  361. ret = iwl_mvm_add_aux_sta(mvm);
  362. if (ret)
  363. goto error;
  364. IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
  365. return 0;
  366. error:
  367. iwl_trans_stop_device(mvm->trans);
  368. return ret;
  369. }
  370. int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
  371. {
  372. int ret, i;
  373. lockdep_assert_held(&mvm->mutex);
  374. ret = iwl_trans_start_hw(mvm->trans);
  375. if (ret)
  376. return ret;
  377. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
  378. if (ret) {
  379. IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
  380. goto error;
  381. }
  382. ret = iwl_send_tx_ant_cfg(mvm, mvm->nvm_data->valid_tx_ant);
  383. if (ret)
  384. goto error;
  385. /* Send phy db control command and then phy db calibration*/
  386. ret = iwl_send_phy_db_data(mvm->phy_db);
  387. if (ret)
  388. goto error;
  389. ret = iwl_send_phy_cfg_cmd(mvm);
  390. if (ret)
  391. goto error;
  392. /* init the fw <-> mac80211 STA mapping */
  393. for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
  394. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
  395. /* Add auxiliary station for scanning */
  396. ret = iwl_mvm_add_aux_sta(mvm);
  397. if (ret)
  398. goto error;
  399. return 0;
  400. error:
  401. iwl_trans_stop_device(mvm->trans);
  402. return ret;
  403. }
  404. int iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
  405. struct iwl_rx_cmd_buffer *rxb,
  406. struct iwl_device_cmd *cmd)
  407. {
  408. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  409. struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
  410. u32 flags = le32_to_cpu(card_state_notif->flags);
  411. IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
  412. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  413. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  414. (flags & CT_KILL_CARD_DISABLED) ?
  415. "Reached" : "Not reached");
  416. return 0;
  417. }
  418. int iwl_mvm_rx_radio_ver(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  419. struct iwl_device_cmd *cmd)
  420. {
  421. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  422. struct iwl_radio_version_notif *radio_version = (void *)pkt->data;
  423. /* TODO: what to do with that? */
  424. IWL_DEBUG_INFO(mvm,
  425. "Radio version: flavor: 0x%08x, step 0x%08x, dash 0x%08x\n",
  426. le32_to_cpu(radio_version->radio_flavor),
  427. le32_to_cpu(radio_version->radio_step),
  428. le32_to_cpu(radio_version->radio_dash));
  429. return 0;
  430. }