channel.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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. 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. if (abort)
  797. ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
  798. else
  799. ath_dbg(common, CHAN_CTX, "HW scan complete\n");
  800. sc->offchannel.scan_req = NULL;
  801. sc->offchannel.scan_vif = NULL;
  802. sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
  803. ieee80211_scan_completed(sc->hw, abort);
  804. clear_bit(ATH_OP_SCANNING, &common->op_flags);
  805. spin_lock_bh(&sc->chan_lock);
  806. if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
  807. sc->sched.force_noa_update = true;
  808. spin_unlock_bh(&sc->chan_lock);
  809. ath_offchannel_next(sc);
  810. ath9k_ps_restore(sc);
  811. }
  812. static void ath_scan_send_probe(struct ath_softc *sc,
  813. struct cfg80211_ssid *ssid)
  814. {
  815. struct cfg80211_scan_request *req = sc->offchannel.scan_req;
  816. struct ieee80211_vif *vif = sc->offchannel.scan_vif;
  817. struct ath_tx_control txctl = {};
  818. struct sk_buff *skb;
  819. struct ieee80211_tx_info *info;
  820. int band = sc->offchannel.chan.chandef.chan->band;
  821. skb = ieee80211_probereq_get(sc->hw, vif->addr,
  822. ssid->ssid, ssid->ssid_len, req->ie_len);
  823. if (!skb)
  824. return;
  825. info = IEEE80211_SKB_CB(skb);
  826. if (req->no_cck)
  827. info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  828. if (req->ie_len)
  829. memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
  830. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  831. if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
  832. goto error;
  833. txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
  834. txctl.force_channel = true;
  835. if (ath_tx_start(sc->hw, skb, &txctl))
  836. goto error;
  837. return;
  838. error:
  839. ieee80211_free_txskb(sc->hw, skb);
  840. }
  841. static void ath_scan_channel_start(struct ath_softc *sc)
  842. {
  843. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  844. struct cfg80211_scan_request *req = sc->offchannel.scan_req;
  845. int i;
  846. if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
  847. req->n_ssids) {
  848. for (i = 0; i < req->n_ssids; i++)
  849. ath_scan_send_probe(sc, &req->ssids[i]);
  850. }
  851. ath_dbg(common, CHAN_CTX,
  852. "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
  853. sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
  854. mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
  855. }
  856. static void ath_chanctx_timer(unsigned long data)
  857. {
  858. struct ath_softc *sc = (struct ath_softc *) data;
  859. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  860. ath_dbg(common, CHAN_CTX,
  861. "Channel context timer invoked\n");
  862. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
  863. }
  864. static void ath_offchannel_timer(unsigned long data)
  865. {
  866. struct ath_softc *sc = (struct ath_softc *)data;
  867. struct ath_chanctx *ctx;
  868. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  869. ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
  870. __func__, offchannel_state_string(sc->offchannel.state));
  871. switch (sc->offchannel.state) {
  872. case ATH_OFFCHANNEL_PROBE_WAIT:
  873. if (!sc->offchannel.scan_req)
  874. return;
  875. /* get first active channel context */
  876. ctx = ath_chanctx_get_oper_chan(sc, true);
  877. if (ctx->active) {
  878. ath_dbg(common, CHAN_CTX,
  879. "Switch to oper/active context, "
  880. "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
  881. sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
  882. ath_chanctx_switch(sc, ctx, NULL);
  883. mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
  884. break;
  885. }
  886. /* fall through */
  887. case ATH_OFFCHANNEL_SUSPEND:
  888. if (!sc->offchannel.scan_req)
  889. return;
  890. ath_scan_next_channel(sc);
  891. break;
  892. case ATH_OFFCHANNEL_ROC_START:
  893. case ATH_OFFCHANNEL_ROC_WAIT:
  894. sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
  895. ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
  896. break;
  897. default:
  898. break;
  899. }
  900. }
  901. static bool
  902. ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
  903. bool powersave)
  904. {
  905. struct ieee80211_vif *vif = avp->vif;
  906. struct ieee80211_sta *sta = NULL;
  907. struct ieee80211_hdr_3addr *nullfunc;
  908. struct ath_tx_control txctl;
  909. struct sk_buff *skb;
  910. int band = sc->cur_chan->chandef.chan->band;
  911. switch (vif->type) {
  912. case NL80211_IFTYPE_STATION:
  913. if (!avp->assoc)
  914. return false;
  915. skb = ieee80211_nullfunc_get(sc->hw, vif);
  916. if (!skb)
  917. return false;
  918. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  919. if (powersave)
  920. nullfunc->frame_control |=
  921. cpu_to_le16(IEEE80211_FCTL_PM);
  922. skb->priority = 7;
  923. skb_set_queue_mapping(skb, IEEE80211_AC_VO);
  924. if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
  925. dev_kfree_skb_any(skb);
  926. return false;
  927. }
  928. break;
  929. default:
  930. return false;
  931. }
  932. memset(&txctl, 0, sizeof(txctl));
  933. txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
  934. txctl.sta = sta;
  935. txctl.force_channel = true;
  936. if (ath_tx_start(sc->hw, skb, &txctl)) {
  937. ieee80211_free_txskb(sc->hw, skb);
  938. return false;
  939. }
  940. return true;
  941. }
  942. static bool
  943. ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
  944. {
  945. struct ath_vif *avp;
  946. bool sent = false;
  947. rcu_read_lock();
  948. list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
  949. if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
  950. sent = true;
  951. }
  952. rcu_read_unlock();
  953. return sent;
  954. }
  955. static bool ath_chanctx_defer_switch(struct ath_softc *sc)
  956. {
  957. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  958. if (sc->cur_chan == &sc->offchannel.chan)
  959. return false;
  960. switch (sc->sched.state) {
  961. case ATH_CHANCTX_STATE_SWITCH:
  962. return false;
  963. case ATH_CHANCTX_STATE_IDLE:
  964. if (!sc->cur_chan->switch_after_beacon)
  965. return false;
  966. ath_dbg(common, CHAN_CTX,
  967. "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
  968. sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
  969. break;
  970. default:
  971. break;
  972. }
  973. return true;
  974. }
  975. static void ath_offchannel_channel_change(struct ath_softc *sc)
  976. {
  977. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  978. ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
  979. __func__, offchannel_state_string(sc->offchannel.state));
  980. switch (sc->offchannel.state) {
  981. case ATH_OFFCHANNEL_PROBE_SEND:
  982. if (!sc->offchannel.scan_req)
  983. return;
  984. if (sc->cur_chan->chandef.chan !=
  985. sc->offchannel.chan.chandef.chan)
  986. return;
  987. ath_scan_channel_start(sc);
  988. break;
  989. case ATH_OFFCHANNEL_IDLE:
  990. if (!sc->offchannel.scan_req)
  991. return;
  992. ath_scan_complete(sc, false);
  993. break;
  994. case ATH_OFFCHANNEL_ROC_START:
  995. if (sc->cur_chan != &sc->offchannel.chan)
  996. break;
  997. sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
  998. mod_timer(&sc->offchannel.timer,
  999. jiffies + sc->offchannel.duration);
  1000. ieee80211_ready_on_channel(sc->hw);
  1001. break;
  1002. case ATH_OFFCHANNEL_ROC_DONE:
  1003. break;
  1004. default:
  1005. break;
  1006. }
  1007. }
  1008. void ath_chanctx_set_next(struct ath_softc *sc, bool force)
  1009. {
  1010. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1011. struct ath_chanctx *old_ctx;
  1012. struct timespec ts;
  1013. bool measure_time = false;
  1014. bool send_ps = false;
  1015. bool queues_stopped = false;
  1016. spin_lock_bh(&sc->chan_lock);
  1017. if (!sc->next_chan) {
  1018. spin_unlock_bh(&sc->chan_lock);
  1019. return;
  1020. }
  1021. if (!force && ath_chanctx_defer_switch(sc)) {
  1022. spin_unlock_bh(&sc->chan_lock);
  1023. return;
  1024. }
  1025. ath_dbg(common, CHAN_CTX,
  1026. "%s: current: %d MHz, next: %d MHz\n",
  1027. __func__,
  1028. sc->cur_chan->chandef.center_freq1,
  1029. sc->next_chan->chandef.center_freq1);
  1030. if (sc->cur_chan != sc->next_chan) {
  1031. ath_dbg(common, CHAN_CTX,
  1032. "Stopping current chanctx: %d\n",
  1033. sc->cur_chan->chandef.center_freq1);
  1034. sc->cur_chan->stopped = true;
  1035. spin_unlock_bh(&sc->chan_lock);
  1036. if (sc->next_chan == &sc->offchannel.chan) {
  1037. getrawmonotonic(&ts);
  1038. measure_time = true;
  1039. }
  1040. ath9k_chanctx_stop_queues(sc, sc->cur_chan);
  1041. queues_stopped = true;
  1042. __ath9k_flush(sc->hw, ~0, true, false, false);
  1043. if (ath_chanctx_send_ps_frame(sc, true))
  1044. __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
  1045. false, false, false);
  1046. send_ps = true;
  1047. spin_lock_bh(&sc->chan_lock);
  1048. if (sc->cur_chan != &sc->offchannel.chan) {
  1049. getrawmonotonic(&sc->cur_chan->tsf_ts);
  1050. sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
  1051. }
  1052. }
  1053. old_ctx = sc->cur_chan;
  1054. sc->cur_chan = sc->next_chan;
  1055. sc->cur_chan->stopped = false;
  1056. sc->next_chan = NULL;
  1057. if (!sc->sched.offchannel_pending)
  1058. sc->sched.offchannel_duration = 0;
  1059. if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
  1060. sc->sched.state = ATH_CHANCTX_STATE_IDLE;
  1061. spin_unlock_bh(&sc->chan_lock);
  1062. if (sc->sc_ah->chip_fullsleep ||
  1063. memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
  1064. sizeof(sc->cur_chandef))) {
  1065. ath_dbg(common, CHAN_CTX,
  1066. "%s: Set channel %d MHz\n",
  1067. __func__, sc->cur_chan->chandef.center_freq1);
  1068. ath_set_channel(sc);
  1069. if (measure_time)
  1070. sc->sched.channel_switch_time =
  1071. ath9k_hw_get_tsf_offset(&ts, NULL);
  1072. /*
  1073. * A reset will ensure that all queues are woken up,
  1074. * so there is no need to awaken them again.
  1075. */
  1076. goto out;
  1077. }
  1078. if (queues_stopped)
  1079. ath9k_chanctx_wake_queues(sc, old_ctx);
  1080. out:
  1081. if (send_ps)
  1082. ath_chanctx_send_ps_frame(sc, false);
  1083. ath_offchannel_channel_change(sc);
  1084. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
  1085. }
  1086. static void ath_chanctx_work(struct work_struct *work)
  1087. {
  1088. struct ath_softc *sc = container_of(work, struct ath_softc,
  1089. chanctx_work);
  1090. mutex_lock(&sc->mutex);
  1091. ath_chanctx_set_next(sc, false);
  1092. mutex_unlock(&sc->mutex);
  1093. }
  1094. void ath9k_offchannel_init(struct ath_softc *sc)
  1095. {
  1096. struct ath_chanctx *ctx;
  1097. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1098. struct ieee80211_supported_band *sband;
  1099. struct ieee80211_channel *chan;
  1100. int i;
  1101. sband = &common->sbands[IEEE80211_BAND_2GHZ];
  1102. if (!sband->n_channels)
  1103. sband = &common->sbands[IEEE80211_BAND_5GHZ];
  1104. chan = &sband->channels[0];
  1105. ctx = &sc->offchannel.chan;
  1106. INIT_LIST_HEAD(&ctx->vifs);
  1107. ctx->txpower = ATH_TXPOWER_MAX;
  1108. cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
  1109. for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
  1110. INIT_LIST_HEAD(&ctx->acq[i]);
  1111. sc->offchannel.chan.offchannel = true;
  1112. }
  1113. void ath9k_init_channel_context(struct ath_softc *sc)
  1114. {
  1115. INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
  1116. setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
  1117. (unsigned long)sc);
  1118. setup_timer(&sc->sched.timer, ath_chanctx_timer,
  1119. (unsigned long)sc);
  1120. init_completion(&sc->go_beacon);
  1121. }
  1122. void ath9k_deinit_channel_context(struct ath_softc *sc)
  1123. {
  1124. cancel_work_sync(&sc->chanctx_work);
  1125. }
  1126. bool ath9k_is_chanctx_enabled(void)
  1127. {
  1128. return (ath9k_use_chanctx == 1);
  1129. }
  1130. /********************/
  1131. /* Queue management */
  1132. /********************/
  1133. void ath9k_chanctx_stop_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_stop_queue(sc->hw,
  1139. sc->hw->offchannel_tx_hw_queue);
  1140. } else {
  1141. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1142. ieee80211_stop_queue(sc->hw,
  1143. ctx->hw_queue_base + i);
  1144. }
  1145. if (ah->opmode == NL80211_IFTYPE_AP)
  1146. ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
  1147. }
  1148. void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
  1149. {
  1150. struct ath_hw *ah = sc->sc_ah;
  1151. int i;
  1152. if (ctx == &sc->offchannel.chan) {
  1153. ieee80211_wake_queue(sc->hw,
  1154. sc->hw->offchannel_tx_hw_queue);
  1155. } else {
  1156. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1157. ieee80211_wake_queue(sc->hw,
  1158. ctx->hw_queue_base + i);
  1159. }
  1160. if (ah->opmode == NL80211_IFTYPE_AP)
  1161. ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
  1162. }
  1163. /*****************/
  1164. /* P2P Powersave */
  1165. /*****************/
  1166. static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
  1167. {
  1168. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1169. struct ath_hw *ah = sc->sc_ah;
  1170. u32 tsf, target_tsf;
  1171. if (!avp || !avp->noa.has_next_tsf)
  1172. return;
  1173. ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
  1174. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1175. target_tsf = avp->noa.next_tsf;
  1176. if (!avp->noa.absent)
  1177. target_tsf -= ATH_P2P_PS_STOP_TIME;
  1178. else
  1179. target_tsf += ATH_P2P_PS_STOP_TIME;
  1180. if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
  1181. target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
  1182. ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
  1183. __func__, avp->noa.absent, tsf, target_tsf,
  1184. (target_tsf - tsf) / 1000);
  1185. ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
  1186. }
  1187. static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
  1188. {
  1189. struct ath_vif *avp = (void *)vif->drv_priv;
  1190. u32 tsf;
  1191. if (!sc->p2p_ps_timer)
  1192. return;
  1193. if (vif->type != NL80211_IFTYPE_STATION)
  1194. return;
  1195. sc->p2p_ps_vif = avp;
  1196. if (sc->ps_flags & PS_BEACON_SYNC)
  1197. return;
  1198. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1199. ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
  1200. ath9k_update_p2p_ps_timer(sc, avp);
  1201. }
  1202. static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
  1203. {
  1204. struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
  1205. u8 switch_time, ctwin;
  1206. /*
  1207. * Channel switch in multi-channel mode is deferred
  1208. * by a quarter beacon interval when handling
  1209. * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
  1210. * interface is guaranteed to be discoverable
  1211. * for that duration after a TBTT.
  1212. */
  1213. switch_time = cur_conf->beacon_interval / 4;
  1214. ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
  1215. if (ctwin && (ctwin < switch_time))
  1216. return ctwin;
  1217. if (switch_time < P2P_DEFAULT_CTWIN)
  1218. return 0;
  1219. return P2P_DEFAULT_CTWIN;
  1220. }
  1221. void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
  1222. struct sk_buff *skb)
  1223. {
  1224. static const u8 noa_ie_hdr[] = {
  1225. WLAN_EID_VENDOR_SPECIFIC, /* type */
  1226. 0, /* length */
  1227. 0x50, 0x6f, 0x9a, /* WFA OUI */
  1228. 0x09, /* P2P subtype */
  1229. 0x0c, /* Notice of Absence */
  1230. 0x00, /* LSB of little-endian len */
  1231. 0x00, /* MSB of little-endian len */
  1232. };
  1233. struct ieee80211_p2p_noa_attr *noa;
  1234. int noa_len, noa_desc, i = 0;
  1235. u8 *hdr;
  1236. if (!avp->offchannel_duration && !avp->noa_duration)
  1237. return;
  1238. noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
  1239. noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
  1240. hdr = skb_put(skb, sizeof(noa_ie_hdr));
  1241. memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
  1242. hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
  1243. hdr[7] = noa_len;
  1244. noa = (void *) skb_put(skb, noa_len);
  1245. memset(noa, 0, noa_len);
  1246. noa->index = avp->noa_index;
  1247. noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
  1248. if (noa->oppps_ctwindow)
  1249. noa->oppps_ctwindow |= BIT(7);
  1250. if (avp->noa_duration) {
  1251. if (avp->periodic_noa) {
  1252. u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
  1253. noa->desc[i].count = 255;
  1254. noa->desc[i].interval = cpu_to_le32(interval);
  1255. } else {
  1256. noa->desc[i].count = 1;
  1257. }
  1258. noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
  1259. noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
  1260. i++;
  1261. }
  1262. if (avp->offchannel_duration) {
  1263. noa->desc[i].count = 1;
  1264. noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
  1265. noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
  1266. }
  1267. }
  1268. void ath9k_p2p_ps_timer(void *priv)
  1269. {
  1270. struct ath_softc *sc = priv;
  1271. struct ath_vif *avp = sc->p2p_ps_vif;
  1272. struct ieee80211_vif *vif;
  1273. struct ieee80211_sta *sta;
  1274. struct ath_node *an;
  1275. u32 tsf;
  1276. del_timer_sync(&sc->sched.timer);
  1277. ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
  1278. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
  1279. if (!avp || avp->chanctx != sc->cur_chan)
  1280. return;
  1281. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1282. if (!avp->noa.absent)
  1283. tsf += ATH_P2P_PS_STOP_TIME;
  1284. else
  1285. tsf -= ATH_P2P_PS_STOP_TIME;
  1286. if (!avp->noa.has_next_tsf ||
  1287. avp->noa.next_tsf - tsf > BIT(31))
  1288. ieee80211_update_p2p_noa(&avp->noa, tsf);
  1289. ath9k_update_p2p_ps_timer(sc, avp);
  1290. rcu_read_lock();
  1291. vif = avp->vif;
  1292. sta = ieee80211_find_sta(vif, avp->bssid);
  1293. if (!sta)
  1294. goto out;
  1295. an = (void *) sta->drv_priv;
  1296. if (an->sleeping == !!avp->noa.absent)
  1297. goto out;
  1298. an->sleeping = avp->noa.absent;
  1299. if (an->sleeping)
  1300. ath_tx_aggr_sleep(sta, sc, an);
  1301. else
  1302. ath_tx_aggr_wakeup(sc, an);
  1303. out:
  1304. rcu_read_unlock();
  1305. }
  1306. void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
  1307. struct ieee80211_vif *vif)
  1308. {
  1309. unsigned long flags;
  1310. spin_lock_bh(&sc->sc_pcu_lock);
  1311. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1312. ath9k_update_p2p_ps(sc, vif);
  1313. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1314. spin_unlock_bh(&sc->sc_pcu_lock);
  1315. }
  1316. void ath9k_p2p_beacon_sync(struct ath_softc *sc)
  1317. {
  1318. if (sc->p2p_ps_vif)
  1319. ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
  1320. }
  1321. void ath9k_p2p_remove_vif(struct ath_softc *sc,
  1322. struct ieee80211_vif *vif)
  1323. {
  1324. struct ath_vif *avp = (void *)vif->drv_priv;
  1325. spin_lock_bh(&sc->sc_pcu_lock);
  1326. if (avp == sc->p2p_ps_vif) {
  1327. sc->p2p_ps_vif = NULL;
  1328. ath9k_update_p2p_ps_timer(sc, NULL);
  1329. }
  1330. spin_unlock_bh(&sc->sc_pcu_lock);
  1331. }
  1332. int ath9k_init_p2p(struct ath_softc *sc)
  1333. {
  1334. sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
  1335. NULL, sc, AR_FIRST_NDP_TIMER);
  1336. if (!sc->p2p_ps_timer)
  1337. return -ENOMEM;
  1338. return 0;
  1339. }
  1340. void ath9k_deinit_p2p(struct ath_softc *sc)
  1341. {
  1342. if (sc->p2p_ps_timer)
  1343. ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
  1344. }
  1345. #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */