mac80211.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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 - 2014 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 - 2014 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. #include "testmode.h"
  80. static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
  81. {
  82. .max = 1,
  83. .types = BIT(NL80211_IFTYPE_STATION),
  84. },
  85. {
  86. .max = 1,
  87. .types = BIT(NL80211_IFTYPE_AP) |
  88. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  89. BIT(NL80211_IFTYPE_P2P_GO),
  90. },
  91. {
  92. .max = 1,
  93. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  94. },
  95. };
  96. static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
  97. {
  98. .num_different_channels = 1,
  99. .max_interfaces = 3,
  100. .limits = iwl_mvm_limits,
  101. .n_limits = ARRAY_SIZE(iwl_mvm_limits),
  102. },
  103. };
  104. #ifdef CONFIG_PM_SLEEP
  105. static const struct nl80211_wowlan_tcp_data_token_feature
  106. iwl_mvm_wowlan_tcp_token_feature = {
  107. .min_len = 0,
  108. .max_len = 255,
  109. .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
  110. };
  111. static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
  112. .tok = &iwl_mvm_wowlan_tcp_token_feature,
  113. .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
  114. sizeof(struct ethhdr) -
  115. sizeof(struct iphdr) -
  116. sizeof(struct tcphdr),
  117. .data_interval_max = 65535, /* __le16 in API */
  118. .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
  119. sizeof(struct ethhdr) -
  120. sizeof(struct iphdr) -
  121. sizeof(struct tcphdr),
  122. .seq = true,
  123. };
  124. #endif
  125. static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
  126. {
  127. int i;
  128. memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
  129. for (i = 0; i < NUM_PHY_CTX; i++) {
  130. mvm->phy_ctxts[i].id = i;
  131. mvm->phy_ctxts[i].ref = 0;
  132. }
  133. }
  134. static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
  135. {
  136. /* we create the 802.11 header and SSID element */
  137. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
  138. return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
  139. return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
  140. }
  141. int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
  142. {
  143. struct ieee80211_hw *hw = mvm->hw;
  144. int num_mac, ret, i;
  145. /* Tell mac80211 our characteristics */
  146. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  147. IEEE80211_HW_SPECTRUM_MGMT |
  148. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  149. IEEE80211_HW_QUEUE_CONTROL |
  150. IEEE80211_HW_WANT_MONITOR_VIF |
  151. IEEE80211_HW_SUPPORTS_PS |
  152. IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
  153. IEEE80211_HW_AMPDU_AGGREGATION |
  154. IEEE80211_HW_TIMING_BEACON_ONLY |
  155. IEEE80211_HW_CONNECTION_MONITOR |
  156. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  157. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  158. hw->queues = mvm->first_agg_queue;
  159. hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
  160. hw->rate_control_algorithm = "iwl-mvm-rs";
  161. /*
  162. * Enable 11w if advertised by firmware and software crypto
  163. * is not enabled (as the firmware will interpret some mgmt
  164. * packets, so enabling it with software crypto isn't safe)
  165. */
  166. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
  167. !iwlwifi_mod_params.sw_crypto)
  168. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  169. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT) {
  170. hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
  171. hw->uapsd_queues = IWL_UAPSD_AC_INFO;
  172. hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
  173. }
  174. hw->sta_data_size = sizeof(struct iwl_mvm_sta);
  175. hw->vif_data_size = sizeof(struct iwl_mvm_vif);
  176. hw->chanctx_data_size = sizeof(u16);
  177. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  178. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  179. BIT(NL80211_IFTYPE_AP) |
  180. BIT(NL80211_IFTYPE_P2P_GO) |
  181. BIT(NL80211_IFTYPE_P2P_DEVICE);
  182. /* IBSS has bugs in older versions */
  183. if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 8)
  184. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
  185. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  186. hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
  187. REGULATORY_DISABLE_BEACON_HINTS;
  188. hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
  189. hw->wiphy->n_iface_combinations =
  190. ARRAY_SIZE(iwl_mvm_iface_combinations);
  191. hw->wiphy->max_remain_on_channel_duration = 10000;
  192. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  193. /* Extract MAC address */
  194. memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
  195. hw->wiphy->addresses = mvm->addresses;
  196. hw->wiphy->n_addresses = 1;
  197. /* Extract additional MAC addresses if available */
  198. num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
  199. min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
  200. for (i = 1; i < num_mac; i++) {
  201. memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
  202. ETH_ALEN);
  203. mvm->addresses[i].addr[5]++;
  204. hw->wiphy->n_addresses++;
  205. }
  206. iwl_mvm_reset_phy_ctxts(mvm);
  207. hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
  208. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  209. if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
  210. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  211. &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  212. if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
  213. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  214. &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  215. hw->wiphy->hw_version = mvm->trans->hw_id;
  216. if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
  217. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  218. else
  219. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  220. if (0 && mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SCHED_SCAN) {
  221. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
  222. hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
  223. hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
  224. /* we create the 802.11 header and zero length SSID IE. */
  225. hw->wiphy->max_sched_scan_ie_len =
  226. SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
  227. }
  228. hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
  229. NL80211_FEATURE_P2P_GO_OPPPS |
  230. NL80211_FEATURE_LOW_PRIORITY_SCAN;
  231. mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  232. /* currently FW API supports only one optional cipher scheme */
  233. if (mvm->fw->cs[0].cipher) {
  234. mvm->hw->n_cipher_schemes = 1;
  235. mvm->hw->cipher_schemes = &mvm->fw->cs[0];
  236. }
  237. #ifdef CONFIG_PM_SLEEP
  238. if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
  239. mvm->trans->ops->d3_suspend &&
  240. mvm->trans->ops->d3_resume &&
  241. device_can_wakeup(mvm->trans->dev)) {
  242. mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  243. WIPHY_WOWLAN_DISCONNECT |
  244. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  245. WIPHY_WOWLAN_RFKILL_RELEASE;
  246. if (!iwlwifi_mod_params.sw_crypto)
  247. mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  248. WIPHY_WOWLAN_GTK_REKEY_FAILURE |
  249. WIPHY_WOWLAN_4WAY_HANDSHAKE;
  250. mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
  251. mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
  252. mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
  253. mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
  254. hw->wiphy->wowlan = &mvm->wowlan;
  255. }
  256. #endif
  257. ret = iwl_mvm_leds_init(mvm);
  258. if (ret)
  259. return ret;
  260. ret = ieee80211_register_hw(mvm->hw);
  261. if (ret)
  262. iwl_mvm_leds_exit(mvm);
  263. return ret;
  264. }
  265. static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
  266. struct ieee80211_tx_control *control,
  267. struct sk_buff *skb)
  268. {
  269. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  270. if (iwl_mvm_is_radio_killed(mvm)) {
  271. IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
  272. goto drop;
  273. }
  274. if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
  275. !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
  276. goto drop;
  277. if (control->sta) {
  278. if (iwl_mvm_tx_skb(mvm, skb, control->sta))
  279. goto drop;
  280. return;
  281. }
  282. if (iwl_mvm_tx_skb_non_sta(mvm, skb))
  283. goto drop;
  284. return;
  285. drop:
  286. ieee80211_free_txskb(hw, skb);
  287. }
  288. static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
  289. {
  290. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
  291. return false;
  292. return true;
  293. }
  294. static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
  295. {
  296. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
  297. return false;
  298. if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
  299. return true;
  300. /* enabled by default */
  301. return true;
  302. }
  303. static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
  304. struct ieee80211_vif *vif,
  305. enum ieee80211_ampdu_mlme_action action,
  306. struct ieee80211_sta *sta, u16 tid,
  307. u16 *ssn, u8 buf_size)
  308. {
  309. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  310. int ret;
  311. IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
  312. sta->addr, tid, action);
  313. if (!(mvm->nvm_data->sku_cap_11n_enable))
  314. return -EACCES;
  315. mutex_lock(&mvm->mutex);
  316. switch (action) {
  317. case IEEE80211_AMPDU_RX_START:
  318. if (!iwl_enable_rx_ampdu(mvm->cfg)) {
  319. ret = -EINVAL;
  320. break;
  321. }
  322. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
  323. break;
  324. case IEEE80211_AMPDU_RX_STOP:
  325. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
  326. break;
  327. case IEEE80211_AMPDU_TX_START:
  328. if (!iwl_enable_tx_ampdu(mvm->cfg)) {
  329. ret = -EINVAL;
  330. break;
  331. }
  332. ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
  333. break;
  334. case IEEE80211_AMPDU_TX_STOP_CONT:
  335. ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
  336. break;
  337. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  338. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  339. ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
  340. break;
  341. case IEEE80211_AMPDU_TX_OPERATIONAL:
  342. ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
  343. break;
  344. default:
  345. WARN_ON_ONCE(1);
  346. ret = -EINVAL;
  347. break;
  348. }
  349. mutex_unlock(&mvm->mutex);
  350. return ret;
  351. }
  352. static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
  353. struct ieee80211_vif *vif)
  354. {
  355. struct iwl_mvm *mvm = data;
  356. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  357. mvmvif->uploaded = false;
  358. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  359. /* does this make sense at all? */
  360. mvmvif->color++;
  361. spin_lock_bh(&mvm->time_event_lock);
  362. iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
  363. spin_unlock_bh(&mvm->time_event_lock);
  364. mvmvif->phy_ctxt = NULL;
  365. }
  366. static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
  367. {
  368. iwl_trans_stop_device(mvm->trans);
  369. mvm->scan_status = IWL_MVM_SCAN_NONE;
  370. /* just in case one was running */
  371. ieee80211_remain_on_channel_expired(mvm->hw);
  372. ieee80211_iterate_active_interfaces_atomic(
  373. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  374. iwl_mvm_cleanup_iterator, mvm);
  375. mvm->p2p_device_vif = NULL;
  376. iwl_mvm_reset_phy_ctxts(mvm);
  377. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  378. memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
  379. ieee80211_wake_queues(mvm->hw);
  380. mvm->vif_count = 0;
  381. mvm->rx_ba_sessions = 0;
  382. }
  383. static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
  384. {
  385. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  386. int ret;
  387. mutex_lock(&mvm->mutex);
  388. /* Clean up some internal and mac80211 state on restart */
  389. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  390. iwl_mvm_restart_cleanup(mvm);
  391. ret = iwl_mvm_up(mvm);
  392. mutex_unlock(&mvm->mutex);
  393. return ret;
  394. }
  395. static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
  396. {
  397. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  398. int ret;
  399. mutex_lock(&mvm->mutex);
  400. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  401. ret = iwl_mvm_update_quotas(mvm, NULL);
  402. if (ret)
  403. IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
  404. ret);
  405. mutex_unlock(&mvm->mutex);
  406. }
  407. static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
  408. {
  409. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  410. flush_work(&mvm->async_handlers_wk);
  411. mutex_lock(&mvm->mutex);
  412. /* async_handlers_wk is now blocked */
  413. /*
  414. * The work item could be running or queued if the
  415. * ROC time event stops just as we get here.
  416. */
  417. cancel_work_sync(&mvm->roc_done_wk);
  418. iwl_trans_stop_device(mvm->trans);
  419. iwl_mvm_async_handlers_purge(mvm);
  420. /* async_handlers_list is empty and will stay empty: HW is stopped */
  421. /* the fw is stopped, the aux sta is dead: clean up driver state */
  422. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  423. mutex_unlock(&mvm->mutex);
  424. /*
  425. * The worker might have been waiting for the mutex, let it run and
  426. * discover that its list is now empty.
  427. */
  428. cancel_work_sync(&mvm->async_handlers_wk);
  429. }
  430. static void iwl_mvm_power_update_iterator(void *data, u8 *mac,
  431. struct ieee80211_vif *vif)
  432. {
  433. struct iwl_mvm *mvm = data;
  434. iwl_mvm_power_update_mode(mvm, vif);
  435. }
  436. static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
  437. {
  438. u16 i;
  439. lockdep_assert_held(&mvm->mutex);
  440. for (i = 0; i < NUM_PHY_CTX; i++)
  441. if (!mvm->phy_ctxts[i].ref)
  442. return &mvm->phy_ctxts[i];
  443. IWL_ERR(mvm, "No available PHY context\n");
  444. return NULL;
  445. }
  446. static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  447. s8 tx_power)
  448. {
  449. /* FW is in charge of regulatory enforcement */
  450. struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
  451. .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
  452. .pwr_restriction = cpu_to_le16(tx_power),
  453. };
  454. return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, CMD_SYNC,
  455. sizeof(reduce_txpwr_cmd),
  456. &reduce_txpwr_cmd);
  457. }
  458. static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
  459. struct ieee80211_vif *vif)
  460. {
  461. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  462. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  463. int ret;
  464. /*
  465. * Not much to do here. The stack will not allow interface
  466. * types or combinations that we didn't advertise, so we
  467. * don't really have to check the types.
  468. */
  469. mutex_lock(&mvm->mutex);
  470. /* Allocate resources for the MAC context, and add it to the fw */
  471. ret = iwl_mvm_mac_ctxt_init(mvm, vif);
  472. if (ret)
  473. goto out_unlock;
  474. /* Counting number of interfaces is needed for legacy PM */
  475. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  476. mvm->vif_count++;
  477. /*
  478. * The AP binding flow can be done only after the beacon
  479. * template is configured (which happens only in the mac80211
  480. * start_ap() flow), and adding the broadcast station can happen
  481. * only after the binding.
  482. * In addition, since modifying the MAC before adding a bcast
  483. * station is not allowed by the FW, delay the adding of MAC context to
  484. * the point where we can also add the bcast station.
  485. * In short: there's not much we can do at this point, other than
  486. * allocating resources :)
  487. */
  488. if (vif->type == NL80211_IFTYPE_AP ||
  489. vif->type == NL80211_IFTYPE_ADHOC) {
  490. u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
  491. ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
  492. qmask);
  493. if (ret) {
  494. IWL_ERR(mvm, "Failed to allocate bcast sta\n");
  495. goto out_release;
  496. }
  497. iwl_mvm_vif_dbgfs_register(mvm, vif);
  498. goto out_unlock;
  499. }
  500. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  501. if (ret)
  502. goto out_release;
  503. iwl_mvm_power_disable(mvm, vif);
  504. /* beacon filtering */
  505. ret = iwl_mvm_disable_beacon_filter(mvm, vif);
  506. if (ret)
  507. goto out_remove_mac;
  508. if (!mvm->bf_allowed_vif &&
  509. vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
  510. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED){
  511. mvm->bf_allowed_vif = mvmvif;
  512. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
  513. IEEE80211_VIF_SUPPORTS_CQM_RSSI;
  514. }
  515. /*
  516. * P2P_DEVICE interface does not have a channel context assigned to it,
  517. * so a dedicated PHY context is allocated to it and the corresponding
  518. * MAC context is bound to it at this stage.
  519. */
  520. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  521. mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  522. if (!mvmvif->phy_ctxt) {
  523. ret = -ENOSPC;
  524. goto out_free_bf;
  525. }
  526. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  527. ret = iwl_mvm_binding_add_vif(mvm, vif);
  528. if (ret)
  529. goto out_unref_phy;
  530. ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
  531. if (ret)
  532. goto out_unbind;
  533. /* Save a pointer to p2p device vif, so it can later be used to
  534. * update the p2p device MAC when a GO is started/stopped */
  535. mvm->p2p_device_vif = vif;
  536. }
  537. iwl_mvm_vif_dbgfs_register(mvm, vif);
  538. goto out_unlock;
  539. out_unbind:
  540. iwl_mvm_binding_remove_vif(mvm, vif);
  541. out_unref_phy:
  542. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  543. out_free_bf:
  544. if (mvm->bf_allowed_vif == mvmvif) {
  545. mvm->bf_allowed_vif = NULL;
  546. vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
  547. IEEE80211_VIF_SUPPORTS_CQM_RSSI);
  548. }
  549. out_remove_mac:
  550. mvmvif->phy_ctxt = NULL;
  551. iwl_mvm_mac_ctxt_remove(mvm, vif);
  552. out_release:
  553. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  554. mvm->vif_count--;
  555. /* TODO: remove this when legacy PM will be discarded */
  556. ieee80211_iterate_active_interfaces(
  557. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  558. iwl_mvm_power_update_iterator, mvm);
  559. iwl_mvm_mac_ctxt_release(mvm, vif);
  560. out_unlock:
  561. mutex_unlock(&mvm->mutex);
  562. return ret;
  563. }
  564. static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
  565. struct ieee80211_vif *vif)
  566. {
  567. u32 tfd_msk = 0, ac;
  568. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  569. if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  570. tfd_msk |= BIT(vif->hw_queue[ac]);
  571. if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
  572. tfd_msk |= BIT(vif->cab_queue);
  573. if (tfd_msk) {
  574. mutex_lock(&mvm->mutex);
  575. iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
  576. mutex_unlock(&mvm->mutex);
  577. }
  578. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  579. /*
  580. * Flush the ROC worker which will flush the OFFCHANNEL queue.
  581. * We assume here that all the packets sent to the OFFCHANNEL
  582. * queue are sent in ROC session.
  583. */
  584. flush_work(&mvm->roc_done_wk);
  585. } else {
  586. /*
  587. * By now, all the AC queues are empty. The AGG queues are
  588. * empty too. We already got all the Tx responses for all the
  589. * packets in the queues. The drain work can have been
  590. * triggered. Flush it.
  591. */
  592. flush_work(&mvm->sta_drained_wk);
  593. }
  594. }
  595. static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
  596. struct ieee80211_vif *vif)
  597. {
  598. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  599. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  600. iwl_mvm_prepare_mac_removal(mvm, vif);
  601. mutex_lock(&mvm->mutex);
  602. if (mvm->bf_allowed_vif == mvmvif) {
  603. mvm->bf_allowed_vif = NULL;
  604. vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
  605. IEEE80211_VIF_SUPPORTS_CQM_RSSI);
  606. }
  607. iwl_mvm_vif_dbgfs_clean(mvm, vif);
  608. /*
  609. * For AP/GO interface, the tear down of the resources allocated to the
  610. * interface is be handled as part of the stop_ap flow.
  611. */
  612. if (vif->type == NL80211_IFTYPE_AP ||
  613. vif->type == NL80211_IFTYPE_ADHOC) {
  614. #ifdef CONFIG_NL80211_TESTMODE
  615. if (vif == mvm->noa_vif) {
  616. mvm->noa_vif = NULL;
  617. mvm->noa_duration = 0;
  618. }
  619. #endif
  620. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  621. goto out_release;
  622. }
  623. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  624. mvm->p2p_device_vif = NULL;
  625. iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  626. iwl_mvm_binding_remove_vif(mvm, vif);
  627. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  628. mvmvif->phy_ctxt = NULL;
  629. }
  630. if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
  631. mvm->vif_count--;
  632. /* TODO: remove this when legacy PM will be discarded */
  633. ieee80211_iterate_active_interfaces(
  634. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  635. iwl_mvm_power_update_iterator, mvm);
  636. iwl_mvm_mac_ctxt_remove(mvm, vif);
  637. out_release:
  638. iwl_mvm_mac_ctxt_release(mvm, vif);
  639. mutex_unlock(&mvm->mutex);
  640. }
  641. static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
  642. {
  643. return 0;
  644. }
  645. struct iwl_mvm_mc_iter_data {
  646. struct iwl_mvm *mvm;
  647. int port_id;
  648. };
  649. static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
  650. struct ieee80211_vif *vif)
  651. {
  652. struct iwl_mvm_mc_iter_data *data = _data;
  653. struct iwl_mvm *mvm = data->mvm;
  654. struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
  655. int ret, len;
  656. /* if we don't have free ports, mcast frames will be dropped */
  657. if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
  658. return;
  659. if (vif->type != NL80211_IFTYPE_STATION ||
  660. !vif->bss_conf.assoc)
  661. return;
  662. cmd->port_id = data->port_id++;
  663. memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
  664. len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
  665. ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_SYNC, len, cmd);
  666. if (ret)
  667. IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
  668. }
  669. static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
  670. {
  671. struct iwl_mvm_mc_iter_data iter_data = {
  672. .mvm = mvm,
  673. };
  674. lockdep_assert_held(&mvm->mutex);
  675. if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
  676. return;
  677. ieee80211_iterate_active_interfaces(
  678. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  679. iwl_mvm_mc_iface_iterator, &iter_data);
  680. }
  681. static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
  682. struct netdev_hw_addr_list *mc_list)
  683. {
  684. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  685. struct iwl_mcast_filter_cmd *cmd;
  686. struct netdev_hw_addr *addr;
  687. int addr_count = netdev_hw_addr_list_count(mc_list);
  688. bool pass_all = false;
  689. int len;
  690. if (addr_count > MAX_MCAST_FILTERING_ADDRESSES) {
  691. pass_all = true;
  692. addr_count = 0;
  693. }
  694. len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
  695. cmd = kzalloc(len, GFP_ATOMIC);
  696. if (!cmd)
  697. return 0;
  698. if (pass_all) {
  699. cmd->pass_all = 1;
  700. return (u64)(unsigned long)cmd;
  701. }
  702. netdev_hw_addr_list_for_each(addr, mc_list) {
  703. IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
  704. cmd->count, addr->addr);
  705. memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
  706. addr->addr, ETH_ALEN);
  707. cmd->count++;
  708. }
  709. return (u64)(unsigned long)cmd;
  710. }
  711. static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
  712. unsigned int changed_flags,
  713. unsigned int *total_flags,
  714. u64 multicast)
  715. {
  716. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  717. struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
  718. mutex_lock(&mvm->mutex);
  719. /* replace previous configuration */
  720. kfree(mvm->mcast_filter_cmd);
  721. mvm->mcast_filter_cmd = cmd;
  722. if (!cmd)
  723. goto out;
  724. iwl_mvm_recalc_multicast(mvm);
  725. out:
  726. mutex_unlock(&mvm->mutex);
  727. *total_flags = 0;
  728. }
  729. static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
  730. struct ieee80211_vif *vif,
  731. struct ieee80211_bss_conf *bss_conf,
  732. u32 changes)
  733. {
  734. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  735. int ret;
  736. /*
  737. * Re-calculate the tsf id, as the master-slave relations depend on the
  738. * beacon interval, which was not known when the station interface was
  739. * added.
  740. */
  741. if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
  742. iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
  743. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  744. if (ret)
  745. IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
  746. if (changes & BSS_CHANGED_ASSOC) {
  747. if (bss_conf->assoc) {
  748. /* add quota for this interface */
  749. ret = iwl_mvm_update_quotas(mvm, vif);
  750. if (ret) {
  751. IWL_ERR(mvm, "failed to update quotas\n");
  752. return;
  753. }
  754. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
  755. &mvm->status)) {
  756. /*
  757. * If we're restarting then the firmware will
  758. * obviously have lost synchronisation with
  759. * the AP. It will attempt to synchronise by
  760. * itself, but we can make it more reliable by
  761. * scheduling a session protection time event.
  762. *
  763. * The firmware needs to receive a beacon to
  764. * catch up with synchronisation, use 110% of
  765. * the beacon interval.
  766. *
  767. * Set a large maximum delay to allow for more
  768. * than a single interface.
  769. */
  770. u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
  771. iwl_mvm_protect_session(mvm, vif, dur, dur,
  772. 5 * dur);
  773. }
  774. iwl_mvm_sf_update(mvm, vif, false);
  775. iwl_mvm_power_vif_assoc(mvm, vif);
  776. } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  777. /*
  778. * If update fails - SF might be running in associated
  779. * mode while disassociated - which is forbidden.
  780. */
  781. WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
  782. "Failed to update SF upon disassociation\n");
  783. /* remove AP station now that the MAC is unassoc */
  784. ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
  785. if (ret)
  786. IWL_ERR(mvm, "failed to remove AP station\n");
  787. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  788. /* remove quota for this interface */
  789. ret = iwl_mvm_update_quotas(mvm, NULL);
  790. if (ret)
  791. IWL_ERR(mvm, "failed to update quotas\n");
  792. }
  793. iwl_mvm_recalc_multicast(mvm);
  794. /* reset rssi values */
  795. mvmvif->bf_data.ave_beacon_signal = 0;
  796. if (!(mvm->fw->ucode_capa.flags &
  797. IWL_UCODE_TLV_FLAGS_PM_CMD_SUPPORT)) {
  798. /* Workaround for FW bug, otherwise FW disables device
  799. * power save upon disassociation
  800. */
  801. ret = iwl_mvm_power_update_mode(mvm, vif);
  802. if (ret)
  803. IWL_ERR(mvm, "failed to update power mode\n");
  804. }
  805. iwl_mvm_bt_coex_vif_change(mvm);
  806. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
  807. IEEE80211_SMPS_AUTOMATIC);
  808. } else if (changes & BSS_CHANGED_BEACON_INFO) {
  809. /*
  810. * We received a beacon _after_ association so
  811. * remove the session protection.
  812. */
  813. iwl_mvm_remove_time_event(mvm, mvmvif,
  814. &mvmvif->time_event_data);
  815. } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS |
  816. BSS_CHANGED_QOS)) {
  817. ret = iwl_mvm_power_update_mode(mvm, vif);
  818. if (ret)
  819. IWL_ERR(mvm, "failed to update power mode\n");
  820. }
  821. if (changes & BSS_CHANGED_TXPOWER) {
  822. IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
  823. bss_conf->txpower);
  824. iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
  825. }
  826. if (changes & BSS_CHANGED_CQM) {
  827. IWL_DEBUG_MAC80211(mvm, "cqm info_changed");
  828. /* reset cqm events tracking */
  829. mvmvif->bf_data.last_cqm_event = 0;
  830. ret = iwl_mvm_update_beacon_filter(mvm, vif);
  831. if (ret)
  832. IWL_ERR(mvm, "failed to update CQM thresholds\n");
  833. }
  834. }
  835. static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
  836. struct ieee80211_vif *vif)
  837. {
  838. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  839. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  840. int ret;
  841. mutex_lock(&mvm->mutex);
  842. /* Send the beacon template */
  843. ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
  844. if (ret)
  845. goto out_unlock;
  846. /*
  847. * Re-calculate the tsf id, as the master-slave relations depend on the
  848. * beacon interval, which was not known when the AP interface was added.
  849. */
  850. if (vif->type == NL80211_IFTYPE_AP)
  851. iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
  852. /* Add the mac context */
  853. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  854. if (ret)
  855. goto out_unlock;
  856. /* Perform the binding */
  857. ret = iwl_mvm_binding_add_vif(mvm, vif);
  858. if (ret)
  859. goto out_remove;
  860. mvmvif->ap_ibss_active = true;
  861. /* Send the bcast station. At this stage the TBTT and DTIM time events
  862. * are added and applied to the scheduler */
  863. ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
  864. if (ret)
  865. goto out_unbind;
  866. /* must be set before quota calculations */
  867. mvmvif->ap_ibss_active = true;
  868. /* power updated needs to be done before quotas */
  869. mvm->bound_vif_cnt++;
  870. iwl_mvm_power_update_binding(mvm, vif, true);
  871. ret = iwl_mvm_update_quotas(mvm, vif);
  872. if (ret)
  873. goto out_quota_failed;
  874. /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
  875. if (vif->p2p && mvm->p2p_device_vif)
  876. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
  877. iwl_mvm_bt_coex_vif_change(mvm);
  878. mutex_unlock(&mvm->mutex);
  879. return 0;
  880. out_quota_failed:
  881. mvm->bound_vif_cnt--;
  882. iwl_mvm_power_update_binding(mvm, vif, false);
  883. mvmvif->ap_ibss_active = false;
  884. iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  885. out_unbind:
  886. iwl_mvm_binding_remove_vif(mvm, vif);
  887. out_remove:
  888. iwl_mvm_mac_ctxt_remove(mvm, vif);
  889. out_unlock:
  890. mutex_unlock(&mvm->mutex);
  891. return ret;
  892. }
  893. static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
  894. struct ieee80211_vif *vif)
  895. {
  896. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  897. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  898. iwl_mvm_prepare_mac_removal(mvm, vif);
  899. mutex_lock(&mvm->mutex);
  900. mvmvif->ap_ibss_active = false;
  901. iwl_mvm_bt_coex_vif_change(mvm);
  902. /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
  903. if (vif->p2p && mvm->p2p_device_vif)
  904. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif);
  905. iwl_mvm_update_quotas(mvm, NULL);
  906. iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  907. iwl_mvm_binding_remove_vif(mvm, vif);
  908. mvm->bound_vif_cnt--;
  909. iwl_mvm_power_update_binding(mvm, vif, false);
  910. iwl_mvm_mac_ctxt_remove(mvm, vif);
  911. mutex_unlock(&mvm->mutex);
  912. }
  913. static void
  914. iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
  915. struct ieee80211_vif *vif,
  916. struct ieee80211_bss_conf *bss_conf,
  917. u32 changes)
  918. {
  919. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  920. enum ieee80211_bss_change ht_change = BSS_CHANGED_ERP_CTS_PROT |
  921. BSS_CHANGED_HT |
  922. BSS_CHANGED_BANDWIDTH;
  923. int ret;
  924. /* Changes will be applied when the AP/IBSS is started */
  925. if (!mvmvif->ap_ibss_active)
  926. return;
  927. if (changes & ht_change) {
  928. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  929. if (ret)
  930. IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
  931. }
  932. /* Need to send a new beacon template to the FW */
  933. if (changes & BSS_CHANGED_BEACON) {
  934. if (iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
  935. IWL_WARN(mvm, "Failed updating beacon data\n");
  936. }
  937. }
  938. static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
  939. struct ieee80211_vif *vif,
  940. struct ieee80211_bss_conf *bss_conf,
  941. u32 changes)
  942. {
  943. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  944. mutex_lock(&mvm->mutex);
  945. switch (vif->type) {
  946. case NL80211_IFTYPE_STATION:
  947. iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
  948. break;
  949. case NL80211_IFTYPE_AP:
  950. case NL80211_IFTYPE_ADHOC:
  951. iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
  952. break;
  953. default:
  954. /* shouldn't happen */
  955. WARN_ON_ONCE(1);
  956. }
  957. mutex_unlock(&mvm->mutex);
  958. }
  959. static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
  960. struct ieee80211_vif *vif,
  961. struct cfg80211_scan_request *req)
  962. {
  963. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  964. int ret;
  965. if (req->n_channels == 0 || req->n_channels > MAX_NUM_SCAN_CHANNELS)
  966. return -EINVAL;
  967. mutex_lock(&mvm->mutex);
  968. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  969. ret = iwl_mvm_scan_request(mvm, vif, req);
  970. else
  971. ret = -EBUSY;
  972. mutex_unlock(&mvm->mutex);
  973. return ret;
  974. }
  975. static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
  976. struct ieee80211_vif *vif)
  977. {
  978. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  979. mutex_lock(&mvm->mutex);
  980. iwl_mvm_cancel_scan(mvm);
  981. mutex_unlock(&mvm->mutex);
  982. }
  983. static void
  984. iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
  985. struct ieee80211_sta *sta, u16 tid,
  986. int num_frames,
  987. enum ieee80211_frame_release_type reason,
  988. bool more_data)
  989. {
  990. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  991. /* TODO: how do we tell the fw to send frames for a specific TID */
  992. /*
  993. * The fw will send EOSP notification when the last frame will be
  994. * transmitted.
  995. */
  996. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames);
  997. }
  998. static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
  999. struct ieee80211_vif *vif,
  1000. enum sta_notify_cmd cmd,
  1001. struct ieee80211_sta *sta)
  1002. {
  1003. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1004. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1005. switch (cmd) {
  1006. case STA_NOTIFY_SLEEP:
  1007. if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
  1008. ieee80211_sta_block_awake(hw, sta, true);
  1009. /*
  1010. * The fw updates the STA to be asleep. Tx packets on the Tx
  1011. * queues to this station will not be transmitted. The fw will
  1012. * send a Tx response with TX_STATUS_FAIL_DEST_PS.
  1013. */
  1014. break;
  1015. case STA_NOTIFY_AWAKE:
  1016. if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  1017. break;
  1018. iwl_mvm_sta_modify_ps_wake(mvm, sta);
  1019. break;
  1020. default:
  1021. break;
  1022. }
  1023. }
  1024. static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
  1025. struct ieee80211_vif *vif,
  1026. struct ieee80211_sta *sta)
  1027. {
  1028. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1029. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  1030. /*
  1031. * This is called before mac80211 does RCU synchronisation,
  1032. * so here we already invalidate our internal RCU-protected
  1033. * station pointer. The rest of the code will thus no longer
  1034. * be able to find the station this way, and we don't rely
  1035. * on further RCU synchronisation after the sta_state()
  1036. * callback deleted the station.
  1037. */
  1038. mutex_lock(&mvm->mutex);
  1039. if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
  1040. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  1041. ERR_PTR(-ENOENT));
  1042. mutex_unlock(&mvm->mutex);
  1043. }
  1044. static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
  1045. struct ieee80211_vif *vif,
  1046. struct ieee80211_sta *sta,
  1047. enum ieee80211_sta_state old_state,
  1048. enum ieee80211_sta_state new_state)
  1049. {
  1050. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1051. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1052. int ret;
  1053. IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
  1054. sta->addr, old_state, new_state);
  1055. /* this would be a mac80211 bug ... but don't crash */
  1056. if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
  1057. return -EINVAL;
  1058. /* if a STA is being removed, reuse its ID */
  1059. flush_work(&mvm->sta_drained_wk);
  1060. mutex_lock(&mvm->mutex);
  1061. if (old_state == IEEE80211_STA_NOTEXIST &&
  1062. new_state == IEEE80211_STA_NONE) {
  1063. /*
  1064. * Firmware bug - it'll crash if the beacon interval is less
  1065. * than 16. We can't avoid connecting at all, so refuse the
  1066. * station state change, this will cause mac80211 to abandon
  1067. * attempts to connect to this AP, and eventually wpa_s will
  1068. * blacklist the AP...
  1069. */
  1070. if (vif->type == NL80211_IFTYPE_STATION &&
  1071. vif->bss_conf.beacon_int < 16) {
  1072. IWL_ERR(mvm,
  1073. "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
  1074. sta->addr, vif->bss_conf.beacon_int);
  1075. ret = -EINVAL;
  1076. goto out_unlock;
  1077. }
  1078. ret = iwl_mvm_add_sta(mvm, vif, sta);
  1079. } else if (old_state == IEEE80211_STA_NONE &&
  1080. new_state == IEEE80211_STA_AUTH) {
  1081. ret = 0;
  1082. } else if (old_state == IEEE80211_STA_AUTH &&
  1083. new_state == IEEE80211_STA_ASSOC) {
  1084. ret = iwl_mvm_update_sta(mvm, vif, sta);
  1085. if (ret == 0)
  1086. iwl_mvm_rs_rate_init(mvm, sta,
  1087. mvmvif->phy_ctxt->channel->band,
  1088. true);
  1089. } else if (old_state == IEEE80211_STA_ASSOC &&
  1090. new_state == IEEE80211_STA_AUTHORIZED) {
  1091. /* enable beacon filtering */
  1092. WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
  1093. ret = 0;
  1094. } else if (old_state == IEEE80211_STA_AUTHORIZED &&
  1095. new_state == IEEE80211_STA_ASSOC) {
  1096. /* disable beacon filtering */
  1097. WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif));
  1098. ret = 0;
  1099. } else if (old_state == IEEE80211_STA_ASSOC &&
  1100. new_state == IEEE80211_STA_AUTH) {
  1101. ret = 0;
  1102. } else if (old_state == IEEE80211_STA_AUTH &&
  1103. new_state == IEEE80211_STA_NONE) {
  1104. ret = 0;
  1105. } else if (old_state == IEEE80211_STA_NONE &&
  1106. new_state == IEEE80211_STA_NOTEXIST) {
  1107. ret = iwl_mvm_rm_sta(mvm, vif, sta);
  1108. } else {
  1109. ret = -EIO;
  1110. }
  1111. out_unlock:
  1112. mutex_unlock(&mvm->mutex);
  1113. return ret;
  1114. }
  1115. static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  1116. {
  1117. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1118. mvm->rts_threshold = value;
  1119. return 0;
  1120. }
  1121. static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
  1122. struct ieee80211_vif *vif,
  1123. struct ieee80211_sta *sta, u32 changed)
  1124. {
  1125. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1126. if (vif->type == NL80211_IFTYPE_STATION &&
  1127. changed & IEEE80211_RC_NSS_CHANGED)
  1128. iwl_mvm_sf_update(mvm, vif, false);
  1129. }
  1130. static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
  1131. struct ieee80211_vif *vif, u16 ac,
  1132. const struct ieee80211_tx_queue_params *params)
  1133. {
  1134. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1135. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1136. mvmvif->queue_params[ac] = *params;
  1137. /*
  1138. * No need to update right away, we'll get BSS_CHANGED_QOS
  1139. * The exception is P2P_DEVICE interface which needs immediate update.
  1140. */
  1141. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  1142. int ret;
  1143. mutex_lock(&mvm->mutex);
  1144. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  1145. mutex_unlock(&mvm->mutex);
  1146. return ret;
  1147. }
  1148. return 0;
  1149. }
  1150. static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
  1151. struct ieee80211_vif *vif)
  1152. {
  1153. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1154. u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
  1155. 200 + vif->bss_conf.beacon_int);
  1156. u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
  1157. 100 + vif->bss_conf.beacon_int);
  1158. if (WARN_ON_ONCE(vif->bss_conf.assoc))
  1159. return;
  1160. mutex_lock(&mvm->mutex);
  1161. /* Try really hard to protect the session and hear a beacon */
  1162. iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
  1163. mutex_unlock(&mvm->mutex);
  1164. }
  1165. static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
  1166. struct ieee80211_vif *vif,
  1167. struct cfg80211_sched_scan_request *req,
  1168. struct ieee80211_sched_scan_ies *ies)
  1169. {
  1170. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1171. int ret;
  1172. mutex_lock(&mvm->mutex);
  1173. if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
  1174. IWL_DEBUG_SCAN(mvm,
  1175. "SCHED SCAN request during internal scan - abort\n");
  1176. ret = -EBUSY;
  1177. goto out;
  1178. }
  1179. mvm->scan_status = IWL_MVM_SCAN_SCHED;
  1180. ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
  1181. if (ret)
  1182. goto err;
  1183. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  1184. if (ret)
  1185. goto err;
  1186. ret = iwl_mvm_sched_scan_start(mvm, req);
  1187. if (!ret)
  1188. goto out;
  1189. err:
  1190. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1191. out:
  1192. mutex_unlock(&mvm->mutex);
  1193. return ret;
  1194. }
  1195. static void iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
  1196. struct ieee80211_vif *vif)
  1197. {
  1198. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1199. mutex_lock(&mvm->mutex);
  1200. iwl_mvm_sched_scan_stop(mvm);
  1201. mutex_unlock(&mvm->mutex);
  1202. }
  1203. static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
  1204. enum set_key_cmd cmd,
  1205. struct ieee80211_vif *vif,
  1206. struct ieee80211_sta *sta,
  1207. struct ieee80211_key_conf *key)
  1208. {
  1209. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1210. int ret;
  1211. if (iwlwifi_mod_params.sw_crypto) {
  1212. IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
  1213. return -EOPNOTSUPP;
  1214. }
  1215. switch (key->cipher) {
  1216. case WLAN_CIPHER_SUITE_TKIP:
  1217. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1218. /* fall-through */
  1219. case WLAN_CIPHER_SUITE_CCMP:
  1220. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1221. break;
  1222. case WLAN_CIPHER_SUITE_AES_CMAC:
  1223. WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
  1224. break;
  1225. case WLAN_CIPHER_SUITE_WEP40:
  1226. case WLAN_CIPHER_SUITE_WEP104:
  1227. /*
  1228. * Support for TX only, at least for now, so accept
  1229. * the key and do nothing else. Then mac80211 will
  1230. * pass it for TX but we don't have to use it for RX.
  1231. */
  1232. return 0;
  1233. default:
  1234. /* currently FW supports only one optional cipher scheme */
  1235. if (hw->n_cipher_schemes &&
  1236. hw->cipher_schemes->cipher == key->cipher)
  1237. key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  1238. else
  1239. return -EOPNOTSUPP;
  1240. }
  1241. mutex_lock(&mvm->mutex);
  1242. switch (cmd) {
  1243. case SET_KEY:
  1244. if ((vif->type == NL80211_IFTYPE_ADHOC ||
  1245. vif->type == NL80211_IFTYPE_AP) && !sta) {
  1246. /*
  1247. * GTK on AP interface is a TX-only key, return 0;
  1248. * on IBSS they're per-station and because we're lazy
  1249. * we don't support them for RX, so do the same.
  1250. */
  1251. ret = 0;
  1252. key->hw_key_idx = STA_KEY_IDX_INVALID;
  1253. break;
  1254. }
  1255. IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
  1256. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
  1257. if (ret) {
  1258. IWL_WARN(mvm, "set key failed\n");
  1259. /*
  1260. * can't add key for RX, but we don't need it
  1261. * in the device for TX so still return 0
  1262. */
  1263. key->hw_key_idx = STA_KEY_IDX_INVALID;
  1264. ret = 0;
  1265. }
  1266. break;
  1267. case DISABLE_KEY:
  1268. if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
  1269. ret = 0;
  1270. break;
  1271. }
  1272. IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
  1273. ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
  1274. break;
  1275. default:
  1276. ret = -EINVAL;
  1277. }
  1278. mutex_unlock(&mvm->mutex);
  1279. return ret;
  1280. }
  1281. static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
  1282. struct ieee80211_vif *vif,
  1283. struct ieee80211_key_conf *keyconf,
  1284. struct ieee80211_sta *sta,
  1285. u32 iv32, u16 *phase1key)
  1286. {
  1287. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1288. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  1289. return;
  1290. iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
  1291. }
  1292. static int iwl_mvm_roc(struct ieee80211_hw *hw,
  1293. struct ieee80211_vif *vif,
  1294. struct ieee80211_channel *channel,
  1295. int duration,
  1296. enum ieee80211_roc_type type)
  1297. {
  1298. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1299. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1300. struct cfg80211_chan_def chandef;
  1301. struct iwl_mvm_phy_ctxt *phy_ctxt;
  1302. int ret, i;
  1303. IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
  1304. duration, type);
  1305. if (vif->type != NL80211_IFTYPE_P2P_DEVICE) {
  1306. IWL_ERR(mvm, "vif isn't a P2P_DEVICE: %d\n", vif->type);
  1307. return -EINVAL;
  1308. }
  1309. mutex_lock(&mvm->mutex);
  1310. for (i = 0; i < NUM_PHY_CTX; i++) {
  1311. phy_ctxt = &mvm->phy_ctxts[i];
  1312. if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
  1313. continue;
  1314. if (phy_ctxt->ref && channel == phy_ctxt->channel) {
  1315. /*
  1316. * Unbind the P2P_DEVICE from the current PHY context,
  1317. * and if the PHY context is not used remove it.
  1318. */
  1319. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  1320. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  1321. goto out_unlock;
  1322. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  1323. /* Bind the P2P_DEVICE to the current PHY Context */
  1324. mvmvif->phy_ctxt = phy_ctxt;
  1325. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1326. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  1327. goto out_unlock;
  1328. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  1329. goto schedule_time_event;
  1330. }
  1331. }
  1332. /* Need to update the PHY context only if the ROC channel changed */
  1333. if (channel == mvmvif->phy_ctxt->channel)
  1334. goto schedule_time_event;
  1335. cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
  1336. /*
  1337. * Change the PHY context configuration as it is currently referenced
  1338. * only by the P2P Device MAC
  1339. */
  1340. if (mvmvif->phy_ctxt->ref == 1) {
  1341. ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
  1342. &chandef, 1, 1);
  1343. if (ret)
  1344. goto out_unlock;
  1345. } else {
  1346. /*
  1347. * The PHY context is shared with other MACs. Need to remove the
  1348. * P2P Device from the binding, allocate an new PHY context and
  1349. * create a new binding
  1350. */
  1351. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  1352. if (!phy_ctxt) {
  1353. ret = -ENOSPC;
  1354. goto out_unlock;
  1355. }
  1356. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
  1357. 1, 1);
  1358. if (ret) {
  1359. IWL_ERR(mvm, "Failed to change PHY context\n");
  1360. goto out_unlock;
  1361. }
  1362. /* Unbind the P2P_DEVICE from the current PHY context */
  1363. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  1364. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  1365. goto out_unlock;
  1366. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  1367. /* Bind the P2P_DEVICE to the new allocated PHY context */
  1368. mvmvif->phy_ctxt = phy_ctxt;
  1369. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1370. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  1371. goto out_unlock;
  1372. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  1373. }
  1374. schedule_time_event:
  1375. /* Schedule the time events */
  1376. ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
  1377. out_unlock:
  1378. mutex_unlock(&mvm->mutex);
  1379. IWL_DEBUG_MAC80211(mvm, "leave\n");
  1380. return ret;
  1381. }
  1382. static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
  1383. {
  1384. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1385. IWL_DEBUG_MAC80211(mvm, "enter\n");
  1386. mutex_lock(&mvm->mutex);
  1387. iwl_mvm_stop_p2p_roc(mvm);
  1388. mutex_unlock(&mvm->mutex);
  1389. IWL_DEBUG_MAC80211(mvm, "leave\n");
  1390. return 0;
  1391. }
  1392. static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
  1393. struct ieee80211_chanctx_conf *ctx)
  1394. {
  1395. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1396. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1397. struct iwl_mvm_phy_ctxt *phy_ctxt;
  1398. int ret;
  1399. IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
  1400. mutex_lock(&mvm->mutex);
  1401. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  1402. if (!phy_ctxt) {
  1403. ret = -ENOSPC;
  1404. goto out;
  1405. }
  1406. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
  1407. ctx->rx_chains_static,
  1408. ctx->rx_chains_dynamic);
  1409. if (ret) {
  1410. IWL_ERR(mvm, "Failed to add PHY context\n");
  1411. goto out;
  1412. }
  1413. iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
  1414. *phy_ctxt_id = phy_ctxt->id;
  1415. out:
  1416. mutex_unlock(&mvm->mutex);
  1417. return ret;
  1418. }
  1419. static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
  1420. struct ieee80211_chanctx_conf *ctx)
  1421. {
  1422. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1423. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1424. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  1425. mutex_lock(&mvm->mutex);
  1426. iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
  1427. mutex_unlock(&mvm->mutex);
  1428. }
  1429. static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
  1430. struct ieee80211_chanctx_conf *ctx,
  1431. u32 changed)
  1432. {
  1433. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1434. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1435. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  1436. if (WARN_ONCE((phy_ctxt->ref > 1) &&
  1437. (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
  1438. IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
  1439. IEEE80211_CHANCTX_CHANGE_RADAR |
  1440. IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
  1441. "Cannot change PHY. Ref=%d, changed=0x%X\n",
  1442. phy_ctxt->ref, changed))
  1443. return;
  1444. mutex_lock(&mvm->mutex);
  1445. iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
  1446. ctx->rx_chains_static,
  1447. ctx->rx_chains_dynamic);
  1448. iwl_mvm_bt_coex_vif_change(mvm);
  1449. mutex_unlock(&mvm->mutex);
  1450. }
  1451. static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
  1452. struct ieee80211_vif *vif,
  1453. struct ieee80211_chanctx_conf *ctx)
  1454. {
  1455. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1456. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  1457. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  1458. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1459. int ret;
  1460. mutex_lock(&mvm->mutex);
  1461. mvmvif->phy_ctxt = phy_ctxt;
  1462. switch (vif->type) {
  1463. case NL80211_IFTYPE_AP:
  1464. case NL80211_IFTYPE_ADHOC:
  1465. /*
  1466. * The AP binding flow is handled as part of the start_ap flow
  1467. * (in bss_info_changed), similarly for IBSS.
  1468. */
  1469. ret = 0;
  1470. goto out_unlock;
  1471. case NL80211_IFTYPE_STATION:
  1472. case NL80211_IFTYPE_MONITOR:
  1473. break;
  1474. default:
  1475. ret = -EINVAL;
  1476. goto out_unlock;
  1477. }
  1478. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1479. if (ret)
  1480. goto out_unlock;
  1481. /*
  1482. * Power state must be updated before quotas,
  1483. * otherwise fw will complain.
  1484. */
  1485. mvm->bound_vif_cnt++;
  1486. iwl_mvm_power_update_binding(mvm, vif, true);
  1487. /* Setting the quota at this stage is only required for monitor
  1488. * interfaces. For the other types, the bss_info changed flow
  1489. * will handle quota settings.
  1490. */
  1491. if (vif->type == NL80211_IFTYPE_MONITOR) {
  1492. mvmvif->monitor_active = true;
  1493. ret = iwl_mvm_update_quotas(mvm, vif);
  1494. if (ret)
  1495. goto out_remove_binding;
  1496. }
  1497. goto out_unlock;
  1498. out_remove_binding:
  1499. iwl_mvm_binding_remove_vif(mvm, vif);
  1500. mvm->bound_vif_cnt--;
  1501. iwl_mvm_power_update_binding(mvm, vif, false);
  1502. out_unlock:
  1503. mutex_unlock(&mvm->mutex);
  1504. if (ret)
  1505. mvmvif->phy_ctxt = NULL;
  1506. return ret;
  1507. }
  1508. static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1509. struct ieee80211_vif *vif,
  1510. struct ieee80211_chanctx_conf *ctx)
  1511. {
  1512. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1513. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1514. mutex_lock(&mvm->mutex);
  1515. iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
  1516. switch (vif->type) {
  1517. case NL80211_IFTYPE_AP:
  1518. case NL80211_IFTYPE_ADHOC:
  1519. goto out_unlock;
  1520. case NL80211_IFTYPE_MONITOR:
  1521. mvmvif->monitor_active = false;
  1522. iwl_mvm_update_quotas(mvm, NULL);
  1523. break;
  1524. default:
  1525. break;
  1526. }
  1527. iwl_mvm_binding_remove_vif(mvm, vif);
  1528. mvm->bound_vif_cnt--;
  1529. iwl_mvm_power_update_binding(mvm, vif, false);
  1530. out_unlock:
  1531. mvmvif->phy_ctxt = NULL;
  1532. mutex_unlock(&mvm->mutex);
  1533. }
  1534. static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
  1535. struct ieee80211_sta *sta,
  1536. bool set)
  1537. {
  1538. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1539. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  1540. if (!mvm_sta || !mvm_sta->vif) {
  1541. IWL_ERR(mvm, "Station is not associated to a vif\n");
  1542. return -EINVAL;
  1543. }
  1544. return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
  1545. }
  1546. #ifdef CONFIG_NL80211_TESTMODE
  1547. static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
  1548. [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
  1549. [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
  1550. [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
  1551. };
  1552. static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
  1553. struct ieee80211_vif *vif,
  1554. void *data, int len)
  1555. {
  1556. struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
  1557. int err;
  1558. u32 noa_duration;
  1559. err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
  1560. if (err)
  1561. return err;
  1562. if (!tb[IWL_MVM_TM_ATTR_CMD])
  1563. return -EINVAL;
  1564. switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
  1565. case IWL_MVM_TM_CMD_SET_NOA:
  1566. if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
  1567. !vif->bss_conf.enable_beacon ||
  1568. !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
  1569. return -EINVAL;
  1570. noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
  1571. if (noa_duration >= vif->bss_conf.beacon_int)
  1572. return -EINVAL;
  1573. mvm->noa_duration = noa_duration;
  1574. mvm->noa_vif = vif;
  1575. return iwl_mvm_update_quotas(mvm, NULL);
  1576. case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
  1577. /* must be associated client vif - ignore authorized */
  1578. if (!vif || vif->type != NL80211_IFTYPE_STATION ||
  1579. !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
  1580. !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
  1581. return -EINVAL;
  1582. if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
  1583. return iwl_mvm_enable_beacon_filter(mvm, vif);
  1584. return iwl_mvm_disable_beacon_filter(mvm, vif);
  1585. }
  1586. return -EOPNOTSUPP;
  1587. }
  1588. static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
  1589. struct ieee80211_vif *vif,
  1590. void *data, int len)
  1591. {
  1592. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1593. int err;
  1594. mutex_lock(&mvm->mutex);
  1595. err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
  1596. mutex_unlock(&mvm->mutex);
  1597. return err;
  1598. }
  1599. #endif
  1600. struct ieee80211_ops iwl_mvm_hw_ops = {
  1601. .tx = iwl_mvm_mac_tx,
  1602. .ampdu_action = iwl_mvm_mac_ampdu_action,
  1603. .start = iwl_mvm_mac_start,
  1604. .restart_complete = iwl_mvm_mac_restart_complete,
  1605. .stop = iwl_mvm_mac_stop,
  1606. .add_interface = iwl_mvm_mac_add_interface,
  1607. .remove_interface = iwl_mvm_mac_remove_interface,
  1608. .config = iwl_mvm_mac_config,
  1609. .prepare_multicast = iwl_mvm_prepare_multicast,
  1610. .configure_filter = iwl_mvm_configure_filter,
  1611. .bss_info_changed = iwl_mvm_bss_info_changed,
  1612. .hw_scan = iwl_mvm_mac_hw_scan,
  1613. .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
  1614. .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
  1615. .sta_state = iwl_mvm_mac_sta_state,
  1616. .sta_notify = iwl_mvm_mac_sta_notify,
  1617. .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
  1618. .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
  1619. .sta_rc_update = iwl_mvm_sta_rc_update,
  1620. .conf_tx = iwl_mvm_mac_conf_tx,
  1621. .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
  1622. .sched_scan_start = iwl_mvm_mac_sched_scan_start,
  1623. .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
  1624. .set_key = iwl_mvm_mac_set_key,
  1625. .update_tkip_key = iwl_mvm_mac_update_tkip_key,
  1626. .remain_on_channel = iwl_mvm_roc,
  1627. .cancel_remain_on_channel = iwl_mvm_cancel_roc,
  1628. .add_chanctx = iwl_mvm_add_chanctx,
  1629. .remove_chanctx = iwl_mvm_remove_chanctx,
  1630. .change_chanctx = iwl_mvm_change_chanctx,
  1631. .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
  1632. .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
  1633. .start_ap = iwl_mvm_start_ap_ibss,
  1634. .stop_ap = iwl_mvm_stop_ap_ibss,
  1635. .join_ibss = iwl_mvm_start_ap_ibss,
  1636. .leave_ibss = iwl_mvm_stop_ap_ibss,
  1637. .set_tim = iwl_mvm_set_tim,
  1638. CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
  1639. #ifdef CONFIG_PM_SLEEP
  1640. /* look at d3.c */
  1641. .suspend = iwl_mvm_suspend,
  1642. .resume = iwl_mvm_resume,
  1643. .set_wakeup = iwl_mvm_set_wakeup,
  1644. .set_rekey_data = iwl_mvm_set_rekey_data,
  1645. #if IS_ENABLED(CONFIG_IPV6)
  1646. .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
  1647. #endif
  1648. .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
  1649. #endif
  1650. };