mac80211.c 42 KB

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