mac80211.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  115. IEEE80211_HW_SUPPORTS_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_channel_switch *ch_switch)
  797. {
  798. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  799. struct ieee80211_conf *conf = &hw->conf;
  800. struct ieee80211_channel *channel = ch_switch->chandef.chan;
  801. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  802. /*
  803. * MULTI-FIXME
  804. * When we add support for multiple interfaces, we need to
  805. * revisit this. The channel switch command in the device
  806. * only affects the BSS context, but what does that really
  807. * mean? And what if we get a CSA on the second interface?
  808. * This needs a lot of work.
  809. */
  810. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  811. u16 ch;
  812. IWL_DEBUG_MAC80211(priv, "enter\n");
  813. mutex_lock(&priv->mutex);
  814. if (iwl_is_rfkill(priv))
  815. goto out;
  816. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  817. test_bit(STATUS_SCANNING, &priv->status) ||
  818. test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  819. goto out;
  820. if (!iwl_is_associated_ctx(ctx))
  821. goto out;
  822. if (!priv->lib->set_channel_switch)
  823. goto out;
  824. ch = channel->hw_value;
  825. if (le16_to_cpu(ctx->active.channel) == ch)
  826. goto out;
  827. priv->current_ht_config.smps = conf->smps_mode;
  828. /* Configure HT40 channels */
  829. switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
  830. case NL80211_CHAN_NO_HT:
  831. case NL80211_CHAN_HT20:
  832. ctx->ht.is_40mhz = false;
  833. ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  834. break;
  835. case NL80211_CHAN_HT40MINUS:
  836. ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  837. ctx->ht.is_40mhz = true;
  838. break;
  839. case NL80211_CHAN_HT40PLUS:
  840. ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  841. ctx->ht.is_40mhz = true;
  842. break;
  843. }
  844. if ((le16_to_cpu(ctx->staging.channel) != ch))
  845. ctx->staging.flags = 0;
  846. iwl_set_rxon_channel(priv, channel, ctx);
  847. iwl_set_rxon_ht(priv, ht_conf);
  848. iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
  849. /*
  850. * at this point, staging_rxon has the
  851. * configuration for channel switch
  852. */
  853. set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
  854. priv->switch_channel = cpu_to_le16(ch);
  855. if (priv->lib->set_channel_switch(priv, ch_switch)) {
  856. clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
  857. priv->switch_channel = 0;
  858. ieee80211_chswitch_done(ctx->vif, false);
  859. }
  860. out:
  861. mutex_unlock(&priv->mutex);
  862. IWL_DEBUG_MAC80211(priv, "leave\n");
  863. }
  864. void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
  865. {
  866. /*
  867. * MULTI-FIXME
  868. * See iwlagn_mac_channel_switch.
  869. */
  870. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  871. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  872. return;
  873. if (!test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  874. return;
  875. if (ctx->vif)
  876. ieee80211_chswitch_done(ctx->vif, is_success);
  877. }
  878. static void iwlagn_configure_filter(struct ieee80211_hw *hw,
  879. unsigned int changed_flags,
  880. unsigned int *total_flags,
  881. u64 multicast)
  882. {
  883. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  884. __le32 filter_or = 0, filter_nand = 0;
  885. struct iwl_rxon_context *ctx;
  886. #define CHK(test, flag) do { \
  887. if (*total_flags & (test)) \
  888. filter_or |= (flag); \
  889. else \
  890. filter_nand |= (flag); \
  891. } while (0)
  892. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  893. changed_flags, *total_flags);
  894. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  895. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  896. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  897. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  898. #undef CHK
  899. mutex_lock(&priv->mutex);
  900. for_each_context(priv, ctx) {
  901. ctx->staging.filter_flags &= ~filter_nand;
  902. ctx->staging.filter_flags |= filter_or;
  903. /*
  904. * Not committing directly because hardware can perform a scan,
  905. * but we'll eventually commit the filter flags change anyway.
  906. */
  907. }
  908. mutex_unlock(&priv->mutex);
  909. /*
  910. * Receiving all multicast frames is always enabled by the
  911. * default flags setup in iwl_connection_init_rx_config()
  912. * since we currently do not support programming multicast
  913. * filters into the device.
  914. */
  915. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  916. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  917. }
  918. static void iwlagn_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  919. u32 queues, bool drop)
  920. {
  921. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  922. mutex_lock(&priv->mutex);
  923. IWL_DEBUG_MAC80211(priv, "enter\n");
  924. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  925. IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
  926. goto done;
  927. }
  928. if (iwl_is_rfkill(priv)) {
  929. IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
  930. goto done;
  931. }
  932. /*
  933. * mac80211 will not push any more frames for transmit
  934. * until the flush is completed
  935. */
  936. if (drop) {
  937. IWL_DEBUG_MAC80211(priv, "send flush command\n");
  938. if (iwlagn_txfifo_flush(priv, 0)) {
  939. IWL_ERR(priv, "flush request fail\n");
  940. goto done;
  941. }
  942. }
  943. IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
  944. iwl_trans_wait_tx_queue_empty(priv->trans, 0xffffffff);
  945. done:
  946. mutex_unlock(&priv->mutex);
  947. IWL_DEBUG_MAC80211(priv, "leave\n");
  948. }
  949. static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
  950. struct ieee80211_vif *vif,
  951. enum ieee80211_rssi_event rssi_event)
  952. {
  953. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  954. IWL_DEBUG_MAC80211(priv, "enter\n");
  955. mutex_lock(&priv->mutex);
  956. if (priv->lib->bt_params &&
  957. priv->lib->bt_params->advanced_bt_coexist) {
  958. if (rssi_event == RSSI_EVENT_LOW)
  959. priv->bt_enable_pspoll = true;
  960. else if (rssi_event == RSSI_EVENT_HIGH)
  961. priv->bt_enable_pspoll = false;
  962. iwlagn_send_advance_bt_config(priv);
  963. } else {
  964. IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
  965. "ignoring RSSI callback\n");
  966. }
  967. mutex_unlock(&priv->mutex);
  968. IWL_DEBUG_MAC80211(priv, "leave\n");
  969. }
  970. static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
  971. struct ieee80211_sta *sta, bool set)
  972. {
  973. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  974. queue_work(priv->workqueue, &priv->beacon_update);
  975. return 0;
  976. }
  977. static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
  978. struct ieee80211_vif *vif, u16 queue,
  979. const struct ieee80211_tx_queue_params *params)
  980. {
  981. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  982. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  983. struct iwl_rxon_context *ctx = vif_priv->ctx;
  984. int q;
  985. if (WARN_ON(!ctx))
  986. return -EINVAL;
  987. IWL_DEBUG_MAC80211(priv, "enter\n");
  988. if (!iwl_is_ready_rf(priv)) {
  989. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  990. return -EIO;
  991. }
  992. if (queue >= AC_NUM) {
  993. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  994. return 0;
  995. }
  996. q = AC_NUM - 1 - queue;
  997. mutex_lock(&priv->mutex);
  998. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  999. cpu_to_le16(params->cw_min);
  1000. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  1001. cpu_to_le16(params->cw_max);
  1002. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  1003. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  1004. cpu_to_le16((params->txop * 32));
  1005. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  1006. mutex_unlock(&priv->mutex);
  1007. IWL_DEBUG_MAC80211(priv, "leave\n");
  1008. return 0;
  1009. }
  1010. static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
  1011. {
  1012. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1013. return priv->ibss_manager == IWL_IBSS_MANAGER;
  1014. }
  1015. static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  1016. {
  1017. iwl_connection_init_rx_config(priv, ctx);
  1018. iwlagn_set_rxon_chain(priv, ctx);
  1019. return iwlagn_commit_rxon(priv, ctx);
  1020. }
  1021. static int iwl_setup_interface(struct iwl_priv *priv,
  1022. struct iwl_rxon_context *ctx)
  1023. {
  1024. struct ieee80211_vif *vif = ctx->vif;
  1025. int err, ac;
  1026. lockdep_assert_held(&priv->mutex);
  1027. /*
  1028. * This variable will be correct only when there's just
  1029. * a single context, but all code using it is for hardware
  1030. * that supports only one context.
  1031. */
  1032. priv->iw_mode = vif->type;
  1033. ctx->is_active = true;
  1034. err = iwl_set_mode(priv, ctx);
  1035. if (err) {
  1036. if (!ctx->always_active)
  1037. ctx->is_active = false;
  1038. return err;
  1039. }
  1040. if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist &&
  1041. vif->type == NL80211_IFTYPE_ADHOC) {
  1042. /*
  1043. * pretend to have high BT traffic as long as we
  1044. * are operating in IBSS mode, as this will cause
  1045. * the rate scaling etc. to behave as intended.
  1046. */
  1047. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1048. }
  1049. /* set up queue mappings */
  1050. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  1051. vif->hw_queue[ac] = ctx->ac_to_queue[ac];
  1052. if (vif->type == NL80211_IFTYPE_AP)
  1053. vif->cab_queue = ctx->mcast_queue;
  1054. else
  1055. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  1056. return 0;
  1057. }
  1058. static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
  1059. struct ieee80211_vif *vif)
  1060. {
  1061. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1062. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1063. struct iwl_rxon_context *tmp, *ctx = NULL;
  1064. int err;
  1065. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
  1066. bool reset = false;
  1067. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1068. viftype, vif->addr);
  1069. mutex_lock(&priv->mutex);
  1070. if (!iwl_is_ready_rf(priv)) {
  1071. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1072. err = -EINVAL;
  1073. goto out;
  1074. }
  1075. for_each_context(priv, tmp) {
  1076. u32 possible_modes =
  1077. tmp->interface_modes | tmp->exclusive_interface_modes;
  1078. if (tmp->vif) {
  1079. /* On reset we need to add the same interface again */
  1080. if (tmp->vif == vif) {
  1081. reset = true;
  1082. ctx = tmp;
  1083. break;
  1084. }
  1085. /* check if this busy context is exclusive */
  1086. if (tmp->exclusive_interface_modes &
  1087. BIT(tmp->vif->type)) {
  1088. err = -EINVAL;
  1089. goto out;
  1090. }
  1091. continue;
  1092. }
  1093. if (!(possible_modes & BIT(viftype)))
  1094. continue;
  1095. /* have maybe usable context w/o interface */
  1096. ctx = tmp;
  1097. break;
  1098. }
  1099. if (!ctx) {
  1100. err = -EOPNOTSUPP;
  1101. goto out;
  1102. }
  1103. vif_priv->ctx = ctx;
  1104. ctx->vif = vif;
  1105. /*
  1106. * In SNIFFER device type, the firmware reports the FCS to
  1107. * the host, rather than snipping it off. Unfortunately,
  1108. * mac80211 doesn't (yet) provide a per-packet flag for
  1109. * this, so that we have to set the hardware flag based
  1110. * on the interfaces added. As the monitor interface can
  1111. * only be present by itself, and will be removed before
  1112. * other interfaces are added, this is safe.
  1113. */
  1114. if (vif->type == NL80211_IFTYPE_MONITOR)
  1115. priv->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
  1116. else
  1117. priv->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
  1118. err = iwl_setup_interface(priv, ctx);
  1119. if (!err || reset)
  1120. goto out;
  1121. ctx->vif = NULL;
  1122. priv->iw_mode = NL80211_IFTYPE_STATION;
  1123. out:
  1124. mutex_unlock(&priv->mutex);
  1125. IWL_DEBUG_MAC80211(priv, "leave\n");
  1126. return err;
  1127. }
  1128. static void iwl_teardown_interface(struct iwl_priv *priv,
  1129. struct ieee80211_vif *vif,
  1130. bool mode_change)
  1131. {
  1132. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1133. lockdep_assert_held(&priv->mutex);
  1134. if (priv->scan_vif == vif) {
  1135. iwl_scan_cancel_timeout(priv, 200);
  1136. iwl_force_scan_end(priv);
  1137. }
  1138. if (!mode_change) {
  1139. iwl_set_mode(priv, ctx);
  1140. if (!ctx->always_active)
  1141. ctx->is_active = false;
  1142. }
  1143. /*
  1144. * When removing the IBSS interface, overwrite the
  1145. * BT traffic load with the stored one from the last
  1146. * notification, if any. If this is a device that
  1147. * doesn't implement this, this has no effect since
  1148. * both values are the same and zero.
  1149. */
  1150. if (vif->type == NL80211_IFTYPE_ADHOC)
  1151. priv->bt_traffic_load = priv->last_bt_traffic_load;
  1152. }
  1153. static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
  1154. struct ieee80211_vif *vif)
  1155. {
  1156. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1157. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1158. IWL_DEBUG_MAC80211(priv, "enter\n");
  1159. mutex_lock(&priv->mutex);
  1160. if (WARN_ON(ctx->vif != vif)) {
  1161. struct iwl_rxon_context *tmp;
  1162. IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
  1163. for_each_context(priv, tmp)
  1164. IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
  1165. tmp->ctxid, tmp, tmp->vif);
  1166. }
  1167. ctx->vif = NULL;
  1168. iwl_teardown_interface(priv, vif, false);
  1169. mutex_unlock(&priv->mutex);
  1170. IWL_DEBUG_MAC80211(priv, "leave\n");
  1171. }
  1172. static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
  1173. struct ieee80211_vif *vif,
  1174. enum nl80211_iftype newtype, bool newp2p)
  1175. {
  1176. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1177. struct iwl_rxon_context *ctx, *tmp;
  1178. enum nl80211_iftype newviftype = newtype;
  1179. u32 interface_modes;
  1180. int err;
  1181. IWL_DEBUG_MAC80211(priv, "enter\n");
  1182. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1183. mutex_lock(&priv->mutex);
  1184. ctx = iwl_rxon_ctx_from_vif(vif);
  1185. /*
  1186. * To simplify this code, only support changes on the
  1187. * BSS context. The PAN context is usually reassigned
  1188. * by creating/removing P2P interfaces anyway.
  1189. */
  1190. if (ctx->ctxid != IWL_RXON_CTX_BSS) {
  1191. err = -EBUSY;
  1192. goto out;
  1193. }
  1194. if (!ctx->vif || !iwl_is_ready_rf(priv)) {
  1195. /*
  1196. * Huh? But wait ... this can maybe happen when
  1197. * we're in the middle of a firmware restart!
  1198. */
  1199. err = -EBUSY;
  1200. goto out;
  1201. }
  1202. /* Check if the switch is supported in the same context */
  1203. interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
  1204. if (!(interface_modes & BIT(newtype))) {
  1205. err = -EBUSY;
  1206. goto out;
  1207. }
  1208. if (ctx->exclusive_interface_modes & BIT(newtype)) {
  1209. for_each_context(priv, tmp) {
  1210. if (ctx == tmp)
  1211. continue;
  1212. if (!tmp->is_active)
  1213. continue;
  1214. /*
  1215. * The current mode switch would be exclusive, but
  1216. * another context is active ... refuse the switch.
  1217. */
  1218. err = -EBUSY;
  1219. goto out;
  1220. }
  1221. }
  1222. /* success */
  1223. iwl_teardown_interface(priv, vif, true);
  1224. vif->type = newviftype;
  1225. vif->p2p = newp2p;
  1226. err = iwl_setup_interface(priv, ctx);
  1227. WARN_ON(err);
  1228. /*
  1229. * We've switched internally, but submitting to the
  1230. * device may have failed for some reason. Mask this
  1231. * error, because otherwise mac80211 will not switch
  1232. * (and set the interface type back) and we'll be
  1233. * out of sync with it.
  1234. */
  1235. err = 0;
  1236. out:
  1237. mutex_unlock(&priv->mutex);
  1238. IWL_DEBUG_MAC80211(priv, "leave\n");
  1239. return err;
  1240. }
  1241. static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
  1242. struct ieee80211_vif *vif,
  1243. struct cfg80211_scan_request *req)
  1244. {
  1245. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1246. int ret;
  1247. IWL_DEBUG_MAC80211(priv, "enter\n");
  1248. if (req->n_channels == 0)
  1249. return -EINVAL;
  1250. mutex_lock(&priv->mutex);
  1251. /*
  1252. * If an internal scan is in progress, just set
  1253. * up the scan_request as per above.
  1254. */
  1255. if (priv->scan_type != IWL_SCAN_NORMAL) {
  1256. IWL_DEBUG_SCAN(priv,
  1257. "SCAN request during internal scan - defer\n");
  1258. priv->scan_request = req;
  1259. priv->scan_vif = vif;
  1260. ret = 0;
  1261. } else {
  1262. priv->scan_request = req;
  1263. priv->scan_vif = vif;
  1264. /*
  1265. * mac80211 will only ask for one band at a time
  1266. * so using channels[0] here is ok
  1267. */
  1268. ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
  1269. req->channels[0]->band);
  1270. if (ret) {
  1271. priv->scan_request = NULL;
  1272. priv->scan_vif = NULL;
  1273. }
  1274. }
  1275. IWL_DEBUG_MAC80211(priv, "leave\n");
  1276. mutex_unlock(&priv->mutex);
  1277. return ret;
  1278. }
  1279. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1280. {
  1281. struct iwl_addsta_cmd cmd = {
  1282. .mode = STA_CONTROL_MODIFY_MSK,
  1283. .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
  1284. .sta.sta_id = sta_id,
  1285. };
  1286. iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
  1287. }
  1288. static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
  1289. struct ieee80211_vif *vif,
  1290. enum sta_notify_cmd cmd,
  1291. struct ieee80211_sta *sta)
  1292. {
  1293. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1294. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  1295. int sta_id;
  1296. IWL_DEBUG_MAC80211(priv, "enter\n");
  1297. switch (cmd) {
  1298. case STA_NOTIFY_SLEEP:
  1299. WARN_ON(!sta_priv->client);
  1300. sta_priv->asleep = true;
  1301. if (atomic_read(&sta_priv->pending_frames) > 0)
  1302. ieee80211_sta_block_awake(hw, sta, true);
  1303. break;
  1304. case STA_NOTIFY_AWAKE:
  1305. WARN_ON(!sta_priv->client);
  1306. if (!sta_priv->asleep)
  1307. break;
  1308. sta_priv->asleep = false;
  1309. sta_id = iwl_sta_id(sta);
  1310. if (sta_id != IWL_INVALID_STATION)
  1311. iwl_sta_modify_ps_wake(priv, sta_id);
  1312. break;
  1313. default:
  1314. break;
  1315. }
  1316. IWL_DEBUG_MAC80211(priv, "leave\n");
  1317. }
  1318. const struct ieee80211_ops iwlagn_hw_ops = {
  1319. .tx = iwlagn_mac_tx,
  1320. .start = iwlagn_mac_start,
  1321. .stop = iwlagn_mac_stop,
  1322. #ifdef CONFIG_PM_SLEEP
  1323. .suspend = iwlagn_mac_suspend,
  1324. .resume = iwlagn_mac_resume,
  1325. .set_wakeup = iwlagn_mac_set_wakeup,
  1326. #endif
  1327. .add_interface = iwlagn_mac_add_interface,
  1328. .remove_interface = iwlagn_mac_remove_interface,
  1329. .change_interface = iwlagn_mac_change_interface,
  1330. .config = iwlagn_mac_config,
  1331. .configure_filter = iwlagn_configure_filter,
  1332. .set_key = iwlagn_mac_set_key,
  1333. .update_tkip_key = iwlagn_mac_update_tkip_key,
  1334. .set_rekey_data = iwlagn_mac_set_rekey_data,
  1335. .conf_tx = iwlagn_mac_conf_tx,
  1336. .bss_info_changed = iwlagn_bss_info_changed,
  1337. .ampdu_action = iwlagn_mac_ampdu_action,
  1338. .hw_scan = iwlagn_mac_hw_scan,
  1339. .sta_notify = iwlagn_mac_sta_notify,
  1340. .sta_state = iwlagn_mac_sta_state,
  1341. .channel_switch = iwlagn_mac_channel_switch,
  1342. .flush = iwlagn_mac_flush,
  1343. .tx_last_beacon = iwlagn_mac_tx_last_beacon,
  1344. .rssi_callback = iwlagn_mac_rssi_callback,
  1345. .set_tim = iwlagn_mac_set_tim,
  1346. };
  1347. /* This function both allocates and initializes hw and priv. */
  1348. struct ieee80211_hw *iwl_alloc_all(void)
  1349. {
  1350. struct iwl_priv *priv;
  1351. struct iwl_op_mode *op_mode;
  1352. /* mac80211 allocates memory for this device instance, including
  1353. * space for this driver's private structure */
  1354. struct ieee80211_hw *hw;
  1355. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
  1356. sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
  1357. if (!hw)
  1358. goto out;
  1359. op_mode = hw->priv;
  1360. priv = IWL_OP_MODE_GET_DVM(op_mode);
  1361. priv->hw = hw;
  1362. out:
  1363. return hw;
  1364. }