iwl-mac80211.c 41 KB

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