mac80211.c 98 KB

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