iwl-agn-sta.c 40 KB

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