channel.c 42 KB

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