iwl-mac80211.c 42 KB

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