chan.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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. ieee80211_check_fast_xmit_iface(sdata);
  525. return ret;
  526. }
  527. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  528. struct ieee80211_chanctx *chanctx)
  529. {
  530. struct ieee80211_sub_if_data *sdata;
  531. u8 rx_chains_static, rx_chains_dynamic;
  532. lockdep_assert_held(&local->chanctx_mtx);
  533. rx_chains_static = 1;
  534. rx_chains_dynamic = 1;
  535. rcu_read_lock();
  536. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  537. u8 needed_static, needed_dynamic;
  538. if (!ieee80211_sdata_running(sdata))
  539. continue;
  540. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  541. &chanctx->conf)
  542. continue;
  543. switch (sdata->vif.type) {
  544. case NL80211_IFTYPE_P2P_DEVICE:
  545. continue;
  546. case NL80211_IFTYPE_STATION:
  547. if (!sdata->u.mgd.associated)
  548. continue;
  549. break;
  550. case NL80211_IFTYPE_AP_VLAN:
  551. continue;
  552. case NL80211_IFTYPE_AP:
  553. case NL80211_IFTYPE_ADHOC:
  554. case NL80211_IFTYPE_WDS:
  555. case NL80211_IFTYPE_MESH_POINT:
  556. case NL80211_IFTYPE_OCB:
  557. break;
  558. default:
  559. WARN_ON_ONCE(1);
  560. }
  561. switch (sdata->smps_mode) {
  562. default:
  563. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  564. sdata->smps_mode);
  565. /* fall through */
  566. case IEEE80211_SMPS_OFF:
  567. needed_static = sdata->needed_rx_chains;
  568. needed_dynamic = sdata->needed_rx_chains;
  569. break;
  570. case IEEE80211_SMPS_DYNAMIC:
  571. needed_static = 1;
  572. needed_dynamic = sdata->needed_rx_chains;
  573. break;
  574. case IEEE80211_SMPS_STATIC:
  575. needed_static = 1;
  576. needed_dynamic = 1;
  577. break;
  578. }
  579. rx_chains_static = max(rx_chains_static, needed_static);
  580. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  581. }
  582. /* Disable SMPS for the monitor interface */
  583. sdata = rcu_dereference(local->monitor_sdata);
  584. if (sdata &&
  585. rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
  586. rx_chains_dynamic = rx_chains_static = local->rx_chains;
  587. rcu_read_unlock();
  588. if (!local->use_chanctx) {
  589. if (rx_chains_static > 1)
  590. local->smps_mode = IEEE80211_SMPS_OFF;
  591. else if (rx_chains_dynamic > 1)
  592. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  593. else
  594. local->smps_mode = IEEE80211_SMPS_STATIC;
  595. ieee80211_hw_config(local, 0);
  596. }
  597. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  598. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  599. return;
  600. chanctx->conf.rx_chains_static = rx_chains_static;
  601. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  602. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  603. }
  604. static void
  605. __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  606. bool clear)
  607. {
  608. struct ieee80211_local *local __maybe_unused = sdata->local;
  609. struct ieee80211_sub_if_data *vlan;
  610. struct ieee80211_chanctx_conf *conf;
  611. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
  612. return;
  613. lockdep_assert_held(&local->mtx);
  614. /* Check that conf exists, even when clearing this function
  615. * must be called with the AP's channel context still there
  616. * as it would otherwise cause VLANs to have an invalid
  617. * channel context pointer for a while, possibly pointing
  618. * to a channel context that has already been freed.
  619. */
  620. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  621. lockdep_is_held(&local->chanctx_mtx));
  622. WARN_ON(!conf);
  623. if (clear)
  624. conf = NULL;
  625. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  626. rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
  627. }
  628. void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  629. bool clear)
  630. {
  631. struct ieee80211_local *local = sdata->local;
  632. mutex_lock(&local->chanctx_mtx);
  633. __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
  634. mutex_unlock(&local->chanctx_mtx);
  635. }
  636. int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
  637. {
  638. struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
  639. lockdep_assert_held(&sdata->local->chanctx_mtx);
  640. if (WARN_ON(!ctx))
  641. return -EINVAL;
  642. list_del(&sdata->reserved_chanctx_list);
  643. sdata->reserved_chanctx = NULL;
  644. if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0) {
  645. if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
  646. if (WARN_ON(!ctx->replace_ctx))
  647. return -EINVAL;
  648. WARN_ON(ctx->replace_ctx->replace_state !=
  649. IEEE80211_CHANCTX_WILL_BE_REPLACED);
  650. WARN_ON(ctx->replace_ctx->replace_ctx != ctx);
  651. ctx->replace_ctx->replace_ctx = NULL;
  652. ctx->replace_ctx->replace_state =
  653. IEEE80211_CHANCTX_REPLACE_NONE;
  654. list_del_rcu(&ctx->list);
  655. kfree_rcu(ctx, rcu_head);
  656. } else {
  657. ieee80211_free_chanctx(sdata->local, ctx);
  658. }
  659. }
  660. return 0;
  661. }
  662. int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
  663. const struct cfg80211_chan_def *chandef,
  664. enum ieee80211_chanctx_mode mode,
  665. bool radar_required)
  666. {
  667. struct ieee80211_local *local = sdata->local;
  668. struct ieee80211_chanctx *new_ctx, *curr_ctx, *ctx;
  669. lockdep_assert_held(&local->chanctx_mtx);
  670. curr_ctx = ieee80211_vif_get_chanctx(sdata);
  671. if (curr_ctx && local->use_chanctx && !local->ops->switch_vif_chanctx)
  672. return -ENOTSUPP;
  673. new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
  674. if (!new_ctx) {
  675. if (ieee80211_can_create_new_chanctx(local)) {
  676. new_ctx = ieee80211_new_chanctx(local, chandef, mode);
  677. if (IS_ERR(new_ctx))
  678. return PTR_ERR(new_ctx);
  679. } else {
  680. if (!curr_ctx ||
  681. (curr_ctx->replace_state ==
  682. IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
  683. !list_empty(&curr_ctx->reserved_vifs)) {
  684. /*
  685. * Another vif already requested this context
  686. * for a reservation. Find another one hoping
  687. * all vifs assigned to it will also switch
  688. * soon enough.
  689. *
  690. * TODO: This needs a little more work as some
  691. * cases (more than 2 chanctx capable devices)
  692. * may fail which could otherwise succeed
  693. * provided some channel context juggling was
  694. * performed.
  695. *
  696. * Consider ctx1..3, vif1..6, each ctx has 2
  697. * vifs. vif1 and vif2 from ctx1 request new
  698. * different chandefs starting 2 in-place
  699. * reserations with ctx4 and ctx5 replacing
  700. * ctx1 and ctx2 respectively. Next vif5 and
  701. * vif6 from ctx3 reserve ctx4. If vif3 and
  702. * vif4 remain on ctx2 as they are then this
  703. * fails unless `replace_ctx` from ctx5 is
  704. * replaced with ctx3.
  705. */
  706. list_for_each_entry(ctx, &local->chanctx_list,
  707. list) {
  708. if (ctx->replace_state !=
  709. IEEE80211_CHANCTX_REPLACE_NONE)
  710. continue;
  711. if (!list_empty(&ctx->reserved_vifs))
  712. continue;
  713. curr_ctx = ctx;
  714. break;
  715. }
  716. }
  717. /*
  718. * If that's true then all available contexts already
  719. * have reservations and cannot be used.
  720. */
  721. if (!curr_ctx ||
  722. (curr_ctx->replace_state ==
  723. IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
  724. !list_empty(&curr_ctx->reserved_vifs))
  725. return -EBUSY;
  726. new_ctx = ieee80211_alloc_chanctx(local, chandef, mode);
  727. if (!new_ctx)
  728. return -ENOMEM;
  729. new_ctx->replace_ctx = curr_ctx;
  730. new_ctx->replace_state =
  731. IEEE80211_CHANCTX_REPLACES_OTHER;
  732. curr_ctx->replace_ctx = new_ctx;
  733. curr_ctx->replace_state =
  734. IEEE80211_CHANCTX_WILL_BE_REPLACED;
  735. list_add_rcu(&new_ctx->list, &local->chanctx_list);
  736. }
  737. }
  738. list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
  739. sdata->reserved_chanctx = new_ctx;
  740. sdata->reserved_chandef = *chandef;
  741. sdata->reserved_radar_required = radar_required;
  742. sdata->reserved_ready = false;
  743. return 0;
  744. }
  745. static void
  746. ieee80211_vif_chanctx_reservation_complete(struct ieee80211_sub_if_data *sdata)
  747. {
  748. switch (sdata->vif.type) {
  749. case NL80211_IFTYPE_ADHOC:
  750. case NL80211_IFTYPE_AP:
  751. case NL80211_IFTYPE_MESH_POINT:
  752. case NL80211_IFTYPE_OCB:
  753. ieee80211_queue_work(&sdata->local->hw,
  754. &sdata->csa_finalize_work);
  755. break;
  756. case NL80211_IFTYPE_STATION:
  757. ieee80211_queue_work(&sdata->local->hw,
  758. &sdata->u.mgd.chswitch_work);
  759. break;
  760. case NL80211_IFTYPE_UNSPECIFIED:
  761. case NL80211_IFTYPE_AP_VLAN:
  762. case NL80211_IFTYPE_WDS:
  763. case NL80211_IFTYPE_MONITOR:
  764. case NL80211_IFTYPE_P2P_CLIENT:
  765. case NL80211_IFTYPE_P2P_GO:
  766. case NL80211_IFTYPE_P2P_DEVICE:
  767. case NUM_NL80211_IFTYPES:
  768. WARN_ON(1);
  769. break;
  770. }
  771. }
  772. static void
  773. ieee80211_vif_update_chandef(struct ieee80211_sub_if_data *sdata,
  774. const struct cfg80211_chan_def *chandef)
  775. {
  776. struct ieee80211_sub_if_data *vlan;
  777. sdata->vif.bss_conf.chandef = *chandef;
  778. if (sdata->vif.type != NL80211_IFTYPE_AP)
  779. return;
  780. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  781. vlan->vif.bss_conf.chandef = *chandef;
  782. }
  783. static int
  784. ieee80211_vif_use_reserved_reassign(struct ieee80211_sub_if_data *sdata)
  785. {
  786. struct ieee80211_local *local = sdata->local;
  787. struct ieee80211_vif_chanctx_switch vif_chsw[1] = {};
  788. struct ieee80211_chanctx *old_ctx, *new_ctx;
  789. const struct cfg80211_chan_def *chandef;
  790. u32 changed = 0;
  791. int err;
  792. lockdep_assert_held(&local->mtx);
  793. lockdep_assert_held(&local->chanctx_mtx);
  794. new_ctx = sdata->reserved_chanctx;
  795. old_ctx = ieee80211_vif_get_chanctx(sdata);
  796. if (WARN_ON(!sdata->reserved_ready))
  797. return -EBUSY;
  798. if (WARN_ON(!new_ctx))
  799. return -EINVAL;
  800. if (WARN_ON(!old_ctx))
  801. return -EINVAL;
  802. if (WARN_ON(new_ctx->replace_state ==
  803. IEEE80211_CHANCTX_REPLACES_OTHER))
  804. return -EINVAL;
  805. chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
  806. &sdata->reserved_chandef);
  807. if (WARN_ON(!chandef))
  808. return -EINVAL;
  809. ieee80211_change_chanctx(local, new_ctx, chandef);
  810. vif_chsw[0].vif = &sdata->vif;
  811. vif_chsw[0].old_ctx = &old_ctx->conf;
  812. vif_chsw[0].new_ctx = &new_ctx->conf;
  813. list_del(&sdata->reserved_chanctx_list);
  814. sdata->reserved_chanctx = NULL;
  815. err = drv_switch_vif_chanctx(local, vif_chsw, 1,
  816. CHANCTX_SWMODE_REASSIGN_VIF);
  817. if (err) {
  818. if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
  819. ieee80211_free_chanctx(local, new_ctx);
  820. goto out;
  821. }
  822. list_move(&sdata->assigned_chanctx_list, &new_ctx->assigned_vifs);
  823. rcu_assign_pointer(sdata->vif.chanctx_conf, &new_ctx->conf);
  824. if (sdata->vif.type == NL80211_IFTYPE_AP)
  825. __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
  826. ieee80211_check_fast_xmit_iface(sdata);
  827. if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
  828. ieee80211_free_chanctx(local, old_ctx);
  829. if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
  830. changed = BSS_CHANGED_BANDWIDTH;
  831. ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
  832. ieee80211_recalc_smps_chanctx(local, new_ctx);
  833. ieee80211_recalc_radar_chanctx(local, new_ctx);
  834. ieee80211_recalc_chanctx_min_def(local, new_ctx);
  835. if (changed)
  836. ieee80211_bss_info_change_notify(sdata, changed);
  837. out:
  838. ieee80211_vif_chanctx_reservation_complete(sdata);
  839. return err;
  840. }
  841. static int
  842. ieee80211_vif_use_reserved_assign(struct ieee80211_sub_if_data *sdata)
  843. {
  844. struct ieee80211_local *local = sdata->local;
  845. struct ieee80211_chanctx *old_ctx, *new_ctx;
  846. const struct cfg80211_chan_def *chandef;
  847. int err;
  848. old_ctx = ieee80211_vif_get_chanctx(sdata);
  849. new_ctx = sdata->reserved_chanctx;
  850. if (WARN_ON(!sdata->reserved_ready))
  851. return -EINVAL;
  852. if (WARN_ON(old_ctx))
  853. return -EINVAL;
  854. if (WARN_ON(!new_ctx))
  855. return -EINVAL;
  856. if (WARN_ON(new_ctx->replace_state ==
  857. IEEE80211_CHANCTX_REPLACES_OTHER))
  858. return -EINVAL;
  859. chandef = ieee80211_chanctx_non_reserved_chandef(local, new_ctx,
  860. &sdata->reserved_chandef);
  861. if (WARN_ON(!chandef))
  862. return -EINVAL;
  863. ieee80211_change_chanctx(local, new_ctx, chandef);
  864. list_del(&sdata->reserved_chanctx_list);
  865. sdata->reserved_chanctx = NULL;
  866. err = ieee80211_assign_vif_chanctx(sdata, new_ctx);
  867. if (err) {
  868. if (ieee80211_chanctx_refcount(local, new_ctx) == 0)
  869. ieee80211_free_chanctx(local, new_ctx);
  870. goto out;
  871. }
  872. out:
  873. ieee80211_vif_chanctx_reservation_complete(sdata);
  874. return err;
  875. }
  876. static bool
  877. ieee80211_vif_has_in_place_reservation(struct ieee80211_sub_if_data *sdata)
  878. {
  879. struct ieee80211_chanctx *old_ctx, *new_ctx;
  880. lockdep_assert_held(&sdata->local->chanctx_mtx);
  881. new_ctx = sdata->reserved_chanctx;
  882. old_ctx = ieee80211_vif_get_chanctx(sdata);
  883. if (!old_ctx)
  884. return false;
  885. if (WARN_ON(!new_ctx))
  886. return false;
  887. if (old_ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
  888. return false;
  889. if (new_ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  890. return false;
  891. return true;
  892. }
  893. static int ieee80211_chsw_switch_hwconf(struct ieee80211_local *local,
  894. struct ieee80211_chanctx *new_ctx)
  895. {
  896. const struct cfg80211_chan_def *chandef;
  897. lockdep_assert_held(&local->mtx);
  898. lockdep_assert_held(&local->chanctx_mtx);
  899. chandef = ieee80211_chanctx_reserved_chandef(local, new_ctx, NULL);
  900. if (WARN_ON(!chandef))
  901. return -EINVAL;
  902. local->hw.conf.radar_enabled = new_ctx->conf.radar_enabled;
  903. local->_oper_chandef = *chandef;
  904. ieee80211_hw_config(local, 0);
  905. return 0;
  906. }
  907. static int ieee80211_chsw_switch_vifs(struct ieee80211_local *local,
  908. int n_vifs)
  909. {
  910. struct ieee80211_vif_chanctx_switch *vif_chsw;
  911. struct ieee80211_sub_if_data *sdata;
  912. struct ieee80211_chanctx *ctx, *old_ctx;
  913. int i, err;
  914. lockdep_assert_held(&local->mtx);
  915. lockdep_assert_held(&local->chanctx_mtx);
  916. vif_chsw = kzalloc(sizeof(vif_chsw[0]) * n_vifs, GFP_KERNEL);
  917. if (!vif_chsw)
  918. return -ENOMEM;
  919. i = 0;
  920. list_for_each_entry(ctx, &local->chanctx_list, list) {
  921. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  922. continue;
  923. if (WARN_ON(!ctx->replace_ctx)) {
  924. err = -EINVAL;
  925. goto out;
  926. }
  927. list_for_each_entry(sdata, &ctx->reserved_vifs,
  928. reserved_chanctx_list) {
  929. if (!ieee80211_vif_has_in_place_reservation(
  930. sdata))
  931. continue;
  932. old_ctx = ieee80211_vif_get_chanctx(sdata);
  933. vif_chsw[i].vif = &sdata->vif;
  934. vif_chsw[i].old_ctx = &old_ctx->conf;
  935. vif_chsw[i].new_ctx = &ctx->conf;
  936. i++;
  937. }
  938. }
  939. err = drv_switch_vif_chanctx(local, vif_chsw, n_vifs,
  940. CHANCTX_SWMODE_SWAP_CONTEXTS);
  941. out:
  942. kfree(vif_chsw);
  943. return err;
  944. }
  945. static int ieee80211_chsw_switch_ctxs(struct ieee80211_local *local)
  946. {
  947. struct ieee80211_chanctx *ctx;
  948. int err;
  949. lockdep_assert_held(&local->mtx);
  950. lockdep_assert_held(&local->chanctx_mtx);
  951. list_for_each_entry(ctx, &local->chanctx_list, list) {
  952. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  953. continue;
  954. if (!list_empty(&ctx->replace_ctx->assigned_vifs))
  955. continue;
  956. ieee80211_del_chanctx(local, ctx->replace_ctx);
  957. err = ieee80211_add_chanctx(local, ctx);
  958. if (err)
  959. goto err;
  960. }
  961. return 0;
  962. err:
  963. WARN_ON(ieee80211_add_chanctx(local, ctx));
  964. list_for_each_entry_continue_reverse(ctx, &local->chanctx_list, list) {
  965. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  966. continue;
  967. if (!list_empty(&ctx->replace_ctx->assigned_vifs))
  968. continue;
  969. ieee80211_del_chanctx(local, ctx);
  970. WARN_ON(ieee80211_add_chanctx(local, ctx->replace_ctx));
  971. }
  972. return err;
  973. }
  974. static int ieee80211_vif_use_reserved_switch(struct ieee80211_local *local)
  975. {
  976. struct ieee80211_sub_if_data *sdata, *sdata_tmp;
  977. struct ieee80211_chanctx *ctx, *ctx_tmp, *old_ctx;
  978. struct ieee80211_chanctx *new_ctx = NULL;
  979. int i, err, n_assigned, n_reserved, n_ready;
  980. int n_ctx = 0, n_vifs_switch = 0, n_vifs_assign = 0, n_vifs_ctxless = 0;
  981. lockdep_assert_held(&local->mtx);
  982. lockdep_assert_held(&local->chanctx_mtx);
  983. /*
  984. * If there are 2 independent pairs of channel contexts performing
  985. * cross-switch of their vifs this code will still wait until both are
  986. * ready even though it could be possible to switch one before the
  987. * other is ready.
  988. *
  989. * For practical reasons and code simplicity just do a single huge
  990. * switch.
  991. */
  992. /*
  993. * Verify if the reservation is still feasible.
  994. * - if it's not then disconnect
  995. * - if it is but not all vifs necessary are ready then defer
  996. */
  997. list_for_each_entry(ctx, &local->chanctx_list, list) {
  998. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  999. continue;
  1000. if (WARN_ON(!ctx->replace_ctx)) {
  1001. err = -EINVAL;
  1002. goto err;
  1003. }
  1004. if (!local->use_chanctx)
  1005. new_ctx = ctx;
  1006. n_ctx++;
  1007. n_assigned = 0;
  1008. n_reserved = 0;
  1009. n_ready = 0;
  1010. list_for_each_entry(sdata, &ctx->replace_ctx->assigned_vifs,
  1011. assigned_chanctx_list) {
  1012. n_assigned++;
  1013. if (sdata->reserved_chanctx) {
  1014. n_reserved++;
  1015. if (sdata->reserved_ready)
  1016. n_ready++;
  1017. }
  1018. }
  1019. if (n_assigned != n_reserved) {
  1020. if (n_ready == n_reserved) {
  1021. wiphy_info(local->hw.wiphy,
  1022. "channel context reservation cannot be finalized because some interfaces aren't switching\n");
  1023. err = -EBUSY;
  1024. goto err;
  1025. }
  1026. return -EAGAIN;
  1027. }
  1028. ctx->conf.radar_enabled = false;
  1029. list_for_each_entry(sdata, &ctx->reserved_vifs,
  1030. reserved_chanctx_list) {
  1031. if (ieee80211_vif_has_in_place_reservation(sdata) &&
  1032. !sdata->reserved_ready)
  1033. return -EAGAIN;
  1034. old_ctx = ieee80211_vif_get_chanctx(sdata);
  1035. if (old_ctx) {
  1036. if (old_ctx->replace_state ==
  1037. IEEE80211_CHANCTX_WILL_BE_REPLACED)
  1038. n_vifs_switch++;
  1039. else
  1040. n_vifs_assign++;
  1041. } else {
  1042. n_vifs_ctxless++;
  1043. }
  1044. if (sdata->reserved_radar_required)
  1045. ctx->conf.radar_enabled = true;
  1046. }
  1047. }
  1048. if (WARN_ON(n_ctx == 0) ||
  1049. WARN_ON(n_vifs_switch == 0 &&
  1050. n_vifs_assign == 0 &&
  1051. n_vifs_ctxless == 0) ||
  1052. WARN_ON(n_ctx > 1 && !local->use_chanctx) ||
  1053. WARN_ON(!new_ctx && !local->use_chanctx)) {
  1054. err = -EINVAL;
  1055. goto err;
  1056. }
  1057. /*
  1058. * All necessary vifs are ready. Perform the switch now depending on
  1059. * reservations and driver capabilities.
  1060. */
  1061. if (local->use_chanctx) {
  1062. if (n_vifs_switch > 0) {
  1063. err = ieee80211_chsw_switch_vifs(local, n_vifs_switch);
  1064. if (err)
  1065. goto err;
  1066. }
  1067. if (n_vifs_assign > 0 || n_vifs_ctxless > 0) {
  1068. err = ieee80211_chsw_switch_ctxs(local);
  1069. if (err)
  1070. goto err;
  1071. }
  1072. } else {
  1073. err = ieee80211_chsw_switch_hwconf(local, new_ctx);
  1074. if (err)
  1075. goto err;
  1076. }
  1077. /*
  1078. * Update all structures, values and pointers to point to new channel
  1079. * context(s).
  1080. */
  1081. i = 0;
  1082. list_for_each_entry(ctx, &local->chanctx_list, list) {
  1083. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  1084. continue;
  1085. if (WARN_ON(!ctx->replace_ctx)) {
  1086. err = -EINVAL;
  1087. goto err;
  1088. }
  1089. list_for_each_entry(sdata, &ctx->reserved_vifs,
  1090. reserved_chanctx_list) {
  1091. u32 changed = 0;
  1092. if (!ieee80211_vif_has_in_place_reservation(sdata))
  1093. continue;
  1094. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  1095. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1096. __ieee80211_vif_copy_chanctx_to_vlans(sdata,
  1097. false);
  1098. ieee80211_check_fast_xmit_iface(sdata);
  1099. sdata->radar_required = sdata->reserved_radar_required;
  1100. if (sdata->vif.bss_conf.chandef.width !=
  1101. sdata->reserved_chandef.width)
  1102. changed = BSS_CHANGED_BANDWIDTH;
  1103. ieee80211_vif_update_chandef(sdata, &sdata->reserved_chandef);
  1104. if (changed)
  1105. ieee80211_bss_info_change_notify(sdata,
  1106. changed);
  1107. ieee80211_recalc_txpower(sdata, false);
  1108. }
  1109. ieee80211_recalc_chanctx_chantype(local, ctx);
  1110. ieee80211_recalc_smps_chanctx(local, ctx);
  1111. ieee80211_recalc_radar_chanctx(local, ctx);
  1112. ieee80211_recalc_chanctx_min_def(local, ctx);
  1113. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1114. reserved_chanctx_list) {
  1115. if (ieee80211_vif_get_chanctx(sdata) != ctx)
  1116. continue;
  1117. list_del(&sdata->reserved_chanctx_list);
  1118. list_move(&sdata->assigned_chanctx_list,
  1119. &ctx->assigned_vifs);
  1120. sdata->reserved_chanctx = NULL;
  1121. ieee80211_vif_chanctx_reservation_complete(sdata);
  1122. }
  1123. /*
  1124. * This context might have been a dependency for an already
  1125. * ready re-assign reservation interface that was deferred. Do
  1126. * not propagate error to the caller though. The in-place
  1127. * reservation for originally requested interface has already
  1128. * succeeded at this point.
  1129. */
  1130. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1131. reserved_chanctx_list) {
  1132. if (WARN_ON(ieee80211_vif_has_in_place_reservation(
  1133. sdata)))
  1134. continue;
  1135. if (WARN_ON(sdata->reserved_chanctx != ctx))
  1136. continue;
  1137. if (!sdata->reserved_ready)
  1138. continue;
  1139. if (ieee80211_vif_get_chanctx(sdata))
  1140. err = ieee80211_vif_use_reserved_reassign(
  1141. sdata);
  1142. else
  1143. err = ieee80211_vif_use_reserved_assign(sdata);
  1144. if (err) {
  1145. sdata_info(sdata,
  1146. "failed to finalize (re-)assign reservation (err=%d)\n",
  1147. err);
  1148. ieee80211_vif_unreserve_chanctx(sdata);
  1149. cfg80211_stop_iface(local->hw.wiphy,
  1150. &sdata->wdev,
  1151. GFP_KERNEL);
  1152. }
  1153. }
  1154. }
  1155. /*
  1156. * Finally free old contexts
  1157. */
  1158. list_for_each_entry_safe(ctx, ctx_tmp, &local->chanctx_list, list) {
  1159. if (ctx->replace_state != IEEE80211_CHANCTX_WILL_BE_REPLACED)
  1160. continue;
  1161. ctx->replace_ctx->replace_ctx = NULL;
  1162. ctx->replace_ctx->replace_state =
  1163. IEEE80211_CHANCTX_REPLACE_NONE;
  1164. list_del_rcu(&ctx->list);
  1165. kfree_rcu(ctx, rcu_head);
  1166. }
  1167. return 0;
  1168. err:
  1169. list_for_each_entry(ctx, &local->chanctx_list, list) {
  1170. if (ctx->replace_state != IEEE80211_CHANCTX_REPLACES_OTHER)
  1171. continue;
  1172. list_for_each_entry_safe(sdata, sdata_tmp, &ctx->reserved_vifs,
  1173. reserved_chanctx_list) {
  1174. ieee80211_vif_unreserve_chanctx(sdata);
  1175. ieee80211_vif_chanctx_reservation_complete(sdata);
  1176. }
  1177. }
  1178. return err;
  1179. }
  1180. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  1181. {
  1182. struct ieee80211_local *local = sdata->local;
  1183. struct ieee80211_chanctx_conf *conf;
  1184. struct ieee80211_chanctx *ctx;
  1185. bool use_reserved_switch = false;
  1186. lockdep_assert_held(&local->chanctx_mtx);
  1187. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1188. lockdep_is_held(&local->chanctx_mtx));
  1189. if (!conf)
  1190. return;
  1191. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1192. if (sdata->reserved_chanctx) {
  1193. if (sdata->reserved_chanctx->replace_state ==
  1194. IEEE80211_CHANCTX_REPLACES_OTHER &&
  1195. ieee80211_chanctx_num_reserved(local,
  1196. sdata->reserved_chanctx) > 1)
  1197. use_reserved_switch = true;
  1198. ieee80211_vif_unreserve_chanctx(sdata);
  1199. }
  1200. ieee80211_assign_vif_chanctx(sdata, NULL);
  1201. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  1202. ieee80211_free_chanctx(local, ctx);
  1203. sdata->radar_required = false;
  1204. /* Unreserving may ready an in-place reservation. */
  1205. if (use_reserved_switch)
  1206. ieee80211_vif_use_reserved_switch(local);
  1207. }
  1208. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  1209. const struct cfg80211_chan_def *chandef,
  1210. enum ieee80211_chanctx_mode mode)
  1211. {
  1212. struct ieee80211_local *local = sdata->local;
  1213. struct ieee80211_chanctx *ctx;
  1214. u8 radar_detect_width = 0;
  1215. int ret;
  1216. lockdep_assert_held(&local->mtx);
  1217. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  1218. mutex_lock(&local->chanctx_mtx);
  1219. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  1220. chandef,
  1221. sdata->wdev.iftype);
  1222. if (ret < 0)
  1223. goto out;
  1224. if (ret > 0)
  1225. radar_detect_width = BIT(chandef->width);
  1226. sdata->radar_required = ret;
  1227. ret = ieee80211_check_combinations(sdata, chandef, mode,
  1228. radar_detect_width);
  1229. if (ret < 0)
  1230. goto out;
  1231. __ieee80211_vif_release_channel(sdata);
  1232. ctx = ieee80211_find_chanctx(local, chandef, mode);
  1233. if (!ctx)
  1234. ctx = ieee80211_new_chanctx(local, chandef, mode);
  1235. if (IS_ERR(ctx)) {
  1236. ret = PTR_ERR(ctx);
  1237. goto out;
  1238. }
  1239. ieee80211_vif_update_chandef(sdata, chandef);
  1240. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  1241. if (ret) {
  1242. /* if assign fails refcount stays the same */
  1243. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  1244. ieee80211_free_chanctx(local, ctx);
  1245. goto out;
  1246. }
  1247. ieee80211_recalc_smps_chanctx(local, ctx);
  1248. ieee80211_recalc_radar_chanctx(local, ctx);
  1249. out:
  1250. if (ret)
  1251. sdata->radar_required = false;
  1252. mutex_unlock(&local->chanctx_mtx);
  1253. return ret;
  1254. }
  1255. int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata)
  1256. {
  1257. struct ieee80211_local *local = sdata->local;
  1258. struct ieee80211_chanctx *new_ctx;
  1259. struct ieee80211_chanctx *old_ctx;
  1260. int err;
  1261. lockdep_assert_held(&local->mtx);
  1262. lockdep_assert_held(&local->chanctx_mtx);
  1263. new_ctx = sdata->reserved_chanctx;
  1264. old_ctx = ieee80211_vif_get_chanctx(sdata);
  1265. if (WARN_ON(!new_ctx))
  1266. return -EINVAL;
  1267. if (WARN_ON(new_ctx->replace_state ==
  1268. IEEE80211_CHANCTX_WILL_BE_REPLACED))
  1269. return -EINVAL;
  1270. if (WARN_ON(sdata->reserved_ready))
  1271. return -EINVAL;
  1272. sdata->reserved_ready = true;
  1273. if (new_ctx->replace_state == IEEE80211_CHANCTX_REPLACE_NONE) {
  1274. if (old_ctx)
  1275. err = ieee80211_vif_use_reserved_reassign(sdata);
  1276. else
  1277. err = ieee80211_vif_use_reserved_assign(sdata);
  1278. if (err)
  1279. return err;
  1280. }
  1281. /*
  1282. * In-place reservation may need to be finalized now either if:
  1283. * a) sdata is taking part in the swapping itself and is the last one
  1284. * b) sdata has switched with a re-assign reservation to an existing
  1285. * context readying in-place switching of old_ctx
  1286. *
  1287. * In case of (b) do not propagate the error up because the requested
  1288. * sdata already switched successfully. Just spill an extra warning.
  1289. * The ieee80211_vif_use_reserved_switch() already stops all necessary
  1290. * interfaces upon failure.
  1291. */
  1292. if ((old_ctx &&
  1293. old_ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED) ||
  1294. new_ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER) {
  1295. err = ieee80211_vif_use_reserved_switch(local);
  1296. if (err && err != -EAGAIN) {
  1297. if (new_ctx->replace_state ==
  1298. IEEE80211_CHANCTX_REPLACES_OTHER)
  1299. return err;
  1300. wiphy_info(local->hw.wiphy,
  1301. "depending in-place reservation failed (err=%d)\n",
  1302. err);
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  1308. const struct cfg80211_chan_def *chandef,
  1309. u32 *changed)
  1310. {
  1311. struct ieee80211_local *local = sdata->local;
  1312. struct ieee80211_chanctx_conf *conf;
  1313. struct ieee80211_chanctx *ctx;
  1314. const struct cfg80211_chan_def *compat;
  1315. int ret;
  1316. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  1317. IEEE80211_CHAN_DISABLED))
  1318. return -EINVAL;
  1319. mutex_lock(&local->chanctx_mtx);
  1320. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  1321. ret = 0;
  1322. goto out;
  1323. }
  1324. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  1325. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  1326. ret = -EINVAL;
  1327. goto out;
  1328. }
  1329. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1330. lockdep_is_held(&local->chanctx_mtx));
  1331. if (!conf) {
  1332. ret = -EINVAL;
  1333. goto out;
  1334. }
  1335. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1336. compat = cfg80211_chandef_compatible(&conf->def, chandef);
  1337. if (!compat) {
  1338. ret = -EINVAL;
  1339. goto out;
  1340. }
  1341. switch (ctx->replace_state) {
  1342. case IEEE80211_CHANCTX_REPLACE_NONE:
  1343. if (!ieee80211_chanctx_reserved_chandef(local, ctx, compat)) {
  1344. ret = -EBUSY;
  1345. goto out;
  1346. }
  1347. break;
  1348. case IEEE80211_CHANCTX_WILL_BE_REPLACED:
  1349. /* TODO: Perhaps the bandwidth change could be treated as a
  1350. * reservation itself? */
  1351. ret = -EBUSY;
  1352. goto out;
  1353. case IEEE80211_CHANCTX_REPLACES_OTHER:
  1354. /* channel context that is going to replace another channel
  1355. * context doesn't really exist and shouldn't be assigned
  1356. * anywhere yet */
  1357. WARN_ON(1);
  1358. break;
  1359. }
  1360. ieee80211_vif_update_chandef(sdata, chandef);
  1361. ieee80211_recalc_chanctx_chantype(local, ctx);
  1362. *changed |= BSS_CHANGED_BANDWIDTH;
  1363. ret = 0;
  1364. out:
  1365. mutex_unlock(&local->chanctx_mtx);
  1366. return ret;
  1367. }
  1368. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  1369. {
  1370. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  1371. lockdep_assert_held(&sdata->local->mtx);
  1372. mutex_lock(&sdata->local->chanctx_mtx);
  1373. __ieee80211_vif_release_channel(sdata);
  1374. mutex_unlock(&sdata->local->chanctx_mtx);
  1375. }
  1376. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  1377. {
  1378. struct ieee80211_local *local = sdata->local;
  1379. struct ieee80211_sub_if_data *ap;
  1380. struct ieee80211_chanctx_conf *conf;
  1381. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  1382. return;
  1383. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  1384. mutex_lock(&local->chanctx_mtx);
  1385. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  1386. lockdep_is_held(&local->chanctx_mtx));
  1387. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  1388. mutex_unlock(&local->chanctx_mtx);
  1389. }
  1390. void ieee80211_iter_chan_contexts_atomic(
  1391. struct ieee80211_hw *hw,
  1392. void (*iter)(struct ieee80211_hw *hw,
  1393. struct ieee80211_chanctx_conf *chanctx_conf,
  1394. void *data),
  1395. void *iter_data)
  1396. {
  1397. struct ieee80211_local *local = hw_to_local(hw);
  1398. struct ieee80211_chanctx *ctx;
  1399. rcu_read_lock();
  1400. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  1401. if (ctx->driver_present)
  1402. iter(hw, &ctx->conf, iter_data);
  1403. rcu_read_unlock();
  1404. }
  1405. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);