utils.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  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. * @init: This command is sent as part of station initialization right
  781. * after station has been added.
  782. *
  783. * The link quality command is sent as the last step of station creation.
  784. * This is the special case in which init is set and we call a callback in
  785. * this case to clear the state indicating that station creation is in
  786. * progress.
  787. */
  788. int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq, bool init)
  789. {
  790. struct iwl_host_cmd cmd = {
  791. .id = LQ_CMD,
  792. .len = { sizeof(struct iwl_lq_cmd), },
  793. .flags = init ? 0 : CMD_ASYNC,
  794. .data = { lq, },
  795. };
  796. if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA ||
  797. iwl_mvm_has_tlc_offload(mvm)))
  798. return -EINVAL;
  799. return iwl_mvm_send_cmd(mvm, &cmd);
  800. }
  801. /**
  802. * iwl_mvm_update_smps - Get a request to change the SMPS mode
  803. * @req_type: The part of the driver who call for a change.
  804. * @smps_requests: The request to change the SMPS mode.
  805. *
  806. * Get a requst to change the SMPS mode,
  807. * and change it according to all other requests in the driver.
  808. */
  809. void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  810. enum iwl_mvm_smps_type_request req_type,
  811. enum ieee80211_smps_mode smps_request)
  812. {
  813. struct iwl_mvm_vif *mvmvif;
  814. enum ieee80211_smps_mode smps_mode;
  815. int i;
  816. lockdep_assert_held(&mvm->mutex);
  817. /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
  818. if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
  819. return;
  820. if (vif->type == NL80211_IFTYPE_AP)
  821. smps_mode = IEEE80211_SMPS_OFF;
  822. else
  823. smps_mode = IEEE80211_SMPS_AUTOMATIC;
  824. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  825. mvmvif->smps_requests[req_type] = smps_request;
  826. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
  827. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
  828. smps_mode = IEEE80211_SMPS_STATIC;
  829. break;
  830. }
  831. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
  832. smps_mode = IEEE80211_SMPS_DYNAMIC;
  833. }
  834. ieee80211_request_smps(vif, smps_mode);
  835. }
  836. int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
  837. {
  838. struct iwl_statistics_cmd scmd = {
  839. .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
  840. };
  841. struct iwl_host_cmd cmd = {
  842. .id = STATISTICS_CMD,
  843. .len[0] = sizeof(scmd),
  844. .data[0] = &scmd,
  845. .flags = CMD_WANT_SKB,
  846. };
  847. int ret;
  848. ret = iwl_mvm_send_cmd(mvm, &cmd);
  849. if (ret)
  850. return ret;
  851. iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
  852. iwl_free_resp(&cmd);
  853. if (clear)
  854. iwl_mvm_accu_radio_stats(mvm);
  855. return 0;
  856. }
  857. void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
  858. {
  859. mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
  860. mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
  861. mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
  862. mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
  863. }
  864. static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
  865. struct ieee80211_vif *vif)
  866. {
  867. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  868. bool *result = _data;
  869. int i;
  870. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
  871. if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
  872. mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
  873. *result = false;
  874. }
  875. }
  876. bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
  877. {
  878. bool result = true;
  879. lockdep_assert_held(&mvm->mutex);
  880. if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
  881. return false;
  882. if (mvm->cfg->rx_with_siso_diversity)
  883. return false;
  884. ieee80211_iterate_active_interfaces_atomic(
  885. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  886. iwl_mvm_diversity_iter, &result);
  887. return result;
  888. }
  889. int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  890. bool low_latency,
  891. enum iwl_mvm_low_latency_cause cause)
  892. {
  893. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  894. int res;
  895. bool prev;
  896. lockdep_assert_held(&mvm->mutex);
  897. prev = iwl_mvm_vif_low_latency(mvmvif);
  898. iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
  899. low_latency = iwl_mvm_vif_low_latency(mvmvif);
  900. if (low_latency == prev)
  901. return 0;
  902. if (fw_has_capa(&mvm->fw->ucode_capa,
  903. IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA)) {
  904. struct iwl_mac_low_latency_cmd cmd = {
  905. .mac_id = cpu_to_le32(mvmvif->id)
  906. };
  907. if (low_latency) {
  908. /* currently we don't care about the direction */
  909. cmd.low_latency_rx = 1;
  910. cmd.low_latency_tx = 1;
  911. }
  912. res = iwl_mvm_send_cmd_pdu(mvm,
  913. iwl_cmd_id(LOW_LATENCY_CMD,
  914. MAC_CONF_GROUP, 0),
  915. 0, sizeof(cmd), &cmd);
  916. if (res)
  917. IWL_ERR(mvm, "Failed to send low latency command\n");
  918. }
  919. res = iwl_mvm_update_quotas(mvm, false, NULL);
  920. if (res)
  921. return res;
  922. iwl_mvm_bt_coex_vif_change(mvm);
  923. return iwl_mvm_power_update_mac(mvm);
  924. }
  925. struct iwl_mvm_low_latency_iter {
  926. bool result;
  927. bool result_per_band[NUM_NL80211_BANDS];
  928. };
  929. static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
  930. {
  931. struct iwl_mvm_low_latency_iter *result = _data;
  932. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  933. enum nl80211_band band;
  934. if (iwl_mvm_vif_low_latency(mvmvif)) {
  935. result->result = true;
  936. if (!mvmvif->phy_ctxt)
  937. return;
  938. band = mvmvif->phy_ctxt->channel->band;
  939. result->result_per_band[band] = true;
  940. }
  941. }
  942. bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
  943. {
  944. struct iwl_mvm_low_latency_iter data = {};
  945. ieee80211_iterate_active_interfaces_atomic(
  946. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  947. iwl_mvm_ll_iter, &data);
  948. return data.result;
  949. }
  950. bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
  951. {
  952. struct iwl_mvm_low_latency_iter data = {};
  953. ieee80211_iterate_active_interfaces_atomic(
  954. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  955. iwl_mvm_ll_iter, &data);
  956. return data.result_per_band[band];
  957. }
  958. struct iwl_bss_iter_data {
  959. struct ieee80211_vif *vif;
  960. bool error;
  961. };
  962. static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
  963. struct ieee80211_vif *vif)
  964. {
  965. struct iwl_bss_iter_data *data = _data;
  966. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  967. return;
  968. if (data->vif) {
  969. data->error = true;
  970. return;
  971. }
  972. data->vif = vif;
  973. }
  974. struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
  975. {
  976. struct iwl_bss_iter_data bss_iter_data = {};
  977. ieee80211_iterate_active_interfaces_atomic(
  978. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  979. iwl_mvm_bss_iface_iterator, &bss_iter_data);
  980. if (bss_iter_data.error) {
  981. IWL_ERR(mvm, "More than one managed interface active!\n");
  982. return ERR_PTR(-EINVAL);
  983. }
  984. return bss_iter_data.vif;
  985. }
  986. struct iwl_sta_iter_data {
  987. bool assoc;
  988. };
  989. static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
  990. struct ieee80211_vif *vif)
  991. {
  992. struct iwl_sta_iter_data *data = _data;
  993. if (vif->type != NL80211_IFTYPE_STATION)
  994. return;
  995. if (vif->bss_conf.assoc)
  996. data->assoc = true;
  997. }
  998. bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
  999. {
  1000. struct iwl_sta_iter_data data = {
  1001. .assoc = false,
  1002. };
  1003. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1004. IEEE80211_IFACE_ITER_NORMAL,
  1005. iwl_mvm_sta_iface_iterator,
  1006. &data);
  1007. return data.assoc;
  1008. }
  1009. unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
  1010. struct ieee80211_vif *vif,
  1011. bool tdls, bool cmd_q)
  1012. {
  1013. struct iwl_fw_dbg_trigger_tlv *trigger;
  1014. struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
  1015. unsigned int default_timeout =
  1016. cmd_q ? IWL_DEF_WD_TIMEOUT : mvm->cfg->base_params->wd_timeout;
  1017. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
  1018. /*
  1019. * We can't know when the station is asleep or awake, so we
  1020. * must disable the queue hang detection.
  1021. */
  1022. if (fw_has_capa(&mvm->fw->ucode_capa,
  1023. IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
  1024. vif && vif->type == NL80211_IFTYPE_AP)
  1025. return IWL_WATCHDOG_DISABLED;
  1026. return iwlmvm_mod_params.tfd_q_hang_detect ?
  1027. default_timeout : IWL_WATCHDOG_DISABLED;
  1028. }
  1029. trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
  1030. txq_timer = (void *)trigger->data;
  1031. if (tdls)
  1032. return le32_to_cpu(txq_timer->tdls);
  1033. if (cmd_q)
  1034. return le32_to_cpu(txq_timer->command_queue);
  1035. if (WARN_ON(!vif))
  1036. return default_timeout;
  1037. switch (ieee80211_vif_type_p2p(vif)) {
  1038. case NL80211_IFTYPE_ADHOC:
  1039. return le32_to_cpu(txq_timer->ibss);
  1040. case NL80211_IFTYPE_STATION:
  1041. return le32_to_cpu(txq_timer->bss);
  1042. case NL80211_IFTYPE_AP:
  1043. return le32_to_cpu(txq_timer->softap);
  1044. case NL80211_IFTYPE_P2P_CLIENT:
  1045. return le32_to_cpu(txq_timer->p2p_client);
  1046. case NL80211_IFTYPE_P2P_GO:
  1047. return le32_to_cpu(txq_timer->p2p_go);
  1048. case NL80211_IFTYPE_P2P_DEVICE:
  1049. return le32_to_cpu(txq_timer->p2p_device);
  1050. case NL80211_IFTYPE_MONITOR:
  1051. return default_timeout;
  1052. default:
  1053. WARN_ON(1);
  1054. return mvm->cfg->base_params->wd_timeout;
  1055. }
  1056. }
  1057. void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1058. const char *errmsg)
  1059. {
  1060. struct iwl_fw_dbg_trigger_tlv *trig;
  1061. struct iwl_fw_dbg_trigger_mlme *trig_mlme;
  1062. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
  1063. goto out;
  1064. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
  1065. trig_mlme = (void *)trig->data;
  1066. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  1067. ieee80211_vif_to_wdev(vif), trig))
  1068. goto out;
  1069. if (trig_mlme->stop_connection_loss &&
  1070. --trig_mlme->stop_connection_loss)
  1071. goto out;
  1072. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
  1073. out:
  1074. ieee80211_connection_loss(vif);
  1075. }
  1076. /*
  1077. * Remove inactive TIDs of a given queue.
  1078. * If all queue TIDs are inactive - mark the queue as inactive
  1079. * If only some the queue TIDs are inactive - unmap them from the queue
  1080. */
  1081. static void iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
  1082. struct iwl_mvm_sta *mvmsta, int queue,
  1083. unsigned long tid_bitmap)
  1084. {
  1085. int tid;
  1086. lockdep_assert_held(&mvmsta->lock);
  1087. lockdep_assert_held(&mvm->queue_info_lock);
  1088. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  1089. return;
  1090. /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
  1091. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  1092. /* If some TFDs are still queued - don't mark TID as inactive */
  1093. if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
  1094. tid_bitmap &= ~BIT(tid);
  1095. /* Don't mark as inactive any TID that has an active BA */
  1096. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
  1097. tid_bitmap &= ~BIT(tid);
  1098. }
  1099. /* If all TIDs in the queue are inactive - mark queue as inactive. */
  1100. if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
  1101. mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
  1102. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1)
  1103. mvmsta->tid_data[tid].is_tid_active = false;
  1104. IWL_DEBUG_TX_QUEUES(mvm, "Queue %d marked as inactive\n",
  1105. queue);
  1106. return;
  1107. }
  1108. /*
  1109. * If we are here, this is a shared queue and not all TIDs timed-out.
  1110. * Remove the ones that did.
  1111. */
  1112. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  1113. int mac_queue = mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]];
  1114. mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
  1115. mvm->hw_queue_to_mac80211[queue] &= ~BIT(mac_queue);
  1116. mvm->queue_info[queue].hw_queue_refcount--;
  1117. mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
  1118. mvmsta->tid_data[tid].is_tid_active = false;
  1119. IWL_DEBUG_TX_QUEUES(mvm,
  1120. "Removing inactive TID %d from shared Q:%d\n",
  1121. tid, queue);
  1122. }
  1123. IWL_DEBUG_TX_QUEUES(mvm,
  1124. "TXQ #%d left with tid bitmap 0x%x\n", queue,
  1125. mvm->queue_info[queue].tid_bitmap);
  1126. /*
  1127. * There may be different TIDs with the same mac queues, so make
  1128. * sure all TIDs have existing corresponding mac queues enabled
  1129. */
  1130. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  1131. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  1132. mvm->hw_queue_to_mac80211[queue] |=
  1133. BIT(mvmsta->vif->hw_queue[tid_to_mac80211_ac[tid]]);
  1134. }
  1135. /* If the queue is marked as shared - "unshare" it */
  1136. if (mvm->queue_info[queue].hw_queue_refcount == 1 &&
  1137. mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
  1138. mvm->queue_info[queue].status = IWL_MVM_QUEUE_RECONFIGURING;
  1139. IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
  1140. queue);
  1141. }
  1142. }
  1143. void iwl_mvm_inactivity_check(struct iwl_mvm *mvm)
  1144. {
  1145. unsigned long timeout_queues_map = 0;
  1146. unsigned long now = jiffies;
  1147. int i;
  1148. if (iwl_mvm_has_new_tx_api(mvm))
  1149. return;
  1150. spin_lock_bh(&mvm->queue_info_lock);
  1151. for (i = 0; i < IWL_MAX_HW_QUEUES; i++)
  1152. if (mvm->queue_info[i].hw_queue_refcount > 0)
  1153. timeout_queues_map |= BIT(i);
  1154. spin_unlock_bh(&mvm->queue_info_lock);
  1155. rcu_read_lock();
  1156. /*
  1157. * If a queue time outs - mark it as INACTIVE (don't remove right away
  1158. * if we don't have to.) This is an optimization in case traffic comes
  1159. * later, and we don't HAVE to use a currently-inactive queue
  1160. */
  1161. for_each_set_bit(i, &timeout_queues_map, IWL_MAX_HW_QUEUES) {
  1162. struct ieee80211_sta *sta;
  1163. struct iwl_mvm_sta *mvmsta;
  1164. u8 sta_id;
  1165. int tid;
  1166. unsigned long inactive_tid_bitmap = 0;
  1167. unsigned long queue_tid_bitmap;
  1168. spin_lock_bh(&mvm->queue_info_lock);
  1169. queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
  1170. /* If TXQ isn't in active use anyway - nothing to do here... */
  1171. if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
  1172. mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED) {
  1173. spin_unlock_bh(&mvm->queue_info_lock);
  1174. continue;
  1175. }
  1176. /* Check to see if there are inactive TIDs on this queue */
  1177. for_each_set_bit(tid, &queue_tid_bitmap,
  1178. IWL_MAX_TID_COUNT + 1) {
  1179. if (time_after(mvm->queue_info[i].last_frame_time[tid] +
  1180. IWL_MVM_DQA_QUEUE_TIMEOUT, now))
  1181. continue;
  1182. inactive_tid_bitmap |= BIT(tid);
  1183. }
  1184. spin_unlock_bh(&mvm->queue_info_lock);
  1185. /* If all TIDs are active - finish check on this queue */
  1186. if (!inactive_tid_bitmap)
  1187. continue;
  1188. /*
  1189. * If we are here - the queue hadn't been served recently and is
  1190. * in use
  1191. */
  1192. sta_id = mvm->queue_info[i].ra_sta_id;
  1193. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1194. /*
  1195. * If the STA doesn't exist anymore, it isn't an error. It could
  1196. * be that it was removed since getting the queues, and in this
  1197. * case it should've inactivated its queues anyway.
  1198. */
  1199. if (IS_ERR_OR_NULL(sta))
  1200. continue;
  1201. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1202. spin_lock_bh(&mvmsta->lock);
  1203. spin_lock(&mvm->queue_info_lock);
  1204. iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
  1205. inactive_tid_bitmap);
  1206. spin_unlock(&mvm->queue_info_lock);
  1207. spin_unlock_bh(&mvmsta->lock);
  1208. }
  1209. rcu_read_unlock();
  1210. }
  1211. void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
  1212. struct ieee80211_vif *vif,
  1213. const struct ieee80211_sta *sta,
  1214. u16 tid)
  1215. {
  1216. struct iwl_fw_dbg_trigger_tlv *trig;
  1217. struct iwl_fw_dbg_trigger_ba *ba_trig;
  1218. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
  1219. return;
  1220. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
  1221. ba_trig = (void *)trig->data;
  1222. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  1223. ieee80211_vif_to_wdev(vif), trig))
  1224. return;
  1225. if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
  1226. return;
  1227. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
  1228. "Frame from %pM timed out, tid %d",
  1229. sta->addr, tid);
  1230. }
  1231. u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
  1232. {
  1233. if (!elapsed)
  1234. return 0;
  1235. return (100 * airtime / elapsed) / USEC_PER_MSEC;
  1236. }
  1237. static enum iwl_mvm_traffic_load
  1238. iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
  1239. {
  1240. u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
  1241. if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
  1242. return IWL_MVM_TRAFFIC_HIGH;
  1243. if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
  1244. return IWL_MVM_TRAFFIC_MEDIUM;
  1245. return IWL_MVM_TRAFFIC_LOW;
  1246. }
  1247. struct iwl_mvm_tcm_iter_data {
  1248. struct iwl_mvm *mvm;
  1249. bool any_sent;
  1250. };
  1251. static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
  1252. {
  1253. struct iwl_mvm_tcm_iter_data *data = _data;
  1254. struct iwl_mvm *mvm = data->mvm;
  1255. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1256. bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
  1257. if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
  1258. return;
  1259. low_latency = mvm->tcm.result.low_latency[mvmvif->id];
  1260. if (!mvm->tcm.result.change[mvmvif->id] &&
  1261. prev == low_latency) {
  1262. iwl_mvm_update_quotas(mvm, false, NULL);
  1263. return;
  1264. }
  1265. if (prev != low_latency) {
  1266. /* this sends traffic load and updates quota as well */
  1267. iwl_mvm_update_low_latency(mvm, vif, low_latency,
  1268. LOW_LATENCY_TRAFFIC);
  1269. } else {
  1270. iwl_mvm_update_quotas(mvm, false, NULL);
  1271. }
  1272. data->any_sent = true;
  1273. }
  1274. static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
  1275. {
  1276. struct iwl_mvm_tcm_iter_data data = {
  1277. .mvm = mvm,
  1278. .any_sent = false,
  1279. };
  1280. mutex_lock(&mvm->mutex);
  1281. ieee80211_iterate_active_interfaces(
  1282. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1283. iwl_mvm_tcm_iter, &data);
  1284. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
  1285. iwl_mvm_config_scan(mvm);
  1286. mutex_unlock(&mvm->mutex);
  1287. }
  1288. static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
  1289. {
  1290. struct iwl_mvm *mvm;
  1291. struct iwl_mvm_vif *mvmvif;
  1292. struct ieee80211_vif *vif;
  1293. mvmvif = container_of(wk, struct iwl_mvm_vif,
  1294. uapsd_nonagg_detected_wk.work);
  1295. vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
  1296. mvm = mvmvif->mvm;
  1297. if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
  1298. return;
  1299. /* remember that this AP is broken */
  1300. memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
  1301. vif->bss_conf.bssid, ETH_ALEN);
  1302. mvm->uapsd_noagg_bssid_write_idx++;
  1303. if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
  1304. mvm->uapsd_noagg_bssid_write_idx = 0;
  1305. iwl_mvm_connection_loss(mvm, vif,
  1306. "AP isn't using AMPDU with uAPSD enabled");
  1307. }
  1308. static void iwl_mvm_uapsd_agg_disconnect_iter(void *data, u8 *mac,
  1309. struct ieee80211_vif *vif)
  1310. {
  1311. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1312. struct iwl_mvm *mvm = mvmvif->mvm;
  1313. int *mac_id = data;
  1314. if (vif->type != NL80211_IFTYPE_STATION)
  1315. return;
  1316. if (mvmvif->id != *mac_id)
  1317. return;
  1318. if (!vif->bss_conf.assoc)
  1319. return;
  1320. if (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
  1321. !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
  1322. !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
  1323. !mvmvif->queue_params[IEEE80211_AC_BK].uapsd)
  1324. return;
  1325. if (mvm->tcm.data[*mac_id].uapsd_nonagg_detect.detected)
  1326. return;
  1327. mvm->tcm.data[*mac_id].uapsd_nonagg_detect.detected = true;
  1328. IWL_INFO(mvm,
  1329. "detected AP should do aggregation but isn't, likely due to U-APSD\n");
  1330. schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 15 * HZ);
  1331. }
  1332. static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
  1333. unsigned int elapsed,
  1334. int mac)
  1335. {
  1336. u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
  1337. u64 tpt;
  1338. unsigned long rate;
  1339. rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
  1340. if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
  1341. mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
  1342. return;
  1343. if (iwl_mvm_has_new_rx_api(mvm)) {
  1344. tpt = 8 * bytes; /* kbps */
  1345. do_div(tpt, elapsed);
  1346. rate *= 1000; /* kbps */
  1347. if (tpt < 22 * rate / 100)
  1348. return;
  1349. } else {
  1350. /*
  1351. * the rate here is actually the threshold, in 100Kbps units,
  1352. * so do the needed conversion from bytes to 100Kbps:
  1353. * 100kb = bits / (100 * 1000),
  1354. * 100kbps = 100kb / (msecs / 1000) ==
  1355. * (bits / (100 * 1000)) / (msecs / 1000) ==
  1356. * bits / (100 * msecs)
  1357. */
  1358. tpt = (8 * bytes);
  1359. do_div(tpt, elapsed * 100);
  1360. if (tpt < rate)
  1361. return;
  1362. }
  1363. ieee80211_iterate_active_interfaces_atomic(
  1364. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1365. iwl_mvm_uapsd_agg_disconnect_iter, &mac);
  1366. }
  1367. static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
  1368. struct ieee80211_vif *vif)
  1369. {
  1370. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1371. u32 *band = _data;
  1372. if (!mvmvif->phy_ctxt)
  1373. return;
  1374. band[mvmvif->id] = mvmvif->phy_ctxt->channel->band;
  1375. }
  1376. static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
  1377. unsigned long ts,
  1378. bool handle_uapsd)
  1379. {
  1380. unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
  1381. unsigned int uapsd_elapsed =
  1382. jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
  1383. u32 total_airtime = 0;
  1384. u32 band_airtime[NUM_NL80211_BANDS] = {0};
  1385. u32 band[NUM_MAC_INDEX_DRIVER] = {0};
  1386. int ac, mac, i;
  1387. bool low_latency = false;
  1388. enum iwl_mvm_traffic_load load, band_load;
  1389. bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
  1390. if (handle_ll)
  1391. mvm->tcm.ll_ts = ts;
  1392. if (handle_uapsd)
  1393. mvm->tcm.uapsd_nonagg_ts = ts;
  1394. mvm->tcm.result.elapsed = elapsed;
  1395. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1396. IEEE80211_IFACE_ITER_NORMAL,
  1397. iwl_mvm_tcm_iterator,
  1398. &band);
  1399. for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
  1400. struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
  1401. u32 vo_vi_pkts = 0;
  1402. u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
  1403. total_airtime += airtime;
  1404. band_airtime[band[mac]] += airtime;
  1405. load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
  1406. mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
  1407. mvm->tcm.result.load[mac] = load;
  1408. mvm->tcm.result.airtime[mac] = airtime;
  1409. for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
  1410. vo_vi_pkts += mdata->rx.pkts[ac] +
  1411. mdata->tx.pkts[ac];
  1412. /* enable immediately with enough packets but defer disabling */
  1413. if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
  1414. mvm->tcm.result.low_latency[mac] = true;
  1415. else if (handle_ll)
  1416. mvm->tcm.result.low_latency[mac] = false;
  1417. if (handle_ll) {
  1418. /* clear old data */
  1419. memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
  1420. memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
  1421. }
  1422. low_latency |= mvm->tcm.result.low_latency[mac];
  1423. if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
  1424. iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
  1425. mac);
  1426. /* clear old data */
  1427. if (handle_uapsd)
  1428. mdata->uapsd_nonagg_detect.rx_bytes = 0;
  1429. memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
  1430. memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
  1431. }
  1432. load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
  1433. mvm->tcm.result.global_change = load != mvm->tcm.result.global_load;
  1434. mvm->tcm.result.global_load = load;
  1435. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1436. band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
  1437. mvm->tcm.result.band_load[i] = band_load;
  1438. }
  1439. /*
  1440. * If the current load isn't low we need to force re-evaluation
  1441. * in the TCM period, so that we can return to low load if there
  1442. * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
  1443. * triggered by traffic).
  1444. */
  1445. if (load != IWL_MVM_TRAFFIC_LOW)
  1446. return MVM_TCM_PERIOD;
  1447. /*
  1448. * If low-latency is active we need to force re-evaluation after
  1449. * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
  1450. * when there's no traffic at all.
  1451. */
  1452. if (low_latency)
  1453. return MVM_LL_PERIOD;
  1454. /*
  1455. * Otherwise, we don't need to run the work struct because we're
  1456. * in the default "idle" state - traffic indication is low (which
  1457. * also covers the "no traffic" case) and low-latency is disabled
  1458. * so there's no state that may need to be disabled when there's
  1459. * no traffic at all.
  1460. *
  1461. * Note that this has no impact on the regular scheduling of the
  1462. * updates triggered by traffic - those happen whenever one of the
  1463. * two timeouts expire (if there's traffic at all.)
  1464. */
  1465. return 0;
  1466. }
  1467. void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
  1468. {
  1469. unsigned long ts = jiffies;
  1470. bool handle_uapsd =
  1471. time_after(ts, mvm->tcm.uapsd_nonagg_ts +
  1472. msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
  1473. spin_lock(&mvm->tcm.lock);
  1474. if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
  1475. spin_unlock(&mvm->tcm.lock);
  1476. return;
  1477. }
  1478. spin_unlock(&mvm->tcm.lock);
  1479. if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
  1480. mutex_lock(&mvm->mutex);
  1481. if (iwl_mvm_request_statistics(mvm, true))
  1482. handle_uapsd = false;
  1483. mutex_unlock(&mvm->mutex);
  1484. }
  1485. spin_lock(&mvm->tcm.lock);
  1486. /* re-check if somebody else won the recheck race */
  1487. if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
  1488. /* calculate statistics */
  1489. unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
  1490. handle_uapsd);
  1491. /* the memset needs to be visible before the timestamp */
  1492. smp_mb();
  1493. mvm->tcm.ts = ts;
  1494. if (work_delay)
  1495. schedule_delayed_work(&mvm->tcm.work, work_delay);
  1496. }
  1497. spin_unlock(&mvm->tcm.lock);
  1498. iwl_mvm_tcm_results(mvm);
  1499. }
  1500. void iwl_mvm_tcm_work(struct work_struct *work)
  1501. {
  1502. struct delayed_work *delayed_work = to_delayed_work(work);
  1503. struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
  1504. tcm.work);
  1505. iwl_mvm_recalc_tcm(mvm);
  1506. }
  1507. void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
  1508. {
  1509. spin_lock_bh(&mvm->tcm.lock);
  1510. mvm->tcm.paused = true;
  1511. spin_unlock_bh(&mvm->tcm.lock);
  1512. if (with_cancel)
  1513. cancel_delayed_work_sync(&mvm->tcm.work);
  1514. }
  1515. void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
  1516. {
  1517. int mac;
  1518. spin_lock_bh(&mvm->tcm.lock);
  1519. mvm->tcm.ts = jiffies;
  1520. mvm->tcm.ll_ts = jiffies;
  1521. for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
  1522. struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
  1523. memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
  1524. memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
  1525. memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
  1526. memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
  1527. }
  1528. /* The TCM data needs to be reset before "paused" flag changes */
  1529. smp_mb();
  1530. mvm->tcm.paused = false;
  1531. spin_unlock_bh(&mvm->tcm.lock);
  1532. }
  1533. void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1534. {
  1535. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1536. INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
  1537. iwl_mvm_tcm_uapsd_nonagg_detected_wk);
  1538. }
  1539. void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1540. {
  1541. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1542. cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
  1543. }
  1544. void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
  1545. {
  1546. bool ps_disabled;
  1547. lockdep_assert_held(&mvm->mutex);
  1548. /* Disable power save when reading GP2 */
  1549. ps_disabled = mvm->ps_disabled;
  1550. if (!ps_disabled) {
  1551. mvm->ps_disabled = true;
  1552. iwl_mvm_power_update_device(mvm);
  1553. }
  1554. *gp2 = iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
  1555. *boottime = ktime_get_boot_ns();
  1556. if (!ps_disabled) {
  1557. mvm->ps_disabled = ps_disabled;
  1558. iwl_mvm_power_update_device(mvm);
  1559. }
  1560. }