mac-ctxt.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  37. * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/etherdevice.h>
  68. #include <net/mac80211.h>
  69. #include "iwl-io.h"
  70. #include "iwl-prph.h"
  71. #include "fw-api.h"
  72. #include "mvm.h"
  73. #include "time-event.h"
  74. const u8 iwl_mvm_ac_to_tx_fifo[] = {
  75. IWL_MVM_TX_FIFO_VO,
  76. IWL_MVM_TX_FIFO_VI,
  77. IWL_MVM_TX_FIFO_BE,
  78. IWL_MVM_TX_FIFO_BK,
  79. };
  80. const u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
  81. IWL_GEN2_EDCA_TX_FIFO_VO,
  82. IWL_GEN2_EDCA_TX_FIFO_VI,
  83. IWL_GEN2_EDCA_TX_FIFO_BE,
  84. IWL_GEN2_EDCA_TX_FIFO_BK,
  85. IWL_GEN2_TRIG_TX_FIFO_VO,
  86. IWL_GEN2_TRIG_TX_FIFO_VI,
  87. IWL_GEN2_TRIG_TX_FIFO_BE,
  88. IWL_GEN2_TRIG_TX_FIFO_BK,
  89. };
  90. struct iwl_mvm_mac_iface_iterator_data {
  91. struct iwl_mvm *mvm;
  92. struct ieee80211_vif *vif;
  93. unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
  94. unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
  95. enum iwl_tsf_id preferred_tsf;
  96. bool found_vif;
  97. };
  98. struct iwl_mvm_hw_queues_iface_iterator_data {
  99. struct ieee80211_vif *exclude_vif;
  100. unsigned long used_hw_queues;
  101. };
  102. static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
  103. struct ieee80211_vif *vif)
  104. {
  105. struct iwl_mvm_mac_iface_iterator_data *data = _data;
  106. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  107. u16 min_bi;
  108. /* Skip the interface for which we are trying to assign a tsf_id */
  109. if (vif == data->vif)
  110. return;
  111. /*
  112. * The TSF is a hardware/firmware resource, there are 4 and
  113. * the driver should assign and free them as needed. However,
  114. * there are cases where 2 MACs should share the same TSF ID
  115. * for the purpose of clock sync, an optimization to avoid
  116. * clock drift causing overlapping TBTTs/DTIMs for a GO and
  117. * client in the system.
  118. *
  119. * The firmware will decide according to the MAC type which
  120. * will be the master and slave. Clients that need to sync
  121. * with a remote station will be the master, and an AP or GO
  122. * will be the slave.
  123. *
  124. * Depending on the new interface type it can be slaved to
  125. * or become the master of an existing interface.
  126. */
  127. switch (data->vif->type) {
  128. case NL80211_IFTYPE_STATION:
  129. /*
  130. * The new interface is a client, so if the one we're iterating
  131. * is an AP, and the beacon interval of the AP is a multiple or
  132. * divisor of the beacon interval of the client, the same TSF
  133. * should be used to avoid drift between the new client and
  134. * existing AP. The existing AP will get drift updates from the
  135. * new client context in this case.
  136. */
  137. if (vif->type != NL80211_IFTYPE_AP ||
  138. data->preferred_tsf != NUM_TSF_IDS ||
  139. !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
  140. break;
  141. min_bi = min(data->vif->bss_conf.beacon_int,
  142. vif->bss_conf.beacon_int);
  143. if (!min_bi)
  144. break;
  145. if ((data->vif->bss_conf.beacon_int -
  146. vif->bss_conf.beacon_int) % min_bi == 0) {
  147. data->preferred_tsf = mvmvif->tsf_id;
  148. return;
  149. }
  150. break;
  151. case NL80211_IFTYPE_AP:
  152. /*
  153. * The new interface is AP/GO, so if its beacon interval is a
  154. * multiple or a divisor of the beacon interval of an existing
  155. * interface, it should get drift updates from an existing
  156. * client or use the same TSF as an existing GO. There's no
  157. * drift between TSFs internally but if they used different
  158. * TSFs then a new client MAC could update one of them and
  159. * cause drift that way.
  160. */
  161. if ((vif->type != NL80211_IFTYPE_AP &&
  162. vif->type != NL80211_IFTYPE_STATION) ||
  163. data->preferred_tsf != NUM_TSF_IDS ||
  164. !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
  165. break;
  166. min_bi = min(data->vif->bss_conf.beacon_int,
  167. vif->bss_conf.beacon_int);
  168. if (!min_bi)
  169. break;
  170. if ((data->vif->bss_conf.beacon_int -
  171. vif->bss_conf.beacon_int) % min_bi == 0) {
  172. data->preferred_tsf = mvmvif->tsf_id;
  173. return;
  174. }
  175. break;
  176. default:
  177. /*
  178. * For all other interface types there's no need to
  179. * take drift into account. Either they're exclusive
  180. * like IBSS and monitor, or we don't care much about
  181. * their TSF (like P2P Device), but we won't be able
  182. * to share the TSF resource.
  183. */
  184. break;
  185. }
  186. /*
  187. * Unless we exited above, we can't share the TSF resource
  188. * that the virtual interface we're iterating over is using
  189. * with the new one, so clear the available bit and if this
  190. * was the preferred one, reset that as well.
  191. */
  192. __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
  193. if (data->preferred_tsf == mvmvif->tsf_id)
  194. data->preferred_tsf = NUM_TSF_IDS;
  195. }
  196. /*
  197. * Get the mask of the queues used by the vif
  198. */
  199. u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
  200. {
  201. u32 qmask = 0, ac;
  202. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  203. return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
  204. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  205. if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  206. qmask |= BIT(vif->hw_queue[ac]);
  207. }
  208. if (vif->type == NL80211_IFTYPE_AP ||
  209. vif->type == NL80211_IFTYPE_ADHOC)
  210. qmask |= BIT(vif->cab_queue);
  211. return qmask;
  212. }
  213. static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
  214. struct ieee80211_vif *vif)
  215. {
  216. struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
  217. /* exclude the given vif */
  218. if (vif == data->exclude_vif)
  219. return;
  220. data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
  221. }
  222. unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
  223. struct ieee80211_vif *exclude_vif)
  224. {
  225. struct iwl_mvm_hw_queues_iface_iterator_data data = {
  226. .exclude_vif = exclude_vif,
  227. .used_hw_queues =
  228. BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
  229. BIT(mvm->aux_queue) |
  230. BIT(IWL_MVM_DQA_GCAST_QUEUE),
  231. };
  232. lockdep_assert_held(&mvm->mutex);
  233. /* mark all VIF used hw queues */
  234. ieee80211_iterate_active_interfaces_atomic(
  235. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  236. iwl_mvm_iface_hw_queues_iter, &data);
  237. return data.used_hw_queues;
  238. }
  239. static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
  240. struct ieee80211_vif *vif)
  241. {
  242. struct iwl_mvm_mac_iface_iterator_data *data = _data;
  243. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  244. /* Iterator may already find the interface being added -- skip it */
  245. if (vif == data->vif) {
  246. data->found_vif = true;
  247. return;
  248. }
  249. /* Mark MAC IDs as used by clearing the available bit, and
  250. * (below) mark TSFs as used if their existing use is not
  251. * compatible with the new interface type.
  252. * No locking or atomic bit operations are needed since the
  253. * data is on the stack of the caller function.
  254. */
  255. __clear_bit(mvmvif->id, data->available_mac_ids);
  256. /* find a suitable tsf_id */
  257. iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
  258. }
  259. void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
  260. struct ieee80211_vif *vif)
  261. {
  262. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  263. struct iwl_mvm_mac_iface_iterator_data data = {
  264. .mvm = mvm,
  265. .vif = vif,
  266. .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
  267. /* no preference yet */
  268. .preferred_tsf = NUM_TSF_IDS,
  269. };
  270. ieee80211_iterate_active_interfaces_atomic(
  271. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  272. iwl_mvm_mac_tsf_id_iter, &data);
  273. if (data.preferred_tsf != NUM_TSF_IDS)
  274. mvmvif->tsf_id = data.preferred_tsf;
  275. else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
  276. mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
  277. NUM_TSF_IDS);
  278. }
  279. int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  280. {
  281. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  282. struct iwl_mvm_mac_iface_iterator_data data = {
  283. .mvm = mvm,
  284. .vif = vif,
  285. .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
  286. .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
  287. /* no preference yet */
  288. .preferred_tsf = NUM_TSF_IDS,
  289. .found_vif = false,
  290. };
  291. u32 ac;
  292. int ret, i, queue_limit;
  293. unsigned long used_hw_queues;
  294. lockdep_assert_held(&mvm->mutex);
  295. /*
  296. * Allocate a MAC ID and a TSF for this MAC, along with the queues
  297. * and other resources.
  298. */
  299. /*
  300. * Before the iterator, we start with all MAC IDs and TSFs available.
  301. *
  302. * During iteration, all MAC IDs are cleared that are in use by other
  303. * virtual interfaces, and all TSF IDs are cleared that can't be used
  304. * by this new virtual interface because they're used by an interface
  305. * that can't share it with the new one.
  306. * At the same time, we check if there's a preferred TSF in the case
  307. * that we should share it with another interface.
  308. */
  309. /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
  310. switch (vif->type) {
  311. case NL80211_IFTYPE_ADHOC:
  312. break;
  313. case NL80211_IFTYPE_STATION:
  314. if (!vif->p2p)
  315. break;
  316. /* fall through */
  317. default:
  318. __clear_bit(0, data.available_mac_ids);
  319. }
  320. ieee80211_iterate_active_interfaces_atomic(
  321. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  322. iwl_mvm_mac_iface_iterator, &data);
  323. used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
  324. /*
  325. * In the case we're getting here during resume, it's similar to
  326. * firmware restart, and with RESUME_ALL the iterator will find
  327. * the vif being added already.
  328. * We don't want to reassign any IDs in either case since doing
  329. * so would probably assign different IDs (as interfaces aren't
  330. * necessarily added in the same order), but the old IDs were
  331. * preserved anyway, so skip ID assignment for both resume and
  332. * recovery.
  333. */
  334. if (data.found_vif)
  335. return 0;
  336. /* Therefore, in recovery, we can't get here */
  337. if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
  338. return -EBUSY;
  339. mvmvif->id = find_first_bit(data.available_mac_ids,
  340. NUM_MAC_INDEX_DRIVER);
  341. if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
  342. IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
  343. ret = -EIO;
  344. goto exit_fail;
  345. }
  346. if (data.preferred_tsf != NUM_TSF_IDS)
  347. mvmvif->tsf_id = data.preferred_tsf;
  348. else
  349. mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
  350. NUM_TSF_IDS);
  351. if (mvmvif->tsf_id == NUM_TSF_IDS) {
  352. IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
  353. ret = -EIO;
  354. goto exit_fail;
  355. }
  356. mvmvif->color = 0;
  357. INIT_LIST_HEAD(&mvmvif->time_event_data.list);
  358. mvmvif->time_event_data.id = TE_MAX;
  359. /* No need to allocate data queues to P2P Device MAC.*/
  360. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  361. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  362. vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
  363. return 0;
  364. }
  365. /*
  366. * queues in mac80211 almost entirely independent of
  367. * the ones here - no real limit
  368. */
  369. queue_limit = IEEE80211_MAX_QUEUES;
  370. BUILD_BUG_ON(IEEE80211_MAX_QUEUES >
  371. BITS_PER_BYTE *
  372. sizeof(mvm->hw_queue_to_mac80211[0]));
  373. /*
  374. * Find available queues, and allocate them to the ACs. When in
  375. * DQA-mode they aren't really used, and this is done only so the
  376. * mac80211 ieee80211_check_queues() function won't fail
  377. */
  378. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  379. u8 queue = find_first_zero_bit(&used_hw_queues, queue_limit);
  380. if (queue >= queue_limit) {
  381. IWL_ERR(mvm, "Failed to allocate queue\n");
  382. ret = -EIO;
  383. goto exit_fail;
  384. }
  385. __set_bit(queue, &used_hw_queues);
  386. vif->hw_queue[ac] = queue;
  387. }
  388. /* Allocate the CAB queue for softAP and GO interfaces */
  389. if (vif->type == NL80211_IFTYPE_AP ||
  390. vif->type == NL80211_IFTYPE_ADHOC) {
  391. /*
  392. * For TVQM this will be overwritten later with the FW assigned
  393. * queue value (when queue is enabled).
  394. */
  395. mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
  396. vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
  397. } else {
  398. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  399. }
  400. mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
  401. mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
  402. mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
  403. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
  404. mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
  405. return 0;
  406. exit_fail:
  407. memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
  408. memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
  409. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  410. return ret;
  411. }
  412. static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
  413. struct ieee80211_vif *vif,
  414. enum nl80211_band band,
  415. u8 *cck_rates, u8 *ofdm_rates)
  416. {
  417. struct ieee80211_supported_band *sband;
  418. unsigned long basic = vif->bss_conf.basic_rates;
  419. int lowest_present_ofdm = 100;
  420. int lowest_present_cck = 100;
  421. u8 cck = 0;
  422. u8 ofdm = 0;
  423. int i;
  424. sband = mvm->hw->wiphy->bands[band];
  425. for_each_set_bit(i, &basic, BITS_PER_LONG) {
  426. int hw = sband->bitrates[i].hw_value;
  427. if (hw >= IWL_FIRST_OFDM_RATE) {
  428. ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
  429. if (lowest_present_ofdm > hw)
  430. lowest_present_ofdm = hw;
  431. } else {
  432. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  433. cck |= BIT(hw);
  434. if (lowest_present_cck > hw)
  435. lowest_present_cck = hw;
  436. }
  437. }
  438. /*
  439. * Now we've got the basic rates as bitmaps in the ofdm and cck
  440. * variables. This isn't sufficient though, as there might not
  441. * be all the right rates in the bitmap. E.g. if the only basic
  442. * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
  443. * and 6 Mbps because the 802.11-2007 standard says in 9.6:
  444. *
  445. * [...] a STA responding to a received frame shall transmit
  446. * its Control Response frame [...] at the highest rate in the
  447. * BSSBasicRateSet parameter that is less than or equal to the
  448. * rate of the immediately previous frame in the frame exchange
  449. * sequence ([...]) and that is of the same modulation class
  450. * ([...]) as the received frame. If no rate contained in the
  451. * BSSBasicRateSet parameter meets these conditions, then the
  452. * control frame sent in response to a received frame shall be
  453. * transmitted at the highest mandatory rate of the PHY that is
  454. * less than or equal to the rate of the received frame, and
  455. * that is of the same modulation class as the received frame.
  456. *
  457. * As a consequence, we need to add all mandatory rates that are
  458. * lower than all of the basic rates to these bitmaps.
  459. */
  460. if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
  461. ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
  462. if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
  463. ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
  464. /* 6M already there or needed so always add */
  465. ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
  466. /*
  467. * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
  468. * Note, however:
  469. * - if no CCK rates are basic, it must be ERP since there must
  470. * be some basic rates at all, so they're OFDM => ERP PHY
  471. * (or we're in 5 GHz, and the cck bitmap will never be used)
  472. * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
  473. * - if 5.5M is basic, 1M and 2M are mandatory
  474. * - if 2M is basic, 1M is mandatory
  475. * - if 1M is basic, that's the only valid ACK rate.
  476. * As a consequence, it's not as complicated as it sounds, just add
  477. * any lower rates to the ACK rate bitmap.
  478. */
  479. if (IWL_RATE_11M_INDEX < lowest_present_cck)
  480. cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
  481. if (IWL_RATE_5M_INDEX < lowest_present_cck)
  482. cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
  483. if (IWL_RATE_2M_INDEX < lowest_present_cck)
  484. cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
  485. /* 1M already there or needed so always add */
  486. cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
  487. *cck_rates = cck;
  488. *ofdm_rates = ofdm;
  489. }
  490. static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
  491. struct ieee80211_vif *vif,
  492. struct iwl_mac_ctx_cmd *cmd)
  493. {
  494. /* for both sta and ap, ht_operation_mode hold the protection_mode */
  495. u8 protection_mode = vif->bss_conf.ht_operation_mode &
  496. IEEE80211_HT_OP_MODE_PROTECTION;
  497. /* The fw does not distinguish between ht and fat */
  498. u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
  499. IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
  500. /*
  501. * See section 9.23.3.1 of IEEE 80211-2012.
  502. * Nongreenfield HT STAs Present is not supported.
  503. */
  504. switch (protection_mode) {
  505. case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
  506. break;
  507. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  508. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  509. cmd->protection_flags |= cpu_to_le32(ht_flag);
  510. break;
  511. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  512. /* Protect when channel wider than 20MHz */
  513. if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
  514. cmd->protection_flags |= cpu_to_le32(ht_flag);
  515. break;
  516. default:
  517. IWL_ERR(mvm, "Illegal protection mode %d\n",
  518. protection_mode);
  519. break;
  520. }
  521. }
  522. static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
  523. struct ieee80211_vif *vif,
  524. struct iwl_mac_ctx_cmd *cmd,
  525. const u8 *bssid_override,
  526. u32 action)
  527. {
  528. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  529. struct ieee80211_chanctx_conf *chanctx;
  530. bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
  531. IEEE80211_HT_OP_MODE_PROTECTION);
  532. u8 cck_ack_rates, ofdm_ack_rates;
  533. const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
  534. int i;
  535. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  536. mvmvif->color));
  537. cmd->action = cpu_to_le32(action);
  538. switch (vif->type) {
  539. case NL80211_IFTYPE_STATION:
  540. if (vif->p2p)
  541. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
  542. else
  543. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
  544. break;
  545. case NL80211_IFTYPE_AP:
  546. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
  547. break;
  548. case NL80211_IFTYPE_MONITOR:
  549. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
  550. break;
  551. case NL80211_IFTYPE_P2P_DEVICE:
  552. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
  553. break;
  554. case NL80211_IFTYPE_ADHOC:
  555. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
  556. break;
  557. default:
  558. WARN_ON_ONCE(1);
  559. }
  560. cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
  561. memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
  562. if (bssid)
  563. memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
  564. else
  565. eth_broadcast_addr(cmd->bssid_addr);
  566. rcu_read_lock();
  567. chanctx = rcu_dereference(vif->chanctx_conf);
  568. iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
  569. : NL80211_BAND_2GHZ,
  570. &cck_ack_rates, &ofdm_ack_rates);
  571. rcu_read_unlock();
  572. cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
  573. cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
  574. cmd->cck_short_preamble =
  575. cpu_to_le32(vif->bss_conf.use_short_preamble ?
  576. MAC_FLG_SHORT_PREAMBLE : 0);
  577. cmd->short_slot =
  578. cpu_to_le32(vif->bss_conf.use_short_slot ?
  579. MAC_FLG_SHORT_SLOT : 0);
  580. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
  581. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  582. u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
  583. cmd->ac[txf].cw_min =
  584. cpu_to_le16(mvmvif->queue_params[i].cw_min);
  585. cmd->ac[txf].cw_max =
  586. cpu_to_le16(mvmvif->queue_params[i].cw_max);
  587. cmd->ac[txf].edca_txop =
  588. cpu_to_le16(mvmvif->queue_params[i].txop * 32);
  589. cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
  590. cmd->ac[txf].fifos_mask = BIT(txf);
  591. }
  592. if (vif->bss_conf.qos)
  593. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
  594. if (vif->bss_conf.use_cts_prot)
  595. cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  596. IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
  597. vif->bss_conf.use_cts_prot,
  598. vif->bss_conf.ht_operation_mode);
  599. if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
  600. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
  601. if (ht_enabled)
  602. iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
  603. }
  604. static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
  605. struct iwl_mac_ctx_cmd *cmd)
  606. {
  607. int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
  608. sizeof(*cmd), cmd);
  609. if (ret)
  610. IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
  611. le32_to_cpu(cmd->action), ret);
  612. return ret;
  613. }
  614. static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
  615. struct ieee80211_vif *vif,
  616. u32 action, bool force_assoc_off,
  617. const u8 *bssid_override)
  618. {
  619. struct iwl_mac_ctx_cmd cmd = {};
  620. struct iwl_mac_data_sta *ctxt_sta;
  621. WARN_ON(vif->type != NL80211_IFTYPE_STATION);
  622. /* Fill the common data for all mac context types */
  623. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
  624. if (vif->p2p) {
  625. struct ieee80211_p2p_noa_attr *noa =
  626. &vif->bss_conf.p2p_noa_attr;
  627. cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  628. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  629. ctxt_sta = &cmd.p2p_sta.sta;
  630. } else {
  631. ctxt_sta = &cmd.sta;
  632. }
  633. /* We need the dtim_period to set the MAC as associated */
  634. if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
  635. !force_assoc_off) {
  636. u32 dtim_offs;
  637. /*
  638. * The DTIM count counts down, so when it is N that means N
  639. * more beacon intervals happen until the DTIM TBTT. Therefore
  640. * add this to the current time. If that ends up being in the
  641. * future, the firmware will handle it.
  642. *
  643. * Also note that the system_timestamp (which we get here as
  644. * "sync_device_ts") and TSF timestamp aren't at exactly the
  645. * same offset in the frame -- the TSF is at the first symbol
  646. * of the TSF, the system timestamp is at signal acquisition
  647. * time. This means there's an offset between them of at most
  648. * a few hundred microseconds (24 * 8 bits + PLCP time gives
  649. * 384us in the longest case), this is currently not relevant
  650. * as the firmware wakes up around 2ms before the TBTT.
  651. */
  652. dtim_offs = vif->bss_conf.sync_dtim_count *
  653. vif->bss_conf.beacon_int;
  654. /* convert TU to usecs */
  655. dtim_offs *= 1024;
  656. ctxt_sta->dtim_tsf =
  657. cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
  658. ctxt_sta->dtim_time =
  659. cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
  660. ctxt_sta->assoc_beacon_arrive_time =
  661. cpu_to_le32(vif->bss_conf.sync_device_ts);
  662. IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
  663. le64_to_cpu(ctxt_sta->dtim_tsf),
  664. le32_to_cpu(ctxt_sta->dtim_time),
  665. dtim_offs);
  666. ctxt_sta->is_assoc = cpu_to_le32(1);
  667. } else {
  668. ctxt_sta->is_assoc = cpu_to_le32(0);
  669. /* Allow beacons to pass through as long as we are not
  670. * associated, or we do not have dtim period information.
  671. */
  672. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
  673. }
  674. ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  675. ctxt_sta->bi_reciprocal =
  676. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  677. ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  678. vif->bss_conf.dtim_period);
  679. ctxt_sta->dtim_reciprocal =
  680. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  681. vif->bss_conf.dtim_period));
  682. ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
  683. ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
  684. if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
  685. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  686. if (vif->bss_conf.assoc && vif->bss_conf.he_support &&
  687. !iwlwifi_mod_params.disable_11ax)
  688. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
  689. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  690. }
  691. static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
  692. struct ieee80211_vif *vif,
  693. u32 action)
  694. {
  695. struct iwl_mac_ctx_cmd cmd = {};
  696. u32 tfd_queue_msk = BIT(mvm->snif_queue);
  697. int ret;
  698. WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
  699. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  700. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
  701. MAC_FILTER_IN_CONTROL_AND_MGMT |
  702. MAC_FILTER_IN_BEACON |
  703. MAC_FILTER_IN_PROBE_REQUEST |
  704. MAC_FILTER_IN_CRC32);
  705. ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
  706. /* Allocate sniffer station */
  707. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
  708. vif->type, IWL_STA_GENERAL_PURPOSE);
  709. if (ret)
  710. return ret;
  711. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  712. }
  713. static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
  714. struct ieee80211_vif *vif,
  715. u32 action)
  716. {
  717. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  718. struct iwl_mac_ctx_cmd cmd = {};
  719. WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
  720. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  721. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
  722. MAC_FILTER_IN_PROBE_REQUEST);
  723. /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
  724. cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
  725. cmd.ibss.bi_reciprocal =
  726. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  727. /* TODO: Assumes that the beacon id == mac context id */
  728. cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
  729. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  730. }
  731. struct iwl_mvm_go_iterator_data {
  732. bool go_active;
  733. };
  734. static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
  735. {
  736. struct iwl_mvm_go_iterator_data *data = _data;
  737. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  738. if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
  739. mvmvif->ap_ibss_active)
  740. data->go_active = true;
  741. }
  742. static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
  743. struct ieee80211_vif *vif,
  744. u32 action)
  745. {
  746. struct iwl_mac_ctx_cmd cmd = {};
  747. struct iwl_mvm_go_iterator_data data = {};
  748. WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
  749. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  750. cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  751. /* Override the filter flags to accept only probe requests */
  752. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  753. /*
  754. * This flag should be set to true when the P2P Device is
  755. * discoverable and there is at least another active P2P GO. Settings
  756. * this flag will allow the P2P Device to be discoverable on other
  757. * channels in addition to its listen channel.
  758. * Note that this flag should not be set in other cases as it opens the
  759. * Rx filters on all MAC and increases the number of interrupts.
  760. */
  761. ieee80211_iterate_active_interfaces_atomic(
  762. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  763. iwl_mvm_go_iterator, &data);
  764. cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
  765. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  766. }
  767. static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
  768. __le32 *tim_index, __le32 *tim_size,
  769. u8 *beacon, u32 frame_size)
  770. {
  771. u32 tim_idx;
  772. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  773. /* The index is relative to frame start but we start looking at the
  774. * variable-length part of the beacon. */
  775. tim_idx = mgmt->u.beacon.variable - beacon;
  776. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  777. while ((tim_idx < (frame_size - 2)) &&
  778. (beacon[tim_idx] != WLAN_EID_TIM))
  779. tim_idx += beacon[tim_idx+1] + 2;
  780. /* If TIM field was found, set variables */
  781. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  782. *tim_index = cpu_to_le32(tim_idx);
  783. *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
  784. } else {
  785. IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
  786. }
  787. }
  788. static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
  789. {
  790. struct ieee80211_mgmt *mgmt = (void *)beacon;
  791. const u8 *ie;
  792. if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
  793. return 0;
  794. frame_size -= mgmt->u.beacon.variable - beacon;
  795. ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
  796. if (!ie)
  797. return 0;
  798. return ie - beacon;
  799. }
  800. static u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
  801. struct ieee80211_vif *vif)
  802. {
  803. u8 rate;
  804. if (info->band == NL80211_BAND_5GHZ || vif->p2p)
  805. rate = IWL_FIRST_OFDM_RATE;
  806. else
  807. rate = IWL_FIRST_CCK_RATE;
  808. return rate;
  809. }
  810. static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
  811. struct ieee80211_vif *vif,
  812. struct sk_buff *beacon,
  813. struct iwl_tx_cmd *tx)
  814. {
  815. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  816. struct ieee80211_tx_info *info;
  817. u8 rate;
  818. u32 tx_flags;
  819. info = IEEE80211_SKB_CB(beacon);
  820. /* Set up TX command fields */
  821. tx->len = cpu_to_le16((u16)beacon->len);
  822. tx->sta_id = mvmvif->bcast_sta.sta_id;
  823. tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  824. tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
  825. tx_flags |=
  826. iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
  827. TX_CMD_FLG_BT_PRIO_POS;
  828. tx->tx_flags = cpu_to_le32(tx_flags);
  829. if (!fw_has_capa(&mvm->fw->ucode_capa,
  830. IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
  831. mvm->mgmt_last_antenna_idx =
  832. iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
  833. mvm->mgmt_last_antenna_idx);
  834. }
  835. tx->rate_n_flags =
  836. cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
  837. RATE_MCS_ANT_POS);
  838. rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
  839. tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
  840. if (rate == IWL_FIRST_CCK_RATE)
  841. tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
  842. }
  843. static int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
  844. struct sk_buff *beacon,
  845. void *data, int len)
  846. {
  847. struct iwl_host_cmd cmd = {
  848. .id = BEACON_TEMPLATE_CMD,
  849. .flags = CMD_ASYNC,
  850. };
  851. cmd.len[0] = len;
  852. cmd.data[0] = data;
  853. cmd.dataflags[0] = 0;
  854. cmd.len[1] = beacon->len;
  855. cmd.data[1] = beacon->data;
  856. cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
  857. return iwl_mvm_send_cmd(mvm, &cmd);
  858. }
  859. static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
  860. struct ieee80211_vif *vif,
  861. struct sk_buff *beacon)
  862. {
  863. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  864. struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
  865. iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
  866. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  867. if (vif->type == NL80211_IFTYPE_AP)
  868. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
  869. &beacon_cmd.tim_size,
  870. beacon->data, beacon->len);
  871. return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
  872. sizeof(beacon_cmd));
  873. }
  874. static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
  875. struct ieee80211_vif *vif,
  876. struct sk_buff *beacon)
  877. {
  878. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  879. struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
  880. iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
  881. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  882. if (vif->type == NL80211_IFTYPE_AP)
  883. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
  884. &beacon_cmd.tim_size,
  885. beacon->data, beacon->len);
  886. beacon_cmd.csa_offset =
  887. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  888. WLAN_EID_CHANNEL_SWITCH,
  889. beacon->len));
  890. beacon_cmd.ecsa_offset =
  891. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  892. WLAN_EID_EXT_CHANSWITCH_ANN,
  893. beacon->len));
  894. return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
  895. sizeof(beacon_cmd));
  896. }
  897. static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
  898. struct ieee80211_vif *vif,
  899. struct sk_buff *beacon)
  900. {
  901. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  902. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
  903. struct iwl_mac_beacon_cmd beacon_cmd = {};
  904. u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
  905. u16 flags;
  906. flags = iwl_mvm_mac80211_idx_to_hwrate(rate);
  907. if (rate == IWL_FIRST_CCK_RATE)
  908. flags |= IWL_MAC_BEACON_CCK;
  909. beacon_cmd.flags = cpu_to_le16(flags);
  910. beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
  911. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  912. if (vif->type == NL80211_IFTYPE_AP)
  913. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
  914. &beacon_cmd.tim_size,
  915. beacon->data, beacon->len);
  916. beacon_cmd.csa_offset =
  917. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  918. WLAN_EID_CHANNEL_SWITCH,
  919. beacon->len));
  920. beacon_cmd.ecsa_offset =
  921. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  922. WLAN_EID_EXT_CHANSWITCH_ANN,
  923. beacon->len));
  924. return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
  925. sizeof(beacon_cmd));
  926. }
  927. static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
  928. struct ieee80211_vif *vif,
  929. struct sk_buff *beacon)
  930. {
  931. if (WARN_ON(!beacon))
  932. return -EINVAL;
  933. if (!fw_has_capa(&mvm->fw->ucode_capa,
  934. IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
  935. return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
  936. if (fw_has_api(&mvm->fw->ucode_capa,
  937. IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
  938. return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
  939. return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
  940. }
  941. /* The beacon template for the AP/GO/IBSS has changed and needs update */
  942. int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
  943. struct ieee80211_vif *vif)
  944. {
  945. struct sk_buff *beacon;
  946. int ret;
  947. WARN_ON(vif->type != NL80211_IFTYPE_AP &&
  948. vif->type != NL80211_IFTYPE_ADHOC);
  949. beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
  950. if (!beacon)
  951. return -ENOMEM;
  952. ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
  953. dev_kfree_skb(beacon);
  954. return ret;
  955. }
  956. struct iwl_mvm_mac_ap_iterator_data {
  957. struct iwl_mvm *mvm;
  958. struct ieee80211_vif *vif;
  959. u32 beacon_device_ts;
  960. u16 beacon_int;
  961. };
  962. /* Find the beacon_device_ts and beacon_int for a managed interface */
  963. static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
  964. struct ieee80211_vif *vif)
  965. {
  966. struct iwl_mvm_mac_ap_iterator_data *data = _data;
  967. if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
  968. return;
  969. /* Station client has higher priority over P2P client*/
  970. if (vif->p2p && data->beacon_device_ts)
  971. return;
  972. data->beacon_device_ts = vif->bss_conf.sync_device_ts;
  973. data->beacon_int = vif->bss_conf.beacon_int;
  974. }
  975. /*
  976. * Fill the specific data for mac context of type AP of P2P GO
  977. */
  978. static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
  979. struct ieee80211_vif *vif,
  980. struct iwl_mac_ctx_cmd *cmd,
  981. struct iwl_mac_data_ap *ctxt_ap,
  982. bool add)
  983. {
  984. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  985. struct iwl_mvm_mac_ap_iterator_data data = {
  986. .mvm = mvm,
  987. .vif = vif,
  988. .beacon_device_ts = 0
  989. };
  990. /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
  991. cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
  992. /*
  993. * in AP mode, pass probe requests and beacons from other APs
  994. * (needed for ht protection); when there're no any associated
  995. * station don't ask FW to pass beacons to prevent unnecessary
  996. * wake-ups.
  997. */
  998. cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  999. if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
  1000. cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
  1001. IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
  1002. } else {
  1003. IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
  1004. }
  1005. ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  1006. ctxt_ap->bi_reciprocal =
  1007. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  1008. ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  1009. vif->bss_conf.dtim_period);
  1010. ctxt_ap->dtim_reciprocal =
  1011. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  1012. vif->bss_conf.dtim_period));
  1013. if (!fw_has_api(&mvm->fw->ucode_capa,
  1014. IWL_UCODE_TLV_API_STA_TYPE))
  1015. ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
  1016. /*
  1017. * Only set the beacon time when the MAC is being added, when we
  1018. * just modify the MAC then we should keep the time -- the firmware
  1019. * can otherwise have a "jumping" TBTT.
  1020. */
  1021. if (add) {
  1022. /*
  1023. * If there is a station/P2P client interface which is
  1024. * associated, set the AP's TBTT far enough from the station's
  1025. * TBTT. Otherwise, set it to the current system time
  1026. */
  1027. ieee80211_iterate_active_interfaces_atomic(
  1028. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  1029. iwl_mvm_mac_ap_iterator, &data);
  1030. if (data.beacon_device_ts) {
  1031. u32 rand = (prandom_u32() % (64 - 36)) + 36;
  1032. mvmvif->ap_beacon_time = data.beacon_device_ts +
  1033. ieee80211_tu_to_usec(data.beacon_int * rand /
  1034. 100);
  1035. } else {
  1036. mvmvif->ap_beacon_time =
  1037. iwl_read_prph(mvm->trans,
  1038. DEVICE_SYSTEM_TIME_REG);
  1039. }
  1040. }
  1041. ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
  1042. ctxt_ap->beacon_tsf = 0; /* unused */
  1043. /* TODO: Assume that the beacon id == mac context id */
  1044. ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
  1045. }
  1046. static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
  1047. struct ieee80211_vif *vif,
  1048. u32 action)
  1049. {
  1050. struct iwl_mac_ctx_cmd cmd = {};
  1051. WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
  1052. /* Fill the common data for all mac context types */
  1053. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  1054. /* Fill the data specific for ap mode */
  1055. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
  1056. action == FW_CTXT_ACTION_ADD);
  1057. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  1058. }
  1059. static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
  1060. struct ieee80211_vif *vif,
  1061. u32 action)
  1062. {
  1063. struct iwl_mac_ctx_cmd cmd = {};
  1064. struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
  1065. WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
  1066. /* Fill the common data for all mac context types */
  1067. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  1068. /* Fill the data specific for GO mode */
  1069. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
  1070. action == FW_CTXT_ACTION_ADD);
  1071. cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  1072. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  1073. cmd.go.opp_ps_enabled =
  1074. cpu_to_le32(!!(noa->oppps_ctwindow &
  1075. IEEE80211_P2P_OPPPS_ENABLE_BIT));
  1076. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  1077. }
  1078. static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1079. u32 action, bool force_assoc_off,
  1080. const u8 *bssid_override)
  1081. {
  1082. switch (vif->type) {
  1083. case NL80211_IFTYPE_STATION:
  1084. return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
  1085. force_assoc_off,
  1086. bssid_override);
  1087. break;
  1088. case NL80211_IFTYPE_AP:
  1089. if (!vif->p2p)
  1090. return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
  1091. else
  1092. return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
  1093. break;
  1094. case NL80211_IFTYPE_MONITOR:
  1095. return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
  1096. case NL80211_IFTYPE_P2P_DEVICE:
  1097. return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
  1098. case NL80211_IFTYPE_ADHOC:
  1099. return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
  1100. default:
  1101. break;
  1102. }
  1103. return -EOPNOTSUPP;
  1104. }
  1105. int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1106. {
  1107. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1108. int ret;
  1109. if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
  1110. vif->addr, ieee80211_vif_type_p2p(vif)))
  1111. return -EIO;
  1112. ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
  1113. true, NULL);
  1114. if (ret)
  1115. return ret;
  1116. /* will only do anything at resume from D3 time */
  1117. iwl_mvm_set_last_nonqos_seq(mvm, vif);
  1118. mvmvif->uploaded = true;
  1119. return 0;
  1120. }
  1121. int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1122. bool force_assoc_off, const u8 *bssid_override)
  1123. {
  1124. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1125. if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
  1126. vif->addr, ieee80211_vif_type_p2p(vif)))
  1127. return -EIO;
  1128. return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
  1129. force_assoc_off, bssid_override);
  1130. }
  1131. int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1132. {
  1133. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1134. struct iwl_mac_ctx_cmd cmd;
  1135. int ret;
  1136. if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
  1137. vif->addr, ieee80211_vif_type_p2p(vif)))
  1138. return -EIO;
  1139. memset(&cmd, 0, sizeof(cmd));
  1140. cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  1141. mvmvif->color));
  1142. cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  1143. ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
  1144. sizeof(cmd), &cmd);
  1145. if (ret) {
  1146. IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
  1147. return ret;
  1148. }
  1149. mvmvif->uploaded = false;
  1150. if (vif->type == NL80211_IFTYPE_MONITOR) {
  1151. __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
  1152. iwl_mvm_dealloc_snif_sta(mvm);
  1153. }
  1154. return 0;
  1155. }
  1156. static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
  1157. struct ieee80211_vif *csa_vif, u32 gp2,
  1158. bool tx_success)
  1159. {
  1160. struct iwl_mvm_vif *mvmvif =
  1161. iwl_mvm_vif_from_mac80211(csa_vif);
  1162. /* Don't start to countdown from a failed beacon */
  1163. if (!tx_success && !mvmvif->csa_countdown)
  1164. return;
  1165. mvmvif->csa_countdown = true;
  1166. if (!ieee80211_csa_is_complete(csa_vif)) {
  1167. int c = ieee80211_csa_update_counter(csa_vif);
  1168. iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
  1169. if (csa_vif->p2p &&
  1170. !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
  1171. tx_success) {
  1172. u32 rel_time = (c + 1) *
  1173. csa_vif->bss_conf.beacon_int -
  1174. IWL_MVM_CHANNEL_SWITCH_TIME_GO;
  1175. u32 apply_time = gp2 + rel_time * 1024;
  1176. iwl_mvm_schedule_csa_period(mvm, csa_vif,
  1177. IWL_MVM_CHANNEL_SWITCH_TIME_GO -
  1178. IWL_MVM_CHANNEL_SWITCH_MARGIN,
  1179. apply_time);
  1180. }
  1181. } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
  1182. /* we don't have CSA NoA scheduled yet, switch now */
  1183. ieee80211_csa_finish(csa_vif);
  1184. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  1185. }
  1186. }
  1187. void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
  1188. struct iwl_rx_cmd_buffer *rxb)
  1189. {
  1190. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1191. struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
  1192. struct iwl_mvm_tx_resp *beacon_notify_hdr;
  1193. struct ieee80211_vif *csa_vif;
  1194. struct ieee80211_vif *tx_blocked_vif;
  1195. struct agg_tx_status *agg_status;
  1196. u16 status;
  1197. lockdep_assert_held(&mvm->mutex);
  1198. beacon_notify_hdr = &beacon->beacon_notify_hdr;
  1199. mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
  1200. mvm->ibss_manager = beacon->ibss_mgr_status != 0;
  1201. agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
  1202. status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
  1203. IWL_DEBUG_RX(mvm,
  1204. "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
  1205. status, beacon_notify_hdr->failure_frame,
  1206. le64_to_cpu(beacon->tsf),
  1207. mvm->ap_last_beacon_gp2,
  1208. le32_to_cpu(beacon_notify_hdr->initial_rate));
  1209. csa_vif = rcu_dereference_protected(mvm->csa_vif,
  1210. lockdep_is_held(&mvm->mutex));
  1211. if (unlikely(csa_vif && csa_vif->csa_active))
  1212. iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
  1213. (status == TX_STATUS_SUCCESS));
  1214. tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
  1215. lockdep_is_held(&mvm->mutex));
  1216. if (unlikely(tx_blocked_vif)) {
  1217. struct iwl_mvm_vif *mvmvif =
  1218. iwl_mvm_vif_from_mac80211(tx_blocked_vif);
  1219. /*
  1220. * The channel switch is started and we have blocked the
  1221. * stations. If this is the first beacon (the timeout wasn't
  1222. * set), set the unblock timeout, otherwise countdown
  1223. */
  1224. if (!mvm->csa_tx_block_bcn_timeout)
  1225. mvm->csa_tx_block_bcn_timeout =
  1226. IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
  1227. else
  1228. mvm->csa_tx_block_bcn_timeout--;
  1229. /* Check if the timeout is expired, and unblock tx */
  1230. if (mvm->csa_tx_block_bcn_timeout == 0) {
  1231. iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
  1232. RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
  1233. }
  1234. }
  1235. }
  1236. static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
  1237. struct ieee80211_vif *vif)
  1238. {
  1239. struct iwl_missed_beacons_notif *missed_beacons = _data;
  1240. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1241. struct iwl_mvm *mvm = mvmvif->mvm;
  1242. struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
  1243. struct iwl_fw_dbg_trigger_tlv *trigger;
  1244. u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
  1245. u32 rx_missed_bcon, rx_missed_bcon_since_rx;
  1246. if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
  1247. return;
  1248. rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
  1249. rx_missed_bcon_since_rx =
  1250. le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
  1251. /*
  1252. * TODO: the threshold should be adjusted based on latency conditions,
  1253. * and/or in case of a CS flow on one of the other AP vifs.
  1254. */
  1255. if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
  1256. IWL_MVM_MISSED_BEACONS_THRESHOLD)
  1257. ieee80211_beacon_loss(vif);
  1258. if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
  1259. FW_DBG_TRIGGER_MISSED_BEACONS))
  1260. return;
  1261. trigger = iwl_fw_dbg_get_trigger(mvm->fw,
  1262. FW_DBG_TRIGGER_MISSED_BEACONS);
  1263. bcon_trig = (void *)trigger->data;
  1264. stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
  1265. stop_trig_missed_bcon_since_rx =
  1266. le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
  1267. /* TODO: implement start trigger */
  1268. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  1269. ieee80211_vif_to_wdev(vif),
  1270. trigger))
  1271. return;
  1272. if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
  1273. rx_missed_bcon >= stop_trig_missed_bcon)
  1274. iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
  1275. }
  1276. void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
  1277. struct iwl_rx_cmd_buffer *rxb)
  1278. {
  1279. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1280. struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
  1281. IWL_DEBUG_INFO(mvm,
  1282. "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
  1283. le32_to_cpu(mb->mac_id),
  1284. le32_to_cpu(mb->consec_missed_beacons),
  1285. le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
  1286. le32_to_cpu(mb->num_recvd_beacons),
  1287. le32_to_cpu(mb->num_expected_beacons));
  1288. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1289. IEEE80211_IFACE_ITER_NORMAL,
  1290. iwl_mvm_beacon_loss_iterator,
  1291. mb);
  1292. }
  1293. void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
  1294. struct iwl_rx_cmd_buffer *rxb)
  1295. {
  1296. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1297. struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
  1298. struct ieee80211_rx_status rx_status;
  1299. struct sk_buff *skb;
  1300. u32 size = le32_to_cpu(sb->byte_count);
  1301. if (size == 0)
  1302. return;
  1303. skb = alloc_skb(size, GFP_ATOMIC);
  1304. if (!skb) {
  1305. IWL_ERR(mvm, "alloc_skb failed\n");
  1306. return;
  1307. }
  1308. /* update rx_status according to the notification's metadata */
  1309. memset(&rx_status, 0, sizeof(rx_status));
  1310. rx_status.mactime = le64_to_cpu(sb->tsf);
  1311. /* TSF as indicated by the firmware is at INA time */
  1312. rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
  1313. rx_status.device_timestamp = le32_to_cpu(sb->system_time);
  1314. rx_status.band =
  1315. (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
  1316. NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
  1317. rx_status.freq =
  1318. ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
  1319. rx_status.band);
  1320. /* copy the data */
  1321. skb_put_data(skb, sb->data, size);
  1322. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1323. /* pass it as regular rx to mac80211 */
  1324. ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
  1325. }
  1326. void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
  1327. struct iwl_rx_cmd_buffer *rxb)
  1328. {
  1329. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1330. struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
  1331. struct ieee80211_vif *csa_vif;
  1332. struct iwl_mvm_vif *mvmvif;
  1333. int len = iwl_rx_packet_payload_len(pkt);
  1334. u32 id_n_color;
  1335. if (WARN_ON_ONCE(len < sizeof(*notif)))
  1336. return;
  1337. rcu_read_lock();
  1338. csa_vif = rcu_dereference(mvm->csa_vif);
  1339. if (WARN_ON(!csa_vif || !csa_vif->csa_active))
  1340. goto out_unlock;
  1341. id_n_color = le32_to_cpu(notif->id_and_color);
  1342. mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
  1343. if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
  1344. "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
  1345. FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
  1346. goto out_unlock;
  1347. IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
  1348. schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
  1349. msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
  1350. csa_vif->bss_conf.beacon_int));
  1351. ieee80211_csa_finish(csa_vif);
  1352. rcu_read_unlock();
  1353. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  1354. return;
  1355. out_unlock:
  1356. rcu_read_unlock();
  1357. }