ops.c 39 KB

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