chan.c 53 KB

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