mac80211.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449
  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. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11. * Copyright(c) 2018 Intel Corporation
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of version 2 of the GNU General Public License as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  25. * USA
  26. *
  27. * The full GNU General Public License is included in this distribution
  28. * in the file called COPYING.
  29. *
  30. * Contact Information:
  31. * Intel Linux Wireless <linuxwifi@intel.com>
  32. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  33. *
  34. * BSD LICENSE
  35. *
  36. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  37. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  38. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  39. * All rights reserved.
  40. *
  41. * Redistribution and use in source and binary forms, with or without
  42. * modification, are permitted provided that the following conditions
  43. * are met:
  44. *
  45. * * Redistributions of source code must retain the above copyright
  46. * notice, this list of conditions and the following disclaimer.
  47. * * Redistributions in binary form must reproduce the above copyright
  48. * notice, this list of conditions and the following disclaimer in
  49. * the documentation and/or other materials provided with the
  50. * distribution.
  51. * * Neither the name Intel Corporation nor the names of its
  52. * contributors may be used to endorse or promote products derived
  53. * from this software without specific prior written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  56. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  57. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  58. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  59. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  60. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  61. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  62. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  63. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  64. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  65. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  66. *
  67. *****************************************************************************/
  68. #include <linux/kernel.h>
  69. #include <linux/slab.h>
  70. #include <linux/skbuff.h>
  71. #include <linux/netdevice.h>
  72. #include <linux/etherdevice.h>
  73. #include <linux/ip.h>
  74. #include <linux/if_arp.h>
  75. #include <linux/time.h>
  76. #include <net/mac80211.h>
  77. #include <net/ieee80211_radiotap.h>
  78. #include <net/tcp.h>
  79. #include "iwl-op-mode.h"
  80. #include "iwl-io.h"
  81. #include "mvm.h"
  82. #include "sta.h"
  83. #include "time-event.h"
  84. #include "iwl-eeprom-parse.h"
  85. #include "iwl-phy-db.h"
  86. #include "testmode.h"
  87. #include "fw/error-dump.h"
  88. #include "iwl-prph.h"
  89. #include "iwl-nvm-parse.h"
  90. static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
  91. {
  92. .max = 1,
  93. .types = BIT(NL80211_IFTYPE_STATION),
  94. },
  95. {
  96. .max = 1,
  97. .types = BIT(NL80211_IFTYPE_AP) |
  98. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  99. BIT(NL80211_IFTYPE_P2P_GO),
  100. },
  101. {
  102. .max = 1,
  103. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  104. },
  105. };
  106. static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
  107. {
  108. .num_different_channels = 2,
  109. .max_interfaces = 3,
  110. .limits = iwl_mvm_limits,
  111. .n_limits = ARRAY_SIZE(iwl_mvm_limits),
  112. },
  113. };
  114. #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
  115. /*
  116. * Use the reserved field to indicate magic values.
  117. * these values will only be used internally by the driver,
  118. * and won't make it to the fw (reserved will be 0).
  119. * BC_FILTER_MAGIC_IP - configure the val of this attribute to
  120. * be the vif's ip address. in case there is not a single
  121. * ip address (0, or more than 1), this attribute will
  122. * be skipped.
  123. * BC_FILTER_MAGIC_MAC - set the val of this attribute to
  124. * the LSB bytes of the vif's mac address
  125. */
  126. enum {
  127. BC_FILTER_MAGIC_NONE = 0,
  128. BC_FILTER_MAGIC_IP,
  129. BC_FILTER_MAGIC_MAC,
  130. };
  131. static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
  132. {
  133. /* arp */
  134. .discard = 0,
  135. .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
  136. .attrs = {
  137. {
  138. /* frame type - arp, hw type - ethernet */
  139. .offset_type =
  140. BCAST_FILTER_OFFSET_PAYLOAD_START,
  141. .offset = sizeof(rfc1042_header),
  142. .val = cpu_to_be32(0x08060001),
  143. .mask = cpu_to_be32(0xffffffff),
  144. },
  145. {
  146. /* arp dest ip */
  147. .offset_type =
  148. BCAST_FILTER_OFFSET_PAYLOAD_START,
  149. .offset = sizeof(rfc1042_header) + 2 +
  150. sizeof(struct arphdr) +
  151. ETH_ALEN + sizeof(__be32) +
  152. ETH_ALEN,
  153. .mask = cpu_to_be32(0xffffffff),
  154. /* mark it as special field */
  155. .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
  156. },
  157. },
  158. },
  159. {
  160. /* dhcp offer bcast */
  161. .discard = 0,
  162. .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
  163. .attrs = {
  164. {
  165. /* udp dest port - 68 (bootp client)*/
  166. .offset_type = BCAST_FILTER_OFFSET_IP_END,
  167. .offset = offsetof(struct udphdr, dest),
  168. .val = cpu_to_be32(0x00440000),
  169. .mask = cpu_to_be32(0xffff0000),
  170. },
  171. {
  172. /* dhcp - lsb bytes of client hw address */
  173. .offset_type = BCAST_FILTER_OFFSET_IP_END,
  174. .offset = 38,
  175. .mask = cpu_to_be32(0xffffffff),
  176. /* mark it as special field */
  177. .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
  178. },
  179. },
  180. },
  181. /* last filter must be empty */
  182. {},
  183. };
  184. #endif
  185. void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
  186. {
  187. if (!iwl_mvm_is_d0i3_supported(mvm))
  188. return;
  189. IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
  190. spin_lock_bh(&mvm->refs_lock);
  191. mvm->refs[ref_type]++;
  192. spin_unlock_bh(&mvm->refs_lock);
  193. iwl_trans_ref(mvm->trans);
  194. }
  195. void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
  196. {
  197. if (!iwl_mvm_is_d0i3_supported(mvm))
  198. return;
  199. IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
  200. spin_lock_bh(&mvm->refs_lock);
  201. if (WARN_ON(!mvm->refs[ref_type])) {
  202. spin_unlock_bh(&mvm->refs_lock);
  203. return;
  204. }
  205. mvm->refs[ref_type]--;
  206. spin_unlock_bh(&mvm->refs_lock);
  207. iwl_trans_unref(mvm->trans);
  208. }
  209. static void iwl_mvm_unref_all_except(struct iwl_mvm *mvm,
  210. enum iwl_mvm_ref_type except_ref)
  211. {
  212. int i, j;
  213. if (!iwl_mvm_is_d0i3_supported(mvm))
  214. return;
  215. spin_lock_bh(&mvm->refs_lock);
  216. for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
  217. if (except_ref == i || !mvm->refs[i])
  218. continue;
  219. IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d (%d)\n",
  220. i, mvm->refs[i]);
  221. for (j = 0; j < mvm->refs[i]; j++)
  222. iwl_trans_unref(mvm->trans);
  223. mvm->refs[i] = 0;
  224. }
  225. spin_unlock_bh(&mvm->refs_lock);
  226. }
  227. bool iwl_mvm_ref_taken(struct iwl_mvm *mvm)
  228. {
  229. int i;
  230. bool taken = false;
  231. if (!iwl_mvm_is_d0i3_supported(mvm))
  232. return true;
  233. spin_lock_bh(&mvm->refs_lock);
  234. for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
  235. if (mvm->refs[i]) {
  236. taken = true;
  237. break;
  238. }
  239. }
  240. spin_unlock_bh(&mvm->refs_lock);
  241. return taken;
  242. }
  243. int iwl_mvm_ref_sync(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
  244. {
  245. iwl_mvm_ref(mvm, ref_type);
  246. if (!wait_event_timeout(mvm->d0i3_exit_waitq,
  247. !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status),
  248. HZ)) {
  249. WARN_ON_ONCE(1);
  250. iwl_mvm_unref(mvm, ref_type);
  251. return -EIO;
  252. }
  253. return 0;
  254. }
  255. static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
  256. {
  257. int i;
  258. memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
  259. for (i = 0; i < NUM_PHY_CTX; i++) {
  260. mvm->phy_ctxts[i].id = i;
  261. mvm->phy_ctxts[i].ref = 0;
  262. }
  263. }
  264. struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
  265. const char *alpha2,
  266. enum iwl_mcc_source src_id,
  267. bool *changed)
  268. {
  269. struct ieee80211_regdomain *regd = NULL;
  270. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  271. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  272. struct iwl_mcc_update_resp *resp;
  273. IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
  274. lockdep_assert_held(&mvm->mutex);
  275. resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
  276. if (IS_ERR_OR_NULL(resp)) {
  277. IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
  278. PTR_ERR_OR_ZERO(resp));
  279. goto out;
  280. }
  281. if (changed)
  282. *changed = (resp->status == MCC_RESP_NEW_CHAN_PROFILE);
  283. regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
  284. __le32_to_cpu(resp->n_channels),
  285. resp->channels,
  286. __le16_to_cpu(resp->mcc),
  287. __le16_to_cpu(resp->geo_info));
  288. /* Store the return source id */
  289. src_id = resp->source_id;
  290. kfree(resp);
  291. if (IS_ERR_OR_NULL(regd)) {
  292. IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
  293. PTR_ERR_OR_ZERO(regd));
  294. goto out;
  295. }
  296. IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
  297. regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
  298. mvm->lar_regdom_set = true;
  299. mvm->mcc_src = src_id;
  300. out:
  301. return regd;
  302. }
  303. void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
  304. {
  305. bool changed;
  306. struct ieee80211_regdomain *regd;
  307. if (!iwl_mvm_is_lar_supported(mvm))
  308. return;
  309. regd = iwl_mvm_get_current_regdomain(mvm, &changed);
  310. if (!IS_ERR_OR_NULL(regd)) {
  311. /* only update the regulatory core if changed */
  312. if (changed)
  313. regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
  314. kfree(regd);
  315. }
  316. }
  317. struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
  318. bool *changed)
  319. {
  320. return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
  321. iwl_mvm_is_wifi_mcc_supported(mvm) ?
  322. MCC_SOURCE_GET_CURRENT :
  323. MCC_SOURCE_OLD_FW, changed);
  324. }
  325. int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
  326. {
  327. enum iwl_mcc_source used_src;
  328. struct ieee80211_regdomain *regd;
  329. int ret;
  330. bool changed;
  331. const struct ieee80211_regdomain *r =
  332. rtnl_dereference(mvm->hw->wiphy->regd);
  333. if (!r)
  334. return -ENOENT;
  335. /* save the last source in case we overwrite it below */
  336. used_src = mvm->mcc_src;
  337. if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
  338. /* Notify the firmware we support wifi location updates */
  339. regd = iwl_mvm_get_current_regdomain(mvm, NULL);
  340. if (!IS_ERR_OR_NULL(regd))
  341. kfree(regd);
  342. }
  343. /* Now set our last stored MCC and source */
  344. regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
  345. &changed);
  346. if (IS_ERR_OR_NULL(regd))
  347. return -EIO;
  348. /* update cfg80211 if the regdomain was changed */
  349. if (changed)
  350. ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
  351. else
  352. ret = 0;
  353. kfree(regd);
  354. return ret;
  355. }
  356. int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
  357. {
  358. struct ieee80211_hw *hw = mvm->hw;
  359. int num_mac, ret, i;
  360. static const u32 mvm_ciphers[] = {
  361. WLAN_CIPHER_SUITE_WEP40,
  362. WLAN_CIPHER_SUITE_WEP104,
  363. WLAN_CIPHER_SUITE_TKIP,
  364. WLAN_CIPHER_SUITE_CCMP,
  365. };
  366. /* Tell mac80211 our characteristics */
  367. ieee80211_hw_set(hw, SIGNAL_DBM);
  368. ieee80211_hw_set(hw, SPECTRUM_MGMT);
  369. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  370. ieee80211_hw_set(hw, QUEUE_CONTROL);
  371. ieee80211_hw_set(hw, WANT_MONITOR_VIF);
  372. ieee80211_hw_set(hw, SUPPORTS_PS);
  373. ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
  374. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  375. ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
  376. ieee80211_hw_set(hw, CONNECTION_MONITOR);
  377. ieee80211_hw_set(hw, CHANCTX_STA_CSA);
  378. ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
  379. ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
  380. ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
  381. ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
  382. ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
  383. if (iwl_mvm_has_tlc_offload(mvm)) {
  384. ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
  385. ieee80211_hw_set(hw, HAS_RATE_CONTROL);
  386. }
  387. if (iwl_mvm_has_new_rx_api(mvm))
  388. ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
  389. if (fw_has_capa(&mvm->fw->ucode_capa,
  390. IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
  391. ieee80211_hw_set(hw, AP_LINK_PS);
  392. } else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
  393. /*
  394. * we absolutely need this for the new TX API since that comes
  395. * with many more queues than the current code can deal with
  396. * for station powersave
  397. */
  398. return -EINVAL;
  399. }
  400. if (mvm->trans->num_rx_queues > 1)
  401. ieee80211_hw_set(hw, USES_RSS);
  402. if (mvm->trans->max_skb_frags)
  403. hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
  404. hw->queues = IEEE80211_MAX_QUEUES;
  405. hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
  406. hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
  407. IEEE80211_RADIOTAP_MCS_HAVE_STBC;
  408. hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
  409. IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
  410. hw->radiotap_timestamp.units_pos =
  411. IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
  412. IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
  413. /* this is the case for CCK frames, it's better (only 8) for OFDM */
  414. hw->radiotap_timestamp.accuracy = 22;
  415. if (!iwl_mvm_has_tlc_offload(mvm))
  416. hw->rate_control_algorithm = RS_NAME;
  417. hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
  418. hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
  419. BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
  420. memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
  421. hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
  422. hw->wiphy->cipher_suites = mvm->ciphers;
  423. if (iwl_mvm_has_new_rx_api(mvm)) {
  424. mvm->ciphers[hw->wiphy->n_cipher_suites] =
  425. WLAN_CIPHER_SUITE_GCMP;
  426. hw->wiphy->n_cipher_suites++;
  427. mvm->ciphers[hw->wiphy->n_cipher_suites] =
  428. WLAN_CIPHER_SUITE_GCMP_256;
  429. hw->wiphy->n_cipher_suites++;
  430. }
  431. /* Enable 11w if software crypto is not enabled (as the
  432. * firmware will interpret some mgmt packets, so enabling it
  433. * with software crypto isn't safe).
  434. */
  435. if (!iwlwifi_mod_params.swcrypto) {
  436. ieee80211_hw_set(hw, MFP_CAPABLE);
  437. mvm->ciphers[hw->wiphy->n_cipher_suites] =
  438. WLAN_CIPHER_SUITE_AES_CMAC;
  439. hw->wiphy->n_cipher_suites++;
  440. if (iwl_mvm_has_new_rx_api(mvm)) {
  441. mvm->ciphers[hw->wiphy->n_cipher_suites] =
  442. WLAN_CIPHER_SUITE_BIP_GMAC_128;
  443. hw->wiphy->n_cipher_suites++;
  444. mvm->ciphers[hw->wiphy->n_cipher_suites] =
  445. WLAN_CIPHER_SUITE_BIP_GMAC_256;
  446. hw->wiphy->n_cipher_suites++;
  447. }
  448. }
  449. /* currently FW API supports only one optional cipher scheme */
  450. if (mvm->fw->cs[0].cipher) {
  451. const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
  452. struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
  453. mvm->hw->n_cipher_schemes = 1;
  454. cs->cipher = le32_to_cpu(fwcs->cipher);
  455. cs->iftype = BIT(NL80211_IFTYPE_STATION);
  456. cs->hdr_len = fwcs->hdr_len;
  457. cs->pn_len = fwcs->pn_len;
  458. cs->pn_off = fwcs->pn_off;
  459. cs->key_idx_off = fwcs->key_idx_off;
  460. cs->key_idx_mask = fwcs->key_idx_mask;
  461. cs->key_idx_shift = fwcs->key_idx_shift;
  462. cs->mic_len = fwcs->mic_len;
  463. mvm->hw->cipher_schemes = mvm->cs;
  464. mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
  465. hw->wiphy->n_cipher_suites++;
  466. }
  467. ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
  468. hw->wiphy->features |=
  469. NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
  470. NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
  471. NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
  472. hw->sta_data_size = sizeof(struct iwl_mvm_sta);
  473. hw->vif_data_size = sizeof(struct iwl_mvm_vif);
  474. hw->chanctx_data_size = sizeof(u16);
  475. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  476. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  477. BIT(NL80211_IFTYPE_AP) |
  478. BIT(NL80211_IFTYPE_P2P_GO) |
  479. BIT(NL80211_IFTYPE_P2P_DEVICE) |
  480. BIT(NL80211_IFTYPE_ADHOC);
  481. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  482. hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
  483. if (iwl_mvm_is_lar_supported(mvm))
  484. hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
  485. else
  486. hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
  487. REGULATORY_DISABLE_BEACON_HINTS;
  488. hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  489. hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  490. hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
  491. hw->wiphy->n_iface_combinations =
  492. ARRAY_SIZE(iwl_mvm_iface_combinations);
  493. hw->wiphy->max_remain_on_channel_duration = 10000;
  494. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  495. /* we can compensate an offset of up to 3 channels = 15 MHz */
  496. hw->wiphy->max_adj_channel_rssi_comp = 3 * 5;
  497. /* Extract MAC address */
  498. memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
  499. hw->wiphy->addresses = mvm->addresses;
  500. hw->wiphy->n_addresses = 1;
  501. /* Extract additional MAC addresses if available */
  502. num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
  503. min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
  504. for (i = 1; i < num_mac; i++) {
  505. memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
  506. ETH_ALEN);
  507. mvm->addresses[i].addr[5]++;
  508. hw->wiphy->n_addresses++;
  509. }
  510. iwl_mvm_reset_phy_ctxts(mvm);
  511. hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
  512. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  513. BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
  514. BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
  515. IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
  516. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
  517. mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
  518. else
  519. mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
  520. if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
  521. hw->wiphy->bands[NL80211_BAND_2GHZ] =
  522. &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
  523. if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
  524. hw->wiphy->bands[NL80211_BAND_5GHZ] =
  525. &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
  526. if (fw_has_capa(&mvm->fw->ucode_capa,
  527. IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
  528. fw_has_api(&mvm->fw->ucode_capa,
  529. IWL_UCODE_TLV_API_LQ_SS_PARAMS))
  530. hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
  531. IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
  532. }
  533. hw->wiphy->hw_version = mvm->trans->hw_id;
  534. if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
  535. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  536. else
  537. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  538. hw->wiphy->max_sched_scan_reqs = 1;
  539. hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
  540. hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
  541. /* we create the 802.11 header and zero length SSID IE. */
  542. hw->wiphy->max_sched_scan_ie_len =
  543. SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
  544. hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
  545. hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
  546. /*
  547. * the firmware uses u8 for num of iterations, but 0xff is saved for
  548. * infinite loop, so the maximum number of iterations is actually 254.
  549. */
  550. hw->wiphy->max_sched_scan_plan_iterations = 254;
  551. hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
  552. NL80211_FEATURE_LOW_PRIORITY_SCAN |
  553. NL80211_FEATURE_P2P_GO_OPPPS |
  554. NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
  555. NL80211_FEATURE_DYNAMIC_SMPS |
  556. NL80211_FEATURE_STATIC_SMPS |
  557. NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
  558. if (fw_has_capa(&mvm->fw->ucode_capa,
  559. IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
  560. hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
  561. if (fw_has_capa(&mvm->fw->ucode_capa,
  562. IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
  563. hw->wiphy->features |= NL80211_FEATURE_QUIET;
  564. if (fw_has_capa(&mvm->fw->ucode_capa,
  565. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
  566. hw->wiphy->features |=
  567. NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
  568. if (fw_has_capa(&mvm->fw->ucode_capa,
  569. IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
  570. hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
  571. if (fw_has_api(&mvm->fw->ucode_capa,
  572. IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
  573. wiphy_ext_feature_set(hw->wiphy,
  574. NL80211_EXT_FEATURE_SCAN_START_TIME);
  575. wiphy_ext_feature_set(hw->wiphy,
  576. NL80211_EXT_FEATURE_BSS_PARENT_TSF);
  577. wiphy_ext_feature_set(hw->wiphy,
  578. NL80211_EXT_FEATURE_SET_SCAN_DWELL);
  579. }
  580. if (iwl_mvm_is_oce_supported(mvm)) {
  581. wiphy_ext_feature_set(hw->wiphy,
  582. NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
  583. wiphy_ext_feature_set(hw->wiphy,
  584. NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
  585. wiphy_ext_feature_set(hw->wiphy,
  586. NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
  587. wiphy_ext_feature_set(hw->wiphy,
  588. NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
  589. }
  590. mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  591. #ifdef CONFIG_PM_SLEEP
  592. if (iwl_mvm_is_d0i3_supported(mvm) &&
  593. device_can_wakeup(mvm->trans->dev)) {
  594. mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
  595. hw->wiphy->wowlan = &mvm->wowlan;
  596. }
  597. if (mvm->fw->img[IWL_UCODE_WOWLAN].num_sec &&
  598. mvm->trans->ops->d3_suspend &&
  599. mvm->trans->ops->d3_resume &&
  600. device_can_wakeup(mvm->trans->dev)) {
  601. mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
  602. WIPHY_WOWLAN_DISCONNECT |
  603. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  604. WIPHY_WOWLAN_RFKILL_RELEASE |
  605. WIPHY_WOWLAN_NET_DETECT;
  606. if (!iwlwifi_mod_params.swcrypto)
  607. mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  608. WIPHY_WOWLAN_GTK_REKEY_FAILURE |
  609. WIPHY_WOWLAN_4WAY_HANDSHAKE;
  610. mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
  611. mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
  612. mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
  613. mvm->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES;
  614. hw->wiphy->wowlan = &mvm->wowlan;
  615. }
  616. #endif
  617. #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
  618. /* assign default bcast filtering configuration */
  619. mvm->bcast_filters = iwl_mvm_default_bcast_filters;
  620. #endif
  621. ret = iwl_mvm_leds_init(mvm);
  622. if (ret)
  623. return ret;
  624. if (fw_has_capa(&mvm->fw->ucode_capa,
  625. IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
  626. IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
  627. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
  628. ieee80211_hw_set(hw, TDLS_WIDER_BW);
  629. }
  630. if (fw_has_capa(&mvm->fw->ucode_capa,
  631. IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
  632. IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
  633. hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
  634. }
  635. hw->netdev_features |= mvm->cfg->features;
  636. if (!iwl_mvm_is_csum_supported(mvm)) {
  637. hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
  638. NETIF_F_RXCSUM);
  639. /* We may support SW TX CSUM */
  640. if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
  641. hw->netdev_features |= IWL_TX_CSUM_NETIF_FLAGS;
  642. }
  643. ret = ieee80211_register_hw(mvm->hw);
  644. if (ret)
  645. iwl_mvm_leds_exit(mvm);
  646. mvm->init_status |= IWL_MVM_INIT_STATUS_REG_HW_INIT_COMPLETE;
  647. if (mvm->cfg->vht_mu_mimo_supported)
  648. wiphy_ext_feature_set(hw->wiphy,
  649. NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
  650. return ret;
  651. }
  652. static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
  653. struct ieee80211_sta *sta,
  654. struct sk_buff *skb)
  655. {
  656. struct iwl_mvm_sta *mvmsta;
  657. bool defer = false;
  658. /*
  659. * double check the IN_D0I3 flag both before and after
  660. * taking the spinlock, in order to prevent taking
  661. * the spinlock when not needed.
  662. */
  663. if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
  664. return false;
  665. spin_lock(&mvm->d0i3_tx_lock);
  666. /*
  667. * testing the flag again ensures the skb dequeue
  668. * loop (on d0i3 exit) hasn't run yet.
  669. */
  670. if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
  671. goto out;
  672. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  673. if (mvmsta->sta_id == IWL_MVM_INVALID_STA ||
  674. mvmsta->sta_id != mvm->d0i3_ap_sta_id)
  675. goto out;
  676. __skb_queue_tail(&mvm->d0i3_tx, skb);
  677. ieee80211_stop_queues(mvm->hw);
  678. /* trigger wakeup */
  679. iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
  680. iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
  681. defer = true;
  682. out:
  683. spin_unlock(&mvm->d0i3_tx_lock);
  684. return defer;
  685. }
  686. static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
  687. struct ieee80211_tx_control *control,
  688. struct sk_buff *skb)
  689. {
  690. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  691. struct ieee80211_sta *sta = control->sta;
  692. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  693. struct ieee80211_hdr *hdr = (void *)skb->data;
  694. if (iwl_mvm_is_radio_killed(mvm)) {
  695. IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
  696. goto drop;
  697. }
  698. if (info->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
  699. !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
  700. !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
  701. goto drop;
  702. /* treat non-bufferable MMPDUs on AP interfaces as broadcast */
  703. if ((info->control.vif->type == NL80211_IFTYPE_AP ||
  704. info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
  705. ieee80211_is_mgmt(hdr->frame_control) &&
  706. !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
  707. sta = NULL;
  708. if (sta) {
  709. if (iwl_mvm_defer_tx(mvm, sta, skb))
  710. return;
  711. if (iwl_mvm_tx_skb(mvm, skb, sta))
  712. goto drop;
  713. return;
  714. }
  715. if (iwl_mvm_tx_skb_non_sta(mvm, skb))
  716. goto drop;
  717. return;
  718. drop:
  719. ieee80211_free_txskb(hw, skb);
  720. }
  721. static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
  722. {
  723. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
  724. return false;
  725. return true;
  726. }
  727. static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
  728. {
  729. if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
  730. return false;
  731. if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
  732. return true;
  733. /* enabled by default */
  734. return true;
  735. }
  736. #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...) \
  737. do { \
  738. if (!(le16_to_cpu(_tid_bm) & BIT(_tid))) \
  739. break; \
  740. iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt); \
  741. } while (0)
  742. static void
  743. iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  744. struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
  745. enum ieee80211_ampdu_mlme_action action)
  746. {
  747. struct iwl_fw_dbg_trigger_tlv *trig;
  748. struct iwl_fw_dbg_trigger_ba *ba_trig;
  749. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
  750. return;
  751. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
  752. ba_trig = (void *)trig->data;
  753. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  754. ieee80211_vif_to_wdev(vif), trig))
  755. return;
  756. switch (action) {
  757. case IEEE80211_AMPDU_TX_OPERATIONAL: {
  758. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  759. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  760. CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
  761. "TX AGG START: MAC %pM tid %d ssn %d\n",
  762. sta->addr, tid, tid_data->ssn);
  763. break;
  764. }
  765. case IEEE80211_AMPDU_TX_STOP_CONT:
  766. CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
  767. "TX AGG STOP: MAC %pM tid %d\n",
  768. sta->addr, tid);
  769. break;
  770. case IEEE80211_AMPDU_RX_START:
  771. CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
  772. "RX AGG START: MAC %pM tid %d ssn %d\n",
  773. sta->addr, tid, rx_ba_ssn);
  774. break;
  775. case IEEE80211_AMPDU_RX_STOP:
  776. CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
  777. "RX AGG STOP: MAC %pM tid %d\n",
  778. sta->addr, tid);
  779. break;
  780. default:
  781. break;
  782. }
  783. }
  784. static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
  785. struct ieee80211_vif *vif,
  786. struct ieee80211_ampdu_params *params)
  787. {
  788. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  789. int ret;
  790. bool tx_agg_ref = false;
  791. struct ieee80211_sta *sta = params->sta;
  792. enum ieee80211_ampdu_mlme_action action = params->action;
  793. u16 tid = params->tid;
  794. u16 *ssn = &params->ssn;
  795. u8 buf_size = params->buf_size;
  796. bool amsdu = params->amsdu;
  797. u16 timeout = params->timeout;
  798. IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
  799. sta->addr, tid, action);
  800. if (!(mvm->nvm_data->sku_cap_11n_enable))
  801. return -EACCES;
  802. /* return from D0i3 before starting a new Tx aggregation */
  803. switch (action) {
  804. case IEEE80211_AMPDU_TX_START:
  805. case IEEE80211_AMPDU_TX_STOP_CONT:
  806. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  807. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  808. case IEEE80211_AMPDU_TX_OPERATIONAL:
  809. /*
  810. * for tx start, wait synchronously until D0i3 exit to
  811. * get the correct sequence number for the tid.
  812. * additionally, some other ampdu actions use direct
  813. * target access, which is not handled automatically
  814. * by the trans layer (unlike commands), so wait for
  815. * d0i3 exit in these cases as well.
  816. */
  817. ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
  818. if (ret)
  819. return ret;
  820. tx_agg_ref = true;
  821. break;
  822. default:
  823. break;
  824. }
  825. mutex_lock(&mvm->mutex);
  826. switch (action) {
  827. case IEEE80211_AMPDU_RX_START:
  828. if (!iwl_enable_rx_ampdu(mvm->cfg)) {
  829. ret = -EINVAL;
  830. break;
  831. }
  832. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
  833. timeout);
  834. break;
  835. case IEEE80211_AMPDU_RX_STOP:
  836. ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
  837. timeout);
  838. break;
  839. case IEEE80211_AMPDU_TX_START:
  840. if (!iwl_enable_tx_ampdu(mvm->cfg)) {
  841. ret = -EINVAL;
  842. break;
  843. }
  844. ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
  845. break;
  846. case IEEE80211_AMPDU_TX_STOP_CONT:
  847. ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
  848. break;
  849. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  850. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  851. ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
  852. break;
  853. case IEEE80211_AMPDU_TX_OPERATIONAL:
  854. ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
  855. buf_size, amsdu);
  856. break;
  857. default:
  858. WARN_ON_ONCE(1);
  859. ret = -EINVAL;
  860. break;
  861. }
  862. if (!ret) {
  863. u16 rx_ba_ssn = 0;
  864. if (action == IEEE80211_AMPDU_RX_START)
  865. rx_ba_ssn = *ssn;
  866. iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
  867. rx_ba_ssn, action);
  868. }
  869. mutex_unlock(&mvm->mutex);
  870. /*
  871. * If the tid is marked as started, we won't use it for offloaded
  872. * traffic on the next D0i3 entry. It's safe to unref.
  873. */
  874. if (tx_agg_ref)
  875. iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
  876. return ret;
  877. }
  878. static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
  879. struct ieee80211_vif *vif)
  880. {
  881. struct iwl_mvm *mvm = data;
  882. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  883. mvmvif->uploaded = false;
  884. mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
  885. spin_lock_bh(&mvm->time_event_lock);
  886. iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
  887. spin_unlock_bh(&mvm->time_event_lock);
  888. mvmvif->phy_ctxt = NULL;
  889. memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
  890. }
  891. static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
  892. {
  893. /* clear the D3 reconfig, we only need it to avoid dumping a
  894. * firmware coredump on reconfiguration, we shouldn't do that
  895. * on D3->D0 transition
  896. */
  897. if (!test_and_clear_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status)) {
  898. mvm->fwrt.dump.desc = &iwl_dump_desc_assert;
  899. iwl_fw_error_dump(&mvm->fwrt);
  900. }
  901. /* cleanup all stale references (scan, roc), but keep the
  902. * ucode_down ref until reconfig is complete
  903. */
  904. iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
  905. iwl_mvm_stop_device(mvm);
  906. mvm->scan_status = 0;
  907. mvm->ps_disabled = false;
  908. mvm->calibrating = false;
  909. /* just in case one was running */
  910. iwl_mvm_cleanup_roc_te(mvm);
  911. ieee80211_remain_on_channel_expired(mvm->hw);
  912. /*
  913. * cleanup all interfaces, even inactive ones, as some might have
  914. * gone down during the HW restart
  915. */
  916. ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
  917. mvm->p2p_device_vif = NULL;
  918. mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
  919. iwl_mvm_reset_phy_ctxts(mvm);
  920. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  921. memset(mvm->sta_deferred_frames, 0, sizeof(mvm->sta_deferred_frames));
  922. memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
  923. memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
  924. ieee80211_wake_queues(mvm->hw);
  925. /* clear any stale d0i3 state */
  926. clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
  927. mvm->vif_count = 0;
  928. mvm->rx_ba_sessions = 0;
  929. mvm->fwrt.dump.conf = FW_DBG_INVALID;
  930. mvm->monitor_on = false;
  931. /* keep statistics ticking */
  932. iwl_mvm_accu_radio_stats(mvm);
  933. }
  934. int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
  935. {
  936. int ret;
  937. lockdep_assert_held(&mvm->mutex);
  938. if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
  939. /*
  940. * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
  941. * so later code will - from now on - see that we're doing it.
  942. */
  943. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  944. clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
  945. /* Clean up some internal and mac80211 state on restart */
  946. iwl_mvm_restart_cleanup(mvm);
  947. } else {
  948. /* Hold the reference to prevent runtime suspend while
  949. * the start procedure runs. It's a bit confusing
  950. * that the UCODE_DOWN reference is taken, but it just
  951. * means "UCODE is not UP yet". ( TODO: rename this
  952. * reference).
  953. */
  954. iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
  955. }
  956. ret = iwl_mvm_up(mvm);
  957. if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  958. /* Something went wrong - we need to finish some cleanup
  959. * that normally iwl_mvm_mac_restart_complete() below
  960. * would do.
  961. */
  962. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  963. iwl_mvm_d0i3_enable_tx(mvm, NULL);
  964. }
  965. return ret;
  966. }
  967. static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
  968. {
  969. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  970. int ret;
  971. /* Some hw restart cleanups must not hold the mutex */
  972. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  973. /*
  974. * Make sure we are out of d0i3. This is needed
  975. * to make sure the reference accounting is correct
  976. * (and there is no stale d0i3_exit_work).
  977. */
  978. wait_event_timeout(mvm->d0i3_exit_waitq,
  979. !test_bit(IWL_MVM_STATUS_IN_D0I3,
  980. &mvm->status),
  981. HZ);
  982. }
  983. mutex_lock(&mvm->mutex);
  984. ret = __iwl_mvm_mac_start(mvm);
  985. mutex_unlock(&mvm->mutex);
  986. return ret;
  987. }
  988. static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
  989. {
  990. int ret;
  991. mutex_lock(&mvm->mutex);
  992. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  993. iwl_mvm_d0i3_enable_tx(mvm, NULL);
  994. ret = iwl_mvm_update_quotas(mvm, true, NULL);
  995. if (ret)
  996. IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
  997. ret);
  998. /* allow transport/FW low power modes */
  999. iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
  1000. /*
  1001. * If we have TDLS peers, remove them. We don't know the last seqno/PN
  1002. * of packets the FW sent out, so we must reconnect.
  1003. */
  1004. iwl_mvm_teardown_tdls_peers(mvm);
  1005. mutex_unlock(&mvm->mutex);
  1006. }
  1007. static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
  1008. {
  1009. if (iwl_mvm_is_d0i3_supported(mvm) &&
  1010. iwl_mvm_enter_d0i3_on_suspend(mvm))
  1011. WARN_ONCE(!wait_event_timeout(mvm->d0i3_exit_waitq,
  1012. !test_bit(IWL_MVM_STATUS_IN_D0I3,
  1013. &mvm->status),
  1014. HZ),
  1015. "D0i3 exit on resume timed out\n");
  1016. }
  1017. static void
  1018. iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
  1019. enum ieee80211_reconfig_type reconfig_type)
  1020. {
  1021. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1022. switch (reconfig_type) {
  1023. case IEEE80211_RECONFIG_TYPE_RESTART:
  1024. iwl_mvm_restart_complete(mvm);
  1025. break;
  1026. case IEEE80211_RECONFIG_TYPE_SUSPEND:
  1027. iwl_mvm_resume_complete(mvm);
  1028. break;
  1029. }
  1030. }
  1031. void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
  1032. {
  1033. lockdep_assert_held(&mvm->mutex);
  1034. /* firmware counters are obviously reset now, but we shouldn't
  1035. * partially track so also clear the fw_reset_accu counters.
  1036. */
  1037. memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
  1038. /* async_handlers_wk is now blocked */
  1039. /*
  1040. * The work item could be running or queued if the
  1041. * ROC time event stops just as we get here.
  1042. */
  1043. flush_work(&mvm->roc_done_wk);
  1044. iwl_mvm_stop_device(mvm);
  1045. iwl_mvm_async_handlers_purge(mvm);
  1046. /* async_handlers_list is empty and will stay empty: HW is stopped */
  1047. /* the fw is stopped, the aux sta is dead: clean up driver state */
  1048. iwl_mvm_del_aux_sta(mvm);
  1049. /*
  1050. * Clear IN_HW_RESTART flag when stopping the hw (as restart_complete()
  1051. * won't be called in this case).
  1052. * But make sure to cleanup interfaces that have gone down before/during
  1053. * HW restart was requested.
  1054. */
  1055. if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  1056. ieee80211_iterate_interfaces(mvm->hw, 0,
  1057. iwl_mvm_cleanup_iterator, mvm);
  1058. /* We shouldn't have any UIDs still set. Loop over all the UIDs to
  1059. * make sure there's nothing left there and warn if any is found.
  1060. */
  1061. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
  1062. int i;
  1063. for (i = 0; i < mvm->max_scans; i++) {
  1064. if (WARN_ONCE(mvm->scan_uid_status[i],
  1065. "UMAC scan UID %d status was not cleaned\n",
  1066. i))
  1067. mvm->scan_uid_status[i] = 0;
  1068. }
  1069. }
  1070. }
  1071. static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
  1072. {
  1073. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1074. flush_work(&mvm->d0i3_exit_work);
  1075. flush_work(&mvm->async_handlers_wk);
  1076. flush_work(&mvm->add_stream_wk);
  1077. /*
  1078. * Lock and clear the firmware running bit here already, so that
  1079. * new commands coming in elsewhere, e.g. from debugfs, will not
  1080. * be able to proceed. This is important here because one of those
  1081. * debugfs files causes the firmware dump to be triggered, and if we
  1082. * don't stop debugfs accesses before canceling that it could be
  1083. * retriggered after we flush it but before we've cleared the bit.
  1084. */
  1085. clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
  1086. iwl_fw_cancel_dump(&mvm->fwrt);
  1087. cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
  1088. cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
  1089. iwl_fw_free_dump_desc(&mvm->fwrt);
  1090. mutex_lock(&mvm->mutex);
  1091. __iwl_mvm_mac_stop(mvm);
  1092. mutex_unlock(&mvm->mutex);
  1093. /*
  1094. * The worker might have been waiting for the mutex, let it run and
  1095. * discover that its list is now empty.
  1096. */
  1097. cancel_work_sync(&mvm->async_handlers_wk);
  1098. }
  1099. static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
  1100. {
  1101. u16 i;
  1102. lockdep_assert_held(&mvm->mutex);
  1103. for (i = 0; i < NUM_PHY_CTX; i++)
  1104. if (!mvm->phy_ctxts[i].ref)
  1105. return &mvm->phy_ctxts[i];
  1106. IWL_ERR(mvm, "No available PHY context\n");
  1107. return NULL;
  1108. }
  1109. static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1110. s16 tx_power)
  1111. {
  1112. struct iwl_dev_tx_power_cmd cmd = {
  1113. .v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
  1114. .v3.mac_context_id =
  1115. cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
  1116. .v3.pwr_restriction = cpu_to_le16(8 * tx_power),
  1117. };
  1118. int len = sizeof(cmd);
  1119. if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
  1120. cmd.v3.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
  1121. if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
  1122. len = sizeof(cmd.v3);
  1123. return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
  1124. }
  1125. static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
  1126. struct ieee80211_vif *vif)
  1127. {
  1128. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1129. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1130. int ret;
  1131. mvmvif->mvm = mvm;
  1132. /*
  1133. * make sure D0i3 exit is completed, otherwise a target access
  1134. * during tx queue configuration could be done when still in
  1135. * D0i3 state.
  1136. */
  1137. ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
  1138. if (ret)
  1139. return ret;
  1140. /*
  1141. * Not much to do here. The stack will not allow interface
  1142. * types or combinations that we didn't advertise, so we
  1143. * don't really have to check the types.
  1144. */
  1145. mutex_lock(&mvm->mutex);
  1146. /* make sure that beacon statistics don't go backwards with FW reset */
  1147. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  1148. mvmvif->beacon_stats.accu_num_beacons +=
  1149. mvmvif->beacon_stats.num_beacons;
  1150. /* Allocate resources for the MAC context, and add it to the fw */
  1151. ret = iwl_mvm_mac_ctxt_init(mvm, vif);
  1152. if (ret)
  1153. goto out_unlock;
  1154. /* Counting number of interfaces is needed for legacy PM */
  1155. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  1156. mvm->vif_count++;
  1157. /*
  1158. * The AP binding flow can be done only after the beacon
  1159. * template is configured (which happens only in the mac80211
  1160. * start_ap() flow), and adding the broadcast station can happen
  1161. * only after the binding.
  1162. * In addition, since modifying the MAC before adding a bcast
  1163. * station is not allowed by the FW, delay the adding of MAC context to
  1164. * the point where we can also add the bcast station.
  1165. * In short: there's not much we can do at this point, other than
  1166. * allocating resources :)
  1167. */
  1168. if (vif->type == NL80211_IFTYPE_AP ||
  1169. vif->type == NL80211_IFTYPE_ADHOC) {
  1170. ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
  1171. if (ret) {
  1172. IWL_ERR(mvm, "Failed to allocate bcast sta\n");
  1173. goto out_release;
  1174. }
  1175. /*
  1176. * Only queue for this station is the mcast queue,
  1177. * which shouldn't be in TFD mask anyway
  1178. */
  1179. ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
  1180. 0, vif->type,
  1181. IWL_STA_MULTICAST);
  1182. if (ret)
  1183. goto out_release;
  1184. iwl_mvm_vif_dbgfs_register(mvm, vif);
  1185. goto out_unlock;
  1186. }
  1187. mvmvif->features |= hw->netdev_features;
  1188. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  1189. if (ret)
  1190. goto out_release;
  1191. ret = iwl_mvm_power_update_mac(mvm);
  1192. if (ret)
  1193. goto out_remove_mac;
  1194. /* beacon filtering */
  1195. ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
  1196. if (ret)
  1197. goto out_remove_mac;
  1198. if (!mvm->bf_allowed_vif &&
  1199. vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
  1200. mvm->bf_allowed_vif = mvmvif;
  1201. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
  1202. IEEE80211_VIF_SUPPORTS_CQM_RSSI;
  1203. }
  1204. /*
  1205. * P2P_DEVICE interface does not have a channel context assigned to it,
  1206. * so a dedicated PHY context is allocated to it and the corresponding
  1207. * MAC context is bound to it at this stage.
  1208. */
  1209. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  1210. mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  1211. if (!mvmvif->phy_ctxt) {
  1212. ret = -ENOSPC;
  1213. goto out_free_bf;
  1214. }
  1215. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  1216. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1217. if (ret)
  1218. goto out_unref_phy;
  1219. ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
  1220. if (ret)
  1221. goto out_unbind;
  1222. /* Save a pointer to p2p device vif, so it can later be used to
  1223. * update the p2p device MAC when a GO is started/stopped */
  1224. mvm->p2p_device_vif = vif;
  1225. }
  1226. if (vif->type == NL80211_IFTYPE_MONITOR)
  1227. mvm->monitor_on = true;
  1228. iwl_mvm_vif_dbgfs_register(mvm, vif);
  1229. goto out_unlock;
  1230. out_unbind:
  1231. iwl_mvm_binding_remove_vif(mvm, vif);
  1232. out_unref_phy:
  1233. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  1234. out_free_bf:
  1235. if (mvm->bf_allowed_vif == mvmvif) {
  1236. mvm->bf_allowed_vif = NULL;
  1237. vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
  1238. IEEE80211_VIF_SUPPORTS_CQM_RSSI);
  1239. }
  1240. out_remove_mac:
  1241. mvmvif->phy_ctxt = NULL;
  1242. iwl_mvm_mac_ctxt_remove(mvm, vif);
  1243. out_release:
  1244. if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
  1245. mvm->vif_count--;
  1246. out_unlock:
  1247. mutex_unlock(&mvm->mutex);
  1248. iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
  1249. return ret;
  1250. }
  1251. static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
  1252. struct ieee80211_vif *vif)
  1253. {
  1254. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  1255. /*
  1256. * Flush the ROC worker which will flush the OFFCHANNEL queue.
  1257. * We assume here that all the packets sent to the OFFCHANNEL
  1258. * queue are sent in ROC session.
  1259. */
  1260. flush_work(&mvm->roc_done_wk);
  1261. }
  1262. }
  1263. static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
  1264. struct ieee80211_vif *vif)
  1265. {
  1266. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1267. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1268. iwl_mvm_prepare_mac_removal(mvm, vif);
  1269. mutex_lock(&mvm->mutex);
  1270. if (mvm->bf_allowed_vif == mvmvif) {
  1271. mvm->bf_allowed_vif = NULL;
  1272. vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
  1273. IEEE80211_VIF_SUPPORTS_CQM_RSSI);
  1274. }
  1275. iwl_mvm_vif_dbgfs_clean(mvm, vif);
  1276. /*
  1277. * For AP/GO interface, the tear down of the resources allocated to the
  1278. * interface is be handled as part of the stop_ap flow.
  1279. */
  1280. if (vif->type == NL80211_IFTYPE_AP ||
  1281. vif->type == NL80211_IFTYPE_ADHOC) {
  1282. #ifdef CONFIG_NL80211_TESTMODE
  1283. if (vif == mvm->noa_vif) {
  1284. mvm->noa_vif = NULL;
  1285. mvm->noa_duration = 0;
  1286. }
  1287. #endif
  1288. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
  1289. iwl_mvm_dealloc_bcast_sta(mvm, vif);
  1290. goto out_release;
  1291. }
  1292. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  1293. mvm->p2p_device_vif = NULL;
  1294. iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
  1295. iwl_mvm_binding_remove_vif(mvm, vif);
  1296. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  1297. mvmvif->phy_ctxt = NULL;
  1298. }
  1299. if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
  1300. mvm->vif_count--;
  1301. iwl_mvm_power_update_mac(mvm);
  1302. iwl_mvm_mac_ctxt_remove(mvm, vif);
  1303. if (vif->type == NL80211_IFTYPE_MONITOR)
  1304. mvm->monitor_on = false;
  1305. out_release:
  1306. mutex_unlock(&mvm->mutex);
  1307. }
  1308. static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
  1309. {
  1310. return 0;
  1311. }
  1312. struct iwl_mvm_mc_iter_data {
  1313. struct iwl_mvm *mvm;
  1314. int port_id;
  1315. };
  1316. static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
  1317. struct ieee80211_vif *vif)
  1318. {
  1319. struct iwl_mvm_mc_iter_data *data = _data;
  1320. struct iwl_mvm *mvm = data->mvm;
  1321. struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
  1322. struct iwl_host_cmd hcmd = {
  1323. .id = MCAST_FILTER_CMD,
  1324. .flags = CMD_ASYNC,
  1325. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1326. };
  1327. int ret, len;
  1328. /* if we don't have free ports, mcast frames will be dropped */
  1329. if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
  1330. return;
  1331. if (vif->type != NL80211_IFTYPE_STATION ||
  1332. !vif->bss_conf.assoc)
  1333. return;
  1334. cmd->port_id = data->port_id++;
  1335. memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
  1336. len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
  1337. hcmd.len[0] = len;
  1338. hcmd.data[0] = cmd;
  1339. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1340. if (ret)
  1341. IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
  1342. }
  1343. static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
  1344. {
  1345. struct iwl_mvm_mc_iter_data iter_data = {
  1346. .mvm = mvm,
  1347. };
  1348. lockdep_assert_held(&mvm->mutex);
  1349. if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
  1350. return;
  1351. ieee80211_iterate_active_interfaces_atomic(
  1352. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1353. iwl_mvm_mc_iface_iterator, &iter_data);
  1354. }
  1355. static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
  1356. struct netdev_hw_addr_list *mc_list)
  1357. {
  1358. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1359. struct iwl_mcast_filter_cmd *cmd;
  1360. struct netdev_hw_addr *addr;
  1361. int addr_count;
  1362. bool pass_all;
  1363. int len;
  1364. addr_count = netdev_hw_addr_list_count(mc_list);
  1365. pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
  1366. IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
  1367. if (pass_all)
  1368. addr_count = 0;
  1369. len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
  1370. cmd = kzalloc(len, GFP_ATOMIC);
  1371. if (!cmd)
  1372. return 0;
  1373. if (pass_all) {
  1374. cmd->pass_all = 1;
  1375. return (u64)(unsigned long)cmd;
  1376. }
  1377. netdev_hw_addr_list_for_each(addr, mc_list) {
  1378. IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
  1379. cmd->count, addr->addr);
  1380. memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
  1381. addr->addr, ETH_ALEN);
  1382. cmd->count++;
  1383. }
  1384. return (u64)(unsigned long)cmd;
  1385. }
  1386. static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
  1387. unsigned int changed_flags,
  1388. unsigned int *total_flags,
  1389. u64 multicast)
  1390. {
  1391. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1392. struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
  1393. mutex_lock(&mvm->mutex);
  1394. /* replace previous configuration */
  1395. kfree(mvm->mcast_filter_cmd);
  1396. mvm->mcast_filter_cmd = cmd;
  1397. if (!cmd)
  1398. goto out;
  1399. if (changed_flags & FIF_ALLMULTI)
  1400. cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
  1401. if (cmd->pass_all)
  1402. cmd->count = 0;
  1403. iwl_mvm_recalc_multicast(mvm);
  1404. out:
  1405. mutex_unlock(&mvm->mutex);
  1406. *total_flags = 0;
  1407. }
  1408. static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
  1409. struct ieee80211_vif *vif,
  1410. unsigned int filter_flags,
  1411. unsigned int changed_flags)
  1412. {
  1413. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1414. /* We support only filter for probe requests */
  1415. if (!(changed_flags & FIF_PROBE_REQ))
  1416. return;
  1417. /* Supported only for p2p client interfaces */
  1418. if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
  1419. !vif->p2p)
  1420. return;
  1421. mutex_lock(&mvm->mutex);
  1422. iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  1423. mutex_unlock(&mvm->mutex);
  1424. }
  1425. #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
  1426. struct iwl_bcast_iter_data {
  1427. struct iwl_mvm *mvm;
  1428. struct iwl_bcast_filter_cmd *cmd;
  1429. u8 current_filter;
  1430. };
  1431. static void
  1432. iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
  1433. const struct iwl_fw_bcast_filter *in_filter,
  1434. struct iwl_fw_bcast_filter *out_filter)
  1435. {
  1436. struct iwl_fw_bcast_filter_attr *attr;
  1437. int i;
  1438. memcpy(out_filter, in_filter, sizeof(*out_filter));
  1439. for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
  1440. attr = &out_filter->attrs[i];
  1441. if (!attr->mask)
  1442. break;
  1443. switch (attr->reserved1) {
  1444. case cpu_to_le16(BC_FILTER_MAGIC_IP):
  1445. if (vif->bss_conf.arp_addr_cnt != 1) {
  1446. attr->mask = 0;
  1447. continue;
  1448. }
  1449. attr->val = vif->bss_conf.arp_addr_list[0];
  1450. break;
  1451. case cpu_to_le16(BC_FILTER_MAGIC_MAC):
  1452. attr->val = *(__be32 *)&vif->addr[2];
  1453. break;
  1454. default:
  1455. break;
  1456. }
  1457. attr->reserved1 = 0;
  1458. out_filter->num_attrs++;
  1459. }
  1460. }
  1461. static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
  1462. struct ieee80211_vif *vif)
  1463. {
  1464. struct iwl_bcast_iter_data *data = _data;
  1465. struct iwl_mvm *mvm = data->mvm;
  1466. struct iwl_bcast_filter_cmd *cmd = data->cmd;
  1467. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1468. struct iwl_fw_bcast_mac *bcast_mac;
  1469. int i;
  1470. if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
  1471. return;
  1472. bcast_mac = &cmd->macs[mvmvif->id];
  1473. /*
  1474. * enable filtering only for associated stations, but not for P2P
  1475. * Clients
  1476. */
  1477. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
  1478. !vif->bss_conf.assoc)
  1479. return;
  1480. bcast_mac->default_discard = 1;
  1481. /* copy all configured filters */
  1482. for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
  1483. /*
  1484. * Make sure we don't exceed our filters limit.
  1485. * if there is still a valid filter to be configured,
  1486. * be on the safe side and just allow bcast for this mac.
  1487. */
  1488. if (WARN_ON_ONCE(data->current_filter >=
  1489. ARRAY_SIZE(cmd->filters))) {
  1490. bcast_mac->default_discard = 0;
  1491. bcast_mac->attached_filters = 0;
  1492. break;
  1493. }
  1494. iwl_mvm_set_bcast_filter(vif,
  1495. &mvm->bcast_filters[i],
  1496. &cmd->filters[data->current_filter]);
  1497. /* skip current filter if it contains no attributes */
  1498. if (!cmd->filters[data->current_filter].num_attrs)
  1499. continue;
  1500. /* attach the filter to current mac */
  1501. bcast_mac->attached_filters |=
  1502. cpu_to_le16(BIT(data->current_filter));
  1503. data->current_filter++;
  1504. }
  1505. }
  1506. bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
  1507. struct iwl_bcast_filter_cmd *cmd)
  1508. {
  1509. struct iwl_bcast_iter_data iter_data = {
  1510. .mvm = mvm,
  1511. .cmd = cmd,
  1512. };
  1513. if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
  1514. return false;
  1515. memset(cmd, 0, sizeof(*cmd));
  1516. cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
  1517. cmd->max_macs = ARRAY_SIZE(cmd->macs);
  1518. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1519. /* use debugfs filters/macs if override is configured */
  1520. if (mvm->dbgfs_bcast_filtering.override) {
  1521. memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
  1522. sizeof(cmd->filters));
  1523. memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
  1524. sizeof(cmd->macs));
  1525. return true;
  1526. }
  1527. #endif
  1528. /* if no filters are configured, do nothing */
  1529. if (!mvm->bcast_filters)
  1530. return false;
  1531. /* configure and attach these filters for each associated sta vif */
  1532. ieee80211_iterate_active_interfaces(
  1533. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1534. iwl_mvm_bcast_filter_iterator, &iter_data);
  1535. return true;
  1536. }
  1537. static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
  1538. {
  1539. struct iwl_bcast_filter_cmd cmd;
  1540. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
  1541. return 0;
  1542. if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
  1543. return 0;
  1544. return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
  1545. sizeof(cmd), &cmd);
  1546. }
  1547. #else
  1548. static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
  1549. {
  1550. return 0;
  1551. }
  1552. #endif
  1553. static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
  1554. struct ieee80211_vif *vif)
  1555. {
  1556. struct iwl_mu_group_mgmt_cmd cmd = {};
  1557. memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
  1558. WLAN_MEMBERSHIP_LEN);
  1559. memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
  1560. WLAN_USER_POSITION_LEN);
  1561. return iwl_mvm_send_cmd_pdu(mvm,
  1562. WIDE_ID(DATA_PATH_GROUP,
  1563. UPDATE_MU_GROUPS_CMD),
  1564. 0, sizeof(cmd), &cmd);
  1565. }
  1566. static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
  1567. struct ieee80211_vif *vif)
  1568. {
  1569. if (vif->mu_mimo_owner) {
  1570. struct iwl_mu_group_mgmt_notif *notif = _data;
  1571. /*
  1572. * MU-MIMO Group Id action frame is little endian. We treat
  1573. * the data received from firmware as if it came from the
  1574. * action frame, so no conversion is needed.
  1575. */
  1576. ieee80211_update_mu_groups(vif,
  1577. (u8 *)&notif->membership_status,
  1578. (u8 *)&notif->user_position);
  1579. }
  1580. }
  1581. void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
  1582. struct iwl_rx_cmd_buffer *rxb)
  1583. {
  1584. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1585. struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
  1586. ieee80211_iterate_active_interfaces_atomic(
  1587. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1588. iwl_mvm_mu_mimo_iface_iterator, notif);
  1589. }
  1590. static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
  1591. struct ieee80211_vif *vif,
  1592. struct ieee80211_bss_conf *bss_conf,
  1593. u32 changes)
  1594. {
  1595. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1596. int ret;
  1597. /*
  1598. * Re-calculate the tsf id, as the master-slave relations depend on the
  1599. * beacon interval, which was not known when the station interface was
  1600. * added.
  1601. */
  1602. if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
  1603. iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
  1604. /*
  1605. * If we're not associated yet, take the (new) BSSID before associating
  1606. * so the firmware knows. If we're already associated, then use the old
  1607. * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
  1608. * branch for disassociation below.
  1609. */
  1610. if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
  1611. memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
  1612. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
  1613. if (ret)
  1614. IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
  1615. /* after sending it once, adopt mac80211 data */
  1616. memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
  1617. mvmvif->associated = bss_conf->assoc;
  1618. if (changes & BSS_CHANGED_ASSOC) {
  1619. if (bss_conf->assoc) {
  1620. /* clear statistics to get clean beacon counter */
  1621. iwl_mvm_request_statistics(mvm, true);
  1622. memset(&mvmvif->beacon_stats, 0,
  1623. sizeof(mvmvif->beacon_stats));
  1624. /* add quota for this interface */
  1625. ret = iwl_mvm_update_quotas(mvm, true, NULL);
  1626. if (ret) {
  1627. IWL_ERR(mvm, "failed to update quotas\n");
  1628. return;
  1629. }
  1630. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
  1631. &mvm->status)) {
  1632. /*
  1633. * If we're restarting then the firmware will
  1634. * obviously have lost synchronisation with
  1635. * the AP. It will attempt to synchronise by
  1636. * itself, but we can make it more reliable by
  1637. * scheduling a session protection time event.
  1638. *
  1639. * The firmware needs to receive a beacon to
  1640. * catch up with synchronisation, use 110% of
  1641. * the beacon interval.
  1642. *
  1643. * Set a large maximum delay to allow for more
  1644. * than a single interface.
  1645. */
  1646. u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
  1647. iwl_mvm_protect_session(mvm, vif, dur, dur,
  1648. 5 * dur, false);
  1649. }
  1650. iwl_mvm_sf_update(mvm, vif, false);
  1651. iwl_mvm_power_vif_assoc(mvm, vif);
  1652. if (vif->p2p) {
  1653. iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
  1654. iwl_mvm_update_smps(mvm, vif,
  1655. IWL_MVM_SMPS_REQ_PROT,
  1656. IEEE80211_SMPS_DYNAMIC);
  1657. }
  1658. } else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
  1659. /*
  1660. * If update fails - SF might be running in associated
  1661. * mode while disassociated - which is forbidden.
  1662. */
  1663. WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
  1664. "Failed to update SF upon disassociation\n");
  1665. /*
  1666. * If we get an assert during the connection (after the
  1667. * station has been added, but before the vif is set
  1668. * to associated), mac80211 will re-add the station and
  1669. * then configure the vif. Since the vif is not
  1670. * associated, we would remove the station here and
  1671. * this would fail the recovery.
  1672. */
  1673. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
  1674. &mvm->status)) {
  1675. /*
  1676. * Remove AP station now that
  1677. * the MAC is unassoc
  1678. */
  1679. ret = iwl_mvm_rm_sta_id(mvm, vif,
  1680. mvmvif->ap_sta_id);
  1681. if (ret)
  1682. IWL_ERR(mvm,
  1683. "failed to remove AP station\n");
  1684. if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
  1685. mvm->d0i3_ap_sta_id =
  1686. IWL_MVM_INVALID_STA;
  1687. mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
  1688. }
  1689. /* remove quota for this interface */
  1690. ret = iwl_mvm_update_quotas(mvm, false, NULL);
  1691. if (ret)
  1692. IWL_ERR(mvm, "failed to update quotas\n");
  1693. if (vif->p2p)
  1694. iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
  1695. /* this will take the cleared BSSID from bss_conf */
  1696. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  1697. if (ret)
  1698. IWL_ERR(mvm,
  1699. "failed to update MAC %pM (clear after unassoc)\n",
  1700. vif->addr);
  1701. }
  1702. /*
  1703. * The firmware tracks the MU-MIMO group on its own.
  1704. * However, on HW restart we should restore this data.
  1705. */
  1706. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1707. (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
  1708. ret = iwl_mvm_update_mu_groups(mvm, vif);
  1709. if (ret)
  1710. IWL_ERR(mvm,
  1711. "failed to update VHT MU_MIMO groups\n");
  1712. }
  1713. iwl_mvm_recalc_multicast(mvm);
  1714. iwl_mvm_configure_bcast_filter(mvm);
  1715. /* reset rssi values */
  1716. mvmvif->bf_data.ave_beacon_signal = 0;
  1717. iwl_mvm_bt_coex_vif_change(mvm);
  1718. iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
  1719. IEEE80211_SMPS_AUTOMATIC);
  1720. if (fw_has_capa(&mvm->fw->ucode_capa,
  1721. IWL_UCODE_TLV_CAPA_UMAC_SCAN))
  1722. iwl_mvm_config_scan(mvm);
  1723. }
  1724. if (changes & BSS_CHANGED_BEACON_INFO) {
  1725. /*
  1726. * We received a beacon from the associated AP so
  1727. * remove the session protection.
  1728. */
  1729. iwl_mvm_stop_session_protection(mvm, vif);
  1730. iwl_mvm_sf_update(mvm, vif, false);
  1731. WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
  1732. }
  1733. if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
  1734. /*
  1735. * Send power command on every beacon change,
  1736. * because we may have not enabled beacon abort yet.
  1737. */
  1738. BSS_CHANGED_BEACON_INFO)) {
  1739. ret = iwl_mvm_power_update_mac(mvm);
  1740. if (ret)
  1741. IWL_ERR(mvm, "failed to update power mode\n");
  1742. }
  1743. if (changes & BSS_CHANGED_TXPOWER) {
  1744. IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
  1745. bss_conf->txpower);
  1746. iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
  1747. }
  1748. if (changes & BSS_CHANGED_CQM) {
  1749. IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
  1750. /* reset cqm events tracking */
  1751. mvmvif->bf_data.last_cqm_event = 0;
  1752. if (mvmvif->bf_data.bf_enabled) {
  1753. ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
  1754. if (ret)
  1755. IWL_ERR(mvm,
  1756. "failed to update CQM thresholds\n");
  1757. }
  1758. }
  1759. if (changes & BSS_CHANGED_ARP_FILTER) {
  1760. IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
  1761. iwl_mvm_configure_bcast_filter(mvm);
  1762. }
  1763. }
  1764. static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
  1765. struct ieee80211_vif *vif)
  1766. {
  1767. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1768. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1769. int ret;
  1770. /*
  1771. * iwl_mvm_mac_ctxt_add() might read directly from the device
  1772. * (the system time), so make sure it is available.
  1773. */
  1774. ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
  1775. if (ret)
  1776. return ret;
  1777. mutex_lock(&mvm->mutex);
  1778. /* Send the beacon template */
  1779. ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
  1780. if (ret)
  1781. goto out_unlock;
  1782. /*
  1783. * Re-calculate the tsf id, as the master-slave relations depend on the
  1784. * beacon interval, which was not known when the AP interface was added.
  1785. */
  1786. if (vif->type == NL80211_IFTYPE_AP)
  1787. iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
  1788. mvmvif->ap_assoc_sta_count = 0;
  1789. /* Add the mac context */
  1790. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  1791. if (ret)
  1792. goto out_unlock;
  1793. /* Perform the binding */
  1794. ret = iwl_mvm_binding_add_vif(mvm, vif);
  1795. if (ret)
  1796. goto out_remove;
  1797. /*
  1798. * This is not very nice, but the simplest:
  1799. * For older FWs adding the mcast sta before the bcast station may
  1800. * cause assert 0x2b00.
  1801. * This is fixed in later FW so make the order of removal depend on
  1802. * the TLV
  1803. */
  1804. if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
  1805. ret = iwl_mvm_add_mcast_sta(mvm, vif);
  1806. if (ret)
  1807. goto out_unbind;
  1808. /*
  1809. * Send the bcast station. At this stage the TBTT and DTIM time
  1810. * events are added and applied to the scheduler
  1811. */
  1812. ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
  1813. if (ret) {
  1814. iwl_mvm_rm_mcast_sta(mvm, vif);
  1815. goto out_unbind;
  1816. }
  1817. } else {
  1818. /*
  1819. * Send the bcast station. At this stage the TBTT and DTIM time
  1820. * events are added and applied to the scheduler
  1821. */
  1822. ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
  1823. if (ret)
  1824. goto out_unbind;
  1825. ret = iwl_mvm_add_mcast_sta(mvm, vif);
  1826. if (ret) {
  1827. iwl_mvm_send_rm_bcast_sta(mvm, vif);
  1828. goto out_unbind;
  1829. }
  1830. }
  1831. /* must be set before quota calculations */
  1832. mvmvif->ap_ibss_active = true;
  1833. /* power updated needs to be done before quotas */
  1834. iwl_mvm_power_update_mac(mvm);
  1835. ret = iwl_mvm_update_quotas(mvm, false, NULL);
  1836. if (ret)
  1837. goto out_quota_failed;
  1838. /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
  1839. if (vif->p2p && mvm->p2p_device_vif)
  1840. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
  1841. iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
  1842. iwl_mvm_bt_coex_vif_change(mvm);
  1843. /* we don't support TDLS during DCM */
  1844. if (iwl_mvm_phy_ctx_count(mvm) > 1)
  1845. iwl_mvm_teardown_tdls_peers(mvm);
  1846. goto out_unlock;
  1847. out_quota_failed:
  1848. iwl_mvm_power_update_mac(mvm);
  1849. mvmvif->ap_ibss_active = false;
  1850. iwl_mvm_send_rm_bcast_sta(mvm, vif);
  1851. iwl_mvm_rm_mcast_sta(mvm, vif);
  1852. out_unbind:
  1853. iwl_mvm_binding_remove_vif(mvm, vif);
  1854. out_remove:
  1855. iwl_mvm_mac_ctxt_remove(mvm, vif);
  1856. out_unlock:
  1857. mutex_unlock(&mvm->mutex);
  1858. iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
  1859. return ret;
  1860. }
  1861. static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
  1862. struct ieee80211_vif *vif)
  1863. {
  1864. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1865. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1866. iwl_mvm_prepare_mac_removal(mvm, vif);
  1867. mutex_lock(&mvm->mutex);
  1868. /* Handle AP stop while in CSA */
  1869. if (rcu_access_pointer(mvm->csa_vif) == vif) {
  1870. iwl_mvm_remove_time_event(mvm, mvmvif,
  1871. &mvmvif->time_event_data);
  1872. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  1873. mvmvif->csa_countdown = false;
  1874. }
  1875. if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
  1876. RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
  1877. mvm->csa_tx_block_bcn_timeout = 0;
  1878. }
  1879. mvmvif->ap_ibss_active = false;
  1880. mvm->ap_last_beacon_gp2 = 0;
  1881. iwl_mvm_bt_coex_vif_change(mvm);
  1882. iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
  1883. /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
  1884. if (vif->p2p && mvm->p2p_device_vif)
  1885. iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
  1886. iwl_mvm_update_quotas(mvm, false, NULL);
  1887. /*
  1888. * This is not very nice, but the simplest:
  1889. * For older FWs removing the mcast sta before the bcast station may
  1890. * cause assert 0x2b00.
  1891. * This is fixed in later FW (which will stop beaconing when removing
  1892. * bcast station).
  1893. * So make the order of removal depend on the TLV
  1894. */
  1895. if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
  1896. iwl_mvm_rm_mcast_sta(mvm, vif);
  1897. iwl_mvm_send_rm_bcast_sta(mvm, vif);
  1898. if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
  1899. iwl_mvm_rm_mcast_sta(mvm, vif);
  1900. iwl_mvm_binding_remove_vif(mvm, vif);
  1901. iwl_mvm_power_update_mac(mvm);
  1902. iwl_mvm_mac_ctxt_remove(mvm, vif);
  1903. mutex_unlock(&mvm->mutex);
  1904. }
  1905. static void
  1906. iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
  1907. struct ieee80211_vif *vif,
  1908. struct ieee80211_bss_conf *bss_conf,
  1909. u32 changes)
  1910. {
  1911. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1912. /* Changes will be applied when the AP/IBSS is started */
  1913. if (!mvmvif->ap_ibss_active)
  1914. return;
  1915. if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
  1916. BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
  1917. iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
  1918. IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
  1919. /* Need to send a new beacon template to the FW */
  1920. if (changes & BSS_CHANGED_BEACON &&
  1921. iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
  1922. IWL_WARN(mvm, "Failed updating beacon data\n");
  1923. if (changes & BSS_CHANGED_TXPOWER) {
  1924. IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
  1925. bss_conf->txpower);
  1926. iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
  1927. }
  1928. }
  1929. static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
  1930. struct ieee80211_vif *vif,
  1931. struct ieee80211_bss_conf *bss_conf,
  1932. u32 changes)
  1933. {
  1934. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1935. /*
  1936. * iwl_mvm_bss_info_changed_station() might call
  1937. * iwl_mvm_protect_session(), which reads directly from
  1938. * the device (the system time), so make sure it is available.
  1939. */
  1940. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
  1941. return;
  1942. mutex_lock(&mvm->mutex);
  1943. if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
  1944. iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
  1945. switch (vif->type) {
  1946. case NL80211_IFTYPE_STATION:
  1947. iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
  1948. break;
  1949. case NL80211_IFTYPE_AP:
  1950. case NL80211_IFTYPE_ADHOC:
  1951. iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
  1952. break;
  1953. case NL80211_IFTYPE_MONITOR:
  1954. if (changes & BSS_CHANGED_MU_GROUPS)
  1955. iwl_mvm_update_mu_groups(mvm, vif);
  1956. break;
  1957. default:
  1958. /* shouldn't happen */
  1959. WARN_ON_ONCE(1);
  1960. }
  1961. mutex_unlock(&mvm->mutex);
  1962. iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
  1963. }
  1964. static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
  1965. struct ieee80211_vif *vif,
  1966. struct ieee80211_scan_request *hw_req)
  1967. {
  1968. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1969. int ret;
  1970. if (hw_req->req.n_channels == 0 ||
  1971. hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1972. return -EINVAL;
  1973. mutex_lock(&mvm->mutex);
  1974. ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
  1975. mutex_unlock(&mvm->mutex);
  1976. return ret;
  1977. }
  1978. static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
  1979. struct ieee80211_vif *vif)
  1980. {
  1981. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1982. mutex_lock(&mvm->mutex);
  1983. /* Due to a race condition, it's possible that mac80211 asks
  1984. * us to stop a hw_scan when it's already stopped. This can
  1985. * happen, for instance, if we stopped the scan ourselves,
  1986. * called ieee80211_scan_completed() and the userspace called
  1987. * cancel scan scan before ieee80211_scan_work() could run.
  1988. * To handle that, simply return if the scan is not running.
  1989. */
  1990. if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
  1991. iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
  1992. mutex_unlock(&mvm->mutex);
  1993. }
  1994. static void
  1995. iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
  1996. struct ieee80211_sta *sta, u16 tids,
  1997. int num_frames,
  1998. enum ieee80211_frame_release_type reason,
  1999. bool more_data)
  2000. {
  2001. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2002. /* Called when we need to transmit (a) frame(s) from mac80211 */
  2003. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
  2004. tids, more_data, false);
  2005. }
  2006. static void
  2007. iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
  2008. struct ieee80211_sta *sta, u16 tids,
  2009. int num_frames,
  2010. enum ieee80211_frame_release_type reason,
  2011. bool more_data)
  2012. {
  2013. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2014. /* Called when we need to transmit (a) frame(s) from agg or dqa queue */
  2015. iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
  2016. tids, more_data, true);
  2017. }
  2018. static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
  2019. enum sta_notify_cmd cmd,
  2020. struct ieee80211_sta *sta)
  2021. {
  2022. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2023. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2024. unsigned long txqs = 0, tids = 0;
  2025. int tid;
  2026. /*
  2027. * If we have TVQM then we get too high queue numbers - luckily
  2028. * we really shouldn't get here with that because such hardware
  2029. * should have firmware supporting buffer station offload.
  2030. */
  2031. if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
  2032. return;
  2033. spin_lock_bh(&mvmsta->lock);
  2034. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  2035. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  2036. if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
  2037. continue;
  2038. __set_bit(tid_data->txq_id, &txqs);
  2039. if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
  2040. continue;
  2041. __set_bit(tid, &tids);
  2042. }
  2043. switch (cmd) {
  2044. case STA_NOTIFY_SLEEP:
  2045. for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
  2046. ieee80211_sta_set_buffered(sta, tid, true);
  2047. if (txqs)
  2048. iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
  2049. /*
  2050. * The fw updates the STA to be asleep. Tx packets on the Tx
  2051. * queues to this station will not be transmitted. The fw will
  2052. * send a Tx response with TX_STATUS_FAIL_DEST_PS.
  2053. */
  2054. break;
  2055. case STA_NOTIFY_AWAKE:
  2056. if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
  2057. break;
  2058. if (txqs)
  2059. iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
  2060. iwl_mvm_sta_modify_ps_wake(mvm, sta);
  2061. break;
  2062. default:
  2063. break;
  2064. }
  2065. spin_unlock_bh(&mvmsta->lock);
  2066. }
  2067. static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
  2068. struct ieee80211_vif *vif,
  2069. enum sta_notify_cmd cmd,
  2070. struct ieee80211_sta *sta)
  2071. {
  2072. __iwl_mvm_mac_sta_notify(hw, cmd, sta);
  2073. }
  2074. void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
  2075. {
  2076. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  2077. struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
  2078. struct ieee80211_sta *sta;
  2079. struct iwl_mvm_sta *mvmsta;
  2080. bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
  2081. if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
  2082. return;
  2083. rcu_read_lock();
  2084. sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
  2085. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  2086. rcu_read_unlock();
  2087. return;
  2088. }
  2089. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2090. if (!mvmsta->vif ||
  2091. mvmsta->vif->type != NL80211_IFTYPE_AP) {
  2092. rcu_read_unlock();
  2093. return;
  2094. }
  2095. if (mvmsta->sleeping != sleeping) {
  2096. mvmsta->sleeping = sleeping;
  2097. __iwl_mvm_mac_sta_notify(mvm->hw,
  2098. sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
  2099. sta);
  2100. ieee80211_sta_ps_transition(sta, sleeping);
  2101. }
  2102. if (sleeping) {
  2103. switch (notif->type) {
  2104. case IWL_MVM_PM_EVENT_AWAKE:
  2105. case IWL_MVM_PM_EVENT_ASLEEP:
  2106. break;
  2107. case IWL_MVM_PM_EVENT_UAPSD:
  2108. ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
  2109. break;
  2110. case IWL_MVM_PM_EVENT_PS_POLL:
  2111. ieee80211_sta_pspoll(sta);
  2112. break;
  2113. default:
  2114. break;
  2115. }
  2116. }
  2117. rcu_read_unlock();
  2118. }
  2119. static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
  2120. struct ieee80211_vif *vif,
  2121. struct ieee80211_sta *sta)
  2122. {
  2123. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2124. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2125. /*
  2126. * This is called before mac80211 does RCU synchronisation,
  2127. * so here we already invalidate our internal RCU-protected
  2128. * station pointer. The rest of the code will thus no longer
  2129. * be able to find the station this way, and we don't rely
  2130. * on further RCU synchronisation after the sta_state()
  2131. * callback deleted the station.
  2132. */
  2133. mutex_lock(&mvm->mutex);
  2134. if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
  2135. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  2136. ERR_PTR(-ENOENT));
  2137. mutex_unlock(&mvm->mutex);
  2138. }
  2139. static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  2140. const u8 *bssid)
  2141. {
  2142. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
  2143. return;
  2144. if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
  2145. vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
  2146. return;
  2147. }
  2148. if (!vif->p2p &&
  2149. (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
  2150. vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
  2151. return;
  2152. }
  2153. vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
  2154. }
  2155. static void
  2156. iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
  2157. struct ieee80211_vif *vif, u8 *peer_addr,
  2158. enum nl80211_tdls_operation action)
  2159. {
  2160. struct iwl_fw_dbg_trigger_tlv *trig;
  2161. struct iwl_fw_dbg_trigger_tdls *tdls_trig;
  2162. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TDLS))
  2163. return;
  2164. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TDLS);
  2165. tdls_trig = (void *)trig->data;
  2166. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  2167. ieee80211_vif_to_wdev(vif), trig))
  2168. return;
  2169. if (!(tdls_trig->action_bitmap & BIT(action)))
  2170. return;
  2171. if (tdls_trig->peer_mode &&
  2172. memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
  2173. return;
  2174. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
  2175. "TDLS event occurred, peer %pM, action %d",
  2176. peer_addr, action);
  2177. }
  2178. static void iwl_mvm_purge_deferred_tx_frames(struct iwl_mvm *mvm,
  2179. struct iwl_mvm_sta *mvm_sta)
  2180. {
  2181. struct iwl_mvm_tid_data *tid_data;
  2182. struct sk_buff *skb;
  2183. int i;
  2184. spin_lock_bh(&mvm_sta->lock);
  2185. for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
  2186. tid_data = &mvm_sta->tid_data[i];
  2187. while ((skb = __skb_dequeue(&tid_data->deferred_tx_frames))) {
  2188. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2189. /*
  2190. * The first deferred frame should've stopped the MAC
  2191. * queues, so we should never get a second deferred
  2192. * frame for the RA/TID.
  2193. */
  2194. iwl_mvm_start_mac_queues(mvm, BIT(info->hw_queue));
  2195. ieee80211_free_txskb(mvm->hw, skb);
  2196. }
  2197. }
  2198. spin_unlock_bh(&mvm_sta->lock);
  2199. }
  2200. static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
  2201. struct ieee80211_vif *vif,
  2202. struct ieee80211_sta *sta,
  2203. enum ieee80211_sta_state old_state,
  2204. enum ieee80211_sta_state new_state)
  2205. {
  2206. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2207. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2208. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2209. int ret;
  2210. IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
  2211. sta->addr, old_state, new_state);
  2212. /* this would be a mac80211 bug ... but don't crash */
  2213. if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
  2214. return -EINVAL;
  2215. /*
  2216. * If we are in a STA removal flow and in DQA mode:
  2217. *
  2218. * This is after the sync_rcu part, so the queues have already been
  2219. * flushed. No more TXs on their way in mac80211's path, and no more in
  2220. * the queues.
  2221. * Also, we won't be getting any new TX frames for this station.
  2222. * What we might have are deferred TX frames that need to be taken care
  2223. * of.
  2224. *
  2225. * Drop any still-queued deferred-frame before removing the STA, and
  2226. * make sure the worker is no longer handling frames for this STA.
  2227. */
  2228. if (old_state == IEEE80211_STA_NONE &&
  2229. new_state == IEEE80211_STA_NOTEXIST) {
  2230. iwl_mvm_purge_deferred_tx_frames(mvm, mvm_sta);
  2231. flush_work(&mvm->add_stream_wk);
  2232. /*
  2233. * No need to make sure deferred TX indication is off since the
  2234. * worker will already remove it if it was on
  2235. */
  2236. }
  2237. mutex_lock(&mvm->mutex);
  2238. /* track whether or not the station is associated */
  2239. mvm_sta->associated = new_state >= IEEE80211_STA_ASSOC;
  2240. if (old_state == IEEE80211_STA_NOTEXIST &&
  2241. new_state == IEEE80211_STA_NONE) {
  2242. /*
  2243. * Firmware bug - it'll crash if the beacon interval is less
  2244. * than 16. We can't avoid connecting at all, so refuse the
  2245. * station state change, this will cause mac80211 to abandon
  2246. * attempts to connect to this AP, and eventually wpa_s will
  2247. * blacklist the AP...
  2248. */
  2249. if (vif->type == NL80211_IFTYPE_STATION &&
  2250. vif->bss_conf.beacon_int < 16) {
  2251. IWL_ERR(mvm,
  2252. "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
  2253. sta->addr, vif->bss_conf.beacon_int);
  2254. ret = -EINVAL;
  2255. goto out_unlock;
  2256. }
  2257. if (sta->tdls &&
  2258. (vif->p2p ||
  2259. iwl_mvm_tdls_sta_count(mvm, NULL) ==
  2260. IWL_MVM_TDLS_STA_COUNT ||
  2261. iwl_mvm_phy_ctx_count(mvm) > 1)) {
  2262. IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
  2263. ret = -EBUSY;
  2264. goto out_unlock;
  2265. }
  2266. ret = iwl_mvm_add_sta(mvm, vif, sta);
  2267. if (sta->tdls && ret == 0) {
  2268. iwl_mvm_recalc_tdls_state(mvm, vif, true);
  2269. iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
  2270. NL80211_TDLS_SETUP);
  2271. }
  2272. } else if (old_state == IEEE80211_STA_NONE &&
  2273. new_state == IEEE80211_STA_AUTH) {
  2274. /*
  2275. * EBS may be disabled due to previous failures reported by FW.
  2276. * Reset EBS status here assuming environment has been changed.
  2277. */
  2278. mvm->last_ebs_successful = true;
  2279. iwl_mvm_check_uapsd(mvm, vif, sta->addr);
  2280. ret = 0;
  2281. } else if (old_state == IEEE80211_STA_AUTH &&
  2282. new_state == IEEE80211_STA_ASSOC) {
  2283. if (vif->type == NL80211_IFTYPE_AP) {
  2284. mvmvif->ap_assoc_sta_count++;
  2285. iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  2286. }
  2287. iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
  2288. true);
  2289. ret = iwl_mvm_update_sta(mvm, vif, sta);
  2290. } else if (old_state == IEEE80211_STA_ASSOC &&
  2291. new_state == IEEE80211_STA_AUTHORIZED) {
  2292. /* we don't support TDLS during DCM */
  2293. if (iwl_mvm_phy_ctx_count(mvm) > 1)
  2294. iwl_mvm_teardown_tdls_peers(mvm);
  2295. if (sta->tdls)
  2296. iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
  2297. NL80211_TDLS_ENABLE_LINK);
  2298. /* enable beacon filtering */
  2299. WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
  2300. iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
  2301. false);
  2302. ret = 0;
  2303. } else if (old_state == IEEE80211_STA_AUTHORIZED &&
  2304. new_state == IEEE80211_STA_ASSOC) {
  2305. /* disable beacon filtering */
  2306. WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
  2307. ret = 0;
  2308. } else if (old_state == IEEE80211_STA_ASSOC &&
  2309. new_state == IEEE80211_STA_AUTH) {
  2310. if (vif->type == NL80211_IFTYPE_AP) {
  2311. mvmvif->ap_assoc_sta_count--;
  2312. iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  2313. }
  2314. ret = 0;
  2315. } else if (old_state == IEEE80211_STA_AUTH &&
  2316. new_state == IEEE80211_STA_NONE) {
  2317. ret = 0;
  2318. } else if (old_state == IEEE80211_STA_NONE &&
  2319. new_state == IEEE80211_STA_NOTEXIST) {
  2320. ret = iwl_mvm_rm_sta(mvm, vif, sta);
  2321. if (sta->tdls) {
  2322. iwl_mvm_recalc_tdls_state(mvm, vif, false);
  2323. iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
  2324. NL80211_TDLS_DISABLE_LINK);
  2325. }
  2326. } else {
  2327. ret = -EIO;
  2328. }
  2329. out_unlock:
  2330. mutex_unlock(&mvm->mutex);
  2331. if (sta->tdls && ret == 0) {
  2332. if (old_state == IEEE80211_STA_NOTEXIST &&
  2333. new_state == IEEE80211_STA_NONE)
  2334. ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
  2335. else if (old_state == IEEE80211_STA_NONE &&
  2336. new_state == IEEE80211_STA_NOTEXIST)
  2337. ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
  2338. }
  2339. return ret;
  2340. }
  2341. static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  2342. {
  2343. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2344. mvm->rts_threshold = value;
  2345. return 0;
  2346. }
  2347. static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
  2348. struct ieee80211_vif *vif,
  2349. struct ieee80211_sta *sta, u32 changed)
  2350. {
  2351. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2352. if (vif->type == NL80211_IFTYPE_STATION &&
  2353. changed & IEEE80211_RC_NSS_CHANGED)
  2354. iwl_mvm_sf_update(mvm, vif, false);
  2355. }
  2356. static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
  2357. struct ieee80211_vif *vif, u16 ac,
  2358. const struct ieee80211_tx_queue_params *params)
  2359. {
  2360. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2361. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2362. mvmvif->queue_params[ac] = *params;
  2363. /*
  2364. * No need to update right away, we'll get BSS_CHANGED_QOS
  2365. * The exception is P2P_DEVICE interface which needs immediate update.
  2366. */
  2367. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  2368. int ret;
  2369. mutex_lock(&mvm->mutex);
  2370. ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  2371. mutex_unlock(&mvm->mutex);
  2372. return ret;
  2373. }
  2374. return 0;
  2375. }
  2376. static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
  2377. struct ieee80211_vif *vif)
  2378. {
  2379. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2380. u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
  2381. u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
  2382. /*
  2383. * iwl_mvm_protect_session() reads directly from the device
  2384. * (the system time), so make sure it is available.
  2385. */
  2386. if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
  2387. return;
  2388. mutex_lock(&mvm->mutex);
  2389. /* Try really hard to protect the session and hear a beacon */
  2390. iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
  2391. mutex_unlock(&mvm->mutex);
  2392. iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
  2393. }
  2394. static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
  2395. struct ieee80211_vif *vif,
  2396. struct cfg80211_sched_scan_request *req,
  2397. struct ieee80211_scan_ies *ies)
  2398. {
  2399. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2400. int ret;
  2401. mutex_lock(&mvm->mutex);
  2402. if (!vif->bss_conf.idle) {
  2403. ret = -EBUSY;
  2404. goto out;
  2405. }
  2406. ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
  2407. out:
  2408. mutex_unlock(&mvm->mutex);
  2409. return ret;
  2410. }
  2411. static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
  2412. struct ieee80211_vif *vif)
  2413. {
  2414. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2415. int ret;
  2416. mutex_lock(&mvm->mutex);
  2417. /* Due to a race condition, it's possible that mac80211 asks
  2418. * us to stop a sched_scan when it's already stopped. This
  2419. * can happen, for instance, if we stopped the scan ourselves,
  2420. * called ieee80211_sched_scan_stopped() and the userspace called
  2421. * stop sched scan scan before ieee80211_sched_scan_stopped_work()
  2422. * could run. To handle this, simply return if the scan is
  2423. * not running.
  2424. */
  2425. if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
  2426. mutex_unlock(&mvm->mutex);
  2427. return 0;
  2428. }
  2429. ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
  2430. mutex_unlock(&mvm->mutex);
  2431. iwl_mvm_wait_for_async_handlers(mvm);
  2432. return ret;
  2433. }
  2434. static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
  2435. enum set_key_cmd cmd,
  2436. struct ieee80211_vif *vif,
  2437. struct ieee80211_sta *sta,
  2438. struct ieee80211_key_conf *key)
  2439. {
  2440. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2441. struct iwl_mvm_sta *mvmsta;
  2442. struct iwl_mvm_key_pn *ptk_pn;
  2443. int keyidx = key->keyidx;
  2444. int ret;
  2445. u8 key_offset;
  2446. if (iwlwifi_mod_params.swcrypto) {
  2447. IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
  2448. return -EOPNOTSUPP;
  2449. }
  2450. switch (key->cipher) {
  2451. case WLAN_CIPHER_SUITE_TKIP:
  2452. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2453. key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  2454. break;
  2455. case WLAN_CIPHER_SUITE_CCMP:
  2456. case WLAN_CIPHER_SUITE_GCMP:
  2457. case WLAN_CIPHER_SUITE_GCMP_256:
  2458. if (!iwl_mvm_has_new_tx_api(mvm))
  2459. key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  2460. break;
  2461. case WLAN_CIPHER_SUITE_AES_CMAC:
  2462. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  2463. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  2464. WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
  2465. break;
  2466. case WLAN_CIPHER_SUITE_WEP40:
  2467. case WLAN_CIPHER_SUITE_WEP104:
  2468. /* For non-client mode, only use WEP keys for TX as we probably
  2469. * don't have a station yet anyway and would then have to keep
  2470. * track of the keys, linking them to each of the clients/peers
  2471. * as they appear. For now, don't do that, for performance WEP
  2472. * offload doesn't really matter much, but we need it for some
  2473. * other offload features in client mode.
  2474. */
  2475. if (vif->type != NL80211_IFTYPE_STATION)
  2476. return 0;
  2477. break;
  2478. default:
  2479. /* currently FW supports only one optional cipher scheme */
  2480. if (hw->n_cipher_schemes &&
  2481. hw->cipher_schemes->cipher == key->cipher)
  2482. key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
  2483. else
  2484. return -EOPNOTSUPP;
  2485. }
  2486. mutex_lock(&mvm->mutex);
  2487. switch (cmd) {
  2488. case SET_KEY:
  2489. if ((vif->type == NL80211_IFTYPE_ADHOC ||
  2490. vif->type == NL80211_IFTYPE_AP) && !sta) {
  2491. /*
  2492. * GTK on AP interface is a TX-only key, return 0;
  2493. * on IBSS they're per-station and because we're lazy
  2494. * we don't support them for RX, so do the same.
  2495. * CMAC/GMAC in AP/IBSS modes must be done in software.
  2496. */
  2497. if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
  2498. key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
  2499. key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
  2500. ret = -EOPNOTSUPP;
  2501. else
  2502. ret = 0;
  2503. if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
  2504. key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
  2505. !iwl_mvm_has_new_tx_api(mvm)) {
  2506. key->hw_key_idx = STA_KEY_IDX_INVALID;
  2507. break;
  2508. }
  2509. }
  2510. /* During FW restart, in order to restore the state as it was,
  2511. * don't try to reprogram keys we previously failed for.
  2512. */
  2513. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  2514. key->hw_key_idx == STA_KEY_IDX_INVALID) {
  2515. IWL_DEBUG_MAC80211(mvm,
  2516. "skip invalid idx key programming during restart\n");
  2517. ret = 0;
  2518. break;
  2519. }
  2520. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  2521. sta && iwl_mvm_has_new_rx_api(mvm) &&
  2522. key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
  2523. (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
  2524. key->cipher == WLAN_CIPHER_SUITE_GCMP ||
  2525. key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
  2526. struct ieee80211_key_seq seq;
  2527. int tid, q;
  2528. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2529. WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
  2530. ptk_pn = kzalloc(struct_size(ptk_pn, q,
  2531. mvm->trans->num_rx_queues),
  2532. GFP_KERNEL);
  2533. if (!ptk_pn) {
  2534. ret = -ENOMEM;
  2535. break;
  2536. }
  2537. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
  2538. ieee80211_get_key_rx_seq(key, tid, &seq);
  2539. for (q = 0; q < mvm->trans->num_rx_queues; q++)
  2540. memcpy(ptk_pn->q[q].pn[tid],
  2541. seq.ccmp.pn,
  2542. IEEE80211_CCMP_PN_LEN);
  2543. }
  2544. rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
  2545. }
  2546. /* in HW restart reuse the index, otherwise request a new one */
  2547. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  2548. key_offset = key->hw_key_idx;
  2549. else
  2550. key_offset = STA_KEY_IDX_INVALID;
  2551. IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
  2552. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
  2553. if (ret) {
  2554. IWL_WARN(mvm, "set key failed\n");
  2555. /*
  2556. * can't add key for RX, but we don't need it
  2557. * in the device for TX so still return 0
  2558. */
  2559. key->hw_key_idx = STA_KEY_IDX_INVALID;
  2560. ret = 0;
  2561. }
  2562. break;
  2563. case DISABLE_KEY:
  2564. if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
  2565. ret = 0;
  2566. break;
  2567. }
  2568. if (sta && iwl_mvm_has_new_rx_api(mvm) &&
  2569. key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
  2570. (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
  2571. key->cipher == WLAN_CIPHER_SUITE_GCMP ||
  2572. key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
  2573. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2574. ptk_pn = rcu_dereference_protected(
  2575. mvmsta->ptk_pn[keyidx],
  2576. lockdep_is_held(&mvm->mutex));
  2577. RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
  2578. if (ptk_pn)
  2579. kfree_rcu(ptk_pn, rcu_head);
  2580. }
  2581. IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
  2582. ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
  2583. break;
  2584. default:
  2585. ret = -EINVAL;
  2586. }
  2587. mutex_unlock(&mvm->mutex);
  2588. return ret;
  2589. }
  2590. static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
  2591. struct ieee80211_vif *vif,
  2592. struct ieee80211_key_conf *keyconf,
  2593. struct ieee80211_sta *sta,
  2594. u32 iv32, u16 *phase1key)
  2595. {
  2596. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2597. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  2598. return;
  2599. iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
  2600. }
  2601. static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
  2602. struct iwl_rx_packet *pkt, void *data)
  2603. {
  2604. struct iwl_mvm *mvm =
  2605. container_of(notif_wait, struct iwl_mvm, notif_wait);
  2606. struct iwl_hs20_roc_res *resp;
  2607. int resp_len = iwl_rx_packet_payload_len(pkt);
  2608. struct iwl_mvm_time_event_data *te_data = data;
  2609. if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
  2610. return true;
  2611. if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
  2612. IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
  2613. return true;
  2614. }
  2615. resp = (void *)pkt->data;
  2616. IWL_DEBUG_TE(mvm,
  2617. "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
  2618. resp->status, resp->event_unique_id);
  2619. te_data->uid = le32_to_cpu(resp->event_unique_id);
  2620. IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
  2621. te_data->uid);
  2622. spin_lock_bh(&mvm->time_event_lock);
  2623. list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
  2624. spin_unlock_bh(&mvm->time_event_lock);
  2625. return true;
  2626. }
  2627. #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
  2628. #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
  2629. #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
  2630. #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
  2631. #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
  2632. static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
  2633. struct ieee80211_channel *channel,
  2634. struct ieee80211_vif *vif,
  2635. int duration)
  2636. {
  2637. int res, time_reg = DEVICE_SYSTEM_TIME_REG;
  2638. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2639. struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
  2640. static const u16 time_event_response[] = { HOT_SPOT_CMD };
  2641. struct iwl_notification_wait wait_time_event;
  2642. u32 dtim_interval = vif->bss_conf.dtim_period *
  2643. vif->bss_conf.beacon_int;
  2644. u32 req_dur, delay;
  2645. struct iwl_hs20_roc_req aux_roc_req = {
  2646. .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
  2647. .id_and_color =
  2648. cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
  2649. .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
  2650. /* Set the channel info data */
  2651. .channel_info.band = (channel->band == NL80211_BAND_2GHZ) ?
  2652. PHY_BAND_24 : PHY_BAND_5,
  2653. .channel_info.channel = channel->hw_value,
  2654. .channel_info.width = PHY_VHT_CHANNEL_MODE20,
  2655. /* Set the time and duration */
  2656. .apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
  2657. };
  2658. delay = AUX_ROC_MIN_DELAY;
  2659. req_dur = MSEC_TO_TU(duration);
  2660. /*
  2661. * If we are associated we want the delay time to be at least one
  2662. * dtim interval so that the FW can wait until after the DTIM and
  2663. * then start the time event, this will potentially allow us to
  2664. * remain off-channel for the max duration.
  2665. * Since we want to use almost a whole dtim interval we would also
  2666. * like the delay to be for 2-3 dtim intervals, in case there are
  2667. * other time events with higher priority.
  2668. */
  2669. if (vif->bss_conf.assoc) {
  2670. delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
  2671. /* We cannot remain off-channel longer than the DTIM interval */
  2672. if (dtim_interval <= req_dur) {
  2673. req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
  2674. if (req_dur <= AUX_ROC_MIN_DURATION)
  2675. req_dur = dtim_interval -
  2676. AUX_ROC_MIN_SAFETY_BUFFER;
  2677. }
  2678. }
  2679. aux_roc_req.duration = cpu_to_le32(req_dur);
  2680. aux_roc_req.apply_time_max_delay = cpu_to_le32(delay);
  2681. IWL_DEBUG_TE(mvm,
  2682. "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
  2683. channel->hw_value, req_dur, duration, delay,
  2684. dtim_interval);
  2685. /* Set the node address */
  2686. memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
  2687. lockdep_assert_held(&mvm->mutex);
  2688. spin_lock_bh(&mvm->time_event_lock);
  2689. if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
  2690. spin_unlock_bh(&mvm->time_event_lock);
  2691. return -EIO;
  2692. }
  2693. te_data->vif = vif;
  2694. te_data->duration = duration;
  2695. te_data->id = HOT_SPOT_CMD;
  2696. spin_unlock_bh(&mvm->time_event_lock);
  2697. /*
  2698. * Use a notification wait, which really just processes the
  2699. * command response and doesn't wait for anything, in order
  2700. * to be able to process the response and get the UID inside
  2701. * the RX path. Using CMD_WANT_SKB doesn't work because it
  2702. * stores the buffer and then wakes up this thread, by which
  2703. * time another notification (that the time event started)
  2704. * might already be processed unsuccessfully.
  2705. */
  2706. iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
  2707. time_event_response,
  2708. ARRAY_SIZE(time_event_response),
  2709. iwl_mvm_rx_aux_roc, te_data);
  2710. res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
  2711. &aux_roc_req);
  2712. if (res) {
  2713. IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
  2714. iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
  2715. goto out_clear_te;
  2716. }
  2717. /* No need to wait for anything, so just pass 1 (0 isn't valid) */
  2718. res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
  2719. /* should never fail */
  2720. WARN_ON_ONCE(res);
  2721. if (res) {
  2722. out_clear_te:
  2723. spin_lock_bh(&mvm->time_event_lock);
  2724. iwl_mvm_te_clear_data(mvm, te_data);
  2725. spin_unlock_bh(&mvm->time_event_lock);
  2726. }
  2727. return res;
  2728. }
  2729. static int iwl_mvm_roc(struct ieee80211_hw *hw,
  2730. struct ieee80211_vif *vif,
  2731. struct ieee80211_channel *channel,
  2732. int duration,
  2733. enum ieee80211_roc_type type)
  2734. {
  2735. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2736. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2737. struct cfg80211_chan_def chandef;
  2738. struct iwl_mvm_phy_ctxt *phy_ctxt;
  2739. int ret, i;
  2740. IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
  2741. duration, type);
  2742. /*
  2743. * Flush the done work, just in case it's still pending, so that
  2744. * the work it does can complete and we can accept new frames.
  2745. */
  2746. flush_work(&mvm->roc_done_wk);
  2747. mutex_lock(&mvm->mutex);
  2748. switch (vif->type) {
  2749. case NL80211_IFTYPE_STATION:
  2750. if (fw_has_capa(&mvm->fw->ucode_capa,
  2751. IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
  2752. /* Use aux roc framework (HS20) */
  2753. ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
  2754. vif, duration);
  2755. goto out_unlock;
  2756. }
  2757. IWL_ERR(mvm, "hotspot not supported\n");
  2758. ret = -EINVAL;
  2759. goto out_unlock;
  2760. case NL80211_IFTYPE_P2P_DEVICE:
  2761. /* handle below */
  2762. break;
  2763. default:
  2764. IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
  2765. ret = -EINVAL;
  2766. goto out_unlock;
  2767. }
  2768. for (i = 0; i < NUM_PHY_CTX; i++) {
  2769. phy_ctxt = &mvm->phy_ctxts[i];
  2770. if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
  2771. continue;
  2772. if (phy_ctxt->ref && channel == phy_ctxt->channel) {
  2773. /*
  2774. * Unbind the P2P_DEVICE from the current PHY context,
  2775. * and if the PHY context is not used remove it.
  2776. */
  2777. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  2778. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  2779. goto out_unlock;
  2780. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  2781. /* Bind the P2P_DEVICE to the current PHY Context */
  2782. mvmvif->phy_ctxt = phy_ctxt;
  2783. ret = iwl_mvm_binding_add_vif(mvm, vif);
  2784. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  2785. goto out_unlock;
  2786. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  2787. goto schedule_time_event;
  2788. }
  2789. }
  2790. /* Need to update the PHY context only if the ROC channel changed */
  2791. if (channel == mvmvif->phy_ctxt->channel)
  2792. goto schedule_time_event;
  2793. cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
  2794. /*
  2795. * Change the PHY context configuration as it is currently referenced
  2796. * only by the P2P Device MAC
  2797. */
  2798. if (mvmvif->phy_ctxt->ref == 1) {
  2799. ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
  2800. &chandef, 1, 1);
  2801. if (ret)
  2802. goto out_unlock;
  2803. } else {
  2804. /*
  2805. * The PHY context is shared with other MACs. Need to remove the
  2806. * P2P Device from the binding, allocate an new PHY context and
  2807. * create a new binding
  2808. */
  2809. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  2810. if (!phy_ctxt) {
  2811. ret = -ENOSPC;
  2812. goto out_unlock;
  2813. }
  2814. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
  2815. 1, 1);
  2816. if (ret) {
  2817. IWL_ERR(mvm, "Failed to change PHY context\n");
  2818. goto out_unlock;
  2819. }
  2820. /* Unbind the P2P_DEVICE from the current PHY context */
  2821. ret = iwl_mvm_binding_remove_vif(mvm, vif);
  2822. if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
  2823. goto out_unlock;
  2824. iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
  2825. /* Bind the P2P_DEVICE to the new allocated PHY context */
  2826. mvmvif->phy_ctxt = phy_ctxt;
  2827. ret = iwl_mvm_binding_add_vif(mvm, vif);
  2828. if (WARN(ret, "Failed binding P2P_DEVICE\n"))
  2829. goto out_unlock;
  2830. iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
  2831. }
  2832. schedule_time_event:
  2833. /* Schedule the time events */
  2834. ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
  2835. out_unlock:
  2836. mutex_unlock(&mvm->mutex);
  2837. IWL_DEBUG_MAC80211(mvm, "leave\n");
  2838. return ret;
  2839. }
  2840. static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
  2841. {
  2842. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2843. IWL_DEBUG_MAC80211(mvm, "enter\n");
  2844. mutex_lock(&mvm->mutex);
  2845. iwl_mvm_stop_roc(mvm);
  2846. mutex_unlock(&mvm->mutex);
  2847. IWL_DEBUG_MAC80211(mvm, "leave\n");
  2848. return 0;
  2849. }
  2850. static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
  2851. struct ieee80211_chanctx_conf *ctx)
  2852. {
  2853. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2854. struct iwl_mvm_phy_ctxt *phy_ctxt;
  2855. int ret;
  2856. lockdep_assert_held(&mvm->mutex);
  2857. IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
  2858. phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
  2859. if (!phy_ctxt) {
  2860. ret = -ENOSPC;
  2861. goto out;
  2862. }
  2863. ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
  2864. ctx->rx_chains_static,
  2865. ctx->rx_chains_dynamic);
  2866. if (ret) {
  2867. IWL_ERR(mvm, "Failed to add PHY context\n");
  2868. goto out;
  2869. }
  2870. iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
  2871. *phy_ctxt_id = phy_ctxt->id;
  2872. out:
  2873. return ret;
  2874. }
  2875. static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
  2876. struct ieee80211_chanctx_conf *ctx)
  2877. {
  2878. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2879. int ret;
  2880. mutex_lock(&mvm->mutex);
  2881. ret = __iwl_mvm_add_chanctx(mvm, ctx);
  2882. mutex_unlock(&mvm->mutex);
  2883. return ret;
  2884. }
  2885. static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
  2886. struct ieee80211_chanctx_conf *ctx)
  2887. {
  2888. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2889. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  2890. lockdep_assert_held(&mvm->mutex);
  2891. iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
  2892. }
  2893. static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
  2894. struct ieee80211_chanctx_conf *ctx)
  2895. {
  2896. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2897. mutex_lock(&mvm->mutex);
  2898. __iwl_mvm_remove_chanctx(mvm, ctx);
  2899. mutex_unlock(&mvm->mutex);
  2900. }
  2901. static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
  2902. struct ieee80211_chanctx_conf *ctx,
  2903. u32 changed)
  2904. {
  2905. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  2906. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2907. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  2908. if (WARN_ONCE((phy_ctxt->ref > 1) &&
  2909. (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
  2910. IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
  2911. IEEE80211_CHANCTX_CHANGE_RADAR |
  2912. IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
  2913. "Cannot change PHY. Ref=%d, changed=0x%X\n",
  2914. phy_ctxt->ref, changed))
  2915. return;
  2916. mutex_lock(&mvm->mutex);
  2917. /* we are only changing the min_width, may be a noop */
  2918. if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
  2919. if (phy_ctxt->width == ctx->min_def.width)
  2920. goto out_unlock;
  2921. /* we are just toggling between 20_NOHT and 20 */
  2922. if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
  2923. ctx->min_def.width <= NL80211_CHAN_WIDTH_20)
  2924. goto out_unlock;
  2925. }
  2926. iwl_mvm_bt_coex_vif_change(mvm);
  2927. iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
  2928. ctx->rx_chains_static,
  2929. ctx->rx_chains_dynamic);
  2930. out_unlock:
  2931. mutex_unlock(&mvm->mutex);
  2932. }
  2933. static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
  2934. struct ieee80211_vif *vif,
  2935. struct ieee80211_chanctx_conf *ctx,
  2936. bool switching_chanctx)
  2937. {
  2938. u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
  2939. struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
  2940. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2941. int ret;
  2942. lockdep_assert_held(&mvm->mutex);
  2943. mvmvif->phy_ctxt = phy_ctxt;
  2944. switch (vif->type) {
  2945. case NL80211_IFTYPE_AP:
  2946. /* only needed if we're switching chanctx (i.e. during CSA) */
  2947. if (switching_chanctx) {
  2948. mvmvif->ap_ibss_active = true;
  2949. break;
  2950. }
  2951. case NL80211_IFTYPE_ADHOC:
  2952. /*
  2953. * The AP binding flow is handled as part of the start_ap flow
  2954. * (in bss_info_changed), similarly for IBSS.
  2955. */
  2956. ret = 0;
  2957. goto out;
  2958. case NL80211_IFTYPE_STATION:
  2959. mvmvif->csa_bcn_pending = false;
  2960. break;
  2961. case NL80211_IFTYPE_MONITOR:
  2962. /* always disable PS when a monitor interface is active */
  2963. mvmvif->ps_disabled = true;
  2964. break;
  2965. default:
  2966. ret = -EINVAL;
  2967. goto out;
  2968. }
  2969. ret = iwl_mvm_binding_add_vif(mvm, vif);
  2970. if (ret)
  2971. goto out;
  2972. /*
  2973. * Power state must be updated before quotas,
  2974. * otherwise fw will complain.
  2975. */
  2976. iwl_mvm_power_update_mac(mvm);
  2977. /* Setting the quota at this stage is only required for monitor
  2978. * interfaces. For the other types, the bss_info changed flow
  2979. * will handle quota settings.
  2980. */
  2981. if (vif->type == NL80211_IFTYPE_MONITOR) {
  2982. mvmvif->monitor_active = true;
  2983. ret = iwl_mvm_update_quotas(mvm, false, NULL);
  2984. if (ret)
  2985. goto out_remove_binding;
  2986. ret = iwl_mvm_add_snif_sta(mvm, vif);
  2987. if (ret)
  2988. goto out_remove_binding;
  2989. }
  2990. /* Handle binding during CSA */
  2991. if (vif->type == NL80211_IFTYPE_AP) {
  2992. iwl_mvm_update_quotas(mvm, false, NULL);
  2993. iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  2994. }
  2995. if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
  2996. u32 duration = 3 * vif->bss_conf.beacon_int;
  2997. /* iwl_mvm_protect_session() reads directly from the
  2998. * device (the system time), so make sure it is
  2999. * available.
  3000. */
  3001. ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
  3002. if (ret)
  3003. goto out_remove_binding;
  3004. /* Protect the session to make sure we hear the first
  3005. * beacon on the new channel.
  3006. */
  3007. mvmvif->csa_bcn_pending = true;
  3008. iwl_mvm_protect_session(mvm, vif, duration, duration,
  3009. vif->bss_conf.beacon_int / 2,
  3010. true);
  3011. iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
  3012. iwl_mvm_update_quotas(mvm, false, NULL);
  3013. }
  3014. goto out;
  3015. out_remove_binding:
  3016. iwl_mvm_binding_remove_vif(mvm, vif);
  3017. iwl_mvm_power_update_mac(mvm);
  3018. out:
  3019. if (ret)
  3020. mvmvif->phy_ctxt = NULL;
  3021. return ret;
  3022. }
  3023. static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
  3024. struct ieee80211_vif *vif,
  3025. struct ieee80211_chanctx_conf *ctx)
  3026. {
  3027. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3028. int ret;
  3029. mutex_lock(&mvm->mutex);
  3030. ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
  3031. mutex_unlock(&mvm->mutex);
  3032. return ret;
  3033. }
  3034. static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
  3035. struct ieee80211_vif *vif,
  3036. struct ieee80211_chanctx_conf *ctx,
  3037. bool switching_chanctx)
  3038. {
  3039. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  3040. struct ieee80211_vif *disabled_vif = NULL;
  3041. lockdep_assert_held(&mvm->mutex);
  3042. iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
  3043. switch (vif->type) {
  3044. case NL80211_IFTYPE_ADHOC:
  3045. goto out;
  3046. case NL80211_IFTYPE_MONITOR:
  3047. mvmvif->monitor_active = false;
  3048. mvmvif->ps_disabled = false;
  3049. iwl_mvm_rm_snif_sta(mvm, vif);
  3050. break;
  3051. case NL80211_IFTYPE_AP:
  3052. /* This part is triggered only during CSA */
  3053. if (!switching_chanctx || !mvmvif->ap_ibss_active)
  3054. goto out;
  3055. mvmvif->csa_countdown = false;
  3056. /* Set CS bit on all the stations */
  3057. iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
  3058. /* Save blocked iface, the timeout is set on the next beacon */
  3059. rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
  3060. mvmvif->ap_ibss_active = false;
  3061. break;
  3062. case NL80211_IFTYPE_STATION:
  3063. if (!switching_chanctx)
  3064. break;
  3065. disabled_vif = vif;
  3066. iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
  3067. break;
  3068. default:
  3069. break;
  3070. }
  3071. iwl_mvm_update_quotas(mvm, false, disabled_vif);
  3072. iwl_mvm_binding_remove_vif(mvm, vif);
  3073. out:
  3074. mvmvif->phy_ctxt = NULL;
  3075. iwl_mvm_power_update_mac(mvm);
  3076. }
  3077. static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
  3078. struct ieee80211_vif *vif,
  3079. struct ieee80211_chanctx_conf *ctx)
  3080. {
  3081. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3082. mutex_lock(&mvm->mutex);
  3083. __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
  3084. mutex_unlock(&mvm->mutex);
  3085. }
  3086. static int
  3087. iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
  3088. struct ieee80211_vif_chanctx_switch *vifs)
  3089. {
  3090. int ret;
  3091. mutex_lock(&mvm->mutex);
  3092. __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
  3093. __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
  3094. ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
  3095. if (ret) {
  3096. IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
  3097. goto out_reassign;
  3098. }
  3099. ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
  3100. true);
  3101. if (ret) {
  3102. IWL_ERR(mvm,
  3103. "failed to assign new_ctx during channel switch\n");
  3104. goto out_remove;
  3105. }
  3106. /* we don't support TDLS during DCM - can be caused by channel switch */
  3107. if (iwl_mvm_phy_ctx_count(mvm) > 1)
  3108. iwl_mvm_teardown_tdls_peers(mvm);
  3109. goto out;
  3110. out_remove:
  3111. __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
  3112. out_reassign:
  3113. if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
  3114. IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
  3115. goto out_restart;
  3116. }
  3117. if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
  3118. true)) {
  3119. IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
  3120. goto out_restart;
  3121. }
  3122. goto out;
  3123. out_restart:
  3124. /* things keep failing, better restart the hw */
  3125. iwl_mvm_nic_restart(mvm, false);
  3126. out:
  3127. mutex_unlock(&mvm->mutex);
  3128. return ret;
  3129. }
  3130. static int
  3131. iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
  3132. struct ieee80211_vif_chanctx_switch *vifs)
  3133. {
  3134. int ret;
  3135. mutex_lock(&mvm->mutex);
  3136. __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
  3137. ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
  3138. true);
  3139. if (ret) {
  3140. IWL_ERR(mvm,
  3141. "failed to assign new_ctx during channel switch\n");
  3142. goto out_reassign;
  3143. }
  3144. goto out;
  3145. out_reassign:
  3146. if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
  3147. true)) {
  3148. IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
  3149. goto out_restart;
  3150. }
  3151. goto out;
  3152. out_restart:
  3153. /* things keep failing, better restart the hw */
  3154. iwl_mvm_nic_restart(mvm, false);
  3155. out:
  3156. mutex_unlock(&mvm->mutex);
  3157. return ret;
  3158. }
  3159. static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
  3160. struct ieee80211_vif_chanctx_switch *vifs,
  3161. int n_vifs,
  3162. enum ieee80211_chanctx_switch_mode mode)
  3163. {
  3164. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3165. int ret;
  3166. /* we only support a single-vif right now */
  3167. if (n_vifs > 1)
  3168. return -EOPNOTSUPP;
  3169. switch (mode) {
  3170. case CHANCTX_SWMODE_SWAP_CONTEXTS:
  3171. ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
  3172. break;
  3173. case CHANCTX_SWMODE_REASSIGN_VIF:
  3174. ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
  3175. break;
  3176. default:
  3177. ret = -EOPNOTSUPP;
  3178. break;
  3179. }
  3180. return ret;
  3181. }
  3182. static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
  3183. {
  3184. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3185. return mvm->ibss_manager;
  3186. }
  3187. static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
  3188. struct ieee80211_sta *sta,
  3189. bool set)
  3190. {
  3191. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3192. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  3193. if (!mvm_sta || !mvm_sta->vif) {
  3194. IWL_ERR(mvm, "Station is not associated to a vif\n");
  3195. return -EINVAL;
  3196. }
  3197. return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
  3198. }
  3199. #ifdef CONFIG_NL80211_TESTMODE
  3200. static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
  3201. [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
  3202. [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
  3203. [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
  3204. };
  3205. static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
  3206. struct ieee80211_vif *vif,
  3207. void *data, int len)
  3208. {
  3209. struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
  3210. int err;
  3211. u32 noa_duration;
  3212. err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy,
  3213. NULL);
  3214. if (err)
  3215. return err;
  3216. if (!tb[IWL_MVM_TM_ATTR_CMD])
  3217. return -EINVAL;
  3218. switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
  3219. case IWL_MVM_TM_CMD_SET_NOA:
  3220. if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
  3221. !vif->bss_conf.enable_beacon ||
  3222. !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
  3223. return -EINVAL;
  3224. noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
  3225. if (noa_duration >= vif->bss_conf.beacon_int)
  3226. return -EINVAL;
  3227. mvm->noa_duration = noa_duration;
  3228. mvm->noa_vif = vif;
  3229. return iwl_mvm_update_quotas(mvm, true, NULL);
  3230. case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
  3231. /* must be associated client vif - ignore authorized */
  3232. if (!vif || vif->type != NL80211_IFTYPE_STATION ||
  3233. !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
  3234. !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
  3235. return -EINVAL;
  3236. if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
  3237. return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
  3238. return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
  3239. }
  3240. return -EOPNOTSUPP;
  3241. }
  3242. static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
  3243. struct ieee80211_vif *vif,
  3244. void *data, int len)
  3245. {
  3246. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3247. int err;
  3248. mutex_lock(&mvm->mutex);
  3249. err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
  3250. mutex_unlock(&mvm->mutex);
  3251. return err;
  3252. }
  3253. #endif
  3254. static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
  3255. struct ieee80211_vif *vif,
  3256. struct ieee80211_channel_switch *chsw)
  3257. {
  3258. /* By implementing this operation, we prevent mac80211 from
  3259. * starting its own channel switch timer, so that we can call
  3260. * ieee80211_chswitch_done() ourselves at the right time
  3261. * (which is when the absence time event starts).
  3262. */
  3263. IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
  3264. "dummy channel switch op\n");
  3265. }
  3266. static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
  3267. struct ieee80211_vif *vif,
  3268. struct ieee80211_channel_switch *chsw)
  3269. {
  3270. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3271. struct ieee80211_vif *csa_vif;
  3272. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  3273. u32 apply_time;
  3274. int ret;
  3275. mutex_lock(&mvm->mutex);
  3276. mvmvif->csa_failed = false;
  3277. IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
  3278. chsw->chandef.center_freq1);
  3279. iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
  3280. ieee80211_vif_to_wdev(vif),
  3281. FW_DBG_TRIGGER_CHANNEL_SWITCH);
  3282. switch (vif->type) {
  3283. case NL80211_IFTYPE_AP:
  3284. csa_vif =
  3285. rcu_dereference_protected(mvm->csa_vif,
  3286. lockdep_is_held(&mvm->mutex));
  3287. if (WARN_ONCE(csa_vif && csa_vif->csa_active,
  3288. "Another CSA is already in progress")) {
  3289. ret = -EBUSY;
  3290. goto out_unlock;
  3291. }
  3292. /* we still didn't unblock tx. prevent new CS meanwhile */
  3293. if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
  3294. lockdep_is_held(&mvm->mutex))) {
  3295. ret = -EBUSY;
  3296. goto out_unlock;
  3297. }
  3298. rcu_assign_pointer(mvm->csa_vif, vif);
  3299. if (WARN_ONCE(mvmvif->csa_countdown,
  3300. "Previous CSA countdown didn't complete")) {
  3301. ret = -EBUSY;
  3302. goto out_unlock;
  3303. }
  3304. mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
  3305. break;
  3306. case NL80211_IFTYPE_STATION:
  3307. /* Schedule the time event to a bit before beacon 1,
  3308. * to make sure we're in the new channel when the
  3309. * GO/AP arrives. In case count <= 1 immediately schedule the
  3310. * TE (this might result with some packet loss or connection
  3311. * loss).
  3312. */
  3313. if (chsw->count <= 1)
  3314. apply_time = 0;
  3315. else
  3316. apply_time = chsw->device_timestamp +
  3317. ((vif->bss_conf.beacon_int * (chsw->count - 1) -
  3318. IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
  3319. if (chsw->block_tx)
  3320. iwl_mvm_csa_client_absent(mvm, vif);
  3321. iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
  3322. apply_time);
  3323. if (mvmvif->bf_data.bf_enabled) {
  3324. ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
  3325. if (ret)
  3326. goto out_unlock;
  3327. }
  3328. break;
  3329. default:
  3330. break;
  3331. }
  3332. mvmvif->ps_disabled = true;
  3333. ret = iwl_mvm_power_update_ps(mvm);
  3334. if (ret)
  3335. goto out_unlock;
  3336. /* we won't be on this channel any longer */
  3337. iwl_mvm_teardown_tdls_peers(mvm);
  3338. out_unlock:
  3339. mutex_unlock(&mvm->mutex);
  3340. return ret;
  3341. }
  3342. static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
  3343. struct ieee80211_vif *vif)
  3344. {
  3345. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  3346. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3347. int ret;
  3348. mutex_lock(&mvm->mutex);
  3349. if (mvmvif->csa_failed) {
  3350. mvmvif->csa_failed = false;
  3351. ret = -EIO;
  3352. goto out_unlock;
  3353. }
  3354. if (vif->type == NL80211_IFTYPE_STATION) {
  3355. struct iwl_mvm_sta *mvmsta;
  3356. mvmvif->csa_bcn_pending = false;
  3357. mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
  3358. mvmvif->ap_sta_id);
  3359. if (WARN_ON(!mvmsta)) {
  3360. ret = -EIO;
  3361. goto out_unlock;
  3362. }
  3363. iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
  3364. iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
  3365. ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
  3366. if (ret)
  3367. goto out_unlock;
  3368. iwl_mvm_stop_session_protection(mvm, vif);
  3369. }
  3370. mvmvif->ps_disabled = false;
  3371. ret = iwl_mvm_power_update_ps(mvm);
  3372. out_unlock:
  3373. mutex_unlock(&mvm->mutex);
  3374. return ret;
  3375. }
  3376. static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
  3377. {
  3378. int i;
  3379. if (!iwl_mvm_has_new_tx_api(mvm)) {
  3380. if (drop) {
  3381. mutex_lock(&mvm->mutex);
  3382. iwl_mvm_flush_tx_path(mvm,
  3383. iwl_mvm_flushable_queues(mvm) & queues, 0);
  3384. mutex_unlock(&mvm->mutex);
  3385. } else {
  3386. iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
  3387. }
  3388. return;
  3389. }
  3390. mutex_lock(&mvm->mutex);
  3391. for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
  3392. struct ieee80211_sta *sta;
  3393. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  3394. lockdep_is_held(&mvm->mutex));
  3395. if (IS_ERR_OR_NULL(sta))
  3396. continue;
  3397. if (drop)
  3398. iwl_mvm_flush_sta_tids(mvm, i, 0xFF, 0);
  3399. else
  3400. iwl_mvm_wait_sta_queues_empty(mvm,
  3401. iwl_mvm_sta_from_mac80211(sta));
  3402. }
  3403. mutex_unlock(&mvm->mutex);
  3404. }
  3405. static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
  3406. struct ieee80211_vif *vif, u32 queues, bool drop)
  3407. {
  3408. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3409. struct iwl_mvm_vif *mvmvif;
  3410. struct iwl_mvm_sta *mvmsta;
  3411. struct ieee80211_sta *sta;
  3412. int i;
  3413. u32 msk = 0;
  3414. if (!vif) {
  3415. iwl_mvm_flush_no_vif(mvm, queues, drop);
  3416. return;
  3417. }
  3418. if (vif->type != NL80211_IFTYPE_STATION)
  3419. return;
  3420. /* Make sure we're done with the deferred traffic before flushing */
  3421. flush_work(&mvm->add_stream_wk);
  3422. mutex_lock(&mvm->mutex);
  3423. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  3424. /* flush the AP-station and all TDLS peers */
  3425. for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
  3426. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  3427. lockdep_is_held(&mvm->mutex));
  3428. if (IS_ERR_OR_NULL(sta))
  3429. continue;
  3430. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  3431. if (mvmsta->vif != vif)
  3432. continue;
  3433. /* make sure only TDLS peers or the AP are flushed */
  3434. WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
  3435. if (drop) {
  3436. if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
  3437. IWL_ERR(mvm, "flush request fail\n");
  3438. } else {
  3439. msk |= mvmsta->tfd_queue_msk;
  3440. if (iwl_mvm_has_new_tx_api(mvm))
  3441. iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
  3442. }
  3443. }
  3444. mutex_unlock(&mvm->mutex);
  3445. /* this can take a while, and we may need/want other operations
  3446. * to succeed while doing this, so do it without the mutex held
  3447. */
  3448. if (!drop && !iwl_mvm_has_new_tx_api(mvm))
  3449. iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
  3450. }
  3451. static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
  3452. struct survey_info *survey)
  3453. {
  3454. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3455. int ret;
  3456. memset(survey, 0, sizeof(*survey));
  3457. /* only support global statistics right now */
  3458. if (idx != 0)
  3459. return -ENOENT;
  3460. if (!fw_has_capa(&mvm->fw->ucode_capa,
  3461. IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
  3462. return -ENOENT;
  3463. mutex_lock(&mvm->mutex);
  3464. if (iwl_mvm_firmware_running(mvm)) {
  3465. ret = iwl_mvm_request_statistics(mvm, false);
  3466. if (ret)
  3467. goto out;
  3468. }
  3469. survey->filled = SURVEY_INFO_TIME |
  3470. SURVEY_INFO_TIME_RX |
  3471. SURVEY_INFO_TIME_TX |
  3472. SURVEY_INFO_TIME_SCAN;
  3473. survey->time = mvm->accu_radio_stats.on_time_rf +
  3474. mvm->radio_stats.on_time_rf;
  3475. do_div(survey->time, USEC_PER_MSEC);
  3476. survey->time_rx = mvm->accu_radio_stats.rx_time +
  3477. mvm->radio_stats.rx_time;
  3478. do_div(survey->time_rx, USEC_PER_MSEC);
  3479. survey->time_tx = mvm->accu_radio_stats.tx_time +
  3480. mvm->radio_stats.tx_time;
  3481. do_div(survey->time_tx, USEC_PER_MSEC);
  3482. survey->time_scan = mvm->accu_radio_stats.on_time_scan +
  3483. mvm->radio_stats.on_time_scan;
  3484. do_div(survey->time_scan, USEC_PER_MSEC);
  3485. ret = 0;
  3486. out:
  3487. mutex_unlock(&mvm->mutex);
  3488. return ret;
  3489. }
  3490. static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
  3491. struct ieee80211_vif *vif,
  3492. struct ieee80211_sta *sta,
  3493. struct station_info *sinfo)
  3494. {
  3495. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3496. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  3497. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  3498. if (mvmsta->avg_energy) {
  3499. sinfo->signal_avg = mvmsta->avg_energy;
  3500. sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
  3501. }
  3502. if (!fw_has_capa(&mvm->fw->ucode_capa,
  3503. IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
  3504. return;
  3505. /* if beacon filtering isn't on mac80211 does it anyway */
  3506. if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
  3507. return;
  3508. if (!vif->bss_conf.assoc)
  3509. return;
  3510. mutex_lock(&mvm->mutex);
  3511. if (mvmvif->ap_sta_id != mvmsta->sta_id)
  3512. goto unlock;
  3513. if (iwl_mvm_request_statistics(mvm, false))
  3514. goto unlock;
  3515. sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
  3516. mvmvif->beacon_stats.accu_num_beacons;
  3517. sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX);
  3518. if (mvmvif->beacon_stats.avg_signal) {
  3519. /* firmware only reports a value after RXing a few beacons */
  3520. sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
  3521. sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
  3522. }
  3523. unlock:
  3524. mutex_unlock(&mvm->mutex);
  3525. }
  3526. static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
  3527. struct ieee80211_vif *vif,
  3528. const struct ieee80211_event *event)
  3529. {
  3530. #define CHECK_MLME_TRIGGER(_cnt, _fmt...) \
  3531. do { \
  3532. if ((trig_mlme->_cnt) && --(trig_mlme->_cnt)) \
  3533. break; \
  3534. iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt); \
  3535. } while (0)
  3536. struct iwl_fw_dbg_trigger_tlv *trig;
  3537. struct iwl_fw_dbg_trigger_mlme *trig_mlme;
  3538. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
  3539. return;
  3540. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
  3541. trig_mlme = (void *)trig->data;
  3542. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  3543. ieee80211_vif_to_wdev(vif), trig))
  3544. return;
  3545. if (event->u.mlme.data == ASSOC_EVENT) {
  3546. if (event->u.mlme.status == MLME_DENIED)
  3547. CHECK_MLME_TRIGGER(stop_assoc_denied,
  3548. "DENIED ASSOC: reason %d",
  3549. event->u.mlme.reason);
  3550. else if (event->u.mlme.status == MLME_TIMEOUT)
  3551. CHECK_MLME_TRIGGER(stop_assoc_timeout,
  3552. "ASSOC TIMEOUT");
  3553. } else if (event->u.mlme.data == AUTH_EVENT) {
  3554. if (event->u.mlme.status == MLME_DENIED)
  3555. CHECK_MLME_TRIGGER(stop_auth_denied,
  3556. "DENIED AUTH: reason %d",
  3557. event->u.mlme.reason);
  3558. else if (event->u.mlme.status == MLME_TIMEOUT)
  3559. CHECK_MLME_TRIGGER(stop_auth_timeout,
  3560. "AUTH TIMEOUT");
  3561. } else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
  3562. CHECK_MLME_TRIGGER(stop_rx_deauth,
  3563. "DEAUTH RX %d", event->u.mlme.reason);
  3564. } else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
  3565. CHECK_MLME_TRIGGER(stop_tx_deauth,
  3566. "DEAUTH TX %d", event->u.mlme.reason);
  3567. }
  3568. #undef CHECK_MLME_TRIGGER
  3569. }
  3570. static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
  3571. struct ieee80211_vif *vif,
  3572. const struct ieee80211_event *event)
  3573. {
  3574. struct iwl_fw_dbg_trigger_tlv *trig;
  3575. struct iwl_fw_dbg_trigger_ba *ba_trig;
  3576. if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
  3577. return;
  3578. trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
  3579. ba_trig = (void *)trig->data;
  3580. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  3581. ieee80211_vif_to_wdev(vif), trig))
  3582. return;
  3583. if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
  3584. return;
  3585. iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
  3586. "BAR received from %pM, tid %d, ssn %d",
  3587. event->u.ba.sta->addr, event->u.ba.tid,
  3588. event->u.ba.ssn);
  3589. }
  3590. static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
  3591. struct ieee80211_vif *vif,
  3592. const struct ieee80211_event *event)
  3593. {
  3594. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3595. switch (event->type) {
  3596. case MLME_EVENT:
  3597. iwl_mvm_event_mlme_callback(mvm, vif, event);
  3598. break;
  3599. case BAR_RX_EVENT:
  3600. iwl_mvm_event_bar_rx_callback(mvm, vif, event);
  3601. break;
  3602. case BA_FRAME_TIMEOUT:
  3603. iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
  3604. event->u.ba.tid);
  3605. break;
  3606. default:
  3607. break;
  3608. }
  3609. }
  3610. void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
  3611. struct iwl_mvm_internal_rxq_notif *notif,
  3612. u32 size)
  3613. {
  3614. u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
  3615. int ret;
  3616. lockdep_assert_held(&mvm->mutex);
  3617. if (!iwl_mvm_has_new_rx_api(mvm))
  3618. return;
  3619. notif->cookie = mvm->queue_sync_cookie;
  3620. if (notif->sync)
  3621. atomic_set(&mvm->queue_sync_counter,
  3622. mvm->trans->num_rx_queues);
  3623. /* TODO - remove this when we have RXQ config API */
  3624. if (mvm->trans->cfg->device_family == IWL_DEVICE_FAMILY_22000) {
  3625. qmask = BIT(0);
  3626. if (notif->sync)
  3627. atomic_set(&mvm->queue_sync_counter, 1);
  3628. }
  3629. ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif, size);
  3630. if (ret) {
  3631. IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
  3632. goto out;
  3633. }
  3634. if (notif->sync) {
  3635. ret = wait_event_timeout(mvm->rx_sync_waitq,
  3636. atomic_read(&mvm->queue_sync_counter) == 0 ||
  3637. iwl_mvm_is_radio_killed(mvm),
  3638. HZ);
  3639. WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
  3640. }
  3641. out:
  3642. atomic_set(&mvm->queue_sync_counter, 0);
  3643. mvm->queue_sync_cookie++;
  3644. }
  3645. static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
  3646. {
  3647. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  3648. struct iwl_mvm_internal_rxq_notif data = {
  3649. .type = IWL_MVM_RXQ_EMPTY,
  3650. .sync = 1,
  3651. };
  3652. mutex_lock(&mvm->mutex);
  3653. iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
  3654. mutex_unlock(&mvm->mutex);
  3655. }
  3656. const struct ieee80211_ops iwl_mvm_hw_ops = {
  3657. .tx = iwl_mvm_mac_tx,
  3658. .ampdu_action = iwl_mvm_mac_ampdu_action,
  3659. .start = iwl_mvm_mac_start,
  3660. .reconfig_complete = iwl_mvm_mac_reconfig_complete,
  3661. .stop = iwl_mvm_mac_stop,
  3662. .add_interface = iwl_mvm_mac_add_interface,
  3663. .remove_interface = iwl_mvm_mac_remove_interface,
  3664. .config = iwl_mvm_mac_config,
  3665. .prepare_multicast = iwl_mvm_prepare_multicast,
  3666. .configure_filter = iwl_mvm_configure_filter,
  3667. .config_iface_filter = iwl_mvm_config_iface_filter,
  3668. .bss_info_changed = iwl_mvm_bss_info_changed,
  3669. .hw_scan = iwl_mvm_mac_hw_scan,
  3670. .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
  3671. .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
  3672. .sta_state = iwl_mvm_mac_sta_state,
  3673. .sta_notify = iwl_mvm_mac_sta_notify,
  3674. .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
  3675. .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
  3676. .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
  3677. .sta_rc_update = iwl_mvm_sta_rc_update,
  3678. .conf_tx = iwl_mvm_mac_conf_tx,
  3679. .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
  3680. .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
  3681. .flush = iwl_mvm_mac_flush,
  3682. .sched_scan_start = iwl_mvm_mac_sched_scan_start,
  3683. .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
  3684. .set_key = iwl_mvm_mac_set_key,
  3685. .update_tkip_key = iwl_mvm_mac_update_tkip_key,
  3686. .remain_on_channel = iwl_mvm_roc,
  3687. .cancel_remain_on_channel = iwl_mvm_cancel_roc,
  3688. .add_chanctx = iwl_mvm_add_chanctx,
  3689. .remove_chanctx = iwl_mvm_remove_chanctx,
  3690. .change_chanctx = iwl_mvm_change_chanctx,
  3691. .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
  3692. .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
  3693. .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
  3694. .start_ap = iwl_mvm_start_ap_ibss,
  3695. .stop_ap = iwl_mvm_stop_ap_ibss,
  3696. .join_ibss = iwl_mvm_start_ap_ibss,
  3697. .leave_ibss = iwl_mvm_stop_ap_ibss,
  3698. .tx_last_beacon = iwl_mvm_tx_last_beacon,
  3699. .set_tim = iwl_mvm_set_tim,
  3700. .channel_switch = iwl_mvm_channel_switch,
  3701. .pre_channel_switch = iwl_mvm_pre_channel_switch,
  3702. .post_channel_switch = iwl_mvm_post_channel_switch,
  3703. .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
  3704. .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
  3705. .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
  3706. .event_callback = iwl_mvm_mac_event_callback,
  3707. .sync_rx_queues = iwl_mvm_sync_rx_queues,
  3708. CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
  3709. #ifdef CONFIG_PM_SLEEP
  3710. /* look at d3.c */
  3711. .suspend = iwl_mvm_suspend,
  3712. .resume = iwl_mvm_resume,
  3713. .set_wakeup = iwl_mvm_set_wakeup,
  3714. .set_rekey_data = iwl_mvm_set_rekey_data,
  3715. #if IS_ENABLED(CONFIG_IPV6)
  3716. .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
  3717. #endif
  3718. .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
  3719. #endif
  3720. .get_survey = iwl_mvm_mac_get_survey,
  3721. .sta_statistics = iwl_mvm_mac_sta_statistics,
  3722. #ifdef CONFIG_IWLWIFI_DEBUGFS
  3723. .sta_add_debugfs = iwl_mvm_sta_add_debugfs,
  3724. #endif
  3725. };