ops.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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. /*
  86. * module name, copyright, version, etc.
  87. */
  88. #define DRV_DESCRIPTION "The new Intel(R) wireless AGN driver for Linux"
  89. #define DRV_VERSION IWLWIFI_VERSION
  90. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  91. MODULE_VERSION(DRV_VERSION);
  92. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  93. MODULE_LICENSE("GPL");
  94. static const struct iwl_op_mode_ops iwl_mvm_ops;
  95. struct iwl_mvm_mod_params iwlmvm_mod_params = {
  96. .power_scheme = IWL_POWER_SCHEME_BPS,
  97. /* rest of fields are 0 by default */
  98. };
  99. module_param_named(init_dbg, iwlmvm_mod_params.init_dbg, bool, S_IRUGO);
  100. MODULE_PARM_DESC(init_dbg,
  101. "set to true to debug an ASSERT in INIT fw (default: false");
  102. module_param_named(power_scheme, iwlmvm_mod_params.power_scheme, int, S_IRUGO);
  103. MODULE_PARM_DESC(power_scheme,
  104. "power management scheme: 1-active, 2-balanced, 3-low power, default: 2");
  105. /*
  106. * module init and exit functions
  107. */
  108. static int __init iwl_mvm_init(void)
  109. {
  110. int ret;
  111. ret = iwl_mvm_rate_control_register();
  112. if (ret) {
  113. pr_err("Unable to register rate control algorithm: %d\n", ret);
  114. return ret;
  115. }
  116. ret = iwl_opmode_register("iwlmvm", &iwl_mvm_ops);
  117. if (ret) {
  118. pr_err("Unable to register MVM op_mode: %d\n", ret);
  119. iwl_mvm_rate_control_unregister();
  120. }
  121. return ret;
  122. }
  123. module_init(iwl_mvm_init);
  124. static void __exit iwl_mvm_exit(void)
  125. {
  126. iwl_opmode_deregister("iwlmvm");
  127. iwl_mvm_rate_control_unregister();
  128. }
  129. module_exit(iwl_mvm_exit);
  130. static void iwl_mvm_nic_config(struct iwl_op_mode *op_mode)
  131. {
  132. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  133. u8 radio_cfg_type, radio_cfg_step, radio_cfg_dash;
  134. u32 reg_val = 0;
  135. radio_cfg_type = (mvm->fw->phy_config & FW_PHY_CFG_RADIO_TYPE) >>
  136. FW_PHY_CFG_RADIO_TYPE_POS;
  137. radio_cfg_step = (mvm->fw->phy_config & FW_PHY_CFG_RADIO_STEP) >>
  138. FW_PHY_CFG_RADIO_STEP_POS;
  139. radio_cfg_dash = (mvm->fw->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->device_family != IWL_DEVICE_FAMILY_8000)
  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(EOSP_NOTIFICATION, iwl_mvm_rx_eosp_notif, false),
  212. RX_HANDLER(SCAN_REQUEST_CMD, iwl_mvm_rx_scan_response, false),
  213. RX_HANDLER(SCAN_COMPLETE_NOTIFICATION, iwl_mvm_rx_scan_complete, true),
  214. RX_HANDLER(SCAN_OFFLOAD_COMPLETE,
  215. iwl_mvm_rx_scan_offload_complete_notif, true),
  216. RX_HANDLER(MATCH_FOUND_NOTIFICATION, iwl_mvm_rx_scan_offload_results,
  217. false),
  218. RX_HANDLER(SCAN_COMPLETE_UMAC, iwl_mvm_rx_umac_scan_complete_notif,
  219. true),
  220. RX_HANDLER(RADIO_VERSION_NOTIFICATION, iwl_mvm_rx_radio_ver, false),
  221. RX_HANDLER(CARD_STATE_NOTIFICATION, iwl_mvm_rx_card_state_notif, false),
  222. RX_HANDLER(MISSED_BEACONS_NOTIFICATION, iwl_mvm_rx_missed_beacons_notif,
  223. false),
  224. RX_HANDLER(REPLY_ERROR, iwl_mvm_rx_fw_error, false),
  225. RX_HANDLER(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION,
  226. iwl_mvm_power_uapsd_misbehaving_ap_notif, false),
  227. RX_HANDLER(DTS_MEASUREMENT_NOTIFICATION, iwl_mvm_temp_notif, true),
  228. RX_HANDLER(TDLS_CHANNEL_SWITCH_NOTIFICATION, iwl_mvm_rx_tdls_notif,
  229. true),
  230. RX_HANDLER(MFUART_LOAD_NOTIFICATION, iwl_mvm_rx_mfuart_notif, false),
  231. };
  232. #undef RX_HANDLER
  233. #define CMD(x) [x] = #x
  234. static const char *const iwl_mvm_cmd_strings[REPLY_MAX] = {
  235. CMD(MVM_ALIVE),
  236. CMD(REPLY_ERROR),
  237. CMD(INIT_COMPLETE_NOTIF),
  238. CMD(PHY_CONTEXT_CMD),
  239. CMD(MGMT_MCAST_KEY),
  240. CMD(TX_CMD),
  241. CMD(TXPATH_FLUSH),
  242. CMD(MAC_CONTEXT_CMD),
  243. CMD(TIME_EVENT_CMD),
  244. CMD(TIME_EVENT_NOTIFICATION),
  245. CMD(BINDING_CONTEXT_CMD),
  246. CMD(TIME_QUOTA_CMD),
  247. CMD(NON_QOS_TX_COUNTER_CMD),
  248. CMD(RADIO_VERSION_NOTIFICATION),
  249. CMD(SCAN_REQUEST_CMD),
  250. CMD(SCAN_ABORT_CMD),
  251. CMD(SCAN_START_NOTIFICATION),
  252. CMD(SCAN_RESULTS_NOTIFICATION),
  253. CMD(SCAN_COMPLETE_NOTIFICATION),
  254. CMD(NVM_ACCESS_CMD),
  255. CMD(PHY_CONFIGURATION_CMD),
  256. CMD(CALIB_RES_NOTIF_PHY_DB),
  257. CMD(SET_CALIB_DEFAULT_CMD),
  258. CMD(CALIBRATION_COMPLETE_NOTIFICATION),
  259. CMD(ADD_STA_KEY),
  260. CMD(ADD_STA),
  261. CMD(REMOVE_STA),
  262. CMD(LQ_CMD),
  263. CMD(SCAN_OFFLOAD_CONFIG_CMD),
  264. CMD(MATCH_FOUND_NOTIFICATION),
  265. CMD(SCAN_OFFLOAD_REQUEST_CMD),
  266. CMD(SCAN_OFFLOAD_ABORT_CMD),
  267. CMD(HOT_SPOT_CMD),
  268. CMD(SCAN_OFFLOAD_COMPLETE),
  269. CMD(SCAN_OFFLOAD_UPDATE_PROFILES_CMD),
  270. CMD(SCAN_ITERATION_COMPLETE),
  271. CMD(POWER_TABLE_CMD),
  272. CMD(WEP_KEY),
  273. CMD(REPLY_RX_PHY_CMD),
  274. CMD(REPLY_RX_MPDU_CMD),
  275. CMD(BEACON_NOTIFICATION),
  276. CMD(BEACON_TEMPLATE_CMD),
  277. CMD(STATISTICS_NOTIFICATION),
  278. CMD(EOSP_NOTIFICATION),
  279. CMD(REDUCE_TX_POWER_CMD),
  280. CMD(TX_ANT_CONFIGURATION_CMD),
  281. CMD(D3_CONFIG_CMD),
  282. CMD(D0I3_END_CMD),
  283. CMD(PROT_OFFLOAD_CONFIG_CMD),
  284. CMD(OFFLOADS_QUERY_CMD),
  285. CMD(REMOTE_WAKE_CONFIG_CMD),
  286. CMD(WOWLAN_PATTERNS),
  287. CMD(WOWLAN_CONFIGURATION),
  288. CMD(WOWLAN_TSC_RSC_PARAM),
  289. CMD(WOWLAN_TKIP_PARAM),
  290. CMD(WOWLAN_KEK_KCK_MATERIAL),
  291. CMD(WOWLAN_GET_STATUSES),
  292. CMD(WOWLAN_TX_POWER_PER_DB),
  293. CMD(SCAN_OFFLOAD_PROFILES_QUERY_CMD),
  294. CMD(SCAN_OFFLOAD_HOTSPOTS_CONFIG_CMD),
  295. CMD(SCAN_OFFLOAD_HOTSPOTS_QUERY_CMD),
  296. CMD(CARD_STATE_NOTIFICATION),
  297. CMD(MISSED_BEACONS_NOTIFICATION),
  298. CMD(BT_COEX_PRIO_TABLE),
  299. CMD(BT_COEX_PROT_ENV),
  300. CMD(BT_PROFILE_NOTIFICATION),
  301. CMD(BT_CONFIG),
  302. CMD(MCAST_FILTER_CMD),
  303. CMD(BCAST_FILTER_CMD),
  304. CMD(REPLY_SF_CFG_CMD),
  305. CMD(REPLY_BEACON_FILTERING_CMD),
  306. CMD(CMD_DTS_MEASUREMENT_TRIGGER),
  307. CMD(DTS_MEASUREMENT_NOTIFICATION),
  308. CMD(REPLY_THERMAL_MNG_BACKOFF),
  309. CMD(MAC_PM_POWER_TABLE),
  310. CMD(LTR_CONFIG),
  311. CMD(BT_COEX_CI),
  312. CMD(BT_COEX_UPDATE_SW_BOOST),
  313. CMD(BT_COEX_UPDATE_CORUN_LUT),
  314. CMD(BT_COEX_UPDATE_REDUCED_TXP),
  315. CMD(PSM_UAPSD_AP_MISBEHAVING_NOTIFICATION),
  316. CMD(ANTENNA_COUPLING_NOTIFICATION),
  317. CMD(SCD_QUEUE_CFG),
  318. CMD(SCAN_CFG_CMD),
  319. CMD(SCAN_REQ_UMAC),
  320. CMD(SCAN_ABORT_UMAC),
  321. CMD(SCAN_COMPLETE_UMAC),
  322. CMD(TDLS_CHANNEL_SWITCH_CMD),
  323. CMD(TDLS_CHANNEL_SWITCH_NOTIFICATION),
  324. CMD(TDLS_CONFIG_CMD),
  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_WORK(&mvm->fw_error_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. if (!iwlwifi_mod_params.wd_disable)
  422. trans_cfg.queue_watchdog_timeout = cfg->base_params->wd_timeout;
  423. else
  424. trans_cfg.queue_watchdog_timeout = IWL_WATCHDOG_DISABLED;
  425. trans_cfg.command_names = iwl_mvm_cmd_strings;
  426. trans_cfg.cmd_queue = IWL_MVM_CMD_QUEUE;
  427. trans_cfg.cmd_fifo = IWL_MVM_TX_FIFO_CMD;
  428. trans_cfg.scd_set_active = 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. /* set up notification wait support */
  441. iwl_notification_wait_init(&mvm->notif_wait);
  442. /* Init phy db */
  443. mvm->phy_db = iwl_phy_db_init(trans);
  444. if (!mvm->phy_db) {
  445. IWL_ERR(mvm, "Cannot init phy_db\n");
  446. goto out_free;
  447. }
  448. IWL_INFO(mvm, "Detected %s, REV=0x%X\n",
  449. mvm->cfg->name, mvm->trans->hw_rev);
  450. min_backoff = calc_min_backoff(trans, cfg);
  451. iwl_mvm_tt_initialize(mvm, min_backoff);
  452. /* set the nvm_file_name according to priority */
  453. if (iwlwifi_mod_params.nvm_file)
  454. mvm->nvm_file_name = iwlwifi_mod_params.nvm_file;
  455. else
  456. mvm->nvm_file_name = mvm->cfg->default_nvm_file;
  457. if (WARN(cfg->no_power_up_nic_in_init && !mvm->nvm_file_name,
  458. "not allowing power-up and not having nvm_file\n"))
  459. goto out_free;
  460. /*
  461. * Even if nvm exists in the nvm_file driver should read again the nvm
  462. * from the nic because there might be entries that exist in the OTP
  463. * and not in the file.
  464. * for nics with no_power_up_nic_in_init: rely completley on nvm_file
  465. */
  466. if (cfg->no_power_up_nic_in_init && mvm->nvm_file_name) {
  467. err = iwl_nvm_init(mvm, false);
  468. if (err)
  469. goto out_free;
  470. } else {
  471. err = iwl_trans_start_hw(mvm->trans);
  472. if (err)
  473. goto out_free;
  474. mutex_lock(&mvm->mutex);
  475. err = iwl_run_init_mvm_ucode(mvm, true);
  476. if (!err || !iwlmvm_mod_params.init_dbg)
  477. iwl_trans_stop_device(trans);
  478. mutex_unlock(&mvm->mutex);
  479. /* returns 0 if successful, 1 if success but in rfkill */
  480. if (err < 0 && !iwlmvm_mod_params.init_dbg) {
  481. IWL_ERR(mvm, "Failed to run INIT ucode: %d\n", err);
  482. goto out_free;
  483. }
  484. }
  485. scan_size = iwl_mvm_scan_size(mvm);
  486. mvm->scan_cmd = kmalloc(scan_size, GFP_KERNEL);
  487. if (!mvm->scan_cmd)
  488. goto out_free;
  489. err = iwl_mvm_mac_setup_register(mvm);
  490. if (err)
  491. goto out_free;
  492. err = iwl_mvm_dbgfs_register(mvm, dbgfs_dir);
  493. if (err)
  494. goto out_unregister;
  495. memset(&mvm->rx_stats, 0, sizeof(struct mvm_statistics_rx));
  496. /* rpm starts with a taken ref. only set the appropriate bit here. */
  497. mvm->refs[IWL_MVM_REF_UCODE_DOWN] = 1;
  498. return op_mode;
  499. out_unregister:
  500. ieee80211_unregister_hw(mvm->hw);
  501. iwl_mvm_leds_exit(mvm);
  502. out_free:
  503. iwl_phy_db_free(mvm->phy_db);
  504. kfree(mvm->scan_cmd);
  505. if (!cfg->no_power_up_nic_in_init || !mvm->nvm_file_name)
  506. iwl_trans_op_mode_leave(trans);
  507. ieee80211_free_hw(mvm->hw);
  508. return NULL;
  509. }
  510. static void iwl_op_mode_mvm_stop(struct iwl_op_mode *op_mode)
  511. {
  512. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  513. int i;
  514. iwl_mvm_leds_exit(mvm);
  515. iwl_mvm_tt_exit(mvm);
  516. ieee80211_unregister_hw(mvm->hw);
  517. kfree(mvm->scan_cmd);
  518. kfree(mvm->mcast_filter_cmd);
  519. mvm->mcast_filter_cmd = NULL;
  520. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_IWLWIFI_DEBUGFS)
  521. kfree(mvm->d3_resume_sram);
  522. if (mvm->nd_config) {
  523. kfree(mvm->nd_config->match_sets);
  524. kfree(mvm->nd_config);
  525. mvm->nd_config = NULL;
  526. }
  527. #endif
  528. iwl_trans_op_mode_leave(mvm->trans);
  529. iwl_phy_db_free(mvm->phy_db);
  530. mvm->phy_db = NULL;
  531. iwl_free_nvm_data(mvm->nvm_data);
  532. for (i = 0; i < NVM_MAX_NUM_SECTIONS; i++)
  533. kfree(mvm->nvm_sections[i].data);
  534. ieee80211_free_hw(mvm->hw);
  535. }
  536. struct iwl_async_handler_entry {
  537. struct list_head list;
  538. struct iwl_rx_cmd_buffer rxb;
  539. int (*fn)(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  540. struct iwl_device_cmd *cmd);
  541. };
  542. void iwl_mvm_async_handlers_purge(struct iwl_mvm *mvm)
  543. {
  544. struct iwl_async_handler_entry *entry, *tmp;
  545. spin_lock_bh(&mvm->async_handlers_lock);
  546. list_for_each_entry_safe(entry, tmp, &mvm->async_handlers_list, list) {
  547. iwl_free_rxb(&entry->rxb);
  548. list_del(&entry->list);
  549. kfree(entry);
  550. }
  551. spin_unlock_bh(&mvm->async_handlers_lock);
  552. }
  553. static void iwl_mvm_async_handlers_wk(struct work_struct *wk)
  554. {
  555. struct iwl_mvm *mvm =
  556. container_of(wk, struct iwl_mvm, async_handlers_wk);
  557. struct iwl_async_handler_entry *entry, *tmp;
  558. struct list_head local_list;
  559. INIT_LIST_HEAD(&local_list);
  560. /* Ensure that we are not in stop flow (check iwl_mvm_mac_stop) */
  561. mutex_lock(&mvm->mutex);
  562. /*
  563. * Sync with Rx path with a lock. Remove all the entries from this list,
  564. * add them to a local one (lock free), and then handle them.
  565. */
  566. spin_lock_bh(&mvm->async_handlers_lock);
  567. list_splice_init(&mvm->async_handlers_list, &local_list);
  568. spin_unlock_bh(&mvm->async_handlers_lock);
  569. list_for_each_entry_safe(entry, tmp, &local_list, list) {
  570. if (entry->fn(mvm, &entry->rxb, NULL))
  571. IWL_WARN(mvm,
  572. "returned value from ASYNC handlers are ignored\n");
  573. iwl_free_rxb(&entry->rxb);
  574. list_del(&entry->list);
  575. kfree(entry);
  576. }
  577. mutex_unlock(&mvm->mutex);
  578. }
  579. static int iwl_mvm_rx_dispatch(struct iwl_op_mode *op_mode,
  580. struct iwl_rx_cmd_buffer *rxb,
  581. struct iwl_device_cmd *cmd)
  582. {
  583. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  584. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  585. u8 i;
  586. /*
  587. * Do the notification wait before RX handlers so
  588. * even if the RX handler consumes the RXB we have
  589. * access to it in the notification wait entry.
  590. */
  591. iwl_notification_wait_notify(&mvm->notif_wait, pkt);
  592. for (i = 0; i < ARRAY_SIZE(iwl_mvm_rx_handlers); i++) {
  593. const struct iwl_rx_handlers *rx_h = &iwl_mvm_rx_handlers[i];
  594. struct iwl_async_handler_entry *entry;
  595. if (rx_h->cmd_id != pkt->hdr.cmd)
  596. continue;
  597. if (!rx_h->async)
  598. return rx_h->fn(mvm, rxb, cmd);
  599. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  600. /* we can't do much... */
  601. if (!entry)
  602. return 0;
  603. entry->rxb._page = rxb_steal_page(rxb);
  604. entry->rxb._offset = rxb->_offset;
  605. entry->rxb._rx_page_order = rxb->_rx_page_order;
  606. entry->fn = rx_h->fn;
  607. spin_lock(&mvm->async_handlers_lock);
  608. list_add_tail(&entry->list, &mvm->async_handlers_list);
  609. spin_unlock(&mvm->async_handlers_lock);
  610. schedule_work(&mvm->async_handlers_wk);
  611. break;
  612. }
  613. return 0;
  614. }
  615. static void iwl_mvm_stop_sw_queue(struct iwl_op_mode *op_mode, int queue)
  616. {
  617. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  618. int mq = mvm->queue_to_mac80211[queue];
  619. if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
  620. return;
  621. if (atomic_inc_return(&mvm->mac80211_queue_stop_count[mq]) > 1) {
  622. IWL_DEBUG_TX_QUEUES(mvm,
  623. "queue %d (mac80211 %d) already stopped\n",
  624. queue, mq);
  625. return;
  626. }
  627. ieee80211_stop_queue(mvm->hw, mq);
  628. }
  629. static void iwl_mvm_wake_sw_queue(struct iwl_op_mode *op_mode, int queue)
  630. {
  631. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  632. int mq = mvm->queue_to_mac80211[queue];
  633. if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
  634. return;
  635. if (atomic_dec_return(&mvm->mac80211_queue_stop_count[mq]) > 0) {
  636. IWL_DEBUG_TX_QUEUES(mvm,
  637. "queue %d (mac80211 %d) still stopped\n",
  638. queue, mq);
  639. return;
  640. }
  641. ieee80211_wake_queue(mvm->hw, mq);
  642. }
  643. void iwl_mvm_set_hw_ctkill_state(struct iwl_mvm *mvm, bool state)
  644. {
  645. if (state)
  646. set_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
  647. else
  648. clear_bit(IWL_MVM_STATUS_HW_CTKILL, &mvm->status);
  649. wiphy_rfkill_set_hw_state(mvm->hw->wiphy, iwl_mvm_is_radio_killed(mvm));
  650. }
  651. static bool iwl_mvm_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
  652. {
  653. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  654. bool calibrating = ACCESS_ONCE(mvm->calibrating);
  655. if (state)
  656. set_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
  657. else
  658. clear_bit(IWL_MVM_STATUS_HW_RFKILL, &mvm->status);
  659. wiphy_rfkill_set_hw_state(mvm->hw->wiphy, iwl_mvm_is_radio_killed(mvm));
  660. /* iwl_run_init_mvm_ucode is waiting for results, abort it */
  661. if (calibrating)
  662. iwl_abort_notification_waits(&mvm->notif_wait);
  663. /*
  664. * Stop the device if we run OPERATIONAL firmware or if we are in the
  665. * middle of the calibrations.
  666. */
  667. return state && (mvm->cur_ucode != IWL_UCODE_INIT || calibrating);
  668. }
  669. static void iwl_mvm_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  670. {
  671. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  672. struct ieee80211_tx_info *info;
  673. info = IEEE80211_SKB_CB(skb);
  674. iwl_trans_free_tx_cmd(mvm->trans, info->driver_data[1]);
  675. ieee80211_free_txskb(mvm->hw, skb);
  676. }
  677. struct iwl_mvm_reprobe {
  678. struct device *dev;
  679. struct work_struct work;
  680. };
  681. static void iwl_mvm_reprobe_wk(struct work_struct *wk)
  682. {
  683. struct iwl_mvm_reprobe *reprobe;
  684. reprobe = container_of(wk, struct iwl_mvm_reprobe, work);
  685. if (device_reprobe(reprobe->dev))
  686. dev_err(reprobe->dev, "reprobe failed!\n");
  687. kfree(reprobe);
  688. module_put(THIS_MODULE);
  689. }
  690. static void iwl_mvm_fw_error_dump_wk(struct work_struct *work)
  691. {
  692. struct iwl_mvm *mvm =
  693. container_of(work, struct iwl_mvm, fw_error_dump_wk);
  694. mutex_lock(&mvm->mutex);
  695. iwl_mvm_fw_error_dump(mvm);
  696. mutex_unlock(&mvm->mutex);
  697. }
  698. void iwl_mvm_nic_restart(struct iwl_mvm *mvm, bool fw_error)
  699. {
  700. iwl_abort_notification_waits(&mvm->notif_wait);
  701. /*
  702. * This is a bit racy, but worst case we tell mac80211 about
  703. * a stopped/aborted scan when that was already done which
  704. * is not a problem. It is necessary to abort any os scan
  705. * here because mac80211 requires having the scan cleared
  706. * before restarting.
  707. * We'll reset the scan_status to NONE in restart cleanup in
  708. * the next start() call from mac80211. If restart isn't called
  709. * (no fw restart) scan status will stay busy.
  710. */
  711. switch (mvm->scan_status) {
  712. case IWL_MVM_SCAN_NONE:
  713. break;
  714. case IWL_MVM_SCAN_OS:
  715. ieee80211_scan_completed(mvm->hw, true);
  716. break;
  717. case IWL_MVM_SCAN_SCHED:
  718. /* Sched scan will be restarted by mac80211 in restart_hw. */
  719. if (!mvm->restart_fw)
  720. ieee80211_sched_scan_stopped(mvm->hw);
  721. break;
  722. }
  723. /*
  724. * If we're restarting already, don't cycle restarts.
  725. * If INIT fw asserted, it will likely fail again.
  726. * If WoWLAN fw asserted, don't restart either, mac80211
  727. * can't recover this since we're already half suspended.
  728. */
  729. if (test_and_set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  730. struct iwl_mvm_reprobe *reprobe;
  731. IWL_ERR(mvm,
  732. "Firmware error during reconfiguration - reprobe!\n");
  733. /*
  734. * get a module reference to avoid doing this while unloading
  735. * anyway and to avoid scheduling a work with code that's
  736. * being removed.
  737. */
  738. if (!try_module_get(THIS_MODULE)) {
  739. IWL_ERR(mvm, "Module is being unloaded - abort\n");
  740. return;
  741. }
  742. reprobe = kzalloc(sizeof(*reprobe), GFP_ATOMIC);
  743. if (!reprobe) {
  744. module_put(THIS_MODULE);
  745. return;
  746. }
  747. reprobe->dev = mvm->trans->dev;
  748. INIT_WORK(&reprobe->work, iwl_mvm_reprobe_wk);
  749. schedule_work(&reprobe->work);
  750. } else if (mvm->cur_ucode == IWL_UCODE_REGULAR &&
  751. (!fw_error || mvm->restart_fw)) {
  752. /* don't let the transport/FW power down */
  753. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  754. if (fw_error && mvm->restart_fw > 0)
  755. mvm->restart_fw--;
  756. ieee80211_restart_hw(mvm->hw);
  757. } else if (fw_error) {
  758. schedule_work(&mvm->fw_error_dump_wk);
  759. }
  760. }
  761. static void iwl_mvm_nic_error(struct iwl_op_mode *op_mode)
  762. {
  763. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  764. iwl_mvm_dump_nic_error_log(mvm);
  765. iwl_mvm_nic_restart(mvm, true);
  766. }
  767. static void iwl_mvm_cmd_queue_full(struct iwl_op_mode *op_mode)
  768. {
  769. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  770. WARN_ON(1);
  771. iwl_mvm_nic_restart(mvm, true);
  772. }
  773. struct iwl_d0i3_iter_data {
  774. struct iwl_mvm *mvm;
  775. u8 ap_sta_id;
  776. u8 vif_count;
  777. u8 offloading_tid;
  778. bool disable_offloading;
  779. };
  780. static bool iwl_mvm_disallow_offloading(struct iwl_mvm *mvm,
  781. struct ieee80211_vif *vif,
  782. struct iwl_d0i3_iter_data *iter_data)
  783. {
  784. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  785. struct ieee80211_sta *ap_sta;
  786. struct iwl_mvm_sta *mvmsta;
  787. u32 available_tids = 0;
  788. u8 tid;
  789. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION ||
  790. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT))
  791. return false;
  792. ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id]);
  793. if (IS_ERR_OR_NULL(ap_sta))
  794. return false;
  795. mvmsta = iwl_mvm_sta_from_mac80211(ap_sta);
  796. spin_lock_bh(&mvmsta->lock);
  797. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  798. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  799. /*
  800. * in case of pending tx packets, don't use this tid
  801. * for offloading in order to prevent reuse of the same
  802. * qos seq counters.
  803. */
  804. if (iwl_mvm_tid_queued(tid_data))
  805. continue;
  806. if (tid_data->state != IWL_AGG_OFF)
  807. continue;
  808. available_tids |= BIT(tid);
  809. }
  810. spin_unlock_bh(&mvmsta->lock);
  811. /*
  812. * disallow protocol offloading if we have no available tid
  813. * (with no pending frames and no active aggregation,
  814. * as we don't handle "holes" properly - the scheduler needs the
  815. * frame's seq number and TFD index to match)
  816. */
  817. if (!available_tids)
  818. return true;
  819. /* for simplicity, just use the first available tid */
  820. iter_data->offloading_tid = ffs(available_tids) - 1;
  821. return false;
  822. }
  823. static void iwl_mvm_enter_d0i3_iterator(void *_data, u8 *mac,
  824. struct ieee80211_vif *vif)
  825. {
  826. struct iwl_d0i3_iter_data *data = _data;
  827. struct iwl_mvm *mvm = data->mvm;
  828. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  829. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE;
  830. IWL_DEBUG_RPM(mvm, "entering D0i3 - vif %pM\n", vif->addr);
  831. if (vif->type != NL80211_IFTYPE_STATION ||
  832. !vif->bss_conf.assoc)
  833. return;
  834. /*
  835. * in case of pending tx packets or active aggregations,
  836. * avoid offloading features in order to prevent reuse of
  837. * the same qos seq counters.
  838. */
  839. if (iwl_mvm_disallow_offloading(mvm, vif, data))
  840. data->disable_offloading = true;
  841. iwl_mvm_update_d0i3_power_mode(mvm, vif, true, flags);
  842. iwl_mvm_send_proto_offload(mvm, vif, data->disable_offloading, flags);
  843. /*
  844. * on init/association, mvm already configures POWER_TABLE_CMD
  845. * and REPLY_MCAST_FILTER_CMD, so currently don't
  846. * reconfigure them (we might want to use different
  847. * params later on, though).
  848. */
  849. data->ap_sta_id = mvmvif->ap_sta_id;
  850. data->vif_count++;
  851. }
  852. static void iwl_mvm_set_wowlan_data(struct iwl_mvm *mvm,
  853. struct iwl_wowlan_config_cmd *cmd,
  854. struct iwl_d0i3_iter_data *iter_data)
  855. {
  856. struct ieee80211_sta *ap_sta;
  857. struct iwl_mvm_sta *mvm_ap_sta;
  858. if (iter_data->ap_sta_id == IWL_MVM_STATION_COUNT)
  859. return;
  860. rcu_read_lock();
  861. ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[iter_data->ap_sta_id]);
  862. if (IS_ERR_OR_NULL(ap_sta))
  863. goto out;
  864. mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
  865. cmd->is_11n_connection = ap_sta->ht_cap.ht_supported;
  866. cmd->offloading_tid = iter_data->offloading_tid;
  867. /*
  868. * The d0i3 uCode takes care of the nonqos counters,
  869. * so configure only the qos seq ones.
  870. */
  871. iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, cmd);
  872. out:
  873. rcu_read_unlock();
  874. }
  875. static int iwl_mvm_enter_d0i3(struct iwl_op_mode *op_mode)
  876. {
  877. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  878. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE;
  879. int ret;
  880. struct iwl_d0i3_iter_data d0i3_iter_data = {
  881. .mvm = mvm,
  882. };
  883. struct iwl_wowlan_config_cmd wowlan_config_cmd = {
  884. .wakeup_filter = cpu_to_le32(IWL_WOWLAN_WAKEUP_RX_FRAME |
  885. IWL_WOWLAN_WAKEUP_BEACON_MISS |
  886. IWL_WOWLAN_WAKEUP_LINK_CHANGE |
  887. IWL_WOWLAN_WAKEUP_BCN_FILTERING),
  888. };
  889. struct iwl_d3_manager_config d3_cfg_cmd = {
  890. .min_sleep_time = cpu_to_le32(1000),
  891. };
  892. IWL_DEBUG_RPM(mvm, "MVM entering D0i3\n");
  893. /* make sure we have no running tx while configuring the qos */
  894. set_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
  895. synchronize_net();
  896. /*
  897. * iwl_mvm_ref_sync takes a reference before checking the flag.
  898. * so by checking there is no held reference we prevent a state
  899. * in which iwl_mvm_ref_sync continues successfully while we
  900. * configure the firmware to enter d0i3
  901. */
  902. if (iwl_mvm_ref_taken(mvm)) {
  903. IWL_DEBUG_RPM(mvm->trans, "abort d0i3 due to taken ref\n");
  904. clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
  905. wake_up(&mvm->d0i3_exit_waitq);
  906. return 1;
  907. }
  908. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  909. IEEE80211_IFACE_ITER_NORMAL,
  910. iwl_mvm_enter_d0i3_iterator,
  911. &d0i3_iter_data);
  912. if (d0i3_iter_data.vif_count == 1) {
  913. mvm->d0i3_ap_sta_id = d0i3_iter_data.ap_sta_id;
  914. mvm->d0i3_offloading = !d0i3_iter_data.disable_offloading;
  915. } else {
  916. WARN_ON_ONCE(d0i3_iter_data.vif_count > 1);
  917. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  918. mvm->d0i3_offloading = false;
  919. }
  920. iwl_mvm_set_wowlan_data(mvm, &wowlan_config_cmd, &d0i3_iter_data);
  921. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, flags,
  922. sizeof(wowlan_config_cmd),
  923. &wowlan_config_cmd);
  924. if (ret)
  925. return ret;
  926. return iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD,
  927. flags | CMD_MAKE_TRANS_IDLE,
  928. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  929. }
  930. static void iwl_mvm_exit_d0i3_iterator(void *_data, u8 *mac,
  931. struct ieee80211_vif *vif)
  932. {
  933. struct iwl_mvm *mvm = _data;
  934. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO;
  935. IWL_DEBUG_RPM(mvm, "exiting D0i3 - vif %pM\n", vif->addr);
  936. if (vif->type != NL80211_IFTYPE_STATION ||
  937. !vif->bss_conf.assoc)
  938. return;
  939. iwl_mvm_update_d0i3_power_mode(mvm, vif, false, flags);
  940. }
  941. static void iwl_mvm_d0i3_disconnect_iter(void *data, u8 *mac,
  942. struct ieee80211_vif *vif)
  943. {
  944. struct iwl_mvm *mvm = data;
  945. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  946. if (vif->type == NL80211_IFTYPE_STATION && vif->bss_conf.assoc &&
  947. mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
  948. ieee80211_connection_loss(vif);
  949. }
  950. void iwl_mvm_d0i3_enable_tx(struct iwl_mvm *mvm, __le16 *qos_seq)
  951. {
  952. struct ieee80211_sta *sta = NULL;
  953. struct iwl_mvm_sta *mvm_ap_sta;
  954. int i;
  955. bool wake_queues = false;
  956. lockdep_assert_held(&mvm->mutex);
  957. spin_lock_bh(&mvm->d0i3_tx_lock);
  958. if (mvm->d0i3_ap_sta_id == IWL_MVM_STATION_COUNT)
  959. goto out;
  960. IWL_DEBUG_RPM(mvm, "re-enqueue packets\n");
  961. /* get the sta in order to update seq numbers and re-enqueue skbs */
  962. sta = rcu_dereference_protected(
  963. mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id],
  964. lockdep_is_held(&mvm->mutex));
  965. if (IS_ERR_OR_NULL(sta)) {
  966. sta = NULL;
  967. goto out;
  968. }
  969. if (mvm->d0i3_offloading && qos_seq) {
  970. /* update qos seq numbers if offloading was enabled */
  971. mvm_ap_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  972. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  973. u16 seq = le16_to_cpu(qos_seq[i]);
  974. /* firmware stores last-used one, we store next one */
  975. seq += 0x10;
  976. mvm_ap_sta->tid_data[i].seq_number = seq;
  977. }
  978. }
  979. out:
  980. /* re-enqueue (or drop) all packets */
  981. while (!skb_queue_empty(&mvm->d0i3_tx)) {
  982. struct sk_buff *skb = __skb_dequeue(&mvm->d0i3_tx);
  983. if (!sta || iwl_mvm_tx_skb(mvm, skb, sta))
  984. ieee80211_free_txskb(mvm->hw, skb);
  985. /* if the skb_queue is not empty, we need to wake queues */
  986. wake_queues = true;
  987. }
  988. clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
  989. wake_up(&mvm->d0i3_exit_waitq);
  990. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  991. if (wake_queues)
  992. ieee80211_wake_queues(mvm->hw);
  993. spin_unlock_bh(&mvm->d0i3_tx_lock);
  994. }
  995. static void iwl_mvm_d0i3_exit_work(struct work_struct *wk)
  996. {
  997. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, d0i3_exit_work);
  998. struct iwl_host_cmd get_status_cmd = {
  999. .id = WOWLAN_GET_STATUSES,
  1000. .flags = CMD_HIGH_PRIO | CMD_WANT_SKB,
  1001. };
  1002. struct iwl_wowlan_status *status;
  1003. int ret;
  1004. u32 disconnection_reasons, wakeup_reasons;
  1005. __le16 *qos_seq = NULL;
  1006. mutex_lock(&mvm->mutex);
  1007. ret = iwl_mvm_send_cmd(mvm, &get_status_cmd);
  1008. if (ret)
  1009. goto out;
  1010. if (!get_status_cmd.resp_pkt)
  1011. goto out;
  1012. status = (void *)get_status_cmd.resp_pkt->data;
  1013. wakeup_reasons = le32_to_cpu(status->wakeup_reasons);
  1014. qos_seq = status->qos_seq_ctr;
  1015. IWL_DEBUG_RPM(mvm, "wakeup reasons: 0x%x\n", wakeup_reasons);
  1016. disconnection_reasons =
  1017. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1018. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1019. if (wakeup_reasons & disconnection_reasons)
  1020. ieee80211_iterate_active_interfaces(
  1021. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1022. iwl_mvm_d0i3_disconnect_iter, mvm);
  1023. iwl_free_resp(&get_status_cmd);
  1024. out:
  1025. iwl_mvm_d0i3_enable_tx(mvm, qos_seq);
  1026. iwl_mvm_unref(mvm, IWL_MVM_REF_EXIT_WORK);
  1027. mutex_unlock(&mvm->mutex);
  1028. }
  1029. int _iwl_mvm_exit_d0i3(struct iwl_mvm *mvm)
  1030. {
  1031. u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
  1032. CMD_WAKE_UP_TRANS;
  1033. int ret;
  1034. IWL_DEBUG_RPM(mvm, "MVM exiting D0i3\n");
  1035. mutex_lock(&mvm->d0i3_suspend_mutex);
  1036. if (test_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags)) {
  1037. IWL_DEBUG_RPM(mvm, "Deferring d0i3 exit until resume\n");
  1038. __set_bit(D0I3_PENDING_WAKEUP, &mvm->d0i3_suspend_flags);
  1039. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1040. return 0;
  1041. }
  1042. mutex_unlock(&mvm->d0i3_suspend_mutex);
  1043. ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
  1044. if (ret)
  1045. goto out;
  1046. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1047. IEEE80211_IFACE_ITER_NORMAL,
  1048. iwl_mvm_exit_d0i3_iterator,
  1049. mvm);
  1050. out:
  1051. schedule_work(&mvm->d0i3_exit_work);
  1052. return ret;
  1053. }
  1054. static int iwl_mvm_exit_d0i3(struct iwl_op_mode *op_mode)
  1055. {
  1056. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  1057. iwl_mvm_ref(mvm, IWL_MVM_REF_EXIT_WORK);
  1058. return _iwl_mvm_exit_d0i3(mvm);
  1059. }
  1060. static void iwl_mvm_napi_add(struct iwl_op_mode *op_mode,
  1061. struct napi_struct *napi,
  1062. struct net_device *napi_dev,
  1063. int (*poll)(struct napi_struct *, int),
  1064. int weight)
  1065. {
  1066. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  1067. ieee80211_napi_add(mvm->hw, napi, napi_dev, poll, weight);
  1068. }
  1069. static const struct iwl_op_mode_ops iwl_mvm_ops = {
  1070. .start = iwl_op_mode_mvm_start,
  1071. .stop = iwl_op_mode_mvm_stop,
  1072. .rx = iwl_mvm_rx_dispatch,
  1073. .queue_full = iwl_mvm_stop_sw_queue,
  1074. .queue_not_full = iwl_mvm_wake_sw_queue,
  1075. .hw_rf_kill = iwl_mvm_set_hw_rfkill_state,
  1076. .free_skb = iwl_mvm_free_skb,
  1077. .nic_error = iwl_mvm_nic_error,
  1078. .cmd_queue_full = iwl_mvm_cmd_queue_full,
  1079. .nic_config = iwl_mvm_nic_config,
  1080. .enter_d0i3 = iwl_mvm_enter_d0i3,
  1081. .exit_d0i3 = iwl_mvm_exit_d0i3,
  1082. .napi_add = iwl_mvm_napi_add,
  1083. };