ops.c 39 KB

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