iwl-mac80211.c 41 KB

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