sta.c 40 KB

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