channel.c 41 KB

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