chan.c 44 KB

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