chan.c 45 KB

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