chan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. rcu_read_unlock();
  92. return max_bw;
  93. }
  94. /*
  95. * recalc the min required chan width of the channel context, which is
  96. * the max of min required widths of all the interfaces bound to this
  97. * channel context.
  98. */
  99. void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
  100. struct ieee80211_chanctx *ctx)
  101. {
  102. enum nl80211_chan_width max_bw;
  103. struct cfg80211_chan_def min_def;
  104. lockdep_assert_held(&local->chanctx_mtx);
  105. /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
  106. if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
  107. ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
  108. ctx->conf.radar_enabled) {
  109. ctx->conf.min_def = ctx->conf.def;
  110. return;
  111. }
  112. max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
  113. /* downgrade chandef up to max_bw */
  114. min_def = ctx->conf.def;
  115. while (min_def.width > max_bw)
  116. ieee80211_chandef_downgrade(&min_def);
  117. if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
  118. return;
  119. ctx->conf.min_def = min_def;
  120. if (!ctx->driver_present)
  121. return;
  122. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
  123. }
  124. static void ieee80211_change_chanctx(struct ieee80211_local *local,
  125. struct ieee80211_chanctx *ctx,
  126. const struct cfg80211_chan_def *chandef)
  127. {
  128. if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
  129. return;
  130. WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
  131. ctx->conf.def = *chandef;
  132. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
  133. ieee80211_recalc_chanctx_min_def(local, ctx);
  134. if (!local->use_chanctx) {
  135. local->_oper_chandef = *chandef;
  136. ieee80211_hw_config(local, 0);
  137. }
  138. }
  139. static struct ieee80211_chanctx *
  140. ieee80211_find_chanctx(struct ieee80211_local *local,
  141. const struct cfg80211_chan_def *chandef,
  142. enum ieee80211_chanctx_mode mode)
  143. {
  144. struct ieee80211_chanctx *ctx;
  145. lockdep_assert_held(&local->chanctx_mtx);
  146. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  147. return NULL;
  148. list_for_each_entry(ctx, &local->chanctx_list, list) {
  149. const struct cfg80211_chan_def *compat;
  150. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  151. continue;
  152. compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
  153. if (!compat)
  154. continue;
  155. ieee80211_change_chanctx(local, ctx, compat);
  156. return ctx;
  157. }
  158. return NULL;
  159. }
  160. static bool ieee80211_is_radar_required(struct ieee80211_local *local)
  161. {
  162. struct ieee80211_sub_if_data *sdata;
  163. rcu_read_lock();
  164. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  165. if (sdata->radar_required) {
  166. rcu_read_unlock();
  167. return true;
  168. }
  169. }
  170. rcu_read_unlock();
  171. return false;
  172. }
  173. static struct ieee80211_chanctx *
  174. ieee80211_new_chanctx(struct ieee80211_local *local,
  175. const struct cfg80211_chan_def *chandef,
  176. enum ieee80211_chanctx_mode mode)
  177. {
  178. struct ieee80211_chanctx *ctx;
  179. u32 changed;
  180. int err;
  181. lockdep_assert_held(&local->chanctx_mtx);
  182. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  183. if (!ctx)
  184. return ERR_PTR(-ENOMEM);
  185. ctx->conf.def = *chandef;
  186. ctx->conf.rx_chains_static = 1;
  187. ctx->conf.rx_chains_dynamic = 1;
  188. ctx->mode = mode;
  189. ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
  190. ieee80211_recalc_chanctx_min_def(local, ctx);
  191. if (!local->use_chanctx)
  192. local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
  193. /* acquire mutex to prevent idle from changing */
  194. mutex_lock(&local->mtx);
  195. /* turn idle off *before* setting channel -- some drivers need that */
  196. changed = ieee80211_idle_off(local);
  197. if (changed)
  198. ieee80211_hw_config(local, changed);
  199. if (!local->use_chanctx) {
  200. local->_oper_chandef = *chandef;
  201. ieee80211_hw_config(local, 0);
  202. } else {
  203. err = drv_add_chanctx(local, ctx);
  204. if (err) {
  205. kfree(ctx);
  206. ctx = ERR_PTR(err);
  207. ieee80211_recalc_idle(local);
  208. goto out;
  209. }
  210. }
  211. /* and keep the mutex held until the new chanctx is on the list */
  212. list_add_rcu(&ctx->list, &local->chanctx_list);
  213. out:
  214. mutex_unlock(&local->mtx);
  215. return ctx;
  216. }
  217. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  218. struct ieee80211_chanctx *ctx)
  219. {
  220. bool check_single_channel = false;
  221. lockdep_assert_held(&local->chanctx_mtx);
  222. WARN_ON_ONCE(ctx->refcount != 0);
  223. if (!local->use_chanctx) {
  224. struct cfg80211_chan_def *chandef = &local->_oper_chandef;
  225. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  226. chandef->center_freq1 = chandef->chan->center_freq;
  227. chandef->center_freq2 = 0;
  228. /* NOTE: Disabling radar is only valid here for
  229. * single channel context. To be sure, check it ...
  230. */
  231. if (local->hw.conf.radar_enabled)
  232. check_single_channel = true;
  233. local->hw.conf.radar_enabled = false;
  234. ieee80211_hw_config(local, 0);
  235. } else {
  236. drv_remove_chanctx(local, ctx);
  237. }
  238. list_del_rcu(&ctx->list);
  239. kfree_rcu(ctx, rcu_head);
  240. /* throw a warning if this wasn't the only channel context. */
  241. WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
  242. mutex_lock(&local->mtx);
  243. ieee80211_recalc_idle(local);
  244. mutex_unlock(&local->mtx);
  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_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  291. struct ieee80211_chanctx *ctx)
  292. {
  293. struct ieee80211_local *local = sdata->local;
  294. lockdep_assert_held(&local->chanctx_mtx);
  295. ctx->refcount--;
  296. rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
  297. sdata->vif.bss_conf.idle = true;
  298. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  299. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  300. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  301. drv_unassign_vif_chanctx(local, sdata, ctx);
  302. if (ctx->refcount > 0) {
  303. ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
  304. ieee80211_recalc_smps_chanctx(local, ctx);
  305. ieee80211_recalc_radar_chanctx(local, ctx);
  306. ieee80211_recalc_chanctx_min_def(local, ctx);
  307. }
  308. }
  309. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  310. {
  311. struct ieee80211_local *local = sdata->local;
  312. struct ieee80211_chanctx_conf *conf;
  313. struct ieee80211_chanctx *ctx;
  314. lockdep_assert_held(&local->chanctx_mtx);
  315. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  316. lockdep_is_held(&local->chanctx_mtx));
  317. if (!conf)
  318. return;
  319. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  320. ieee80211_unassign_vif_chanctx(sdata, ctx);
  321. if (ctx->refcount == 0)
  322. ieee80211_free_chanctx(local, ctx);
  323. }
  324. void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
  325. struct ieee80211_chanctx *chanctx)
  326. {
  327. bool radar_enabled;
  328. lockdep_assert_held(&local->chanctx_mtx);
  329. radar_enabled = ieee80211_is_radar_required(local);
  330. if (radar_enabled == chanctx->conf.radar_enabled)
  331. return;
  332. chanctx->conf.radar_enabled = radar_enabled;
  333. local->radar_detect_enabled = chanctx->conf.radar_enabled;
  334. if (!local->use_chanctx) {
  335. local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
  336. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  337. }
  338. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
  339. }
  340. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  341. struct ieee80211_chanctx *chanctx)
  342. {
  343. struct ieee80211_sub_if_data *sdata;
  344. u8 rx_chains_static, rx_chains_dynamic;
  345. lockdep_assert_held(&local->chanctx_mtx);
  346. rx_chains_static = 1;
  347. rx_chains_dynamic = 1;
  348. rcu_read_lock();
  349. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  350. u8 needed_static, needed_dynamic;
  351. if (!ieee80211_sdata_running(sdata))
  352. continue;
  353. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  354. &chanctx->conf)
  355. continue;
  356. switch (sdata->vif.type) {
  357. case NL80211_IFTYPE_P2P_DEVICE:
  358. continue;
  359. case NL80211_IFTYPE_STATION:
  360. if (!sdata->u.mgd.associated)
  361. continue;
  362. break;
  363. case NL80211_IFTYPE_AP_VLAN:
  364. continue;
  365. case NL80211_IFTYPE_AP:
  366. case NL80211_IFTYPE_ADHOC:
  367. case NL80211_IFTYPE_WDS:
  368. case NL80211_IFTYPE_MESH_POINT:
  369. break;
  370. default:
  371. WARN_ON_ONCE(1);
  372. }
  373. switch (sdata->smps_mode) {
  374. default:
  375. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  376. sdata->smps_mode);
  377. /* fall through */
  378. case IEEE80211_SMPS_OFF:
  379. needed_static = sdata->needed_rx_chains;
  380. needed_dynamic = sdata->needed_rx_chains;
  381. break;
  382. case IEEE80211_SMPS_DYNAMIC:
  383. needed_static = 1;
  384. needed_dynamic = sdata->needed_rx_chains;
  385. break;
  386. case IEEE80211_SMPS_STATIC:
  387. needed_static = 1;
  388. needed_dynamic = 1;
  389. break;
  390. }
  391. rx_chains_static = max(rx_chains_static, needed_static);
  392. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  393. }
  394. rcu_read_unlock();
  395. if (!local->use_chanctx) {
  396. if (rx_chains_static > 1)
  397. local->smps_mode = IEEE80211_SMPS_OFF;
  398. else if (rx_chains_dynamic > 1)
  399. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  400. else
  401. local->smps_mode = IEEE80211_SMPS_STATIC;
  402. ieee80211_hw_config(local, 0);
  403. }
  404. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  405. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  406. return;
  407. chanctx->conf.rx_chains_static = rx_chains_static;
  408. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  409. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  410. }
  411. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  412. const struct cfg80211_chan_def *chandef,
  413. enum ieee80211_chanctx_mode mode)
  414. {
  415. struct ieee80211_local *local = sdata->local;
  416. struct ieee80211_chanctx *ctx;
  417. int ret;
  418. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  419. mutex_lock(&local->chanctx_mtx);
  420. __ieee80211_vif_release_channel(sdata);
  421. ctx = ieee80211_find_chanctx(local, chandef, mode);
  422. if (!ctx)
  423. ctx = ieee80211_new_chanctx(local, chandef, mode);
  424. if (IS_ERR(ctx)) {
  425. ret = PTR_ERR(ctx);
  426. goto out;
  427. }
  428. sdata->vif.bss_conf.chandef = *chandef;
  429. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  430. if (ret) {
  431. /* if assign fails refcount stays the same */
  432. if (ctx->refcount == 0)
  433. ieee80211_free_chanctx(local, ctx);
  434. goto out;
  435. }
  436. ieee80211_recalc_smps_chanctx(local, ctx);
  437. ieee80211_recalc_radar_chanctx(local, ctx);
  438. out:
  439. mutex_unlock(&local->chanctx_mtx);
  440. return ret;
  441. }
  442. int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
  443. u32 *changed)
  444. {
  445. struct ieee80211_local *local = sdata->local;
  446. struct ieee80211_chanctx_conf *conf;
  447. struct ieee80211_chanctx *ctx;
  448. const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
  449. int ret;
  450. u32 chanctx_changed = 0;
  451. /* should never be called if not performing a channel switch. */
  452. if (WARN_ON(!sdata->vif.csa_active))
  453. return -EINVAL;
  454. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  455. IEEE80211_CHAN_DISABLED))
  456. return -EINVAL;
  457. mutex_lock(&local->chanctx_mtx);
  458. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  459. lockdep_is_held(&local->chanctx_mtx));
  460. if (!conf) {
  461. ret = -EINVAL;
  462. goto out;
  463. }
  464. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  465. if (ctx->refcount != 1) {
  466. ret = -EINVAL;
  467. goto out;
  468. }
  469. if (sdata->vif.bss_conf.chandef.width != chandef->width) {
  470. chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
  471. *changed |= BSS_CHANGED_BANDWIDTH;
  472. }
  473. sdata->vif.bss_conf.chandef = *chandef;
  474. ctx->conf.def = *chandef;
  475. chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
  476. drv_change_chanctx(local, ctx, chanctx_changed);
  477. ieee80211_recalc_chanctx_chantype(local, ctx);
  478. ieee80211_recalc_smps_chanctx(local, ctx);
  479. ieee80211_recalc_radar_chanctx(local, ctx);
  480. ieee80211_recalc_chanctx_min_def(local, ctx);
  481. ret = 0;
  482. out:
  483. mutex_unlock(&local->chanctx_mtx);
  484. return ret;
  485. }
  486. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  487. const struct cfg80211_chan_def *chandef,
  488. u32 *changed)
  489. {
  490. struct ieee80211_local *local = sdata->local;
  491. struct ieee80211_chanctx_conf *conf;
  492. struct ieee80211_chanctx *ctx;
  493. int ret;
  494. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  495. IEEE80211_CHAN_DISABLED))
  496. return -EINVAL;
  497. mutex_lock(&local->chanctx_mtx);
  498. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  499. ret = 0;
  500. goto out;
  501. }
  502. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  503. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  504. ret = -EINVAL;
  505. goto out;
  506. }
  507. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  508. lockdep_is_held(&local->chanctx_mtx));
  509. if (!conf) {
  510. ret = -EINVAL;
  511. goto out;
  512. }
  513. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  514. if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
  515. ret = -EINVAL;
  516. goto out;
  517. }
  518. sdata->vif.bss_conf.chandef = *chandef;
  519. ieee80211_recalc_chanctx_chantype(local, ctx);
  520. *changed |= BSS_CHANGED_BANDWIDTH;
  521. ret = 0;
  522. out:
  523. mutex_unlock(&local->chanctx_mtx);
  524. return ret;
  525. }
  526. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  527. {
  528. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  529. mutex_lock(&sdata->local->chanctx_mtx);
  530. __ieee80211_vif_release_channel(sdata);
  531. mutex_unlock(&sdata->local->chanctx_mtx);
  532. }
  533. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  534. {
  535. struct ieee80211_local *local = sdata->local;
  536. struct ieee80211_sub_if_data *ap;
  537. struct ieee80211_chanctx_conf *conf;
  538. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  539. return;
  540. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  541. mutex_lock(&local->chanctx_mtx);
  542. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  543. lockdep_is_held(&local->chanctx_mtx));
  544. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  545. mutex_unlock(&local->chanctx_mtx);
  546. }
  547. void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  548. bool clear)
  549. {
  550. struct ieee80211_local *local = sdata->local;
  551. struct ieee80211_sub_if_data *vlan;
  552. struct ieee80211_chanctx_conf *conf;
  553. ASSERT_RTNL();
  554. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
  555. return;
  556. mutex_lock(&local->chanctx_mtx);
  557. /*
  558. * Check that conf exists, even when clearing this function
  559. * must be called with the AP's channel context still there
  560. * as it would otherwise cause VLANs to have an invalid
  561. * channel context pointer for a while, possibly pointing
  562. * to a channel context that has already been freed.
  563. */
  564. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  565. lockdep_is_held(&local->chanctx_mtx));
  566. WARN_ON(!conf);
  567. if (clear)
  568. conf = NULL;
  569. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  570. rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
  571. mutex_unlock(&local->chanctx_mtx);
  572. }
  573. void ieee80211_iter_chan_contexts_atomic(
  574. struct ieee80211_hw *hw,
  575. void (*iter)(struct ieee80211_hw *hw,
  576. struct ieee80211_chanctx_conf *chanctx_conf,
  577. void *data),
  578. void *iter_data)
  579. {
  580. struct ieee80211_local *local = hw_to_local(hw);
  581. struct ieee80211_chanctx *ctx;
  582. rcu_read_lock();
  583. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  584. if (ctx->driver_present)
  585. iter(hw, &ctx->conf, iter_data);
  586. rcu_read_unlock();
  587. }
  588. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);