sta.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  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 - 2015 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <net/mac80211.h>
  68. #include "mvm.h"
  69. #include "sta.h"
  70. #include "rs.h"
  71. /*
  72. * New version of ADD_STA_sta command added new fields at the end of the
  73. * structure, so sending the size of the relevant API's structure is enough to
  74. * support both API versions.
  75. */
  76. static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
  77. {
  78. return iwl_mvm_has_new_rx_api(mvm) ?
  79. sizeof(struct iwl_mvm_add_sta_cmd) :
  80. sizeof(struct iwl_mvm_add_sta_cmd_v7);
  81. }
  82. static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
  83. enum nl80211_iftype iftype)
  84. {
  85. int sta_id;
  86. u32 reserved_ids = 0;
  87. BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
  88. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  89. lockdep_assert_held(&mvm->mutex);
  90. /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
  91. if (iftype != NL80211_IFTYPE_STATION)
  92. reserved_ids = BIT(0);
  93. /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
  94. for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
  95. if (BIT(sta_id) & reserved_ids)
  96. continue;
  97. if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  98. lockdep_is_held(&mvm->mutex)))
  99. return sta_id;
  100. }
  101. return IWL_MVM_STATION_COUNT;
  102. }
  103. /* send station add/update command to firmware */
  104. int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  105. bool update, unsigned int flags)
  106. {
  107. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  108. struct iwl_mvm_add_sta_cmd add_sta_cmd = {
  109. .sta_id = mvm_sta->sta_id,
  110. .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
  111. .add_modify = update ? 1 : 0,
  112. .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
  113. STA_FLG_MIMO_EN_MSK),
  114. .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
  115. };
  116. int ret;
  117. u32 status;
  118. u32 agg_size = 0, mpdu_dens = 0;
  119. if (!update || (flags & STA_MODIFY_QUEUES)) {
  120. add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  121. memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
  122. if (flags & STA_MODIFY_QUEUES)
  123. add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
  124. }
  125. switch (sta->bandwidth) {
  126. case IEEE80211_STA_RX_BW_160:
  127. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
  128. /* fall through */
  129. case IEEE80211_STA_RX_BW_80:
  130. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
  131. /* fall through */
  132. case IEEE80211_STA_RX_BW_40:
  133. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
  134. /* fall through */
  135. case IEEE80211_STA_RX_BW_20:
  136. if (sta->ht_cap.ht_supported)
  137. add_sta_cmd.station_flags |=
  138. cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
  139. break;
  140. }
  141. switch (sta->rx_nss) {
  142. case 1:
  143. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  144. break;
  145. case 2:
  146. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
  147. break;
  148. case 3 ... 8:
  149. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
  150. break;
  151. }
  152. switch (sta->smps_mode) {
  153. case IEEE80211_SMPS_AUTOMATIC:
  154. case IEEE80211_SMPS_NUM_MODES:
  155. WARN_ON(1);
  156. break;
  157. case IEEE80211_SMPS_STATIC:
  158. /* override NSS */
  159. add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
  160. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  161. break;
  162. case IEEE80211_SMPS_DYNAMIC:
  163. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
  164. break;
  165. case IEEE80211_SMPS_OFF:
  166. /* nothing */
  167. break;
  168. }
  169. if (sta->ht_cap.ht_supported) {
  170. add_sta_cmd.station_flags_msk |=
  171. cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
  172. STA_FLG_AGG_MPDU_DENS_MSK);
  173. mpdu_dens = sta->ht_cap.ampdu_density;
  174. }
  175. if (sta->vht_cap.vht_supported) {
  176. agg_size = sta->vht_cap.cap &
  177. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  178. agg_size >>=
  179. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  180. } else if (sta->ht_cap.ht_supported) {
  181. agg_size = sta->ht_cap.ampdu_factor;
  182. }
  183. add_sta_cmd.station_flags |=
  184. cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
  185. add_sta_cmd.station_flags |=
  186. cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
  187. status = ADD_STA_SUCCESS;
  188. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  189. iwl_mvm_add_sta_cmd_size(mvm),
  190. &add_sta_cmd, &status);
  191. if (ret)
  192. return ret;
  193. switch (status & IWL_ADD_STA_STATUS_MASK) {
  194. case ADD_STA_SUCCESS:
  195. IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
  196. break;
  197. default:
  198. ret = -EIO;
  199. IWL_ERR(mvm, "ADD_STA failed\n");
  200. break;
  201. }
  202. return ret;
  203. }
  204. static void iwl_mvm_rx_agg_session_expired(unsigned long data)
  205. {
  206. struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
  207. struct iwl_mvm_baid_data *ba_data;
  208. struct ieee80211_sta *sta;
  209. struct iwl_mvm_sta *mvm_sta;
  210. unsigned long timeout;
  211. rcu_read_lock();
  212. ba_data = rcu_dereference(*rcu_ptr);
  213. if (WARN_ON(!ba_data))
  214. goto unlock;
  215. if (!ba_data->timeout)
  216. goto unlock;
  217. timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
  218. if (time_is_after_jiffies(timeout)) {
  219. mod_timer(&ba_data->session_timer, timeout);
  220. goto unlock;
  221. }
  222. /* Timer expired */
  223. sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
  224. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  225. ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
  226. sta->addr, ba_data->tid);
  227. unlock:
  228. rcu_read_unlock();
  229. }
  230. static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
  231. struct ieee80211_sta *sta)
  232. {
  233. unsigned long used_hw_queues;
  234. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  235. unsigned int wdg_timeout =
  236. iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
  237. u32 ac;
  238. lockdep_assert_held(&mvm->mutex);
  239. used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
  240. /* Find available queues, and allocate them to the ACs */
  241. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  242. u8 queue = find_first_zero_bit(&used_hw_queues,
  243. mvm->first_agg_queue);
  244. if (queue >= mvm->first_agg_queue) {
  245. IWL_ERR(mvm, "Failed to allocate STA queue\n");
  246. return -EBUSY;
  247. }
  248. __set_bit(queue, &used_hw_queues);
  249. mvmsta->hw_queue[ac] = queue;
  250. }
  251. /* Found a place for all queues - enable them */
  252. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  253. iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
  254. mvmsta->hw_queue[ac],
  255. iwl_mvm_ac_to_tx_fifo[ac], 0,
  256. wdg_timeout);
  257. mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
  258. }
  259. return 0;
  260. }
  261. static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
  262. struct ieee80211_sta *sta)
  263. {
  264. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  265. unsigned long sta_msk;
  266. int i;
  267. lockdep_assert_held(&mvm->mutex);
  268. /* disable the TDLS STA-specific queues */
  269. sta_msk = mvmsta->tfd_queue_msk;
  270. for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
  271. iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
  272. }
  273. /* Disable aggregations for a bitmap of TIDs for a given station */
  274. static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
  275. unsigned long disable_agg_tids,
  276. bool remove_queue)
  277. {
  278. struct iwl_mvm_add_sta_cmd cmd = {};
  279. struct ieee80211_sta *sta;
  280. struct iwl_mvm_sta *mvmsta;
  281. u32 status;
  282. u8 sta_id;
  283. int ret;
  284. spin_lock_bh(&mvm->queue_info_lock);
  285. sta_id = mvm->queue_info[queue].ra_sta_id;
  286. spin_unlock_bh(&mvm->queue_info_lock);
  287. rcu_read_lock();
  288. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  289. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  290. rcu_read_unlock();
  291. return -EINVAL;
  292. }
  293. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  294. mvmsta->tid_disable_agg |= disable_agg_tids;
  295. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  296. cmd.sta_id = mvmsta->sta_id;
  297. cmd.add_modify = STA_MODE_MODIFY;
  298. cmd.modify_mask = STA_MODIFY_QUEUES;
  299. if (disable_agg_tids)
  300. cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
  301. if (remove_queue)
  302. cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
  303. cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
  304. cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
  305. rcu_read_unlock();
  306. /* Notify FW of queue removal from the STA queues */
  307. status = ADD_STA_SUCCESS;
  308. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  309. iwl_mvm_add_sta_cmd_size(mvm),
  310. &cmd, &status);
  311. return ret;
  312. }
  313. static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
  314. {
  315. struct ieee80211_sta *sta;
  316. struct iwl_mvm_sta *mvmsta;
  317. unsigned long tid_bitmap;
  318. unsigned long agg_tids = 0;
  319. s8 sta_id;
  320. int tid;
  321. lockdep_assert_held(&mvm->mutex);
  322. spin_lock_bh(&mvm->queue_info_lock);
  323. sta_id = mvm->queue_info[queue].ra_sta_id;
  324. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  325. spin_unlock_bh(&mvm->queue_info_lock);
  326. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  327. lockdep_is_held(&mvm->mutex));
  328. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
  329. return -EINVAL;
  330. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  331. spin_lock_bh(&mvmsta->lock);
  332. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  333. if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
  334. agg_tids |= BIT(tid);
  335. }
  336. spin_unlock_bh(&mvmsta->lock);
  337. return agg_tids;
  338. }
  339. /*
  340. * Remove a queue from a station's resources.
  341. * Note that this only marks as free. It DOESN'T delete a BA agreement, and
  342. * doesn't disable the queue
  343. */
  344. static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
  345. {
  346. struct ieee80211_sta *sta;
  347. struct iwl_mvm_sta *mvmsta;
  348. unsigned long tid_bitmap;
  349. unsigned long disable_agg_tids = 0;
  350. u8 sta_id;
  351. int tid;
  352. lockdep_assert_held(&mvm->mutex);
  353. spin_lock_bh(&mvm->queue_info_lock);
  354. sta_id = mvm->queue_info[queue].ra_sta_id;
  355. tid_bitmap = mvm->queue_info[queue].tid_bitmap;
  356. spin_unlock_bh(&mvm->queue_info_lock);
  357. rcu_read_lock();
  358. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  359. if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
  360. rcu_read_unlock();
  361. return 0;
  362. }
  363. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  364. spin_lock_bh(&mvmsta->lock);
  365. /* Unmap MAC queues and TIDs from this queue */
  366. for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
  367. if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
  368. disable_agg_tids |= BIT(tid);
  369. mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
  370. }
  371. mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
  372. spin_unlock_bh(&mvmsta->lock);
  373. rcu_read_unlock();
  374. spin_lock_bh(&mvm->queue_info_lock);
  375. /* Unmap MAC queues and TIDs from this queue */
  376. mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
  377. mvm->queue_info[queue].hw_queue_refcount = 0;
  378. mvm->queue_info[queue].tid_bitmap = 0;
  379. spin_unlock_bh(&mvm->queue_info_lock);
  380. return disable_agg_tids;
  381. }
  382. static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
  383. unsigned long tfd_queue_mask, u8 ac)
  384. {
  385. int queue = 0;
  386. u8 ac_to_queue[IEEE80211_NUM_ACS];
  387. int i;
  388. lockdep_assert_held(&mvm->queue_info_lock);
  389. memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
  390. /* See what ACs the existing queues for this STA have */
  391. for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
  392. /* Only DATA queues can be shared */
  393. if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
  394. i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
  395. continue;
  396. ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
  397. }
  398. /*
  399. * The queue to share is chosen only from DATA queues as follows (in
  400. * descending priority):
  401. * 1. An AC_BE queue
  402. * 2. Same AC queue
  403. * 3. Highest AC queue that is lower than new AC
  404. * 4. Any existing AC (there always is at least 1 DATA queue)
  405. */
  406. /* Priority 1: An AC_BE queue */
  407. if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
  408. queue = ac_to_queue[IEEE80211_AC_BE];
  409. /* Priority 2: Same AC queue */
  410. else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  411. queue = ac_to_queue[ac];
  412. /* Priority 3a: If new AC is VO and VI exists - use VI */
  413. else if (ac == IEEE80211_AC_VO &&
  414. ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
  415. queue = ac_to_queue[IEEE80211_AC_VI];
  416. /* Priority 3b: No BE so only AC less than the new one is BK */
  417. else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
  418. queue = ac_to_queue[IEEE80211_AC_BK];
  419. /* Priority 4a: No BE nor BK - use VI if exists */
  420. else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
  421. queue = ac_to_queue[IEEE80211_AC_VI];
  422. /* Priority 4b: No BE, BK nor VI - use VO if exists */
  423. else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
  424. queue = ac_to_queue[IEEE80211_AC_VO];
  425. /* Make sure queue found (or not) is legal */
  426. if (!((queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE &&
  427. queue <= IWL_MVM_DQA_MAX_MGMT_QUEUE) ||
  428. (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE &&
  429. queue <= IWL_MVM_DQA_MAX_DATA_QUEUE) ||
  430. (queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE))) {
  431. IWL_ERR(mvm, "No DATA queues available to share\n");
  432. queue = -ENOSPC;
  433. }
  434. return queue;
  435. }
  436. /*
  437. * If a given queue has a higher AC than the TID stream that is being added to
  438. * it, the queue needs to be redirected to the lower AC. This function does that
  439. * in such a case, otherwise - if no redirection required - it does nothing,
  440. * unless the %force param is true.
  441. */
  442. static int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
  443. int ac, int ssn, unsigned int wdg_timeout,
  444. bool force)
  445. {
  446. struct iwl_scd_txq_cfg_cmd cmd = {
  447. .scd_queue = queue,
  448. .enable = 0,
  449. };
  450. bool shared_queue;
  451. unsigned long mq;
  452. int ret;
  453. /*
  454. * If the AC is lower than current one - FIFO needs to be redirected to
  455. * the lowest one of the streams in the queue. Check if this is needed
  456. * here.
  457. * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
  458. * value 3 and VO with value 0, so to check if ac X is lower than ac Y
  459. * we need to check if the numerical value of X is LARGER than of Y.
  460. */
  461. spin_lock_bh(&mvm->queue_info_lock);
  462. if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
  463. spin_unlock_bh(&mvm->queue_info_lock);
  464. IWL_DEBUG_TX_QUEUES(mvm,
  465. "No redirection needed on TXQ #%d\n",
  466. queue);
  467. return 0;
  468. }
  469. cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
  470. cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
  471. mq = mvm->queue_info[queue].hw_queue_to_mac80211;
  472. shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
  473. spin_unlock_bh(&mvm->queue_info_lock);
  474. IWL_DEBUG_TX_QUEUES(mvm, "Redirecting shared TXQ #%d to FIFO #%d\n",
  475. queue, iwl_mvm_ac_to_tx_fifo[ac]);
  476. /* Stop MAC queues and wait for this queue to empty */
  477. iwl_mvm_stop_mac_queues(mvm, mq);
  478. ret = iwl_trans_wait_tx_queue_empty(mvm->trans, BIT(queue));
  479. if (ret) {
  480. IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
  481. queue);
  482. ret = -EIO;
  483. goto out;
  484. }
  485. /* Before redirecting the queue we need to de-activate it */
  486. iwl_trans_txq_disable(mvm->trans, queue, false);
  487. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
  488. if (ret)
  489. IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
  490. ret);
  491. /* Make sure the SCD wrptr is correctly set before reconfiguring */
  492. iwl_trans_txq_enable(mvm->trans, queue, iwl_mvm_ac_to_tx_fifo[ac],
  493. cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
  494. ssn, wdg_timeout);
  495. /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
  496. /* Redirect to lower AC */
  497. iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
  498. cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
  499. ssn);
  500. /* Update AC marking of the queue */
  501. spin_lock_bh(&mvm->queue_info_lock);
  502. mvm->queue_info[queue].mac80211_ac = ac;
  503. spin_unlock_bh(&mvm->queue_info_lock);
  504. /*
  505. * Mark queue as shared in transport if shared
  506. * Note this has to be done after queue enablement because enablement
  507. * can also set this value, and there is no indication there to shared
  508. * queues
  509. */
  510. if (shared_queue)
  511. iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
  512. out:
  513. /* Continue using the MAC queues */
  514. iwl_mvm_start_mac_queues(mvm, mq);
  515. return ret;
  516. }
  517. static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
  518. struct ieee80211_sta *sta, u8 ac, int tid,
  519. struct ieee80211_hdr *hdr)
  520. {
  521. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  522. struct iwl_trans_txq_scd_cfg cfg = {
  523. .fifo = iwl_mvm_ac_to_tx_fifo[ac],
  524. .sta_id = mvmsta->sta_id,
  525. .tid = tid,
  526. .frame_limit = IWL_FRAME_LIMIT,
  527. };
  528. unsigned int wdg_timeout =
  529. iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
  530. u8 mac_queue = mvmsta->vif->hw_queue[ac];
  531. int queue = -1;
  532. bool using_inactive_queue = false;
  533. unsigned long disable_agg_tids = 0;
  534. enum iwl_mvm_agg_state queue_state;
  535. bool shared_queue = false;
  536. int ssn;
  537. unsigned long tfd_queue_mask;
  538. int ret;
  539. lockdep_assert_held(&mvm->mutex);
  540. spin_lock_bh(&mvmsta->lock);
  541. tfd_queue_mask = mvmsta->tfd_queue_msk;
  542. spin_unlock_bh(&mvmsta->lock);
  543. spin_lock_bh(&mvm->queue_info_lock);
  544. /*
  545. * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
  546. * exists
  547. */
  548. if (!ieee80211_is_data_qos(hdr->frame_control) ||
  549. ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  550. queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  551. IWL_MVM_DQA_MIN_MGMT_QUEUE,
  552. IWL_MVM_DQA_MAX_MGMT_QUEUE);
  553. if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
  554. IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
  555. queue);
  556. /* If no such queue is found, we'll use a DATA queue instead */
  557. }
  558. if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
  559. (mvm->queue_info[mvmsta->reserved_queue].status ==
  560. IWL_MVM_QUEUE_RESERVED ||
  561. mvm->queue_info[mvmsta->reserved_queue].status ==
  562. IWL_MVM_QUEUE_INACTIVE)) {
  563. queue = mvmsta->reserved_queue;
  564. mvm->queue_info[queue].reserved = true;
  565. IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
  566. }
  567. if (queue < 0)
  568. queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  569. IWL_MVM_DQA_MIN_DATA_QUEUE,
  570. IWL_MVM_DQA_MAX_DATA_QUEUE);
  571. /*
  572. * Check if this queue is already allocated but inactive.
  573. * In such a case, we'll need to first free this queue before enabling
  574. * it again, so we'll mark it as reserved to make sure no new traffic
  575. * arrives on it
  576. */
  577. if (queue > 0 &&
  578. mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
  579. mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
  580. using_inactive_queue = true;
  581. IWL_DEBUG_TX_QUEUES(mvm,
  582. "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
  583. queue, mvmsta->sta_id, tid);
  584. }
  585. /* No free queue - we'll have to share */
  586. if (queue <= 0) {
  587. queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
  588. if (queue > 0) {
  589. shared_queue = true;
  590. mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
  591. }
  592. }
  593. /*
  594. * Mark TXQ as ready, even though it hasn't been fully configured yet,
  595. * to make sure no one else takes it.
  596. * This will allow avoiding re-acquiring the lock at the end of the
  597. * configuration. On error we'll mark it back as free.
  598. */
  599. if ((queue > 0) && !shared_queue)
  600. mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
  601. spin_unlock_bh(&mvm->queue_info_lock);
  602. /* This shouldn't happen - out of queues */
  603. if (WARN_ON(queue <= 0)) {
  604. IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
  605. tid, cfg.sta_id);
  606. return -ENOSPC;
  607. }
  608. /*
  609. * Actual en/disablement of aggregations is through the ADD_STA HCMD,
  610. * but for configuring the SCD to send A-MPDUs we need to mark the queue
  611. * as aggregatable.
  612. * Mark all DATA queues as allowing to be aggregated at some point
  613. */
  614. cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
  615. queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
  616. /*
  617. * If this queue was previously inactive (idle) - we need to free it
  618. * first
  619. */
  620. if (using_inactive_queue) {
  621. struct iwl_scd_txq_cfg_cmd cmd = {
  622. .scd_queue = queue,
  623. .enable = 0,
  624. };
  625. u8 ac;
  626. disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
  627. spin_lock_bh(&mvm->queue_info_lock);
  628. ac = mvm->queue_info[queue].mac80211_ac;
  629. cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
  630. cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[ac];
  631. spin_unlock_bh(&mvm->queue_info_lock);
  632. /* Disable the queue */
  633. iwl_mvm_invalidate_sta_queue(mvm, queue, disable_agg_tids,
  634. true);
  635. iwl_trans_txq_disable(mvm->trans, queue, false);
  636. ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
  637. &cmd);
  638. if (ret) {
  639. IWL_ERR(mvm,
  640. "Failed to free inactive queue %d (ret=%d)\n",
  641. queue, ret);
  642. /* Re-mark the inactive queue as inactive */
  643. spin_lock_bh(&mvm->queue_info_lock);
  644. mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
  645. spin_unlock_bh(&mvm->queue_info_lock);
  646. return ret;
  647. }
  648. }
  649. IWL_DEBUG_TX_QUEUES(mvm,
  650. "Allocating %squeue #%d to sta %d on tid %d\n",
  651. shared_queue ? "shared " : "", queue,
  652. mvmsta->sta_id, tid);
  653. if (shared_queue) {
  654. /* Disable any open aggs on this queue */
  655. disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
  656. if (disable_agg_tids) {
  657. IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
  658. queue);
  659. iwl_mvm_invalidate_sta_queue(mvm, queue,
  660. disable_agg_tids, false);
  661. }
  662. }
  663. ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  664. iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
  665. wdg_timeout);
  666. /*
  667. * Mark queue as shared in transport if shared
  668. * Note this has to be done after queue enablement because enablement
  669. * can also set this value, and there is no indication there to shared
  670. * queues
  671. */
  672. if (shared_queue)
  673. iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
  674. spin_lock_bh(&mvmsta->lock);
  675. mvmsta->tid_data[tid].txq_id = queue;
  676. mvmsta->tid_data[tid].is_tid_active = true;
  677. mvmsta->tfd_queue_msk |= BIT(queue);
  678. queue_state = mvmsta->tid_data[tid].state;
  679. if (mvmsta->reserved_queue == queue)
  680. mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
  681. spin_unlock_bh(&mvmsta->lock);
  682. if (!shared_queue) {
  683. ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
  684. if (ret)
  685. goto out_err;
  686. /* If we need to re-enable aggregations... */
  687. if (queue_state == IWL_AGG_ON) {
  688. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  689. if (ret)
  690. goto out_err;
  691. }
  692. } else {
  693. /* Redirect queue, if needed */
  694. ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
  695. wdg_timeout, false);
  696. if (ret)
  697. goto out_err;
  698. }
  699. return 0;
  700. out_err:
  701. iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
  702. return ret;
  703. }
  704. static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
  705. {
  706. if (tid == IWL_MAX_TID_COUNT)
  707. return IEEE80211_AC_VO; /* MGMT */
  708. return tid_to_mac80211_ac[tid];
  709. }
  710. static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
  711. struct ieee80211_sta *sta, int tid)
  712. {
  713. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  714. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  715. struct sk_buff *skb;
  716. struct ieee80211_hdr *hdr;
  717. struct sk_buff_head deferred_tx;
  718. u8 mac_queue;
  719. bool no_queue = false; /* Marks if there is a problem with the queue */
  720. u8 ac;
  721. lockdep_assert_held(&mvm->mutex);
  722. skb = skb_peek(&tid_data->deferred_tx_frames);
  723. if (!skb)
  724. return;
  725. hdr = (void *)skb->data;
  726. ac = iwl_mvm_tid_to_ac_queue(tid);
  727. mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
  728. if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
  729. iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
  730. IWL_ERR(mvm,
  731. "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
  732. mvmsta->sta_id, tid);
  733. /*
  734. * Mark queue as problematic so later the deferred traffic is
  735. * freed, as we can do nothing with it
  736. */
  737. no_queue = true;
  738. }
  739. __skb_queue_head_init(&deferred_tx);
  740. /* Disable bottom-halves when entering TX path */
  741. local_bh_disable();
  742. spin_lock(&mvmsta->lock);
  743. skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
  744. spin_unlock(&mvmsta->lock);
  745. while ((skb = __skb_dequeue(&deferred_tx)))
  746. if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
  747. ieee80211_free_txskb(mvm->hw, skb);
  748. local_bh_enable();
  749. /* Wake queue */
  750. iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
  751. }
  752. void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
  753. {
  754. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
  755. add_stream_wk);
  756. struct ieee80211_sta *sta;
  757. struct iwl_mvm_sta *mvmsta;
  758. unsigned long deferred_tid_traffic;
  759. int sta_id, tid;
  760. /* Check inactivity of queues */
  761. iwl_mvm_inactivity_check(mvm);
  762. mutex_lock(&mvm->mutex);
  763. /* Go over all stations with deferred traffic */
  764. for_each_set_bit(sta_id, mvm->sta_deferred_frames,
  765. IWL_MVM_STATION_COUNT) {
  766. clear_bit(sta_id, mvm->sta_deferred_frames);
  767. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  768. lockdep_is_held(&mvm->mutex));
  769. if (IS_ERR_OR_NULL(sta))
  770. continue;
  771. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  772. deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
  773. for_each_set_bit(tid, &deferred_tid_traffic,
  774. IWL_MAX_TID_COUNT + 1)
  775. iwl_mvm_tx_deferred_stream(mvm, sta, tid);
  776. }
  777. mutex_unlock(&mvm->mutex);
  778. }
  779. static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
  780. struct ieee80211_sta *sta,
  781. enum nl80211_iftype vif_type)
  782. {
  783. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  784. int queue;
  785. /*
  786. * Check for inactive queues, so we don't reach a situation where we
  787. * can't add a STA due to a shortage in queues that doesn't really exist
  788. */
  789. iwl_mvm_inactivity_check(mvm);
  790. spin_lock_bh(&mvm->queue_info_lock);
  791. /* Make sure we have free resources for this STA */
  792. if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
  793. !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
  794. (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
  795. IWL_MVM_QUEUE_FREE))
  796. queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
  797. else
  798. queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  799. IWL_MVM_DQA_MIN_DATA_QUEUE,
  800. IWL_MVM_DQA_MAX_DATA_QUEUE);
  801. if (queue < 0) {
  802. spin_unlock_bh(&mvm->queue_info_lock);
  803. IWL_ERR(mvm, "No available queues for new station\n");
  804. return -ENOSPC;
  805. }
  806. mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
  807. spin_unlock_bh(&mvm->queue_info_lock);
  808. mvmsta->reserved_queue = queue;
  809. IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
  810. queue, mvmsta->sta_id);
  811. return 0;
  812. }
  813. int iwl_mvm_add_sta(struct iwl_mvm *mvm,
  814. struct ieee80211_vif *vif,
  815. struct ieee80211_sta *sta)
  816. {
  817. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  818. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  819. struct iwl_mvm_rxq_dup_data *dup_data;
  820. int i, ret, sta_id;
  821. lockdep_assert_held(&mvm->mutex);
  822. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  823. sta_id = iwl_mvm_find_free_sta_id(mvm,
  824. ieee80211_vif_type_p2p(vif));
  825. else
  826. sta_id = mvm_sta->sta_id;
  827. if (sta_id == IWL_MVM_STATION_COUNT)
  828. return -ENOSPC;
  829. spin_lock_init(&mvm_sta->lock);
  830. mvm_sta->sta_id = sta_id;
  831. mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
  832. mvmvif->color);
  833. mvm_sta->vif = vif;
  834. mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  835. mvm_sta->tx_protection = 0;
  836. mvm_sta->tt_tx_protection = false;
  837. /* HW restart, don't assume the memory has been zeroed */
  838. atomic_set(&mvm->pending_frames[sta_id], 0);
  839. mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
  840. mvm_sta->tfd_queue_msk = 0;
  841. /*
  842. * Allocate new queues for a TDLS station, unless we're in DQA mode,
  843. * and then they'll be allocated dynamically
  844. */
  845. if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
  846. ret = iwl_mvm_tdls_sta_init(mvm, sta);
  847. if (ret)
  848. return ret;
  849. } else if (!iwl_mvm_is_dqa_supported(mvm)) {
  850. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  851. if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
  852. mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
  853. }
  854. /* for HW restart - reset everything but the sequence number */
  855. for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
  856. u16 seq = mvm_sta->tid_data[i].seq_number;
  857. memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
  858. mvm_sta->tid_data[i].seq_number = seq;
  859. if (!iwl_mvm_is_dqa_supported(mvm))
  860. continue;
  861. /*
  862. * Mark all queues for this STA as unallocated and defer TX
  863. * frames until the queue is allocated
  864. */
  865. mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
  866. skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
  867. }
  868. mvm_sta->deferred_traffic_tid_map = 0;
  869. mvm_sta->agg_tids = 0;
  870. if (iwl_mvm_has_new_rx_api(mvm) &&
  871. !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  872. dup_data = kcalloc(mvm->trans->num_rx_queues,
  873. sizeof(*dup_data),
  874. GFP_KERNEL);
  875. if (!dup_data)
  876. return -ENOMEM;
  877. mvm_sta->dup_data = dup_data;
  878. }
  879. if (iwl_mvm_is_dqa_supported(mvm)) {
  880. ret = iwl_mvm_reserve_sta_stream(mvm, sta,
  881. ieee80211_vif_type_p2p(vif));
  882. if (ret)
  883. goto err;
  884. }
  885. ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
  886. if (ret)
  887. goto err;
  888. if (vif->type == NL80211_IFTYPE_STATION) {
  889. if (!sta->tdls) {
  890. WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
  891. mvmvif->ap_sta_id = sta_id;
  892. } else {
  893. WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
  894. }
  895. }
  896. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  897. return 0;
  898. err:
  899. if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
  900. iwl_mvm_tdls_sta_deinit(mvm, sta);
  901. return ret;
  902. }
  903. int iwl_mvm_update_sta(struct iwl_mvm *mvm,
  904. struct ieee80211_vif *vif,
  905. struct ieee80211_sta *sta)
  906. {
  907. return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
  908. }
  909. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  910. bool drain)
  911. {
  912. struct iwl_mvm_add_sta_cmd cmd = {};
  913. int ret;
  914. u32 status;
  915. lockdep_assert_held(&mvm->mutex);
  916. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  917. cmd.sta_id = mvmsta->sta_id;
  918. cmd.add_modify = STA_MODE_MODIFY;
  919. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  920. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  921. status = ADD_STA_SUCCESS;
  922. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  923. iwl_mvm_add_sta_cmd_size(mvm),
  924. &cmd, &status);
  925. if (ret)
  926. return ret;
  927. switch (status & IWL_ADD_STA_STATUS_MASK) {
  928. case ADD_STA_SUCCESS:
  929. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  930. mvmsta->sta_id);
  931. break;
  932. default:
  933. ret = -EIO;
  934. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  935. mvmsta->sta_id);
  936. break;
  937. }
  938. return ret;
  939. }
  940. /*
  941. * Remove a station from the FW table. Before sending the command to remove
  942. * the station validate that the station is indeed known to the driver (sanity
  943. * only).
  944. */
  945. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  946. {
  947. struct ieee80211_sta *sta;
  948. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  949. .sta_id = sta_id,
  950. };
  951. int ret;
  952. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  953. lockdep_is_held(&mvm->mutex));
  954. /* Note: internal stations are marked as error values */
  955. if (!sta) {
  956. IWL_ERR(mvm, "Invalid station id\n");
  957. return -EINVAL;
  958. }
  959. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
  960. sizeof(rm_sta_cmd), &rm_sta_cmd);
  961. if (ret) {
  962. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  963. return ret;
  964. }
  965. return 0;
  966. }
  967. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  968. {
  969. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  970. u8 sta_id;
  971. /*
  972. * The mutex is needed because of the SYNC cmd, but not only: if the
  973. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  974. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  975. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  976. * that later.
  977. */
  978. mutex_lock(&mvm->mutex);
  979. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  980. int ret;
  981. struct ieee80211_sta *sta =
  982. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  983. lockdep_is_held(&mvm->mutex));
  984. /*
  985. * This station is in use or RCU-removed; the latter happens in
  986. * managed mode, where mac80211 removes the station before we
  987. * can remove it from firmware (we can only do that after the
  988. * MAC is marked unassociated), and possibly while the deauth
  989. * frame to disconnect from the AP is still queued. Then, the
  990. * station pointer is -ENOENT when the last skb is reclaimed.
  991. */
  992. if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
  993. continue;
  994. if (PTR_ERR(sta) == -EINVAL) {
  995. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  996. sta_id);
  997. continue;
  998. }
  999. if (!sta) {
  1000. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  1001. sta_id);
  1002. continue;
  1003. }
  1004. WARN_ON(PTR_ERR(sta) != -EBUSY);
  1005. /* This station was removed and we waited until it got drained,
  1006. * we can now proceed and remove it.
  1007. */
  1008. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  1009. if (ret) {
  1010. IWL_ERR(mvm,
  1011. "Couldn't remove sta %d after it was drained\n",
  1012. sta_id);
  1013. continue;
  1014. }
  1015. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  1016. clear_bit(sta_id, mvm->sta_drained);
  1017. if (mvm->tfd_drained[sta_id]) {
  1018. unsigned long i, msk = mvm->tfd_drained[sta_id];
  1019. for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
  1020. iwl_mvm_disable_txq(mvm, i, i,
  1021. IWL_MAX_TID_COUNT, 0);
  1022. mvm->tfd_drained[sta_id] = 0;
  1023. IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
  1024. sta_id, msk);
  1025. }
  1026. }
  1027. mutex_unlock(&mvm->mutex);
  1028. }
  1029. static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
  1030. struct ieee80211_vif *vif,
  1031. struct iwl_mvm_sta *mvm_sta)
  1032. {
  1033. int ac;
  1034. int i;
  1035. lockdep_assert_held(&mvm->mutex);
  1036. for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
  1037. if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
  1038. continue;
  1039. ac = iwl_mvm_tid_to_ac_queue(i);
  1040. iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
  1041. vif->hw_queue[ac], i, 0);
  1042. mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
  1043. }
  1044. }
  1045. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  1046. struct ieee80211_vif *vif,
  1047. struct ieee80211_sta *sta)
  1048. {
  1049. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1050. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1051. int ret;
  1052. lockdep_assert_held(&mvm->mutex);
  1053. if (iwl_mvm_has_new_rx_api(mvm))
  1054. kfree(mvm_sta->dup_data);
  1055. if ((vif->type == NL80211_IFTYPE_STATION &&
  1056. mvmvif->ap_sta_id == mvm_sta->sta_id) ||
  1057. iwl_mvm_is_dqa_supported(mvm)){
  1058. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  1059. if (ret)
  1060. return ret;
  1061. /* flush its queues here since we are freeing mvm_sta */
  1062. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
  1063. if (ret)
  1064. return ret;
  1065. ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
  1066. mvm_sta->tfd_queue_msk);
  1067. if (ret)
  1068. return ret;
  1069. ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
  1070. /* If DQA is supported - the queues can be disabled now */
  1071. if (iwl_mvm_is_dqa_supported(mvm))
  1072. iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
  1073. if (vif->type == NL80211_IFTYPE_STATION &&
  1074. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  1075. /* if associated - we can't remove the AP STA now */
  1076. if (vif->bss_conf.assoc)
  1077. return ret;
  1078. /* unassoc - go ahead - remove the AP STA now */
  1079. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  1080. /* clear d0i3_ap_sta_id if no longer relevant */
  1081. if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
  1082. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  1083. }
  1084. }
  1085. /*
  1086. * This shouldn't happen - the TDLS channel switch should be canceled
  1087. * before the STA is removed.
  1088. */
  1089. if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
  1090. mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
  1091. cancel_delayed_work(&mvm->tdls_cs.dwork);
  1092. }
  1093. /*
  1094. * Make sure that the tx response code sees the station as -EBUSY and
  1095. * calls the drain worker.
  1096. */
  1097. spin_lock_bh(&mvm_sta->lock);
  1098. /*
  1099. * There are frames pending on the AC queues for this station.
  1100. * We need to wait until all the frames are drained...
  1101. */
  1102. if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
  1103. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  1104. ERR_PTR(-EBUSY));
  1105. spin_unlock_bh(&mvm_sta->lock);
  1106. /* disable TDLS sta queues on drain complete */
  1107. if (sta->tdls) {
  1108. mvm->tfd_drained[mvm_sta->sta_id] =
  1109. mvm_sta->tfd_queue_msk;
  1110. IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
  1111. mvm_sta->sta_id);
  1112. }
  1113. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  1114. } else {
  1115. spin_unlock_bh(&mvm_sta->lock);
  1116. if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
  1117. iwl_mvm_tdls_sta_deinit(mvm, sta);
  1118. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  1119. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  1120. }
  1121. return ret;
  1122. }
  1123. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  1124. struct ieee80211_vif *vif,
  1125. u8 sta_id)
  1126. {
  1127. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  1128. lockdep_assert_held(&mvm->mutex);
  1129. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  1130. return ret;
  1131. }
  1132. int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
  1133. struct iwl_mvm_int_sta *sta,
  1134. u32 qmask, enum nl80211_iftype iftype)
  1135. {
  1136. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  1137. sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
  1138. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  1139. return -ENOSPC;
  1140. }
  1141. sta->tfd_queue_msk = qmask;
  1142. /* put a non-NULL value so iterating over the stations won't stop */
  1143. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  1144. return 0;
  1145. }
  1146. static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
  1147. struct iwl_mvm_int_sta *sta)
  1148. {
  1149. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  1150. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  1151. sta->sta_id = IWL_MVM_STATION_COUNT;
  1152. }
  1153. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  1154. struct iwl_mvm_int_sta *sta,
  1155. const u8 *addr,
  1156. u16 mac_id, u16 color)
  1157. {
  1158. struct iwl_mvm_add_sta_cmd cmd;
  1159. int ret;
  1160. u32 status;
  1161. lockdep_assert_held(&mvm->mutex);
  1162. memset(&cmd, 0, sizeof(cmd));
  1163. cmd.sta_id = sta->sta_id;
  1164. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  1165. color));
  1166. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  1167. cmd.tid_disable_tx = cpu_to_le16(0xffff);
  1168. if (addr)
  1169. memcpy(cmd.addr, addr, ETH_ALEN);
  1170. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  1171. iwl_mvm_add_sta_cmd_size(mvm),
  1172. &cmd, &status);
  1173. if (ret)
  1174. return ret;
  1175. switch (status & IWL_ADD_STA_STATUS_MASK) {
  1176. case ADD_STA_SUCCESS:
  1177. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  1178. return 0;
  1179. default:
  1180. ret = -EIO;
  1181. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  1182. status);
  1183. break;
  1184. }
  1185. return ret;
  1186. }
  1187. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  1188. {
  1189. unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
  1190. mvm->cfg->base_params->wd_timeout :
  1191. IWL_WATCHDOG_DISABLED;
  1192. int ret;
  1193. lockdep_assert_held(&mvm->mutex);
  1194. /* Map Aux queue to fifo - needs to happen before adding Aux station */
  1195. if (!iwl_mvm_is_dqa_supported(mvm))
  1196. iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
  1197. IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
  1198. /* Allocate aux station and assign to it the aux queue */
  1199. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
  1200. NL80211_IFTYPE_UNSPECIFIED);
  1201. if (ret)
  1202. return ret;
  1203. if (iwl_mvm_is_dqa_supported(mvm)) {
  1204. struct iwl_trans_txq_scd_cfg cfg = {
  1205. .fifo = IWL_MVM_TX_FIFO_MCAST,
  1206. .sta_id = mvm->aux_sta.sta_id,
  1207. .tid = IWL_MAX_TID_COUNT,
  1208. .aggregate = false,
  1209. .frame_limit = IWL_FRAME_LIMIT,
  1210. };
  1211. iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
  1212. wdg_timeout);
  1213. }
  1214. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  1215. MAC_INDEX_AUX, 0);
  1216. if (ret)
  1217. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  1218. return ret;
  1219. }
  1220. int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1221. {
  1222. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1223. lockdep_assert_held(&mvm->mutex);
  1224. return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
  1225. mvmvif->id, 0);
  1226. }
  1227. int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1228. {
  1229. int ret;
  1230. lockdep_assert_held(&mvm->mutex);
  1231. ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
  1232. if (ret)
  1233. IWL_WARN(mvm, "Failed sending remove station\n");
  1234. return ret;
  1235. }
  1236. void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
  1237. {
  1238. iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
  1239. }
  1240. void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
  1241. {
  1242. lockdep_assert_held(&mvm->mutex);
  1243. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  1244. }
  1245. /*
  1246. * Send the add station command for the vif's broadcast station.
  1247. * Assumes that the station was already allocated.
  1248. *
  1249. * @mvm: the mvm component
  1250. * @vif: the interface to which the broadcast station is added
  1251. * @bsta: the broadcast station to add.
  1252. */
  1253. int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1254. {
  1255. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1256. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  1257. static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  1258. const u8 *baddr = _baddr;
  1259. lockdep_assert_held(&mvm->mutex);
  1260. if (iwl_mvm_is_dqa_supported(mvm)) {
  1261. struct iwl_trans_txq_scd_cfg cfg = {
  1262. .fifo = IWL_MVM_TX_FIFO_VO,
  1263. .sta_id = mvmvif->bcast_sta.sta_id,
  1264. .tid = IWL_MAX_TID_COUNT,
  1265. .aggregate = false,
  1266. .frame_limit = IWL_FRAME_LIMIT,
  1267. };
  1268. unsigned int wdg_timeout =
  1269. iwl_mvm_get_wd_timeout(mvm, vif, false, false);
  1270. int queue;
  1271. if ((vif->type == NL80211_IFTYPE_AP) &&
  1272. (mvmvif->bcast_sta.tfd_queue_msk &
  1273. BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
  1274. queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
  1275. else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
  1276. (mvmvif->bcast_sta.tfd_queue_msk &
  1277. BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
  1278. queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
  1279. else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
  1280. return -EINVAL;
  1281. iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
  1282. wdg_timeout);
  1283. }
  1284. if (vif->type == NL80211_IFTYPE_ADHOC)
  1285. baddr = vif->bss_conf.bssid;
  1286. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  1287. return -ENOSPC;
  1288. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  1289. mvmvif->id, mvmvif->color);
  1290. }
  1291. /* Send the FW a request to remove the station from it's internal data
  1292. * structures, but DO NOT remove the entry from the local data structures. */
  1293. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1294. {
  1295. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1296. int ret;
  1297. lockdep_assert_held(&mvm->mutex);
  1298. ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
  1299. if (ret)
  1300. IWL_WARN(mvm, "Failed sending remove station\n");
  1301. return ret;
  1302. }
  1303. int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1304. {
  1305. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1306. u32 qmask = 0;
  1307. lockdep_assert_held(&mvm->mutex);
  1308. if (!iwl_mvm_is_dqa_supported(mvm))
  1309. qmask = iwl_mvm_mac_get_queues_mask(vif);
  1310. if (vif->type == NL80211_IFTYPE_AP) {
  1311. /*
  1312. * The firmware defines the TFD queue mask to only be relevant
  1313. * for *unicast* queues, so the multicast (CAB) queue shouldn't
  1314. * be included.
  1315. */
  1316. qmask &= ~BIT(vif->cab_queue);
  1317. if (iwl_mvm_is_dqa_supported(mvm))
  1318. qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
  1319. } else if (iwl_mvm_is_dqa_supported(mvm) &&
  1320. vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  1321. qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
  1322. }
  1323. return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
  1324. ieee80211_vif_type_p2p(vif));
  1325. }
  1326. /* Allocate a new station entry for the broadcast station to the given vif,
  1327. * and send it to the FW.
  1328. * Note that each P2P mac should have its own broadcast station.
  1329. *
  1330. * @mvm: the mvm component
  1331. * @vif: the interface to which the broadcast station is added
  1332. * @bsta: the broadcast station to add. */
  1333. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1334. {
  1335. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1336. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  1337. int ret;
  1338. lockdep_assert_held(&mvm->mutex);
  1339. ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
  1340. if (ret)
  1341. return ret;
  1342. ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
  1343. if (ret)
  1344. iwl_mvm_dealloc_int_sta(mvm, bsta);
  1345. return ret;
  1346. }
  1347. void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1348. {
  1349. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1350. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  1351. }
  1352. /*
  1353. * Send the FW a request to remove the station from it's internal data
  1354. * structures, and in addition remove it from the local data structure.
  1355. */
  1356. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1357. {
  1358. int ret;
  1359. lockdep_assert_held(&mvm->mutex);
  1360. ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
  1361. iwl_mvm_dealloc_bcast_sta(mvm, vif);
  1362. return ret;
  1363. }
  1364. #define IWL_MAX_RX_BA_SESSIONS 16
  1365. static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
  1366. {
  1367. struct iwl_mvm_delba_notif notif = {
  1368. .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
  1369. .metadata.sync = 1,
  1370. .delba.baid = baid,
  1371. };
  1372. iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
  1373. };
  1374. static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
  1375. struct iwl_mvm_baid_data *data)
  1376. {
  1377. int i;
  1378. iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
  1379. for (i = 0; i < mvm->trans->num_rx_queues; i++) {
  1380. int j;
  1381. struct iwl_mvm_reorder_buffer *reorder_buf =
  1382. &data->reorder_buf[i];
  1383. spin_lock_bh(&reorder_buf->lock);
  1384. if (likely(!reorder_buf->num_stored)) {
  1385. spin_unlock_bh(&reorder_buf->lock);
  1386. continue;
  1387. }
  1388. /*
  1389. * This shouldn't happen in regular DELBA since the internal
  1390. * delBA notification should trigger a release of all frames in
  1391. * the reorder buffer.
  1392. */
  1393. WARN_ON(1);
  1394. for (j = 0; j < reorder_buf->buf_size; j++)
  1395. __skb_queue_purge(&reorder_buf->entries[j]);
  1396. /*
  1397. * Prevent timer re-arm. This prevents a very far fetched case
  1398. * where we timed out on the notification. There may be prior
  1399. * RX frames pending in the RX queue before the notification
  1400. * that might get processed between now and the actual deletion
  1401. * and we would re-arm the timer although we are deleting the
  1402. * reorder buffer.
  1403. */
  1404. reorder_buf->removed = true;
  1405. spin_unlock_bh(&reorder_buf->lock);
  1406. del_timer_sync(&reorder_buf->reorder_timer);
  1407. }
  1408. }
  1409. static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
  1410. u32 sta_id,
  1411. struct iwl_mvm_baid_data *data,
  1412. u16 ssn, u8 buf_size)
  1413. {
  1414. int i;
  1415. for (i = 0; i < mvm->trans->num_rx_queues; i++) {
  1416. struct iwl_mvm_reorder_buffer *reorder_buf =
  1417. &data->reorder_buf[i];
  1418. int j;
  1419. reorder_buf->num_stored = 0;
  1420. reorder_buf->head_sn = ssn;
  1421. reorder_buf->buf_size = buf_size;
  1422. /* rx reorder timer */
  1423. reorder_buf->reorder_timer.function =
  1424. iwl_mvm_reorder_timer_expired;
  1425. reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
  1426. init_timer(&reorder_buf->reorder_timer);
  1427. spin_lock_init(&reorder_buf->lock);
  1428. reorder_buf->mvm = mvm;
  1429. reorder_buf->queue = i;
  1430. reorder_buf->sta_id = sta_id;
  1431. for (j = 0; j < reorder_buf->buf_size; j++)
  1432. __skb_queue_head_init(&reorder_buf->entries[j]);
  1433. }
  1434. }
  1435. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1436. int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
  1437. {
  1438. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1439. struct iwl_mvm_add_sta_cmd cmd = {};
  1440. struct iwl_mvm_baid_data *baid_data = NULL;
  1441. int ret;
  1442. u32 status;
  1443. lockdep_assert_held(&mvm->mutex);
  1444. if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
  1445. IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
  1446. return -ENOSPC;
  1447. }
  1448. if (iwl_mvm_has_new_rx_api(mvm) && start) {
  1449. /*
  1450. * Allocate here so if allocation fails we can bail out early
  1451. * before starting the BA session in the firmware
  1452. */
  1453. baid_data = kzalloc(sizeof(*baid_data) +
  1454. mvm->trans->num_rx_queues *
  1455. sizeof(baid_data->reorder_buf[0]),
  1456. GFP_KERNEL);
  1457. if (!baid_data)
  1458. return -ENOMEM;
  1459. }
  1460. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  1461. cmd.sta_id = mvm_sta->sta_id;
  1462. cmd.add_modify = STA_MODE_MODIFY;
  1463. if (start) {
  1464. cmd.add_immediate_ba_tid = (u8) tid;
  1465. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1466. cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
  1467. } else {
  1468. cmd.remove_immediate_ba_tid = (u8) tid;
  1469. }
  1470. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  1471. STA_MODIFY_REMOVE_BA_TID;
  1472. status = ADD_STA_SUCCESS;
  1473. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  1474. iwl_mvm_add_sta_cmd_size(mvm),
  1475. &cmd, &status);
  1476. if (ret)
  1477. goto out_free;
  1478. switch (status & IWL_ADD_STA_STATUS_MASK) {
  1479. case ADD_STA_SUCCESS:
  1480. IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
  1481. start ? "start" : "stopp");
  1482. break;
  1483. case ADD_STA_IMMEDIATE_BA_FAILURE:
  1484. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  1485. ret = -ENOSPC;
  1486. break;
  1487. default:
  1488. ret = -EIO;
  1489. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  1490. start ? "start" : "stopp", status);
  1491. break;
  1492. }
  1493. if (ret)
  1494. goto out_free;
  1495. if (start) {
  1496. u8 baid;
  1497. mvm->rx_ba_sessions++;
  1498. if (!iwl_mvm_has_new_rx_api(mvm))
  1499. return 0;
  1500. if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
  1501. ret = -EINVAL;
  1502. goto out_free;
  1503. }
  1504. baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
  1505. IWL_ADD_STA_BAID_SHIFT);
  1506. baid_data->baid = baid;
  1507. baid_data->timeout = timeout;
  1508. baid_data->last_rx = jiffies;
  1509. init_timer(&baid_data->session_timer);
  1510. baid_data->session_timer.function =
  1511. iwl_mvm_rx_agg_session_expired;
  1512. baid_data->session_timer.data =
  1513. (unsigned long)&mvm->baid_map[baid];
  1514. baid_data->mvm = mvm;
  1515. baid_data->tid = tid;
  1516. baid_data->sta_id = mvm_sta->sta_id;
  1517. mvm_sta->tid_to_baid[tid] = baid;
  1518. if (timeout)
  1519. mod_timer(&baid_data->session_timer,
  1520. TU_TO_EXP_TIME(timeout * 2));
  1521. iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
  1522. baid_data, ssn, buf_size);
  1523. /*
  1524. * protect the BA data with RCU to cover a case where our
  1525. * internal RX sync mechanism will timeout (not that it's
  1526. * supposed to happen) and we will free the session data while
  1527. * RX is being processed in parallel
  1528. */
  1529. IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
  1530. mvm_sta->sta_id, tid, baid);
  1531. WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
  1532. rcu_assign_pointer(mvm->baid_map[baid], baid_data);
  1533. } else {
  1534. u8 baid = mvm_sta->tid_to_baid[tid];
  1535. if (mvm->rx_ba_sessions > 0)
  1536. /* check that restart flow didn't zero the counter */
  1537. mvm->rx_ba_sessions--;
  1538. if (!iwl_mvm_has_new_rx_api(mvm))
  1539. return 0;
  1540. if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
  1541. return -EINVAL;
  1542. baid_data = rcu_access_pointer(mvm->baid_map[baid]);
  1543. if (WARN_ON(!baid_data))
  1544. return -EINVAL;
  1545. /* synchronize all rx queues so we can safely delete */
  1546. iwl_mvm_free_reorder(mvm, baid_data);
  1547. del_timer_sync(&baid_data->session_timer);
  1548. RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
  1549. kfree_rcu(baid_data, rcu_head);
  1550. IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
  1551. }
  1552. return 0;
  1553. out_free:
  1554. kfree(baid_data);
  1555. return ret;
  1556. }
  1557. int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1558. int tid, u8 queue, bool start)
  1559. {
  1560. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1561. struct iwl_mvm_add_sta_cmd cmd = {};
  1562. int ret;
  1563. u32 status;
  1564. lockdep_assert_held(&mvm->mutex);
  1565. if (start) {
  1566. mvm_sta->tfd_queue_msk |= BIT(queue);
  1567. mvm_sta->tid_disable_agg &= ~BIT(tid);
  1568. } else {
  1569. /* In DQA-mode the queue isn't removed on agg termination */
  1570. if (!iwl_mvm_is_dqa_supported(mvm))
  1571. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  1572. mvm_sta->tid_disable_agg |= BIT(tid);
  1573. }
  1574. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  1575. cmd.sta_id = mvm_sta->sta_id;
  1576. cmd.add_modify = STA_MODE_MODIFY;
  1577. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  1578. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  1579. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  1580. status = ADD_STA_SUCCESS;
  1581. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
  1582. iwl_mvm_add_sta_cmd_size(mvm),
  1583. &cmd, &status);
  1584. if (ret)
  1585. return ret;
  1586. switch (status & IWL_ADD_STA_STATUS_MASK) {
  1587. case ADD_STA_SUCCESS:
  1588. break;
  1589. default:
  1590. ret = -EIO;
  1591. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  1592. start ? "start" : "stopp", status);
  1593. break;
  1594. }
  1595. return ret;
  1596. }
  1597. const u8 tid_to_mac80211_ac[] = {
  1598. IEEE80211_AC_BE,
  1599. IEEE80211_AC_BK,
  1600. IEEE80211_AC_BK,
  1601. IEEE80211_AC_BE,
  1602. IEEE80211_AC_VI,
  1603. IEEE80211_AC_VI,
  1604. IEEE80211_AC_VO,
  1605. IEEE80211_AC_VO,
  1606. IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
  1607. };
  1608. static const u8 tid_to_ucode_ac[] = {
  1609. AC_BE,
  1610. AC_BK,
  1611. AC_BK,
  1612. AC_BE,
  1613. AC_VI,
  1614. AC_VI,
  1615. AC_VO,
  1616. AC_VO,
  1617. };
  1618. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1619. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  1620. {
  1621. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1622. struct iwl_mvm_tid_data *tid_data;
  1623. int txq_id;
  1624. int ret;
  1625. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  1626. return -EINVAL;
  1627. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  1628. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  1629. mvmsta->tid_data[tid].state);
  1630. return -ENXIO;
  1631. }
  1632. lockdep_assert_held(&mvm->mutex);
  1633. spin_lock_bh(&mvmsta->lock);
  1634. /* possible race condition - we entered D0i3 while starting agg */
  1635. if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
  1636. spin_unlock_bh(&mvmsta->lock);
  1637. IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
  1638. return -EIO;
  1639. }
  1640. spin_lock_bh(&mvm->queue_info_lock);
  1641. /*
  1642. * Note the possible cases:
  1643. * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
  1644. * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
  1645. * one and mark it as reserved
  1646. * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
  1647. * non-DQA mode, since the TXQ hasn't yet been allocated
  1648. */
  1649. txq_id = mvmsta->tid_data[tid].txq_id;
  1650. if (!iwl_mvm_is_dqa_supported(mvm) ||
  1651. mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
  1652. txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
  1653. mvm->first_agg_queue,
  1654. mvm->last_agg_queue);
  1655. if (txq_id < 0) {
  1656. ret = txq_id;
  1657. spin_unlock_bh(&mvm->queue_info_lock);
  1658. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  1659. goto release_locks;
  1660. }
  1661. /* TXQ hasn't yet been enabled, so mark it only as reserved */
  1662. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
  1663. }
  1664. spin_unlock_bh(&mvm->queue_info_lock);
  1665. IWL_DEBUG_TX_QUEUES(mvm,
  1666. "AGG for tid %d will be on queue #%d\n",
  1667. tid, txq_id);
  1668. tid_data = &mvmsta->tid_data[tid];
  1669. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  1670. tid_data->txq_id = txq_id;
  1671. *ssn = tid_data->ssn;
  1672. IWL_DEBUG_TX_QUEUES(mvm,
  1673. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  1674. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  1675. tid_data->next_reclaimed);
  1676. if (tid_data->ssn == tid_data->next_reclaimed) {
  1677. tid_data->state = IWL_AGG_STARTING;
  1678. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1679. } else {
  1680. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  1681. }
  1682. ret = 0;
  1683. release_locks:
  1684. spin_unlock_bh(&mvmsta->lock);
  1685. return ret;
  1686. }
  1687. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1688. struct ieee80211_sta *sta, u16 tid, u8 buf_size,
  1689. bool amsdu)
  1690. {
  1691. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1692. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  1693. unsigned int wdg_timeout =
  1694. iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
  1695. int queue, ret;
  1696. bool alloc_queue = true;
  1697. u16 ssn;
  1698. struct iwl_trans_txq_scd_cfg cfg = {
  1699. .sta_id = mvmsta->sta_id,
  1700. .tid = tid,
  1701. .frame_limit = buf_size,
  1702. .aggregate = true,
  1703. };
  1704. BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
  1705. != IWL_MAX_TID_COUNT);
  1706. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  1707. spin_lock_bh(&mvmsta->lock);
  1708. ssn = tid_data->ssn;
  1709. queue = tid_data->txq_id;
  1710. tid_data->state = IWL_AGG_ON;
  1711. mvmsta->agg_tids |= BIT(tid);
  1712. tid_data->ssn = 0xffff;
  1713. tid_data->amsdu_in_ampdu_allowed = amsdu;
  1714. spin_unlock_bh(&mvmsta->lock);
  1715. cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  1716. /* In DQA mode, the existing queue might need to be reconfigured */
  1717. if (iwl_mvm_is_dqa_supported(mvm)) {
  1718. spin_lock_bh(&mvm->queue_info_lock);
  1719. /* Maybe there is no need to even alloc a queue... */
  1720. if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
  1721. alloc_queue = false;
  1722. spin_unlock_bh(&mvm->queue_info_lock);
  1723. /*
  1724. * Only reconfig the SCD for the queue if the window size has
  1725. * changed from current (become smaller)
  1726. */
  1727. if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
  1728. /*
  1729. * If reconfiguring an existing queue, it first must be
  1730. * drained
  1731. */
  1732. ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
  1733. BIT(queue));
  1734. if (ret) {
  1735. IWL_ERR(mvm,
  1736. "Error draining queue before reconfig\n");
  1737. return ret;
  1738. }
  1739. ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
  1740. mvmsta->sta_id, tid,
  1741. buf_size, ssn);
  1742. if (ret) {
  1743. IWL_ERR(mvm,
  1744. "Error reconfiguring TXQ #%d\n", queue);
  1745. return ret;
  1746. }
  1747. }
  1748. }
  1749. if (alloc_queue)
  1750. iwl_mvm_enable_txq(mvm, queue,
  1751. vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
  1752. &cfg, wdg_timeout);
  1753. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  1754. if (ret)
  1755. return -EIO;
  1756. /* No need to mark as reserved */
  1757. spin_lock_bh(&mvm->queue_info_lock);
  1758. mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
  1759. spin_unlock_bh(&mvm->queue_info_lock);
  1760. /*
  1761. * Even though in theory the peer could have different
  1762. * aggregation reorder buffer sizes for different sessions,
  1763. * our ucode doesn't allow for that and has a global limit
  1764. * for each station. Therefore, use the minimum of all the
  1765. * aggregation sessions and our default value.
  1766. */
  1767. mvmsta->max_agg_bufsize =
  1768. min(mvmsta->max_agg_bufsize, buf_size);
  1769. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  1770. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  1771. sta->addr, tid);
  1772. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
  1773. }
  1774. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1775. struct ieee80211_sta *sta, u16 tid)
  1776. {
  1777. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1778. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  1779. u16 txq_id;
  1780. int err;
  1781. /*
  1782. * If mac80211 is cleaning its state, then say that we finished since
  1783. * our state has been cleared anyway.
  1784. */
  1785. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  1786. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1787. return 0;
  1788. }
  1789. spin_lock_bh(&mvmsta->lock);
  1790. txq_id = tid_data->txq_id;
  1791. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  1792. mvmsta->sta_id, tid, txq_id, tid_data->state);
  1793. mvmsta->agg_tids &= ~BIT(tid);
  1794. spin_lock_bh(&mvm->queue_info_lock);
  1795. /*
  1796. * The TXQ is marked as reserved only if no traffic came through yet
  1797. * This means no traffic has been sent on this TID (agg'd or not), so
  1798. * we no longer have use for the queue. Since it hasn't even been
  1799. * allocated through iwl_mvm_enable_txq, so we can just mark it back as
  1800. * free.
  1801. */
  1802. if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
  1803. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
  1804. spin_unlock_bh(&mvm->queue_info_lock);
  1805. switch (tid_data->state) {
  1806. case IWL_AGG_ON:
  1807. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  1808. IWL_DEBUG_TX_QUEUES(mvm,
  1809. "ssn = %d, next_recl = %d\n",
  1810. tid_data->ssn, tid_data->next_reclaimed);
  1811. /* There are still packets for this RA / TID in the HW */
  1812. if (tid_data->ssn != tid_data->next_reclaimed) {
  1813. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  1814. err = 0;
  1815. break;
  1816. }
  1817. tid_data->ssn = 0xffff;
  1818. tid_data->state = IWL_AGG_OFF;
  1819. spin_unlock_bh(&mvmsta->lock);
  1820. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1821. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  1822. if (!iwl_mvm_is_dqa_supported(mvm)) {
  1823. int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
  1824. iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
  1825. }
  1826. return 0;
  1827. case IWL_AGG_STARTING:
  1828. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  1829. /*
  1830. * The agg session has been stopped before it was set up. This
  1831. * can happen when the AddBA timer times out for example.
  1832. */
  1833. /* No barriers since we are under mutex */
  1834. lockdep_assert_held(&mvm->mutex);
  1835. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1836. tid_data->state = IWL_AGG_OFF;
  1837. err = 0;
  1838. break;
  1839. default:
  1840. IWL_ERR(mvm,
  1841. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  1842. mvmsta->sta_id, tid, tid_data->state);
  1843. IWL_ERR(mvm,
  1844. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  1845. err = -EINVAL;
  1846. }
  1847. spin_unlock_bh(&mvmsta->lock);
  1848. return err;
  1849. }
  1850. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1851. struct ieee80211_sta *sta, u16 tid)
  1852. {
  1853. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1854. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  1855. u16 txq_id;
  1856. enum iwl_mvm_agg_state old_state;
  1857. /*
  1858. * First set the agg state to OFF to avoid calling
  1859. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  1860. */
  1861. spin_lock_bh(&mvmsta->lock);
  1862. txq_id = tid_data->txq_id;
  1863. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  1864. mvmsta->sta_id, tid, txq_id, tid_data->state);
  1865. old_state = tid_data->state;
  1866. tid_data->state = IWL_AGG_OFF;
  1867. mvmsta->agg_tids &= ~BIT(tid);
  1868. spin_unlock_bh(&mvmsta->lock);
  1869. spin_lock_bh(&mvm->queue_info_lock);
  1870. /*
  1871. * The TXQ is marked as reserved only if no traffic came through yet
  1872. * This means no traffic has been sent on this TID (agg'd or not), so
  1873. * we no longer have use for the queue. Since it hasn't even been
  1874. * allocated through iwl_mvm_enable_txq, so we can just mark it back as
  1875. * free.
  1876. */
  1877. if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
  1878. mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
  1879. spin_unlock_bh(&mvm->queue_info_lock);
  1880. if (old_state >= IWL_AGG_ON) {
  1881. iwl_mvm_drain_sta(mvm, mvmsta, true);
  1882. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
  1883. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  1884. iwl_trans_wait_tx_queue_empty(mvm->trans,
  1885. mvmsta->tfd_queue_msk);
  1886. iwl_mvm_drain_sta(mvm, mvmsta, false);
  1887. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  1888. if (!iwl_mvm_is_dqa_supported(mvm)) {
  1889. int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
  1890. iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
  1891. tid, 0);
  1892. }
  1893. }
  1894. return 0;
  1895. }
  1896. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  1897. {
  1898. int i, max = -1, max_offs = -1;
  1899. lockdep_assert_held(&mvm->mutex);
  1900. /* Pick the unused key offset with the highest 'deleted'
  1901. * counter. Every time a key is deleted, all the counters
  1902. * are incremented and the one that was just deleted is
  1903. * reset to zero. Thus, the highest counter is the one
  1904. * that was deleted longest ago. Pick that one.
  1905. */
  1906. for (i = 0; i < STA_KEY_MAX_NUM; i++) {
  1907. if (test_bit(i, mvm->fw_key_table))
  1908. continue;
  1909. if (mvm->fw_key_deleted[i] > max) {
  1910. max = mvm->fw_key_deleted[i];
  1911. max_offs = i;
  1912. }
  1913. }
  1914. if (max_offs < 0)
  1915. return STA_KEY_IDX_INVALID;
  1916. return max_offs;
  1917. }
  1918. static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
  1919. struct ieee80211_vif *vif,
  1920. struct ieee80211_sta *sta)
  1921. {
  1922. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1923. if (sta)
  1924. return iwl_mvm_sta_from_mac80211(sta);
  1925. /*
  1926. * The device expects GTKs for station interfaces to be
  1927. * installed as GTKs for the AP station. If we have no
  1928. * station ID, then use AP's station ID.
  1929. */
  1930. if (vif->type == NL80211_IFTYPE_STATION &&
  1931. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  1932. u8 sta_id = mvmvif->ap_sta_id;
  1933. sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
  1934. lockdep_is_held(&mvm->mutex));
  1935. /*
  1936. * It is possible that the 'sta' parameter is NULL,
  1937. * for example when a GTK is removed - the sta_id will then
  1938. * be the AP ID, and no station was passed by mac80211.
  1939. */
  1940. if (IS_ERR_OR_NULL(sta))
  1941. return NULL;
  1942. return iwl_mvm_sta_from_mac80211(sta);
  1943. }
  1944. return NULL;
  1945. }
  1946. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  1947. struct iwl_mvm_sta *mvm_sta,
  1948. struct ieee80211_key_conf *keyconf, bool mcast,
  1949. u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
  1950. u8 key_offset)
  1951. {
  1952. struct iwl_mvm_add_sta_key_cmd cmd = {};
  1953. __le16 key_flags;
  1954. int ret;
  1955. u32 status;
  1956. u16 keyidx;
  1957. int i;
  1958. u8 sta_id = mvm_sta->sta_id;
  1959. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1960. STA_KEY_FLG_KEYID_MSK;
  1961. key_flags = cpu_to_le16(keyidx);
  1962. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  1963. switch (keyconf->cipher) {
  1964. case WLAN_CIPHER_SUITE_TKIP:
  1965. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  1966. cmd.tkip_rx_tsc_byte2 = tkip_iv32;
  1967. for (i = 0; i < 5; i++)
  1968. cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  1969. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  1970. break;
  1971. case WLAN_CIPHER_SUITE_CCMP:
  1972. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  1973. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  1974. break;
  1975. case WLAN_CIPHER_SUITE_WEP104:
  1976. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
  1977. /* fall through */
  1978. case WLAN_CIPHER_SUITE_WEP40:
  1979. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
  1980. memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
  1981. break;
  1982. case WLAN_CIPHER_SUITE_GCMP_256:
  1983. key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
  1984. /* fall through */
  1985. case WLAN_CIPHER_SUITE_GCMP:
  1986. key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
  1987. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  1988. break;
  1989. default:
  1990. key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
  1991. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  1992. }
  1993. if (mcast)
  1994. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1995. cmd.key_offset = key_offset;
  1996. cmd.key_flags = key_flags;
  1997. cmd.sta_id = sta_id;
  1998. status = ADD_STA_SUCCESS;
  1999. if (cmd_flags & CMD_ASYNC)
  2000. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
  2001. sizeof(cmd), &cmd);
  2002. else
  2003. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  2004. &cmd, &status);
  2005. switch (status) {
  2006. case ADD_STA_SUCCESS:
  2007. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  2008. break;
  2009. default:
  2010. ret = -EIO;
  2011. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  2012. break;
  2013. }
  2014. return ret;
  2015. }
  2016. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  2017. struct ieee80211_key_conf *keyconf,
  2018. u8 sta_id, bool remove_key)
  2019. {
  2020. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  2021. /* verify the key details match the required command's expectations */
  2022. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  2023. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  2024. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  2025. return -EINVAL;
  2026. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  2027. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  2028. if (remove_key) {
  2029. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  2030. } else {
  2031. struct ieee80211_key_seq seq;
  2032. const u8 *pn;
  2033. switch (keyconf->cipher) {
  2034. case WLAN_CIPHER_SUITE_AES_CMAC:
  2035. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
  2036. break;
  2037. default:
  2038. return -EINVAL;
  2039. }
  2040. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  2041. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  2042. pn = seq.aes_cmac.pn;
  2043. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  2044. ((u64) pn[4] << 8) |
  2045. ((u64) pn[3] << 16) |
  2046. ((u64) pn[2] << 24) |
  2047. ((u64) pn[1] << 32) |
  2048. ((u64) pn[0] << 40));
  2049. }
  2050. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  2051. remove_key ? "removing" : "installing",
  2052. igtk_cmd.sta_id);
  2053. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
  2054. sizeof(igtk_cmd), &igtk_cmd);
  2055. }
  2056. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  2057. struct ieee80211_vif *vif,
  2058. struct ieee80211_sta *sta)
  2059. {
  2060. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2061. if (sta)
  2062. return sta->addr;
  2063. if (vif->type == NL80211_IFTYPE_STATION &&
  2064. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  2065. u8 sta_id = mvmvif->ap_sta_id;
  2066. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  2067. lockdep_is_held(&mvm->mutex));
  2068. return sta->addr;
  2069. }
  2070. return NULL;
  2071. }
  2072. static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  2073. struct ieee80211_vif *vif,
  2074. struct ieee80211_sta *sta,
  2075. struct ieee80211_key_conf *keyconf,
  2076. u8 key_offset,
  2077. bool mcast)
  2078. {
  2079. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2080. int ret;
  2081. const u8 *addr;
  2082. struct ieee80211_key_seq seq;
  2083. u16 p1k[5];
  2084. switch (keyconf->cipher) {
  2085. case WLAN_CIPHER_SUITE_TKIP:
  2086. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  2087. /* get phase 1 key from mac80211 */
  2088. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  2089. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  2090. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2091. seq.tkip.iv32, p1k, 0, key_offset);
  2092. break;
  2093. case WLAN_CIPHER_SUITE_CCMP:
  2094. case WLAN_CIPHER_SUITE_WEP40:
  2095. case WLAN_CIPHER_SUITE_WEP104:
  2096. case WLAN_CIPHER_SUITE_GCMP:
  2097. case WLAN_CIPHER_SUITE_GCMP_256:
  2098. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2099. 0, NULL, 0, key_offset);
  2100. break;
  2101. default:
  2102. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2103. 0, NULL, 0, key_offset);
  2104. }
  2105. return ret;
  2106. }
  2107. static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
  2108. struct ieee80211_key_conf *keyconf,
  2109. bool mcast)
  2110. {
  2111. struct iwl_mvm_add_sta_key_cmd cmd = {};
  2112. __le16 key_flags;
  2113. int ret;
  2114. u32 status;
  2115. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  2116. STA_KEY_FLG_KEYID_MSK);
  2117. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  2118. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  2119. if (mcast)
  2120. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  2121. cmd.key_flags = key_flags;
  2122. cmd.key_offset = keyconf->hw_key_idx;
  2123. cmd.sta_id = sta_id;
  2124. status = ADD_STA_SUCCESS;
  2125. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  2126. &cmd, &status);
  2127. switch (status) {
  2128. case ADD_STA_SUCCESS:
  2129. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  2130. break;
  2131. default:
  2132. ret = -EIO;
  2133. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  2134. break;
  2135. }
  2136. return ret;
  2137. }
  2138. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  2139. struct ieee80211_vif *vif,
  2140. struct ieee80211_sta *sta,
  2141. struct ieee80211_key_conf *keyconf,
  2142. u8 key_offset)
  2143. {
  2144. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  2145. struct iwl_mvm_sta *mvm_sta;
  2146. u8 sta_id;
  2147. int ret;
  2148. static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
  2149. lockdep_assert_held(&mvm->mutex);
  2150. /* Get the station id from the mvm local station table */
  2151. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  2152. if (!mvm_sta) {
  2153. IWL_ERR(mvm, "Failed to find station\n");
  2154. return -EINVAL;
  2155. }
  2156. sta_id = mvm_sta->sta_id;
  2157. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  2158. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  2159. goto end;
  2160. }
  2161. /*
  2162. * It is possible that the 'sta' parameter is NULL, and thus
  2163. * there is a need to retrieve the sta from the local station table.
  2164. */
  2165. if (!sta) {
  2166. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  2167. lockdep_is_held(&mvm->mutex));
  2168. if (IS_ERR_OR_NULL(sta)) {
  2169. IWL_ERR(mvm, "Invalid station id\n");
  2170. return -EINVAL;
  2171. }
  2172. }
  2173. if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
  2174. return -EINVAL;
  2175. /* If the key_offset is not pre-assigned, we need to find a
  2176. * new offset to use. In normal cases, the offset is not
  2177. * pre-assigned, but during HW_RESTART we want to reuse the
  2178. * same indices, so we pass them when this function is called.
  2179. *
  2180. * In D3 entry, we need to hardcoded the indices (because the
  2181. * firmware hardcodes the PTK offset to 0). In this case, we
  2182. * need to make sure we don't overwrite the hw_key_idx in the
  2183. * keyconf structure, because otherwise we cannot configure
  2184. * the original ones back when resuming.
  2185. */
  2186. if (key_offset == STA_KEY_IDX_INVALID) {
  2187. key_offset = iwl_mvm_set_fw_key_idx(mvm);
  2188. if (key_offset == STA_KEY_IDX_INVALID)
  2189. return -ENOSPC;
  2190. keyconf->hw_key_idx = key_offset;
  2191. }
  2192. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
  2193. if (ret)
  2194. goto end;
  2195. /*
  2196. * For WEP, the same key is used for multicast and unicast. Upload it
  2197. * again, using the same key offset, and now pointing the other one
  2198. * to the same key slot (offset).
  2199. * If this fails, remove the original as well.
  2200. */
  2201. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2202. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
  2203. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
  2204. key_offset, !mcast);
  2205. if (ret) {
  2206. __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  2207. goto end;
  2208. }
  2209. }
  2210. __set_bit(key_offset, mvm->fw_key_table);
  2211. end:
  2212. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  2213. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  2214. sta ? sta->addr : zero_addr, ret);
  2215. return ret;
  2216. }
  2217. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  2218. struct ieee80211_vif *vif,
  2219. struct ieee80211_sta *sta,
  2220. struct ieee80211_key_conf *keyconf)
  2221. {
  2222. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  2223. struct iwl_mvm_sta *mvm_sta;
  2224. u8 sta_id = IWL_MVM_STATION_COUNT;
  2225. int ret, i;
  2226. lockdep_assert_held(&mvm->mutex);
  2227. /* Get the station from the mvm local station table */
  2228. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  2229. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  2230. keyconf->keyidx, sta_id);
  2231. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  2232. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  2233. if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
  2234. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  2235. keyconf->hw_key_idx);
  2236. return -ENOENT;
  2237. }
  2238. /* track which key was deleted last */
  2239. for (i = 0; i < STA_KEY_MAX_NUM; i++) {
  2240. if (mvm->fw_key_deleted[i] < U8_MAX)
  2241. mvm->fw_key_deleted[i]++;
  2242. }
  2243. mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
  2244. if (!mvm_sta) {
  2245. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  2246. return 0;
  2247. }
  2248. sta_id = mvm_sta->sta_id;
  2249. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  2250. if (ret)
  2251. return ret;
  2252. /* delete WEP key twice to get rid of (now useless) offset */
  2253. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2254. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
  2255. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
  2256. return ret;
  2257. }
  2258. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  2259. struct ieee80211_vif *vif,
  2260. struct ieee80211_key_conf *keyconf,
  2261. struct ieee80211_sta *sta, u32 iv32,
  2262. u16 *phase1key)
  2263. {
  2264. struct iwl_mvm_sta *mvm_sta;
  2265. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  2266. rcu_read_lock();
  2267. mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
  2268. if (WARN_ON_ONCE(!mvm_sta))
  2269. goto unlock;
  2270. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  2271. iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
  2272. unlock:
  2273. rcu_read_unlock();
  2274. }
  2275. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  2276. struct ieee80211_sta *sta)
  2277. {
  2278. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2279. struct iwl_mvm_add_sta_cmd cmd = {
  2280. .add_modify = STA_MODE_MODIFY,
  2281. .sta_id = mvmsta->sta_id,
  2282. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  2283. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  2284. };
  2285. int ret;
  2286. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  2287. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  2288. if (ret)
  2289. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  2290. }
  2291. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  2292. struct ieee80211_sta *sta,
  2293. enum ieee80211_frame_release_type reason,
  2294. u16 cnt, u16 tids, bool more_data,
  2295. bool agg)
  2296. {
  2297. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2298. struct iwl_mvm_add_sta_cmd cmd = {
  2299. .add_modify = STA_MODE_MODIFY,
  2300. .sta_id = mvmsta->sta_id,
  2301. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  2302. .sleep_tx_count = cpu_to_le16(cnt),
  2303. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  2304. };
  2305. int tid, ret;
  2306. unsigned long _tids = tids;
  2307. /* convert TIDs to ACs - we don't support TSPEC so that's OK
  2308. * Note that this field is reserved and unused by firmware not
  2309. * supporting GO uAPSD, so it's safe to always do this.
  2310. */
  2311. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
  2312. cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
  2313. /* If we're releasing frames from aggregation queues then check if the
  2314. * all queues combined that we're releasing frames from have
  2315. * - more frames than the service period, in which case more_data
  2316. * needs to be set
  2317. * - fewer than 'cnt' frames, in which case we need to adjust the
  2318. * firmware command (but do that unconditionally)
  2319. */
  2320. if (agg) {
  2321. int remaining = cnt;
  2322. int sleep_tx_count;
  2323. spin_lock_bh(&mvmsta->lock);
  2324. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
  2325. struct iwl_mvm_tid_data *tid_data;
  2326. u16 n_queued;
  2327. tid_data = &mvmsta->tid_data[tid];
  2328. if (WARN(tid_data->state != IWL_AGG_ON &&
  2329. tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
  2330. "TID %d state is %d\n",
  2331. tid, tid_data->state)) {
  2332. spin_unlock_bh(&mvmsta->lock);
  2333. ieee80211_sta_eosp(sta);
  2334. return;
  2335. }
  2336. n_queued = iwl_mvm_tid_queued(tid_data);
  2337. if (n_queued > remaining) {
  2338. more_data = true;
  2339. remaining = 0;
  2340. break;
  2341. }
  2342. remaining -= n_queued;
  2343. }
  2344. sleep_tx_count = cnt - remaining;
  2345. if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
  2346. mvmsta->sleep_tx_count = sleep_tx_count;
  2347. spin_unlock_bh(&mvmsta->lock);
  2348. cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
  2349. if (WARN_ON(cnt - remaining == 0)) {
  2350. ieee80211_sta_eosp(sta);
  2351. return;
  2352. }
  2353. }
  2354. /* Note: this is ignored by firmware not supporting GO uAPSD */
  2355. if (more_data)
  2356. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
  2357. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
  2358. mvmsta->next_status_eosp = true;
  2359. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
  2360. } else {
  2361. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
  2362. }
  2363. /* block the Tx queues until the FW updated the sleep Tx count */
  2364. iwl_trans_block_txq_ptrs(mvm->trans, true);
  2365. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
  2366. CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
  2367. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  2368. if (ret)
  2369. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  2370. }
  2371. void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
  2372. struct iwl_rx_cmd_buffer *rxb)
  2373. {
  2374. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  2375. struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
  2376. struct ieee80211_sta *sta;
  2377. u32 sta_id = le32_to_cpu(notif->sta_id);
  2378. if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
  2379. return;
  2380. rcu_read_lock();
  2381. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  2382. if (!IS_ERR_OR_NULL(sta))
  2383. ieee80211_sta_eosp(sta);
  2384. rcu_read_unlock();
  2385. }
  2386. void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
  2387. struct iwl_mvm_sta *mvmsta, bool disable)
  2388. {
  2389. struct iwl_mvm_add_sta_cmd cmd = {
  2390. .add_modify = STA_MODE_MODIFY,
  2391. .sta_id = mvmsta->sta_id,
  2392. .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
  2393. .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
  2394. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  2395. };
  2396. int ret;
  2397. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  2398. iwl_mvm_add_sta_cmd_size(mvm), &cmd);
  2399. if (ret)
  2400. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  2401. }
  2402. void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
  2403. struct ieee80211_sta *sta,
  2404. bool disable)
  2405. {
  2406. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2407. spin_lock_bh(&mvm_sta->lock);
  2408. if (mvm_sta->disable_tx == disable) {
  2409. spin_unlock_bh(&mvm_sta->lock);
  2410. return;
  2411. }
  2412. mvm_sta->disable_tx = disable;
  2413. /*
  2414. * Tell mac80211 to start/stop queuing tx for this station,
  2415. * but don't stop queuing if there are still pending frames
  2416. * for this station.
  2417. */
  2418. if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
  2419. ieee80211_sta_block_awake(mvm->hw, sta, disable);
  2420. iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
  2421. spin_unlock_bh(&mvm_sta->lock);
  2422. }
  2423. void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
  2424. struct iwl_mvm_vif *mvmvif,
  2425. bool disable)
  2426. {
  2427. struct ieee80211_sta *sta;
  2428. struct iwl_mvm_sta *mvm_sta;
  2429. int i;
  2430. lockdep_assert_held(&mvm->mutex);
  2431. /* Block/unblock all the stations of the given mvmvif */
  2432. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  2433. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  2434. lockdep_is_held(&mvm->mutex));
  2435. if (IS_ERR_OR_NULL(sta))
  2436. continue;
  2437. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  2438. if (mvm_sta->mac_id_n_color !=
  2439. FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
  2440. continue;
  2441. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
  2442. }
  2443. }
  2444. void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  2445. {
  2446. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  2447. struct iwl_mvm_sta *mvmsta;
  2448. rcu_read_lock();
  2449. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
  2450. if (!WARN_ON(!mvmsta))
  2451. iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
  2452. rcu_read_unlock();
  2453. }