iwl-sta.c 40 KB

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