iwl-mac80211.c 42 KB

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