iwl-mac80211.c 42 KB

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