iwl-agn-sta.c 37 KB

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