sta.c 39 KB

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