channel.c 38 KB

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