iwl-agn-sta.c 39 KB

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