chan.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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 void
  747. ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
  748. const struct cfg80211_chan_def *chandef)
  749. {
  750. struct ieee80211_sub_if_data *vlan;
  751. sdata->vif.bss_conf.chandef = *chandef;
  752. if (sdata->vif.type != NL80211_IFTYPE_AP)
  753. return;
  754. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  755. vlan->vif.bss_conf.chandef = *chandef;
  756. }
  757. static int
  758. ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
  759. {
  760. struct ieee80211_local *local = sdata->local;
  761. struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
  762. struct ieee80211_chanctx *old_ctx, *new_ctx;
  763. const struct cfg80211_chan_def *chandef;
  764. u32 changed = 0;
  765. int err;
  766. lockdep_assert_held(&local->mtx);
  767. lockdep_assert_held(&local->chanctx_mtx);
  768. new_ctx = sdata->reserved_chanctx;
  769. old_ctx = ieee80211_vif_get_chanctx(sdata);
  770. if (WARN_ON(!sdata->reserved_ready))
  771. return -EBUSY;
  772. if (WARN_ON(!new_ctx))
  773. return -EINVAL;
  774. if (WARN_ON(!old_ctx))
  775. return -EINVAL;
  776. if (WARN_ON(new_ctx->replace_state ==
  777. IEEE80211_CHANCTX_REPLACES_OTHER))
  778. return -EINVAL;
  779. chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
  780. &sdata->reserved_chandef);
  781. if (WARN_ON(!chandef))
  782. return -EINVAL;
  783. vif_chsw[0].vif = &sdata->vif;
  784. vif_chsw[0].old_ctx = &old_ctx->conf;
  785. vif_chsw[0].new_ctx = &new_ctx->conf;
  786. list_del(&sdata->reserved_chanctx_list);
  787. sdata->reserved_chanctx = NULL;
  788. err = drv_switch_vif_chanctx(local, vif_chsw, 1,
  789. CHANCTX_SWMODE_REASSIGN_VIF);
  790. if (err) {
  791. if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
  792. ieee80211_free_chanctx(local, new_ctx);
  793. goto out;
  794. }
  795. list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
  796. rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
  797. if (sdata->vif.type == NL80211_IFTYPE_AP)
  798. __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
  799. if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
  800. ieee80211_free_chanctx(local, old_ctx);
  801. if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
  802. changed = BSS_CHANGED_BANDWIDTH;
  803. ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
  804. if (changed)
  805. ieee80211_bss_info_change_notify(sdata, changed);
  806. out:
  807. ieee80211_vif_chanctx_reservation_complete(sdata);
  808. return err;
  809. }
  810. static int
  811. ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
  812. {
  813. struct ieee80211_local *local = sdata->local;
  814. struct ieee80211_chanctx *old_ctx, *new_ctx;
  815. const struct cfg80211_chan_def *chandef;
  816. int err;
  817. old_ctx = ieee80211_vif_get_chanctx(sdata);
  818. new_ctx = sdata->reserved_chanctx;
  819. if (WARN_ON(!sdata->reserved_ready))
  820. return -EINVAL;
  821. if (WARN_ON(old_ctx))
  822. return -EINVAL;
  823. if (WARN_ON(!new_ctx))
  824. return -EINVAL;
  825. if (WARN_ON(new_ctx->replace_state ==
  826. IEEE80211_CHANCTX_REPLACES_OTHER))
  827. return -EINVAL;
  828. chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
  829. &sdata->reserved_chandef);
  830. if (WARN_ON(!chandef))
  831. return -EINVAL;
  832. list_del(&sdata->reserved_chanctx_list);
  833. sdata->reserved_chanctx = NULL;
  834. err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
  835. if (err) {
  836. if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
  837. ieee80211_free_chanctx(local, new_ctx);
  838. goto out;
  839. }
  840. out:
  841. ieee80211_vif_chanctx_reservation_complete(sdata);
  842. return err;
  843. }
  844. static bool
  845. ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
  846. {
  847. struct ieee80211_chanctx *old_ctx, *new_ctx;
  848. lockdep_assert_held(&sdata->local->chanctx_mtx);
  849. new_ctx = sdata->reserved_chanctx;
  850. old_ctx = ieee80211_vif_get_chanctx(sdata);
  851. if (!old_ctx)
  852. return false;
  853. if (WARN_ON(!new_ctx))
  854. return false;
  855. if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
  856. return false;
  857. if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  858. return false;
  859. return true;
  860. }
  861. static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
  862. struct ieee80211_chanctx *new_ctx)
  863. {
  864. const struct cfg80211_chan_def *chandef;
  865. lockdep_assert_held(&local->mtx);
  866. lockdep_assert_held(&local->chanctx_mtx);
  867. chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
  868. if (WARN_ON(!chandef))
  869. return -EINVAL;
  870. local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
  871. local->_oper_chandef = *chandef;
  872. ieee80211_hw_config(local, 0);
  873. return 0;
  874. }
  875. static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
  876. int n_vifs)
  877. {
  878. struct ieee80211_vif_chanctx_switch *vif_chsw;
  879. struct ieee80211_sub_if_data *sdata;
  880. struct ieee80211_chanctx *ctx, *old_ctx;
  881. int i, err;
  882. lockdep_assert_held(&local->mtx);
  883. lockdep_assert_held(&local->chanctx_mtx);
  884. vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
  885. if (!vif_chsw)
  886. return -ENOMEM;
  887. i = 0;
  888. list_for_each_entry(ctx, &local->chanctx_list, list) {
  889. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  890. continue;
  891. if (WARN_ON(!ctx->replace_ctx)) {
  892. err = -EINVAL;
  893. goto out;
  894. }
  895. list_for_each_entry(sdata, &ctx->reserved_vifs,
  896. reserved_chanctx_list) {
  897. if (!ieee80211_vif_has_in_place_reservation(
  898. sdata))
  899. continue;
  900. old_ctx = ieee80211_vif_get_chanctx(sdata);
  901. vif_chsw[i].vif = &sdata->vif;
  902. vif_chsw[i].old_ctx = &old_ctx->conf;
  903. vif_chsw[i].new_ctx = &ctx->conf;
  904. i++;
  905. }
  906. }
  907. err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
  908. CHANCTX_SWMODE_SWAP_CONTEXTS);
  909. out:
  910. kfree(vif_chsw);
  911. return err;
  912. }
  913. static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
  914. {
  915. struct ieee80211_chanctx *ctx;
  916. int err;
  917. lockdep_assert_held(&local->mtx);
  918. lockdep_assert_held(&local->chanctx_mtx);
  919. list_for_each_entry(ctx, &local->chanctx_list, list) {
  920. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  921. continue;
  922. if (!list_empty(&ctx->replace_ctx->assigned_vifs))
  923. continue;
  924. ieee80211_del_chanctx(local, ctx->replace_ctx);
  925. err = ieee80211_add_chanctx(local, ctx);
  926. if (err)
  927. goto err;
  928. }
  929. return 0;
  930. err:
  931. WARN_ON(ieee80211_add_chanctx(local, ctx));
  932. list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
  933. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  934. continue;
  935. if (!list_empty(&ctx->replace_ctx->assigned_vifs))
  936. continue;
  937. ieee80211_del_chanctx(local, ctx);
  938. WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
  939. }
  940. return err;
  941. }
  942. static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
  943. {
  944. struct ieee80211_sub_if_data *sdata, *sdata_tmp;
  945. struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
  946. struct ieee80211_chanctx *new_ctx = NULL;
  947. int i, err, n_assigned, n_reserved, n_ready;
  948. int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
  949. lockdep_assert_held(&local->mtx);
  950. lockdep_assert_held(&local->chanctx_mtx);
  951. /*
  952. * If there are 2 independent pairs of channel contexts performing
  953. * cross-switch of their vifs this code will still wait until both are
  954. * ready even though it could be possible to switch one before the
  955. * other is ready.
  956. *
  957. * For practical reasons and code simplicity just do a single huge
  958. * switch.
  959. */
  960. /*
  961. * Verify if the reservation is still feasible.
  962. * - if it's not then disconnect
  963. * - if it is but not all vifs necessary are ready then defer
  964. */
  965. list_for_each_entry(ctx, &local->chanctx_list, list) {
  966. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  967. continue;
  968. if (WARN_ON(!ctx->replace_ctx)) {
  969. err = -EINVAL;
  970. goto err;
  971. }
  972. if (!local->use_chanctx)
  973. new_ctx = ctx;
  974. n_ctx++;
  975. n_assigned = 0;
  976. n_reserved = 0;
  977. n_ready = 0;
  978. list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
  979. assigned_chanctx_list) {
  980. n_assigned++;
  981. if (sdata->reserved_chanctx) {
  982. n_reserved++;
  983. if (sdata->reserved_ready)
  984. n_ready++;
  985. }
  986. }
  987. if (n_assigned != n_reserved) {
  988. if (n_ready == n_reserved) {
  989. wiphy_info(local->hw.wiphy,
  990. "channel context reservation cannot be finalized because some interfaces aren't switching\n");
  991. err = -EBUSY;
  992. goto err;
  993. }
  994. return -EAGAIN;
  995. }
  996. ctx->conf.radar_enabled = false;
  997. list_for_each_entry(sdata, &ctx->reserved_vifs,
  998. reserved_chanctx_list) {
  999. if (ieee80211_vif_has_in_place_reservation(sdata) &&
  1000. !sdata->reserved_ready)
  1001. return -EAGAIN;
  1002. old_ctx = ieee80211_vif_get_chanctx(sdata);
  1003. if (old_ctx) {
  1004. if (old_ctx->replace_state ==
  1005. IEEE80211_CHANCTX_WILL_BE_REPLACED)
  1006. n_vifs_switch++;
  1007. else
  1008. n_vifs_assign++;
  1009. } else {
  1010. n_vifs_ctxless++;
  1011. }
  1012. if (sdata->reserved_radar_required)
  1013. ctx->conf.radar_enabled = true;
  1014. }
  1015. }
  1016. if (WARN_ON(n_ctx == 0) ||
  1017. WARN_ON(n_vifs_switch == 0 &&
  1018. n_vifs_assign == 0 &&
  1019. n_vifs_ctxless == 0) ||
  1020. WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
  1021. WARN_ON(!new_ctx && !local->use_chanctx)) {
  1022. err = -EINVAL;
  1023. goto err;
  1024. }
  1025. /*
  1026. * All necessary vifs are ready. Perform the switch now depending on
  1027. * reservations and driver capabilities.
  1028. */
  1029. if (local->use_chanctx) {
  1030. if (n_vifs_switch > 0) {
  1031. err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
  1032. if (err)
  1033. goto err;
  1034. }
  1035. if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
  1036. err = ieee80211_chsw_switch_ctxs(local);
  1037. if (err)
  1038. goto err;
  1039. }
  1040. } else {
  1041. err = ieee80211_chsw_switch_hwconf(local, new_ctx);
  1042. if (err)
  1043. goto err;
  1044. }
  1045. /*
  1046. * Update all structures, values and pointers to point to new channel
  1047. * context(s).
  1048. */
  1049. i = 0;
  1050. list_for_each_entry(ctx, &local->chanctx_list, list) {
  1051. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  1052. continue;
  1053. if (WARN_ON(!ctx->replace_ctx)) {
  1054. err = -EINVAL;
  1055. goto err;
  1056. }
  1057. list_for_each_entry(sdata, &ctx->reserved_vifs,
  1058. reserved_chanctx_list) {
  1059. u32 changed = 0;
  1060. if (!ieee80211_vif_has_in_place_reservation(sdata))
  1061. continue;
  1062. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  1063. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1064. __ieee80211_vif_copy_chanctx_to_vlans(sdata,
  1065. false);
  1066. sdata->radar_required = sdata->reserved_radar_required;
  1067. if (sdata->vif.bss_conf.chandef.width !=
  1068. sdata->reserved_chandef.width)
  1069. changed = BSS_CHANGED_BANDWIDTH;
  1070. ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
  1071. if (changed)
  1072. ieee80211_bss_info_change_notify(sdata,
  1073. changed);
  1074. ieee80211_recalc_txpower(sdata);
  1075. }
  1076. ieee80211_recalc_chanctx_chantype(local, ctx);
  1077. ieee80211_recalc_smps_chanctx(local, ctx);
  1078. ieee80211_recalc_radar_chanctx(local, ctx);
  1079. ieee80211_recalc_chanctx_min_def(local, ctx);
  1080. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1081. reserved_chanctx_list) {
  1082. if (ieee80211_vif_get_chanctx(sdata) != ctx)
  1083. continue;
  1084. list_del(&sdata->reserved_chanctx_list);
  1085. list_move(&sdata->assigned_chanctx_list,
  1086. &ctx->assigned_vifs);
  1087. sdata->reserved_chanctx = NULL;
  1088. ieee80211_vif_chanctx_reservation_complete(sdata);
  1089. }
  1090. /*
  1091. * This context might have been a dependency for an already
  1092. * ready re-assign reservation interface that was deferred. Do
  1093. * not propagate error to the caller though. The in-place
  1094. * reservation for originally requested interface has already
  1095. * succeeded at this point.
  1096. */
  1097. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1098. reserved_chanctx_list) {
  1099. if (WARN_ON(ieee80211_vif_has_in_place_reservation(
  1100. sdata)))
  1101. continue;
  1102. if (WARN_ON(sdata->reserved_chanctx != ctx))
  1103. continue;
  1104. if (!sdata->reserved_ready)
  1105. continue;
  1106. if (ieee80211_vif_get_chanctx(sdata))
  1107. err = ieee80211_vif_use_reserved_reassign(
  1108. sdata);
  1109. else
  1110. err = ieee80211_vif_use_reserved_assign(sdata);
  1111. if (err) {
  1112. sdata_info(sdata,
  1113. "failed to finalize (re-)assign reservation (err=%d)\n",
  1114. err);
  1115. ieee80211_vif_unreserve_chanctx(sdata);
  1116. cfg80211_stop_iface(local->hw.wiphy,
  1117. &sdata->wdev,
  1118. GFP_KERNEL);
  1119. }
  1120. }
  1121. }
  1122. /*
  1123. * Finally free old contexts
  1124. */
  1125. list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
  1126. if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
  1127. continue;
  1128. ctx->replace_ctx->replace_ctx = NULL;
  1129. ctx->replace_ctx->replace_state =
  1130. IEEE80211_CHANCTX_REPLACE_NONE;
  1131. list_del_rcu(&ctx->list);
  1132. kfree_rcu(ctx, rcu_head);
  1133. }
  1134. return 0;
  1135. err:
  1136. list_for_each_entry(ctx, &local->chanctx_list, list) {
  1137. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  1138. continue;
  1139. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1140. reserved_chanctx_list) {
  1141. ieee80211_vif_unreserve_chanctx(sdata);
  1142. ieee80211_vif_chanctx_reservation_complete(sdata);
  1143. }
  1144. }
  1145. return err;
  1146. }
  1147. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  1148. {
  1149. struct ieee80211_local *local = sdata->local;
  1150. struct ieee80211_chanctx_conf *conf;
  1151. struct ieee80211_chanctx *ctx;
  1152. bool use_reserved_switch = false;
  1153. lockdep_assert_held(&local->chanctx_mtx);
  1154. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1155. lockdep_is_held(&local->chanctx_mtx));
  1156. if (!conf)
  1157. return;
  1158. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1159. if (sdata->reserved_chanctx) {
  1160. if (sdata->reserved_chanctx->replace_state ==
  1161. IEEE80211_CHANCTX_REPLACES_OTHER &&
  1162. ieee80211_chanctx_num_reserved(local,
  1163. sdata->reserved_chanctx) > 1)
  1164. use_reserved_switch = true;
  1165. ieee80211_vif_unreserve_chanctx(sdata);
  1166. }
  1167. ieee80211_assign_vif_chanctx(sdata, NULL);
  1168. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  1169. ieee80211_free_chanctx(local, ctx);
  1170. /* Unreserving may ready an in-place reservation. */
  1171. if (use_reserved_switch)
  1172. ieee80211_vif_use_reserved_switch(local);
  1173. }
  1174. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  1175. const struct cfg80211_chan_def *chandef,
  1176. enum ieee80211_chanctx_mode mode)
  1177. {
  1178. struct ieee80211_local *local = sdata->local;
  1179. struct ieee80211_chanctx *ctx;
  1180. u8 radar_detect_width = 0;
  1181. int ret;
  1182. lockdep_assert_held(&local->mtx);
  1183. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  1184. mutex_lock(&local->chanctx_mtx);
  1185. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1186. chandef,
  1187. sdata->wdev.iftype);
  1188. if (ret < 0)
  1189. goto out;
  1190. if (ret > 0)
  1191. radar_detect_width = BIT(chandef->width);
  1192. sdata->radar_required = ret;
  1193. ret = ieee80211_check_combinations(sdata, chandef, mode,
  1194. radar_detect_width);
  1195. if (ret < 0)
  1196. goto out;
  1197. __ieee80211_vif_release_channel(sdata);
  1198. ctx = ieee80211_find_chanctx(local, chandef, mode);
  1199. if (!ctx)
  1200. ctx = ieee80211_new_chanctx(local, chandef, mode);
  1201. if (IS_ERR(ctx)) {
  1202. ret = PTR_ERR(ctx);
  1203. goto out;
  1204. }
  1205. ieee80211_vif_update_chandef(sdata, chandef);
  1206. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  1207. if (ret) {
  1208. /* if assign fails refcount stays the same */
  1209. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  1210. ieee80211_free_chanctx(local, ctx);
  1211. goto out;
  1212. }
  1213. ieee80211_recalc_smps_chanctx(local, ctx);
  1214. ieee80211_recalc_radar_chanctx(local, ctx);
  1215. out:
  1216. mutex_unlock(&local->chanctx_mtx);
  1217. return ret;
  1218. }
  1219. int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
  1220. {
  1221. struct ieee80211_local *local = sdata->local;
  1222. struct ieee80211_chanctx *new_ctx;
  1223. struct ieee80211_chanctx *old_ctx;
  1224. int err;
  1225. lockdep_assert_held(&local->mtx);
  1226. lockdep_assert_held(&local->chanctx_mtx);
  1227. new_ctx = sdata->reserved_chanctx;
  1228. old_ctx = ieee80211_vif_get_chanctx(sdata);
  1229. if (WARN_ON(!new_ctx))
  1230. return -EINVAL;
  1231. if (WARN_ON(new_ctx->replace_state ==
  1232. IEEE80211_CHANCTX_WILL_BE_REPLACED))
  1233. return -EINVAL;
  1234. if (WARN_ON(sdata->reserved_ready))
  1235. return -EINVAL;
  1236. sdata->reserved_ready = true;
  1237. if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
  1238. if (old_ctx)
  1239. err = ieee80211_vif_use_reserved_reassign(sdata);
  1240. else
  1241. err = ieee80211_vif_use_reserved_assign(sdata);
  1242. if (err)
  1243. return err;
  1244. }
  1245. /*
  1246. * In-place reservation may need to be finalized now either if:
  1247. * a) sdata is taking part in the swapping itself and is the last one
  1248. * b) sdata has switched with a re-assign reservation to an existing
  1249. * context readying in-place switching of old_ctx
  1250. *
  1251. * In case of (b) do not propagate the error up because the requested
  1252. * sdata already switched successfully. Just spill an extra warning.
  1253. * The ieee80211_vif_use_reserved_switch() already stops all necessary
  1254. * interfaces upon failure.
  1255. */
  1256. if ((old_ctx &&
  1257. old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
  1258. new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
  1259. err = ieee80211_vif_use_reserved_switch(local);
  1260. if (err && err != -EAGAIN) {
  1261. if (new_ctx->replace_state ==
  1262. IEEE80211_CHANCTX_REPLACES_OTHER)
  1263. return err;
  1264. wiphy_info(local->hw.wiphy,
  1265. "depending in-place reservation failed (err=%d)\n",
  1266. err);
  1267. }
  1268. }
  1269. return 0;
  1270. }
  1271. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  1272. const struct cfg80211_chan_def *chandef,
  1273. u32 *changed)
  1274. {
  1275. struct ieee80211_local *local = sdata->local;
  1276. struct ieee80211_chanctx_conf *conf;
  1277. struct ieee80211_chanctx *ctx;
  1278. const struct cfg80211_chan_def *compat;
  1279. int ret;
  1280. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  1281. IEEE80211_CHAN_DISABLED))
  1282. return -EINVAL;
  1283. mutex_lock(&local->chanctx_mtx);
  1284. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  1285. ret = 0;
  1286. goto out;
  1287. }
  1288. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  1289. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  1290. ret = -EINVAL;
  1291. goto out;
  1292. }
  1293. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1294. lockdep_is_held(&local->chanctx_mtx));
  1295. if (!conf) {
  1296. ret = -EINVAL;
  1297. goto out;
  1298. }
  1299. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1300. compat = cfg80211_chandef_compatible(&conf->def, chandef);
  1301. if (!compat) {
  1302. ret = -EINVAL;
  1303. goto out;
  1304. }
  1305. switch (ctx->replace_state) {
  1306. case IEEE80211_CHANCTX_REPLACE_NONE:
  1307. if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
  1308. ret = -EBUSY;
  1309. goto out;
  1310. }
  1311. break;
  1312. case IEEE80211_CHANCTX_WILL_BE_REPLACED:
  1313. /* TODO: Perhaps the bandwith change could be treated as a
  1314. * reservation itself? */
  1315. ret = -EBUSY;
  1316. goto out;
  1317. case IEEE80211_CHANCTX_REPLACES_OTHER:
  1318. /* channel context that is going to replace another channel
  1319. * context doesn't really exist and shouldn't be assigned
  1320. * anywhere yet */
  1321. WARN_ON(1);
  1322. break;
  1323. }
  1324. ieee80211_vif_update_chandef(sdata, chandef);
  1325. ieee80211_recalc_chanctx_chantype(local, ctx);
  1326. *changed |= BSS_CHANGED_BANDWIDTH;
  1327. ret = 0;
  1328. out:
  1329. mutex_unlock(&local->chanctx_mtx);
  1330. return ret;
  1331. }
  1332. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  1333. {
  1334. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  1335. lockdep_assert_held(&sdata->local->mtx);
  1336. mutex_lock(&sdata->local->chanctx_mtx);
  1337. __ieee80211_vif_release_channel(sdata);
  1338. mutex_unlock(&sdata->local->chanctx_mtx);
  1339. }
  1340. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  1341. {
  1342. struct ieee80211_local *local = sdata->local;
  1343. struct ieee80211_sub_if_data *ap;
  1344. struct ieee80211_chanctx_conf *conf;
  1345. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  1346. return;
  1347. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  1348. mutex_lock(&local->chanctx_mtx);
  1349. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  1350. lockdep_is_held(&local->chanctx_mtx));
  1351. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  1352. mutex_unlock(&local->chanctx_mtx);
  1353. }
  1354. void ieee80211_iter_chan_contexts_atomic(
  1355. struct ieee80211_hw *hw,
  1356. void (*iter)(struct ieee80211_hw *hw,
  1357. struct ieee80211_chanctx_conf *chanctx_conf,
  1358. void *data),
  1359. void *iter_data)
  1360. {
  1361. struct ieee80211_local *local = hw_to_local(hw);
  1362. struct ieee80211_chanctx *ctx;
  1363. rcu_read_lock();
  1364. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  1365. if (ctx->driver_present)
  1366. iter(hw, &ctx->conf, iter_data);
  1367. rcu_read_unlock();
  1368. }
  1369. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);