chan.c 45 KB

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