iwl-sta.c 41 KB

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