iwl-sta.c 39 KB

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