chan.c 45 KB

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