mac80211.c 42 KB

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