iwl-sta.c 42 KB

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