chan.c 43 KB

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