mac80211.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/slab.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/delay.h>
  34. #include <linux/sched.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/etherdevice.h>
  38. #include <linux/if_arp.h>
  39. #include <net/ieee80211_radiotap.h>
  40. #include <net/mac80211.h>
  41. #include <asm/div64.h>
  42. #include "iwl-io.h"
  43. #include "iwl-trans.h"
  44. #include "iwl-op-mode.h"
  45. #include "iwl-modparams.h"
  46. #include "dev.h"
  47. #include "calib.h"
  48. #include "agn.h"
  49. /*****************************************************************************
  50. *
  51. * mac80211 entry point functions
  52. *
  53. *****************************************************************************/
  54. static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
  55. {
  56. .max = 1,
  57. .types = BIT(NL80211_IFTYPE_STATION),
  58. },
  59. {
  60. .max = 1,
  61. .types = BIT(NL80211_IFTYPE_AP),
  62. },
  63. };
  64. static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
  65. {
  66. .max = 2,
  67. .types = BIT(NL80211_IFTYPE_STATION),
  68. },
  69. };
  70. static const struct ieee80211_iface_combination
  71. iwlagn_iface_combinations_dualmode[] = {
  72. { .num_different_channels = 1,
  73. .max_interfaces = 2,
  74. .beacon_int_infra_match = true,
  75. .limits = iwlagn_sta_ap_limits,
  76. .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
  77. },
  78. { .num_different_channels = 1,
  79. .max_interfaces = 2,
  80. .limits = iwlagn_2sta_limits,
  81. .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
  82. },
  83. };
  84. /*
  85. * Not a mac80211 entry point function, but it fits in with all the
  86. * other mac80211 functions grouped here.
  87. */
  88. int iwlagn_mac_setup_register(struct iwl_priv *priv,
  89. const struct iwl_ucode_capabilities *capa)
  90. {
  91. int ret;
  92. struct ieee80211_hw *hw = priv->hw;
  93. struct iwl_rxon_context *ctx;
  94. hw->rate_control_algorithm = "iwl-agn-rs";
  95. /* Tell mac80211 our characteristics */
  96. ieee80211_hw_set(hw, SIGNAL_DBM);
  97. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  98. ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
  99. ieee80211_hw_set(hw, SPECTRUM_MGMT);
  100. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  101. ieee80211_hw_set(hw, QUEUE_CONTROL);
  102. ieee80211_hw_set(hw, SUPPORTS_PS);
  103. ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
  104. ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
  105. ieee80211_hw_set(hw, WANT_MONITOR_VIF);
  106. hw->offchannel_tx_hw_queue = IWL_AUX_QUEUE;
  107. hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
  108. /*
  109. * Including the following line will crash some AP's. This
  110. * workaround removes the stimulus which causes the crash until
  111. * the AP software can be fixed.
  112. hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  113. */
  114. if (priv->nvm_data->sku_cap_11n_enable)
  115. hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS |
  116. NL80211_FEATURE_STATIC_SMPS;
  117. /*
  118. * Enable 11w if advertised by firmware and software crypto
  119. * is not enabled (as the firmware will interpret some mgmt
  120. * packets, so enabling it with software crypto isn't safe)
  121. */
  122. if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
  123. !iwlwifi_mod_params.sw_crypto)
  124. ieee80211_hw_set(hw, MFP_CAPABLE);
  125. hw->sta_data_size = sizeof(struct iwl_station_priv);
  126. hw->vif_data_size = sizeof(struct iwl_vif_priv);
  127. for_each_context(priv, ctx) {
  128. hw->wiphy->interface_modes |= ctx->interface_modes;
  129. hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
  130. }
  131. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  132. if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
  133. hw->wiphy->iface_combinations =
  134. iwlagn_iface_combinations_dualmode;
  135. hw->wiphy->n_iface_combinations =
  136. ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
  137. }
  138. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  139. hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
  140. REGULATORY_DISABLE_BEACON_HINTS;
  141. #ifdef CONFIG_PM_SLEEP
  142. if (priv->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
  143. priv->trans->ops->d3_suspend &&
  144. priv->trans->ops->d3_resume &&
  145. device_can_wakeup(priv->trans->dev)) {
  146. priv->wowlan_support.flags = WIPHY_WOWLAN_MAGIC_PKT |
  147. WIPHY_WOWLAN_DISCONNECT |
  148. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  149. WIPHY_WOWLAN_RFKILL_RELEASE;
  150. if (!iwlwifi_mod_params.sw_crypto)
  151. priv->wowlan_support.flags |=
  152. WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  153. WIPHY_WOWLAN_GTK_REKEY_FAILURE;
  154. priv->wowlan_support.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
  155. priv->wowlan_support.pattern_min_len =
  156. IWLAGN_WOWLAN_MIN_PATTERN_LEN;
  157. priv->wowlan_support.pattern_max_len =
  158. IWLAGN_WOWLAN_MAX_PATTERN_LEN;
  159. hw->wiphy->wowlan = &priv->wowlan_support;
  160. }
  161. #endif
  162. if (iwlwifi_mod_params.power_save)
  163. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  164. else
  165. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  166. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  167. /* we create the 802.11 header and a max-length SSID element */
  168. hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 34;
  169. /*
  170. * We don't use all queues: 4 and 9 are unused and any
  171. * aggregation queue gets mapped down to the AC queue.
  172. */
  173. hw->queues = IWLAGN_FIRST_AMPDU_QUEUE;
  174. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  175. if (priv->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
  176. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  177. &priv->nvm_data->bands[IEEE80211_BAND_2GHZ];
  178. if (priv->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
  179. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  180. &priv->nvm_data->bands[IEEE80211_BAND_5GHZ];
  181. hw->wiphy->hw_version = priv->trans->hw_id;
  182. iwl_leds_init(priv);
  183. ret = ieee80211_register_hw(priv->hw);
  184. if (ret) {
  185. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  186. iwl_leds_exit(priv);
  187. return ret;
  188. }
  189. priv->mac80211_registered = 1;
  190. return 0;
  191. }
  192. void iwlagn_mac_unregister(struct iwl_priv *priv)
  193. {
  194. if (!priv->mac80211_registered)
  195. return;
  196. iwl_leds_exit(priv);
  197. ieee80211_unregister_hw(priv->hw);
  198. priv->mac80211_registered = 0;
  199. }
  200. static int __iwl_up(struct iwl_priv *priv)
  201. {
  202. struct iwl_rxon_context *ctx;
  203. int ret;
  204. lockdep_assert_held(&priv->mutex);
  205. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  206. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  207. return -EIO;
  208. }
  209. for_each_context(priv, ctx) {
  210. ret = iwlagn_alloc_bcast_station(priv, ctx);
  211. if (ret) {
  212. iwl_dealloc_bcast_stations(priv);
  213. return ret;
  214. }
  215. }
  216. ret = iwl_run_init_ucode(priv);
  217. if (ret) {
  218. IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
  219. goto error;
  220. }
  221. ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
  222. if (ret) {
  223. IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
  224. goto error;
  225. }
  226. ret = iwl_alive_start(priv);
  227. if (ret)
  228. goto error;
  229. return 0;
  230. error:
  231. set_bit(STATUS_EXIT_PENDING, &priv->status);
  232. iwl_down(priv);
  233. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  234. IWL_ERR(priv, "Unable to initialize device.\n");
  235. return ret;
  236. }
  237. static int iwlagn_mac_start(struct ieee80211_hw *hw)
  238. {
  239. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  240. int ret;
  241. IWL_DEBUG_MAC80211(priv, "enter\n");
  242. /* we should be verifying the device is ready to be opened */
  243. mutex_lock(&priv->mutex);
  244. ret = __iwl_up(priv);
  245. mutex_unlock(&priv->mutex);
  246. if (ret)
  247. return ret;
  248. IWL_DEBUG_INFO(priv, "Start UP work done.\n");
  249. /* Now we should be done, and the READY bit should be set. */
  250. if (WARN_ON(!test_bit(STATUS_READY, &priv->status)))
  251. ret = -EIO;
  252. iwlagn_led_enable(priv);
  253. priv->is_open = 1;
  254. IWL_DEBUG_MAC80211(priv, "leave\n");
  255. return 0;
  256. }
  257. static void iwlagn_mac_stop(struct ieee80211_hw *hw)
  258. {
  259. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  260. IWL_DEBUG_MAC80211(priv, "enter\n");
  261. if (!priv->is_open)
  262. return;
  263. priv->is_open = 0;
  264. mutex_lock(&priv->mutex);
  265. iwl_down(priv);
  266. mutex_unlock(&priv->mutex);
  267. iwl_cancel_deferred_work(priv);
  268. flush_workqueue(priv->workqueue);
  269. IWL_DEBUG_MAC80211(priv, "leave\n");
  270. }
  271. static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
  272. struct ieee80211_vif *vif,
  273. struct cfg80211_gtk_rekey_data *data)
  274. {
  275. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  276. if (iwlwifi_mod_params.sw_crypto)
  277. return;
  278. IWL_DEBUG_MAC80211(priv, "enter\n");
  279. mutex_lock(&priv->mutex);
  280. if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
  281. goto out;
  282. memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
  283. memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
  284. priv->replay_ctr =
  285. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  286. priv->have_rekey_data = true;
  287. out:
  288. mutex_unlock(&priv->mutex);
  289. IWL_DEBUG_MAC80211(priv, "leave\n");
  290. }
  291. #ifdef CONFIG_PM_SLEEP
  292. static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
  293. struct cfg80211_wowlan *wowlan)
  294. {
  295. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  296. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  297. int ret;
  298. if (WARN_ON(!wowlan))
  299. return -EINVAL;
  300. IWL_DEBUG_MAC80211(priv, "enter\n");
  301. mutex_lock(&priv->mutex);
  302. /* Don't attempt WoWLAN when not associated, tear down instead. */
  303. if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
  304. !iwl_is_associated_ctx(ctx)) {
  305. ret = 1;
  306. goto out;
  307. }
  308. ret = iwlagn_suspend(priv, wowlan);
  309. if (ret)
  310. goto error;
  311. /* let the ucode operate on its own */
  312. iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
  313. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  314. iwl_trans_d3_suspend(priv->trans, false);
  315. goto out;
  316. error:
  317. priv->wowlan = false;
  318. iwlagn_prepare_restart(priv);
  319. ieee80211_restart_hw(priv->hw);
  320. out:
  321. mutex_unlock(&priv->mutex);
  322. IWL_DEBUG_MAC80211(priv, "leave\n");
  323. return ret;
  324. }
  325. struct iwl_resume_data {
  326. struct iwl_priv *priv;
  327. struct iwlagn_wowlan_status *cmd;
  328. bool valid;
  329. };
  330. static bool iwl_resume_status_fn(struct iwl_notif_wait_data *notif_wait,
  331. struct iwl_rx_packet *pkt, void *data)
  332. {
  333. struct iwl_resume_data *resume_data = data;
  334. struct iwl_priv *priv = resume_data->priv;
  335. if (iwl_rx_packet_payload_len(pkt) != sizeof(*resume_data->cmd)) {
  336. IWL_ERR(priv, "rx wrong size data\n");
  337. return true;
  338. }
  339. memcpy(resume_data->cmd, pkt->data, sizeof(*resume_data->cmd));
  340. resume_data->valid = true;
  341. return true;
  342. }
  343. static int iwlagn_mac_resume(struct ieee80211_hw *hw)
  344. {
  345. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  346. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  347. struct ieee80211_vif *vif;
  348. u32 base;
  349. int ret;
  350. enum iwl_d3_status d3_status;
  351. struct error_table_start {
  352. /* cf. struct iwl_error_event_table */
  353. u32 valid;
  354. u32 error_id;
  355. } err_info;
  356. struct iwl_notification_wait status_wait;
  357. static const u8 status_cmd[] = {
  358. REPLY_WOWLAN_GET_STATUS,
  359. };
  360. struct iwlagn_wowlan_status status_data = {};
  361. struct iwl_resume_data resume_data = {
  362. .priv = priv,
  363. .cmd = &status_data,
  364. .valid = false,
  365. };
  366. struct cfg80211_wowlan_wakeup wakeup = {
  367. .pattern_idx = -1,
  368. };
  369. #ifdef CONFIG_IWLWIFI_DEBUGFS
  370. const struct fw_img *img;
  371. #endif
  372. IWL_DEBUG_MAC80211(priv, "enter\n");
  373. mutex_lock(&priv->mutex);
  374. /* we'll clear ctx->vif during iwlagn_prepare_restart() */
  375. vif = ctx->vif;
  376. ret = iwl_trans_d3_resume(priv->trans, &d3_status, false);
  377. if (ret)
  378. goto out_unlock;
  379. if (d3_status != IWL_D3_STATUS_ALIVE) {
  380. IWL_INFO(priv, "Device was reset during suspend\n");
  381. goto out_unlock;
  382. }
  383. /* uCode is no longer operating by itself */
  384. iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
  385. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  386. base = priv->device_pointers.error_event_table;
  387. if (!iwlagn_hw_valid_rtc_data_addr(base)) {
  388. IWL_WARN(priv, "Invalid error table during resume!\n");
  389. goto out_unlock;
  390. }
  391. iwl_trans_read_mem_bytes(priv->trans, base,
  392. &err_info, sizeof(err_info));
  393. if (err_info.valid) {
  394. IWL_INFO(priv, "error table is valid (%d, 0x%x)\n",
  395. err_info.valid, err_info.error_id);
  396. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  397. wakeup.rfkill_release = true;
  398. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  399. GFP_KERNEL);
  400. }
  401. goto out_unlock;
  402. }
  403. #ifdef CONFIG_IWLWIFI_DEBUGFS
  404. img = &priv->fw->img[IWL_UCODE_WOWLAN];
  405. if (!priv->wowlan_sram)
  406. priv->wowlan_sram =
  407. kzalloc(img->sec[IWL_UCODE_SECTION_DATA].len,
  408. GFP_KERNEL);
  409. if (priv->wowlan_sram)
  410. iwl_trans_read_mem(priv->trans, 0x800000,
  411. priv->wowlan_sram,
  412. img->sec[IWL_UCODE_SECTION_DATA].len / 4);
  413. #endif
  414. /*
  415. * This is very strange. The GET_STATUS command is sent but the device
  416. * doesn't reply properly, it seems it doesn't close the RBD so one is
  417. * always left open ... As a result, we need to send another command
  418. * and have to reset the driver afterwards. As we need to switch to
  419. * runtime firmware again that'll happen.
  420. */
  421. iwl_init_notification_wait(&priv->notif_wait, &status_wait, status_cmd,
  422. ARRAY_SIZE(status_cmd), iwl_resume_status_fn,
  423. &resume_data);
  424. iwl_dvm_send_cmd_pdu(priv, REPLY_WOWLAN_GET_STATUS, CMD_ASYNC, 0, NULL);
  425. iwl_dvm_send_cmd_pdu(priv, REPLY_ECHO, CMD_ASYNC, 0, NULL);
  426. /* an RBD is left open in the firmware now! */
  427. ret = iwl_wait_notification(&priv->notif_wait, &status_wait, HZ/5);
  428. if (ret)
  429. goto out_unlock;
  430. if (resume_data.valid && priv->contexts[IWL_RXON_CTX_BSS].vif) {
  431. u32 reasons = le32_to_cpu(status_data.wakeup_reason);
  432. struct cfg80211_wowlan_wakeup *wakeup_report;
  433. IWL_INFO(priv, "WoWLAN wakeup reason(s): 0x%.8x\n", reasons);
  434. if (reasons) {
  435. if (reasons & IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET)
  436. wakeup.magic_pkt = true;
  437. if (reasons & IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH)
  438. wakeup.pattern_idx = status_data.pattern_number;
  439. if (reasons & (IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
  440. IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE))
  441. wakeup.disconnect = true;
  442. if (reasons & IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL)
  443. wakeup.gtk_rekey_failure = true;
  444. if (reasons & IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ)
  445. wakeup.eap_identity_req = true;
  446. if (reasons & IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE)
  447. wakeup.four_way_handshake = true;
  448. wakeup_report = &wakeup;
  449. } else {
  450. wakeup_report = NULL;
  451. }
  452. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  453. }
  454. priv->wowlan = false;
  455. iwlagn_prepare_restart(priv);
  456. memset((void *)&ctx->active, 0, sizeof(ctx->active));
  457. iwl_connection_init_rx_config(priv, ctx);
  458. iwlagn_set_rxon_chain(priv, ctx);
  459. out_unlock:
  460. mutex_unlock(&priv->mutex);
  461. IWL_DEBUG_MAC80211(priv, "leave\n");
  462. ieee80211_resume_disconnect(vif);
  463. return 1;
  464. }
  465. static void iwlagn_mac_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  466. {
  467. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  468. device_set_wakeup_enable(priv->trans->dev, enabled);
  469. }
  470. #endif
  471. static void iwlagn_mac_tx(struct ieee80211_hw *hw,
  472. struct ieee80211_tx_control *control,
  473. struct sk_buff *skb)
  474. {
  475. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  476. if (iwlagn_tx_skb(priv, control->sta, skb))
  477. ieee80211_free_txskb(hw, skb);
  478. }
  479. static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
  480. struct ieee80211_vif *vif,
  481. struct ieee80211_key_conf *keyconf,
  482. struct ieee80211_sta *sta,
  483. u32 iv32, u16 *phase1key)
  484. {
  485. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  486. iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
  487. }
  488. static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  489. struct ieee80211_vif *vif,
  490. struct ieee80211_sta *sta,
  491. struct ieee80211_key_conf *key)
  492. {
  493. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  494. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  495. struct iwl_rxon_context *ctx = vif_priv->ctx;
  496. int ret;
  497. bool is_default_wep_key = false;
  498. IWL_DEBUG_MAC80211(priv, "enter\n");
  499. if (iwlwifi_mod_params.sw_crypto) {
  500. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  501. return -EOPNOTSUPP;
  502. }
  503. switch (key->cipher) {
  504. case WLAN_CIPHER_SUITE_TKIP:
  505. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  506. /* fall through */
  507. case WLAN_CIPHER_SUITE_CCMP:
  508. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  509. break;
  510. default:
  511. break;
  512. }
  513. /*
  514. * We could program these keys into the hardware as well, but we
  515. * don't expect much multicast traffic in IBSS and having keys
  516. * for more stations is probably more useful.
  517. *
  518. * Mark key TX-only and return 0.
  519. */
  520. if (vif->type == NL80211_IFTYPE_ADHOC &&
  521. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  522. key->hw_key_idx = WEP_INVALID_OFFSET;
  523. return 0;
  524. }
  525. /* If they key was TX-only, accept deletion */
  526. if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
  527. return 0;
  528. mutex_lock(&priv->mutex);
  529. iwl_scan_cancel_timeout(priv, 100);
  530. BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
  531. /*
  532. * If we are getting WEP group key and we didn't receive any key mapping
  533. * so far, we are in legacy wep mode (group key only), otherwise we are
  534. * in 1X mode.
  535. * In legacy wep mode, we use another host command to the uCode.
  536. */
  537. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  538. key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
  539. if (cmd == SET_KEY)
  540. is_default_wep_key = !ctx->key_mapping_keys;
  541. else
  542. is_default_wep_key =
  543. key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
  544. }
  545. switch (cmd) {
  546. case SET_KEY:
  547. if (is_default_wep_key) {
  548. ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
  549. break;
  550. }
  551. ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
  552. if (ret) {
  553. /*
  554. * can't add key for RX, but we don't need it
  555. * in the device for TX so still return 0
  556. */
  557. ret = 0;
  558. key->hw_key_idx = WEP_INVALID_OFFSET;
  559. }
  560. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  561. break;
  562. case DISABLE_KEY:
  563. if (is_default_wep_key)
  564. ret = iwl_remove_default_wep_key(priv, ctx, key);
  565. else
  566. ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
  567. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  568. break;
  569. default:
  570. ret = -EINVAL;
  571. }
  572. mutex_unlock(&priv->mutex);
  573. IWL_DEBUG_MAC80211(priv, "leave\n");
  574. return ret;
  575. }
  576. static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
  577. {
  578. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
  579. return false;
  580. return true;
  581. }
  582. static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
  583. {
  584. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
  585. return false;
  586. if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
  587. return true;
  588. /* disabled by default */
  589. return false;
  590. }
  591. static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
  592. struct ieee80211_vif *vif,
  593. enum ieee80211_ampdu_mlme_action action,
  594. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  595. u8 buf_size)
  596. {
  597. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  598. int ret = -EINVAL;
  599. struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
  600. IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
  601. sta->addr, tid);
  602. if (!(priv->nvm_data->sku_cap_11n_enable))
  603. return -EACCES;
  604. IWL_DEBUG_MAC80211(priv, "enter\n");
  605. mutex_lock(&priv->mutex);
  606. switch (action) {
  607. case IEEE80211_AMPDU_RX_START:
  608. if (!iwl_enable_rx_ampdu(priv->cfg))
  609. break;
  610. IWL_DEBUG_HT(priv, "start Rx\n");
  611. ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
  612. break;
  613. case IEEE80211_AMPDU_RX_STOP:
  614. IWL_DEBUG_HT(priv, "stop Rx\n");
  615. ret = iwl_sta_rx_agg_stop(priv, sta, tid);
  616. break;
  617. case IEEE80211_AMPDU_TX_START:
  618. if (!priv->trans->ops->txq_enable)
  619. break;
  620. if (!iwl_enable_tx_ampdu(priv->cfg))
  621. break;
  622. IWL_DEBUG_HT(priv, "start Tx\n");
  623. ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
  624. break;
  625. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  626. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  627. IWL_DEBUG_HT(priv, "Flush Tx\n");
  628. ret = iwlagn_tx_agg_flush(priv, vif, sta, tid);
  629. break;
  630. case IEEE80211_AMPDU_TX_STOP_CONT:
  631. IWL_DEBUG_HT(priv, "stop Tx\n");
  632. ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
  633. if ((ret == 0) && (priv->agg_tids_count > 0)) {
  634. priv->agg_tids_count--;
  635. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  636. priv->agg_tids_count);
  637. }
  638. if (!priv->agg_tids_count &&
  639. priv->hw_params.use_rts_for_aggregation) {
  640. /*
  641. * switch off RTS/CTS if it was previously enabled
  642. */
  643. sta_priv->lq_sta.lq.general_params.flags &=
  644. ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  645. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  646. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  647. }
  648. break;
  649. case IEEE80211_AMPDU_TX_OPERATIONAL:
  650. ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
  651. break;
  652. }
  653. mutex_unlock(&priv->mutex);
  654. IWL_DEBUG_MAC80211(priv, "leave\n");
  655. return ret;
  656. }
  657. static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
  658. struct ieee80211_vif *vif,
  659. struct ieee80211_sta *sta)
  660. {
  661. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  662. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  663. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  664. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  665. int ret;
  666. u8 sta_id;
  667. IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
  668. sta->addr);
  669. sta_priv->sta_id = IWL_INVALID_STATION;
  670. atomic_set(&sta_priv->pending_frames, 0);
  671. if (vif->type == NL80211_IFTYPE_AP)
  672. sta_priv->client = true;
  673. ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
  674. is_ap, sta, &sta_id);
  675. if (ret) {
  676. IWL_ERR(priv, "Unable to add station %pM (%d)\n",
  677. sta->addr, ret);
  678. /* Should we return success if return code is EEXIST ? */
  679. return ret;
  680. }
  681. sta_priv->sta_id = sta_id;
  682. return 0;
  683. }
  684. static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
  685. struct ieee80211_vif *vif,
  686. struct ieee80211_sta *sta)
  687. {
  688. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  689. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  690. int ret;
  691. IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n", sta->addr);
  692. if (vif->type == NL80211_IFTYPE_STATION) {
  693. /*
  694. * Station will be removed from device when the RXON
  695. * is set to unassociated -- just deactivate it here
  696. * to avoid re-programming it.
  697. */
  698. ret = 0;
  699. iwl_deactivate_station(priv, sta_priv->sta_id, sta->addr);
  700. } else {
  701. ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
  702. if (ret)
  703. IWL_DEBUG_QUIET_RFKILL(priv,
  704. "Error removing station %pM\n", sta->addr);
  705. }
  706. return ret;
  707. }
  708. static int iwlagn_mac_sta_state(struct ieee80211_hw *hw,
  709. struct ieee80211_vif *vif,
  710. struct ieee80211_sta *sta,
  711. enum ieee80211_sta_state old_state,
  712. enum ieee80211_sta_state new_state)
  713. {
  714. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  715. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  716. enum {
  717. NONE, ADD, REMOVE, HT_RATE_INIT, ADD_RATE_INIT,
  718. } op = NONE;
  719. int ret;
  720. IWL_DEBUG_MAC80211(priv, "station %pM state change %d->%d\n",
  721. sta->addr, old_state, new_state);
  722. mutex_lock(&priv->mutex);
  723. if (vif->type == NL80211_IFTYPE_STATION) {
  724. if (old_state == IEEE80211_STA_NOTEXIST &&
  725. new_state == IEEE80211_STA_NONE)
  726. op = ADD;
  727. else if (old_state == IEEE80211_STA_NONE &&
  728. new_state == IEEE80211_STA_NOTEXIST)
  729. op = REMOVE;
  730. else if (old_state == IEEE80211_STA_AUTH &&
  731. new_state == IEEE80211_STA_ASSOC)
  732. op = HT_RATE_INIT;
  733. } else {
  734. if (old_state == IEEE80211_STA_AUTH &&
  735. new_state == IEEE80211_STA_ASSOC)
  736. op = ADD_RATE_INIT;
  737. else if (old_state == IEEE80211_STA_ASSOC &&
  738. new_state == IEEE80211_STA_AUTH)
  739. op = REMOVE;
  740. }
  741. switch (op) {
  742. case ADD:
  743. ret = iwlagn_mac_sta_add(hw, vif, sta);
  744. if (ret)
  745. break;
  746. /*
  747. * Clear the in-progress flag, the AP station entry was added
  748. * but we'll initialize LQ only when we've associated (which
  749. * would also clear the in-progress flag). This is necessary
  750. * in case we never initialize LQ because association fails.
  751. */
  752. spin_lock_bh(&priv->sta_lock);
  753. priv->stations[iwl_sta_id(sta)].used &=
  754. ~IWL_STA_UCODE_INPROGRESS;
  755. spin_unlock_bh(&priv->sta_lock);
  756. break;
  757. case REMOVE:
  758. ret = iwlagn_mac_sta_remove(hw, vif, sta);
  759. break;
  760. case ADD_RATE_INIT:
  761. ret = iwlagn_mac_sta_add(hw, vif, sta);
  762. if (ret)
  763. break;
  764. /* Initialize rate scaling */
  765. IWL_DEBUG_INFO(priv,
  766. "Initializing rate scaling for station %pM\n",
  767. sta->addr);
  768. iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
  769. ret = 0;
  770. break;
  771. case HT_RATE_INIT:
  772. /* Initialize rate scaling */
  773. ret = iwl_sta_update_ht(priv, vif_priv->ctx, sta);
  774. if (ret)
  775. break;
  776. IWL_DEBUG_INFO(priv,
  777. "Initializing rate scaling for station %pM\n",
  778. sta->addr);
  779. iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
  780. ret = 0;
  781. break;
  782. default:
  783. ret = 0;
  784. break;
  785. }
  786. /*
  787. * mac80211 might WARN if we fail, but due the way we
  788. * (badly) handle hard rfkill, we might fail here
  789. */
  790. if (iwl_is_rfkill(priv))
  791. ret = 0;
  792. mutex_unlock(&priv->mutex);
  793. IWL_DEBUG_MAC80211(priv, "leave\n");
  794. return ret;
  795. }
  796. static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
  797. struct ieee80211_vif *vif,
  798. struct ieee80211_channel_switch *ch_switch)
  799. {
  800. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  801. struct ieee80211_conf *conf = &hw->conf;
  802. struct ieee80211_channel *channel = ch_switch->chandef.chan;
  803. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  804. /*
  805. * MULTI-FIXME
  806. * When we add support for multiple interfaces, we need to
  807. * revisit this. The channel switch command in the device
  808. * only affects the BSS context, but what does that really
  809. * mean? And what if we get a CSA on the second interface?
  810. * This needs a lot of work.
  811. */
  812. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  813. u16 ch;
  814. IWL_DEBUG_MAC80211(priv, "enter\n");
  815. mutex_lock(&priv->mutex);
  816. if (iwl_is_rfkill(priv))
  817. goto out;
  818. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  819. test_bit(STATUS_SCANNING, &priv->status) ||
  820. test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  821. goto out;
  822. if (!iwl_is_associated_ctx(ctx))
  823. goto out;
  824. if (!priv->lib->set_channel_switch)
  825. goto out;
  826. ch = channel->hw_value;
  827. if (le16_to_cpu(ctx->active.channel) == ch)
  828. goto out;
  829. priv->current_ht_config.smps = conf->smps_mode;
  830. /* Configure HT40 channels */
  831. switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
  832. case NL80211_CHAN_NO_HT:
  833. case NL80211_CHAN_HT20:
  834. ctx->ht.is_40mhz = false;
  835. ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  836. break;
  837. case NL80211_CHAN_HT40MINUS:
  838. ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  839. ctx->ht.is_40mhz = true;
  840. break;
  841. case NL80211_CHAN_HT40PLUS:
  842. ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  843. ctx->ht.is_40mhz = true;
  844. break;
  845. }
  846. if ((le16_to_cpu(ctx->staging.channel) != ch))
  847. ctx->staging.flags = 0;
  848. iwl_set_rxon_channel(priv, channel, ctx);
  849. iwl_set_rxon_ht(priv, ht_conf);
  850. iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
  851. /*
  852. * at this point, staging_rxon has the
  853. * configuration for channel switch
  854. */
  855. set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
  856. priv->switch_channel = cpu_to_le16(ch);
  857. if (priv->lib->set_channel_switch(priv, ch_switch)) {
  858. clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
  859. priv->switch_channel = 0;
  860. ieee80211_chswitch_done(ctx->vif, false);
  861. }
  862. out:
  863. mutex_unlock(&priv->mutex);
  864. IWL_DEBUG_MAC80211(priv, "leave\n");
  865. }
  866. void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
  867. {
  868. /*
  869. * MULTI-FIXME
  870. * See iwlagn_mac_channel_switch.
  871. */
  872. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  873. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  874. return;
  875. if (!test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  876. return;
  877. if (ctx->vif)
  878. ieee80211_chswitch_done(ctx->vif, is_success);
  879. }
  880. static void iwlagn_configure_filter(struct ieee80211_hw *hw,
  881. unsigned int changed_flags,
  882. unsigned int *total_flags,
  883. u64 multicast)
  884. {
  885. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  886. __le32 filter_or = 0, filter_nand = 0;
  887. struct iwl_rxon_context *ctx;
  888. #define CHK(test, flag) do { \
  889. if (*total_flags & (test)) \
  890. filter_or |= (flag); \
  891. else \
  892. filter_nand |= (flag); \
  893. } while (0)
  894. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  895. changed_flags, *total_flags);
  896. CHK(FIF_OTHER_BSS, RXON_FILTER_PROMISC_MSK);
  897. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  898. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  899. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  900. #undef CHK
  901. mutex_lock(&priv->mutex);
  902. for_each_context(priv, ctx) {
  903. ctx->staging.filter_flags &= ~filter_nand;
  904. ctx->staging.filter_flags |= filter_or;
  905. /*
  906. * Not committing directly because hardware can perform a scan,
  907. * but we'll eventually commit the filter flags change anyway.
  908. */
  909. }
  910. mutex_unlock(&priv->mutex);
  911. /*
  912. * Receiving all multicast frames is always enabled by the
  913. * default flags setup in iwl_connection_init_rx_config()
  914. * since we currently do not support programming multicast
  915. * filters into the device.
  916. */
  917. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI |
  918. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  919. }
  920. static void iwlagn_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  921. u32 queues, bool drop)
  922. {
  923. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  924. u32 scd_queues;
  925. mutex_lock(&priv->mutex);
  926. IWL_DEBUG_MAC80211(priv, "enter\n");
  927. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  928. IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
  929. goto done;
  930. }
  931. if (iwl_is_rfkill(priv)) {
  932. IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
  933. goto done;
  934. }
  935. scd_queues = BIT(priv->cfg->base_params->num_of_queues) - 1;
  936. scd_queues &= ~(BIT(IWL_IPAN_CMD_QUEUE_NUM) |
  937. BIT(IWL_DEFAULT_CMD_QUEUE_NUM));
  938. if (drop) {
  939. IWL_DEBUG_TX_QUEUES(priv, "Flushing SCD queues: 0x%x\n",
  940. scd_queues);
  941. if (iwlagn_txfifo_flush(priv, scd_queues)) {
  942. IWL_ERR(priv, "flush request fail\n");
  943. goto done;
  944. }
  945. }
  946. IWL_DEBUG_TX_QUEUES(priv, "wait transmit/flush all frames\n");
  947. iwl_trans_wait_tx_queue_empty(priv->trans, scd_queues);
  948. done:
  949. mutex_unlock(&priv->mutex);
  950. IWL_DEBUG_MAC80211(priv, "leave\n");
  951. }
  952. static void iwlagn_mac_event_callback(struct ieee80211_hw *hw,
  953. struct ieee80211_vif *vif,
  954. const struct ieee80211_event *event)
  955. {
  956. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  957. if (event->type != RSSI_EVENT)
  958. return;
  959. IWL_DEBUG_MAC80211(priv, "enter\n");
  960. if (priv->lib->bt_params &&
  961. priv->lib->bt_params->advanced_bt_coexist) {
  962. if (event->u.rssi.data == RSSI_EVENT_LOW)
  963. priv->bt_enable_pspoll = true;
  964. else if (event->u.rssi.data == RSSI_EVENT_HIGH)
  965. priv->bt_enable_pspoll = false;
  966. queue_work(priv->workqueue, &priv->bt_runtime_config);
  967. } else {
  968. IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
  969. "ignoring RSSI callback\n");
  970. }
  971. IWL_DEBUG_MAC80211(priv, "leave\n");
  972. }
  973. static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
  974. struct ieee80211_sta *sta, bool set)
  975. {
  976. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  977. queue_work(priv->workqueue, &priv->beacon_update);
  978. return 0;
  979. }
  980. static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
  981. struct ieee80211_vif *vif, u16 queue,
  982. const struct ieee80211_tx_queue_params *params)
  983. {
  984. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  985. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  986. struct iwl_rxon_context *ctx = vif_priv->ctx;
  987. int q;
  988. if (WARN_ON(!ctx))
  989. return -EINVAL;
  990. IWL_DEBUG_MAC80211(priv, "enter\n");
  991. if (!iwl_is_ready_rf(priv)) {
  992. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  993. return -EIO;
  994. }
  995. if (queue >= AC_NUM) {
  996. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  997. return 0;
  998. }
  999. q = AC_NUM - 1 - queue;
  1000. mutex_lock(&priv->mutex);
  1001. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  1002. cpu_to_le16(params->cw_min);
  1003. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  1004. cpu_to_le16(params->cw_max);
  1005. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  1006. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  1007. cpu_to_le16((params->txop * 32));
  1008. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  1009. mutex_unlock(&priv->mutex);
  1010. IWL_DEBUG_MAC80211(priv, "leave\n");
  1011. return 0;
  1012. }
  1013. static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
  1014. {
  1015. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1016. return priv->ibss_manager == IWL_IBSS_MANAGER;
  1017. }
  1018. static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  1019. {
  1020. iwl_connection_init_rx_config(priv, ctx);
  1021. iwlagn_set_rxon_chain(priv, ctx);
  1022. return iwlagn_commit_rxon(priv, ctx);
  1023. }
  1024. static int iwl_setup_interface(struct iwl_priv *priv,
  1025. struct iwl_rxon_context *ctx)
  1026. {
  1027. struct ieee80211_vif *vif = ctx->vif;
  1028. int err, ac;
  1029. lockdep_assert_held(&priv->mutex);
  1030. /*
  1031. * This variable will be correct only when there's just
  1032. * a single context, but all code using it is for hardware
  1033. * that supports only one context.
  1034. */
  1035. priv->iw_mode = vif->type;
  1036. ctx->is_active = true;
  1037. err = iwl_set_mode(priv, ctx);
  1038. if (err) {
  1039. if (!ctx->always_active)
  1040. ctx->is_active = false;
  1041. return err;
  1042. }
  1043. if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist &&
  1044. vif->type == NL80211_IFTYPE_ADHOC) {
  1045. /*
  1046. * pretend to have high BT traffic as long as we
  1047. * are operating in IBSS mode, as this will cause
  1048. * the rate scaling etc. to behave as intended.
  1049. */
  1050. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1051. }
  1052. /* set up queue mappings */
  1053. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  1054. vif->hw_queue[ac] = ctx->ac_to_queue[ac];
  1055. if (vif->type == NL80211_IFTYPE_AP)
  1056. vif->cab_queue = ctx->mcast_queue;
  1057. else
  1058. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  1059. return 0;
  1060. }
  1061. static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
  1062. struct ieee80211_vif *vif)
  1063. {
  1064. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1065. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1066. struct iwl_rxon_context *tmp, *ctx = NULL;
  1067. int err;
  1068. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
  1069. bool reset = false;
  1070. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1071. viftype, vif->addr);
  1072. mutex_lock(&priv->mutex);
  1073. if (!iwl_is_ready_rf(priv)) {
  1074. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1075. err = -EINVAL;
  1076. goto out;
  1077. }
  1078. for_each_context(priv, tmp) {
  1079. u32 possible_modes =
  1080. tmp->interface_modes | tmp->exclusive_interface_modes;
  1081. if (tmp->vif) {
  1082. /* On reset we need to add the same interface again */
  1083. if (tmp->vif == vif) {
  1084. reset = true;
  1085. ctx = tmp;
  1086. break;
  1087. }
  1088. /* check if this busy context is exclusive */
  1089. if (tmp->exclusive_interface_modes &
  1090. BIT(tmp->vif->type)) {
  1091. err = -EINVAL;
  1092. goto out;
  1093. }
  1094. continue;
  1095. }
  1096. if (!(possible_modes & BIT(viftype)))
  1097. continue;
  1098. /* have maybe usable context w/o interface */
  1099. ctx = tmp;
  1100. break;
  1101. }
  1102. if (!ctx) {
  1103. err = -EOPNOTSUPP;
  1104. goto out;
  1105. }
  1106. vif_priv->ctx = ctx;
  1107. ctx->vif = vif;
  1108. /*
  1109. * In SNIFFER device type, the firmware reports the FCS to
  1110. * the host, rather than snipping it off. Unfortunately,
  1111. * mac80211 doesn't (yet) provide a per-packet flag for
  1112. * this, so that we have to set the hardware flag based
  1113. * on the interfaces added. As the monitor interface can
  1114. * only be present by itself, and will be removed before
  1115. * other interfaces are added, this is safe.
  1116. */
  1117. if (vif->type == NL80211_IFTYPE_MONITOR)
  1118. ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
  1119. else
  1120. __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, priv->hw->flags);
  1121. err = iwl_setup_interface(priv, ctx);
  1122. if (!err || reset)
  1123. goto out;
  1124. ctx->vif = NULL;
  1125. priv->iw_mode = NL80211_IFTYPE_STATION;
  1126. out:
  1127. mutex_unlock(&priv->mutex);
  1128. IWL_DEBUG_MAC80211(priv, "leave\n");
  1129. return err;
  1130. }
  1131. static void iwl_teardown_interface(struct iwl_priv *priv,
  1132. struct ieee80211_vif *vif,
  1133. bool mode_change)
  1134. {
  1135. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1136. lockdep_assert_held(&priv->mutex);
  1137. if (priv->scan_vif == vif) {
  1138. iwl_scan_cancel_timeout(priv, 200);
  1139. iwl_force_scan_end(priv);
  1140. }
  1141. if (!mode_change) {
  1142. iwl_set_mode(priv, ctx);
  1143. if (!ctx->always_active)
  1144. ctx->is_active = false;
  1145. }
  1146. /*
  1147. * When removing the IBSS interface, overwrite the
  1148. * BT traffic load with the stored one from the last
  1149. * notification, if any. If this is a device that
  1150. * doesn't implement this, this has no effect since
  1151. * both values are the same and zero.
  1152. */
  1153. if (vif->type == NL80211_IFTYPE_ADHOC)
  1154. priv->bt_traffic_load = priv->last_bt_traffic_load;
  1155. }
  1156. static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
  1157. struct ieee80211_vif *vif)
  1158. {
  1159. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1160. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1161. IWL_DEBUG_MAC80211(priv, "enter\n");
  1162. mutex_lock(&priv->mutex);
  1163. if (WARN_ON(ctx->vif != vif)) {
  1164. struct iwl_rxon_context *tmp;
  1165. IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
  1166. for_each_context(priv, tmp)
  1167. IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
  1168. tmp->ctxid, tmp, tmp->vif);
  1169. }
  1170. ctx->vif = NULL;
  1171. iwl_teardown_interface(priv, vif, false);
  1172. mutex_unlock(&priv->mutex);
  1173. IWL_DEBUG_MAC80211(priv, "leave\n");
  1174. }
  1175. static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
  1176. struct ieee80211_vif *vif,
  1177. enum nl80211_iftype newtype, bool newp2p)
  1178. {
  1179. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1180. struct iwl_rxon_context *ctx, *tmp;
  1181. enum nl80211_iftype newviftype = newtype;
  1182. u32 interface_modes;
  1183. int err;
  1184. IWL_DEBUG_MAC80211(priv, "enter\n");
  1185. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1186. mutex_lock(&priv->mutex);
  1187. ctx = iwl_rxon_ctx_from_vif(vif);
  1188. /*
  1189. * To simplify this code, only support changes on the
  1190. * BSS context. The PAN context is usually reassigned
  1191. * by creating/removing P2P interfaces anyway.
  1192. */
  1193. if (ctx->ctxid != IWL_RXON_CTX_BSS) {
  1194. err = -EBUSY;
  1195. goto out;
  1196. }
  1197. if (!ctx->vif || !iwl_is_ready_rf(priv)) {
  1198. /*
  1199. * Huh? But wait ... this can maybe happen when
  1200. * we're in the middle of a firmware restart!
  1201. */
  1202. err = -EBUSY;
  1203. goto out;
  1204. }
  1205. /* Check if the switch is supported in the same context */
  1206. interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
  1207. if (!(interface_modes & BIT(newtype))) {
  1208. err = -EBUSY;
  1209. goto out;
  1210. }
  1211. if (ctx->exclusive_interface_modes & BIT(newtype)) {
  1212. for_each_context(priv, tmp) {
  1213. if (ctx == tmp)
  1214. continue;
  1215. if (!tmp->is_active)
  1216. continue;
  1217. /*
  1218. * The current mode switch would be exclusive, but
  1219. * another context is active ... refuse the switch.
  1220. */
  1221. err = -EBUSY;
  1222. goto out;
  1223. }
  1224. }
  1225. /* success */
  1226. iwl_teardown_interface(priv, vif, true);
  1227. vif->type = newviftype;
  1228. vif->p2p = newp2p;
  1229. err = iwl_setup_interface(priv, ctx);
  1230. WARN_ON(err);
  1231. /*
  1232. * We've switched internally, but submitting to the
  1233. * device may have failed for some reason. Mask this
  1234. * error, because otherwise mac80211 will not switch
  1235. * (and set the interface type back) and we'll be
  1236. * out of sync with it.
  1237. */
  1238. err = 0;
  1239. out:
  1240. mutex_unlock(&priv->mutex);
  1241. IWL_DEBUG_MAC80211(priv, "leave\n");
  1242. return err;
  1243. }
  1244. static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
  1245. struct ieee80211_vif *vif,
  1246. struct ieee80211_scan_request *hw_req)
  1247. {
  1248. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1249. struct cfg80211_scan_request *req = &hw_req->req;
  1250. int ret;
  1251. IWL_DEBUG_MAC80211(priv, "enter\n");
  1252. if (req->n_channels == 0)
  1253. return -EINVAL;
  1254. mutex_lock(&priv->mutex);
  1255. /*
  1256. * If an internal scan is in progress, just set
  1257. * up the scan_request as per above.
  1258. */
  1259. if (priv->scan_type != IWL_SCAN_NORMAL) {
  1260. IWL_DEBUG_SCAN(priv,
  1261. "SCAN request during internal scan - defer\n");
  1262. priv->scan_request = req;
  1263. priv->scan_vif = vif;
  1264. ret = 0;
  1265. } else {
  1266. priv->scan_request = req;
  1267. priv->scan_vif = vif;
  1268. /*
  1269. * mac80211 will only ask for one band at a time
  1270. * so using channels[0] here is ok
  1271. */
  1272. ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
  1273. req->channels[0]->band);
  1274. if (ret) {
  1275. priv->scan_request = NULL;
  1276. priv->scan_vif = NULL;
  1277. }
  1278. }
  1279. IWL_DEBUG_MAC80211(priv, "leave\n");
  1280. mutex_unlock(&priv->mutex);
  1281. return ret;
  1282. }
  1283. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1284. {
  1285. struct iwl_addsta_cmd cmd = {
  1286. .mode = STA_CONTROL_MODIFY_MSK,
  1287. .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
  1288. .sta.sta_id = sta_id,
  1289. };
  1290. iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
  1291. }
  1292. static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
  1293. struct ieee80211_vif *vif,
  1294. enum sta_notify_cmd cmd,
  1295. struct ieee80211_sta *sta)
  1296. {
  1297. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1298. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  1299. int sta_id;
  1300. IWL_DEBUG_MAC80211(priv, "enter\n");
  1301. switch (cmd) {
  1302. case STA_NOTIFY_SLEEP:
  1303. WARN_ON(!sta_priv->client);
  1304. sta_priv->asleep = true;
  1305. if (atomic_read(&sta_priv->pending_frames) > 0)
  1306. ieee80211_sta_block_awake(hw, sta, true);
  1307. break;
  1308. case STA_NOTIFY_AWAKE:
  1309. WARN_ON(!sta_priv->client);
  1310. if (!sta_priv->asleep)
  1311. break;
  1312. sta_priv->asleep = false;
  1313. sta_id = iwl_sta_id(sta);
  1314. if (sta_id != IWL_INVALID_STATION)
  1315. iwl_sta_modify_ps_wake(priv, sta_id);
  1316. break;
  1317. default:
  1318. break;
  1319. }
  1320. IWL_DEBUG_MAC80211(priv, "leave\n");
  1321. }
  1322. const struct ieee80211_ops iwlagn_hw_ops = {
  1323. .tx = iwlagn_mac_tx,
  1324. .start = iwlagn_mac_start,
  1325. .stop = iwlagn_mac_stop,
  1326. #ifdef CONFIG_PM_SLEEP
  1327. .suspend = iwlagn_mac_suspend,
  1328. .resume = iwlagn_mac_resume,
  1329. .set_wakeup = iwlagn_mac_set_wakeup,
  1330. #endif
  1331. .add_interface = iwlagn_mac_add_interface,
  1332. .remove_interface = iwlagn_mac_remove_interface,
  1333. .change_interface = iwlagn_mac_change_interface,
  1334. .config = iwlagn_mac_config,
  1335. .configure_filter = iwlagn_configure_filter,
  1336. .set_key = iwlagn_mac_set_key,
  1337. .update_tkip_key = iwlagn_mac_update_tkip_key,
  1338. .set_rekey_data = iwlagn_mac_set_rekey_data,
  1339. .conf_tx = iwlagn_mac_conf_tx,
  1340. .bss_info_changed = iwlagn_bss_info_changed,
  1341. .ampdu_action = iwlagn_mac_ampdu_action,
  1342. .hw_scan = iwlagn_mac_hw_scan,
  1343. .sta_notify = iwlagn_mac_sta_notify,
  1344. .sta_state = iwlagn_mac_sta_state,
  1345. .channel_switch = iwlagn_mac_channel_switch,
  1346. .flush = iwlagn_mac_flush,
  1347. .tx_last_beacon = iwlagn_mac_tx_last_beacon,
  1348. .event_callback = iwlagn_mac_event_callback,
  1349. .set_tim = iwlagn_mac_set_tim,
  1350. };
  1351. /* This function both allocates and initializes hw and priv. */
  1352. struct ieee80211_hw *iwl_alloc_all(void)
  1353. {
  1354. struct iwl_priv *priv;
  1355. struct iwl_op_mode *op_mode;
  1356. /* mac80211 allocates memory for this device instance, including
  1357. * space for this driver's private structure */
  1358. struct ieee80211_hw *hw;
  1359. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
  1360. sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
  1361. if (!hw)
  1362. goto out;
  1363. op_mode = hw->priv;
  1364. priv = IWL_OP_MODE_GET_DVM(op_mode);
  1365. priv->hw = hw;
  1366. out:
  1367. return hw;
  1368. }