sta.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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 - reset everything but the sequence number */
  293. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  294. u16 seq = mvm_sta->tid_data[i].seq_number;
  295. memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
  296. mvm_sta->tid_data[i].seq_number = seq;
  297. }
  298. ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
  299. if (ret)
  300. return ret;
  301. /* The first station added is the AP, the others are TDLS STAs */
  302. if (vif->type == NL80211_IFTYPE_STATION &&
  303. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  304. mvmvif->ap_sta_id = sta_id;
  305. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  306. return 0;
  307. }
  308. int iwl_mvm_update_sta(struct iwl_mvm *mvm,
  309. struct ieee80211_vif *vif,
  310. struct ieee80211_sta *sta)
  311. {
  312. return iwl_mvm_sta_send_to_fw(mvm, sta, true);
  313. }
  314. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  315. bool drain)
  316. {
  317. struct iwl_mvm_add_sta_cmd_v6 cmd = {};
  318. int ret;
  319. u32 status;
  320. lockdep_assert_held(&mvm->mutex);
  321. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  322. cmd.sta_id = mvmsta->sta_id;
  323. cmd.add_modify = STA_MODE_MODIFY;
  324. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  325. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  326. status = ADD_STA_SUCCESS;
  327. ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
  328. if (ret)
  329. return ret;
  330. switch (status) {
  331. case ADD_STA_SUCCESS:
  332. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  333. mvmsta->sta_id);
  334. break;
  335. default:
  336. ret = -EIO;
  337. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  338. mvmsta->sta_id);
  339. break;
  340. }
  341. return ret;
  342. }
  343. /*
  344. * Remove a station from the FW table. Before sending the command to remove
  345. * the station validate that the station is indeed known to the driver (sanity
  346. * only).
  347. */
  348. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  349. {
  350. struct ieee80211_sta *sta;
  351. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  352. .sta_id = sta_id,
  353. };
  354. int ret;
  355. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  356. lockdep_is_held(&mvm->mutex));
  357. /* Note: internal stations are marked as error values */
  358. if (!sta) {
  359. IWL_ERR(mvm, "Invalid station id\n");
  360. return -EINVAL;
  361. }
  362. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
  363. sizeof(rm_sta_cmd), &rm_sta_cmd);
  364. if (ret) {
  365. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  366. return ret;
  367. }
  368. return 0;
  369. }
  370. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  371. {
  372. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  373. u8 sta_id;
  374. /*
  375. * The mutex is needed because of the SYNC cmd, but not only: if the
  376. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  377. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  378. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  379. * that later.
  380. */
  381. mutex_lock(&mvm->mutex);
  382. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  383. int ret;
  384. struct ieee80211_sta *sta =
  385. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  386. lockdep_is_held(&mvm->mutex));
  387. /*
  388. * This station is in use or RCU-removed; the latter happens in
  389. * managed mode, where mac80211 removes the station before we
  390. * can remove it from firmware (we can only do that after the
  391. * MAC is marked unassociated), and possibly while the deauth
  392. * frame to disconnect from the AP is still queued. Then, the
  393. * station pointer is -ENOENT when the last skb is reclaimed.
  394. */
  395. if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
  396. continue;
  397. if (PTR_ERR(sta) == -EINVAL) {
  398. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  399. sta_id);
  400. continue;
  401. }
  402. if (!sta) {
  403. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  404. sta_id);
  405. continue;
  406. }
  407. WARN_ON(PTR_ERR(sta) != -EBUSY);
  408. /* This station was removed and we waited until it got drained,
  409. * we can now proceed and remove it.
  410. */
  411. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  412. if (ret) {
  413. IWL_ERR(mvm,
  414. "Couldn't remove sta %d after it was drained\n",
  415. sta_id);
  416. continue;
  417. }
  418. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
  419. clear_bit(sta_id, mvm->sta_drained);
  420. }
  421. mutex_unlock(&mvm->mutex);
  422. }
  423. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  424. struct ieee80211_vif *vif,
  425. struct ieee80211_sta *sta)
  426. {
  427. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  428. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  429. int ret;
  430. lockdep_assert_held(&mvm->mutex);
  431. if (vif->type == NL80211_IFTYPE_STATION &&
  432. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  433. /* flush its queues here since we are freeing mvm_sta */
  434. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
  435. /*
  436. * Put a non-NULL since the fw station isn't removed.
  437. * It will be removed after the MAC will be set as
  438. * unassoc.
  439. */
  440. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  441. ERR_PTR(-EINVAL));
  442. /* if we are associated - we can't remove the AP STA now */
  443. if (vif->bss_conf.assoc)
  444. return ret;
  445. /* unassoc - go ahead - remove the AP STA now */
  446. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  447. }
  448. /*
  449. * Make sure that the tx response code sees the station as -EBUSY and
  450. * calls the drain worker.
  451. */
  452. spin_lock_bh(&mvm_sta->lock);
  453. /*
  454. * There are frames pending on the AC queues for this station.
  455. * We need to wait until all the frames are drained...
  456. */
  457. if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
  458. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  459. ERR_PTR(-EBUSY));
  460. spin_unlock_bh(&mvm_sta->lock);
  461. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  462. } else {
  463. spin_unlock_bh(&mvm_sta->lock);
  464. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  465. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  466. }
  467. return ret;
  468. }
  469. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  470. struct ieee80211_vif *vif,
  471. u8 sta_id)
  472. {
  473. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  474. lockdep_assert_held(&mvm->mutex);
  475. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
  476. return ret;
  477. }
  478. int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
  479. u32 qmask)
  480. {
  481. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  482. sta->sta_id = iwl_mvm_find_free_sta_id(mvm);
  483. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  484. return -ENOSPC;
  485. }
  486. sta->tfd_queue_msk = qmask;
  487. /* put a non-NULL value so iterating over the stations won't stop */
  488. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  489. return 0;
  490. }
  491. void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
  492. {
  493. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  494. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  495. sta->sta_id = IWL_MVM_STATION_COUNT;
  496. }
  497. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  498. struct iwl_mvm_int_sta *sta,
  499. const u8 *addr,
  500. u16 mac_id, u16 color)
  501. {
  502. struct iwl_mvm_add_sta_cmd_v6 cmd;
  503. int ret;
  504. u32 status;
  505. lockdep_assert_held(&mvm->mutex);
  506. memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd_v6));
  507. cmd.sta_id = sta->sta_id;
  508. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  509. color));
  510. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  511. if (addr)
  512. memcpy(cmd.addr, addr, ETH_ALEN);
  513. ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
  514. if (ret)
  515. return ret;
  516. switch (status) {
  517. case ADD_STA_SUCCESS:
  518. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  519. return 0;
  520. default:
  521. ret = -EIO;
  522. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  523. status);
  524. break;
  525. }
  526. return ret;
  527. }
  528. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  529. {
  530. int ret;
  531. lockdep_assert_held(&mvm->mutex);
  532. /* Add the aux station, but without any queues */
  533. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0);
  534. if (ret)
  535. return ret;
  536. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  537. MAC_INDEX_AUX, 0);
  538. if (ret)
  539. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  540. return ret;
  541. }
  542. /*
  543. * Send the add station command for the vif's broadcast station.
  544. * Assumes that the station was already allocated.
  545. *
  546. * @mvm: the mvm component
  547. * @vif: the interface to which the broadcast station is added
  548. * @bsta: the broadcast station to add.
  549. */
  550. int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  551. struct iwl_mvm_int_sta *bsta)
  552. {
  553. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  554. static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  555. static const u8 *baddr = _baddr;
  556. lockdep_assert_held(&mvm->mutex);
  557. if (vif->type == NL80211_IFTYPE_ADHOC)
  558. baddr = vif->bss_conf.bssid;
  559. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  560. return -ENOSPC;
  561. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  562. mvmvif->id, mvmvif->color);
  563. }
  564. /* Send the FW a request to remove the station from it's internal data
  565. * structures, but DO NOT remove the entry from the local data structures. */
  566. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
  567. struct iwl_mvm_int_sta *bsta)
  568. {
  569. int ret;
  570. lockdep_assert_held(&mvm->mutex);
  571. ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
  572. if (ret)
  573. IWL_WARN(mvm, "Failed sending remove station\n");
  574. return ret;
  575. }
  576. /* Allocate a new station entry for the broadcast station to the given vif,
  577. * and send it to the FW.
  578. * Note that each P2P mac should have its own broadcast station.
  579. *
  580. * @mvm: the mvm component
  581. * @vif: the interface to which the broadcast station is added
  582. * @bsta: the broadcast station to add. */
  583. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  584. struct iwl_mvm_int_sta *bsta)
  585. {
  586. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  587. static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  588. u32 qmask;
  589. int ret;
  590. lockdep_assert_held(&mvm->mutex);
  591. qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
  592. ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask);
  593. if (ret)
  594. return ret;
  595. ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  596. mvmvif->id, mvmvif->color);
  597. if (ret)
  598. iwl_mvm_dealloc_int_sta(mvm, bsta);
  599. return ret;
  600. }
  601. /*
  602. * Send the FW a request to remove the station from it's internal data
  603. * structures, and in addition remove it from the local data structure.
  604. */
  605. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
  606. {
  607. int ret;
  608. lockdep_assert_held(&mvm->mutex);
  609. ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
  610. if (ret)
  611. return ret;
  612. iwl_mvm_dealloc_int_sta(mvm, bsta);
  613. return ret;
  614. }
  615. #define IWL_MAX_RX_BA_SESSIONS 16
  616. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  617. int tid, u16 ssn, bool start)
  618. {
  619. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  620. struct iwl_mvm_add_sta_cmd_v6 cmd = {};
  621. int ret;
  622. u32 status;
  623. lockdep_assert_held(&mvm->mutex);
  624. if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
  625. IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
  626. return -ENOSPC;
  627. }
  628. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  629. cmd.sta_id = mvm_sta->sta_id;
  630. cmd.add_modify = STA_MODE_MODIFY;
  631. if (start) {
  632. cmd.add_immediate_ba_tid = (u8) tid;
  633. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  634. } else {
  635. cmd.remove_immediate_ba_tid = (u8) tid;
  636. }
  637. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  638. STA_MODIFY_REMOVE_BA_TID;
  639. status = ADD_STA_SUCCESS;
  640. ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
  641. if (ret)
  642. return ret;
  643. switch (status) {
  644. case ADD_STA_SUCCESS:
  645. IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
  646. start ? "start" : "stopp");
  647. break;
  648. case ADD_STA_IMMEDIATE_BA_FAILURE:
  649. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  650. ret = -ENOSPC;
  651. break;
  652. default:
  653. ret = -EIO;
  654. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  655. start ? "start" : "stopp", status);
  656. break;
  657. }
  658. if (!ret) {
  659. if (start)
  660. mvm->rx_ba_sessions++;
  661. else if (mvm->rx_ba_sessions > 0)
  662. /* check that restart flow didn't zero the counter */
  663. mvm->rx_ba_sessions--;
  664. }
  665. return ret;
  666. }
  667. static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  668. int tid, u8 queue, bool start)
  669. {
  670. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  671. struct iwl_mvm_add_sta_cmd_v6 cmd = {};
  672. int ret;
  673. u32 status;
  674. lockdep_assert_held(&mvm->mutex);
  675. if (start) {
  676. mvm_sta->tfd_queue_msk |= BIT(queue);
  677. mvm_sta->tid_disable_agg &= ~BIT(tid);
  678. } else {
  679. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  680. mvm_sta->tid_disable_agg |= BIT(tid);
  681. }
  682. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  683. cmd.sta_id = mvm_sta->sta_id;
  684. cmd.add_modify = STA_MODE_MODIFY;
  685. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  686. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  687. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  688. status = ADD_STA_SUCCESS;
  689. ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
  690. if (ret)
  691. return ret;
  692. switch (status) {
  693. case ADD_STA_SUCCESS:
  694. break;
  695. default:
  696. ret = -EIO;
  697. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  698. start ? "start" : "stopp", status);
  699. break;
  700. }
  701. return ret;
  702. }
  703. static const u8 tid_to_ac[] = {
  704. IEEE80211_AC_BE,
  705. IEEE80211_AC_BK,
  706. IEEE80211_AC_BK,
  707. IEEE80211_AC_BE,
  708. IEEE80211_AC_VI,
  709. IEEE80211_AC_VI,
  710. IEEE80211_AC_VO,
  711. IEEE80211_AC_VO,
  712. };
  713. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  714. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  715. {
  716. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  717. struct iwl_mvm_tid_data *tid_data;
  718. int txq_id;
  719. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  720. return -EINVAL;
  721. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  722. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  723. mvmsta->tid_data[tid].state);
  724. return -ENXIO;
  725. }
  726. lockdep_assert_held(&mvm->mutex);
  727. for (txq_id = mvm->first_agg_queue;
  728. txq_id <= mvm->last_agg_queue; txq_id++)
  729. if (mvm->queue_to_mac80211[txq_id] ==
  730. IWL_INVALID_MAC80211_QUEUE)
  731. break;
  732. if (txq_id > mvm->last_agg_queue) {
  733. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  734. return -EIO;
  735. }
  736. /* the new tx queue is still connected to the same mac80211 queue */
  737. mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
  738. spin_lock_bh(&mvmsta->lock);
  739. tid_data = &mvmsta->tid_data[tid];
  740. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  741. tid_data->txq_id = txq_id;
  742. *ssn = tid_data->ssn;
  743. IWL_DEBUG_TX_QUEUES(mvm,
  744. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  745. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  746. tid_data->next_reclaimed);
  747. if (tid_data->ssn == tid_data->next_reclaimed) {
  748. tid_data->state = IWL_AGG_STARTING;
  749. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  750. } else {
  751. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  752. }
  753. spin_unlock_bh(&mvmsta->lock);
  754. return 0;
  755. }
  756. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  757. struct ieee80211_sta *sta, u16 tid, u8 buf_size)
  758. {
  759. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  760. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  761. int queue, fifo, ret;
  762. u16 ssn;
  763. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  764. spin_lock_bh(&mvmsta->lock);
  765. ssn = tid_data->ssn;
  766. queue = tid_data->txq_id;
  767. tid_data->state = IWL_AGG_ON;
  768. tid_data->ssn = 0xffff;
  769. spin_unlock_bh(&mvmsta->lock);
  770. fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
  771. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  772. if (ret)
  773. return -EIO;
  774. iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
  775. buf_size, ssn);
  776. /*
  777. * Even though in theory the peer could have different
  778. * aggregation reorder buffer sizes for different sessions,
  779. * our ucode doesn't allow for that and has a global limit
  780. * for each station. Therefore, use the minimum of all the
  781. * aggregation sessions and our default value.
  782. */
  783. mvmsta->max_agg_bufsize =
  784. min(mvmsta->max_agg_bufsize, buf_size);
  785. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  786. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  787. sta->addr, tid);
  788. if (mvm->cfg->ht_params->use_rts_for_aggregation) {
  789. /*
  790. * switch to RTS/CTS if it is the prefer protection
  791. * method for HT traffic
  792. * this function also sends the LQ command
  793. */
  794. return iwl_mvm_tx_protection(mvm, mvmsta, true);
  795. /*
  796. * TODO: remove the TLC_RTS flag when we tear down the last
  797. * AGG session (agg_tids_count in DVM)
  798. */
  799. }
  800. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
  801. }
  802. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  803. struct ieee80211_sta *sta, u16 tid)
  804. {
  805. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  806. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  807. u16 txq_id;
  808. int err;
  809. /*
  810. * If mac80211 is cleaning its state, then say that we finished since
  811. * our state has been cleared anyway.
  812. */
  813. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  814. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  815. return 0;
  816. }
  817. spin_lock_bh(&mvmsta->lock);
  818. txq_id = tid_data->txq_id;
  819. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  820. mvmsta->sta_id, tid, txq_id, tid_data->state);
  821. switch (tid_data->state) {
  822. case IWL_AGG_ON:
  823. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  824. IWL_DEBUG_TX_QUEUES(mvm,
  825. "ssn = %d, next_recl = %d\n",
  826. tid_data->ssn, tid_data->next_reclaimed);
  827. /* There are still packets for this RA / TID in the HW */
  828. if (tid_data->ssn != tid_data->next_reclaimed) {
  829. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  830. err = 0;
  831. break;
  832. }
  833. tid_data->ssn = 0xffff;
  834. iwl_trans_txq_disable(mvm->trans, txq_id);
  835. /* fall through */
  836. case IWL_AGG_STARTING:
  837. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  838. /*
  839. * The agg session has been stopped before it was set up. This
  840. * can happen when the AddBA timer times out for example.
  841. */
  842. /* No barriers since we are under mutex */
  843. lockdep_assert_held(&mvm->mutex);
  844. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  845. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  846. tid_data->state = IWL_AGG_OFF;
  847. err = 0;
  848. break;
  849. default:
  850. IWL_ERR(mvm,
  851. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  852. mvmsta->sta_id, tid, tid_data->state);
  853. IWL_ERR(mvm,
  854. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  855. err = -EINVAL;
  856. }
  857. spin_unlock_bh(&mvmsta->lock);
  858. return err;
  859. }
  860. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  861. struct ieee80211_sta *sta, u16 tid)
  862. {
  863. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  864. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  865. u16 txq_id;
  866. enum iwl_mvm_agg_state old_state;
  867. /*
  868. * First set the agg state to OFF to avoid calling
  869. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  870. */
  871. spin_lock_bh(&mvmsta->lock);
  872. txq_id = tid_data->txq_id;
  873. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  874. mvmsta->sta_id, tid, txq_id, tid_data->state);
  875. old_state = tid_data->state;
  876. tid_data->state = IWL_AGG_OFF;
  877. spin_unlock_bh(&mvmsta->lock);
  878. if (old_state >= IWL_AGG_ON) {
  879. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
  880. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  881. iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
  882. }
  883. mvm->queue_to_mac80211[tid_data->txq_id] =
  884. IWL_INVALID_MAC80211_QUEUE;
  885. return 0;
  886. }
  887. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  888. {
  889. int i;
  890. lockdep_assert_held(&mvm->mutex);
  891. i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
  892. if (i == STA_KEY_MAX_NUM)
  893. return STA_KEY_IDX_INVALID;
  894. __set_bit(i, mvm->fw_key_table);
  895. return i;
  896. }
  897. static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
  898. struct ieee80211_sta *sta)
  899. {
  900. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  901. if (sta) {
  902. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  903. return mvm_sta->sta_id;
  904. }
  905. /*
  906. * The device expects GTKs for station interfaces to be
  907. * installed as GTKs for the AP station. If we have no
  908. * station ID, then use AP's station ID.
  909. */
  910. if (vif->type == NL80211_IFTYPE_STATION &&
  911. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
  912. return mvmvif->ap_sta_id;
  913. return IWL_MVM_STATION_COUNT;
  914. }
  915. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  916. struct iwl_mvm_sta *mvm_sta,
  917. struct ieee80211_key_conf *keyconf,
  918. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  919. u32 cmd_flags)
  920. {
  921. __le16 key_flags;
  922. struct iwl_mvm_add_sta_key_cmd cmd = {};
  923. int ret, status;
  924. u16 keyidx;
  925. int i;
  926. u32 mac_id_n_color = mvm_sta->mac_id_n_color;
  927. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  928. STA_KEY_FLG_KEYID_MSK;
  929. key_flags = cpu_to_le16(keyidx);
  930. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  931. switch (keyconf->cipher) {
  932. case WLAN_CIPHER_SUITE_TKIP:
  933. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  934. cmd.tkip_rx_tsc_byte2 = tkip_iv32;
  935. for (i = 0; i < 5; i++)
  936. cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  937. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  938. break;
  939. case WLAN_CIPHER_SUITE_CCMP:
  940. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  941. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  942. break;
  943. default:
  944. WARN_ON(1);
  945. return -EINVAL;
  946. }
  947. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  948. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  949. cmd.key_offset = keyconf->hw_key_idx;
  950. cmd.key_flags = key_flags;
  951. cmd.sta_id = sta_id;
  952. status = ADD_STA_SUCCESS;
  953. if (cmd_flags == CMD_SYNC)
  954. ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
  955. mac_id_n_color,
  956. &status);
  957. else
  958. ret = iwl_mvm_send_add_sta_key_cmd(mvm, CMD_ASYNC, &cmd,
  959. mac_id_n_color);
  960. switch (status) {
  961. case ADD_STA_SUCCESS:
  962. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  963. break;
  964. default:
  965. ret = -EIO;
  966. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  967. break;
  968. }
  969. return ret;
  970. }
  971. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  972. struct ieee80211_key_conf *keyconf,
  973. u8 sta_id, bool remove_key)
  974. {
  975. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  976. /* verify the key details match the required command's expectations */
  977. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  978. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  979. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  980. return -EINVAL;
  981. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  982. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  983. if (remove_key) {
  984. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  985. } else {
  986. struct ieee80211_key_seq seq;
  987. const u8 *pn;
  988. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  989. ieee80211_aes_cmac_calculate_k1_k2(keyconf,
  990. igtk_cmd.K1, igtk_cmd.K2);
  991. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  992. pn = seq.aes_cmac.pn;
  993. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  994. ((u64) pn[4] << 8) |
  995. ((u64) pn[3] << 16) |
  996. ((u64) pn[2] << 24) |
  997. ((u64) pn[1] << 32) |
  998. ((u64) pn[0] << 40));
  999. }
  1000. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  1001. remove_key ? "removing" : "installing",
  1002. igtk_cmd.sta_id);
  1003. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
  1004. sizeof(igtk_cmd), &igtk_cmd);
  1005. }
  1006. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  1007. struct ieee80211_vif *vif,
  1008. struct ieee80211_sta *sta)
  1009. {
  1010. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  1011. if (sta)
  1012. return sta->addr;
  1013. if (vif->type == NL80211_IFTYPE_STATION &&
  1014. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  1015. u8 sta_id = mvmvif->ap_sta_id;
  1016. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1017. lockdep_is_held(&mvm->mutex));
  1018. return sta->addr;
  1019. }
  1020. return NULL;
  1021. }
  1022. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  1023. struct ieee80211_vif *vif,
  1024. struct ieee80211_sta *sta,
  1025. struct ieee80211_key_conf *keyconf,
  1026. bool have_key_offset)
  1027. {
  1028. struct iwl_mvm_sta *mvm_sta;
  1029. int ret;
  1030. u8 *addr, sta_id;
  1031. struct ieee80211_key_seq seq;
  1032. u16 p1k[5];
  1033. lockdep_assert_held(&mvm->mutex);
  1034. /* Get the station id from the mvm local station table */
  1035. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1036. if (sta_id == IWL_MVM_STATION_COUNT) {
  1037. IWL_ERR(mvm, "Failed to find station id\n");
  1038. return -EINVAL;
  1039. }
  1040. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  1041. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  1042. goto end;
  1043. }
  1044. /*
  1045. * It is possible that the 'sta' parameter is NULL, and thus
  1046. * there is a need to retrieve the sta from the local station table.
  1047. */
  1048. if (!sta) {
  1049. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1050. lockdep_is_held(&mvm->mutex));
  1051. if (IS_ERR_OR_NULL(sta)) {
  1052. IWL_ERR(mvm, "Invalid station id\n");
  1053. return -EINVAL;
  1054. }
  1055. }
  1056. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1057. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1058. return -EINVAL;
  1059. if (!have_key_offset) {
  1060. /*
  1061. * The D3 firmware hardcodes the PTK offset to 0, so we have to
  1062. * configure it there. As a result, this workaround exists to
  1063. * let the caller set the key offset (hw_key_idx), see d3.c.
  1064. */
  1065. keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
  1066. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  1067. return -ENOSPC;
  1068. }
  1069. switch (keyconf->cipher) {
  1070. case WLAN_CIPHER_SUITE_TKIP:
  1071. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  1072. /* get phase 1 key from mac80211 */
  1073. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1074. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1075. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1076. seq.tkip.iv32, p1k, CMD_SYNC);
  1077. break;
  1078. case WLAN_CIPHER_SUITE_CCMP:
  1079. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1080. 0, NULL, CMD_SYNC);
  1081. break;
  1082. default:
  1083. IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
  1084. ret = -EINVAL;
  1085. }
  1086. if (ret)
  1087. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1088. end:
  1089. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1090. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1091. sta->addr, ret);
  1092. return ret;
  1093. }
  1094. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  1095. struct ieee80211_vif *vif,
  1096. struct ieee80211_sta *sta,
  1097. struct ieee80211_key_conf *keyconf)
  1098. {
  1099. struct iwl_mvm_sta *mvm_sta;
  1100. struct iwl_mvm_add_sta_key_cmd cmd = {};
  1101. __le16 key_flags;
  1102. int ret, status;
  1103. u8 sta_id;
  1104. lockdep_assert_held(&mvm->mutex);
  1105. /* Get the station id from the mvm local station table */
  1106. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1107. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  1108. keyconf->keyidx, sta_id);
  1109. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  1110. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  1111. ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1112. if (!ret) {
  1113. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  1114. keyconf->hw_key_idx);
  1115. return -ENOENT;
  1116. }
  1117. if (sta_id == IWL_MVM_STATION_COUNT) {
  1118. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  1119. return 0;
  1120. }
  1121. /*
  1122. * It is possible that the 'sta' parameter is NULL, and thus
  1123. * there is a need to retrieve the sta from the local station table,
  1124. * for example when a GTK is removed (where the sta_id will then be
  1125. * the AP ID, and no station was passed by mac80211.)
  1126. */
  1127. if (!sta) {
  1128. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1129. lockdep_is_held(&mvm->mutex));
  1130. if (!sta) {
  1131. IWL_ERR(mvm, "Invalid station id\n");
  1132. return -EINVAL;
  1133. }
  1134. }
  1135. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1136. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1137. return -EINVAL;
  1138. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1139. STA_KEY_FLG_KEYID_MSK);
  1140. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  1141. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  1142. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1143. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1144. cmd.key_flags = key_flags;
  1145. cmd.key_offset = keyconf->hw_key_idx;
  1146. cmd.sta_id = sta_id;
  1147. status = ADD_STA_SUCCESS;
  1148. ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
  1149. mvm_sta->mac_id_n_color,
  1150. &status);
  1151. switch (status) {
  1152. case ADD_STA_SUCCESS:
  1153. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  1154. break;
  1155. default:
  1156. ret = -EIO;
  1157. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  1158. break;
  1159. }
  1160. return ret;
  1161. }
  1162. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  1163. struct ieee80211_vif *vif,
  1164. struct ieee80211_key_conf *keyconf,
  1165. struct ieee80211_sta *sta, u32 iv32,
  1166. u16 *phase1key)
  1167. {
  1168. struct iwl_mvm_sta *mvm_sta;
  1169. u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1170. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  1171. return;
  1172. rcu_read_lock();
  1173. if (!sta) {
  1174. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1175. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  1176. rcu_read_unlock();
  1177. return;
  1178. }
  1179. }
  1180. mvm_sta = (void *)sta->drv_priv;
  1181. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1182. iv32, phase1key, CMD_ASYNC);
  1183. rcu_read_unlock();
  1184. }
  1185. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  1186. struct ieee80211_sta *sta)
  1187. {
  1188. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1189. struct iwl_mvm_add_sta_cmd_v6 cmd = {
  1190. .add_modify = STA_MODE_MODIFY,
  1191. .sta_id = mvmsta->sta_id,
  1192. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  1193. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1194. };
  1195. int ret;
  1196. ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
  1197. if (ret)
  1198. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1199. }
  1200. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  1201. struct ieee80211_sta *sta,
  1202. enum ieee80211_frame_release_type reason,
  1203. u16 cnt)
  1204. {
  1205. u16 sleep_state_flags =
  1206. (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
  1207. STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
  1208. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1209. struct iwl_mvm_add_sta_cmd_v6 cmd = {
  1210. .add_modify = STA_MODE_MODIFY,
  1211. .sta_id = mvmsta->sta_id,
  1212. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1213. .sleep_tx_count = cpu_to_le16(cnt),
  1214. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1215. /*
  1216. * Same modify mask for sleep_tx_count and sleep_state_flags so
  1217. * we must set the sleep_state_flags too.
  1218. */
  1219. .sleep_state_flags = cpu_to_le16(sleep_state_flags),
  1220. };
  1221. int ret;
  1222. /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
  1223. ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
  1224. if (ret)
  1225. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1226. }