mac-ctxt.c 41 KB

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