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