iwl-mac80211.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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 <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/firmware.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <net/mac80211.h>
  42. #include <asm/div64.h>
  43. #include "iwl-eeprom.h"
  44. #include "iwl-dev.h"
  45. #include "iwl-core.h"
  46. #include "iwl-io.h"
  47. #include "iwl-agn-calib.h"
  48. #include "iwl-agn.h"
  49. #include "iwl-shared.h"
  50. #include "iwl-bus.h"
  51. #include "iwl-trans.h"
  52. /*****************************************************************************
  53. *
  54. * mac80211 entry point functions
  55. *
  56. *****************************************************************************/
  57. static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
  58. {
  59. .max = 1,
  60. .types = BIT(NL80211_IFTYPE_STATION),
  61. },
  62. {
  63. .max = 1,
  64. .types = BIT(NL80211_IFTYPE_AP),
  65. },
  66. };
  67. static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
  68. {
  69. .max = 2,
  70. .types = BIT(NL80211_IFTYPE_STATION),
  71. },
  72. };
  73. static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
  74. {
  75. .max = 1,
  76. .types = BIT(NL80211_IFTYPE_STATION),
  77. },
  78. {
  79. .max = 1,
  80. .types = BIT(NL80211_IFTYPE_P2P_GO) |
  81. BIT(NL80211_IFTYPE_AP),
  82. },
  83. };
  84. static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
  85. {
  86. .max = 2,
  87. .types = BIT(NL80211_IFTYPE_STATION),
  88. },
  89. {
  90. .max = 1,
  91. .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
  92. },
  93. };
  94. static const struct ieee80211_iface_combination
  95. iwlagn_iface_combinations_dualmode[] = {
  96. { .num_different_channels = 1,
  97. .max_interfaces = 2,
  98. .beacon_int_infra_match = true,
  99. .limits = iwlagn_sta_ap_limits,
  100. .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
  101. },
  102. { .num_different_channels = 1,
  103. .max_interfaces = 2,
  104. .limits = iwlagn_2sta_limits,
  105. .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
  106. },
  107. };
  108. static const struct ieee80211_iface_combination
  109. iwlagn_iface_combinations_p2p[] = {
  110. { .num_different_channels = 1,
  111. .max_interfaces = 2,
  112. .beacon_int_infra_match = true,
  113. .limits = iwlagn_p2p_sta_go_limits,
  114. .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
  115. },
  116. { .num_different_channels = 1,
  117. .max_interfaces = 2,
  118. .limits = iwlagn_p2p_2sta_limits,
  119. .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
  120. },
  121. };
  122. /*
  123. * Not a mac80211 entry point function, but it fits in with all the
  124. * other mac80211 functions grouped here.
  125. */
  126. int iwlagn_mac_setup_register(struct iwl_priv *priv,
  127. struct iwlagn_ucode_capabilities *capa)
  128. {
  129. int ret;
  130. struct ieee80211_hw *hw = priv->hw;
  131. struct iwl_rxon_context *ctx;
  132. hw->rate_control_algorithm = "iwl-agn-rs";
  133. /* Tell mac80211 our characteristics */
  134. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  135. IEEE80211_HW_AMPDU_AGGREGATION |
  136. IEEE80211_HW_NEED_DTIM_PERIOD |
  137. IEEE80211_HW_SPECTRUM_MGMT |
  138. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  139. /*
  140. * Including the following line will crash some AP's. This
  141. * workaround removes the stimulus which causes the crash until
  142. * the AP software can be fixed.
  143. hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  144. */
  145. hw->flags |= IEEE80211_HW_SUPPORTS_PS |
  146. IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
  147. if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
  148. hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  149. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  150. if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
  151. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  152. hw->sta_data_size = sizeof(struct iwl_station_priv);
  153. hw->vif_data_size = sizeof(struct iwl_vif_priv);
  154. for_each_context(priv, ctx) {
  155. hw->wiphy->interface_modes |= ctx->interface_modes;
  156. hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
  157. }
  158. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  159. if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
  160. hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
  161. hw->wiphy->n_iface_combinations =
  162. ARRAY_SIZE(iwlagn_iface_combinations_p2p);
  163. } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
  164. hw->wiphy->iface_combinations =
  165. iwlagn_iface_combinations_dualmode;
  166. hw->wiphy->n_iface_combinations =
  167. ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
  168. }
  169. hw->wiphy->max_remain_on_channel_duration = 1000;
  170. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  171. WIPHY_FLAG_DISABLE_BEACON_HINTS |
  172. WIPHY_FLAG_IBSS_RSN;
  173. if (trans(priv)->ucode_wowlan.code.len &&
  174. device_can_wakeup(bus(priv)->dev)) {
  175. hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  176. WIPHY_WOWLAN_DISCONNECT |
  177. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  178. WIPHY_WOWLAN_RFKILL_RELEASE;
  179. if (!iwlagn_mod_params.sw_crypto)
  180. hw->wiphy->wowlan.flags |=
  181. WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  182. WIPHY_WOWLAN_GTK_REKEY_FAILURE;
  183. hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
  184. hw->wiphy->wowlan.pattern_min_len =
  185. IWLAGN_WOWLAN_MIN_PATTERN_LEN;
  186. hw->wiphy->wowlan.pattern_max_len =
  187. IWLAGN_WOWLAN_MAX_PATTERN_LEN;
  188. }
  189. if (iwlagn_mod_params.power_save)
  190. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  191. else
  192. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  193. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  194. /* we create the 802.11 header and a zero-length SSID element */
  195. hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 2;
  196. /* Default value; 4 EDCA QOS priorities */
  197. hw->queues = 4;
  198. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  199. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  200. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  201. &priv->bands[IEEE80211_BAND_2GHZ];
  202. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  203. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  204. &priv->bands[IEEE80211_BAND_5GHZ];
  205. iwl_leds_init(priv);
  206. ret = ieee80211_register_hw(priv->hw);
  207. if (ret) {
  208. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  209. return ret;
  210. }
  211. priv->mac80211_registered = 1;
  212. return 0;
  213. }
  214. static int __iwl_up(struct iwl_priv *priv)
  215. {
  216. struct iwl_rxon_context *ctx;
  217. int ret;
  218. lockdep_assert_held(&priv->shrd->mutex);
  219. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  220. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  221. return -EIO;
  222. }
  223. for_each_context(priv, ctx) {
  224. ret = iwlagn_alloc_bcast_station(priv, ctx);
  225. if (ret) {
  226. iwl_dealloc_bcast_stations(priv);
  227. return ret;
  228. }
  229. }
  230. ret = iwlagn_run_init_ucode(priv);
  231. if (ret) {
  232. IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
  233. goto error;
  234. }
  235. ret = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
  236. if (ret) {
  237. IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
  238. goto error;
  239. }
  240. ret = iwl_alive_start(priv);
  241. if (ret)
  242. goto error;
  243. return 0;
  244. error:
  245. set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  246. __iwl_down(priv);
  247. clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  248. IWL_ERR(priv, "Unable to initialize device.\n");
  249. return ret;
  250. }
  251. static int iwlagn_mac_start(struct ieee80211_hw *hw)
  252. {
  253. struct iwl_priv *priv = hw->priv;
  254. int ret;
  255. IWL_DEBUG_MAC80211(priv, "enter\n");
  256. /* we should be verifying the device is ready to be opened */
  257. mutex_lock(&priv->shrd->mutex);
  258. ret = __iwl_up(priv);
  259. mutex_unlock(&priv->shrd->mutex);
  260. if (ret)
  261. return ret;
  262. IWL_DEBUG_INFO(priv, "Start UP work done.\n");
  263. /* Now we should be done, and the READY bit should be set. */
  264. if (WARN_ON(!test_bit(STATUS_READY, &priv->shrd->status)))
  265. ret = -EIO;
  266. iwlagn_led_enable(priv);
  267. priv->is_open = 1;
  268. IWL_DEBUG_MAC80211(priv, "leave\n");
  269. return 0;
  270. }
  271. static void iwlagn_mac_stop(struct ieee80211_hw *hw)
  272. {
  273. struct iwl_priv *priv = hw->priv;
  274. IWL_DEBUG_MAC80211(priv, "enter\n");
  275. if (!priv->is_open)
  276. return;
  277. priv->is_open = 0;
  278. iwl_down(priv);
  279. flush_workqueue(priv->shrd->workqueue);
  280. /* User space software may expect getting rfkill changes
  281. * even if interface is down */
  282. iwl_write32(bus(priv), CSR_INT, 0xFFFFFFFF);
  283. iwl_enable_rfkill_int(priv);
  284. IWL_DEBUG_MAC80211(priv, "leave\n");
  285. }
  286. static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
  287. struct ieee80211_vif *vif,
  288. struct cfg80211_gtk_rekey_data *data)
  289. {
  290. struct iwl_priv *priv = hw->priv;
  291. if (iwlagn_mod_params.sw_crypto)
  292. return;
  293. IWL_DEBUG_MAC80211(priv, "enter\n");
  294. mutex_lock(&priv->shrd->mutex);
  295. if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
  296. goto out;
  297. memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
  298. memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
  299. priv->replay_ctr =
  300. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  301. priv->have_rekey_data = true;
  302. out:
  303. mutex_unlock(&priv->shrd->mutex);
  304. IWL_DEBUG_MAC80211(priv, "leave\n");
  305. }
  306. #ifdef CONFIG_PM_SLEEP
  307. struct wowlan_key_data {
  308. struct iwl_rxon_context *ctx;
  309. struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  310. struct iwlagn_wowlan_tkip_params_cmd *tkip;
  311. const u8 *bssid;
  312. bool error, use_rsc_tsc, use_tkip;
  313. };
  314. static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
  315. {
  316. int i;
  317. for (i = 0; i < IWLAGN_P1K_SIZE; i++)
  318. out[i] = cpu_to_le16(p1k[i]);
  319. }
  320. static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
  321. struct ieee80211_vif *vif,
  322. struct ieee80211_sta *sta,
  323. struct ieee80211_key_conf *key,
  324. void *_data)
  325. {
  326. struct iwl_priv *priv = hw->priv;
  327. struct wowlan_key_data *data = _data;
  328. struct iwl_rxon_context *ctx = data->ctx;
  329. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  330. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  331. struct iwlagn_p1k_cache *rx_p1ks;
  332. u8 *rx_mic_key;
  333. struct ieee80211_key_seq seq;
  334. u32 cur_rx_iv32 = 0;
  335. u16 p1k[IWLAGN_P1K_SIZE];
  336. int ret, i;
  337. mutex_lock(&priv->shrd->mutex);
  338. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  339. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  340. !sta && !ctx->key_mapping_keys)
  341. ret = iwl_set_default_wep_key(priv, ctx, key);
  342. else
  343. ret = iwl_set_dynamic_key(priv, ctx, key, sta);
  344. if (ret) {
  345. IWL_ERR(priv, "Error setting key during suspend!\n");
  346. data->error = true;
  347. }
  348. switch (key->cipher) {
  349. case WLAN_CIPHER_SUITE_TKIP:
  350. if (sta) {
  351. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  352. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  353. rx_p1ks = data->tkip->rx_uni;
  354. ieee80211_get_key_tx_seq(key, &seq);
  355. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  356. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  357. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  358. iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
  359. memcpy(data->tkip->mic_keys.tx,
  360. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  361. IWLAGN_MIC_KEY_SIZE);
  362. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  363. } else {
  364. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  365. rx_p1ks = data->tkip->rx_multi;
  366. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  367. }
  368. /*
  369. * For non-QoS this relies on the fact that both the uCode and
  370. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  371. * for checking the IV in the frames.
  372. */
  373. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  374. ieee80211_get_key_rx_seq(key, i, &seq);
  375. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  376. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  377. /* wrapping isn't allowed, AP must rekey */
  378. if (seq.tkip.iv32 > cur_rx_iv32)
  379. cur_rx_iv32 = seq.tkip.iv32;
  380. }
  381. ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
  382. iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
  383. ieee80211_get_tkip_rx_p1k(key, data->bssid,
  384. cur_rx_iv32 + 1, p1k);
  385. iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
  386. memcpy(rx_mic_key,
  387. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  388. IWLAGN_MIC_KEY_SIZE);
  389. data->use_tkip = true;
  390. data->use_rsc_tsc = true;
  391. break;
  392. case WLAN_CIPHER_SUITE_CCMP:
  393. if (sta) {
  394. u8 *pn = seq.ccmp.pn;
  395. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  396. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  397. ieee80211_get_key_tx_seq(key, &seq);
  398. aes_tx_sc->pn = cpu_to_le64(
  399. (u64)pn[5] |
  400. ((u64)pn[4] << 8) |
  401. ((u64)pn[3] << 16) |
  402. ((u64)pn[2] << 24) |
  403. ((u64)pn[1] << 32) |
  404. ((u64)pn[0] << 40));
  405. } else
  406. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  407. /*
  408. * For non-QoS this relies on the fact that both the uCode and
  409. * mac80211 use TID 0 for checking the IV in the frames.
  410. */
  411. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  412. u8 *pn = seq.ccmp.pn;
  413. ieee80211_get_key_rx_seq(key, i, &seq);
  414. aes_sc->pn = cpu_to_le64(
  415. (u64)pn[5] |
  416. ((u64)pn[4] << 8) |
  417. ((u64)pn[3] << 16) |
  418. ((u64)pn[2] << 24) |
  419. ((u64)pn[1] << 32) |
  420. ((u64)pn[0] << 40));
  421. }
  422. data->use_rsc_tsc = true;
  423. break;
  424. }
  425. mutex_unlock(&priv->shrd->mutex);
  426. }
  427. static int iwlagn_send_patterns(struct iwl_priv *priv,
  428. struct cfg80211_wowlan *wowlan)
  429. {
  430. struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
  431. struct iwl_host_cmd cmd = {
  432. .id = REPLY_WOWLAN_PATTERNS,
  433. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  434. .flags = CMD_SYNC,
  435. };
  436. int i, err;
  437. if (!wowlan->n_patterns)
  438. return 0;
  439. cmd.len[0] = sizeof(*pattern_cmd) +
  440. wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
  441. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  442. if (!pattern_cmd)
  443. return -ENOMEM;
  444. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  445. for (i = 0; i < wowlan->n_patterns; i++) {
  446. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  447. memcpy(&pattern_cmd->patterns[i].mask,
  448. wowlan->patterns[i].mask, mask_len);
  449. memcpy(&pattern_cmd->patterns[i].pattern,
  450. wowlan->patterns[i].pattern,
  451. wowlan->patterns[i].pattern_len);
  452. pattern_cmd->patterns[i].mask_size = mask_len;
  453. pattern_cmd->patterns[i].pattern_size =
  454. wowlan->patterns[i].pattern_len;
  455. }
  456. cmd.data[0] = pattern_cmd;
  457. err = iwl_trans_send_cmd(trans(priv), &cmd);
  458. kfree(pattern_cmd);
  459. return err;
  460. }
  461. static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
  462. struct cfg80211_wowlan *wowlan)
  463. {
  464. struct iwl_priv *priv = hw->priv;
  465. struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
  466. struct iwl_rxon_cmd rxon;
  467. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  468. struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
  469. struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
  470. struct wowlan_key_data key_data = {
  471. .ctx = ctx,
  472. .bssid = ctx->active.bssid_addr,
  473. .use_rsc_tsc = false,
  474. .tkip = &tkip_cmd,
  475. .use_tkip = false,
  476. };
  477. struct iwlagn_d3_config_cmd d3_cfg_cmd = {};
  478. int ret, i;
  479. u16 seq;
  480. if (WARN_ON(!wowlan))
  481. return -EINVAL;
  482. IWL_DEBUG_MAC80211(priv, "enter\n");
  483. mutex_lock(&priv->shrd->mutex);
  484. /* Don't attempt WoWLAN when not associated, tear down instead. */
  485. if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
  486. !iwl_is_associated_ctx(ctx)) {
  487. ret = 1;
  488. goto out;
  489. }
  490. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  491. if (!key_data.rsc_tsc) {
  492. ret = -ENOMEM;
  493. goto out;
  494. }
  495. memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
  496. /*
  497. * We know the last used seqno, and the uCode expects to know that
  498. * one, it will increment before TX.
  499. */
  500. seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
  501. wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
  502. /*
  503. * For QoS counters, we store the one to use next, so subtract 0x10
  504. * since the uCode will add 0x10 before using the value.
  505. */
  506. for (i = 0; i < 8; i++) {
  507. seq = priv->shrd->tid_data[IWL_AP_ID][i].seq_number;
  508. seq -= 0x10;
  509. wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
  510. }
  511. if (wowlan->disconnect)
  512. wakeup_filter_cmd.enabled |=
  513. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
  514. IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
  515. if (wowlan->magic_pkt)
  516. wakeup_filter_cmd.enabled |=
  517. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
  518. if (wowlan->gtk_rekey_failure)
  519. wakeup_filter_cmd.enabled |=
  520. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  521. if (wowlan->eap_identity_req)
  522. wakeup_filter_cmd.enabled |=
  523. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  524. if (wowlan->four_way_handshake)
  525. wakeup_filter_cmd.enabled |=
  526. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  527. if (wowlan->n_patterns)
  528. wakeup_filter_cmd.enabled |=
  529. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
  530. if (wowlan->rfkill_release)
  531. d3_cfg_cmd.wakeup_flags |=
  532. cpu_to_le32(IWLAGN_D3_WAKEUP_RFKILL);
  533. iwl_scan_cancel_timeout(priv, 200);
  534. memcpy(&rxon, &ctx->active, sizeof(rxon));
  535. iwl_trans_stop_device(trans(priv));
  536. priv->shrd->wowlan = true;
  537. ret = iwlagn_load_ucode_wait_alive(priv, IWL_UCODE_WOWLAN);
  538. if (ret)
  539. goto error;
  540. /* now configure WoWLAN ucode */
  541. ret = iwl_alive_start(priv);
  542. if (ret)
  543. goto error;
  544. memcpy(&ctx->staging, &rxon, sizeof(rxon));
  545. ret = iwlagn_commit_rxon(priv, ctx);
  546. if (ret)
  547. goto error;
  548. ret = iwl_power_update_mode(priv, true);
  549. if (ret)
  550. goto error;
  551. if (!iwlagn_mod_params.sw_crypto) {
  552. /* mark all keys clear */
  553. priv->ucode_key_table = 0;
  554. ctx->key_mapping_keys = 0;
  555. /*
  556. * This needs to be unlocked due to lock ordering
  557. * constraints. Since we're in the suspend path
  558. * that isn't really a problem though.
  559. */
  560. mutex_unlock(&priv->shrd->mutex);
  561. ieee80211_iter_keys(priv->hw, ctx->vif,
  562. iwlagn_wowlan_program_keys,
  563. &key_data);
  564. mutex_lock(&priv->shrd->mutex);
  565. if (key_data.error) {
  566. ret = -EIO;
  567. goto error;
  568. }
  569. if (key_data.use_rsc_tsc) {
  570. struct iwl_host_cmd rsc_tsc_cmd = {
  571. .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
  572. .flags = CMD_SYNC,
  573. .data[0] = key_data.rsc_tsc,
  574. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  575. .len[0] = sizeof(*key_data.rsc_tsc),
  576. };
  577. ret = iwl_trans_send_cmd(trans(priv), &rsc_tsc_cmd);
  578. if (ret)
  579. goto error;
  580. }
  581. if (key_data.use_tkip) {
  582. ret = iwl_trans_send_cmd_pdu(trans(priv),
  583. REPLY_WOWLAN_TKIP_PARAMS,
  584. CMD_SYNC, sizeof(tkip_cmd),
  585. &tkip_cmd);
  586. if (ret)
  587. goto error;
  588. }
  589. if (priv->have_rekey_data) {
  590. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  591. memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
  592. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  593. memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
  594. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  595. kek_kck_cmd.replay_ctr = priv->replay_ctr;
  596. ret = iwl_trans_send_cmd_pdu(trans(priv),
  597. REPLY_WOWLAN_KEK_KCK_MATERIAL,
  598. CMD_SYNC, sizeof(kek_kck_cmd),
  599. &kek_kck_cmd);
  600. if (ret)
  601. goto error;
  602. }
  603. }
  604. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_D3_CONFIG, CMD_SYNC,
  605. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  606. if (ret)
  607. goto error;
  608. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_WOWLAN_WAKEUP_FILTER,
  609. CMD_SYNC, sizeof(wakeup_filter_cmd),
  610. &wakeup_filter_cmd);
  611. if (ret)
  612. goto error;
  613. ret = iwlagn_send_patterns(priv, wowlan);
  614. if (ret)
  615. goto error;
  616. device_set_wakeup_enable(bus(priv)->dev, true);
  617. /* Now let the ucode operate on its own */
  618. iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
  619. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  620. goto out;
  621. error:
  622. priv->shrd->wowlan = false;
  623. iwlagn_prepare_restart(priv);
  624. ieee80211_restart_hw(priv->hw);
  625. out:
  626. mutex_unlock(&priv->shrd->mutex);
  627. kfree(key_data.rsc_tsc);
  628. IWL_DEBUG_MAC80211(priv, "leave\n");
  629. return ret;
  630. }
  631. static int iwlagn_mac_resume(struct ieee80211_hw *hw)
  632. {
  633. struct iwl_priv *priv = hw->priv;
  634. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  635. struct ieee80211_vif *vif;
  636. unsigned long flags;
  637. u32 base, status = 0xffffffff;
  638. int ret = -EIO;
  639. IWL_DEBUG_MAC80211(priv, "enter\n");
  640. mutex_lock(&priv->shrd->mutex);
  641. iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
  642. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  643. base = priv->device_pointers.error_event_table;
  644. if (iwlagn_hw_valid_rtc_data_addr(base)) {
  645. spin_lock_irqsave(&bus(priv)->reg_lock, flags);
  646. ret = iwl_grab_nic_access_silent(bus(priv));
  647. if (ret == 0) {
  648. iwl_write32(bus(priv), HBUS_TARG_MEM_RADDR, base);
  649. status = iwl_read32(bus(priv), HBUS_TARG_MEM_RDAT);
  650. iwl_release_nic_access(bus(priv));
  651. }
  652. spin_unlock_irqrestore(&bus(priv)->reg_lock, flags);
  653. #ifdef CONFIG_IWLWIFI_DEBUGFS
  654. if (ret == 0) {
  655. struct iwl_trans *trans = trans(priv);
  656. if (!priv->wowlan_sram)
  657. priv->wowlan_sram =
  658. kzalloc(trans->ucode_wowlan.data.len,
  659. GFP_KERNEL);
  660. if (priv->wowlan_sram)
  661. _iwl_read_targ_mem_words(
  662. bus(priv), 0x800000, priv->wowlan_sram,
  663. trans->ucode_wowlan.data.len / 4);
  664. }
  665. #endif
  666. }
  667. /* we'll clear ctx->vif during iwlagn_prepare_restart() */
  668. vif = ctx->vif;
  669. priv->shrd->wowlan = false;
  670. device_set_wakeup_enable(bus(priv)->dev, false);
  671. iwlagn_prepare_restart(priv);
  672. memset((void *)&ctx->active, 0, sizeof(ctx->active));
  673. iwl_connection_init_rx_config(priv, ctx);
  674. iwlagn_set_rxon_chain(priv, ctx);
  675. mutex_unlock(&priv->shrd->mutex);
  676. IWL_DEBUG_MAC80211(priv, "leave\n");
  677. ieee80211_resume_disconnect(vif);
  678. return 1;
  679. }
  680. #endif
  681. static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  682. {
  683. struct iwl_priv *priv = hw->priv;
  684. IWL_DEBUG_MACDUMP(priv, "enter\n");
  685. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  686. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  687. if (iwlagn_tx_skb(priv, skb))
  688. dev_kfree_skb_any(skb);
  689. IWL_DEBUG_MACDUMP(priv, "leave\n");
  690. }
  691. static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
  692. struct ieee80211_vif *vif,
  693. struct ieee80211_key_conf *keyconf,
  694. struct ieee80211_sta *sta,
  695. u32 iv32, u16 *phase1key)
  696. {
  697. struct iwl_priv *priv = hw->priv;
  698. iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
  699. }
  700. static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  701. struct ieee80211_vif *vif,
  702. struct ieee80211_sta *sta,
  703. struct ieee80211_key_conf *key)
  704. {
  705. struct iwl_priv *priv = hw->priv;
  706. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  707. struct iwl_rxon_context *ctx = vif_priv->ctx;
  708. int ret;
  709. bool is_default_wep_key = false;
  710. IWL_DEBUG_MAC80211(priv, "enter\n");
  711. if (iwlagn_mod_params.sw_crypto) {
  712. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  713. return -EOPNOTSUPP;
  714. }
  715. /*
  716. * We could program these keys into the hardware as well, but we
  717. * don't expect much multicast traffic in IBSS and having keys
  718. * for more stations is probably more useful.
  719. *
  720. * Mark key TX-only and return 0.
  721. */
  722. if (vif->type == NL80211_IFTYPE_ADHOC &&
  723. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  724. key->hw_key_idx = WEP_INVALID_OFFSET;
  725. return 0;
  726. }
  727. /* If they key was TX-only, accept deletion */
  728. if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
  729. return 0;
  730. mutex_lock(&priv->shrd->mutex);
  731. iwl_scan_cancel_timeout(priv, 100);
  732. BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
  733. /*
  734. * If we are getting WEP group key and we didn't receive any key mapping
  735. * so far, we are in legacy wep mode (group key only), otherwise we are
  736. * in 1X mode.
  737. * In legacy wep mode, we use another host command to the uCode.
  738. */
  739. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  740. key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
  741. if (cmd == SET_KEY)
  742. is_default_wep_key = !ctx->key_mapping_keys;
  743. else
  744. is_default_wep_key =
  745. key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
  746. }
  747. switch (cmd) {
  748. case SET_KEY:
  749. if (is_default_wep_key) {
  750. ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
  751. break;
  752. }
  753. ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
  754. if (ret) {
  755. /*
  756. * can't add key for RX, but we don't need it
  757. * in the device for TX so still return 0
  758. */
  759. ret = 0;
  760. key->hw_key_idx = WEP_INVALID_OFFSET;
  761. }
  762. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  763. break;
  764. case DISABLE_KEY:
  765. if (is_default_wep_key)
  766. ret = iwl_remove_default_wep_key(priv, ctx, key);
  767. else
  768. ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
  769. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  770. break;
  771. default:
  772. ret = -EINVAL;
  773. }
  774. mutex_unlock(&priv->shrd->mutex);
  775. IWL_DEBUG_MAC80211(priv, "leave\n");
  776. return ret;
  777. }
  778. static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
  779. struct ieee80211_vif *vif,
  780. enum ieee80211_ampdu_mlme_action action,
  781. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  782. u8 buf_size)
  783. {
  784. struct iwl_priv *priv = hw->priv;
  785. int ret = -EINVAL;
  786. struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
  787. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  788. IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
  789. sta->addr, tid);
  790. if (!(priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE))
  791. return -EACCES;
  792. IWL_DEBUG_MAC80211(priv, "enter\n");
  793. mutex_lock(&priv->shrd->mutex);
  794. switch (action) {
  795. case IEEE80211_AMPDU_RX_START:
  796. IWL_DEBUG_HT(priv, "start Rx\n");
  797. ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
  798. break;
  799. case IEEE80211_AMPDU_RX_STOP:
  800. IWL_DEBUG_HT(priv, "stop Rx\n");
  801. ret = iwl_sta_rx_agg_stop(priv, sta, tid);
  802. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  803. ret = 0;
  804. break;
  805. case IEEE80211_AMPDU_TX_START:
  806. IWL_DEBUG_HT(priv, "start Tx\n");
  807. ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
  808. break;
  809. case IEEE80211_AMPDU_TX_STOP:
  810. IWL_DEBUG_HT(priv, "stop Tx\n");
  811. ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
  812. if ((ret == 0) && (priv->agg_tids_count > 0)) {
  813. priv->agg_tids_count--;
  814. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  815. priv->agg_tids_count);
  816. }
  817. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  818. ret = 0;
  819. if (!priv->agg_tids_count && priv->cfg->ht_params &&
  820. priv->cfg->ht_params->use_rts_for_aggregation) {
  821. /*
  822. * switch off RTS/CTS if it was previously enabled
  823. */
  824. sta_priv->lq_sta.lq.general_params.flags &=
  825. ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  826. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  827. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  828. }
  829. break;
  830. case IEEE80211_AMPDU_TX_OPERATIONAL:
  831. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  832. iwl_trans_tx_agg_setup(trans(priv), ctx->ctxid, iwl_sta_id(sta),
  833. tid, buf_size);
  834. /*
  835. * If the limit is 0, then it wasn't initialised yet,
  836. * use the default. We can do that since we take the
  837. * minimum below, and we don't want to go above our
  838. * default due to hardware restrictions.
  839. */
  840. if (sta_priv->max_agg_bufsize == 0)
  841. sta_priv->max_agg_bufsize =
  842. LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  843. /*
  844. * Even though in theory the peer could have different
  845. * aggregation reorder buffer sizes for different sessions,
  846. * our ucode doesn't allow for that and has a global limit
  847. * for each station. Therefore, use the minimum of all the
  848. * aggregation sessions and our default value.
  849. */
  850. sta_priv->max_agg_bufsize =
  851. min(sta_priv->max_agg_bufsize, buf_size);
  852. if (priv->cfg->ht_params &&
  853. priv->cfg->ht_params->use_rts_for_aggregation) {
  854. /*
  855. * switch to RTS/CTS if it is the prefer protection
  856. * method for HT traffic
  857. */
  858. sta_priv->lq_sta.lq.general_params.flags |=
  859. LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  860. }
  861. priv->agg_tids_count++;
  862. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  863. priv->agg_tids_count);
  864. sta_priv->lq_sta.lq.agg_params.agg_frame_cnt_limit =
  865. sta_priv->max_agg_bufsize;
  866. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  867. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  868. IWL_INFO(priv, "Tx aggregation enabled on ra = %pM tid = %d\n",
  869. sta->addr, tid);
  870. ret = 0;
  871. break;
  872. }
  873. mutex_unlock(&priv->shrd->mutex);
  874. IWL_DEBUG_MAC80211(priv, "leave\n");
  875. return ret;
  876. }
  877. static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
  878. struct ieee80211_vif *vif,
  879. struct ieee80211_sta *sta)
  880. {
  881. struct iwl_priv *priv = hw->priv;
  882. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  883. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  884. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  885. int ret = 0;
  886. u8 sta_id;
  887. IWL_DEBUG_MAC80211(priv, "received request to add station %pM\n",
  888. sta->addr);
  889. mutex_lock(&priv->shrd->mutex);
  890. IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
  891. sta->addr);
  892. sta_priv->sta_id = IWL_INVALID_STATION;
  893. atomic_set(&sta_priv->pending_frames, 0);
  894. if (vif->type == NL80211_IFTYPE_AP)
  895. sta_priv->client = true;
  896. ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
  897. is_ap, sta, &sta_id);
  898. if (ret) {
  899. IWL_ERR(priv, "Unable to add station %pM (%d)\n",
  900. sta->addr, ret);
  901. /* Should we return success if return code is EEXIST ? */
  902. goto out;
  903. }
  904. sta_priv->sta_id = sta_id;
  905. /* Initialize rate scaling */
  906. IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
  907. sta->addr);
  908. iwl_rs_rate_init(priv, sta, sta_id);
  909. out:
  910. mutex_unlock(&priv->shrd->mutex);
  911. IWL_DEBUG_MAC80211(priv, "leave\n");
  912. return ret;
  913. }
  914. static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
  915. struct ieee80211_channel_switch *ch_switch)
  916. {
  917. struct iwl_priv *priv = hw->priv;
  918. const struct iwl_channel_info *ch_info;
  919. struct ieee80211_conf *conf = &hw->conf;
  920. struct ieee80211_channel *channel = ch_switch->channel;
  921. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  922. /*
  923. * MULTI-FIXME
  924. * When we add support for multiple interfaces, we need to
  925. * revisit this. The channel switch command in the device
  926. * only affects the BSS context, but what does that really
  927. * mean? And what if we get a CSA on the second interface?
  928. * This needs a lot of work.
  929. */
  930. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  931. u16 ch;
  932. IWL_DEBUG_MAC80211(priv, "enter\n");
  933. mutex_lock(&priv->shrd->mutex);
  934. if (iwl_is_rfkill(priv->shrd))
  935. goto out;
  936. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
  937. test_bit(STATUS_SCANNING, &priv->shrd->status) ||
  938. test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
  939. goto out;
  940. if (!iwl_is_associated_ctx(ctx))
  941. goto out;
  942. if (!priv->cfg->lib->set_channel_switch)
  943. goto out;
  944. ch = channel->hw_value;
  945. if (le16_to_cpu(ctx->active.channel) == ch)
  946. goto out;
  947. ch_info = iwl_get_channel_info(priv, channel->band, ch);
  948. if (!is_channel_valid(ch_info)) {
  949. IWL_DEBUG_MAC80211(priv, "invalid channel\n");
  950. goto out;
  951. }
  952. spin_lock_irq(&priv->shrd->lock);
  953. priv->current_ht_config.smps = conf->smps_mode;
  954. /* Configure HT40 channels */
  955. ctx->ht.enabled = conf_is_ht(conf);
  956. if (ctx->ht.enabled) {
  957. if (conf_is_ht40_minus(conf)) {
  958. ctx->ht.extension_chan_offset =
  959. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  960. ctx->ht.is_40mhz = true;
  961. } else if (conf_is_ht40_plus(conf)) {
  962. ctx->ht.extension_chan_offset =
  963. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  964. ctx->ht.is_40mhz = true;
  965. } else {
  966. ctx->ht.extension_chan_offset =
  967. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  968. ctx->ht.is_40mhz = false;
  969. }
  970. } else
  971. ctx->ht.is_40mhz = false;
  972. if ((le16_to_cpu(ctx->staging.channel) != ch))
  973. ctx->staging.flags = 0;
  974. iwl_set_rxon_channel(priv, channel, ctx);
  975. iwl_set_rxon_ht(priv, ht_conf);
  976. iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
  977. spin_unlock_irq(&priv->shrd->lock);
  978. iwl_set_rate(priv);
  979. /*
  980. * at this point, staging_rxon has the
  981. * configuration for channel switch
  982. */
  983. set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  984. priv->switch_channel = cpu_to_le16(ch);
  985. if (priv->cfg->lib->set_channel_switch(priv, ch_switch)) {
  986. clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  987. priv->switch_channel = 0;
  988. ieee80211_chswitch_done(ctx->vif, false);
  989. }
  990. out:
  991. mutex_unlock(&priv->shrd->mutex);
  992. IWL_DEBUG_MAC80211(priv, "leave\n");
  993. }
  994. static void iwlagn_configure_filter(struct ieee80211_hw *hw,
  995. unsigned int changed_flags,
  996. unsigned int *total_flags,
  997. u64 multicast)
  998. {
  999. struct iwl_priv *priv = hw->priv;
  1000. __le32 filter_or = 0, filter_nand = 0;
  1001. struct iwl_rxon_context *ctx;
  1002. #define CHK(test, flag) do { \
  1003. if (*total_flags & (test)) \
  1004. filter_or |= (flag); \
  1005. else \
  1006. filter_nand |= (flag); \
  1007. } while (0)
  1008. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  1009. changed_flags, *total_flags);
  1010. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  1011. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  1012. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  1013. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  1014. #undef CHK
  1015. mutex_lock(&priv->shrd->mutex);
  1016. for_each_context(priv, ctx) {
  1017. ctx->staging.filter_flags &= ~filter_nand;
  1018. ctx->staging.filter_flags |= filter_or;
  1019. /*
  1020. * Not committing directly because hardware can perform a scan,
  1021. * but we'll eventually commit the filter flags change anyway.
  1022. */
  1023. }
  1024. mutex_unlock(&priv->shrd->mutex);
  1025. /*
  1026. * Receiving all multicast frames is always enabled by the
  1027. * default flags setup in iwl_connection_init_rx_config()
  1028. * since we currently do not support programming multicast
  1029. * filters into the device.
  1030. */
  1031. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  1032. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  1033. }
  1034. static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
  1035. {
  1036. struct iwl_priv *priv = hw->priv;
  1037. mutex_lock(&priv->shrd->mutex);
  1038. IWL_DEBUG_MAC80211(priv, "enter\n");
  1039. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  1040. IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
  1041. goto done;
  1042. }
  1043. if (iwl_is_rfkill(priv->shrd)) {
  1044. IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
  1045. goto done;
  1046. }
  1047. /*
  1048. * mac80211 will not push any more frames for transmit
  1049. * until the flush is completed
  1050. */
  1051. if (drop) {
  1052. IWL_DEBUG_MAC80211(priv, "send flush command\n");
  1053. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  1054. IWL_ERR(priv, "flush request fail\n");
  1055. goto done;
  1056. }
  1057. }
  1058. IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
  1059. iwl_trans_wait_tx_queue_empty(trans(priv));
  1060. done:
  1061. mutex_unlock(&priv->shrd->mutex);
  1062. IWL_DEBUG_MAC80211(priv, "leave\n");
  1063. }
  1064. static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
  1065. struct ieee80211_channel *channel,
  1066. enum nl80211_channel_type channel_type,
  1067. int duration)
  1068. {
  1069. struct iwl_priv *priv = hw->priv;
  1070. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  1071. int err = 0;
  1072. if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  1073. return -EOPNOTSUPP;
  1074. if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
  1075. return -EOPNOTSUPP;
  1076. IWL_DEBUG_MAC80211(priv, "enter\n");
  1077. mutex_lock(&priv->shrd->mutex);
  1078. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  1079. err = -EBUSY;
  1080. goto out;
  1081. }
  1082. priv->hw_roc_channel = channel;
  1083. priv->hw_roc_chantype = channel_type;
  1084. priv->hw_roc_duration = duration;
  1085. priv->hw_roc_start_notified = false;
  1086. cancel_delayed_work(&priv->hw_roc_disable_work);
  1087. if (!ctx->is_active) {
  1088. ctx->is_active = true;
  1089. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  1090. memcpy(ctx->staging.node_addr,
  1091. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  1092. ETH_ALEN);
  1093. memcpy(ctx->staging.bssid_addr,
  1094. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  1095. ETH_ALEN);
  1096. err = iwlagn_commit_rxon(priv, ctx);
  1097. if (err)
  1098. goto out;
  1099. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
  1100. RXON_FILTER_PROMISC_MSK |
  1101. RXON_FILTER_CTL2HOST_MSK;
  1102. err = iwlagn_commit_rxon(priv, ctx);
  1103. if (err) {
  1104. iwlagn_disable_roc(priv);
  1105. goto out;
  1106. }
  1107. priv->hw_roc_setup = true;
  1108. }
  1109. err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
  1110. if (err)
  1111. iwlagn_disable_roc(priv);
  1112. out:
  1113. mutex_unlock(&priv->shrd->mutex);
  1114. IWL_DEBUG_MAC80211(priv, "leave\n");
  1115. return err;
  1116. }
  1117. static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
  1118. {
  1119. struct iwl_priv *priv = hw->priv;
  1120. if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  1121. return -EOPNOTSUPP;
  1122. IWL_DEBUG_MAC80211(priv, "enter\n");
  1123. mutex_lock(&priv->shrd->mutex);
  1124. iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
  1125. iwlagn_disable_roc(priv);
  1126. mutex_unlock(&priv->shrd->mutex);
  1127. IWL_DEBUG_MAC80211(priv, "leave\n");
  1128. return 0;
  1129. }
  1130. static int iwlagn_mac_tx_sync(struct ieee80211_hw *hw,
  1131. struct ieee80211_vif *vif,
  1132. const u8 *bssid,
  1133. enum ieee80211_tx_sync_type type)
  1134. {
  1135. struct iwl_priv *priv = hw->priv;
  1136. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1137. struct iwl_rxon_context *ctx = vif_priv->ctx;
  1138. int ret;
  1139. u8 sta_id;
  1140. IWL_DEBUG_MAC80211(priv, "enter\n");
  1141. mutex_lock(&priv->shrd->mutex);
  1142. if (iwl_is_associated_ctx(ctx)) {
  1143. ret = 0;
  1144. goto out;
  1145. }
  1146. if (ctx->preauth_bssid || test_bit(STATUS_SCAN_HW,
  1147. &priv->shrd->status)) {
  1148. ret = -EBUSY;
  1149. goto out;
  1150. }
  1151. ret = iwl_add_station_common(priv, ctx, bssid, true, NULL, &sta_id);
  1152. if (ret)
  1153. goto out;
  1154. if (WARN_ON(sta_id != ctx->ap_sta_id)) {
  1155. ret = -EIO;
  1156. goto out_remove_sta;
  1157. }
  1158. memcpy(ctx->bssid, bssid, ETH_ALEN);
  1159. ctx->preauth_bssid = true;
  1160. ret = iwlagn_commit_rxon(priv, ctx);
  1161. if (ret == 0)
  1162. goto out;
  1163. out_remove_sta:
  1164. iwl_remove_station(priv, sta_id, bssid);
  1165. out:
  1166. mutex_unlock(&priv->shrd->mutex);
  1167. IWL_DEBUG_MAC80211(priv, "leave\n");
  1168. return ret;
  1169. }
  1170. static void iwlagn_mac_finish_tx_sync(struct ieee80211_hw *hw,
  1171. struct ieee80211_vif *vif,
  1172. const u8 *bssid,
  1173. enum ieee80211_tx_sync_type type)
  1174. {
  1175. struct iwl_priv *priv = hw->priv;
  1176. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1177. struct iwl_rxon_context *ctx = vif_priv->ctx;
  1178. IWL_DEBUG_MAC80211(priv, "enter\n");
  1179. mutex_lock(&priv->shrd->mutex);
  1180. if (iwl_is_associated_ctx(ctx))
  1181. goto out;
  1182. iwl_remove_station(priv, ctx->ap_sta_id, bssid);
  1183. ctx->preauth_bssid = false;
  1184. /* no need to commit */
  1185. out:
  1186. mutex_unlock(&priv->shrd->mutex);
  1187. IWL_DEBUG_MAC80211(priv, "leave\n");
  1188. }
  1189. static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
  1190. enum ieee80211_rssi_event rssi_event)
  1191. {
  1192. struct iwl_priv *priv = hw->priv;
  1193. IWL_DEBUG_MAC80211(priv, "enter\n");
  1194. mutex_lock(&priv->shrd->mutex);
  1195. if (priv->cfg->bt_params &&
  1196. priv->cfg->bt_params->advanced_bt_coexist) {
  1197. if (rssi_event == RSSI_EVENT_LOW)
  1198. priv->bt_enable_pspoll = true;
  1199. else if (rssi_event == RSSI_EVENT_HIGH)
  1200. priv->bt_enable_pspoll = false;
  1201. iwlagn_send_advance_bt_config(priv);
  1202. } else {
  1203. IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
  1204. "ignoring RSSI callback\n");
  1205. }
  1206. mutex_unlock(&priv->shrd->mutex);
  1207. IWL_DEBUG_MAC80211(priv, "leave\n");
  1208. }
  1209. static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
  1210. struct ieee80211_sta *sta, bool set)
  1211. {
  1212. struct iwl_priv *priv = hw->priv;
  1213. queue_work(priv->shrd->workqueue, &priv->beacon_update);
  1214. return 0;
  1215. }
  1216. struct ieee80211_ops iwlagn_hw_ops = {
  1217. .tx = iwlagn_mac_tx,
  1218. .start = iwlagn_mac_start,
  1219. .stop = iwlagn_mac_stop,
  1220. #ifdef CONFIG_PM_SLEEP
  1221. .suspend = iwlagn_mac_suspend,
  1222. .resume = iwlagn_mac_resume,
  1223. #endif
  1224. .add_interface = iwlagn_mac_add_interface,
  1225. .remove_interface = iwlagn_mac_remove_interface,
  1226. .change_interface = iwlagn_mac_change_interface,
  1227. .config = iwlagn_mac_config,
  1228. .configure_filter = iwlagn_configure_filter,
  1229. .set_key = iwlagn_mac_set_key,
  1230. .update_tkip_key = iwlagn_mac_update_tkip_key,
  1231. .set_rekey_data = iwlagn_mac_set_rekey_data,
  1232. .conf_tx = iwlagn_mac_conf_tx,
  1233. .bss_info_changed = iwlagn_bss_info_changed,
  1234. .ampdu_action = iwlagn_mac_ampdu_action,
  1235. .hw_scan = iwlagn_mac_hw_scan,
  1236. .sta_notify = iwlagn_mac_sta_notify,
  1237. .sta_add = iwlagn_mac_sta_add,
  1238. .sta_remove = iwlagn_mac_sta_remove,
  1239. .channel_switch = iwlagn_mac_channel_switch,
  1240. .flush = iwlagn_mac_flush,
  1241. .tx_last_beacon = iwlagn_mac_tx_last_beacon,
  1242. .remain_on_channel = iwlagn_mac_remain_on_channel,
  1243. .cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
  1244. .rssi_callback = iwlagn_mac_rssi_callback,
  1245. CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
  1246. CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
  1247. .tx_sync = iwlagn_mac_tx_sync,
  1248. .finish_tx_sync = iwlagn_mac_finish_tx_sync,
  1249. .set_tim = iwlagn_mac_set_tim,
  1250. };
  1251. /* This function both allocates and initializes hw and priv. */
  1252. struct ieee80211_hw *iwl_alloc_all(void)
  1253. {
  1254. struct iwl_priv *priv;
  1255. /* mac80211 allocates memory for this device instance, including
  1256. * space for this driver's private structure */
  1257. struct ieee80211_hw *hw;
  1258. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwlagn_hw_ops);
  1259. if (!hw)
  1260. goto out;
  1261. priv = hw->priv;
  1262. priv->hw = hw;
  1263. out:
  1264. return hw;
  1265. }