iwl-sta.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 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 <linux/etherdevice.h>
  31. #include <linux/sched.h>
  32. #include "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  36. {
  37. int i;
  38. int start = 0;
  39. int ret = IWL_INVALID_STATION;
  40. unsigned long flags;
  41. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
  42. (priv->iw_mode == NL80211_IFTYPE_AP))
  43. start = IWL_STA_ID;
  44. if (is_broadcast_ether_addr(addr))
  45. return priv->hw_params.bcast_sta_id;
  46. spin_lock_irqsave(&priv->sta_lock, flags);
  47. for (i = start; i < priv->hw_params.max_stations; i++)
  48. if (priv->stations[i].used &&
  49. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  50. addr))) {
  51. ret = i;
  52. goto out;
  53. }
  54. IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
  55. addr, priv->num_stations);
  56. out:
  57. /*
  58. * It may be possible that more commands interacting with stations
  59. * arrive before we completed processing the adding of
  60. * station
  61. */
  62. if (ret != IWL_INVALID_STATION &&
  63. (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
  64. ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
  65. (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
  66. IWL_ERR(priv, "Requested station info for sta %d before ready.\n",
  67. ret);
  68. ret = IWL_INVALID_STATION;
  69. }
  70. spin_unlock_irqrestore(&priv->sta_lock, flags);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL(iwl_find_station);
  74. int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  75. {
  76. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  77. return IWL_AP_ID;
  78. } else {
  79. u8 *da = ieee80211_get_DA(hdr);
  80. return iwl_find_station(priv, da);
  81. }
  82. }
  83. EXPORT_SYMBOL(iwl_get_ra_sta_id);
  84. /* priv->sta_lock must be held */
  85. static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
  86. {
  87. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
  88. IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
  89. sta_id, priv->stations[sta_id].sta.sta.addr);
  90. if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
  91. IWL_DEBUG_ASSOC(priv,
  92. "STA id %u addr %pM already present in uCode (according to driver)\n",
  93. sta_id, priv->stations[sta_id].sta.sta.addr);
  94. } else {
  95. priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
  96. IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
  97. sta_id, priv->stations[sta_id].sta.sta.addr);
  98. }
  99. }
  100. static void iwl_process_add_sta_resp(struct iwl_priv *priv,
  101. struct iwl_addsta_cmd *addsta,
  102. struct iwl_rx_packet *pkt,
  103. bool sync)
  104. {
  105. u8 sta_id = addsta->sta.sta_id;
  106. unsigned long flags;
  107. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  108. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  109. pkt->hdr.flags);
  110. return;
  111. }
  112. IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
  113. sta_id);
  114. spin_lock_irqsave(&priv->sta_lock, flags);
  115. switch (pkt->u.add_sta.status) {
  116. case ADD_STA_SUCCESS_MSK:
  117. IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
  118. iwl_sta_ucode_activate(priv, sta_id);
  119. break;
  120. case ADD_STA_NO_ROOM_IN_TABLE:
  121. IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
  122. sta_id);
  123. break;
  124. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  125. IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
  126. sta_id);
  127. break;
  128. case ADD_STA_MODIFY_NON_EXIST_STA:
  129. IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
  130. sta_id);
  131. break;
  132. default:
  133. IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
  134. pkt->u.add_sta.status);
  135. break;
  136. }
  137. IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
  138. priv->stations[sta_id].sta.mode ==
  139. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  140. sta_id, priv->stations[sta_id].sta.sta.addr);
  141. /*
  142. * XXX: The MAC address in the command buffer is often changed from
  143. * the original sent to the device. That is, the MAC address
  144. * written to the command buffer often is not the same MAC adress
  145. * read from the command buffer when the command returns. This
  146. * issue has not yet been resolved and this debugging is left to
  147. * observe the problem.
  148. */
  149. IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
  150. priv->stations[sta_id].sta.mode ==
  151. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  152. addsta->sta.addr);
  153. spin_unlock_irqrestore(&priv->sta_lock, flags);
  154. }
  155. static void iwl_add_sta_callback(struct iwl_priv *priv,
  156. struct iwl_device_cmd *cmd,
  157. struct iwl_rx_packet *pkt)
  158. {
  159. struct iwl_addsta_cmd *addsta =
  160. (struct iwl_addsta_cmd *)cmd->cmd.payload;
  161. iwl_process_add_sta_resp(priv, addsta, pkt, false);
  162. }
  163. int iwl_send_add_sta(struct iwl_priv *priv,
  164. struct iwl_addsta_cmd *sta, u8 flags)
  165. {
  166. struct iwl_rx_packet *pkt = NULL;
  167. int ret = 0;
  168. u8 data[sizeof(*sta)];
  169. struct iwl_host_cmd cmd = {
  170. .id = REPLY_ADD_STA,
  171. .flags = flags,
  172. .data = data,
  173. };
  174. u8 sta_id __maybe_unused = sta->sta.sta_id;
  175. IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
  176. sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
  177. if (flags & CMD_ASYNC)
  178. cmd.callback = iwl_add_sta_callback;
  179. else
  180. cmd.flags |= CMD_WANT_SKB;
  181. cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  182. ret = iwl_send_cmd(priv, &cmd);
  183. if (ret || (flags & CMD_ASYNC))
  184. return ret;
  185. if (ret == 0) {
  186. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  187. iwl_process_add_sta_resp(priv, sta, pkt, true);
  188. }
  189. iwl_free_pages(priv, cmd.reply_page);
  190. return ret;
  191. }
  192. EXPORT_SYMBOL(iwl_send_add_sta);
  193. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  194. struct ieee80211_sta_ht_cap *sta_ht_inf)
  195. {
  196. __le32 sta_flags;
  197. u8 mimo_ps_mode;
  198. if (!sta_ht_inf || !sta_ht_inf->ht_supported)
  199. goto done;
  200. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  201. IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
  202. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
  203. "static" :
  204. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
  205. "dynamic" : "disabled");
  206. sta_flags = priv->stations[index].sta.station_flags;
  207. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  208. switch (mimo_ps_mode) {
  209. case WLAN_HT_CAP_SM_PS_STATIC:
  210. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  211. break;
  212. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  213. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  214. break;
  215. case WLAN_HT_CAP_SM_PS_DISABLED:
  216. break;
  217. default:
  218. IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
  219. break;
  220. }
  221. sta_flags |= cpu_to_le32(
  222. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  223. sta_flags |= cpu_to_le32(
  224. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  225. if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
  226. sta_flags |= STA_FLG_HT40_EN_MSK;
  227. else
  228. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  229. priv->stations[index].sta.station_flags = sta_flags;
  230. done:
  231. return;
  232. }
  233. /**
  234. * iwl_prep_station - Prepare station information for addition
  235. *
  236. * should be called with sta_lock held
  237. */
  238. static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
  239. bool is_ap,
  240. struct ieee80211_sta_ht_cap *ht_info)
  241. {
  242. struct iwl_station_entry *station;
  243. int i;
  244. u8 sta_id = IWL_INVALID_STATION;
  245. u16 rate;
  246. if (is_ap)
  247. sta_id = IWL_AP_ID;
  248. else if (is_broadcast_ether_addr(addr))
  249. sta_id = priv->hw_params.bcast_sta_id;
  250. else
  251. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  252. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  253. addr)) {
  254. sta_id = i;
  255. break;
  256. }
  257. if (!priv->stations[i].used &&
  258. sta_id == IWL_INVALID_STATION)
  259. sta_id = i;
  260. }
  261. /*
  262. * These two conditions have the same outcome, but keep them
  263. * separate
  264. */
  265. if (unlikely(sta_id == IWL_INVALID_STATION))
  266. return sta_id;
  267. /*
  268. * uCode is not able to deal with multiple requests to add a
  269. * station. Keep track if one is in progress so that we do not send
  270. * another.
  271. */
  272. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  273. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  274. sta_id);
  275. return sta_id;
  276. }
  277. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  278. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
  279. !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
  280. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  281. sta_id, addr);
  282. return sta_id;
  283. }
  284. station = &priv->stations[sta_id];
  285. station->used = IWL_STA_DRIVER_ACTIVE;
  286. IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
  287. sta_id, addr);
  288. priv->num_stations++;
  289. /* Set up the REPLY_ADD_STA command to send to device */
  290. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  291. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  292. station->sta.mode = 0;
  293. station->sta.sta.sta_id = sta_id;
  294. station->sta.station_flags = 0;
  295. /* BCAST station and IBSS stations do not work in HT mode */
  296. if (sta_id != priv->hw_params.bcast_sta_id &&
  297. priv->iw_mode != NL80211_IFTYPE_ADHOC)
  298. iwl_set_ht_add_station(priv, sta_id, ht_info);
  299. /* 3945 only */
  300. rate = (priv->band == IEEE80211_BAND_5GHZ) ?
  301. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
  302. /* Turn on both antennas for the station... */
  303. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  304. return sta_id;
  305. }
  306. #define STA_WAIT_TIMEOUT (HZ/2)
  307. /**
  308. * iwl_add_station_common -
  309. */
  310. int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
  311. bool is_ap,
  312. struct ieee80211_sta_ht_cap *ht_info,
  313. u8 *sta_id_r)
  314. {
  315. struct iwl_station_entry *station;
  316. unsigned long flags_spin;
  317. int ret = 0;
  318. u8 sta_id;
  319. *sta_id_r = 0;
  320. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  321. sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
  322. if (sta_id == IWL_INVALID_STATION) {
  323. IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
  324. addr);
  325. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  326. return -EINVAL;
  327. }
  328. /*
  329. * uCode is not able to deal with multiple requests to add a
  330. * station. Keep track if one is in progress so that we do not send
  331. * another.
  332. */
  333. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  334. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  335. sta_id);
  336. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  337. return -EEXIST;
  338. }
  339. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  340. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  341. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  342. sta_id, addr);
  343. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  344. return -EEXIST;
  345. }
  346. priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
  347. station = &priv->stations[sta_id];
  348. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  349. /* Add station to device's station table */
  350. ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
  351. if (ret) {
  352. IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
  353. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  354. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  355. priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  356. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  357. }
  358. *sta_id_r = sta_id;
  359. return ret;
  360. }
  361. EXPORT_SYMBOL(iwl_add_station_common);
  362. static struct iwl_link_quality_cmd *iwl_sta_init_lq(struct iwl_priv *priv,
  363. const u8 *addr, bool is_ap)
  364. {
  365. int i, r;
  366. struct iwl_link_quality_cmd *link_cmd;
  367. u32 rate_flags;
  368. int ret = 0;
  369. link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
  370. if (!link_cmd) {
  371. IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
  372. return NULL;
  373. }
  374. /* Set up the rate scaling to start at selected rate, fall back
  375. * all the way down to 1M in IEEE order, and then spin on 1M */
  376. if (is_ap)
  377. r = IWL_RATE_54M_INDEX;
  378. else if (priv->band == IEEE80211_BAND_5GHZ)
  379. r = IWL_RATE_6M_INDEX;
  380. else
  381. r = IWL_RATE_1M_INDEX;
  382. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  383. rate_flags = 0;
  384. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  385. rate_flags |= RATE_MCS_CCK_MSK;
  386. rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
  387. RATE_MCS_ANT_POS;
  388. link_cmd->rs_table[i].rate_n_flags =
  389. iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  390. r = iwl_get_prev_ieee_rate(r);
  391. }
  392. link_cmd->general_params.single_stream_ant_msk =
  393. first_antenna(priv->hw_params.valid_tx_ant);
  394. link_cmd->general_params.dual_stream_ant_msk =
  395. priv->hw_params.valid_tx_ant &
  396. ~first_antenna(priv->hw_params.valid_tx_ant);
  397. if (!link_cmd->general_params.dual_stream_ant_msk) {
  398. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  399. } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
  400. link_cmd->general_params.dual_stream_ant_msk =
  401. priv->hw_params.valid_tx_ant;
  402. }
  403. link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  404. link_cmd->agg_params.agg_time_limit =
  405. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  406. /* Update the rate scaling for control frame Tx to AP */
  407. link_cmd->sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
  408. ret = iwl_send_lq_cmd(priv, link_cmd, CMD_SYNC, true);
  409. if (ret)
  410. IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
  411. return link_cmd;
  412. }
  413. /*
  414. * iwl_add_local_stations - Add stations not requested by mac80211
  415. *
  416. * This will be either the broadcast station or the bssid station needed by
  417. * ad-hoc.
  418. *
  419. * Function sleeps.
  420. */
  421. int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs)
  422. {
  423. int ret;
  424. u8 sta_id;
  425. struct iwl_link_quality_cmd *link_cmd;
  426. unsigned long flags;
  427. ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
  428. if (ret) {
  429. IWL_ERR(priv, "Unable to add station %pM\n", addr);
  430. return ret;
  431. }
  432. spin_lock_irqsave(&priv->sta_lock, flags);
  433. priv->stations[sta_id].used |= IWL_STA_LOCAL;
  434. spin_unlock_irqrestore(&priv->sta_lock, flags);
  435. if (init_rs) {
  436. /* Set up default rate scaling table in device's station table */
  437. link_cmd = iwl_sta_init_lq(priv, addr, false);
  438. if (!link_cmd) {
  439. IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
  440. addr);
  441. return -ENOMEM;
  442. }
  443. spin_lock_irqsave(&priv->sta_lock, flags);
  444. priv->stations[sta_id].lq = link_cmd;
  445. spin_unlock_irqrestore(&priv->sta_lock, flags);
  446. }
  447. return 0;
  448. }
  449. EXPORT_SYMBOL(iwl_add_local_station);
  450. /**
  451. * iwl_sta_ucode_deactivate - deactivate ucode status for a station
  452. *
  453. * priv->sta_lock must be held
  454. */
  455. static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
  456. {
  457. /* Ucode must be active and driver must be non active */
  458. if ((priv->stations[sta_id].used &
  459. (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
  460. IWL_ERR(priv, "removed non active STA %u\n", sta_id);
  461. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  462. memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
  463. IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
  464. }
  465. static int iwl_send_remove_station(struct iwl_priv *priv,
  466. struct iwl_station_entry *station)
  467. {
  468. struct iwl_rx_packet *pkt;
  469. int ret;
  470. unsigned long flags_spin;
  471. struct iwl_rem_sta_cmd rm_sta_cmd;
  472. struct iwl_host_cmd cmd = {
  473. .id = REPLY_REMOVE_STA,
  474. .len = sizeof(struct iwl_rem_sta_cmd),
  475. .flags = CMD_SYNC,
  476. .data = &rm_sta_cmd,
  477. };
  478. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  479. rm_sta_cmd.num_sta = 1;
  480. memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
  481. cmd.flags |= CMD_WANT_SKB;
  482. ret = iwl_send_cmd(priv, &cmd);
  483. if (ret)
  484. return ret;
  485. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  486. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  487. IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  488. pkt->hdr.flags);
  489. ret = -EIO;
  490. }
  491. if (!ret) {
  492. switch (pkt->u.rem_sta.status) {
  493. case REM_STA_SUCCESS_MSK:
  494. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  495. iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
  496. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  497. IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
  498. break;
  499. default:
  500. ret = -EIO;
  501. IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
  502. break;
  503. }
  504. }
  505. iwl_free_pages(priv, cmd.reply_page);
  506. return ret;
  507. }
  508. /**
  509. * iwl_remove_station - Remove driver's knowledge of station.
  510. */
  511. int iwl_remove_station(struct iwl_priv *priv, const u8 *addr)
  512. {
  513. int sta_id = IWL_INVALID_STATION;
  514. int i, ret = -EINVAL;
  515. unsigned long flags;
  516. bool is_ap = priv->iw_mode == NL80211_IFTYPE_STATION;
  517. struct iwl_station_entry *station;
  518. if (!iwl_is_ready(priv)) {
  519. IWL_DEBUG_INFO(priv,
  520. "Unable to remove station %pM, device not ready.\n",
  521. addr);
  522. /*
  523. * It is typical for stations to be removed when we are
  524. * going down. Return success since device will be down
  525. * soon anyway
  526. */
  527. return 0;
  528. }
  529. spin_lock_irqsave(&priv->sta_lock, flags);
  530. if (is_ap)
  531. sta_id = IWL_AP_ID;
  532. else
  533. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  534. if (priv->stations[i].used &&
  535. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  536. addr)) {
  537. sta_id = i;
  538. break;
  539. }
  540. if (unlikely(sta_id == IWL_INVALID_STATION))
  541. goto out;
  542. IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
  543. sta_id, addr);
  544. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  545. IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
  546. addr);
  547. goto out;
  548. }
  549. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  550. IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
  551. addr);
  552. goto out;
  553. }
  554. if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
  555. kfree(priv->stations[sta_id].lq);
  556. priv->stations[sta_id].lq = NULL;
  557. }
  558. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  559. priv->num_stations--;
  560. BUG_ON(priv->num_stations < 0);
  561. station = &priv->stations[sta_id];
  562. spin_unlock_irqrestore(&priv->sta_lock, flags);
  563. ret = iwl_send_remove_station(priv, station);
  564. return ret;
  565. out:
  566. spin_unlock_irqrestore(&priv->sta_lock, flags);
  567. return ret;
  568. }
  569. EXPORT_SYMBOL_GPL(iwl_remove_station);
  570. /**
  571. * iwl_clear_ucode_stations() - clear entire station table driver and/or ucode
  572. * @priv:
  573. * @force: If set then the uCode station table needs to be cleared here. If
  574. * not set then the uCode station table has already been cleared,
  575. * for example after sending it a RXON command without ASSOC bit
  576. * set, and we just need to change driver state here.
  577. */
  578. void iwl_clear_ucode_stations(struct iwl_priv *priv, bool force)
  579. {
  580. int i;
  581. unsigned long flags_spin;
  582. bool cleared = false;
  583. IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver%s\n",
  584. force ? " and ucode" : "");
  585. if (force) {
  586. if (!iwl_is_ready(priv)) {
  587. /*
  588. * If device is not ready at this point the station
  589. * table is likely already empty (uCode not ready
  590. * to receive station requests) or will soon be
  591. * due to interface going down.
  592. */
  593. IWL_DEBUG_INFO(priv, "Unable to remove stations from device - device not ready\n");
  594. } else {
  595. iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL);
  596. }
  597. }
  598. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  599. if (force) {
  600. IWL_DEBUG_INFO(priv, "Clearing all station information in driver\n");
  601. /*
  602. * The station entry contains a link to the LQ command. For
  603. * all stations managed by mac80211 this memory will be
  604. * managed by it also. For local stations (broadcast and
  605. * bssid station when in adhoc mode) we need to maintain
  606. * this lq command separately. This memory is created when
  607. * these stations are added.
  608. */
  609. for (i = 0; i < priv->hw_params.max_stations; i++) {
  610. if (priv->stations[i].used & IWL_STA_LOCAL) {
  611. kfree(priv->stations[i].lq);
  612. priv->stations[i].lq = NULL;
  613. }
  614. }
  615. priv->num_stations = 0;
  616. memset(priv->stations, 0, sizeof(priv->stations));
  617. cleared = true;
  618. } else {
  619. for (i = 0; i < priv->hw_params.max_stations; i++) {
  620. if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
  621. IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
  622. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  623. cleared = true;
  624. }
  625. }
  626. }
  627. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  628. if (!cleared)
  629. IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
  630. }
  631. EXPORT_SYMBOL(iwl_clear_ucode_stations);
  632. /**
  633. * iwl_restore_stations() - Restore driver known stations to device
  634. *
  635. * All stations considered active by driver, but not present in ucode, is
  636. * restored.
  637. *
  638. * Function sleeps.
  639. */
  640. void iwl_restore_stations(struct iwl_priv *priv)
  641. {
  642. struct iwl_station_entry *station;
  643. unsigned long flags_spin;
  644. int i;
  645. bool found = false;
  646. int ret;
  647. if (!iwl_is_ready(priv)) {
  648. IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
  649. return;
  650. }
  651. IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
  652. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  653. for (i = 0; i < priv->hw_params.max_stations; i++) {
  654. if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
  655. !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
  656. IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
  657. priv->stations[i].sta.sta.addr);
  658. priv->stations[i].sta.mode = 0;
  659. priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
  660. found = true;
  661. }
  662. }
  663. for (i = 0; i < priv->hw_params.max_stations; i++) {
  664. if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
  665. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  666. station = &priv->stations[i];
  667. ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
  668. if (ret) {
  669. IWL_ERR(priv, "Adding station %pM failed.\n",
  670. station->sta.sta.addr);
  671. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  672. priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
  673. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  674. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  675. }
  676. /*
  677. * Rate scaling has already been initialized, send
  678. * current LQ command
  679. */
  680. if (station->lq)
  681. iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
  682. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  683. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  684. }
  685. }
  686. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  687. if (!found)
  688. IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
  689. else
  690. IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
  691. }
  692. EXPORT_SYMBOL(iwl_restore_stations);
  693. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  694. {
  695. int i;
  696. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  697. if (!test_and_set_bit(i, &priv->ucode_key_table))
  698. return i;
  699. return WEP_INVALID_OFFSET;
  700. }
  701. EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
  702. static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  703. {
  704. int i, not_empty = 0;
  705. u8 buff[sizeof(struct iwl_wep_cmd) +
  706. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  707. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  708. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  709. struct iwl_host_cmd cmd = {
  710. .id = REPLY_WEPKEY,
  711. .data = wep_cmd,
  712. .flags = CMD_SYNC,
  713. };
  714. might_sleep();
  715. memset(wep_cmd, 0, cmd_size +
  716. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  717. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  718. wep_cmd->key[i].key_index = i;
  719. if (priv->wep_keys[i].key_size) {
  720. wep_cmd->key[i].key_offset = i;
  721. not_empty = 1;
  722. } else {
  723. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  724. }
  725. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  726. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  727. priv->wep_keys[i].key_size);
  728. }
  729. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  730. wep_cmd->num_keys = WEP_KEYS_MAX;
  731. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  732. cmd.len = cmd_size;
  733. if (not_empty || send_if_empty)
  734. return iwl_send_cmd(priv, &cmd);
  735. else
  736. return 0;
  737. }
  738. int iwl_restore_default_wep_keys(struct iwl_priv *priv)
  739. {
  740. WARN_ON(!mutex_is_locked(&priv->mutex));
  741. return iwl_send_static_wepkey_cmd(priv, 0);
  742. }
  743. EXPORT_SYMBOL(iwl_restore_default_wep_keys);
  744. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  745. struct ieee80211_key_conf *keyconf)
  746. {
  747. int ret;
  748. WARN_ON(!mutex_is_locked(&priv->mutex));
  749. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  750. keyconf->keyidx);
  751. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  752. if (iwl_is_rfkill(priv)) {
  753. IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  754. /* but keys in device are clear anyway so return success */
  755. return 0;
  756. }
  757. ret = iwl_send_static_wepkey_cmd(priv, 1);
  758. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  759. keyconf->keyidx, ret);
  760. return ret;
  761. }
  762. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  763. int iwl_set_default_wep_key(struct iwl_priv *priv,
  764. struct ieee80211_key_conf *keyconf)
  765. {
  766. int ret;
  767. WARN_ON(!mutex_is_locked(&priv->mutex));
  768. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  769. keyconf->keylen != WEP_KEY_LEN_64) {
  770. IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
  771. return -EINVAL;
  772. }
  773. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  774. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  775. priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
  776. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  777. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  778. keyconf->keylen);
  779. ret = iwl_send_static_wepkey_cmd(priv, 0);
  780. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  781. keyconf->keylen, keyconf->keyidx, ret);
  782. return ret;
  783. }
  784. EXPORT_SYMBOL(iwl_set_default_wep_key);
  785. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  786. struct ieee80211_key_conf *keyconf,
  787. u8 sta_id)
  788. {
  789. unsigned long flags;
  790. __le16 key_flags = 0;
  791. int ret;
  792. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  793. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  794. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  795. key_flags &= ~STA_KEY_FLG_INVALID;
  796. if (keyconf->keylen == WEP_KEY_LEN_128)
  797. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  798. if (sta_id == priv->hw_params.bcast_sta_id)
  799. key_flags |= STA_KEY_MULTICAST_MSK;
  800. spin_lock_irqsave(&priv->sta_lock, flags);
  801. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  802. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  803. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  804. memcpy(priv->stations[sta_id].keyinfo.key,
  805. keyconf->key, keyconf->keylen);
  806. memcpy(&priv->stations[sta_id].sta.key.key[3],
  807. keyconf->key, keyconf->keylen);
  808. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  809. == STA_KEY_FLG_NO_ENC)
  810. priv->stations[sta_id].sta.key.key_offset =
  811. iwl_get_free_ucode_key_index(priv);
  812. /* else, we are overriding an existing key => no need to allocated room
  813. * in uCode. */
  814. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  815. "no space for a new key");
  816. priv->stations[sta_id].sta.key.key_flags = key_flags;
  817. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  818. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  819. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  820. spin_unlock_irqrestore(&priv->sta_lock, flags);
  821. return ret;
  822. }
  823. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  824. struct ieee80211_key_conf *keyconf,
  825. u8 sta_id)
  826. {
  827. unsigned long flags;
  828. __le16 key_flags = 0;
  829. int ret;
  830. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  831. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  832. key_flags &= ~STA_KEY_FLG_INVALID;
  833. if (sta_id == priv->hw_params.bcast_sta_id)
  834. key_flags |= STA_KEY_MULTICAST_MSK;
  835. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  836. spin_lock_irqsave(&priv->sta_lock, flags);
  837. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  838. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  839. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  840. keyconf->keylen);
  841. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  842. keyconf->keylen);
  843. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  844. == STA_KEY_FLG_NO_ENC)
  845. priv->stations[sta_id].sta.key.key_offset =
  846. iwl_get_free_ucode_key_index(priv);
  847. /* else, we are overriding an existing key => no need to allocated room
  848. * in uCode. */
  849. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  850. "no space for a new key");
  851. priv->stations[sta_id].sta.key.key_flags = key_flags;
  852. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  853. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  854. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  855. spin_unlock_irqrestore(&priv->sta_lock, flags);
  856. return ret;
  857. }
  858. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  859. struct ieee80211_key_conf *keyconf,
  860. u8 sta_id)
  861. {
  862. unsigned long flags;
  863. int ret = 0;
  864. __le16 key_flags = 0;
  865. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  866. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  867. key_flags &= ~STA_KEY_FLG_INVALID;
  868. if (sta_id == priv->hw_params.bcast_sta_id)
  869. key_flags |= STA_KEY_MULTICAST_MSK;
  870. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  871. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  872. spin_lock_irqsave(&priv->sta_lock, flags);
  873. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  874. priv->stations[sta_id].keyinfo.keylen = 16;
  875. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  876. == STA_KEY_FLG_NO_ENC)
  877. priv->stations[sta_id].sta.key.key_offset =
  878. iwl_get_free_ucode_key_index(priv);
  879. /* else, we are overriding an existing key => no need to allocated room
  880. * in uCode. */
  881. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  882. "no space for a new key");
  883. priv->stations[sta_id].sta.key.key_flags = key_flags;
  884. /* This copy is acutally not needed: we get the key with each TX */
  885. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  886. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  887. spin_unlock_irqrestore(&priv->sta_lock, flags);
  888. return ret;
  889. }
  890. void iwl_update_tkip_key(struct iwl_priv *priv,
  891. struct ieee80211_key_conf *keyconf,
  892. const u8 *addr, u32 iv32, u16 *phase1key)
  893. {
  894. u8 sta_id = IWL_INVALID_STATION;
  895. unsigned long flags;
  896. int i;
  897. sta_id = iwl_find_station(priv, addr);
  898. if (sta_id == IWL_INVALID_STATION) {
  899. IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
  900. addr);
  901. return;
  902. }
  903. if (iwl_scan_cancel(priv)) {
  904. /* cancel scan failed, just live w/ bad key and rely
  905. briefly on SW decryption */
  906. return;
  907. }
  908. spin_lock_irqsave(&priv->sta_lock, flags);
  909. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  910. for (i = 0; i < 5; i++)
  911. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  912. cpu_to_le16(phase1key[i]);
  913. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  914. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  915. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  916. spin_unlock_irqrestore(&priv->sta_lock, flags);
  917. }
  918. EXPORT_SYMBOL(iwl_update_tkip_key);
  919. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  920. struct ieee80211_key_conf *keyconf,
  921. u8 sta_id)
  922. {
  923. unsigned long flags;
  924. int ret = 0;
  925. u16 key_flags;
  926. u8 keyidx;
  927. priv->key_mapping_key--;
  928. spin_lock_irqsave(&priv->sta_lock, flags);
  929. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  930. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  931. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  932. keyconf->keyidx, sta_id);
  933. if (keyconf->keyidx != keyidx) {
  934. /* We need to remove a key with index different that the one
  935. * in the uCode. This means that the key we need to remove has
  936. * been replaced by another one with different index.
  937. * Don't do anything and return ok
  938. */
  939. spin_unlock_irqrestore(&priv->sta_lock, flags);
  940. return 0;
  941. }
  942. if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  943. IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
  944. keyconf->keyidx, key_flags);
  945. spin_unlock_irqrestore(&priv->sta_lock, flags);
  946. return 0;
  947. }
  948. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  949. &priv->ucode_key_table))
  950. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  951. priv->stations[sta_id].sta.key.key_offset);
  952. memset(&priv->stations[sta_id].keyinfo, 0,
  953. sizeof(struct iwl_hw_key));
  954. memset(&priv->stations[sta_id].sta.key, 0,
  955. sizeof(struct iwl4965_keyinfo));
  956. priv->stations[sta_id].sta.key.key_flags =
  957. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  958. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  959. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  960. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  961. if (iwl_is_rfkill(priv)) {
  962. IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
  963. spin_unlock_irqrestore(&priv->sta_lock, flags);
  964. return 0;
  965. }
  966. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  967. spin_unlock_irqrestore(&priv->sta_lock, flags);
  968. return ret;
  969. }
  970. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  971. int iwl_set_dynamic_key(struct iwl_priv *priv,
  972. struct ieee80211_key_conf *keyconf, u8 sta_id)
  973. {
  974. int ret;
  975. priv->key_mapping_key++;
  976. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  977. switch (keyconf->alg) {
  978. case ALG_CCMP:
  979. ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  980. break;
  981. case ALG_TKIP:
  982. ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  983. break;
  984. case ALG_WEP:
  985. ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  986. break;
  987. default:
  988. IWL_ERR(priv,
  989. "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  990. ret = -EINVAL;
  991. }
  992. IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  993. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  994. sta_id, ret);
  995. return ret;
  996. }
  997. EXPORT_SYMBOL(iwl_set_dynamic_key);
  998. #ifdef CONFIG_IWLWIFI_DEBUG
  999. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  1000. struct iwl_link_quality_cmd *lq)
  1001. {
  1002. int i;
  1003. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  1004. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  1005. lq->general_params.single_stream_ant_msk,
  1006. lq->general_params.dual_stream_ant_msk);
  1007. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  1008. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  1009. i, lq->rs_table[i].rate_n_flags);
  1010. }
  1011. #else
  1012. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  1013. struct iwl_link_quality_cmd *lq)
  1014. {
  1015. }
  1016. #endif
  1017. /**
  1018. * iwl_send_lq_cmd() - Send link quality command
  1019. * @init: This command is sent as part of station initialization right
  1020. * after station has been added.
  1021. *
  1022. * The link quality command is sent as the last step of station creation.
  1023. * This is the special case in which init is set and we call a callback in
  1024. * this case to clear the state indicating that station creation is in
  1025. * progress.
  1026. */
  1027. int iwl_send_lq_cmd(struct iwl_priv *priv,
  1028. struct iwl_link_quality_cmd *lq, u8 flags, bool init)
  1029. {
  1030. int ret = 0;
  1031. unsigned long flags_spin;
  1032. struct iwl_host_cmd cmd = {
  1033. .id = REPLY_TX_LINK_QUALITY_CMD,
  1034. .len = sizeof(struct iwl_link_quality_cmd),
  1035. .flags = flags,
  1036. .data = lq,
  1037. };
  1038. if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
  1039. return -EINVAL;
  1040. iwl_dump_lq_cmd(priv, lq);
  1041. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  1042. ret = iwl_send_cmd(priv, &cmd);
  1043. if (cmd.flags & CMD_ASYNC)
  1044. return ret;
  1045. if (init) {
  1046. IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
  1047. lq->sta_id);
  1048. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  1049. priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  1050. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  1051. }
  1052. return ret;
  1053. }
  1054. EXPORT_SYMBOL(iwl_send_lq_cmd);
  1055. /**
  1056. * iwl_add_bcast_station - add broadcast station into station table.
  1057. */
  1058. int iwl_add_bcast_station(struct iwl_priv *priv)
  1059. {
  1060. IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
  1061. return iwl_add_local_station(priv, iwl_bcast_addr, true);
  1062. }
  1063. EXPORT_SYMBOL(iwl_add_bcast_station);
  1064. /**
  1065. * iwl3945_add_bcast_station - add broadcast station into station table.
  1066. */
  1067. int iwl3945_add_bcast_station(struct iwl_priv *priv)
  1068. {
  1069. int ret;
  1070. IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
  1071. ret = iwl_add_local_station(priv, iwl_bcast_addr, false);
  1072. /*
  1073. * It is assumed that when station is added more initialization
  1074. * needs to be done, but for 3945 it is not the case and we can
  1075. * just release station table access right here.
  1076. */
  1077. priv->stations[priv->hw_params.bcast_sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  1078. return ret;
  1079. }
  1080. EXPORT_SYMBOL(iwl3945_add_bcast_station);
  1081. /**
  1082. * iwl_get_sta_id - Find station's index within station table
  1083. *
  1084. * If new IBSS station, create new entry in station table
  1085. */
  1086. int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  1087. {
  1088. int sta_id;
  1089. __le16 fc = hdr->frame_control;
  1090. /* If this frame is broadcast or management, use broadcast station id */
  1091. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
  1092. return priv->hw_params.bcast_sta_id;
  1093. switch (priv->iw_mode) {
  1094. /* If we are a client station in a BSS network, use the special
  1095. * AP station entry (that's the only station we communicate with) */
  1096. case NL80211_IFTYPE_STATION:
  1097. /*
  1098. * If addition of station not complete yet, which means
  1099. * that rate scaling has not been initialized, then return
  1100. * the broadcast station.
  1101. */
  1102. if (!(priv->stations[IWL_AP_ID].used & IWL_STA_UCODE_ACTIVE))
  1103. return priv->hw_params.bcast_sta_id;
  1104. return IWL_AP_ID;
  1105. /* If we are an AP, then find the station, or use BCAST */
  1106. case NL80211_IFTYPE_AP:
  1107. sta_id = iwl_find_station(priv, hdr->addr1);
  1108. if (sta_id != IWL_INVALID_STATION)
  1109. return sta_id;
  1110. return priv->hw_params.bcast_sta_id;
  1111. /* If this frame is going out to an IBSS network, find the station,
  1112. * or create a new station table entry */
  1113. case NL80211_IFTYPE_ADHOC:
  1114. sta_id = iwl_find_station(priv, hdr->addr1);
  1115. if (sta_id != IWL_INVALID_STATION)
  1116. return sta_id;
  1117. IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
  1118. "Defaulting to broadcast...\n",
  1119. hdr->addr1);
  1120. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  1121. return priv->hw_params.bcast_sta_id;
  1122. default:
  1123. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  1124. priv->iw_mode);
  1125. return priv->hw_params.bcast_sta_id;
  1126. }
  1127. }
  1128. EXPORT_SYMBOL(iwl_get_sta_id);
  1129. /**
  1130. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1131. */
  1132. void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  1133. {
  1134. unsigned long flags;
  1135. /* Remove "disable" flag, to enable Tx for this TID */
  1136. spin_lock_irqsave(&priv->sta_lock, flags);
  1137. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1138. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1139. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1140. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1141. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1142. }
  1143. EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
  1144. int iwl_sta_rx_agg_start(struct iwl_priv *priv,
  1145. const u8 *addr, int tid, u16 ssn)
  1146. {
  1147. unsigned long flags;
  1148. int sta_id;
  1149. sta_id = iwl_find_station(priv, addr);
  1150. if (sta_id == IWL_INVALID_STATION)
  1151. return -ENXIO;
  1152. spin_lock_irqsave(&priv->sta_lock, flags);
  1153. priv->stations[sta_id].sta.station_flags_msk = 0;
  1154. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1155. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1156. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1157. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1158. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1159. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1160. CMD_ASYNC);
  1161. }
  1162. EXPORT_SYMBOL(iwl_sta_rx_agg_start);
  1163. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
  1164. {
  1165. unsigned long flags;
  1166. int sta_id;
  1167. sta_id = iwl_find_station(priv, addr);
  1168. if (sta_id == IWL_INVALID_STATION) {
  1169. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1170. return -ENXIO;
  1171. }
  1172. spin_lock_irqsave(&priv->sta_lock, flags);
  1173. priv->stations[sta_id].sta.station_flags_msk = 0;
  1174. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1175. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1176. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1177. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1178. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1179. CMD_ASYNC);
  1180. }
  1181. EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
  1182. void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1183. {
  1184. unsigned long flags;
  1185. spin_lock_irqsave(&priv->sta_lock, flags);
  1186. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1187. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1188. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1189. priv->stations[sta_id].sta.sleep_tx_count = 0;
  1190. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1191. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1192. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1193. }
  1194. EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
  1195. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1196. {
  1197. unsigned long flags;
  1198. spin_lock_irqsave(&priv->sta_lock, flags);
  1199. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1200. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1201. priv->stations[sta_id].sta.sta.modify_mask =
  1202. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1203. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1204. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1205. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1206. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1207. }
  1208. EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
  1209. int iwl_mac_sta_remove(struct ieee80211_hw *hw,
  1210. struct ieee80211_vif *vif,
  1211. struct ieee80211_sta *sta)
  1212. {
  1213. int ret;
  1214. struct iwl_priv *priv = hw->priv;
  1215. IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
  1216. sta->addr);
  1217. ret = iwl_remove_station(priv, sta->addr);
  1218. if (ret)
  1219. IWL_ERR(priv, "Error removing station %pM\n",
  1220. sta->addr);
  1221. return ret;
  1222. }
  1223. EXPORT_SYMBOL(iwl_mac_sta_remove);