iwl-sta.c 23 KB

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