utils.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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. * Copyright(c) 2018 Intel Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of version 2 of the GNU General Public License as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING.
  24. *
  25. * Contact Information:
  26. * Intel Linux Wireless <linuxwifi@intel.com>
  27. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28. *
  29. * BSD LICENSE
  30. *
  31. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  32. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  33. * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  34. * Copyright(c) 2018 Intel Corporation
  35. * All rights reserved.
  36. *
  37. * Redistribution and use in source and binary forms, with or without
  38. * modification, are permitted provided that the following conditions
  39. * are met:
  40. *
  41. * * Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * * Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in
  45. * the documentation and/or other materials provided with the
  46. * distribution.
  47. * * Neither the name Intel Corporation nor the names of its
  48. * contributors may be used to endorse or promote products derived
  49. * from this software without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  54. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  55. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  56. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  57. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  58. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  59. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  60. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  61. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  62. *
  63. *****************************************************************************/
  64. #include <net/mac80211.h>
  65. #include "iwl-debug.h"
  66. #include "iwl-io.h"
  67. #include "iwl-prph.h"
  68. #include "iwl-csr.h"
  69. #include "mvm.h"
  70. #include "fw/api/rs.h"
  71. /*
  72. * Will return 0 even if the cmd failed when RFKILL is asserted unless
  73. * CMD_WANT_SKB is set in cmd->flags.
  74. */
  75. int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
  76. {
  77. int ret;
  78. #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
  79. if (WARN_ON(mvm->d3_test_active))
  80. return -EIO;
  81. #endif
  82. /*
  83. * Synchronous commands from this op-mode must hold
  84. * the mutex, this ensures we don't try to send two
  85. * (or more) synchronous commands at a time.
  86. */
  87. if (!(cmd->flags & CMD_ASYNC)) {
  88. lockdep_assert_held(&mvm->mutex);
  89. if (!(cmd->flags & CMD_SEND_IN_IDLE))
  90. iwl_mvm_ref(mvm, IWL_MVM_REF_SENDING_CMD);
  91. }
  92. ret = iwl_trans_send_cmd(mvm->trans, cmd);
  93. if (!(cmd->flags & (CMD_ASYNC | CMD_SEND_IN_IDLE)))
  94. iwl_mvm_unref(mvm, IWL_MVM_REF_SENDING_CMD);
  95. /*
  96. * If the caller wants the SKB, then don't hide any problems, the
  97. * caller might access the response buffer which will be NULL if
  98. * the command failed.
  99. */
  100. if (cmd->flags & CMD_WANT_SKB)
  101. return ret;
  102. /* Silently ignore failures if RFKILL is asserted */
  103. if (!ret || ret == -ERFKILL)
  104. return 0;
  105. return ret;
  106. }
  107. int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
  108. u32 flags, u16 len, const void *data)
  109. {
  110. struct iwl_host_cmd cmd = {
  111. .id = id,
  112. .len = { len, },
  113. .data = { data, },
  114. .flags = flags,
  115. };
  116. return iwl_mvm_send_cmd(mvm, &cmd);
  117. }
  118. /*
  119. * We assume that the caller set the status to the success value
  120. */
  121. int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
  122. u32 *status)
  123. {
  124. struct iwl_rx_packet *pkt;
  125. struct iwl_cmd_response *resp;
  126. int ret, resp_len;
  127. lockdep_assert_held(&mvm->mutex);
  128. #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
  129. if (WARN_ON(mvm->d3_test_active))
  130. return -EIO;
  131. #endif
  132. /*
  133. * Only synchronous commands can wait for status,
  134. * we use WANT_SKB so the caller can't.
  135. */
  136. if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
  137. "cmd flags %x", cmd->flags))
  138. return -EINVAL;
  139. cmd->flags |= CMD_WANT_SKB;
  140. ret = iwl_trans_send_cmd(mvm->trans, cmd);
  141. if (ret == -ERFKILL) {
  142. /*
  143. * The command failed because of RFKILL, don't update
  144. * the status, leave it as success and return 0.
  145. */
  146. return 0;
  147. } else if (ret) {
  148. return ret;
  149. }
  150. pkt = cmd->resp_pkt;
  151. resp_len = iwl_rx_packet_payload_len(pkt);
  152. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  153. ret = -EIO;
  154. goto out_free_resp;
  155. }
  156. resp = (void *)pkt->data;
  157. *status = le32_to_cpu(resp->status);
  158. out_free_resp:
  159. iwl_free_resp(cmd);
  160. return ret;
  161. }
  162. /*
  163. * We assume that the caller set the status to the sucess value
  164. */
  165. int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
  166. const void *data, u32 *status)
  167. {
  168. struct iwl_host_cmd cmd = {
  169. .id = id,
  170. .len = { len, },
  171. .data = { data, },
  172. };
  173. return iwl_mvm_send_cmd_status(mvm, &cmd, status);
  174. }
  175. #define IWL_DECLARE_RATE_INFO(r) \
  176. [IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
  177. /*
  178. * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
  179. */
  180. static const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
  181. IWL_DECLARE_RATE_INFO(1),
  182. IWL_DECLARE_RATE_INFO(2),
  183. IWL_DECLARE_RATE_INFO(5),
  184. IWL_DECLARE_RATE_INFO(11),
  185. IWL_DECLARE_RATE_INFO(6),
  186. IWL_DECLARE_RATE_INFO(9),
  187. IWL_DECLARE_RATE_INFO(12),
  188. IWL_DECLARE_RATE_INFO(18),
  189. IWL_DECLARE_RATE_INFO(24),
  190. IWL_DECLARE_RATE_INFO(36),
  191. IWL_DECLARE_RATE_INFO(48),
  192. IWL_DECLARE_RATE_INFO(54),
  193. };
  194. int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
  195. enum nl80211_band band)
  196. {
  197. int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
  198. int idx;
  199. int band_offset = 0;
  200. /* Legacy rate format, search for match in table */
  201. if (band == NL80211_BAND_5GHZ)
  202. band_offset = IWL_FIRST_OFDM_RATE;
  203. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  204. if (fw_rate_idx_to_plcp[idx] == rate)
  205. return idx - band_offset;
  206. return -1;
  207. }
  208. u8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
  209. {
  210. /* Get PLCP rate for tx_cmd->rate_n_flags */
  211. return fw_rate_idx_to_plcp[rate_idx];
  212. }
  213. void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  214. {
  215. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  216. struct iwl_error_resp *err_resp = (void *)pkt->data;
  217. IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
  218. le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
  219. IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
  220. le16_to_cpu(err_resp->bad_cmd_seq_num),
  221. le32_to_cpu(err_resp->error_service));
  222. IWL_ERR(mvm, "FW Error notification: timestamp 0x%16llX\n",
  223. le64_to_cpu(err_resp->timestamp));
  224. }
  225. /*
  226. * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
  227. * The parameter should also be a combination of ANT_[ABC].
  228. */
  229. u8 first_antenna(u8 mask)
  230. {
  231. BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
  232. if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
  233. return BIT(0);
  234. return BIT(ffs(mask) - 1);
  235. }
  236. /*
  237. * Toggles between TX antennas to send the probe request on.
  238. * Receives the bitmask of valid TX antennas and the *index* used
  239. * for the last TX, and returns the next valid *index* to use.
  240. * In order to set it in the tx_cmd, must do BIT(idx).
  241. */
  242. u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
  243. {
  244. u8 ind = last_idx;
  245. int i;
  246. for (i = 0; i < MAX_ANT_NUM; i++) {
  247. ind = (ind + 1) % MAX_ANT_NUM;
  248. if (valid & BIT(ind))
  249. return ind;
  250. }
  251. WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
  252. return last_idx;
  253. }
  254. static const struct {
  255. const char *name;
  256. u8 num;
  257. } advanced_lookup[] = {
  258. { "NMI_INTERRUPT_WDG", 0x34 },
  259. { "SYSASSERT", 0x35 },
  260. { "UCODE_VERSION_MISMATCH", 0x37 },
  261. { "BAD_COMMAND", 0x38 },
  262. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  263. { "FATAL_ERROR", 0x3D },
  264. { "NMI_TRM_HW_ERR", 0x46 },
  265. { "NMI_INTERRUPT_TRM", 0x4C },
  266. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  267. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  268. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  269. { "NMI_INTERRUPT_HOST", 0x66 },
  270. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  271. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  272. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  273. { "ADVANCED_SYSASSERT", 0 },
  274. };
  275. static const char *desc_lookup(u32 num)
  276. {
  277. int i;
  278. for (i = 0; i < ARRAY_SIZE(advanced_lookup) - 1; i++)
  279. if (advanced_lookup[i].num == num)
  280. return advanced_lookup[i].name;
  281. /* No entry matches 'num', so it is the last: ADVANCED_SYSASSERT */
  282. return advanced_lookup[i].name;
  283. }
  284. /*
  285. * Note: This structure is read from the device with IO accesses,
  286. * and the reading already does the endian conversion. As it is
  287. * read with u32-sized accesses, any members with a different size
  288. * need to be ordered correctly though!
  289. */
  290. struct iwl_error_event_table_v1 {
  291. u32 valid; /* (nonzero) valid, (0) log is empty */
  292. u32 error_id; /* type of error */
  293. u32 pc; /* program counter */
  294. u32 blink1; /* branch link */
  295. u32 blink2; /* branch link */
  296. u32 ilink1; /* interrupt link */
  297. u32 ilink2; /* interrupt link */
  298. u32 data1; /* error-specific data */
  299. u32 data2; /* error-specific data */
  300. u32 data3; /* error-specific data */
  301. u32 bcon_time; /* beacon timer */
  302. u32 tsf_low; /* network timestamp function timer */
  303. u32 tsf_hi; /* network timestamp function timer */
  304. u32 gp1; /* GP1 timer register */
  305. u32 gp2; /* GP2 timer register */
  306. u32 gp3; /* GP3 timer register */
  307. u32 ucode_ver; /* uCode version */
  308. u32 hw_ver; /* HW Silicon version */
  309. u32 brd_ver; /* HW board version */
  310. u32 log_pc; /* log program counter */
  311. u32 frame_ptr; /* frame pointer */
  312. u32 stack_ptr; /* stack pointer */
  313. u32 hcmd; /* last host command header */
  314. u32 isr0; /* isr status register LMPM_NIC_ISR0:
  315. * rxtx_flag */
  316. u32 isr1; /* isr status register LMPM_NIC_ISR1:
  317. * host_flag */
  318. u32 isr2; /* isr status register LMPM_NIC_ISR2:
  319. * enc_flag */
  320. u32 isr3; /* isr status register LMPM_NIC_ISR3:
  321. * time_flag */
  322. u32 isr4; /* isr status register LMPM_NIC_ISR4:
  323. * wico interrupt */
  324. u32 isr_pref; /* isr status register LMPM_NIC_PREF_STAT */
  325. u32 wait_event; /* wait event() caller address */
  326. u32 l2p_control; /* L2pControlField */
  327. u32 l2p_duration; /* L2pDurationField */
  328. u32 l2p_mhvalid; /* L2pMhValidBits */
  329. u32 l2p_addr_match; /* L2pAddrMatchStat */
  330. u32 lmpm_pmg_sel; /* indicate which clocks are turned on
  331. * (LMPM_PMG_SEL) */
  332. u32 u_timestamp; /* indicate when the date and time of the
  333. * compilation */
  334. u32 flow_handler; /* FH read/write pointers, RX credit */
  335. } __packed /* LOG_ERROR_TABLE_API_S_VER_1 */;
  336. struct iwl_error_event_table {
  337. u32 valid; /* (nonzero) valid, (0) log is empty */
  338. u32 error_id; /* type of error */
  339. u32 trm_hw_status0; /* TRM HW status */
  340. u32 trm_hw_status1; /* TRM HW status */
  341. u32 blink2; /* branch link */
  342. u32 ilink1; /* interrupt link */
  343. u32 ilink2; /* interrupt link */
  344. u32 data1; /* error-specific data */
  345. u32 data2; /* error-specific data */
  346. u32 data3; /* error-specific data */
  347. u32 bcon_time; /* beacon timer */
  348. u32 tsf_low; /* network timestamp function timer */
  349. u32 tsf_hi; /* network timestamp function timer */
  350. u32 gp1; /* GP1 timer register */
  351. u32 gp2; /* GP2 timer register */
  352. u32 fw_rev_type; /* firmware revision type */
  353. u32 major; /* uCode version major */
  354. u32 minor; /* uCode version minor */
  355. u32 hw_ver; /* HW Silicon version */
  356. u32 brd_ver; /* HW board version */
  357. u32 log_pc; /* log program counter */
  358. u32 frame_ptr; /* frame pointer */
  359. u32 stack_ptr; /* stack pointer */
  360. u32 hcmd; /* last host command header */
  361. u32 isr0; /* isr status register LMPM_NIC_ISR0:
  362. * rxtx_flag */
  363. u32 isr1; /* isr status register LMPM_NIC_ISR1:
  364. * host_flag */
  365. u32 isr2; /* isr status register LMPM_NIC_ISR2:
  366. * enc_flag */
  367. u32 isr3; /* isr status register LMPM_NIC_ISR3:
  368. * time_flag */
  369. u32 isr4; /* isr status register LMPM_NIC_ISR4:
  370. * wico interrupt */
  371. u32 last_cmd_id; /* last HCMD id handled by the firmware */
  372. u32 wait_event; /* wait event() caller address */
  373. u32 l2p_control; /* L2pControlField */
  374. u32 l2p_duration; /* L2pDurationField */
  375. u32 l2p_mhvalid; /* L2pMhValidBits */
  376. u32 l2p_addr_match; /* L2pAddrMatchStat */
  377. u32 lmpm_pmg_sel; /* indicate which clocks are turned on
  378. * (LMPM_PMG_SEL) */
  379. u32 u_timestamp; /* indicate when the date and time of the
  380. * compilation */
  381. u32 flow_handler; /* FH read/write pointers, RX credit */
  382. } __packed /* LOG_ERROR_TABLE_API_S_VER_3 */;
  383. /*
  384. * UMAC error struct - relevant starting from family 8000 chip.
  385. * Note: This structure is read from the device with IO accesses,
  386. * and the reading already does the endian conversion. As it is
  387. * read with u32-sized accesses, any members with a different size
  388. * need to be ordered correctly though!
  389. */
  390. struct iwl_umac_error_event_table {
  391. u32 valid; /* (nonzero) valid, (0) log is empty */
  392. u32 error_id; /* type of error */
  393. u32 blink1; /* branch link */
  394. u32 blink2; /* branch link */
  395. u32 ilink1; /* interrupt link */
  396. u32 ilink2; /* interrupt link */
  397. u32 data1; /* error-specific data */
  398. u32 data2; /* error-specific data */
  399. u32 data3; /* error-specific data */
  400. u32 umac_major;
  401. u32 umac_minor;
  402. u32 frame_pointer; /* core register 27*/
  403. u32 stack_pointer; /* core register 28 */
  404. u32 cmd_header; /* latest host cmd sent to UMAC */
  405. u32 nic_isr_pref; /* ISR status register */
  406. } __packed;
  407. #define ERROR_START_OFFSET (1 * sizeof(u32))
  408. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  409. static void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
  410. {
  411. struct iwl_trans *trans = mvm->trans;
  412. struct iwl_umac_error_event_table table;
  413. if (!mvm->support_umac_log)
  414. return;
  415. iwl_trans_read_mem_bytes(trans, mvm->umac_error_event_table, &table,
  416. sizeof(table));
  417. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  418. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  419. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  420. mvm->status, table.valid);
  421. }
  422. IWL_ERR(mvm, "0x%08X | %s\n", table.error_id,
  423. desc_lookup(table.error_id));
  424. IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
  425. IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
  426. IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
  427. IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
  428. IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
  429. IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
  430. IWL_ERR(mvm, "0x%08X | umac data3\n", table.data3);
  431. IWL_ERR(mvm, "0x%08X | umac major\n", table.umac_major);
  432. IWL_ERR(mvm, "0x%08X | umac minor\n", table.umac_minor);
  433. IWL_ERR(mvm, "0x%08X | frame pointer\n", table.frame_pointer);
  434. IWL_ERR(mvm, "0x%08X | stack pointer\n", table.stack_pointer);
  435. IWL_ERR(mvm, "0x%08X | last host cmd\n", table.cmd_header);
  436. IWL_ERR(mvm, "0x%08X | isr status reg\n", table.nic_isr_pref);
  437. }
  438. static void iwl_mvm_dump_lmac_error_log(struct iwl_mvm *mvm, u32 base)
  439. {
  440. struct iwl_trans *trans = mvm->trans;
  441. struct iwl_error_event_table table;
  442. u32 val;
  443. if (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) {
  444. if (!base)
  445. base = mvm->fw->init_errlog_ptr;
  446. } else {
  447. if (!base)
  448. base = mvm->fw->inst_errlog_ptr;
  449. }
  450. if (base < 0x400000) {
  451. IWL_ERR(mvm,
  452. "Not valid error log pointer 0x%08X for %s uCode\n",
  453. base,
  454. (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT)
  455. ? "Init" : "RT");
  456. return;
  457. }
  458. /* check if there is a HW error */
  459. val = iwl_trans_read_mem32(trans, base);
  460. if (((val & ~0xf) == 0xa5a5a5a0) || ((val & ~0xf) == 0x5a5a5a50)) {
  461. int err;
  462. IWL_ERR(trans, "HW error, resetting before reading\n");
  463. /* reset the device */
  464. iwl_trans_sw_reset(trans);
  465. /* set INIT_DONE flag */
  466. iwl_set_bit(trans, CSR_GP_CNTRL,
  467. BIT(trans->cfg->csr->flag_init_done));
  468. /* and wait for clock stabilization */
  469. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  470. udelay(2);
  471. err = iwl_poll_bit(trans, CSR_GP_CNTRL,
  472. BIT(trans->cfg->csr->flag_mac_clock_ready),
  473. BIT(trans->cfg->csr->flag_mac_clock_ready),
  474. 25000);
  475. if (err < 0) {
  476. IWL_DEBUG_INFO(trans,
  477. "Failed to reset the card for the dump\n");
  478. return;
  479. }
  480. }
  481. iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
  482. if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
  483. IWL_ERR(trans, "Start IWL Error Log Dump:\n");
  484. IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
  485. mvm->status, table.valid);
  486. }
  487. /* Do not change this output - scripts rely on it */
  488. IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
  489. trace_iwlwifi_dev_ucode_error(trans->dev, &table, table.hw_ver, table.brd_ver);
  490. IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
  491. desc_lookup(table.error_id));
  492. IWL_ERR(mvm, "0x%08X | trm_hw_status0\n", table.trm_hw_status0);
  493. IWL_ERR(mvm, "0x%08X | trm_hw_status1\n", table.trm_hw_status1);
  494. IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
  495. IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
  496. IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
  497. IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
  498. IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
  499. IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
  500. IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
  501. IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
  502. IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
  503. IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
  504. IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
  505. IWL_ERR(mvm, "0x%08X | uCode revision type\n", table.fw_rev_type);
  506. IWL_ERR(mvm, "0x%08X | uCode version major\n", table.major);
  507. IWL_ERR(mvm, "0x%08X | uCode version minor\n", table.minor);
  508. IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
  509. IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
  510. IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
  511. IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
  512. IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
  513. IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
  514. IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
  515. IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
  516. IWL_ERR(mvm, "0x%08X | last cmd Id\n", table.last_cmd_id);
  517. IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
  518. IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
  519. IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
  520. IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
  521. IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
  522. IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
  523. IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
  524. IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
  525. }
  526. void iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
  527. {
  528. if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
  529. IWL_ERR(mvm,
  530. "DEVICE_ENABLED bit is not set. Aborting dump.\n");
  531. return;
  532. }
  533. iwl_mvm_dump_lmac_error_log(mvm, mvm->error_event_table[0]);
  534. if (mvm->error_event_table[1])
  535. iwl_mvm_dump_lmac_error_log(mvm, mvm->error_event_table[1]);
  536. iwl_mvm_dump_umac_error_log(mvm);
  537. }
  538. int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id, u8 minq, u8 maxq)
  539. {
  540. int i;
  541. lockdep_assert_held(&mvm->queue_info_lock);
  542. /* This should not be hit with new TX path */
  543. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  544. return -ENOSPC;
  545. /* Start by looking for a free queue */
  546. for (i = minq; i <= maxq; i++)
  547. if (mvm->queue_info[i].hw_queue_refcount == 0 &&
  548. mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
  549. return i;
  550. /*
  551. * If no free queue found - settle for an inactive one to reconfigure
  552. * Make sure that the inactive queue either already belongs to this STA,
  553. * or that if it belongs to another one - it isn't the reserved queue
  554. */
  555. for (i = minq; i <= maxq; i++)
  556. if (mvm->queue_info[i].status == IWL_MVM_QUEUE_INACTIVE &&
  557. (sta_id == mvm->queue_info[i].ra_sta_id ||
  558. !mvm->queue_info[i].reserved))
  559. return i;
  560. return -ENOSPC;
  561. }
  562. int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, int sta_id,
  563. int tid, int frame_limit, u16 ssn)
  564. {
  565. struct iwl_scd_txq_cfg_cmd cmd = {
  566. .scd_queue = queue,
  567. .action = SCD_CFG_ENABLE_QUEUE,
  568. .window = frame_limit,
  569. .sta_id = sta_id,
  570. .ssn = cpu_to_le16(ssn),
  571. .tx_fifo = fifo,
  572. .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
  573. queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
  574. .tid = tid,
  575. };
  576. int ret;
  577. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  578. return -EINVAL;
  579. spin_lock_bh(&mvm->queue_info_lock);
  580. if (WARN(mvm->queue_info[queue].hw_queue_refcount == 0,
  581. "Trying to reconfig unallocated queue %d\n", queue)) {
  582. spin_unlock_bh(&mvm->queue_info_lock);
  583. return -ENXIO;
  584. }
  585. spin_unlock_bh(&mvm->queue_info_lock);
  586. IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
  587. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
  588. WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
  589. queue, fifo, ret);
  590. return ret;
  591. }
  592. static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm, int queue,
  593. int mac80211_queue, u8 sta_id, u8 tid)
  594. {
  595. bool enable_queue = true;
  596. spin_lock_bh(&mvm->queue_info_lock);
  597. /* Make sure this TID isn't already enabled */
  598. if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
  599. spin_unlock_bh(&mvm->queue_info_lock);
  600. IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
  601. queue, tid);
  602. return false;
  603. }
  604. /* Update mappings and refcounts */
  605. if (mvm->queue_info[queue].hw_queue_refcount > 0)
  606. enable_queue = false;
  607. if (mac80211_queue != IEEE80211_INVAL_HW_QUEUE) {
  608. WARN(mac80211_queue >=
  609. BITS_PER_BYTE * sizeof(mvm->hw_queue_to_mac80211[0]),
  610. "cannot track mac80211 queue %d (queue %d, sta %d, tid %d)\n",
  611. mac80211_queue, queue, sta_id, tid);
  612. mvm->hw_queue_to_mac80211[queue] |= BIT(mac80211_queue);
  613. }
  614. mvm->queue_info[queue].hw_queue_refcount++;
  615. mvm->queue_info[queue].tid_bitmap |= BIT(tid);
  616. mvm->queue_info[queue].ra_sta_id = sta_id;
  617. if (enable_queue) {
  618. if (tid != IWL_MAX_TID_COUNT)
  619. mvm->queue_info[queue].mac80211_ac =
  620. tid_to_mac80211_ac[tid];
  621. else
  622. mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
  623. mvm->queue_info[queue].txq_tid = tid;
  624. }
  625. IWL_DEBUG_TX_QUEUES(mvm,
  626. "Enabling TXQ #%d refcount=%d (mac80211 map:0x%x)\n",
  627. queue, mvm->queue_info[queue].hw_queue_refcount,
  628. mvm->hw_queue_to_mac80211[queue]);
  629. spin_unlock_bh(&mvm->queue_info_lock);
  630. return enable_queue;
  631. }
  632. int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm, int mac80211_queue,
  633. u8 sta_id, u8 tid, unsigned int timeout)
  634. {
  635. int queue, size = IWL_DEFAULT_QUEUE_SIZE;
  636. if (tid == IWL_MAX_TID_COUNT) {
  637. tid = IWL_MGMT_TID;
  638. size = IWL_MGMT_QUEUE_SIZE;
  639. }
  640. queue = iwl_trans_txq_alloc(mvm->trans,
  641. cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
  642. sta_id, tid, SCD_QUEUE_CFG, size, timeout);
  643. if (queue < 0) {
  644. IWL_DEBUG_TX_QUEUES(mvm,
  645. "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
  646. sta_id, tid, queue);
  647. return queue;
  648. }
  649. IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
  650. queue, sta_id, tid);
  651. mvm->hw_queue_to_mac80211[queue] |= BIT(mac80211_queue);
  652. IWL_DEBUG_TX_QUEUES(mvm,
  653. "Enabling TXQ #%d (mac80211 map:0x%x)\n",
  654. queue, mvm->hw_queue_to_mac80211[queue]);
  655. return queue;
  656. }
  657. bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, int queue, int mac80211_queue,
  658. u16 ssn, const struct iwl_trans_txq_scd_cfg *cfg,
  659. unsigned int wdg_timeout)
  660. {
  661. struct iwl_scd_txq_cfg_cmd cmd = {
  662. .scd_queue = queue,
  663. .action = SCD_CFG_ENABLE_QUEUE,
  664. .window = cfg->frame_limit,
  665. .sta_id = cfg->sta_id,
  666. .ssn = cpu_to_le16(ssn),
  667. .tx_fifo = cfg->fifo,
  668. .aggregate = cfg->aggregate,
  669. .tid = cfg->tid,
  670. };
  671. bool inc_ssn;
  672. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  673. return false;
  674. /* Send the enabling command if we need to */
  675. if (!iwl_mvm_update_txq_mapping(mvm, queue, mac80211_queue,
  676. cfg->sta_id, cfg->tid))
  677. return false;
  678. inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
  679. NULL, wdg_timeout);
  680. if (inc_ssn)
  681. le16_add_cpu(&cmd.ssn, 1);
  682. WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
  683. "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
  684. return inc_ssn;
  685. }
  686. int iwl_mvm_disable_txq(struct iwl_mvm *mvm, int queue, int mac80211_queue,
  687. u8 tid, u8 flags)
  688. {
  689. struct iwl_scd_txq_cfg_cmd cmd = {
  690. .scd_queue = queue,
  691. .action = SCD_CFG_DISABLE_QUEUE,
  692. };
  693. bool remove_mac_queue = mac80211_queue != IEEE80211_INVAL_HW_QUEUE;
  694. int ret;
  695. if (WARN_ON(remove_mac_queue && mac80211_queue >= IEEE80211_MAX_QUEUES))
  696. return -EINVAL;
  697. if (iwl_mvm_has_new_tx_api(mvm)) {
  698. spin_lock_bh(&mvm->queue_info_lock);
  699. if (remove_mac_queue)
  700. mvm->hw_queue_to_mac80211[queue] &=
  701. ~BIT(mac80211_queue);
  702. spin_unlock_bh(&mvm->queue_info_lock);
  703. iwl_trans_txq_free(mvm->trans, queue);
  704. return 0;
  705. }
  706. spin_lock_bh(&mvm->queue_info_lock);
  707. if (WARN_ON(mvm->queue_info[queue].hw_queue_refcount == 0)) {
  708. spin_unlock_bh(&mvm->queue_info_lock);
  709. return 0;
  710. }
  711. mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
  712. /*
  713. * If there is another TID with the same AC - don't remove the MAC queue
  714. * from the mapping
  715. */
  716. if (tid < IWL_MAX_TID_COUNT) {
  717. unsigned long tid_bitmap =
  718. mvm->queue_info[queue].tid_bitmap;
  719. int ac = tid_to_mac80211_ac[tid];
  720. int i;
  721. for_each_set_bit(i, &tid_bitmap, IWL_MAX_TID_COUNT) {
  722. if (tid_to_mac80211_ac[i] == ac)
  723. remove_mac_queue = false;
  724. }
  725. }
  726. if (remove_mac_queue)
  727. mvm->hw_queue_to_mac80211[queue] &=
  728. ~BIT(mac80211_queue);
  729. mvm->queue_info[queue].hw_queue_refcount--;
  730. cmd.action = mvm->queue_info[queue].hw_queue_refcount ?
  731. SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
  732. if (cmd.action == SCD_CFG_DISABLE_QUEUE)
  733. mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
  734. IWL_DEBUG_TX_QUEUES(mvm,
  735. "Disabling TXQ #%d refcount=%d (mac80211 map:0x%x)\n",
  736. queue,
  737. mvm->queue_info[queue].hw_queue_refcount,
  738. mvm->hw_queue_to_mac80211[queue]);
  739. /* If the queue is still enabled - nothing left to do in this func */
  740. if (cmd.action == SCD_CFG_ENABLE_QUEUE) {
  741. spin_unlock_bh(&mvm->queue_info_lock);
  742. return 0;
  743. }
  744. cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
  745. cmd.tid = mvm->queue_info[queue].txq_tid;
  746. /* Make sure queue info is correct even though we overwrite it */
  747. WARN(mvm->queue_info[queue].hw_queue_refcount ||
  748. mvm->queue_info[queue].tid_bitmap ||
  749. mvm->hw_queue_to_mac80211[queue],
  750. "TXQ #%d info out-of-sync - refcount=%d, mac map=0x%x, tid=0x%x\n",
  751. queue, mvm->queue_info[queue].hw_queue_refcount,
  752. mvm->hw_queue_to_mac80211[queue],
  753. mvm->queue_info[queue].tid_bitmap);
  754. /* If we are here - the queue is freed and we can zero out these vals */
  755. mvm->queue_info[queue].hw_queue_refcount = 0;
  756. mvm->queue_info[queue].tid_bitmap = 0;
  757. mvm->hw_queue_to_mac80211[queue] = 0;
  758. /* Regardless if this is a reserved TXQ for a STA - mark it as false */
  759. mvm->queue_info[queue].reserved = false;
  760. spin_unlock_bh(&mvm->queue_info_lock);
  761. iwl_trans_txq_disable(mvm->trans, queue, false);
  762. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
  763. sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
  764. if (ret)
  765. IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
  766. queue, ret);
  767. return ret;
  768. }
  769. /**
  770. * iwl_mvm_send_lq_cmd() - Send link quality command
  771. * @sync: This command can be sent synchronously.
  772. *
  773. * The link quality command is sent as the last step of station creation.
  774. * This is the special case in which init is set and we call a callback in
  775. * this case to clear the state indicating that station creation is in
  776. * progress.
  777. */
  778. int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq, bool sync)
  779. {
  780. struct iwl_host_cmd cmd = {
  781. .id = LQ_CMD,
  782. .len = { sizeof(struct iwl_lq_cmd), },
  783. .flags = sync ? 0 : CMD_ASYNC,
  784. .data = { lq, },
  785. };
  786. if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA ||
  787. iwl_mvm_has_tlc_offload(mvm)))
  788. return -EINVAL;
  789. return iwl_mvm_send_cmd(mvm, &cmd);
  790. }
  791. /**
  792. * iwl_mvm_update_smps - Get a request to change the SMPS mode
  793. * @req_type: The part of the driver who call for a change.
  794. * @smps_requests: The request to change the SMPS mode.
  795. *
  796. * Get a requst to change the SMPS mode,
  797. * and change it according to all other requests in the driver.
  798. */
  799. void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  800. enum iwl_mvm_smps_type_request req_type,
  801. enum ieee80211_smps_mode smps_request)
  802. {
  803. struct iwl_mvm_vif *mvmvif;
  804. enum ieee80211_smps_mode smps_mode;
  805. int i;
  806. lockdep_assert_held(&mvm->mutex);
  807. /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
  808. if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
  809. return;
  810. if (vif->type == NL80211_IFTYPE_AP)
  811. smps_mode = IEEE80211_SMPS_OFF;
  812. else
  813. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  814. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  815. mvmvif->smps_requests[req_type] = smps_request;
  816. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
  817. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
  818. smps_mode = IEEE80211_SMPS_STATIC;
  819. break;
  820. }
  821. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
  822. smps_mode = IEEE80211_SMPS_DYNAMIC;
  823. }
  824. ieee80211_request_smps(vif, smps_mode);
  825. }
  826. int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
  827. {
  828. struct iwl_statistics_cmd scmd = {
  829. .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
  830. };
  831. struct iwl_host_cmd cmd = {
  832. .id = STATISTICS_CMD,
  833. .len[0] = sizeof(scmd),
  834. .data[0] = &scmd,
  835. .flags = CMD_WANT_SKB,
  836. };
  837. int ret;
  838. ret = iwl_mvm_send_cmd(mvm, &cmd);
  839. if (ret)
  840. return ret;
  841. iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
  842. iwl_free_resp(&cmd);
  843. if (clear)
  844. iwl_mvm_accu_radio_stats(mvm);
  845. return 0;
  846. }
  847. void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
  848. {
  849. mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
  850. mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
  851. mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
  852. mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
  853. }
  854. static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
  855. struct ieee80211_vif *vif)
  856. {
  857. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  858. bool *result = _data;
  859. int i;
  860. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
  861. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
  862. mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
  863. *result = false;
  864. }
  865. }
  866. bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
  867. {
  868. bool result = true;
  869. lockdep_assert_held(&mvm->mutex);
  870. if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
  871. return false;
  872. if (mvm->cfg->rx_with_siso_diversity)
  873. return false;
  874. ieee80211_iterate_active_interfaces_atomic(
  875. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  876. iwl_mvm_diversity_iter, &result);
  877. return result;
  878. }
  879. int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  880. bool low_latency,
  881. enum iwl_mvm_low_latency_cause cause)
  882. {
  883. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  884. int res;
  885. bool prev;
  886. lockdep_assert_held(&mvm->mutex);
  887. prev = iwl_mvm_vif_low_latency(mvmvif);
  888. iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
  889. low_latency = iwl_mvm_vif_low_latency(mvmvif);
  890. if (low_latency == prev)
  891. return 0;
  892. if (fw_has_capa(&mvm->fw->ucode_capa,
  893. IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA)) {
  894. struct iwl_mac_low_latency_cmd cmd = {
  895. .mac_id = cpu_to_le32(mvmvif->id)
  896. };
  897. if (low_latency) {
  898. /* currently we don't care about the direction */
  899. cmd.low_latency_rx = 1;
  900. cmd.low_latency_tx = 1;
  901. }
  902. res = iwl_mvm_send_cmd_pdu(mvm,
  903. iwl_cmd_id(LOW_LATENCY_CMD,
  904. MAC_CONF_GROUP, 0),
  905. 0, sizeof(cmd), &cmd);
  906. if (res)
  907. IWL_ERR(mvm, "Failed to send low latency command\n");
  908. }
  909. res = iwl_mvm_update_quotas(mvm, false, NULL);
  910. if (res)
  911. return res;
  912. iwl_mvm_bt_coex_vif_change(mvm);
  913. return iwl_mvm_power_update_mac(mvm);
  914. }
  915. struct iwl_mvm_low_latency_iter {
  916. bool result;
  917. bool result_per_band[NUM_NL80211_BANDS];
  918. };
  919. static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
  920. {
  921. struct iwl_mvm_low_latency_iter *result = _data;
  922. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  923. enum nl80211_band band;
  924. if (iwl_mvm_vif_low_latency(mvmvif)) {
  925. result->result = true;
  926. if (!mvmvif->phy_ctxt)
  927. return;
  928. band = mvmvif->phy_ctxt->channel->band;
  929. result->result_per_band[band] = true;
  930. }
  931. }
  932. bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
  933. {
  934. struct iwl_mvm_low_latency_iter data = {};
  935. ieee80211_iterate_active_interfaces_atomic(
  936. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  937. iwl_mvm_ll_iter, &data);
  938. return data.result;
  939. }
  940. bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
  941. {
  942. struct iwl_mvm_low_latency_iter data = {};
  943. ieee80211_iterate_active_interfaces_atomic(
  944. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  945. iwl_mvm_ll_iter, &data);
  946. return data.result_per_band[band];
  947. }
  948. struct iwl_bss_iter_data {
  949. struct ieee80211_vif *vif;
  950. bool error;
  951. };
  952. static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
  953. struct ieee80211_vif *vif)
  954. {
  955. struct iwl_bss_iter_data *data = _data;
  956. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  957. return;
  958. if (data->vif) {
  959. data->error = true;
  960. return;
  961. }
  962. data->vif = vif;
  963. }
  964. struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
  965. {
  966. struct iwl_bss_iter_data bss_iter_data = {};
  967. ieee80211_iterate_active_interfaces_atomic(
  968. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  969. iwl_mvm_bss_iface_iterator, &bss_iter_data);
  970. if (bss_iter_data.error) {
  971. IWL_ERR(mvm, "More than one managed interface active!\n");
  972. return ERR_PTR(-EINVAL);
  973. }
  974. return bss_iter_data.vif;
  975. }
  976. struct iwl_sta_iter_data {
  977. bool assoc;
  978. };
  979. static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
  980. struct ieee80211_vif *vif)
  981. {
  982. struct iwl_sta_iter_data *data = _data;
  983. if (vif->type != NL80211_IFTYPE_STATION)
  984. return;
  985. if (vif->bss_conf.assoc)
  986. data->assoc = true;
  987. }
  988. bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
  989. {
  990. struct iwl_sta_iter_data data = {
  991. .assoc = false,
  992. };
  993. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  994. IEEE80211_IFACE_ITER_NORMAL,
  995. iwl_mvm_sta_iface_iterator,
  996. &data);
  997. return data.assoc;
  998. }
  999. unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
  1000. struct ieee80211_vif *vif,
  1001. bool tdls, bool cmd_q)
  1002. {
  1003. struct iwl_fw_dbg_trigger_tlv *trigger;
  1004. struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
  1005. unsigned int default_timeout =
  1006. cmd_q ? IWL_DEF_WD_TIMEOUT : mvm->cfg->base_params->wd_timeout;
  1007. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
  1008. /*
  1009. * We can't know when the station is asleep or awake, so we
  1010. * must disable the queue hang detection.
  1011. */
  1012. if (fw_has_capa(&mvm->fw->ucode_capa,
  1013. IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
  1014. vif && vif->type == NL80211_IFTYPE_AP)
  1015. return IWL_WATCHDOG_DISABLED;
  1016. return iwlmvm_mod_params.tfd_q_hang_detect ?
  1017. default_timeout : IWL_WATCHDOG_DISABLED;
  1018. }
  1019. trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
  1020. txq_timer = (void *)trigger->data;
  1021. if (tdls)
  1022. return le32_to_cpu(txq_timer->tdls);
  1023. if (cmd_q)
  1024. return le32_to_cpu(txq_timer->command_queue);
  1025. if (WARN_ON(!vif))
  1026. return default_timeout;
  1027. switch (ieee80211_vif_type_p2p(vif)) {
  1028. case NL80211_IFTYPE_ADHOC:
  1029. return le32_to_cpu(txq_timer->ibss);
  1030. case NL80211_IFTYPE_STATION:
  1031. return le32_to_cpu(txq_timer->bss);
  1032. case NL80211_IFTYPE_AP:
  1033. return le32_to_cpu(txq_timer->softap);
  1034. case NL80211_IFTYPE_P2P_CLIENT:
  1035. return le32_to_cpu(txq_timer->p2p_client);
  1036. case NL80211_IFTYPE_P2P_GO:
  1037. return le32_to_cpu(txq_timer->p2p_go);
  1038. case NL80211_IFTYPE_P2P_DEVICE:
  1039. return le32_to_cpu(txq_timer->p2p_device);
  1040. case NL80211_IFTYPE_MONITOR:
  1041. return default_timeout;
  1042. default:
  1043. WARN_ON(1);
  1044. return mvm->cfg->base_params->wd_timeout;
  1045. }
  1046. }
  1047. void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1048. const char *errmsg)
  1049. {
  1050. struct iwl_fw_dbg_trigger_tlv *trig;
  1051. struct iwl_fw_dbg_trigger_mlme *trig_mlme;
  1052. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
  1053. goto out;
  1054. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
  1055. trig_mlme = (void *)trig->data;
  1056. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  1057. ieee80211_vif_to_wdev(vif), trig))
  1058. goto out;
  1059. if (trig_mlme->stop_connection_loss &&
  1060. --trig_mlme->stop_connection_loss)
  1061. goto out;
  1062. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
  1063. out:
  1064. ieee80211_connection_loss(vif);
  1065. }
  1066. /*
  1067. * Remove inactive TIDs of a given queue.
  1068. * If all queue TIDs are inactive - mark the queue as inactive
  1069. * If only some the queue TIDs are inactive - unmap them from the queue
  1070. */
  1071. static void iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
  1072. struct iwl_mvm_sta *mvmsta, int queue,
  1073. unsigned long tid_bitmap)
  1074. {
  1075. int tid;
  1076. lockdep_assert_held(&mvmsta->lock);
  1077. lockdep_assert_held(&mvm->queue_info_lock);
  1078. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  1079. return;
  1080. /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
  1081. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  1082. /* If some TFDs are still queued - don't mark TID as inactive */
  1083. if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
  1084. tid_bitmap &= ~BIT(tid);
  1085. /* Don't mark as inactive any TID that has an active BA */
  1086. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
  1087. tid_bitmap &= ~BIT(tid);
  1088. }
  1089. /* If all TIDs in the queue are inactive - mark queue as inactive. */
  1090. if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
  1091. mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
  1092. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1)
  1093. mvmsta->tid_data[tid].is_tid_active = false;
  1094. IWL_DEBUG_TX_QUEUES(mvm, "Queue %d marked as inactive\n",
  1095. queue);
  1096. return;
  1097. }
  1098. /*
  1099. * If we are here, this is a shared queue and not all TIDs timed-out.
  1100. * Remove the ones that did.
  1101. */
  1102. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  1103. int mac_queue = mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]];
  1104. mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
  1105. mvm->hw_queue_to_mac80211[queue] &= ~BIT(mac_queue);
  1106. mvm->queue_info[queue].hw_queue_refcount--;
  1107. mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
  1108. mvmsta->tid_data[tid].is_tid_active = false;
  1109. IWL_DEBUG_TX_QUEUES(mvm,
  1110. "Removing inactive TID %d from shared Q:%d\n",
  1111. tid, queue);
  1112. }
  1113. IWL_DEBUG_TX_QUEUES(mvm,
  1114. "TXQ #%d left with tid bitmap 0x%x\n", queue,
  1115. mvm->queue_info[queue].tid_bitmap);
  1116. /*
  1117. * There may be different TIDs with the same mac queues, so make
  1118. * sure all TIDs have existing corresponding mac queues enabled
  1119. */
  1120. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  1121. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  1122. mvm->hw_queue_to_mac80211[queue] |=
  1123. BIT(mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]]);
  1124. }
  1125. /* If the queue is marked as shared - "unshare" it */
  1126. if (mvm->queue_info[queue].hw_queue_refcount == 1 &&
  1127. mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
  1128. mvm->queue_info[queue].status = IWL_MVM_QUEUE_RECONFIGURING;
  1129. IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
  1130. queue);
  1131. }
  1132. }
  1133. void iwl_mvm_inactivity_check(struct iwl_mvm *mvm)
  1134. {
  1135. unsigned long timeout_queues_map = 0;
  1136. unsigned long now = jiffies;
  1137. int i;
  1138. if (iwl_mvm_has_new_tx_api(mvm))
  1139. return;
  1140. spin_lock_bh(&mvm->queue_info_lock);
  1141. for (i = 0; i < IWL_MAX_HW_QUEUES; i++)
  1142. if (mvm->queue_info[i].hw_queue_refcount > 0)
  1143. timeout_queues_map |= BIT(i);
  1144. spin_unlock_bh(&mvm->queue_info_lock);
  1145. rcu_read_lock();
  1146. /*
  1147. * If a queue time outs - mark it as INACTIVE (don't remove right away
  1148. * if we don't have to.) This is an optimization in case traffic comes
  1149. * later, and we don't HAVE to use a currently-inactive queue
  1150. */
  1151. for_each_set_bit(i, &timeout_queues_map, IWL_MAX_HW_QUEUES) {
  1152. struct ieee80211_sta *sta;
  1153. struct iwl_mvm_sta *mvmsta;
  1154. u8 sta_id;
  1155. int tid;
  1156. unsigned long inactive_tid_bitmap = 0;
  1157. unsigned long queue_tid_bitmap;
  1158. spin_lock_bh(&mvm->queue_info_lock);
  1159. queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
  1160. /* If TXQ isn't in active use anyway - nothing to do here... */
  1161. if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
  1162. mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED) {
  1163. spin_unlock_bh(&mvm->queue_info_lock);
  1164. continue;
  1165. }
  1166. /* Check to see if there are inactive TIDs on this queue */
  1167. for_each_set_bit(tid, &queue_tid_bitmap,
  1168. IWL_MAX_TID_COUNT + 1) {
  1169. if (time_after(mvm->queue_info[i].last_frame_time[tid] +
  1170. IWL_MVM_DQA_QUEUE_TIMEOUT, now))
  1171. continue;
  1172. inactive_tid_bitmap |= BIT(tid);
  1173. }
  1174. spin_unlock_bh(&mvm->queue_info_lock);
  1175. /* If all TIDs are active - finish check on this queue */
  1176. if (!inactive_tid_bitmap)
  1177. continue;
  1178. /*
  1179. * If we are here - the queue hadn't been served recently and is
  1180. * in use
  1181. */
  1182. sta_id = mvm->queue_info[i].ra_sta_id;
  1183. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1184. /*
  1185. * If the STA doesn't exist anymore, it isn't an error. It could
  1186. * be that it was removed since getting the queues, and in this
  1187. * case it should've inactivated its queues anyway.
  1188. */
  1189. if (IS_ERR_OR_NULL(sta))
  1190. continue;
  1191. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1192. spin_lock_bh(&mvmsta->lock);
  1193. spin_lock(&mvm->queue_info_lock);
  1194. iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
  1195. inactive_tid_bitmap);
  1196. spin_unlock(&mvm->queue_info_lock);
  1197. spin_unlock_bh(&mvmsta->lock);
  1198. }
  1199. rcu_read_unlock();
  1200. }
  1201. void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
  1202. struct ieee80211_vif *vif,
  1203. const struct ieee80211_sta *sta,
  1204. u16 tid)
  1205. {
  1206. struct iwl_fw_dbg_trigger_tlv *trig;
  1207. struct iwl_fw_dbg_trigger_ba *ba_trig;
  1208. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
  1209. return;
  1210. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
  1211. ba_trig = (void *)trig->data;
  1212. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  1213. ieee80211_vif_to_wdev(vif), trig))
  1214. return;
  1215. if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
  1216. return;
  1217. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
  1218. "Frame from %pM timed out, tid %d",
  1219. sta->addr, tid);
  1220. }
  1221. u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
  1222. {
  1223. if (!elapsed)
  1224. return 0;
  1225. return (100 * airtime / elapsed) / USEC_PER_MSEC;
  1226. }
  1227. static enum iwl_mvm_traffic_load
  1228. iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
  1229. {
  1230. u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
  1231. if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
  1232. return IWL_MVM_TRAFFIC_HIGH;
  1233. if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
  1234. return IWL_MVM_TRAFFIC_MEDIUM;
  1235. return IWL_MVM_TRAFFIC_LOW;
  1236. }
  1237. struct iwl_mvm_tcm_iter_data {
  1238. struct iwl_mvm *mvm;
  1239. bool any_sent;
  1240. };
  1241. static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
  1242. {
  1243. struct iwl_mvm_tcm_iter_data *data = _data;
  1244. struct iwl_mvm *mvm = data->mvm;
  1245. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1246. bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
  1247. if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
  1248. return;
  1249. low_latency = mvm->tcm.result.low_latency[mvmvif->id];
  1250. if (!mvm->tcm.result.change[mvmvif->id] &&
  1251. prev == low_latency) {
  1252. iwl_mvm_update_quotas(mvm, false, NULL);
  1253. return;
  1254. }
  1255. if (prev != low_latency) {
  1256. /* this sends traffic load and updates quota as well */
  1257. iwl_mvm_update_low_latency(mvm, vif, low_latency,
  1258. LOW_LATENCY_TRAFFIC);
  1259. } else {
  1260. iwl_mvm_update_quotas(mvm, false, NULL);
  1261. }
  1262. data->any_sent = true;
  1263. }
  1264. static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
  1265. {
  1266. struct iwl_mvm_tcm_iter_data data = {
  1267. .mvm = mvm,
  1268. .any_sent = false,
  1269. };
  1270. mutex_lock(&mvm->mutex);
  1271. ieee80211_iterate_active_interfaces(
  1272. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1273. iwl_mvm_tcm_iter, &data);
  1274. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
  1275. iwl_mvm_config_scan(mvm);
  1276. mutex_unlock(&mvm->mutex);
  1277. }
  1278. static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
  1279. {
  1280. struct iwl_mvm *mvm;
  1281. struct iwl_mvm_vif *mvmvif;
  1282. struct ieee80211_vif *vif;
  1283. mvmvif = container_of(wk, struct iwl_mvm_vif,
  1284. uapsd_nonagg_detected_wk.work);
  1285. vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
  1286. mvm = mvmvif->mvm;
  1287. if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
  1288. return;
  1289. /* remember that this AP is broken */
  1290. memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
  1291. vif->bss_conf.bssid, ETH_ALEN);
  1292. mvm->uapsd_noagg_bssid_write_idx++;
  1293. if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
  1294. mvm->uapsd_noagg_bssid_write_idx = 0;
  1295. iwl_mvm_connection_loss(mvm, vif,
  1296. "AP isn't using AMPDU with uAPSD enabled");
  1297. }
  1298. static void iwl_mvm_uapsd_agg_disconnect_iter(void *data, u8 *mac,
  1299. struct ieee80211_vif *vif)
  1300. {
  1301. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1302. struct iwl_mvm *mvm = mvmvif->mvm;
  1303. int *mac_id = data;
  1304. if (vif->type != NL80211_IFTYPE_STATION)
  1305. return;
  1306. if (mvmvif->id != *mac_id)
  1307. return;
  1308. if (!vif->bss_conf.assoc)
  1309. return;
  1310. if (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
  1311. !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
  1312. !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
  1313. !mvmvif->queue_params[IEEE80211_AC_BK].uapsd)
  1314. return;
  1315. if (mvm->tcm.data[*mac_id].uapsd_nonagg_detect.detected)
  1316. return;
  1317. mvm->tcm.data[*mac_id].uapsd_nonagg_detect.detected = true;
  1318. IWL_INFO(mvm,
  1319. "detected AP should do aggregation but isn't, likely due to U-APSD\n");
  1320. schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 15 * HZ);
  1321. }
  1322. static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
  1323. unsigned int elapsed,
  1324. int mac)
  1325. {
  1326. u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
  1327. u64 tpt;
  1328. unsigned long rate;
  1329. rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
  1330. if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
  1331. mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
  1332. return;
  1333. if (iwl_mvm_has_new_rx_api(mvm)) {
  1334. tpt = 8 * bytes; /* kbps */
  1335. do_div(tpt, elapsed);
  1336. rate *= 1000; /* kbps */
  1337. if (tpt < 22 * rate / 100)
  1338. return;
  1339. } else {
  1340. /*
  1341. * the rate here is actually the threshold, in 100Kbps units,
  1342. * so do the needed conversion from bytes to 100Kbps:
  1343. * 100kb = bits / (100 * 1000),
  1344. * 100kbps = 100kb / (msecs / 1000) ==
  1345. * (bits / (100 * 1000)) / (msecs / 1000) ==
  1346. * bits / (100 * msecs)
  1347. */
  1348. tpt = (8 * bytes);
  1349. do_div(tpt, elapsed * 100);
  1350. if (tpt < rate)
  1351. return;
  1352. }
  1353. ieee80211_iterate_active_interfaces_atomic(
  1354. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1355. iwl_mvm_uapsd_agg_disconnect_iter, &mac);
  1356. }
  1357. static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
  1358. struct ieee80211_vif *vif)
  1359. {
  1360. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1361. u32 *band = _data;
  1362. if (!mvmvif->phy_ctxt)
  1363. return;
  1364. band[mvmvif->id] = mvmvif->phy_ctxt->channel->band;
  1365. }
  1366. static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
  1367. unsigned long ts,
  1368. bool handle_uapsd)
  1369. {
  1370. unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
  1371. unsigned int uapsd_elapsed =
  1372. jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
  1373. u32 total_airtime = 0;
  1374. u32 band_airtime[NUM_NL80211_BANDS] = {0};
  1375. u32 band[NUM_MAC_INDEX_DRIVER] = {0};
  1376. int ac, mac, i;
  1377. bool low_latency = false;
  1378. enum iwl_mvm_traffic_load load, band_load;
  1379. bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
  1380. if (handle_ll)
  1381. mvm->tcm.ll_ts = ts;
  1382. if (handle_uapsd)
  1383. mvm->tcm.uapsd_nonagg_ts = ts;
  1384. mvm->tcm.result.elapsed = elapsed;
  1385. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1386. IEEE80211_IFACE_ITER_NORMAL,
  1387. iwl_mvm_tcm_iterator,
  1388. &band);
  1389. for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
  1390. struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
  1391. u32 vo_vi_pkts = 0;
  1392. u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
  1393. total_airtime += airtime;
  1394. band_airtime[band[mac]] += airtime;
  1395. load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
  1396. mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
  1397. mvm->tcm.result.load[mac] = load;
  1398. mvm->tcm.result.airtime[mac] = airtime;
  1399. for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
  1400. vo_vi_pkts += mdata->rx.pkts[ac] +
  1401. mdata->tx.pkts[ac];
  1402. /* enable immediately with enough packets but defer disabling */
  1403. if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
  1404. mvm->tcm.result.low_latency[mac] = true;
  1405. else if (handle_ll)
  1406. mvm->tcm.result.low_latency[mac] = false;
  1407. if (handle_ll) {
  1408. /* clear old data */
  1409. memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
  1410. memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
  1411. }
  1412. low_latency |= mvm->tcm.result.low_latency[mac];
  1413. if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
  1414. iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
  1415. mac);
  1416. /* clear old data */
  1417. if (handle_uapsd)
  1418. mdata->uapsd_nonagg_detect.rx_bytes = 0;
  1419. memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
  1420. memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
  1421. }
  1422. load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
  1423. mvm->tcm.result.global_change = load != mvm->tcm.result.global_load;
  1424. mvm->tcm.result.global_load = load;
  1425. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1426. band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
  1427. mvm->tcm.result.band_load[i] = band_load;
  1428. }
  1429. /*
  1430. * If the current load isn't low we need to force re-evaluation
  1431. * in the TCM period, so that we can return to low load if there
  1432. * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
  1433. * triggered by traffic).
  1434. */
  1435. if (load != IWL_MVM_TRAFFIC_LOW)
  1436. return MVM_TCM_PERIOD;
  1437. /*
  1438. * If low-latency is active we need to force re-evaluation after
  1439. * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
  1440. * when there's no traffic at all.
  1441. */
  1442. if (low_latency)
  1443. return MVM_LL_PERIOD;
  1444. /*
  1445. * Otherwise, we don't need to run the work struct because we're
  1446. * in the default "idle" state - traffic indication is low (which
  1447. * also covers the "no traffic" case) and low-latency is disabled
  1448. * so there's no state that may need to be disabled when there's
  1449. * no traffic at all.
  1450. *
  1451. * Note that this has no impact on the regular scheduling of the
  1452. * updates triggered by traffic - those happen whenever one of the
  1453. * two timeouts expire (if there's traffic at all.)
  1454. */
  1455. return 0;
  1456. }
  1457. void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
  1458. {
  1459. unsigned long ts = jiffies;
  1460. bool handle_uapsd =
  1461. time_after(ts, mvm->tcm.uapsd_nonagg_ts +
  1462. msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
  1463. spin_lock(&mvm->tcm.lock);
  1464. if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
  1465. spin_unlock(&mvm->tcm.lock);
  1466. return;
  1467. }
  1468. spin_unlock(&mvm->tcm.lock);
  1469. if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
  1470. mutex_lock(&mvm->mutex);
  1471. if (iwl_mvm_request_statistics(mvm, true))
  1472. handle_uapsd = false;
  1473. mutex_unlock(&mvm->mutex);
  1474. }
  1475. spin_lock(&mvm->tcm.lock);
  1476. /* re-check if somebody else won the recheck race */
  1477. if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
  1478. /* calculate statistics */
  1479. unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
  1480. handle_uapsd);
  1481. /* the memset needs to be visible before the timestamp */
  1482. smp_mb();
  1483. mvm->tcm.ts = ts;
  1484. if (work_delay)
  1485. schedule_delayed_work(&mvm->tcm.work, work_delay);
  1486. }
  1487. spin_unlock(&mvm->tcm.lock);
  1488. iwl_mvm_tcm_results(mvm);
  1489. }
  1490. void iwl_mvm_tcm_work(struct work_struct *work)
  1491. {
  1492. struct delayed_work *delayed_work = to_delayed_work(work);
  1493. struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
  1494. tcm.work);
  1495. iwl_mvm_recalc_tcm(mvm);
  1496. }
  1497. void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
  1498. {
  1499. spin_lock_bh(&mvm->tcm.lock);
  1500. mvm->tcm.paused = true;
  1501. spin_unlock_bh(&mvm->tcm.lock);
  1502. if (with_cancel)
  1503. cancel_delayed_work_sync(&mvm->tcm.work);
  1504. }
  1505. void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
  1506. {
  1507. int mac;
  1508. spin_lock_bh(&mvm->tcm.lock);
  1509. mvm->tcm.ts = jiffies;
  1510. mvm->tcm.ll_ts = jiffies;
  1511. for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
  1512. struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
  1513. memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
  1514. memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
  1515. memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
  1516. memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
  1517. }
  1518. /* The TCM data needs to be reset before "paused" flag changes */
  1519. smp_mb();
  1520. mvm->tcm.paused = false;
  1521. spin_unlock_bh(&mvm->tcm.lock);
  1522. }
  1523. void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1524. {
  1525. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1526. INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
  1527. iwl_mvm_tcm_uapsd_nonagg_detected_wk);
  1528. }
  1529. void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1530. {
  1531. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1532. cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
  1533. }
  1534. void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
  1535. {
  1536. bool ps_disabled;
  1537. lockdep_assert_held(&mvm->mutex);
  1538. /* Disable power save when reading GP2 */
  1539. ps_disabled = mvm->ps_disabled;
  1540. if (!ps_disabled) {
  1541. mvm->ps_disabled = true;
  1542. iwl_mvm_power_update_device(mvm);
  1543. }
  1544. *gp2 = iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
  1545. *boottime = ktime_get_boot_ns();
  1546. if (!ps_disabled) {
  1547. mvm->ps_disabled = ps_disabled;
  1548. iwl_mvm_power_update_device(mvm);
  1549. }
  1550. }