chan.c 45 KB

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