sta.c 50 KB

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