utils.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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 - 2014 Intel Mobile Communications GmbH
  10. * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  37. * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <net/mac80211.h>
  68. #include "iwl-debug.h"
  69. #include "iwl-io.h"
  70. #include "iwl-prph.h"
  71. #include "fw-dbg.h"
  72. #include "mvm.h"
  73. #include "fw-api-rs.h"
  74. /*
  75. * Will return 0 even if the cmd failed when RFKILL is asserted unless
  76. * CMD_WANT_SKB is set in cmd->flags.
  77. */
  78. int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
  79. {
  80. int ret;
  81. #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
  82. if (WARN_ON(mvm->d3_test_active))
  83. return -EIO;
  84. #endif
  85. /*
  86. * Synchronous commands from this op-mode must hold
  87. * the mutex, this ensures we don't try to send two
  88. * (or more) synchronous commands at a time.
  89. */
  90. if (!(cmd->flags & CMD_ASYNC)) {
  91. lockdep_assert_held(&mvm->mutex);
  92. if (!(cmd->flags & CMD_SEND_IN_IDLE))
  93. iwl_mvm_ref(mvm, IWL_MVM_REF_SENDING_CMD);
  94. }
  95. ret = iwl_trans_send_cmd(mvm->trans, cmd);
  96. if (!(cmd->flags & (CMD_ASYNC | CMD_SEND_IN_IDLE)))
  97. iwl_mvm_unref(mvm, IWL_MVM_REF_SENDING_CMD);
  98. /*
  99. * If the caller wants the SKB, then don't hide any problems, the
  100. * caller might access the response buffer which will be NULL if
  101. * the command failed.
  102. */
  103. if (cmd->flags & CMD_WANT_SKB)
  104. return ret;
  105. /* Silently ignore failures if RFKILL is asserted */
  106. if (!ret || ret == -ERFKILL)
  107. return 0;
  108. return ret;
  109. }
  110. int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
  111. u32 flags, u16 len, const void *data)
  112. {
  113. struct iwl_host_cmd cmd = {
  114. .id = id,
  115. .len = { len, },
  116. .data = { data, },
  117. .flags = flags,
  118. };
  119. return iwl_mvm_send_cmd(mvm, &cmd);
  120. }
  121. /*
  122. * We assume that the caller set the status to the success value
  123. */
  124. int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
  125. u32 *status)
  126. {
  127. struct iwl_rx_packet *pkt;
  128. struct iwl_cmd_response *resp;
  129. int ret, resp_len;
  130. lockdep_assert_held(&mvm->mutex);
  131. #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
  132. if (WARN_ON(mvm->d3_test_active))
  133. return -EIO;
  134. #endif
  135. /*
  136. * Only synchronous commands can wait for status,
  137. * we use WANT_SKB so the caller can't.
  138. */
  139. if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
  140. "cmd flags %x", cmd->flags))
  141. return -EINVAL;
  142. cmd->flags |= CMD_WANT_SKB;
  143. ret = iwl_trans_send_cmd(mvm->trans, cmd);
  144. if (ret == -ERFKILL) {
  145. /*
  146. * The command failed because of RFKILL, don't update
  147. * the status, leave it as success and return 0.
  148. */
  149. return 0;
  150. } else if (ret) {
  151. return ret;
  152. }
  153. pkt = cmd->resp_pkt;
  154. /* Can happen if RFKILL is asserted */
  155. if (!pkt) {
  156. ret = 0;
  157. goto out_free_resp;
  158. }
  159. resp_len = iwl_rx_packet_payload_len(pkt);
  160. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  161. ret = -EIO;
  162. goto out_free_resp;
  163. }
  164. resp = (void *)pkt->data;
  165. *status = le32_to_cpu(resp->status);
  166. out_free_resp:
  167. iwl_free_resp(cmd);
  168. return ret;
  169. }
  170. /*
  171. * We assume that the caller set the status to the sucess value
  172. */
  173. int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
  174. const void *data, u32 *status)
  175. {
  176. struct iwl_host_cmd cmd = {
  177. .id = id,
  178. .len = { len, },
  179. .data = { data, },
  180. };
  181. return iwl_mvm_send_cmd_status(mvm, &cmd, status);
  182. }
  183. #define IWL_DECLARE_RATE_INFO(r) \
  184. [IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
  185. /*
  186. * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
  187. */
  188. static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
  189. IWL_DECLARE_RATE_INFO(1),
  190. IWL_DECLARE_RATE_INFO(2),
  191. IWL_DECLARE_RATE_INFO(5),
  192. IWL_DECLARE_RATE_INFO(11),
  193. IWL_DECLARE_RATE_INFO(6),
  194. IWL_DECLARE_RATE_INFO(9),
  195. IWL_DECLARE_RATE_INFO(12),
  196. IWL_DECLARE_RATE_INFO(18),
  197. IWL_DECLARE_RATE_INFO(24),
  198. IWL_DECLARE_RATE_INFO(36),
  199. IWL_DECLARE_RATE_INFO(48),
  200. IWL_DECLARE_RATE_INFO(54),
  201. };
  202. int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
  203. enum nl80211_band band)
  204. {
  205. int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
  206. int idx;
  207. int band_offset = 0;
  208. /* Legacy rate format, search for match in table */
  209. if (band == NL80211_BAND_5GHZ)
  210. band_offset = IWL_FIRST_OFDM_RATE;
  211. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  212. if (fw_rate_idx_to_plcp[idx] == rate)
  213. return idx - band_offset;
  214. return -1;
  215. }
  216. u8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
  217. {
  218. /* Get PLCP rate for tx_cmd->rate_n_flags */
  219. return fw_rate_idx_to_plcp[rate_idx];
  220. }
  221. void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  222. {
  223. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  224. struct iwl_error_resp *err_resp = (void *)pkt->data;
  225. IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
  226. le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
  227. IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
  228. le16_to_cpu(err_resp->bad_cmd_seq_num),
  229. le32_to_cpu(err_resp->error_service));
  230. IWL_ERR(mvm, "FW Error notification: timestamp 0x%16llX\n",
  231. le64_to_cpu(err_resp->timestamp));
  232. }
  233. /*
  234. * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
  235. * The parameter should also be a combination of ANT_[ABC].
  236. */
  237. u8 first_antenna(u8 mask)
  238. {
  239. BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
  240. if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
  241. return BIT(0);
  242. return BIT(ffs(mask) - 1);
  243. }
  244. /*
  245. * Toggles between TX antennas to send the probe request on.
  246. * Receives the bitmask of valid TX antennas and the *index* used
  247. * for the last TX, and returns the next valid *index* to use.
  248. * In order to set it in the tx_cmd, must do BIT(idx).
  249. */
  250. u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
  251. {
  252. u8 ind = last_idx;
  253. int i;
  254. for (i = 0; i < RATE_MCS_ANT_NUM; i++) {
  255. ind = (ind + 1) % RATE_MCS_ANT_NUM;
  256. if (valid & BIT(ind))
  257. return ind;
  258. }
  259. WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
  260. return last_idx;
  261. }
  262. static const struct {
  263. const char *name;
  264. u8 num;
  265. } advanced_lookup[] = {
  266. { "NMI_INTERRUPT_WDG", 0x34 },
  267. { "SYSASSERT", 0x35 },
  268. { "UCODE_VERSION_MISMATCH", 0x37 },
  269. { "BAD_COMMAND", 0x38 },
  270. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  271. { "FATAL_ERROR", 0x3D },
  272. { "NMI_TRM_HW_ERR", 0x46 },
  273. { "NMI_INTERRUPT_TRM", 0x4C },
  274. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  275. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  276. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  277. { "NMI_INTERRUPT_HOST", 0x66 },
  278. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  279. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  280. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  281. { "ADVANCED_SYSASSERT", 0 },
  282. };
  283. static const char *desc_lookup(u32 num)
  284. {
  285. int i;
  286. for (i = 0; i < ARRAY_SIZE(advanced_lookup) - 1; i++)
  287. if (advanced_lookup[i].num == num)
  288. return advanced_lookup[i].name;
  289. /* No entry matches 'num', so it is the last: ADVANCED_SYSASSERT */
  290. return advanced_lookup[i].name;
  291. }
  292. /*
  293. * Note: This structure is read from the device with IO accesses,
  294. * and the reading already does the endian conversion. As it is
  295. * read with u32-sized accesses, any members with a different size
  296. * need to be ordered correctly though!
  297. */
  298. struct iwl_error_event_table_v1 {
  299. u32 valid; /* (nonzero) valid, (0) log is empty */
  300. u32 error_id; /* type of error */
  301. u32 pc; /* program counter */
  302. u32 blink1; /* branch link */
  303. u32 blink2; /* branch link */
  304. u32 ilink1; /* interrupt link */
  305. u32 ilink2; /* interrupt link */
  306. u32 data1; /* error-specific data */
  307. u32 data2; /* error-specific data */
  308. u32 data3; /* error-specific data */
  309. u32 bcon_time; /* beacon timer */
  310. u32 tsf_low; /* network timestamp function timer */
  311. u32 tsf_hi; /* network timestamp function timer */
  312. u32 gp1; /* GP1 timer register */
  313. u32 gp2; /* GP2 timer register */
  314. u32 gp3; /* GP3 timer register */
  315. u32 ucode_ver; /* uCode version */
  316. u32 hw_ver; /* HW Silicon version */
  317. u32 brd_ver; /* HW board version */
  318. u32 log_pc; /* log program counter */
  319. u32 frame_ptr; /* frame pointer */
  320. u32 stack_ptr; /* stack pointer */
  321. u32 hcmd; /* last host command header */
  322. u32 isr0; /* isr status register LMPM_NIC_ISR0:
  323. * rxtx_flag */
  324. u32 isr1; /* isr status register LMPM_NIC_ISR1:
  325. * host_flag */
  326. u32 isr2; /* isr status register LMPM_NIC_ISR2:
  327. * enc_flag */
  328. u32 isr3; /* isr status register LMPM_NIC_ISR3:
  329. * time_flag */
  330. u32 isr4; /* isr status register LMPM_NIC_ISR4:
  331. * wico interrupt */
  332. u32 isr_pref; /* isr status register LMPM_NIC_PREF_STAT */
  333. u32 wait_event; /* wait event() caller address */
  334. u32 l2p_control; /* L2pControlField */
  335. u32 l2p_duration; /* L2pDurationField */
  336. u32 l2p_mhvalid; /* L2pMhValidBits */
  337. u32 l2p_addr_match; /* L2pAddrMatchStat */
  338. u32 lmpm_pmg_sel; /* indicate which clocks are turned on
  339. * (LMPM_PMG_SEL) */
  340. u32 u_timestamp; /* indicate when the date and time of the
  341. * compilation */
  342. u32 flow_handler; /* FH read/write pointers, RX credit */
  343. } __packed /* LOG_ERROR_TABLE_API_S_VER_1 */;
  344. struct iwl_error_event_table {
  345. u32 valid; /* (nonzero) valid, (0) log is empty */
  346. u32 error_id; /* type of error */
  347. u32 trm_hw_status0; /* TRM HW status */
  348. u32 trm_hw_status1; /* TRM HW status */
  349. u32 blink2; /* branch link */
  350. u32 ilink1; /* interrupt link */
  351. u32 ilink2; /* interrupt link */
  352. u32 data1; /* error-specific data */
  353. u32 data2; /* error-specific data */
  354. u32 data3; /* error-specific data */
  355. u32 bcon_time; /* beacon timer */
  356. u32 tsf_low; /* network timestamp function timer */
  357. u32 tsf_hi; /* network timestamp function timer */
  358. u32 gp1; /* GP1 timer register */
  359. u32 gp2; /* GP2 timer register */
  360. u32 fw_rev_type; /* firmware revision type */
  361. u32 major; /* uCode version major */
  362. u32 minor; /* uCode version minor */
  363. u32 hw_ver; /* HW Silicon version */
  364. u32 brd_ver; /* HW board version */
  365. u32 log_pc; /* log program counter */
  366. u32 frame_ptr; /* frame pointer */
  367. u32 stack_ptr; /* stack pointer */
  368. u32 hcmd; /* last host command header */
  369. u32 isr0; /* isr status register LMPM_NIC_ISR0:
  370. * rxtx_flag */
  371. u32 isr1; /* isr status register LMPM_NIC_ISR1:
  372. * host_flag */
  373. u32 isr2; /* isr status register LMPM_NIC_ISR2:
  374. * enc_flag */
  375. u32 isr3; /* isr status register LMPM_NIC_ISR3:
  376. * time_flag */
  377. u32 isr4; /* isr status register LMPM_NIC_ISR4:
  378. * wico interrupt */
  379. u32 last_cmd_id; /* last HCMD id handled by the firmware */
  380. u32 wait_event; /* wait event() caller address */
  381. u32 l2p_control; /* L2pControlField */
  382. u32 l2p_duration; /* L2pDurationField */
  383. u32 l2p_mhvalid; /* L2pMhValidBits */
  384. u32 l2p_addr_match; /* L2pAddrMatchStat */
  385. u32 lmpm_pmg_sel; /* indicate which clocks are turned on
  386. * (LMPM_PMG_SEL) */
  387. u32 u_timestamp; /* indicate when the date and time of the
  388. * compilation */
  389. u32 flow_handler; /* FH read/write pointers, RX credit */
  390. } __packed /* LOG_ERROR_TABLE_API_S_VER_3 */;
  391. /*
  392. * UMAC error struct - relevant starting from family 8000 chip.
  393. * Note: This structure is read from the device with IO accesses,
  394. * and the reading already does the endian conversion. As it is
  395. * read with u32-sized accesses, any members with a different size
  396. * need to be ordered correctly though!
  397. */
  398. struct iwl_umac_error_event_table {
  399. u32 valid; /* (nonzero) valid, (0) log is empty */
  400. u32 error_id; /* type of error */
  401. u32 blink1; /* branch link */
  402. u32 blink2; /* branch link */
  403. u32 ilink1; /* interrupt link */
  404. u32 ilink2; /* interrupt link */
  405. u32 data1; /* error-specific data */
  406. u32 data2; /* error-specific data */
  407. u32 data3; /* error-specific data */
  408. u32 umac_major;
  409. u32 umac_minor;
  410. u32 frame_pointer; /* core register 27*/
  411. u32 stack_pointer; /* core register 28 */
  412. u32 cmd_header; /* latest host cmd sent to UMAC */
  413. u32 nic_isr_pref; /* ISR status register */
  414. } __packed;
  415. #define ERROR_START_OFFSET (1 * sizeof(u32))
  416. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  417. static void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
  418. {
  419. struct iwl_trans *trans = mvm->trans;
  420. struct iwl_umac_error_event_table table;
  421. u32 base;
  422. base = mvm->umac_error_event_table;
  423. if (base < 0x800000) {
  424. IWL_ERR(mvm,
  425. "Not valid error log pointer 0x%08X for %s uCode\n",
  426. base,
  427. (mvm->cur_ucode == IWL_UCODE_INIT)
  428. ? "Init" : "RT");
  429. return;
  430. }
  431. iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
  432. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  433. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  434. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  435. mvm->status, table.valid);
  436. }
  437. IWL_ERR(mvm, "0x%08X | %s\n", table.error_id,
  438. desc_lookup(table.error_id));
  439. IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
  440. IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
  441. IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
  442. IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
  443. IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
  444. IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
  445. IWL_ERR(mvm, "0x%08X | umac data3\n", table.data3);
  446. IWL_ERR(mvm, "0x%08X | umac major\n", table.umac_major);
  447. IWL_ERR(mvm, "0x%08X | umac minor\n", table.umac_minor);
  448. IWL_ERR(mvm, "0x%08X | frame pointer\n", table.frame_pointer);
  449. IWL_ERR(mvm, "0x%08X | stack pointer\n", table.stack_pointer);
  450. IWL_ERR(mvm, "0x%08X | last host cmd\n", table.cmd_header);
  451. IWL_ERR(mvm, "0x%08X | isr status reg\n", table.nic_isr_pref);
  452. }
  453. static void iwl_mvm_dump_lmac_error_log(struct iwl_mvm *mvm, u32 base)
  454. {
  455. struct iwl_trans *trans = mvm->trans;
  456. struct iwl_error_event_table table;
  457. if (mvm->cur_ucode == IWL_UCODE_INIT) {
  458. if (!base)
  459. base = mvm->fw->init_errlog_ptr;
  460. } else {
  461. if (!base)
  462. base = mvm->fw->inst_errlog_ptr;
  463. }
  464. if (base < 0x400000) {
  465. IWL_ERR(mvm,
  466. "Not valid error log pointer 0x%08X for %s uCode\n",
  467. base,
  468. (mvm->cur_ucode == IWL_UCODE_INIT)
  469. ? "Init" : "RT");
  470. return;
  471. }
  472. iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
  473. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  474. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  475. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  476. mvm->status, table.valid);
  477. }
  478. /* Do not change this output - scripts rely on it */
  479. IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
  480. trace_iwlwifi_dev_ucode_error(trans->dev, table.error_id, table.tsf_low,
  481. table.data1, table.data2, table.data3,
  482. table.blink2, table.ilink1,
  483. table.ilink2, table.bcon_time, table.gp1,
  484. table.gp2, table.fw_rev_type, table.major,
  485. table.minor, table.hw_ver, table.brd_ver);
  486. IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
  487. desc_lookup(table.error_id));
  488. IWL_ERR(mvm, "0x%08X | trm_hw_status0\n", table.trm_hw_status0);
  489. IWL_ERR(mvm, "0x%08X | trm_hw_status1\n", table.trm_hw_status1);
  490. IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
  491. IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
  492. IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
  493. IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
  494. IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
  495. IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
  496. IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
  497. IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
  498. IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
  499. IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
  500. IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
  501. IWL_ERR(mvm, "0x%08X | uCode revision type\n", table.fw_rev_type);
  502. IWL_ERR(mvm, "0x%08X | uCode version major\n", table.major);
  503. IWL_ERR(mvm, "0x%08X | uCode version minor\n", table.minor);
  504. IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
  505. IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
  506. IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
  507. IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
  508. IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
  509. IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
  510. IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
  511. IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
  512. IWL_ERR(mvm, "0x%08X | last cmd Id\n", table.last_cmd_id);
  513. IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
  514. IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
  515. IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
  516. IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
  517. IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
  518. IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
  519. IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
  520. IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
  521. }
  522. void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
  523. {
  524. iwl_mvm_dump_lmac_error_log(mvm, mvm->error_event_table[0]);
  525. if (mvm->error_event_table[1])
  526. iwl_mvm_dump_lmac_error_log(mvm, mvm->error_event_table[1]);
  527. if (mvm->support_umac_log)
  528. iwl_mvm_dump_umac_error_log(mvm);
  529. }
  530. int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id, u8 minq, u8 maxq)
  531. {
  532. int i;
  533. lockdep_assert_held(&mvm->queue_info_lock);
  534. /* Start by looking for a free queue */
  535. for (i = minq; i <= maxq; i++)
  536. if (mvm->queue_info[i].hw_queue_refcount == 0 &&
  537. mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
  538. return i;
  539. if (iwl_mvm_has_new_tx_api(mvm))
  540. return -ENOSPC;
  541. /*
  542. * If no free queue found - settle for an inactive one to reconfigure
  543. * Make sure that the inactive queue either already belongs to this STA,
  544. * or that if it belongs to another one - it isn't the reserved queue
  545. */
  546. for (i = minq; i <= maxq; i++)
  547. if (mvm->queue_info[i].status == IWL_MVM_QUEUE_INACTIVE &&
  548. (sta_id == mvm->queue_info[i].ra_sta_id ||
  549. !mvm->queue_info[i].reserved))
  550. return i;
  551. return -ENOSPC;
  552. }
  553. int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, int sta_id,
  554. int tid, int frame_limit, u16 ssn)
  555. {
  556. struct iwl_scd_txq_cfg_cmd cmd = {
  557. .scd_queue = queue,
  558. .action = SCD_CFG_ENABLE_QUEUE,
  559. .window = frame_limit,
  560. .sta_id = sta_id,
  561. .ssn = cpu_to_le16(ssn),
  562. .tx_fifo = fifo,
  563. .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
  564. queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
  565. .tid = tid,
  566. };
  567. int ret;
  568. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  569. return -EINVAL;
  570. spin_lock_bh(&mvm->queue_info_lock);
  571. if (WARN(mvm->queue_info[queue].hw_queue_refcount == 0,
  572. "Trying to reconfig unallocated queue %d\n", queue)) {
  573. spin_unlock_bh(&mvm->queue_info_lock);
  574. return -ENXIO;
  575. }
  576. spin_unlock_bh(&mvm->queue_info_lock);
  577. IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
  578. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
  579. WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
  580. queue, fifo, ret);
  581. return ret;
  582. }
  583. static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm, int queue,
  584. int mac80211_queue, u8 sta_id, u8 tid)
  585. {
  586. bool enable_queue = true;
  587. spin_lock_bh(&mvm->queue_info_lock);
  588. /* Make sure this TID isn't already enabled */
  589. if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
  590. spin_unlock_bh(&mvm->queue_info_lock);
  591. IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
  592. queue, tid);
  593. return false;
  594. }
  595. /* Update mappings and refcounts */
  596. if (mvm->queue_info[queue].hw_queue_refcount > 0)
  597. enable_queue = false;
  598. mvm->queue_info[queue].hw_queue_to_mac80211 |= BIT(mac80211_queue);
  599. mvm->queue_info[queue].hw_queue_refcount++;
  600. mvm->queue_info[queue].tid_bitmap |= BIT(tid);
  601. mvm->queue_info[queue].ra_sta_id = sta_id;
  602. if (enable_queue) {
  603. if (tid != IWL_MAX_TID_COUNT)
  604. mvm->queue_info[queue].mac80211_ac =
  605. tid_to_mac80211_ac[tid];
  606. else
  607. mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
  608. mvm->queue_info[queue].txq_tid = tid;
  609. }
  610. IWL_DEBUG_TX_QUEUES(mvm,
  611. "Enabling TXQ #%d refcount=%d (mac80211 map:0x%x)\n",
  612. queue, mvm->queue_info[queue].hw_queue_refcount,
  613. mvm->queue_info[queue].hw_queue_to_mac80211);
  614. spin_unlock_bh(&mvm->queue_info_lock);
  615. return enable_queue;
  616. }
  617. void iwl_mvm_enable_txq(struct iwl_mvm *mvm, int queue, int mac80211_queue,
  618. u16 ssn, const struct iwl_trans_txq_scd_cfg *cfg,
  619. unsigned int wdg_timeout)
  620. {
  621. /* Send the enabling command if we need to */
  622. if (iwl_mvm_update_txq_mapping(mvm, queue, mac80211_queue,
  623. cfg->sta_id, cfg->tid)) {
  624. struct iwl_tx_queue_cfg_cmd cmd = {
  625. .scd_queue = queue,
  626. .action = SCD_CFG_ENABLE_QUEUE,
  627. .window = cfg->frame_limit,
  628. .sta_id = cfg->sta_id,
  629. .ssn = cpu_to_le16(ssn),
  630. .tx_fifo = cfg->fifo,
  631. .aggregate = cfg->aggregate,
  632. .tid = cfg->tid,
  633. };
  634. if (iwl_mvm_has_new_tx_api(mvm)) {
  635. if (cmd.tid == IWL_MAX_TID_COUNT)
  636. cmd.tid = IWL_MGMT_TID;
  637. iwl_trans_txq_alloc(mvm->trans, (void *)&cmd,
  638. SCD_QUEUE_CFG, wdg_timeout);
  639. return;
  640. }
  641. iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL,
  642. wdg_timeout);
  643. WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0,
  644. sizeof(struct iwl_scd_txq_cfg_cmd),
  645. &cmd),
  646. "Failed to configure queue %d on FIFO %d\n", queue,
  647. cfg->fifo);
  648. }
  649. }
  650. int iwl_mvm_disable_txq(struct iwl_mvm *mvm, int queue, int mac80211_queue,
  651. u8 tid, u8 flags)
  652. {
  653. struct iwl_tx_queue_cfg_cmd cmd = {
  654. .scd_queue = queue,
  655. .action = SCD_CFG_DISABLE_QUEUE,
  656. };
  657. bool remove_mac_queue = true;
  658. int ret;
  659. spin_lock_bh(&mvm->queue_info_lock);
  660. if (WARN_ON(mvm->queue_info[queue].hw_queue_refcount == 0)) {
  661. spin_unlock_bh(&mvm->queue_info_lock);
  662. return 0;
  663. }
  664. mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
  665. /*
  666. * If there is another TID with the same AC - don't remove the MAC queue
  667. * from the mapping
  668. */
  669. if (tid < IWL_MAX_TID_COUNT) {
  670. unsigned long tid_bitmap =
  671. mvm->queue_info[queue].tid_bitmap;
  672. int ac = tid_to_mac80211_ac[tid];
  673. int i;
  674. for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT) {
  675. if (tid_to_mac80211_ac[i] == ac)
  676. remove_mac_queue = false;
  677. }
  678. }
  679. if (remove_mac_queue)
  680. mvm->queue_info[queue].hw_queue_to_mac80211 &=
  681. ~BIT(mac80211_queue);
  682. mvm->queue_info[queue].hw_queue_refcount--;
  683. cmd.action = mvm->queue_info[queue].hw_queue_refcount ?
  684. SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
  685. if (cmd.action == SCD_CFG_DISABLE_QUEUE)
  686. mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
  687. IWL_DEBUG_TX_QUEUES(mvm,
  688. "Disabling TXQ #%d refcount=%d (mac80211 map:0x%x)\n",
  689. queue,
  690. mvm->queue_info[queue].hw_queue_refcount,
  691. mvm->queue_info[queue].hw_queue_to_mac80211);
  692. /* If the queue is still enabled - nothing left to do in this func */
  693. if (cmd.action == SCD_CFG_ENABLE_QUEUE) {
  694. spin_unlock_bh(&mvm->queue_info_lock);
  695. return 0;
  696. }
  697. cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
  698. cmd.tid = mvm->queue_info[queue].txq_tid;
  699. /* Make sure queue info is correct even though we overwrite it */
  700. WARN(mvm->queue_info[queue].hw_queue_refcount ||
  701. mvm->queue_info[queue].tid_bitmap ||
  702. mvm->queue_info[queue].hw_queue_to_mac80211,
  703. "TXQ #%d info out-of-sync - refcount=%d, mac map=0x%x, tid=0x%x\n",
  704. queue, mvm->queue_info[queue].hw_queue_refcount,
  705. mvm->queue_info[queue].hw_queue_to_mac80211,
  706. mvm->queue_info[queue].tid_bitmap);
  707. /* If we are here - the queue is freed and we can zero out these vals */
  708. mvm->queue_info[queue].hw_queue_refcount = 0;
  709. mvm->queue_info[queue].tid_bitmap = 0;
  710. mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
  711. /* Regardless if this is a reserved TXQ for a STA - mark it as false */
  712. mvm->queue_info[queue].reserved = false;
  713. spin_unlock_bh(&mvm->queue_info_lock);
  714. if (iwl_mvm_has_new_tx_api(mvm)) {
  715. iwl_trans_txq_free(mvm->trans, queue);
  716. if (cmd.tid == IWL_MAX_TID_COUNT)
  717. cmd.tid = IWL_MGMT_TID;
  718. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
  719. sizeof(cmd), &cmd);
  720. } else {
  721. iwl_trans_txq_disable(mvm->trans, queue, false);
  722. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
  723. sizeof(struct iwl_scd_txq_cfg_cmd),
  724. &cmd);
  725. }
  726. if (ret)
  727. IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
  728. queue, ret);
  729. return ret;
  730. }
  731. /**
  732. * iwl_mvm_send_lq_cmd() - Send link quality command
  733. * @init: This command is sent as part of station initialization right
  734. * after station has been added.
  735. *
  736. * The link quality command is sent as the last step of station creation.
  737. * This is the special case in which init is set and we call a callback in
  738. * this case to clear the state indicating that station creation is in
  739. * progress.
  740. */
  741. int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq, bool init)
  742. {
  743. struct iwl_host_cmd cmd = {
  744. .id = LQ_CMD,
  745. .len = { sizeof(struct iwl_lq_cmd), },
  746. .flags = init ? 0 : CMD_ASYNC,
  747. .data = { lq, },
  748. };
  749. if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA))
  750. return -EINVAL;
  751. return iwl_mvm_send_cmd(mvm, &cmd);
  752. }
  753. /**
  754. * iwl_mvm_update_smps - Get a request to change the SMPS mode
  755. * @req_type: The part of the driver who call for a change.
  756. * @smps_requests: The request to change the SMPS mode.
  757. *
  758. * Get a requst to change the SMPS mode,
  759. * and change it according to all other requests in the driver.
  760. */
  761. void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  762. enum iwl_mvm_smps_type_request req_type,
  763. enum ieee80211_smps_mode smps_request)
  764. {
  765. struct iwl_mvm_vif *mvmvif;
  766. enum ieee80211_smps_mode smps_mode;
  767. int i;
  768. lockdep_assert_held(&mvm->mutex);
  769. /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
  770. if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
  771. return;
  772. if (vif->type == NL80211_IFTYPE_AP)
  773. smps_mode = IEEE80211_SMPS_OFF;
  774. else
  775. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  776. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  777. mvmvif->smps_requests[req_type] = smps_request;
  778. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
  779. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
  780. smps_mode = IEEE80211_SMPS_STATIC;
  781. break;
  782. }
  783. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
  784. smps_mode = IEEE80211_SMPS_DYNAMIC;
  785. }
  786. ieee80211_request_smps(vif, smps_mode);
  787. }
  788. int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
  789. {
  790. struct iwl_statistics_cmd scmd = {
  791. .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
  792. };
  793. struct iwl_host_cmd cmd = {
  794. .id = STATISTICS_CMD,
  795. .len[0] = sizeof(scmd),
  796. .data[0] = &scmd,
  797. .flags = CMD_WANT_SKB,
  798. };
  799. int ret;
  800. ret = iwl_mvm_send_cmd(mvm, &cmd);
  801. if (ret)
  802. return ret;
  803. iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
  804. iwl_free_resp(&cmd);
  805. if (clear)
  806. iwl_mvm_accu_radio_stats(mvm);
  807. return 0;
  808. }
  809. void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
  810. {
  811. mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
  812. mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
  813. mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
  814. mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
  815. }
  816. static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
  817. struct ieee80211_vif *vif)
  818. {
  819. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  820. bool *result = _data;
  821. int i;
  822. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
  823. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
  824. mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
  825. *result = false;
  826. }
  827. }
  828. bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
  829. {
  830. bool result = true;
  831. lockdep_assert_held(&mvm->mutex);
  832. if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
  833. return false;
  834. if (mvm->cfg->rx_with_siso_diversity)
  835. return false;
  836. ieee80211_iterate_active_interfaces_atomic(
  837. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  838. iwl_mvm_diversity_iter, &result);
  839. return result;
  840. }
  841. int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  842. bool prev)
  843. {
  844. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  845. int res;
  846. lockdep_assert_held(&mvm->mutex);
  847. if (iwl_mvm_vif_low_latency(mvmvif) == prev)
  848. return 0;
  849. res = iwl_mvm_update_quotas(mvm, false, NULL);
  850. if (res)
  851. return res;
  852. iwl_mvm_bt_coex_vif_change(mvm);
  853. return iwl_mvm_power_update_mac(mvm);
  854. }
  855. static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
  856. {
  857. bool *result = _data;
  858. if (iwl_mvm_vif_low_latency(iwl_mvm_vif_from_mac80211(vif)))
  859. *result = true;
  860. }
  861. bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
  862. {
  863. bool result = false;
  864. ieee80211_iterate_active_interfaces_atomic(
  865. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  866. iwl_mvm_ll_iter, &result);
  867. return result;
  868. }
  869. struct iwl_bss_iter_data {
  870. struct ieee80211_vif *vif;
  871. bool error;
  872. };
  873. static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
  874. struct ieee80211_vif *vif)
  875. {
  876. struct iwl_bss_iter_data *data = _data;
  877. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  878. return;
  879. if (data->vif) {
  880. data->error = true;
  881. return;
  882. }
  883. data->vif = vif;
  884. }
  885. struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
  886. {
  887. struct iwl_bss_iter_data bss_iter_data = {};
  888. ieee80211_iterate_active_interfaces_atomic(
  889. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  890. iwl_mvm_bss_iface_iterator, &bss_iter_data);
  891. if (bss_iter_data.error) {
  892. IWL_ERR(mvm, "More than one managed interface active!\n");
  893. return ERR_PTR(-EINVAL);
  894. }
  895. return bss_iter_data.vif;
  896. }
  897. unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
  898. struct ieee80211_vif *vif,
  899. bool tdls, bool cmd_q)
  900. {
  901. struct iwl_fw_dbg_trigger_tlv *trigger;
  902. struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
  903. unsigned int default_timeout =
  904. cmd_q ? IWL_DEF_WD_TIMEOUT : mvm->cfg->base_params->wd_timeout;
  905. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS))
  906. return iwlmvm_mod_params.tfd_q_hang_detect ?
  907. default_timeout : IWL_WATCHDOG_DISABLED;
  908. trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
  909. txq_timer = (void *)trigger->data;
  910. if (tdls)
  911. return le32_to_cpu(txq_timer->tdls);
  912. if (cmd_q)
  913. return le32_to_cpu(txq_timer->command_queue);
  914. if (WARN_ON(!vif))
  915. return default_timeout;
  916. switch (ieee80211_vif_type_p2p(vif)) {
  917. case NL80211_IFTYPE_ADHOC:
  918. return le32_to_cpu(txq_timer->ibss);
  919. case NL80211_IFTYPE_STATION:
  920. return le32_to_cpu(txq_timer->bss);
  921. case NL80211_IFTYPE_AP:
  922. return le32_to_cpu(txq_timer->softap);
  923. case NL80211_IFTYPE_P2P_CLIENT:
  924. return le32_to_cpu(txq_timer->p2p_client);
  925. case NL80211_IFTYPE_P2P_GO:
  926. return le32_to_cpu(txq_timer->p2p_go);
  927. case NL80211_IFTYPE_P2P_DEVICE:
  928. return le32_to_cpu(txq_timer->p2p_device);
  929. default:
  930. WARN_ON(1);
  931. return mvm->cfg->base_params->wd_timeout;
  932. }
  933. }
  934. void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  935. const char *errmsg)
  936. {
  937. struct iwl_fw_dbg_trigger_tlv *trig;
  938. struct iwl_fw_dbg_trigger_mlme *trig_mlme;
  939. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
  940. goto out;
  941. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
  942. trig_mlme = (void *)trig->data;
  943. if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trig))
  944. goto out;
  945. if (trig_mlme->stop_connection_loss &&
  946. --trig_mlme->stop_connection_loss)
  947. goto out;
  948. iwl_mvm_fw_dbg_collect_trig(mvm, trig, "%s", errmsg);
  949. out:
  950. ieee80211_connection_loss(vif);
  951. }
  952. /*
  953. * Remove inactive TIDs of a given queue.
  954. * If all queue TIDs are inactive - mark the queue as inactive
  955. * If only some the queue TIDs are inactive - unmap them from the queue
  956. */
  957. static void iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
  958. struct iwl_mvm_sta *mvmsta, int queue,
  959. unsigned long tid_bitmap)
  960. {
  961. int tid;
  962. lockdep_assert_held(&mvmsta->lock);
  963. lockdep_assert_held(&mvm->queue_info_lock);
  964. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  965. return;
  966. /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
  967. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  968. /* If some TFDs are still queued - don't mark TID as inactive */
  969. if (iwl_mvm_tid_queued(&mvmsta->tid_data[tid]))
  970. tid_bitmap &= ~BIT(tid);
  971. }
  972. /* If all TIDs in the queue are inactive - mark queue as inactive. */
  973. if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
  974. mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
  975. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1)
  976. mvmsta->tid_data[tid].is_tid_active = false;
  977. IWL_DEBUG_TX_QUEUES(mvm, "Queue %d marked as inactive\n",
  978. queue);
  979. return;
  980. }
  981. /*
  982. * If we are here, this is a shared queue and not all TIDs timed-out.
  983. * Remove the ones that did.
  984. */
  985. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  986. int mac_queue = mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]];
  987. mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
  988. mvm->queue_info[queue].hw_queue_to_mac80211 &= ~BIT(mac_queue);
  989. mvm->queue_info[queue].hw_queue_refcount--;
  990. mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
  991. mvmsta->tid_data[tid].is_tid_active = false;
  992. IWL_DEBUG_TX_QUEUES(mvm,
  993. "Removing inactive TID %d from shared Q:%d\n",
  994. tid, queue);
  995. }
  996. IWL_DEBUG_TX_QUEUES(mvm,
  997. "TXQ #%d left with tid bitmap 0x%x\n", queue,
  998. mvm->queue_info[queue].tid_bitmap);
  999. /*
  1000. * There may be different TIDs with the same mac queues, so make
  1001. * sure all TIDs have existing corresponding mac queues enabled
  1002. */
  1003. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  1004. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  1005. mvm->queue_info[queue].hw_queue_to_mac80211 |=
  1006. BIT(mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]]);
  1007. }
  1008. /* If the queue is marked as shared - "unshare" it */
  1009. if (mvm->queue_info[queue].hw_queue_refcount == 1 &&
  1010. mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
  1011. mvm->queue_info[queue].status = IWL_MVM_QUEUE_RECONFIGURING;
  1012. IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
  1013. queue);
  1014. }
  1015. }
  1016. void iwl_mvm_inactivity_check(struct iwl_mvm *mvm)
  1017. {
  1018. unsigned long timeout_queues_map = 0;
  1019. unsigned long now = jiffies;
  1020. int i;
  1021. if (iwl_mvm_has_new_tx_api(mvm))
  1022. return;
  1023. spin_lock_bh(&mvm->queue_info_lock);
  1024. for (i = 0; i < IWL_MAX_HW_QUEUES; i++)
  1025. if (mvm->queue_info[i].hw_queue_refcount > 0)
  1026. timeout_queues_map |= BIT(i);
  1027. spin_unlock_bh(&mvm->queue_info_lock);
  1028. rcu_read_lock();
  1029. /*
  1030. * If a queue time outs - mark it as INACTIVE (don't remove right away
  1031. * if we don't have to.) This is an optimization in case traffic comes
  1032. * later, and we don't HAVE to use a currently-inactive queue
  1033. */
  1034. for_each_set_bit(i, &timeout_queues_map, IWL_MAX_HW_QUEUES) {
  1035. struct ieee80211_sta *sta;
  1036. struct iwl_mvm_sta *mvmsta;
  1037. u8 sta_id;
  1038. int tid;
  1039. unsigned long inactive_tid_bitmap = 0;
  1040. unsigned long queue_tid_bitmap;
  1041. spin_lock_bh(&mvm->queue_info_lock);
  1042. queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
  1043. /* If TXQ isn't in active use anyway - nothing to do here... */
  1044. if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
  1045. mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED) {
  1046. spin_unlock_bh(&mvm->queue_info_lock);
  1047. continue;
  1048. }
  1049. /* Check to see if there are inactive TIDs on this queue */
  1050. for_each_set_bit(tid, &queue_tid_bitmap,
  1051. IWL_MAX_TID_COUNT + 1) {
  1052. if (time_after(mvm->queue_info[i].last_frame_time[tid] +
  1053. IWL_MVM_DQA_QUEUE_TIMEOUT, now))
  1054. continue;
  1055. inactive_tid_bitmap |= BIT(tid);
  1056. }
  1057. spin_unlock_bh(&mvm->queue_info_lock);
  1058. /* If all TIDs are active - finish check on this queue */
  1059. if (!inactive_tid_bitmap)
  1060. continue;
  1061. /*
  1062. * If we are here - the queue hadn't been served recently and is
  1063. * in use
  1064. */
  1065. sta_id = mvm->queue_info[i].ra_sta_id;
  1066. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1067. /*
  1068. * If the STA doesn't exist anymore, it isn't an error. It could
  1069. * be that it was removed since getting the queues, and in this
  1070. * case it should've inactivated its queues anyway.
  1071. */
  1072. if (IS_ERR_OR_NULL(sta))
  1073. continue;
  1074. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1075. spin_lock_bh(&mvmsta->lock);
  1076. spin_lock(&mvm->queue_info_lock);
  1077. iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
  1078. inactive_tid_bitmap);
  1079. spin_unlock(&mvm->queue_info_lock);
  1080. spin_unlock_bh(&mvmsta->lock);
  1081. }
  1082. rcu_read_unlock();
  1083. }
  1084. void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
  1085. {
  1086. bool ps_disabled;
  1087. lockdep_assert_held(&mvm->mutex);
  1088. /* Disable power save when reading GP2 */
  1089. ps_disabled = mvm->ps_disabled;
  1090. if (!ps_disabled) {
  1091. mvm->ps_disabled = true;
  1092. iwl_mvm_power_update_device(mvm);
  1093. }
  1094. *gp2 = iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
  1095. *boottime = ktime_get_boot_ns();
  1096. if (!ps_disabled) {
  1097. mvm->ps_disabled = ps_disabled;
  1098. iwl_mvm_power_update_device(mvm);
  1099. }
  1100. }
  1101. int iwl_mvm_send_lqm_cmd(struct ieee80211_vif *vif,
  1102. enum iwl_lqm_cmd_operatrions operation,
  1103. u32 duration, u32 timeout)
  1104. {
  1105. struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);
  1106. struct iwl_link_qual_msrmnt_cmd cmd = {
  1107. .cmd_operation = cpu_to_le32(operation),
  1108. .mac_id = cpu_to_le32(mvm_vif->id),
  1109. .measurement_time = cpu_to_le32(duration),
  1110. .timeout = cpu_to_le32(timeout),
  1111. };
  1112. u32 cmdid =
  1113. iwl_cmd_id(LINK_QUALITY_MEASUREMENT_CMD, MAC_CONF_GROUP, 0);
  1114. int ret;
  1115. if (!fw_has_capa(&mvm_vif->mvm->fw->ucode_capa,
  1116. IWL_UCODE_TLV_CAPA_LQM_SUPPORT))
  1117. return -EOPNOTSUPP;
  1118. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  1119. return -EINVAL;
  1120. switch (operation) {
  1121. case LQM_CMD_OPERATION_START_MEASUREMENT:
  1122. if (iwl_mvm_lqm_active(mvm_vif->mvm))
  1123. return -EBUSY;
  1124. if (!vif->bss_conf.assoc)
  1125. return -EINVAL;
  1126. mvm_vif->lqm_active = true;
  1127. break;
  1128. case LQM_CMD_OPERATION_STOP_MEASUREMENT:
  1129. if (!iwl_mvm_lqm_active(mvm_vif->mvm))
  1130. return -EINVAL;
  1131. break;
  1132. default:
  1133. return -EINVAL;
  1134. }
  1135. ret = iwl_mvm_send_cmd_pdu(mvm_vif->mvm, cmdid, 0, sizeof(cmd),
  1136. &cmd);
  1137. /* command failed - roll back lqm_active state */
  1138. if (ret) {
  1139. mvm_vif->lqm_active =
  1140. operation == LQM_CMD_OPERATION_STOP_MEASUREMENT;
  1141. }
  1142. return ret;
  1143. }
  1144. static void iwl_mvm_lqm_active_iterator(void *_data, u8 *mac,
  1145. struct ieee80211_vif *vif)
  1146. {
  1147. struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);
  1148. bool *lqm_active = _data;
  1149. *lqm_active = *lqm_active || mvm_vif->lqm_active;
  1150. }
  1151. bool iwl_mvm_lqm_active(struct iwl_mvm *mvm)
  1152. {
  1153. bool ret = false;
  1154. lockdep_assert_held(&mvm->mutex);
  1155. ieee80211_iterate_active_interfaces_atomic(
  1156. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1157. iwl_mvm_lqm_active_iterator, &ret);
  1158. return ret;
  1159. }