chan.c 44 KB

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