iwl-mac80211.c 42 KB

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