chan.c 43 KB

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