mac-ctxt.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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. vif->type == NL80211_IFTYPE_ADHOC) {
  387. /*
  388. * For TVQM this will be overwritten later with the FW assigned
  389. * queue value (when queue is enabled).
  390. */
  391. mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
  392. vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
  393. } else {
  394. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  395. }
  396. mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
  397. mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
  398. mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
  399. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
  400. mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
  401. return 0;
  402. exit_fail:
  403. memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
  404. memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
  405. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  406. return ret;
  407. }
  408. static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
  409. struct ieee80211_vif *vif,
  410. enum nl80211_band band,
  411. u8 *cck_rates, u8 *ofdm_rates)
  412. {
  413. struct ieee80211_supported_band *sband;
  414. unsigned long basic = vif->bss_conf.basic_rates;
  415. int lowest_present_ofdm = 100;
  416. int lowest_present_cck = 100;
  417. u8 cck = 0;
  418. u8 ofdm = 0;
  419. int i;
  420. sband = mvm->hw->wiphy->bands[band];
  421. for_each_set_bit(i, &basic, BITS_PER_LONG) {
  422. int hw = sband->bitrates[i].hw_value;
  423. if (hw >= IWL_FIRST_OFDM_RATE) {
  424. ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
  425. if (lowest_present_ofdm > hw)
  426. lowest_present_ofdm = hw;
  427. } else {
  428. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  429. cck |= BIT(hw);
  430. if (lowest_present_cck > hw)
  431. lowest_present_cck = hw;
  432. }
  433. }
  434. /*
  435. * Now we've got the basic rates as bitmaps in the ofdm and cck
  436. * variables. This isn't sufficient though, as there might not
  437. * be all the right rates in the bitmap. E.g. if the only basic
  438. * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
  439. * and 6 Mbps because the 802.11-2007 standard says in 9.6:
  440. *
  441. * [...] a STA responding to a received frame shall transmit
  442. * its Control Response frame [...] at the highest rate in the
  443. * BSSBasicRateSet parameter that is less than or equal to the
  444. * rate of the immediately previous frame in the frame exchange
  445. * sequence ([...]) and that is of the same modulation class
  446. * ([...]) as the received frame. If no rate contained in the
  447. * BSSBasicRateSet parameter meets these conditions, then the
  448. * control frame sent in response to a received frame shall be
  449. * transmitted at the highest mandatory rate of the PHY that is
  450. * less than or equal to the rate of the received frame, and
  451. * that is of the same modulation class as the received frame.
  452. *
  453. * As a consequence, we need to add all mandatory rates that are
  454. * lower than all of the basic rates to these bitmaps.
  455. */
  456. if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
  457. ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
  458. if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
  459. ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
  460. /* 6M already there or needed so always add */
  461. ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
  462. /*
  463. * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
  464. * Note, however:
  465. * - if no CCK rates are basic, it must be ERP since there must
  466. * be some basic rates at all, so they're OFDM => ERP PHY
  467. * (or we're in 5 GHz, and the cck bitmap will never be used)
  468. * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
  469. * - if 5.5M is basic, 1M and 2M are mandatory
  470. * - if 2M is basic, 1M is mandatory
  471. * - if 1M is basic, that's the only valid ACK rate.
  472. * As a consequence, it's not as complicated as it sounds, just add
  473. * any lower rates to the ACK rate bitmap.
  474. */
  475. if (IWL_RATE_11M_INDEX < lowest_present_cck)
  476. cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
  477. if (IWL_RATE_5M_INDEX < lowest_present_cck)
  478. cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
  479. if (IWL_RATE_2M_INDEX < lowest_present_cck)
  480. cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
  481. /* 1M already there or needed so always add */
  482. cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
  483. *cck_rates = cck;
  484. *ofdm_rates = ofdm;
  485. }
  486. static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
  487. struct ieee80211_vif *vif,
  488. struct iwl_mac_ctx_cmd *cmd)
  489. {
  490. /* for both sta and ap, ht_operation_mode hold the protection_mode */
  491. u8 protection_mode = vif->bss_conf.ht_operation_mode &
  492. IEEE80211_HT_OP_MODE_PROTECTION;
  493. /* The fw does not distinguish between ht and fat */
  494. u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
  495. IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
  496. /*
  497. * See section 9.23.3.1 of IEEE 80211-2012.
  498. * Nongreenfield HT STAs Present is not supported.
  499. */
  500. switch (protection_mode) {
  501. case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
  502. break;
  503. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  504. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  505. cmd->protection_flags |= cpu_to_le32(ht_flag);
  506. break;
  507. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  508. /* Protect when channel wider than 20MHz */
  509. if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
  510. cmd->protection_flags |= cpu_to_le32(ht_flag);
  511. break;
  512. default:
  513. IWL_ERR(mvm, "Illegal protection mode %d\n",
  514. protection_mode);
  515. break;
  516. }
  517. }
  518. static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
  519. struct ieee80211_vif *vif,
  520. struct iwl_mac_ctx_cmd *cmd,
  521. const u8 *bssid_override,
  522. u32 action)
  523. {
  524. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  525. struct ieee80211_chanctx_conf *chanctx;
  526. bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
  527. IEEE80211_HT_OP_MODE_PROTECTION);
  528. u8 cck_ack_rates, ofdm_ack_rates;
  529. const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
  530. int i;
  531. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  532. mvmvif->color));
  533. cmd->action = cpu_to_le32(action);
  534. switch (vif->type) {
  535. case NL80211_IFTYPE_STATION:
  536. if (vif->p2p)
  537. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
  538. else
  539. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
  540. break;
  541. case NL80211_IFTYPE_AP:
  542. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
  543. break;
  544. case NL80211_IFTYPE_MONITOR:
  545. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
  546. break;
  547. case NL80211_IFTYPE_P2P_DEVICE:
  548. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
  549. break;
  550. case NL80211_IFTYPE_ADHOC:
  551. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
  552. break;
  553. default:
  554. WARN_ON_ONCE(1);
  555. }
  556. cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
  557. memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
  558. if (bssid)
  559. memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
  560. else
  561. eth_broadcast_addr(cmd->bssid_addr);
  562. rcu_read_lock();
  563. chanctx = rcu_dereference(vif->chanctx_conf);
  564. iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
  565. : NL80211_BAND_2GHZ,
  566. &cck_ack_rates, &ofdm_ack_rates);
  567. rcu_read_unlock();
  568. cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
  569. cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
  570. cmd->cck_short_preamble =
  571. cpu_to_le32(vif->bss_conf.use_short_preamble ?
  572. MAC_FLG_SHORT_PREAMBLE : 0);
  573. cmd->short_slot =
  574. cpu_to_le32(vif->bss_conf.use_short_slot ?
  575. MAC_FLG_SHORT_SLOT : 0);
  576. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
  577. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  578. u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
  579. cmd->ac[txf].cw_min =
  580. cpu_to_le16(mvmvif->queue_params[i].cw_min);
  581. cmd->ac[txf].cw_max =
  582. cpu_to_le16(mvmvif->queue_params[i].cw_max);
  583. cmd->ac[txf].edca_txop =
  584. cpu_to_le16(mvmvif->queue_params[i].txop * 32);
  585. cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
  586. cmd->ac[txf].fifos_mask = BIT(txf);
  587. }
  588. if (vif->bss_conf.qos)
  589. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
  590. if (vif->bss_conf.use_cts_prot)
  591. cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  592. IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
  593. vif->bss_conf.use_cts_prot,
  594. vif->bss_conf.ht_operation_mode);
  595. if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
  596. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
  597. if (ht_enabled)
  598. iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
  599. }
  600. static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
  601. struct iwl_mac_ctx_cmd *cmd)
  602. {
  603. int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
  604. sizeof(*cmd), cmd);
  605. if (ret)
  606. IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
  607. le32_to_cpu(cmd->action), ret);
  608. return ret;
  609. }
  610. static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
  611. struct ieee80211_vif *vif,
  612. u32 action, bool force_assoc_off,
  613. const u8 *bssid_override)
  614. {
  615. struct iwl_mac_ctx_cmd cmd = {};
  616. struct iwl_mac_data_sta *ctxt_sta;
  617. WARN_ON(vif->type != NL80211_IFTYPE_STATION);
  618. /* Fill the common data for all mac context types */
  619. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
  620. if (vif->p2p) {
  621. struct ieee80211_p2p_noa_attr *noa =
  622. &vif->bss_conf.p2p_noa_attr;
  623. cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  624. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  625. ctxt_sta = &cmd.p2p_sta.sta;
  626. } else {
  627. ctxt_sta = &cmd.sta;
  628. }
  629. /* We need the dtim_period to set the MAC as associated */
  630. if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
  631. !force_assoc_off) {
  632. u32 dtim_offs;
  633. /*
  634. * The DTIM count counts down, so when it is N that means N
  635. * more beacon intervals happen until the DTIM TBTT. Therefore
  636. * add this to the current time. If that ends up being in the
  637. * future, the firmware will handle it.
  638. *
  639. * Also note that the system_timestamp (which we get here as
  640. * "sync_device_ts") and TSF timestamp aren't at exactly the
  641. * same offset in the frame -- the TSF is at the first symbol
  642. * of the TSF, the system timestamp is at signal acquisition
  643. * time. This means there's an offset between them of at most
  644. * a few hundred microseconds (24 * 8 bits + PLCP time gives
  645. * 384us in the longest case), this is currently not relevant
  646. * as the firmware wakes up around 2ms before the TBTT.
  647. */
  648. dtim_offs = vif->bss_conf.sync_dtim_count *
  649. vif->bss_conf.beacon_int;
  650. /* convert TU to usecs */
  651. dtim_offs *= 1024;
  652. ctxt_sta->dtim_tsf =
  653. cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
  654. ctxt_sta->dtim_time =
  655. cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
  656. ctxt_sta->assoc_beacon_arrive_time =
  657. cpu_to_le32(vif->bss_conf.sync_device_ts);
  658. IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
  659. le64_to_cpu(ctxt_sta->dtim_tsf),
  660. le32_to_cpu(ctxt_sta->dtim_time),
  661. dtim_offs);
  662. ctxt_sta->is_assoc = cpu_to_le32(1);
  663. } else {
  664. ctxt_sta->is_assoc = cpu_to_le32(0);
  665. /* Allow beacons to pass through as long as we are not
  666. * associated, or we do not have dtim period information.
  667. */
  668. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
  669. }
  670. ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  671. ctxt_sta->bi_reciprocal =
  672. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  673. ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  674. vif->bss_conf.dtim_period);
  675. ctxt_sta->dtim_reciprocal =
  676. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  677. vif->bss_conf.dtim_period));
  678. ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
  679. ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
  680. if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
  681. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  682. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  683. }
  684. static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
  685. struct ieee80211_vif *vif,
  686. u32 action)
  687. {
  688. struct iwl_mac_ctx_cmd cmd = {};
  689. u32 tfd_queue_msk = BIT(mvm->snif_queue);
  690. int ret;
  691. WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
  692. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  693. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
  694. MAC_FILTER_IN_CONTROL_AND_MGMT |
  695. MAC_FILTER_IN_BEACON |
  696. MAC_FILTER_IN_PROBE_REQUEST |
  697. MAC_FILTER_IN_CRC32);
  698. ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
  699. /* Allocate sniffer station */
  700. ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
  701. vif->type, IWL_STA_GENERAL_PURPOSE);
  702. if (ret)
  703. return ret;
  704. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  705. }
  706. static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
  707. struct ieee80211_vif *vif,
  708. u32 action)
  709. {
  710. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  711. struct iwl_mac_ctx_cmd cmd = {};
  712. WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
  713. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  714. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
  715. MAC_FILTER_IN_PROBE_REQUEST);
  716. /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
  717. cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
  718. cmd.ibss.bi_reciprocal =
  719. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  720. /* TODO: Assumes that the beacon id == mac context id */
  721. cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
  722. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  723. }
  724. struct iwl_mvm_go_iterator_data {
  725. bool go_active;
  726. };
  727. static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
  728. {
  729. struct iwl_mvm_go_iterator_data *data = _data;
  730. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  731. if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
  732. mvmvif->ap_ibss_active)
  733. data->go_active = true;
  734. }
  735. static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
  736. struct ieee80211_vif *vif,
  737. u32 action)
  738. {
  739. struct iwl_mac_ctx_cmd cmd = {};
  740. struct iwl_mvm_go_iterator_data data = {};
  741. WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
  742. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  743. cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  744. /* Override the filter flags to accept only probe requests */
  745. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  746. /*
  747. * This flag should be set to true when the P2P Device is
  748. * discoverable and there is at least another active P2P GO. Settings
  749. * this flag will allow the P2P Device to be discoverable on other
  750. * channels in addition to its listen channel.
  751. * Note that this flag should not be set in other cases as it opens the
  752. * Rx filters on all MAC and increases the number of interrupts.
  753. */
  754. ieee80211_iterate_active_interfaces_atomic(
  755. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  756. iwl_mvm_go_iterator, &data);
  757. cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
  758. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  759. }
  760. static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
  761. __le32 *tim_index, __le32 *tim_size,
  762. u8 *beacon, u32 frame_size)
  763. {
  764. u32 tim_idx;
  765. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  766. /* The index is relative to frame start but we start looking at the
  767. * variable-length part of the beacon. */
  768. tim_idx = mgmt->u.beacon.variable - beacon;
  769. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  770. while ((tim_idx < (frame_size - 2)) &&
  771. (beacon[tim_idx] != WLAN_EID_TIM))
  772. tim_idx += beacon[tim_idx+1] + 2;
  773. /* If TIM field was found, set variables */
  774. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  775. *tim_index = cpu_to_le32(tim_idx);
  776. *tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
  777. } else {
  778. IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
  779. }
  780. }
  781. static u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
  782. {
  783. struct ieee80211_mgmt *mgmt = (void *)beacon;
  784. const u8 *ie;
  785. if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
  786. return 0;
  787. frame_size -= mgmt->u.beacon.variable - beacon;
  788. ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
  789. if (!ie)
  790. return 0;
  791. return ie - beacon;
  792. }
  793. static u8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
  794. struct ieee80211_vif *vif)
  795. {
  796. u8 rate;
  797. if (info->band == NL80211_BAND_5GHZ || vif->p2p)
  798. rate = IWL_FIRST_OFDM_RATE;
  799. else
  800. rate = IWL_FIRST_CCK_RATE;
  801. return rate;
  802. }
  803. static void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
  804. struct ieee80211_vif *vif,
  805. struct sk_buff *beacon,
  806. struct iwl_tx_cmd *tx)
  807. {
  808. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  809. struct ieee80211_tx_info *info;
  810. u8 rate;
  811. u32 tx_flags;
  812. info = IEEE80211_SKB_CB(beacon);
  813. /* Set up TX command fields */
  814. tx->len = cpu_to_le16((u16)beacon->len);
  815. tx->sta_id = mvmvif->bcast_sta.sta_id;
  816. tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  817. tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
  818. tx_flags |=
  819. iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
  820. TX_CMD_FLG_BT_PRIO_POS;
  821. tx->tx_flags = cpu_to_le32(tx_flags);
  822. if (!fw_has_capa(&mvm->fw->ucode_capa,
  823. IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION)) {
  824. mvm->mgmt_last_antenna_idx =
  825. iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
  826. mvm->mgmt_last_antenna_idx);
  827. }
  828. tx->rate_n_flags =
  829. cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
  830. RATE_MCS_ANT_POS);
  831. rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
  832. tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
  833. if (rate == IWL_FIRST_CCK_RATE)
  834. tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
  835. }
  836. static int iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
  837. struct sk_buff *beacon,
  838. void *data, int len)
  839. {
  840. struct iwl_host_cmd cmd = {
  841. .id = BEACON_TEMPLATE_CMD,
  842. .flags = CMD_ASYNC,
  843. };
  844. cmd.len[0] = len;
  845. cmd.data[0] = data;
  846. cmd.dataflags[0] = 0;
  847. cmd.len[1] = beacon->len;
  848. cmd.data[1] = beacon->data;
  849. cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
  850. return iwl_mvm_send_cmd(mvm, &cmd);
  851. }
  852. static int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
  853. struct ieee80211_vif *vif,
  854. struct sk_buff *beacon)
  855. {
  856. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  857. struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
  858. iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
  859. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  860. if (vif->type == NL80211_IFTYPE_AP)
  861. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
  862. &beacon_cmd.tim_size,
  863. beacon->data, beacon->len);
  864. return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
  865. sizeof(beacon_cmd));
  866. }
  867. static int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
  868. struct ieee80211_vif *vif,
  869. struct sk_buff *beacon)
  870. {
  871. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  872. struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
  873. iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
  874. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  875. if (vif->type == NL80211_IFTYPE_AP)
  876. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
  877. &beacon_cmd.tim_size,
  878. beacon->data, beacon->len);
  879. beacon_cmd.csa_offset =
  880. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  881. WLAN_EID_CHANNEL_SWITCH,
  882. beacon->len));
  883. beacon_cmd.ecsa_offset =
  884. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  885. WLAN_EID_EXT_CHANSWITCH_ANN,
  886. beacon->len));
  887. return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
  888. sizeof(beacon_cmd));
  889. }
  890. static int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
  891. struct ieee80211_vif *vif,
  892. struct sk_buff *beacon)
  893. {
  894. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  895. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
  896. struct iwl_mac_beacon_cmd beacon_cmd = {};
  897. u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
  898. u16 flags;
  899. flags = iwl_mvm_mac80211_idx_to_hwrate(rate);
  900. if (rate == IWL_FIRST_CCK_RATE)
  901. flags |= IWL_MAC_BEACON_CCK;
  902. beacon_cmd.flags = cpu_to_le16(flags);
  903. beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
  904. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  905. if (vif->type == NL80211_IFTYPE_AP)
  906. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
  907. &beacon_cmd.tim_size,
  908. beacon->data, beacon->len);
  909. beacon_cmd.csa_offset =
  910. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  911. WLAN_EID_CHANNEL_SWITCH,
  912. beacon->len));
  913. beacon_cmd.ecsa_offset =
  914. cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
  915. WLAN_EID_EXT_CHANSWITCH_ANN,
  916. beacon->len));
  917. return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
  918. sizeof(beacon_cmd));
  919. }
  920. static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
  921. struct ieee80211_vif *vif,
  922. struct sk_buff *beacon)
  923. {
  924. if (WARN_ON(!beacon))
  925. return -EINVAL;
  926. if (!fw_has_capa(&mvm->fw->ucode_capa,
  927. IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
  928. return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
  929. if (fw_has_api(&mvm->fw->ucode_capa,
  930. IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
  931. return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
  932. return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
  933. }
  934. /* The beacon template for the AP/GO/IBSS has changed and needs update */
  935. int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
  936. struct ieee80211_vif *vif)
  937. {
  938. struct sk_buff *beacon;
  939. int ret;
  940. WARN_ON(vif->type != NL80211_IFTYPE_AP &&
  941. vif->type != NL80211_IFTYPE_ADHOC);
  942. beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
  943. if (!beacon)
  944. return -ENOMEM;
  945. ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
  946. dev_kfree_skb(beacon);
  947. return ret;
  948. }
  949. struct iwl_mvm_mac_ap_iterator_data {
  950. struct iwl_mvm *mvm;
  951. struct ieee80211_vif *vif;
  952. u32 beacon_device_ts;
  953. u16 beacon_int;
  954. };
  955. /* Find the beacon_device_ts and beacon_int for a managed interface */
  956. static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
  957. struct ieee80211_vif *vif)
  958. {
  959. struct iwl_mvm_mac_ap_iterator_data *data = _data;
  960. if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
  961. return;
  962. /* Station client has higher priority over P2P client*/
  963. if (vif->p2p && data->beacon_device_ts)
  964. return;
  965. data->beacon_device_ts = vif->bss_conf.sync_device_ts;
  966. data->beacon_int = vif->bss_conf.beacon_int;
  967. }
  968. /*
  969. * Fill the specific data for mac context of type AP of P2P GO
  970. */
  971. static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
  972. struct ieee80211_vif *vif,
  973. struct iwl_mac_ctx_cmd *cmd,
  974. struct iwl_mac_data_ap *ctxt_ap,
  975. bool add)
  976. {
  977. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  978. struct iwl_mvm_mac_ap_iterator_data data = {
  979. .mvm = mvm,
  980. .vif = vif,
  981. .beacon_device_ts = 0
  982. };
  983. /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
  984. cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
  985. /*
  986. * in AP mode, pass probe requests and beacons from other APs
  987. * (needed for ht protection); when there're no any associated
  988. * station don't ask FW to pass beacons to prevent unnecessary
  989. * wake-ups.
  990. */
  991. cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  992. if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
  993. cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
  994. IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
  995. } else {
  996. IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
  997. }
  998. ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  999. ctxt_ap->bi_reciprocal =
  1000. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  1001. ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  1002. vif->bss_conf.dtim_period);
  1003. ctxt_ap->dtim_reciprocal =
  1004. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  1005. vif->bss_conf.dtim_period));
  1006. if (!fw_has_api(&mvm->fw->ucode_capa,
  1007. IWL_UCODE_TLV_API_STA_TYPE))
  1008. ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
  1009. /*
  1010. * Only set the beacon time when the MAC is being added, when we
  1011. * just modify the MAC then we should keep the time -- the firmware
  1012. * can otherwise have a "jumping" TBTT.
  1013. */
  1014. if (add) {
  1015. /*
  1016. * If there is a station/P2P client interface which is
  1017. * associated, set the AP's TBTT far enough from the station's
  1018. * TBTT. Otherwise, set it to the current system time
  1019. */
  1020. ieee80211_iterate_active_interfaces_atomic(
  1021. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  1022. iwl_mvm_mac_ap_iterator, &data);
  1023. if (data.beacon_device_ts) {
  1024. u32 rand = (prandom_u32() % (64 - 36)) + 36;
  1025. mvmvif->ap_beacon_time = data.beacon_device_ts +
  1026. ieee80211_tu_to_usec(data.beacon_int * rand /
  1027. 100);
  1028. } else {
  1029. mvmvif->ap_beacon_time =
  1030. iwl_read_prph(mvm->trans,
  1031. DEVICE_SYSTEM_TIME_REG);
  1032. }
  1033. }
  1034. ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
  1035. ctxt_ap->beacon_tsf = 0; /* unused */
  1036. /* TODO: Assume that the beacon id == mac context id */
  1037. ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
  1038. }
  1039. static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
  1040. struct ieee80211_vif *vif,
  1041. u32 action)
  1042. {
  1043. struct iwl_mac_ctx_cmd cmd = {};
  1044. WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
  1045. /* Fill the common data for all mac context types */
  1046. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  1047. /* Fill the data specific for ap mode */
  1048. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
  1049. action == FW_CTXT_ACTION_ADD);
  1050. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  1051. }
  1052. static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
  1053. struct ieee80211_vif *vif,
  1054. u32 action)
  1055. {
  1056. struct iwl_mac_ctx_cmd cmd = {};
  1057. struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
  1058. WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
  1059. /* Fill the common data for all mac context types */
  1060. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  1061. /* Fill the data specific for GO mode */
  1062. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
  1063. action == FW_CTXT_ACTION_ADD);
  1064. cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  1065. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  1066. cmd.go.opp_ps_enabled =
  1067. cpu_to_le32(!!(noa->oppps_ctwindow &
  1068. IEEE80211_P2P_OPPPS_ENABLE_BIT));
  1069. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  1070. }
  1071. static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1072. u32 action, bool force_assoc_off,
  1073. const u8 *bssid_override)
  1074. {
  1075. switch (vif->type) {
  1076. case NL80211_IFTYPE_STATION:
  1077. return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
  1078. force_assoc_off,
  1079. bssid_override);
  1080. break;
  1081. case NL80211_IFTYPE_AP:
  1082. if (!vif->p2p)
  1083. return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
  1084. else
  1085. return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
  1086. break;
  1087. case NL80211_IFTYPE_MONITOR:
  1088. return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
  1089. case NL80211_IFTYPE_P2P_DEVICE:
  1090. return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
  1091. case NL80211_IFTYPE_ADHOC:
  1092. return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
  1093. default:
  1094. break;
  1095. }
  1096. return -EOPNOTSUPP;
  1097. }
  1098. int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1099. {
  1100. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1101. int ret;
  1102. if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
  1103. vif->addr, ieee80211_vif_type_p2p(vif)))
  1104. return -EIO;
  1105. ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
  1106. true, NULL);
  1107. if (ret)
  1108. return ret;
  1109. /* will only do anything at resume from D3 time */
  1110. iwl_mvm_set_last_nonqos_seq(mvm, vif);
  1111. mvmvif->uploaded = true;
  1112. return 0;
  1113. }
  1114. int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1115. bool force_assoc_off, const u8 *bssid_override)
  1116. {
  1117. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1118. if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
  1119. vif->addr, ieee80211_vif_type_p2p(vif)))
  1120. return -EIO;
  1121. return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
  1122. force_assoc_off, bssid_override);
  1123. }
  1124. int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1125. {
  1126. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1127. struct iwl_mac_ctx_cmd cmd;
  1128. int ret;
  1129. if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
  1130. vif->addr, ieee80211_vif_type_p2p(vif)))
  1131. return -EIO;
  1132. memset(&cmd, 0, sizeof(cmd));
  1133. cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  1134. mvmvif->color));
  1135. cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  1136. ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
  1137. sizeof(cmd), &cmd);
  1138. if (ret) {
  1139. IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
  1140. return ret;
  1141. }
  1142. mvmvif->uploaded = false;
  1143. if (vif->type == NL80211_IFTYPE_MONITOR) {
  1144. __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
  1145. iwl_mvm_dealloc_snif_sta(mvm);
  1146. }
  1147. return 0;
  1148. }
  1149. static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
  1150. struct ieee80211_vif *csa_vif, u32 gp2,
  1151. bool tx_success)
  1152. {
  1153. struct iwl_mvm_vif *mvmvif =
  1154. iwl_mvm_vif_from_mac80211(csa_vif);
  1155. /* Don't start to countdown from a failed beacon */
  1156. if (!tx_success && !mvmvif->csa_countdown)
  1157. return;
  1158. mvmvif->csa_countdown = true;
  1159. if (!ieee80211_csa_is_complete(csa_vif)) {
  1160. int c = ieee80211_csa_update_counter(csa_vif);
  1161. iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
  1162. if (csa_vif->p2p &&
  1163. !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
  1164. tx_success) {
  1165. u32 rel_time = (c + 1) *
  1166. csa_vif->bss_conf.beacon_int -
  1167. IWL_MVM_CHANNEL_SWITCH_TIME_GO;
  1168. u32 apply_time = gp2 + rel_time * 1024;
  1169. iwl_mvm_schedule_csa_period(mvm, csa_vif,
  1170. IWL_MVM_CHANNEL_SWITCH_TIME_GO -
  1171. IWL_MVM_CHANNEL_SWITCH_MARGIN,
  1172. apply_time);
  1173. }
  1174. } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
  1175. /* we don't have CSA NoA scheduled yet, switch now */
  1176. ieee80211_csa_finish(csa_vif);
  1177. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  1178. }
  1179. }
  1180. void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
  1181. struct iwl_rx_cmd_buffer *rxb)
  1182. {
  1183. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1184. struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
  1185. struct iwl_mvm_tx_resp *beacon_notify_hdr;
  1186. struct ieee80211_vif *csa_vif;
  1187. struct ieee80211_vif *tx_blocked_vif;
  1188. struct agg_tx_status *agg_status;
  1189. u16 status;
  1190. lockdep_assert_held(&mvm->mutex);
  1191. beacon_notify_hdr = &beacon->beacon_notify_hdr;
  1192. mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
  1193. mvm->ibss_manager = beacon->ibss_mgr_status != 0;
  1194. agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
  1195. status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
  1196. IWL_DEBUG_RX(mvm,
  1197. "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
  1198. status, beacon_notify_hdr->failure_frame,
  1199. le64_to_cpu(beacon->tsf),
  1200. mvm->ap_last_beacon_gp2,
  1201. le32_to_cpu(beacon_notify_hdr->initial_rate));
  1202. csa_vif = rcu_dereference_protected(mvm->csa_vif,
  1203. lockdep_is_held(&mvm->mutex));
  1204. if (unlikely(csa_vif && csa_vif->csa_active))
  1205. iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
  1206. (status == TX_STATUS_SUCCESS));
  1207. tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
  1208. lockdep_is_held(&mvm->mutex));
  1209. if (unlikely(tx_blocked_vif)) {
  1210. struct iwl_mvm_vif *mvmvif =
  1211. iwl_mvm_vif_from_mac80211(tx_blocked_vif);
  1212. /*
  1213. * The channel switch is started and we have blocked the
  1214. * stations. If this is the first beacon (the timeout wasn't
  1215. * set), set the unblock timeout, otherwise countdown
  1216. */
  1217. if (!mvm->csa_tx_block_bcn_timeout)
  1218. mvm->csa_tx_block_bcn_timeout =
  1219. IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
  1220. else
  1221. mvm->csa_tx_block_bcn_timeout--;
  1222. /* Check if the timeout is expired, and unblock tx */
  1223. if (mvm->csa_tx_block_bcn_timeout == 0) {
  1224. iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
  1225. RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
  1226. }
  1227. }
  1228. }
  1229. static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
  1230. struct ieee80211_vif *vif)
  1231. {
  1232. struct iwl_missed_beacons_notif *missed_beacons = _data;
  1233. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1234. struct iwl_mvm *mvm = mvmvif->mvm;
  1235. struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
  1236. struct iwl_fw_dbg_trigger_tlv *trigger;
  1237. u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
  1238. u32 rx_missed_bcon, rx_missed_bcon_since_rx;
  1239. if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
  1240. return;
  1241. rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
  1242. rx_missed_bcon_since_rx =
  1243. le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
  1244. /*
  1245. * TODO: the threshold should be adjusted based on latency conditions,
  1246. * and/or in case of a CS flow on one of the other AP vifs.
  1247. */
  1248. if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
  1249. IWL_MVM_MISSED_BEACONS_THRESHOLD)
  1250. ieee80211_beacon_loss(vif);
  1251. if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
  1252. FW_DBG_TRIGGER_MISSED_BEACONS))
  1253. return;
  1254. trigger = iwl_fw_dbg_get_trigger(mvm->fw,
  1255. FW_DBG_TRIGGER_MISSED_BEACONS);
  1256. bcon_trig = (void *)trigger->data;
  1257. stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
  1258. stop_trig_missed_bcon_since_rx =
  1259. le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
  1260. /* TODO: implement start trigger */
  1261. if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
  1262. ieee80211_vif_to_wdev(vif),
  1263. trigger))
  1264. return;
  1265. if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
  1266. rx_missed_bcon >= stop_trig_missed_bcon)
  1267. iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
  1268. }
  1269. void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
  1270. struct iwl_rx_cmd_buffer *rxb)
  1271. {
  1272. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1273. struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
  1274. IWL_DEBUG_INFO(mvm,
  1275. "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
  1276. le32_to_cpu(mb->mac_id),
  1277. le32_to_cpu(mb->consec_missed_beacons),
  1278. le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
  1279. le32_to_cpu(mb->num_recvd_beacons),
  1280. le32_to_cpu(mb->num_expected_beacons));
  1281. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1282. IEEE80211_IFACE_ITER_NORMAL,
  1283. iwl_mvm_beacon_loss_iterator,
  1284. mb);
  1285. }
  1286. void iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
  1287. struct iwl_rx_cmd_buffer *rxb)
  1288. {
  1289. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1290. struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
  1291. struct ieee80211_rx_status rx_status;
  1292. struct sk_buff *skb;
  1293. u32 size = le32_to_cpu(sb->byte_count);
  1294. if (size == 0)
  1295. return;
  1296. skb = alloc_skb(size, GFP_ATOMIC);
  1297. if (!skb) {
  1298. IWL_ERR(mvm, "alloc_skb failed\n");
  1299. return;
  1300. }
  1301. /* update rx_status according to the notification's metadata */
  1302. memset(&rx_status, 0, sizeof(rx_status));
  1303. rx_status.mactime = le64_to_cpu(sb->tsf);
  1304. /* TSF as indicated by the firmware is at INA time */
  1305. rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
  1306. rx_status.device_timestamp = le32_to_cpu(sb->system_time);
  1307. rx_status.band =
  1308. (sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
  1309. NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
  1310. rx_status.freq =
  1311. ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
  1312. rx_status.band);
  1313. /* copy the data */
  1314. skb_put_data(skb, sb->data, size);
  1315. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1316. /* pass it as regular rx to mac80211 */
  1317. ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
  1318. }
  1319. void iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
  1320. struct iwl_rx_cmd_buffer *rxb)
  1321. {
  1322. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1323. struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
  1324. struct ieee80211_vif *csa_vif;
  1325. struct iwl_mvm_vif *mvmvif;
  1326. int len = iwl_rx_packet_payload_len(pkt);
  1327. u32 id_n_color;
  1328. if (WARN_ON_ONCE(len < sizeof(*notif)))
  1329. return;
  1330. rcu_read_lock();
  1331. csa_vif = rcu_dereference(mvm->csa_vif);
  1332. if (WARN_ON(!csa_vif || !csa_vif->csa_active))
  1333. goto out_unlock;
  1334. id_n_color = le32_to_cpu(notif->id_and_color);
  1335. mvmvif = iwl_mvm_vif_from_mac80211(csa_vif);
  1336. if (WARN(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color) != id_n_color,
  1337. "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
  1338. FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color), id_n_color))
  1339. goto out_unlock;
  1340. IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
  1341. schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
  1342. msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
  1343. csa_vif->bss_conf.beacon_int));
  1344. ieee80211_csa_finish(csa_vif);
  1345. rcu_read_unlock();
  1346. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  1347. return;
  1348. out_unlock:
  1349. rcu_read_unlock();
  1350. }