utils.c 53 KB

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