iwl-sta.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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. * James P. Ketrenos <ipw2100-admin@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 <linux/etherdevice.h>
  31. #include "iwl-dev.h"
  32. #include "iwl-core.h"
  33. #include "iwl-sta.h"
  34. #include "iwl-helpers.h"
  35. #define IWL_STA_DRIVER_ACTIVE 0x1 /* ucode entry is active */
  36. #define IWL_STA_UCODE_ACTIVE 0x2 /* ucode entry is active */
  37. u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  38. {
  39. int i;
  40. int start = 0;
  41. int ret = IWL_INVALID_STATION;
  42. unsigned long flags;
  43. DECLARE_MAC_BUF(mac);
  44. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
  45. (priv->iw_mode == IEEE80211_IF_TYPE_AP))
  46. start = IWL_STA_ID;
  47. if (is_broadcast_ether_addr(addr))
  48. return priv->hw_params.bcast_sta_id;
  49. spin_lock_irqsave(&priv->sta_lock, flags);
  50. for (i = start; i < priv->hw_params.max_stations; i++)
  51. if (priv->stations[i].used &&
  52. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  53. addr))) {
  54. ret = i;
  55. goto out;
  56. }
  57. IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
  58. print_mac(mac, addr), priv->num_stations);
  59. out:
  60. spin_unlock_irqrestore(&priv->sta_lock, flags);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL(iwl_find_station);
  64. int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  65. {
  66. if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
  67. return IWL_AP_ID;
  68. } else {
  69. u8 *da = ieee80211_get_DA(hdr);
  70. return iwl_find_station(priv, da);
  71. }
  72. }
  73. EXPORT_SYMBOL(iwl_get_ra_sta_id);
  74. static int iwl_add_sta_callback(struct iwl_priv *priv,
  75. struct iwl_cmd *cmd, struct sk_buff *skb)
  76. {
  77. struct iwl_rx_packet *res = NULL;
  78. if (!skb) {
  79. IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
  80. return 1;
  81. }
  82. res = (struct iwl_rx_packet *)skb->data;
  83. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  84. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  85. res->hdr.flags);
  86. return 1;
  87. }
  88. switch (res->u.add_sta.status) {
  89. case ADD_STA_SUCCESS_MSK:
  90. /* FIXME: implement iwl_sta_ucode_activate(priv, addr); */
  91. /* fail through */
  92. default:
  93. IWL_DEBUG_HC("Received REPLY_ADD_STA:(0x%08X)\n",
  94. res->u.add_sta.status);
  95. break;
  96. }
  97. /* We didn't cache the SKB; let the caller free it */
  98. return 1;
  99. }
  100. int iwl_send_add_sta(struct iwl_priv *priv,
  101. struct iwl_addsta_cmd *sta, u8 flags)
  102. {
  103. struct iwl_rx_packet *res = NULL;
  104. int ret = 0;
  105. u8 data[sizeof(*sta)];
  106. struct iwl_host_cmd cmd = {
  107. .id = REPLY_ADD_STA,
  108. .meta.flags = flags,
  109. .data = data,
  110. };
  111. if (flags & CMD_ASYNC)
  112. cmd.meta.u.callback = iwl_add_sta_callback;
  113. else
  114. cmd.meta.flags |= CMD_WANT_SKB;
  115. cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  116. ret = iwl_send_cmd(priv, &cmd);
  117. if (ret || (flags & CMD_ASYNC))
  118. return ret;
  119. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  120. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  121. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  122. res->hdr.flags);
  123. ret = -EIO;
  124. }
  125. if (ret == 0) {
  126. switch (res->u.add_sta.status) {
  127. case ADD_STA_SUCCESS_MSK:
  128. IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
  129. break;
  130. default:
  131. ret = -EIO;
  132. IWL_WARNING("REPLY_ADD_STA failed\n");
  133. break;
  134. }
  135. }
  136. priv->alloc_rxb_skb--;
  137. dev_kfree_skb_any(cmd.meta.u.skb);
  138. return ret;
  139. }
  140. EXPORT_SYMBOL(iwl_send_add_sta);
  141. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  142. struct ieee80211_ht_info *sta_ht_inf)
  143. {
  144. __le32 sta_flags;
  145. u8 mimo_ps_mode;
  146. if (!sta_ht_inf || !sta_ht_inf->ht_supported)
  147. goto done;
  148. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
  149. sta_flags = priv->stations[index].sta.station_flags;
  150. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  151. switch (mimo_ps_mode) {
  152. case WLAN_HT_CAP_MIMO_PS_STATIC:
  153. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  154. break;
  155. case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
  156. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  157. break;
  158. case WLAN_HT_CAP_MIMO_PS_DISABLED:
  159. break;
  160. default:
  161. IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
  162. break;
  163. }
  164. sta_flags |= cpu_to_le32(
  165. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  166. sta_flags |= cpu_to_le32(
  167. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  168. if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
  169. sta_flags |= STA_FLG_FAT_EN_MSK;
  170. else
  171. sta_flags &= ~STA_FLG_FAT_EN_MSK;
  172. priv->stations[index].sta.station_flags = sta_flags;
  173. done:
  174. return;
  175. }
  176. /**
  177. * iwl_add_station_flags - Add station to tables in driver and device
  178. */
  179. u8 iwl_add_station_flags(struct iwl_priv *priv, const u8 *addr, int is_ap,
  180. u8 flags, struct ieee80211_ht_info *ht_info)
  181. {
  182. int i;
  183. int index = IWL_INVALID_STATION;
  184. struct iwl_station_entry *station;
  185. unsigned long flags_spin;
  186. DECLARE_MAC_BUF(mac);
  187. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  188. if (is_ap)
  189. index = IWL_AP_ID;
  190. else if (is_broadcast_ether_addr(addr))
  191. index = priv->hw_params.bcast_sta_id;
  192. else
  193. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  194. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  195. addr)) {
  196. index = i;
  197. break;
  198. }
  199. if (!priv->stations[i].used &&
  200. index == IWL_INVALID_STATION)
  201. index = i;
  202. }
  203. /* These two conditions have the same outcome, but keep them separate
  204. since they have different meanings */
  205. if (unlikely(index == IWL_INVALID_STATION)) {
  206. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  207. return index;
  208. }
  209. if (priv->stations[index].used &&
  210. !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
  211. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  212. return index;
  213. }
  214. IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
  215. station = &priv->stations[index];
  216. station->used = 1;
  217. priv->num_stations++;
  218. /* Set up the REPLY_ADD_STA command to send to device */
  219. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  220. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  221. station->sta.mode = 0;
  222. station->sta.sta.sta_id = index;
  223. station->sta.station_flags = 0;
  224. /* BCAST station and IBSS stations do not work in HT mode */
  225. if (index != priv->hw_params.bcast_sta_id &&
  226. priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
  227. iwl_set_ht_add_station(priv, index, ht_info);
  228. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  229. /* Add station to device's station table */
  230. iwl_send_add_sta(priv, &station->sta, flags);
  231. return index;
  232. }
  233. EXPORT_SYMBOL(iwl_add_station_flags);
  234. static int iwl_sta_ucode_deactivate(struct iwl_priv *priv, const char *addr)
  235. {
  236. unsigned long flags;
  237. u8 sta_id;
  238. DECLARE_MAC_BUF(mac);
  239. sta_id = iwl_find_station(priv, addr);
  240. if (sta_id != IWL_INVALID_STATION) {
  241. IWL_DEBUG_ASSOC("Removed STA from Ucode: %s\n",
  242. print_mac(mac, addr));
  243. spin_lock_irqsave(&priv->sta_lock, flags);
  244. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  245. memset(&priv->stations[sta_id], 0,
  246. sizeof(struct iwl_station_entry));
  247. spin_unlock_irqrestore(&priv->sta_lock, flags);
  248. return 0;
  249. }
  250. return -EINVAL;
  251. }
  252. static int iwl_remove_sta_callback(struct iwl_priv *priv,
  253. struct iwl_cmd *cmd, struct sk_buff *skb)
  254. {
  255. struct iwl_rx_packet *res = NULL;
  256. const char *addr = cmd->cmd.rm_sta.addr;
  257. if (!skb) {
  258. IWL_ERROR("Error: Response NULL in REPLY_REMOVE_STA.\n");
  259. return 1;
  260. }
  261. res = (struct iwl_rx_packet *)skb->data;
  262. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  263. IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  264. res->hdr.flags);
  265. return 1;
  266. }
  267. switch (res->u.rem_sta.status) {
  268. case REM_STA_SUCCESS_MSK:
  269. iwl_sta_ucode_deactivate(priv, addr);
  270. break;
  271. default:
  272. break;
  273. }
  274. /* We didn't cache the SKB; let the caller free it */
  275. return 1;
  276. }
  277. static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
  278. u8 flags)
  279. {
  280. struct iwl_rx_packet *res = NULL;
  281. int ret;
  282. struct iwl_rem_sta_cmd rm_sta_cmd;
  283. struct iwl_host_cmd cmd = {
  284. .id = REPLY_REMOVE_STA,
  285. .len = sizeof(struct iwl_rem_sta_cmd),
  286. .meta.flags = flags,
  287. .data = &rm_sta_cmd,
  288. };
  289. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  290. rm_sta_cmd.num_sta = 1;
  291. memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
  292. if (flags & CMD_ASYNC)
  293. cmd.meta.u.callback = iwl_remove_sta_callback;
  294. else
  295. cmd.meta.flags |= CMD_WANT_SKB;
  296. ret = iwl_send_cmd(priv, &cmd);
  297. if (ret || (flags & CMD_ASYNC))
  298. return ret;
  299. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  300. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  301. IWL_ERROR("Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  302. res->hdr.flags);
  303. ret = -EIO;
  304. }
  305. if (!ret) {
  306. switch (res->u.rem_sta.status) {
  307. case REM_STA_SUCCESS_MSK:
  308. iwl_sta_ucode_deactivate(priv, addr);
  309. IWL_DEBUG_ASSOC("REPLY_REMOVE_STA PASSED\n");
  310. break;
  311. default:
  312. ret = -EIO;
  313. IWL_ERROR("REPLY_REMOVE_STA failed\n");
  314. break;
  315. }
  316. }
  317. priv->alloc_rxb_skb--;
  318. dev_kfree_skb_any(cmd.meta.u.skb);
  319. return ret;
  320. }
  321. /**
  322. * iwl_remove_station - Remove driver's knowledge of station.
  323. */
  324. u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  325. {
  326. int index = IWL_INVALID_STATION;
  327. int i;
  328. unsigned long flags;
  329. spin_lock_irqsave(&priv->sta_lock, flags);
  330. if (is_ap)
  331. index = IWL_AP_ID;
  332. else if (is_broadcast_ether_addr(addr))
  333. index = priv->hw_params.bcast_sta_id;
  334. else
  335. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  336. if (priv->stations[i].used &&
  337. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  338. addr)) {
  339. index = i;
  340. break;
  341. }
  342. if (unlikely(index == IWL_INVALID_STATION))
  343. goto out;
  344. if (priv->stations[index].used) {
  345. priv->stations[index].used = 0;
  346. priv->num_stations--;
  347. }
  348. BUG_ON(priv->num_stations < 0);
  349. spin_unlock_irqrestore(&priv->sta_lock, flags);
  350. iwl_send_remove_station(priv, addr, CMD_ASYNC);
  351. return index;
  352. out:
  353. spin_unlock_irqrestore(&priv->sta_lock, flags);
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(iwl_remove_station);
  357. static int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  358. {
  359. int i;
  360. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  361. if (!test_and_set_bit(i, &priv->ucode_key_table))
  362. return i;
  363. return -1;
  364. }
  365. int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  366. {
  367. int i, not_empty = 0;
  368. u8 buff[sizeof(struct iwl_wep_cmd) +
  369. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  370. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  371. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  372. struct iwl_host_cmd cmd = {
  373. .id = REPLY_WEPKEY,
  374. .data = wep_cmd,
  375. .meta.flags = CMD_ASYNC,
  376. };
  377. memset(wep_cmd, 0, cmd_size +
  378. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  379. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  380. wep_cmd->key[i].key_index = i;
  381. if (priv->wep_keys[i].key_size) {
  382. wep_cmd->key[i].key_offset = i;
  383. not_empty = 1;
  384. } else {
  385. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  386. }
  387. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  388. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  389. priv->wep_keys[i].key_size);
  390. }
  391. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  392. wep_cmd->num_keys = WEP_KEYS_MAX;
  393. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  394. cmd.len = cmd_size;
  395. if (not_empty || send_if_empty)
  396. return iwl_send_cmd(priv, &cmd);
  397. else
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
  401. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  402. struct ieee80211_key_conf *keyconf)
  403. {
  404. int ret;
  405. unsigned long flags;
  406. spin_lock_irqsave(&priv->sta_lock, flags);
  407. if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
  408. IWL_ERROR("index %d not used in uCode key table.\n",
  409. keyconf->keyidx);
  410. priv->default_wep_key--;
  411. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  412. ret = iwl_send_static_wepkey_cmd(priv, 1);
  413. IWL_DEBUG_WEP("Remove default WEP key: idx=%d ret=%d\n",
  414. keyconf->keyidx, ret);
  415. spin_unlock_irqrestore(&priv->sta_lock, flags);
  416. return ret;
  417. }
  418. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  419. int iwl_set_default_wep_key(struct iwl_priv *priv,
  420. struct ieee80211_key_conf *keyconf)
  421. {
  422. int ret;
  423. unsigned long flags;
  424. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  425. keyconf->keylen != WEP_KEY_LEN_64) {
  426. IWL_DEBUG_WEP("Bad WEP key length %d\n", keyconf->keylen);
  427. return -EINVAL;
  428. }
  429. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  430. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  431. priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
  432. spin_lock_irqsave(&priv->sta_lock, flags);
  433. priv->default_wep_key++;
  434. if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
  435. IWL_ERROR("index %d already used in uCode key table.\n",
  436. keyconf->keyidx);
  437. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  438. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  439. keyconf->keylen);
  440. ret = iwl_send_static_wepkey_cmd(priv, 0);
  441. IWL_DEBUG_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
  442. keyconf->keylen, keyconf->keyidx, ret);
  443. spin_unlock_irqrestore(&priv->sta_lock, flags);
  444. return ret;
  445. }
  446. EXPORT_SYMBOL(iwl_set_default_wep_key);
  447. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  448. struct ieee80211_key_conf *keyconf,
  449. u8 sta_id)
  450. {
  451. unsigned long flags;
  452. __le16 key_flags = 0;
  453. int ret;
  454. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  455. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  456. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  457. key_flags &= ~STA_KEY_FLG_INVALID;
  458. if (keyconf->keylen == WEP_KEY_LEN_128)
  459. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  460. if (sta_id == priv->hw_params.bcast_sta_id)
  461. key_flags |= STA_KEY_MULTICAST_MSK;
  462. spin_lock_irqsave(&priv->sta_lock, flags);
  463. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  464. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  465. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  466. memcpy(priv->stations[sta_id].keyinfo.key,
  467. keyconf->key, keyconf->keylen);
  468. memcpy(&priv->stations[sta_id].sta.key.key[3],
  469. keyconf->key, keyconf->keylen);
  470. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  471. == STA_KEY_FLG_NO_ENC)
  472. priv->stations[sta_id].sta.key.key_offset =
  473. iwl_get_free_ucode_key_index(priv);
  474. /* else, we are overriding an existing key => no need to allocated room
  475. * in uCode. */
  476. priv->stations[sta_id].sta.key.key_flags = key_flags;
  477. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  478. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  479. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  480. spin_unlock_irqrestore(&priv->sta_lock, flags);
  481. return ret;
  482. }
  483. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  484. struct ieee80211_key_conf *keyconf,
  485. u8 sta_id)
  486. {
  487. unsigned long flags;
  488. __le16 key_flags = 0;
  489. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  490. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  491. key_flags &= ~STA_KEY_FLG_INVALID;
  492. if (sta_id == priv->hw_params.bcast_sta_id)
  493. key_flags |= STA_KEY_MULTICAST_MSK;
  494. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  495. spin_lock_irqsave(&priv->sta_lock, flags);
  496. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  497. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  498. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  499. keyconf->keylen);
  500. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  501. keyconf->keylen);
  502. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  503. == STA_KEY_FLG_NO_ENC)
  504. priv->stations[sta_id].sta.key.key_offset =
  505. iwl_get_free_ucode_key_index(priv);
  506. /* else, we are overriding an existing key => no need to allocated room
  507. * in uCode. */
  508. priv->stations[sta_id].sta.key.key_flags = key_flags;
  509. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  510. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  511. spin_unlock_irqrestore(&priv->sta_lock, flags);
  512. IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
  513. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  514. }
  515. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  516. struct ieee80211_key_conf *keyconf,
  517. u8 sta_id)
  518. {
  519. unsigned long flags;
  520. int ret = 0;
  521. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  522. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  523. spin_lock_irqsave(&priv->sta_lock, flags);
  524. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  525. priv->stations[sta_id].keyinfo.keylen = 16;
  526. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  527. == STA_KEY_FLG_NO_ENC)
  528. priv->stations[sta_id].sta.key.key_offset =
  529. iwl_get_free_ucode_key_index(priv);
  530. /* else, we are overriding an existing key => no need to allocated room
  531. * in uCode. */
  532. /* This copy is acutally not needed: we get the key with each TX */
  533. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  534. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  535. spin_unlock_irqrestore(&priv->sta_lock, flags);
  536. return ret;
  537. }
  538. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  539. struct ieee80211_key_conf *keyconf,
  540. u8 sta_id)
  541. {
  542. unsigned long flags;
  543. int ret = 0;
  544. u16 key_flags;
  545. u8 keyidx;
  546. priv->key_mapping_key--;
  547. spin_lock_irqsave(&priv->sta_lock, flags);
  548. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  549. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  550. IWL_DEBUG_WEP("Remove dynamic key: idx=%d sta=%d\n",
  551. keyconf->keyidx, sta_id);
  552. if (keyconf->keyidx != keyidx) {
  553. /* We need to remove a key with index different that the one
  554. * in the uCode. This means that the key we need to remove has
  555. * been replaced by another one with different index.
  556. * Don't do anything and return ok
  557. */
  558. spin_unlock_irqrestore(&priv->sta_lock, flags);
  559. return 0;
  560. }
  561. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  562. &priv->ucode_key_table))
  563. IWL_ERROR("index %d not used in uCode key table.\n",
  564. priv->stations[sta_id].sta.key.key_offset);
  565. memset(&priv->stations[sta_id].keyinfo, 0,
  566. sizeof(struct iwl_hw_key));
  567. memset(&priv->stations[sta_id].sta.key, 0,
  568. sizeof(struct iwl4965_keyinfo));
  569. priv->stations[sta_id].sta.key.key_flags =
  570. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  571. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  572. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  573. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  574. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  575. spin_unlock_irqrestore(&priv->sta_lock, flags);
  576. return ret;
  577. }
  578. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  579. int iwl_set_dynamic_key(struct iwl_priv *priv,
  580. struct ieee80211_key_conf *keyconf, u8 sta_id)
  581. {
  582. int ret;
  583. priv->key_mapping_key++;
  584. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  585. switch (keyconf->alg) {
  586. case ALG_CCMP:
  587. ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  588. break;
  589. case ALG_TKIP:
  590. ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  591. break;
  592. case ALG_WEP:
  593. ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  594. break;
  595. default:
  596. IWL_ERROR("Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  597. ret = -EINVAL;
  598. }
  599. IWL_DEBUG_WEP("Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  600. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  601. sta_id, ret);
  602. return ret;
  603. }
  604. EXPORT_SYMBOL(iwl_set_dynamic_key);
  605. #ifdef CONFIG_IWLWIFI_DEBUG
  606. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  607. struct iwl_link_quality_cmd *lq)
  608. {
  609. int i;
  610. IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
  611. IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
  612. lq->general_params.single_stream_ant_msk,
  613. lq->general_params.dual_stream_ant_msk);
  614. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  615. IWL_DEBUG_RATE("lq index %d 0x%X\n",
  616. i, lq->rs_table[i].rate_n_flags);
  617. }
  618. #else
  619. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  620. struct iwl_link_quality_cmd *lq)
  621. {
  622. }
  623. #endif
  624. int iwl_send_lq_cmd(struct iwl_priv *priv,
  625. struct iwl_link_quality_cmd *lq, u8 flags)
  626. {
  627. struct iwl_host_cmd cmd = {
  628. .id = REPLY_TX_LINK_QUALITY_CMD,
  629. .len = sizeof(struct iwl_link_quality_cmd),
  630. .meta.flags = flags,
  631. .data = lq,
  632. };
  633. if ((lq->sta_id == 0xFF) &&
  634. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
  635. return -EINVAL;
  636. if (lq->sta_id == 0xFF)
  637. lq->sta_id = IWL_AP_ID;
  638. iwl_dump_lq_cmd(priv,lq);
  639. if (iwl_is_associated(priv) && priv->assoc_station_added &&
  640. priv->lq_mngr.lq_ready)
  641. return iwl_send_cmd(priv, &cmd);
  642. return 0;
  643. }
  644. EXPORT_SYMBOL(iwl_send_lq_cmd);
  645. /**
  646. * iwl_sta_init_lq - Initialize a station's hardware rate table
  647. *
  648. * The uCode's station table contains a table of fallback rates
  649. * for automatic fallback during transmission.
  650. *
  651. * NOTE: This sets up a default set of values. These will be replaced later
  652. * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
  653. * rc80211_simple.
  654. *
  655. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  656. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  657. * which requires station table entry to exist).
  658. */
  659. static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, int is_ap)
  660. {
  661. int i, r;
  662. struct iwl_link_quality_cmd link_cmd = {
  663. .reserved1 = 0,
  664. };
  665. u16 rate_flags;
  666. /* Set up the rate scaling to start at selected rate, fall back
  667. * all the way down to 1M in IEEE order, and then spin on 1M */
  668. if (is_ap)
  669. r = IWL_RATE_54M_INDEX;
  670. else if (priv->band == IEEE80211_BAND_5GHZ)
  671. r = IWL_RATE_6M_INDEX;
  672. else
  673. r = IWL_RATE_1M_INDEX;
  674. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  675. rate_flags = 0;
  676. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  677. rate_flags |= RATE_MCS_CCK_MSK;
  678. /* Use Tx antenna B only */
  679. rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
  680. link_cmd.rs_table[i].rate_n_flags =
  681. iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  682. r = iwl4965_get_prev_ieee_rate(r);
  683. }
  684. link_cmd.general_params.single_stream_ant_msk = 2;
  685. link_cmd.general_params.dual_stream_ant_msk = 3;
  686. link_cmd.agg_params.agg_dis_start_th = 3;
  687. link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
  688. /* Update the rate scaling for control frame Tx to AP */
  689. link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
  690. iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
  691. sizeof(link_cmd), &link_cmd, NULL);
  692. }
  693. /**
  694. * iwl_rxon_add_station - add station into station table.
  695. *
  696. * there is only one AP station with id= IWL_AP_ID
  697. * NOTE: mutex must be held before calling this fnction
  698. */
  699. int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  700. {
  701. u8 sta_id;
  702. /* Add station to device's station table */
  703. struct ieee80211_conf *conf = &priv->hw->conf;
  704. struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
  705. if ((is_ap) &&
  706. (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
  707. (priv->iw_mode == IEEE80211_IF_TYPE_STA))
  708. sta_id = iwl_add_station_flags(priv, addr, is_ap,
  709. 0, cur_ht_config);
  710. else
  711. sta_id = iwl_add_station_flags(priv, addr, is_ap,
  712. 0, NULL);
  713. /* Set up default rate scaling table in device's station table */
  714. iwl_sta_init_lq(priv, addr, is_ap);
  715. return sta_id;
  716. }
  717. EXPORT_SYMBOL(iwl_rxon_add_station);
  718. /**
  719. * iwl_get_sta_id - Find station's index within station table
  720. *
  721. * If new IBSS station, create new entry in station table
  722. */
  723. int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  724. {
  725. int sta_id;
  726. u16 fc = le16_to_cpu(hdr->frame_control);
  727. DECLARE_MAC_BUF(mac);
  728. /* If this frame is broadcast or management, use broadcast station id */
  729. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  730. is_multicast_ether_addr(hdr->addr1))
  731. return priv->hw_params.bcast_sta_id;
  732. switch (priv->iw_mode) {
  733. /* If we are a client station in a BSS network, use the special
  734. * AP station entry (that's the only station we communicate with) */
  735. case IEEE80211_IF_TYPE_STA:
  736. return IWL_AP_ID;
  737. /* If we are an AP, then find the station, or use BCAST */
  738. case IEEE80211_IF_TYPE_AP:
  739. sta_id = iwl_find_station(priv, hdr->addr1);
  740. if (sta_id != IWL_INVALID_STATION)
  741. return sta_id;
  742. return priv->hw_params.bcast_sta_id;
  743. /* If this frame is going out to an IBSS network, find the station,
  744. * or create a new station table entry */
  745. case IEEE80211_IF_TYPE_IBSS:
  746. sta_id = iwl_find_station(priv, hdr->addr1);
  747. if (sta_id != IWL_INVALID_STATION)
  748. return sta_id;
  749. /* Create new station table entry */
  750. sta_id = iwl_add_station_flags(priv, hdr->addr1,
  751. 0, CMD_ASYNC, NULL);
  752. if (sta_id != IWL_INVALID_STATION)
  753. return sta_id;
  754. IWL_DEBUG_DROP("Station %s not in station map. "
  755. "Defaulting to broadcast...\n",
  756. print_mac(mac, hdr->addr1));
  757. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  758. return priv->hw_params.bcast_sta_id;
  759. default:
  760. IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
  761. return priv->hw_params.bcast_sta_id;
  762. }
  763. }
  764. EXPORT_SYMBOL(iwl_get_sta_id);
  765. /**
  766. * iwl_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
  767. */
  768. void iwl_sta_modify_enable_tid_tx(struct iwl_priv *priv, int sta_id, int tid)
  769. {
  770. unsigned long flags;
  771. /* Remove "disable" flag, to enable Tx for this TID */
  772. spin_lock_irqsave(&priv->sta_lock, flags);
  773. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  774. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  775. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  776. spin_unlock_irqrestore(&priv->sta_lock, flags);
  777. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  778. }
  779. EXPORT_SYMBOL(iwl_sta_modify_enable_tid_tx);