chan.c 9.4 KB

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