iwl-sta.c 39 KB

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