mac80211.c 43 KB

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