ops.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/module.h>
  66. #include <linux/vmalloc.h>
  67. #include <net/mac80211.h>
  68. #include "iwl-notif-wait.h"
  69. #include "iwl-trans.h"
  70. #include "iwl-op-mode.h"
  71. #include "iwl-fw.h"
  72. #include "iwl-debug.h"
  73. #include "iwl-drv.h"
  74. #include "iwl-modparams.h"
  75. #include "mvm.h"
  76. #include "iwl-phy-db.h"
  77. #include "iwl-eeprom-parse.h"
  78. #include "iwl-csr.h"
  79. #include "iwl-io.h"
  80. #include "iwl-prph.h"
  81. #include "rs.h"
  82. #include "fw-api-scan.h"
  83. #include "time-event.h"
  84. #define DRV_DESCRIPTION "The new Intel(R) wireless AGN driver for Linux"
  85. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  86. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  87. MODULE_LICENSE("GPL");
  88. static const struct iwl_op_mode_ops iwl_mvm_ops;
  89. struct iwl_mvm_mod_params iwlmvm_mod_params = {
  90. .power_scheme = IWL_POWER_SCHEME_BPS,
  91. .tfd_q_hang_detect = true
  92. /* rest of fields are 0 by default */
  93. };
  94. module_param_named(init_dbg, iwlmvm_mod_params.init_dbg, bool, S_IRUGO);
  95. MODULE_PARM_DESC(init_dbg,
  96. "set to true to debug an ASSERT in INIT fw (default: false");
  97. module_param_named(power_scheme, iwlmvm_mod_params.power_scheme, int, S_IRUGO);
  98. MODULE_PARM_DESC(power_scheme,
  99. "power management scheme: 1-active, 2-balanced, 3-low power, default: 2");
  100. module_param_named(tfd_q_hang_detect, iwlmvm_mod_params.tfd_q_hang_detect,
  101. bool, S_IRUGO);
  102. MODULE_PARM_DESC(tfd_q_hang_detect,
  103. "TFD queues hang detection (default: true");
  104. /*
  105. * module init and exit functions
  106. */
  107. static int __init iwl_mvm_init(void)
  108. {
  109. int ret;
  110. ret = iwl_mvm_rate_control_register();
  111. if (ret) {
  112. pr_err("Unable to register rate control algorithm: %d\n", ret);
  113. return ret;
  114. }
  115. ret = iwl_opmode_register("iwlmvm", &iwl_mvm_ops);
  116. if (ret) {
  117. pr_err("Unable to register MVM op_mode: %d\n", ret);
  118. iwl_mvm_rate_control_unregister();
  119. }
  120. return ret;
  121. }
  122. module_init(iwl_mvm_init);
  123. static void __exit iwl_mvm_exit(void)
  124. {
  125. iwl_opmode_deregister("iwlmvm");
  126. iwl_mvm_rate_control_unregister();
  127. }
  128. module_exit(iwl_mvm_exit);
  129. static void iwl_mvm_nic_config(struct iwl_op_mode *op_mode)
  130. {
  131. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  132. u8 radio_cfg_type, radio_cfg_step, radio_cfg_dash;
  133. u32 reg_val = 0;
  134. u32 phy_config = iwl_mvm_get_phy_config(mvm);
  135. radio_cfg_type = (phy_config & FW_PHY_CFG_RADIO_TYPE) >>
  136. FW_PHY_CFG_RADIO_TYPE_POS;
  137. radio_cfg_step = (phy_config & FW_PHY_CFG_RADIO_STEP) >>
  138. FW_PHY_CFG_RADIO_STEP_POS;
  139. radio_cfg_dash = (phy_config & FW_PHY_CFG_RADIO_DASH) >>
  140. FW_PHY_CFG_RADIO_DASH_POS;
  141. /* SKU control */
  142. reg_val |= CSR_HW_REV_STEP(mvm->trans->hw_rev) <<
  143. CSR_HW_IF_CONFIG_REG_POS_MAC_STEP;
  144. reg_val |= CSR_HW_REV_DASH(mvm->trans->hw_rev) <<
  145. CSR_HW_IF_CONFIG_REG_POS_MAC_DASH;
  146. /* radio configuration */
  147. reg_val |= radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE;
  148. reg_val |= radio_cfg_step << CSR_HW_IF_CONFIG_REG_POS_PHY_STEP;
  149. reg_val |= radio_cfg_dash << CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
  150. WARN_ON((radio_cfg_type << CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE) &
  151. ~CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE);
  152. /*
  153. * TODO: Bits 7-8 of CSR in 8000 HW family set the ADC sampling, and
  154. * shouldn't be set to any non-zero value. The same is supposed to be
  155. * true of the other HW, but unsetting them (such as the 7260) causes
  156. * automatic tests to fail on seemingly unrelated errors. Need to
  157. * further investigate this, but for now we'll separate cases.
  158. */
  159. if (mvm->trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  160. reg_val |= CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI;
  161. iwl_trans_set_bits_mask(mvm->trans, CSR_HW_IF_CONFIG_REG,
  162. CSR_HW_IF_CONFIG_REG_MSK_MAC_DASH |
  163. CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP |
  164. CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
  165. CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
  166. CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH |
  167. CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
  168. CSR_HW_IF_CONFIG_REG_BIT_MAC_SI,
  169. reg_val);
  170. IWL_DEBUG_INFO(mvm, "Radio type=0x%x-0x%x-0x%x\n", radio_cfg_type,
  171. radio_cfg_step, radio_cfg_dash);
  172. /*
  173. * W/A : NIC is stuck in a reset state after Early PCIe power off
  174. * (PCIe power is lost before PERST# is asserted), causing ME FW
  175. * to lose ownership and not being able to obtain it back.
  176. */
  177. if (!mvm->trans->cfg->apmg_not_supported)
  178. iwl_set_bits_mask_prph(mvm->trans, APMG_PS_CTRL_REG,
  179. APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
  180. ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
  181. }
  182. struct iwl_rx_handlers {
  183. u8 cmd_id;
  184. bool async;
  185. int (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  186. struct iwl_device_cmd *cmd);
  187. };
  188. #define RX_HANDLER(_cmd_id, _fn, _async) \
  189. { .cmd_id = _cmd_id , .fn = _fn , .async = _async }
  190. /*
  191. * Handlers for fw notifications
  192. * Convention: RX_HANDLER(CMD_NAME, iwl_mvm_rx_CMD_NAME
  193. * This list should be in order of frequency for performance purposes.
  194. *
  195. * The handler can be SYNC - this means that it will be called in the Rx path
  196. * which can't acquire mvm->mutex. If the handler needs to hold mvm->mutex (and
  197. * only in this case!), it should be set as ASYNC. In that case, it will be
  198. * called from a worker with mvm->mutex held.
  199. */
  200. static const struct iwl_rx_handlers iwl_mvm_rx_handlers[] = {
  201. RX_HANDLER(REPLY_RX_MPDU_CMD, iwl_mvm_rx_rx_mpdu, false),
  202. RX_HANDLER(REPLY_RX_PHY_CMD, iwl_mvm_rx_rx_phy_cmd, false),
  203. RX_HANDLER(TX_CMD, iwl_mvm_rx_tx_cmd, false),
  204. RX_HANDLER(BA_NOTIF, iwl_mvm_rx_ba_notif, false),
  205. RX_HANDLER(BT_PROFILE_NOTIFICATION, iwl_mvm_rx_bt_coex_notif, true),
  206. RX_HANDLER(BEACON_NOTIFICATION, iwl_mvm_rx_beacon_notif, true),
  207. RX_HANDLER(STATISTICS_NOTIFICATION, iwl_mvm_rx_statistics, true),
  208. RX_HANDLER(ANTENNA_COUPLING_NOTIFICATION,
  209. iwl_mvm_rx_ant_coupling_notif, true),
  210. RX_HANDLER(TIME_EVENT_NOTIFICATION, iwl_mvm_rx_time_event_notif, false),
  211. RX_HANDLER(MCC_CHUB_UPDATE_CMD, iwl_mvm_rx_chub_update_mcc, true),
  212. RX_HANDLER(EOSP_NOTIFICATION, iwl_mvm_rx_eosp_notif, false),
  213. RX_HANDLER(SCAN_ITERATION_COMPLETE,
  214. iwl_mvm_rx_lmac_scan_iter_complete_notif, false),
  215. RX_HANDLER(SCAN_OFFLOAD_COMPLETE,
  216. iwl_mvm_rx_lmac_scan_complete_notif, true),
  217. RX_HANDLER(MATCH_FOUND_NOTIFICATION, iwl_mvm_rx_scan_match_found,
  218. false),
  219. RX_HANDLER(SCAN_COMPLETE_UMAC, iwl_mvm_rx_umac_scan_complete_notif,
  220. true),
  221. RX_HANDLER(SCAN_ITERATION_COMPLETE_UMAC,
  222. iwl_mvm_rx_umac_scan_iter_complete_notif, false),
  223. RX_HANDLER(CARD_STATE_NOTIFICATION, iwl_mvm_rx_card_state_notif, false),
  224. RX_HANDLER(MISSED_BEACONS_NOTIFICATION, iwl_mvm_rx_missed_beacons_notif,
  225. false),
  226. RX_HANDLER(REPLY_ERROR, iwl_mvm_rx_fw_error, false),
  227. RX_HANDLER(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION,
  228. iwl_mvm_power_uapsd_misbehaving_ap_notif, false),
  229. RX_HANDLER(DTS_MEASUREMENT_NOTIFICATION, iwl_mvm_temp_notif, true),
  230. RX_HANDLER(TDLS_CHANNEL_SWITCH_NOTIFICATION, iwl_mvm_rx_tdls_notif,
  231. true),
  232. RX_HANDLER(MFUART_LOAD_NOTIFICATION, iwl_mvm_rx_mfuart_notif, false),
  233. };
  234. #undef RX_HANDLER
  235. #define CMD(x) [x] = #x
  236. static const char *const iwl_mvm_cmd_strings[REPLY_MAX] = {
  237. CMD(MVM_ALIVE),
  238. CMD(REPLY_ERROR),
  239. CMD(INIT_COMPLETE_NOTIF),
  240. CMD(PHY_CONTEXT_CMD),
  241. CMD(MGMT_MCAST_KEY),
  242. CMD(TX_CMD),
  243. CMD(TXPATH_FLUSH),
  244. CMD(SHARED_MEM_CFG),
  245. CMD(MAC_CONTEXT_CMD),
  246. CMD(TIME_EVENT_CMD),
  247. CMD(TIME_EVENT_NOTIFICATION),
  248. CMD(BINDING_CONTEXT_CMD),
  249. CMD(TIME_QUOTA_CMD),
  250. CMD(NON_QOS_TX_COUNTER_CMD),
  251. CMD(DC2DC_CONFIG_CMD),
  252. CMD(NVM_ACCESS_CMD),
  253. CMD(PHY_CONFIGURATION_CMD),
  254. CMD(CALIB_RES_NOTIF_PHY_DB),
  255. CMD(SET_CALIB_DEFAULT_CMD),
  256. CMD(ADD_STA_KEY),
  257. CMD(ADD_STA),
  258. CMD(REMOVE_STA),
  259. CMD(LQ_CMD),
  260. CMD(SCAN_OFFLOAD_CONFIG_CMD),
  261. CMD(MATCH_FOUND_NOTIFICATION),
  262. CMD(SCAN_OFFLOAD_REQUEST_CMD),
  263. CMD(SCAN_OFFLOAD_ABORT_CMD),
  264. CMD(HOT_SPOT_CMD),
  265. CMD(SCAN_OFFLOAD_COMPLETE),
  266. CMD(SCAN_OFFLOAD_UPDATE_PROFILES_CMD),
  267. CMD(SCAN_ITERATION_COMPLETE),
  268. CMD(POWER_TABLE_CMD),
  269. CMD(WEP_KEY),
  270. CMD(REPLY_RX_PHY_CMD),
  271. CMD(REPLY_RX_MPDU_CMD),
  272. CMD(BEACON_NOTIFICATION),
  273. CMD(BEACON_TEMPLATE_CMD),
  274. CMD(STATISTICS_CMD),
  275. CMD(STATISTICS_NOTIFICATION),
  276. CMD(EOSP_NOTIFICATION),
  277. CMD(REDUCE_TX_POWER_CMD),
  278. CMD(TX_ANT_CONFIGURATION_CMD),
  279. CMD(D3_CONFIG_CMD),
  280. CMD(D0I3_END_CMD),
  281. CMD(PROT_OFFLOAD_CONFIG_CMD),
  282. CMD(OFFLOADS_QUERY_CMD),
  283. CMD(REMOTE_WAKE_CONFIG_CMD),
  284. CMD(WOWLAN_PATTERNS),
  285. CMD(WOWLAN_CONFIGURATION),
  286. CMD(WOWLAN_TSC_RSC_PARAM),
  287. CMD(WOWLAN_TKIP_PARAM),
  288. CMD(WOWLAN_KEK_KCK_MATERIAL),
  289. CMD(WOWLAN_GET_STATUSES),
  290. CMD(WOWLAN_TX_POWER_PER_DB),
  291. CMD(SCAN_OFFLOAD_PROFILES_QUERY_CMD),
  292. CMD(SCAN_OFFLOAD_HOTSPOTS_CONFIG_CMD),
  293. CMD(SCAN_OFFLOAD_HOTSPOTS_QUERY_CMD),
  294. CMD(CARD_STATE_NOTIFICATION),
  295. CMD(MISSED_BEACONS_NOTIFICATION),
  296. CMD(BT_COEX_PRIO_TABLE),
  297. CMD(BT_COEX_PROT_ENV),
  298. CMD(BT_PROFILE_NOTIFICATION),
  299. CMD(BT_CONFIG),
  300. CMD(MCAST_FILTER_CMD),
  301. CMD(BCAST_FILTER_CMD),
  302. CMD(REPLY_SF_CFG_CMD),
  303. CMD(REPLY_BEACON_FILTERING_CMD),
  304. CMD(CMD_DTS_MEASUREMENT_TRIGGER),
  305. CMD(DTS_MEASUREMENT_NOTIFICATION),
  306. CMD(REPLY_THERMAL_MNG_BACKOFF),
  307. CMD(MAC_PM_POWER_TABLE),
  308. CMD(LTR_CONFIG),
  309. CMD(BT_COEX_CI),
  310. CMD(BT_COEX_UPDATE_SW_BOOST),
  311. CMD(BT_COEX_UPDATE_CORUN_LUT),
  312. CMD(BT_COEX_UPDATE_REDUCED_TXP),
  313. CMD(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION),
  314. CMD(ANTENNA_COUPLING_NOTIFICATION),
  315. CMD(SCD_QUEUE_CFG),
  316. CMD(SCAN_CFG_CMD),
  317. CMD(SCAN_REQ_UMAC),
  318. CMD(SCAN_ABORT_UMAC),
  319. CMD(SCAN_COMPLETE_UMAC),
  320. CMD(TDLS_CHANNEL_SWITCH_CMD),
  321. CMD(TDLS_CHANNEL_SWITCH_NOTIFICATION),
  322. CMD(TDLS_CONFIG_CMD),
  323. CMD(MCC_UPDATE_CMD),
  324. CMD(SCAN_ITERATION_COMPLETE_UMAC),
  325. };
  326. #undef CMD
  327. /* this forward declaration can avoid to export the function */
  328. static void iwl_mvm_async_handlers_wk(struct work_struct *wk);
  329. static void iwl_mvm_d0i3_exit_work(struct work_struct *wk);
  330. static u32 calc_min_backoff(struct iwl_trans *trans, const struct iwl_cfg *cfg)
  331. {
  332. const struct iwl_pwr_tx_backoff *pwr_tx_backoff = cfg->pwr_tx_backoffs;
  333. if (!pwr_tx_backoff)
  334. return 0;
  335. while (pwr_tx_backoff->pwr) {
  336. if (trans->dflt_pwr_limit >= pwr_tx_backoff->pwr)
  337. return pwr_tx_backoff->backoff;
  338. pwr_tx_backoff++;
  339. }
  340. return 0;
  341. }
  342. static void iwl_mvm_fw_error_dump_wk(struct work_struct *work);
  343. static struct iwl_op_mode *
  344. iwl_op_mode_mvm_start(struct iwl_trans *trans, const struct iwl_cfg *cfg,
  345. const struct iwl_fw *fw, struct dentry *dbgfs_dir)
  346. {
  347. struct ieee80211_hw *hw;
  348. struct iwl_op_mode *op_mode;
  349. struct iwl_mvm *mvm;
  350. struct iwl_trans_config trans_cfg = {};
  351. static const u8 no_reclaim_cmds[] = {
  352. TX_CMD,
  353. };
  354. int err, scan_size;
  355. u32 min_backoff;
  356. /*
  357. * We use IWL_MVM_STATION_COUNT to check the validity of the station
  358. * index all over the driver - check that its value corresponds to the
  359. * array size.
  360. */
  361. BUILD_BUG_ON(ARRAY_SIZE(mvm->fw_id_to_mac_id) != IWL_MVM_STATION_COUNT);
  362. /********************************
  363. * 1. Allocating and configuring HW data
  364. ********************************/
  365. hw = ieee80211_alloc_hw(sizeof(struct iwl_op_mode) +
  366. sizeof(struct iwl_mvm),
  367. &iwl_mvm_hw_ops);
  368. if (!hw)
  369. return NULL;
  370. if (cfg->max_rx_agg_size)
  371. hw->max_rx_aggregation_subframes = cfg->max_rx_agg_size;
  372. if (cfg->max_tx_agg_size)
  373. hw->max_tx_aggregation_subframes = cfg->max_tx_agg_size;
  374. op_mode = hw->priv;
  375. op_mode->ops = &iwl_mvm_ops;
  376. mvm = IWL_OP_MODE_GET_MVM(op_mode);
  377. mvm->dev = trans->dev;
  378. mvm->trans = trans;
  379. mvm->cfg = cfg;
  380. mvm->fw = fw;
  381. mvm->hw = hw;
  382. mvm->restart_fw = iwlwifi_mod_params.restart_fw ? -1 : 0;
  383. mvm->aux_queue = 15;
  384. mvm->first_agg_queue = 16;
  385. mvm->last_agg_queue = mvm->cfg->base_params->num_of_queues - 1;
  386. if (mvm->cfg->base_params->num_of_queues == 16) {
  387. mvm->aux_queue = 11;
  388. mvm->first_agg_queue = 12;
  389. }
  390. mvm->sf_state = SF_UNINIT;
  391. mvm->low_latency_agg_frame_limit = 6;
  392. mvm->cur_ucode = IWL_UCODE_INIT;
  393. mutex_init(&mvm->mutex);
  394. mutex_init(&mvm->d0i3_suspend_mutex);
  395. spin_lock_init(&mvm->async_handlers_lock);
  396. INIT_LIST_HEAD(&mvm->time_event_list);
  397. INIT_LIST_HEAD(&mvm->aux_roc_te_list);
  398. INIT_LIST_HEAD(&mvm->async_handlers_list);
  399. spin_lock_init(&mvm->time_event_lock);
  400. INIT_WORK(&mvm->async_handlers_wk, iwl_mvm_async_handlers_wk);
  401. INIT_WORK(&mvm->roc_done_wk, iwl_mvm_roc_done_wk);
  402. INIT_WORK(&mvm->sta_drained_wk, iwl_mvm_sta_drained_wk);
  403. INIT_WORK(&mvm->d0i3_exit_work, iwl_mvm_d0i3_exit_work);
  404. INIT_DELAYED_WORK(&mvm->fw_dump_wk, iwl_mvm_fw_error_dump_wk);
  405. INIT_DELAYED_WORK(&mvm->tdls_cs.dwork, iwl_mvm_tdls_ch_switch_work);
  406. spin_lock_init(&mvm->d0i3_tx_lock);
  407. spin_lock_init(&mvm->refs_lock);
  408. skb_queue_head_init(&mvm->d0i3_tx);
  409. init_waitqueue_head(&mvm->d0i3_exit_waitq);
  410. SET_IEEE80211_DEV(mvm->hw, mvm->trans->dev);
  411. /*
  412. * Populate the state variables that the transport layer needs
  413. * to know about.
  414. */
  415. trans_cfg.op_mode = op_mode;
  416. trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
  417. trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
  418. trans_cfg.rx_buf_size_8k = iwlwifi_mod_params.amsdu_size_8K;
  419. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_DW_BC_TABLE)
  420. trans_cfg.bc_table_dword = true;
  421. trans_cfg.command_names = iwl_mvm_cmd_strings;
  422. trans_cfg.cmd_queue = IWL_MVM_CMD_QUEUE;
  423. trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
  424. trans_cfg.scd_set_active = true;
  425. trans_cfg.sdio_adma_addr = fw->sdio_adma_addr;
  426. /* Set a short watchdog for the command queue */
  427. trans_cfg.cmd_q_wdg_timeout =
  428. iwl_mvm_get_wd_timeout(mvm, NULL, false, true);
  429. snprintf(mvm->hw->wiphy->fw_version,
  430. sizeof(mvm->hw->wiphy->fw_version),
  431. "%s", fw->fw_version);
  432. /* Configure transport layer */
  433. iwl_trans_configure(mvm->trans, &trans_cfg);
  434. trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
  435. trans->rx_mpdu_cmd_hdr_size = sizeof(struct iwl_rx_mpdu_res_start);
  436. trans->dbg_dest_tlv = mvm->fw->dbg_dest_tlv;
  437. trans->dbg_dest_reg_num = mvm->fw->dbg_dest_reg_num;
  438. memcpy(trans->dbg_conf_tlv, mvm->fw->dbg_conf_tlv,
  439. sizeof(trans->dbg_conf_tlv));
  440. trans->dbg_trigger_tlv = mvm->fw->dbg_trigger_tlv;
  441. /* set up notification wait support */
  442. iwl_notification_wait_init(&mvm->notif_wait);
  443. /* Init phy db */
  444. mvm->phy_db = iwl_phy_db_init(trans);
  445. if (!mvm->phy_db) {
  446. IWL_ERR(mvm, "Cannot init phy_db\n");
  447. goto out_free;
  448. }
  449. IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
  450. mvm->cfg->name, mvm->trans->hw_rev);
  451. min_backoff = calc_min_backoff(trans, cfg);
  452. iwl_mvm_tt_initialize(mvm, min_backoff);
  453. if (iwlwifi_mod_params.nvm_file)
  454. mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
  455. else
  456. IWL_DEBUG_EEPROM(mvm->trans->dev,
  457. "working without external nvm file\n");
  458. if (WARN(cfg->no_power_up_nic_in_init && !mvm->nvm_file_name,
  459. "not allowing power-up and not having nvm_file\n"))
  460. goto out_free;
  461. /*
  462. * Even if nvm exists in the nvm_file driver should read again the nvm
  463. * from the nic because there might be entries that exist in the OTP
  464. * and not in the file.
  465. * for nics with no_power_up_nic_in_init: rely completley on nvm_file
  466. */
  467. if (cfg->no_power_up_nic_in_init && mvm->nvm_file_name) {
  468. err = iwl_nvm_init(mvm, false);
  469. if (err)
  470. goto out_free;
  471. } else {
  472. err = iwl_trans_start_hw(mvm->trans);
  473. if (err)
  474. goto out_free;
  475. mutex_lock(&mvm->mutex);
  476. err = iwl_run_init_mvm_ucode(mvm, true);
  477. if (!err || !iwlmvm_mod_params.init_dbg)
  478. iwl_trans_stop_device(trans);
  479. mutex_unlock(&mvm->mutex);
  480. /* returns 0 if successful, 1 if success but in rfkill */
  481. if (err < 0 && !iwlmvm_mod_params.init_dbg) {
  482. IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", err);
  483. goto out_free;
  484. }
  485. }
  486. scan_size = iwl_mvm_scan_size(mvm);
  487. mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
  488. if (!mvm->scan_cmd)
  489. goto out_free;
  490. /* Set EBS as successful as long as not stated otherwise by the FW. */
  491. mvm->last_ebs_successful = true;
  492. err = iwl_mvm_mac_setup_register(mvm);
  493. if (err)
  494. goto out_free;
  495. err = iwl_mvm_dbgfs_register(mvm, dbgfs_dir);
  496. if (err)
  497. goto out_unregister;
  498. memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
  499. /* rpm starts with a taken ref. only set the appropriate bit here. */
  500. mvm->refs[IWL_MVM_REF_UCODE_DOWN] = 1;
  501. return op_mode;
  502. out_unregister:
  503. ieee80211_unregister_hw(mvm->hw);
  504. iwl_mvm_leds_exit(mvm);
  505. out_free:
  506. iwl_phy_db_free(mvm->phy_db);
  507. kfree(mvm->scan_cmd);
  508. if (!cfg->no_power_up_nic_in_init || !mvm->nvm_file_name)
  509. iwl_trans_op_mode_leave(trans);
  510. ieee80211_free_hw(mvm->hw);
  511. return NULL;
  512. }
  513. static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
  514. {
  515. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  516. int i;
  517. iwl_mvm_leds_exit(mvm);
  518. iwl_mvm_tt_exit(mvm);
  519. ieee80211_unregister_hw(mvm->hw);
  520. kfree(mvm->scan_cmd);
  521. kfree(mvm->mcast_filter_cmd);
  522. mvm->mcast_filter_cmd = NULL;
  523. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_IWLWIFI_DEBUGFS)
  524. kfree(mvm->d3_resume_sram);
  525. if (mvm->nd_config) {
  526. kfree(mvm->nd_config->match_sets);
  527. kfree(mvm->nd_config);
  528. mvm->nd_config = NULL;
  529. }
  530. #endif
  531. iwl_trans_op_mode_leave(mvm->trans);
  532. iwl_phy_db_free(mvm->phy_db);
  533. mvm->phy_db = NULL;
  534. iwl_free_nvm_data(mvm->nvm_data);
  535. for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
  536. kfree(mvm->nvm_sections[i].data);
  537. ieee80211_free_hw(mvm->hw);
  538. }
  539. struct iwl_async_handler_entry {
  540. struct list_head list;
  541. struct iwl_rx_cmd_buffer rxb;
  542. int (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  543. struct iwl_device_cmd *cmd);
  544. };
  545. void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
  546. {
  547. struct iwl_async_handler_entry *entry, *tmp;
  548. spin_lock_bh(&mvm->async_handlers_lock);
  549. list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
  550. iwl_free_rxb(&entry->rxb);
  551. list_del(&entry->list);
  552. kfree(entry);
  553. }
  554. spin_unlock_bh(&mvm->async_handlers_lock);
  555. }
  556. static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
  557. {
  558. struct iwl_mvm *mvm =
  559. container_of(wk, struct iwl_mvm, async_handlers_wk);
  560. struct iwl_async_handler_entry *entry, *tmp;
  561. struct list_head local_list;
  562. INIT_LIST_HEAD(&local_list);
  563. /* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
  564. mutex_lock(&mvm->mutex);
  565. /*
  566. * Sync with Rx path with a lock. Remove all the entries from this list,
  567. * add them to a local one (lock free), and then handle them.
  568. */
  569. spin_lock_bh(&mvm->async_handlers_lock);
  570. list_splice_init(&mvm->async_handlers_list, &local_list);
  571. spin_unlock_bh(&mvm->async_handlers_lock);
  572. list_for_each_entry_safe(entry, tmp, &local_list, list) {
  573. if (entry->fn(mvm, &entry->rxb, NULL))
  574. IWL_WARN(mvm,
  575. "returned value from ASYNC handlers are ignored\n");
  576. iwl_free_rxb(&entry->rxb);
  577. list_del(&entry->list);
  578. kfree(entry);
  579. }
  580. mutex_unlock(&mvm->mutex);
  581. }
  582. static inline void iwl_mvm_rx_check_trigger(struct iwl_mvm *mvm,
  583. struct iwl_rx_packet *pkt)
  584. {
  585. struct iwl_fw_dbg_trigger_tlv *trig;
  586. struct iwl_fw_dbg_trigger_cmd *cmds_trig;
  587. int i;
  588. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_FW_NOTIF))
  589. return;
  590. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_FW_NOTIF);
  591. cmds_trig = (void *)trig->data;
  592. if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
  593. return;
  594. for (i = 0; i < ARRAY_SIZE(cmds_trig->cmds); i++) {
  595. /* don't collect on CMD 0 */
  596. if (!cmds_trig->cmds[i].cmd_id)
  597. break;
  598. if (cmds_trig->cmds[i].cmd_id != pkt->hdr.cmd)
  599. continue;
  600. iwl_mvm_fw_dbg_collect_trig(mvm, trig,
  601. "CMD 0x%02x received",
  602. pkt->hdr.cmd);
  603. break;
  604. }
  605. }
  606. static int iwl_mvm_rx_dispatch(struct iwl_op_mode *op_mode,
  607. struct iwl_rx_cmd_buffer *rxb,
  608. struct iwl_device_cmd *cmd)
  609. {
  610. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  611. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  612. u8 i;
  613. iwl_mvm_rx_check_trigger(mvm, pkt);
  614. /*
  615. * Do the notification wait before RX handlers so
  616. * even if the RX handler consumes the RXB we have
  617. * access to it in the notification wait entry.
  618. */
  619. iwl_notification_wait_notify(&mvm->notif_wait, pkt);
  620. for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
  621. const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
  622. struct iwl_async_handler_entry *entry;
  623. if (rx_h->cmd_id != pkt->hdr.cmd)
  624. continue;
  625. if (!rx_h->async)
  626. return rx_h->fn(mvm, rxb, cmd);
  627. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  628. /* we can't do much... */
  629. if (!entry)
  630. return 0;
  631. entry->rxb._page = rxb_steal_page(rxb);
  632. entry->rxb._offset = rxb->_offset;
  633. entry->rxb._rx_page_order = rxb->_rx_page_order;
  634. entry->fn = rx_h->fn;
  635. spin_lock(&mvm->async_handlers_lock);
  636. list_add_tail(&entry->list, &mvm->async_handlers_list);
  637. spin_unlock(&mvm->async_handlers_lock);
  638. schedule_work(&mvm->async_handlers_wk);
  639. break;
  640. }
  641. return 0;
  642. }
  643. static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int queue)
  644. {
  645. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  646. int mq = mvm->queue_to_mac80211[queue];
  647. if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
  648. return;
  649. if (atomic_inc_return(&mvm->mac80211_queue_stop_count[mq]) > 1) {
  650. IWL_DEBUG_TX_QUEUES(mvm,
  651. "queue %d (mac80211 %d) already stopped\n",
  652. queue, mq);
  653. return;
  654. }
  655. ieee80211_stop_queue(mvm->hw, mq);
  656. }
  657. static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int queue)
  658. {
  659. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  660. int mq = mvm->queue_to_mac80211[queue];
  661. if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
  662. return;
  663. if (atomic_dec_return(&mvm->mac80211_queue_stop_count[mq]) > 0) {
  664. IWL_DEBUG_TX_QUEUES(mvm,
  665. "queue %d (mac80211 %d) still stopped\n",
  666. queue, mq);
  667. return;
  668. }
  669. ieee80211_wake_queue(mvm->hw, mq);
  670. }
  671. void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
  672. {
  673. if (state)
  674. set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
  675. else
  676. clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
  677. wiphy_rfkill_set_hw_state(mvm->hw->wiphy, iwl_mvm_is_radio_killed(mvm));
  678. }
  679. static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
  680. {
  681. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  682. bool calibrating = ACCESS_ONCE(mvm->calibrating);
  683. if (state)
  684. set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
  685. else
  686. clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
  687. wiphy_rfkill_set_hw_state(mvm->hw->wiphy, iwl_mvm_is_radio_killed(mvm));
  688. /* iwl_run_init_mvm_ucode is waiting for results, abort it */
  689. if (calibrating)
  690. iwl_abort_notification_waits(&mvm->notif_wait);
  691. /*
  692. * Stop the device if we run OPERATIONAL firmware or if we are in the
  693. * middle of the calibrations.
  694. */
  695. return state && (mvm->cur_ucode != IWL_UCODE_INIT || calibrating);
  696. }
  697. static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  698. {
  699. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  700. struct ieee80211_tx_info *info;
  701. info = IEEE80211_SKB_CB(skb);
  702. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  703. ieee80211_free_txskb(mvm->hw, skb);
  704. }
  705. struct iwl_mvm_reprobe {
  706. struct device *dev;
  707. struct work_struct work;
  708. };
  709. static void iwl_mvm_reprobe_wk(struct work_struct *wk)
  710. {
  711. struct iwl_mvm_reprobe *reprobe;
  712. reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
  713. if (device_reprobe(reprobe->dev))
  714. dev_err(reprobe->dev, "reprobe failed!\n");
  715. kfree(reprobe);
  716. module_put(THIS_MODULE);
  717. }
  718. static void iwl_mvm_fw_error_dump_wk(struct work_struct *work)
  719. {
  720. struct iwl_mvm *mvm =
  721. container_of(work, struct iwl_mvm, fw_dump_wk.work);
  722. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_FW_DBG_COLLECT))
  723. return;
  724. mutex_lock(&mvm->mutex);
  725. /* stop recording */
  726. if (mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
  727. iwl_set_bits_prph(mvm->trans, MON_BUFF_SAMPLE_CTL, 0x100);
  728. } else {
  729. iwl_write_prph(mvm->trans, DBGC_IN_SAMPLE, 0);
  730. /* wait before we collect the data till the DBGC stop */
  731. udelay(100);
  732. }
  733. iwl_mvm_fw_error_dump(mvm);
  734. /* start recording again if the firmware is not crashed */
  735. WARN_ON_ONCE((!test_bit(STATUS_FW_ERROR, &mvm->trans->status)) &&
  736. mvm->fw->dbg_dest_tlv &&
  737. iwl_mvm_start_fw_dbg_conf(mvm, mvm->fw_dbg_conf));
  738. mutex_unlock(&mvm->mutex);
  739. iwl_mvm_unref(mvm, IWL_MVM_REF_FW_DBG_COLLECT);
  740. }
  741. void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
  742. {
  743. iwl_abort_notification_waits(&mvm->notif_wait);
  744. /*
  745. * This is a bit racy, but worst case we tell mac80211 about
  746. * a stopped/aborted scan when that was already done which
  747. * is not a problem. It is necessary to abort any os scan
  748. * here because mac80211 requires having the scan cleared
  749. * before restarting.
  750. * We'll reset the scan_status to NONE in restart cleanup in
  751. * the next start() call from mac80211. If restart isn't called
  752. * (no fw restart) scan status will stay busy.
  753. */
  754. iwl_mvm_report_scan_aborted(mvm);
  755. /*
  756. * If we're restarting already, don't cycle restarts.
  757. * If INIT fw asserted, it will likely fail again.
  758. * If WoWLAN fw asserted, don't restart either, mac80211
  759. * can't recover this since we're already half suspended.
  760. */
  761. if (!mvm->restart_fw && fw_error) {
  762. iwl_mvm_fw_dbg_collect_desc(mvm, &iwl_mvm_dump_desc_assert, 0);
  763. } else if (test_and_set_bit(IWL_MVM_STATUS_IN_HW_RESTART,
  764. &mvm->status)) {
  765. struct iwl_mvm_reprobe *reprobe;
  766. IWL_ERR(mvm,
  767. "Firmware error during reconfiguration - reprobe!\n");
  768. /*
  769. * get a module reference to avoid doing this while unloading
  770. * anyway and to avoid scheduling a work with code that's
  771. * being removed.
  772. */
  773. if (!try_module_get(THIS_MODULE)) {
  774. IWL_ERR(mvm, "Module is being unloaded - abort\n");
  775. return;
  776. }
  777. reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
  778. if (!reprobe) {
  779. module_put(THIS_MODULE);
  780. return;
  781. }
  782. reprobe->dev = mvm->trans->dev;
  783. INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
  784. schedule_work(&reprobe->work);
  785. } else if (mvm->cur_ucode == IWL_UCODE_REGULAR) {
  786. /* don't let the transport/FW power down */
  787. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  788. if (fw_error && mvm->restart_fw > 0)
  789. mvm->restart_fw--;
  790. ieee80211_restart_hw(mvm->hw);
  791. }
  792. }
  793. static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode)
  794. {
  795. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  796. iwl_mvm_dump_nic_error_log(mvm);
  797. iwl_mvm_nic_restart(mvm, true);
  798. }
  799. static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
  800. {
  801. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  802. WARN_ON(1);
  803. iwl_mvm_nic_restart(mvm, true);
  804. }
  805. struct iwl_d0i3_iter_data {
  806. struct iwl_mvm *mvm;
  807. u8 ap_sta_id;
  808. u8 vif_count;
  809. u8 offloading_tid;
  810. bool disable_offloading;
  811. };
  812. static bool iwl_mvm_disallow_offloading(struct iwl_mvm *mvm,
  813. struct ieee80211_vif *vif,
  814. struct iwl_d0i3_iter_data *iter_data)
  815. {
  816. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  817. struct ieee80211_sta *ap_sta;
  818. struct iwl_mvm_sta *mvmsta;
  819. u32 available_tids = 0;
  820. u8 tid;
  821. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION ||
  822. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT))
  823. return false;
  824. ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id]);
  825. if (IS_ERR_OR_NULL(ap_sta))
  826. return false;
  827. mvmsta = iwl_mvm_sta_from_mac80211(ap_sta);
  828. spin_lock_bh(&mvmsta->lock);
  829. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  830. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  831. /*
  832. * in case of pending tx packets, don't use this tid
  833. * for offloading in order to prevent reuse of the same
  834. * qos seq counters.
  835. */
  836. if (iwl_mvm_tid_queued(tid_data))
  837. continue;
  838. if (tid_data->state != IWL_AGG_OFF)
  839. continue;
  840. available_tids |= BIT(tid);
  841. }
  842. spin_unlock_bh(&mvmsta->lock);
  843. /*
  844. * disallow protocol offloading if we have no available tid
  845. * (with no pending frames and no active aggregation,
  846. * as we don't handle "holes" properly - the scheduler needs the
  847. * frame's seq number and TFD index to match)
  848. */
  849. if (!available_tids)
  850. return true;
  851. /* for simplicity, just use the first available tid */
  852. iter_data->offloading_tid = ffs(available_tids) - 1;
  853. return false;
  854. }
  855. static void iwl_mvm_enter_d0i3_iterator(void *_data, u8 *mac,
  856. struct ieee80211_vif *vif)
  857. {
  858. struct iwl_d0i3_iter_data *data = _data;
  859. struct iwl_mvm *mvm = data->mvm;
  860. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  861. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE;
  862. IWL_DEBUG_RPM(mvm, "entering D0i3 - vif %pM\n", vif->addr);
  863. if (vif->type != NL80211_IFTYPE_STATION ||
  864. !vif->bss_conf.assoc)
  865. return;
  866. /*
  867. * in case of pending tx packets or active aggregations,
  868. * avoid offloading features in order to prevent reuse of
  869. * the same qos seq counters.
  870. */
  871. if (iwl_mvm_disallow_offloading(mvm, vif, data))
  872. data->disable_offloading = true;
  873. iwl_mvm_update_d0i3_power_mode(mvm, vif, true, flags);
  874. iwl_mvm_send_proto_offload(mvm, vif, data->disable_offloading, flags);
  875. /*
  876. * on init/association, mvm already configures POWER_TABLE_CMD
  877. * and REPLY_MCAST_FILTER_CMD, so currently don't
  878. * reconfigure them (we might want to use different
  879. * params later on, though).
  880. */
  881. data->ap_sta_id = mvmvif->ap_sta_id;
  882. data->vif_count++;
  883. }
  884. static void iwl_mvm_set_wowlan_data(struct iwl_mvm *mvm,
  885. struct iwl_wowlan_config_cmd *cmd,
  886. struct iwl_d0i3_iter_data *iter_data)
  887. {
  888. struct ieee80211_sta *ap_sta;
  889. struct iwl_mvm_sta *mvm_ap_sta;
  890. if (iter_data->ap_sta_id == IWL_MVM_STATION_COUNT)
  891. return;
  892. rcu_read_lock();
  893. ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[iter_data->ap_sta_id]);
  894. if (IS_ERR_OR_NULL(ap_sta))
  895. goto out;
  896. mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  897. cmd->is_11n_connection = ap_sta->ht_cap.ht_supported;
  898. cmd->offloading_tid = iter_data->offloading_tid;
  899. /*
  900. * The d0i3 uCode takes care of the nonqos counters,
  901. * so configure only the qos seq ones.
  902. */
  903. iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, cmd);
  904. out:
  905. rcu_read_unlock();
  906. }
  907. int iwl_mvm_enter_d0i3(struct iwl_op_mode *op_mode)
  908. {
  909. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  910. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE;
  911. int ret;
  912. struct iwl_d0i3_iter_data d0i3_iter_data = {
  913. .mvm = mvm,
  914. };
  915. struct iwl_wowlan_config_cmd wowlan_config_cmd = {
  916. .wakeup_filter = cpu_to_le32(IWL_WOWLAN_WAKEUP_RX_FRAME |
  917. IWL_WOWLAN_WAKEUP_BEACON_MISS |
  918. IWL_WOWLAN_WAKEUP_LINK_CHANGE |
  919. IWL_WOWLAN_WAKEUP_BCN_FILTERING),
  920. };
  921. struct iwl_d3_manager_config d3_cfg_cmd = {
  922. .min_sleep_time = cpu_to_le32(1000),
  923. .wakeup_flags = cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR),
  924. };
  925. IWL_DEBUG_RPM(mvm, "MVM entering D0i3\n");
  926. /* make sure we have no running tx while configuring the qos */
  927. set_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
  928. synchronize_net();
  929. /*
  930. * iwl_mvm_ref_sync takes a reference before checking the flag.
  931. * so by checking there is no held reference we prevent a state
  932. * in which iwl_mvm_ref_sync continues successfully while we
  933. * configure the firmware to enter d0i3
  934. */
  935. if (iwl_mvm_ref_taken(mvm)) {
  936. IWL_DEBUG_RPM(mvm->trans, "abort d0i3 due to taken ref\n");
  937. clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
  938. wake_up(&mvm->d0i3_exit_waitq);
  939. return 1;
  940. }
  941. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  942. IEEE80211_IFACE_ITER_NORMAL,
  943. iwl_mvm_enter_d0i3_iterator,
  944. &d0i3_iter_data);
  945. if (d0i3_iter_data.vif_count == 1) {
  946. mvm->d0i3_ap_sta_id = d0i3_iter_data.ap_sta_id;
  947. mvm->d0i3_offloading = !d0i3_iter_data.disable_offloading;
  948. } else {
  949. WARN_ON_ONCE(d0i3_iter_data.vif_count > 1);
  950. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  951. mvm->d0i3_offloading = false;
  952. }
  953. iwl_mvm_set_wowlan_data(mvm, &wowlan_config_cmd, &d0i3_iter_data);
  954. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, flags,
  955. sizeof(wowlan_config_cmd),
  956. &wowlan_config_cmd);
  957. if (ret)
  958. return ret;
  959. return iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD,
  960. flags | CMD_MAKE_TRANS_IDLE,
  961. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  962. }
  963. static void iwl_mvm_exit_d0i3_iterator(void *_data, u8 *mac,
  964. struct ieee80211_vif *vif)
  965. {
  966. struct iwl_mvm *mvm = _data;
  967. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO;
  968. IWL_DEBUG_RPM(mvm, "exiting D0i3 - vif %pM\n", vif->addr);
  969. if (vif->type != NL80211_IFTYPE_STATION ||
  970. !vif->bss_conf.assoc)
  971. return;
  972. iwl_mvm_update_d0i3_power_mode(mvm, vif, false, flags);
  973. }
  974. static void iwl_mvm_d0i3_disconnect_iter(void *data, u8 *mac,
  975. struct ieee80211_vif *vif)
  976. {
  977. struct iwl_mvm *mvm = data;
  978. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  979. if (vif->type == NL80211_IFTYPE_STATION && vif->bss_conf.assoc &&
  980. mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
  981. iwl_mvm_connection_loss(mvm, vif, "D0i3");
  982. }
  983. void iwl_mvm_d0i3_enable_tx(struct iwl_mvm *mvm, __le16 *qos_seq)
  984. {
  985. struct ieee80211_sta *sta = NULL;
  986. struct iwl_mvm_sta *mvm_ap_sta;
  987. int i;
  988. bool wake_queues = false;
  989. lockdep_assert_held(&mvm->mutex);
  990. spin_lock_bh(&mvm->d0i3_tx_lock);
  991. if (mvm->d0i3_ap_sta_id == IWL_MVM_STATION_COUNT)
  992. goto out;
  993. IWL_DEBUG_RPM(mvm, "re-enqueue packets\n");
  994. /* get the sta in order to update seq numbers and re-enqueue skbs */
  995. sta = rcu_dereference_protected(
  996. mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id],
  997. lockdep_is_held(&mvm->mutex));
  998. if (IS_ERR_OR_NULL(sta)) {
  999. sta = NULL;
  1000. goto out;
  1001. }
  1002. if (mvm->d0i3_offloading && qos_seq) {
  1003. /* update qos seq numbers if offloading was enabled */
  1004. mvm_ap_sta = iwl_mvm_sta_from_mac80211(sta);
  1005. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1006. u16 seq = le16_to_cpu(qos_seq[i]);
  1007. /* firmware stores last-used one, we store next one */
  1008. seq += 0x10;
  1009. mvm_ap_sta->tid_data[i].seq_number = seq;
  1010. }
  1011. }
  1012. out:
  1013. /* re-enqueue (or drop) all packets */
  1014. while (!skb_queue_empty(&mvm->d0i3_tx)) {
  1015. struct sk_buff *skb = __skb_dequeue(&mvm->d0i3_tx);
  1016. if (!sta || iwl_mvm_tx_skb(mvm, skb, sta))
  1017. ieee80211_free_txskb(mvm->hw, skb);
  1018. /* if the skb_queue is not empty, we need to wake queues */
  1019. wake_queues = true;
  1020. }
  1021. clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
  1022. wake_up(&mvm->d0i3_exit_waitq);
  1023. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  1024. if (wake_queues)
  1025. ieee80211_wake_queues(mvm->hw);
  1026. spin_unlock_bh(&mvm->d0i3_tx_lock);
  1027. }
  1028. static void iwl_mvm_d0i3_exit_work(struct work_struct *wk)
  1029. {
  1030. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, d0i3_exit_work);
  1031. struct iwl_host_cmd get_status_cmd = {
  1032. .id = WOWLAN_GET_STATUSES,
  1033. .flags = CMD_HIGH_PRIO | CMD_WANT_SKB,
  1034. };
  1035. struct iwl_wowlan_status *status;
  1036. int ret;
  1037. u32 disconnection_reasons, wakeup_reasons;
  1038. __le16 *qos_seq = NULL;
  1039. mutex_lock(&mvm->mutex);
  1040. ret = iwl_mvm_send_cmd(mvm, &get_status_cmd);
  1041. if (ret)
  1042. goto out;
  1043. if (!get_status_cmd.resp_pkt)
  1044. goto out;
  1045. status = (void *)get_status_cmd.resp_pkt->data;
  1046. wakeup_reasons = le32_to_cpu(status->wakeup_reasons);
  1047. qos_seq = status->qos_seq_ctr;
  1048. IWL_DEBUG_RPM(mvm, "wakeup reasons: 0x%x\n", wakeup_reasons);
  1049. disconnection_reasons =
  1050. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1051. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1052. if (wakeup_reasons & disconnection_reasons)
  1053. ieee80211_iterate_active_interfaces(
  1054. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1055. iwl_mvm_d0i3_disconnect_iter, mvm);
  1056. out:
  1057. iwl_mvm_d0i3_enable_tx(mvm, qos_seq);
  1058. /* qos_seq might point inside resp_pkt, so free it only now */
  1059. if (get_status_cmd.resp_pkt)
  1060. iwl_free_resp(&get_status_cmd);
  1061. /* the FW might have updated the regdomain */
  1062. iwl_mvm_update_changed_regdom(mvm);
  1063. iwl_mvm_unref(mvm, IWL_MVM_REF_EXIT_WORK);
  1064. mutex_unlock(&mvm->mutex);
  1065. }
  1066. int _iwl_mvm_exit_d0i3(struct iwl_mvm *mvm)
  1067. {
  1068. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
  1069. CMD_WAKE_UP_TRANS;
  1070. int ret;
  1071. IWL_DEBUG_RPM(mvm, "MVM exiting D0i3\n");
  1072. mutex_lock(&mvm->d0i3_suspend_mutex);
  1073. if (test_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags)) {
  1074. IWL_DEBUG_RPM(mvm, "Deferring d0i3 exit until resume\n");
  1075. __set_bit(D0I3_PENDING_WAKEUP, &mvm->d0i3_suspend_flags);
  1076. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1077. return 0;
  1078. }
  1079. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1080. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
  1081. if (ret)
  1082. goto out;
  1083. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1084. IEEE80211_IFACE_ITER_NORMAL,
  1085. iwl_mvm_exit_d0i3_iterator,
  1086. mvm);
  1087. out:
  1088. schedule_work(&mvm->d0i3_exit_work);
  1089. return ret;
  1090. }
  1091. int iwl_mvm_exit_d0i3(struct iwl_op_mode *op_mode)
  1092. {
  1093. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  1094. iwl_mvm_ref(mvm, IWL_MVM_REF_EXIT_WORK);
  1095. return _iwl_mvm_exit_d0i3(mvm);
  1096. }
  1097. static void iwl_mvm_napi_add(struct iwl_op_mode *op_mode,
  1098. struct napi_struct *napi,
  1099. struct net_device *napi_dev,
  1100. int (*poll)(struct napi_struct *, int),
  1101. int weight)
  1102. {
  1103. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  1104. ieee80211_napi_add(mvm->hw, napi, napi_dev, poll, weight);
  1105. }
  1106. static const struct iwl_op_mode_ops iwl_mvm_ops = {
  1107. .start = iwl_op_mode_mvm_start,
  1108. .stop = iwl_op_mode_mvm_stop,
  1109. .rx = iwl_mvm_rx_dispatch,
  1110. .queue_full = iwl_mvm_stop_sw_queue,
  1111. .queue_not_full = iwl_mvm_wake_sw_queue,
  1112. .hw_rf_kill = iwl_mvm_set_hw_rfkill_state,
  1113. .free_skb = iwl_mvm_free_skb,
  1114. .nic_error = iwl_mvm_nic_error,
  1115. .cmd_queue_full = iwl_mvm_cmd_queue_full,
  1116. .nic_config = iwl_mvm_nic_config,
  1117. .enter_d0i3 = iwl_mvm_enter_d0i3,
  1118. .exit_d0i3 = iwl_mvm_exit_d0i3,
  1119. .napi_add = iwl_mvm_napi_add,
  1120. };