sta.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <net/mac80211.h>
  66. #include "mvm.h"
  67. #include "sta.h"
  68. #include "rs.h"
  69. static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
  70. enum nl80211_iftype iftype)
  71. {
  72. int sta_id;
  73. u32 reserved_ids = 0;
  74. BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
  75. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  76. lockdep_assert_held(&mvm->mutex);
  77. /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
  78. if (iftype != NL80211_IFTYPE_STATION)
  79. reserved_ids = BIT(0);
  80. /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
  81. for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
  82. if (BIT(sta_id) & reserved_ids)
  83. continue;
  84. if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  85. lockdep_is_held(&mvm->mutex)))
  86. return sta_id;
  87. }
  88. return IWL_MVM_STATION_COUNT;
  89. }
  90. /* send station add/update command to firmware */
  91. int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  92. bool update)
  93. {
  94. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  95. struct iwl_mvm_add_sta_cmd add_sta_cmd = {
  96. .sta_id = mvm_sta->sta_id,
  97. .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
  98. .add_modify = update ? 1 : 0,
  99. .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
  100. STA_FLG_MIMO_EN_MSK),
  101. };
  102. int ret;
  103. u32 status;
  104. u32 agg_size = 0, mpdu_dens = 0;
  105. if (!update) {
  106. add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  107. memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
  108. }
  109. switch (sta->bandwidth) {
  110. case IEEE80211_STA_RX_BW_160:
  111. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
  112. /* fall through */
  113. case IEEE80211_STA_RX_BW_80:
  114. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
  115. /* fall through */
  116. case IEEE80211_STA_RX_BW_40:
  117. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
  118. /* fall through */
  119. case IEEE80211_STA_RX_BW_20:
  120. if (sta->ht_cap.ht_supported)
  121. add_sta_cmd.station_flags |=
  122. cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
  123. break;
  124. }
  125. switch (sta->rx_nss) {
  126. case 1:
  127. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  128. break;
  129. case 2:
  130. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
  131. break;
  132. case 3 ... 8:
  133. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
  134. break;
  135. }
  136. switch (sta->smps_mode) {
  137. case IEEE80211_SMPS_AUTOMATIC:
  138. case IEEE80211_SMPS_NUM_MODES:
  139. WARN_ON(1);
  140. break;
  141. case IEEE80211_SMPS_STATIC:
  142. /* override NSS */
  143. add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
  144. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
  145. break;
  146. case IEEE80211_SMPS_DYNAMIC:
  147. add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
  148. break;
  149. case IEEE80211_SMPS_OFF:
  150. /* nothing */
  151. break;
  152. }
  153. if (sta->ht_cap.ht_supported) {
  154. add_sta_cmd.station_flags_msk |=
  155. cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
  156. STA_FLG_AGG_MPDU_DENS_MSK);
  157. mpdu_dens = sta->ht_cap.ampdu_density;
  158. }
  159. if (sta->vht_cap.vht_supported) {
  160. agg_size = sta->vht_cap.cap &
  161. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  162. agg_size >>=
  163. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  164. } else if (sta->ht_cap.ht_supported) {
  165. agg_size = sta->ht_cap.ampdu_factor;
  166. }
  167. add_sta_cmd.station_flags |=
  168. cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
  169. add_sta_cmd.station_flags |=
  170. cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
  171. status = ADD_STA_SUCCESS;
  172. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
  173. &add_sta_cmd, &status);
  174. if (ret)
  175. return ret;
  176. switch (status) {
  177. case ADD_STA_SUCCESS:
  178. IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
  179. break;
  180. default:
  181. ret = -EIO;
  182. IWL_ERR(mvm, "ADD_STA failed\n");
  183. break;
  184. }
  185. return ret;
  186. }
  187. static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
  188. struct ieee80211_sta *sta)
  189. {
  190. unsigned long used_hw_queues;
  191. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  192. u32 ac;
  193. lockdep_assert_held(&mvm->mutex);
  194. used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
  195. /* Find available queues, and allocate them to the ACs */
  196. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  197. u8 queue = find_first_zero_bit(&used_hw_queues,
  198. mvm->first_agg_queue);
  199. if (queue >= mvm->first_agg_queue) {
  200. IWL_ERR(mvm, "Failed to allocate STA queue\n");
  201. return -EBUSY;
  202. }
  203. __set_bit(queue, &used_hw_queues);
  204. mvmsta->hw_queue[ac] = queue;
  205. }
  206. /* Found a place for all queues - enable them */
  207. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  208. iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
  209. iwl_mvm_ac_to_tx_fifo[ac]);
  210. mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
  211. }
  212. return 0;
  213. }
  214. static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
  215. struct ieee80211_sta *sta)
  216. {
  217. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  218. unsigned long sta_msk;
  219. int i;
  220. lockdep_assert_held(&mvm->mutex);
  221. /* disable the TDLS STA-specific queues */
  222. sta_msk = mvmsta->tfd_queue_msk;
  223. for_each_set_bit(i, &sta_msk, sizeof(sta_msk))
  224. iwl_mvm_disable_txq(mvm, i);
  225. }
  226. int iwl_mvm_add_sta(struct iwl_mvm *mvm,
  227. struct ieee80211_vif *vif,
  228. struct ieee80211_sta *sta)
  229. {
  230. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  231. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  232. int i, ret, sta_id;
  233. lockdep_assert_held(&mvm->mutex);
  234. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  235. sta_id = iwl_mvm_find_free_sta_id(mvm,
  236. ieee80211_vif_type_p2p(vif));
  237. else
  238. sta_id = mvm_sta->sta_id;
  239. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  240. return -ENOSPC;
  241. spin_lock_init(&mvm_sta->lock);
  242. mvm_sta->sta_id = sta_id;
  243. mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
  244. mvmvif->color);
  245. mvm_sta->vif = vif;
  246. mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  247. mvm_sta->tx_protection = 0;
  248. mvm_sta->tt_tx_protection = false;
  249. /* HW restart, don't assume the memory has been zeroed */
  250. atomic_set(&mvm->pending_frames[sta_id], 0);
  251. mvm_sta->tid_disable_agg = 0;
  252. mvm_sta->tfd_queue_msk = 0;
  253. /* allocate new queues for a TDLS station */
  254. if (sta->tdls) {
  255. ret = iwl_mvm_tdls_sta_init(mvm, sta);
  256. if (ret)
  257. return ret;
  258. } else {
  259. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  260. if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
  261. mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
  262. }
  263. /* for HW restart - reset everything but the sequence number */
  264. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  265. u16 seq = mvm_sta->tid_data[i].seq_number;
  266. memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
  267. mvm_sta->tid_data[i].seq_number = seq;
  268. }
  269. mvm_sta->agg_tids = 0;
  270. ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
  271. if (ret)
  272. goto err;
  273. if (vif->type == NL80211_IFTYPE_STATION) {
  274. if (!sta->tdls) {
  275. WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
  276. mvmvif->ap_sta_id = sta_id;
  277. } else {
  278. WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
  279. }
  280. }
  281. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  282. return 0;
  283. err:
  284. iwl_mvm_tdls_sta_deinit(mvm, sta);
  285. return ret;
  286. }
  287. int iwl_mvm_update_sta(struct iwl_mvm *mvm,
  288. struct ieee80211_vif *vif,
  289. struct ieee80211_sta *sta)
  290. {
  291. return iwl_mvm_sta_send_to_fw(mvm, sta, true);
  292. }
  293. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  294. bool drain)
  295. {
  296. struct iwl_mvm_add_sta_cmd cmd = {};
  297. int ret;
  298. u32 status;
  299. lockdep_assert_held(&mvm->mutex);
  300. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  301. cmd.sta_id = mvmsta->sta_id;
  302. cmd.add_modify = STA_MODE_MODIFY;
  303. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  304. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  305. status = ADD_STA_SUCCESS;
  306. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  307. &cmd, &status);
  308. if (ret)
  309. return ret;
  310. switch (status) {
  311. case ADD_STA_SUCCESS:
  312. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  313. mvmsta->sta_id);
  314. break;
  315. default:
  316. ret = -EIO;
  317. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  318. mvmsta->sta_id);
  319. break;
  320. }
  321. return ret;
  322. }
  323. /*
  324. * Remove a station from the FW table. Before sending the command to remove
  325. * the station validate that the station is indeed known to the driver (sanity
  326. * only).
  327. */
  328. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  329. {
  330. struct ieee80211_sta *sta;
  331. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  332. .sta_id = sta_id,
  333. };
  334. int ret;
  335. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  336. lockdep_is_held(&mvm->mutex));
  337. /* Note: internal stations are marked as error values */
  338. if (!sta) {
  339. IWL_ERR(mvm, "Invalid station id\n");
  340. return -EINVAL;
  341. }
  342. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
  343. sizeof(rm_sta_cmd), &rm_sta_cmd);
  344. if (ret) {
  345. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  346. return ret;
  347. }
  348. return 0;
  349. }
  350. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  351. {
  352. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  353. u8 sta_id;
  354. /*
  355. * The mutex is needed because of the SYNC cmd, but not only: if the
  356. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  357. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  358. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  359. * that later.
  360. */
  361. mutex_lock(&mvm->mutex);
  362. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  363. int ret;
  364. struct ieee80211_sta *sta =
  365. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  366. lockdep_is_held(&mvm->mutex));
  367. /*
  368. * This station is in use or RCU-removed; the latter happens in
  369. * managed mode, where mac80211 removes the station before we
  370. * can remove it from firmware (we can only do that after the
  371. * MAC is marked unassociated), and possibly while the deauth
  372. * frame to disconnect from the AP is still queued. Then, the
  373. * station pointer is -ENOENT when the last skb is reclaimed.
  374. */
  375. if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
  376. continue;
  377. if (PTR_ERR(sta) == -EINVAL) {
  378. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  379. sta_id);
  380. continue;
  381. }
  382. if (!sta) {
  383. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  384. sta_id);
  385. continue;
  386. }
  387. WARN_ON(PTR_ERR(sta) != -EBUSY);
  388. /* This station was removed and we waited until it got drained,
  389. * we can now proceed and remove it.
  390. */
  391. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  392. if (ret) {
  393. IWL_ERR(mvm,
  394. "Couldn't remove sta %d after it was drained\n",
  395. sta_id);
  396. continue;
  397. }
  398. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  399. clear_bit(sta_id, mvm->sta_drained);
  400. if (mvm->tfd_drained[sta_id]) {
  401. unsigned long i, msk = mvm->tfd_drained[sta_id];
  402. for_each_set_bit(i, &msk, sizeof(msk))
  403. iwl_mvm_disable_txq(mvm, i);
  404. mvm->tfd_drained[sta_id] = 0;
  405. IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
  406. sta_id, msk);
  407. }
  408. }
  409. mutex_unlock(&mvm->mutex);
  410. }
  411. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  412. struct ieee80211_vif *vif,
  413. struct ieee80211_sta *sta)
  414. {
  415. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  416. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  417. int ret;
  418. lockdep_assert_held(&mvm->mutex);
  419. if (vif->type == NL80211_IFTYPE_STATION &&
  420. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  421. /* flush its queues here since we are freeing mvm_sta */
  422. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
  423. /* if we are associated - we can't remove the AP STA now */
  424. if (vif->bss_conf.assoc)
  425. return ret;
  426. /* unassoc - go ahead - remove the AP STA now */
  427. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  428. /* clear d0i3_ap_sta_id if no longer relevant */
  429. if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
  430. mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
  431. }
  432. /*
  433. * This shouldn't happen - the TDLS channel switch should be canceled
  434. * before the STA is removed.
  435. */
  436. if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
  437. mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
  438. cancel_delayed_work(&mvm->tdls_cs.dwork);
  439. }
  440. /*
  441. * Make sure that the tx response code sees the station as -EBUSY and
  442. * calls the drain worker.
  443. */
  444. spin_lock_bh(&mvm_sta->lock);
  445. /*
  446. * There are frames pending on the AC queues for this station.
  447. * We need to wait until all the frames are drained...
  448. */
  449. if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
  450. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  451. ERR_PTR(-EBUSY));
  452. spin_unlock_bh(&mvm_sta->lock);
  453. /* disable TDLS sta queues on drain complete */
  454. if (sta->tdls) {
  455. mvm->tfd_drained[mvm_sta->sta_id] =
  456. mvm_sta->tfd_queue_msk;
  457. IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
  458. mvm_sta->sta_id);
  459. }
  460. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  461. } else {
  462. spin_unlock_bh(&mvm_sta->lock);
  463. if (sta->tdls)
  464. iwl_mvm_tdls_sta_deinit(mvm, sta);
  465. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  466. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  467. }
  468. return ret;
  469. }
  470. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  471. struct ieee80211_vif *vif,
  472. u8 sta_id)
  473. {
  474. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  475. lockdep_assert_held(&mvm->mutex);
  476. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
  477. return ret;
  478. }
  479. static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
  480. struct iwl_mvm_int_sta *sta,
  481. u32 qmask, enum nl80211_iftype iftype)
  482. {
  483. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  484. sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
  485. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  486. return -ENOSPC;
  487. }
  488. sta->tfd_queue_msk = qmask;
  489. /* put a non-NULL value so iterating over the stations won't stop */
  490. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  491. return 0;
  492. }
  493. static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
  494. struct iwl_mvm_int_sta *sta)
  495. {
  496. RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  497. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  498. sta->sta_id = IWL_MVM_STATION_COUNT;
  499. }
  500. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  501. struct iwl_mvm_int_sta *sta,
  502. const u8 *addr,
  503. u16 mac_id, u16 color)
  504. {
  505. struct iwl_mvm_add_sta_cmd cmd;
  506. int ret;
  507. u32 status;
  508. lockdep_assert_held(&mvm->mutex);
  509. memset(&cmd, 0, sizeof(cmd));
  510. cmd.sta_id = sta->sta_id;
  511. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  512. color));
  513. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  514. if (addr)
  515. memcpy(cmd.addr, addr, ETH_ALEN);
  516. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  517. &cmd, &status);
  518. if (ret)
  519. return ret;
  520. switch (status) {
  521. case ADD_STA_SUCCESS:
  522. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  523. return 0;
  524. default:
  525. ret = -EIO;
  526. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  527. status);
  528. break;
  529. }
  530. return ret;
  531. }
  532. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  533. {
  534. int ret;
  535. lockdep_assert_held(&mvm->mutex);
  536. /* Map Aux queue to fifo - needs to happen before adding Aux station */
  537. iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue,
  538. IWL_MVM_TX_FIFO_MCAST);
  539. /* Allocate aux station and assign to it the aux queue */
  540. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
  541. NL80211_IFTYPE_UNSPECIFIED);
  542. if (ret)
  543. return ret;
  544. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  545. MAC_INDEX_AUX, 0);
  546. if (ret)
  547. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  548. return ret;
  549. }
  550. void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
  551. {
  552. lockdep_assert_held(&mvm->mutex);
  553. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  554. }
  555. /*
  556. * Send the add station command for the vif's broadcast station.
  557. * Assumes that the station was already allocated.
  558. *
  559. * @mvm: the mvm component
  560. * @vif: the interface to which the broadcast station is added
  561. * @bsta: the broadcast station to add.
  562. */
  563. int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  564. {
  565. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  566. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  567. static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  568. const u8 *baddr = _baddr;
  569. lockdep_assert_held(&mvm->mutex);
  570. if (vif->type == NL80211_IFTYPE_ADHOC)
  571. baddr = vif->bss_conf.bssid;
  572. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  573. return -ENOSPC;
  574. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  575. mvmvif->id, mvmvif->color);
  576. }
  577. /* Send the FW a request to remove the station from it's internal data
  578. * structures, but DO NOT remove the entry from the local data structures. */
  579. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  580. {
  581. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  582. int ret;
  583. lockdep_assert_held(&mvm->mutex);
  584. ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
  585. if (ret)
  586. IWL_WARN(mvm, "Failed sending remove station\n");
  587. return ret;
  588. }
  589. int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  590. {
  591. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  592. u32 qmask;
  593. lockdep_assert_held(&mvm->mutex);
  594. qmask = iwl_mvm_mac_get_queues_mask(vif);
  595. /*
  596. * The firmware defines the TFD queue mask to only be relevant
  597. * for *unicast* queues, so the multicast (CAB) queue shouldn't
  598. * be included.
  599. */
  600. if (vif->type == NL80211_IFTYPE_AP)
  601. qmask &= ~BIT(vif->cab_queue);
  602. return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
  603. ieee80211_vif_type_p2p(vif));
  604. }
  605. /* Allocate a new station entry for the broadcast station to the given vif,
  606. * and send it to the FW.
  607. * Note that each P2P mac should have its own broadcast station.
  608. *
  609. * @mvm: the mvm component
  610. * @vif: the interface to which the broadcast station is added
  611. * @bsta: the broadcast station to add. */
  612. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  613. {
  614. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  615. struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
  616. int ret;
  617. lockdep_assert_held(&mvm->mutex);
  618. ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
  619. if (ret)
  620. return ret;
  621. ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
  622. if (ret)
  623. iwl_mvm_dealloc_int_sta(mvm, bsta);
  624. return ret;
  625. }
  626. void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  627. {
  628. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  629. iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
  630. }
  631. /*
  632. * Send the FW a request to remove the station from it's internal data
  633. * structures, and in addition remove it from the local data structure.
  634. */
  635. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  636. {
  637. int ret;
  638. lockdep_assert_held(&mvm->mutex);
  639. ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
  640. iwl_mvm_dealloc_bcast_sta(mvm, vif);
  641. return ret;
  642. }
  643. #define IWL_MAX_RX_BA_SESSIONS 16
  644. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  645. int tid, u16 ssn, bool start)
  646. {
  647. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  648. struct iwl_mvm_add_sta_cmd cmd = {};
  649. int ret;
  650. u32 status;
  651. lockdep_assert_held(&mvm->mutex);
  652. if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
  653. IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
  654. return -ENOSPC;
  655. }
  656. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  657. cmd.sta_id = mvm_sta->sta_id;
  658. cmd.add_modify = STA_MODE_MODIFY;
  659. if (start) {
  660. cmd.add_immediate_ba_tid = (u8) tid;
  661. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  662. } else {
  663. cmd.remove_immediate_ba_tid = (u8) tid;
  664. }
  665. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  666. STA_MODIFY_REMOVE_BA_TID;
  667. status = ADD_STA_SUCCESS;
  668. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  669. &cmd, &status);
  670. if (ret)
  671. return ret;
  672. switch (status) {
  673. case ADD_STA_SUCCESS:
  674. IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
  675. start ? "start" : "stopp");
  676. break;
  677. case ADD_STA_IMMEDIATE_BA_FAILURE:
  678. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  679. ret = -ENOSPC;
  680. break;
  681. default:
  682. ret = -EIO;
  683. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  684. start ? "start" : "stopp", status);
  685. break;
  686. }
  687. if (!ret) {
  688. if (start)
  689. mvm->rx_ba_sessions++;
  690. else if (mvm->rx_ba_sessions > 0)
  691. /* check that restart flow didn't zero the counter */
  692. mvm->rx_ba_sessions--;
  693. }
  694. return ret;
  695. }
  696. static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  697. int tid, u8 queue, bool start)
  698. {
  699. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  700. struct iwl_mvm_add_sta_cmd cmd = {};
  701. int ret;
  702. u32 status;
  703. lockdep_assert_held(&mvm->mutex);
  704. if (start) {
  705. mvm_sta->tfd_queue_msk |= BIT(queue);
  706. mvm_sta->tid_disable_agg &= ~BIT(tid);
  707. } else {
  708. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  709. mvm_sta->tid_disable_agg |= BIT(tid);
  710. }
  711. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  712. cmd.sta_id = mvm_sta->sta_id;
  713. cmd.add_modify = STA_MODE_MODIFY;
  714. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  715. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  716. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  717. status = ADD_STA_SUCCESS;
  718. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  719. &cmd, &status);
  720. if (ret)
  721. return ret;
  722. switch (status) {
  723. case ADD_STA_SUCCESS:
  724. break;
  725. default:
  726. ret = -EIO;
  727. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  728. start ? "start" : "stopp", status);
  729. break;
  730. }
  731. return ret;
  732. }
  733. const u8 tid_to_mac80211_ac[] = {
  734. IEEE80211_AC_BE,
  735. IEEE80211_AC_BK,
  736. IEEE80211_AC_BK,
  737. IEEE80211_AC_BE,
  738. IEEE80211_AC_VI,
  739. IEEE80211_AC_VI,
  740. IEEE80211_AC_VO,
  741. IEEE80211_AC_VO,
  742. };
  743. static const u8 tid_to_ucode_ac[] = {
  744. AC_BE,
  745. AC_BK,
  746. AC_BK,
  747. AC_BE,
  748. AC_VI,
  749. AC_VI,
  750. AC_VO,
  751. AC_VO,
  752. };
  753. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  754. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  755. {
  756. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  757. struct iwl_mvm_tid_data *tid_data;
  758. int txq_id;
  759. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  760. return -EINVAL;
  761. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  762. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  763. mvmsta->tid_data[tid].state);
  764. return -ENXIO;
  765. }
  766. lockdep_assert_held(&mvm->mutex);
  767. for (txq_id = mvm->first_agg_queue;
  768. txq_id <= mvm->last_agg_queue; txq_id++)
  769. if (mvm->queue_to_mac80211[txq_id] ==
  770. IWL_INVALID_MAC80211_QUEUE)
  771. break;
  772. if (txq_id > mvm->last_agg_queue) {
  773. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  774. return -EIO;
  775. }
  776. spin_lock_bh(&mvmsta->lock);
  777. /* possible race condition - we entered D0i3 while starting agg */
  778. if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
  779. spin_unlock_bh(&mvmsta->lock);
  780. IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
  781. return -EIO;
  782. }
  783. /* the new tx queue is still connected to the same mac80211 queue */
  784. mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
  785. tid_data = &mvmsta->tid_data[tid];
  786. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  787. tid_data->txq_id = txq_id;
  788. *ssn = tid_data->ssn;
  789. IWL_DEBUG_TX_QUEUES(mvm,
  790. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  791. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  792. tid_data->next_reclaimed);
  793. if (tid_data->ssn == tid_data->next_reclaimed) {
  794. tid_data->state = IWL_AGG_STARTING;
  795. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  796. } else {
  797. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  798. }
  799. spin_unlock_bh(&mvmsta->lock);
  800. return 0;
  801. }
  802. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  803. struct ieee80211_sta *sta, u16 tid, u8 buf_size)
  804. {
  805. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  806. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  807. int queue, fifo, ret;
  808. u16 ssn;
  809. BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
  810. != IWL_MAX_TID_COUNT);
  811. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  812. spin_lock_bh(&mvmsta->lock);
  813. ssn = tid_data->ssn;
  814. queue = tid_data->txq_id;
  815. tid_data->state = IWL_AGG_ON;
  816. mvmsta->agg_tids |= BIT(tid);
  817. tid_data->ssn = 0xffff;
  818. spin_unlock_bh(&mvmsta->lock);
  819. fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
  820. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  821. if (ret)
  822. return -EIO;
  823. iwl_mvm_enable_agg_txq(mvm, queue, fifo, mvmsta->sta_id, tid,
  824. buf_size, ssn);
  825. /*
  826. * Even though in theory the peer could have different
  827. * aggregation reorder buffer sizes for different sessions,
  828. * our ucode doesn't allow for that and has a global limit
  829. * for each station. Therefore, use the minimum of all the
  830. * aggregation sessions and our default value.
  831. */
  832. mvmsta->max_agg_bufsize =
  833. min(mvmsta->max_agg_bufsize, buf_size);
  834. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  835. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  836. sta->addr, tid);
  837. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
  838. }
  839. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  840. struct ieee80211_sta *sta, u16 tid)
  841. {
  842. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  843. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  844. u16 txq_id;
  845. int err;
  846. /*
  847. * If mac80211 is cleaning its state, then say that we finished since
  848. * our state has been cleared anyway.
  849. */
  850. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  851. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  852. return 0;
  853. }
  854. spin_lock_bh(&mvmsta->lock);
  855. txq_id = tid_data->txq_id;
  856. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  857. mvmsta->sta_id, tid, txq_id, tid_data->state);
  858. mvmsta->agg_tids &= ~BIT(tid);
  859. switch (tid_data->state) {
  860. case IWL_AGG_ON:
  861. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  862. IWL_DEBUG_TX_QUEUES(mvm,
  863. "ssn = %d, next_recl = %d\n",
  864. tid_data->ssn, tid_data->next_reclaimed);
  865. /* There are still packets for this RA / TID in the HW */
  866. if (tid_data->ssn != tid_data->next_reclaimed) {
  867. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  868. err = 0;
  869. break;
  870. }
  871. tid_data->ssn = 0xffff;
  872. tid_data->state = IWL_AGG_OFF;
  873. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  874. spin_unlock_bh(&mvmsta->lock);
  875. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  876. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  877. iwl_mvm_disable_txq(mvm, txq_id);
  878. return 0;
  879. case IWL_AGG_STARTING:
  880. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  881. /*
  882. * The agg session has been stopped before it was set up. This
  883. * can happen when the AddBA timer times out for example.
  884. */
  885. /* No barriers since we are under mutex */
  886. lockdep_assert_held(&mvm->mutex);
  887. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  888. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  889. tid_data->state = IWL_AGG_OFF;
  890. err = 0;
  891. break;
  892. default:
  893. IWL_ERR(mvm,
  894. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  895. mvmsta->sta_id, tid, tid_data->state);
  896. IWL_ERR(mvm,
  897. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  898. err = -EINVAL;
  899. }
  900. spin_unlock_bh(&mvmsta->lock);
  901. return err;
  902. }
  903. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  904. struct ieee80211_sta *sta, u16 tid)
  905. {
  906. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  907. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  908. u16 txq_id;
  909. enum iwl_mvm_agg_state old_state;
  910. /*
  911. * First set the agg state to OFF to avoid calling
  912. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  913. */
  914. spin_lock_bh(&mvmsta->lock);
  915. txq_id = tid_data->txq_id;
  916. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  917. mvmsta->sta_id, tid, txq_id, tid_data->state);
  918. old_state = tid_data->state;
  919. tid_data->state = IWL_AGG_OFF;
  920. mvmsta->agg_tids &= ~BIT(tid);
  921. spin_unlock_bh(&mvmsta->lock);
  922. if (old_state >= IWL_AGG_ON) {
  923. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
  924. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  925. iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
  926. iwl_mvm_disable_txq(mvm, tid_data->txq_id);
  927. }
  928. mvm->queue_to_mac80211[tid_data->txq_id] =
  929. IWL_INVALID_MAC80211_QUEUE;
  930. return 0;
  931. }
  932. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  933. {
  934. int i;
  935. lockdep_assert_held(&mvm->mutex);
  936. i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
  937. if (i == STA_KEY_MAX_NUM)
  938. return STA_KEY_IDX_INVALID;
  939. __set_bit(i, mvm->fw_key_table);
  940. return i;
  941. }
  942. static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
  943. struct ieee80211_sta *sta)
  944. {
  945. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  946. if (sta) {
  947. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  948. return mvm_sta->sta_id;
  949. }
  950. /*
  951. * The device expects GTKs for station interfaces to be
  952. * installed as GTKs for the AP station. If we have no
  953. * station ID, then use AP's station ID.
  954. */
  955. if (vif->type == NL80211_IFTYPE_STATION &&
  956. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
  957. return mvmvif->ap_sta_id;
  958. return IWL_MVM_STATION_COUNT;
  959. }
  960. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  961. struct iwl_mvm_sta *mvm_sta,
  962. struct ieee80211_key_conf *keyconf, bool mcast,
  963. u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags)
  964. {
  965. struct iwl_mvm_add_sta_key_cmd cmd = {};
  966. __le16 key_flags;
  967. int ret;
  968. u32 status;
  969. u16 keyidx;
  970. int i;
  971. u8 sta_id = mvm_sta->sta_id;
  972. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  973. STA_KEY_FLG_KEYID_MSK;
  974. key_flags = cpu_to_le16(keyidx);
  975. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  976. switch (keyconf->cipher) {
  977. case WLAN_CIPHER_SUITE_TKIP:
  978. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  979. cmd.tkip_rx_tsc_byte2 = tkip_iv32;
  980. for (i = 0; i < 5; i++)
  981. cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  982. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  983. break;
  984. case WLAN_CIPHER_SUITE_CCMP:
  985. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  986. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  987. break;
  988. case WLAN_CIPHER_SUITE_WEP104:
  989. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
  990. case WLAN_CIPHER_SUITE_WEP40:
  991. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
  992. memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
  993. break;
  994. default:
  995. key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
  996. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  997. }
  998. if (mcast)
  999. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1000. cmd.key_offset = keyconf->hw_key_idx;
  1001. cmd.key_flags = key_flags;
  1002. cmd.sta_id = sta_id;
  1003. status = ADD_STA_SUCCESS;
  1004. if (cmd_flags & CMD_ASYNC)
  1005. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
  1006. sizeof(cmd), &cmd);
  1007. else
  1008. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  1009. &cmd, &status);
  1010. switch (status) {
  1011. case ADD_STA_SUCCESS:
  1012. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  1013. break;
  1014. default:
  1015. ret = -EIO;
  1016. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  1017. break;
  1018. }
  1019. return ret;
  1020. }
  1021. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  1022. struct ieee80211_key_conf *keyconf,
  1023. u8 sta_id, bool remove_key)
  1024. {
  1025. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  1026. /* verify the key details match the required command's expectations */
  1027. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  1028. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  1029. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  1030. return -EINVAL;
  1031. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  1032. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  1033. if (remove_key) {
  1034. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  1035. } else {
  1036. struct ieee80211_key_seq seq;
  1037. const u8 *pn;
  1038. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  1039. ieee80211_aes_cmac_calculate_k1_k2(keyconf,
  1040. igtk_cmd.K1, igtk_cmd.K2);
  1041. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1042. pn = seq.aes_cmac.pn;
  1043. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  1044. ((u64) pn[4] << 8) |
  1045. ((u64) pn[3] << 16) |
  1046. ((u64) pn[2] << 24) |
  1047. ((u64) pn[1] << 32) |
  1048. ((u64) pn[0] << 40));
  1049. }
  1050. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  1051. remove_key ? "removing" : "installing",
  1052. igtk_cmd.sta_id);
  1053. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
  1054. sizeof(igtk_cmd), &igtk_cmd);
  1055. }
  1056. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  1057. struct ieee80211_vif *vif,
  1058. struct ieee80211_sta *sta)
  1059. {
  1060. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  1061. if (sta)
  1062. return sta->addr;
  1063. if (vif->type == NL80211_IFTYPE_STATION &&
  1064. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  1065. u8 sta_id = mvmvif->ap_sta_id;
  1066. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1067. lockdep_is_held(&mvm->mutex));
  1068. return sta->addr;
  1069. }
  1070. return NULL;
  1071. }
  1072. static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  1073. struct ieee80211_vif *vif,
  1074. struct ieee80211_sta *sta,
  1075. struct ieee80211_key_conf *keyconf,
  1076. bool mcast)
  1077. {
  1078. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1079. int ret;
  1080. const u8 *addr;
  1081. struct ieee80211_key_seq seq;
  1082. u16 p1k[5];
  1083. switch (keyconf->cipher) {
  1084. case WLAN_CIPHER_SUITE_TKIP:
  1085. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  1086. /* get phase 1 key from mac80211 */
  1087. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1088. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1089. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1090. seq.tkip.iv32, p1k, 0);
  1091. break;
  1092. case WLAN_CIPHER_SUITE_CCMP:
  1093. case WLAN_CIPHER_SUITE_WEP40:
  1094. case WLAN_CIPHER_SUITE_WEP104:
  1095. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1096. 0, NULL, 0);
  1097. break;
  1098. default:
  1099. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1100. 0, NULL, 0);
  1101. }
  1102. return ret;
  1103. }
  1104. static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
  1105. struct ieee80211_key_conf *keyconf,
  1106. bool mcast)
  1107. {
  1108. struct iwl_mvm_add_sta_key_cmd cmd = {};
  1109. __le16 key_flags;
  1110. int ret;
  1111. u32 status;
  1112. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1113. STA_KEY_FLG_KEYID_MSK);
  1114. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  1115. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  1116. if (mcast)
  1117. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1118. cmd.key_flags = key_flags;
  1119. cmd.key_offset = keyconf->hw_key_idx;
  1120. cmd.sta_id = sta_id;
  1121. status = ADD_STA_SUCCESS;
  1122. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
  1123. &cmd, &status);
  1124. switch (status) {
  1125. case ADD_STA_SUCCESS:
  1126. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  1127. break;
  1128. default:
  1129. ret = -EIO;
  1130. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  1131. break;
  1132. }
  1133. return ret;
  1134. }
  1135. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  1136. struct ieee80211_vif *vif,
  1137. struct ieee80211_sta *sta,
  1138. struct ieee80211_key_conf *keyconf,
  1139. bool have_key_offset)
  1140. {
  1141. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1142. u8 sta_id;
  1143. int ret;
  1144. lockdep_assert_held(&mvm->mutex);
  1145. /* Get the station id from the mvm local station table */
  1146. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1147. if (sta_id == IWL_MVM_STATION_COUNT) {
  1148. IWL_ERR(mvm, "Failed to find station id\n");
  1149. return -EINVAL;
  1150. }
  1151. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  1152. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  1153. goto end;
  1154. }
  1155. /*
  1156. * It is possible that the 'sta' parameter is NULL, and thus
  1157. * there is a need to retrieve the sta from the local station table.
  1158. */
  1159. if (!sta) {
  1160. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1161. lockdep_is_held(&mvm->mutex));
  1162. if (IS_ERR_OR_NULL(sta)) {
  1163. IWL_ERR(mvm, "Invalid station id\n");
  1164. return -EINVAL;
  1165. }
  1166. }
  1167. if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
  1168. return -EINVAL;
  1169. if (!have_key_offset) {
  1170. /*
  1171. * The D3 firmware hardcodes the PTK offset to 0, so we have to
  1172. * configure it there. As a result, this workaround exists to
  1173. * let the caller set the key offset (hw_key_idx), see d3.c.
  1174. */
  1175. keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
  1176. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  1177. return -ENOSPC;
  1178. }
  1179. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, mcast);
  1180. if (ret) {
  1181. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1182. goto end;
  1183. }
  1184. /*
  1185. * For WEP, the same key is used for multicast and unicast. Upload it
  1186. * again, using the same key offset, and now pointing the other one
  1187. * to the same key slot (offset).
  1188. * If this fails, remove the original as well.
  1189. */
  1190. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1191. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
  1192. ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, !mcast);
  1193. if (ret) {
  1194. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1195. __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  1196. }
  1197. }
  1198. end:
  1199. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1200. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1201. sta->addr, ret);
  1202. return ret;
  1203. }
  1204. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  1205. struct ieee80211_vif *vif,
  1206. struct ieee80211_sta *sta,
  1207. struct ieee80211_key_conf *keyconf)
  1208. {
  1209. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1210. u8 sta_id;
  1211. int ret;
  1212. lockdep_assert_held(&mvm->mutex);
  1213. /* Get the station id from the mvm local station table */
  1214. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1215. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  1216. keyconf->keyidx, sta_id);
  1217. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  1218. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  1219. if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
  1220. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  1221. keyconf->hw_key_idx);
  1222. return -ENOENT;
  1223. }
  1224. if (sta_id == IWL_MVM_STATION_COUNT) {
  1225. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  1226. return 0;
  1227. }
  1228. /*
  1229. * It is possible that the 'sta' parameter is NULL, and thus
  1230. * there is a need to retrieve the sta from the local station table,
  1231. * for example when a GTK is removed (where the sta_id will then be
  1232. * the AP ID, and no station was passed by mac80211.)
  1233. */
  1234. if (!sta) {
  1235. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1236. lockdep_is_held(&mvm->mutex));
  1237. if (!sta) {
  1238. IWL_ERR(mvm, "Invalid station id\n");
  1239. return -EINVAL;
  1240. }
  1241. }
  1242. if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
  1243. return -EINVAL;
  1244. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
  1245. if (ret)
  1246. return ret;
  1247. /* delete WEP key twice to get rid of (now useless) offset */
  1248. if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1249. keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
  1250. ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
  1251. return ret;
  1252. }
  1253. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  1254. struct ieee80211_vif *vif,
  1255. struct ieee80211_key_conf *keyconf,
  1256. struct ieee80211_sta *sta, u32 iv32,
  1257. u16 *phase1key)
  1258. {
  1259. struct iwl_mvm_sta *mvm_sta;
  1260. u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1261. bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
  1262. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  1263. return;
  1264. rcu_read_lock();
  1265. if (!sta) {
  1266. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1267. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  1268. rcu_read_unlock();
  1269. return;
  1270. }
  1271. }
  1272. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1273. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
  1274. iv32, phase1key, CMD_ASYNC);
  1275. rcu_read_unlock();
  1276. }
  1277. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  1278. struct ieee80211_sta *sta)
  1279. {
  1280. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1281. struct iwl_mvm_add_sta_cmd cmd = {
  1282. .add_modify = STA_MODE_MODIFY,
  1283. .sta_id = mvmsta->sta_id,
  1284. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  1285. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1286. };
  1287. int ret;
  1288. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1289. if (ret)
  1290. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1291. }
  1292. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  1293. struct ieee80211_sta *sta,
  1294. enum ieee80211_frame_release_type reason,
  1295. u16 cnt, u16 tids, bool more_data,
  1296. bool agg)
  1297. {
  1298. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1299. struct iwl_mvm_add_sta_cmd cmd = {
  1300. .add_modify = STA_MODE_MODIFY,
  1301. .sta_id = mvmsta->sta_id,
  1302. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1303. .sleep_tx_count = cpu_to_le16(cnt),
  1304. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1305. };
  1306. int tid, ret;
  1307. unsigned long _tids = tids;
  1308. /* convert TIDs to ACs - we don't support TSPEC so that's OK
  1309. * Note that this field is reserved and unused by firmware not
  1310. * supporting GO uAPSD, so it's safe to always do this.
  1311. */
  1312. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
  1313. cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
  1314. /* If we're releasing frames from aggregation queues then check if the
  1315. * all queues combined that we're releasing frames from have
  1316. * - more frames than the service period, in which case more_data
  1317. * needs to be set
  1318. * - fewer than 'cnt' frames, in which case we need to adjust the
  1319. * firmware command (but do that unconditionally)
  1320. */
  1321. if (agg) {
  1322. int remaining = cnt;
  1323. spin_lock_bh(&mvmsta->lock);
  1324. for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
  1325. struct iwl_mvm_tid_data *tid_data;
  1326. u16 n_queued;
  1327. tid_data = &mvmsta->tid_data[tid];
  1328. if (WARN(tid_data->state != IWL_AGG_ON &&
  1329. tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
  1330. "TID %d state is %d\n",
  1331. tid, tid_data->state)) {
  1332. spin_unlock_bh(&mvmsta->lock);
  1333. ieee80211_sta_eosp(sta);
  1334. return;
  1335. }
  1336. n_queued = iwl_mvm_tid_queued(tid_data);
  1337. if (n_queued > remaining) {
  1338. more_data = true;
  1339. remaining = 0;
  1340. break;
  1341. }
  1342. remaining -= n_queued;
  1343. }
  1344. spin_unlock_bh(&mvmsta->lock);
  1345. cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
  1346. if (WARN_ON(cnt - remaining == 0)) {
  1347. ieee80211_sta_eosp(sta);
  1348. return;
  1349. }
  1350. }
  1351. /* Note: this is ignored by firmware not supporting GO uAPSD */
  1352. if (more_data)
  1353. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
  1354. if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
  1355. mvmsta->next_status_eosp = true;
  1356. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
  1357. } else {
  1358. cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
  1359. }
  1360. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1361. if (ret)
  1362. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1363. }
  1364. int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
  1365. struct iwl_rx_cmd_buffer *rxb,
  1366. struct iwl_device_cmd *cmd)
  1367. {
  1368. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1369. struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
  1370. struct ieee80211_sta *sta;
  1371. u32 sta_id = le32_to_cpu(notif->sta_id);
  1372. if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
  1373. return 0;
  1374. rcu_read_lock();
  1375. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1376. if (!IS_ERR_OR_NULL(sta))
  1377. ieee80211_sta_eosp(sta);
  1378. rcu_read_unlock();
  1379. return 0;
  1380. }
  1381. void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
  1382. struct iwl_mvm_sta *mvmsta, bool disable)
  1383. {
  1384. struct iwl_mvm_add_sta_cmd cmd = {
  1385. .add_modify = STA_MODE_MODIFY,
  1386. .sta_id = mvmsta->sta_id,
  1387. .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
  1388. .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
  1389. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1390. };
  1391. int ret;
  1392. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_DISABLE_STA_TX))
  1393. return;
  1394. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1395. if (ret)
  1396. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1397. }
  1398. void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
  1399. struct ieee80211_sta *sta,
  1400. bool disable)
  1401. {
  1402. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1403. spin_lock_bh(&mvm_sta->lock);
  1404. if (mvm_sta->disable_tx == disable) {
  1405. spin_unlock_bh(&mvm_sta->lock);
  1406. return;
  1407. }
  1408. mvm_sta->disable_tx = disable;
  1409. /*
  1410. * Tell mac80211 to start/stop queueing tx for this station,
  1411. * but don't stop queueing if there are still pending frames
  1412. * for this station.
  1413. */
  1414. if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
  1415. ieee80211_sta_block_awake(mvm->hw, sta, disable);
  1416. iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
  1417. spin_unlock_bh(&mvm_sta->lock);
  1418. }
  1419. void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
  1420. struct iwl_mvm_vif *mvmvif,
  1421. bool disable)
  1422. {
  1423. struct ieee80211_sta *sta;
  1424. struct iwl_mvm_sta *mvm_sta;
  1425. int i;
  1426. lockdep_assert_held(&mvm->mutex);
  1427. /* Block/unblock all the stations of the given mvmvif */
  1428. for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
  1429. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
  1430. lockdep_is_held(&mvm->mutex));
  1431. if (IS_ERR_OR_NULL(sta))
  1432. continue;
  1433. mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1434. if (mvm_sta->mac_id_n_color !=
  1435. FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
  1436. continue;
  1437. iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
  1438. }
  1439. }
  1440. void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1441. {
  1442. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1443. struct iwl_mvm_sta *mvmsta;
  1444. rcu_read_lock();
  1445. mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
  1446. if (!WARN_ON(!mvmsta))
  1447. iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
  1448. rcu_read_unlock();
  1449. }