mac80211.c 111 KB

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