iwl-sta.c 39 KB

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