fw.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  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. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <linuxwifi@intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <net/mac80211.h>
  66. #include "iwl-trans.h"
  67. #include "iwl-op-mode.h"
  68. #include "iwl-fw.h"
  69. #include "iwl-debug.h"
  70. #include "iwl-csr.h" /* for iwl_mvm_rx_card_state_notif */
  71. #include "iwl-io.h" /* for iwl_mvm_rx_card_state_notif */
  72. #include "iwl-prph.h"
  73. #include "iwl-eeprom-parse.h"
  74. #include "mvm.h"
  75. #include "fw-dbg.h"
  76. #include "iwl-phy-db.h"
  77. #define MVM_UCODE_ALIVE_TIMEOUT HZ
  78. #define MVM_UCODE_CALIB_TIMEOUT (2*HZ)
  79. #define UCODE_VALID_OK cpu_to_le32(0x1)
  80. struct iwl_mvm_alive_data {
  81. bool valid;
  82. u32 scd_base_addr;
  83. };
  84. static inline const struct fw_img *
  85. iwl_get_ucode_image(struct iwl_mvm *mvm, enum iwl_ucode_type ucode_type)
  86. {
  87. if (ucode_type >= IWL_UCODE_TYPE_MAX)
  88. return NULL;
  89. return &mvm->fw->img[ucode_type];
  90. }
  91. static int iwl_send_tx_ant_cfg(struct iwl_mvm *mvm, u8 valid_tx_ant)
  92. {
  93. struct iwl_tx_ant_cfg_cmd tx_ant_cmd = {
  94. .valid = cpu_to_le32(valid_tx_ant),
  95. };
  96. IWL_DEBUG_FW(mvm, "select valid tx ant: %u\n", valid_tx_ant);
  97. return iwl_mvm_send_cmd_pdu(mvm, TX_ANT_CONFIGURATION_CMD, 0,
  98. sizeof(tx_ant_cmd), &tx_ant_cmd);
  99. }
  100. static void iwl_free_fw_paging(struct iwl_mvm *mvm)
  101. {
  102. int i;
  103. if (!mvm->fw_paging_db[0].fw_paging_block)
  104. return;
  105. for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
  106. if (!mvm->fw_paging_db[i].fw_paging_block) {
  107. IWL_DEBUG_FW(mvm,
  108. "Paging: block %d already freed, continue to next page\n",
  109. i);
  110. continue;
  111. }
  112. __free_pages(mvm->fw_paging_db[i].fw_paging_block,
  113. get_order(mvm->fw_paging_db[i].fw_paging_size));
  114. }
  115. kfree(mvm->trans->paging_download_buf);
  116. memset(mvm->fw_paging_db, 0, sizeof(mvm->fw_paging_db));
  117. }
  118. static int iwl_fill_paging_mem(struct iwl_mvm *mvm, const struct fw_img *image)
  119. {
  120. int sec_idx, idx;
  121. u32 offset = 0;
  122. /*
  123. * find where is the paging image start point:
  124. * if CPU2 exist and it's in paging format, then the image looks like:
  125. * CPU1 sections (2 or more)
  126. * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
  127. * CPU2 sections (not paged)
  128. * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
  129. * non paged to CPU2 paging sec
  130. * CPU2 paging CSS
  131. * CPU2 paging image (including instruction and data)
  132. */
  133. for (sec_idx = 0; sec_idx < IWL_UCODE_SECTION_MAX; sec_idx++) {
  134. if (image->sec[sec_idx].offset == PAGING_SEPARATOR_SECTION) {
  135. sec_idx++;
  136. break;
  137. }
  138. }
  139. if (sec_idx >= IWL_UCODE_SECTION_MAX) {
  140. IWL_ERR(mvm, "driver didn't find paging image\n");
  141. iwl_free_fw_paging(mvm);
  142. return -EINVAL;
  143. }
  144. /* copy the CSS block to the dram */
  145. IWL_DEBUG_FW(mvm, "Paging: load paging CSS to FW, sec = %d\n",
  146. sec_idx);
  147. memcpy(page_address(mvm->fw_paging_db[0].fw_paging_block),
  148. image->sec[sec_idx].data,
  149. mvm->fw_paging_db[0].fw_paging_size);
  150. IWL_DEBUG_FW(mvm,
  151. "Paging: copied %d CSS bytes to first block\n",
  152. mvm->fw_paging_db[0].fw_paging_size);
  153. sec_idx++;
  154. /*
  155. * copy the paging blocks to the dram
  156. * loop index start from 1 since that CSS block already copied to dram
  157. * and CSS index is 0.
  158. * loop stop at num_of_paging_blk since that last block is not full.
  159. */
  160. for (idx = 1; idx < mvm->num_of_paging_blk; idx++) {
  161. memcpy(page_address(mvm->fw_paging_db[idx].fw_paging_block),
  162. image->sec[sec_idx].data + offset,
  163. mvm->fw_paging_db[idx].fw_paging_size);
  164. IWL_DEBUG_FW(mvm,
  165. "Paging: copied %d paging bytes to block %d\n",
  166. mvm->fw_paging_db[idx].fw_paging_size,
  167. idx);
  168. offset += mvm->fw_paging_db[idx].fw_paging_size;
  169. }
  170. /* copy the last paging block */
  171. if (mvm->num_of_pages_in_last_blk > 0) {
  172. memcpy(page_address(mvm->fw_paging_db[idx].fw_paging_block),
  173. image->sec[sec_idx].data + offset,
  174. FW_PAGING_SIZE * mvm->num_of_pages_in_last_blk);
  175. IWL_DEBUG_FW(mvm,
  176. "Paging: copied %d pages in the last block %d\n",
  177. mvm->num_of_pages_in_last_blk, idx);
  178. }
  179. return 0;
  180. }
  181. static int iwl_alloc_fw_paging_mem(struct iwl_mvm *mvm,
  182. const struct fw_img *image)
  183. {
  184. struct page *block;
  185. dma_addr_t phys = 0;
  186. int blk_idx = 0;
  187. int order, num_of_pages;
  188. int dma_enabled;
  189. if (mvm->fw_paging_db[0].fw_paging_block)
  190. return 0;
  191. dma_enabled = is_device_dma_capable(mvm->trans->dev);
  192. /* ensure BLOCK_2_EXP_SIZE is power of 2 of PAGING_BLOCK_SIZE */
  193. BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
  194. num_of_pages = image->paging_mem_size / FW_PAGING_SIZE;
  195. mvm->num_of_paging_blk = ((num_of_pages - 1) /
  196. NUM_OF_PAGE_PER_GROUP) + 1;
  197. mvm->num_of_pages_in_last_blk =
  198. num_of_pages -
  199. NUM_OF_PAGE_PER_GROUP * (mvm->num_of_paging_blk - 1);
  200. IWL_DEBUG_FW(mvm,
  201. "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
  202. mvm->num_of_paging_blk,
  203. mvm->num_of_pages_in_last_blk);
  204. /* allocate block of 4Kbytes for paging CSS */
  205. order = get_order(FW_PAGING_SIZE);
  206. block = alloc_pages(GFP_KERNEL, order);
  207. if (!block) {
  208. /* free all the previous pages since we failed */
  209. iwl_free_fw_paging(mvm);
  210. return -ENOMEM;
  211. }
  212. mvm->fw_paging_db[blk_idx].fw_paging_block = block;
  213. mvm->fw_paging_db[blk_idx].fw_paging_size = FW_PAGING_SIZE;
  214. if (dma_enabled) {
  215. phys = dma_map_page(mvm->trans->dev, block, 0,
  216. PAGE_SIZE << order, DMA_BIDIRECTIONAL);
  217. if (dma_mapping_error(mvm->trans->dev, phys)) {
  218. /*
  219. * free the previous pages and the current one since
  220. * we failed to map_page.
  221. */
  222. iwl_free_fw_paging(mvm);
  223. return -ENOMEM;
  224. }
  225. mvm->fw_paging_db[blk_idx].fw_paging_phys = phys;
  226. } else {
  227. mvm->fw_paging_db[blk_idx].fw_paging_phys = PAGING_ADDR_SIG |
  228. blk_idx << BLOCK_2_EXP_SIZE;
  229. }
  230. IWL_DEBUG_FW(mvm,
  231. "Paging: allocated 4K(CSS) bytes (order %d) for firmware paging.\n",
  232. order);
  233. /*
  234. * allocate blocks in dram.
  235. * since that CSS allocated in fw_paging_db[0] loop start from index 1
  236. */
  237. for (blk_idx = 1; blk_idx < mvm->num_of_paging_blk + 1; blk_idx++) {
  238. /* allocate block of PAGING_BLOCK_SIZE (32K) */
  239. order = get_order(PAGING_BLOCK_SIZE);
  240. block = alloc_pages(GFP_KERNEL, order);
  241. if (!block) {
  242. /* free all the previous pages since we failed */
  243. iwl_free_fw_paging(mvm);
  244. return -ENOMEM;
  245. }
  246. mvm->fw_paging_db[blk_idx].fw_paging_block = block;
  247. mvm->fw_paging_db[blk_idx].fw_paging_size = PAGING_BLOCK_SIZE;
  248. if (dma_enabled) {
  249. phys = dma_map_page(mvm->trans->dev, block, 0,
  250. PAGE_SIZE << order,
  251. DMA_BIDIRECTIONAL);
  252. if (dma_mapping_error(mvm->trans->dev, phys)) {
  253. /*
  254. * free the previous pages and the current one
  255. * since we failed to map_page.
  256. */
  257. iwl_free_fw_paging(mvm);
  258. return -ENOMEM;
  259. }
  260. mvm->fw_paging_db[blk_idx].fw_paging_phys = phys;
  261. } else {
  262. mvm->fw_paging_db[blk_idx].fw_paging_phys =
  263. PAGING_ADDR_SIG |
  264. blk_idx << BLOCK_2_EXP_SIZE;
  265. }
  266. IWL_DEBUG_FW(mvm,
  267. "Paging: allocated 32K bytes (order %d) for firmware paging.\n",
  268. order);
  269. }
  270. return 0;
  271. }
  272. static int iwl_save_fw_paging(struct iwl_mvm *mvm,
  273. const struct fw_img *fw)
  274. {
  275. int ret;
  276. ret = iwl_alloc_fw_paging_mem(mvm, fw);
  277. if (ret)
  278. return ret;
  279. return iwl_fill_paging_mem(mvm, fw);
  280. }
  281. /* send paging cmd to FW in case CPU2 has paging image */
  282. static int iwl_send_paging_cmd(struct iwl_mvm *mvm, const struct fw_img *fw)
  283. {
  284. int blk_idx;
  285. __le32 dev_phy_addr;
  286. struct iwl_fw_paging_cmd fw_paging_cmd = {
  287. .flags =
  288. cpu_to_le32(PAGING_CMD_IS_SECURED |
  289. PAGING_CMD_IS_ENABLED |
  290. (mvm->num_of_pages_in_last_blk <<
  291. PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
  292. .block_size = cpu_to_le32(BLOCK_2_EXP_SIZE),
  293. .block_num = cpu_to_le32(mvm->num_of_paging_blk),
  294. };
  295. /* loop for for all paging blocks + CSS block */
  296. for (blk_idx = 0; blk_idx < mvm->num_of_paging_blk + 1; blk_idx++) {
  297. dev_phy_addr =
  298. cpu_to_le32(mvm->fw_paging_db[blk_idx].fw_paging_phys >>
  299. PAGE_2_EXP_SIZE);
  300. fw_paging_cmd.device_phy_addr[blk_idx] = dev_phy_addr;
  301. }
  302. return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(FW_PAGING_BLOCK_CMD,
  303. IWL_ALWAYS_LONG_GROUP, 0),
  304. 0, sizeof(fw_paging_cmd), &fw_paging_cmd);
  305. }
  306. /*
  307. * Send paging item cmd to FW in case CPU2 has paging image
  308. */
  309. static int iwl_trans_get_paging_item(struct iwl_mvm *mvm)
  310. {
  311. int ret;
  312. struct iwl_fw_get_item_cmd fw_get_item_cmd = {
  313. .item_id = cpu_to_le32(IWL_FW_ITEM_ID_PAGING),
  314. };
  315. struct iwl_fw_get_item_resp *item_resp;
  316. struct iwl_host_cmd cmd = {
  317. .id = iwl_cmd_id(FW_GET_ITEM_CMD, IWL_ALWAYS_LONG_GROUP, 0),
  318. .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
  319. .data = { &fw_get_item_cmd, },
  320. };
  321. cmd.len[0] = sizeof(struct iwl_fw_get_item_cmd);
  322. ret = iwl_mvm_send_cmd(mvm, &cmd);
  323. if (ret) {
  324. IWL_ERR(mvm,
  325. "Paging: Failed to send FW_GET_ITEM_CMD cmd (err = %d)\n",
  326. ret);
  327. return ret;
  328. }
  329. item_resp = (void *)((struct iwl_rx_packet *)cmd.resp_pkt)->data;
  330. if (item_resp->item_id != cpu_to_le32(IWL_FW_ITEM_ID_PAGING)) {
  331. IWL_ERR(mvm,
  332. "Paging: got wrong item in FW_GET_ITEM_CMD resp (item_id = %u)\n",
  333. le32_to_cpu(item_resp->item_id));
  334. ret = -EIO;
  335. goto exit;
  336. }
  337. mvm->trans->paging_download_buf = kzalloc(MAX_PAGING_IMAGE_SIZE,
  338. GFP_KERNEL);
  339. if (!mvm->trans->paging_download_buf) {
  340. ret = -ENOMEM;
  341. goto exit;
  342. }
  343. mvm->trans->paging_req_addr = le32_to_cpu(item_resp->item_val);
  344. mvm->trans->paging_db = mvm->fw_paging_db;
  345. IWL_DEBUG_FW(mvm,
  346. "Paging: got paging request address (paging_req_addr 0x%08x)\n",
  347. mvm->trans->paging_req_addr);
  348. exit:
  349. iwl_free_resp(&cmd);
  350. return ret;
  351. }
  352. static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
  353. struct iwl_rx_packet *pkt, void *data)
  354. {
  355. struct iwl_mvm *mvm =
  356. container_of(notif_wait, struct iwl_mvm, notif_wait);
  357. struct iwl_mvm_alive_data *alive_data = data;
  358. struct mvm_alive_resp_ver1 *palive1;
  359. struct mvm_alive_resp_ver2 *palive2;
  360. struct mvm_alive_resp *palive;
  361. if (iwl_rx_packet_payload_len(pkt) == sizeof(*palive1)) {
  362. palive1 = (void *)pkt->data;
  363. mvm->support_umac_log = false;
  364. mvm->error_event_table =
  365. le32_to_cpu(palive1->error_event_table_ptr);
  366. mvm->log_event_table =
  367. le32_to_cpu(palive1->log_event_table_ptr);
  368. alive_data->scd_base_addr = le32_to_cpu(palive1->scd_base_ptr);
  369. alive_data->valid = le16_to_cpu(palive1->status) ==
  370. IWL_ALIVE_STATUS_OK;
  371. IWL_DEBUG_FW(mvm,
  372. "Alive VER1 ucode status 0x%04x revision 0x%01X 0x%01X flags 0x%01X\n",
  373. le16_to_cpu(palive1->status), palive1->ver_type,
  374. palive1->ver_subtype, palive1->flags);
  375. } else if (iwl_rx_packet_payload_len(pkt) == sizeof(*palive2)) {
  376. palive2 = (void *)pkt->data;
  377. mvm->error_event_table =
  378. le32_to_cpu(palive2->error_event_table_ptr);
  379. mvm->log_event_table =
  380. le32_to_cpu(palive2->log_event_table_ptr);
  381. alive_data->scd_base_addr = le32_to_cpu(palive2->scd_base_ptr);
  382. mvm->umac_error_event_table =
  383. le32_to_cpu(palive2->error_info_addr);
  384. mvm->sf_space.addr = le32_to_cpu(palive2->st_fwrd_addr);
  385. mvm->sf_space.size = le32_to_cpu(palive2->st_fwrd_size);
  386. alive_data->valid = le16_to_cpu(palive2->status) ==
  387. IWL_ALIVE_STATUS_OK;
  388. if (mvm->umac_error_event_table)
  389. mvm->support_umac_log = true;
  390. IWL_DEBUG_FW(mvm,
  391. "Alive VER2 ucode status 0x%04x revision 0x%01X 0x%01X flags 0x%01X\n",
  392. le16_to_cpu(palive2->status), palive2->ver_type,
  393. palive2->ver_subtype, palive2->flags);
  394. IWL_DEBUG_FW(mvm,
  395. "UMAC version: Major - 0x%x, Minor - 0x%x\n",
  396. palive2->umac_major, palive2->umac_minor);
  397. } else if (iwl_rx_packet_payload_len(pkt) == sizeof(*palive)) {
  398. palive = (void *)pkt->data;
  399. mvm->error_event_table =
  400. le32_to_cpu(palive->error_event_table_ptr);
  401. mvm->log_event_table =
  402. le32_to_cpu(palive->log_event_table_ptr);
  403. alive_data->scd_base_addr = le32_to_cpu(palive->scd_base_ptr);
  404. mvm->umac_error_event_table =
  405. le32_to_cpu(palive->error_info_addr);
  406. mvm->sf_space.addr = le32_to_cpu(palive->st_fwrd_addr);
  407. mvm->sf_space.size = le32_to_cpu(palive->st_fwrd_size);
  408. alive_data->valid = le16_to_cpu(palive->status) ==
  409. IWL_ALIVE_STATUS_OK;
  410. if (mvm->umac_error_event_table)
  411. mvm->support_umac_log = true;
  412. IWL_DEBUG_FW(mvm,
  413. "Alive VER3 ucode status 0x%04x revision 0x%01X 0x%01X flags 0x%01X\n",
  414. le16_to_cpu(palive->status), palive->ver_type,
  415. palive->ver_subtype, palive->flags);
  416. IWL_DEBUG_FW(mvm,
  417. "UMAC version: Major - 0x%x, Minor - 0x%x\n",
  418. le32_to_cpu(palive->umac_major),
  419. le32_to_cpu(palive->umac_minor));
  420. }
  421. return true;
  422. }
  423. static bool iwl_wait_phy_db_entry(struct iwl_notif_wait_data *notif_wait,
  424. struct iwl_rx_packet *pkt, void *data)
  425. {
  426. struct iwl_phy_db *phy_db = data;
  427. if (pkt->hdr.cmd != CALIB_RES_NOTIF_PHY_DB) {
  428. WARN_ON(pkt->hdr.cmd != INIT_COMPLETE_NOTIF);
  429. return true;
  430. }
  431. WARN_ON(iwl_phy_db_set_section(phy_db, pkt, GFP_ATOMIC));
  432. return false;
  433. }
  434. static int iwl_mvm_load_ucode_wait_alive(struct iwl_mvm *mvm,
  435. enum iwl_ucode_type ucode_type)
  436. {
  437. struct iwl_notification_wait alive_wait;
  438. struct iwl_mvm_alive_data alive_data;
  439. const struct fw_img *fw;
  440. int ret, i;
  441. enum iwl_ucode_type old_type = mvm->cur_ucode;
  442. static const u16 alive_cmd[] = { MVM_ALIVE };
  443. struct iwl_sf_region st_fwrd_space;
  444. if (ucode_type == IWL_UCODE_REGULAR &&
  445. iwl_fw_dbg_conf_usniffer(mvm->fw, FW_DBG_START_FROM_ALIVE))
  446. fw = iwl_get_ucode_image(mvm, IWL_UCODE_REGULAR_USNIFFER);
  447. else
  448. fw = iwl_get_ucode_image(mvm, ucode_type);
  449. if (WARN_ON(!fw))
  450. return -EINVAL;
  451. mvm->cur_ucode = ucode_type;
  452. mvm->ucode_loaded = false;
  453. iwl_init_notification_wait(&mvm->notif_wait, &alive_wait,
  454. alive_cmd, ARRAY_SIZE(alive_cmd),
  455. iwl_alive_fn, &alive_data);
  456. ret = iwl_trans_start_fw(mvm->trans, fw, ucode_type == IWL_UCODE_INIT);
  457. if (ret) {
  458. mvm->cur_ucode = old_type;
  459. iwl_remove_notification(&mvm->notif_wait, &alive_wait);
  460. return ret;
  461. }
  462. /*
  463. * Some things may run in the background now, but we
  464. * just wait for the ALIVE notification here.
  465. */
  466. ret = iwl_wait_notification(&mvm->notif_wait, &alive_wait,
  467. MVM_UCODE_ALIVE_TIMEOUT);
  468. if (ret) {
  469. if (mvm->trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  470. IWL_ERR(mvm,
  471. "SecBoot CPU1 Status: 0x%x, CPU2 Status: 0x%x\n",
  472. iwl_read_prph(mvm->trans, SB_CPU_1_STATUS),
  473. iwl_read_prph(mvm->trans, SB_CPU_2_STATUS));
  474. mvm->cur_ucode = old_type;
  475. return ret;
  476. }
  477. if (!alive_data.valid) {
  478. IWL_ERR(mvm, "Loaded ucode is not valid!\n");
  479. mvm->cur_ucode = old_type;
  480. return -EIO;
  481. }
  482. /*
  483. * update the sdio allocation according to the pointer we get in the
  484. * alive notification.
  485. */
  486. st_fwrd_space.addr = mvm->sf_space.addr;
  487. st_fwrd_space.size = mvm->sf_space.size;
  488. ret = iwl_trans_update_sf(mvm->trans, &st_fwrd_space);
  489. if (ret) {
  490. IWL_ERR(mvm, "Failed to update SF size. ret %d\n", ret);
  491. return ret;
  492. }
  493. iwl_trans_fw_alive(mvm->trans, alive_data.scd_base_addr);
  494. /*
  495. * configure and operate fw paging mechanism.
  496. * driver configures the paging flow only once, CPU2 paging image
  497. * included in the IWL_UCODE_INIT image.
  498. */
  499. if (fw->paging_mem_size) {
  500. /*
  501. * When dma is not enabled, the driver needs to copy / write
  502. * the downloaded / uploaded page to / from the smem.
  503. * This gets the location of the place were the pages are
  504. * stored.
  505. */
  506. if (!is_device_dma_capable(mvm->trans->dev)) {
  507. ret = iwl_trans_get_paging_item(mvm);
  508. if (ret) {
  509. IWL_ERR(mvm, "failed to get FW paging item\n");
  510. return ret;
  511. }
  512. }
  513. ret = iwl_save_fw_paging(mvm, fw);
  514. if (ret) {
  515. IWL_ERR(mvm, "failed to save the FW paging image\n");
  516. return ret;
  517. }
  518. ret = iwl_send_paging_cmd(mvm, fw);
  519. if (ret) {
  520. IWL_ERR(mvm, "failed to send the paging cmd\n");
  521. iwl_free_fw_paging(mvm);
  522. return ret;
  523. }
  524. }
  525. /*
  526. * Note: all the queues are enabled as part of the interface
  527. * initialization, but in firmware restart scenarios they
  528. * could be stopped, so wake them up. In firmware restart,
  529. * mac80211 will have the queues stopped as well until the
  530. * reconfiguration completes. During normal startup, they
  531. * will be empty.
  532. */
  533. memset(&mvm->queue_info, 0, sizeof(mvm->queue_info));
  534. mvm->queue_info[IWL_MVM_CMD_QUEUE].hw_queue_refcount = 1;
  535. for (i = 0; i < IEEE80211_MAX_QUEUES; i++)
  536. atomic_set(&mvm->mac80211_queue_stop_count[i], 0);
  537. mvm->ucode_loaded = true;
  538. return 0;
  539. }
  540. static int iwl_send_phy_cfg_cmd(struct iwl_mvm *mvm)
  541. {
  542. struct iwl_phy_cfg_cmd phy_cfg_cmd;
  543. enum iwl_ucode_type ucode_type = mvm->cur_ucode;
  544. /* Set parameters */
  545. phy_cfg_cmd.phy_cfg = cpu_to_le32(iwl_mvm_get_phy_config(mvm));
  546. phy_cfg_cmd.calib_control.event_trigger =
  547. mvm->fw->default_calib[ucode_type].event_trigger;
  548. phy_cfg_cmd.calib_control.flow_trigger =
  549. mvm->fw->default_calib[ucode_type].flow_trigger;
  550. IWL_DEBUG_INFO(mvm, "Sending Phy CFG command: 0x%x\n",
  551. phy_cfg_cmd.phy_cfg);
  552. return iwl_mvm_send_cmd_pdu(mvm, PHY_CONFIGURATION_CMD, 0,
  553. sizeof(phy_cfg_cmd), &phy_cfg_cmd);
  554. }
  555. int iwl_run_init_mvm_ucode(struct iwl_mvm *mvm, bool read_nvm)
  556. {
  557. struct iwl_notification_wait calib_wait;
  558. static const u16 init_complete[] = {
  559. INIT_COMPLETE_NOTIF,
  560. CALIB_RES_NOTIF_PHY_DB
  561. };
  562. int ret;
  563. lockdep_assert_held(&mvm->mutex);
  564. if (WARN_ON_ONCE(mvm->calibrating))
  565. return 0;
  566. iwl_init_notification_wait(&mvm->notif_wait,
  567. &calib_wait,
  568. init_complete,
  569. ARRAY_SIZE(init_complete),
  570. iwl_wait_phy_db_entry,
  571. mvm->phy_db);
  572. /* Will also start the device */
  573. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_INIT);
  574. if (ret) {
  575. IWL_ERR(mvm, "Failed to start INIT ucode: %d\n", ret);
  576. goto error;
  577. }
  578. ret = iwl_send_bt_init_conf(mvm);
  579. if (ret)
  580. goto error;
  581. /* Read the NVM only at driver load time, no need to do this twice */
  582. if (read_nvm) {
  583. /* Read nvm */
  584. ret = iwl_nvm_init(mvm, true);
  585. if (ret) {
  586. IWL_ERR(mvm, "Failed to read NVM: %d\n", ret);
  587. goto error;
  588. }
  589. }
  590. /* In case we read the NVM from external file, load it to the NIC */
  591. if (mvm->nvm_file_name)
  592. iwl_mvm_load_nvm_to_nic(mvm);
  593. ret = iwl_nvm_check_version(mvm->nvm_data, mvm->trans);
  594. WARN_ON(ret);
  595. /*
  596. * abort after reading the nvm in case RF Kill is on, we will complete
  597. * the init seq later when RF kill will switch to off
  598. */
  599. if (iwl_mvm_is_radio_hw_killed(mvm)) {
  600. IWL_DEBUG_RF_KILL(mvm,
  601. "jump over all phy activities due to RF kill\n");
  602. iwl_remove_notification(&mvm->notif_wait, &calib_wait);
  603. ret = 1;
  604. goto out;
  605. }
  606. mvm->calibrating = true;
  607. /* Send TX valid antennas before triggering calibrations */
  608. ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
  609. if (ret)
  610. goto error;
  611. /*
  612. * Send phy configurations command to init uCode
  613. * to start the 16.0 uCode init image internal calibrations.
  614. */
  615. ret = iwl_send_phy_cfg_cmd(mvm);
  616. if (ret) {
  617. IWL_ERR(mvm, "Failed to run INIT calibrations: %d\n",
  618. ret);
  619. goto error;
  620. }
  621. /*
  622. * Some things may run in the background now, but we
  623. * just wait for the calibration complete notification.
  624. */
  625. ret = iwl_wait_notification(&mvm->notif_wait, &calib_wait,
  626. MVM_UCODE_CALIB_TIMEOUT);
  627. if (ret && iwl_mvm_is_radio_hw_killed(mvm)) {
  628. IWL_DEBUG_RF_KILL(mvm, "RFKILL while calibrating.\n");
  629. ret = 1;
  630. }
  631. goto out;
  632. error:
  633. iwl_remove_notification(&mvm->notif_wait, &calib_wait);
  634. out:
  635. mvm->calibrating = false;
  636. if (iwlmvm_mod_params.init_dbg && !mvm->nvm_data) {
  637. /* we want to debug INIT and we have no NVM - fake */
  638. mvm->nvm_data = kzalloc(sizeof(struct iwl_nvm_data) +
  639. sizeof(struct ieee80211_channel) +
  640. sizeof(struct ieee80211_rate),
  641. GFP_KERNEL);
  642. if (!mvm->nvm_data)
  643. return -ENOMEM;
  644. mvm->nvm_data->bands[0].channels = mvm->nvm_data->channels;
  645. mvm->nvm_data->bands[0].n_channels = 1;
  646. mvm->nvm_data->bands[0].n_bitrates = 1;
  647. mvm->nvm_data->bands[0].bitrates =
  648. (void *)mvm->nvm_data->channels + 1;
  649. mvm->nvm_data->bands[0].bitrates->hw_value = 10;
  650. }
  651. return ret;
  652. }
  653. static void iwl_mvm_get_shared_mem_conf(struct iwl_mvm *mvm)
  654. {
  655. struct iwl_host_cmd cmd = {
  656. .id = SHARED_MEM_CFG,
  657. .flags = CMD_WANT_SKB,
  658. .data = { NULL, },
  659. .len = { 0, },
  660. };
  661. struct iwl_rx_packet *pkt;
  662. struct iwl_shared_mem_cfg *mem_cfg;
  663. u32 i;
  664. lockdep_assert_held(&mvm->mutex);
  665. if (WARN_ON(iwl_mvm_send_cmd(mvm, &cmd)))
  666. return;
  667. pkt = cmd.resp_pkt;
  668. mem_cfg = (void *)pkt->data;
  669. mvm->shared_mem_cfg.shared_mem_addr =
  670. le32_to_cpu(mem_cfg->shared_mem_addr);
  671. mvm->shared_mem_cfg.shared_mem_size =
  672. le32_to_cpu(mem_cfg->shared_mem_size);
  673. mvm->shared_mem_cfg.sample_buff_addr =
  674. le32_to_cpu(mem_cfg->sample_buff_addr);
  675. mvm->shared_mem_cfg.sample_buff_size =
  676. le32_to_cpu(mem_cfg->sample_buff_size);
  677. mvm->shared_mem_cfg.txfifo_addr = le32_to_cpu(mem_cfg->txfifo_addr);
  678. for (i = 0; i < ARRAY_SIZE(mvm->shared_mem_cfg.txfifo_size); i++)
  679. mvm->shared_mem_cfg.txfifo_size[i] =
  680. le32_to_cpu(mem_cfg->txfifo_size[i]);
  681. for (i = 0; i < ARRAY_SIZE(mvm->shared_mem_cfg.rxfifo_size); i++)
  682. mvm->shared_mem_cfg.rxfifo_size[i] =
  683. le32_to_cpu(mem_cfg->rxfifo_size[i]);
  684. mvm->shared_mem_cfg.page_buff_addr =
  685. le32_to_cpu(mem_cfg->page_buff_addr);
  686. mvm->shared_mem_cfg.page_buff_size =
  687. le32_to_cpu(mem_cfg->page_buff_size);
  688. IWL_DEBUG_INFO(mvm, "SHARED MEM CFG: got memory offsets/sizes\n");
  689. iwl_free_resp(&cmd);
  690. }
  691. static int iwl_mvm_config_ltr(struct iwl_mvm *mvm)
  692. {
  693. struct iwl_ltr_config_cmd cmd = {
  694. .flags = cpu_to_le32(LTR_CFG_FLAG_FEATURE_ENABLE),
  695. };
  696. if (!mvm->trans->ltr_enabled)
  697. return 0;
  698. return iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0,
  699. sizeof(cmd), &cmd);
  700. }
  701. int iwl_mvm_up(struct iwl_mvm *mvm)
  702. {
  703. int ret, i;
  704. struct ieee80211_channel *chan;
  705. struct cfg80211_chan_def chandef;
  706. lockdep_assert_held(&mvm->mutex);
  707. ret = iwl_trans_start_hw(mvm->trans);
  708. if (ret)
  709. return ret;
  710. /*
  711. * If we haven't completed the run of the init ucode during
  712. * module loading, load init ucode now
  713. * (for example, if we were in RFKILL)
  714. */
  715. ret = iwl_run_init_mvm_ucode(mvm, false);
  716. if (ret && !iwlmvm_mod_params.init_dbg) {
  717. IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", ret);
  718. /* this can't happen */
  719. if (WARN_ON(ret > 0))
  720. ret = -ERFKILL;
  721. goto error;
  722. }
  723. if (!iwlmvm_mod_params.init_dbg) {
  724. /*
  725. * Stop and start the transport without entering low power
  726. * mode. This will save the state of other components on the
  727. * device that are triggered by the INIT firwmare (MFUART).
  728. */
  729. _iwl_trans_stop_device(mvm->trans, false);
  730. ret = _iwl_trans_start_hw(mvm->trans, false);
  731. if (ret)
  732. goto error;
  733. }
  734. if (iwlmvm_mod_params.init_dbg)
  735. return 0;
  736. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_REGULAR);
  737. if (ret) {
  738. IWL_ERR(mvm, "Failed to start RT ucode: %d\n", ret);
  739. goto error;
  740. }
  741. iwl_mvm_get_shared_mem_conf(mvm);
  742. ret = iwl_mvm_sf_update(mvm, NULL, false);
  743. if (ret)
  744. IWL_ERR(mvm, "Failed to initialize Smart Fifo\n");
  745. mvm->fw_dbg_conf = FW_DBG_INVALID;
  746. /* if we have a destination, assume EARLY START */
  747. if (mvm->fw->dbg_dest_tlv)
  748. mvm->fw_dbg_conf = FW_DBG_START_FROM_ALIVE;
  749. iwl_mvm_start_fw_dbg_conf(mvm, FW_DBG_START_FROM_ALIVE);
  750. ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
  751. if (ret)
  752. goto error;
  753. ret = iwl_send_bt_init_conf(mvm);
  754. if (ret)
  755. goto error;
  756. /* Send phy db control command and then phy db calibration*/
  757. ret = iwl_send_phy_db_data(mvm->phy_db);
  758. if (ret)
  759. goto error;
  760. ret = iwl_send_phy_cfg_cmd(mvm);
  761. if (ret)
  762. goto error;
  763. /* init the fw <-> mac80211 STA mapping */
  764. for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
  765. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
  766. mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
  767. /* reset quota debouncing buffer - 0xff will yield invalid data */
  768. memset(&mvm->last_quota_cmd, 0xff, sizeof(mvm->last_quota_cmd));
  769. /* Add auxiliary station for scanning */
  770. ret = iwl_mvm_add_aux_sta(mvm);
  771. if (ret)
  772. goto error;
  773. /* Add all the PHY contexts */
  774. chan = &mvm->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels[0];
  775. cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
  776. for (i = 0; i < NUM_PHY_CTX; i++) {
  777. /*
  778. * The channel used here isn't relevant as it's
  779. * going to be overwritten in the other flows.
  780. * For now use the first channel we have.
  781. */
  782. ret = iwl_mvm_phy_ctxt_add(mvm, &mvm->phy_ctxts[i],
  783. &chandef, 1, 1);
  784. if (ret)
  785. goto error;
  786. }
  787. /* Initialize tx backoffs to the minimal possible */
  788. iwl_mvm_tt_tx_backoff(mvm, 0);
  789. WARN_ON(iwl_mvm_config_ltr(mvm));
  790. ret = iwl_mvm_power_update_device(mvm);
  791. if (ret)
  792. goto error;
  793. /*
  794. * RTNL is not taken during Ct-kill, but we don't need to scan/Tx
  795. * anyway, so don't init MCC.
  796. */
  797. if (!test_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status)) {
  798. ret = iwl_mvm_init_mcc(mvm);
  799. if (ret)
  800. goto error;
  801. }
  802. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
  803. ret = iwl_mvm_config_scan(mvm);
  804. if (ret)
  805. goto error;
  806. }
  807. if (iwl_mvm_is_csum_supported(mvm) &&
  808. mvm->cfg->features & NETIF_F_RXCSUM)
  809. iwl_trans_write_prph(mvm->trans, RX_EN_CSUM, 0x3);
  810. /* allow FW/transport low power modes if not during restart */
  811. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  812. iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
  813. IWL_DEBUG_INFO(mvm, "RT uCode started.\n");
  814. return 0;
  815. error:
  816. iwl_trans_stop_device(mvm->trans);
  817. return ret;
  818. }
  819. int iwl_mvm_load_d3_fw(struct iwl_mvm *mvm)
  820. {
  821. int ret, i;
  822. lockdep_assert_held(&mvm->mutex);
  823. ret = iwl_trans_start_hw(mvm->trans);
  824. if (ret)
  825. return ret;
  826. ret = iwl_mvm_load_ucode_wait_alive(mvm, IWL_UCODE_WOWLAN);
  827. if (ret) {
  828. IWL_ERR(mvm, "Failed to start WoWLAN firmware: %d\n", ret);
  829. goto error;
  830. }
  831. ret = iwl_send_tx_ant_cfg(mvm, iwl_mvm_get_valid_tx_ant(mvm));
  832. if (ret)
  833. goto error;
  834. /* Send phy db control command and then phy db calibration*/
  835. ret = iwl_send_phy_db_data(mvm->phy_db);
  836. if (ret)
  837. goto error;
  838. ret = iwl_send_phy_cfg_cmd(mvm);
  839. if (ret)
  840. goto error;
  841. /* init the fw <-> mac80211 STA mapping */
  842. for (i = 0; i < IWL_MVM_STATION_COUNT; i++)
  843. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[i], NULL);
  844. /* Add auxiliary station for scanning */
  845. ret = iwl_mvm_add_aux_sta(mvm);
  846. if (ret)
  847. goto error;
  848. return 0;
  849. error:
  850. iwl_trans_stop_device(mvm->trans);
  851. return ret;
  852. }
  853. void iwl_mvm_rx_card_state_notif(struct iwl_mvm *mvm,
  854. struct iwl_rx_cmd_buffer *rxb)
  855. {
  856. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  857. struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
  858. u32 flags = le32_to_cpu(card_state_notif->flags);
  859. IWL_DEBUG_RF_KILL(mvm, "Card state received: HW:%s SW:%s CT:%s\n",
  860. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  861. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  862. (flags & CT_KILL_CARD_DISABLED) ?
  863. "Reached" : "Not reached");
  864. }
  865. void iwl_mvm_rx_mfuart_notif(struct iwl_mvm *mvm,
  866. struct iwl_rx_cmd_buffer *rxb)
  867. {
  868. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  869. struct iwl_mfuart_load_notif *mfuart_notif = (void *)pkt->data;
  870. IWL_DEBUG_INFO(mvm,
  871. "MFUART: installed ver: 0x%08x, external ver: 0x%08x, status: 0x%08x, duration: 0x%08x\n",
  872. le32_to_cpu(mfuart_notif->installed_ver),
  873. le32_to_cpu(mfuart_notif->external_ver),
  874. le32_to_cpu(mfuart_notif->status),
  875. le32_to_cpu(mfuart_notif->duration));
  876. }