iwl-sta.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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 "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  36. {
  37. int i;
  38. int start = 0;
  39. int ret = IWL_INVALID_STATION;
  40. unsigned long flags;
  41. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
  42. (priv->iw_mode == NL80211_IFTYPE_AP))
  43. start = IWL_STA_ID;
  44. if (is_broadcast_ether_addr(addr))
  45. return priv->hw_params.bcast_sta_id;
  46. spin_lock_irqsave(&priv->sta_lock, flags);
  47. for (i = start; i < priv->hw_params.max_stations; i++)
  48. if (priv->stations[i].used &&
  49. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  50. addr))) {
  51. ret = i;
  52. goto out;
  53. }
  54. IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
  55. addr, priv->num_stations);
  56. out:
  57. /*
  58. * It may be possible that more commands interacting with stations
  59. * arrive before we completed processing the adding of
  60. * station
  61. */
  62. if (ret != IWL_INVALID_STATION &&
  63. (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
  64. ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
  65. (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
  66. IWL_ERR(priv, "Requested station info for sta %d before ready.\n",
  67. ret);
  68. ret = IWL_INVALID_STATION;
  69. }
  70. spin_unlock_irqrestore(&priv->sta_lock, flags);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL(iwl_find_station);
  74. /* priv->sta_lock must be held */
  75. static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
  76. {
  77. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
  78. IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
  79. sta_id, priv->stations[sta_id].sta.sta.addr);
  80. if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
  81. IWL_DEBUG_ASSOC(priv,
  82. "STA id %u addr %pM already present in uCode (according to driver)\n",
  83. sta_id, priv->stations[sta_id].sta.sta.addr);
  84. } else {
  85. priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
  86. IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
  87. sta_id, priv->stations[sta_id].sta.sta.addr);
  88. }
  89. }
  90. static void iwl_process_add_sta_resp(struct iwl_priv *priv,
  91. struct iwl_addsta_cmd *addsta,
  92. struct iwl_rx_packet *pkt,
  93. bool sync)
  94. {
  95. u8 sta_id = addsta->sta.sta_id;
  96. unsigned long flags;
  97. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  98. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  99. pkt->hdr.flags);
  100. return;
  101. }
  102. IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
  103. sta_id);
  104. spin_lock_irqsave(&priv->sta_lock, flags);
  105. switch (pkt->u.add_sta.status) {
  106. case ADD_STA_SUCCESS_MSK:
  107. IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
  108. iwl_sta_ucode_activate(priv, sta_id);
  109. break;
  110. case ADD_STA_NO_ROOM_IN_TABLE:
  111. IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
  112. sta_id);
  113. break;
  114. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  115. IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
  116. sta_id);
  117. break;
  118. case ADD_STA_MODIFY_NON_EXIST_STA:
  119. IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
  120. sta_id);
  121. break;
  122. default:
  123. IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
  124. pkt->u.add_sta.status);
  125. break;
  126. }
  127. IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
  128. priv->stations[sta_id].sta.mode ==
  129. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  130. sta_id, priv->stations[sta_id].sta.sta.addr);
  131. /*
  132. * XXX: The MAC address in the command buffer is often changed from
  133. * the original sent to the device. That is, the MAC address
  134. * written to the command buffer often is not the same MAC adress
  135. * read from the command buffer when the command returns. This
  136. * issue has not yet been resolved and this debugging is left to
  137. * observe the problem.
  138. */
  139. IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
  140. priv->stations[sta_id].sta.mode ==
  141. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
  142. addsta->sta.addr);
  143. spin_unlock_irqrestore(&priv->sta_lock, flags);
  144. }
  145. static void iwl_add_sta_callback(struct iwl_priv *priv,
  146. struct iwl_device_cmd *cmd,
  147. struct iwl_rx_packet *pkt)
  148. {
  149. struct iwl_addsta_cmd *addsta =
  150. (struct iwl_addsta_cmd *)cmd->cmd.payload;
  151. iwl_process_add_sta_resp(priv, addsta, pkt, false);
  152. }
  153. int iwl_send_add_sta(struct iwl_priv *priv,
  154. struct iwl_addsta_cmd *sta, u8 flags)
  155. {
  156. struct iwl_rx_packet *pkt = NULL;
  157. int ret = 0;
  158. u8 data[sizeof(*sta)];
  159. struct iwl_host_cmd cmd = {
  160. .id = REPLY_ADD_STA,
  161. .flags = flags,
  162. .data = data,
  163. };
  164. u8 sta_id __maybe_unused = sta->sta.sta_id;
  165. IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
  166. sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
  167. if (flags & CMD_ASYNC)
  168. cmd.callback = iwl_add_sta_callback;
  169. else
  170. cmd.flags |= CMD_WANT_SKB;
  171. cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
  172. ret = iwl_send_cmd(priv, &cmd);
  173. if (ret || (flags & CMD_ASYNC))
  174. return ret;
  175. if (ret == 0) {
  176. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  177. iwl_process_add_sta_resp(priv, sta, pkt, true);
  178. }
  179. iwl_free_pages(priv, cmd.reply_page);
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(iwl_send_add_sta);
  183. static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
  184. struct ieee80211_sta_ht_cap *sta_ht_inf)
  185. {
  186. __le32 sta_flags;
  187. u8 mimo_ps_mode;
  188. if (!sta_ht_inf || !sta_ht_inf->ht_supported)
  189. goto done;
  190. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  191. IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
  192. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
  193. "static" :
  194. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
  195. "dynamic" : "disabled");
  196. sta_flags = priv->stations[index].sta.station_flags;
  197. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  198. switch (mimo_ps_mode) {
  199. case WLAN_HT_CAP_SM_PS_STATIC:
  200. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  201. break;
  202. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  203. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  204. break;
  205. case WLAN_HT_CAP_SM_PS_DISABLED:
  206. break;
  207. default:
  208. IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
  209. break;
  210. }
  211. sta_flags |= cpu_to_le32(
  212. (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  213. sta_flags |= cpu_to_le32(
  214. (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  215. if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
  216. sta_flags |= STA_FLG_HT40_EN_MSK;
  217. else
  218. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  219. priv->stations[index].sta.station_flags = sta_flags;
  220. done:
  221. return;
  222. }
  223. /**
  224. * iwl_prep_station - Prepare station information for addition
  225. *
  226. * should be called with sta_lock held
  227. */
  228. static u8 iwl_prep_station(struct iwl_priv *priv, const u8 *addr,
  229. bool is_ap,
  230. struct ieee80211_sta_ht_cap *ht_info)
  231. {
  232. struct iwl_station_entry *station;
  233. int i;
  234. u8 sta_id = IWL_INVALID_STATION;
  235. u16 rate;
  236. if (is_ap)
  237. sta_id = IWL_AP_ID;
  238. else if (is_broadcast_ether_addr(addr))
  239. sta_id = priv->hw_params.bcast_sta_id;
  240. else
  241. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  242. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  243. addr)) {
  244. sta_id = i;
  245. break;
  246. }
  247. if (!priv->stations[i].used &&
  248. sta_id == IWL_INVALID_STATION)
  249. sta_id = i;
  250. }
  251. /*
  252. * These two conditions have the same outcome, but keep them
  253. * separate
  254. */
  255. if (unlikely(sta_id == IWL_INVALID_STATION))
  256. return sta_id;
  257. /*
  258. * uCode is not able to deal with multiple requests to add a
  259. * station. Keep track if one is in progress so that we do not send
  260. * another.
  261. */
  262. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  263. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  264. sta_id);
  265. return sta_id;
  266. }
  267. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  268. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
  269. !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
  270. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  271. sta_id, addr);
  272. return sta_id;
  273. }
  274. station = &priv->stations[sta_id];
  275. station->used = IWL_STA_DRIVER_ACTIVE;
  276. IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
  277. sta_id, addr);
  278. priv->num_stations++;
  279. /* Set up the REPLY_ADD_STA command to send to device */
  280. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  281. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  282. station->sta.mode = 0;
  283. station->sta.sta.sta_id = sta_id;
  284. station->sta.station_flags = 0;
  285. /* BCAST station and IBSS stations do not work in HT mode */
  286. if (sta_id != priv->hw_params.bcast_sta_id &&
  287. priv->iw_mode != NL80211_IFTYPE_ADHOC)
  288. iwl_set_ht_add_station(priv, sta_id, ht_info);
  289. /* 3945 only */
  290. rate = (priv->band == IEEE80211_BAND_5GHZ) ?
  291. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
  292. /* Turn on both antennas for the station... */
  293. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  294. return sta_id;
  295. }
  296. #define STA_WAIT_TIMEOUT (HZ/2)
  297. /**
  298. * iwl_add_station_common -
  299. */
  300. int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
  301. bool is_ap,
  302. struct ieee80211_sta_ht_cap *ht_info,
  303. u8 *sta_id_r)
  304. {
  305. struct iwl_station_entry *station;
  306. unsigned long flags_spin;
  307. int ret = 0;
  308. u8 sta_id;
  309. *sta_id_r = 0;
  310. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  311. sta_id = iwl_prep_station(priv, addr, is_ap, ht_info);
  312. if (sta_id == IWL_INVALID_STATION) {
  313. IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
  314. addr);
  315. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  316. return -EINVAL;
  317. }
  318. /*
  319. * uCode is not able to deal with multiple requests to add a
  320. * station. Keep track if one is in progress so that we do not send
  321. * another.
  322. */
  323. if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
  324. IWL_DEBUG_INFO(priv, "STA %d already in process of being added.\n",
  325. sta_id);
  326. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  327. return -EEXIST;
  328. }
  329. if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
  330. (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  331. IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not adding again.\n",
  332. sta_id, addr);
  333. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  334. return -EEXIST;
  335. }
  336. priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
  337. station = &priv->stations[sta_id];
  338. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  339. /* Add station to device's station table */
  340. ret = iwl_send_add_sta(priv, &station->sta, CMD_SYNC);
  341. if (ret) {
  342. IWL_ERR(priv, "Adding station %pM failed.\n", station->sta.sta.addr);
  343. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  344. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  345. priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  346. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  347. }
  348. *sta_id_r = sta_id;
  349. return ret;
  350. }
  351. EXPORT_SYMBOL(iwl_add_station_common);
  352. static struct iwl_link_quality_cmd *iwl_sta_alloc_lq(struct iwl_priv *priv,
  353. u8 sta_id)
  354. {
  355. int i, r;
  356. struct iwl_link_quality_cmd *link_cmd;
  357. u32 rate_flags;
  358. link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
  359. if (!link_cmd) {
  360. IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
  361. return NULL;
  362. }
  363. /* Set up the rate scaling to start at selected rate, fall back
  364. * all the way down to 1M in IEEE order, and then spin on 1M */
  365. if (priv->band == IEEE80211_BAND_5GHZ)
  366. r = IWL_RATE_6M_INDEX;
  367. else
  368. r = IWL_RATE_1M_INDEX;
  369. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  370. rate_flags = 0;
  371. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  372. rate_flags |= RATE_MCS_CCK_MSK;
  373. rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
  374. RATE_MCS_ANT_POS;
  375. link_cmd->rs_table[i].rate_n_flags =
  376. iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  377. r = iwl_get_prev_ieee_rate(r);
  378. }
  379. link_cmd->general_params.single_stream_ant_msk =
  380. first_antenna(priv->hw_params.valid_tx_ant);
  381. link_cmd->general_params.dual_stream_ant_msk =
  382. priv->hw_params.valid_tx_ant &
  383. ~first_antenna(priv->hw_params.valid_tx_ant);
  384. if (!link_cmd->general_params.dual_stream_ant_msk) {
  385. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  386. } else if (num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
  387. link_cmd->general_params.dual_stream_ant_msk =
  388. priv->hw_params.valid_tx_ant;
  389. }
  390. link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  391. link_cmd->agg_params.agg_time_limit =
  392. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  393. link_cmd->sta_id = sta_id;
  394. return link_cmd;
  395. }
  396. /*
  397. * iwl_add_local_stations - Add stations not requested by mac80211
  398. *
  399. * This will be either the broadcast station or the bssid station needed by
  400. * ad-hoc.
  401. *
  402. * Function sleeps.
  403. */
  404. int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs)
  405. {
  406. int ret;
  407. u8 sta_id;
  408. struct iwl_link_quality_cmd *link_cmd;
  409. unsigned long flags;
  410. ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
  411. if (ret) {
  412. IWL_ERR(priv, "Unable to add station %pM\n", addr);
  413. return ret;
  414. }
  415. spin_lock_irqsave(&priv->sta_lock, flags);
  416. priv->stations[sta_id].used |= IWL_STA_LOCAL;
  417. spin_unlock_irqrestore(&priv->sta_lock, flags);
  418. if (init_rs) {
  419. /* Set up default rate scaling table in device's station table */
  420. link_cmd = iwl_sta_alloc_lq(priv, sta_id);
  421. if (!link_cmd) {
  422. IWL_ERR(priv, "Unable to initialize rate scaling for station %pM.\n",
  423. addr);
  424. return -ENOMEM;
  425. }
  426. ret = iwl_send_lq_cmd(priv, link_cmd, CMD_SYNC, true);
  427. if (ret)
  428. IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
  429. spin_lock_irqsave(&priv->sta_lock, flags);
  430. priv->stations[sta_id].lq = link_cmd;
  431. spin_unlock_irqrestore(&priv->sta_lock, flags);
  432. }
  433. return 0;
  434. }
  435. EXPORT_SYMBOL(iwl_add_local_station);
  436. /**
  437. * iwl_sta_ucode_deactivate - deactivate ucode status for a station
  438. *
  439. * priv->sta_lock must be held
  440. */
  441. static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
  442. {
  443. /* Ucode must be active and driver must be non active */
  444. if ((priv->stations[sta_id].used &
  445. (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) != IWL_STA_UCODE_ACTIVE)
  446. IWL_ERR(priv, "removed non active STA %u\n", sta_id);
  447. priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
  448. memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
  449. IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
  450. }
  451. static int iwl_send_remove_station(struct iwl_priv *priv,
  452. struct iwl_station_entry *station)
  453. {
  454. struct iwl_rx_packet *pkt;
  455. int ret;
  456. unsigned long flags_spin;
  457. struct iwl_rem_sta_cmd rm_sta_cmd;
  458. struct iwl_host_cmd cmd = {
  459. .id = REPLY_REMOVE_STA,
  460. .len = sizeof(struct iwl_rem_sta_cmd),
  461. .flags = CMD_SYNC,
  462. .data = &rm_sta_cmd,
  463. };
  464. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  465. rm_sta_cmd.num_sta = 1;
  466. memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
  467. cmd.flags |= CMD_WANT_SKB;
  468. ret = iwl_send_cmd(priv, &cmd);
  469. if (ret)
  470. return ret;
  471. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  472. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  473. IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
  474. pkt->hdr.flags);
  475. ret = -EIO;
  476. }
  477. if (!ret) {
  478. switch (pkt->u.rem_sta.status) {
  479. case REM_STA_SUCCESS_MSK:
  480. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  481. iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
  482. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  483. IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
  484. break;
  485. default:
  486. ret = -EIO;
  487. IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
  488. break;
  489. }
  490. }
  491. iwl_free_pages(priv, cmd.reply_page);
  492. return ret;
  493. }
  494. /**
  495. * iwl_remove_station - Remove driver's knowledge of station.
  496. */
  497. int iwl_remove_station(struct iwl_priv *priv, const u8 *addr)
  498. {
  499. int sta_id = IWL_INVALID_STATION;
  500. int i, ret = -EINVAL;
  501. unsigned long flags;
  502. bool is_ap = priv->iw_mode == NL80211_IFTYPE_STATION;
  503. struct iwl_station_entry *station;
  504. if (!iwl_is_ready(priv)) {
  505. IWL_DEBUG_INFO(priv,
  506. "Unable to remove station %pM, device not ready.\n",
  507. addr);
  508. /*
  509. * It is typical for stations to be removed when we are
  510. * going down. Return success since device will be down
  511. * soon anyway
  512. */
  513. return 0;
  514. }
  515. spin_lock_irqsave(&priv->sta_lock, flags);
  516. if (is_ap)
  517. sta_id = IWL_AP_ID;
  518. else
  519. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  520. if (priv->stations[i].used &&
  521. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  522. addr)) {
  523. sta_id = i;
  524. break;
  525. }
  526. if (unlikely(sta_id == IWL_INVALID_STATION))
  527. goto out;
  528. IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
  529. sta_id, addr);
  530. if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  531. IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
  532. addr);
  533. goto out;
  534. }
  535. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
  536. IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
  537. addr);
  538. goto out;
  539. }
  540. if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
  541. kfree(priv->stations[sta_id].lq);
  542. priv->stations[sta_id].lq = NULL;
  543. }
  544. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  545. priv->num_stations--;
  546. BUG_ON(priv->num_stations < 0);
  547. station = &priv->stations[sta_id];
  548. spin_unlock_irqrestore(&priv->sta_lock, flags);
  549. ret = iwl_send_remove_station(priv, station);
  550. return ret;
  551. out:
  552. spin_unlock_irqrestore(&priv->sta_lock, flags);
  553. return ret;
  554. }
  555. EXPORT_SYMBOL_GPL(iwl_remove_station);
  556. /**
  557. * iwl_clear_ucode_stations - clear ucode station table bits
  558. *
  559. * This function clears all the bits in the driver indicating
  560. * which stations are active in the ucode. Call when something
  561. * other than explicit station management would cause this in
  562. * the ucode, e.g. unassociated RXON.
  563. */
  564. void iwl_clear_ucode_stations(struct iwl_priv *priv)
  565. {
  566. int i;
  567. unsigned long flags_spin;
  568. bool cleared = false;
  569. IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
  570. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  571. for (i = 0; i < priv->hw_params.max_stations; i++) {
  572. if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
  573. IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
  574. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  575. cleared = true;
  576. }
  577. }
  578. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  579. if (!cleared)
  580. IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
  581. }
  582. EXPORT_SYMBOL(iwl_clear_ucode_stations);
  583. /**
  584. * iwl_restore_stations() - Restore driver known stations to device
  585. *
  586. * All stations considered active by driver, but not present in ucode, is
  587. * restored.
  588. *
  589. * Function sleeps.
  590. */
  591. void iwl_restore_stations(struct iwl_priv *priv)
  592. {
  593. struct iwl_station_entry *station;
  594. unsigned long flags_spin;
  595. int i;
  596. bool found = false;
  597. int ret;
  598. if (!iwl_is_ready(priv)) {
  599. IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
  600. return;
  601. }
  602. IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
  603. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  604. for (i = 0; i < priv->hw_params.max_stations; i++) {
  605. if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
  606. !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
  607. IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
  608. priv->stations[i].sta.sta.addr);
  609. priv->stations[i].sta.mode = 0;
  610. priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
  611. found = true;
  612. }
  613. }
  614. for (i = 0; i < priv->hw_params.max_stations; i++) {
  615. if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
  616. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  617. station = &priv->stations[i];
  618. ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
  619. if (ret) {
  620. IWL_ERR(priv, "Adding station %pM failed.\n",
  621. station->sta.sta.addr);
  622. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  623. priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
  624. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  625. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  626. }
  627. /*
  628. * Rate scaling has already been initialized, send
  629. * current LQ command
  630. */
  631. if (station->lq)
  632. iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
  633. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  634. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  635. }
  636. }
  637. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  638. if (!found)
  639. IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
  640. else
  641. IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
  642. }
  643. EXPORT_SYMBOL(iwl_restore_stations);
  644. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  645. {
  646. int i;
  647. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  648. if (!test_and_set_bit(i, &priv->ucode_key_table))
  649. return i;
  650. return WEP_INVALID_OFFSET;
  651. }
  652. EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
  653. static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  654. {
  655. int i, not_empty = 0;
  656. u8 buff[sizeof(struct iwl_wep_cmd) +
  657. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  658. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  659. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  660. struct iwl_host_cmd cmd = {
  661. .id = REPLY_WEPKEY,
  662. .data = wep_cmd,
  663. .flags = CMD_SYNC,
  664. };
  665. might_sleep();
  666. memset(wep_cmd, 0, cmd_size +
  667. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  668. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  669. wep_cmd->key[i].key_index = i;
  670. if (priv->wep_keys[i].key_size) {
  671. wep_cmd->key[i].key_offset = i;
  672. not_empty = 1;
  673. } else {
  674. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  675. }
  676. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  677. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  678. priv->wep_keys[i].key_size);
  679. }
  680. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  681. wep_cmd->num_keys = WEP_KEYS_MAX;
  682. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  683. cmd.len = cmd_size;
  684. if (not_empty || send_if_empty)
  685. return iwl_send_cmd(priv, &cmd);
  686. else
  687. return 0;
  688. }
  689. int iwl_restore_default_wep_keys(struct iwl_priv *priv)
  690. {
  691. WARN_ON(!mutex_is_locked(&priv->mutex));
  692. return iwl_send_static_wepkey_cmd(priv, 0);
  693. }
  694. EXPORT_SYMBOL(iwl_restore_default_wep_keys);
  695. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  696. struct ieee80211_key_conf *keyconf)
  697. {
  698. int ret;
  699. WARN_ON(!mutex_is_locked(&priv->mutex));
  700. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  701. keyconf->keyidx);
  702. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  703. if (iwl_is_rfkill(priv)) {
  704. IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  705. /* but keys in device are clear anyway so return success */
  706. return 0;
  707. }
  708. ret = iwl_send_static_wepkey_cmd(priv, 1);
  709. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  710. keyconf->keyidx, ret);
  711. return ret;
  712. }
  713. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  714. int iwl_set_default_wep_key(struct iwl_priv *priv,
  715. struct ieee80211_key_conf *keyconf)
  716. {
  717. int ret;
  718. WARN_ON(!mutex_is_locked(&priv->mutex));
  719. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  720. keyconf->keylen != WEP_KEY_LEN_64) {
  721. IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
  722. return -EINVAL;
  723. }
  724. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  725. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  726. priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
  727. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  728. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  729. keyconf->keylen);
  730. ret = iwl_send_static_wepkey_cmd(priv, 0);
  731. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  732. keyconf->keylen, keyconf->keyidx, ret);
  733. return ret;
  734. }
  735. EXPORT_SYMBOL(iwl_set_default_wep_key);
  736. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  737. struct ieee80211_key_conf *keyconf,
  738. u8 sta_id)
  739. {
  740. unsigned long flags;
  741. __le16 key_flags = 0;
  742. int ret;
  743. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  744. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  745. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  746. key_flags &= ~STA_KEY_FLG_INVALID;
  747. if (keyconf->keylen == WEP_KEY_LEN_128)
  748. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  749. if (sta_id == priv->hw_params.bcast_sta_id)
  750. key_flags |= STA_KEY_MULTICAST_MSK;
  751. spin_lock_irqsave(&priv->sta_lock, flags);
  752. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  753. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  754. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  755. memcpy(priv->stations[sta_id].keyinfo.key,
  756. keyconf->key, keyconf->keylen);
  757. memcpy(&priv->stations[sta_id].sta.key.key[3],
  758. keyconf->key, keyconf->keylen);
  759. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  760. == STA_KEY_FLG_NO_ENC)
  761. priv->stations[sta_id].sta.key.key_offset =
  762. iwl_get_free_ucode_key_index(priv);
  763. /* else, we are overriding an existing key => no need to allocated room
  764. * in uCode. */
  765. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  766. "no space for a new key");
  767. priv->stations[sta_id].sta.key.key_flags = key_flags;
  768. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  769. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  770. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  771. spin_unlock_irqrestore(&priv->sta_lock, flags);
  772. return ret;
  773. }
  774. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  775. struct ieee80211_key_conf *keyconf,
  776. u8 sta_id)
  777. {
  778. unsigned long flags;
  779. __le16 key_flags = 0;
  780. int ret;
  781. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  782. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  783. key_flags &= ~STA_KEY_FLG_INVALID;
  784. if (sta_id == priv->hw_params.bcast_sta_id)
  785. key_flags |= STA_KEY_MULTICAST_MSK;
  786. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  787. spin_lock_irqsave(&priv->sta_lock, flags);
  788. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  789. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  790. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  791. keyconf->keylen);
  792. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  793. keyconf->keylen);
  794. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  795. == STA_KEY_FLG_NO_ENC)
  796. priv->stations[sta_id].sta.key.key_offset =
  797. iwl_get_free_ucode_key_index(priv);
  798. /* else, we are overriding an existing key => no need to allocated room
  799. * in uCode. */
  800. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  801. "no space for a new key");
  802. priv->stations[sta_id].sta.key.key_flags = key_flags;
  803. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  804. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  805. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  806. spin_unlock_irqrestore(&priv->sta_lock, flags);
  807. return ret;
  808. }
  809. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  810. struct ieee80211_key_conf *keyconf,
  811. u8 sta_id)
  812. {
  813. unsigned long flags;
  814. int ret = 0;
  815. __le16 key_flags = 0;
  816. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  817. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  818. key_flags &= ~STA_KEY_FLG_INVALID;
  819. if (sta_id == priv->hw_params.bcast_sta_id)
  820. key_flags |= STA_KEY_MULTICAST_MSK;
  821. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  822. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  823. spin_lock_irqsave(&priv->sta_lock, flags);
  824. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  825. priv->stations[sta_id].keyinfo.keylen = 16;
  826. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  827. == STA_KEY_FLG_NO_ENC)
  828. priv->stations[sta_id].sta.key.key_offset =
  829. iwl_get_free_ucode_key_index(priv);
  830. /* else, we are overriding an existing key => no need to allocated room
  831. * in uCode. */
  832. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  833. "no space for a new key");
  834. priv->stations[sta_id].sta.key.key_flags = key_flags;
  835. /* This copy is acutally not needed: we get the key with each TX */
  836. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  837. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  838. spin_unlock_irqrestore(&priv->sta_lock, flags);
  839. return ret;
  840. }
  841. void iwl_update_tkip_key(struct iwl_priv *priv,
  842. struct ieee80211_key_conf *keyconf,
  843. const u8 *addr, u32 iv32, u16 *phase1key)
  844. {
  845. u8 sta_id = IWL_INVALID_STATION;
  846. unsigned long flags;
  847. int i;
  848. sta_id = iwl_find_station(priv, addr);
  849. if (sta_id == IWL_INVALID_STATION) {
  850. IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
  851. addr);
  852. return;
  853. }
  854. if (iwl_scan_cancel(priv)) {
  855. /* cancel scan failed, just live w/ bad key and rely
  856. briefly on SW decryption */
  857. return;
  858. }
  859. spin_lock_irqsave(&priv->sta_lock, flags);
  860. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  861. for (i = 0; i < 5; i++)
  862. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  863. cpu_to_le16(phase1key[i]);
  864. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  865. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  866. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  867. spin_unlock_irqrestore(&priv->sta_lock, flags);
  868. }
  869. EXPORT_SYMBOL(iwl_update_tkip_key);
  870. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  871. struct ieee80211_key_conf *keyconf,
  872. u8 sta_id)
  873. {
  874. unsigned long flags;
  875. int ret = 0;
  876. u16 key_flags;
  877. u8 keyidx;
  878. priv->key_mapping_key--;
  879. spin_lock_irqsave(&priv->sta_lock, flags);
  880. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  881. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  882. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  883. keyconf->keyidx, sta_id);
  884. if (keyconf->keyidx != keyidx) {
  885. /* We need to remove a key with index different that the one
  886. * in the uCode. This means that the key we need to remove has
  887. * been replaced by another one with different index.
  888. * Don't do anything and return ok
  889. */
  890. spin_unlock_irqrestore(&priv->sta_lock, flags);
  891. return 0;
  892. }
  893. if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  894. IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
  895. keyconf->keyidx, key_flags);
  896. spin_unlock_irqrestore(&priv->sta_lock, flags);
  897. return 0;
  898. }
  899. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  900. &priv->ucode_key_table))
  901. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  902. priv->stations[sta_id].sta.key.key_offset);
  903. memset(&priv->stations[sta_id].keyinfo, 0,
  904. sizeof(struct iwl_hw_key));
  905. memset(&priv->stations[sta_id].sta.key, 0,
  906. sizeof(struct iwl4965_keyinfo));
  907. priv->stations[sta_id].sta.key.key_flags =
  908. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  909. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  910. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  911. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  912. if (iwl_is_rfkill(priv)) {
  913. IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
  914. spin_unlock_irqrestore(&priv->sta_lock, flags);
  915. return 0;
  916. }
  917. ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  918. spin_unlock_irqrestore(&priv->sta_lock, flags);
  919. return ret;
  920. }
  921. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  922. int iwl_set_dynamic_key(struct iwl_priv *priv,
  923. struct ieee80211_key_conf *keyconf, u8 sta_id)
  924. {
  925. int ret;
  926. priv->key_mapping_key++;
  927. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  928. switch (keyconf->alg) {
  929. case ALG_CCMP:
  930. ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  931. break;
  932. case ALG_TKIP:
  933. ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  934. break;
  935. case ALG_WEP:
  936. ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  937. break;
  938. default:
  939. IWL_ERR(priv,
  940. "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  941. ret = -EINVAL;
  942. }
  943. IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  944. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  945. sta_id, ret);
  946. return ret;
  947. }
  948. EXPORT_SYMBOL(iwl_set_dynamic_key);
  949. #ifdef CONFIG_IWLWIFI_DEBUG
  950. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  951. struct iwl_link_quality_cmd *lq)
  952. {
  953. int i;
  954. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  955. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  956. lq->general_params.single_stream_ant_msk,
  957. lq->general_params.dual_stream_ant_msk);
  958. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  959. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  960. i, lq->rs_table[i].rate_n_flags);
  961. }
  962. #else
  963. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  964. struct iwl_link_quality_cmd *lq)
  965. {
  966. }
  967. #endif
  968. /**
  969. * is_lq_table_valid() - Test one aspect of LQ cmd for validity
  970. *
  971. * It sometimes happens when a HT rate has been in use and we
  972. * loose connectivity with AP then mac80211 will first tell us that the
  973. * current channel is not HT anymore before removing the station. In such a
  974. * scenario the RXON flags will be updated to indicate we are not
  975. * communicating HT anymore, but the LQ command may still contain HT rates.
  976. * Test for this to prevent driver from sending LQ command between the time
  977. * RXON flags are updated and when LQ command is updated.
  978. */
  979. static bool is_lq_table_valid(struct iwl_priv *priv,
  980. struct iwl_link_quality_cmd *lq)
  981. {
  982. int i;
  983. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  984. if (ht_conf->is_ht)
  985. return true;
  986. IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
  987. priv->active_rxon.channel);
  988. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  989. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
  990. IWL_DEBUG_INFO(priv,
  991. "index %d of LQ expects HT channel\n",
  992. i);
  993. return false;
  994. }
  995. }
  996. return true;
  997. }
  998. /**
  999. * iwl_send_lq_cmd() - Send link quality command
  1000. * @init: This command is sent as part of station initialization right
  1001. * after station has been added.
  1002. *
  1003. * The link quality command is sent as the last step of station creation.
  1004. * This is the special case in which init is set and we call a callback in
  1005. * this case to clear the state indicating that station creation is in
  1006. * progress.
  1007. */
  1008. int iwl_send_lq_cmd(struct iwl_priv *priv,
  1009. struct iwl_link_quality_cmd *lq, u8 flags, bool init)
  1010. {
  1011. int ret = 0;
  1012. unsigned long flags_spin;
  1013. struct iwl_host_cmd cmd = {
  1014. .id = REPLY_TX_LINK_QUALITY_CMD,
  1015. .len = sizeof(struct iwl_link_quality_cmd),
  1016. .flags = flags,
  1017. .data = lq,
  1018. };
  1019. if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
  1020. return -EINVAL;
  1021. iwl_dump_lq_cmd(priv, lq);
  1022. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  1023. if (is_lq_table_valid(priv, lq))
  1024. ret = iwl_send_cmd(priv, &cmd);
  1025. else
  1026. ret = -EINVAL;
  1027. if (cmd.flags & CMD_ASYNC)
  1028. return ret;
  1029. if (init) {
  1030. IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
  1031. lq->sta_id);
  1032. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  1033. priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  1034. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  1035. }
  1036. return ret;
  1037. }
  1038. EXPORT_SYMBOL(iwl_send_lq_cmd);
  1039. /**
  1040. * iwl_alloc_bcast_station - add broadcast station into driver's station table.
  1041. *
  1042. * This adds the broadcast station into the driver's station table
  1043. * and marks it driver active, so that it will be restored to the
  1044. * device at the next best time.
  1045. */
  1046. int iwl_alloc_bcast_station(struct iwl_priv *priv, bool init_lq)
  1047. {
  1048. struct iwl_link_quality_cmd *link_cmd;
  1049. unsigned long flags;
  1050. u8 sta_id;
  1051. spin_lock_irqsave(&priv->sta_lock, flags);
  1052. sta_id = iwl_prep_station(priv, iwl_bcast_addr, false, NULL);
  1053. if (sta_id == IWL_INVALID_STATION) {
  1054. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  1055. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1056. return -EINVAL;
  1057. }
  1058. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  1059. priv->stations[sta_id].used |= IWL_STA_BCAST;
  1060. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1061. if (init_lq) {
  1062. link_cmd = iwl_sta_alloc_lq(priv, sta_id);
  1063. if (!link_cmd) {
  1064. IWL_ERR(priv,
  1065. "Unable to initialize rate scaling for bcast station.\n");
  1066. return -ENOMEM;
  1067. }
  1068. spin_lock_irqsave(&priv->sta_lock, flags);
  1069. priv->stations[sta_id].lq = link_cmd;
  1070. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1071. }
  1072. return 0;
  1073. }
  1074. EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
  1075. void iwl_dealloc_bcast_station(struct iwl_priv *priv)
  1076. {
  1077. unsigned long flags;
  1078. int i;
  1079. spin_lock_irqsave(&priv->sta_lock, flags);
  1080. for (i = 0; i < priv->hw_params.max_stations; i++) {
  1081. if (!(priv->stations[i].used & IWL_STA_BCAST))
  1082. continue;
  1083. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  1084. priv->num_stations--;
  1085. BUG_ON(priv->num_stations < 0);
  1086. kfree(priv->stations[i].lq);
  1087. priv->stations[i].lq = NULL;
  1088. }
  1089. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1090. }
  1091. EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_station);
  1092. /**
  1093. * iwl_get_sta_id - Find station's index within station table
  1094. *
  1095. * If new IBSS station, create new entry in station table
  1096. */
  1097. int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  1098. {
  1099. int sta_id;
  1100. __le16 fc = hdr->frame_control;
  1101. /* If this frame is broadcast or management, use broadcast station id */
  1102. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
  1103. return priv->hw_params.bcast_sta_id;
  1104. switch (priv->iw_mode) {
  1105. /* If we are a client station in a BSS network, use the special
  1106. * AP station entry (that's the only station we communicate with) */
  1107. case NL80211_IFTYPE_STATION:
  1108. /*
  1109. * If addition of station not complete yet, which means
  1110. * that rate scaling has not been initialized, then return
  1111. * the broadcast station.
  1112. */
  1113. if (!(priv->stations[IWL_AP_ID].used & IWL_STA_UCODE_ACTIVE))
  1114. return priv->hw_params.bcast_sta_id;
  1115. return IWL_AP_ID;
  1116. /* If we are an AP, then find the station, or use BCAST */
  1117. case NL80211_IFTYPE_AP:
  1118. sta_id = iwl_find_station(priv, hdr->addr1);
  1119. if (sta_id != IWL_INVALID_STATION)
  1120. return sta_id;
  1121. return priv->hw_params.bcast_sta_id;
  1122. /* If this frame is going out to an IBSS network, find the station,
  1123. * or create a new station table entry */
  1124. case NL80211_IFTYPE_ADHOC:
  1125. sta_id = iwl_find_station(priv, hdr->addr1);
  1126. if (sta_id != IWL_INVALID_STATION)
  1127. return sta_id;
  1128. IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
  1129. "Defaulting to broadcast...\n",
  1130. hdr->addr1);
  1131. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  1132. return priv->hw_params.bcast_sta_id;
  1133. default:
  1134. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  1135. priv->iw_mode);
  1136. return priv->hw_params.bcast_sta_id;
  1137. }
  1138. }
  1139. EXPORT_SYMBOL(iwl_get_sta_id);
  1140. /**
  1141. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1142. */
  1143. void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  1144. {
  1145. unsigned long flags;
  1146. /* Remove "disable" flag, to enable Tx for this TID */
  1147. spin_lock_irqsave(&priv->sta_lock, flags);
  1148. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1149. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1150. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1151. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1152. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1153. }
  1154. EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
  1155. int iwl_sta_rx_agg_start(struct iwl_priv *priv,
  1156. const u8 *addr, int tid, u16 ssn)
  1157. {
  1158. unsigned long flags;
  1159. int sta_id;
  1160. sta_id = iwl_find_station(priv, addr);
  1161. if (sta_id == IWL_INVALID_STATION)
  1162. return -ENXIO;
  1163. spin_lock_irqsave(&priv->sta_lock, flags);
  1164. priv->stations[sta_id].sta.station_flags_msk = 0;
  1165. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1166. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1167. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1168. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1169. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1170. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1171. CMD_ASYNC);
  1172. }
  1173. EXPORT_SYMBOL(iwl_sta_rx_agg_start);
  1174. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
  1175. {
  1176. unsigned long flags;
  1177. int sta_id;
  1178. sta_id = iwl_find_station(priv, addr);
  1179. if (sta_id == IWL_INVALID_STATION) {
  1180. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1181. return -ENXIO;
  1182. }
  1183. spin_lock_irqsave(&priv->sta_lock, flags);
  1184. priv->stations[sta_id].sta.station_flags_msk = 0;
  1185. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1186. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1187. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1188. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1189. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  1190. CMD_ASYNC);
  1191. }
  1192. EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
  1193. void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1194. {
  1195. unsigned long flags;
  1196. spin_lock_irqsave(&priv->sta_lock, flags);
  1197. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1198. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1199. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1200. priv->stations[sta_id].sta.sleep_tx_count = 0;
  1201. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1202. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1203. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1204. }
  1205. EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
  1206. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1207. {
  1208. unsigned long flags;
  1209. spin_lock_irqsave(&priv->sta_lock, flags);
  1210. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1211. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1212. priv->stations[sta_id].sta.sta.modify_mask =
  1213. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1214. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1215. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1216. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1217. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1218. }
  1219. EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
  1220. int iwl_mac_sta_remove(struct ieee80211_hw *hw,
  1221. struct ieee80211_vif *vif,
  1222. struct ieee80211_sta *sta)
  1223. {
  1224. int ret;
  1225. struct iwl_priv *priv = hw->priv;
  1226. IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
  1227. sta->addr);
  1228. ret = iwl_remove_station(priv, sta->addr);
  1229. if (ret)
  1230. IWL_ERR(priv, "Error removing station %pM\n",
  1231. sta->addr);
  1232. return ret;
  1233. }
  1234. EXPORT_SYMBOL(iwl_mac_sta_remove);