sta.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 LICENSE.GPL.
  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. static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm)
  67. {
  68. int sta_id;
  69. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  70. lockdep_assert_held(&mvm->mutex);
  71. /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
  72. for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++)
  73. if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  74. lockdep_is_held(&mvm->mutex)))
  75. return sta_id;
  76. return IWL_MVM_STATION_COUNT;
  77. }
  78. /* add a NEW station to fw */
  79. int iwl_mvm_sta_add_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta)
  80. {
  81. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  82. struct iwl_mvm_add_sta_cmd add_sta_cmd;
  83. int ret;
  84. u32 status;
  85. u32 agg_size = 0, mpdu_dens = 0;
  86. memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
  87. add_sta_cmd.sta_id = mvm_sta->sta_id;
  88. add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  89. add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  90. memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
  91. /* STA_FLG_FAT_EN_MSK ? */
  92. /* STA_FLG_MIMO_EN_MSK ? */
  93. if (sta->ht_cap.ht_supported) {
  94. add_sta_cmd.station_flags_msk |=
  95. cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
  96. STA_FLG_AGG_MPDU_DENS_MSK);
  97. mpdu_dens = sta->ht_cap.ampdu_density;
  98. }
  99. if (sta->vht_cap.vht_supported) {
  100. agg_size = sta->vht_cap.cap &
  101. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  102. agg_size >>=
  103. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  104. } else if (sta->ht_cap.ht_supported) {
  105. agg_size = sta->ht_cap.ampdu_factor;
  106. }
  107. add_sta_cmd.station_flags |=
  108. cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
  109. add_sta_cmd.station_flags |=
  110. cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
  111. status = ADD_STA_SUCCESS;
  112. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
  113. &add_sta_cmd, &status);
  114. if (ret)
  115. return ret;
  116. switch (status) {
  117. case ADD_STA_SUCCESS:
  118. IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
  119. break;
  120. default:
  121. ret = -EIO;
  122. IWL_ERR(mvm, "ADD_STA failed\n");
  123. break;
  124. }
  125. return ret;
  126. }
  127. int iwl_mvm_add_sta(struct iwl_mvm *mvm,
  128. struct ieee80211_vif *vif,
  129. struct ieee80211_sta *sta)
  130. {
  131. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  132. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  133. int i, ret, sta_id;
  134. lockdep_assert_held(&mvm->mutex);
  135. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
  136. sta_id = iwl_mvm_find_free_sta_id(mvm);
  137. else
  138. sta_id = mvm_sta->sta_id;
  139. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  140. return -ENOSPC;
  141. spin_lock_init(&mvm_sta->lock);
  142. mvm_sta->sta_id = sta_id;
  143. mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
  144. mvmvif->color);
  145. mvm_sta->vif = vif;
  146. mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  147. /* HW restart, don't assume the memory has been zeroed */
  148. atomic_set(&mvm_sta->pending_frames, 0);
  149. mvm_sta->tid_disable_agg = 0;
  150. mvm_sta->tfd_queue_msk = 0;
  151. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  152. if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
  153. mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
  154. if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
  155. mvm_sta->tfd_queue_msk |= BIT(vif->cab_queue);
  156. /* for HW restart - need to reset the seq_number etc... */
  157. memset(mvm_sta->tid_data, 0, sizeof(mvm_sta->tid_data));
  158. ret = iwl_mvm_sta_add_to_fw(mvm, sta);
  159. if (ret)
  160. return ret;
  161. /* The first station added is the AP, the others are TDLS STAs */
  162. if (vif->type == NL80211_IFTYPE_STATION &&
  163. mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  164. mvmvif->ap_sta_id = sta_id;
  165. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
  166. return 0;
  167. }
  168. int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  169. bool drain)
  170. {
  171. struct iwl_mvm_add_sta_cmd cmd = {};
  172. int ret;
  173. u32 status;
  174. lockdep_assert_held(&mvm->mutex);
  175. cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
  176. cmd.sta_id = mvmsta->sta_id;
  177. cmd.add_modify = STA_MODE_MODIFY;
  178. cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
  179. cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
  180. status = ADD_STA_SUCCESS;
  181. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  182. &cmd, &status);
  183. if (ret)
  184. return ret;
  185. switch (status) {
  186. case ADD_STA_SUCCESS:
  187. IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
  188. mvmsta->sta_id);
  189. break;
  190. default:
  191. ret = -EIO;
  192. IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
  193. mvmsta->sta_id);
  194. break;
  195. }
  196. return ret;
  197. }
  198. /*
  199. * Remove a station from the FW table. Before sending the command to remove
  200. * the station validate that the station is indeed known to the driver (sanity
  201. * only).
  202. */
  203. static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
  204. {
  205. struct ieee80211_sta *sta;
  206. struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
  207. .sta_id = sta_id,
  208. };
  209. int ret;
  210. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  211. lockdep_is_held(&mvm->mutex));
  212. /* Note: internal stations are marked as error values */
  213. if (!sta) {
  214. IWL_ERR(mvm, "Invalid station id\n");
  215. return -EINVAL;
  216. }
  217. ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
  218. sizeof(rm_sta_cmd), &rm_sta_cmd);
  219. if (ret) {
  220. IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
  221. return ret;
  222. }
  223. return 0;
  224. }
  225. void iwl_mvm_sta_drained_wk(struct work_struct *wk)
  226. {
  227. struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
  228. u8 sta_id;
  229. /*
  230. * The mutex is needed because of the SYNC cmd, but not only: if the
  231. * work would run concurrently with iwl_mvm_rm_sta, it would run before
  232. * iwl_mvm_rm_sta sets the station as busy, and exit. Then
  233. * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
  234. * that later.
  235. */
  236. mutex_lock(&mvm->mutex);
  237. for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
  238. int ret;
  239. struct ieee80211_sta *sta =
  240. rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  241. lockdep_is_held(&mvm->mutex));
  242. /* This station is in use */
  243. if (!IS_ERR(sta))
  244. continue;
  245. if (PTR_ERR(sta) == -EINVAL) {
  246. IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
  247. sta_id);
  248. continue;
  249. }
  250. if (!sta) {
  251. IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
  252. sta_id);
  253. continue;
  254. }
  255. WARN_ON(PTR_ERR(sta) != -EBUSY);
  256. /* This station was removed and we waited until it got drained,
  257. * we can now proceed and remove it.
  258. */
  259. ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  260. if (ret) {
  261. IWL_ERR(mvm,
  262. "Couldn't remove sta %d after it was drained\n",
  263. sta_id);
  264. continue;
  265. }
  266. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
  267. clear_bit(sta_id, mvm->sta_drained);
  268. }
  269. mutex_unlock(&mvm->mutex);
  270. }
  271. int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
  272. struct ieee80211_vif *vif,
  273. struct ieee80211_sta *sta)
  274. {
  275. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  276. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  277. int ret;
  278. lockdep_assert_held(&mvm->mutex);
  279. if (vif->type == NL80211_IFTYPE_STATION &&
  280. mvmvif->ap_sta_id == mvm_sta->sta_id) {
  281. /*
  282. * Put a non-NULL since the fw station isn't removed.
  283. * It will be removed after the MAC will be set as
  284. * unassoc.
  285. */
  286. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  287. ERR_PTR(-EINVAL));
  288. /* flush its queues here since we are freeing mvm_sta */
  289. ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
  290. /* if we are associated - we can't remove the AP STA now */
  291. if (vif->bss_conf.assoc)
  292. return ret;
  293. /* unassoc - go ahead - remove the AP STA now */
  294. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  295. }
  296. /*
  297. * There are frames pending on the AC queues for this station.
  298. * We need to wait until all the frames are drained...
  299. */
  300. if (atomic_read(&mvm_sta->pending_frames)) {
  301. ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
  302. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
  303. ERR_PTR(-EBUSY));
  304. } else {
  305. ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
  306. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
  307. }
  308. return ret;
  309. }
  310. int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
  311. struct ieee80211_vif *vif,
  312. u8 sta_id)
  313. {
  314. int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
  315. lockdep_assert_held(&mvm->mutex);
  316. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
  317. return ret;
  318. }
  319. int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
  320. u32 qmask)
  321. {
  322. if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  323. sta->sta_id = iwl_mvm_find_free_sta_id(mvm);
  324. if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
  325. return -ENOSPC;
  326. }
  327. sta->tfd_queue_msk = qmask;
  328. /* put a non-NULL value so iterating over the stations won't stop */
  329. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
  330. return 0;
  331. }
  332. void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
  333. {
  334. rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
  335. memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
  336. sta->sta_id = IWL_MVM_STATION_COUNT;
  337. }
  338. static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
  339. struct iwl_mvm_int_sta *sta,
  340. const u8 *addr,
  341. u16 mac_id, u16 color)
  342. {
  343. struct iwl_mvm_add_sta_cmd cmd;
  344. int ret;
  345. u32 status;
  346. lockdep_assert_held(&mvm->mutex);
  347. memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd));
  348. cmd.sta_id = sta->sta_id;
  349. cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
  350. color));
  351. cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
  352. if (addr)
  353. memcpy(cmd.addr, addr, ETH_ALEN);
  354. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  355. &cmd, &status);
  356. if (ret)
  357. return ret;
  358. switch (status) {
  359. case ADD_STA_SUCCESS:
  360. IWL_DEBUG_INFO(mvm, "Internal station added.\n");
  361. return 0;
  362. default:
  363. ret = -EIO;
  364. IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
  365. status);
  366. break;
  367. }
  368. return ret;
  369. }
  370. int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
  371. {
  372. int ret;
  373. lockdep_assert_held(&mvm->mutex);
  374. /* Add the aux station, but without any queues */
  375. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0);
  376. if (ret)
  377. return ret;
  378. ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
  379. MAC_INDEX_AUX, 0);
  380. if (ret)
  381. iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
  382. return ret;
  383. }
  384. /*
  385. * Send the add station command for the vif's broadcast station.
  386. * Assumes that the station was already allocated.
  387. *
  388. * @mvm: the mvm component
  389. * @vif: the interface to which the broadcast station is added
  390. * @bsta: the broadcast station to add.
  391. */
  392. int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  393. struct iwl_mvm_int_sta *bsta)
  394. {
  395. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  396. static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  397. lockdep_assert_held(&mvm->mutex);
  398. if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
  399. return -ENOSPC;
  400. return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  401. mvmvif->id, mvmvif->color);
  402. }
  403. /* Send the FW a request to remove the station from it's internal data
  404. * structures, but DO NOT remove the entry from the local data structures. */
  405. int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
  406. struct iwl_mvm_int_sta *bsta)
  407. {
  408. int ret;
  409. lockdep_assert_held(&mvm->mutex);
  410. ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
  411. if (ret)
  412. IWL_WARN(mvm, "Failed sending remove station\n");
  413. return ret;
  414. }
  415. /* Allocate a new station entry for the broadcast station to the given vif,
  416. * and send it to the FW.
  417. * Note that each P2P mac should have its own broadcast station.
  418. *
  419. * @mvm: the mvm component
  420. * @vif: the interface to which the broadcast station is added
  421. * @bsta: the broadcast station to add. */
  422. int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  423. struct iwl_mvm_int_sta *bsta)
  424. {
  425. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  426. static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  427. u32 qmask;
  428. int ret;
  429. lockdep_assert_held(&mvm->mutex);
  430. qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
  431. ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask);
  432. if (ret)
  433. return ret;
  434. ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
  435. mvmvif->id, mvmvif->color);
  436. if (ret)
  437. iwl_mvm_dealloc_int_sta(mvm, bsta);
  438. return ret;
  439. }
  440. /*
  441. * Send the FW a request to remove the station from it's internal data
  442. * structures, and in addition remove it from the local data structure.
  443. */
  444. int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
  445. {
  446. int ret;
  447. lockdep_assert_held(&mvm->mutex);
  448. ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
  449. if (ret)
  450. return ret;
  451. iwl_mvm_dealloc_int_sta(mvm, bsta);
  452. return ret;
  453. }
  454. int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  455. int tid, u16 ssn, bool start)
  456. {
  457. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  458. struct iwl_mvm_add_sta_cmd cmd = {};
  459. int ret;
  460. u32 status;
  461. lockdep_assert_held(&mvm->mutex);
  462. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  463. cmd.sta_id = mvm_sta->sta_id;
  464. cmd.add_modify = STA_MODE_MODIFY;
  465. cmd.add_immediate_ba_tid = (u8) tid;
  466. cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
  467. cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
  468. STA_MODIFY_REMOVE_BA_TID;
  469. status = ADD_STA_SUCCESS;
  470. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  471. &cmd, &status);
  472. if (ret)
  473. return ret;
  474. switch (status) {
  475. case ADD_STA_SUCCESS:
  476. IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
  477. start ? "start" : "stopp");
  478. break;
  479. case ADD_STA_IMMEDIATE_BA_FAILURE:
  480. IWL_WARN(mvm, "RX BA Session refused by fw\n");
  481. ret = -ENOSPC;
  482. break;
  483. default:
  484. ret = -EIO;
  485. IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
  486. start ? "start" : "stopp", status);
  487. break;
  488. }
  489. return ret;
  490. }
  491. static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  492. int tid, u8 queue, bool start)
  493. {
  494. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  495. struct iwl_mvm_add_sta_cmd cmd = {};
  496. int ret;
  497. u32 status;
  498. lockdep_assert_held(&mvm->mutex);
  499. if (start) {
  500. mvm_sta->tfd_queue_msk |= BIT(queue);
  501. mvm_sta->tid_disable_agg &= ~BIT(tid);
  502. } else {
  503. mvm_sta->tfd_queue_msk &= ~BIT(queue);
  504. mvm_sta->tid_disable_agg |= BIT(tid);
  505. }
  506. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  507. cmd.sta_id = mvm_sta->sta_id;
  508. cmd.add_modify = STA_MODE_MODIFY;
  509. cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
  510. cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
  511. cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
  512. status = ADD_STA_SUCCESS;
  513. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  514. &cmd, &status);
  515. if (ret)
  516. return ret;
  517. switch (status) {
  518. case ADD_STA_SUCCESS:
  519. break;
  520. default:
  521. ret = -EIO;
  522. IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
  523. start ? "start" : "stopp", status);
  524. break;
  525. }
  526. return ret;
  527. }
  528. static const u8 tid_to_ac[] = {
  529. IEEE80211_AC_BE,
  530. IEEE80211_AC_BK,
  531. IEEE80211_AC_BK,
  532. IEEE80211_AC_BE,
  533. IEEE80211_AC_VI,
  534. IEEE80211_AC_VI,
  535. IEEE80211_AC_VO,
  536. IEEE80211_AC_VO,
  537. };
  538. int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  539. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  540. {
  541. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  542. struct iwl_mvm_tid_data *tid_data;
  543. int txq_id;
  544. if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
  545. return -EINVAL;
  546. if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
  547. IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
  548. mvmsta->tid_data[tid].state);
  549. return -ENXIO;
  550. }
  551. lockdep_assert_held(&mvm->mutex);
  552. for (txq_id = IWL_MVM_FIRST_AGG_QUEUE;
  553. txq_id <= IWL_MVM_LAST_AGG_QUEUE; txq_id++)
  554. if (mvm->queue_to_mac80211[txq_id] ==
  555. IWL_INVALID_MAC80211_QUEUE)
  556. break;
  557. if (txq_id > IWL_MVM_LAST_AGG_QUEUE) {
  558. IWL_ERR(mvm, "Failed to allocate agg queue\n");
  559. return -EIO;
  560. }
  561. /* the new tx queue is still connected to the same mac80211 queue */
  562. mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
  563. spin_lock_bh(&mvmsta->lock);
  564. tid_data = &mvmsta->tid_data[tid];
  565. tid_data->ssn = SEQ_TO_SN(tid_data->seq_number);
  566. tid_data->txq_id = txq_id;
  567. *ssn = tid_data->ssn;
  568. IWL_DEBUG_TX_QUEUES(mvm,
  569. "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
  570. mvmsta->sta_id, tid, txq_id, tid_data->ssn,
  571. tid_data->next_reclaimed);
  572. if (tid_data->ssn == tid_data->next_reclaimed) {
  573. tid_data->state = IWL_AGG_STARTING;
  574. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  575. } else {
  576. tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  577. }
  578. spin_unlock_bh(&mvmsta->lock);
  579. return 0;
  580. }
  581. int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  582. struct ieee80211_sta *sta, u16 tid, u8 buf_size)
  583. {
  584. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  585. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  586. int queue, fifo, ret;
  587. u16 ssn;
  588. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  589. spin_lock_bh(&mvmsta->lock);
  590. ssn = tid_data->ssn;
  591. queue = tid_data->txq_id;
  592. tid_data->state = IWL_AGG_ON;
  593. tid_data->ssn = 0xffff;
  594. spin_unlock_bh(&mvmsta->lock);
  595. fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
  596. ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
  597. if (ret)
  598. return -EIO;
  599. iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
  600. buf_size, ssn);
  601. /*
  602. * Even though in theory the peer could have different
  603. * aggregation reorder buffer sizes for different sessions,
  604. * our ucode doesn't allow for that and has a global limit
  605. * for each station. Therefore, use the minimum of all the
  606. * aggregation sessions and our default value.
  607. */
  608. mvmsta->max_agg_bufsize =
  609. min(mvmsta->max_agg_bufsize, buf_size);
  610. mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  611. if (mvm->cfg->ht_params->use_rts_for_aggregation) {
  612. /*
  613. * switch to RTS/CTS if it is the prefer protection
  614. * method for HT traffic
  615. */
  616. mvmsta->lq_sta.lq.flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK;
  617. /*
  618. * TODO: remove the TLC_RTS flag when we tear down the last
  619. * AGG session (agg_tids_count in DVM)
  620. */
  621. }
  622. IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
  623. sta->addr, tid);
  624. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false);
  625. }
  626. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  627. struct ieee80211_sta *sta, u16 tid)
  628. {
  629. struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
  630. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  631. u16 txq_id;
  632. int err;
  633. spin_lock_bh(&mvmsta->lock);
  634. txq_id = tid_data->txq_id;
  635. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  636. mvmsta->sta_id, tid, txq_id, tid_data->state);
  637. switch (tid_data->state) {
  638. case IWL_AGG_ON:
  639. tid_data->ssn = SEQ_TO_SN(tid_data->seq_number);
  640. IWL_DEBUG_TX_QUEUES(mvm,
  641. "ssn = %d, next_recl = %d\n",
  642. tid_data->ssn, tid_data->next_reclaimed);
  643. /* There are still packets for this RA / TID in the HW */
  644. if (tid_data->ssn != tid_data->next_reclaimed) {
  645. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  646. err = 0;
  647. break;
  648. }
  649. tid_data->ssn = 0xffff;
  650. iwl_trans_txq_disable(mvm->trans, txq_id);
  651. /* fall through */
  652. case IWL_AGG_STARTING:
  653. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  654. /*
  655. * The agg session has been stopped before it was set up. This
  656. * can happen when the AddBA timer times out for example.
  657. */
  658. /* No barriers since we are under mutex */
  659. lockdep_assert_held(&mvm->mutex);
  660. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  661. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  662. tid_data->state = IWL_AGG_OFF;
  663. err = 0;
  664. break;
  665. default:
  666. IWL_ERR(mvm,
  667. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  668. mvmsta->sta_id, tid, tid_data->state);
  669. IWL_ERR(mvm,
  670. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  671. err = -EINVAL;
  672. }
  673. spin_unlock_bh(&mvmsta->lock);
  674. return err;
  675. }
  676. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  677. {
  678. int i;
  679. lockdep_assert_held(&mvm->mutex);
  680. i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
  681. if (i == STA_KEY_MAX_NUM)
  682. return STA_KEY_IDX_INVALID;
  683. __set_bit(i, mvm->fw_key_table);
  684. return i;
  685. }
  686. static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
  687. struct ieee80211_sta *sta)
  688. {
  689. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  690. if (sta) {
  691. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  692. return mvm_sta->sta_id;
  693. }
  694. /*
  695. * The device expects GTKs for station interfaces to be
  696. * installed as GTKs for the AP station. If we have no
  697. * station ID, then use AP's station ID.
  698. */
  699. if (vif->type == NL80211_IFTYPE_STATION &&
  700. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
  701. return mvmvif->ap_sta_id;
  702. return IWL_INVALID_STATION;
  703. }
  704. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  705. struct iwl_mvm_sta *mvm_sta,
  706. struct ieee80211_key_conf *keyconf,
  707. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  708. u32 cmd_flags)
  709. {
  710. __le16 key_flags;
  711. struct iwl_mvm_add_sta_cmd cmd = {};
  712. int ret, status;
  713. u16 keyidx;
  714. int i;
  715. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  716. STA_KEY_FLG_KEYID_MSK;
  717. key_flags = cpu_to_le16(keyidx);
  718. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  719. switch (keyconf->cipher) {
  720. case WLAN_CIPHER_SUITE_TKIP:
  721. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  722. cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
  723. for (i = 0; i < 5; i++)
  724. cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  725. memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
  726. break;
  727. case WLAN_CIPHER_SUITE_CCMP:
  728. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  729. memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
  730. break;
  731. default:
  732. WARN_ON(1);
  733. return -EINVAL;
  734. }
  735. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  736. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  737. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  738. cmd.key.key_offset = keyconf->hw_key_idx;
  739. cmd.key.key_flags = key_flags;
  740. cmd.add_modify = STA_MODE_MODIFY;
  741. cmd.modify_mask = STA_MODIFY_KEY;
  742. cmd.sta_id = sta_id;
  743. status = ADD_STA_SUCCESS;
  744. if (cmd_flags == CMD_SYNC)
  745. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  746. &cmd, &status);
  747. else
  748. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
  749. sizeof(cmd), &cmd);
  750. switch (status) {
  751. case ADD_STA_SUCCESS:
  752. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  753. break;
  754. default:
  755. ret = -EIO;
  756. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  757. break;
  758. }
  759. return ret;
  760. }
  761. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  762. struct ieee80211_key_conf *keyconf,
  763. u8 sta_id, bool remove_key)
  764. {
  765. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  766. /* verify the key details match the required command's expectations */
  767. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  768. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  769. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  770. return -EINVAL;
  771. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  772. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  773. if (remove_key) {
  774. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  775. } else {
  776. struct ieee80211_key_seq seq;
  777. const u8 *pn;
  778. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  779. ieee80211_aes_cmac_calculate_k1_k2(keyconf,
  780. igtk_cmd.K1, igtk_cmd.K2);
  781. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  782. pn = seq.aes_cmac.pn;
  783. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  784. ((u64) pn[4] << 8) |
  785. ((u64) pn[3] << 16) |
  786. ((u64) pn[2] << 24) |
  787. ((u64) pn[1] << 32) |
  788. ((u64) pn[0] << 40));
  789. }
  790. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  791. remove_key ? "removing" : "installing",
  792. igtk_cmd.sta_id);
  793. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
  794. sizeof(igtk_cmd), &igtk_cmd);
  795. }
  796. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  797. struct ieee80211_vif *vif,
  798. struct ieee80211_sta *sta)
  799. {
  800. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  801. if (sta)
  802. return sta->addr;
  803. if (vif->type == NL80211_IFTYPE_STATION &&
  804. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  805. u8 sta_id = mvmvif->ap_sta_id;
  806. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  807. lockdep_is_held(&mvm->mutex));
  808. return sta->addr;
  809. }
  810. return NULL;
  811. }
  812. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  813. struct ieee80211_vif *vif,
  814. struct ieee80211_sta *sta,
  815. struct ieee80211_key_conf *keyconf,
  816. bool have_key_offset)
  817. {
  818. struct iwl_mvm_sta *mvm_sta;
  819. int ret;
  820. u8 *addr, sta_id;
  821. struct ieee80211_key_seq seq;
  822. u16 p1k[5];
  823. lockdep_assert_held(&mvm->mutex);
  824. /* Get the station id from the mvm local station table */
  825. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  826. if (sta_id == IWL_INVALID_STATION) {
  827. IWL_ERR(mvm, "Failed to find station id\n");
  828. return -EINVAL;
  829. }
  830. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  831. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  832. goto end;
  833. }
  834. /*
  835. * It is possible that the 'sta' parameter is NULL, and thus
  836. * there is a need to retrieve the sta from the local station table.
  837. */
  838. if (!sta) {
  839. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  840. lockdep_is_held(&mvm->mutex));
  841. if (IS_ERR_OR_NULL(sta)) {
  842. IWL_ERR(mvm, "Invalid station id\n");
  843. return -EINVAL;
  844. }
  845. }
  846. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  847. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  848. return -EINVAL;
  849. if (!have_key_offset) {
  850. /*
  851. * The D3 firmware hardcodes the PTK offset to 0, so we have to
  852. * configure it there. As a result, this workaround exists to
  853. * let the caller set the key offset (hw_key_idx), see d3.c.
  854. */
  855. keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
  856. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  857. return -ENOSPC;
  858. }
  859. switch (keyconf->cipher) {
  860. case WLAN_CIPHER_SUITE_TKIP:
  861. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  862. /* get phase 1 key from mac80211 */
  863. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  864. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  865. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  866. seq.tkip.iv32, p1k, CMD_SYNC);
  867. break;
  868. case WLAN_CIPHER_SUITE_CCMP:
  869. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  870. 0, NULL, CMD_SYNC);
  871. break;
  872. default:
  873. IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
  874. ret = -EINVAL;
  875. }
  876. if (ret)
  877. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  878. end:
  879. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  880. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  881. sta->addr, ret);
  882. return ret;
  883. }
  884. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  885. struct ieee80211_vif *vif,
  886. struct ieee80211_sta *sta,
  887. struct ieee80211_key_conf *keyconf)
  888. {
  889. struct iwl_mvm_sta *mvm_sta;
  890. struct iwl_mvm_add_sta_cmd cmd = {};
  891. __le16 key_flags;
  892. int ret, status;
  893. u8 sta_id;
  894. lockdep_assert_held(&mvm->mutex);
  895. /* Get the station id from the mvm local station table */
  896. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  897. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  898. keyconf->keyidx, sta_id);
  899. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  900. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  901. ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  902. if (!ret) {
  903. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  904. keyconf->hw_key_idx);
  905. return -ENOENT;
  906. }
  907. if (sta_id == IWL_INVALID_STATION) {
  908. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  909. return 0;
  910. }
  911. /*
  912. * It is possible that the 'sta' parameter is NULL, and thus
  913. * there is a need to retrieve the sta from the local station table,
  914. * for example when a GTK is removed (where the sta_id will then be
  915. * the AP ID, and no station was passed by mac80211.)
  916. */
  917. if (!sta) {
  918. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  919. lockdep_is_held(&mvm->mutex));
  920. if (!sta) {
  921. IWL_ERR(mvm, "Invalid station id\n");
  922. return -EINVAL;
  923. }
  924. }
  925. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  926. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  927. return -EINVAL;
  928. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  929. STA_KEY_FLG_KEYID_MSK);
  930. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  931. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  932. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  933. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  934. cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
  935. cmd.key.key_flags = key_flags;
  936. cmd.key.key_offset = keyconf->hw_key_idx;
  937. cmd.sta_id = sta_id;
  938. cmd.modify_mask = STA_MODIFY_KEY;
  939. cmd.add_modify = STA_MODE_MODIFY;
  940. status = ADD_STA_SUCCESS;
  941. ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
  942. &cmd, &status);
  943. switch (status) {
  944. case ADD_STA_SUCCESS:
  945. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  946. break;
  947. default:
  948. ret = -EIO;
  949. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  950. break;
  951. }
  952. return ret;
  953. }
  954. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  955. struct ieee80211_vif *vif,
  956. struct ieee80211_key_conf *keyconf,
  957. struct ieee80211_sta *sta, u32 iv32,
  958. u16 *phase1key)
  959. {
  960. struct iwl_mvm_sta *mvm_sta;
  961. u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  962. if (WARN_ON_ONCE(sta_id == IWL_INVALID_STATION))
  963. return;
  964. rcu_read_lock();
  965. if (!sta) {
  966. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  967. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  968. rcu_read_unlock();
  969. return;
  970. }
  971. }
  972. mvm_sta = (void *)sta->drv_priv;
  973. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  974. iv32, phase1key, CMD_ASYNC);
  975. rcu_read_unlock();
  976. }
  977. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm, int sta_id)
  978. {
  979. struct iwl_mvm_add_sta_cmd cmd = {
  980. .add_modify = STA_MODE_MODIFY,
  981. .sta_id = sta_id,
  982. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  983. .sleep_state_flags = cpu_to_le16(STA_SLEEP_STATE_AWAKE),
  984. };
  985. int ret;
  986. /*
  987. * Same modify mask for sleep_tx_count and sleep_state_flags but this
  988. * should be fine since if we set the STA as "awake", then
  989. * sleep_tx_count is not relevant.
  990. */
  991. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  992. if (ret)
  993. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  994. }
  995. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm, int sta_id,
  996. enum ieee80211_frame_release_type reason,
  997. u16 cnt)
  998. {
  999. u16 sleep_state_flags =
  1000. (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
  1001. STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
  1002. struct iwl_mvm_add_sta_cmd cmd = {
  1003. .add_modify = STA_MODE_MODIFY,
  1004. .sta_id = sta_id,
  1005. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1006. .sleep_tx_count = cpu_to_le16(cnt),
  1007. /*
  1008. * Same modify mask for sleep_tx_count and sleep_state_flags so
  1009. * we must set the sleep_state_flags too.
  1010. */
  1011. .sleep_state_flags = cpu_to_le16(sleep_state_flags),
  1012. };
  1013. int ret;
  1014. /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
  1015. ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
  1016. if (ret)
  1017. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1018. }