mac-ctxt.c 44 KB

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