ops.c 36 KB

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