channel.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /*
  2. * Copyright (c) 2014 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "ath9k.h"
  17. /* Set/change channels. If the channel is really being changed, it's done
  18. * by reseting the chip. To accomplish this we must first cleanup any pending
  19. * DMA, then restart stuff.
  20. */
  21. static int ath_set_channel(struct ath_softc *sc)
  22. {
  23. struct ath_hw *ah = sc->sc_ah;
  24. struct ath_common *common = ath9k_hw_common(ah);
  25. struct ieee80211_hw *hw = sc->hw;
  26. struct ath9k_channel *hchan;
  27. struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
  28. struct ieee80211_channel *chan = chandef->chan;
  29. int pos = chan->hw_value;
  30. unsigned long flags;
  31. int old_pos = -1;
  32. int r;
  33. if (test_bit(ATH_OP_INVALID, &common->op_flags))
  34. return -EIO;
  35. if (ah->curchan)
  36. old_pos = ah->curchan - &ah->channels[0];
  37. ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
  38. chan->center_freq, chandef->width);
  39. /* update survey stats for the old channel before switching */
  40. spin_lock_irqsave(&common->cc_lock, flags);
  41. ath_update_survey_stats(sc);
  42. spin_unlock_irqrestore(&common->cc_lock, flags);
  43. ath9k_cmn_get_channel(hw, ah, chandef);
  44. /* If the operating channel changes, change the survey in-use flags
  45. * along with it.
  46. * Reset the survey data for the new channel, unless we're switching
  47. * back to the operating channel from an off-channel operation.
  48. */
  49. if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
  50. if (sc->cur_survey)
  51. sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
  52. sc->cur_survey = &sc->survey[pos];
  53. memset(sc->cur_survey, 0, sizeof(struct survey_info));
  54. sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
  55. } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
  56. memset(&sc->survey[pos], 0, sizeof(struct survey_info));
  57. }
  58. hchan = &sc->sc_ah->channels[pos];
  59. r = ath_reset(sc, hchan);
  60. if (r)
  61. return r;
  62. /* The most recent snapshot of channel->noisefloor for the old
  63. * channel is only available after the hardware reset. Copy it to
  64. * the survey stats now.
  65. */
  66. if (old_pos >= 0)
  67. ath_update_survey_nf(sc, old_pos);
  68. /* Enable radar pulse detection if on a DFS channel. Spectral
  69. * scanning and radar detection can not be used concurrently.
  70. */
  71. if (hw->conf.radar_enabled) {
  72. u32 rxfilter;
  73. rxfilter = ath9k_hw_getrxfilter(ah);
  74. rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
  75. ATH9K_RX_FILTER_PHYERR;
  76. ath9k_hw_setrxfilter(ah, rxfilter);
  77. ath_dbg(common, DFS, "DFS enabled at freq %d\n",
  78. chan->center_freq);
  79. } else {
  80. /* perform spectral scan if requested. */
  81. if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
  82. sc->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
  83. ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
  84. }
  85. return 0;
  86. }
  87. void ath_chanctx_init(struct ath_softc *sc)
  88. {
  89. struct ath_chanctx *ctx;
  90. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  91. struct ieee80211_supported_band *sband;
  92. struct ieee80211_channel *chan;
  93. int i, j;
  94. sband = &common->sbands[NL80211_BAND_2GHZ];
  95. if (!sband->n_channels)
  96. sband = &common->sbands[NL80211_BAND_5GHZ];
  97. chan = &sband->channels[0];
  98. for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
  99. ctx = &sc->chanctx[i];
  100. cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
  101. INIT_LIST_HEAD(&ctx->vifs);
  102. ctx->txpower = ATH_TXPOWER_MAX;
  103. ctx->flush_timeout = HZ / 5; /* 200ms */
  104. for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) {
  105. INIT_LIST_HEAD(&ctx->acq[j].acq_new);
  106. INIT_LIST_HEAD(&ctx->acq[j].acq_old);
  107. spin_lock_init(&ctx->acq[j].lock);
  108. }
  109. }
  110. }
  111. void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
  112. struct cfg80211_chan_def *chandef)
  113. {
  114. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  115. bool cur_chan;
  116. spin_lock_bh(&sc->chan_lock);
  117. if (chandef)
  118. memcpy(&ctx->chandef, chandef, sizeof(*chandef));
  119. cur_chan = sc->cur_chan == ctx;
  120. spin_unlock_bh(&sc->chan_lock);
  121. if (!cur_chan) {
  122. ath_dbg(common, CHAN_CTX,
  123. "Current context differs from the new context\n");
  124. return;
  125. }
  126. ath_set_channel(sc);
  127. }
  128. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  129. /*************/
  130. /* Utilities */
  131. /*************/
  132. struct ath_chanctx* ath_is_go_chanctx_present(struct ath_softc *sc)
  133. {
  134. struct ath_chanctx *ctx;
  135. struct ath_vif *avp;
  136. struct ieee80211_vif *vif;
  137. spin_lock_bh(&sc->chan_lock);
  138. ath_for_each_chanctx(sc, ctx) {
  139. if (!ctx->active)
  140. continue;
  141. list_for_each_entry(avp, &ctx->vifs, list) {
  142. vif = avp->vif;
  143. if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO) {
  144. spin_unlock_bh(&sc->chan_lock);
  145. return ctx;
  146. }
  147. }
  148. }
  149. spin_unlock_bh(&sc->chan_lock);
  150. return NULL;
  151. }
  152. /**********************************************************/
  153. /* Functions to handle the channel context state machine. */
  154. /**********************************************************/
  155. static const char *offchannel_state_string(enum ath_offchannel_state state)
  156. {
  157. switch (state) {
  158. case_rtn_string(ATH_OFFCHANNEL_IDLE);
  159. case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
  160. case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
  161. case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
  162. case_rtn_string(ATH_OFFCHANNEL_ROC_START);
  163. case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
  164. case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
  165. default:
  166. return "unknown";
  167. }
  168. }
  169. static const char *chanctx_event_string(enum ath_chanctx_event ev)
  170. {
  171. switch (ev) {
  172. case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
  173. case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
  174. case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
  175. case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
  176. case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
  177. case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
  178. case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
  179. case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
  180. case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
  181. case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
  182. default:
  183. return "unknown";
  184. }
  185. }
  186. static const char *chanctx_state_string(enum ath_chanctx_state state)
  187. {
  188. switch (state) {
  189. case_rtn_string(ATH_CHANCTX_STATE_IDLE);
  190. case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
  191. case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
  192. case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
  193. case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
  194. default:
  195. return "unknown";
  196. }
  197. }
  198. static u32 chanctx_event_delta(struct ath_softc *sc)
  199. {
  200. u64 ms;
  201. struct timespec64 ts, *old;
  202. ktime_get_raw_ts64(&ts);
  203. old = &sc->last_event_time;
  204. ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  205. ms -= old->tv_sec * 1000 + old->tv_nsec / 1000000;
  206. sc->last_event_time = ts;
  207. return (u32)ms;
  208. }
  209. void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
  210. {
  211. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  212. struct ath_chanctx *ictx;
  213. struct ath_vif *avp;
  214. bool active = false;
  215. u8 n_active = 0;
  216. if (!ctx)
  217. return;
  218. if (ctx == &sc->offchannel.chan) {
  219. spin_lock_bh(&sc->chan_lock);
  220. if (likely(sc->sched.channel_switch_time))
  221. ctx->flush_timeout =
  222. usecs_to_jiffies(sc->sched.channel_switch_time);
  223. else
  224. ctx->flush_timeout =
  225. msecs_to_jiffies(10);
  226. spin_unlock_bh(&sc->chan_lock);
  227. /*
  228. * There is no need to iterate over the
  229. * active/assigned channel contexts if
  230. * the current context is offchannel.
  231. */
  232. return;
  233. }
  234. ictx = ctx;
  235. list_for_each_entry(avp, &ctx->vifs, list) {
  236. struct ieee80211_vif *vif = avp->vif;
  237. switch (vif->type) {
  238. case NL80211_IFTYPE_P2P_CLIENT:
  239. case NL80211_IFTYPE_STATION:
  240. if (avp->assoc)
  241. active = true;
  242. break;
  243. default:
  244. active = true;
  245. break;
  246. }
  247. }
  248. ctx->active = active;
  249. ath_for_each_chanctx(sc, ctx) {
  250. if (!ctx->assigned || list_empty(&ctx->vifs))
  251. continue;
  252. n_active++;
  253. }
  254. spin_lock_bh(&sc->chan_lock);
  255. if (n_active <= 1) {
  256. ictx->flush_timeout = HZ / 5;
  257. clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
  258. spin_unlock_bh(&sc->chan_lock);
  259. return;
  260. }
  261. ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
  262. if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
  263. spin_unlock_bh(&sc->chan_lock);
  264. return;
  265. }
  266. spin_unlock_bh(&sc->chan_lock);
  267. if (ath9k_is_chanctx_enabled()) {
  268. ath_chanctx_event(sc, NULL,
  269. ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
  270. }
  271. }
  272. static struct ath_chanctx *
  273. ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
  274. {
  275. int idx = ctx - &sc->chanctx[0];
  276. return &sc->chanctx[!idx];
  277. }
  278. static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
  279. {
  280. struct ath_chanctx *prev, *cur;
  281. struct timespec64 ts;
  282. u32 cur_tsf, prev_tsf, beacon_int;
  283. s32 offset;
  284. beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
  285. cur = sc->cur_chan;
  286. prev = ath_chanctx_get_next(sc, cur);
  287. if (!prev->switch_after_beacon)
  288. return;
  289. ktime_get_raw_ts64(&ts);
  290. cur_tsf = (u32) cur->tsf_val +
  291. ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
  292. prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
  293. prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
  294. /* Adjust the TSF time of the AP chanctx to keep its beacons
  295. * at half beacon interval offset relative to the STA chanctx.
  296. */
  297. offset = cur_tsf - prev_tsf;
  298. /* Ignore stale data or spurious timestamps */
  299. if (offset < 0 || offset > 3 * beacon_int)
  300. return;
  301. offset = beacon_int / 2 - (offset % beacon_int);
  302. prev->tsf_val += offset;
  303. }
  304. /* Configure the TSF based hardware timer for a channel switch.
  305. * Also set up backup software timer, in case the gen timer fails.
  306. * This could be caused by a hardware reset.
  307. */
  308. static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
  309. {
  310. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  311. struct ath_hw *ah = sc->sc_ah;
  312. unsigned long timeout;
  313. ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
  314. tsf_time -= ath9k_hw_gettsf32(ah);
  315. timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
  316. mod_timer(&sc->sched.timer, jiffies + timeout);
  317. ath_dbg(common, CHAN_CTX,
  318. "Setup chanctx timer with timeout: %d (%d) ms\n",
  319. tsf_time / 1000, jiffies_to_msecs(timeout));
  320. }
  321. static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
  322. struct ath_chanctx *ctx,
  323. struct ath_vif *avp)
  324. {
  325. /*
  326. * Clear the extend_absence flag if it had been
  327. * set during the previous beacon transmission,
  328. * since we need to revert to the normal NoA
  329. * schedule.
  330. */
  331. if (ctx->active && sc->sched.extend_absence) {
  332. avp->noa_duration = 0;
  333. sc->sched.extend_absence = false;
  334. }
  335. /* If at least two consecutive beacons were missed on the STA
  336. * chanctx, stay on the STA channel for one extra beacon period,
  337. * to resync the timer properly.
  338. */
  339. if (ctx->active && sc->sched.beacon_miss >= 2) {
  340. avp->noa_duration = 0;
  341. sc->sched.extend_absence = true;
  342. }
  343. }
  344. static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
  345. struct ath_chanctx *ctx,
  346. struct ath_vif *avp,
  347. u32 tsf_time)
  348. {
  349. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  350. avp->noa_index++;
  351. avp->offchannel_start = tsf_time;
  352. avp->offchannel_duration = sc->sched.offchannel_duration;
  353. ath_dbg(common, CHAN_CTX,
  354. "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
  355. avp->offchannel_duration,
  356. avp->offchannel_start,
  357. avp->noa_index);
  358. /*
  359. * When multiple contexts are active, the NoA
  360. * has to be recalculated and advertised after
  361. * an offchannel operation.
  362. */
  363. if (ctx->active && avp->noa_duration)
  364. avp->noa_duration = 0;
  365. }
  366. static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
  367. struct ath_vif *avp,
  368. struct ath_beacon_config *cur_conf,
  369. u32 tsf_time,
  370. u32 beacon_int)
  371. {
  372. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  373. avp->noa_index++;
  374. avp->noa_start = tsf_time;
  375. if (sc->sched.extend_absence)
  376. avp->noa_duration = (3 * beacon_int / 2) +
  377. sc->sched.channel_switch_time;
  378. else
  379. avp->noa_duration =
  380. TU_TO_USEC(cur_conf->beacon_interval) / 2 +
  381. sc->sched.channel_switch_time;
  382. if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
  383. sc->sched.extend_absence)
  384. avp->periodic_noa = false;
  385. else
  386. avp->periodic_noa = true;
  387. ath_dbg(common, CHAN_CTX,
  388. "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
  389. avp->noa_duration,
  390. avp->noa_start,
  391. avp->noa_index,
  392. avp->periodic_noa);
  393. }
  394. static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
  395. struct ath_vif *avp,
  396. u32 tsf_time,
  397. u32 duration)
  398. {
  399. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  400. avp->noa_index++;
  401. avp->noa_start = tsf_time;
  402. avp->periodic_noa = false;
  403. avp->oneshot_noa = true;
  404. avp->noa_duration = duration + sc->sched.channel_switch_time;
  405. ath_dbg(common, CHAN_CTX,
  406. "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
  407. avp->noa_duration,
  408. avp->noa_start,
  409. avp->noa_index,
  410. avp->periodic_noa);
  411. }
  412. void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
  413. enum ath_chanctx_event ev)
  414. {
  415. struct ath_hw *ah = sc->sc_ah;
  416. struct ath_common *common = ath9k_hw_common(ah);
  417. struct ath_beacon_config *cur_conf;
  418. struct ath_vif *avp = NULL;
  419. struct ath_chanctx *ctx;
  420. u32 tsf_time;
  421. u32 beacon_int;
  422. if (vif)
  423. avp = (struct ath_vif *) vif->drv_priv;
  424. spin_lock_bh(&sc->chan_lock);
  425. ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
  426. sc->cur_chan->chandef.center_freq1,
  427. chanctx_event_string(ev),
  428. chanctx_state_string(sc->sched.state),
  429. chanctx_event_delta(sc));
  430. switch (ev) {
  431. case ATH_CHANCTX_EVENT_BEACON_PREPARE:
  432. if (avp->offchannel_duration)
  433. avp->offchannel_duration = 0;
  434. if (avp->oneshot_noa) {
  435. avp->noa_duration = 0;
  436. avp->oneshot_noa = false;
  437. ath_dbg(common, CHAN_CTX,
  438. "Clearing oneshot NoA\n");
  439. }
  440. if (avp->chanctx != sc->cur_chan) {
  441. ath_dbg(common, CHAN_CTX,
  442. "Contexts differ, not preparing beacon\n");
  443. break;
  444. }
  445. if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
  446. sc->sched.offchannel_pending = false;
  447. sc->next_chan = &sc->offchannel.chan;
  448. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
  449. ath_dbg(common, CHAN_CTX,
  450. "Setting offchannel_pending to false\n");
  451. }
  452. ctx = ath_chanctx_get_next(sc, sc->cur_chan);
  453. if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
  454. sc->next_chan = ctx;
  455. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
  456. ath_dbg(common, CHAN_CTX,
  457. "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
  458. }
  459. /* if the timer missed its window, use the next interval */
  460. if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
  461. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
  462. ath_dbg(common, CHAN_CTX,
  463. "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
  464. }
  465. if (sc->sched.mgd_prepare_tx)
  466. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
  467. /*
  468. * When a context becomes inactive, for example,
  469. * disassociation of a station context, the NoA
  470. * attribute needs to be removed from subsequent
  471. * beacons.
  472. */
  473. if (!ctx->active && avp->noa_duration &&
  474. sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
  475. avp->noa_duration = 0;
  476. avp->periodic_noa = false;
  477. ath_dbg(common, CHAN_CTX,
  478. "Clearing NoA schedule\n");
  479. }
  480. if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
  481. break;
  482. ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
  483. sc->sched.beacon_pending = true;
  484. sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
  485. cur_conf = &sc->cur_chan->beacon;
  486. beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
  487. /* defer channel switch by a quarter beacon interval */
  488. tsf_time = sc->sched.next_tbtt + beacon_int / 4;
  489. sc->sched.switch_start_time = tsf_time;
  490. sc->cur_chan->last_beacon = sc->sched.next_tbtt;
  491. /*
  492. * If an offchannel switch is scheduled to happen after
  493. * a beacon transmission, update the NoA with one-shot
  494. * values and increment the index.
  495. */
  496. if (sc->next_chan == &sc->offchannel.chan) {
  497. ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
  498. break;
  499. }
  500. ath_chanctx_handle_bmiss(sc, ctx, avp);
  501. /*
  502. * If a mgd_prepare_tx() has been called by mac80211,
  503. * a one-shot NoA needs to be sent. This can happen
  504. * with one or more active channel contexts - in both
  505. * cases, a new NoA schedule has to be advertised.
  506. */
  507. if (sc->sched.mgd_prepare_tx) {
  508. ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
  509. jiffies_to_usecs(HZ / 5));
  510. break;
  511. }
  512. /* Prevent wrap-around issues */
  513. if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
  514. avp->noa_duration = 0;
  515. /*
  516. * If multiple contexts are active, start periodic
  517. * NoA and increment the index for the first
  518. * announcement.
  519. */
  520. if (ctx->active &&
  521. (!avp->noa_duration || sc->sched.force_noa_update))
  522. ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
  523. tsf_time, beacon_int);
  524. if (ctx->active && sc->sched.force_noa_update)
  525. sc->sched.force_noa_update = false;
  526. break;
  527. case ATH_CHANCTX_EVENT_BEACON_SENT:
  528. if (!sc->sched.beacon_pending) {
  529. ath_dbg(common, CHAN_CTX,
  530. "No pending beacon\n");
  531. break;
  532. }
  533. sc->sched.beacon_pending = false;
  534. if (sc->sched.mgd_prepare_tx) {
  535. sc->sched.mgd_prepare_tx = false;
  536. complete(&sc->go_beacon);
  537. ath_dbg(common, CHAN_CTX,
  538. "Beacon sent, complete go_beacon\n");
  539. break;
  540. }
  541. if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
  542. break;
  543. ath_dbg(common, CHAN_CTX,
  544. "Move chanctx state to WAIT_FOR_TIMER\n");
  545. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
  546. ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
  547. break;
  548. case ATH_CHANCTX_EVENT_TSF_TIMER:
  549. if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
  550. break;
  551. if (!sc->cur_chan->switch_after_beacon &&
  552. sc->sched.beacon_pending)
  553. sc->sched.beacon_miss++;
  554. ath_dbg(common, CHAN_CTX,
  555. "Move chanctx state to SWITCH\n");
  556. sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
  557. ieee80211_queue_work(sc->hw, &sc->chanctx_work);
  558. break;
  559. case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
  560. if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
  561. sc->cur_chan == &sc->offchannel.chan)
  562. break;
  563. sc->sched.beacon_pending = false;
  564. sc->sched.beacon_miss = 0;
  565. if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
  566. !sc->sched.beacon_adjust ||
  567. !sc->cur_chan->tsf_val)
  568. break;
  569. ath_chanctx_adjust_tbtt_delta(sc);
  570. /* TSF time might have been updated by the incoming beacon,
  571. * need update the channel switch timer to reflect the change.
  572. */
  573. tsf_time = sc->sched.switch_start_time;
  574. tsf_time -= (u32) sc->cur_chan->tsf_val +
  575. ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
  576. tsf_time += ath9k_hw_gettsf32(ah);
  577. sc->sched.beacon_adjust = false;
  578. ath_chanctx_setup_timer(sc, tsf_time);
  579. break;
  580. case ATH_CHANCTX_EVENT_AUTHORIZED:
  581. if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
  582. avp->chanctx != sc->cur_chan)
  583. break;
  584. ath_dbg(common, CHAN_CTX,
  585. "Move chanctx state from FORCE_ACTIVE to IDLE\n");
  586. sc->sched.state = ATH_CHANCTX_STATE_IDLE;
  587. fallthrough;
  588. case ATH_CHANCTX_EVENT_SWITCH:
  589. if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
  590. sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
  591. sc->cur_chan->switch_after_beacon ||
  592. sc->cur_chan == &sc->offchannel.chan)
  593. break;
  594. /* If this is a station chanctx, stay active for a half
  595. * beacon period (minus channel switch time)
  596. */
  597. sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
  598. cur_conf = &sc->cur_chan->beacon;
  599. ath_dbg(common, CHAN_CTX,
  600. "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
  601. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
  602. sc->sched.wait_switch = false;
  603. tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
  604. if (sc->sched.extend_absence) {
  605. sc->sched.beacon_miss = 0;
  606. tsf_time *= 3;
  607. }
  608. tsf_time -= sc->sched.channel_switch_time;
  609. tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
  610. sc->sched.switch_start_time = tsf_time;
  611. ath_chanctx_setup_timer(sc, tsf_time);
  612. sc->sched.beacon_pending = true;
  613. sc->sched.beacon_adjust = true;
  614. break;
  615. case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
  616. if (sc->cur_chan == &sc->offchannel.chan ||
  617. sc->cur_chan->switch_after_beacon)
  618. break;
  619. sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
  620. ieee80211_queue_work(sc->hw, &sc->chanctx_work);
  621. break;
  622. case ATH_CHANCTX_EVENT_UNASSIGN:
  623. if (sc->cur_chan->assigned) {
  624. if (sc->next_chan && !sc->next_chan->assigned &&
  625. sc->next_chan != &sc->offchannel.chan)
  626. sc->sched.state = ATH_CHANCTX_STATE_IDLE;
  627. break;
  628. }
  629. ctx = ath_chanctx_get_next(sc, sc->cur_chan);
  630. sc->sched.state = ATH_CHANCTX_STATE_IDLE;
  631. if (!ctx->assigned)
  632. break;
  633. sc->next_chan = ctx;
  634. ieee80211_queue_work(sc->hw, &sc->chanctx_work);
  635. break;
  636. case ATH_CHANCTX_EVENT_ASSIGN:
  637. break;
  638. case ATH_CHANCTX_EVENT_CHANGE:
  639. break;
  640. }
  641. spin_unlock_bh(&sc->chan_lock);
  642. }
  643. void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
  644. enum ath_chanctx_event ev)
  645. {
  646. if (sc->sched.beacon_pending)
  647. ath_chanctx_event(sc, NULL, ev);
  648. }
  649. void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
  650. enum ath_chanctx_event ev)
  651. {
  652. ath_chanctx_event(sc, NULL, ev);
  653. }
  654. static int ath_scan_channel_duration(struct ath_softc *sc,
  655. struct ieee80211_channel *chan)
  656. {
  657. struct cfg80211_scan_request *req = sc->offchannel.scan_req;
  658. if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
  659. return (HZ / 9); /* ~110 ms */
  660. return (HZ / 16); /* ~60 ms */
  661. }
  662. static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
  663. struct cfg80211_chan_def *chandef)
  664. {
  665. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  666. spin_lock_bh(&sc->chan_lock);
  667. if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
  668. (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
  669. if (chandef)
  670. ctx->chandef = *chandef;
  671. sc->sched.offchannel_pending = true;
  672. sc->sched.wait_switch = true;
  673. sc->sched.offchannel_duration =
  674. jiffies_to_usecs(sc->offchannel.duration) +
  675. sc->sched.channel_switch_time;
  676. spin_unlock_bh(&sc->chan_lock);
  677. ath_dbg(common, CHAN_CTX,
  678. "Set offchannel_pending to true\n");
  679. return;
  680. }
  681. sc->next_chan = ctx;
  682. if (chandef) {
  683. ctx->chandef = *chandef;
  684. ath_dbg(common, CHAN_CTX,
  685. "Assigned next_chan to %d MHz\n", chandef->center_freq1);
  686. }
  687. if (sc->next_chan == &sc->offchannel.chan) {
  688. sc->sched.offchannel_duration =
  689. jiffies_to_usecs(sc->offchannel.duration) +
  690. sc->sched.channel_switch_time;
  691. if (chandef) {
  692. ath_dbg(common, CHAN_CTX,
  693. "Offchannel duration for chan %d MHz : %u\n",
  694. chandef->center_freq1,
  695. sc->sched.offchannel_duration);
  696. }
  697. }
  698. spin_unlock_bh(&sc->chan_lock);
  699. ieee80211_queue_work(sc->hw, &sc->chanctx_work);
  700. }
  701. static void ath_chanctx_offchan_switch(struct ath_softc *sc,
  702. struct ieee80211_channel *chan)
  703. {
  704. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  705. struct cfg80211_chan_def chandef;
  706. cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
  707. ath_dbg(common, CHAN_CTX,
  708. "Channel definition created: %d MHz\n", chandef.center_freq1);
  709. ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
  710. }
  711. static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
  712. bool active)
  713. {
  714. struct ath_chanctx *ctx;
  715. ath_for_each_chanctx(sc, ctx) {
  716. if (!ctx->assigned || list_empty(&ctx->vifs))
  717. continue;
  718. if (active && !ctx->active)
  719. continue;
  720. if (ctx->switch_after_beacon)
  721. return ctx;
  722. }
  723. return &sc->chanctx[0];
  724. }
  725. static void
  726. ath_scan_next_channel(struct ath_softc *sc)
  727. {
  728. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  729. struct cfg80211_scan_request *req = sc->offchannel.scan_req;
  730. struct ieee80211_channel *chan;
  731. if (sc->offchannel.scan_idx >= req->n_channels) {
  732. ath_dbg(common, CHAN_CTX,
  733. "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
  734. "scan_idx: %d, n_channels: %d\n",
  735. sc->offchannel.scan_idx,
  736. req->n_channels);
  737. sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
  738. ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
  739. NULL);
  740. return;
  741. }
  742. ath_dbg(common, CHAN_CTX,
  743. "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
  744. sc->offchannel.scan_idx);
  745. chan = req->channels[sc->offchannel.scan_idx++];
  746. sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
  747. sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
  748. ath_chanctx_offchan_switch(sc, chan);
  749. }
  750. void ath_offchannel_next(struct ath_softc *sc)
  751. {
  752. struct ieee80211_vif *vif;
  753. if (sc->offchannel.scan_req) {
  754. vif = sc->offchannel.scan_vif;
  755. sc->offchannel.chan.txpower = vif->bss_conf.txpower;
  756. ath_scan_next_channel(sc);
  757. } else if (sc->offchannel.roc_vif) {
  758. vif = sc->offchannel.roc_vif;
  759. sc->offchannel.chan.txpower = vif->bss_conf.txpower;
  760. sc->offchannel.duration =
  761. msecs_to_jiffies(sc->offchannel.roc_duration);
  762. sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
  763. ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
  764. } else {
  765. spin_lock_bh(&sc->chan_lock);
  766. sc->sched.offchannel_pending = false;
  767. sc->sched.wait_switch = false;
  768. spin_unlock_bh(&sc->chan_lock);
  769. ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
  770. NULL);
  771. sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
  772. if (sc->ps_idle)
  773. ath_cancel_work(sc);
  774. }
  775. }
  776. void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
  777. {
  778. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  779. sc->offchannel.roc_vif = NULL;
  780. sc->offchannel.roc_chan = NULL;
  781. switch (reason) {
  782. case ATH_ROC_COMPLETE_ABORT:
  783. ath_dbg(common, CHAN_CTX, "RoC aborted\n");
  784. ieee80211_remain_on_channel_expired(sc->hw);
  785. break;
  786. case ATH_ROC_COMPLETE_EXPIRE:
  787. ath_dbg(common, CHAN_CTX, "RoC expired\n");
  788. ieee80211_remain_on_channel_expired(sc->hw);
  789. break;
  790. case ATH_ROC_COMPLETE_CANCEL:
  791. ath_dbg(common, CHAN_CTX, "RoC canceled\n");
  792. break;
  793. }
  794. ath_offchannel_next(sc);
  795. ath9k_ps_restore(sc);
  796. }
  797. void ath_scan_complete(struct ath_softc *sc, bool abort)
  798. {
  799. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  800. struct cfg80211_scan_info info = {
  801. .aborted = abort,
  802. };
  803. if (abort)
  804. ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
  805. else
  806. ath_dbg(common, CHAN_CTX, "HW scan complete\n");
  807. sc->offchannel.scan_req = NULL;
  808. sc->offchannel.scan_vif = NULL;
  809. sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
  810. ieee80211_scan_completed(sc->hw, &info);
  811. clear_bit(ATH_OP_SCANNING, &common->op_flags);
  812. spin_lock_bh(&sc->chan_lock);
  813. if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
  814. sc->sched.force_noa_update = true;
  815. spin_unlock_bh(&sc->chan_lock);
  816. ath_offchannel_next(sc);
  817. ath9k_ps_restore(sc);
  818. }
  819. static void ath_scan_send_probe(struct ath_softc *sc,
  820. struct cfg80211_ssid *ssid)
  821. {
  822. struct cfg80211_scan_request *req = sc->offchannel.scan_req;
  823. struct ieee80211_vif *vif = sc->offchannel.scan_vif;
  824. struct ath_tx_control txctl = {};
  825. struct sk_buff *skb;
  826. struct ieee80211_tx_info *info;
  827. int band = sc->offchannel.chan.chandef.chan->band;
  828. skb = ieee80211_probereq_get(sc->hw, vif->addr,
  829. ssid->ssid, ssid->ssid_len, req->ie_len);
  830. if (!skb)
  831. return;
  832. info = IEEE80211_SKB_CB(skb);
  833. if (req->no_cck)
  834. info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  835. if (req->ie_len)
  836. skb_put_data(skb, req->ie, req->ie_len);
  837. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  838. if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
  839. goto error;
  840. txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
  841. if (ath_tx_start(sc->hw, skb, &txctl))
  842. goto error;
  843. return;
  844. error:
  845. ieee80211_free_txskb(sc->hw, skb);
  846. }
  847. static void ath_scan_channel_start(struct ath_softc *sc)
  848. {
  849. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  850. struct cfg80211_scan_request *req = sc->offchannel.scan_req;
  851. int i;
  852. if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
  853. req->n_ssids) {
  854. for (i = 0; i < req->n_ssids; i++)
  855. ath_scan_send_probe(sc, &req->ssids[i]);
  856. }
  857. ath_dbg(common, CHAN_CTX,
  858. "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
  859. sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
  860. mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
  861. }
  862. static void ath_chanctx_timer(struct timer_list *t)
  863. {
  864. struct ath_softc *sc = from_timer(sc, t, sched.timer);
  865. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  866. ath_dbg(common, CHAN_CTX,
  867. "Channel context timer invoked\n");
  868. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
  869. }
  870. static void ath_offchannel_timer(struct timer_list *t)
  871. {
  872. struct ath_softc *sc = from_timer(sc, t, offchannel.timer);
  873. struct ath_chanctx *ctx;
  874. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  875. ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
  876. __func__, offchannel_state_string(sc->offchannel.state));
  877. switch (sc->offchannel.state) {
  878. case ATH_OFFCHANNEL_PROBE_WAIT:
  879. if (!sc->offchannel.scan_req)
  880. return;
  881. /* get first active channel context */
  882. ctx = ath_chanctx_get_oper_chan(sc, true);
  883. if (ctx->active) {
  884. ath_dbg(common, CHAN_CTX,
  885. "Switch to oper/active context, "
  886. "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
  887. sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
  888. ath_chanctx_switch(sc, ctx, NULL);
  889. mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
  890. break;
  891. }
  892. fallthrough;
  893. case ATH_OFFCHANNEL_SUSPEND:
  894. if (!sc->offchannel.scan_req)
  895. return;
  896. ath_scan_next_channel(sc);
  897. break;
  898. case ATH_OFFCHANNEL_ROC_START:
  899. case ATH_OFFCHANNEL_ROC_WAIT:
  900. sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
  901. ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
  902. break;
  903. default:
  904. break;
  905. }
  906. }
  907. static bool
  908. ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
  909. bool powersave)
  910. {
  911. struct ieee80211_vif *vif = avp->vif;
  912. struct ieee80211_sta *sta = NULL;
  913. struct ieee80211_hdr_3addr *nullfunc;
  914. struct ath_tx_control txctl;
  915. struct sk_buff *skb;
  916. int band = sc->cur_chan->chandef.chan->band;
  917. switch (vif->type) {
  918. case NL80211_IFTYPE_STATION:
  919. if (!avp->assoc)
  920. return false;
  921. skb = ieee80211_nullfunc_get(sc->hw, vif, -1, false);
  922. if (!skb)
  923. return false;
  924. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  925. if (powersave)
  926. nullfunc->frame_control |=
  927. cpu_to_le16(IEEE80211_FCTL_PM);
  928. skb->priority = 7;
  929. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  930. if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
  931. dev_kfree_skb_any(skb);
  932. return false;
  933. }
  934. break;
  935. default:
  936. return false;
  937. }
  938. memset(&txctl, 0, sizeof(txctl));
  939. txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
  940. txctl.sta = sta;
  941. if (ath_tx_start(sc->hw, skb, &txctl)) {
  942. ieee80211_free_txskb(sc->hw, skb);
  943. return false;
  944. }
  945. return true;
  946. }
  947. static bool
  948. ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
  949. {
  950. struct ath_vif *avp;
  951. bool sent = false;
  952. rcu_read_lock();
  953. list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
  954. if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
  955. sent = true;
  956. }
  957. rcu_read_unlock();
  958. return sent;
  959. }
  960. static bool ath_chanctx_defer_switch(struct ath_softc *sc)
  961. {
  962. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  963. if (sc->cur_chan == &sc->offchannel.chan)
  964. return false;
  965. switch (sc->sched.state) {
  966. case ATH_CHANCTX_STATE_SWITCH:
  967. return false;
  968. case ATH_CHANCTX_STATE_IDLE:
  969. if (!sc->cur_chan->switch_after_beacon)
  970. return false;
  971. ath_dbg(common, CHAN_CTX,
  972. "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
  973. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
  974. break;
  975. default:
  976. break;
  977. }
  978. return true;
  979. }
  980. static void ath_offchannel_channel_change(struct ath_softc *sc)
  981. {
  982. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  983. ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
  984. __func__, offchannel_state_string(sc->offchannel.state));
  985. switch (sc->offchannel.state) {
  986. case ATH_OFFCHANNEL_PROBE_SEND:
  987. if (!sc->offchannel.scan_req)
  988. return;
  989. if (sc->cur_chan->chandef.chan !=
  990. sc->offchannel.chan.chandef.chan)
  991. return;
  992. ath_scan_channel_start(sc);
  993. break;
  994. case ATH_OFFCHANNEL_IDLE:
  995. if (!sc->offchannel.scan_req)
  996. return;
  997. ath_scan_complete(sc, false);
  998. break;
  999. case ATH_OFFCHANNEL_ROC_START:
  1000. if (sc->cur_chan != &sc->offchannel.chan)
  1001. break;
  1002. sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
  1003. mod_timer(&sc->offchannel.timer,
  1004. jiffies + sc->offchannel.duration);
  1005. ieee80211_ready_on_channel(sc->hw);
  1006. break;
  1007. case ATH_OFFCHANNEL_ROC_DONE:
  1008. break;
  1009. default:
  1010. break;
  1011. }
  1012. }
  1013. void ath_chanctx_set_next(struct ath_softc *sc, bool force)
  1014. {
  1015. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1016. struct ath_chanctx *old_ctx;
  1017. struct timespec64 ts;
  1018. bool measure_time = false;
  1019. bool send_ps = false;
  1020. bool queues_stopped = false;
  1021. spin_lock_bh(&sc->chan_lock);
  1022. if (!sc->next_chan) {
  1023. spin_unlock_bh(&sc->chan_lock);
  1024. return;
  1025. }
  1026. if (!force && ath_chanctx_defer_switch(sc)) {
  1027. spin_unlock_bh(&sc->chan_lock);
  1028. return;
  1029. }
  1030. ath_dbg(common, CHAN_CTX,
  1031. "%s: current: %d MHz, next: %d MHz\n",
  1032. __func__,
  1033. sc->cur_chan->chandef.center_freq1,
  1034. sc->next_chan->chandef.center_freq1);
  1035. if (sc->cur_chan != sc->next_chan) {
  1036. ath_dbg(common, CHAN_CTX,
  1037. "Stopping current chanctx: %d\n",
  1038. sc->cur_chan->chandef.center_freq1);
  1039. sc->cur_chan->stopped = true;
  1040. spin_unlock_bh(&sc->chan_lock);
  1041. if (sc->next_chan == &sc->offchannel.chan) {
  1042. ktime_get_raw_ts64(&ts);
  1043. measure_time = true;
  1044. }
  1045. ath9k_chanctx_stop_queues(sc, sc->cur_chan);
  1046. queues_stopped = true;
  1047. __ath9k_flush(sc->hw, ~0, true, false, false);
  1048. if (ath_chanctx_send_ps_frame(sc, true))
  1049. __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
  1050. false, false, false);
  1051. send_ps = true;
  1052. spin_lock_bh(&sc->chan_lock);
  1053. if (sc->cur_chan != &sc->offchannel.chan) {
  1054. ktime_get_raw_ts64(&sc->cur_chan->tsf_ts);
  1055. sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
  1056. }
  1057. }
  1058. old_ctx = sc->cur_chan;
  1059. sc->cur_chan = sc->next_chan;
  1060. sc->cur_chan->stopped = false;
  1061. sc->next_chan = NULL;
  1062. if (!sc->sched.offchannel_pending)
  1063. sc->sched.offchannel_duration = 0;
  1064. if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
  1065. sc->sched.state = ATH_CHANCTX_STATE_IDLE;
  1066. spin_unlock_bh(&sc->chan_lock);
  1067. if (sc->sc_ah->chip_fullsleep ||
  1068. memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
  1069. sizeof(sc->cur_chandef))) {
  1070. ath_dbg(common, CHAN_CTX,
  1071. "%s: Set channel %d MHz\n",
  1072. __func__, sc->cur_chan->chandef.center_freq1);
  1073. ath_set_channel(sc);
  1074. if (measure_time)
  1075. sc->sched.channel_switch_time =
  1076. ath9k_hw_get_tsf_offset(&ts, NULL);
  1077. /*
  1078. * A reset will ensure that all queues are woken up,
  1079. * so there is no need to awaken them again.
  1080. */
  1081. goto out;
  1082. }
  1083. if (queues_stopped)
  1084. ath9k_chanctx_wake_queues(sc, old_ctx);
  1085. out:
  1086. if (send_ps)
  1087. ath_chanctx_send_ps_frame(sc, false);
  1088. ath_offchannel_channel_change(sc);
  1089. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
  1090. }
  1091. static void ath_chanctx_work(struct work_struct *work)
  1092. {
  1093. struct ath_softc *sc = container_of(work, struct ath_softc,
  1094. chanctx_work);
  1095. mutex_lock(&sc->mutex);
  1096. ath_chanctx_set_next(sc, false);
  1097. mutex_unlock(&sc->mutex);
  1098. }
  1099. void ath9k_offchannel_init(struct ath_softc *sc)
  1100. {
  1101. struct ath_chanctx *ctx;
  1102. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1103. struct ieee80211_supported_band *sband;
  1104. struct ieee80211_channel *chan;
  1105. int i;
  1106. sband = &common->sbands[NL80211_BAND_2GHZ];
  1107. if (!sband->n_channels)
  1108. sband = &common->sbands[NL80211_BAND_5GHZ];
  1109. chan = &sband->channels[0];
  1110. ctx = &sc->offchannel.chan;
  1111. INIT_LIST_HEAD(&ctx->vifs);
  1112. ctx->txpower = ATH_TXPOWER_MAX;
  1113. cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
  1114. for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) {
  1115. INIT_LIST_HEAD(&ctx->acq[i].acq_new);
  1116. INIT_LIST_HEAD(&ctx->acq[i].acq_old);
  1117. spin_lock_init(&ctx->acq[i].lock);
  1118. }
  1119. sc->offchannel.chan.offchannel = true;
  1120. }
  1121. void ath9k_init_channel_context(struct ath_softc *sc)
  1122. {
  1123. INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
  1124. timer_setup(&sc->offchannel.timer, ath_offchannel_timer, 0);
  1125. timer_setup(&sc->sched.timer, ath_chanctx_timer, 0);
  1126. init_completion(&sc->go_beacon);
  1127. }
  1128. void ath9k_deinit_channel_context(struct ath_softc *sc)
  1129. {
  1130. cancel_work_sync(&sc->chanctx_work);
  1131. }
  1132. bool ath9k_is_chanctx_enabled(void)
  1133. {
  1134. return (ath9k_use_chanctx == 1);
  1135. }
  1136. /********************/
  1137. /* Queue management */
  1138. /********************/
  1139. void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
  1140. {
  1141. struct ath_hw *ah = sc->sc_ah;
  1142. int i;
  1143. if (ctx == &sc->offchannel.chan) {
  1144. ieee80211_stop_queue(sc->hw,
  1145. sc->hw->offchannel_tx_hw_queue);
  1146. } else {
  1147. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1148. ieee80211_stop_queue(sc->hw,
  1149. ctx->hw_queue_base + i);
  1150. }
  1151. if (ah->opmode == NL80211_IFTYPE_AP)
  1152. ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
  1153. }
  1154. void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
  1155. {
  1156. struct ath_hw *ah = sc->sc_ah;
  1157. int i;
  1158. if (ctx == &sc->offchannel.chan) {
  1159. ieee80211_wake_queue(sc->hw,
  1160. sc->hw->offchannel_tx_hw_queue);
  1161. } else {
  1162. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1163. ieee80211_wake_queue(sc->hw,
  1164. ctx->hw_queue_base + i);
  1165. }
  1166. if (ah->opmode == NL80211_IFTYPE_AP)
  1167. ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
  1168. }
  1169. /*****************/
  1170. /* P2P Powersave */
  1171. /*****************/
  1172. static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
  1173. {
  1174. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1175. struct ath_hw *ah = sc->sc_ah;
  1176. u32 tsf, target_tsf;
  1177. if (!avp || !avp->noa.has_next_tsf)
  1178. return;
  1179. ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
  1180. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1181. target_tsf = avp->noa.next_tsf;
  1182. if (!avp->noa.absent)
  1183. target_tsf -= ATH_P2P_PS_STOP_TIME;
  1184. else
  1185. target_tsf += ATH_P2P_PS_STOP_TIME;
  1186. if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
  1187. target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
  1188. ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
  1189. __func__, avp->noa.absent, tsf, target_tsf,
  1190. (target_tsf - tsf) / 1000);
  1191. ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
  1192. }
  1193. static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
  1194. {
  1195. struct ath_vif *avp = (void *)vif->drv_priv;
  1196. u32 tsf;
  1197. if (!sc->p2p_ps_timer)
  1198. return;
  1199. if (vif->type != NL80211_IFTYPE_STATION)
  1200. return;
  1201. sc->p2p_ps_vif = avp;
  1202. if (sc->ps_flags & PS_BEACON_SYNC)
  1203. return;
  1204. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1205. ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
  1206. ath9k_update_p2p_ps_timer(sc, avp);
  1207. }
  1208. static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
  1209. {
  1210. struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
  1211. u8 switch_time, ctwin;
  1212. /*
  1213. * Channel switch in multi-channel mode is deferred
  1214. * by a quarter beacon interval when handling
  1215. * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
  1216. * interface is guaranteed to be discoverable
  1217. * for that duration after a TBTT.
  1218. */
  1219. switch_time = cur_conf->beacon_interval / 4;
  1220. ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
  1221. if (ctwin && (ctwin < switch_time))
  1222. return ctwin;
  1223. if (switch_time < P2P_DEFAULT_CTWIN)
  1224. return 0;
  1225. return P2P_DEFAULT_CTWIN;
  1226. }
  1227. void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
  1228. struct sk_buff *skb)
  1229. {
  1230. static const u8 noa_ie_hdr[] = {
  1231. WLAN_EID_VENDOR_SPECIFIC, /* type */
  1232. 0, /* length */
  1233. 0x50, 0x6f, 0x9a, /* WFA OUI */
  1234. 0x09, /* P2P subtype */
  1235. 0x0c, /* Notice of Absence */
  1236. 0x00, /* LSB of little-endian len */
  1237. 0x00, /* MSB of little-endian len */
  1238. };
  1239. struct ieee80211_p2p_noa_attr *noa;
  1240. int noa_len, noa_desc, i = 0;
  1241. u8 *hdr;
  1242. if (!avp->offchannel_duration && !avp->noa_duration)
  1243. return;
  1244. noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
  1245. noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
  1246. hdr = skb_put_data(skb, noa_ie_hdr, sizeof(noa_ie_hdr));
  1247. hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
  1248. hdr[7] = noa_len;
  1249. noa = skb_put_zero(skb, noa_len);
  1250. noa->index = avp->noa_index;
  1251. noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
  1252. if (noa->oppps_ctwindow)
  1253. noa->oppps_ctwindow |= BIT(7);
  1254. if (avp->noa_duration) {
  1255. if (avp->periodic_noa) {
  1256. u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
  1257. noa->desc[i].count = 255;
  1258. noa->desc[i].interval = cpu_to_le32(interval);
  1259. } else {
  1260. noa->desc[i].count = 1;
  1261. }
  1262. noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
  1263. noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
  1264. i++;
  1265. }
  1266. if (avp->offchannel_duration) {
  1267. noa->desc[i].count = 1;
  1268. noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
  1269. noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
  1270. }
  1271. }
  1272. void ath9k_p2p_ps_timer(void *priv)
  1273. {
  1274. struct ath_softc *sc = priv;
  1275. struct ath_vif *avp = sc->p2p_ps_vif;
  1276. struct ieee80211_vif *vif;
  1277. struct ieee80211_sta *sta;
  1278. struct ath_node *an;
  1279. u32 tsf;
  1280. del_timer_sync(&sc->sched.timer);
  1281. ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
  1282. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
  1283. if (!avp || avp->chanctx != sc->cur_chan)
  1284. return;
  1285. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1286. if (!avp->noa.absent)
  1287. tsf += ATH_P2P_PS_STOP_TIME;
  1288. else
  1289. tsf -= ATH_P2P_PS_STOP_TIME;
  1290. if (!avp->noa.has_next_tsf ||
  1291. avp->noa.next_tsf - tsf > BIT(31))
  1292. ieee80211_update_p2p_noa(&avp->noa, tsf);
  1293. ath9k_update_p2p_ps_timer(sc, avp);
  1294. rcu_read_lock();
  1295. vif = avp->vif;
  1296. sta = ieee80211_find_sta(vif, avp->bssid);
  1297. if (!sta)
  1298. goto out;
  1299. an = (void *) sta->drv_priv;
  1300. if (an->sleeping == !!avp->noa.absent)
  1301. goto out;
  1302. an->sleeping = avp->noa.absent;
  1303. if (an->sleeping)
  1304. ath_tx_aggr_sleep(sta, sc, an);
  1305. else
  1306. ath_tx_aggr_wakeup(sc, an);
  1307. out:
  1308. rcu_read_unlock();
  1309. }
  1310. void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
  1311. struct ieee80211_vif *vif)
  1312. {
  1313. unsigned long flags;
  1314. spin_lock_bh(&sc->sc_pcu_lock);
  1315. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1316. ath9k_update_p2p_ps(sc, vif);
  1317. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1318. spin_unlock_bh(&sc->sc_pcu_lock);
  1319. }
  1320. void ath9k_p2p_beacon_sync(struct ath_softc *sc)
  1321. {
  1322. if (sc->p2p_ps_vif)
  1323. ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
  1324. }
  1325. void ath9k_p2p_remove_vif(struct ath_softc *sc,
  1326. struct ieee80211_vif *vif)
  1327. {
  1328. struct ath_vif *avp = (void *)vif->drv_priv;
  1329. spin_lock_bh(&sc->sc_pcu_lock);
  1330. if (avp == sc->p2p_ps_vif) {
  1331. sc->p2p_ps_vif = NULL;
  1332. ath9k_update_p2p_ps_timer(sc, NULL);
  1333. }
  1334. spin_unlock_bh(&sc->sc_pcu_lock);
  1335. }
  1336. int ath9k_init_p2p(struct ath_softc *sc)
  1337. {
  1338. sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
  1339. NULL, sc, AR_FIRST_NDP_TIMER);
  1340. if (!sc->p2p_ps_timer)
  1341. return -ENOMEM;
  1342. return 0;
  1343. }
  1344. void ath9k_deinit_p2p(struct ath_softc *sc)
  1345. {
  1346. if (sc->p2p_ps_timer)
  1347. ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
  1348. }
  1349. #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */