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