mac80211.c 42 KB

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