sta.c 39 KB

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