iwl-mac80211.c 41 KB

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