chan.c 43 KB

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