mac80211.c 65 KB

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