mac80211.c 84 KB

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