iwl-mac80211.c 41 KB

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