sta.c 48 KB

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