iwl-sta.c 41 KB

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