channel.c 41 KB

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