chan.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  1. /*
  2. * mac80211 - channel management
  3. */
  4. #include <linux/nl80211.h>
  5. #include <linux/export.h>
  6. #include <linux/rtnetlink.h>
  7. #include <net/cfg80211.h>
  8. #include "ieee80211_i.h"
  9. #include "driver-ops.h"
  10. static int ieee80211_chanctx_num_assigned(struct ieee80211_local *local,
  11. struct ieee80211_chanctx *ctx)
  12. {
  13. struct ieee80211_sub_if_data *sdata;
  14. int num = 0;
  15. lockdep_assert_held(&local->chanctx_mtx);
  16. list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
  17. num++;
  18. return num;
  19. }
  20. static int ieee80211_chanctx_num_reserved(struct ieee80211_local *local,
  21. struct ieee80211_chanctx *ctx)
  22. {
  23. struct ieee80211_sub_if_data *sdata;
  24. int num = 0;
  25. lockdep_assert_held(&local->chanctx_mtx);
  26. list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
  27. num++;
  28. return num;
  29. }
  30. int ieee80211_chanctx_refcount(struct ieee80211_local *local,
  31. struct ieee80211_chanctx *ctx)
  32. {
  33. return ieee80211_chanctx_num_assigned(local, ctx) +
  34. ieee80211_chanctx_num_reserved(local, ctx);
  35. }
  36. static int ieee80211_num_chanctx(struct ieee80211_local *local)
  37. {
  38. struct ieee80211_chanctx *ctx;
  39. int num = 0;
  40. lockdep_assert_held(&local->chanctx_mtx);
  41. list_for_each_entry(ctx, &local->chanctx_list, list)
  42. num++;
  43. return num;
  44. }
  45. static bool ieee80211_can_create_new_chanctx(struct ieee80211_local *local)
  46. {
  47. lockdep_assert_held(&local->chanctx_mtx);
  48. return ieee80211_num_chanctx(local) < ieee80211_max_num_channels(local);
  49. }
  50. static struct ieee80211_chanctx *
  51. ieee80211_vif_get_chanctx(struct ieee80211_sub_if_data *sdata)
  52. {
  53. struct ieee80211_local *local __maybe_unused = sdata->local;
  54. struct ieee80211_chanctx_conf *conf;
  55. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  56. lockdep_is_held(&local->chanctx_mtx));
  57. if (!conf)
  58. return NULL;
  59. return container_of(conf, struct ieee80211_chanctx, conf);
  60. }
  61. static const struct cfg80211_chan_def *
  62. ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
  63. struct ieee80211_chanctx *ctx,
  64. const struct cfg80211_chan_def *compat)
  65. {
  66. struct ieee80211_sub_if_data *sdata;
  67. lockdep_assert_held(&local->chanctx_mtx);
  68. list_for_each_entry(sdata, &ctx->reserved_vifs,
  69. reserved_chanctx_list) {
  70. if (!compat)
  71. compat = &sdata->reserved_chandef;
  72. compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
  73. compat);
  74. if (!compat)
  75. break;
  76. }
  77. return compat;
  78. }
  79. static const struct cfg80211_chan_def *
  80. ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
  81. struct ieee80211_chanctx *ctx,
  82. const struct cfg80211_chan_def *compat)
  83. {
  84. struct ieee80211_sub_if_data *sdata;
  85. lockdep_assert_held(&local->chanctx_mtx);
  86. list_for_each_entry(sdata, &ctx->assigned_vifs,
  87. assigned_chanctx_list) {
  88. if (sdata->reserved_chanctx != NULL)
  89. continue;
  90. if (!compat)
  91. compat = &sdata->vif.bss_conf.chandef;
  92. compat = cfg80211_chandef_compatible(
  93. &sdata->vif.bss_conf.chandef, compat);
  94. if (!compat)
  95. break;
  96. }
  97. return compat;
  98. }
  99. static const struct cfg80211_chan_def *
  100. ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
  101. struct ieee80211_chanctx *ctx,
  102. const struct cfg80211_chan_def *compat)
  103. {
  104. lockdep_assert_held(&local->chanctx_mtx);
  105. compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
  106. if (!compat)
  107. return NULL;
  108. compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
  109. if (!compat)
  110. return NULL;
  111. return compat;
  112. }
  113. static bool
  114. ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
  115. struct ieee80211_chanctx *ctx,
  116. const struct cfg80211_chan_def *def)
  117. {
  118. lockdep_assert_held(&local->chanctx_mtx);
  119. if (ieee80211_chanctx_combined_chandef(local, ctx, def))
  120. return true;
  121. if (!list_empty(&ctx->reserved_vifs) &&
  122. ieee80211_chanctx_reserved_chandef(local, ctx, def))
  123. return true;
  124. return false;
  125. }
  126. static struct ieee80211_chanctx *
  127. ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
  128. const struct cfg80211_chan_def *chandef,
  129. enum ieee80211_chanctx_mode mode)
  130. {
  131. struct ieee80211_chanctx *ctx;
  132. lockdep_assert_held(&local->chanctx_mtx);
  133. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  134. return NULL;
  135. list_for_each_entry(ctx, &local->chanctx_list, list) {
  136. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  137. continue;
  138. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  139. continue;
  140. if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
  141. chandef))
  142. continue;
  143. return ctx;
  144. }
  145. return NULL;
  146. }
  147. static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
  148. {
  149. switch (sta->bandwidth) {
  150. case IEEE80211_STA_RX_BW_20:
  151. if (sta->ht_cap.ht_supported)
  152. return NL80211_CHAN_WIDTH_20;
  153. else
  154. return NL80211_CHAN_WIDTH_20_NOHT;
  155. case IEEE80211_STA_RX_BW_40:
  156. return NL80211_CHAN_WIDTH_40;
  157. case IEEE80211_STA_RX_BW_80:
  158. return NL80211_CHAN_WIDTH_80;
  159. case IEEE80211_STA_RX_BW_160:
  160. /*
  161. * This applied for both 160 and 80+80. since we use
  162. * the returned value to consider degradation of
  163. * ctx->conf.min_def, we have to make sure to take
  164. * the bigger one (NL80211_CHAN_WIDTH_160).
  165. * Otherwise we might try degrading even when not
  166. * needed, as the max required sta_bw returned (80+80)
  167. * might be smaller than the configured bw (160).
  168. */
  169. return NL80211_CHAN_WIDTH_160;
  170. default:
  171. WARN_ON(1);
  172. return NL80211_CHAN_WIDTH_20;
  173. }
  174. }
  175. static enum nl80211_chan_width
  176. ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
  177. {
  178. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  179. struct sta_info *sta;
  180. rcu_read_lock();
  181. list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  182. if (sdata != sta->sdata &&
  183. !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
  184. continue;
  185. if (!sta->uploaded)
  186. continue;
  187. max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
  188. }
  189. rcu_read_unlock();
  190. return max_bw;
  191. }
  192. static enum nl80211_chan_width
  193. ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
  194. struct ieee80211_chanctx_conf *conf)
  195. {
  196. struct ieee80211_sub_if_data *sdata;
  197. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  198. rcu_read_lock();
  199. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  200. struct ieee80211_vif *vif = &sdata->vif;
  201. enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
  202. if (!ieee80211_sdata_running(sdata))
  203. continue;
  204. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  205. continue;
  206. switch (vif->type) {
  207. case NL80211_IFTYPE_AP:
  208. case NL80211_IFTYPE_AP_VLAN:
  209. width = ieee80211_get_max_required_bw(sdata);
  210. break;
  211. case NL80211_IFTYPE_P2P_DEVICE:
  212. continue;
  213. case NL80211_IFTYPE_STATION:
  214. case NL80211_IFTYPE_ADHOC:
  215. case NL80211_IFTYPE_WDS:
  216. case NL80211_IFTYPE_MESH_POINT:
  217. width = vif->bss_conf.chandef.width;
  218. break;
  219. case NL80211_IFTYPE_UNSPECIFIED:
  220. case NUM_NL80211_IFTYPES:
  221. case NL80211_IFTYPE_MONITOR:
  222. case NL80211_IFTYPE_P2P_CLIENT:
  223. case NL80211_IFTYPE_P2P_GO:
  224. WARN_ON_ONCE(1);
  225. }
  226. max_bw = max(max_bw, width);
  227. }
  228. /* use the configured bandwidth in case of monitor interface */
  229. sdata = rcu_dereference(local->monitor_sdata);
  230. if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
  231. max_bw = max(max_bw, conf->def.width);
  232. rcu_read_unlock();
  233. return max_bw;
  234. }
  235. /*
  236. * recalc the min required chan width of the channel context, which is
  237. * the max of min required widths of all the interfaces bound to this
  238. * channel context.
  239. */
  240. void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
  241. struct ieee80211_chanctx *ctx)
  242. {
  243. enum nl80211_chan_width max_bw;
  244. struct cfg80211_chan_def min_def;
  245. lockdep_assert_held(&local->chanctx_mtx);
  246. /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
  247. if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
  248. ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
  249. ctx->conf.radar_enabled) {
  250. ctx->conf.min_def = ctx->conf.def;
  251. return;
  252. }
  253. max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
  254. /* downgrade chandef up to max_bw */
  255. min_def = ctx->conf.def;
  256. while (min_def.width > max_bw)
  257. ieee80211_chandef_downgrade(&min_def);
  258. if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
  259. return;
  260. ctx->conf.min_def = min_def;
  261. if (!ctx->driver_present)
  262. return;
  263. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
  264. }
  265. static void ieee80211_change_chanctx(struct ieee80211_local *local,
  266. struct ieee80211_chanctx *ctx,
  267. const struct cfg80211_chan_def *chandef)
  268. {
  269. if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
  270. return;
  271. WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
  272. ctx->conf.def = *chandef;
  273. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
  274. ieee80211_recalc_chanctx_min_def(local, ctx);
  275. if (!local->use_chanctx) {
  276. local->_oper_chandef = *chandef;
  277. ieee80211_hw_config(local, 0);
  278. }
  279. }
  280. static struct ieee80211_chanctx *
  281. ieee80211_find_chanctx(struct ieee80211_local *local,
  282. const struct cfg80211_chan_def *chandef,
  283. enum ieee80211_chanctx_mode mode)
  284. {
  285. struct ieee80211_chanctx *ctx;
  286. lockdep_assert_held(&local->chanctx_mtx);
  287. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  288. return NULL;
  289. list_for_each_entry(ctx, &local->chanctx_list, list) {
  290. const struct cfg80211_chan_def *compat;
  291. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACE_NONE)
  292. continue;
  293. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  294. continue;
  295. compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
  296. if (!compat)
  297. continue;
  298. compat = ieee80211_chanctx_reserved_chandef(local, ctx,
  299. compat);
  300. if (!compat)
  301. continue;
  302. ieee80211_change_chanctx(local, ctx, compat);
  303. return ctx;
  304. }
  305. return NULL;
  306. }
  307. static bool ieee80211_is_radar_required(struct ieee80211_local *local)
  308. {
  309. struct ieee80211_sub_if_data *sdata;
  310. lockdep_assert_held(&local->mtx);
  311. rcu_read_lock();
  312. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  313. if (sdata->radar_required) {
  314. rcu_read_unlock();
  315. return true;
  316. }
  317. }
  318. rcu_read_unlock();
  319. return false;
  320. }
  321. static struct ieee80211_chanctx *
  322. ieee80211_alloc_chanctx(struct ieee80211_local *local,
  323. const struct cfg80211_chan_def *chandef,
  324. enum ieee80211_chanctx_mode mode)
  325. {
  326. struct ieee80211_chanctx *ctx;
  327. lockdep_assert_held(&local->chanctx_mtx);
  328. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  329. if (!ctx)
  330. return NULL;
  331. INIT_LIST_HEAD(&ctx->assigned_vifs);
  332. INIT_LIST_HEAD(&ctx->reserved_vifs);
  333. ctx->conf.def = *chandef;
  334. ctx->conf.rx_chains_static = 1;
  335. ctx->conf.rx_chains_dynamic = 1;
  336. ctx->mode = mode;
  337. ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
  338. ieee80211_recalc_chanctx_min_def(local, ctx);
  339. return ctx;
  340. }
  341. static int ieee80211_add_chanctx(struct ieee80211_local *local,
  342. struct ieee80211_chanctx *ctx)
  343. {
  344. u32 changed;
  345. int err;
  346. lockdep_assert_held(&local->mtx);
  347. lockdep_assert_held(&local->chanctx_mtx);
  348. if (!local->use_chanctx)
  349. local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
  350. /* turn idle off *before* setting channel -- some drivers need that */
  351. changed = ieee80211_idle_off(local);
  352. if (changed)
  353. ieee80211_hw_config(local, changed);
  354. if (!local->use_chanctx) {
  355. local->_oper_chandef = ctx->conf.def;
  356. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  357. } else {
  358. err = drv_add_chanctx(local, ctx);
  359. if (err) {
  360. ieee80211_recalc_idle(local);
  361. return err;
  362. }
  363. }
  364. return 0;
  365. }
  366. static struct ieee80211_chanctx *
  367. ieee80211_new_chanctx(struct ieee80211_local *local,
  368. const struct cfg80211_chan_def *chandef,
  369. enum ieee80211_chanctx_mode mode)
  370. {
  371. struct ieee80211_chanctx *ctx;
  372. int err;
  373. lockdep_assert_held(&local->mtx);
  374. lockdep_assert_held(&local->chanctx_mtx);
  375. ctx = ieee80211_alloc_chanctx(local, chandef, mode);
  376. if (!ctx)
  377. return ERR_PTR(-ENOMEM);
  378. err = ieee80211_add_chanctx(local, ctx);
  379. if (err) {
  380. kfree(ctx);
  381. return ERR_PTR(err);
  382. }
  383. list_add_rcu(&ctx->list, &local->chanctx_list);
  384. return ctx;
  385. }
  386. static void ieee80211_del_chanctx(struct ieee80211_local *local,
  387. struct ieee80211_chanctx *ctx)
  388. {
  389. lockdep_assert_held(&local->chanctx_mtx);
  390. if (!local->use_chanctx) {
  391. struct cfg80211_chan_def *chandef = &local->_oper_chandef;
  392. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  393. chandef->center_freq1 = chandef->chan->center_freq;
  394. chandef->center_freq2 = 0;
  395. /* NOTE: Disabling radar is only valid here for
  396. * single channel context. To be sure, check it ...
  397. */
  398. WARN_ON(local->hw.conf.radar_enabled &&
  399. !list_empty(&local->chanctx_list));
  400. local->hw.conf.radar_enabled = false;
  401. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  402. } else {
  403. drv_remove_chanctx(local, ctx);
  404. }
  405. ieee80211_recalc_idle(local);
  406. }
  407. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  408. struct ieee80211_chanctx *ctx)
  409. {
  410. lockdep_assert_held(&local->chanctx_mtx);
  411. WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
  412. list_del_rcu(&ctx->list);
  413. ieee80211_del_chanctx(local, ctx);
  414. kfree_rcu(ctx, rcu_head);
  415. }
  416. static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
  417. struct ieee80211_chanctx *ctx)
  418. {
  419. struct ieee80211_chanctx_conf *conf = &ctx->conf;
  420. struct ieee80211_sub_if_data *sdata;
  421. const struct cfg80211_chan_def *compat = NULL;
  422. lockdep_assert_held(&local->chanctx_mtx);
  423. rcu_read_lock();
  424. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  425. if (!ieee80211_sdata_running(sdata))
  426. continue;
  427. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  428. continue;
  429. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  430. continue;
  431. if (!compat)
  432. compat = &sdata->vif.bss_conf.chandef;
  433. compat = cfg80211_chandef_compatible(
  434. &sdata->vif.bss_conf.chandef, compat);
  435. if (WARN_ON_ONCE(!compat))
  436. break;
  437. }
  438. rcu_read_unlock();
  439. if (!compat)
  440. return;
  441. ieee80211_change_chanctx(local, ctx, compat);
  442. }
  443. static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
  444. struct ieee80211_chanctx *chanctx)
  445. {
  446. bool radar_enabled;
  447. lockdep_assert_held(&local->chanctx_mtx);
  448. /* for setting local->radar_detect_enabled */
  449. lockdep_assert_held(&local->mtx);
  450. radar_enabled = ieee80211_is_radar_required(local);
  451. if (radar_enabled == chanctx->conf.radar_enabled)
  452. return;
  453. chanctx->conf.radar_enabled = radar_enabled;
  454. local->radar_detect_enabled = chanctx->conf.radar_enabled;
  455. if (!local->use_chanctx) {
  456. local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
  457. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  458. }
  459. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
  460. }
  461. static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  462. struct ieee80211_chanctx *new_ctx)
  463. {
  464. struct ieee80211_local *local = sdata->local;
  465. struct ieee80211_chanctx_conf *conf;
  466. struct ieee80211_chanctx *curr_ctx = NULL;
  467. int ret = 0;
  468. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  469. lockdep_is_held(&local->chanctx_mtx));
  470. if (conf) {
  471. curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
  472. drv_unassign_vif_chanctx(local, sdata, curr_ctx);
  473. conf = NULL;
  474. list_del(&sdata->assigned_chanctx_list);
  475. }
  476. if (new_ctx) {
  477. ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
  478. if (ret)
  479. goto out;
  480. conf = &new_ctx->conf;
  481. list_add(&sdata->assigned_chanctx_list,
  482. &new_ctx->assigned_vifs);
  483. }
  484. out:
  485. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  486. sdata->vif.bss_conf.idle = !conf;
  487. if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
  488. ieee80211_recalc_chanctx_chantype(local, curr_ctx);
  489. ieee80211_recalc_smps_chanctx(local, curr_ctx);
  490. ieee80211_recalc_radar_chanctx(local, curr_ctx);
  491. ieee80211_recalc_chanctx_min_def(local, curr_ctx);
  492. }
  493. if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
  494. ieee80211_recalc_txpower(sdata);
  495. ieee80211_recalc_chanctx_min_def(local, new_ctx);
  496. }
  497. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  498. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  499. ieee80211_bss_info_change_notify(sdata,
  500. BSS_CHANGED_IDLE);
  501. return ret;
  502. }
  503. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  504. struct ieee80211_chanctx *chanctx)
  505. {
  506. struct ieee80211_sub_if_data *sdata;
  507. u8 rx_chains_static, rx_chains_dynamic;
  508. lockdep_assert_held(&local->chanctx_mtx);
  509. rx_chains_static = 1;
  510. rx_chains_dynamic = 1;
  511. rcu_read_lock();
  512. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  513. u8 needed_static, needed_dynamic;
  514. if (!ieee80211_sdata_running(sdata))
  515. continue;
  516. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  517. &chanctx->conf)
  518. continue;
  519. switch (sdata->vif.type) {
  520. case NL80211_IFTYPE_P2P_DEVICE:
  521. continue;
  522. case NL80211_IFTYPE_STATION:
  523. if (!sdata->u.mgd.associated)
  524. continue;
  525. break;
  526. case NL80211_IFTYPE_AP_VLAN:
  527. continue;
  528. case NL80211_IFTYPE_AP:
  529. case NL80211_IFTYPE_ADHOC:
  530. case NL80211_IFTYPE_WDS:
  531. case NL80211_IFTYPE_MESH_POINT:
  532. break;
  533. default:
  534. WARN_ON_ONCE(1);
  535. }
  536. switch (sdata->smps_mode) {
  537. default:
  538. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  539. sdata->smps_mode);
  540. /* fall through */
  541. case IEEE80211_SMPS_OFF:
  542. needed_static = sdata->needed_rx_chains;
  543. needed_dynamic = sdata->needed_rx_chains;
  544. break;
  545. case IEEE80211_SMPS_DYNAMIC:
  546. needed_static = 1;
  547. needed_dynamic = sdata->needed_rx_chains;
  548. break;
  549. case IEEE80211_SMPS_STATIC:
  550. needed_static = 1;
  551. needed_dynamic = 1;
  552. break;
  553. }
  554. rx_chains_static = max(rx_chains_static, needed_static);
  555. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  556. }
  557. /* Disable SMPS for the monitor interface */
  558. sdata = rcu_dereference(local->monitor_sdata);
  559. if (sdata &&
  560. rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
  561. rx_chains_dynamic = rx_chains_static = local->rx_chains;
  562. rcu_read_unlock();
  563. if (!local->use_chanctx) {
  564. if (rx_chains_static > 1)
  565. local->smps_mode = IEEE80211_SMPS_OFF;
  566. else if (rx_chains_dynamic > 1)
  567. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  568. else
  569. local->smps_mode = IEEE80211_SMPS_STATIC;
  570. ieee80211_hw_config(local, 0);
  571. }
  572. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  573. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  574. return;
  575. chanctx->conf.rx_chains_static = rx_chains_static;
  576. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  577. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  578. }
  579. static void
  580. __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  581. bool clear)
  582. {
  583. struct ieee80211_local *local __maybe_unused = sdata->local;
  584. struct ieee80211_sub_if_data *vlan;
  585. struct ieee80211_chanctx_conf *conf;
  586. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
  587. return;
  588. lockdep_assert_held(&local->mtx);
  589. /* Check that conf exists, even when clearing this function
  590. * must be called with the AP's channel context still there
  591. * as it would otherwise cause VLANs to have an invalid
  592. * channel context pointer for a while, possibly pointing
  593. * to a channel context that has already been freed.
  594. */
  595. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  596. lockdep_is_held(&local->chanctx_mtx));
  597. WARN_ON(!conf);
  598. if (clear)
  599. conf = NULL;
  600. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  601. rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
  602. }
  603. void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  604. bool clear)
  605. {
  606. struct ieee80211_local *local = sdata->local;
  607. mutex_lock(&local->chanctx_mtx);
  608. __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
  609. mutex_unlock(&local->chanctx_mtx);
  610. }
  611. int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
  612. {
  613. struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
  614. lockdep_assert_held(&sdata->local->chanctx_mtx);
  615. if (WARN_ON(!ctx))
  616. return -EINVAL;
  617. list_del(&sdata->reserved_chanctx_list);
  618. sdata->reserved_chanctx = NULL;
  619. if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
  620. if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
  621. if (WARN_ON(!ctx->replace_ctx))
  622. return -EINVAL;
  623. WARN_ON(ctx->replace_ctx->replace_state !=
  624. IEEE80211_CHANCTX_WILL_BE_REPLACED);
  625. WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
  626. ctx->replace_ctx->replace_ctx = NULL;
  627. ctx->replace_ctx->replace_state =
  628. IEEE80211_CHANCTX_REPLACE_NONE;
  629. list_del_rcu(&ctx->list);
  630. kfree_rcu(ctx, rcu_head);
  631. } else {
  632. ieee80211_free_chanctx(sdata->local, ctx);
  633. }
  634. }
  635. return 0;
  636. }
  637. int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
  638. const struct cfg80211_chan_def *chandef,
  639. enum ieee80211_chanctx_mode mode,
  640. bool radar_required)
  641. {
  642. struct ieee80211_local *local = sdata->local;
  643. struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
  644. lockdep_assert_held(&local->chanctx_mtx);
  645. curr_ctx = ieee80211_vif_get_chanctx(sdata);
  646. if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
  647. return -ENOTSUPP;
  648. new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
  649. if (!new_ctx) {
  650. if (ieee80211_can_create_new_chanctx(local)) {
  651. new_ctx = ieee80211_new_chanctx(local, chandef, mode);
  652. if (IS_ERR(new_ctx))
  653. return PTR_ERR(new_ctx);
  654. } else {
  655. if (!curr_ctx ||
  656. (curr_ctx->replace_state ==
  657. IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
  658. !list_empty(&curr_ctx->reserved_vifs)) {
  659. /*
  660. * Another vif already requested this context
  661. * for a reservation. Find another one hoping
  662. * all vifs assigned to it will also switch
  663. * soon enough.
  664. *
  665. * TODO: This needs a little more work as some
  666. * cases (more than 2 chanctx capable devices)
  667. * may fail which could otherwise succeed
  668. * provided some channel context juggling was
  669. * performed.
  670. *
  671. * Consider ctx1..3, vif1..6, each ctx has 2
  672. * vifs. vif1 and vif2 from ctx1 request new
  673. * different chandefs starting 2 in-place
  674. * reserations with ctx4 and ctx5 replacing
  675. * ctx1 and ctx2 respectively. Next vif5 and
  676. * vif6 from ctx3 reserve ctx4. If vif3 and
  677. * vif4 remain on ctx2 as they are then this
  678. * fails unless `replace_ctx` from ctx5 is
  679. * replaced with ctx3.
  680. */
  681. list_for_each_entry(ctx, &local->chanctx_list,
  682. list) {
  683. if (ctx->replace_state !=
  684. IEEE80211_CHANCTX_REPLACE_NONE)
  685. continue;
  686. if (!list_empty(&ctx->reserved_vifs))
  687. continue;
  688. curr_ctx = ctx;
  689. break;
  690. }
  691. }
  692. /*
  693. * If that's true then all available contexts already
  694. * have reservations and cannot be used.
  695. */
  696. if (!curr_ctx ||
  697. (curr_ctx->replace_state ==
  698. IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
  699. !list_empty(&curr_ctx->reserved_vifs))
  700. return -EBUSY;
  701. new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
  702. if (!new_ctx)
  703. return -ENOMEM;
  704. new_ctx->replace_ctx = curr_ctx;
  705. new_ctx->replace_state =
  706. IEEE80211_CHANCTX_REPLACES_OTHER;
  707. curr_ctx->replace_ctx = new_ctx;
  708. curr_ctx->replace_state =
  709. IEEE80211_CHANCTX_WILL_BE_REPLACED;
  710. list_add_rcu(&new_ctx->list, &local->chanctx_list);
  711. }
  712. }
  713. list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
  714. sdata->reserved_chanctx = new_ctx;
  715. sdata->reserved_chandef = *chandef;
  716. sdata->reserved_radar_required = radar_required;
  717. sdata->reserved_ready = false;
  718. return 0;
  719. }
  720. static void
  721. ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
  722. {
  723. switch (sdata->vif.type) {
  724. case NL80211_IFTYPE_ADHOC:
  725. case NL80211_IFTYPE_AP:
  726. case NL80211_IFTYPE_MESH_POINT:
  727. ieee80211_queue_work(&sdata->local->hw,
  728. &sdata->csa_finalize_work);
  729. break;
  730. case NL80211_IFTYPE_STATION:
  731. ieee80211_queue_work(&sdata->local->hw,
  732. &sdata->u.mgd.chswitch_work);
  733. break;
  734. case NL80211_IFTYPE_UNSPECIFIED:
  735. case NL80211_IFTYPE_AP_VLAN:
  736. case NL80211_IFTYPE_WDS:
  737. case NL80211_IFTYPE_MONITOR:
  738. case NL80211_IFTYPE_P2P_CLIENT:
  739. case NL80211_IFTYPE_P2P_GO:
  740. case NL80211_IFTYPE_P2P_DEVICE:
  741. case NUM_NL80211_IFTYPES:
  742. WARN_ON(1);
  743. break;
  744. }
  745. }
  746. static int
  747. ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
  748. {
  749. struct ieee80211_local *local = sdata->local;
  750. struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
  751. struct ieee80211_chanctx *old_ctx, *new_ctx;
  752. const struct cfg80211_chan_def *chandef;
  753. u32 changed = 0;
  754. int err;
  755. lockdep_assert_held(&local->mtx);
  756. lockdep_assert_held(&local->chanctx_mtx);
  757. new_ctx = sdata->reserved_chanctx;
  758. old_ctx = ieee80211_vif_get_chanctx(sdata);
  759. if (WARN_ON(!sdata->reserved_ready))
  760. return -EBUSY;
  761. if (WARN_ON(!new_ctx))
  762. return -EINVAL;
  763. if (WARN_ON(!old_ctx))
  764. return -EINVAL;
  765. if (WARN_ON(new_ctx->replace_state ==
  766. IEEE80211_CHANCTX_REPLACES_OTHER))
  767. return -EINVAL;
  768. chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
  769. &sdata->reserved_chandef);
  770. if (WARN_ON(!chandef))
  771. return -EINVAL;
  772. vif_chsw[0].vif = &sdata->vif;
  773. vif_chsw[0].old_ctx = &old_ctx->conf;
  774. vif_chsw[0].new_ctx = &new_ctx->conf;
  775. list_del(&sdata->reserved_chanctx_list);
  776. sdata->reserved_chanctx = NULL;
  777. err = drv_switch_vif_chanctx(local, vif_chsw, 1,
  778. CHANCTX_SWMODE_REASSIGN_VIF);
  779. if (err) {
  780. if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
  781. ieee80211_free_chanctx(local, new_ctx);
  782. goto out;
  783. }
  784. list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
  785. rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
  786. if (sdata->vif.type == NL80211_IFTYPE_AP)
  787. __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
  788. if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
  789. ieee80211_free_chanctx(local, old_ctx);
  790. if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
  791. changed = BSS_CHANGED_BANDWIDTH;
  792. sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
  793. if (changed)
  794. ieee80211_bss_info_change_notify(sdata, changed);
  795. out:
  796. ieee80211_vif_chanctx_reservation_complete(sdata);
  797. return err;
  798. }
  799. static int
  800. ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
  801. {
  802. struct ieee80211_local *local = sdata->local;
  803. struct ieee80211_chanctx *old_ctx, *new_ctx;
  804. const struct cfg80211_chan_def *chandef;
  805. int err;
  806. old_ctx = ieee80211_vif_get_chanctx(sdata);
  807. new_ctx = sdata->reserved_chanctx;
  808. if (WARN_ON(!sdata->reserved_ready))
  809. return -EINVAL;
  810. if (WARN_ON(old_ctx))
  811. return -EINVAL;
  812. if (WARN_ON(!new_ctx))
  813. return -EINVAL;
  814. if (WARN_ON(new_ctx->replace_state ==
  815. IEEE80211_CHANCTX_REPLACES_OTHER))
  816. return -EINVAL;
  817. chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
  818. &sdata->reserved_chandef);
  819. if (WARN_ON(!chandef))
  820. return -EINVAL;
  821. list_del(&sdata->reserved_chanctx_list);
  822. sdata->reserved_chanctx = NULL;
  823. err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
  824. if (err) {
  825. if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
  826. ieee80211_free_chanctx(local, new_ctx);
  827. goto out;
  828. }
  829. out:
  830. ieee80211_vif_chanctx_reservation_complete(sdata);
  831. return err;
  832. }
  833. static bool
  834. ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
  835. {
  836. struct ieee80211_chanctx *old_ctx, *new_ctx;
  837. lockdep_assert_held(&sdata->local->chanctx_mtx);
  838. new_ctx = sdata->reserved_chanctx;
  839. old_ctx = ieee80211_vif_get_chanctx(sdata);
  840. if (!old_ctx)
  841. return false;
  842. if (WARN_ON(!new_ctx))
  843. return false;
  844. if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
  845. return false;
  846. if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  847. return false;
  848. return true;
  849. }
  850. static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
  851. struct ieee80211_chanctx *new_ctx)
  852. {
  853. const struct cfg80211_chan_def *chandef;
  854. lockdep_assert_held(&local->mtx);
  855. lockdep_assert_held(&local->chanctx_mtx);
  856. chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
  857. if (WARN_ON(!chandef))
  858. return -EINVAL;
  859. local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
  860. local->_oper_chandef = *chandef;
  861. ieee80211_hw_config(local, 0);
  862. return 0;
  863. }
  864. static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
  865. int n_vifs)
  866. {
  867. struct ieee80211_vif_chanctx_switch *vif_chsw;
  868. struct ieee80211_sub_if_data *sdata;
  869. struct ieee80211_chanctx *ctx, *old_ctx;
  870. int i, err;
  871. lockdep_assert_held(&local->mtx);
  872. lockdep_assert_held(&local->chanctx_mtx);
  873. vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
  874. if (!vif_chsw)
  875. return -ENOMEM;
  876. i = 0;
  877. list_for_each_entry(ctx, &local->chanctx_list, list) {
  878. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  879. continue;
  880. if (WARN_ON(!ctx->replace_ctx)) {
  881. err = -EINVAL;
  882. goto out;
  883. }
  884. list_for_each_entry(sdata, &ctx->reserved_vifs,
  885. reserved_chanctx_list) {
  886. if (!ieee80211_vif_has_in_place_reservation(
  887. sdata))
  888. continue;
  889. old_ctx = ieee80211_vif_get_chanctx(sdata);
  890. vif_chsw[i].vif = &sdata->vif;
  891. vif_chsw[i].old_ctx = &old_ctx->conf;
  892. vif_chsw[i].new_ctx = &ctx->conf;
  893. i++;
  894. }
  895. }
  896. err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
  897. CHANCTX_SWMODE_SWAP_CONTEXTS);
  898. out:
  899. kfree(vif_chsw);
  900. return err;
  901. }
  902. static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
  903. {
  904. struct ieee80211_chanctx *ctx;
  905. int err;
  906. lockdep_assert_held(&local->mtx);
  907. lockdep_assert_held(&local->chanctx_mtx);
  908. list_for_each_entry(ctx, &local->chanctx_list, list) {
  909. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  910. continue;
  911. if (!list_empty(&ctx->replace_ctx->assigned_vifs))
  912. continue;
  913. ieee80211_del_chanctx(local, ctx->replace_ctx);
  914. err = ieee80211_add_chanctx(local, ctx);
  915. if (err)
  916. goto err;
  917. }
  918. return 0;
  919. err:
  920. WARN_ON(ieee80211_add_chanctx(local, ctx));
  921. list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
  922. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  923. continue;
  924. if (!list_empty(&ctx->replace_ctx->assigned_vifs))
  925. continue;
  926. ieee80211_del_chanctx(local, ctx);
  927. WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
  928. }
  929. return err;
  930. }
  931. static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
  932. {
  933. struct ieee80211_sub_if_data *sdata, *sdata_tmp;
  934. struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
  935. struct ieee80211_chanctx *new_ctx = NULL;
  936. int i, err, n_assigned, n_reserved, n_ready;
  937. int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
  938. lockdep_assert_held(&local->mtx);
  939. lockdep_assert_held(&local->chanctx_mtx);
  940. /*
  941. * If there are 2 independent pairs of channel contexts performing
  942. * cross-switch of their vifs this code will still wait until both are
  943. * ready even though it could be possible to switch one before the
  944. * other is ready.
  945. *
  946. * For practical reasons and code simplicity just do a single huge
  947. * switch.
  948. */
  949. /*
  950. * Verify if the reservation is still feasible.
  951. * - if it's not then disconnect
  952. * - if it is but not all vifs necessary are ready then defer
  953. */
  954. list_for_each_entry(ctx, &local->chanctx_list, list) {
  955. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  956. continue;
  957. if (WARN_ON(!ctx->replace_ctx)) {
  958. err = -EINVAL;
  959. goto err;
  960. }
  961. if (!local->use_chanctx)
  962. new_ctx = ctx;
  963. n_ctx++;
  964. n_assigned = 0;
  965. n_reserved = 0;
  966. n_ready = 0;
  967. list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
  968. assigned_chanctx_list) {
  969. n_assigned++;
  970. if (sdata->reserved_chanctx) {
  971. n_reserved++;
  972. if (sdata->reserved_ready)
  973. n_ready++;
  974. }
  975. }
  976. if (n_assigned != n_reserved) {
  977. if (n_ready == n_reserved) {
  978. wiphy_info(local->hw.wiphy,
  979. "channel context reservation cannot be finalized because some interfaces aren't switching\n");
  980. err = -EBUSY;
  981. goto err;
  982. }
  983. return -EAGAIN;
  984. }
  985. ctx->conf.radar_enabled = false;
  986. list_for_each_entry(sdata, &ctx->reserved_vifs,
  987. reserved_chanctx_list) {
  988. if (ieee80211_vif_has_in_place_reservation(sdata) &&
  989. !sdata->reserved_ready)
  990. return -EAGAIN;
  991. old_ctx = ieee80211_vif_get_chanctx(sdata);
  992. if (old_ctx) {
  993. if (old_ctx->replace_state ==
  994. IEEE80211_CHANCTX_WILL_BE_REPLACED)
  995. n_vifs_switch++;
  996. else
  997. n_vifs_assign++;
  998. } else {
  999. n_vifs_ctxless++;
  1000. }
  1001. if (sdata->reserved_radar_required)
  1002. ctx->conf.radar_enabled = true;
  1003. }
  1004. }
  1005. if (WARN_ON(n_ctx == 0) ||
  1006. WARN_ON(n_vifs_switch == 0 &&
  1007. n_vifs_assign == 0 &&
  1008. n_vifs_ctxless == 0) ||
  1009. WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
  1010. WARN_ON(!new_ctx && !local->use_chanctx)) {
  1011. err = -EINVAL;
  1012. goto err;
  1013. }
  1014. /*
  1015. * All necessary vifs are ready. Perform the switch now depending on
  1016. * reservations and driver capabilities.
  1017. */
  1018. if (local->use_chanctx) {
  1019. if (n_vifs_switch > 0) {
  1020. err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
  1021. if (err)
  1022. goto err;
  1023. }
  1024. if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
  1025. err = ieee80211_chsw_switch_ctxs(local);
  1026. if (err)
  1027. goto err;
  1028. }
  1029. } else {
  1030. err = ieee80211_chsw_switch_hwconf(local, new_ctx);
  1031. if (err)
  1032. goto err;
  1033. }
  1034. /*
  1035. * Update all structures, values and pointers to point to new channel
  1036. * context(s).
  1037. */
  1038. i = 0;
  1039. list_for_each_entry(ctx, &local->chanctx_list, list) {
  1040. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  1041. continue;
  1042. if (WARN_ON(!ctx->replace_ctx)) {
  1043. err = -EINVAL;
  1044. goto err;
  1045. }
  1046. list_for_each_entry(sdata, &ctx->reserved_vifs,
  1047. reserved_chanctx_list) {
  1048. u32 changed = 0;
  1049. if (!ieee80211_vif_has_in_place_reservation(sdata))
  1050. continue;
  1051. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  1052. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1053. __ieee80211_vif_copy_chanctx_to_vlans(sdata,
  1054. false);
  1055. sdata->radar_required = sdata->reserved_radar_required;
  1056. if (sdata->vif.bss_conf.chandef.width !=
  1057. sdata->reserved_chandef.width)
  1058. changed = BSS_CHANGED_BANDWIDTH;
  1059. sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
  1060. if (changed)
  1061. ieee80211_bss_info_change_notify(sdata,
  1062. changed);
  1063. ieee80211_recalc_txpower(sdata);
  1064. }
  1065. ieee80211_recalc_chanctx_chantype(local, ctx);
  1066. ieee80211_recalc_smps_chanctx(local, ctx);
  1067. ieee80211_recalc_radar_chanctx(local, ctx);
  1068. ieee80211_recalc_chanctx_min_def(local, ctx);
  1069. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1070. reserved_chanctx_list) {
  1071. if (ieee80211_vif_get_chanctx(sdata) != ctx)
  1072. continue;
  1073. list_del(&sdata->reserved_chanctx_list);
  1074. list_move(&sdata->assigned_chanctx_list,
  1075. &ctx->assigned_vifs);
  1076. sdata->reserved_chanctx = NULL;
  1077. ieee80211_vif_chanctx_reservation_complete(sdata);
  1078. }
  1079. /*
  1080. * This context might have been a dependency for an already
  1081. * ready re-assign reservation interface that was deferred. Do
  1082. * not propagate error to the caller though. The in-place
  1083. * reservation for originally requested interface has already
  1084. * succeeded at this point.
  1085. */
  1086. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1087. reserved_chanctx_list) {
  1088. if (WARN_ON(ieee80211_vif_has_in_place_reservation(
  1089. sdata)))
  1090. continue;
  1091. if (WARN_ON(sdata->reserved_chanctx != ctx))
  1092. continue;
  1093. if (!sdata->reserved_ready)
  1094. continue;
  1095. if (ieee80211_vif_get_chanctx(sdata))
  1096. err = ieee80211_vif_use_reserved_reassign(
  1097. sdata);
  1098. else
  1099. err = ieee80211_vif_use_reserved_assign(sdata);
  1100. if (err) {
  1101. sdata_info(sdata,
  1102. "failed to finalize (re-)assign reservation (err=%d)\n",
  1103. err);
  1104. ieee80211_vif_unreserve_chanctx(sdata);
  1105. cfg80211_stop_iface(local->hw.wiphy,
  1106. &sdata->wdev,
  1107. GFP_KERNEL);
  1108. }
  1109. }
  1110. }
  1111. /*
  1112. * Finally free old contexts
  1113. */
  1114. list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
  1115. if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
  1116. continue;
  1117. ctx->replace_ctx->replace_ctx = NULL;
  1118. ctx->replace_ctx->replace_state =
  1119. IEEE80211_CHANCTX_REPLACE_NONE;
  1120. list_del_rcu(&ctx->list);
  1121. kfree_rcu(ctx, rcu_head);
  1122. }
  1123. return 0;
  1124. err:
  1125. list_for_each_entry(ctx, &local->chanctx_list, list) {
  1126. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  1127. continue;
  1128. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1129. reserved_chanctx_list) {
  1130. ieee80211_vif_unreserve_chanctx(sdata);
  1131. ieee80211_vif_chanctx_reservation_complete(sdata);
  1132. }
  1133. }
  1134. return err;
  1135. }
  1136. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  1137. {
  1138. struct ieee80211_local *local = sdata->local;
  1139. struct ieee80211_chanctx_conf *conf;
  1140. struct ieee80211_chanctx *ctx;
  1141. bool use_reserved_switch = false;
  1142. lockdep_assert_held(&local->chanctx_mtx);
  1143. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1144. lockdep_is_held(&local->chanctx_mtx));
  1145. if (!conf)
  1146. return;
  1147. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1148. if (sdata->reserved_chanctx) {
  1149. if (sdata->reserved_chanctx->replace_state ==
  1150. IEEE80211_CHANCTX_REPLACES_OTHER &&
  1151. ieee80211_chanctx_num_reserved(local,
  1152. sdata->reserved_chanctx) > 1)
  1153. use_reserved_switch = true;
  1154. ieee80211_vif_unreserve_chanctx(sdata);
  1155. }
  1156. ieee80211_assign_vif_chanctx(sdata, NULL);
  1157. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  1158. ieee80211_free_chanctx(local, ctx);
  1159. /* Unreserving may ready an in-place reservation. */
  1160. if (use_reserved_switch)
  1161. ieee80211_vif_use_reserved_switch(local);
  1162. }
  1163. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  1164. const struct cfg80211_chan_def *chandef,
  1165. enum ieee80211_chanctx_mode mode)
  1166. {
  1167. struct ieee80211_local *local = sdata->local;
  1168. struct ieee80211_chanctx *ctx;
  1169. u8 radar_detect_width = 0;
  1170. int ret;
  1171. lockdep_assert_held(&local->mtx);
  1172. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  1173. mutex_lock(&local->chanctx_mtx);
  1174. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1175. chandef,
  1176. sdata->wdev.iftype);
  1177. if (ret < 0)
  1178. goto out;
  1179. if (ret > 0)
  1180. radar_detect_width = BIT(chandef->width);
  1181. sdata->radar_required = ret;
  1182. ret = ieee80211_check_combinations(sdata, chandef, mode,
  1183. radar_detect_width);
  1184. if (ret < 0)
  1185. goto out;
  1186. __ieee80211_vif_release_channel(sdata);
  1187. ctx = ieee80211_find_chanctx(local, chandef, mode);
  1188. if (!ctx)
  1189. ctx = ieee80211_new_chanctx(local, chandef, mode);
  1190. if (IS_ERR(ctx)) {
  1191. ret = PTR_ERR(ctx);
  1192. goto out;
  1193. }
  1194. sdata->vif.bss_conf.chandef = *chandef;
  1195. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  1196. if (ret) {
  1197. /* if assign fails refcount stays the same */
  1198. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  1199. ieee80211_free_chanctx(local, ctx);
  1200. goto out;
  1201. }
  1202. ieee80211_recalc_smps_chanctx(local, ctx);
  1203. ieee80211_recalc_radar_chanctx(local, ctx);
  1204. out:
  1205. mutex_unlock(&local->chanctx_mtx);
  1206. return ret;
  1207. }
  1208. int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
  1209. {
  1210. struct ieee80211_local *local = sdata->local;
  1211. struct ieee80211_chanctx *new_ctx;
  1212. struct ieee80211_chanctx *old_ctx;
  1213. int err;
  1214. lockdep_assert_held(&local->mtx);
  1215. lockdep_assert_held(&local->chanctx_mtx);
  1216. new_ctx = sdata->reserved_chanctx;
  1217. old_ctx = ieee80211_vif_get_chanctx(sdata);
  1218. if (WARN_ON(!new_ctx))
  1219. return -EINVAL;
  1220. if (WARN_ON(new_ctx->replace_state ==
  1221. IEEE80211_CHANCTX_WILL_BE_REPLACED))
  1222. return -EINVAL;
  1223. if (WARN_ON(sdata->reserved_ready))
  1224. return -EINVAL;
  1225. sdata->reserved_ready = true;
  1226. if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
  1227. if (old_ctx)
  1228. err = ieee80211_vif_use_reserved_reassign(sdata);
  1229. else
  1230. err = ieee80211_vif_use_reserved_assign(sdata);
  1231. if (err)
  1232. return err;
  1233. }
  1234. /*
  1235. * In-place reservation may need to be finalized now either if:
  1236. * a) sdata is taking part in the swapping itself and is the last one
  1237. * b) sdata has switched with a re-assign reservation to an existing
  1238. * context readying in-place switching of old_ctx
  1239. *
  1240. * In case of (b) do not propagate the error up because the requested
  1241. * sdata already switched successfully. Just spill an extra warning.
  1242. * The ieee80211_vif_use_reserved_switch() already stops all necessary
  1243. * interfaces upon failure.
  1244. */
  1245. if ((old_ctx &&
  1246. old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
  1247. new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
  1248. err = ieee80211_vif_use_reserved_switch(local);
  1249. if (err && err != -EAGAIN) {
  1250. if (new_ctx->replace_state ==
  1251. IEEE80211_CHANCTX_REPLACES_OTHER)
  1252. return err;
  1253. wiphy_info(local->hw.wiphy,
  1254. "depending in-place reservation failed (err=%d)\n",
  1255. err);
  1256. }
  1257. }
  1258. return 0;
  1259. }
  1260. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  1261. const struct cfg80211_chan_def *chandef,
  1262. u32 *changed)
  1263. {
  1264. struct ieee80211_local *local = sdata->local;
  1265. struct ieee80211_chanctx_conf *conf;
  1266. struct ieee80211_chanctx *ctx;
  1267. const struct cfg80211_chan_def *compat;
  1268. int ret;
  1269. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  1270. IEEE80211_CHAN_DISABLED))
  1271. return -EINVAL;
  1272. mutex_lock(&local->chanctx_mtx);
  1273. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  1274. ret = 0;
  1275. goto out;
  1276. }
  1277. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  1278. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  1279. ret = -EINVAL;
  1280. goto out;
  1281. }
  1282. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1283. lockdep_is_held(&local->chanctx_mtx));
  1284. if (!conf) {
  1285. ret = -EINVAL;
  1286. goto out;
  1287. }
  1288. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1289. compat = cfg80211_chandef_compatible(&conf->def, chandef);
  1290. if (!compat) {
  1291. ret = -EINVAL;
  1292. goto out;
  1293. }
  1294. switch (ctx->replace_state) {
  1295. case IEEE80211_CHANCTX_REPLACE_NONE:
  1296. if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
  1297. ret = -EBUSY;
  1298. goto out;
  1299. }
  1300. break;
  1301. case IEEE80211_CHANCTX_WILL_BE_REPLACED:
  1302. /* TODO: Perhaps the bandwith change could be treated as a
  1303. * reservation itself? */
  1304. ret = -EBUSY;
  1305. goto out;
  1306. case IEEE80211_CHANCTX_REPLACES_OTHER:
  1307. /* channel context that is going to replace another channel
  1308. * context doesn't really exist and shouldn't be assigned
  1309. * anywhere yet */
  1310. WARN_ON(1);
  1311. break;
  1312. }
  1313. sdata->vif.bss_conf.chandef = *chandef;
  1314. ieee80211_recalc_chanctx_chantype(local, ctx);
  1315. *changed |= BSS_CHANGED_BANDWIDTH;
  1316. ret = 0;
  1317. out:
  1318. mutex_unlock(&local->chanctx_mtx);
  1319. return ret;
  1320. }
  1321. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  1322. {
  1323. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  1324. lockdep_assert_held(&sdata->local->mtx);
  1325. mutex_lock(&sdata->local->chanctx_mtx);
  1326. __ieee80211_vif_release_channel(sdata);
  1327. mutex_unlock(&sdata->local->chanctx_mtx);
  1328. }
  1329. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  1330. {
  1331. struct ieee80211_local *local = sdata->local;
  1332. struct ieee80211_sub_if_data *ap;
  1333. struct ieee80211_chanctx_conf *conf;
  1334. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  1335. return;
  1336. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  1337. mutex_lock(&local->chanctx_mtx);
  1338. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  1339. lockdep_is_held(&local->chanctx_mtx));
  1340. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  1341. mutex_unlock(&local->chanctx_mtx);
  1342. }
  1343. void ieee80211_iter_chan_contexts_atomic(
  1344. struct ieee80211_hw *hw,
  1345. void (*iter)(struct ieee80211_hw *hw,
  1346. struct ieee80211_chanctx_conf *chanctx_conf,
  1347. void *data),
  1348. void *iter_data)
  1349. {
  1350. struct ieee80211_local *local = hw_to_local(hw);
  1351. struct ieee80211_chanctx *ctx;
  1352. rcu_read_lock();
  1353. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  1354. if (ctx->driver_present)
  1355. iter(hw, &ctx->conf, iter_data);
  1356. rcu_read_unlock();
  1357. }
  1358. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);