ops.c 35 KB

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