mac80211.c 43 KB

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