iwl-mac80211.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 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/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/if_arp.h>
  40. #include <net/mac80211.h>
  41. #include <asm/div64.h>
  42. #include "iwl-ucode.h"
  43. #include "iwl-eeprom.h"
  44. #include "iwl-wifi.h"
  45. #include "iwl-dev.h"
  46. #include "iwl-core.h"
  47. #include "iwl-io.h"
  48. #include "iwl-agn-calib.h"
  49. #include "iwl-agn.h"
  50. #include "iwl-shared.h"
  51. #include "iwl-trans.h"
  52. #include "iwl-op-mode.h"
  53. /*****************************************************************************
  54. *
  55. * mac80211 entry point functions
  56. *
  57. *****************************************************************************/
  58. static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
  59. {
  60. .max = 1,
  61. .types = BIT(NL80211_IFTYPE_STATION),
  62. },
  63. {
  64. .max = 1,
  65. .types = BIT(NL80211_IFTYPE_AP),
  66. },
  67. };
  68. static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
  69. {
  70. .max = 2,
  71. .types = BIT(NL80211_IFTYPE_STATION),
  72. },
  73. };
  74. static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
  75. {
  76. .max = 1,
  77. .types = BIT(NL80211_IFTYPE_STATION),
  78. },
  79. {
  80. .max = 1,
  81. .types = BIT(NL80211_IFTYPE_P2P_GO) |
  82. BIT(NL80211_IFTYPE_AP),
  83. },
  84. };
  85. static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
  86. {
  87. .max = 2,
  88. .types = BIT(NL80211_IFTYPE_STATION),
  89. },
  90. {
  91. .max = 1,
  92. .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
  93. },
  94. };
  95. static const struct ieee80211_iface_combination
  96. iwlagn_iface_combinations_dualmode[] = {
  97. { .num_different_channels = 1,
  98. .max_interfaces = 2,
  99. .beacon_int_infra_match = true,
  100. .limits = iwlagn_sta_ap_limits,
  101. .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
  102. },
  103. { .num_different_channels = 1,
  104. .max_interfaces = 2,
  105. .limits = iwlagn_2sta_limits,
  106. .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
  107. },
  108. };
  109. static const struct ieee80211_iface_combination
  110. iwlagn_iface_combinations_p2p[] = {
  111. { .num_different_channels = 1,
  112. .max_interfaces = 2,
  113. .beacon_int_infra_match = true,
  114. .limits = iwlagn_p2p_sta_go_limits,
  115. .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
  116. },
  117. { .num_different_channels = 1,
  118. .max_interfaces = 2,
  119. .limits = iwlagn_p2p_2sta_limits,
  120. .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
  121. },
  122. };
  123. /*
  124. * Not a mac80211 entry point function, but it fits in with all the
  125. * other mac80211 functions grouped here.
  126. */
  127. int iwlagn_mac_setup_register(struct iwl_priv *priv,
  128. struct iwl_ucode_capabilities *capa)
  129. {
  130. int ret;
  131. struct ieee80211_hw *hw = priv->hw;
  132. struct iwl_rxon_context *ctx;
  133. hw->rate_control_algorithm = "iwl-agn-rs";
  134. /* Tell mac80211 our characteristics */
  135. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  136. IEEE80211_HW_AMPDU_AGGREGATION |
  137. IEEE80211_HW_NEED_DTIM_PERIOD |
  138. IEEE80211_HW_SPECTRUM_MGMT |
  139. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  140. /*
  141. * Including the following line will crash some AP's. This
  142. * workaround removes the stimulus which causes the crash until
  143. * the AP software can be fixed.
  144. hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  145. */
  146. hw->flags |= IEEE80211_HW_SUPPORTS_PS |
  147. IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
  148. if (hw_params(priv).sku & EEPROM_SKU_CAP_11N_ENABLE)
  149. hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  150. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  151. if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
  152. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  153. hw->sta_data_size = sizeof(struct iwl_station_priv);
  154. hw->vif_data_size = sizeof(struct iwl_vif_priv);
  155. for_each_context(priv, ctx) {
  156. hw->wiphy->interface_modes |= ctx->interface_modes;
  157. hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
  158. }
  159. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  160. if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
  161. hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
  162. hw->wiphy->n_iface_combinations =
  163. ARRAY_SIZE(iwlagn_iface_combinations_p2p);
  164. } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
  165. hw->wiphy->iface_combinations =
  166. iwlagn_iface_combinations_dualmode;
  167. hw->wiphy->n_iface_combinations =
  168. ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
  169. }
  170. hw->wiphy->max_remain_on_channel_duration = 1000;
  171. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  172. WIPHY_FLAG_DISABLE_BEACON_HINTS |
  173. WIPHY_FLAG_IBSS_RSN;
  174. if (nic(priv)->fw.ucode_wowlan.code.len &&
  175. trans(priv)->ops->wowlan_suspend &&
  176. device_can_wakeup(trans(priv)->dev)) {
  177. hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  178. WIPHY_WOWLAN_DISCONNECT |
  179. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  180. WIPHY_WOWLAN_RFKILL_RELEASE;
  181. if (!iwlagn_mod_params.sw_crypto)
  182. hw->wiphy->wowlan.flags |=
  183. WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  184. WIPHY_WOWLAN_GTK_REKEY_FAILURE;
  185. hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
  186. hw->wiphy->wowlan.pattern_min_len =
  187. IWLAGN_WOWLAN_MIN_PATTERN_LEN;
  188. hw->wiphy->wowlan.pattern_max_len =
  189. IWLAGN_WOWLAN_MAX_PATTERN_LEN;
  190. }
  191. if (iwlagn_mod_params.power_save)
  192. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  193. else
  194. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  195. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  196. /* we create the 802.11 header and a zero-length SSID element */
  197. hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 2;
  198. /* Default value; 4 EDCA QOS priorities */
  199. hw->queues = 4;
  200. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  201. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  202. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  203. &priv->bands[IEEE80211_BAND_2GHZ];
  204. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  205. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  206. &priv->bands[IEEE80211_BAND_5GHZ];
  207. hw->wiphy->hw_version = trans(priv)->hw_id;
  208. iwl_leds_init(priv);
  209. ret = ieee80211_register_hw(priv->hw);
  210. if (ret) {
  211. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  212. return ret;
  213. }
  214. priv->mac80211_registered = 1;
  215. return 0;
  216. }
  217. void iwlagn_mac_unregister(struct iwl_priv *priv)
  218. {
  219. if (!priv->mac80211_registered)
  220. return;
  221. iwl_leds_exit(priv);
  222. ieee80211_unregister_hw(priv->hw);
  223. priv->mac80211_registered = 0;
  224. }
  225. static int __iwl_up(struct iwl_priv *priv)
  226. {
  227. struct iwl_rxon_context *ctx;
  228. int ret;
  229. lockdep_assert_held(&priv->shrd->mutex);
  230. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  231. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  232. return -EIO;
  233. }
  234. for_each_context(priv, ctx) {
  235. ret = iwlagn_alloc_bcast_station(priv, ctx);
  236. if (ret) {
  237. iwl_dealloc_bcast_stations(priv);
  238. return ret;
  239. }
  240. }
  241. ret = iwl_run_init_ucode(trans(priv));
  242. if (ret) {
  243. IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
  244. goto error;
  245. }
  246. ret = iwl_load_ucode_wait_alive(trans(priv), IWL_UCODE_REGULAR);
  247. if (ret) {
  248. IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
  249. goto error;
  250. }
  251. ret = iwl_alive_start(priv);
  252. if (ret)
  253. goto error;
  254. return 0;
  255. error:
  256. set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  257. iwl_down(priv);
  258. clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  259. IWL_ERR(priv, "Unable to initialize device.\n");
  260. return ret;
  261. }
  262. static int iwlagn_mac_start(struct ieee80211_hw *hw)
  263. {
  264. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  265. int ret;
  266. IWL_DEBUG_MAC80211(priv, "enter\n");
  267. /* we should be verifying the device is ready to be opened */
  268. mutex_lock(&priv->shrd->mutex);
  269. ret = __iwl_up(priv);
  270. mutex_unlock(&priv->shrd->mutex);
  271. if (ret)
  272. return ret;
  273. IWL_DEBUG_INFO(priv, "Start UP work done.\n");
  274. /* Now we should be done, and the READY bit should be set. */
  275. if (WARN_ON(!test_bit(STATUS_READY, &priv->shrd->status)))
  276. ret = -EIO;
  277. iwlagn_led_enable(priv);
  278. priv->is_open = 1;
  279. IWL_DEBUG_MAC80211(priv, "leave\n");
  280. return 0;
  281. }
  282. static void iwlagn_mac_stop(struct ieee80211_hw *hw)
  283. {
  284. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  285. IWL_DEBUG_MAC80211(priv, "enter\n");
  286. if (!priv->is_open)
  287. return;
  288. priv->is_open = 0;
  289. mutex_lock(&priv->shrd->mutex);
  290. iwl_down(priv);
  291. mutex_unlock(&priv->shrd->mutex);
  292. iwl_cancel_deferred_work(priv);
  293. flush_workqueue(priv->workqueue);
  294. /* User space software may expect getting rfkill changes
  295. * even if interface is down, trans->down will leave the RF
  296. * kill interrupt enabled
  297. */
  298. iwl_trans_stop_hw(trans(priv));
  299. IWL_DEBUG_MAC80211(priv, "leave\n");
  300. }
  301. static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
  302. struct ieee80211_vif *vif,
  303. struct cfg80211_gtk_rekey_data *data)
  304. {
  305. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  306. if (iwlagn_mod_params.sw_crypto)
  307. return;
  308. IWL_DEBUG_MAC80211(priv, "enter\n");
  309. mutex_lock(&priv->shrd->mutex);
  310. if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
  311. goto out;
  312. memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
  313. memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
  314. priv->replay_ctr =
  315. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  316. priv->have_rekey_data = true;
  317. out:
  318. mutex_unlock(&priv->shrd->mutex);
  319. IWL_DEBUG_MAC80211(priv, "leave\n");
  320. }
  321. #ifdef CONFIG_PM_SLEEP
  322. static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
  323. struct cfg80211_wowlan *wowlan)
  324. {
  325. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  326. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  327. int ret;
  328. if (WARN_ON(!wowlan))
  329. return -EINVAL;
  330. IWL_DEBUG_MAC80211(priv, "enter\n");
  331. mutex_lock(&priv->shrd->mutex);
  332. /* Don't attempt WoWLAN when not associated, tear down instead. */
  333. if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
  334. !iwl_is_associated_ctx(ctx)) {
  335. ret = 1;
  336. goto out;
  337. }
  338. ret = iwlagn_suspend(priv, hw, wowlan);
  339. if (ret)
  340. goto error;
  341. device_set_wakeup_enable(trans(priv)->dev, true);
  342. iwl_trans_wowlan_suspend(trans(priv));
  343. goto out;
  344. error:
  345. priv->wowlan = false;
  346. iwlagn_prepare_restart(priv);
  347. ieee80211_restart_hw(priv->hw);
  348. out:
  349. mutex_unlock(&priv->shrd->mutex);
  350. IWL_DEBUG_MAC80211(priv, "leave\n");
  351. return ret;
  352. }
  353. static int iwlagn_mac_resume(struct ieee80211_hw *hw)
  354. {
  355. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  356. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  357. struct ieee80211_vif *vif;
  358. unsigned long flags;
  359. u32 base, status = 0xffffffff;
  360. int ret = -EIO;
  361. IWL_DEBUG_MAC80211(priv, "enter\n");
  362. mutex_lock(&priv->shrd->mutex);
  363. iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_CLR,
  364. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  365. base = priv->shrd->device_pointers.error_event_table;
  366. if (iwlagn_hw_valid_rtc_data_addr(base)) {
  367. spin_lock_irqsave(&trans(priv)->reg_lock, flags);
  368. ret = iwl_grab_nic_access_silent(trans(priv));
  369. if (ret == 0) {
  370. iwl_write32(trans(priv), HBUS_TARG_MEM_RADDR, base);
  371. status = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
  372. iwl_release_nic_access(trans(priv));
  373. }
  374. spin_unlock_irqrestore(&trans(priv)->reg_lock, flags);
  375. #ifdef CONFIG_IWLWIFI_DEBUGFS
  376. if (ret == 0) {
  377. struct iwl_nic *nic = nic(priv);
  378. if (!priv->wowlan_sram)
  379. priv->wowlan_sram =
  380. kzalloc(nic->fw.ucode_wowlan.data.len,
  381. GFP_KERNEL);
  382. if (priv->wowlan_sram)
  383. _iwl_read_targ_mem_words(
  384. trans(priv), 0x800000,
  385. priv->wowlan_sram,
  386. nic->fw.ucode_wowlan.data.len / 4);
  387. }
  388. #endif
  389. }
  390. /* we'll clear ctx->vif during iwlagn_prepare_restart() */
  391. vif = ctx->vif;
  392. priv->wowlan = false;
  393. device_set_wakeup_enable(trans(priv)->dev, false);
  394. iwlagn_prepare_restart(priv);
  395. memset((void *)&ctx->active, 0, sizeof(ctx->active));
  396. iwl_connection_init_rx_config(priv, ctx);
  397. iwlagn_set_rxon_chain(priv, ctx);
  398. mutex_unlock(&priv->shrd->mutex);
  399. IWL_DEBUG_MAC80211(priv, "leave\n");
  400. ieee80211_resume_disconnect(vif);
  401. return 1;
  402. }
  403. #endif
  404. static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  405. {
  406. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  407. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  408. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  409. if (iwlagn_tx_skb(priv, skb))
  410. dev_kfree_skb_any(skb);
  411. }
  412. static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
  413. struct ieee80211_vif *vif,
  414. struct ieee80211_key_conf *keyconf,
  415. struct ieee80211_sta *sta,
  416. u32 iv32, u16 *phase1key)
  417. {
  418. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  419. iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
  420. }
  421. static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  422. struct ieee80211_vif *vif,
  423. struct ieee80211_sta *sta,
  424. struct ieee80211_key_conf *key)
  425. {
  426. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  427. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  428. struct iwl_rxon_context *ctx = vif_priv->ctx;
  429. int ret;
  430. bool is_default_wep_key = false;
  431. IWL_DEBUG_MAC80211(priv, "enter\n");
  432. if (iwlagn_mod_params.sw_crypto) {
  433. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  434. return -EOPNOTSUPP;
  435. }
  436. switch (key->cipher) {
  437. case WLAN_CIPHER_SUITE_TKIP:
  438. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  439. /* fall through */
  440. case WLAN_CIPHER_SUITE_CCMP:
  441. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  442. break;
  443. default:
  444. break;
  445. }
  446. /*
  447. * We could program these keys into the hardware as well, but we
  448. * don't expect much multicast traffic in IBSS and having keys
  449. * for more stations is probably more useful.
  450. *
  451. * Mark key TX-only and return 0.
  452. */
  453. if (vif->type == NL80211_IFTYPE_ADHOC &&
  454. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  455. key->hw_key_idx = WEP_INVALID_OFFSET;
  456. return 0;
  457. }
  458. /* If they key was TX-only, accept deletion */
  459. if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
  460. return 0;
  461. mutex_lock(&priv->shrd->mutex);
  462. iwl_scan_cancel_timeout(priv, 100);
  463. BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
  464. /*
  465. * If we are getting WEP group key and we didn't receive any key mapping
  466. * so far, we are in legacy wep mode (group key only), otherwise we are
  467. * in 1X mode.
  468. * In legacy wep mode, we use another host command to the uCode.
  469. */
  470. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  471. key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
  472. if (cmd == SET_KEY)
  473. is_default_wep_key = !ctx->key_mapping_keys;
  474. else
  475. is_default_wep_key =
  476. key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
  477. }
  478. switch (cmd) {
  479. case SET_KEY:
  480. if (is_default_wep_key) {
  481. ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
  482. break;
  483. }
  484. ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
  485. if (ret) {
  486. /*
  487. * can't add key for RX, but we don't need it
  488. * in the device for TX so still return 0
  489. */
  490. ret = 0;
  491. key->hw_key_idx = WEP_INVALID_OFFSET;
  492. }
  493. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  494. break;
  495. case DISABLE_KEY:
  496. if (is_default_wep_key)
  497. ret = iwl_remove_default_wep_key(priv, ctx, key);
  498. else
  499. ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
  500. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  501. break;
  502. default:
  503. ret = -EINVAL;
  504. }
  505. mutex_unlock(&priv->shrd->mutex);
  506. IWL_DEBUG_MAC80211(priv, "leave\n");
  507. return ret;
  508. }
  509. static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
  510. struct ieee80211_vif *vif,
  511. enum ieee80211_ampdu_mlme_action action,
  512. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  513. u8 buf_size)
  514. {
  515. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  516. int ret = -EINVAL;
  517. struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
  518. IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
  519. sta->addr, tid);
  520. if (!(hw_params(priv).sku & EEPROM_SKU_CAP_11N_ENABLE))
  521. return -EACCES;
  522. IWL_DEBUG_MAC80211(priv, "enter\n");
  523. mutex_lock(&priv->shrd->mutex);
  524. switch (action) {
  525. case IEEE80211_AMPDU_RX_START:
  526. if (iwlagn_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
  527. break;
  528. IWL_DEBUG_HT(priv, "start Rx\n");
  529. ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
  530. break;
  531. case IEEE80211_AMPDU_RX_STOP:
  532. IWL_DEBUG_HT(priv, "stop Rx\n");
  533. ret = iwl_sta_rx_agg_stop(priv, sta, tid);
  534. break;
  535. case IEEE80211_AMPDU_TX_START:
  536. if (iwlagn_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
  537. break;
  538. IWL_DEBUG_HT(priv, "start Tx\n");
  539. ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
  540. break;
  541. case IEEE80211_AMPDU_TX_STOP:
  542. IWL_DEBUG_HT(priv, "stop Tx\n");
  543. ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
  544. if ((ret == 0) && (priv->agg_tids_count > 0)) {
  545. priv->agg_tids_count--;
  546. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  547. priv->agg_tids_count);
  548. }
  549. if (!priv->agg_tids_count && cfg(priv)->ht_params &&
  550. cfg(priv)->ht_params->use_rts_for_aggregation) {
  551. /*
  552. * switch off RTS/CTS if it was previously enabled
  553. */
  554. sta_priv->lq_sta.lq.general_params.flags &=
  555. ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  556. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  557. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  558. }
  559. break;
  560. case IEEE80211_AMPDU_TX_OPERATIONAL:
  561. ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
  562. break;
  563. }
  564. mutex_unlock(&priv->shrd->mutex);
  565. IWL_DEBUG_MAC80211(priv, "leave\n");
  566. return ret;
  567. }
  568. static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
  569. struct ieee80211_vif *vif,
  570. struct ieee80211_sta *sta)
  571. {
  572. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  573. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  574. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  575. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  576. int ret;
  577. u8 sta_id;
  578. IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
  579. sta->addr);
  580. sta_priv->sta_id = IWL_INVALID_STATION;
  581. atomic_set(&sta_priv->pending_frames, 0);
  582. if (vif->type == NL80211_IFTYPE_AP)
  583. sta_priv->client = true;
  584. ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
  585. is_ap, sta, &sta_id);
  586. if (ret) {
  587. IWL_ERR(priv, "Unable to add station %pM (%d)\n",
  588. sta->addr, ret);
  589. /* Should we return success if return code is EEXIST ? */
  590. return ret;
  591. }
  592. sta_priv->sta_id = sta_id;
  593. return 0;
  594. }
  595. static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
  596. struct ieee80211_vif *vif,
  597. struct ieee80211_sta *sta)
  598. {
  599. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  600. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  601. int ret;
  602. IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
  603. sta->addr);
  604. ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
  605. if (ret)
  606. IWL_DEBUG_QUIET_RFKILL(priv, "Error removing station %pM\n",
  607. sta->addr);
  608. return ret;
  609. }
  610. static int iwlagn_mac_sta_state(struct ieee80211_hw *hw,
  611. struct ieee80211_vif *vif,
  612. struct ieee80211_sta *sta,
  613. enum ieee80211_sta_state old_state,
  614. enum ieee80211_sta_state new_state)
  615. {
  616. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  617. enum {
  618. NONE, ADD, REMOVE, RATE_INIT, ADD_RATE_INIT,
  619. } op = NONE;
  620. int ret;
  621. IWL_DEBUG_MAC80211(priv, "station %pM state change %d->%d\n",
  622. sta->addr, old_state, new_state);
  623. mutex_lock(&priv->shrd->mutex);
  624. if (vif->type == NL80211_IFTYPE_STATION) {
  625. if (old_state == IEEE80211_STA_NOTEXIST &&
  626. new_state == IEEE80211_STA_NONE)
  627. op = ADD;
  628. else if (old_state == IEEE80211_STA_NONE &&
  629. new_state == IEEE80211_STA_NOTEXIST)
  630. op = REMOVE;
  631. else if (old_state == IEEE80211_STA_AUTH &&
  632. new_state == IEEE80211_STA_ASSOC)
  633. op = RATE_INIT;
  634. } else {
  635. if (old_state == IEEE80211_STA_AUTH &&
  636. new_state == IEEE80211_STA_ASSOC)
  637. op = ADD_RATE_INIT;
  638. else if (old_state == IEEE80211_STA_ASSOC &&
  639. new_state == IEEE80211_STA_AUTH)
  640. op = REMOVE;
  641. }
  642. switch (op) {
  643. case ADD:
  644. ret = iwlagn_mac_sta_add(hw, vif, sta);
  645. break;
  646. case REMOVE:
  647. ret = iwlagn_mac_sta_remove(hw, vif, sta);
  648. break;
  649. case ADD_RATE_INIT:
  650. ret = iwlagn_mac_sta_add(hw, vif, sta);
  651. if (ret)
  652. break;
  653. /* fall through */
  654. case RATE_INIT:
  655. /* Initialize rate scaling */
  656. IWL_DEBUG_INFO(priv,
  657. "Initializing rate scaling for station %pM\n",
  658. sta->addr);
  659. iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
  660. ret = 0;
  661. break;
  662. default:
  663. ret = 0;
  664. break;
  665. }
  666. /*
  667. * mac80211 might WARN if we fail, but due the way we
  668. * (badly) handle hard rfkill, we might fail here
  669. */
  670. if (iwl_is_rfkill(priv->shrd))
  671. ret = 0;
  672. mutex_unlock(&priv->shrd->mutex);
  673. IWL_DEBUG_MAC80211(priv, "leave\n");
  674. return ret;
  675. }
  676. static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
  677. struct ieee80211_channel_switch *ch_switch)
  678. {
  679. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  680. const struct iwl_channel_info *ch_info;
  681. struct ieee80211_conf *conf = &hw->conf;
  682. struct ieee80211_channel *channel = ch_switch->channel;
  683. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  684. /*
  685. * MULTI-FIXME
  686. * When we add support for multiple interfaces, we need to
  687. * revisit this. The channel switch command in the device
  688. * only affects the BSS context, but what does that really
  689. * mean? And what if we get a CSA on the second interface?
  690. * This needs a lot of work.
  691. */
  692. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  693. u16 ch;
  694. IWL_DEBUG_MAC80211(priv, "enter\n");
  695. mutex_lock(&priv->shrd->mutex);
  696. if (iwl_is_rfkill(priv->shrd))
  697. goto out;
  698. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
  699. test_bit(STATUS_SCANNING, &priv->shrd->status) ||
  700. test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
  701. goto out;
  702. if (!iwl_is_associated_ctx(ctx))
  703. goto out;
  704. if (!cfg(priv)->lib->set_channel_switch)
  705. goto out;
  706. ch = channel->hw_value;
  707. if (le16_to_cpu(ctx->active.channel) == ch)
  708. goto out;
  709. ch_info = iwl_get_channel_info(priv, channel->band, ch);
  710. if (!is_channel_valid(ch_info)) {
  711. IWL_DEBUG_MAC80211(priv, "invalid channel\n");
  712. goto out;
  713. }
  714. priv->current_ht_config.smps = conf->smps_mode;
  715. /* Configure HT40 channels */
  716. ctx->ht.enabled = conf_is_ht(conf);
  717. if (ctx->ht.enabled)
  718. iwlagn_config_ht40(conf, ctx);
  719. else
  720. ctx->ht.is_40mhz = false;
  721. if ((le16_to_cpu(ctx->staging.channel) != ch))
  722. ctx->staging.flags = 0;
  723. iwl_set_rxon_channel(priv, channel, ctx);
  724. iwl_set_rxon_ht(priv, ht_conf);
  725. iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
  726. iwl_set_rate(priv);
  727. /*
  728. * at this point, staging_rxon has the
  729. * configuration for channel switch
  730. */
  731. set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  732. priv->switch_channel = cpu_to_le16(ch);
  733. if (cfg(priv)->lib->set_channel_switch(priv, ch_switch)) {
  734. clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  735. priv->switch_channel = 0;
  736. ieee80211_chswitch_done(ctx->vif, false);
  737. }
  738. out:
  739. mutex_unlock(&priv->shrd->mutex);
  740. IWL_DEBUG_MAC80211(priv, "leave\n");
  741. }
  742. static void iwlagn_configure_filter(struct ieee80211_hw *hw,
  743. unsigned int changed_flags,
  744. unsigned int *total_flags,
  745. u64 multicast)
  746. {
  747. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  748. __le32 filter_or = 0, filter_nand = 0;
  749. struct iwl_rxon_context *ctx;
  750. #define CHK(test, flag) do { \
  751. if (*total_flags & (test)) \
  752. filter_or |= (flag); \
  753. else \
  754. filter_nand |= (flag); \
  755. } while (0)
  756. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  757. changed_flags, *total_flags);
  758. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  759. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  760. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  761. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  762. #undef CHK
  763. mutex_lock(&priv->shrd->mutex);
  764. for_each_context(priv, ctx) {
  765. ctx->staging.filter_flags &= ~filter_nand;
  766. ctx->staging.filter_flags |= filter_or;
  767. /*
  768. * Not committing directly because hardware can perform a scan,
  769. * but we'll eventually commit the filter flags change anyway.
  770. */
  771. }
  772. mutex_unlock(&priv->shrd->mutex);
  773. /*
  774. * Receiving all multicast frames is always enabled by the
  775. * default flags setup in iwl_connection_init_rx_config()
  776. * since we currently do not support programming multicast
  777. * filters into the device.
  778. */
  779. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  780. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  781. }
  782. static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
  783. {
  784. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  785. mutex_lock(&priv->shrd->mutex);
  786. IWL_DEBUG_MAC80211(priv, "enter\n");
  787. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  788. IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
  789. goto done;
  790. }
  791. if (iwl_is_rfkill(priv->shrd)) {
  792. IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
  793. goto done;
  794. }
  795. /*
  796. * mac80211 will not push any more frames for transmit
  797. * until the flush is completed
  798. */
  799. if (drop) {
  800. IWL_DEBUG_MAC80211(priv, "send flush command\n");
  801. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  802. IWL_ERR(priv, "flush request fail\n");
  803. goto done;
  804. }
  805. }
  806. IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
  807. iwl_trans_wait_tx_queue_empty(trans(priv));
  808. done:
  809. mutex_unlock(&priv->shrd->mutex);
  810. IWL_DEBUG_MAC80211(priv, "leave\n");
  811. }
  812. static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
  813. struct ieee80211_channel *channel,
  814. enum nl80211_channel_type channel_type,
  815. int duration)
  816. {
  817. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  818. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  819. int err = 0;
  820. if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  821. return -EOPNOTSUPP;
  822. if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
  823. return -EOPNOTSUPP;
  824. IWL_DEBUG_MAC80211(priv, "enter\n");
  825. mutex_lock(&priv->shrd->mutex);
  826. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  827. err = -EBUSY;
  828. goto out;
  829. }
  830. priv->hw_roc_channel = channel;
  831. priv->hw_roc_chantype = channel_type;
  832. /* convert from ms to TU */
  833. priv->hw_roc_duration = DIV_ROUND_UP(1000 * duration, 1024);
  834. priv->hw_roc_start_notified = false;
  835. cancel_delayed_work(&priv->hw_roc_disable_work);
  836. if (!ctx->is_active) {
  837. static const struct iwl_qos_info default_qos_data = {
  838. .def_qos_parm = {
  839. .ac[0] = {
  840. .cw_min = cpu_to_le16(3),
  841. .cw_max = cpu_to_le16(7),
  842. .aifsn = 2,
  843. .edca_txop = cpu_to_le16(1504),
  844. },
  845. .ac[1] = {
  846. .cw_min = cpu_to_le16(7),
  847. .cw_max = cpu_to_le16(15),
  848. .aifsn = 2,
  849. .edca_txop = cpu_to_le16(3008),
  850. },
  851. .ac[2] = {
  852. .cw_min = cpu_to_le16(15),
  853. .cw_max = cpu_to_le16(1023),
  854. .aifsn = 3,
  855. },
  856. .ac[3] = {
  857. .cw_min = cpu_to_le16(15),
  858. .cw_max = cpu_to_le16(1023),
  859. .aifsn = 7,
  860. },
  861. },
  862. };
  863. ctx->is_active = true;
  864. ctx->qos_data = default_qos_data;
  865. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  866. memcpy(ctx->staging.node_addr,
  867. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  868. ETH_ALEN);
  869. memcpy(ctx->staging.bssid_addr,
  870. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  871. ETH_ALEN);
  872. err = iwlagn_commit_rxon(priv, ctx);
  873. if (err)
  874. goto out;
  875. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
  876. RXON_FILTER_PROMISC_MSK |
  877. RXON_FILTER_CTL2HOST_MSK;
  878. err = iwlagn_commit_rxon(priv, ctx);
  879. if (err) {
  880. iwlagn_disable_roc(priv);
  881. goto out;
  882. }
  883. priv->hw_roc_setup = true;
  884. }
  885. err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
  886. if (err)
  887. iwlagn_disable_roc(priv);
  888. out:
  889. mutex_unlock(&priv->shrd->mutex);
  890. IWL_DEBUG_MAC80211(priv, "leave\n");
  891. return err;
  892. }
  893. static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
  894. {
  895. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  896. if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  897. return -EOPNOTSUPP;
  898. IWL_DEBUG_MAC80211(priv, "enter\n");
  899. mutex_lock(&priv->shrd->mutex);
  900. iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
  901. iwlagn_disable_roc(priv);
  902. mutex_unlock(&priv->shrd->mutex);
  903. IWL_DEBUG_MAC80211(priv, "leave\n");
  904. return 0;
  905. }
  906. static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
  907. enum ieee80211_rssi_event rssi_event)
  908. {
  909. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  910. IWL_DEBUG_MAC80211(priv, "enter\n");
  911. mutex_lock(&priv->shrd->mutex);
  912. if (cfg(priv)->bt_params &&
  913. cfg(priv)->bt_params->advanced_bt_coexist) {
  914. if (rssi_event == RSSI_EVENT_LOW)
  915. priv->bt_enable_pspoll = true;
  916. else if (rssi_event == RSSI_EVENT_HIGH)
  917. priv->bt_enable_pspoll = false;
  918. iwlagn_send_advance_bt_config(priv);
  919. } else {
  920. IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
  921. "ignoring RSSI callback\n");
  922. }
  923. mutex_unlock(&priv->shrd->mutex);
  924. IWL_DEBUG_MAC80211(priv, "leave\n");
  925. }
  926. static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
  927. struct ieee80211_sta *sta, bool set)
  928. {
  929. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  930. queue_work(priv->workqueue, &priv->beacon_update);
  931. return 0;
  932. }
  933. static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
  934. struct ieee80211_vif *vif, u16 queue,
  935. const struct ieee80211_tx_queue_params *params)
  936. {
  937. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  938. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  939. struct iwl_rxon_context *ctx = vif_priv->ctx;
  940. int q;
  941. if (WARN_ON(!ctx))
  942. return -EINVAL;
  943. IWL_DEBUG_MAC80211(priv, "enter\n");
  944. if (!iwl_is_ready_rf(priv->shrd)) {
  945. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  946. return -EIO;
  947. }
  948. if (queue >= AC_NUM) {
  949. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  950. return 0;
  951. }
  952. q = AC_NUM - 1 - queue;
  953. mutex_lock(&priv->shrd->mutex);
  954. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  955. cpu_to_le16(params->cw_min);
  956. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  957. cpu_to_le16(params->cw_max);
  958. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  959. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  960. cpu_to_le16((params->txop * 32));
  961. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  962. mutex_unlock(&priv->shrd->mutex);
  963. IWL_DEBUG_MAC80211(priv, "leave\n");
  964. return 0;
  965. }
  966. static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
  967. {
  968. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  969. return priv->ibss_manager == IWL_IBSS_MANAGER;
  970. }
  971. static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  972. {
  973. iwl_connection_init_rx_config(priv, ctx);
  974. iwlagn_set_rxon_chain(priv, ctx);
  975. return iwlagn_commit_rxon(priv, ctx);
  976. }
  977. static int iwl_setup_interface(struct iwl_priv *priv,
  978. struct iwl_rxon_context *ctx)
  979. {
  980. struct ieee80211_vif *vif = ctx->vif;
  981. int err;
  982. lockdep_assert_held(&priv->shrd->mutex);
  983. /*
  984. * This variable will be correct only when there's just
  985. * a single context, but all code using it is for hardware
  986. * that supports only one context.
  987. */
  988. priv->iw_mode = vif->type;
  989. ctx->is_active = true;
  990. err = iwl_set_mode(priv, ctx);
  991. if (err) {
  992. if (!ctx->always_active)
  993. ctx->is_active = false;
  994. return err;
  995. }
  996. if (cfg(priv)->bt_params && cfg(priv)->bt_params->advanced_bt_coexist &&
  997. vif->type == NL80211_IFTYPE_ADHOC) {
  998. /*
  999. * pretend to have high BT traffic as long as we
  1000. * are operating in IBSS mode, as this will cause
  1001. * the rate scaling etc. to behave as intended.
  1002. */
  1003. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1004. }
  1005. return 0;
  1006. }
  1007. static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
  1008. struct ieee80211_vif *vif)
  1009. {
  1010. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1011. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1012. struct iwl_rxon_context *tmp, *ctx = NULL;
  1013. int err;
  1014. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
  1015. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1016. viftype, vif->addr);
  1017. cancel_delayed_work_sync(&priv->hw_roc_disable_work);
  1018. mutex_lock(&priv->shrd->mutex);
  1019. iwlagn_disable_roc(priv);
  1020. if (!iwl_is_ready_rf(priv->shrd)) {
  1021. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1022. err = -EINVAL;
  1023. goto out;
  1024. }
  1025. for_each_context(priv, tmp) {
  1026. u32 possible_modes =
  1027. tmp->interface_modes | tmp->exclusive_interface_modes;
  1028. if (tmp->vif) {
  1029. /* check if this busy context is exclusive */
  1030. if (tmp->exclusive_interface_modes &
  1031. BIT(tmp->vif->type)) {
  1032. err = -EINVAL;
  1033. goto out;
  1034. }
  1035. continue;
  1036. }
  1037. if (!(possible_modes & BIT(viftype)))
  1038. continue;
  1039. /* have maybe usable context w/o interface */
  1040. ctx = tmp;
  1041. break;
  1042. }
  1043. if (!ctx) {
  1044. err = -EOPNOTSUPP;
  1045. goto out;
  1046. }
  1047. vif_priv->ctx = ctx;
  1048. ctx->vif = vif;
  1049. err = iwl_setup_interface(priv, ctx);
  1050. if (!err)
  1051. goto out;
  1052. ctx->vif = NULL;
  1053. priv->iw_mode = NL80211_IFTYPE_STATION;
  1054. out:
  1055. mutex_unlock(&priv->shrd->mutex);
  1056. IWL_DEBUG_MAC80211(priv, "leave\n");
  1057. return err;
  1058. }
  1059. static void iwl_teardown_interface(struct iwl_priv *priv,
  1060. struct ieee80211_vif *vif,
  1061. bool mode_change)
  1062. {
  1063. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1064. lockdep_assert_held(&priv->shrd->mutex);
  1065. if (priv->scan_vif == vif) {
  1066. iwl_scan_cancel_timeout(priv, 200);
  1067. iwl_force_scan_end(priv);
  1068. }
  1069. if (!mode_change) {
  1070. iwl_set_mode(priv, ctx);
  1071. if (!ctx->always_active)
  1072. ctx->is_active = false;
  1073. }
  1074. /*
  1075. * When removing the IBSS interface, overwrite the
  1076. * BT traffic load with the stored one from the last
  1077. * notification, if any. If this is a device that
  1078. * doesn't implement this, this has no effect since
  1079. * both values are the same and zero.
  1080. */
  1081. if (vif->type == NL80211_IFTYPE_ADHOC)
  1082. priv->bt_traffic_load = priv->last_bt_traffic_load;
  1083. }
  1084. static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
  1085. struct ieee80211_vif *vif)
  1086. {
  1087. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1088. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1089. IWL_DEBUG_MAC80211(priv, "enter\n");
  1090. mutex_lock(&priv->shrd->mutex);
  1091. if (WARN_ON(ctx->vif != vif)) {
  1092. struct iwl_rxon_context *tmp;
  1093. IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
  1094. for_each_context(priv, tmp)
  1095. IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
  1096. tmp->ctxid, tmp, tmp->vif);
  1097. }
  1098. ctx->vif = NULL;
  1099. iwl_teardown_interface(priv, vif, false);
  1100. mutex_unlock(&priv->shrd->mutex);
  1101. IWL_DEBUG_MAC80211(priv, "leave\n");
  1102. }
  1103. static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
  1104. struct ieee80211_vif *vif,
  1105. enum nl80211_iftype newtype, bool newp2p)
  1106. {
  1107. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1108. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1109. struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1110. struct iwl_rxon_context *tmp;
  1111. enum nl80211_iftype newviftype = newtype;
  1112. u32 interface_modes;
  1113. int err;
  1114. IWL_DEBUG_MAC80211(priv, "enter\n");
  1115. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1116. mutex_lock(&priv->shrd->mutex);
  1117. if (!ctx->vif || !iwl_is_ready_rf(priv->shrd)) {
  1118. /*
  1119. * Huh? But wait ... this can maybe happen when
  1120. * we're in the middle of a firmware restart!
  1121. */
  1122. err = -EBUSY;
  1123. goto out;
  1124. }
  1125. interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
  1126. if (!(interface_modes & BIT(newtype))) {
  1127. err = -EBUSY;
  1128. goto out;
  1129. }
  1130. /*
  1131. * Refuse a change that should be done by moving from the PAN
  1132. * context to the BSS context instead, if the BSS context is
  1133. * available and can support the new interface type.
  1134. */
  1135. if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
  1136. (bss_ctx->interface_modes & BIT(newtype) ||
  1137. bss_ctx->exclusive_interface_modes & BIT(newtype))) {
  1138. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  1139. err = -EBUSY;
  1140. goto out;
  1141. }
  1142. if (ctx->exclusive_interface_modes & BIT(newtype)) {
  1143. for_each_context(priv, tmp) {
  1144. if (ctx == tmp)
  1145. continue;
  1146. if (!tmp->vif)
  1147. continue;
  1148. /*
  1149. * The current mode switch would be exclusive, but
  1150. * another context is active ... refuse the switch.
  1151. */
  1152. err = -EBUSY;
  1153. goto out;
  1154. }
  1155. }
  1156. /* success */
  1157. iwl_teardown_interface(priv, vif, true);
  1158. vif->type = newviftype;
  1159. vif->p2p = newp2p;
  1160. err = iwl_setup_interface(priv, ctx);
  1161. WARN_ON(err);
  1162. /*
  1163. * We've switched internally, but submitting to the
  1164. * device may have failed for some reason. Mask this
  1165. * error, because otherwise mac80211 will not switch
  1166. * (and set the interface type back) and we'll be
  1167. * out of sync with it.
  1168. */
  1169. err = 0;
  1170. out:
  1171. mutex_unlock(&priv->shrd->mutex);
  1172. IWL_DEBUG_MAC80211(priv, "leave\n");
  1173. return err;
  1174. }
  1175. static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
  1176. struct ieee80211_vif *vif,
  1177. struct cfg80211_scan_request *req)
  1178. {
  1179. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1180. int ret;
  1181. IWL_DEBUG_MAC80211(priv, "enter\n");
  1182. if (req->n_channels == 0)
  1183. return -EINVAL;
  1184. mutex_lock(&priv->shrd->mutex);
  1185. /*
  1186. * If an internal scan is in progress, just set
  1187. * up the scan_request as per above.
  1188. */
  1189. if (priv->scan_type != IWL_SCAN_NORMAL) {
  1190. IWL_DEBUG_SCAN(priv,
  1191. "SCAN request during internal scan - defer\n");
  1192. priv->scan_request = req;
  1193. priv->scan_vif = vif;
  1194. ret = 0;
  1195. } else {
  1196. priv->scan_request = req;
  1197. priv->scan_vif = vif;
  1198. /*
  1199. * mac80211 will only ask for one band at a time
  1200. * so using channels[0] here is ok
  1201. */
  1202. ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
  1203. req->channels[0]->band);
  1204. if (ret) {
  1205. priv->scan_request = NULL;
  1206. priv->scan_vif = NULL;
  1207. }
  1208. }
  1209. IWL_DEBUG_MAC80211(priv, "leave\n");
  1210. mutex_unlock(&priv->shrd->mutex);
  1211. return ret;
  1212. }
  1213. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1214. {
  1215. struct iwl_addsta_cmd cmd = {
  1216. .mode = STA_CONTROL_MODIFY_MSK,
  1217. .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
  1218. .sta.sta_id = sta_id,
  1219. };
  1220. iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
  1221. }
  1222. static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
  1223. struct ieee80211_vif *vif,
  1224. enum sta_notify_cmd cmd,
  1225. struct ieee80211_sta *sta)
  1226. {
  1227. struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
  1228. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  1229. int sta_id;
  1230. IWL_DEBUG_MAC80211(priv, "enter\n");
  1231. switch (cmd) {
  1232. case STA_NOTIFY_SLEEP:
  1233. WARN_ON(!sta_priv->client);
  1234. sta_priv->asleep = true;
  1235. if (atomic_read(&sta_priv->pending_frames) > 0)
  1236. ieee80211_sta_block_awake(hw, sta, true);
  1237. break;
  1238. case STA_NOTIFY_AWAKE:
  1239. WARN_ON(!sta_priv->client);
  1240. if (!sta_priv->asleep)
  1241. break;
  1242. sta_priv->asleep = false;
  1243. sta_id = iwl_sta_id(sta);
  1244. if (sta_id != IWL_INVALID_STATION)
  1245. iwl_sta_modify_ps_wake(priv, sta_id);
  1246. break;
  1247. default:
  1248. break;
  1249. }
  1250. IWL_DEBUG_MAC80211(priv, "leave\n");
  1251. }
  1252. struct ieee80211_ops iwlagn_hw_ops = {
  1253. .tx = iwlagn_mac_tx,
  1254. .start = iwlagn_mac_start,
  1255. .stop = iwlagn_mac_stop,
  1256. #ifdef CONFIG_PM_SLEEP
  1257. .suspend = iwlagn_mac_suspend,
  1258. .resume = iwlagn_mac_resume,
  1259. #endif
  1260. .add_interface = iwlagn_mac_add_interface,
  1261. .remove_interface = iwlagn_mac_remove_interface,
  1262. .change_interface = iwlagn_mac_change_interface,
  1263. .config = iwlagn_mac_config,
  1264. .configure_filter = iwlagn_configure_filter,
  1265. .set_key = iwlagn_mac_set_key,
  1266. .update_tkip_key = iwlagn_mac_update_tkip_key,
  1267. .set_rekey_data = iwlagn_mac_set_rekey_data,
  1268. .conf_tx = iwlagn_mac_conf_tx,
  1269. .bss_info_changed = iwlagn_bss_info_changed,
  1270. .ampdu_action = iwlagn_mac_ampdu_action,
  1271. .hw_scan = iwlagn_mac_hw_scan,
  1272. .sta_notify = iwlagn_mac_sta_notify,
  1273. .sta_state = iwlagn_mac_sta_state,
  1274. .channel_switch = iwlagn_mac_channel_switch,
  1275. .flush = iwlagn_mac_flush,
  1276. .tx_last_beacon = iwlagn_mac_tx_last_beacon,
  1277. .remain_on_channel = iwlagn_mac_remain_on_channel,
  1278. .cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
  1279. .rssi_callback = iwlagn_mac_rssi_callback,
  1280. CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
  1281. CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
  1282. .set_tim = iwlagn_mac_set_tim,
  1283. };
  1284. /* This function both allocates and initializes hw and priv. */
  1285. struct ieee80211_hw *iwl_alloc_all(void)
  1286. {
  1287. struct iwl_priv *priv;
  1288. struct iwl_op_mode *op_mode;
  1289. /* mac80211 allocates memory for this device instance, including
  1290. * space for this driver's private structure */
  1291. struct ieee80211_hw *hw;
  1292. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
  1293. sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
  1294. if (!hw)
  1295. goto out;
  1296. op_mode = hw->priv;
  1297. priv = IWL_OP_MODE_GET_DVM(op_mode);
  1298. priv->hw = hw;
  1299. out:
  1300. return hw;
  1301. }