mac80211.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/kernel.h>
  64. #include <linux/slab.h>
  65. #include <linux/skbuff.h>
  66. #include <linux/netdevice.h>
  67. #include <linux/etherdevice.h>
  68. #include <linux/ip.h>
  69. #include <net/mac80211.h>
  70. #include <net/tcp.h>
  71. #include "iwl-op-mode.h"
  72. #include "iwl-io.h"
  73. #include "mvm.h"
  74. #include "sta.h"
  75. #include "time-event.h"
  76. #include "iwl-eeprom-parse.h"
  77. #include "fw-api-scan.h"
  78. #include "iwl-phy-db.h"
  79. static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
  80. {
  81. .max = 1,
  82. .types = BIT(NL80211_IFTYPE_STATION),
  83. },
  84. {
  85. .max = 1,
  86. .types = BIT(NL80211_IFTYPE_AP) |
  87. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  88. BIT(NL80211_IFTYPE_P2P_GO),
  89. },
  90. {
  91. .max = 1,
  92. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  93. },
  94. };
  95. static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
  96. {
  97. .num_different_channels = 1,
  98. .max_interfaces = 3,
  99. .limits = iwl_mvm_limits,
  100. .n_limits = ARRAY_SIZE(iwl_mvm_limits),
  101. },
  102. };
  103. #ifdef CONFIG_PM_SLEEP
  104. static const struct nl80211_wowlan_tcp_data_token_feature
  105. iwl_mvm_wowlan_tcp_token_feature = {
  106. .min_len = 0,
  107. .max_len = 255,
  108. .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
  109. };
  110. static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
  111. .tok = &iwl_mvm_wowlan_tcp_token_feature,
  112. .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
  113. sizeof(struct ethhdr) -
  114. sizeof(struct iphdr) -
  115. sizeof(struct tcphdr),
  116. .data_interval_max = 65535, /* __le16 in API */
  117. .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
  118. sizeof(struct ethhdr) -
  119. sizeof(struct iphdr) -
  120. sizeof(struct tcphdr),
  121. .seq = true,
  122. };
  123. #endif
  124. static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
  125. {
  126. int i;
  127. memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
  128. for (i = 0; i < NUM_PHY_CTX; i++) {
  129. mvm->phy_ctxts[i].id = i;
  130. mvm->phy_ctxts[i].ref = 0;
  131. }
  132. }
  133. int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
  134. {
  135. struct ieee80211_hw *hw = mvm->hw;
  136. int num_mac, ret, i;
  137. /* Tell mac80211 our characteristics */
  138. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  139. IEEE80211_HW_SPECTRUM_MGMT |
  140. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  141. IEEE80211_HW_QUEUE_CONTROL |
  142. IEEE80211_HW_WANT_MONITOR_VIF |
  143. IEEE80211_HW_SUPPORTS_PS |
  144. IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
  145. IEEE80211_HW_AMPDU_AGGREGATION |
  146. IEEE80211_HW_TIMING_BEACON_ONLY |
  147. IEEE80211_HW_CONNECTION_MONITOR |
  148. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  149. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  150. hw->queues = IWL_MVM_FIRST_AGG_QUEUE;
  151. hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
  152. hw->rate_control_algorithm = "iwl-mvm-rs";
  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 (mvm->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_mvm_sta);
  162. hw->vif_data_size = sizeof(struct iwl_mvm_vif);
  163. hw->chanctx_data_size = sizeof(u16);
  164. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  165. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  166. BIT(NL80211_IFTYPE_AP) |
  167. BIT(NL80211_IFTYPE_P2P_GO) |
  168. BIT(NL80211_IFTYPE_P2P_DEVICE);
  169. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  170. WIPHY_FLAG_DISABLE_BEACON_HINTS |
  171. WIPHY_FLAG_IBSS_RSN;
  172. hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
  173. hw->wiphy->n_iface_combinations =
  174. ARRAY_SIZE(iwl_mvm_iface_combinations);
  175. hw->wiphy->max_remain_on_channel_duration = 10000;
  176. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  177. /* Extract MAC address */
  178. memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
  179. hw->wiphy->addresses = mvm->addresses;
  180. hw->wiphy->n_addresses = 1;
  181. /* Extract additional MAC addresses if available */
  182. num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
  183. min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
  184. for (i = 1; i < num_mac; i++) {
  185. memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
  186. ETH_ALEN);
  187. mvm->addresses[i].addr[5]++;
  188. hw->wiphy->n_addresses++;
  189. }
  190. iwl_mvm_reset_phy_ctxts(mvm);
  191. /* we create the 802.11 header and a max-length SSID element */
  192. hw->wiphy->max_scan_ie_len =
  193. mvm->fw->ucode_capa.max_probe_length - 24 - 34;
  194. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  195. if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
  196. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  197. &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  198. if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
  199. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  200. &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  201. hw->wiphy->hw_version = mvm->trans->hw_id;
  202. if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
  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->features |= NL80211_FEATURE_P2P_GO_CTWIN |
  207. NL80211_FEATURE_P2P_GO_OPPPS;
  208. mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  209. #ifdef CONFIG_PM_SLEEP
  210. if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
  211. mvm->trans->ops->d3_suspend &&
  212. mvm->trans->ops->d3_resume &&
  213. device_can_wakeup(mvm->trans->dev)) {
  214. mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  215. WIPHY_WOWLAN_DISCONNECT |
  216. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  217. WIPHY_WOWLAN_RFKILL_RELEASE;
  218. if (!iwlwifi_mod_params.sw_crypto)
  219. mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  220. WIPHY_WOWLAN_GTK_REKEY_FAILURE |
  221. WIPHY_WOWLAN_4WAY_HANDSHAKE;
  222. mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
  223. mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
  224. mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
  225. mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
  226. hw->wiphy->wowlan = &mvm->wowlan;
  227. }
  228. #endif
  229. ret = iwl_mvm_leds_init(mvm);
  230. if (ret)
  231. return ret;
  232. ret = ieee80211_register_hw(mvm->hw);
  233. if (ret)
  234. iwl_mvm_leds_exit(mvm);
  235. return ret;
  236. }
  237. static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
  238. struct ieee80211_tx_control *control,
  239. struct sk_buff *skb)
  240. {
  241. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  242. if (iwl_mvm_is_radio_killed(mvm)) {
  243. IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
  244. goto drop;
  245. }
  246. if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
  247. !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
  248. goto drop;
  249. if (control->sta) {
  250. if (iwl_mvm_tx_skb(mvm, skb, control->sta))
  251. goto drop;
  252. return;
  253. }
  254. if (iwl_mvm_tx_skb_non_sta(mvm, skb))
  255. goto drop;
  256. return;
  257. drop:
  258. ieee80211_free_txskb(hw, skb);
  259. }
  260. static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
  261. struct ieee80211_vif *vif,
  262. enum ieee80211_ampdu_mlme_action action,
  263. struct ieee80211_sta *sta, u16 tid,
  264. u16 *ssn, u8 buf_size)
  265. {
  266. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  267. int ret;
  268. IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
  269. sta->addr, tid, action);
  270. if (!(mvm->nvm_data->sku_cap_11n_enable))
  271. return -EACCES;
  272. mutex_lock(&mvm->mutex);
  273. switch (action) {
  274. case IEEE80211_AMPDU_RX_START:
  275. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG) {
  276. ret = -EINVAL;
  277. break;
  278. }
  279. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
  280. break;
  281. case IEEE80211_AMPDU_RX_STOP:
  282. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
  283. break;
  284. case IEEE80211_AMPDU_TX_START:
  285. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG) {
  286. ret = -EINVAL;
  287. break;
  288. }
  289. ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
  290. break;
  291. case IEEE80211_AMPDU_TX_STOP_CONT:
  292. ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
  293. break;
  294. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  295. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  296. ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
  297. break;
  298. case IEEE80211_AMPDU_TX_OPERATIONAL:
  299. ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
  300. break;
  301. default:
  302. WARN_ON_ONCE(1);
  303. ret = -EINVAL;
  304. break;
  305. }
  306. mutex_unlock(&mvm->mutex);
  307. return ret;
  308. }
  309. static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
  310. struct ieee80211_vif *vif)
  311. {
  312. struct iwl_mvm *mvm = data;
  313. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  314. mvmvif->uploaded = false;
  315. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  316. /* does this make sense at all? */
  317. mvmvif->color++;
  318. spin_lock_bh(&mvm->time_event_lock);
  319. iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
  320. spin_unlock_bh(&mvm->time_event_lock);
  321. mvmvif->phy_ctxt = NULL;
  322. }
  323. static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
  324. {
  325. iwl_trans_stop_device(mvm->trans);
  326. iwl_trans_stop_hw(mvm->trans, false);
  327. mvm->scan_status = IWL_MVM_SCAN_NONE;
  328. /* just in case one was running */
  329. ieee80211_remain_on_channel_expired(mvm->hw);
  330. ieee80211_iterate_active_interfaces_atomic(
  331. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  332. iwl_mvm_cleanup_iterator, mvm);
  333. mvm->p2p_device_vif = NULL;
  334. iwl_mvm_reset_phy_ctxts(mvm);
  335. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  336. memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
  337. ieee80211_wake_queues(mvm->hw);
  338. mvm->vif_count = 0;
  339. mvm->rx_ba_sessions = 0;
  340. }
  341. static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
  342. {
  343. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  344. int ret;
  345. mutex_lock(&mvm->mutex);
  346. /* Clean up some internal and mac80211 state on restart */
  347. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  348. iwl_mvm_restart_cleanup(mvm);
  349. ret = iwl_mvm_up(mvm);
  350. mutex_unlock(&mvm->mutex);
  351. return ret;
  352. }
  353. static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
  354. {
  355. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  356. int ret;
  357. mutex_lock(&mvm->mutex);
  358. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  359. ret = iwl_mvm_update_quotas(mvm, NULL);
  360. if (ret)
  361. IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
  362. ret);
  363. mutex_unlock(&mvm->mutex);
  364. }
  365. static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
  366. {
  367. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  368. flush_work(&mvm->async_handlers_wk);
  369. mutex_lock(&mvm->mutex);
  370. /* async_handlers_wk is now blocked */
  371. /*
  372. * The work item could be running or queued if the
  373. * ROC time event stops just as we get here.
  374. */
  375. cancel_work_sync(&mvm->roc_done_wk);
  376. iwl_trans_stop_device(mvm->trans);
  377. iwl_trans_stop_hw(mvm->trans, false);
  378. iwl_mvm_async_handlers_purge(mvm);
  379. /* async_handlers_list is empty and will stay empty: HW is stopped */
  380. /* the fw is stopped, the aux sta is dead: clean up driver state */
  381. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  382. mutex_unlock(&mvm->mutex);
  383. /*
  384. * The worker might have been waiting for the mutex, let it run and
  385. * discover that its list is now empty.
  386. */
  387. cancel_work_sync(&mvm->async_handlers_wk);
  388. }
  389. static void iwl_mvm_pm_disable_iterator(void *data, u8 *mac,
  390. struct ieee80211_vif *vif)
  391. {
  392. struct iwl_mvm *mvm = data;
  393. int ret;
  394. ret = iwl_mvm_power_disable(mvm, vif);
  395. if (ret)
  396. IWL_ERR(mvm, "failed to disable power management\n");
  397. }
  398. static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
  399. struct ieee80211_vif *vif)
  400. {
  401. struct iwl_mvm *mvm = data;
  402. iwl_mvm_power_update_mode(mvm, vif);
  403. }
  404. static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
  405. {
  406. u16 i;
  407. lockdep_assert_held(&mvm->mutex);
  408. for (i = 0; i < NUM_PHY_CTX; i++)
  409. if (!mvm->phy_ctxts[i].ref)
  410. return &mvm->phy_ctxts[i];
  411. IWL_ERR(mvm, "No available PHY context\n");
  412. return NULL;
  413. }
  414. static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
  415. struct ieee80211_vif *vif)
  416. {
  417. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  418. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  419. int ret;
  420. /*
  421. * Not much to do here. The stack will not allow interface
  422. * types or combinations that we didn't advertise, so we
  423. * don't really have to check the types.
  424. */
  425. mutex_lock(&mvm->mutex);
  426. /* Allocate resources for the MAC context, and add it to the fw */
  427. ret = iwl_mvm_mac_ctxt_init(mvm, vif);
  428. if (ret)
  429. goto out_unlock;
  430. /*
  431. * TODO: remove this temporary code.
  432. * Currently MVM FW supports power management only on single MAC.
  433. * If new interface added, disable PM on existing interface.
  434. * P2P device is a special case, since it is handled by FW similary to
  435. * scan. If P2P deviced is added, PM remains enabled on existing
  436. * interface.
  437. * Note: the method below does not count the new interface being added
  438. * at this moment.
  439. */
  440. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  441. mvm->vif_count++;
  442. if (mvm->vif_count > 1) {
  443. IWL_DEBUG_MAC80211(mvm,
  444. "Disable power on existing interfaces\n");
  445. ieee80211_iterate_active_interfaces_atomic(
  446. mvm->hw,
  447. IEEE80211_IFACE_ITER_NORMAL,
  448. iwl_mvm_pm_disable_iterator, mvm);
  449. }
  450. /*
  451. * The AP binding flow can be done only after the beacon
  452. * template is configured (which happens only in the mac80211
  453. * start_ap() flow), and adding the broadcast station can happen
  454. * only after the binding.
  455. * In addition, since modifying the MAC before adding a bcast
  456. * station is not allowed by the FW, delay the adding of MAC context to
  457. * the point where we can also add the bcast station.
  458. * In short: there's not much we can do at this point, other than
  459. * allocating resources :)
  460. */
  461. if (vif->type == NL80211_IFTYPE_AP) {
  462. u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
  463. ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
  464. qmask);
  465. if (ret) {
  466. IWL_ERR(mvm, "Failed to allocate bcast sta\n");
  467. goto out_release;
  468. }
  469. iwl_mvm_vif_dbgfs_register(mvm, vif);
  470. goto out_unlock;
  471. }
  472. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  473. if (ret)
  474. goto out_release;
  475. /*
  476. * Update power state on the new interface. Admittedly, based on
  477. * mac80211 logics this power update will disable power management
  478. */
  479. iwl_mvm_power_update_mode(mvm, vif);
  480. /* beacon filtering */
  481. ret = iwl_mvm_disable_beacon_filter(mvm, vif);
  482. if (ret)
  483. goto out_remove_mac;
  484. if (!mvm->bf_allowed_vif &&
  485. vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
  486. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
  487. mvm->bf_allowed_vif = mvmvif;
  488. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
  489. }
  490. /*
  491. * P2P_DEVICE interface does not have a channel context assigned to it,
  492. * so a dedicated PHY context is allocated to it and the corresponding
  493. * MAC context is bound to it at this stage.
  494. */
  495. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  496. mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  497. if (!mvmvif->phy_ctxt) {
  498. ret = -ENOSPC;
  499. goto out_free_bf;
  500. }
  501. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  502. ret = iwl_mvm_binding_add_vif(mvm, vif);
  503. if (ret)
  504. goto out_unref_phy;
  505. ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
  506. if (ret)
  507. goto out_unbind;
  508. /* Save a pointer to p2p device vif, so it can later be used to
  509. * update the p2p device MAC when a GO is started/stopped */
  510. mvm->p2p_device_vif = vif;
  511. }
  512. iwl_mvm_vif_dbgfs_register(mvm, vif);
  513. goto out_unlock;
  514. out_unbind:
  515. iwl_mvm_binding_remove_vif(mvm, vif);
  516. out_unref_phy:
  517. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  518. out_free_bf:
  519. if (mvm->bf_allowed_vif == mvmvif) {
  520. mvm->bf_allowed_vif = NULL;
  521. vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
  522. }
  523. out_remove_mac:
  524. mvmvif->phy_ctxt = NULL;
  525. iwl_mvm_mac_ctxt_remove(mvm, vif);
  526. out_release:
  527. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  528. mvm->vif_count--;
  529. ieee80211_iterate_active_interfaces(
  530. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  531. iwl_mvm_power_update_iterator, mvm);
  532. iwl_mvm_mac_ctxt_release(mvm, vif);
  533. out_unlock:
  534. mutex_unlock(&mvm->mutex);
  535. return ret;
  536. }
  537. static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
  538. struct ieee80211_vif *vif)
  539. {
  540. u32 tfd_msk = 0, ac;
  541. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  542. if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  543. tfd_msk |= BIT(vif->hw_queue[ac]);
  544. if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
  545. tfd_msk |= BIT(vif->cab_queue);
  546. if (tfd_msk) {
  547. mutex_lock(&mvm->mutex);
  548. iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
  549. mutex_unlock(&mvm->mutex);
  550. }
  551. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  552. /*
  553. * Flush the ROC worker which will flush the OFFCHANNEL queue.
  554. * We assume here that all the packets sent to the OFFCHANNEL
  555. * queue are sent in ROC session.
  556. */
  557. flush_work(&mvm->roc_done_wk);
  558. } else {
  559. /*
  560. * By now, all the AC queues are empty. The AGG queues are
  561. * empty too. We already got all the Tx responses for all the
  562. * packets in the queues. The drain work can have been
  563. * triggered. Flush it.
  564. */
  565. flush_work(&mvm->sta_drained_wk);
  566. }
  567. }
  568. static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
  569. struct ieee80211_vif *vif)
  570. {
  571. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  572. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  573. iwl_mvm_prepare_mac_removal(mvm, vif);
  574. mutex_lock(&mvm->mutex);
  575. if (mvm->bf_allowed_vif == mvmvif) {
  576. mvm->bf_allowed_vif = NULL;
  577. vif->driver_flags &= ~IEEE80211_VIF_BEACON_FILTER;
  578. }
  579. iwl_mvm_vif_dbgfs_clean(mvm, vif);
  580. /*
  581. * For AP/GO interface, the tear down of the resources allocated to the
  582. * interface is be handled as part of the stop_ap flow.
  583. */
  584. if (vif->type == NL80211_IFTYPE_AP) {
  585. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  586. goto out_release;
  587. }
  588. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  589. mvm->p2p_device_vif = NULL;
  590. iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  591. iwl_mvm_binding_remove_vif(mvm, vif);
  592. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  593. mvmvif->phy_ctxt = NULL;
  594. }
  595. /*
  596. * TODO: remove this temporary code.
  597. * Currently MVM FW supports power management only on single MAC.
  598. * Check if only one additional interface remains after removing
  599. * current one. Update power mode on the remaining interface.
  600. */
  601. if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
  602. mvm->vif_count--;
  603. IWL_DEBUG_MAC80211(mvm, "Currently %d interfaces active\n",
  604. mvm->vif_count);
  605. if (mvm->vif_count == 1) {
  606. ieee80211_iterate_active_interfaces(
  607. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  608. iwl_mvm_power_update_iterator, mvm);
  609. }
  610. iwl_mvm_mac_ctxt_remove(mvm, vif);
  611. out_release:
  612. iwl_mvm_mac_ctxt_release(mvm, vif);
  613. mutex_unlock(&mvm->mutex);
  614. }
  615. static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  616. s8 tx_power)
  617. {
  618. /* FW is in charge of regulatory enforcement */
  619. struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
  620. .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
  621. .pwr_restriction = cpu_to_le16(tx_power),
  622. };
  623. return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
  624. sizeof(reduce_txpwr_cmd),
  625. &reduce_txpwr_cmd);
  626. }
  627. static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
  628. {
  629. return 0;
  630. }
  631. static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
  632. unsigned int changed_flags,
  633. unsigned int *total_flags,
  634. u64 multicast)
  635. {
  636. *total_flags = 0;
  637. }
  638. static int iwl_mvm_configure_mcast_filter(struct iwl_mvm *mvm,
  639. struct ieee80211_vif *vif)
  640. {
  641. struct iwl_mcast_filter_cmd mcast_filter_cmd = {
  642. .pass_all = 1,
  643. };
  644. memcpy(mcast_filter_cmd.bssid, vif->bss_conf.bssid, ETH_ALEN);
  645. return iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC,
  646. sizeof(mcast_filter_cmd),
  647. &mcast_filter_cmd);
  648. }
  649. static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
  650. struct ieee80211_vif *vif,
  651. struct ieee80211_bss_conf *bss_conf,
  652. u32 changes)
  653. {
  654. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  655. int ret;
  656. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  657. if (ret)
  658. IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
  659. if (changes & BSS_CHANGED_ASSOC) {
  660. if (bss_conf->assoc) {
  661. /* add quota for this interface */
  662. ret = iwl_mvm_update_quotas(mvm, vif);
  663. if (ret) {
  664. IWL_ERR(mvm, "failed to update quotas\n");
  665. return;
  666. }
  667. iwl_mvm_configure_mcast_filter(mvm, vif);
  668. } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  669. /* remove AP station now that the MAC is unassoc */
  670. ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
  671. if (ret)
  672. IWL_ERR(mvm, "failed to remove AP station\n");
  673. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  674. /* remove quota for this interface */
  675. ret = iwl_mvm_update_quotas(mvm, NULL);
  676. if (ret)
  677. IWL_ERR(mvm, "failed to update quotas\n");
  678. }
  679. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD)) {
  680. /* Workaround for FW bug, otherwise FW disables device
  681. * power save upon disassociation
  682. */
  683. ret = iwl_mvm_power_update_mode(mvm, vif);
  684. if (ret)
  685. IWL_ERR(mvm, "failed to update power mode\n");
  686. }
  687. iwl_mvm_bt_coex_vif_assoc(mvm, vif);
  688. } else if (changes & BSS_CHANGED_BEACON_INFO) {
  689. /*
  690. * We received a beacon _after_ association so
  691. * remove the session protection.
  692. */
  693. iwl_mvm_remove_time_event(mvm, mvmvif,
  694. &mvmvif->time_event_data);
  695. } else if (changes & BSS_CHANGED_PS) {
  696. ret = iwl_mvm_power_update_mode(mvm, vif);
  697. if (ret)
  698. IWL_ERR(mvm, "failed to update power mode\n");
  699. }
  700. if (changes & BSS_CHANGED_TXPOWER) {
  701. IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
  702. bss_conf->txpower);
  703. iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
  704. }
  705. }
  706. static int iwl_mvm_start_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  707. {
  708. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  709. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  710. int ret;
  711. mutex_lock(&mvm->mutex);
  712. /* Send the beacon template */
  713. ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
  714. if (ret)
  715. goto out_unlock;
  716. /* Add the mac context */
  717. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  718. if (ret)
  719. goto out_unlock;
  720. /* Perform the binding */
  721. ret = iwl_mvm_binding_add_vif(mvm, vif);
  722. if (ret)
  723. goto out_remove;
  724. mvmvif->ap_active = true;
  725. /* Send the bcast station. At this stage the TBTT and DTIM time events
  726. * are added and applied to the scheduler */
  727. ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
  728. if (ret)
  729. goto out_unbind;
  730. ret = iwl_mvm_update_quotas(mvm, vif);
  731. if (ret)
  732. goto out_rm_bcast;
  733. /* Need to update the P2P Device MAC */
  734. if (vif->p2p && mvm->p2p_device_vif)
  735. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
  736. mutex_unlock(&mvm->mutex);
  737. return 0;
  738. out_rm_bcast:
  739. iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  740. out_unbind:
  741. iwl_mvm_binding_remove_vif(mvm, vif);
  742. out_remove:
  743. iwl_mvm_mac_ctxt_remove(mvm, vif);
  744. out_unlock:
  745. mutex_unlock(&mvm->mutex);
  746. return ret;
  747. }
  748. static void iwl_mvm_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  749. {
  750. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  751. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  752. iwl_mvm_prepare_mac_removal(mvm, vif);
  753. mutex_lock(&mvm->mutex);
  754. mvmvif->ap_active = false;
  755. /* Need to update the P2P Device MAC */
  756. if (vif->p2p && mvm->p2p_device_vif)
  757. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
  758. iwl_mvm_update_quotas(mvm, NULL);
  759. iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  760. iwl_mvm_binding_remove_vif(mvm, vif);
  761. iwl_mvm_mac_ctxt_remove(mvm, vif);
  762. mutex_unlock(&mvm->mutex);
  763. }
  764. static void iwl_mvm_bss_info_changed_ap(struct iwl_mvm *mvm,
  765. struct ieee80211_vif *vif,
  766. struct ieee80211_bss_conf *bss_conf,
  767. u32 changes)
  768. {
  769. /* Need to send a new beacon template to the FW */
  770. if (changes & BSS_CHANGED_BEACON) {
  771. if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
  772. IWL_WARN(mvm, "Failed updating beacon data\n");
  773. }
  774. }
  775. static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
  776. struct ieee80211_vif *vif,
  777. struct ieee80211_bss_conf *bss_conf,
  778. u32 changes)
  779. {
  780. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  781. mutex_lock(&mvm->mutex);
  782. switch (vif->type) {
  783. case NL80211_IFTYPE_STATION:
  784. iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
  785. break;
  786. case NL80211_IFTYPE_AP:
  787. iwl_mvm_bss_info_changed_ap(mvm, vif, bss_conf, changes);
  788. break;
  789. default:
  790. /* shouldn't happen */
  791. WARN_ON_ONCE(1);
  792. }
  793. mutex_unlock(&mvm->mutex);
  794. }
  795. static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
  796. struct ieee80211_vif *vif,
  797. struct cfg80211_scan_request *req)
  798. {
  799. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  800. int ret;
  801. if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
  802. return -EINVAL;
  803. mutex_lock(&mvm->mutex);
  804. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  805. ret = iwl_mvm_scan_request(mvm, vif, req);
  806. else
  807. ret = -EBUSY;
  808. mutex_unlock(&mvm->mutex);
  809. return ret;
  810. }
  811. static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
  812. struct ieee80211_vif *vif)
  813. {
  814. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  815. mutex_lock(&mvm->mutex);
  816. iwl_mvm_cancel_scan(mvm);
  817. mutex_unlock(&mvm->mutex);
  818. }
  819. static void
  820. iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
  821. struct ieee80211_sta *sta, u16 tid,
  822. int num_frames,
  823. enum ieee80211_frame_release_type reason,
  824. bool more_data)
  825. {
  826. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  827. /* TODO: how do we tell the fw to send frames for a specific TID */
  828. /*
  829. * The fw will send EOSP notification when the last frame will be
  830. * transmitted.
  831. */
  832. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
  833. }
  834. static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
  835. struct ieee80211_vif *vif,
  836. enum sta_notify_cmd cmd,
  837. struct ieee80211_sta *sta)
  838. {
  839. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  840. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  841. switch (cmd) {
  842. case STA_NOTIFY_SLEEP:
  843. if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
  844. ieee80211_sta_block_awake(hw, sta, true);
  845. /*
  846. * The fw updates the STA to be asleep. Tx packets on the Tx
  847. * queues to this station will not be transmitted. The fw will
  848. * send a Tx response with TX_STATUS_FAIL_DEST_PS.
  849. */
  850. break;
  851. case STA_NOTIFY_AWAKE:
  852. if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  853. break;
  854. iwl_mvm_sta_modify_ps_wake(mvm, sta);
  855. break;
  856. default:
  857. break;
  858. }
  859. }
  860. static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
  861. struct ieee80211_vif *vif,
  862. struct ieee80211_sta *sta,
  863. enum ieee80211_sta_state old_state,
  864. enum ieee80211_sta_state new_state)
  865. {
  866. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  867. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  868. int ret;
  869. IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
  870. sta->addr, old_state, new_state);
  871. /* this would be a mac80211 bug ... but don't crash */
  872. if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
  873. return -EINVAL;
  874. /* if a STA is being removed, reuse its ID */
  875. flush_work(&mvm->sta_drained_wk);
  876. mutex_lock(&mvm->mutex);
  877. if (old_state == IEEE80211_STA_NOTEXIST &&
  878. new_state == IEEE80211_STA_NONE) {
  879. /*
  880. * Firmware bug - it'll crash if the beacon interval is less
  881. * than 16. We can't avoid connecting at all, so refuse the
  882. * station state change, this will cause mac80211 to abandon
  883. * attempts to connect to this AP, and eventually wpa_s will
  884. * blacklist the AP...
  885. */
  886. if (vif->type == NL80211_IFTYPE_STATION &&
  887. vif->bss_conf.beacon_int < 16) {
  888. IWL_ERR(mvm,
  889. "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
  890. sta->addr, vif->bss_conf.beacon_int);
  891. ret = -EINVAL;
  892. goto out_unlock;
  893. }
  894. ret = iwl_mvm_add_sta(mvm, vif, sta);
  895. } else if (old_state == IEEE80211_STA_NONE &&
  896. new_state == IEEE80211_STA_AUTH) {
  897. ret = 0;
  898. } else if (old_state == IEEE80211_STA_AUTH &&
  899. new_state == IEEE80211_STA_ASSOC) {
  900. ret = iwl_mvm_update_sta(mvm, vif, sta);
  901. if (ret == 0)
  902. iwl_mvm_rs_rate_init(mvm, sta,
  903. mvmvif->phy_ctxt->channel->band);
  904. } else if (old_state == IEEE80211_STA_ASSOC &&
  905. new_state == IEEE80211_STA_AUTHORIZED) {
  906. /* enable beacon filtering */
  907. WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
  908. ret = 0;
  909. } else if (old_state == IEEE80211_STA_AUTHORIZED &&
  910. new_state == IEEE80211_STA_ASSOC) {
  911. /* disable beacon filtering */
  912. WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
  913. ret = 0;
  914. } else if (old_state == IEEE80211_STA_ASSOC &&
  915. new_state == IEEE80211_STA_AUTH) {
  916. ret = 0;
  917. } else if (old_state == IEEE80211_STA_AUTH &&
  918. new_state == IEEE80211_STA_NONE) {
  919. ret = 0;
  920. } else if (old_state == IEEE80211_STA_NONE &&
  921. new_state == IEEE80211_STA_NOTEXIST) {
  922. ret = iwl_mvm_rm_sta(mvm, vif, sta);
  923. } else {
  924. ret = -EIO;
  925. }
  926. out_unlock:
  927. mutex_unlock(&mvm->mutex);
  928. return ret;
  929. }
  930. static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  931. {
  932. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  933. mvm->rts_threshold = value;
  934. return 0;
  935. }
  936. static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
  937. struct ieee80211_vif *vif, u16 ac,
  938. const struct ieee80211_tx_queue_params *params)
  939. {
  940. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  941. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  942. mvmvif->queue_params[ac] = *params;
  943. /*
  944. * No need to update right away, we'll get BSS_CHANGED_QOS
  945. * The exception is P2P_DEVICE interface which needs immediate update.
  946. */
  947. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  948. int ret;
  949. mutex_lock(&mvm->mutex);
  950. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  951. mutex_unlock(&mvm->mutex);
  952. return ret;
  953. }
  954. return 0;
  955. }
  956. static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
  957. struct ieee80211_vif *vif)
  958. {
  959. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  960. u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
  961. 200 + vif->bss_conf.beacon_int);
  962. u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
  963. 100 + vif->bss_conf.beacon_int);
  964. if (WARN_ON_ONCE(vif->bss_conf.assoc))
  965. return;
  966. mutex_lock(&mvm->mutex);
  967. /* Try really hard to protect the session and hear a beacon */
  968. iwl_mvm_protect_session(mvm, vif, duration, min_duration);
  969. mutex_unlock(&mvm->mutex);
  970. }
  971. static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
  972. enum set_key_cmd cmd,
  973. struct ieee80211_vif *vif,
  974. struct ieee80211_sta *sta,
  975. struct ieee80211_key_conf *key)
  976. {
  977. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  978. int ret;
  979. if (iwlwifi_mod_params.sw_crypto) {
  980. IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
  981. return -EOPNOTSUPP;
  982. }
  983. switch (key->cipher) {
  984. case WLAN_CIPHER_SUITE_TKIP:
  985. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  986. /* fall-through */
  987. case WLAN_CIPHER_SUITE_CCMP:
  988. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  989. break;
  990. case WLAN_CIPHER_SUITE_AES_CMAC:
  991. WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
  992. break;
  993. case WLAN_CIPHER_SUITE_WEP40:
  994. case WLAN_CIPHER_SUITE_WEP104:
  995. /*
  996. * Support for TX only, at least for now, so accept
  997. * the key and do nothing else. Then mac80211 will
  998. * pass it for TX but we don't have to use it for RX.
  999. */
  1000. return 0;
  1001. default:
  1002. return -EOPNOTSUPP;
  1003. }
  1004. mutex_lock(&mvm->mutex);
  1005. switch (cmd) {
  1006. case SET_KEY:
  1007. if (vif->type == NL80211_IFTYPE_AP && !sta) {
  1008. /* GTK on AP interface is a TX-only key, return 0 */
  1009. ret = 0;
  1010. key->hw_key_idx = STA_KEY_IDX_INVALID;
  1011. break;
  1012. }
  1013. IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
  1014. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
  1015. if (ret) {
  1016. IWL_WARN(mvm, "set key failed\n");
  1017. /*
  1018. * can't add key for RX, but we don't need it
  1019. * in the device for TX so still return 0
  1020. */
  1021. key->hw_key_idx = STA_KEY_IDX_INVALID;
  1022. ret = 0;
  1023. }
  1024. break;
  1025. case DISABLE_KEY:
  1026. if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
  1027. ret = 0;
  1028. break;
  1029. }
  1030. IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
  1031. ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
  1032. break;
  1033. default:
  1034. ret = -EINVAL;
  1035. }
  1036. mutex_unlock(&mvm->mutex);
  1037. return ret;
  1038. }
  1039. static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
  1040. struct ieee80211_vif *vif,
  1041. struct ieee80211_key_conf *keyconf,
  1042. struct ieee80211_sta *sta,
  1043. u32 iv32, u16 *phase1key)
  1044. {
  1045. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1046. iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
  1047. }
  1048. static int iwl_mvm_roc(struct ieee80211_hw *hw,
  1049. struct ieee80211_vif *vif,
  1050. struct ieee80211_channel *channel,
  1051. int duration,
  1052. enum ieee80211_roc_type type)
  1053. {
  1054. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1055. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1056. struct cfg80211_chan_def chandef;
  1057. struct iwl_mvm_phy_ctxt *phy_ctxt;
  1058. int ret, i;
  1059. IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
  1060. duration, type);
  1061. if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
  1062. IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
  1063. return -EINVAL;
  1064. }
  1065. mutex_lock(&mvm->mutex);
  1066. for (i = 0; i < NUM_PHY_CTX; i++) {
  1067. phy_ctxt = &mvm->phy_ctxts[i];
  1068. if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
  1069. continue;
  1070. if (phy_ctxt->ref && channel == phy_ctxt->channel) {
  1071. /*
  1072. * Unbind the P2P_DEVICE from the current PHY context,
  1073. * and if the PHY context is not used remove it.
  1074. */
  1075. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  1076. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  1077. goto out_unlock;
  1078. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  1079. /* Bind the P2P_DEVICE to the current PHY Context */
  1080. mvmvif->phy_ctxt = phy_ctxt;
  1081. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1082. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  1083. goto out_unlock;
  1084. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  1085. goto schedule_time_event;
  1086. }
  1087. }
  1088. /* Need to update the PHY context only if the ROC channel changed */
  1089. if (channel == mvmvif->phy_ctxt->channel)
  1090. goto schedule_time_event;
  1091. cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
  1092. /*
  1093. * Change the PHY context configuration as it is currently referenced
  1094. * only by the P2P Device MAC
  1095. */
  1096. if (mvmvif->phy_ctxt->ref == 1) {
  1097. ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
  1098. &chandef, 1, 1);
  1099. if (ret)
  1100. goto out_unlock;
  1101. } else {
  1102. /*
  1103. * The PHY context is shared with other MACs. Need to remove the
  1104. * P2P Device from the binding, allocate an new PHY context and
  1105. * create a new binding
  1106. */
  1107. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  1108. if (!phy_ctxt) {
  1109. ret = -ENOSPC;
  1110. goto out_unlock;
  1111. }
  1112. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
  1113. 1, 1);
  1114. if (ret) {
  1115. IWL_ERR(mvm, "Failed to change PHY context\n");
  1116. goto out_unlock;
  1117. }
  1118. /* Unbind the P2P_DEVICE from the current PHY context */
  1119. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  1120. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  1121. goto out_unlock;
  1122. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  1123. /* Bind the P2P_DEVICE to the new allocated PHY context */
  1124. mvmvif->phy_ctxt = phy_ctxt;
  1125. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1126. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  1127. goto out_unlock;
  1128. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  1129. }
  1130. schedule_time_event:
  1131. /* Schedule the time events */
  1132. ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
  1133. out_unlock:
  1134. mutex_unlock(&mvm->mutex);
  1135. IWL_DEBUG_MAC80211(mvm, "leave\n");
  1136. return ret;
  1137. }
  1138. static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
  1139. {
  1140. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1141. IWL_DEBUG_MAC80211(mvm, "enter\n");
  1142. mutex_lock(&mvm->mutex);
  1143. iwl_mvm_stop_p2p_roc(mvm);
  1144. mutex_unlock(&mvm->mutex);
  1145. IWL_DEBUG_MAC80211(mvm, "leave\n");
  1146. return 0;
  1147. }
  1148. static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
  1149. struct ieee80211_chanctx_conf *ctx)
  1150. {
  1151. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1152. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1153. struct iwl_mvm_phy_ctxt *phy_ctxt;
  1154. int ret;
  1155. IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
  1156. mutex_lock(&mvm->mutex);
  1157. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  1158. if (!phy_ctxt) {
  1159. ret = -ENOSPC;
  1160. goto out;
  1161. }
  1162. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
  1163. ctx->rx_chains_static,
  1164. ctx->rx_chains_dynamic);
  1165. if (ret) {
  1166. IWL_ERR(mvm, "Failed to add PHY context\n");
  1167. goto out;
  1168. }
  1169. iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
  1170. *phy_ctxt_id = phy_ctxt->id;
  1171. out:
  1172. mutex_unlock(&mvm->mutex);
  1173. return ret;
  1174. }
  1175. static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
  1176. struct ieee80211_chanctx_conf *ctx)
  1177. {
  1178. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1179. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1180. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  1181. mutex_lock(&mvm->mutex);
  1182. iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
  1183. mutex_unlock(&mvm->mutex);
  1184. }
  1185. static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
  1186. struct ieee80211_chanctx_conf *ctx,
  1187. u32 changed)
  1188. {
  1189. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1190. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1191. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  1192. if (WARN_ONCE((phy_ctxt->ref > 1) &&
  1193. (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
  1194. IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
  1195. IEEE80211_CHANCTX_CHANGE_RADAR)),
  1196. "Cannot change PHY. Ref=%d, changed=0x%X\n",
  1197. phy_ctxt->ref, changed))
  1198. return;
  1199. mutex_lock(&mvm->mutex);
  1200. iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->def,
  1201. ctx->rx_chains_static,
  1202. ctx->rx_chains_dynamic);
  1203. mutex_unlock(&mvm->mutex);
  1204. }
  1205. static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
  1206. struct ieee80211_vif *vif,
  1207. struct ieee80211_chanctx_conf *ctx)
  1208. {
  1209. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1210. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1211. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  1212. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1213. int ret;
  1214. mutex_lock(&mvm->mutex);
  1215. mvmvif->phy_ctxt = phy_ctxt;
  1216. switch (vif->type) {
  1217. case NL80211_IFTYPE_AP:
  1218. /*
  1219. * The AP binding flow is handled as part of the start_ap flow
  1220. * (in bss_info_changed).
  1221. */
  1222. ret = 0;
  1223. goto out_unlock;
  1224. case NL80211_IFTYPE_STATION:
  1225. case NL80211_IFTYPE_ADHOC:
  1226. case NL80211_IFTYPE_MONITOR:
  1227. break;
  1228. default:
  1229. ret = -EINVAL;
  1230. goto out_unlock;
  1231. }
  1232. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1233. if (ret)
  1234. goto out_unlock;
  1235. /*
  1236. * Setting the quota at this stage is only required for monitor
  1237. * interfaces. For the other types, the bss_info changed flow
  1238. * will handle quota settings.
  1239. */
  1240. if (vif->type == NL80211_IFTYPE_MONITOR) {
  1241. mvmvif->monitor_active = true;
  1242. ret = iwl_mvm_update_quotas(mvm, vif);
  1243. if (ret)
  1244. goto out_remove_binding;
  1245. }
  1246. goto out_unlock;
  1247. out_remove_binding:
  1248. iwl_mvm_binding_remove_vif(mvm, vif);
  1249. out_unlock:
  1250. mutex_unlock(&mvm->mutex);
  1251. if (ret)
  1252. mvmvif->phy_ctxt = NULL;
  1253. return ret;
  1254. }
  1255. static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1256. struct ieee80211_vif *vif,
  1257. struct ieee80211_chanctx_conf *ctx)
  1258. {
  1259. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1260. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1261. mutex_lock(&mvm->mutex);
  1262. iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
  1263. if (vif->type == NL80211_IFTYPE_AP)
  1264. goto out_unlock;
  1265. switch (vif->type) {
  1266. case NL80211_IFTYPE_MONITOR:
  1267. mvmvif->monitor_active = false;
  1268. iwl_mvm_update_quotas(mvm, NULL);
  1269. break;
  1270. default:
  1271. break;
  1272. }
  1273. iwl_mvm_binding_remove_vif(mvm, vif);
  1274. out_unlock:
  1275. mvmvif->phy_ctxt = NULL;
  1276. mutex_unlock(&mvm->mutex);
  1277. }
  1278. static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
  1279. struct ieee80211_sta *sta,
  1280. bool set)
  1281. {
  1282. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1283. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  1284. if (!mvm_sta || !mvm_sta->vif) {
  1285. IWL_ERR(mvm, "Station is not associated to a vif\n");
  1286. return -EINVAL;
  1287. }
  1288. return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
  1289. }
  1290. static void iwl_mvm_mac_rssi_callback(struct ieee80211_hw *hw,
  1291. struct ieee80211_vif *vif,
  1292. enum ieee80211_rssi_event rssi_event)
  1293. {
  1294. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1295. iwl_mvm_bt_rssi_event(mvm, vif, rssi_event);
  1296. }
  1297. struct ieee80211_ops iwl_mvm_hw_ops = {
  1298. .tx = iwl_mvm_mac_tx,
  1299. .ampdu_action = iwl_mvm_mac_ampdu_action,
  1300. .start = iwl_mvm_mac_start,
  1301. .restart_complete = iwl_mvm_mac_restart_complete,
  1302. .stop = iwl_mvm_mac_stop,
  1303. .add_interface = iwl_mvm_mac_add_interface,
  1304. .remove_interface = iwl_mvm_mac_remove_interface,
  1305. .config = iwl_mvm_mac_config,
  1306. .configure_filter = iwl_mvm_configure_filter,
  1307. .bss_info_changed = iwl_mvm_bss_info_changed,
  1308. .hw_scan = iwl_mvm_mac_hw_scan,
  1309. .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
  1310. .sta_state = iwl_mvm_mac_sta_state,
  1311. .sta_notify = iwl_mvm_mac_sta_notify,
  1312. .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
  1313. .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
  1314. .conf_tx = iwl_mvm_mac_conf_tx,
  1315. .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
  1316. .set_key = iwl_mvm_mac_set_key,
  1317. .update_tkip_key = iwl_mvm_mac_update_tkip_key,
  1318. .remain_on_channel = iwl_mvm_roc,
  1319. .cancel_remain_on_channel = iwl_mvm_cancel_roc,
  1320. .rssi_callback = iwl_mvm_mac_rssi_callback,
  1321. .add_chanctx = iwl_mvm_add_chanctx,
  1322. .remove_chanctx = iwl_mvm_remove_chanctx,
  1323. .change_chanctx = iwl_mvm_change_chanctx,
  1324. .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
  1325. .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
  1326. .start_ap = iwl_mvm_start_ap,
  1327. .stop_ap = iwl_mvm_stop_ap,
  1328. .set_tim = iwl_mvm_set_tim,
  1329. #ifdef CONFIG_PM_SLEEP
  1330. /* look at d3.c */
  1331. .suspend = iwl_mvm_suspend,
  1332. .resume = iwl_mvm_resume,
  1333. .set_wakeup = iwl_mvm_set_wakeup,
  1334. .set_rekey_data = iwl_mvm_set_rekey_data,
  1335. #if IS_ENABLED(CONFIG_IPV6)
  1336. .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
  1337. #endif
  1338. .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
  1339. #endif
  1340. };