chan.c 9.6 KB

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