iwl-sta.c 23 KB

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