ops.c 36 KB

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