mac80211.c 83 KB

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