mac-ctxt.c 50 KB

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