iwl-agn-sta.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. __le16 key_flags;
  1009. /* if station isn't there, neither is the key */
  1010. if (sta_id == IWL_INVALID_STATION)
  1011. return -ENOENT;
  1012. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1013. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
  1014. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
  1015. sta_id = IWL_INVALID_STATION;
  1016. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1017. if (sta_id == IWL_INVALID_STATION)
  1018. return 0;
  1019. lockdep_assert_held(&priv->shrd->mutex);
  1020. ctx->key_mapping_keys--;
  1021. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  1022. keyconf->keyidx, sta_id);
  1023. if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
  1024. IWL_ERR(priv, "offset %d not used in uCode key table.\n",
  1025. keyconf->hw_key_idx);
  1026. key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  1027. key_flags |= STA_KEY_FLG_MAP_KEY_MSK | STA_KEY_FLG_NO_ENC |
  1028. STA_KEY_FLG_INVALID;
  1029. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1030. key_flags |= STA_KEY_MULTICAST_MSK;
  1031. sta_cmd.key.key_flags = key_flags;
  1032. sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
  1033. sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1034. sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
  1035. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1036. }
  1037. int iwl_set_dynamic_key(struct iwl_priv *priv,
  1038. struct iwl_rxon_context *ctx,
  1039. struct ieee80211_key_conf *keyconf,
  1040. struct ieee80211_sta *sta)
  1041. {
  1042. struct ieee80211_key_seq seq;
  1043. u16 p1k[5];
  1044. int ret;
  1045. u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
  1046. const u8 *addr;
  1047. if (sta_id == IWL_INVALID_STATION)
  1048. return -EINVAL;
  1049. lockdep_assert_held(&priv->shrd->mutex);
  1050. keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
  1051. if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
  1052. return -ENOSPC;
  1053. ctx->key_mapping_keys++;
  1054. switch (keyconf->cipher) {
  1055. case WLAN_CIPHER_SUITE_TKIP:
  1056. if (sta)
  1057. addr = sta->addr;
  1058. else /* station mode case only */
  1059. addr = ctx->active.bssid_addr;
  1060. /* pre-fill phase 1 key into device cache */
  1061. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1062. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1063. ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
  1064. seq.tkip.iv32, p1k, CMD_SYNC);
  1065. break;
  1066. case WLAN_CIPHER_SUITE_CCMP:
  1067. case WLAN_CIPHER_SUITE_WEP40:
  1068. case WLAN_CIPHER_SUITE_WEP104:
  1069. ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
  1070. 0, NULL, CMD_SYNC);
  1071. break;
  1072. default:
  1073. IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
  1074. ret = -EINVAL;
  1075. }
  1076. if (ret) {
  1077. ctx->key_mapping_keys--;
  1078. clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
  1079. }
  1080. IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1081. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1082. sta ? sta->addr : NULL, ret);
  1083. return ret;
  1084. }
  1085. /**
  1086. * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
  1087. *
  1088. * This adds the broadcast station into the driver's station table
  1089. * and marks it driver active, so that it will be restored to the
  1090. * device at the next best time.
  1091. */
  1092. int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
  1093. struct iwl_rxon_context *ctx)
  1094. {
  1095. struct iwl_link_quality_cmd *link_cmd;
  1096. unsigned long flags;
  1097. u8 sta_id;
  1098. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1099. sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
  1100. if (sta_id == IWL_INVALID_STATION) {
  1101. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  1102. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1103. return -EINVAL;
  1104. }
  1105. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  1106. priv->stations[sta_id].used |= IWL_STA_BCAST;
  1107. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1108. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  1109. if (!link_cmd) {
  1110. IWL_ERR(priv,
  1111. "Unable to initialize rate scaling for bcast station.\n");
  1112. return -ENOMEM;
  1113. }
  1114. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1115. priv->stations[sta_id].lq = link_cmd;
  1116. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1117. return 0;
  1118. }
  1119. /**
  1120. * iwl_update_bcast_station - update broadcast station's LQ command
  1121. *
  1122. * Only used by iwlagn. Placed here to have all bcast station management
  1123. * code together.
  1124. */
  1125. int iwl_update_bcast_station(struct iwl_priv *priv,
  1126. struct iwl_rxon_context *ctx)
  1127. {
  1128. unsigned long flags;
  1129. struct iwl_link_quality_cmd *link_cmd;
  1130. u8 sta_id = ctx->bcast_sta_id;
  1131. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  1132. if (!link_cmd) {
  1133. IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
  1134. return -ENOMEM;
  1135. }
  1136. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1137. if (priv->stations[sta_id].lq)
  1138. kfree(priv->stations[sta_id].lq);
  1139. else
  1140. IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
  1141. priv->stations[sta_id].lq = link_cmd;
  1142. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1143. return 0;
  1144. }
  1145. int iwl_update_bcast_stations(struct iwl_priv *priv)
  1146. {
  1147. struct iwl_rxon_context *ctx;
  1148. int ret = 0;
  1149. for_each_context(priv, ctx) {
  1150. ret = iwl_update_bcast_station(priv, ctx);
  1151. if (ret)
  1152. break;
  1153. }
  1154. return ret;
  1155. }
  1156. /**
  1157. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1158. */
  1159. int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  1160. {
  1161. unsigned long flags;
  1162. struct iwl_addsta_cmd sta_cmd;
  1163. lockdep_assert_held(&priv->shrd->mutex);
  1164. /* Remove "disable" flag, to enable Tx for this TID */
  1165. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1166. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1167. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1168. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1169. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1170. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1171. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1172. }
  1173. int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1174. int tid, u16 ssn)
  1175. {
  1176. unsigned long flags;
  1177. int sta_id;
  1178. struct iwl_addsta_cmd sta_cmd;
  1179. lockdep_assert_held(&priv->shrd->mutex);
  1180. sta_id = iwl_sta_id(sta);
  1181. if (sta_id == IWL_INVALID_STATION)
  1182. return -ENXIO;
  1183. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1184. priv->stations[sta_id].sta.station_flags_msk = 0;
  1185. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1186. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1187. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1188. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1189. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1190. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1191. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1192. }
  1193. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1194. int tid)
  1195. {
  1196. unsigned long flags;
  1197. int sta_id;
  1198. struct iwl_addsta_cmd sta_cmd;
  1199. lockdep_assert_held(&priv->shrd->mutex);
  1200. sta_id = iwl_sta_id(sta);
  1201. if (sta_id == IWL_INVALID_STATION) {
  1202. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1203. return -ENXIO;
  1204. }
  1205. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1206. priv->stations[sta_id].sta.station_flags_msk = 0;
  1207. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1208. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1209. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1210. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1211. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1212. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1213. }
  1214. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1215. {
  1216. unsigned long flags;
  1217. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1218. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1219. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1220. priv->stations[sta_id].sta.sta.modify_mask =
  1221. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1222. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1223. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1224. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1225. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1226. }