mac80211.c 67 KB

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