iwl-agn-sta.c 41 KB

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