iwl-agn-sta.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <net/mac80211.h>
  30. #include "iwl-dev.h"
  31. #include "iwl-core.h"
  32. #include "iwl-sta.h"
  33. #include "iwl-agn.h"
  34. static struct iwl_link_quality_cmd *
  35. iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx, u8 sta_id)
  36. {
  37. int i, r;
  38. struct iwl_link_quality_cmd *link_cmd;
  39. u32 rate_flags = 0;
  40. __le32 rate_n_flags;
  41. link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
  42. if (!link_cmd) {
  43. IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
  44. return NULL;
  45. }
  46. lockdep_assert_held(&priv->mutex);
  47. /* Set up the rate scaling to start at selected rate, fall back
  48. * all the way down to 1M in IEEE order, and then spin on 1M */
  49. if (priv->band == IEEE80211_BAND_5GHZ)
  50. r = IWL_RATE_6M_INDEX;
  51. else if (ctx && ctx->vif && ctx->vif->p2p)
  52. r = IWL_RATE_6M_INDEX;
  53. else
  54. r = IWL_RATE_1M_INDEX;
  55. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  56. rate_flags |= RATE_MCS_CCK_MSK;
  57. rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
  58. RATE_MCS_ANT_POS;
  59. rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  60. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  61. link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
  62. link_cmd->general_params.single_stream_ant_msk =
  63. first_antenna(priv->hw_params.valid_tx_ant);
  64. link_cmd->general_params.dual_stream_ant_msk =
  65. priv->hw_params.valid_tx_ant &
  66. ~first_antenna(priv->hw_params.valid_tx_ant);
  67. if (!link_cmd->general_params.dual_stream_ant_msk) {
  68. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  69. } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
  70. link_cmd->general_params.dual_stream_ant_msk =
  71. priv->hw_params.valid_tx_ant;
  72. }
  73. link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  74. link_cmd->agg_params.agg_time_limit =
  75. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  76. link_cmd->sta_id = sta_id;
  77. return link_cmd;
  78. }
  79. /*
  80. * iwlagn_add_bssid_station - Add the special IBSS BSSID station
  81. *
  82. * Function sleeps.
  83. */
  84. int iwlagn_add_bssid_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  85. const u8 *addr, u8 *sta_id_r)
  86. {
  87. int ret;
  88. u8 sta_id;
  89. struct iwl_link_quality_cmd *link_cmd;
  90. unsigned long flags;
  91. if (sta_id_r)
  92. *sta_id_r = IWL_INVALID_STATION;
  93. ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
  94. if (ret) {
  95. IWL_ERR(priv, "Unable to add station %pM\n", addr);
  96. return ret;
  97. }
  98. if (sta_id_r)
  99. *sta_id_r = sta_id;
  100. spin_lock_irqsave(&priv->sta_lock, flags);
  101. priv->stations[sta_id].used |= IWL_STA_LOCAL;
  102. spin_unlock_irqrestore(&priv->sta_lock, flags);
  103. /* Set up default rate scaling table in device's station table */
  104. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  105. if (!link_cmd) {
  106. IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
  107. addr);
  108. return -ENOMEM;
  109. }
  110. ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
  111. if (ret)
  112. IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
  113. spin_lock_irqsave(&priv->sta_lock, flags);
  114. priv->stations[sta_id].lq = link_cmd;
  115. spin_unlock_irqrestore(&priv->sta_lock, flags);
  116. return 0;
  117. }
  118. static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
  119. struct iwl_rxon_context *ctx,
  120. bool send_if_empty)
  121. {
  122. int i, not_empty = 0;
  123. u8 buff[sizeof(struct iwl_wep_cmd) +
  124. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  125. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  126. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  127. struct iwl_host_cmd cmd = {
  128. .id = ctx->wep_key_cmd,
  129. .data = { wep_cmd, },
  130. .flags = CMD_SYNC,
  131. };
  132. might_sleep();
  133. memset(wep_cmd, 0, cmd_size +
  134. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  135. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  136. wep_cmd->key[i].key_index = i;
  137. if (ctx->wep_keys[i].key_size) {
  138. wep_cmd->key[i].key_offset = i;
  139. not_empty = 1;
  140. } else {
  141. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  142. }
  143. wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
  144. memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
  145. ctx->wep_keys[i].key_size);
  146. }
  147. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  148. wep_cmd->num_keys = WEP_KEYS_MAX;
  149. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  150. cmd.len[0] = cmd_size;
  151. if (not_empty || send_if_empty)
  152. return iwl_send_cmd(priv, &cmd);
  153. else
  154. return 0;
  155. }
  156. int iwl_restore_default_wep_keys(struct iwl_priv *priv,
  157. struct iwl_rxon_context *ctx)
  158. {
  159. lockdep_assert_held(&priv->mutex);
  160. return iwl_send_static_wepkey_cmd(priv, ctx, false);
  161. }
  162. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  163. struct iwl_rxon_context *ctx,
  164. struct ieee80211_key_conf *keyconf)
  165. {
  166. int ret;
  167. lockdep_assert_held(&priv->mutex);
  168. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  169. keyconf->keyidx);
  170. memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
  171. if (iwl_is_rfkill(priv)) {
  172. IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  173. /* but keys in device are clear anyway so return success */
  174. return 0;
  175. }
  176. ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
  177. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  178. keyconf->keyidx, ret);
  179. return ret;
  180. }
  181. int iwl_set_default_wep_key(struct iwl_priv *priv,
  182. struct iwl_rxon_context *ctx,
  183. struct ieee80211_key_conf *keyconf)
  184. {
  185. int ret;
  186. lockdep_assert_held(&priv->mutex);
  187. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  188. keyconf->keylen != WEP_KEY_LEN_64) {
  189. IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
  190. return -EINVAL;
  191. }
  192. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  193. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  194. priv->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
  195. ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  196. memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
  197. keyconf->keylen);
  198. ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
  199. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  200. keyconf->keylen, keyconf->keyidx, ret);
  201. return ret;
  202. }
  203. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  204. struct iwl_rxon_context *ctx,
  205. struct ieee80211_key_conf *keyconf,
  206. u8 sta_id)
  207. {
  208. unsigned long flags;
  209. __le16 key_flags = 0;
  210. struct iwl_addsta_cmd sta_cmd;
  211. lockdep_assert_held(&priv->mutex);
  212. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  213. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  214. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  215. key_flags &= ~STA_KEY_FLG_INVALID;
  216. if (keyconf->keylen == WEP_KEY_LEN_128)
  217. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  218. if (sta_id == ctx->bcast_sta_id)
  219. key_flags |= STA_KEY_MULTICAST_MSK;
  220. spin_lock_irqsave(&priv->sta_lock, flags);
  221. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  222. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  223. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  224. memcpy(priv->stations[sta_id].keyinfo.key,
  225. keyconf->key, keyconf->keylen);
  226. memcpy(&priv->stations[sta_id].sta.key.key[3],
  227. keyconf->key, keyconf->keylen);
  228. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  229. == STA_KEY_FLG_NO_ENC)
  230. priv->stations[sta_id].sta.key.key_offset =
  231. iwl_get_free_ucode_key_index(priv);
  232. /* else, we are overriding an existing key => no need to allocated room
  233. * in uCode. */
  234. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  235. "no space for a new key");
  236. priv->stations[sta_id].sta.key.key_flags = key_flags;
  237. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  238. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  239. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  240. spin_unlock_irqrestore(&priv->sta_lock, flags);
  241. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  242. }
  243. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  244. struct iwl_rxon_context *ctx,
  245. struct ieee80211_key_conf *keyconf,
  246. u8 sta_id)
  247. {
  248. unsigned long flags;
  249. __le16 key_flags = 0;
  250. struct iwl_addsta_cmd sta_cmd;
  251. lockdep_assert_held(&priv->mutex);
  252. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  253. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  254. key_flags &= ~STA_KEY_FLG_INVALID;
  255. if (sta_id == ctx->bcast_sta_id)
  256. key_flags |= STA_KEY_MULTICAST_MSK;
  257. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  258. spin_lock_irqsave(&priv->sta_lock, flags);
  259. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  260. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  261. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  262. keyconf->keylen);
  263. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  264. keyconf->keylen);
  265. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  266. == STA_KEY_FLG_NO_ENC)
  267. priv->stations[sta_id].sta.key.key_offset =
  268. iwl_get_free_ucode_key_index(priv);
  269. /* else, we are overriding an existing key => no need to allocated room
  270. * in uCode. */
  271. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  272. "no space for a new key");
  273. priv->stations[sta_id].sta.key.key_flags = key_flags;
  274. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  275. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  276. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  277. spin_unlock_irqrestore(&priv->sta_lock, flags);
  278. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  279. }
  280. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  281. struct iwl_rxon_context *ctx,
  282. struct ieee80211_key_conf *keyconf,
  283. u8 sta_id)
  284. {
  285. unsigned long flags;
  286. int ret = 0;
  287. __le16 key_flags = 0;
  288. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  289. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  290. key_flags &= ~STA_KEY_FLG_INVALID;
  291. if (sta_id == ctx->bcast_sta_id)
  292. key_flags |= STA_KEY_MULTICAST_MSK;
  293. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  294. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  295. spin_lock_irqsave(&priv->sta_lock, flags);
  296. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  297. priv->stations[sta_id].keyinfo.keylen = 16;
  298. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  299. == STA_KEY_FLG_NO_ENC)
  300. priv->stations[sta_id].sta.key.key_offset =
  301. iwl_get_free_ucode_key_index(priv);
  302. /* else, we are overriding an existing key => no need to allocated room
  303. * in uCode. */
  304. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  305. "no space for a new key");
  306. priv->stations[sta_id].sta.key.key_flags = key_flags;
  307. /* This copy is acutally not needed: we get the key with each TX */
  308. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  309. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  310. spin_unlock_irqrestore(&priv->sta_lock, flags);
  311. return ret;
  312. }
  313. void iwl_update_tkip_key(struct iwl_priv *priv,
  314. struct iwl_rxon_context *ctx,
  315. struct ieee80211_key_conf *keyconf,
  316. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  317. {
  318. u8 sta_id;
  319. unsigned long flags;
  320. int i;
  321. if (iwl_scan_cancel(priv)) {
  322. /* cancel scan failed, just live w/ bad key and rely
  323. briefly on SW decryption */
  324. return;
  325. }
  326. sta_id = iwl_sta_id_or_broadcast(priv, ctx, sta);
  327. if (sta_id == IWL_INVALID_STATION)
  328. return;
  329. spin_lock_irqsave(&priv->sta_lock, flags);
  330. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  331. for (i = 0; i < 5; i++)
  332. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  333. cpu_to_le16(phase1key[i]);
  334. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  335. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  336. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  337. spin_unlock_irqrestore(&priv->sta_lock, flags);
  338. }
  339. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  340. struct iwl_rxon_context *ctx,
  341. struct ieee80211_key_conf *keyconf,
  342. u8 sta_id)
  343. {
  344. unsigned long flags;
  345. u16 key_flags;
  346. u8 keyidx;
  347. struct iwl_addsta_cmd sta_cmd;
  348. lockdep_assert_held(&priv->mutex);
  349. ctx->key_mapping_keys--;
  350. spin_lock_irqsave(&priv->sta_lock, flags);
  351. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  352. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  353. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  354. keyconf->keyidx, sta_id);
  355. if (keyconf->keyidx != keyidx) {
  356. /* We need to remove a key with index different that the one
  357. * in the uCode. This means that the key we need to remove has
  358. * been replaced by another one with different index.
  359. * Don't do anything and return ok
  360. */
  361. spin_unlock_irqrestore(&priv->sta_lock, flags);
  362. return 0;
  363. }
  364. if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  365. IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
  366. keyconf->keyidx, key_flags);
  367. spin_unlock_irqrestore(&priv->sta_lock, flags);
  368. return 0;
  369. }
  370. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  371. &priv->ucode_key_table))
  372. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  373. priv->stations[sta_id].sta.key.key_offset);
  374. memset(&priv->stations[sta_id].keyinfo, 0,
  375. sizeof(struct iwl_hw_key));
  376. memset(&priv->stations[sta_id].sta.key, 0,
  377. sizeof(struct iwl_keyinfo));
  378. priv->stations[sta_id].sta.key.key_flags =
  379. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  380. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  381. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  382. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  383. if (iwl_is_rfkill(priv)) {
  384. IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
  385. spin_unlock_irqrestore(&priv->sta_lock, flags);
  386. return 0;
  387. }
  388. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  389. spin_unlock_irqrestore(&priv->sta_lock, flags);
  390. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  391. }
  392. int iwl_set_dynamic_key(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  393. struct ieee80211_key_conf *keyconf, u8 sta_id)
  394. {
  395. int ret;
  396. lockdep_assert_held(&priv->mutex);
  397. ctx->key_mapping_keys++;
  398. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  399. switch (keyconf->cipher) {
  400. case WLAN_CIPHER_SUITE_CCMP:
  401. ret = iwl_set_ccmp_dynamic_key_info(priv, ctx, keyconf, sta_id);
  402. break;
  403. case WLAN_CIPHER_SUITE_TKIP:
  404. ret = iwl_set_tkip_dynamic_key_info(priv, ctx, keyconf, sta_id);
  405. break;
  406. case WLAN_CIPHER_SUITE_WEP40:
  407. case WLAN_CIPHER_SUITE_WEP104:
  408. ret = iwl_set_wep_dynamic_key_info(priv, ctx, keyconf, sta_id);
  409. break;
  410. default:
  411. IWL_ERR(priv,
  412. "Unknown alg: %s cipher = %x\n", __func__,
  413. keyconf->cipher);
  414. ret = -EINVAL;
  415. }
  416. IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
  417. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  418. sta_id, ret);
  419. return ret;
  420. }
  421. /**
  422. * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
  423. *
  424. * This adds the broadcast station into the driver's station table
  425. * and marks it driver active, so that it will be restored to the
  426. * device at the next best time.
  427. */
  428. int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
  429. struct iwl_rxon_context *ctx)
  430. {
  431. struct iwl_link_quality_cmd *link_cmd;
  432. unsigned long flags;
  433. u8 sta_id;
  434. spin_lock_irqsave(&priv->sta_lock, flags);
  435. sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
  436. if (sta_id == IWL_INVALID_STATION) {
  437. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  438. spin_unlock_irqrestore(&priv->sta_lock, flags);
  439. return -EINVAL;
  440. }
  441. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  442. priv->stations[sta_id].used |= IWL_STA_BCAST;
  443. spin_unlock_irqrestore(&priv->sta_lock, flags);
  444. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  445. if (!link_cmd) {
  446. IWL_ERR(priv,
  447. "Unable to initialize rate scaling for bcast station.\n");
  448. return -ENOMEM;
  449. }
  450. spin_lock_irqsave(&priv->sta_lock, flags);
  451. priv->stations[sta_id].lq = link_cmd;
  452. spin_unlock_irqrestore(&priv->sta_lock, flags);
  453. return 0;
  454. }
  455. /**
  456. * iwl_update_bcast_station - update broadcast station's LQ command
  457. *
  458. * Only used by iwlagn. Placed here to have all bcast station management
  459. * code together.
  460. */
  461. int iwl_update_bcast_station(struct iwl_priv *priv,
  462. struct iwl_rxon_context *ctx)
  463. {
  464. unsigned long flags;
  465. struct iwl_link_quality_cmd *link_cmd;
  466. u8 sta_id = ctx->bcast_sta_id;
  467. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  468. if (!link_cmd) {
  469. IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
  470. return -ENOMEM;
  471. }
  472. spin_lock_irqsave(&priv->sta_lock, flags);
  473. if (priv->stations[sta_id].lq)
  474. kfree(priv->stations[sta_id].lq);
  475. else
  476. IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
  477. priv->stations[sta_id].lq = link_cmd;
  478. spin_unlock_irqrestore(&priv->sta_lock, flags);
  479. return 0;
  480. }
  481. int iwl_update_bcast_stations(struct iwl_priv *priv)
  482. {
  483. struct iwl_rxon_context *ctx;
  484. int ret = 0;
  485. for_each_context(priv, ctx) {
  486. ret = iwl_update_bcast_station(priv, ctx);
  487. if (ret)
  488. break;
  489. }
  490. return ret;
  491. }
  492. /**
  493. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  494. */
  495. int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  496. {
  497. unsigned long flags;
  498. struct iwl_addsta_cmd sta_cmd;
  499. lockdep_assert_held(&priv->mutex);
  500. /* Remove "disable" flag, to enable Tx for this TID */
  501. spin_lock_irqsave(&priv->sta_lock, flags);
  502. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  503. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  504. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  505. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  506. spin_unlock_irqrestore(&priv->sta_lock, flags);
  507. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  508. }
  509. int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
  510. int tid, u16 ssn)
  511. {
  512. unsigned long flags;
  513. int sta_id;
  514. struct iwl_addsta_cmd sta_cmd;
  515. lockdep_assert_held(&priv->mutex);
  516. sta_id = iwl_sta_id(sta);
  517. if (sta_id == IWL_INVALID_STATION)
  518. return -ENXIO;
  519. spin_lock_irqsave(&priv->sta_lock, flags);
  520. priv->stations[sta_id].sta.station_flags_msk = 0;
  521. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  522. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  523. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  524. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  525. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  526. spin_unlock_irqrestore(&priv->sta_lock, flags);
  527. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  528. }
  529. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
  530. int tid)
  531. {
  532. unsigned long flags;
  533. int sta_id;
  534. struct iwl_addsta_cmd sta_cmd;
  535. lockdep_assert_held(&priv->mutex);
  536. sta_id = iwl_sta_id(sta);
  537. if (sta_id == IWL_INVALID_STATION) {
  538. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  539. return -ENXIO;
  540. }
  541. spin_lock_irqsave(&priv->sta_lock, flags);
  542. priv->stations[sta_id].sta.station_flags_msk = 0;
  543. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  544. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  545. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  546. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  547. spin_unlock_irqrestore(&priv->sta_lock, flags);
  548. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  549. }
  550. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  551. {
  552. unsigned long flags;
  553. spin_lock_irqsave(&priv->sta_lock, flags);
  554. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  555. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  556. priv->stations[sta_id].sta.sta.modify_mask = 0;
  557. priv->stations[sta_id].sta.sleep_tx_count = 0;
  558. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  559. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  560. spin_unlock_irqrestore(&priv->sta_lock, flags);
  561. }
  562. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  563. {
  564. unsigned long flags;
  565. spin_lock_irqsave(&priv->sta_lock, flags);
  566. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  567. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  568. priv->stations[sta_id].sta.sta.modify_mask =
  569. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  570. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  571. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  572. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  573. spin_unlock_irqrestore(&priv->sta_lock, flags);
  574. }
  575. void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
  576. struct ieee80211_vif *vif,
  577. enum sta_notify_cmd cmd,
  578. struct ieee80211_sta *sta)
  579. {
  580. struct iwl_priv *priv = hw->priv;
  581. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  582. int sta_id;
  583. switch (cmd) {
  584. case STA_NOTIFY_SLEEP:
  585. WARN_ON(!sta_priv->client);
  586. sta_priv->asleep = true;
  587. if (atomic_read(&sta_priv->pending_frames) > 0)
  588. ieee80211_sta_block_awake(hw, sta, true);
  589. break;
  590. case STA_NOTIFY_AWAKE:
  591. WARN_ON(!sta_priv->client);
  592. if (!sta_priv->asleep)
  593. break;
  594. sta_priv->asleep = false;
  595. sta_id = iwl_sta_id(sta);
  596. if (sta_id != IWL_INVALID_STATION)
  597. iwl_sta_modify_ps_wake(priv, sta_id);
  598. break;
  599. default:
  600. break;
  601. }
  602. }