sta.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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 - 2014 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 - 2014 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. 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. return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
  789. }
  790. int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  791. struct ieee80211_sta *sta, u16 tid)
  792. {
  793. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  794. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  795. u16 txq_id;
  796. int err;
  797. /*
  798. * If mac80211 is cleaning its state, then say that we finished since
  799. * our state has been cleared anyway.
  800. */
  801. if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
  802. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  803. return 0;
  804. }
  805. spin_lock_bh(&mvmsta->lock);
  806. txq_id = tid_data->txq_id;
  807. IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
  808. mvmsta->sta_id, tid, txq_id, tid_data->state);
  809. switch (tid_data->state) {
  810. case IWL_AGG_ON:
  811. tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  812. IWL_DEBUG_TX_QUEUES(mvm,
  813. "ssn = %d, next_recl = %d\n",
  814. tid_data->ssn, tid_data->next_reclaimed);
  815. /* There are still packets for this RA / TID in the HW */
  816. if (tid_data->ssn != tid_data->next_reclaimed) {
  817. tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
  818. err = 0;
  819. break;
  820. }
  821. tid_data->ssn = 0xffff;
  822. iwl_trans_txq_disable(mvm->trans, txq_id);
  823. /* fall through */
  824. case IWL_AGG_STARTING:
  825. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  826. /*
  827. * The agg session has been stopped before it was set up. This
  828. * can happen when the AddBA timer times out for example.
  829. */
  830. /* No barriers since we are under mutex */
  831. lockdep_assert_held(&mvm->mutex);
  832. mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
  833. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  834. tid_data->state = IWL_AGG_OFF;
  835. err = 0;
  836. break;
  837. default:
  838. IWL_ERR(mvm,
  839. "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
  840. mvmsta->sta_id, tid, tid_data->state);
  841. IWL_ERR(mvm,
  842. "\ttid_data->txq_id = %d\n", tid_data->txq_id);
  843. err = -EINVAL;
  844. }
  845. spin_unlock_bh(&mvmsta->lock);
  846. return err;
  847. }
  848. int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  849. struct ieee80211_sta *sta, u16 tid)
  850. {
  851. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  852. struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
  853. u16 txq_id;
  854. enum iwl_mvm_agg_state old_state;
  855. /*
  856. * First set the agg state to OFF to avoid calling
  857. * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
  858. */
  859. spin_lock_bh(&mvmsta->lock);
  860. txq_id = tid_data->txq_id;
  861. IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
  862. mvmsta->sta_id, tid, txq_id, tid_data->state);
  863. old_state = tid_data->state;
  864. tid_data->state = IWL_AGG_OFF;
  865. spin_unlock_bh(&mvmsta->lock);
  866. if (old_state >= IWL_AGG_ON) {
  867. if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
  868. IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
  869. iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
  870. }
  871. mvm->queue_to_mac80211[tid_data->txq_id] =
  872. IWL_INVALID_MAC80211_QUEUE;
  873. return 0;
  874. }
  875. static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
  876. {
  877. int i;
  878. lockdep_assert_held(&mvm->mutex);
  879. i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
  880. if (i == STA_KEY_MAX_NUM)
  881. return STA_KEY_IDX_INVALID;
  882. __set_bit(i, mvm->fw_key_table);
  883. return i;
  884. }
  885. static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
  886. struct ieee80211_sta *sta)
  887. {
  888. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  889. if (sta) {
  890. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  891. return mvm_sta->sta_id;
  892. }
  893. /*
  894. * The device expects GTKs for station interfaces to be
  895. * installed as GTKs for the AP station. If we have no
  896. * station ID, then use AP's station ID.
  897. */
  898. if (vif->type == NL80211_IFTYPE_STATION &&
  899. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
  900. return mvmvif->ap_sta_id;
  901. return IWL_MVM_STATION_COUNT;
  902. }
  903. static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
  904. struct iwl_mvm_sta *mvm_sta,
  905. struct ieee80211_key_conf *keyconf,
  906. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  907. u32 cmd_flags)
  908. {
  909. __le16 key_flags;
  910. struct iwl_mvm_add_sta_key_cmd cmd = {};
  911. int ret, status;
  912. u16 keyidx;
  913. int i;
  914. u32 mac_id_n_color = mvm_sta->mac_id_n_color;
  915. keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  916. STA_KEY_FLG_KEYID_MSK;
  917. key_flags = cpu_to_le16(keyidx);
  918. key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
  919. switch (keyconf->cipher) {
  920. case WLAN_CIPHER_SUITE_TKIP:
  921. key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
  922. cmd.tkip_rx_tsc_byte2 = tkip_iv32;
  923. for (i = 0; i < 5; i++)
  924. cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  925. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  926. break;
  927. case WLAN_CIPHER_SUITE_CCMP:
  928. key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
  929. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  930. break;
  931. default:
  932. key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
  933. memcpy(cmd.key, keyconf->key, keyconf->keylen);
  934. }
  935. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  936. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  937. cmd.key_offset = keyconf->hw_key_idx;
  938. cmd.key_flags = key_flags;
  939. cmd.sta_id = sta_id;
  940. status = ADD_STA_SUCCESS;
  941. if (cmd_flags == CMD_SYNC)
  942. ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
  943. mac_id_n_color,
  944. &status);
  945. else
  946. ret = iwl_mvm_send_add_sta_key_cmd(mvm, CMD_ASYNC, &cmd,
  947. mac_id_n_color);
  948. switch (status) {
  949. case ADD_STA_SUCCESS:
  950. IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
  951. break;
  952. default:
  953. ret = -EIO;
  954. IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
  955. break;
  956. }
  957. return ret;
  958. }
  959. static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
  960. struct ieee80211_key_conf *keyconf,
  961. u8 sta_id, bool remove_key)
  962. {
  963. struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
  964. /* verify the key details match the required command's expectations */
  965. if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
  966. (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
  967. (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
  968. return -EINVAL;
  969. igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
  970. igtk_cmd.sta_id = cpu_to_le32(sta_id);
  971. if (remove_key) {
  972. igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
  973. } else {
  974. struct ieee80211_key_seq seq;
  975. const u8 *pn;
  976. memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
  977. ieee80211_aes_cmac_calculate_k1_k2(keyconf,
  978. igtk_cmd.K1, igtk_cmd.K2);
  979. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  980. pn = seq.aes_cmac.pn;
  981. igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
  982. ((u64) pn[4] << 8) |
  983. ((u64) pn[3] << 16) |
  984. ((u64) pn[2] << 24) |
  985. ((u64) pn[1] << 32) |
  986. ((u64) pn[0] << 40));
  987. }
  988. IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
  989. remove_key ? "removing" : "installing",
  990. igtk_cmd.sta_id);
  991. return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
  992. sizeof(igtk_cmd), &igtk_cmd);
  993. }
  994. static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
  995. struct ieee80211_vif *vif,
  996. struct ieee80211_sta *sta)
  997. {
  998. struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
  999. if (sta)
  1000. return sta->addr;
  1001. if (vif->type == NL80211_IFTYPE_STATION &&
  1002. mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
  1003. u8 sta_id = mvmvif->ap_sta_id;
  1004. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1005. lockdep_is_held(&mvm->mutex));
  1006. return sta->addr;
  1007. }
  1008. return NULL;
  1009. }
  1010. int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
  1011. struct ieee80211_vif *vif,
  1012. struct ieee80211_sta *sta,
  1013. struct ieee80211_key_conf *keyconf,
  1014. bool have_key_offset)
  1015. {
  1016. struct iwl_mvm_sta *mvm_sta;
  1017. int ret;
  1018. u8 *addr, sta_id;
  1019. struct ieee80211_key_seq seq;
  1020. u16 p1k[5];
  1021. lockdep_assert_held(&mvm->mutex);
  1022. /* Get the station id from the mvm local station table */
  1023. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1024. if (sta_id == IWL_MVM_STATION_COUNT) {
  1025. IWL_ERR(mvm, "Failed to find station id\n");
  1026. return -EINVAL;
  1027. }
  1028. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  1029. ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
  1030. goto end;
  1031. }
  1032. /*
  1033. * It is possible that the 'sta' parameter is NULL, and thus
  1034. * there is a need to retrieve the sta from the local station table.
  1035. */
  1036. if (!sta) {
  1037. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1038. lockdep_is_held(&mvm->mutex));
  1039. if (IS_ERR_OR_NULL(sta)) {
  1040. IWL_ERR(mvm, "Invalid station id\n");
  1041. return -EINVAL;
  1042. }
  1043. }
  1044. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1045. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1046. return -EINVAL;
  1047. if (!have_key_offset) {
  1048. /*
  1049. * The D3 firmware hardcodes the PTK offset to 0, so we have to
  1050. * configure it there. As a result, this workaround exists to
  1051. * let the caller set the key offset (hw_key_idx), see d3.c.
  1052. */
  1053. keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
  1054. if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
  1055. return -ENOSPC;
  1056. }
  1057. switch (keyconf->cipher) {
  1058. case WLAN_CIPHER_SUITE_TKIP:
  1059. addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
  1060. /* get phase 1 key from mac80211 */
  1061. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1062. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1063. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1064. seq.tkip.iv32, p1k, CMD_SYNC);
  1065. break;
  1066. case WLAN_CIPHER_SUITE_CCMP:
  1067. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1068. 0, NULL, CMD_SYNC);
  1069. break;
  1070. default:
  1071. ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
  1072. sta_id, 0, NULL, CMD_SYNC);
  1073. }
  1074. if (ret)
  1075. __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1076. end:
  1077. IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1078. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1079. sta->addr, ret);
  1080. return ret;
  1081. }
  1082. int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
  1083. struct ieee80211_vif *vif,
  1084. struct ieee80211_sta *sta,
  1085. struct ieee80211_key_conf *keyconf)
  1086. {
  1087. struct iwl_mvm_sta *mvm_sta;
  1088. struct iwl_mvm_add_sta_key_cmd cmd = {};
  1089. __le16 key_flags;
  1090. int ret, status;
  1091. u8 sta_id;
  1092. lockdep_assert_held(&mvm->mutex);
  1093. /* Get the station id from the mvm local station table */
  1094. sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1095. IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
  1096. keyconf->keyidx, sta_id);
  1097. if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  1098. return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
  1099. ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
  1100. if (!ret) {
  1101. IWL_ERR(mvm, "offset %d not used in fw key table.\n",
  1102. keyconf->hw_key_idx);
  1103. return -ENOENT;
  1104. }
  1105. if (sta_id == IWL_MVM_STATION_COUNT) {
  1106. IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
  1107. return 0;
  1108. }
  1109. /*
  1110. * It is possible that the 'sta' parameter is NULL, and thus
  1111. * there is a need to retrieve the sta from the local station table,
  1112. * for example when a GTK is removed (where the sta_id will then be
  1113. * the AP ID, and no station was passed by mac80211.)
  1114. */
  1115. if (!sta) {
  1116. sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
  1117. lockdep_is_held(&mvm->mutex));
  1118. if (!sta) {
  1119. IWL_ERR(mvm, "Invalid station id\n");
  1120. return -EINVAL;
  1121. }
  1122. }
  1123. mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
  1124. if (WARN_ON_ONCE(mvm_sta->vif != vif))
  1125. return -EINVAL;
  1126. key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
  1127. STA_KEY_FLG_KEYID_MSK);
  1128. key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
  1129. key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
  1130. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1131. key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
  1132. cmd.key_flags = key_flags;
  1133. cmd.key_offset = keyconf->hw_key_idx;
  1134. cmd.sta_id = sta_id;
  1135. status = ADD_STA_SUCCESS;
  1136. ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
  1137. mvm_sta->mac_id_n_color,
  1138. &status);
  1139. switch (status) {
  1140. case ADD_STA_SUCCESS:
  1141. IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
  1142. break;
  1143. default:
  1144. ret = -EIO;
  1145. IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
  1146. break;
  1147. }
  1148. return ret;
  1149. }
  1150. void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
  1151. struct ieee80211_vif *vif,
  1152. struct ieee80211_key_conf *keyconf,
  1153. struct ieee80211_sta *sta, u32 iv32,
  1154. u16 *phase1key)
  1155. {
  1156. struct iwl_mvm_sta *mvm_sta;
  1157. u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
  1158. if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
  1159. return;
  1160. rcu_read_lock();
  1161. if (!sta) {
  1162. sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
  1163. if (WARN_ON(IS_ERR_OR_NULL(sta))) {
  1164. rcu_read_unlock();
  1165. return;
  1166. }
  1167. }
  1168. mvm_sta = (void *)sta->drv_priv;
  1169. iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
  1170. iv32, phase1key, CMD_ASYNC);
  1171. rcu_read_unlock();
  1172. }
  1173. void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
  1174. struct ieee80211_sta *sta)
  1175. {
  1176. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1177. struct iwl_mvm_add_sta_cmd_v6 cmd = {
  1178. .add_modify = STA_MODE_MODIFY,
  1179. .sta_id = mvmsta->sta_id,
  1180. .station_flags_msk = cpu_to_le32(STA_FLG_PS),
  1181. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1182. };
  1183. int ret;
  1184. ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
  1185. if (ret)
  1186. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1187. }
  1188. void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
  1189. struct ieee80211_sta *sta,
  1190. enum ieee80211_frame_release_type reason,
  1191. u16 cnt)
  1192. {
  1193. u16 sleep_state_flags =
  1194. (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
  1195. STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
  1196. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1197. struct iwl_mvm_add_sta_cmd_v6 cmd = {
  1198. .add_modify = STA_MODE_MODIFY,
  1199. .sta_id = mvmsta->sta_id,
  1200. .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
  1201. .sleep_tx_count = cpu_to_le16(cnt),
  1202. .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
  1203. /*
  1204. * Same modify mask for sleep_tx_count and sleep_state_flags so
  1205. * we must set the sleep_state_flags too.
  1206. */
  1207. .sleep_state_flags = cpu_to_le16(sleep_state_flags),
  1208. };
  1209. int ret;
  1210. /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
  1211. ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
  1212. if (ret)
  1213. IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
  1214. }