channel.c 42 KB

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