iwl-sta.c 27 KB

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