ops.c 37 KB

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