mac80211.c 112 KB

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