chan.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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 const struct cfg80211_chan_def *
  51. ieee80211_chanctx_reserved_chandef(struct ieee80211_local *local,
  52. struct ieee80211_chanctx *ctx,
  53. const struct cfg80211_chan_def *compat)
  54. {
  55. struct ieee80211_sub_if_data *sdata;
  56. lockdep_assert_held(&local->chanctx_mtx);
  57. list_for_each_entry(sdata, &ctx->reserved_vifs,
  58. reserved_chanctx_list) {
  59. if (!compat)
  60. compat = &sdata->reserved_chandef;
  61. compat = cfg80211_chandef_compatible(&sdata->reserved_chandef,
  62. compat);
  63. if (!compat)
  64. break;
  65. }
  66. return compat;
  67. }
  68. static const struct cfg80211_chan_def *
  69. ieee80211_chanctx_non_reserved_chandef(struct ieee80211_local *local,
  70. struct ieee80211_chanctx *ctx,
  71. const struct cfg80211_chan_def *compat)
  72. {
  73. struct ieee80211_sub_if_data *sdata;
  74. lockdep_assert_held(&local->chanctx_mtx);
  75. list_for_each_entry(sdata, &ctx->assigned_vifs,
  76. assigned_chanctx_list) {
  77. if (sdata->reserved_chanctx != NULL)
  78. continue;
  79. if (!compat)
  80. compat = &sdata->vif.bss_conf.chandef;
  81. compat = cfg80211_chandef_compatible(
  82. &sdata->vif.bss_conf.chandef, compat);
  83. if (!compat)
  84. break;
  85. }
  86. return compat;
  87. }
  88. static const struct cfg80211_chan_def *
  89. ieee80211_chanctx_combined_chandef(struct ieee80211_local *local,
  90. struct ieee80211_chanctx *ctx,
  91. const struct cfg80211_chan_def *compat)
  92. {
  93. lockdep_assert_held(&local->chanctx_mtx);
  94. compat = ieee80211_chanctx_reserved_chandef(local, ctx, compat);
  95. if (!compat)
  96. return NULL;
  97. compat = ieee80211_chanctx_non_reserved_chandef(local, ctx, compat);
  98. if (!compat)
  99. return NULL;
  100. return compat;
  101. }
  102. static bool
  103. ieee80211_chanctx_can_reserve_chandef(struct ieee80211_local *local,
  104. struct ieee80211_chanctx *ctx,
  105. const struct cfg80211_chan_def *def)
  106. {
  107. lockdep_assert_held(&local->chanctx_mtx);
  108. if (ieee80211_chanctx_combined_chandef(local, ctx, def))
  109. return true;
  110. if (!list_empty(&ctx->reserved_vifs) &&
  111. ieee80211_chanctx_reserved_chandef(local, ctx, def))
  112. return true;
  113. return false;
  114. }
  115. static struct ieee80211_chanctx *
  116. ieee80211_find_reservation_chanctx(struct ieee80211_local *local,
  117. const struct cfg80211_chan_def *chandef,
  118. enum ieee80211_chanctx_mode mode)
  119. {
  120. struct ieee80211_chanctx *ctx;
  121. lockdep_assert_held(&local->chanctx_mtx);
  122. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  123. return NULL;
  124. list_for_each_entry(ctx, &local->chanctx_list, list) {
  125. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  126. continue;
  127. if (!ieee80211_chanctx_can_reserve_chandef(local, ctx,
  128. chandef))
  129. continue;
  130. return ctx;
  131. }
  132. return NULL;
  133. }
  134. static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
  135. {
  136. switch (sta->bandwidth) {
  137. case IEEE80211_STA_RX_BW_20:
  138. if (sta->ht_cap.ht_supported)
  139. return NL80211_CHAN_WIDTH_20;
  140. else
  141. return NL80211_CHAN_WIDTH_20_NOHT;
  142. case IEEE80211_STA_RX_BW_40:
  143. return NL80211_CHAN_WIDTH_40;
  144. case IEEE80211_STA_RX_BW_80:
  145. return NL80211_CHAN_WIDTH_80;
  146. case IEEE80211_STA_RX_BW_160:
  147. /*
  148. * This applied for both 160 and 80+80. since we use
  149. * the returned value to consider degradation of
  150. * ctx->conf.min_def, we have to make sure to take
  151. * the bigger one (NL80211_CHAN_WIDTH_160).
  152. * Otherwise we might try degrading even when not
  153. * needed, as the max required sta_bw returned (80+80)
  154. * might be smaller than the configured bw (160).
  155. */
  156. return NL80211_CHAN_WIDTH_160;
  157. default:
  158. WARN_ON(1);
  159. return NL80211_CHAN_WIDTH_20;
  160. }
  161. }
  162. static enum nl80211_chan_width
  163. ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
  164. {
  165. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  166. struct sta_info *sta;
  167. rcu_read_lock();
  168. list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  169. if (sdata != sta->sdata &&
  170. !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
  171. continue;
  172. if (!sta->uploaded)
  173. continue;
  174. max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
  175. }
  176. rcu_read_unlock();
  177. return max_bw;
  178. }
  179. static enum nl80211_chan_width
  180. ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
  181. struct ieee80211_chanctx_conf *conf)
  182. {
  183. struct ieee80211_sub_if_data *sdata;
  184. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  185. rcu_read_lock();
  186. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  187. struct ieee80211_vif *vif = &sdata->vif;
  188. enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
  189. if (!ieee80211_sdata_running(sdata))
  190. continue;
  191. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  192. continue;
  193. switch (vif->type) {
  194. case NL80211_IFTYPE_AP:
  195. case NL80211_IFTYPE_AP_VLAN:
  196. width = ieee80211_get_max_required_bw(sdata);
  197. break;
  198. case NL80211_IFTYPE_P2P_DEVICE:
  199. continue;
  200. case NL80211_IFTYPE_STATION:
  201. case NL80211_IFTYPE_ADHOC:
  202. case NL80211_IFTYPE_WDS:
  203. case NL80211_IFTYPE_MESH_POINT:
  204. width = vif->bss_conf.chandef.width;
  205. break;
  206. case NL80211_IFTYPE_UNSPECIFIED:
  207. case NUM_NL80211_IFTYPES:
  208. case NL80211_IFTYPE_MONITOR:
  209. case NL80211_IFTYPE_P2P_CLIENT:
  210. case NL80211_IFTYPE_P2P_GO:
  211. WARN_ON_ONCE(1);
  212. }
  213. max_bw = max(max_bw, width);
  214. }
  215. /* use the configured bandwidth in case of monitor interface */
  216. sdata = rcu_dereference(local->monitor_sdata);
  217. if (sdata && rcu_access_pointer(sdata->vif.chanctx_conf) == conf)
  218. max_bw = max(max_bw, conf->def.width);
  219. rcu_read_unlock();
  220. return max_bw;
  221. }
  222. /*
  223. * recalc the min required chan width of the channel context, which is
  224. * the max of min required widths of all the interfaces bound to this
  225. * channel context.
  226. */
  227. void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
  228. struct ieee80211_chanctx *ctx)
  229. {
  230. enum nl80211_chan_width max_bw;
  231. struct cfg80211_chan_def min_def;
  232. lockdep_assert_held(&local->chanctx_mtx);
  233. /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
  234. if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
  235. ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
  236. ctx->conf.radar_enabled) {
  237. ctx->conf.min_def = ctx->conf.def;
  238. return;
  239. }
  240. max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
  241. /* downgrade chandef up to max_bw */
  242. min_def = ctx->conf.def;
  243. while (min_def.width > max_bw)
  244. ieee80211_chandef_downgrade(&min_def);
  245. if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
  246. return;
  247. ctx->conf.min_def = min_def;
  248. if (!ctx->driver_present)
  249. return;
  250. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
  251. }
  252. static void ieee80211_change_chanctx(struct ieee80211_local *local,
  253. struct ieee80211_chanctx *ctx,
  254. const struct cfg80211_chan_def *chandef)
  255. {
  256. if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
  257. return;
  258. WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
  259. ctx->conf.def = *chandef;
  260. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
  261. ieee80211_recalc_chanctx_min_def(local, ctx);
  262. if (!local->use_chanctx) {
  263. local->_oper_chandef = *chandef;
  264. ieee80211_hw_config(local, 0);
  265. }
  266. }
  267. static struct ieee80211_chanctx *
  268. ieee80211_find_chanctx(struct ieee80211_local *local,
  269. const struct cfg80211_chan_def *chandef,
  270. enum ieee80211_chanctx_mode mode)
  271. {
  272. struct ieee80211_chanctx *ctx;
  273. lockdep_assert_held(&local->chanctx_mtx);
  274. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  275. return NULL;
  276. list_for_each_entry(ctx, &local->chanctx_list, list) {
  277. const struct cfg80211_chan_def *compat;
  278. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  279. continue;
  280. compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
  281. if (!compat)
  282. continue;
  283. compat = ieee80211_chanctx_reserved_chandef(local, ctx,
  284. compat);
  285. if (!compat)
  286. continue;
  287. ieee80211_change_chanctx(local, ctx, compat);
  288. return ctx;
  289. }
  290. return NULL;
  291. }
  292. static bool ieee80211_is_radar_required(struct ieee80211_local *local)
  293. {
  294. struct ieee80211_sub_if_data *sdata;
  295. lockdep_assert_held(&local->mtx);
  296. rcu_read_lock();
  297. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  298. if (sdata->radar_required) {
  299. rcu_read_unlock();
  300. return true;
  301. }
  302. }
  303. rcu_read_unlock();
  304. return false;
  305. }
  306. static struct ieee80211_chanctx *
  307. ieee80211_alloc_chanctx(struct ieee80211_local *local,
  308. const struct cfg80211_chan_def *chandef,
  309. enum ieee80211_chanctx_mode mode)
  310. {
  311. struct ieee80211_chanctx *ctx;
  312. lockdep_assert_held(&local->chanctx_mtx);
  313. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  314. if (!ctx)
  315. return NULL;
  316. INIT_LIST_HEAD(&ctx->assigned_vifs);
  317. INIT_LIST_HEAD(&ctx->reserved_vifs);
  318. ctx->conf.def = *chandef;
  319. ctx->conf.rx_chains_static = 1;
  320. ctx->conf.rx_chains_dynamic = 1;
  321. ctx->mode = mode;
  322. ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
  323. ieee80211_recalc_chanctx_min_def(local, ctx);
  324. return ctx;
  325. }
  326. static int ieee80211_add_chanctx(struct ieee80211_local *local,
  327. struct ieee80211_chanctx *ctx)
  328. {
  329. u32 changed;
  330. int err;
  331. lockdep_assert_held(&local->mtx);
  332. lockdep_assert_held(&local->chanctx_mtx);
  333. if (!local->use_chanctx)
  334. local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
  335. /* turn idle off *before* setting channel -- some drivers need that */
  336. changed = ieee80211_idle_off(local);
  337. if (changed)
  338. ieee80211_hw_config(local, changed);
  339. if (!local->use_chanctx) {
  340. local->_oper_chandef = ctx->conf.def;
  341. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  342. } else {
  343. err = drv_add_chanctx(local, ctx);
  344. if (err) {
  345. ieee80211_recalc_idle(local);
  346. return err;
  347. }
  348. }
  349. return 0;
  350. }
  351. static struct ieee80211_chanctx *
  352. ieee80211_new_chanctx(struct ieee80211_local *local,
  353. const struct cfg80211_chan_def *chandef,
  354. enum ieee80211_chanctx_mode mode)
  355. {
  356. struct ieee80211_chanctx *ctx;
  357. int err;
  358. lockdep_assert_held(&local->mtx);
  359. lockdep_assert_held(&local->chanctx_mtx);
  360. ctx = ieee80211_alloc_chanctx(local, chandef, mode);
  361. if (!ctx)
  362. return ERR_PTR(-ENOMEM);
  363. err = ieee80211_add_chanctx(local, ctx);
  364. if (err) {
  365. kfree(ctx);
  366. return ERR_PTR(err);
  367. }
  368. list_add_rcu(&ctx->list, &local->chanctx_list);
  369. return ctx;
  370. }
  371. static void ieee80211_del_chanctx(struct ieee80211_local *local,
  372. struct ieee80211_chanctx *ctx)
  373. {
  374. lockdep_assert_held(&local->chanctx_mtx);
  375. if (!local->use_chanctx) {
  376. struct cfg80211_chan_def *chandef = &local->_oper_chandef;
  377. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  378. chandef->center_freq1 = chandef->chan->center_freq;
  379. chandef->center_freq2 = 0;
  380. /* NOTE: Disabling radar is only valid here for
  381. * single channel context. To be sure, check it ...
  382. */
  383. WARN_ON(local->hw.conf.radar_enabled &&
  384. !list_empty(&local->chanctx_list));
  385. local->hw.conf.radar_enabled = false;
  386. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  387. } else {
  388. drv_remove_chanctx(local, ctx);
  389. }
  390. ieee80211_recalc_idle(local);
  391. }
  392. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  393. struct ieee80211_chanctx *ctx)
  394. {
  395. lockdep_assert_held(&local->chanctx_mtx);
  396. WARN_ON_ONCE(ieee80211_chanctx_refcount(local, ctx) != 0);
  397. list_del_rcu(&ctx->list);
  398. ieee80211_del_chanctx(local, ctx);
  399. kfree_rcu(ctx, rcu_head);
  400. }
  401. static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
  402. struct ieee80211_chanctx *ctx)
  403. {
  404. struct ieee80211_chanctx_conf *conf = &ctx->conf;
  405. struct ieee80211_sub_if_data *sdata;
  406. const struct cfg80211_chan_def *compat = NULL;
  407. lockdep_assert_held(&local->chanctx_mtx);
  408. rcu_read_lock();
  409. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  410. if (!ieee80211_sdata_running(sdata))
  411. continue;
  412. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  413. continue;
  414. if (!compat)
  415. compat = &sdata->vif.bss_conf.chandef;
  416. compat = cfg80211_chandef_compatible(
  417. &sdata->vif.bss_conf.chandef, compat);
  418. if (!compat)
  419. break;
  420. }
  421. rcu_read_unlock();
  422. if (WARN_ON_ONCE(!compat))
  423. return;
  424. ieee80211_change_chanctx(local, ctx, compat);
  425. }
  426. static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
  427. struct ieee80211_chanctx *chanctx)
  428. {
  429. bool radar_enabled;
  430. lockdep_assert_held(&local->chanctx_mtx);
  431. /* for setting local->radar_detect_enabled */
  432. lockdep_assert_held(&local->mtx);
  433. radar_enabled = ieee80211_is_radar_required(local);
  434. if (radar_enabled == chanctx->conf.radar_enabled)
  435. return;
  436. chanctx->conf.radar_enabled = radar_enabled;
  437. local->radar_detect_enabled = chanctx->conf.radar_enabled;
  438. if (!local->use_chanctx) {
  439. local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
  440. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  441. }
  442. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
  443. }
  444. static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  445. struct ieee80211_chanctx *new_ctx)
  446. {
  447. struct ieee80211_local *local = sdata->local;
  448. struct ieee80211_chanctx_conf *conf;
  449. struct ieee80211_chanctx *curr_ctx = NULL;
  450. int ret = 0;
  451. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  452. lockdep_is_held(&local->chanctx_mtx));
  453. if (conf) {
  454. curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
  455. drv_unassign_vif_chanctx(local, sdata, curr_ctx);
  456. conf = NULL;
  457. list_del(&sdata->assigned_chanctx_list);
  458. }
  459. if (new_ctx) {
  460. ret = drv_assign_vif_chanctx(local, sdata, new_ctx);
  461. if (ret)
  462. goto out;
  463. conf = &new_ctx->conf;
  464. list_add(&sdata->assigned_chanctx_list,
  465. &new_ctx->assigned_vifs);
  466. }
  467. out:
  468. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  469. sdata->vif.bss_conf.idle = !conf;
  470. if (curr_ctx && ieee80211_chanctx_num_assigned(local, curr_ctx) > 0) {
  471. ieee80211_recalc_chanctx_chantype(local, curr_ctx);
  472. ieee80211_recalc_smps_chanctx(local, curr_ctx);
  473. ieee80211_recalc_radar_chanctx(local, curr_ctx);
  474. ieee80211_recalc_chanctx_min_def(local, curr_ctx);
  475. }
  476. if (new_ctx && ieee80211_chanctx_num_assigned(local, new_ctx) > 0) {
  477. ieee80211_recalc_txpower(sdata);
  478. ieee80211_recalc_chanctx_min_def(local, new_ctx);
  479. }
  480. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  481. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  482. ieee80211_bss_info_change_notify(sdata,
  483. BSS_CHANGED_IDLE);
  484. return ret;
  485. }
  486. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  487. {
  488. struct ieee80211_local *local = sdata->local;
  489. struct ieee80211_chanctx_conf *conf;
  490. struct ieee80211_chanctx *ctx;
  491. lockdep_assert_held(&local->chanctx_mtx);
  492. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  493. lockdep_is_held(&local->chanctx_mtx));
  494. if (!conf)
  495. return;
  496. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  497. if (sdata->reserved_chanctx)
  498. ieee80211_vif_unreserve_chanctx(sdata);
  499. ieee80211_assign_vif_chanctx(sdata, NULL);
  500. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  501. ieee80211_free_chanctx(local, ctx);
  502. }
  503. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  504. struct ieee80211_chanctx *chanctx)
  505. {
  506. struct ieee80211_sub_if_data *sdata;
  507. u8 rx_chains_static, rx_chains_dynamic;
  508. lockdep_assert_held(&local->chanctx_mtx);
  509. rx_chains_static = 1;
  510. rx_chains_dynamic = 1;
  511. rcu_read_lock();
  512. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  513. u8 needed_static, needed_dynamic;
  514. if (!ieee80211_sdata_running(sdata))
  515. continue;
  516. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  517. &chanctx->conf)
  518. continue;
  519. switch (sdata->vif.type) {
  520. case NL80211_IFTYPE_P2P_DEVICE:
  521. continue;
  522. case NL80211_IFTYPE_STATION:
  523. if (!sdata->u.mgd.associated)
  524. continue;
  525. break;
  526. case NL80211_IFTYPE_AP_VLAN:
  527. continue;
  528. case NL80211_IFTYPE_AP:
  529. case NL80211_IFTYPE_ADHOC:
  530. case NL80211_IFTYPE_WDS:
  531. case NL80211_IFTYPE_MESH_POINT:
  532. break;
  533. default:
  534. WARN_ON_ONCE(1);
  535. }
  536. switch (sdata->smps_mode) {
  537. default:
  538. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  539. sdata->smps_mode);
  540. /* fall through */
  541. case IEEE80211_SMPS_OFF:
  542. needed_static = sdata->needed_rx_chains;
  543. needed_dynamic = sdata->needed_rx_chains;
  544. break;
  545. case IEEE80211_SMPS_DYNAMIC:
  546. needed_static = 1;
  547. needed_dynamic = sdata->needed_rx_chains;
  548. break;
  549. case IEEE80211_SMPS_STATIC:
  550. needed_static = 1;
  551. needed_dynamic = 1;
  552. break;
  553. }
  554. rx_chains_static = max(rx_chains_static, needed_static);
  555. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  556. }
  557. /* Disable SMPS for the monitor interface */
  558. sdata = rcu_dereference(local->monitor_sdata);
  559. if (sdata &&
  560. rcu_access_pointer(sdata->vif.chanctx_conf) == &chanctx->conf)
  561. rx_chains_dynamic = rx_chains_static = local->rx_chains;
  562. rcu_read_unlock();
  563. if (!local->use_chanctx) {
  564. if (rx_chains_static > 1)
  565. local->smps_mode = IEEE80211_SMPS_OFF;
  566. else if (rx_chains_dynamic > 1)
  567. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  568. else
  569. local->smps_mode = IEEE80211_SMPS_STATIC;
  570. ieee80211_hw_config(local, 0);
  571. }
  572. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  573. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  574. return;
  575. chanctx->conf.rx_chains_static = rx_chains_static;
  576. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  577. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  578. }
  579. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  580. const struct cfg80211_chan_def *chandef,
  581. enum ieee80211_chanctx_mode mode)
  582. {
  583. struct ieee80211_local *local = sdata->local;
  584. struct ieee80211_chanctx *ctx;
  585. u8 radar_detect_width = 0;
  586. int ret;
  587. lockdep_assert_held(&local->mtx);
  588. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  589. mutex_lock(&local->chanctx_mtx);
  590. ret = cfg80211_chandef_dfs_required(local->hw.wiphy,
  591. chandef,
  592. sdata->wdev.iftype);
  593. if (ret < 0)
  594. goto out;
  595. if (ret > 0)
  596. radar_detect_width = BIT(chandef->width);
  597. sdata->radar_required = ret;
  598. ret = ieee80211_check_combinations(sdata, chandef, mode,
  599. radar_detect_width);
  600. if (ret < 0)
  601. goto out;
  602. __ieee80211_vif_release_channel(sdata);
  603. ctx = ieee80211_find_chanctx(local, chandef, mode);
  604. if (!ctx)
  605. ctx = ieee80211_new_chanctx(local, chandef, mode);
  606. if (IS_ERR(ctx)) {
  607. ret = PTR_ERR(ctx);
  608. goto out;
  609. }
  610. sdata->vif.bss_conf.chandef = *chandef;
  611. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  612. if (ret) {
  613. /* if assign fails refcount stays the same */
  614. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  615. ieee80211_free_chanctx(local, ctx);
  616. goto out;
  617. }
  618. ieee80211_recalc_smps_chanctx(local, ctx);
  619. ieee80211_recalc_radar_chanctx(local, ctx);
  620. out:
  621. mutex_unlock(&local->chanctx_mtx);
  622. return ret;
  623. }
  624. static int __ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
  625. struct ieee80211_chanctx *ctx,
  626. u32 *changed)
  627. {
  628. struct ieee80211_local *local = sdata->local;
  629. const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
  630. u32 chanctx_changed = 0;
  631. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  632. IEEE80211_CHAN_DISABLED))
  633. return -EINVAL;
  634. if (ieee80211_chanctx_refcount(local, ctx) != 1)
  635. return -EINVAL;
  636. if (sdata->vif.bss_conf.chandef.width != chandef->width) {
  637. chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
  638. *changed |= BSS_CHANGED_BANDWIDTH;
  639. }
  640. sdata->vif.bss_conf.chandef = *chandef;
  641. ctx->conf.def = *chandef;
  642. chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
  643. drv_change_chanctx(local, ctx, chanctx_changed);
  644. ieee80211_recalc_chanctx_chantype(local, ctx);
  645. ieee80211_recalc_smps_chanctx(local, ctx);
  646. ieee80211_recalc_radar_chanctx(local, ctx);
  647. ieee80211_recalc_chanctx_min_def(local, ctx);
  648. return 0;
  649. }
  650. int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
  651. u32 *changed)
  652. {
  653. struct ieee80211_local *local = sdata->local;
  654. struct ieee80211_chanctx_conf *conf;
  655. struct ieee80211_chanctx *ctx;
  656. int ret;
  657. lockdep_assert_held(&local->mtx);
  658. /* should never be called if not performing a channel switch. */
  659. if (WARN_ON(!sdata->vif.csa_active))
  660. return -EINVAL;
  661. mutex_lock(&local->chanctx_mtx);
  662. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  663. lockdep_is_held(&local->chanctx_mtx));
  664. if (!conf) {
  665. ret = -EINVAL;
  666. goto out;
  667. }
  668. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  669. ret = __ieee80211_vif_change_channel(sdata, ctx, changed);
  670. out:
  671. mutex_unlock(&local->chanctx_mtx);
  672. return ret;
  673. }
  674. static void
  675. __ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  676. bool clear)
  677. {
  678. struct ieee80211_local *local __maybe_unused = sdata->local;
  679. struct ieee80211_sub_if_data *vlan;
  680. struct ieee80211_chanctx_conf *conf;
  681. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
  682. return;
  683. lockdep_assert_held(&local->mtx);
  684. /* Check that conf exists, even when clearing this function
  685. * must be called with the AP's channel context still there
  686. * as it would otherwise cause VLANs to have an invalid
  687. * channel context pointer for a while, possibly pointing
  688. * to a channel context that has already been freed.
  689. */
  690. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  691. lockdep_is_held(&local->chanctx_mtx));
  692. WARN_ON(!conf);
  693. if (clear)
  694. conf = NULL;
  695. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  696. rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
  697. }
  698. void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  699. bool clear)
  700. {
  701. struct ieee80211_local *local = sdata->local;
  702. mutex_lock(&local->chanctx_mtx);
  703. __ieee80211_vif_copy_chanctx_to_vlans(sdata, clear);
  704. mutex_unlock(&local->chanctx_mtx);
  705. }
  706. int ieee80211_vif_unreserve_chanctx(struct ieee80211_sub_if_data *sdata)
  707. {
  708. struct ieee80211_chanctx *ctx = sdata->reserved_chanctx;
  709. lockdep_assert_held(&sdata->local->chanctx_mtx);
  710. if (WARN_ON(!ctx))
  711. return -EINVAL;
  712. list_del(&sdata->reserved_chanctx_list);
  713. sdata->reserved_chanctx = NULL;
  714. if (ieee80211_chanctx_refcount(sdata->local, ctx) == 0)
  715. ieee80211_free_chanctx(sdata->local, ctx);
  716. return 0;
  717. }
  718. int ieee80211_vif_reserve_chanctx(struct ieee80211_sub_if_data *sdata,
  719. const struct cfg80211_chan_def *chandef,
  720. enum ieee80211_chanctx_mode mode,
  721. bool radar_required)
  722. {
  723. struct ieee80211_local *local = sdata->local;
  724. struct ieee80211_chanctx_conf *conf;
  725. struct ieee80211_chanctx *new_ctx, *curr_ctx;
  726. int ret = 0;
  727. mutex_lock(&local->chanctx_mtx);
  728. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  729. lockdep_is_held(&local->chanctx_mtx));
  730. if (!conf) {
  731. ret = -EINVAL;
  732. goto out;
  733. }
  734. curr_ctx = container_of(conf, struct ieee80211_chanctx, conf);
  735. new_ctx = ieee80211_find_reservation_chanctx(local, chandef, mode);
  736. if (!new_ctx) {
  737. if (ieee80211_chanctx_refcount(local, curr_ctx) == 1 &&
  738. (local->hw.flags & IEEE80211_HW_CHANGE_RUNNING_CHANCTX)) {
  739. /* if we're the only users of the chanctx and
  740. * the driver supports changing a running
  741. * context, reserve our current context
  742. */
  743. new_ctx = curr_ctx;
  744. } else if (ieee80211_can_create_new_chanctx(local)) {
  745. /* create a new context and reserve it */
  746. new_ctx = ieee80211_new_chanctx(local, chandef, mode);
  747. if (IS_ERR(new_ctx)) {
  748. ret = PTR_ERR(new_ctx);
  749. goto out;
  750. }
  751. } else {
  752. ret = -EBUSY;
  753. goto out;
  754. }
  755. }
  756. list_add(&sdata->reserved_chanctx_list, &new_ctx->reserved_vifs);
  757. sdata->reserved_chanctx = new_ctx;
  758. sdata->reserved_chandef = *chandef;
  759. sdata->reserved_radar_required = radar_required;
  760. out:
  761. mutex_unlock(&local->chanctx_mtx);
  762. return ret;
  763. }
  764. int ieee80211_vif_use_reserved_context(struct ieee80211_sub_if_data *sdata,
  765. u32 *changed)
  766. {
  767. struct ieee80211_local *local = sdata->local;
  768. struct ieee80211_chanctx *ctx;
  769. struct ieee80211_chanctx *old_ctx;
  770. struct ieee80211_chanctx_conf *conf;
  771. int ret;
  772. u32 tmp_changed = *changed;
  773. /* TODO: need to recheck if the chandef is usable etc.? */
  774. lockdep_assert_held(&local->mtx);
  775. mutex_lock(&local->chanctx_mtx);
  776. ctx = sdata->reserved_chanctx;
  777. if (WARN_ON(!ctx)) {
  778. ret = -EINVAL;
  779. goto out;
  780. }
  781. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  782. lockdep_is_held(&local->chanctx_mtx));
  783. if (!conf) {
  784. ret = -EINVAL;
  785. goto out;
  786. }
  787. old_ctx = container_of(conf, struct ieee80211_chanctx, conf);
  788. if (sdata->vif.bss_conf.chandef.width != sdata->reserved_chandef.width)
  789. tmp_changed |= BSS_CHANGED_BANDWIDTH;
  790. sdata->vif.bss_conf.chandef = sdata->reserved_chandef;
  791. /* unref our reservation */
  792. sdata->reserved_chanctx = NULL;
  793. sdata->radar_required = sdata->reserved_radar_required;
  794. list_del(&sdata->reserved_chanctx_list);
  795. if (old_ctx == ctx) {
  796. /* This is our own context, just change it */
  797. ret = __ieee80211_vif_change_channel(sdata, old_ctx,
  798. &tmp_changed);
  799. if (ret)
  800. goto out;
  801. } else {
  802. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  803. if (ieee80211_chanctx_refcount(local, old_ctx) == 0)
  804. ieee80211_free_chanctx(local, old_ctx);
  805. if (ret) {
  806. /* if assign fails refcount stays the same */
  807. if (ieee80211_chanctx_refcount(local, ctx) == 0)
  808. ieee80211_free_chanctx(local, ctx);
  809. goto out;
  810. }
  811. if (sdata->vif.type == NL80211_IFTYPE_AP)
  812. __ieee80211_vif_copy_chanctx_to_vlans(sdata, false);
  813. }
  814. *changed = tmp_changed;
  815. ieee80211_recalc_chanctx_chantype(local, ctx);
  816. ieee80211_recalc_smps_chanctx(local, ctx);
  817. ieee80211_recalc_radar_chanctx(local, ctx);
  818. ieee80211_recalc_chanctx_min_def(local, ctx);
  819. out:
  820. mutex_unlock(&local->chanctx_mtx);
  821. return ret;
  822. }
  823. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  824. const struct cfg80211_chan_def *chandef,
  825. u32 *changed)
  826. {
  827. struct ieee80211_local *local = sdata->local;
  828. struct ieee80211_chanctx_conf *conf;
  829. struct ieee80211_chanctx *ctx;
  830. int ret;
  831. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  832. IEEE80211_CHAN_DISABLED))
  833. return -EINVAL;
  834. mutex_lock(&local->chanctx_mtx);
  835. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  836. ret = 0;
  837. goto out;
  838. }
  839. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  840. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  841. ret = -EINVAL;
  842. goto out;
  843. }
  844. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  845. lockdep_is_held(&local->chanctx_mtx));
  846. if (!conf) {
  847. ret = -EINVAL;
  848. goto out;
  849. }
  850. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  851. if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
  852. ret = -EINVAL;
  853. goto out;
  854. }
  855. sdata->vif.bss_conf.chandef = *chandef;
  856. ieee80211_recalc_chanctx_chantype(local, ctx);
  857. *changed |= BSS_CHANGED_BANDWIDTH;
  858. ret = 0;
  859. out:
  860. mutex_unlock(&local->chanctx_mtx);
  861. return ret;
  862. }
  863. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  864. {
  865. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  866. lockdep_assert_held(&sdata->local->mtx);
  867. mutex_lock(&sdata->local->chanctx_mtx);
  868. __ieee80211_vif_release_channel(sdata);
  869. mutex_unlock(&sdata->local->chanctx_mtx);
  870. }
  871. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  872. {
  873. struct ieee80211_local *local = sdata->local;
  874. struct ieee80211_sub_if_data *ap;
  875. struct ieee80211_chanctx_conf *conf;
  876. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  877. return;
  878. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  879. mutex_lock(&local->chanctx_mtx);
  880. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  881. lockdep_is_held(&local->chanctx_mtx));
  882. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  883. mutex_unlock(&local->chanctx_mtx);
  884. }
  885. void ieee80211_iter_chan_contexts_atomic(
  886. struct ieee80211_hw *hw,
  887. void (*iter)(struct ieee80211_hw *hw,
  888. struct ieee80211_chanctx_conf *chanctx_conf,
  889. void *data),
  890. void *iter_data)
  891. {
  892. struct ieee80211_local *local = hw_to_local(hw);
  893. struct ieee80211_chanctx *ctx;
  894. rcu_read_lock();
  895. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  896. if (ctx->driver_present)
  897. iter(hw, &ctx->conf, iter_data);
  898. rcu_read_unlock();
  899. }
  900. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);