mac-ctxt.c 49 KB

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