chan.c 19 KB

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