iwl-agn-sta.c 40 KB

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