mac80211.c 109 KB

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