chan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
  11. {
  12. switch (sta->bandwidth) {
  13. case IEEE80211_STA_RX_BW_20:
  14. if (sta->ht_cap.ht_supported)
  15. return NL80211_CHAN_WIDTH_20;
  16. else
  17. return NL80211_CHAN_WIDTH_20_NOHT;
  18. case IEEE80211_STA_RX_BW_40:
  19. return NL80211_CHAN_WIDTH_40;
  20. case IEEE80211_STA_RX_BW_80:
  21. return NL80211_CHAN_WIDTH_80;
  22. case IEEE80211_STA_RX_BW_160:
  23. /*
  24. * This applied for both 160 and 80+80. since we use
  25. * the returned value to consider degradation of
  26. * ctx->conf.min_def, we have to make sure to take
  27. * the bigger one (NL80211_CHAN_WIDTH_160).
  28. * Otherwise we might try degrading even when not
  29. * needed, as the max required sta_bw returned (80+80)
  30. * might be smaller than the configured bw (160).
  31. */
  32. return NL80211_CHAN_WIDTH_160;
  33. default:
  34. WARN_ON(1);
  35. return NL80211_CHAN_WIDTH_20;
  36. }
  37. }
  38. static enum nl80211_chan_width
  39. ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
  40. {
  41. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  42. struct sta_info *sta;
  43. rcu_read_lock();
  44. list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  45. if (sdata != sta->sdata &&
  46. !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
  47. continue;
  48. if (!sta->uploaded)
  49. continue;
  50. max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
  51. }
  52. rcu_read_unlock();
  53. return max_bw;
  54. }
  55. static enum nl80211_chan_width
  56. ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
  57. struct ieee80211_chanctx_conf *conf)
  58. {
  59. struct ieee80211_sub_if_data *sdata;
  60. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  61. rcu_read_lock();
  62. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  63. struct ieee80211_vif *vif = &sdata->vif;
  64. enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
  65. if (!ieee80211_sdata_running(sdata))
  66. continue;
  67. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  68. continue;
  69. switch (vif->type) {
  70. case NL80211_IFTYPE_AP:
  71. case NL80211_IFTYPE_AP_VLAN:
  72. width = ieee80211_get_max_required_bw(sdata);
  73. break;
  74. case NL80211_IFTYPE_P2P_DEVICE:
  75. continue;
  76. case NL80211_IFTYPE_STATION:
  77. case NL80211_IFTYPE_ADHOC:
  78. case NL80211_IFTYPE_WDS:
  79. case NL80211_IFTYPE_MESH_POINT:
  80. width = vif->bss_conf.chandef.width;
  81. break;
  82. case NL80211_IFTYPE_UNSPECIFIED:
  83. case NUM_NL80211_IFTYPES:
  84. case NL80211_IFTYPE_MONITOR:
  85. case NL80211_IFTYPE_P2P_CLIENT:
  86. case NL80211_IFTYPE_P2P_GO:
  87. WARN_ON_ONCE(1);
  88. }
  89. max_bw = max(max_bw, width);
  90. }
  91. /* use the configured bandwidth in case of monitor interface */
  92. sdata = rcu_dereference(local->monitor_sdata);
  93. if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
  94. max_bw = max(max_bw, conf->def.width);
  95. rcu_read_unlock();
  96. return max_bw;
  97. }
  98. /*
  99. * recalc the min required chan width of the channel context, which is
  100. * the max of min required widths of all the interfaces bound to this
  101. * channel context.
  102. */
  103. void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
  104. struct ieee80211_chanctx *ctx)
  105. {
  106. enum nl80211_chan_width max_bw;
  107. struct cfg80211_chan_def min_def;
  108. lockdep_assert_held(&local->chanctx_mtx);
  109. /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
  110. if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
  111. ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
  112. ctx->conf.radar_enabled) {
  113. ctx->conf.min_def = ctx->conf.def;
  114. return;
  115. }
  116. max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
  117. /* downgrade chandef up to max_bw */
  118. min_def = ctx->conf.def;
  119. while (min_def.width > max_bw)
  120. ieee80211_chandef_downgrade(&min_def);
  121. if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
  122. return;
  123. ctx->conf.min_def = min_def;
  124. if (!ctx->driver_present)
  125. return;
  126. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
  127. }
  128. static void ieee80211_change_chanctx(struct ieee80211_local *local,
  129. struct ieee80211_chanctx *ctx,
  130. const struct cfg80211_chan_def *chandef)
  131. {
  132. if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
  133. return;
  134. WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
  135. ctx->conf.def = *chandef;
  136. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
  137. ieee80211_recalc_chanctx_min_def(local, ctx);
  138. if (!local->use_chanctx) {
  139. local->_oper_chandef = *chandef;
  140. ieee80211_hw_config(local, 0);
  141. }
  142. }
  143. static struct ieee80211_chanctx *
  144. ieee80211_find_chanctx(struct ieee80211_local *local,
  145. const struct cfg80211_chan_def *chandef,
  146. enum ieee80211_chanctx_mode mode)
  147. {
  148. struct ieee80211_chanctx *ctx;
  149. lockdep_assert_held(&local->chanctx_mtx);
  150. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  151. return NULL;
  152. list_for_each_entry(ctx, &local->chanctx_list, list) {
  153. const struct cfg80211_chan_def *compat;
  154. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  155. continue;
  156. compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
  157. if (!compat)
  158. continue;
  159. ieee80211_change_chanctx(local, ctx, compat);
  160. return ctx;
  161. }
  162. return NULL;
  163. }
  164. static bool ieee80211_is_radar_required(struct ieee80211_local *local)
  165. {
  166. struct ieee80211_sub_if_data *sdata;
  167. lockdep_assert_held(&local->mtx);
  168. rcu_read_lock();
  169. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  170. if (sdata->radar_required) {
  171. rcu_read_unlock();
  172. return true;
  173. }
  174. }
  175. rcu_read_unlock();
  176. return false;
  177. }
  178. static struct ieee80211_chanctx *
  179. ieee80211_new_chanctx(struct ieee80211_local *local,
  180. const struct cfg80211_chan_def *chandef,
  181. enum ieee80211_chanctx_mode mode)
  182. {
  183. struct ieee80211_chanctx *ctx;
  184. u32 changed;
  185. int err;
  186. lockdep_assert_held(&local->chanctx_mtx);
  187. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  188. if (!ctx)
  189. return ERR_PTR(-ENOMEM);
  190. ctx->conf.def = *chandef;
  191. ctx->conf.rx_chains_static = 1;
  192. ctx->conf.rx_chains_dynamic = 1;
  193. ctx->mode = mode;
  194. ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
  195. ieee80211_recalc_chanctx_min_def(local, ctx);
  196. if (!local->use_chanctx)
  197. local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
  198. /* we hold the mutex to prevent idle from changing */
  199. lockdep_assert_held(&local->mtx);
  200. /* turn idle off *before* setting channel -- some drivers need that */
  201. changed = ieee80211_idle_off(local);
  202. if (changed)
  203. ieee80211_hw_config(local, changed);
  204. if (!local->use_chanctx) {
  205. local->_oper_chandef = *chandef;
  206. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  207. } else {
  208. err = drv_add_chanctx(local, ctx);
  209. if (err) {
  210. kfree(ctx);
  211. ieee80211_recalc_idle(local);
  212. return ERR_PTR(err);
  213. }
  214. }
  215. /* and keep the mutex held until the new chanctx is on the list */
  216. list_add_rcu(&ctx->list, &local->chanctx_list);
  217. return ctx;
  218. }
  219. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  220. struct ieee80211_chanctx *ctx)
  221. {
  222. bool check_single_channel = false;
  223. lockdep_assert_held(&local->chanctx_mtx);
  224. WARN_ON_ONCE(ctx->refcount != 0);
  225. if (!local->use_chanctx) {
  226. struct cfg80211_chan_def *chandef = &local->_oper_chandef;
  227. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  228. chandef->center_freq1 = chandef->chan->center_freq;
  229. chandef->center_freq2 = 0;
  230. /* NOTE: Disabling radar is only valid here for
  231. * single channel context. To be sure, check it ...
  232. */
  233. if (local->hw.conf.radar_enabled)
  234. check_single_channel = true;
  235. local->hw.conf.radar_enabled = false;
  236. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  237. } else {
  238. drv_remove_chanctx(local, ctx);
  239. }
  240. list_del_rcu(&ctx->list);
  241. kfree_rcu(ctx, rcu_head);
  242. /* throw a warning if this wasn't the only channel context. */
  243. WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
  244. ieee80211_recalc_idle(local);
  245. }
  246. static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  247. struct ieee80211_chanctx *ctx)
  248. {
  249. struct ieee80211_local *local = sdata->local;
  250. int ret;
  251. lockdep_assert_held(&local->chanctx_mtx);
  252. ret = drv_assign_vif_chanctx(local, sdata, ctx);
  253. if (ret)
  254. return ret;
  255. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  256. ctx->refcount++;
  257. ieee80211_recalc_txpower(sdata);
  258. ieee80211_recalc_chanctx_min_def(local, ctx);
  259. sdata->vif.bss_conf.idle = false;
  260. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  261. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  262. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  263. return 0;
  264. }
  265. static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
  266. struct ieee80211_chanctx *ctx)
  267. {
  268. struct ieee80211_chanctx_conf *conf = &ctx->conf;
  269. struct ieee80211_sub_if_data *sdata;
  270. const struct cfg80211_chan_def *compat = NULL;
  271. lockdep_assert_held(&local->chanctx_mtx);
  272. rcu_read_lock();
  273. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  274. if (!ieee80211_sdata_running(sdata))
  275. continue;
  276. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  277. continue;
  278. if (!compat)
  279. compat = &sdata->vif.bss_conf.chandef;
  280. compat = cfg80211_chandef_compatible(
  281. &sdata->vif.bss_conf.chandef, compat);
  282. if (!compat)
  283. break;
  284. }
  285. rcu_read_unlock();
  286. if (WARN_ON_ONCE(!compat))
  287. return;
  288. ieee80211_change_chanctx(local, ctx, compat);
  289. }
  290. static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
  291. struct ieee80211_chanctx *chanctx)
  292. {
  293. bool radar_enabled;
  294. lockdep_assert_held(&local->chanctx_mtx);
  295. /* for setting local->radar_detect_enabled */
  296. lockdep_assert_held(&local->mtx);
  297. radar_enabled = ieee80211_is_radar_required(local);
  298. if (radar_enabled == chanctx->conf.radar_enabled)
  299. return;
  300. chanctx->conf.radar_enabled = radar_enabled;
  301. local->radar_detect_enabled = chanctx->conf.radar_enabled;
  302. if (!local->use_chanctx) {
  303. local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
  304. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  305. }
  306. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
  307. }
  308. static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  309. struct ieee80211_chanctx *ctx)
  310. {
  311. struct ieee80211_local *local = sdata->local;
  312. lockdep_assert_held(&local->chanctx_mtx);
  313. ctx->refcount--;
  314. rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
  315. sdata->vif.bss_conf.idle = true;
  316. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  317. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  318. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  319. drv_unassign_vif_chanctx(local, sdata, ctx);
  320. if (ctx->refcount > 0) {
  321. ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
  322. ieee80211_recalc_smps_chanctx(local, ctx);
  323. ieee80211_recalc_radar_chanctx(local, ctx);
  324. ieee80211_recalc_chanctx_min_def(local, ctx);
  325. }
  326. }
  327. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  328. {
  329. struct ieee80211_local *local = sdata->local;
  330. struct ieee80211_chanctx_conf *conf;
  331. struct ieee80211_chanctx *ctx;
  332. lockdep_assert_held(&local->chanctx_mtx);
  333. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  334. lockdep_is_held(&local->chanctx_mtx));
  335. if (!conf)
  336. return;
  337. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  338. ieee80211_unassign_vif_chanctx(sdata, ctx);
  339. if (ctx->refcount == 0)
  340. ieee80211_free_chanctx(local, ctx);
  341. }
  342. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  343. struct ieee80211_chanctx *chanctx)
  344. {
  345. struct ieee80211_sub_if_data *sdata;
  346. u8 rx_chains_static, rx_chains_dynamic;
  347. lockdep_assert_held(&local->chanctx_mtx);
  348. rx_chains_static = 1;
  349. rx_chains_dynamic = 1;
  350. rcu_read_lock();
  351. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  352. u8 needed_static, needed_dynamic;
  353. if (!ieee80211_sdata_running(sdata))
  354. continue;
  355. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  356. &chanctx->conf)
  357. continue;
  358. switch (sdata->vif.type) {
  359. case NL80211_IFTYPE_P2P_DEVICE:
  360. continue;
  361. case NL80211_IFTYPE_STATION:
  362. if (!sdata->u.mgd.associated)
  363. continue;
  364. break;
  365. case NL80211_IFTYPE_AP_VLAN:
  366. continue;
  367. case NL80211_IFTYPE_AP:
  368. case NL80211_IFTYPE_ADHOC:
  369. case NL80211_IFTYPE_WDS:
  370. case NL80211_IFTYPE_MESH_POINT:
  371. break;
  372. default:
  373. WARN_ON_ONCE(1);
  374. }
  375. switch (sdata->smps_mode) {
  376. default:
  377. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  378. sdata->smps_mode);
  379. /* fall through */
  380. case IEEE80211_SMPS_OFF:
  381. needed_static = sdata->needed_rx_chains;
  382. needed_dynamic = sdata->needed_rx_chains;
  383. break;
  384. case IEEE80211_SMPS_DYNAMIC:
  385. needed_static = 1;
  386. needed_dynamic = sdata->needed_rx_chains;
  387. break;
  388. case IEEE80211_SMPS_STATIC:
  389. needed_static = 1;
  390. needed_dynamic = 1;
  391. break;
  392. }
  393. rx_chains_static = max(rx_chains_static, needed_static);
  394. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  395. }
  396. /* Disable SMPS for the monitor interface */
  397. sdata = rcu_dereference(local->monitor_sdata);
  398. if (sdata &&
  399. rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
  400. rx_chains_dynamic = rx_chains_static = local->rx_chains;
  401. rcu_read_unlock();
  402. if (!local->use_chanctx) {
  403. if (rx_chains_static > 1)
  404. local->smps_mode = IEEE80211_SMPS_OFF;
  405. else if (rx_chains_dynamic > 1)
  406. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  407. else
  408. local->smps_mode = IEEE80211_SMPS_STATIC;
  409. ieee80211_hw_config(local, 0);
  410. }
  411. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  412. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  413. return;
  414. chanctx->conf.rx_chains_static = rx_chains_static;
  415. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  416. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  417. }
  418. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  419. const struct cfg80211_chan_def *chandef,
  420. enum ieee80211_chanctx_mode mode)
  421. {
  422. struct ieee80211_local *local = sdata->local;
  423. struct ieee80211_chanctx *ctx;
  424. int ret;
  425. lockdep_assert_held(&local->mtx);
  426. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  427. mutex_lock(&local->chanctx_mtx);
  428. __ieee80211_vif_release_channel(sdata);
  429. ctx = ieee80211_find_chanctx(local, chandef, mode);
  430. if (!ctx)
  431. ctx = ieee80211_new_chanctx(local, chandef, mode);
  432. if (IS_ERR(ctx)) {
  433. ret = PTR_ERR(ctx);
  434. goto out;
  435. }
  436. sdata->vif.bss_conf.chandef = *chandef;
  437. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  438. if (ret) {
  439. /* if assign fails refcount stays the same */
  440. if (ctx->refcount == 0)
  441. ieee80211_free_chanctx(local, ctx);
  442. goto out;
  443. }
  444. ieee80211_recalc_smps_chanctx(local, ctx);
  445. ieee80211_recalc_radar_chanctx(local, ctx);
  446. out:
  447. mutex_unlock(&local->chanctx_mtx);
  448. return ret;
  449. }
  450. int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
  451. u32 *changed)
  452. {
  453. struct ieee80211_local *local = sdata->local;
  454. struct ieee80211_chanctx_conf *conf;
  455. struct ieee80211_chanctx *ctx;
  456. const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
  457. int ret;
  458. u32 chanctx_changed = 0;
  459. lockdep_assert_held(&local->mtx);
  460. /* should never be called if not performing a channel switch. */
  461. if (WARN_ON(!sdata->vif.csa_active))
  462. return -EINVAL;
  463. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  464. IEEE80211_CHAN_DISABLED))
  465. return -EINVAL;
  466. mutex_lock(&local->chanctx_mtx);
  467. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  468. lockdep_is_held(&local->chanctx_mtx));
  469. if (!conf) {
  470. ret = -EINVAL;
  471. goto out;
  472. }
  473. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  474. if (ctx->refcount != 1) {
  475. ret = -EINVAL;
  476. goto out;
  477. }
  478. if (sdata->vif.bss_conf.chandef.width != chandef->width) {
  479. chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
  480. *changed |= BSS_CHANGED_BANDWIDTH;
  481. }
  482. sdata->vif.bss_conf.chandef = *chandef;
  483. ctx->conf.def = *chandef;
  484. chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
  485. drv_change_chanctx(local, ctx, chanctx_changed);
  486. ieee80211_recalc_chanctx_chantype(local, ctx);
  487. ieee80211_recalc_smps_chanctx(local, ctx);
  488. ieee80211_recalc_radar_chanctx(local, ctx);
  489. ieee80211_recalc_chanctx_min_def(local, ctx);
  490. ret = 0;
  491. out:
  492. mutex_unlock(&local->chanctx_mtx);
  493. return ret;
  494. }
  495. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  496. const struct cfg80211_chan_def *chandef,
  497. u32 *changed)
  498. {
  499. struct ieee80211_local *local = sdata->local;
  500. struct ieee80211_chanctx_conf *conf;
  501. struct ieee80211_chanctx *ctx;
  502. int ret;
  503. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  504. IEEE80211_CHAN_DISABLED))
  505. return -EINVAL;
  506. mutex_lock(&local->chanctx_mtx);
  507. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  508. ret = 0;
  509. goto out;
  510. }
  511. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  512. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  513. ret = -EINVAL;
  514. goto out;
  515. }
  516. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  517. lockdep_is_held(&local->chanctx_mtx));
  518. if (!conf) {
  519. ret = -EINVAL;
  520. goto out;
  521. }
  522. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  523. if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
  524. ret = -EINVAL;
  525. goto out;
  526. }
  527. sdata->vif.bss_conf.chandef = *chandef;
  528. ieee80211_recalc_chanctx_chantype(local, ctx);
  529. *changed |= BSS_CHANGED_BANDWIDTH;
  530. ret = 0;
  531. out:
  532. mutex_unlock(&local->chanctx_mtx);
  533. return ret;
  534. }
  535. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  536. {
  537. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  538. lockdep_assert_held(&sdata->local->mtx);
  539. mutex_lock(&sdata->local->chanctx_mtx);
  540. __ieee80211_vif_release_channel(sdata);
  541. mutex_unlock(&sdata->local->chanctx_mtx);
  542. }
  543. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  544. {
  545. struct ieee80211_local *local = sdata->local;
  546. struct ieee80211_sub_if_data *ap;
  547. struct ieee80211_chanctx_conf *conf;
  548. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  549. return;
  550. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  551. mutex_lock(&local->chanctx_mtx);
  552. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  553. lockdep_is_held(&local->chanctx_mtx));
  554. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  555. mutex_unlock(&local->chanctx_mtx);
  556. }
  557. void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  558. bool clear)
  559. {
  560. struct ieee80211_local *local = sdata->local;
  561. struct ieee80211_sub_if_data *vlan;
  562. struct ieee80211_chanctx_conf *conf;
  563. ASSERT_RTNL();
  564. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
  565. return;
  566. mutex_lock(&local->chanctx_mtx);
  567. /*
  568. * Check that conf exists, even when clearing this function
  569. * must be called with the AP's channel context still there
  570. * as it would otherwise cause VLANs to have an invalid
  571. * channel context pointer for a while, possibly pointing
  572. * to a channel context that has already been freed.
  573. */
  574. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  575. lockdep_is_held(&local->chanctx_mtx));
  576. WARN_ON(!conf);
  577. if (clear)
  578. conf = NULL;
  579. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  580. rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
  581. mutex_unlock(&local->chanctx_mtx);
  582. }
  583. void ieee80211_iter_chan_contexts_atomic(
  584. struct ieee80211_hw *hw,
  585. void (*iter)(struct ieee80211_hw *hw,
  586. struct ieee80211_chanctx_conf *chanctx_conf,
  587. void *data),
  588. void *iter_data)
  589. {
  590. struct ieee80211_local *local = hw_to_local(hw);
  591. struct ieee80211_chanctx *ctx;
  592. rcu_read_lock();
  593. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  594. if (ctx->driver_present)
  595. iter(hw, &ctx->conf, iter_data);
  596. rcu_read_unlock();
  597. }
  598. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);