mac80211.c 107 KB

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