iwl-sta.c 23 KB

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