iwl-agn-sta.c 21 KB

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