sta.c 36 KB

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