mac-ctxt.c 44 KB

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