sta.c 39 KB

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