mac-ctxt.c 49 KB

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