sta.c 38 KB

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