iwl-mac80211.c 41 KB

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