iwl-mac80211.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  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. static const struct iwl_qos_info default_qos_data = {
  1089. .def_qos_parm = {
  1090. .ac[0] = {
  1091. .cw_min = cpu_to_le16(3),
  1092. .cw_max = cpu_to_le16(7),
  1093. .aifsn = 2,
  1094. .edca_txop = cpu_to_le16(1504),
  1095. },
  1096. .ac[1] = {
  1097. .cw_min = cpu_to_le16(7),
  1098. .cw_max = cpu_to_le16(15),
  1099. .aifsn = 2,
  1100. .edca_txop = cpu_to_le16(3008),
  1101. },
  1102. .ac[2] = {
  1103. .cw_min = cpu_to_le16(15),
  1104. .cw_max = cpu_to_le16(1023),
  1105. .aifsn = 3,
  1106. },
  1107. .ac[3] = {
  1108. .cw_min = cpu_to_le16(15),
  1109. .cw_max = cpu_to_le16(1023),
  1110. .aifsn = 7,
  1111. },
  1112. },
  1113. };
  1114. ctx->is_active = true;
  1115. ctx->qos_data = default_qos_data;
  1116. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  1117. memcpy(ctx->staging.node_addr,
  1118. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  1119. ETH_ALEN);
  1120. memcpy(ctx->staging.bssid_addr,
  1121. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  1122. ETH_ALEN);
  1123. err = iwlagn_commit_rxon(priv, ctx);
  1124. if (err)
  1125. goto out;
  1126. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
  1127. RXON_FILTER_PROMISC_MSK |
  1128. RXON_FILTER_CTL2HOST_MSK;
  1129. err = iwlagn_commit_rxon(priv, ctx);
  1130. if (err) {
  1131. iwlagn_disable_roc(priv);
  1132. goto out;
  1133. }
  1134. priv->hw_roc_setup = true;
  1135. }
  1136. err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
  1137. if (err)
  1138. iwlagn_disable_roc(priv);
  1139. out:
  1140. mutex_unlock(&priv->shrd->mutex);
  1141. IWL_DEBUG_MAC80211(priv, "leave\n");
  1142. return err;
  1143. }
  1144. static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
  1145. {
  1146. struct iwl_priv *priv = hw->priv;
  1147. if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  1148. return -EOPNOTSUPP;
  1149. IWL_DEBUG_MAC80211(priv, "enter\n");
  1150. mutex_lock(&priv->shrd->mutex);
  1151. iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
  1152. iwlagn_disable_roc(priv);
  1153. mutex_unlock(&priv->shrd->mutex);
  1154. IWL_DEBUG_MAC80211(priv, "leave\n");
  1155. return 0;
  1156. }
  1157. static int iwlagn_mac_tx_sync(struct ieee80211_hw *hw,
  1158. struct ieee80211_vif *vif,
  1159. const u8 *bssid,
  1160. enum ieee80211_tx_sync_type type)
  1161. {
  1162. struct iwl_priv *priv = hw->priv;
  1163. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1164. struct iwl_rxon_context *ctx = vif_priv->ctx;
  1165. int ret;
  1166. u8 sta_id;
  1167. IWL_DEBUG_MAC80211(priv, "enter\n");
  1168. mutex_lock(&priv->shrd->mutex);
  1169. if (iwl_is_associated_ctx(ctx)) {
  1170. ret = 0;
  1171. goto out;
  1172. }
  1173. if (ctx->preauth_bssid || test_bit(STATUS_SCAN_HW,
  1174. &priv->shrd->status)) {
  1175. ret = -EBUSY;
  1176. goto out;
  1177. }
  1178. ret = iwl_add_station_common(priv, ctx, bssid, true, NULL, &sta_id);
  1179. if (ret)
  1180. goto out;
  1181. if (WARN_ON(sta_id != ctx->ap_sta_id)) {
  1182. ret = -EIO;
  1183. goto out_remove_sta;
  1184. }
  1185. memcpy(ctx->bssid, bssid, ETH_ALEN);
  1186. ctx->preauth_bssid = true;
  1187. ret = iwlagn_commit_rxon(priv, ctx);
  1188. if (ret == 0)
  1189. goto out;
  1190. out_remove_sta:
  1191. iwl_remove_station(priv, sta_id, bssid);
  1192. out:
  1193. mutex_unlock(&priv->shrd->mutex);
  1194. IWL_DEBUG_MAC80211(priv, "leave\n");
  1195. return ret;
  1196. }
  1197. static void iwlagn_mac_finish_tx_sync(struct ieee80211_hw *hw,
  1198. struct ieee80211_vif *vif,
  1199. const u8 *bssid,
  1200. enum ieee80211_tx_sync_type type)
  1201. {
  1202. struct iwl_priv *priv = hw->priv;
  1203. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1204. struct iwl_rxon_context *ctx = vif_priv->ctx;
  1205. IWL_DEBUG_MAC80211(priv, "enter\n");
  1206. mutex_lock(&priv->shrd->mutex);
  1207. if (iwl_is_associated_ctx(ctx))
  1208. goto out;
  1209. iwl_remove_station(priv, ctx->ap_sta_id, bssid);
  1210. ctx->preauth_bssid = false;
  1211. /* no need to commit */
  1212. out:
  1213. mutex_unlock(&priv->shrd->mutex);
  1214. IWL_DEBUG_MAC80211(priv, "leave\n");
  1215. }
  1216. static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
  1217. enum ieee80211_rssi_event rssi_event)
  1218. {
  1219. struct iwl_priv *priv = hw->priv;
  1220. IWL_DEBUG_MAC80211(priv, "enter\n");
  1221. mutex_lock(&priv->shrd->mutex);
  1222. if (priv->cfg->bt_params &&
  1223. priv->cfg->bt_params->advanced_bt_coexist) {
  1224. if (rssi_event == RSSI_EVENT_LOW)
  1225. priv->bt_enable_pspoll = true;
  1226. else if (rssi_event == RSSI_EVENT_HIGH)
  1227. priv->bt_enable_pspoll = false;
  1228. iwlagn_send_advance_bt_config(priv);
  1229. } else {
  1230. IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
  1231. "ignoring RSSI callback\n");
  1232. }
  1233. mutex_unlock(&priv->shrd->mutex);
  1234. IWL_DEBUG_MAC80211(priv, "leave\n");
  1235. }
  1236. static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
  1237. struct ieee80211_sta *sta, bool set)
  1238. {
  1239. struct iwl_priv *priv = hw->priv;
  1240. queue_work(priv->shrd->workqueue, &priv->beacon_update);
  1241. return 0;
  1242. }
  1243. static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
  1244. struct ieee80211_vif *vif, u16 queue,
  1245. const struct ieee80211_tx_queue_params *params)
  1246. {
  1247. struct iwl_priv *priv = hw->priv;
  1248. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1249. struct iwl_rxon_context *ctx = vif_priv->ctx;
  1250. unsigned long flags;
  1251. int q;
  1252. if (WARN_ON(!ctx))
  1253. return -EINVAL;
  1254. IWL_DEBUG_MAC80211(priv, "enter\n");
  1255. if (!iwl_is_ready_rf(priv->shrd)) {
  1256. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  1257. return -EIO;
  1258. }
  1259. if (queue >= AC_NUM) {
  1260. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  1261. return 0;
  1262. }
  1263. q = AC_NUM - 1 - queue;
  1264. spin_lock_irqsave(&priv->shrd->lock, flags);
  1265. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  1266. cpu_to_le16(params->cw_min);
  1267. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  1268. cpu_to_le16(params->cw_max);
  1269. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  1270. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  1271. cpu_to_le16((params->txop * 32));
  1272. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  1273. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  1274. IWL_DEBUG_MAC80211(priv, "leave\n");
  1275. return 0;
  1276. }
  1277. static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
  1278. {
  1279. struct iwl_priv *priv = hw->priv;
  1280. return priv->ibss_manager == IWL_IBSS_MANAGER;
  1281. }
  1282. static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  1283. {
  1284. iwl_connection_init_rx_config(priv, ctx);
  1285. iwlagn_set_rxon_chain(priv, ctx);
  1286. return iwlagn_commit_rxon(priv, ctx);
  1287. }
  1288. static int iwl_setup_interface(struct iwl_priv *priv,
  1289. struct iwl_rxon_context *ctx)
  1290. {
  1291. struct ieee80211_vif *vif = ctx->vif;
  1292. int err;
  1293. lockdep_assert_held(&priv->shrd->mutex);
  1294. /*
  1295. * This variable will be correct only when there's just
  1296. * a single context, but all code using it is for hardware
  1297. * that supports only one context.
  1298. */
  1299. priv->iw_mode = vif->type;
  1300. ctx->is_active = true;
  1301. err = iwl_set_mode(priv, ctx);
  1302. if (err) {
  1303. if (!ctx->always_active)
  1304. ctx->is_active = false;
  1305. return err;
  1306. }
  1307. if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
  1308. vif->type == NL80211_IFTYPE_ADHOC) {
  1309. /*
  1310. * pretend to have high BT traffic as long as we
  1311. * are operating in IBSS mode, as this will cause
  1312. * the rate scaling etc. to behave as intended.
  1313. */
  1314. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1315. }
  1316. return 0;
  1317. }
  1318. static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
  1319. struct ieee80211_vif *vif)
  1320. {
  1321. struct iwl_priv *priv = hw->priv;
  1322. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1323. struct iwl_rxon_context *tmp, *ctx = NULL;
  1324. int err;
  1325. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
  1326. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1327. viftype, vif->addr);
  1328. cancel_delayed_work_sync(&priv->hw_roc_disable_work);
  1329. mutex_lock(&priv->shrd->mutex);
  1330. iwlagn_disable_roc(priv);
  1331. if (!iwl_is_ready_rf(priv->shrd)) {
  1332. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1333. err = -EINVAL;
  1334. goto out;
  1335. }
  1336. for_each_context(priv, tmp) {
  1337. u32 possible_modes =
  1338. tmp->interface_modes | tmp->exclusive_interface_modes;
  1339. if (tmp->vif) {
  1340. /* check if this busy context is exclusive */
  1341. if (tmp->exclusive_interface_modes &
  1342. BIT(tmp->vif->type)) {
  1343. err = -EINVAL;
  1344. goto out;
  1345. }
  1346. continue;
  1347. }
  1348. if (!(possible_modes & BIT(viftype)))
  1349. continue;
  1350. /* have maybe usable context w/o interface */
  1351. ctx = tmp;
  1352. break;
  1353. }
  1354. if (!ctx) {
  1355. err = -EOPNOTSUPP;
  1356. goto out;
  1357. }
  1358. vif_priv->ctx = ctx;
  1359. ctx->vif = vif;
  1360. err = iwl_setup_interface(priv, ctx);
  1361. if (!err)
  1362. goto out;
  1363. ctx->vif = NULL;
  1364. priv->iw_mode = NL80211_IFTYPE_STATION;
  1365. out:
  1366. mutex_unlock(&priv->shrd->mutex);
  1367. IWL_DEBUG_MAC80211(priv, "leave\n");
  1368. return err;
  1369. }
  1370. static void iwl_teardown_interface(struct iwl_priv *priv,
  1371. struct ieee80211_vif *vif,
  1372. bool mode_change)
  1373. {
  1374. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1375. lockdep_assert_held(&priv->shrd->mutex);
  1376. if (priv->scan_vif == vif) {
  1377. iwl_scan_cancel_timeout(priv, 200);
  1378. iwl_force_scan_end(priv);
  1379. }
  1380. if (!mode_change) {
  1381. iwl_set_mode(priv, ctx);
  1382. if (!ctx->always_active)
  1383. ctx->is_active = false;
  1384. }
  1385. /*
  1386. * When removing the IBSS interface, overwrite the
  1387. * BT traffic load with the stored one from the last
  1388. * notification, if any. If this is a device that
  1389. * doesn't implement this, this has no effect since
  1390. * both values are the same and zero.
  1391. */
  1392. if (vif->type == NL80211_IFTYPE_ADHOC)
  1393. priv->bt_traffic_load = priv->last_bt_traffic_load;
  1394. }
  1395. static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
  1396. struct ieee80211_vif *vif)
  1397. {
  1398. struct iwl_priv *priv = hw->priv;
  1399. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1400. IWL_DEBUG_MAC80211(priv, "enter\n");
  1401. mutex_lock(&priv->shrd->mutex);
  1402. if (WARN_ON(ctx->vif != vif)) {
  1403. struct iwl_rxon_context *tmp;
  1404. IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
  1405. for_each_context(priv, tmp)
  1406. IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
  1407. tmp->ctxid, tmp, tmp->vif);
  1408. }
  1409. ctx->vif = NULL;
  1410. iwl_teardown_interface(priv, vif, false);
  1411. mutex_unlock(&priv->shrd->mutex);
  1412. IWL_DEBUG_MAC80211(priv, "leave\n");
  1413. }
  1414. static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
  1415. struct ieee80211_vif *vif,
  1416. enum nl80211_iftype newtype, bool newp2p)
  1417. {
  1418. struct iwl_priv *priv = hw->priv;
  1419. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1420. struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1421. struct iwl_rxon_context *tmp;
  1422. enum nl80211_iftype newviftype = newtype;
  1423. u32 interface_modes;
  1424. int err;
  1425. IWL_DEBUG_MAC80211(priv, "enter\n");
  1426. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1427. mutex_lock(&priv->shrd->mutex);
  1428. if (!ctx->vif || !iwl_is_ready_rf(priv->shrd)) {
  1429. /*
  1430. * Huh? But wait ... this can maybe happen when
  1431. * we're in the middle of a firmware restart!
  1432. */
  1433. err = -EBUSY;
  1434. goto out;
  1435. }
  1436. interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
  1437. if (!(interface_modes & BIT(newtype))) {
  1438. err = -EBUSY;
  1439. goto out;
  1440. }
  1441. /*
  1442. * Refuse a change that should be done by moving from the PAN
  1443. * context to the BSS context instead, if the BSS context is
  1444. * available and can support the new interface type.
  1445. */
  1446. if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
  1447. (bss_ctx->interface_modes & BIT(newtype) ||
  1448. bss_ctx->exclusive_interface_modes & BIT(newtype))) {
  1449. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  1450. err = -EBUSY;
  1451. goto out;
  1452. }
  1453. if (ctx->exclusive_interface_modes & BIT(newtype)) {
  1454. for_each_context(priv, tmp) {
  1455. if (ctx == tmp)
  1456. continue;
  1457. if (!tmp->vif)
  1458. continue;
  1459. /*
  1460. * The current mode switch would be exclusive, but
  1461. * another context is active ... refuse the switch.
  1462. */
  1463. err = -EBUSY;
  1464. goto out;
  1465. }
  1466. }
  1467. /* success */
  1468. iwl_teardown_interface(priv, vif, true);
  1469. vif->type = newviftype;
  1470. vif->p2p = newp2p;
  1471. err = iwl_setup_interface(priv, ctx);
  1472. WARN_ON(err);
  1473. /*
  1474. * We've switched internally, but submitting to the
  1475. * device may have failed for some reason. Mask this
  1476. * error, because otherwise mac80211 will not switch
  1477. * (and set the interface type back) and we'll be
  1478. * out of sync with it.
  1479. */
  1480. err = 0;
  1481. out:
  1482. mutex_unlock(&priv->shrd->mutex);
  1483. IWL_DEBUG_MAC80211(priv, "leave\n");
  1484. return err;
  1485. }
  1486. static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
  1487. struct ieee80211_vif *vif,
  1488. struct cfg80211_scan_request *req)
  1489. {
  1490. struct iwl_priv *priv = hw->priv;
  1491. int ret;
  1492. IWL_DEBUG_MAC80211(priv, "enter\n");
  1493. if (req->n_channels == 0)
  1494. return -EINVAL;
  1495. mutex_lock(&priv->shrd->mutex);
  1496. /*
  1497. * If an internal scan is in progress, just set
  1498. * up the scan_request as per above.
  1499. */
  1500. if (priv->scan_type != IWL_SCAN_NORMAL) {
  1501. IWL_DEBUG_SCAN(priv,
  1502. "SCAN request during internal scan - defer\n");
  1503. priv->scan_request = req;
  1504. priv->scan_vif = vif;
  1505. ret = 0;
  1506. } else {
  1507. priv->scan_request = req;
  1508. priv->scan_vif = vif;
  1509. /*
  1510. * mac80211 will only ask for one band at a time
  1511. * so using channels[0] here is ok
  1512. */
  1513. ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
  1514. req->channels[0]->band);
  1515. if (ret) {
  1516. priv->scan_request = NULL;
  1517. priv->scan_vif = NULL;
  1518. }
  1519. }
  1520. IWL_DEBUG_MAC80211(priv, "leave\n");
  1521. mutex_unlock(&priv->shrd->mutex);
  1522. return ret;
  1523. }
  1524. struct ieee80211_ops iwlagn_hw_ops = {
  1525. .tx = iwlagn_mac_tx,
  1526. .start = iwlagn_mac_start,
  1527. .stop = iwlagn_mac_stop,
  1528. #ifdef CONFIG_PM_SLEEP
  1529. .suspend = iwlagn_mac_suspend,
  1530. .resume = iwlagn_mac_resume,
  1531. #endif
  1532. .add_interface = iwlagn_mac_add_interface,
  1533. .remove_interface = iwlagn_mac_remove_interface,
  1534. .change_interface = iwlagn_mac_change_interface,
  1535. .config = iwlagn_mac_config,
  1536. .configure_filter = iwlagn_configure_filter,
  1537. .set_key = iwlagn_mac_set_key,
  1538. .update_tkip_key = iwlagn_mac_update_tkip_key,
  1539. .set_rekey_data = iwlagn_mac_set_rekey_data,
  1540. .conf_tx = iwlagn_mac_conf_tx,
  1541. .bss_info_changed = iwlagn_bss_info_changed,
  1542. .ampdu_action = iwlagn_mac_ampdu_action,
  1543. .hw_scan = iwlagn_mac_hw_scan,
  1544. .sta_notify = iwlagn_mac_sta_notify,
  1545. .sta_add = iwlagn_mac_sta_add,
  1546. .sta_remove = iwlagn_mac_sta_remove,
  1547. .channel_switch = iwlagn_mac_channel_switch,
  1548. .flush = iwlagn_mac_flush,
  1549. .tx_last_beacon = iwlagn_mac_tx_last_beacon,
  1550. .remain_on_channel = iwlagn_mac_remain_on_channel,
  1551. .cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
  1552. .rssi_callback = iwlagn_mac_rssi_callback,
  1553. CFG80211_TESTMODE_CMD(iwlagn_mac_testmode_cmd)
  1554. CFG80211_TESTMODE_DUMP(iwlagn_mac_testmode_dump)
  1555. .tx_sync = iwlagn_mac_tx_sync,
  1556. .finish_tx_sync = iwlagn_mac_finish_tx_sync,
  1557. .set_tim = iwlagn_mac_set_tim,
  1558. };
  1559. /* This function both allocates and initializes hw and priv. */
  1560. struct ieee80211_hw *iwl_alloc_all(void)
  1561. {
  1562. struct iwl_priv *priv;
  1563. /* mac80211 allocates memory for this device instance, including
  1564. * space for this driver's private structure */
  1565. struct ieee80211_hw *hw;
  1566. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwlagn_hw_ops);
  1567. if (!hw)
  1568. goto out;
  1569. priv = hw->priv;
  1570. priv->hw = hw;
  1571. out:
  1572. return hw;
  1573. }