mac80211.c 42 KB

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