chan.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * mac80211 - channel management
  3. */
  4. #include <linux/nl80211.h>
  5. #include <linux/export.h>
  6. #include <net/cfg80211.h>
  7. #include "ieee80211_i.h"
  8. #include "driver-ops.h"
  9. static void ieee80211_change_chandef(struct ieee80211_local *local,
  10. struct ieee80211_chanctx *ctx,
  11. const struct cfg80211_chan_def *chandef)
  12. {
  13. if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
  14. return;
  15. WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
  16. ctx->conf.def = *chandef;
  17. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
  18. if (!local->use_chanctx) {
  19. local->_oper_channel_type = cfg80211_get_chandef_type(chandef);
  20. ieee80211_hw_config(local, 0);
  21. }
  22. }
  23. static struct ieee80211_chanctx *
  24. ieee80211_find_chanctx(struct ieee80211_local *local,
  25. const struct cfg80211_chan_def *chandef,
  26. enum ieee80211_chanctx_mode mode)
  27. {
  28. struct ieee80211_chanctx *ctx;
  29. lockdep_assert_held(&local->chanctx_mtx);
  30. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  31. return NULL;
  32. list_for_each_entry(ctx, &local->chanctx_list, list) {
  33. const struct cfg80211_chan_def *compat;
  34. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  35. continue;
  36. compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
  37. if (!compat)
  38. continue;
  39. ieee80211_change_chandef(local, ctx, compat);
  40. return ctx;
  41. }
  42. return NULL;
  43. }
  44. static struct ieee80211_chanctx *
  45. ieee80211_new_chanctx(struct ieee80211_local *local,
  46. const struct cfg80211_chan_def *chandef,
  47. enum ieee80211_chanctx_mode mode)
  48. {
  49. struct ieee80211_chanctx *ctx;
  50. int err;
  51. lockdep_assert_held(&local->chanctx_mtx);
  52. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  53. if (!ctx)
  54. return ERR_PTR(-ENOMEM);
  55. ctx->conf.def = *chandef;
  56. ctx->conf.rx_chains_static = 1;
  57. ctx->conf.rx_chains_dynamic = 1;
  58. ctx->mode = mode;
  59. if (!local->use_chanctx) {
  60. local->_oper_channel_type =
  61. cfg80211_get_chandef_type(chandef);
  62. local->_oper_channel = chandef->chan;
  63. ieee80211_hw_config(local, 0);
  64. } else {
  65. err = drv_add_chanctx(local, ctx);
  66. if (err) {
  67. kfree(ctx);
  68. return ERR_PTR(err);
  69. }
  70. }
  71. list_add_rcu(&ctx->list, &local->chanctx_list);
  72. return ctx;
  73. }
  74. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  75. struct ieee80211_chanctx *ctx)
  76. {
  77. lockdep_assert_held(&local->chanctx_mtx);
  78. WARN_ON_ONCE(ctx->refcount != 0);
  79. if (!local->use_chanctx) {
  80. local->_oper_channel_type = NL80211_CHAN_NO_HT;
  81. ieee80211_hw_config(local, 0);
  82. } else {
  83. drv_remove_chanctx(local, ctx);
  84. }
  85. list_del_rcu(&ctx->list);
  86. kfree_rcu(ctx, rcu_head);
  87. }
  88. static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  89. struct ieee80211_chanctx *ctx)
  90. {
  91. struct ieee80211_local *local = sdata->local;
  92. int ret;
  93. lockdep_assert_held(&local->chanctx_mtx);
  94. ret = drv_assign_vif_chanctx(local, sdata, ctx);
  95. if (ret)
  96. return ret;
  97. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  98. ctx->refcount++;
  99. ieee80211_recalc_txpower(sdata);
  100. return 0;
  101. }
  102. static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
  103. struct ieee80211_chanctx *ctx)
  104. {
  105. struct ieee80211_chanctx_conf *conf = &ctx->conf;
  106. struct ieee80211_sub_if_data *sdata;
  107. const struct cfg80211_chan_def *compat = NULL;
  108. lockdep_assert_held(&local->chanctx_mtx);
  109. rcu_read_lock();
  110. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  111. if (!ieee80211_sdata_running(sdata))
  112. continue;
  113. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  114. continue;
  115. if (!compat)
  116. compat = &sdata->vif.bss_conf.chandef;
  117. compat = cfg80211_chandef_compatible(
  118. &sdata->vif.bss_conf.chandef, compat);
  119. if (!compat)
  120. break;
  121. }
  122. rcu_read_unlock();
  123. if (WARN_ON_ONCE(!compat))
  124. return;
  125. ieee80211_change_chandef(local, ctx, compat);
  126. }
  127. static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  128. struct ieee80211_chanctx *ctx)
  129. {
  130. struct ieee80211_local *local = sdata->local;
  131. lockdep_assert_held(&local->chanctx_mtx);
  132. ctx->refcount--;
  133. rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
  134. drv_unassign_vif_chanctx(local, sdata, ctx);
  135. if (ctx->refcount > 0) {
  136. ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
  137. ieee80211_recalc_smps_chanctx(local, ctx);
  138. }
  139. }
  140. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  141. {
  142. struct ieee80211_local *local = sdata->local;
  143. struct ieee80211_chanctx_conf *conf;
  144. struct ieee80211_chanctx *ctx;
  145. lockdep_assert_held(&local->chanctx_mtx);
  146. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  147. lockdep_is_held(&local->chanctx_mtx));
  148. if (!conf)
  149. return;
  150. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  151. ieee80211_unassign_vif_chanctx(sdata, ctx);
  152. if (ctx->refcount == 0)
  153. ieee80211_free_chanctx(local, ctx);
  154. }
  155. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  156. struct ieee80211_chanctx *chanctx)
  157. {
  158. struct ieee80211_sub_if_data *sdata;
  159. u8 rx_chains_static, rx_chains_dynamic;
  160. lockdep_assert_held(&local->chanctx_mtx);
  161. rx_chains_static = 1;
  162. rx_chains_dynamic = 1;
  163. rcu_read_lock();
  164. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  165. u8 needed_static, needed_dynamic;
  166. if (!ieee80211_sdata_running(sdata))
  167. continue;
  168. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  169. &chanctx->conf)
  170. continue;
  171. switch (sdata->vif.type) {
  172. case NL80211_IFTYPE_P2P_DEVICE:
  173. continue;
  174. case NL80211_IFTYPE_STATION:
  175. if (!sdata->u.mgd.associated)
  176. continue;
  177. break;
  178. case NL80211_IFTYPE_AP_VLAN:
  179. continue;
  180. case NL80211_IFTYPE_AP:
  181. case NL80211_IFTYPE_ADHOC:
  182. case NL80211_IFTYPE_WDS:
  183. case NL80211_IFTYPE_MESH_POINT:
  184. break;
  185. default:
  186. WARN_ON_ONCE(1);
  187. }
  188. switch (sdata->smps_mode) {
  189. default:
  190. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  191. sdata->smps_mode);
  192. /* fall through */
  193. case IEEE80211_SMPS_OFF:
  194. needed_static = sdata->needed_rx_chains;
  195. needed_dynamic = sdata->needed_rx_chains;
  196. break;
  197. case IEEE80211_SMPS_DYNAMIC:
  198. needed_static = 1;
  199. needed_dynamic = sdata->needed_rx_chains;
  200. break;
  201. case IEEE80211_SMPS_STATIC:
  202. needed_static = 1;
  203. needed_dynamic = 1;
  204. break;
  205. }
  206. rx_chains_static = max(rx_chains_static, needed_static);
  207. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  208. }
  209. rcu_read_unlock();
  210. if (!local->use_chanctx) {
  211. if (rx_chains_static > 1)
  212. local->smps_mode = IEEE80211_SMPS_OFF;
  213. else if (rx_chains_dynamic > 1)
  214. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  215. else
  216. local->smps_mode = IEEE80211_SMPS_STATIC;
  217. ieee80211_hw_config(local, 0);
  218. }
  219. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  220. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  221. return;
  222. chanctx->conf.rx_chains_static = rx_chains_static;
  223. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  224. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  225. }
  226. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  227. const struct cfg80211_chan_def *chandef,
  228. enum ieee80211_chanctx_mode mode)
  229. {
  230. struct ieee80211_local *local = sdata->local;
  231. struct ieee80211_chanctx *ctx;
  232. int ret;
  233. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  234. mutex_lock(&local->chanctx_mtx);
  235. __ieee80211_vif_release_channel(sdata);
  236. ctx = ieee80211_find_chanctx(local, chandef, mode);
  237. if (!ctx)
  238. ctx = ieee80211_new_chanctx(local, chandef, mode);
  239. if (IS_ERR(ctx)) {
  240. ret = PTR_ERR(ctx);
  241. goto out;
  242. }
  243. sdata->vif.bss_conf.chandef = *chandef;
  244. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  245. if (ret) {
  246. /* if assign fails refcount stays the same */
  247. if (ctx->refcount == 0)
  248. ieee80211_free_chanctx(local, ctx);
  249. goto out;
  250. }
  251. ieee80211_recalc_smps_chanctx(local, ctx);
  252. out:
  253. mutex_unlock(&local->chanctx_mtx);
  254. return ret;
  255. }
  256. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  257. {
  258. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  259. mutex_lock(&sdata->local->chanctx_mtx);
  260. __ieee80211_vif_release_channel(sdata);
  261. mutex_unlock(&sdata->local->chanctx_mtx);
  262. }
  263. void ieee80211_iter_chan_contexts_atomic(
  264. struct ieee80211_hw *hw,
  265. void (*iter)(struct ieee80211_hw *hw,
  266. struct ieee80211_chanctx_conf *chanctx_conf,
  267. void *data),
  268. void *iter_data)
  269. {
  270. struct ieee80211_local *local = hw_to_local(hw);
  271. struct ieee80211_chanctx *ctx;
  272. rcu_read_lock();
  273. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  274. iter(hw, &ctx->conf, iter_data);
  275. rcu_read_unlock();
  276. }
  277. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);