iwl-agn-sta.c 40 KB

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