iwl-sta.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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, const u8 *addr,
  196. 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 = priv->hw_params.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. /*
  253. * OK to call unconditionally, since local stations (IBSS BSSID
  254. * STA and broadcast STA) pass in a NULL ht_info, and mac80211
  255. * doesn't allow HT IBSS.
  256. */
  257. iwl_set_ht_add_station(priv, sta_id, ht_info);
  258. /* 3945 only */
  259. rate = (priv->band == IEEE80211_BAND_5GHZ) ?
  260. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
  261. /* Turn on both antennas for the station... */
  262. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  263. return sta_id;
  264. }
  265. #define STA_WAIT_TIMEOUT (HZ/2)
  266. /**
  267. * iwl_add_station_common -
  268. */
  269. int iwl_add_station_common(struct iwl_priv *priv, const u8 *addr,
  270. bool is_ap,
  271. struct ieee80211_sta_ht_cap *ht_info,
  272. 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, 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, const u8 *addr, bool init_rs,
  372. 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, 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. {
  523. int i;
  524. unsigned long flags_spin;
  525. bool cleared = false;
  526. IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
  527. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  528. for (i = 0; i < priv->hw_params.max_stations; i++) {
  529. if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
  530. IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
  531. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  532. cleared = true;
  533. }
  534. }
  535. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  536. if (!cleared)
  537. IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
  538. }
  539. EXPORT_SYMBOL(iwl_clear_ucode_stations);
  540. /**
  541. * iwl_restore_stations() - Restore driver known stations to device
  542. *
  543. * All stations considered active by driver, but not present in ucode, is
  544. * restored.
  545. *
  546. * Function sleeps.
  547. */
  548. void iwl_restore_stations(struct iwl_priv *priv)
  549. {
  550. struct iwl_addsta_cmd sta_cmd;
  551. struct iwl_link_quality_cmd lq;
  552. unsigned long flags_spin;
  553. int i;
  554. bool found = false;
  555. int ret;
  556. bool send_lq;
  557. if (!iwl_is_ready(priv)) {
  558. IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
  559. return;
  560. }
  561. IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
  562. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  563. for (i = 0; i < priv->hw_params.max_stations; i++) {
  564. if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
  565. !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
  566. IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
  567. priv->stations[i].sta.sta.addr);
  568. priv->stations[i].sta.mode = 0;
  569. priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
  570. found = true;
  571. }
  572. }
  573. for (i = 0; i < priv->hw_params.max_stations; i++) {
  574. if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
  575. memcpy(&sta_cmd, &priv->stations[i].sta,
  576. sizeof(struct iwl_addsta_cmd));
  577. send_lq = false;
  578. if (priv->stations[i].lq) {
  579. memcpy(&lq, priv->stations[i].lq,
  580. sizeof(struct iwl_link_quality_cmd));
  581. send_lq = true;
  582. }
  583. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  584. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  585. if (ret) {
  586. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  587. IWL_ERR(priv, "Adding station %pM failed.\n",
  588. priv->stations[i].sta.sta.addr);
  589. priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
  590. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  591. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  592. }
  593. /*
  594. * Rate scaling has already been initialized, send
  595. * current LQ command
  596. */
  597. if (send_lq)
  598. iwl_send_lq_cmd(priv, &lq, CMD_SYNC, true);
  599. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  600. priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
  601. }
  602. }
  603. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  604. if (!found)
  605. IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
  606. else
  607. IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
  608. }
  609. EXPORT_SYMBOL(iwl_restore_stations);
  610. int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
  611. {
  612. int i;
  613. for (i = 0; i < STA_KEY_MAX_NUM; i++)
  614. if (!test_and_set_bit(i, &priv->ucode_key_table))
  615. return i;
  616. return WEP_INVALID_OFFSET;
  617. }
  618. EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
  619. static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
  620. {
  621. int i, not_empty = 0;
  622. u8 buff[sizeof(struct iwl_wep_cmd) +
  623. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  624. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  625. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  626. struct iwl_host_cmd cmd = {
  627. .id = REPLY_WEPKEY,
  628. .data = wep_cmd,
  629. .flags = CMD_SYNC,
  630. };
  631. might_sleep();
  632. memset(wep_cmd, 0, cmd_size +
  633. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  634. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  635. wep_cmd->key[i].key_index = i;
  636. if (priv->wep_keys[i].key_size) {
  637. wep_cmd->key[i].key_offset = i;
  638. not_empty = 1;
  639. } else {
  640. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  641. }
  642. wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
  643. memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
  644. priv->wep_keys[i].key_size);
  645. }
  646. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  647. wep_cmd->num_keys = WEP_KEYS_MAX;
  648. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  649. cmd.len = cmd_size;
  650. if (not_empty || send_if_empty)
  651. return iwl_send_cmd(priv, &cmd);
  652. else
  653. return 0;
  654. }
  655. int iwl_restore_default_wep_keys(struct iwl_priv *priv)
  656. {
  657. lockdep_assert_held(&priv->mutex);
  658. return iwl_send_static_wepkey_cmd(priv, 0);
  659. }
  660. EXPORT_SYMBOL(iwl_restore_default_wep_keys);
  661. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  662. struct ieee80211_key_conf *keyconf)
  663. {
  664. int ret;
  665. lockdep_assert_held(&priv->mutex);
  666. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  667. keyconf->keyidx);
  668. memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
  669. if (iwl_is_rfkill(priv)) {
  670. IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  671. /* but keys in device are clear anyway so return success */
  672. return 0;
  673. }
  674. ret = iwl_send_static_wepkey_cmd(priv, 1);
  675. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  676. keyconf->keyidx, ret);
  677. return ret;
  678. }
  679. EXPORT_SYMBOL(iwl_remove_default_wep_key);
  680. int iwl_set_default_wep_key(struct iwl_priv *priv,
  681. struct ieee80211_key_conf *keyconf)
  682. {
  683. int ret;
  684. lockdep_assert_held(&priv->mutex);
  685. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  686. keyconf->keylen != WEP_KEY_LEN_64) {
  687. IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
  688. return -EINVAL;
  689. }
  690. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  691. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  692. priv->stations[IWL_AP_ID].keyinfo.cipher = keyconf->cipher;
  693. priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  694. memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
  695. keyconf->keylen);
  696. ret = iwl_send_static_wepkey_cmd(priv, 0);
  697. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  698. keyconf->keylen, keyconf->keyidx, ret);
  699. return ret;
  700. }
  701. EXPORT_SYMBOL(iwl_set_default_wep_key);
  702. static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
  703. struct ieee80211_key_conf *keyconf,
  704. u8 sta_id)
  705. {
  706. unsigned long flags;
  707. __le16 key_flags = 0;
  708. struct iwl_addsta_cmd sta_cmd;
  709. lockdep_assert_held(&priv->mutex);
  710. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  711. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  712. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  713. key_flags &= ~STA_KEY_FLG_INVALID;
  714. if (keyconf->keylen == WEP_KEY_LEN_128)
  715. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  716. if (sta_id == priv->hw_params.bcast_sta_id)
  717. key_flags |= STA_KEY_MULTICAST_MSK;
  718. spin_lock_irqsave(&priv->sta_lock, flags);
  719. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  720. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  721. priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  722. memcpy(priv->stations[sta_id].keyinfo.key,
  723. keyconf->key, keyconf->keylen);
  724. memcpy(&priv->stations[sta_id].sta.key.key[3],
  725. keyconf->key, keyconf->keylen);
  726. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  727. == STA_KEY_FLG_NO_ENC)
  728. priv->stations[sta_id].sta.key.key_offset =
  729. iwl_get_free_ucode_key_index(priv);
  730. /* else, we are overriding an existing key => no need to allocated room
  731. * in uCode. */
  732. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  733. "no space for a new key");
  734. priv->stations[sta_id].sta.key.key_flags = key_flags;
  735. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  736. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  737. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  738. spin_unlock_irqrestore(&priv->sta_lock, flags);
  739. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  740. }
  741. static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  742. struct ieee80211_key_conf *keyconf,
  743. u8 sta_id)
  744. {
  745. unsigned long flags;
  746. __le16 key_flags = 0;
  747. struct iwl_addsta_cmd sta_cmd;
  748. lockdep_assert_held(&priv->mutex);
  749. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  750. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  751. key_flags &= ~STA_KEY_FLG_INVALID;
  752. if (sta_id == priv->hw_params.bcast_sta_id)
  753. key_flags |= STA_KEY_MULTICAST_MSK;
  754. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  755. spin_lock_irqsave(&priv->sta_lock, flags);
  756. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  757. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  758. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  759. keyconf->keylen);
  760. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  761. keyconf->keylen);
  762. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  763. == STA_KEY_FLG_NO_ENC)
  764. priv->stations[sta_id].sta.key.key_offset =
  765. iwl_get_free_ucode_key_index(priv);
  766. /* else, we are overriding an existing key => no need to allocated room
  767. * in uCode. */
  768. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  769. "no space for a new key");
  770. priv->stations[sta_id].sta.key.key_flags = key_flags;
  771. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  772. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  773. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  774. spin_unlock_irqrestore(&priv->sta_lock, flags);
  775. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  776. }
  777. static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  778. struct ieee80211_key_conf *keyconf,
  779. u8 sta_id)
  780. {
  781. unsigned long flags;
  782. int ret = 0;
  783. __le16 key_flags = 0;
  784. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  785. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  786. key_flags &= ~STA_KEY_FLG_INVALID;
  787. if (sta_id == priv->hw_params.bcast_sta_id)
  788. key_flags |= STA_KEY_MULTICAST_MSK;
  789. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  790. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  791. spin_lock_irqsave(&priv->sta_lock, flags);
  792. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  793. priv->stations[sta_id].keyinfo.keylen = 16;
  794. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  795. == STA_KEY_FLG_NO_ENC)
  796. priv->stations[sta_id].sta.key.key_offset =
  797. iwl_get_free_ucode_key_index(priv);
  798. /* else, we are overriding an existing key => no need to allocated room
  799. * in uCode. */
  800. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  801. "no space for a new key");
  802. priv->stations[sta_id].sta.key.key_flags = key_flags;
  803. /* This copy is acutally not needed: we get the key with each TX */
  804. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
  805. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
  806. spin_unlock_irqrestore(&priv->sta_lock, flags);
  807. return ret;
  808. }
  809. void iwl_update_tkip_key(struct iwl_priv *priv,
  810. struct ieee80211_key_conf *keyconf,
  811. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  812. {
  813. u8 sta_id;
  814. unsigned long flags;
  815. int i;
  816. if (iwl_scan_cancel(priv)) {
  817. /* cancel scan failed, just live w/ bad key and rely
  818. briefly on SW decryption */
  819. return;
  820. }
  821. sta_id = iwl_sta_id_or_broadcast(priv, sta);
  822. if (sta_id == IWL_INVALID_STATION)
  823. return;
  824. spin_lock_irqsave(&priv->sta_lock, flags);
  825. priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  826. for (i = 0; i < 5; i++)
  827. priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  828. cpu_to_le16(phase1key[i]);
  829. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  830. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  831. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  832. spin_unlock_irqrestore(&priv->sta_lock, flags);
  833. }
  834. EXPORT_SYMBOL(iwl_update_tkip_key);
  835. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  836. struct ieee80211_key_conf *keyconf,
  837. u8 sta_id)
  838. {
  839. unsigned long flags;
  840. u16 key_flags;
  841. u8 keyidx;
  842. struct iwl_addsta_cmd sta_cmd;
  843. lockdep_assert_held(&priv->mutex);
  844. priv->key_mapping_key--;
  845. spin_lock_irqsave(&priv->sta_lock, flags);
  846. key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
  847. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  848. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  849. keyconf->keyidx, sta_id);
  850. if (keyconf->keyidx != keyidx) {
  851. /* We need to remove a key with index different that the one
  852. * in the uCode. This means that the key we need to remove has
  853. * been replaced by another one with different index.
  854. * Don't do anything and return ok
  855. */
  856. spin_unlock_irqrestore(&priv->sta_lock, flags);
  857. return 0;
  858. }
  859. if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  860. IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
  861. keyconf->keyidx, key_flags);
  862. spin_unlock_irqrestore(&priv->sta_lock, flags);
  863. return 0;
  864. }
  865. if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
  866. &priv->ucode_key_table))
  867. IWL_ERR(priv, "index %d not used in uCode key table.\n",
  868. priv->stations[sta_id].sta.key.key_offset);
  869. memset(&priv->stations[sta_id].keyinfo, 0,
  870. sizeof(struct iwl_hw_key));
  871. memset(&priv->stations[sta_id].sta.key, 0,
  872. sizeof(struct iwl4965_keyinfo));
  873. priv->stations[sta_id].sta.key.key_flags =
  874. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  875. priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  876. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  877. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  878. if (iwl_is_rfkill(priv)) {
  879. IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
  880. spin_unlock_irqrestore(&priv->sta_lock, flags);
  881. return 0;
  882. }
  883. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  884. spin_unlock_irqrestore(&priv->sta_lock, flags);
  885. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  886. }
  887. EXPORT_SYMBOL(iwl_remove_dynamic_key);
  888. int iwl_set_dynamic_key(struct iwl_priv *priv,
  889. struct ieee80211_key_conf *keyconf, u8 sta_id)
  890. {
  891. int ret;
  892. lockdep_assert_held(&priv->mutex);
  893. priv->key_mapping_key++;
  894. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  895. switch (keyconf->cipher) {
  896. case WLAN_CIPHER_SUITE_CCMP:
  897. ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  898. break;
  899. case WLAN_CIPHER_SUITE_TKIP:
  900. ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  901. break;
  902. case WLAN_CIPHER_SUITE_WEP40:
  903. case WLAN_CIPHER_SUITE_WEP104:
  904. ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  905. break;
  906. default:
  907. IWL_ERR(priv,
  908. "Unknown alg: %s cipher = %x\n", __func__,
  909. keyconf->cipher);
  910. ret = -EINVAL;
  911. }
  912. IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
  913. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  914. sta_id, ret);
  915. return ret;
  916. }
  917. EXPORT_SYMBOL(iwl_set_dynamic_key);
  918. #ifdef CONFIG_IWLWIFI_DEBUG
  919. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  920. struct iwl_link_quality_cmd *lq)
  921. {
  922. int i;
  923. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  924. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  925. lq->general_params.single_stream_ant_msk,
  926. lq->general_params.dual_stream_ant_msk);
  927. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  928. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  929. i, lq->rs_table[i].rate_n_flags);
  930. }
  931. #else
  932. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  933. struct iwl_link_quality_cmd *lq)
  934. {
  935. }
  936. #endif
  937. /**
  938. * is_lq_table_valid() - Test one aspect of LQ cmd for validity
  939. *
  940. * It sometimes happens when a HT rate has been in use and we
  941. * loose connectivity with AP then mac80211 will first tell us that the
  942. * current channel is not HT anymore before removing the station. In such a
  943. * scenario the RXON flags will be updated to indicate we are not
  944. * communicating HT anymore, but the LQ command may still contain HT rates.
  945. * Test for this to prevent driver from sending LQ command between the time
  946. * RXON flags are updated and when LQ command is updated.
  947. */
  948. static bool is_lq_table_valid(struct iwl_priv *priv,
  949. struct iwl_link_quality_cmd *lq)
  950. {
  951. int i;
  952. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  953. if (ht_conf->is_ht)
  954. return true;
  955. IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
  956. priv->active_rxon.channel);
  957. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  958. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
  959. IWL_DEBUG_INFO(priv,
  960. "index %d of LQ expects HT channel\n",
  961. i);
  962. return false;
  963. }
  964. }
  965. return true;
  966. }
  967. /**
  968. * iwl_send_lq_cmd() - Send link quality command
  969. * @init: This command is sent as part of station initialization right
  970. * after station has been added.
  971. *
  972. * The link quality command is sent as the last step of station creation.
  973. * This is the special case in which init is set and we call a callback in
  974. * this case to clear the state indicating that station creation is in
  975. * progress.
  976. */
  977. int iwl_send_lq_cmd(struct iwl_priv *priv,
  978. struct iwl_link_quality_cmd *lq, u8 flags, bool init)
  979. {
  980. int ret = 0;
  981. unsigned long flags_spin;
  982. struct iwl_host_cmd cmd = {
  983. .id = REPLY_TX_LINK_QUALITY_CMD,
  984. .len = sizeof(struct iwl_link_quality_cmd),
  985. .flags = flags,
  986. .data = lq,
  987. };
  988. if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
  989. return -EINVAL;
  990. iwl_dump_lq_cmd(priv, lq);
  991. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  992. if (is_lq_table_valid(priv, lq))
  993. ret = iwl_send_cmd(priv, &cmd);
  994. else
  995. ret = -EINVAL;
  996. if (cmd.flags & CMD_ASYNC)
  997. return ret;
  998. if (init) {
  999. IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
  1000. lq->sta_id);
  1001. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  1002. priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  1003. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  1004. }
  1005. return ret;
  1006. }
  1007. EXPORT_SYMBOL(iwl_send_lq_cmd);
  1008. /**
  1009. * iwl_alloc_bcast_station - add broadcast station into driver's station table.
  1010. *
  1011. * This adds the broadcast station into the driver's station table
  1012. * and marks it driver active, so that it will be restored to the
  1013. * device at the next best time.
  1014. */
  1015. int iwl_alloc_bcast_station(struct iwl_priv *priv, bool init_lq)
  1016. {
  1017. struct iwl_link_quality_cmd *link_cmd;
  1018. unsigned long flags;
  1019. u8 sta_id;
  1020. spin_lock_irqsave(&priv->sta_lock, flags);
  1021. sta_id = iwl_prep_station(priv, iwl_bcast_addr, false, NULL);
  1022. if (sta_id == IWL_INVALID_STATION) {
  1023. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  1024. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1025. return -EINVAL;
  1026. }
  1027. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  1028. priv->stations[sta_id].used |= IWL_STA_BCAST;
  1029. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1030. if (init_lq) {
  1031. link_cmd = iwl_sta_alloc_lq(priv, sta_id);
  1032. if (!link_cmd) {
  1033. IWL_ERR(priv,
  1034. "Unable to initialize rate scaling for bcast station.\n");
  1035. return -ENOMEM;
  1036. }
  1037. spin_lock_irqsave(&priv->sta_lock, flags);
  1038. priv->stations[sta_id].lq = link_cmd;
  1039. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1040. }
  1041. return 0;
  1042. }
  1043. EXPORT_SYMBOL_GPL(iwl_alloc_bcast_station);
  1044. /**
  1045. * iwl_update_bcast_station - update broadcast station's LQ command
  1046. *
  1047. * Only used by iwlagn. Placed here to have all bcast station management
  1048. * code together.
  1049. */
  1050. int iwl_update_bcast_station(struct iwl_priv *priv)
  1051. {
  1052. unsigned long flags;
  1053. struct iwl_link_quality_cmd *link_cmd;
  1054. u8 sta_id = priv->hw_params.bcast_sta_id;
  1055. link_cmd = iwl_sta_alloc_lq(priv, sta_id);
  1056. if (!link_cmd) {
  1057. IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
  1058. return -ENOMEM;
  1059. }
  1060. spin_lock_irqsave(&priv->sta_lock, flags);
  1061. if (priv->stations[sta_id].lq)
  1062. kfree(priv->stations[sta_id].lq);
  1063. else
  1064. IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
  1065. priv->stations[sta_id].lq = link_cmd;
  1066. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1067. return 0;
  1068. }
  1069. EXPORT_SYMBOL_GPL(iwl_update_bcast_station);
  1070. void iwl_dealloc_bcast_station(struct iwl_priv *priv)
  1071. {
  1072. unsigned long flags;
  1073. int i;
  1074. spin_lock_irqsave(&priv->sta_lock, flags);
  1075. for (i = 0; i < priv->hw_params.max_stations; i++) {
  1076. if (!(priv->stations[i].used & IWL_STA_BCAST))
  1077. continue;
  1078. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  1079. priv->num_stations--;
  1080. BUG_ON(priv->num_stations < 0);
  1081. kfree(priv->stations[i].lq);
  1082. priv->stations[i].lq = NULL;
  1083. }
  1084. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1085. }
  1086. EXPORT_SYMBOL_GPL(iwl_dealloc_bcast_station);
  1087. /**
  1088. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1089. */
  1090. int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  1091. {
  1092. unsigned long flags;
  1093. struct iwl_addsta_cmd sta_cmd;
  1094. lockdep_assert_held(&priv->mutex);
  1095. /* Remove "disable" flag, to enable Tx for this TID */
  1096. spin_lock_irqsave(&priv->sta_lock, flags);
  1097. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1098. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1099. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1100. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1101. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1102. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1103. }
  1104. EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
  1105. int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1106. int tid, u16 ssn)
  1107. {
  1108. unsigned long flags;
  1109. int sta_id;
  1110. struct iwl_addsta_cmd sta_cmd;
  1111. lockdep_assert_held(&priv->mutex);
  1112. sta_id = iwl_sta_id(sta);
  1113. if (sta_id == IWL_INVALID_STATION)
  1114. return -ENXIO;
  1115. spin_lock_irqsave(&priv->sta_lock, flags);
  1116. priv->stations[sta_id].sta.station_flags_msk = 0;
  1117. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1118. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1119. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1120. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1121. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1122. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1123. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1124. }
  1125. EXPORT_SYMBOL(iwl_sta_rx_agg_start);
  1126. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1127. int tid)
  1128. {
  1129. unsigned long flags;
  1130. int sta_id;
  1131. struct iwl_addsta_cmd sta_cmd;
  1132. lockdep_assert_held(&priv->mutex);
  1133. sta_id = iwl_sta_id(sta);
  1134. if (sta_id == IWL_INVALID_STATION) {
  1135. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1136. return -ENXIO;
  1137. }
  1138. spin_lock_irqsave(&priv->sta_lock, flags);
  1139. priv->stations[sta_id].sta.station_flags_msk = 0;
  1140. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1141. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1142. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1143. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1144. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1145. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1146. }
  1147. EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
  1148. void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1149. {
  1150. unsigned long flags;
  1151. spin_lock_irqsave(&priv->sta_lock, flags);
  1152. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1153. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1154. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1155. priv->stations[sta_id].sta.sleep_tx_count = 0;
  1156. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1157. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1158. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1159. }
  1160. EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
  1161. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1162. {
  1163. unsigned long flags;
  1164. spin_lock_irqsave(&priv->sta_lock, flags);
  1165. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1166. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1167. priv->stations[sta_id].sta.sta.modify_mask =
  1168. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1169. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1170. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1171. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1172. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1173. }
  1174. EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
  1175. int iwl_mac_sta_remove(struct ieee80211_hw *hw,
  1176. struct ieee80211_vif *vif,
  1177. struct ieee80211_sta *sta)
  1178. {
  1179. struct iwl_priv *priv = hw->priv;
  1180. struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
  1181. int ret;
  1182. IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
  1183. sta->addr);
  1184. mutex_lock(&priv->mutex);
  1185. IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
  1186. sta->addr);
  1187. ret = iwl_remove_station(priv, sta_common->sta_id, sta->addr);
  1188. if (ret)
  1189. IWL_ERR(priv, "Error removing station %pM\n",
  1190. sta->addr);
  1191. mutex_unlock(&priv->mutex);
  1192. return ret;
  1193. }
  1194. EXPORT_SYMBOL(iwl_mac_sta_remove);