iwl-sta.c 23 KB

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