iwl-sta.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <net/mac80211.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/sched.h>
  32. #include <linux/lockdep.h>
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-trans.h"
  37. /* priv->sta_lock must be held */
  38. static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
  39. {
  40. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
  41. IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
  42. sta_id, priv->stations[sta_id].sta.sta.addr);
  43. if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
  44. IWL_DEBUG_ASSOC(priv,
  45. "STA id %u addr %pM already present in uCode (according to driver)\n",
  46. sta_id, priv->stations[sta_id].sta.sta.addr);
  47. } else {
  48. priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
  49. IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
  50. sta_id, priv->stations[sta_id].sta.sta.addr);
  51. }
  52. }
  53. static int iwl_process_add_sta_resp(struct iwl_priv *priv,
  54. struct iwl_addsta_cmd *addsta,
  55. struct iwl_rx_packet *pkt,
  56. bool sync)
  57. {
  58. u8 sta_id = addsta->sta.sta_id;
  59. unsigned long flags;
  60. int ret = -EIO;
  61. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  62. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  63. pkt->hdr.flags);
  64. return ret;
  65. }
  66. IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
  67. sta_id);
  68. spin_lock_irqsave(&priv->sta_lock, flags);
  69. switch (pkt->u.add_sta.status) {
  70. case ADD_STA_SUCCESS_MSK:
  71. IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
  72. iwl_sta_ucode_activate(priv, sta_id);
  73. ret = 0;
  74. break;
  75. case ADD_STA_NO_ROOM_IN_TABLE:
  76. IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
  77. sta_id);
  78. break;
  79. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  80. IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
  81. sta_id);
  82. break;
  83. case ADD_STA_MODIFY_NON_EXIST_STA:
  84. IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
  85. sta_id);
  86. break;
  87. default:
  88. IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
  89. pkt->u.add_sta.status);
  90. break;
  91. }
  92. IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
  93. priv->stations[sta_id].sta.mode ==
  94. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  95. sta_id, priv->stations[sta_id].sta.sta.addr);
  96. /*
  97. * XXX: The MAC address in the command buffer is often changed from
  98. * the original sent to the device. That is, the MAC address
  99. * written to the command buffer often is not the same MAC address
  100. * read from the command buffer when the command returns. This
  101. * issue has not yet been resolved and this debugging is left to
  102. * observe the problem.
  103. */
  104. IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
  105. priv->stations[sta_id].sta.mode ==
  106. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  107. addsta->sta.addr);
  108. spin_unlock_irqrestore(&priv->sta_lock, flags);
  109. return ret;
  110. }
  111. static void iwl_add_sta_callback(struct iwl_priv *priv,
  112. struct iwl_device_cmd *cmd,
  113. struct iwl_rx_packet *pkt)
  114. {
  115. struct iwl_addsta_cmd *addsta =
  116. (struct iwl_addsta_cmd *)cmd->cmd.payload;
  117. iwl_process_add_sta_resp(priv, addsta, pkt, false);
  118. }
  119. int iwl_send_add_sta(struct iwl_priv *priv,
  120. struct iwl_addsta_cmd *sta, u8 flags)
  121. {
  122. struct iwl_rx_packet *pkt = NULL;
  123. int ret = 0;
  124. u8 data[sizeof(*sta)];
  125. struct iwl_host_cmd cmd = {
  126. .id = REPLY_ADD_STA,
  127. .flags = flags,
  128. .data = { data, },
  129. };
  130. u8 sta_id __maybe_unused = sta->sta.sta_id;
  131. IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
  132. sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
  133. if (flags & CMD_ASYNC)
  134. cmd.callback = iwl_add_sta_callback;
  135. else {
  136. cmd.flags |= CMD_WANT_SKB;
  137. might_sleep();
  138. }
  139. cmd.len[0] = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  140. ret = trans_send_cmd(priv, &cmd);
  141. if (ret || (flags & CMD_ASYNC))
  142. return ret;
  143. if (ret == 0) {
  144. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  145. ret = iwl_process_add_sta_resp(priv, sta, pkt, true);
  146. }
  147. iwl_free_pages(priv, cmd.reply_page);
  148. return ret;
  149. }
  150. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  151. struct ieee80211_sta *sta,
  152. struct iwl_rxon_context *ctx)
  153. {
  154. struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
  155. __le32 sta_flags;
  156. u8 mimo_ps_mode;
  157. if (!sta || !sta_ht_inf->ht_supported)
  158. goto done;
  159. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  160. IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
  161. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
  162. "static" :
  163. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
  164. "dynamic" : "disabled");
  165. sta_flags = priv->stations[index].sta.station_flags;
  166. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  167. switch (mimo_ps_mode) {
  168. case WLAN_HT_CAP_SM_PS_STATIC:
  169. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  170. break;
  171. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  172. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  173. break;
  174. case WLAN_HT_CAP_SM_PS_DISABLED:
  175. break;
  176. default:
  177. IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
  178. break;
  179. }
  180. sta_flags |= cpu_to_le32(
  181. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  182. sta_flags |= cpu_to_le32(
  183. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  184. if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
  185. sta_flags |= STA_FLG_HT40_EN_MSK;
  186. else
  187. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  188. priv->stations[index].sta.station_flags = sta_flags;
  189. done:
  190. return;
  191. }
  192. /**
  193. * iwl_prep_station - Prepare station information for addition
  194. *
  195. * should be called with sta_lock held
  196. */
  197. u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  198. const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
  199. {
  200. struct iwl_station_entry *station;
  201. int i;
  202. u8 sta_id = IWL_INVALID_STATION;
  203. if (is_ap)
  204. sta_id = ctx->ap_sta_id;
  205. else if (is_broadcast_ether_addr(addr))
  206. sta_id = ctx->bcast_sta_id;
  207. else
  208. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  209. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  210. addr)) {
  211. sta_id = i;
  212. break;
  213. }
  214. if (!priv->stations[i].used &&
  215. sta_id == IWL_INVALID_STATION)
  216. sta_id = i;
  217. }
  218. /*
  219. * These two conditions have the same outcome, but keep them
  220. * separate
  221. */
  222. if (unlikely(sta_id == IWL_INVALID_STATION))
  223. return sta_id;
  224. /*
  225. * uCode is not able to deal with multiple requests to add a
  226. * station. Keep track if one is in progress so that we do not send
  227. * another.
  228. */
  229. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  230. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  231. sta_id);
  232. return sta_id;
  233. }
  234. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  235. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
  236. !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
  237. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  238. sta_id, addr);
  239. return sta_id;
  240. }
  241. station = &priv->stations[sta_id];
  242. station->used = IWL_STA_DRIVER_ACTIVE;
  243. IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
  244. sta_id, addr);
  245. priv->num_stations++;
  246. /* Set up the REPLY_ADD_STA command to send to device */
  247. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  248. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  249. station->sta.mode = 0;
  250. station->sta.sta.sta_id = sta_id;
  251. station->sta.station_flags = ctx->station_flags;
  252. station->ctxid = ctx->ctxid;
  253. if (sta) {
  254. struct iwl_station_priv_common *sta_priv;
  255. sta_priv = (void *)sta->drv_priv;
  256. sta_priv->ctx = ctx;
  257. }
  258. /*
  259. * OK to call unconditionally, since local stations (IBSS BSSID
  260. * STA and broadcast STA) pass in a NULL sta, and mac80211
  261. * doesn't allow HT IBSS.
  262. */
  263. iwl_set_ht_add_station(priv, sta_id, sta, ctx);
  264. return sta_id;
  265. }
  266. #define STA_WAIT_TIMEOUT (HZ/2)
  267. /**
  268. * iwl_add_station_common -
  269. */
  270. int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  271. const u8 *addr, bool is_ap,
  272. struct ieee80211_sta *sta, u8 *sta_id_r)
  273. {
  274. unsigned long flags_spin;
  275. int ret = 0;
  276. u8 sta_id;
  277. struct iwl_addsta_cmd sta_cmd;
  278. *sta_id_r = 0;
  279. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  280. sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
  281. if (sta_id == IWL_INVALID_STATION) {
  282. IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
  283. addr);
  284. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  285. return -EINVAL;
  286. }
  287. /*
  288. * uCode is not able to deal with multiple requests to add a
  289. * station. Keep track if one is in progress so that we do not send
  290. * another.
  291. */
  292. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  293. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  294. sta_id);
  295. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  296. return -EEXIST;
  297. }
  298. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  299. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  300. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  301. sta_id, addr);
  302. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  303. return -EEXIST;
  304. }
  305. priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
  306. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  307. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  308. /* Add station to device's station table */
  309. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  310. if (ret) {
  311. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  312. IWL_ERR(priv, "Adding station %pM failed.\n",
  313. priv->stations[sta_id].sta.sta.addr);
  314. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  315. priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  316. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  317. }
  318. *sta_id_r = sta_id;
  319. return ret;
  320. }
  321. /**
  322. * iwl_sta_ucode_deactivate - deactivate ucode status for a station
  323. *
  324. * priv->sta_lock must be held
  325. */
  326. static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
  327. {
  328. /* Ucode must be active and driver must be non active */
  329. if ((priv->stations[sta_id].used &
  330. (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
  331. IWL_ERR(priv, "removed non active STA %u\n", sta_id);
  332. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  333. memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
  334. IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
  335. }
  336. static int iwl_send_remove_station(struct iwl_priv *priv,
  337. const u8 *addr, int sta_id,
  338. bool temporary)
  339. {
  340. struct iwl_rx_packet *pkt;
  341. int ret;
  342. unsigned long flags_spin;
  343. struct iwl_rem_sta_cmd rm_sta_cmd;
  344. struct iwl_host_cmd cmd = {
  345. .id = REPLY_REMOVE_STA,
  346. .len = { sizeof(struct iwl_rem_sta_cmd), },
  347. .flags = CMD_SYNC,
  348. .data = { &rm_sta_cmd, },
  349. };
  350. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  351. rm_sta_cmd.num_sta = 1;
  352. memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
  353. cmd.flags |= CMD_WANT_SKB;
  354. ret = trans_send_cmd(priv, &cmd);
  355. if (ret)
  356. return ret;
  357. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  358. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  359. IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  360. pkt->hdr.flags);
  361. ret = -EIO;
  362. }
  363. if (!ret) {
  364. switch (pkt->u.rem_sta.status) {
  365. case REM_STA_SUCCESS_MSK:
  366. if (!temporary) {
  367. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  368. iwl_sta_ucode_deactivate(priv, sta_id);
  369. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  370. }
  371. IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
  372. break;
  373. default:
  374. ret = -EIO;
  375. IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
  376. break;
  377. }
  378. }
  379. iwl_free_pages(priv, cmd.reply_page);
  380. return ret;
  381. }
  382. /**
  383. * iwl_remove_station - Remove driver's knowledge of station.
  384. */
  385. int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
  386. const u8 *addr)
  387. {
  388. unsigned long flags;
  389. if (!iwl_is_ready(priv)) {
  390. IWL_DEBUG_INFO(priv,
  391. "Unable to remove station %pM, device not ready.\n",
  392. addr);
  393. /*
  394. * It is typical for stations to be removed when we are
  395. * going down. Return success since device will be down
  396. * soon anyway
  397. */
  398. return 0;
  399. }
  400. IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
  401. sta_id, addr);
  402. if (WARN_ON(sta_id == IWL_INVALID_STATION))
  403. return -EINVAL;
  404. spin_lock_irqsave(&priv->sta_lock, flags);
  405. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  406. IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
  407. addr);
  408. goto out_err;
  409. }
  410. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  411. IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
  412. addr);
  413. goto out_err;
  414. }
  415. if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
  416. kfree(priv->stations[sta_id].lq);
  417. priv->stations[sta_id].lq = NULL;
  418. }
  419. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  420. priv->num_stations--;
  421. if (WARN_ON(priv->num_stations < 0))
  422. priv->num_stations = 0;
  423. spin_unlock_irqrestore(&priv->sta_lock, flags);
  424. return iwl_send_remove_station(priv, addr, sta_id, false);
  425. out_err:
  426. spin_unlock_irqrestore(&priv->sta_lock, flags);
  427. return -EINVAL;
  428. }
  429. /**
  430. * iwl_clear_ucode_stations - clear ucode station table bits
  431. *
  432. * This function clears all the bits in the driver indicating
  433. * which stations are active in the ucode. Call when something
  434. * other than explicit station management would cause this in
  435. * the ucode, e.g. unassociated RXON.
  436. */
  437. void iwl_clear_ucode_stations(struct iwl_priv *priv,
  438. struct iwl_rxon_context *ctx)
  439. {
  440. int i;
  441. unsigned long flags_spin;
  442. bool cleared = false;
  443. IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
  444. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  445. for (i = 0; i < priv->hw_params.max_stations; i++) {
  446. if (ctx && ctx->ctxid != priv->stations[i].ctxid)
  447. continue;
  448. if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
  449. IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
  450. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  451. cleared = true;
  452. }
  453. }
  454. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  455. if (!cleared)
  456. IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
  457. }
  458. /**
  459. * iwl_restore_stations() - Restore driver known stations to device
  460. *
  461. * All stations considered active by driver, but not present in ucode, is
  462. * restored.
  463. *
  464. * Function sleeps.
  465. */
  466. void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  467. {
  468. struct iwl_addsta_cmd sta_cmd;
  469. struct iwl_link_quality_cmd lq;
  470. unsigned long flags_spin;
  471. int i;
  472. bool found = false;
  473. int ret;
  474. bool send_lq;
  475. if (!iwl_is_ready(priv)) {
  476. IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
  477. return;
  478. }
  479. IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
  480. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  481. for (i = 0; i < priv->hw_params.max_stations; i++) {
  482. if (ctx->ctxid != priv->stations[i].ctxid)
  483. continue;
  484. if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
  485. !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
  486. IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
  487. priv->stations[i].sta.sta.addr);
  488. priv->stations[i].sta.mode = 0;
  489. priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
  490. found = true;
  491. }
  492. }
  493. for (i = 0; i < priv->hw_params.max_stations; i++) {
  494. if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
  495. memcpy(&sta_cmd, &priv->stations[i].sta,
  496. sizeof(struct iwl_addsta_cmd));
  497. send_lq = false;
  498. if (priv->stations[i].lq) {
  499. memcpy(&lq, priv->stations[i].lq,
  500. sizeof(struct iwl_link_quality_cmd));
  501. send_lq = true;
  502. }
  503. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  504. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  505. if (ret) {
  506. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  507. IWL_ERR(priv, "Adding station %pM failed.\n",
  508. priv->stations[i].sta.sta.addr);
  509. priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
  510. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  511. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  512. }
  513. /*
  514. * Rate scaling has already been initialized, send
  515. * current LQ command
  516. */
  517. if (send_lq)
  518. iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
  519. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  520. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  521. }
  522. }
  523. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  524. if (!found)
  525. IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
  526. else
  527. IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
  528. }
  529. void iwl_reprogram_ap_sta(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  530. {
  531. unsigned long flags;
  532. int sta_id = ctx->ap_sta_id;
  533. int ret;
  534. struct iwl_addsta_cmd sta_cmd;
  535. struct iwl_link_quality_cmd lq;
  536. bool active;
  537. spin_lock_irqsave(&priv->sta_lock, flags);
  538. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  539. spin_unlock_irqrestore(&priv->sta_lock, flags);
  540. return;
  541. }
  542. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
  543. sta_cmd.mode = 0;
  544. memcpy(&lq, priv->stations[sta_id].lq, sizeof(lq));
  545. active = priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE;
  546. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  547. spin_unlock_irqrestore(&priv->sta_lock, flags);
  548. if (active) {
  549. ret = iwl_send_remove_station(
  550. priv, priv->stations[sta_id].sta.sta.addr,
  551. sta_id, true);
  552. if (ret)
  553. IWL_ERR(priv, "failed to remove STA %pM (%d)\n",
  554. priv->stations[sta_id].sta.sta.addr, ret);
  555. }
  556. spin_lock_irqsave(&priv->sta_lock, flags);
  557. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  558. spin_unlock_irqrestore(&priv->sta_lock, flags);
  559. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  560. if (ret)
  561. IWL_ERR(priv, "failed to re-add STA %pM (%d)\n",
  562. priv->stations[sta_id].sta.sta.addr, ret);
  563. iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
  564. }
  565. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  566. {
  567. int i;
  568. for (i = 0; i < priv->sta_key_max_num; i++)
  569. if (!test_and_set_bit(i, &priv->ucode_key_table))
  570. return i;
  571. return WEP_INVALID_OFFSET;
  572. }
  573. void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
  574. {
  575. unsigned long flags;
  576. int i;
  577. spin_lock_irqsave(&priv->sta_lock, flags);
  578. for (i = 0; i < priv->hw_params.max_stations; i++) {
  579. if (!(priv->stations[i].used & IWL_STA_BCAST))
  580. continue;
  581. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  582. priv->num_stations--;
  583. if (WARN_ON(priv->num_stations < 0))
  584. priv->num_stations = 0;
  585. kfree(priv->stations[i].lq);
  586. priv->stations[i].lq = NULL;
  587. }
  588. spin_unlock_irqrestore(&priv->sta_lock, flags);
  589. }
  590. #ifdef CONFIG_IWLWIFI_DEBUG
  591. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  592. struct iwl_link_quality_cmd *lq)
  593. {
  594. int i;
  595. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  596. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  597. lq->general_params.single_stream_ant_msk,
  598. lq->general_params.dual_stream_ant_msk);
  599. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  600. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  601. i, lq->rs_table[i].rate_n_flags);
  602. }
  603. #else
  604. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  605. struct iwl_link_quality_cmd *lq)
  606. {
  607. }
  608. #endif
  609. /**
  610. * is_lq_table_valid() - Test one aspect of LQ cmd for validity
  611. *
  612. * It sometimes happens when a HT rate has been in use and we
  613. * loose connectivity with AP then mac80211 will first tell us that the
  614. * current channel is not HT anymore before removing the station. In such a
  615. * scenario the RXON flags will be updated to indicate we are not
  616. * communicating HT anymore, but the LQ command may still contain HT rates.
  617. * Test for this to prevent driver from sending LQ command between the time
  618. * RXON flags are updated and when LQ command is updated.
  619. */
  620. static bool is_lq_table_valid(struct iwl_priv *priv,
  621. struct iwl_rxon_context *ctx,
  622. struct iwl_link_quality_cmd *lq)
  623. {
  624. int i;
  625. if (ctx->ht.enabled)
  626. return true;
  627. IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
  628. ctx->active.channel);
  629. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  630. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
  631. IWL_DEBUG_INFO(priv,
  632. "index %d of LQ expects HT channel\n",
  633. i);
  634. return false;
  635. }
  636. }
  637. return true;
  638. }
  639. /**
  640. * iwl_send_lq_cmd() - Send link quality command
  641. * @init: This command is sent as part of station initialization right
  642. * after station has been added.
  643. *
  644. * The link quality command is sent as the last step of station creation.
  645. * This is the special case in which init is set and we call a callback in
  646. * this case to clear the state indicating that station creation is in
  647. * progress.
  648. */
  649. int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  650. struct iwl_link_quality_cmd *lq, u8 flags, bool init)
  651. {
  652. int ret = 0;
  653. unsigned long flags_spin;
  654. struct iwl_host_cmd cmd = {
  655. .id = REPLY_TX_LINK_QUALITY_CMD,
  656. .len = { sizeof(struct iwl_link_quality_cmd), },
  657. .flags = flags,
  658. .data = { lq, },
  659. };
  660. if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
  661. return -EINVAL;
  662. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  663. if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  664. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  665. return -EINVAL;
  666. }
  667. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  668. iwl_dump_lq_cmd(priv, lq);
  669. if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
  670. return -EINVAL;
  671. if (is_lq_table_valid(priv, ctx, lq))
  672. ret = trans_send_cmd(priv, &cmd);
  673. else
  674. ret = -EINVAL;
  675. if (cmd.flags & CMD_ASYNC)
  676. return ret;
  677. if (init) {
  678. IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
  679. lq->sta_id);
  680. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  681. priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  682. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  683. }
  684. return ret;
  685. }
  686. int iwl_mac_sta_remove(struct ieee80211_hw *hw,
  687. struct ieee80211_vif *vif,
  688. struct ieee80211_sta *sta)
  689. {
  690. struct iwl_priv *priv = hw->priv;
  691. struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
  692. int ret;
  693. IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
  694. sta->addr);
  695. mutex_lock(&priv->mutex);
  696. IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
  697. sta->addr);
  698. ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
  699. if (ret)
  700. IWL_ERR(priv, "Error removing station %pM\n",
  701. sta->addr);
  702. mutex_unlock(&priv->mutex);
  703. return ret;
  704. }