chan.c 9.1 KB

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