iwl-agn-sta.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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;
  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. memcpy(&lq, priv->stations[sta_id].lq, sizeof(lq));
  568. active = priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE;
  569. priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
  570. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  571. if (active) {
  572. ret = iwl_send_remove_station(
  573. priv, priv->stations[sta_id].sta.sta.addr,
  574. sta_id, true);
  575. if (ret)
  576. IWL_ERR(priv, "failed to remove STA %pM (%d)\n",
  577. priv->stations[sta_id].sta.sta.addr, ret);
  578. }
  579. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  580. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  581. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  582. ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  583. if (ret)
  584. IWL_ERR(priv, "failed to re-add STA %pM (%d)\n",
  585. priv->stations[sta_id].sta.sta.addr, ret);
  586. iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
  587. }
  588. int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
  589. {
  590. int i;
  591. for (i = 0; i < priv->sta_key_max_num; i++)
  592. if (!test_and_set_bit(i, &priv->ucode_key_table))
  593. return i;
  594. return WEP_INVALID_OFFSET;
  595. }
  596. void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
  597. {
  598. unsigned long flags;
  599. int i;
  600. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  601. for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
  602. if (!(priv->stations[i].used & IWL_STA_BCAST))
  603. continue;
  604. priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
  605. priv->num_stations--;
  606. if (WARN_ON(priv->num_stations < 0))
  607. priv->num_stations = 0;
  608. kfree(priv->stations[i].lq);
  609. priv->stations[i].lq = NULL;
  610. }
  611. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  612. }
  613. #ifdef CONFIG_IWLWIFI_DEBUG
  614. static void iwl_dump_lq_cmd(struct iwl_priv *priv,
  615. struct iwl_link_quality_cmd *lq)
  616. {
  617. int i;
  618. IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
  619. IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
  620. lq->general_params.single_stream_ant_msk,
  621. lq->general_params.dual_stream_ant_msk);
  622. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  623. IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
  624. i, lq->rs_table[i].rate_n_flags);
  625. }
  626. #else
  627. static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
  628. struct iwl_link_quality_cmd *lq)
  629. {
  630. }
  631. #endif
  632. /**
  633. * is_lq_table_valid() - Test one aspect of LQ cmd for validity
  634. *
  635. * It sometimes happens when a HT rate has been in use and we
  636. * loose connectivity with AP then mac80211 will first tell us that the
  637. * current channel is not HT anymore before removing the station. In such a
  638. * scenario the RXON flags will be updated to indicate we are not
  639. * communicating HT anymore, but the LQ command may still contain HT rates.
  640. * Test for this to prevent driver from sending LQ command between the time
  641. * RXON flags are updated and when LQ command is updated.
  642. */
  643. static bool is_lq_table_valid(struct iwl_priv *priv,
  644. struct iwl_rxon_context *ctx,
  645. struct iwl_link_quality_cmd *lq)
  646. {
  647. int i;
  648. if (ctx->ht.enabled)
  649. return true;
  650. IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
  651. ctx->active.channel);
  652. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  653. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
  654. RATE_MCS_HT_MSK) {
  655. IWL_DEBUG_INFO(priv,
  656. "index %d of LQ expects HT channel\n",
  657. i);
  658. return false;
  659. }
  660. }
  661. return true;
  662. }
  663. /**
  664. * iwl_send_lq_cmd() - Send link quality command
  665. * @init: This command is sent as part of station initialization right
  666. * after station has been added.
  667. *
  668. * The link quality command is sent as the last step of station creation.
  669. * This is the special case in which init is set and we call a callback in
  670. * this case to clear the state indicating that station creation is in
  671. * progress.
  672. */
  673. int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  674. struct iwl_link_quality_cmd *lq, u8 flags, bool init)
  675. {
  676. int ret = 0;
  677. unsigned long flags_spin;
  678. struct iwl_host_cmd cmd = {
  679. .id = REPLY_TX_LINK_QUALITY_CMD,
  680. .len = { sizeof(struct iwl_link_quality_cmd), },
  681. .flags = flags,
  682. .data = { lq, },
  683. };
  684. if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
  685. return -EINVAL;
  686. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  687. if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
  688. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  689. return -EINVAL;
  690. }
  691. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  692. iwl_dump_lq_cmd(priv, lq);
  693. if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
  694. return -EINVAL;
  695. if (is_lq_table_valid(priv, ctx, lq))
  696. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  697. else
  698. ret = -EINVAL;
  699. if (cmd.flags & CMD_ASYNC)
  700. return ret;
  701. if (init) {
  702. IWL_DEBUG_INFO(priv, "init LQ command complete, "
  703. "clearing sta addition status for sta %d\n",
  704. lq->sta_id);
  705. spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
  706. priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
  707. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
  708. }
  709. return ret;
  710. }
  711. int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
  712. struct ieee80211_vif *vif,
  713. struct ieee80211_sta *sta)
  714. {
  715. struct iwl_priv *priv = hw->priv;
  716. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  717. int ret;
  718. IWL_DEBUG_MAC80211(priv, "enter: received request to remove "
  719. "station %pM\n", sta->addr);
  720. mutex_lock(&priv->shrd->mutex);
  721. IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
  722. sta->addr);
  723. ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
  724. if (ret)
  725. IWL_ERR(priv, "Error removing station %pM\n",
  726. sta->addr);
  727. mutex_unlock(&priv->shrd->mutex);
  728. IWL_DEBUG_MAC80211(priv, "leave\n");
  729. return ret;
  730. }
  731. void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  732. u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
  733. {
  734. int i, r;
  735. u32 rate_flags = 0;
  736. __le32 rate_n_flags;
  737. lockdep_assert_held(&priv->shrd->mutex);
  738. memset(link_cmd, 0, sizeof(*link_cmd));
  739. /* Set up the rate scaling to start at selected rate, fall back
  740. * all the way down to 1M in IEEE order, and then spin on 1M */
  741. if (priv->band == IEEE80211_BAND_5GHZ)
  742. r = IWL_RATE_6M_INDEX;
  743. else if (ctx && ctx->vif && ctx->vif->p2p)
  744. r = IWL_RATE_6M_INDEX;
  745. else
  746. r = IWL_RATE_1M_INDEX;
  747. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  748. rate_flags |= RATE_MCS_CCK_MSK;
  749. rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
  750. RATE_MCS_ANT_POS;
  751. rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
  752. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  753. link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
  754. link_cmd->general_params.single_stream_ant_msk =
  755. first_antenna(hw_params(priv).valid_tx_ant);
  756. link_cmd->general_params.dual_stream_ant_msk =
  757. hw_params(priv).valid_tx_ant &
  758. ~first_antenna(hw_params(priv).valid_tx_ant);
  759. if (!link_cmd->general_params.dual_stream_ant_msk) {
  760. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  761. } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
  762. link_cmd->general_params.dual_stream_ant_msk =
  763. hw_params(priv).valid_tx_ant;
  764. }
  765. link_cmd->agg_params.agg_dis_start_th =
  766. LINK_QUAL_AGG_DISABLE_START_DEF;
  767. link_cmd->agg_params.agg_time_limit =
  768. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  769. link_cmd->sta_id = sta_id;
  770. }
  771. static struct iwl_link_quality_cmd *
  772. iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  773. u8 sta_id)
  774. {
  775. struct iwl_link_quality_cmd *link_cmd;
  776. link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
  777. if (!link_cmd) {
  778. IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
  779. return NULL;
  780. }
  781. iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
  782. return link_cmd;
  783. }
  784. /*
  785. * iwlagn_add_bssid_station - Add the special IBSS BSSID station
  786. *
  787. * Function sleeps.
  788. */
  789. int iwlagn_add_bssid_station(struct iwl_priv *priv,
  790. struct iwl_rxon_context *ctx,
  791. const u8 *addr, u8 *sta_id_r)
  792. {
  793. int ret;
  794. u8 sta_id;
  795. struct iwl_link_quality_cmd *link_cmd;
  796. unsigned long flags;
  797. if (sta_id_r)
  798. *sta_id_r = IWL_INVALID_STATION;
  799. ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
  800. if (ret) {
  801. IWL_ERR(priv, "Unable to add station %pM\n", addr);
  802. return ret;
  803. }
  804. if (sta_id_r)
  805. *sta_id_r = sta_id;
  806. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  807. priv->stations[sta_id].used |= IWL_STA_LOCAL;
  808. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  809. /* Set up default rate scaling table in device's station table */
  810. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  811. if (!link_cmd) {
  812. IWL_ERR(priv,
  813. "Unable to initialize rate scaling for station %pM.\n",
  814. addr);
  815. return -ENOMEM;
  816. }
  817. ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
  818. if (ret)
  819. IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
  820. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  821. priv->stations[sta_id].lq = link_cmd;
  822. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  823. return 0;
  824. }
  825. /*
  826. * static WEP keys
  827. *
  828. * For each context, the device has a table of 4 static WEP keys
  829. * (one for each key index) that is updated with the following
  830. * commands.
  831. */
  832. static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
  833. struct iwl_rxon_context *ctx,
  834. bool send_if_empty)
  835. {
  836. int i, not_empty = 0;
  837. u8 buff[sizeof(struct iwl_wep_cmd) +
  838. sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
  839. struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
  840. size_t cmd_size = sizeof(struct iwl_wep_cmd);
  841. struct iwl_host_cmd cmd = {
  842. .id = ctx->wep_key_cmd,
  843. .data = { wep_cmd, },
  844. .flags = CMD_SYNC,
  845. };
  846. might_sleep();
  847. memset(wep_cmd, 0, cmd_size +
  848. (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
  849. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  850. wep_cmd->key[i].key_index = i;
  851. if (ctx->wep_keys[i].key_size) {
  852. wep_cmd->key[i].key_offset = i;
  853. not_empty = 1;
  854. } else {
  855. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  856. }
  857. wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
  858. memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
  859. ctx->wep_keys[i].key_size);
  860. }
  861. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  862. wep_cmd->num_keys = WEP_KEYS_MAX;
  863. cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
  864. cmd.len[0] = cmd_size;
  865. if (not_empty || send_if_empty)
  866. return iwl_trans_send_cmd(trans(priv), &cmd);
  867. else
  868. return 0;
  869. }
  870. int iwl_restore_default_wep_keys(struct iwl_priv *priv,
  871. struct iwl_rxon_context *ctx)
  872. {
  873. lockdep_assert_held(&priv->shrd->mutex);
  874. return iwl_send_static_wepkey_cmd(priv, ctx, false);
  875. }
  876. int iwl_remove_default_wep_key(struct iwl_priv *priv,
  877. struct iwl_rxon_context *ctx,
  878. struct ieee80211_key_conf *keyconf)
  879. {
  880. int ret;
  881. lockdep_assert_held(&priv->shrd->mutex);
  882. IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
  883. keyconf->keyidx);
  884. memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
  885. if (iwl_is_rfkill(priv->shrd)) {
  886. IWL_DEBUG_WEP(priv,
  887. "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  888. /* but keys in device are clear anyway so return success */
  889. return 0;
  890. }
  891. ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
  892. IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
  893. keyconf->keyidx, ret);
  894. return ret;
  895. }
  896. int iwl_set_default_wep_key(struct iwl_priv *priv,
  897. struct iwl_rxon_context *ctx,
  898. struct ieee80211_key_conf *keyconf)
  899. {
  900. int ret;
  901. lockdep_assert_held(&priv->shrd->mutex);
  902. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  903. keyconf->keylen != WEP_KEY_LEN_64) {
  904. IWL_DEBUG_WEP(priv,
  905. "Bad WEP key length %d\n", keyconf->keylen);
  906. return -EINVAL;
  907. }
  908. keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
  909. ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  910. memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
  911. keyconf->keylen);
  912. ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
  913. IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
  914. keyconf->keylen, keyconf->keyidx, ret);
  915. return ret;
  916. }
  917. /*
  918. * dynamic (per-station) keys
  919. *
  920. * The dynamic keys are a little more complicated. The device has
  921. * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
  922. * These are linked to stations by a table that contains an index
  923. * into the key table for each station/key index/{mcast,unicast},
  924. * i.e. it's basically an array of pointers like this:
  925. * key_offset_t key_mapping[NUM_STATIONS][4][2];
  926. * (it really works differently, but you can think of it as such)
  927. *
  928. * The key uploading and linking happens in the same command, the
  929. * add station command with STA_MODIFY_KEY_MASK.
  930. */
  931. static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
  932. struct ieee80211_vif *vif,
  933. struct ieee80211_sta *sta)
  934. {
  935. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  936. u8 sta_id = IWL_INVALID_STATION;
  937. if (sta)
  938. sta_id = iwl_sta_id(sta);
  939. /*
  940. * The device expects GTKs for station interfaces to be
  941. * installed as GTKs for the AP station. If we have no
  942. * station ID, then use the ap_sta_id in that case.
  943. */
  944. if (!sta && vif && vif_priv->ctx) {
  945. switch (vif->type) {
  946. case NL80211_IFTYPE_STATION:
  947. sta_id = vif_priv->ctx->ap_sta_id;
  948. break;
  949. default:
  950. /*
  951. * In all other cases, the key will be
  952. * used either for TX only or is bound
  953. * to a station already.
  954. */
  955. break;
  956. }
  957. }
  958. return sta_id;
  959. }
  960. static int iwlagn_send_sta_key(struct iwl_priv *priv,
  961. struct ieee80211_key_conf *keyconf,
  962. u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
  963. u32 cmd_flags)
  964. {
  965. unsigned long flags;
  966. __le16 key_flags;
  967. struct iwl_addsta_cmd sta_cmd;
  968. int i;
  969. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  970. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
  971. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  972. key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  973. key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
  974. switch (keyconf->cipher) {
  975. case WLAN_CIPHER_SUITE_CCMP:
  976. key_flags |= STA_KEY_FLG_CCMP;
  977. memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
  978. break;
  979. case WLAN_CIPHER_SUITE_TKIP:
  980. key_flags |= STA_KEY_FLG_TKIP;
  981. sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
  982. for (i = 0; i < 5; i++)
  983. sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
  984. memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
  985. break;
  986. case WLAN_CIPHER_SUITE_WEP104:
  987. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  988. /* fall through */
  989. case WLAN_CIPHER_SUITE_WEP40:
  990. key_flags |= STA_KEY_FLG_WEP;
  991. memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
  992. break;
  993. default:
  994. WARN_ON(1);
  995. return -EINVAL;
  996. }
  997. if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  998. key_flags |= STA_KEY_MULTICAST_MSK;
  999. /* key pointer (offset) */
  1000. sta_cmd.key.key_offset = keyconf->hw_key_idx;
  1001. sta_cmd.key.key_flags = key_flags;
  1002. sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
  1003. sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1004. return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
  1005. }
  1006. void iwl_update_tkip_key(struct iwl_priv *priv,
  1007. struct ieee80211_vif *vif,
  1008. struct ieee80211_key_conf *keyconf,
  1009. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  1010. {
  1011. u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
  1012. if (sta_id == IWL_INVALID_STATION)
  1013. return;
  1014. if (iwl_scan_cancel(priv)) {
  1015. /* cancel scan failed, just live w/ bad key and rely
  1016. briefly on SW decryption */
  1017. return;
  1018. }
  1019. iwlagn_send_sta_key(priv, keyconf, sta_id,
  1020. iv32, phase1key, CMD_ASYNC);
  1021. }
  1022. int iwl_remove_dynamic_key(struct iwl_priv *priv,
  1023. struct iwl_rxon_context *ctx,
  1024. struct ieee80211_key_conf *keyconf,
  1025. struct ieee80211_sta *sta)
  1026. {
  1027. unsigned long flags;
  1028. struct iwl_addsta_cmd sta_cmd;
  1029. u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
  1030. /* if station isn't there, neither is the key */
  1031. if (sta_id == IWL_INVALID_STATION)
  1032. return -ENOENT;
  1033. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1034. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
  1035. if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
  1036. sta_id = IWL_INVALID_STATION;
  1037. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1038. if (sta_id == IWL_INVALID_STATION)
  1039. return 0;
  1040. lockdep_assert_held(&priv->shrd->mutex);
  1041. ctx->key_mapping_keys--;
  1042. IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
  1043. keyconf->keyidx, sta_id);
  1044. if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
  1045. IWL_ERR(priv, "offset %d not used in uCode key table.\n",
  1046. keyconf->hw_key_idx);
  1047. sta_cmd.key.key_flags = STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  1048. sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
  1049. sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1050. sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
  1051. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1052. }
  1053. int iwl_set_dynamic_key(struct iwl_priv *priv,
  1054. struct iwl_rxon_context *ctx,
  1055. struct ieee80211_key_conf *keyconf,
  1056. struct ieee80211_sta *sta)
  1057. {
  1058. struct ieee80211_key_seq seq;
  1059. u16 p1k[5];
  1060. int ret;
  1061. u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
  1062. const u8 *addr;
  1063. if (sta_id == IWL_INVALID_STATION)
  1064. return -EINVAL;
  1065. lockdep_assert_held(&priv->shrd->mutex);
  1066. keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
  1067. if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
  1068. return -ENOSPC;
  1069. ctx->key_mapping_keys++;
  1070. switch (keyconf->cipher) {
  1071. case WLAN_CIPHER_SUITE_TKIP:
  1072. if (sta)
  1073. addr = sta->addr;
  1074. else /* station mode case only */
  1075. addr = ctx->active.bssid_addr;
  1076. /* pre-fill phase 1 key into device cache */
  1077. ieee80211_get_key_rx_seq(keyconf, 0, &seq);
  1078. ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
  1079. ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
  1080. seq.tkip.iv32, p1k, CMD_SYNC);
  1081. break;
  1082. case WLAN_CIPHER_SUITE_CCMP:
  1083. case WLAN_CIPHER_SUITE_WEP40:
  1084. case WLAN_CIPHER_SUITE_WEP104:
  1085. ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
  1086. 0, NULL, CMD_SYNC);
  1087. break;
  1088. default:
  1089. IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
  1090. ret = -EINVAL;
  1091. }
  1092. if (ret) {
  1093. ctx->key_mapping_keys--;
  1094. clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
  1095. }
  1096. IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
  1097. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1098. sta ? sta->addr : NULL, ret);
  1099. return ret;
  1100. }
  1101. /**
  1102. * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
  1103. *
  1104. * This adds the broadcast station into the driver's station table
  1105. * and marks it driver active, so that it will be restored to the
  1106. * device at the next best time.
  1107. */
  1108. int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
  1109. struct iwl_rxon_context *ctx)
  1110. {
  1111. struct iwl_link_quality_cmd *link_cmd;
  1112. unsigned long flags;
  1113. u8 sta_id;
  1114. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1115. sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
  1116. if (sta_id == IWL_INVALID_STATION) {
  1117. IWL_ERR(priv, "Unable to prepare broadcast station\n");
  1118. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1119. return -EINVAL;
  1120. }
  1121. priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
  1122. priv->stations[sta_id].used |= IWL_STA_BCAST;
  1123. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1124. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  1125. if (!link_cmd) {
  1126. IWL_ERR(priv,
  1127. "Unable to initialize rate scaling for bcast station.\n");
  1128. return -ENOMEM;
  1129. }
  1130. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1131. priv->stations[sta_id].lq = link_cmd;
  1132. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1133. return 0;
  1134. }
  1135. /**
  1136. * iwl_update_bcast_station - update broadcast station's LQ command
  1137. *
  1138. * Only used by iwlagn. Placed here to have all bcast station management
  1139. * code together.
  1140. */
  1141. int iwl_update_bcast_station(struct iwl_priv *priv,
  1142. struct iwl_rxon_context *ctx)
  1143. {
  1144. unsigned long flags;
  1145. struct iwl_link_quality_cmd *link_cmd;
  1146. u8 sta_id = ctx->bcast_sta_id;
  1147. link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
  1148. if (!link_cmd) {
  1149. IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
  1150. return -ENOMEM;
  1151. }
  1152. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1153. if (priv->stations[sta_id].lq)
  1154. kfree(priv->stations[sta_id].lq);
  1155. else
  1156. IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
  1157. priv->stations[sta_id].lq = link_cmd;
  1158. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1159. return 0;
  1160. }
  1161. int iwl_update_bcast_stations(struct iwl_priv *priv)
  1162. {
  1163. struct iwl_rxon_context *ctx;
  1164. int ret = 0;
  1165. for_each_context(priv, ctx) {
  1166. ret = iwl_update_bcast_station(priv, ctx);
  1167. if (ret)
  1168. break;
  1169. }
  1170. return ret;
  1171. }
  1172. /**
  1173. * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1174. */
  1175. int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
  1176. {
  1177. unsigned long flags;
  1178. struct iwl_addsta_cmd sta_cmd;
  1179. lockdep_assert_held(&priv->shrd->mutex);
  1180. /* Remove "disable" flag, to enable Tx for this TID */
  1181. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1182. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1183. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1184. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1185. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1186. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1187. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1188. }
  1189. int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1190. int tid, u16 ssn)
  1191. {
  1192. unsigned long flags;
  1193. int sta_id;
  1194. struct iwl_addsta_cmd sta_cmd;
  1195. lockdep_assert_held(&priv->shrd->mutex);
  1196. sta_id = iwl_sta_id(sta);
  1197. if (sta_id == IWL_INVALID_STATION)
  1198. return -ENXIO;
  1199. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1200. priv->stations[sta_id].sta.station_flags_msk = 0;
  1201. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1202. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1203. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1204. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1205. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1206. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1207. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1208. }
  1209. int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
  1210. int tid)
  1211. {
  1212. unsigned long flags;
  1213. int sta_id;
  1214. struct iwl_addsta_cmd sta_cmd;
  1215. lockdep_assert_held(&priv->shrd->mutex);
  1216. sta_id = iwl_sta_id(sta);
  1217. if (sta_id == IWL_INVALID_STATION) {
  1218. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  1219. return -ENXIO;
  1220. }
  1221. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1222. priv->stations[sta_id].sta.station_flags_msk = 0;
  1223. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1224. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1225. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1226. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
  1227. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1228. return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  1229. }
  1230. static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1231. {
  1232. unsigned long flags;
  1233. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1234. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1235. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1236. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1237. priv->stations[sta_id].sta.sleep_tx_count = 0;
  1238. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1239. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1240. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1241. }
  1242. void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
  1243. {
  1244. unsigned long flags;
  1245. spin_lock_irqsave(&priv->shrd->sta_lock, flags);
  1246. priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1247. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1248. priv->stations[sta_id].sta.sta.modify_mask =
  1249. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1250. priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1251. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1252. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1253. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  1254. }
  1255. void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
  1256. struct ieee80211_vif *vif,
  1257. enum sta_notify_cmd cmd,
  1258. struct ieee80211_sta *sta)
  1259. {
  1260. struct iwl_priv *priv = hw->priv;
  1261. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  1262. int sta_id;
  1263. IWL_DEBUG_MAC80211(priv, "enter\n");
  1264. switch (cmd) {
  1265. case STA_NOTIFY_SLEEP:
  1266. WARN_ON(!sta_priv->client);
  1267. sta_priv->asleep = true;
  1268. if (atomic_read(&sta_priv->pending_frames) > 0)
  1269. ieee80211_sta_block_awake(hw, sta, true);
  1270. break;
  1271. case STA_NOTIFY_AWAKE:
  1272. WARN_ON(!sta_priv->client);
  1273. if (!sta_priv->asleep)
  1274. break;
  1275. sta_priv->asleep = false;
  1276. sta_id = iwl_sta_id(sta);
  1277. if (sta_id != IWL_INVALID_STATION)
  1278. iwl_sta_modify_ps_wake(priv, sta_id);
  1279. break;
  1280. default:
  1281. break;
  1282. }
  1283. IWL_DEBUG_MAC80211(priv, "leave\n");
  1284. }