iwl-sta.c 24 KB

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