mac-ctxt.c 50 KB

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