sta.c 48 KB

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