iwl-agn-sta.c 38 KB

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