iwl-agn-sta.c 38 KB

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