mac80211.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084
  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. #include "iwl-fw-error-dump.h"
  83. #include "iwl-prph.h"
  84. static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
  85. {
  86. .max = 1,
  87. .types = BIT(NL80211_IFTYPE_STATION),
  88. },
  89. {
  90. .max = 1,
  91. .types = BIT(NL80211_IFTYPE_AP) |
  92. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  93. BIT(NL80211_IFTYPE_P2P_GO),
  94. },
  95. {
  96. .max = 1,
  97. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  98. },
  99. };
  100. static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
  101. {
  102. .num_different_channels = 1,
  103. .max_interfaces = 3,
  104. .limits = iwl_mvm_limits,
  105. .n_limits = ARRAY_SIZE(iwl_mvm_limits),
  106. },
  107. };
  108. #ifdef CONFIG_PM_SLEEP
  109. static const struct nl80211_wowlan_tcp_data_token_feature
  110. iwl_mvm_wowlan_tcp_token_feature = {
  111. .min_len = 0,
  112. .max_len = 255,
  113. .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
  114. };
  115. static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
  116. .tok = &iwl_mvm_wowlan_tcp_token_feature,
  117. .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
  118. sizeof(struct ethhdr) -
  119. sizeof(struct iphdr) -
  120. sizeof(struct tcphdr),
  121. .data_interval_max = 65535, /* __le16 in API */
  122. .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
  123. sizeof(struct ethhdr) -
  124. sizeof(struct iphdr) -
  125. sizeof(struct tcphdr),
  126. .seq = true,
  127. };
  128. #endif
  129. #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
  130. /*
  131. * Use the reserved field to indicate magic values.
  132. * these values will only be used internally by the driver,
  133. * and won't make it to the fw (reserved will be 0).
  134. * BC_FILTER_MAGIC_IP - configure the val of this attribute to
  135. * be the vif's ip address. in case there is not a single
  136. * ip address (0, or more than 1), this attribute will
  137. * be skipped.
  138. * BC_FILTER_MAGIC_MAC - set the val of this attribute to
  139. * the LSB bytes of the vif's mac address
  140. */
  141. enum {
  142. BC_FILTER_MAGIC_NONE = 0,
  143. BC_FILTER_MAGIC_IP,
  144. BC_FILTER_MAGIC_MAC,
  145. };
  146. static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
  147. {
  148. /* arp */
  149. .discard = 0,
  150. .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
  151. .attrs = {
  152. {
  153. /* frame type - arp, hw type - ethernet */
  154. .offset_type =
  155. BCAST_FILTER_OFFSET_PAYLOAD_START,
  156. .offset = sizeof(rfc1042_header),
  157. .val = cpu_to_be32(0x08060001),
  158. .mask = cpu_to_be32(0xffffffff),
  159. },
  160. {
  161. /* arp dest ip */
  162. .offset_type =
  163. BCAST_FILTER_OFFSET_PAYLOAD_START,
  164. .offset = sizeof(rfc1042_header) + 2 +
  165. sizeof(struct arphdr) +
  166. ETH_ALEN + sizeof(__be32) +
  167. ETH_ALEN,
  168. .mask = cpu_to_be32(0xffffffff),
  169. /* mark it as special field */
  170. .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
  171. },
  172. },
  173. },
  174. {
  175. /* dhcp offer bcast */
  176. .discard = 0,
  177. .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
  178. .attrs = {
  179. {
  180. /* udp dest port - 68 (bootp client)*/
  181. .offset_type = BCAST_FILTER_OFFSET_IP_END,
  182. .offset = offsetof(struct udphdr, dest),
  183. .val = cpu_to_be32(0x00440000),
  184. .mask = cpu_to_be32(0xffff0000),
  185. },
  186. {
  187. /* dhcp - lsb bytes of client hw address */
  188. .offset_type = BCAST_FILTER_OFFSET_IP_END,
  189. .offset = 38,
  190. .mask = cpu_to_be32(0xffffffff),
  191. /* mark it as special field */
  192. .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
  193. },
  194. },
  195. },
  196. /* last filter must be empty */
  197. {},
  198. };
  199. #endif
  200. void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
  201. {
  202. if (!iwl_mvm_is_d0i3_supported(mvm))
  203. return;
  204. IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
  205. spin_lock_bh(&mvm->refs_lock);
  206. mvm->refs[ref_type]++;
  207. spin_unlock_bh(&mvm->refs_lock);
  208. iwl_trans_ref(mvm->trans);
  209. }
  210. void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
  211. {
  212. if (!iwl_mvm_is_d0i3_supported(mvm))
  213. return;
  214. IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
  215. spin_lock_bh(&mvm->refs_lock);
  216. WARN_ON(!mvm->refs[ref_type]--);
  217. spin_unlock_bh(&mvm->refs_lock);
  218. iwl_trans_unref(mvm->trans);
  219. }
  220. static void iwl_mvm_unref_all_except(struct iwl_mvm *mvm,
  221. enum iwl_mvm_ref_type except_ref)
  222. {
  223. int i, j;
  224. if (!iwl_mvm_is_d0i3_supported(mvm))
  225. return;
  226. spin_lock_bh(&mvm->refs_lock);
  227. for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
  228. if (except_ref == i || !mvm->refs[i])
  229. continue;
  230. IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d (%d)\n",
  231. i, mvm->refs[i]);
  232. for (j = 0; j < mvm->refs[i]; j++)
  233. iwl_trans_unref(mvm->trans);
  234. mvm->refs[i] = 0;
  235. }
  236. spin_unlock_bh(&mvm->refs_lock);
  237. }
  238. int iwl_mvm_ref_sync(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
  239. {
  240. iwl_mvm_ref(mvm, ref_type);
  241. if (!wait_event_timeout(mvm->d0i3_exit_waitq,
  242. !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status),
  243. HZ)) {
  244. WARN_ON_ONCE(1);
  245. iwl_mvm_unref(mvm, ref_type);
  246. return -EIO;
  247. }
  248. return 0;
  249. }
  250. static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
  251. {
  252. int i;
  253. memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
  254. for (i = 0; i < NUM_PHY_CTX; i++) {
  255. mvm->phy_ctxts[i].id = i;
  256. mvm->phy_ctxts[i].ref = 0;
  257. }
  258. }
  259. static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
  260. {
  261. /* we create the 802.11 header and SSID element */
  262. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
  263. return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
  264. return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
  265. }
  266. int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
  267. {
  268. struct ieee80211_hw *hw = mvm->hw;
  269. int num_mac, ret, i;
  270. /* Tell mac80211 our characteristics */
  271. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  272. IEEE80211_HW_SPECTRUM_MGMT |
  273. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  274. IEEE80211_HW_QUEUE_CONTROL |
  275. IEEE80211_HW_WANT_MONITOR_VIF |
  276. IEEE80211_HW_SUPPORTS_PS |
  277. IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
  278. IEEE80211_HW_AMPDU_AGGREGATION |
  279. IEEE80211_HW_TIMING_BEACON_ONLY |
  280. IEEE80211_HW_CONNECTION_MONITOR |
  281. IEEE80211_HW_CHANCTX_STA_CSA |
  282. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  283. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  284. hw->queues = mvm->first_agg_queue;
  285. hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
  286. hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
  287. IEEE80211_RADIOTAP_MCS_HAVE_STBC;
  288. hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC;
  289. hw->rate_control_algorithm = "iwl-mvm-rs";
  290. /*
  291. * Enable 11w if advertised by firmware and software crypto
  292. * is not enabled (as the firmware will interpret some mgmt
  293. * packets, so enabling it with software crypto isn't safe)
  294. */
  295. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
  296. !iwlwifi_mod_params.sw_crypto)
  297. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  298. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT &&
  299. IWL_UCODE_API(mvm->fw->ucode_ver) >= 9 &&
  300. !iwlwifi_mod_params.uapsd_disable) {
  301. hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
  302. hw->uapsd_queues = IWL_UAPSD_AC_INFO;
  303. hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
  304. }
  305. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  306. hw->flags |= IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS;
  307. hw->sta_data_size = sizeof(struct iwl_mvm_sta);
  308. hw->vif_data_size = sizeof(struct iwl_mvm_vif);
  309. hw->chanctx_data_size = sizeof(u16);
  310. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  311. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  312. BIT(NL80211_IFTYPE_AP) |
  313. BIT(NL80211_IFTYPE_P2P_GO) |
  314. BIT(NL80211_IFTYPE_P2P_DEVICE) |
  315. BIT(NL80211_IFTYPE_ADHOC);
  316. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  317. hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
  318. REGULATORY_DISABLE_BEACON_HINTS;
  319. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
  320. hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  321. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_CSA_FLOW)
  322. hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  323. hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
  324. hw->wiphy->n_iface_combinations =
  325. ARRAY_SIZE(iwl_mvm_iface_combinations);
  326. hw->wiphy->max_remain_on_channel_duration = 10000;
  327. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  328. /* Extract MAC address */
  329. memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
  330. hw->wiphy->addresses = mvm->addresses;
  331. hw->wiphy->n_addresses = 1;
  332. /* Extract additional MAC addresses if available */
  333. num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
  334. min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
  335. for (i = 1; i < num_mac; i++) {
  336. memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
  337. ETH_ALEN);
  338. mvm->addresses[i].addr[5]++;
  339. hw->wiphy->n_addresses++;
  340. }
  341. iwl_mvm_reset_phy_ctxts(mvm);
  342. hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
  343. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  344. if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
  345. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  346. &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  347. if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
  348. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  349. &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  350. hw->wiphy->hw_version = mvm->trans->hw_id;
  351. if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
  352. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  353. else
  354. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  355. if (IWL_UCODE_API(mvm->fw->ucode_ver) >= 10) {
  356. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
  357. hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
  358. hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
  359. /* we create the 802.11 header and zero length SSID IE. */
  360. hw->wiphy->max_sched_scan_ie_len =
  361. SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
  362. }
  363. hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
  364. NL80211_FEATURE_LOW_PRIORITY_SCAN |
  365. NL80211_FEATURE_P2P_GO_OPPPS;
  366. mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  367. /* currently FW API supports only one optional cipher scheme */
  368. if (mvm->fw->cs[0].cipher) {
  369. mvm->hw->n_cipher_schemes = 1;
  370. mvm->hw->cipher_schemes = &mvm->fw->cs[0];
  371. }
  372. #ifdef CONFIG_PM_SLEEP
  373. if (iwl_mvm_is_d0i3_supported(mvm) &&
  374. device_can_wakeup(mvm->trans->dev)) {
  375. mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
  376. hw->wiphy->wowlan = &mvm->wowlan;
  377. } else if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
  378. mvm->trans->ops->d3_suspend &&
  379. mvm->trans->ops->d3_resume &&
  380. device_can_wakeup(mvm->trans->dev)) {
  381. mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  382. WIPHY_WOWLAN_DISCONNECT |
  383. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  384. WIPHY_WOWLAN_RFKILL_RELEASE;
  385. if (!iwlwifi_mod_params.sw_crypto)
  386. mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  387. WIPHY_WOWLAN_GTK_REKEY_FAILURE |
  388. WIPHY_WOWLAN_4WAY_HANDSHAKE;
  389. mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
  390. mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
  391. mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
  392. mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
  393. hw->wiphy->wowlan = &mvm->wowlan;
  394. }
  395. #endif
  396. #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
  397. /* assign default bcast filtering configuration */
  398. mvm->bcast_filters = iwl_mvm_default_bcast_filters;
  399. #endif
  400. ret = iwl_mvm_leds_init(mvm);
  401. if (ret)
  402. return ret;
  403. ret = ieee80211_register_hw(mvm->hw);
  404. if (ret)
  405. iwl_mvm_leds_exit(mvm);
  406. return ret;
  407. }
  408. static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
  409. struct ieee80211_sta *sta,
  410. struct sk_buff *skb)
  411. {
  412. struct iwl_mvm_sta *mvmsta;
  413. bool defer = false;
  414. /*
  415. * double check the IN_D0I3 flag both before and after
  416. * taking the spinlock, in order to prevent taking
  417. * the spinlock when not needed.
  418. */
  419. if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
  420. return false;
  421. spin_lock(&mvm->d0i3_tx_lock);
  422. /*
  423. * testing the flag again ensures the skb dequeue
  424. * loop (on d0i3 exit) hasn't run yet.
  425. */
  426. if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
  427. goto out;
  428. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  429. if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
  430. mvmsta->sta_id != mvm->d0i3_ap_sta_id)
  431. goto out;
  432. __skb_queue_tail(&mvm->d0i3_tx, skb);
  433. ieee80211_stop_queues(mvm->hw);
  434. /* trigger wakeup */
  435. iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
  436. iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
  437. defer = true;
  438. out:
  439. spin_unlock(&mvm->d0i3_tx_lock);
  440. return defer;
  441. }
  442. static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
  443. struct ieee80211_tx_control *control,
  444. struct sk_buff *skb)
  445. {
  446. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  447. struct ieee80211_sta *sta = control->sta;
  448. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  449. struct ieee80211_hdr *hdr = (void *)skb->data;
  450. if (iwl_mvm_is_radio_killed(mvm)) {
  451. IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
  452. goto drop;
  453. }
  454. if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
  455. !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
  456. goto drop;
  457. /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
  458. if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
  459. ieee80211_is_mgmt(hdr->frame_control) &&
  460. !ieee80211_is_deauth(hdr->frame_control) &&
  461. !ieee80211_is_disassoc(hdr->frame_control) &&
  462. !ieee80211_is_action(hdr->frame_control)))
  463. sta = NULL;
  464. if (sta) {
  465. if (iwl_mvm_defer_tx(mvm, sta, skb))
  466. return;
  467. if (iwl_mvm_tx_skb(mvm, skb, sta))
  468. goto drop;
  469. return;
  470. }
  471. if (iwl_mvm_tx_skb_non_sta(mvm, skb))
  472. goto drop;
  473. return;
  474. drop:
  475. ieee80211_free_txskb(hw, skb);
  476. }
  477. static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
  478. {
  479. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
  480. return false;
  481. return true;
  482. }
  483. static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
  484. {
  485. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
  486. return false;
  487. if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
  488. return true;
  489. /* enabled by default */
  490. return true;
  491. }
  492. static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
  493. struct ieee80211_vif *vif,
  494. enum ieee80211_ampdu_mlme_action action,
  495. struct ieee80211_sta *sta, u16 tid,
  496. u16 *ssn, u8 buf_size)
  497. {
  498. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  499. int ret;
  500. bool tx_agg_ref = false;
  501. IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
  502. sta->addr, tid, action);
  503. if (!(mvm->nvm_data->sku_cap_11n_enable))
  504. return -EACCES;
  505. /* return from D0i3 before starting a new Tx aggregation */
  506. switch (action) {
  507. case IEEE80211_AMPDU_TX_START:
  508. case IEEE80211_AMPDU_TX_STOP_CONT:
  509. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  510. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  511. case IEEE80211_AMPDU_TX_OPERATIONAL:
  512. /*
  513. * for tx start, wait synchronously until D0i3 exit to
  514. * get the correct sequence number for the tid.
  515. * additionally, some other ampdu actions use direct
  516. * target access, which is not handled automatically
  517. * by the trans layer (unlike commands), so wait for
  518. * d0i3 exit in these cases as well.
  519. */
  520. ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
  521. if (ret)
  522. return ret;
  523. tx_agg_ref = true;
  524. break;
  525. default:
  526. break;
  527. }
  528. mutex_lock(&mvm->mutex);
  529. switch (action) {
  530. case IEEE80211_AMPDU_RX_START:
  531. if (!iwl_enable_rx_ampdu(mvm->cfg)) {
  532. ret = -EINVAL;
  533. break;
  534. }
  535. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
  536. break;
  537. case IEEE80211_AMPDU_RX_STOP:
  538. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
  539. break;
  540. case IEEE80211_AMPDU_TX_START:
  541. if (!iwl_enable_tx_ampdu(mvm->cfg)) {
  542. ret = -EINVAL;
  543. break;
  544. }
  545. ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
  546. break;
  547. case IEEE80211_AMPDU_TX_STOP_CONT:
  548. ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
  549. break;
  550. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  551. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  552. ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
  553. break;
  554. case IEEE80211_AMPDU_TX_OPERATIONAL:
  555. ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
  556. break;
  557. default:
  558. WARN_ON_ONCE(1);
  559. ret = -EINVAL;
  560. break;
  561. }
  562. mutex_unlock(&mvm->mutex);
  563. /*
  564. * If the tid is marked as started, we won't use it for offloaded
  565. * traffic on the next D0i3 entry. It's safe to unref.
  566. */
  567. if (tx_agg_ref)
  568. iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
  569. return ret;
  570. }
  571. static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
  572. struct ieee80211_vif *vif)
  573. {
  574. struct iwl_mvm *mvm = data;
  575. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  576. mvmvif->uploaded = false;
  577. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  578. /* does this make sense at all? */
  579. mvmvif->color++;
  580. spin_lock_bh(&mvm->time_event_lock);
  581. iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
  582. spin_unlock_bh(&mvm->time_event_lock);
  583. mvmvif->phy_ctxt = NULL;
  584. memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
  585. }
  586. #ifdef CONFIG_IWLWIFI_DEBUGFS
  587. static void iwl_mvm_fw_error_dump(struct iwl_mvm *mvm)
  588. {
  589. struct iwl_fw_error_dump_file *dump_file;
  590. struct iwl_fw_error_dump_data *dump_data;
  591. struct iwl_fw_error_dump_info *dump_info;
  592. struct iwl_mvm_dump_ptrs *fw_error_dump;
  593. const struct fw_img *img;
  594. u32 sram_len, sram_ofs;
  595. u32 file_len, rxf_len;
  596. unsigned long flags;
  597. int reg_val;
  598. lockdep_assert_held(&mvm->mutex);
  599. if (mvm->fw_error_dump)
  600. return;
  601. fw_error_dump = kzalloc(sizeof(*mvm->fw_error_dump), GFP_KERNEL);
  602. if (!fw_error_dump)
  603. return;
  604. img = &mvm->fw->img[mvm->cur_ucode];
  605. sram_ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  606. sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
  607. /* reading buffer size */
  608. reg_val = iwl_trans_read_prph(mvm->trans, RXF_SIZE_ADDR);
  609. rxf_len = (reg_val & RXF_SIZE_BYTE_CNT_MSK) >> RXF_SIZE_BYTE_CND_POS;
  610. /* the register holds the value divided by 128 */
  611. rxf_len = rxf_len << 7;
  612. file_len = sizeof(*dump_file) +
  613. sizeof(*dump_data) * 3 +
  614. sram_len +
  615. rxf_len +
  616. sizeof(*dump_info);
  617. dump_file = vzalloc(file_len);
  618. if (!dump_file) {
  619. kfree(fw_error_dump);
  620. return;
  621. }
  622. fw_error_dump->op_mode_ptr = dump_file;
  623. dump_file->barker = cpu_to_le32(IWL_FW_ERROR_DUMP_BARKER);
  624. dump_data = (void *)dump_file->data;
  625. dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_DEV_FW_INFO);
  626. dump_data->len = cpu_to_le32(sizeof(*dump_info));
  627. dump_info = (void *) dump_data->data;
  628. dump_info->device_family =
  629. mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000 ?
  630. cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_7) :
  631. cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_8);
  632. memcpy(dump_info->fw_human_readable, mvm->fw->human_readable,
  633. sizeof(dump_info->fw_human_readable));
  634. strncpy(dump_info->dev_human_readable, mvm->cfg->name,
  635. sizeof(dump_info->dev_human_readable));
  636. strncpy(dump_info->bus_human_readable, mvm->dev->bus->name,
  637. sizeof(dump_info->bus_human_readable));
  638. dump_data = iwl_fw_error_next_data(dump_data);
  639. dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RXF);
  640. dump_data->len = cpu_to_le32(rxf_len);
  641. if (iwl_trans_grab_nic_access(mvm->trans, false, &flags)) {
  642. u32 *rxf = (void *)dump_data->data;
  643. int i;
  644. for (i = 0; i < (rxf_len / sizeof(u32)); i++) {
  645. iwl_trans_write_prph(mvm->trans,
  646. RXF_LD_FENCE_OFFSET_ADDR,
  647. i * sizeof(u32));
  648. rxf[i] = iwl_trans_read_prph(mvm->trans,
  649. RXF_FIFO_RD_FENCE_ADDR);
  650. }
  651. iwl_trans_release_nic_access(mvm->trans, &flags);
  652. }
  653. dump_data = iwl_fw_error_next_data(dump_data);
  654. dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_SRAM);
  655. dump_data->len = cpu_to_le32(sram_len);
  656. iwl_trans_read_mem_bytes(mvm->trans, sram_ofs, dump_data->data,
  657. sram_len);
  658. fw_error_dump->trans_ptr = iwl_trans_dump_data(mvm->trans);
  659. fw_error_dump->op_mode_len = file_len;
  660. if (fw_error_dump->trans_ptr)
  661. file_len += fw_error_dump->trans_ptr->len;
  662. dump_file->file_len = cpu_to_le32(file_len);
  663. mvm->fw_error_dump = fw_error_dump;
  664. }
  665. #endif
  666. static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
  667. {
  668. #ifdef CONFIG_IWLWIFI_DEBUGFS
  669. static char *env[] = { "DRIVER=iwlwifi", "EVENT=error_dump", NULL };
  670. iwl_mvm_fw_error_dump(mvm);
  671. /* notify the userspace about the error we had */
  672. kobject_uevent_env(&mvm->hw->wiphy->dev.kobj, KOBJ_CHANGE, env);
  673. #endif
  674. iwl_trans_stop_device(mvm->trans);
  675. mvm->scan_status = IWL_MVM_SCAN_NONE;
  676. /* just in case one was running */
  677. ieee80211_remain_on_channel_expired(mvm->hw);
  678. ieee80211_iterate_active_interfaces_atomic(
  679. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  680. iwl_mvm_cleanup_iterator, mvm);
  681. mvm->p2p_device_vif = NULL;
  682. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  683. iwl_mvm_reset_phy_ctxts(mvm);
  684. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  685. memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
  686. memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
  687. memset(&mvm->last_bt_notif_old, 0, sizeof(mvm->last_bt_notif_old));
  688. memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
  689. memset(&mvm->last_bt_ci_cmd_old, 0, sizeof(mvm->last_bt_ci_cmd_old));
  690. memset(&mvm->bt_ack_kill_msk, 0, sizeof(mvm->bt_ack_kill_msk));
  691. memset(&mvm->bt_cts_kill_msk, 0, sizeof(mvm->bt_cts_kill_msk));
  692. ieee80211_wake_queues(mvm->hw);
  693. /* cleanup all stale references (scan, roc), but keep the
  694. * ucode_down ref until reconfig is complete */
  695. iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
  696. mvm->vif_count = 0;
  697. mvm->rx_ba_sessions = 0;
  698. }
  699. static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
  700. {
  701. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  702. int ret;
  703. mutex_lock(&mvm->mutex);
  704. /* Clean up some internal and mac80211 state on restart */
  705. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  706. iwl_mvm_restart_cleanup(mvm);
  707. ret = iwl_mvm_up(mvm);
  708. if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  709. /* Something went wrong - we need to finish some cleanup
  710. * that normally iwl_mvm_mac_restart_complete() below
  711. * would do.
  712. */
  713. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  714. iwl_mvm_d0i3_enable_tx(mvm, NULL);
  715. }
  716. mutex_unlock(&mvm->mutex);
  717. return ret;
  718. }
  719. static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
  720. {
  721. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  722. int ret;
  723. mutex_lock(&mvm->mutex);
  724. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  725. iwl_mvm_d0i3_enable_tx(mvm, NULL);
  726. ret = iwl_mvm_update_quotas(mvm, NULL);
  727. if (ret)
  728. IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
  729. ret);
  730. /* allow transport/FW low power modes */
  731. iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
  732. mutex_unlock(&mvm->mutex);
  733. }
  734. static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
  735. {
  736. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  737. flush_work(&mvm->d0i3_exit_work);
  738. flush_work(&mvm->async_handlers_wk);
  739. mutex_lock(&mvm->mutex);
  740. /* disallow low power states when the FW is down */
  741. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  742. /* async_handlers_wk is now blocked */
  743. /*
  744. * The work item could be running or queued if the
  745. * ROC time event stops just as we get here.
  746. */
  747. cancel_work_sync(&mvm->roc_done_wk);
  748. iwl_trans_stop_device(mvm->trans);
  749. iwl_mvm_async_handlers_purge(mvm);
  750. /* async_handlers_list is empty and will stay empty: HW is stopped */
  751. /* the fw is stopped, the aux sta is dead: clean up driver state */
  752. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  753. mutex_unlock(&mvm->mutex);
  754. /*
  755. * The worker might have been waiting for the mutex, let it run and
  756. * discover that its list is now empty.
  757. */
  758. cancel_work_sync(&mvm->async_handlers_wk);
  759. }
  760. static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
  761. {
  762. u16 i;
  763. lockdep_assert_held(&mvm->mutex);
  764. for (i = 0; i < NUM_PHY_CTX; i++)
  765. if (!mvm->phy_ctxts[i].ref)
  766. return &mvm->phy_ctxts[i];
  767. IWL_ERR(mvm, "No available PHY context\n");
  768. return NULL;
  769. }
  770. static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  771. s8 tx_power)
  772. {
  773. /* FW is in charge of regulatory enforcement */
  774. struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
  775. .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
  776. .pwr_restriction = cpu_to_le16(tx_power),
  777. };
  778. return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0,
  779. sizeof(reduce_txpwr_cmd),
  780. &reduce_txpwr_cmd);
  781. }
  782. static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
  783. struct ieee80211_vif *vif)
  784. {
  785. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  786. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  787. int ret;
  788. /*
  789. * make sure D0i3 exit is completed, otherwise a target access
  790. * during tx queue configuration could be done when still in
  791. * D0i3 state.
  792. */
  793. ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
  794. if (ret)
  795. return ret;
  796. /*
  797. * Not much to do here. The stack will not allow interface
  798. * types or combinations that we didn't advertise, so we
  799. * don't really have to check the types.
  800. */
  801. mutex_lock(&mvm->mutex);
  802. /* Allocate resources for the MAC context, and add it to the fw */
  803. ret = iwl_mvm_mac_ctxt_init(mvm, vif);
  804. if (ret)
  805. goto out_unlock;
  806. /* Counting number of interfaces is needed for legacy PM */
  807. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  808. mvm->vif_count++;
  809. /*
  810. * The AP binding flow can be done only after the beacon
  811. * template is configured (which happens only in the mac80211
  812. * start_ap() flow), and adding the broadcast station can happen
  813. * only after the binding.
  814. * In addition, since modifying the MAC before adding a bcast
  815. * station is not allowed by the FW, delay the adding of MAC context to
  816. * the point where we can also add the bcast station.
  817. * In short: there's not much we can do at this point, other than
  818. * allocating resources :)
  819. */
  820. if (vif->type == NL80211_IFTYPE_AP ||
  821. vif->type == NL80211_IFTYPE_ADHOC) {
  822. u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
  823. ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
  824. qmask,
  825. ieee80211_vif_type_p2p(vif));
  826. if (ret) {
  827. IWL_ERR(mvm, "Failed to allocate bcast sta\n");
  828. goto out_release;
  829. }
  830. iwl_mvm_vif_dbgfs_register(mvm, vif);
  831. goto out_unlock;
  832. }
  833. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  834. if (ret)
  835. goto out_release;
  836. ret = iwl_mvm_power_update_mac(mvm);
  837. if (ret)
  838. goto out_release;
  839. /* beacon filtering */
  840. ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
  841. if (ret)
  842. goto out_remove_mac;
  843. if (!mvm->bf_allowed_vif &&
  844. vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
  845. mvm->bf_allowed_vif = mvmvif;
  846. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
  847. IEEE80211_VIF_SUPPORTS_CQM_RSSI;
  848. }
  849. /*
  850. * P2P_DEVICE interface does not have a channel context assigned to it,
  851. * so a dedicated PHY context is allocated to it and the corresponding
  852. * MAC context is bound to it at this stage.
  853. */
  854. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  855. mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  856. if (!mvmvif->phy_ctxt) {
  857. ret = -ENOSPC;
  858. goto out_free_bf;
  859. }
  860. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  861. ret = iwl_mvm_binding_add_vif(mvm, vif);
  862. if (ret)
  863. goto out_unref_phy;
  864. ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
  865. if (ret)
  866. goto out_unbind;
  867. /* Save a pointer to p2p device vif, so it can later be used to
  868. * update the p2p device MAC when a GO is started/stopped */
  869. mvm->p2p_device_vif = vif;
  870. }
  871. iwl_mvm_vif_dbgfs_register(mvm, vif);
  872. goto out_unlock;
  873. out_unbind:
  874. iwl_mvm_binding_remove_vif(mvm, vif);
  875. out_unref_phy:
  876. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  877. out_free_bf:
  878. if (mvm->bf_allowed_vif == mvmvif) {
  879. mvm->bf_allowed_vif = NULL;
  880. vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
  881. IEEE80211_VIF_SUPPORTS_CQM_RSSI);
  882. }
  883. out_remove_mac:
  884. mvmvif->phy_ctxt = NULL;
  885. iwl_mvm_mac_ctxt_remove(mvm, vif);
  886. out_release:
  887. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  888. mvm->vif_count--;
  889. iwl_mvm_mac_ctxt_release(mvm, vif);
  890. out_unlock:
  891. mutex_unlock(&mvm->mutex);
  892. iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
  893. return ret;
  894. }
  895. static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
  896. struct ieee80211_vif *vif)
  897. {
  898. u32 tfd_msk = 0, ac;
  899. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  900. if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  901. tfd_msk |= BIT(vif->hw_queue[ac]);
  902. if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
  903. tfd_msk |= BIT(vif->cab_queue);
  904. if (tfd_msk) {
  905. mutex_lock(&mvm->mutex);
  906. iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
  907. mutex_unlock(&mvm->mutex);
  908. }
  909. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  910. /*
  911. * Flush the ROC worker which will flush the OFFCHANNEL queue.
  912. * We assume here that all the packets sent to the OFFCHANNEL
  913. * queue are sent in ROC session.
  914. */
  915. flush_work(&mvm->roc_done_wk);
  916. } else {
  917. /*
  918. * By now, all the AC queues are empty. The AGG queues are
  919. * empty too. We already got all the Tx responses for all the
  920. * packets in the queues. The drain work can have been
  921. * triggered. Flush it.
  922. */
  923. flush_work(&mvm->sta_drained_wk);
  924. }
  925. }
  926. static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
  927. struct ieee80211_vif *vif)
  928. {
  929. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  930. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  931. iwl_mvm_prepare_mac_removal(mvm, vif);
  932. mutex_lock(&mvm->mutex);
  933. if (mvm->bf_allowed_vif == mvmvif) {
  934. mvm->bf_allowed_vif = NULL;
  935. vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
  936. IEEE80211_VIF_SUPPORTS_CQM_RSSI);
  937. }
  938. iwl_mvm_vif_dbgfs_clean(mvm, vif);
  939. /*
  940. * For AP/GO interface, the tear down of the resources allocated to the
  941. * interface is be handled as part of the stop_ap flow.
  942. */
  943. if (vif->type == NL80211_IFTYPE_AP ||
  944. vif->type == NL80211_IFTYPE_ADHOC) {
  945. #ifdef CONFIG_NL80211_TESTMODE
  946. if (vif == mvm->noa_vif) {
  947. mvm->noa_vif = NULL;
  948. mvm->noa_duration = 0;
  949. }
  950. #endif
  951. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  952. goto out_release;
  953. }
  954. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  955. mvm->p2p_device_vif = NULL;
  956. iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  957. iwl_mvm_binding_remove_vif(mvm, vif);
  958. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  959. mvmvif->phy_ctxt = NULL;
  960. }
  961. if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
  962. mvm->vif_count--;
  963. iwl_mvm_power_update_mac(mvm);
  964. iwl_mvm_mac_ctxt_remove(mvm, vif);
  965. out_release:
  966. iwl_mvm_mac_ctxt_release(mvm, vif);
  967. mutex_unlock(&mvm->mutex);
  968. }
  969. static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
  970. {
  971. return 0;
  972. }
  973. struct iwl_mvm_mc_iter_data {
  974. struct iwl_mvm *mvm;
  975. int port_id;
  976. };
  977. static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
  978. struct ieee80211_vif *vif)
  979. {
  980. struct iwl_mvm_mc_iter_data *data = _data;
  981. struct iwl_mvm *mvm = data->mvm;
  982. struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
  983. int ret, len;
  984. /* if we don't have free ports, mcast frames will be dropped */
  985. if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
  986. return;
  987. if (vif->type != NL80211_IFTYPE_STATION ||
  988. !vif->bss_conf.assoc)
  989. return;
  990. cmd->port_id = data->port_id++;
  991. memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
  992. len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
  993. ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
  994. if (ret)
  995. IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
  996. }
  997. static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
  998. {
  999. struct iwl_mvm_mc_iter_data iter_data = {
  1000. .mvm = mvm,
  1001. };
  1002. lockdep_assert_held(&mvm->mutex);
  1003. if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
  1004. return;
  1005. ieee80211_iterate_active_interfaces_atomic(
  1006. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1007. iwl_mvm_mc_iface_iterator, &iter_data);
  1008. }
  1009. static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
  1010. struct netdev_hw_addr_list *mc_list)
  1011. {
  1012. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1013. struct iwl_mcast_filter_cmd *cmd;
  1014. struct netdev_hw_addr *addr;
  1015. int addr_count = netdev_hw_addr_list_count(mc_list);
  1016. bool pass_all = false;
  1017. int len;
  1018. if (addr_count > MAX_MCAST_FILTERING_ADDRESSES) {
  1019. pass_all = true;
  1020. addr_count = 0;
  1021. }
  1022. len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
  1023. cmd = kzalloc(len, GFP_ATOMIC);
  1024. if (!cmd)
  1025. return 0;
  1026. if (pass_all) {
  1027. cmd->pass_all = 1;
  1028. return (u64)(unsigned long)cmd;
  1029. }
  1030. netdev_hw_addr_list_for_each(addr, mc_list) {
  1031. IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
  1032. cmd->count, addr->addr);
  1033. memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
  1034. addr->addr, ETH_ALEN);
  1035. cmd->count++;
  1036. }
  1037. return (u64)(unsigned long)cmd;
  1038. }
  1039. static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
  1040. unsigned int changed_flags,
  1041. unsigned int *total_flags,
  1042. u64 multicast)
  1043. {
  1044. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1045. struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
  1046. mutex_lock(&mvm->mutex);
  1047. /* replace previous configuration */
  1048. kfree(mvm->mcast_filter_cmd);
  1049. mvm->mcast_filter_cmd = cmd;
  1050. if (!cmd)
  1051. goto out;
  1052. iwl_mvm_recalc_multicast(mvm);
  1053. out:
  1054. mutex_unlock(&mvm->mutex);
  1055. *total_flags = 0;
  1056. }
  1057. #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
  1058. struct iwl_bcast_iter_data {
  1059. struct iwl_mvm *mvm;
  1060. struct iwl_bcast_filter_cmd *cmd;
  1061. u8 current_filter;
  1062. };
  1063. static void
  1064. iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
  1065. const struct iwl_fw_bcast_filter *in_filter,
  1066. struct iwl_fw_bcast_filter *out_filter)
  1067. {
  1068. struct iwl_fw_bcast_filter_attr *attr;
  1069. int i;
  1070. memcpy(out_filter, in_filter, sizeof(*out_filter));
  1071. for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
  1072. attr = &out_filter->attrs[i];
  1073. if (!attr->mask)
  1074. break;
  1075. switch (attr->reserved1) {
  1076. case cpu_to_le16(BC_FILTER_MAGIC_IP):
  1077. if (vif->bss_conf.arp_addr_cnt != 1) {
  1078. attr->mask = 0;
  1079. continue;
  1080. }
  1081. attr->val = vif->bss_conf.arp_addr_list[0];
  1082. break;
  1083. case cpu_to_le16(BC_FILTER_MAGIC_MAC):
  1084. attr->val = *(__be32 *)&vif->addr[2];
  1085. break;
  1086. default:
  1087. break;
  1088. }
  1089. attr->reserved1 = 0;
  1090. out_filter->num_attrs++;
  1091. }
  1092. }
  1093. static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
  1094. struct ieee80211_vif *vif)
  1095. {
  1096. struct iwl_bcast_iter_data *data = _data;
  1097. struct iwl_mvm *mvm = data->mvm;
  1098. struct iwl_bcast_filter_cmd *cmd = data->cmd;
  1099. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1100. struct iwl_fw_bcast_mac *bcast_mac;
  1101. int i;
  1102. if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
  1103. return;
  1104. bcast_mac = &cmd->macs[mvmvif->id];
  1105. /*
  1106. * enable filtering only for associated stations, but not for P2P
  1107. * Clients
  1108. */
  1109. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
  1110. !vif->bss_conf.assoc)
  1111. return;
  1112. bcast_mac->default_discard = 1;
  1113. /* copy all configured filters */
  1114. for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
  1115. /*
  1116. * Make sure we don't exceed our filters limit.
  1117. * if there is still a valid filter to be configured,
  1118. * be on the safe side and just allow bcast for this mac.
  1119. */
  1120. if (WARN_ON_ONCE(data->current_filter >=
  1121. ARRAY_SIZE(cmd->filters))) {
  1122. bcast_mac->default_discard = 0;
  1123. bcast_mac->attached_filters = 0;
  1124. break;
  1125. }
  1126. iwl_mvm_set_bcast_filter(vif,
  1127. &mvm->bcast_filters[i],
  1128. &cmd->filters[data->current_filter]);
  1129. /* skip current filter if it contains no attributes */
  1130. if (!cmd->filters[data->current_filter].num_attrs)
  1131. continue;
  1132. /* attach the filter to current mac */
  1133. bcast_mac->attached_filters |=
  1134. cpu_to_le16(BIT(data->current_filter));
  1135. data->current_filter++;
  1136. }
  1137. }
  1138. bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
  1139. struct iwl_bcast_filter_cmd *cmd)
  1140. {
  1141. struct iwl_bcast_iter_data iter_data = {
  1142. .mvm = mvm,
  1143. .cmd = cmd,
  1144. };
  1145. memset(cmd, 0, sizeof(*cmd));
  1146. cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
  1147. cmd->max_macs = ARRAY_SIZE(cmd->macs);
  1148. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1149. /* use debugfs filters/macs if override is configured */
  1150. if (mvm->dbgfs_bcast_filtering.override) {
  1151. memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
  1152. sizeof(cmd->filters));
  1153. memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
  1154. sizeof(cmd->macs));
  1155. return true;
  1156. }
  1157. #endif
  1158. /* if no filters are configured, do nothing */
  1159. if (!mvm->bcast_filters)
  1160. return false;
  1161. /* configure and attach these filters for each associated sta vif */
  1162. ieee80211_iterate_active_interfaces(
  1163. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1164. iwl_mvm_bcast_filter_iterator, &iter_data);
  1165. return true;
  1166. }
  1167. static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
  1168. struct ieee80211_vif *vif)
  1169. {
  1170. struct iwl_bcast_filter_cmd cmd;
  1171. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
  1172. return 0;
  1173. if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
  1174. return 0;
  1175. return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
  1176. sizeof(cmd), &cmd);
  1177. }
  1178. #else
  1179. static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
  1180. struct ieee80211_vif *vif)
  1181. {
  1182. return 0;
  1183. }
  1184. #endif
  1185. static void iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
  1186. {
  1187. struct ieee80211_sta *sta;
  1188. struct iwl_mvm_sta *mvmsta;
  1189. int i;
  1190. lockdep_assert_held(&mvm->mutex);
  1191. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  1192. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  1193. lockdep_is_held(&mvm->mutex));
  1194. if (!sta || IS_ERR(sta) || !sta->tdls)
  1195. continue;
  1196. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1197. ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
  1198. NL80211_TDLS_TEARDOWN,
  1199. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
  1200. GFP_KERNEL);
  1201. }
  1202. }
  1203. static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
  1204. struct ieee80211_vif *vif,
  1205. struct ieee80211_bss_conf *bss_conf,
  1206. u32 changes)
  1207. {
  1208. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1209. int ret;
  1210. /*
  1211. * Re-calculate the tsf id, as the master-slave relations depend on the
  1212. * beacon interval, which was not known when the station interface was
  1213. * added.
  1214. */
  1215. if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
  1216. iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
  1217. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
  1218. if (ret)
  1219. IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
  1220. if (changes & BSS_CHANGED_ASSOC) {
  1221. if (bss_conf->assoc) {
  1222. /* add quota for this interface */
  1223. ret = iwl_mvm_update_quotas(mvm, NULL);
  1224. if (ret) {
  1225. IWL_ERR(mvm, "failed to update quotas\n");
  1226. return;
  1227. }
  1228. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
  1229. &mvm->status)) {
  1230. /*
  1231. * If we're restarting then the firmware will
  1232. * obviously have lost synchronisation with
  1233. * the AP. It will attempt to synchronise by
  1234. * itself, but we can make it more reliable by
  1235. * scheduling a session protection time event.
  1236. *
  1237. * The firmware needs to receive a beacon to
  1238. * catch up with synchronisation, use 110% of
  1239. * the beacon interval.
  1240. *
  1241. * Set a large maximum delay to allow for more
  1242. * than a single interface.
  1243. */
  1244. u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
  1245. iwl_mvm_protect_session(mvm, vif, dur, dur,
  1246. 5 * dur);
  1247. }
  1248. iwl_mvm_sf_update(mvm, vif, false);
  1249. iwl_mvm_power_vif_assoc(mvm, vif);
  1250. if (vif->p2p)
  1251. iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
  1252. } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  1253. /*
  1254. * If update fails - SF might be running in associated
  1255. * mode while disassociated - which is forbidden.
  1256. */
  1257. WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
  1258. "Failed to update SF upon disassociation\n");
  1259. /* remove AP station now that the MAC is unassoc */
  1260. ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
  1261. if (ret)
  1262. IWL_ERR(mvm, "failed to remove AP station\n");
  1263. if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
  1264. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  1265. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  1266. /* remove quota for this interface */
  1267. ret = iwl_mvm_update_quotas(mvm, NULL);
  1268. if (ret)
  1269. IWL_ERR(mvm, "failed to update quotas\n");
  1270. if (vif->p2p)
  1271. iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
  1272. }
  1273. iwl_mvm_recalc_multicast(mvm);
  1274. iwl_mvm_configure_bcast_filter(mvm, vif);
  1275. /* reset rssi values */
  1276. mvmvif->bf_data.ave_beacon_signal = 0;
  1277. iwl_mvm_bt_coex_vif_change(mvm);
  1278. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
  1279. IEEE80211_SMPS_AUTOMATIC);
  1280. } else if (changes & BSS_CHANGED_BEACON_INFO) {
  1281. /*
  1282. * We received a beacon _after_ association so
  1283. * remove the session protection.
  1284. */
  1285. iwl_mvm_remove_time_event(mvm, mvmvif,
  1286. &mvmvif->time_event_data);
  1287. }
  1288. if (changes & BSS_CHANGED_BEACON_INFO) {
  1289. iwl_mvm_sf_update(mvm, vif, false);
  1290. WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
  1291. }
  1292. if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS)) {
  1293. ret = iwl_mvm_power_update_mac(mvm);
  1294. if (ret)
  1295. IWL_ERR(mvm, "failed to update power mode\n");
  1296. }
  1297. if (changes & BSS_CHANGED_TXPOWER) {
  1298. IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
  1299. bss_conf->txpower);
  1300. iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
  1301. }
  1302. if (changes & BSS_CHANGED_CQM) {
  1303. IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
  1304. /* reset cqm events tracking */
  1305. mvmvif->bf_data.last_cqm_event = 0;
  1306. if (mvmvif->bf_data.bf_enabled) {
  1307. ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
  1308. if (ret)
  1309. IWL_ERR(mvm,
  1310. "failed to update CQM thresholds\n");
  1311. }
  1312. }
  1313. if (changes & BSS_CHANGED_ARP_FILTER) {
  1314. IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
  1315. iwl_mvm_configure_bcast_filter(mvm, vif);
  1316. }
  1317. }
  1318. static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
  1319. struct ieee80211_vif *vif)
  1320. {
  1321. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1322. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1323. int ret;
  1324. /*
  1325. * iwl_mvm_mac_ctxt_add() might read directly from the device
  1326. * (the system time), so make sure it is available.
  1327. */
  1328. ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
  1329. if (ret)
  1330. return ret;
  1331. mutex_lock(&mvm->mutex);
  1332. /* Send the beacon template */
  1333. ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
  1334. if (ret)
  1335. goto out_unlock;
  1336. /*
  1337. * Re-calculate the tsf id, as the master-slave relations depend on the
  1338. * beacon interval, which was not known when the AP interface was added.
  1339. */
  1340. if (vif->type == NL80211_IFTYPE_AP)
  1341. iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
  1342. /* Add the mac context */
  1343. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  1344. if (ret)
  1345. goto out_unlock;
  1346. /* Perform the binding */
  1347. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1348. if (ret)
  1349. goto out_remove;
  1350. /* Send the bcast station. At this stage the TBTT and DTIM time events
  1351. * are added and applied to the scheduler */
  1352. ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
  1353. if (ret)
  1354. goto out_unbind;
  1355. /* must be set before quota calculations */
  1356. mvmvif->ap_ibss_active = true;
  1357. /* power updated needs to be done before quotas */
  1358. iwl_mvm_power_update_mac(mvm);
  1359. ret = iwl_mvm_update_quotas(mvm, NULL);
  1360. if (ret)
  1361. goto out_quota_failed;
  1362. /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
  1363. if (vif->p2p && mvm->p2p_device_vif)
  1364. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
  1365. iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
  1366. iwl_mvm_bt_coex_vif_change(mvm);
  1367. /* we don't support TDLS during DCM */
  1368. if (iwl_mvm_phy_ctx_count(mvm) > 1)
  1369. iwl_mvm_teardown_tdls_peers(mvm);
  1370. mutex_unlock(&mvm->mutex);
  1371. return 0;
  1372. out_quota_failed:
  1373. iwl_mvm_power_update_mac(mvm);
  1374. mvmvif->ap_ibss_active = false;
  1375. iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  1376. out_unbind:
  1377. iwl_mvm_binding_remove_vif(mvm, vif);
  1378. out_remove:
  1379. iwl_mvm_mac_ctxt_remove(mvm, vif);
  1380. out_unlock:
  1381. mutex_unlock(&mvm->mutex);
  1382. iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
  1383. return ret;
  1384. }
  1385. static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
  1386. struct ieee80211_vif *vif)
  1387. {
  1388. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1389. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1390. iwl_mvm_prepare_mac_removal(mvm, vif);
  1391. mutex_lock(&mvm->mutex);
  1392. /* Handle AP stop while in CSA */
  1393. if (rcu_access_pointer(mvm->csa_vif) == vif) {
  1394. iwl_mvm_remove_time_event(mvm, mvmvif,
  1395. &mvmvif->time_event_data);
  1396. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  1397. }
  1398. if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
  1399. RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
  1400. mvm->csa_tx_block_bcn_timeout = 0;
  1401. }
  1402. mvmvif->ap_ibss_active = false;
  1403. mvm->ap_last_beacon_gp2 = 0;
  1404. iwl_mvm_bt_coex_vif_change(mvm);
  1405. iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
  1406. /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
  1407. if (vif->p2p && mvm->p2p_device_vif)
  1408. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
  1409. iwl_mvm_update_quotas(mvm, NULL);
  1410. iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
  1411. iwl_mvm_binding_remove_vif(mvm, vif);
  1412. iwl_mvm_power_update_mac(mvm);
  1413. iwl_mvm_mac_ctxt_remove(mvm, vif);
  1414. mutex_unlock(&mvm->mutex);
  1415. }
  1416. static void
  1417. iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
  1418. struct ieee80211_vif *vif,
  1419. struct ieee80211_bss_conf *bss_conf,
  1420. u32 changes)
  1421. {
  1422. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1423. /* Changes will be applied when the AP/IBSS is started */
  1424. if (!mvmvif->ap_ibss_active)
  1425. return;
  1426. if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
  1427. BSS_CHANGED_BANDWIDTH) &&
  1428. iwl_mvm_mac_ctxt_changed(mvm, vif, false))
  1429. IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
  1430. /* Need to send a new beacon template to the FW */
  1431. if (changes & BSS_CHANGED_BEACON &&
  1432. iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
  1433. IWL_WARN(mvm, "Failed updating beacon data\n");
  1434. }
  1435. static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
  1436. struct ieee80211_vif *vif,
  1437. struct ieee80211_bss_conf *bss_conf,
  1438. u32 changes)
  1439. {
  1440. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1441. /*
  1442. * iwl_mvm_bss_info_changed_station() might call
  1443. * iwl_mvm_protect_session(), which reads directly from
  1444. * the device (the system time), so make sure it is available.
  1445. */
  1446. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
  1447. return;
  1448. mutex_lock(&mvm->mutex);
  1449. if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
  1450. iwl_mvm_scan_offload_stop(mvm, true);
  1451. switch (vif->type) {
  1452. case NL80211_IFTYPE_STATION:
  1453. iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
  1454. break;
  1455. case NL80211_IFTYPE_AP:
  1456. case NL80211_IFTYPE_ADHOC:
  1457. iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
  1458. break;
  1459. default:
  1460. /* shouldn't happen */
  1461. WARN_ON_ONCE(1);
  1462. }
  1463. mutex_unlock(&mvm->mutex);
  1464. iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
  1465. }
  1466. static int iwl_mvm_cancel_scan_wait_notif(struct iwl_mvm *mvm,
  1467. enum iwl_scan_status scan_type)
  1468. {
  1469. int ret;
  1470. bool wait_for_handlers = false;
  1471. mutex_lock(&mvm->mutex);
  1472. if (mvm->scan_status != scan_type) {
  1473. ret = 0;
  1474. /* make sure there are no pending notifications */
  1475. wait_for_handlers = true;
  1476. goto out;
  1477. }
  1478. switch (scan_type) {
  1479. case IWL_MVM_SCAN_SCHED:
  1480. ret = iwl_mvm_scan_offload_stop(mvm, true);
  1481. break;
  1482. case IWL_MVM_SCAN_OS:
  1483. ret = iwl_mvm_cancel_scan(mvm);
  1484. break;
  1485. case IWL_MVM_SCAN_NONE:
  1486. default:
  1487. WARN_ON_ONCE(1);
  1488. ret = -EINVAL;
  1489. break;
  1490. }
  1491. if (ret)
  1492. goto out;
  1493. wait_for_handlers = true;
  1494. out:
  1495. mutex_unlock(&mvm->mutex);
  1496. /* make sure we consume the completion notification */
  1497. if (wait_for_handlers)
  1498. iwl_mvm_wait_for_async_handlers(mvm);
  1499. return ret;
  1500. }
  1501. static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
  1502. struct ieee80211_vif *vif,
  1503. struct ieee80211_scan_request *hw_req)
  1504. {
  1505. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1506. struct cfg80211_scan_request *req = &hw_req->req;
  1507. int ret;
  1508. if (req->n_channels == 0 ||
  1509. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1510. return -EINVAL;
  1511. ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_SCHED);
  1512. if (ret)
  1513. return ret;
  1514. mutex_lock(&mvm->mutex);
  1515. if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
  1516. ret = -EBUSY;
  1517. goto out;
  1518. }
  1519. iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
  1520. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1521. ret = iwl_mvm_unified_scan_lmac(mvm, vif, hw_req);
  1522. else
  1523. ret = iwl_mvm_scan_request(mvm, vif, req);
  1524. if (ret)
  1525. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1526. out:
  1527. mutex_unlock(&mvm->mutex);
  1528. return ret;
  1529. }
  1530. static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
  1531. struct ieee80211_vif *vif)
  1532. {
  1533. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1534. mutex_lock(&mvm->mutex);
  1535. iwl_mvm_cancel_scan(mvm);
  1536. mutex_unlock(&mvm->mutex);
  1537. }
  1538. static void
  1539. iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
  1540. struct ieee80211_sta *sta, u16 tids,
  1541. int num_frames,
  1542. enum ieee80211_frame_release_type reason,
  1543. bool more_data)
  1544. {
  1545. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1546. /* Called when we need to transmit (a) frame(s) from mac80211 */
  1547. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
  1548. tids, more_data, false);
  1549. }
  1550. static void
  1551. iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
  1552. struct ieee80211_sta *sta, u16 tids,
  1553. int num_frames,
  1554. enum ieee80211_frame_release_type reason,
  1555. bool more_data)
  1556. {
  1557. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1558. /* Called when we need to transmit (a) frame(s) from agg queue */
  1559. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
  1560. tids, more_data, true);
  1561. }
  1562. static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
  1563. struct ieee80211_vif *vif,
  1564. enum sta_notify_cmd cmd,
  1565. struct ieee80211_sta *sta)
  1566. {
  1567. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1568. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1569. int tid;
  1570. switch (cmd) {
  1571. case STA_NOTIFY_SLEEP:
  1572. if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
  1573. ieee80211_sta_block_awake(hw, sta, true);
  1574. spin_lock_bh(&mvmsta->lock);
  1575. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  1576. struct iwl_mvm_tid_data *tid_data;
  1577. tid_data = &mvmsta->tid_data[tid];
  1578. if (tid_data->state != IWL_AGG_ON &&
  1579. tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
  1580. continue;
  1581. if (iwl_mvm_tid_queued(tid_data) == 0)
  1582. continue;
  1583. ieee80211_sta_set_buffered(sta, tid, true);
  1584. }
  1585. spin_unlock_bh(&mvmsta->lock);
  1586. /*
  1587. * The fw updates the STA to be asleep. Tx packets on the Tx
  1588. * queues to this station will not be transmitted. The fw will
  1589. * send a Tx response with TX_STATUS_FAIL_DEST_PS.
  1590. */
  1591. break;
  1592. case STA_NOTIFY_AWAKE:
  1593. if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
  1594. break;
  1595. iwl_mvm_sta_modify_ps_wake(mvm, sta);
  1596. break;
  1597. default:
  1598. break;
  1599. }
  1600. }
  1601. static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
  1602. struct ieee80211_vif *vif,
  1603. struct ieee80211_sta *sta)
  1604. {
  1605. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1606. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  1607. /*
  1608. * This is called before mac80211 does RCU synchronisation,
  1609. * so here we already invalidate our internal RCU-protected
  1610. * station pointer. The rest of the code will thus no longer
  1611. * be able to find the station this way, and we don't rely
  1612. * on further RCU synchronisation after the sta_state()
  1613. * callback deleted the station.
  1614. */
  1615. mutex_lock(&mvm->mutex);
  1616. if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
  1617. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  1618. ERR_PTR(-ENOENT));
  1619. mutex_unlock(&mvm->mutex);
  1620. }
  1621. int iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1622. {
  1623. struct ieee80211_sta *sta;
  1624. struct iwl_mvm_sta *mvmsta;
  1625. int count = 0;
  1626. int i;
  1627. lockdep_assert_held(&mvm->mutex);
  1628. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  1629. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  1630. lockdep_is_held(&mvm->mutex));
  1631. if (!sta || IS_ERR(sta) || !sta->tdls)
  1632. continue;
  1633. if (vif) {
  1634. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1635. if (mvmsta->vif != vif)
  1636. continue;
  1637. }
  1638. count++;
  1639. }
  1640. return count;
  1641. }
  1642. static void iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm,
  1643. struct ieee80211_vif *vif,
  1644. bool sta_added)
  1645. {
  1646. int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
  1647. /*
  1648. * Disable ps when the first TDLS sta is added and re-enable it
  1649. * when the last TDLS sta is removed
  1650. */
  1651. if ((tdls_sta_cnt == 1 && sta_added) ||
  1652. (tdls_sta_cnt == 0 && !sta_added))
  1653. iwl_mvm_power_update_mac(mvm);
  1654. }
  1655. static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
  1656. struct ieee80211_vif *vif,
  1657. struct ieee80211_sta *sta,
  1658. enum ieee80211_sta_state old_state,
  1659. enum ieee80211_sta_state new_state)
  1660. {
  1661. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1662. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1663. int ret;
  1664. IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
  1665. sta->addr, old_state, new_state);
  1666. /* this would be a mac80211 bug ... but don't crash */
  1667. if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
  1668. return -EINVAL;
  1669. /* if a STA is being removed, reuse its ID */
  1670. flush_work(&mvm->sta_drained_wk);
  1671. mutex_lock(&mvm->mutex);
  1672. if (old_state == IEEE80211_STA_NOTEXIST &&
  1673. new_state == IEEE80211_STA_NONE) {
  1674. /*
  1675. * Firmware bug - it'll crash if the beacon interval is less
  1676. * than 16. We can't avoid connecting at all, so refuse the
  1677. * station state change, this will cause mac80211 to abandon
  1678. * attempts to connect to this AP, and eventually wpa_s will
  1679. * blacklist the AP...
  1680. */
  1681. if (vif->type == NL80211_IFTYPE_STATION &&
  1682. vif->bss_conf.beacon_int < 16) {
  1683. IWL_ERR(mvm,
  1684. "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
  1685. sta->addr, vif->bss_conf.beacon_int);
  1686. ret = -EINVAL;
  1687. goto out_unlock;
  1688. }
  1689. if (sta->tdls &&
  1690. (vif->p2p ||
  1691. iwl_mvm_tdls_sta_count(mvm, NULL) ==
  1692. IWL_MVM_TDLS_STA_COUNT ||
  1693. iwl_mvm_phy_ctx_count(mvm) > 1)) {
  1694. IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
  1695. ret = -EBUSY;
  1696. goto out_unlock;
  1697. }
  1698. ret = iwl_mvm_add_sta(mvm, vif, sta);
  1699. if (sta->tdls && ret == 0)
  1700. iwl_mvm_recalc_tdls_state(mvm, vif, true);
  1701. } else if (old_state == IEEE80211_STA_NONE &&
  1702. new_state == IEEE80211_STA_AUTH) {
  1703. /*
  1704. * EBS may be disabled due to previous failures reported by FW.
  1705. * Reset EBS status here assuming environment has been changed.
  1706. */
  1707. mvm->last_ebs_successful = true;
  1708. ret = 0;
  1709. } else if (old_state == IEEE80211_STA_AUTH &&
  1710. new_state == IEEE80211_STA_ASSOC) {
  1711. ret = iwl_mvm_update_sta(mvm, vif, sta);
  1712. if (ret == 0)
  1713. iwl_mvm_rs_rate_init(mvm, sta,
  1714. mvmvif->phy_ctxt->channel->band,
  1715. true);
  1716. } else if (old_state == IEEE80211_STA_ASSOC &&
  1717. new_state == IEEE80211_STA_AUTHORIZED) {
  1718. /* we don't support TDLS during DCM */
  1719. if (iwl_mvm_phy_ctx_count(mvm) > 1)
  1720. iwl_mvm_teardown_tdls_peers(mvm);
  1721. /* enable beacon filtering */
  1722. WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
  1723. ret = 0;
  1724. } else if (old_state == IEEE80211_STA_AUTHORIZED &&
  1725. new_state == IEEE80211_STA_ASSOC) {
  1726. /* disable beacon filtering */
  1727. WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
  1728. ret = 0;
  1729. } else if (old_state == IEEE80211_STA_ASSOC &&
  1730. new_state == IEEE80211_STA_AUTH) {
  1731. ret = 0;
  1732. } else if (old_state == IEEE80211_STA_AUTH &&
  1733. new_state == IEEE80211_STA_NONE) {
  1734. ret = 0;
  1735. } else if (old_state == IEEE80211_STA_NONE &&
  1736. new_state == IEEE80211_STA_NOTEXIST) {
  1737. ret = iwl_mvm_rm_sta(mvm, vif, sta);
  1738. if (sta->tdls)
  1739. iwl_mvm_recalc_tdls_state(mvm, vif, false);
  1740. } else {
  1741. ret = -EIO;
  1742. }
  1743. out_unlock:
  1744. mutex_unlock(&mvm->mutex);
  1745. return ret;
  1746. }
  1747. static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  1748. {
  1749. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1750. mvm->rts_threshold = value;
  1751. return 0;
  1752. }
  1753. static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
  1754. struct ieee80211_vif *vif,
  1755. struct ieee80211_sta *sta, u32 changed)
  1756. {
  1757. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1758. if (vif->type == NL80211_IFTYPE_STATION &&
  1759. changed & IEEE80211_RC_NSS_CHANGED)
  1760. iwl_mvm_sf_update(mvm, vif, false);
  1761. }
  1762. static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
  1763. struct ieee80211_vif *vif, u16 ac,
  1764. const struct ieee80211_tx_queue_params *params)
  1765. {
  1766. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1767. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1768. mvmvif->queue_params[ac] = *params;
  1769. /*
  1770. * No need to update right away, we'll get BSS_CHANGED_QOS
  1771. * The exception is P2P_DEVICE interface which needs immediate update.
  1772. */
  1773. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  1774. int ret;
  1775. mutex_lock(&mvm->mutex);
  1776. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
  1777. mutex_unlock(&mvm->mutex);
  1778. return ret;
  1779. }
  1780. return 0;
  1781. }
  1782. static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
  1783. struct ieee80211_vif *vif)
  1784. {
  1785. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1786. u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
  1787. 200 + vif->bss_conf.beacon_int);
  1788. u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
  1789. 100 + vif->bss_conf.beacon_int);
  1790. if (WARN_ON_ONCE(vif->bss_conf.assoc))
  1791. return;
  1792. /*
  1793. * iwl_mvm_protect_session() reads directly from the device
  1794. * (the system time), so make sure it is available.
  1795. */
  1796. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
  1797. return;
  1798. mutex_lock(&mvm->mutex);
  1799. /* Try really hard to protect the session and hear a beacon */
  1800. iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
  1801. mutex_unlock(&mvm->mutex);
  1802. iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
  1803. }
  1804. static void iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
  1805. struct ieee80211_vif *vif)
  1806. {
  1807. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1808. u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
  1809. /*
  1810. * iwl_mvm_protect_session() reads directly from the device
  1811. * (the system time), so make sure it is available.
  1812. */
  1813. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_TDLS))
  1814. return;
  1815. mutex_lock(&mvm->mutex);
  1816. /* Protect the session to hear the TDLS setup response on the channel */
  1817. iwl_mvm_protect_session(mvm, vif, duration, duration, 100);
  1818. mutex_unlock(&mvm->mutex);
  1819. iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_TDLS);
  1820. }
  1821. static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
  1822. struct ieee80211_vif *vif,
  1823. struct cfg80211_sched_scan_request *req,
  1824. struct ieee80211_scan_ies *ies)
  1825. {
  1826. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1827. int ret;
  1828. ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_OS);
  1829. if (ret)
  1830. return ret;
  1831. mutex_lock(&mvm->mutex);
  1832. if (!iwl_mvm_is_idle(mvm)) {
  1833. ret = -EBUSY;
  1834. goto out;
  1835. }
  1836. if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
  1837. ret = -EBUSY;
  1838. goto out;
  1839. }
  1840. mvm->scan_status = IWL_MVM_SCAN_SCHED;
  1841. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  1842. ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
  1843. if (ret)
  1844. goto err;
  1845. }
  1846. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  1847. if (ret)
  1848. goto err;
  1849. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1850. ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
  1851. else
  1852. ret = iwl_mvm_sched_scan_start(mvm, req);
  1853. if (!ret)
  1854. goto out;
  1855. err:
  1856. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1857. out:
  1858. mutex_unlock(&mvm->mutex);
  1859. return ret;
  1860. }
  1861. static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
  1862. struct ieee80211_vif *vif)
  1863. {
  1864. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1865. int ret;
  1866. mutex_lock(&mvm->mutex);
  1867. ret = iwl_mvm_scan_offload_stop(mvm, false);
  1868. mutex_unlock(&mvm->mutex);
  1869. iwl_mvm_wait_for_async_handlers(mvm);
  1870. return ret;
  1871. }
  1872. static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
  1873. enum set_key_cmd cmd,
  1874. struct ieee80211_vif *vif,
  1875. struct ieee80211_sta *sta,
  1876. struct ieee80211_key_conf *key)
  1877. {
  1878. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1879. int ret;
  1880. if (iwlwifi_mod_params.sw_crypto) {
  1881. IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
  1882. return -EOPNOTSUPP;
  1883. }
  1884. switch (key->cipher) {
  1885. case WLAN_CIPHER_SUITE_TKIP:
  1886. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1887. /* fall-through */
  1888. case WLAN_CIPHER_SUITE_CCMP:
  1889. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1890. break;
  1891. case WLAN_CIPHER_SUITE_AES_CMAC:
  1892. WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
  1893. break;
  1894. case WLAN_CIPHER_SUITE_WEP40:
  1895. case WLAN_CIPHER_SUITE_WEP104:
  1896. /*
  1897. * Support for TX only, at least for now, so accept
  1898. * the key and do nothing else. Then mac80211 will
  1899. * pass it for TX but we don't have to use it for RX.
  1900. */
  1901. return 0;
  1902. default:
  1903. /* currently FW supports only one optional cipher scheme */
  1904. if (hw->n_cipher_schemes &&
  1905. hw->cipher_schemes->cipher == key->cipher)
  1906. key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  1907. else
  1908. return -EOPNOTSUPP;
  1909. }
  1910. mutex_lock(&mvm->mutex);
  1911. switch (cmd) {
  1912. case SET_KEY:
  1913. if ((vif->type == NL80211_IFTYPE_ADHOC ||
  1914. vif->type == NL80211_IFTYPE_AP) && !sta) {
  1915. /*
  1916. * GTK on AP interface is a TX-only key, return 0;
  1917. * on IBSS they're per-station and because we're lazy
  1918. * we don't support them for RX, so do the same.
  1919. */
  1920. ret = 0;
  1921. key->hw_key_idx = STA_KEY_IDX_INVALID;
  1922. break;
  1923. }
  1924. IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
  1925. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
  1926. if (ret) {
  1927. IWL_WARN(mvm, "set key failed\n");
  1928. /*
  1929. * can't add key for RX, but we don't need it
  1930. * in the device for TX so still return 0
  1931. */
  1932. key->hw_key_idx = STA_KEY_IDX_INVALID;
  1933. ret = 0;
  1934. }
  1935. break;
  1936. case DISABLE_KEY:
  1937. if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
  1938. ret = 0;
  1939. break;
  1940. }
  1941. IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
  1942. ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
  1943. break;
  1944. default:
  1945. ret = -EINVAL;
  1946. }
  1947. mutex_unlock(&mvm->mutex);
  1948. return ret;
  1949. }
  1950. static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
  1951. struct ieee80211_vif *vif,
  1952. struct ieee80211_key_conf *keyconf,
  1953. struct ieee80211_sta *sta,
  1954. u32 iv32, u16 *phase1key)
  1955. {
  1956. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1957. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  1958. return;
  1959. iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
  1960. }
  1961. static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
  1962. struct iwl_rx_packet *pkt, void *data)
  1963. {
  1964. struct iwl_mvm *mvm =
  1965. container_of(notif_wait, struct iwl_mvm, notif_wait);
  1966. struct iwl_hs20_roc_res *resp;
  1967. int resp_len = iwl_rx_packet_payload_len(pkt);
  1968. struct iwl_mvm_time_event_data *te_data = data;
  1969. if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
  1970. return true;
  1971. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  1972. IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
  1973. return true;
  1974. }
  1975. resp = (void *)pkt->data;
  1976. IWL_DEBUG_TE(mvm,
  1977. "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
  1978. resp->status, resp->event_unique_id);
  1979. te_data->uid = le32_to_cpu(resp->event_unique_id);
  1980. IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
  1981. te_data->uid);
  1982. spin_lock_bh(&mvm->time_event_lock);
  1983. list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
  1984. spin_unlock_bh(&mvm->time_event_lock);
  1985. return true;
  1986. }
  1987. #define AUX_ROC_MAX_DELAY_ON_CHANNEL 5000
  1988. static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
  1989. struct ieee80211_channel *channel,
  1990. struct ieee80211_vif *vif,
  1991. int duration)
  1992. {
  1993. int res, time_reg = DEVICE_SYSTEM_TIME_REG;
  1994. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1995. struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
  1996. static const u8 time_event_response[] = { HOT_SPOT_CMD };
  1997. struct iwl_notification_wait wait_time_event;
  1998. struct iwl_hs20_roc_req aux_roc_req = {
  1999. .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
  2000. .id_and_color =
  2001. cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
  2002. .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
  2003. /* Set the channel info data */
  2004. .channel_info.band = (channel->band == IEEE80211_BAND_2GHZ) ?
  2005. PHY_BAND_24 : PHY_BAND_5,
  2006. .channel_info.channel = channel->hw_value,
  2007. .channel_info.width = PHY_VHT_CHANNEL_MODE20,
  2008. /* Set the time and duration */
  2009. .apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
  2010. .apply_time_max_delay =
  2011. cpu_to_le32(MSEC_TO_TU(AUX_ROC_MAX_DELAY_ON_CHANNEL)),
  2012. .duration = cpu_to_le32(MSEC_TO_TU(duration)),
  2013. };
  2014. /* Set the node address */
  2015. memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
  2016. te_data->vif = vif;
  2017. te_data->duration = duration;
  2018. te_data->id = HOT_SPOT_CMD;
  2019. lockdep_assert_held(&mvm->mutex);
  2020. spin_lock_bh(&mvm->time_event_lock);
  2021. list_add_tail(&te_data->list, &mvm->time_event_list);
  2022. spin_unlock_bh(&mvm->time_event_lock);
  2023. /*
  2024. * Use a notification wait, which really just processes the
  2025. * command response and doesn't wait for anything, in order
  2026. * to be able to process the response and get the UID inside
  2027. * the RX path. Using CMD_WANT_SKB doesn't work because it
  2028. * stores the buffer and then wakes up this thread, by which
  2029. * time another notification (that the time event started)
  2030. * might already be processed unsuccessfully.
  2031. */
  2032. iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
  2033. time_event_response,
  2034. ARRAY_SIZE(time_event_response),
  2035. iwl_mvm_rx_aux_roc, te_data);
  2036. res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
  2037. &aux_roc_req);
  2038. if (res) {
  2039. IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
  2040. iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
  2041. goto out_clear_te;
  2042. }
  2043. /* No need to wait for anything, so just pass 1 (0 isn't valid) */
  2044. res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
  2045. /* should never fail */
  2046. WARN_ON_ONCE(res);
  2047. if (res) {
  2048. out_clear_te:
  2049. spin_lock_bh(&mvm->time_event_lock);
  2050. iwl_mvm_te_clear_data(mvm, te_data);
  2051. spin_unlock_bh(&mvm->time_event_lock);
  2052. }
  2053. return res;
  2054. }
  2055. static int iwl_mvm_roc(struct ieee80211_hw *hw,
  2056. struct ieee80211_vif *vif,
  2057. struct ieee80211_channel *channel,
  2058. int duration,
  2059. enum ieee80211_roc_type type)
  2060. {
  2061. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2062. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2063. struct cfg80211_chan_def chandef;
  2064. struct iwl_mvm_phy_ctxt *phy_ctxt;
  2065. int ret, i;
  2066. IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
  2067. duration, type);
  2068. switch (vif->type) {
  2069. case NL80211_IFTYPE_STATION:
  2070. /* Use aux roc framework (HS20) */
  2071. ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
  2072. vif, duration);
  2073. return ret;
  2074. case NL80211_IFTYPE_P2P_DEVICE:
  2075. /* handle below */
  2076. break;
  2077. default:
  2078. IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
  2079. return -EINVAL;
  2080. }
  2081. mutex_lock(&mvm->mutex);
  2082. for (i = 0; i < NUM_PHY_CTX; i++) {
  2083. phy_ctxt = &mvm->phy_ctxts[i];
  2084. if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
  2085. continue;
  2086. if (phy_ctxt->ref && channel == phy_ctxt->channel) {
  2087. /*
  2088. * Unbind the P2P_DEVICE from the current PHY context,
  2089. * and if the PHY context is not used remove it.
  2090. */
  2091. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  2092. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  2093. goto out_unlock;
  2094. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  2095. /* Bind the P2P_DEVICE to the current PHY Context */
  2096. mvmvif->phy_ctxt = phy_ctxt;
  2097. ret = iwl_mvm_binding_add_vif(mvm, vif);
  2098. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  2099. goto out_unlock;
  2100. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  2101. goto schedule_time_event;
  2102. }
  2103. }
  2104. /* Need to update the PHY context only if the ROC channel changed */
  2105. if (channel == mvmvif->phy_ctxt->channel)
  2106. goto schedule_time_event;
  2107. cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
  2108. /*
  2109. * Change the PHY context configuration as it is currently referenced
  2110. * only by the P2P Device MAC
  2111. */
  2112. if (mvmvif->phy_ctxt->ref == 1) {
  2113. ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
  2114. &chandef, 1, 1);
  2115. if (ret)
  2116. goto out_unlock;
  2117. } else {
  2118. /*
  2119. * The PHY context is shared with other MACs. Need to remove the
  2120. * P2P Device from the binding, allocate an new PHY context and
  2121. * create a new binding
  2122. */
  2123. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  2124. if (!phy_ctxt) {
  2125. ret = -ENOSPC;
  2126. goto out_unlock;
  2127. }
  2128. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
  2129. 1, 1);
  2130. if (ret) {
  2131. IWL_ERR(mvm, "Failed to change PHY context\n");
  2132. goto out_unlock;
  2133. }
  2134. /* Unbind the P2P_DEVICE from the current PHY context */
  2135. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  2136. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  2137. goto out_unlock;
  2138. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  2139. /* Bind the P2P_DEVICE to the new allocated PHY context */
  2140. mvmvif->phy_ctxt = phy_ctxt;
  2141. ret = iwl_mvm_binding_add_vif(mvm, vif);
  2142. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  2143. goto out_unlock;
  2144. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  2145. }
  2146. schedule_time_event:
  2147. /* Schedule the time events */
  2148. ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
  2149. out_unlock:
  2150. mutex_unlock(&mvm->mutex);
  2151. IWL_DEBUG_MAC80211(mvm, "leave\n");
  2152. return ret;
  2153. }
  2154. static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
  2155. {
  2156. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2157. IWL_DEBUG_MAC80211(mvm, "enter\n");
  2158. mutex_lock(&mvm->mutex);
  2159. iwl_mvm_stop_p2p_roc(mvm);
  2160. mutex_unlock(&mvm->mutex);
  2161. IWL_DEBUG_MAC80211(mvm, "leave\n");
  2162. return 0;
  2163. }
  2164. static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
  2165. struct ieee80211_chanctx_conf *ctx)
  2166. {
  2167. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2168. struct iwl_mvm_phy_ctxt *phy_ctxt;
  2169. int ret;
  2170. lockdep_assert_held(&mvm->mutex);
  2171. IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
  2172. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  2173. if (!phy_ctxt) {
  2174. ret = -ENOSPC;
  2175. goto out;
  2176. }
  2177. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
  2178. ctx->rx_chains_static,
  2179. ctx->rx_chains_dynamic);
  2180. if (ret) {
  2181. IWL_ERR(mvm, "Failed to add PHY context\n");
  2182. goto out;
  2183. }
  2184. iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
  2185. *phy_ctxt_id = phy_ctxt->id;
  2186. out:
  2187. return ret;
  2188. }
  2189. static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
  2190. struct ieee80211_chanctx_conf *ctx)
  2191. {
  2192. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2193. int ret;
  2194. mutex_lock(&mvm->mutex);
  2195. ret = __iwl_mvm_add_chanctx(mvm, ctx);
  2196. mutex_unlock(&mvm->mutex);
  2197. return ret;
  2198. }
  2199. static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
  2200. struct ieee80211_chanctx_conf *ctx)
  2201. {
  2202. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2203. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  2204. lockdep_assert_held(&mvm->mutex);
  2205. iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
  2206. }
  2207. static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
  2208. struct ieee80211_chanctx_conf *ctx)
  2209. {
  2210. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2211. mutex_lock(&mvm->mutex);
  2212. __iwl_mvm_remove_chanctx(mvm, ctx);
  2213. mutex_unlock(&mvm->mutex);
  2214. }
  2215. static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
  2216. struct ieee80211_chanctx_conf *ctx,
  2217. u32 changed)
  2218. {
  2219. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2220. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2221. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  2222. if (WARN_ONCE((phy_ctxt->ref > 1) &&
  2223. (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
  2224. IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
  2225. IEEE80211_CHANCTX_CHANGE_RADAR |
  2226. IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
  2227. "Cannot change PHY. Ref=%d, changed=0x%X\n",
  2228. phy_ctxt->ref, changed))
  2229. return;
  2230. mutex_lock(&mvm->mutex);
  2231. iwl_mvm_bt_coex_vif_change(mvm);
  2232. iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
  2233. ctx->rx_chains_static,
  2234. ctx->rx_chains_dynamic);
  2235. mutex_unlock(&mvm->mutex);
  2236. }
  2237. static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
  2238. struct ieee80211_vif *vif,
  2239. struct ieee80211_chanctx_conf *ctx,
  2240. bool switching_chanctx)
  2241. {
  2242. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2243. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  2244. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2245. int ret;
  2246. lockdep_assert_held(&mvm->mutex);
  2247. mvmvif->phy_ctxt = phy_ctxt;
  2248. switch (vif->type) {
  2249. case NL80211_IFTYPE_AP:
  2250. /* Unless it's a CSA flow we have nothing to do here */
  2251. if (vif->csa_active) {
  2252. mvmvif->ap_ibss_active = true;
  2253. break;
  2254. }
  2255. case NL80211_IFTYPE_ADHOC:
  2256. /*
  2257. * The AP binding flow is handled as part of the start_ap flow
  2258. * (in bss_info_changed), similarly for IBSS.
  2259. */
  2260. ret = 0;
  2261. goto out;
  2262. case NL80211_IFTYPE_STATION:
  2263. case NL80211_IFTYPE_MONITOR:
  2264. break;
  2265. default:
  2266. ret = -EINVAL;
  2267. goto out;
  2268. }
  2269. ret = iwl_mvm_binding_add_vif(mvm, vif);
  2270. if (ret)
  2271. goto out;
  2272. /*
  2273. * Power state must be updated before quotas,
  2274. * otherwise fw will complain.
  2275. */
  2276. iwl_mvm_power_update_mac(mvm);
  2277. /* Setting the quota at this stage is only required for monitor
  2278. * interfaces. For the other types, the bss_info changed flow
  2279. * will handle quota settings.
  2280. */
  2281. if (vif->type == NL80211_IFTYPE_MONITOR) {
  2282. mvmvif->monitor_active = true;
  2283. ret = iwl_mvm_update_quotas(mvm, NULL);
  2284. if (ret)
  2285. goto out_remove_binding;
  2286. }
  2287. /* Handle binding during CSA */
  2288. if ((vif->type == NL80211_IFTYPE_AP) ||
  2289. (switching_chanctx && (vif->type == NL80211_IFTYPE_STATION))) {
  2290. iwl_mvm_update_quotas(mvm, NULL);
  2291. iwl_mvm_mac_ctxt_changed(mvm, vif, false);
  2292. }
  2293. goto out;
  2294. out_remove_binding:
  2295. iwl_mvm_binding_remove_vif(mvm, vif);
  2296. iwl_mvm_power_update_mac(mvm);
  2297. out:
  2298. if (ret)
  2299. mvmvif->phy_ctxt = NULL;
  2300. return ret;
  2301. }
  2302. static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
  2303. struct ieee80211_vif *vif,
  2304. struct ieee80211_chanctx_conf *ctx)
  2305. {
  2306. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2307. int ret;
  2308. mutex_lock(&mvm->mutex);
  2309. ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
  2310. mutex_unlock(&mvm->mutex);
  2311. return ret;
  2312. }
  2313. static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
  2314. struct ieee80211_vif *vif,
  2315. struct ieee80211_chanctx_conf *ctx,
  2316. bool switching_chanctx)
  2317. {
  2318. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2319. struct ieee80211_vif *disabled_vif = NULL;
  2320. lockdep_assert_held(&mvm->mutex);
  2321. iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
  2322. switch (vif->type) {
  2323. case NL80211_IFTYPE_ADHOC:
  2324. goto out;
  2325. case NL80211_IFTYPE_MONITOR:
  2326. mvmvif->monitor_active = false;
  2327. break;
  2328. case NL80211_IFTYPE_AP:
  2329. /* This part is triggered only during CSA */
  2330. if (!vif->csa_active || !mvmvif->ap_ibss_active)
  2331. goto out;
  2332. /* Set CS bit on all the stations */
  2333. iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
  2334. /* Save blocked iface, the timeout is set on the next beacon */
  2335. rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
  2336. mvmvif->ap_ibss_active = false;
  2337. break;
  2338. case NL80211_IFTYPE_STATION:
  2339. if (!switching_chanctx)
  2340. break;
  2341. disabled_vif = vif;
  2342. iwl_mvm_mac_ctxt_changed(mvm, vif, true);
  2343. break;
  2344. default:
  2345. break;
  2346. }
  2347. iwl_mvm_update_quotas(mvm, disabled_vif);
  2348. iwl_mvm_binding_remove_vif(mvm, vif);
  2349. out:
  2350. mvmvif->phy_ctxt = NULL;
  2351. iwl_mvm_power_update_mac(mvm);
  2352. }
  2353. static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
  2354. struct ieee80211_vif *vif,
  2355. struct ieee80211_chanctx_conf *ctx)
  2356. {
  2357. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2358. mutex_lock(&mvm->mutex);
  2359. __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
  2360. mutex_unlock(&mvm->mutex);
  2361. }
  2362. static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
  2363. struct ieee80211_vif_chanctx_switch *vifs,
  2364. int n_vifs,
  2365. enum ieee80211_chanctx_switch_mode mode)
  2366. {
  2367. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2368. int ret;
  2369. /* we only support SWAP_CONTEXTS and with a single-vif right now */
  2370. if (mode != CHANCTX_SWMODE_SWAP_CONTEXTS || n_vifs > 1)
  2371. return -EOPNOTSUPP;
  2372. mutex_lock(&mvm->mutex);
  2373. __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
  2374. __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
  2375. ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
  2376. if (ret) {
  2377. IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
  2378. goto out_reassign;
  2379. }
  2380. ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
  2381. true);
  2382. if (ret) {
  2383. IWL_ERR(mvm,
  2384. "failed to assign new_ctx during channel switch\n");
  2385. goto out_remove;
  2386. }
  2387. /* we don't support TDLS during DCM - can be caused by channel switch */
  2388. if (iwl_mvm_phy_ctx_count(mvm) > 1)
  2389. iwl_mvm_teardown_tdls_peers(mvm);
  2390. goto out;
  2391. out_remove:
  2392. __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
  2393. out_reassign:
  2394. ret = __iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx);
  2395. if (ret) {
  2396. IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
  2397. goto out_restart;
  2398. }
  2399. ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
  2400. true);
  2401. if (ret) {
  2402. IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
  2403. goto out_restart;
  2404. }
  2405. goto out;
  2406. out_restart:
  2407. /* things keep failing, better restart the hw */
  2408. iwl_mvm_nic_restart(mvm, false);
  2409. out:
  2410. mutex_unlock(&mvm->mutex);
  2411. return ret;
  2412. }
  2413. static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
  2414. struct ieee80211_sta *sta,
  2415. bool set)
  2416. {
  2417. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2418. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  2419. if (!mvm_sta || !mvm_sta->vif) {
  2420. IWL_ERR(mvm, "Station is not associated to a vif\n");
  2421. return -EINVAL;
  2422. }
  2423. return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
  2424. }
  2425. #ifdef CONFIG_NL80211_TESTMODE
  2426. static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
  2427. [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
  2428. [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
  2429. [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
  2430. };
  2431. static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
  2432. struct ieee80211_vif *vif,
  2433. void *data, int len)
  2434. {
  2435. struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
  2436. int err;
  2437. u32 noa_duration;
  2438. err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
  2439. if (err)
  2440. return err;
  2441. if (!tb[IWL_MVM_TM_ATTR_CMD])
  2442. return -EINVAL;
  2443. switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
  2444. case IWL_MVM_TM_CMD_SET_NOA:
  2445. if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
  2446. !vif->bss_conf.enable_beacon ||
  2447. !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
  2448. return -EINVAL;
  2449. noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
  2450. if (noa_duration >= vif->bss_conf.beacon_int)
  2451. return -EINVAL;
  2452. mvm->noa_duration = noa_duration;
  2453. mvm->noa_vif = vif;
  2454. return iwl_mvm_update_quotas(mvm, NULL);
  2455. case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
  2456. /* must be associated client vif - ignore authorized */
  2457. if (!vif || vif->type != NL80211_IFTYPE_STATION ||
  2458. !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
  2459. !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
  2460. return -EINVAL;
  2461. if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
  2462. return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
  2463. return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
  2464. }
  2465. return -EOPNOTSUPP;
  2466. }
  2467. static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
  2468. struct ieee80211_vif *vif,
  2469. void *data, int len)
  2470. {
  2471. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2472. int err;
  2473. mutex_lock(&mvm->mutex);
  2474. err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
  2475. mutex_unlock(&mvm->mutex);
  2476. return err;
  2477. }
  2478. #endif
  2479. static void iwl_mvm_channel_switch_beacon(struct ieee80211_hw *hw,
  2480. struct ieee80211_vif *vif,
  2481. struct cfg80211_chan_def *chandef)
  2482. {
  2483. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2484. struct ieee80211_vif *csa_vif;
  2485. mutex_lock(&mvm->mutex);
  2486. csa_vif = rcu_dereference_protected(mvm->csa_vif,
  2487. lockdep_is_held(&mvm->mutex));
  2488. if (WARN(csa_vif && csa_vif->csa_active,
  2489. "Another CSA is already in progress"))
  2490. goto out_unlock;
  2491. IWL_DEBUG_MAC80211(mvm, "CSA started to freq %d\n",
  2492. chandef->center_freq1);
  2493. rcu_assign_pointer(mvm->csa_vif, vif);
  2494. out_unlock:
  2495. mutex_unlock(&mvm->mutex);
  2496. }
  2497. static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
  2498. struct ieee80211_vif *vif, u32 queues, bool drop)
  2499. {
  2500. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2501. struct iwl_mvm_vif *mvmvif;
  2502. struct iwl_mvm_sta *mvmsta;
  2503. if (!vif || vif->type != NL80211_IFTYPE_STATION)
  2504. return;
  2505. mutex_lock(&mvm->mutex);
  2506. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2507. mvmsta = iwl_mvm_sta_from_staid_protected(mvm, mvmvif->ap_sta_id);
  2508. if (WARN_ON_ONCE(!mvmsta))
  2509. goto done;
  2510. if (drop) {
  2511. if (iwl_mvm_flush_tx_path(mvm, mvmsta->tfd_queue_msk, true))
  2512. IWL_ERR(mvm, "flush request fail\n");
  2513. } else {
  2514. iwl_trans_wait_tx_queue_empty(mvm->trans,
  2515. mvmsta->tfd_queue_msk);
  2516. }
  2517. done:
  2518. mutex_unlock(&mvm->mutex);
  2519. }
  2520. const struct ieee80211_ops iwl_mvm_hw_ops = {
  2521. .tx = iwl_mvm_mac_tx,
  2522. .ampdu_action = iwl_mvm_mac_ampdu_action,
  2523. .start = iwl_mvm_mac_start,
  2524. .restart_complete = iwl_mvm_mac_restart_complete,
  2525. .stop = iwl_mvm_mac_stop,
  2526. .add_interface = iwl_mvm_mac_add_interface,
  2527. .remove_interface = iwl_mvm_mac_remove_interface,
  2528. .config = iwl_mvm_mac_config,
  2529. .prepare_multicast = iwl_mvm_prepare_multicast,
  2530. .configure_filter = iwl_mvm_configure_filter,
  2531. .bss_info_changed = iwl_mvm_bss_info_changed,
  2532. .hw_scan = iwl_mvm_mac_hw_scan,
  2533. .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
  2534. .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
  2535. .sta_state = iwl_mvm_mac_sta_state,
  2536. .sta_notify = iwl_mvm_mac_sta_notify,
  2537. .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
  2538. .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
  2539. .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
  2540. .sta_rc_update = iwl_mvm_sta_rc_update,
  2541. .conf_tx = iwl_mvm_mac_conf_tx,
  2542. .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
  2543. .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
  2544. .flush = iwl_mvm_mac_flush,
  2545. .sched_scan_start = iwl_mvm_mac_sched_scan_start,
  2546. .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
  2547. .set_key = iwl_mvm_mac_set_key,
  2548. .update_tkip_key = iwl_mvm_mac_update_tkip_key,
  2549. .remain_on_channel = iwl_mvm_roc,
  2550. .cancel_remain_on_channel = iwl_mvm_cancel_roc,
  2551. .add_chanctx = iwl_mvm_add_chanctx,
  2552. .remove_chanctx = iwl_mvm_remove_chanctx,
  2553. .change_chanctx = iwl_mvm_change_chanctx,
  2554. .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
  2555. .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
  2556. .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
  2557. .start_ap = iwl_mvm_start_ap_ibss,
  2558. .stop_ap = iwl_mvm_stop_ap_ibss,
  2559. .join_ibss = iwl_mvm_start_ap_ibss,
  2560. .leave_ibss = iwl_mvm_stop_ap_ibss,
  2561. .set_tim = iwl_mvm_set_tim,
  2562. .channel_switch_beacon = iwl_mvm_channel_switch_beacon,
  2563. CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
  2564. #ifdef CONFIG_PM_SLEEP
  2565. /* look at d3.c */
  2566. .suspend = iwl_mvm_suspend,
  2567. .resume = iwl_mvm_resume,
  2568. .set_wakeup = iwl_mvm_set_wakeup,
  2569. .set_rekey_data = iwl_mvm_set_rekey_data,
  2570. #if IS_ENABLED(CONFIG_IPV6)
  2571. .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
  2572. #endif
  2573. .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
  2574. #endif
  2575. };