iwl-sta.c 21 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. /* il->sta_lock must be held */
  37. static void il_sta_ucode_activate(struct il_priv *il, u8 sta_id)
  38. {
  39. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE))
  40. IL_ERR(
  41. "ACTIVATE a non DRIVER active station id %u addr %pM\n",
  42. sta_id, il->stations[sta_id].sta.sta.addr);
  43. if (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) {
  44. D_ASSOC(
  45. "STA id %u addr %pM already present"
  46. " in uCode (according to driver)\n",
  47. sta_id, il->stations[sta_id].sta.sta.addr);
  48. } else {
  49. il->stations[sta_id].used |= IL_STA_UCODE_ACTIVE;
  50. D_ASSOC("Added STA id %u addr %pM to uCode\n",
  51. sta_id, il->stations[sta_id].sta.sta.addr);
  52. }
  53. }
  54. static int il_process_add_sta_resp(struct il_priv *il,
  55. struct il_addsta_cmd *addsta,
  56. struct il_rx_pkt *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 & IL_CMD_FAILED_MSK) {
  63. IL_ERR("Bad return from C_ADD_STA (0x%08X)\n",
  64. pkt->hdr.flags);
  65. return ret;
  66. }
  67. D_INFO("Processing response for adding station %u\n",
  68. sta_id);
  69. spin_lock_irqsave(&il->sta_lock, flags);
  70. switch (pkt->u.add_sta.status) {
  71. case ADD_STA_SUCCESS_MSK:
  72. D_INFO("C_ADD_STA PASSED\n");
  73. il_sta_ucode_activate(il, sta_id);
  74. ret = 0;
  75. break;
  76. case ADD_STA_NO_ROOM_IN_TBL:
  77. IL_ERR("Adding station %d failed, no room in table.\n",
  78. sta_id);
  79. break;
  80. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  81. IL_ERR(
  82. "Adding station %d failed, no block ack resource.\n",
  83. sta_id);
  84. break;
  85. case ADD_STA_MODIFY_NON_EXIST_STA:
  86. IL_ERR("Attempting to modify non-existing station %d\n",
  87. sta_id);
  88. break;
  89. default:
  90. D_ASSOC("Received C_ADD_STA:(0x%08X)\n",
  91. pkt->u.add_sta.status);
  92. break;
  93. }
  94. D_INFO("%s station id %u addr %pM\n",
  95. il->stations[sta_id].sta.mode ==
  96. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  97. sta_id, il->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. D_INFO("%s station according to cmd buffer %pM\n",
  107. il->stations[sta_id].sta.mode ==
  108. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  109. addsta->sta.addr);
  110. spin_unlock_irqrestore(&il->sta_lock, flags);
  111. return ret;
  112. }
  113. static void il_add_sta_callback(struct il_priv *il,
  114. struct il_device_cmd *cmd,
  115. struct il_rx_pkt *pkt)
  116. {
  117. struct il_addsta_cmd *addsta =
  118. (struct il_addsta_cmd *)cmd->cmd.payload;
  119. il_process_add_sta_resp(il, addsta, pkt, false);
  120. }
  121. int il_send_add_sta(struct il_priv *il,
  122. struct il_addsta_cmd *sta, u8 flags)
  123. {
  124. struct il_rx_pkt *pkt = NULL;
  125. int ret = 0;
  126. u8 data[sizeof(*sta)];
  127. struct il_host_cmd cmd = {
  128. .id = C_ADD_STA,
  129. .flags = flags,
  130. .data = data,
  131. };
  132. u8 sta_id __maybe_unused = sta->sta.sta_id;
  133. D_INFO("Adding sta %u (%pM) %ssynchronously\n",
  134. sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
  135. if (flags & CMD_ASYNC)
  136. cmd.callback = il_add_sta_callback;
  137. else {
  138. cmd.flags |= CMD_WANT_SKB;
  139. might_sleep();
  140. }
  141. cmd.len = il->cfg->ops->utils->build_addsta_hcmd(sta, data);
  142. ret = il_send_cmd(il, &cmd);
  143. if (ret || (flags & CMD_ASYNC))
  144. return ret;
  145. if (ret == 0) {
  146. pkt = (struct il_rx_pkt *)cmd.reply_page;
  147. ret = il_process_add_sta_resp(il, sta, pkt, true);
  148. }
  149. il_free_pages(il, cmd.reply_page);
  150. return ret;
  151. }
  152. EXPORT_SYMBOL(il_send_add_sta);
  153. static void il_set_ht_add_station(struct il_priv *il, u8 idx,
  154. struct ieee80211_sta *sta,
  155. struct il_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. D_ASSOC("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 = il->stations[idx].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. IL_WARN("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 (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
  188. sta_flags |= STA_FLG_HT40_EN_MSK;
  189. else
  190. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  191. il->stations[idx].sta.station_flags = sta_flags;
  192. done:
  193. return;
  194. }
  195. /**
  196. * il_prep_station - Prepare station information for addition
  197. *
  198. * should be called with sta_lock held
  199. */
  200. u8 il_prep_station(struct il_priv *il, struct il_rxon_context *ctx,
  201. const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
  202. {
  203. struct il_station_entry *station;
  204. int i;
  205. u8 sta_id = IL_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 = IL_STA_ID; i < il->hw_params.max_stations; i++) {
  213. if (!compare_ether_addr(il->stations[i].sta.sta.addr,
  214. addr)) {
  215. sta_id = i;
  216. break;
  217. }
  218. if (!il->stations[i].used &&
  219. sta_id == IL_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 == IL_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 (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  234. D_INFO(
  235. "STA %d already in process of being added.\n",
  236. sta_id);
  237. return sta_id;
  238. }
  239. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  240. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) &&
  241. !compare_ether_addr(il->stations[sta_id].sta.sta.addr, addr)) {
  242. D_ASSOC(
  243. "STA %d (%pM) already added, not adding again.\n",
  244. sta_id, addr);
  245. return sta_id;
  246. }
  247. station = &il->stations[sta_id];
  248. station->used = IL_STA_DRIVER_ACTIVE;
  249. D_ASSOC("Add STA to driver ID %d: %pM\n",
  250. sta_id, addr);
  251. il->num_stations++;
  252. /* Set up the C_ADD_STA command to send to device */
  253. memset(&station->sta, 0, sizeof(struct il_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 il_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. il_set_ht_add_station(il, sta_id, sta, ctx);
  270. /* 3945 only */
  271. rate = (il->band == IEEE80211_BAND_5GHZ) ?
  272. RATE_6M_PLCP : 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(il_prep_station);
  278. #define STA_WAIT_TIMEOUT (HZ/2)
  279. /**
  280. * il_add_station_common -
  281. */
  282. int
  283. il_add_station_common(struct il_priv *il,
  284. struct il_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 il_addsta_cmd sta_cmd;
  292. *sta_id_r = 0;
  293. spin_lock_irqsave(&il->sta_lock, flags_spin);
  294. sta_id = il_prep_station(il, ctx, addr, is_ap, sta);
  295. if (sta_id == IL_INVALID_STATION) {
  296. IL_ERR("Unable to prepare station %pM for addition\n",
  297. addr);
  298. spin_unlock_irqrestore(&il->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 (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  307. D_INFO(
  308. "STA %d already in process of being added.\n",
  309. sta_id);
  310. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  311. return -EEXIST;
  312. }
  313. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  314. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  315. D_ASSOC(
  316. "STA %d (%pM) already added, not adding again.\n",
  317. sta_id, addr);
  318. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  319. return -EEXIST;
  320. }
  321. il->stations[sta_id].used |= IL_STA_UCODE_INPROGRESS;
  322. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  323. sizeof(struct il_addsta_cmd));
  324. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  325. /* Add station to device's station table */
  326. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  327. if (ret) {
  328. spin_lock_irqsave(&il->sta_lock, flags_spin);
  329. IL_ERR("Adding station %pM failed.\n",
  330. il->stations[sta_id].sta.sta.addr);
  331. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  332. il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  333. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  334. }
  335. *sta_id_r = sta_id;
  336. return ret;
  337. }
  338. EXPORT_SYMBOL(il_add_station_common);
  339. /**
  340. * il_sta_ucode_deactivate - deactivate ucode status for a station
  341. *
  342. * il->sta_lock must be held
  343. */
  344. static void il_sta_ucode_deactivate(struct il_priv *il, u8 sta_id)
  345. {
  346. /* Ucode must be active and driver must be non active */
  347. if ((il->stations[sta_id].used &
  348. (IL_STA_UCODE_ACTIVE | IL_STA_DRIVER_ACTIVE)) !=
  349. IL_STA_UCODE_ACTIVE)
  350. IL_ERR("removed non active STA %u\n", sta_id);
  351. il->stations[sta_id].used &= ~IL_STA_UCODE_ACTIVE;
  352. memset(&il->stations[sta_id], 0, sizeof(struct il_station_entry));
  353. D_ASSOC("Removed STA %u\n", sta_id);
  354. }
  355. static int il_send_remove_station(struct il_priv *il,
  356. const u8 *addr, int sta_id,
  357. bool temporary)
  358. {
  359. struct il_rx_pkt *pkt;
  360. int ret;
  361. unsigned long flags_spin;
  362. struct il_rem_sta_cmd rm_sta_cmd;
  363. struct il_host_cmd cmd = {
  364. .id = C_REM_STA,
  365. .len = sizeof(struct il_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 = il_send_cmd(il, &cmd);
  374. if (ret)
  375. return ret;
  376. pkt = (struct il_rx_pkt *)cmd.reply_page;
  377. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  378. IL_ERR("Bad return from C_REM_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(&il->sta_lock, flags_spin);
  387. il_sta_ucode_deactivate(il, sta_id);
  388. spin_unlock_irqrestore(&il->sta_lock,
  389. flags_spin);
  390. }
  391. D_ASSOC("C_REM_STA PASSED\n");
  392. break;
  393. default:
  394. ret = -EIO;
  395. IL_ERR("C_REM_STA failed\n");
  396. break;
  397. }
  398. }
  399. il_free_pages(il, cmd.reply_page);
  400. return ret;
  401. }
  402. /**
  403. * il_remove_station - Remove driver's knowledge of station.
  404. */
  405. int il_remove_station(struct il_priv *il, const u8 sta_id,
  406. const u8 *addr)
  407. {
  408. unsigned long flags;
  409. if (!il_is_ready(il)) {
  410. D_INFO(
  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. D_ASSOC("Removing STA from driver:%d %pM\n",
  421. sta_id, addr);
  422. if (WARN_ON(sta_id == IL_INVALID_STATION))
  423. return -EINVAL;
  424. spin_lock_irqsave(&il->sta_lock, flags);
  425. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  426. D_INFO("Removing %pM but non DRIVER active\n",
  427. addr);
  428. goto out_err;
  429. }
  430. if (!(il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  431. D_INFO("Removing %pM but non UCODE active\n",
  432. addr);
  433. goto out_err;
  434. }
  435. if (il->stations[sta_id].used & IL_STA_LOCAL) {
  436. kfree(il->stations[sta_id].lq);
  437. il->stations[sta_id].lq = NULL;
  438. }
  439. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  440. il->num_stations--;
  441. BUG_ON(il->num_stations < 0);
  442. spin_unlock_irqrestore(&il->sta_lock, flags);
  443. return il_send_remove_station(il, addr, sta_id, false);
  444. out_err:
  445. spin_unlock_irqrestore(&il->sta_lock, flags);
  446. return -EINVAL;
  447. }
  448. EXPORT_SYMBOL_GPL(il_remove_station);
  449. /**
  450. * il_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 il_clear_ucode_stations(struct il_priv *il,
  458. struct il_rxon_context *ctx)
  459. {
  460. int i;
  461. unsigned long flags_spin;
  462. bool cleared = false;
  463. D_INFO("Clearing ucode stations in driver\n");
  464. spin_lock_irqsave(&il->sta_lock, flags_spin);
  465. for (i = 0; i < il->hw_params.max_stations; i++) {
  466. if (ctx && ctx->ctxid != il->stations[i].ctxid)
  467. continue;
  468. if (il->stations[i].used & IL_STA_UCODE_ACTIVE) {
  469. D_INFO(
  470. "Clearing ucode active for station %d\n", i);
  471. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  472. cleared = true;
  473. }
  474. }
  475. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  476. if (!cleared)
  477. D_INFO(
  478. "No active stations found to be cleared\n");
  479. }
  480. EXPORT_SYMBOL(il_clear_ucode_stations);
  481. /**
  482. * il_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. il_restore_stations(struct il_priv *il, struct il_rxon_context *ctx)
  491. {
  492. struct il_addsta_cmd sta_cmd;
  493. struct il_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 (!il_is_ready(il)) {
  500. D_INFO(
  501. "Not ready yet, not restoring any stations.\n");
  502. return;
  503. }
  504. D_ASSOC("Restoring all known stations ... start.\n");
  505. spin_lock_irqsave(&il->sta_lock, flags_spin);
  506. for (i = 0; i < il->hw_params.max_stations; i++) {
  507. if (ctx->ctxid != il->stations[i].ctxid)
  508. continue;
  509. if ((il->stations[i].used & IL_STA_DRIVER_ACTIVE) &&
  510. !(il->stations[i].used & IL_STA_UCODE_ACTIVE)) {
  511. D_ASSOC("Restoring sta %pM\n",
  512. il->stations[i].sta.sta.addr);
  513. il->stations[i].sta.mode = 0;
  514. il->stations[i].used |= IL_STA_UCODE_INPROGRESS;
  515. found = true;
  516. }
  517. }
  518. for (i = 0; i < il->hw_params.max_stations; i++) {
  519. if ((il->stations[i].used & IL_STA_UCODE_INPROGRESS)) {
  520. memcpy(&sta_cmd, &il->stations[i].sta,
  521. sizeof(struct il_addsta_cmd));
  522. send_lq = false;
  523. if (il->stations[i].lq) {
  524. memcpy(&lq, il->stations[i].lq,
  525. sizeof(struct il_link_quality_cmd));
  526. send_lq = true;
  527. }
  528. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  529. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  530. if (ret) {
  531. spin_lock_irqsave(&il->sta_lock, flags_spin);
  532. IL_ERR("Adding station %pM failed.\n",
  533. il->stations[i].sta.sta.addr);
  534. il->stations[i].used &=
  535. ~IL_STA_DRIVER_ACTIVE;
  536. il->stations[i].used &=
  537. ~IL_STA_UCODE_INPROGRESS;
  538. spin_unlock_irqrestore(&il->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. il_send_lq_cmd(il, ctx, &lq,
  547. CMD_SYNC, true);
  548. spin_lock_irqsave(&il->sta_lock, flags_spin);
  549. il->stations[i].used &= ~IL_STA_UCODE_INPROGRESS;
  550. }
  551. }
  552. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  553. if (!found)
  554. D_INFO("Restoring all known stations"
  555. " .... no stations to be restored.\n");
  556. else
  557. D_INFO("Restoring all known stations"
  558. " .... complete.\n");
  559. }
  560. EXPORT_SYMBOL(il_restore_stations);
  561. int il_get_free_ucode_key_idx(struct il_priv *il)
  562. {
  563. int i;
  564. for (i = 0; i < il->sta_key_max_num; i++)
  565. if (!test_and_set_bit(i, &il->ucode_key_table))
  566. return i;
  567. return WEP_INVALID_OFFSET;
  568. }
  569. EXPORT_SYMBOL(il_get_free_ucode_key_idx);
  570. void il_dealloc_bcast_stations(struct il_priv *il)
  571. {
  572. unsigned long flags;
  573. int i;
  574. spin_lock_irqsave(&il->sta_lock, flags);
  575. for (i = 0; i < il->hw_params.max_stations; i++) {
  576. if (!(il->stations[i].used & IL_STA_BCAST))
  577. continue;
  578. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  579. il->num_stations--;
  580. BUG_ON(il->num_stations < 0);
  581. kfree(il->stations[i].lq);
  582. il->stations[i].lq = NULL;
  583. }
  584. spin_unlock_irqrestore(&il->sta_lock, flags);
  585. }
  586. EXPORT_SYMBOL_GPL(il_dealloc_bcast_stations);
  587. #ifdef CONFIG_IWLEGACY_DEBUG
  588. static void il_dump_lq_cmd(struct il_priv *il,
  589. struct il_link_quality_cmd *lq)
  590. {
  591. int i;
  592. D_RATE("lq station id 0x%x\n", lq->sta_id);
  593. D_RATE("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. D_RATE("lq idx %d 0x%X\n",
  598. i, lq->rs_table[i].rate_n_flags);
  599. }
  600. #else
  601. static inline void il_dump_lq_cmd(struct il_priv *il,
  602. struct il_link_quality_cmd *lq)
  603. {
  604. }
  605. #endif
  606. /**
  607. * il_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 il_is_lq_table_valid(struct il_priv *il,
  618. struct il_rxon_context *ctx,
  619. struct il_link_quality_cmd *lq)
  620. {
  621. int i;
  622. if (ctx->ht.enabled)
  623. return true;
  624. D_INFO("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. D_INFO(
  630. "idx %d of LQ expects HT channel\n",
  631. i);
  632. return false;
  633. }
  634. }
  635. return true;
  636. }
  637. /**
  638. * il_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 il_send_lq_cmd(struct il_priv *il, struct il_rxon_context *ctx,
  648. struct il_link_quality_cmd *lq, u8 flags, bool init)
  649. {
  650. int ret = 0;
  651. unsigned long flags_spin;
  652. struct il_host_cmd cmd = {
  653. .id = C_TX_LINK_QUALITY_CMD,
  654. .len = sizeof(struct il_link_quality_cmd),
  655. .flags = flags,
  656. .data = lq,
  657. };
  658. if (WARN_ON(lq->sta_id == IL_INVALID_STATION))
  659. return -EINVAL;
  660. spin_lock_irqsave(&il->sta_lock, flags_spin);
  661. if (!(il->stations[lq->sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  662. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  663. return -EINVAL;
  664. }
  665. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  666. il_dump_lq_cmd(il, lq);
  667. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  668. if (il_is_lq_table_valid(il, ctx, lq))
  669. ret = il_send_cmd(il, &cmd);
  670. else
  671. ret = -EINVAL;
  672. if (cmd.flags & CMD_ASYNC)
  673. return ret;
  674. if (init) {
  675. D_INFO("init LQ command complete,"
  676. " clearing sta addition status for sta %d\n",
  677. lq->sta_id);
  678. spin_lock_irqsave(&il->sta_lock, flags_spin);
  679. il->stations[lq->sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  680. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  681. }
  682. return ret;
  683. }
  684. EXPORT_SYMBOL(il_send_lq_cmd);
  685. int il_mac_sta_remove(struct ieee80211_hw *hw,
  686. struct ieee80211_vif *vif,
  687. struct ieee80211_sta *sta)
  688. {
  689. struct il_priv *il = hw->priv;
  690. struct il_station_priv_common *sta_common = (void *)sta->drv_priv;
  691. int ret;
  692. D_INFO("received request to remove station %pM\n",
  693. sta->addr);
  694. mutex_lock(&il->mutex);
  695. D_INFO("proceeding to remove station %pM\n",
  696. sta->addr);
  697. ret = il_remove_station(il, sta_common->sta_id, sta->addr);
  698. if (ret)
  699. IL_ERR("Error removing station %pM\n",
  700. sta->addr);
  701. mutex_unlock(&il->mutex);
  702. return ret;
  703. }
  704. EXPORT_SYMBOL(il_mac_sta_remove);