driver-ops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2015 Intel Deutschland GmbH
  4. * Copyright (C) 2022-2024 Intel Corporation
  5. */
  6. #include <net/mac80211.h>
  7. #include "ieee80211_i.h"
  8. #include "trace.h"
  9. #include "driver-ops.h"
  10. #include "debugfs_sta.h"
  11. #include "debugfs_netdev.h"
  12. int drv_start(struct ieee80211_local *local)
  13. {
  14. int ret;
  15. might_sleep();
  16. lockdep_assert_wiphy(local->hw.wiphy);
  17. if (WARN_ON(local->started))
  18. return -EALREADY;
  19. trace_drv_start(local);
  20. local->started = true;
  21. /* allow rx frames */
  22. smp_mb();
  23. ret = local->ops->start(&local->hw);
  24. trace_drv_return_int(local, ret);
  25. if (ret)
  26. local->started = false;
  27. return ret;
  28. }
  29. void drv_stop(struct ieee80211_local *local, bool suspend)
  30. {
  31. might_sleep();
  32. lockdep_assert_wiphy(local->hw.wiphy);
  33. if (WARN_ON(!local->started))
  34. return;
  35. trace_drv_stop(local, suspend);
  36. local->ops->stop(&local->hw, suspend);
  37. trace_drv_return_void(local);
  38. /* sync away all work on the tasklet before clearing started */
  39. tasklet_disable(&local->tasklet);
  40. tasklet_enable(&local->tasklet);
  41. barrier();
  42. local->started = false;
  43. }
  44. int drv_add_interface(struct ieee80211_local *local,
  45. struct ieee80211_sub_if_data *sdata)
  46. {
  47. int ret;
  48. might_sleep();
  49. lockdep_assert_wiphy(local->hw.wiphy);
  50. if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  51. (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
  52. !ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF) &&
  53. !(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))))
  54. return -EINVAL;
  55. trace_drv_add_interface(local, sdata);
  56. ret = local->ops->add_interface(&local->hw, &sdata->vif);
  57. trace_drv_return_int(local, ret);
  58. if (ret)
  59. return ret;
  60. if (!(sdata->flags & IEEE80211_SDATA_IN_DRIVER)) {
  61. sdata->flags |= IEEE80211_SDATA_IN_DRIVER;
  62. drv_vif_add_debugfs(local, sdata);
  63. /* initially vif is not MLD */
  64. ieee80211_link_debugfs_drv_add(&sdata->deflink);
  65. }
  66. return 0;
  67. }
  68. int drv_change_interface(struct ieee80211_local *local,
  69. struct ieee80211_sub_if_data *sdata,
  70. enum nl80211_iftype type, bool p2p)
  71. {
  72. int ret;
  73. might_sleep();
  74. lockdep_assert_wiphy(local->hw.wiphy);
  75. if (!check_sdata_in_driver(sdata))
  76. return -EIO;
  77. trace_drv_change_interface(local, sdata, type, p2p);
  78. ret = local->ops->change_interface(&local->hw, &sdata->vif, type, p2p);
  79. trace_drv_return_int(local, ret);
  80. return ret;
  81. }
  82. void drv_remove_interface(struct ieee80211_local *local,
  83. struct ieee80211_sub_if_data *sdata)
  84. {
  85. might_sleep();
  86. lockdep_assert_wiphy(local->hw.wiphy);
  87. if (!check_sdata_in_driver(sdata))
  88. return;
  89. sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
  90. /*
  91. * Remove driver debugfs entries.
  92. * The virtual monitor interface doesn't get a debugfs
  93. * entry, so it's exempt here.
  94. */
  95. if (sdata != rcu_access_pointer(local->monitor_sdata))
  96. ieee80211_debugfs_recreate_netdev(sdata,
  97. sdata->vif.valid_links);
  98. trace_drv_remove_interface(local, sdata);
  99. local->ops->remove_interface(&local->hw, &sdata->vif);
  100. trace_drv_return_void(local);
  101. }
  102. __must_check
  103. int drv_sta_state(struct ieee80211_local *local,
  104. struct ieee80211_sub_if_data *sdata,
  105. struct sta_info *sta,
  106. enum ieee80211_sta_state old_state,
  107. enum ieee80211_sta_state new_state)
  108. {
  109. int ret = 0;
  110. might_sleep();
  111. lockdep_assert_wiphy(local->hw.wiphy);
  112. sdata = get_bss_sdata(sdata);
  113. if (!check_sdata_in_driver(sdata))
  114. return -EIO;
  115. trace_drv_sta_state(local, sdata, &sta->sta, old_state, new_state);
  116. if (local->ops->sta_state) {
  117. ret = local->ops->sta_state(&local->hw, &sdata->vif, &sta->sta,
  118. old_state, new_state);
  119. } else if (old_state == IEEE80211_STA_AUTH &&
  120. new_state == IEEE80211_STA_ASSOC) {
  121. ret = drv_sta_add(local, sdata, &sta->sta);
  122. if (ret == 0) {
  123. sta->uploaded = true;
  124. if (rcu_access_pointer(sta->sta.rates))
  125. drv_sta_rate_tbl_update(local, sdata, &sta->sta);
  126. }
  127. } else if (old_state == IEEE80211_STA_ASSOC &&
  128. new_state == IEEE80211_STA_AUTH) {
  129. drv_sta_remove(local, sdata, &sta->sta);
  130. }
  131. trace_drv_return_int(local, ret);
  132. return ret;
  133. }
  134. __must_check
  135. int drv_sta_set_txpwr(struct ieee80211_local *local,
  136. struct ieee80211_sub_if_data *sdata,
  137. struct sta_info *sta)
  138. {
  139. int ret = -EOPNOTSUPP;
  140. might_sleep();
  141. lockdep_assert_wiphy(local->hw.wiphy);
  142. sdata = get_bss_sdata(sdata);
  143. if (!check_sdata_in_driver(sdata))
  144. return -EIO;
  145. trace_drv_sta_set_txpwr(local, sdata, &sta->sta);
  146. if (local->ops->sta_set_txpwr)
  147. ret = local->ops->sta_set_txpwr(&local->hw, &sdata->vif,
  148. &sta->sta);
  149. trace_drv_return_int(local, ret);
  150. return ret;
  151. }
  152. void drv_sta_rc_update(struct ieee80211_local *local,
  153. struct ieee80211_sub_if_data *sdata,
  154. struct ieee80211_sta *sta, u32 changed)
  155. {
  156. sdata = get_bss_sdata(sdata);
  157. if (!check_sdata_in_driver(sdata))
  158. return;
  159. WARN_ON(changed & IEEE80211_RC_SUPP_RATES_CHANGED &&
  160. (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  161. sdata->vif.type != NL80211_IFTYPE_MESH_POINT));
  162. trace_drv_sta_rc_update(local, sdata, sta, changed);
  163. if (local->ops->sta_rc_update)
  164. local->ops->sta_rc_update(&local->hw, &sdata->vif,
  165. sta, changed);
  166. trace_drv_return_void(local);
  167. }
  168. int drv_conf_tx(struct ieee80211_local *local,
  169. struct ieee80211_link_data *link, u16 ac,
  170. const struct ieee80211_tx_queue_params *params)
  171. {
  172. struct ieee80211_sub_if_data *sdata = link->sdata;
  173. int ret = -EOPNOTSUPP;
  174. might_sleep();
  175. lockdep_assert_wiphy(local->hw.wiphy);
  176. if (!check_sdata_in_driver(sdata))
  177. return -EIO;
  178. if (!ieee80211_vif_link_active(&sdata->vif, link->link_id))
  179. return 0;
  180. if (params->cw_min == 0 || params->cw_min > params->cw_max) {
  181. /*
  182. * If we can't configure hardware anyway, don't warn. We may
  183. * never have initialized the CW parameters.
  184. */
  185. WARN_ONCE(local->ops->conf_tx,
  186. "%s: invalid CW_min/CW_max: %d/%d\n",
  187. sdata->name, params->cw_min, params->cw_max);
  188. return -EINVAL;
  189. }
  190. trace_drv_conf_tx(local, sdata, link->link_id, ac, params);
  191. if (local->ops->conf_tx)
  192. ret = local->ops->conf_tx(&local->hw, &sdata->vif,
  193. link->link_id, ac, params);
  194. trace_drv_return_int(local, ret);
  195. return ret;
  196. }
  197. u64 drv_get_tsf(struct ieee80211_local *local,
  198. struct ieee80211_sub_if_data *sdata)
  199. {
  200. u64 ret = -1ULL;
  201. might_sleep();
  202. lockdep_assert_wiphy(local->hw.wiphy);
  203. if (!check_sdata_in_driver(sdata))
  204. return ret;
  205. trace_drv_get_tsf(local, sdata);
  206. if (local->ops->get_tsf)
  207. ret = local->ops->get_tsf(&local->hw, &sdata->vif);
  208. trace_drv_return_u64(local, ret);
  209. return ret;
  210. }
  211. void drv_set_tsf(struct ieee80211_local *local,
  212. struct ieee80211_sub_if_data *sdata,
  213. u64 tsf)
  214. {
  215. might_sleep();
  216. lockdep_assert_wiphy(local->hw.wiphy);
  217. if (!check_sdata_in_driver(sdata))
  218. return;
  219. trace_drv_set_tsf(local, sdata, tsf);
  220. if (local->ops->set_tsf)
  221. local->ops->set_tsf(&local->hw, &sdata->vif, tsf);
  222. trace_drv_return_void(local);
  223. }
  224. void drv_offset_tsf(struct ieee80211_local *local,
  225. struct ieee80211_sub_if_data *sdata,
  226. s64 offset)
  227. {
  228. might_sleep();
  229. lockdep_assert_wiphy(local->hw.wiphy);
  230. if (!check_sdata_in_driver(sdata))
  231. return;
  232. trace_drv_offset_tsf(local, sdata, offset);
  233. if (local->ops->offset_tsf)
  234. local->ops->offset_tsf(&local->hw, &sdata->vif, offset);
  235. trace_drv_return_void(local);
  236. }
  237. void drv_reset_tsf(struct ieee80211_local *local,
  238. struct ieee80211_sub_if_data *sdata)
  239. {
  240. might_sleep();
  241. lockdep_assert_wiphy(local->hw.wiphy);
  242. if (!check_sdata_in_driver(sdata))
  243. return;
  244. trace_drv_reset_tsf(local, sdata);
  245. if (local->ops->reset_tsf)
  246. local->ops->reset_tsf(&local->hw, &sdata->vif);
  247. trace_drv_return_void(local);
  248. }
  249. int drv_assign_vif_chanctx(struct ieee80211_local *local,
  250. struct ieee80211_sub_if_data *sdata,
  251. struct ieee80211_bss_conf *link_conf,
  252. struct ieee80211_chanctx *ctx)
  253. {
  254. int ret = 0;
  255. might_sleep();
  256. lockdep_assert_wiphy(local->hw.wiphy);
  257. /*
  258. * We should perhaps push emulate chanctx down and only
  259. * make it call ->config() when the chanctx is actually
  260. * assigned here (and unassigned below), but that's yet
  261. * another change to all drivers to add assign/unassign
  262. * emulation callbacks. Maybe later.
  263. */
  264. if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
  265. local->emulate_chanctx &&
  266. !ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
  267. return 0;
  268. if (!check_sdata_in_driver(sdata))
  269. return -EIO;
  270. if (!ieee80211_vif_link_active(&sdata->vif, link_conf->link_id))
  271. return 0;
  272. trace_drv_assign_vif_chanctx(local, sdata, link_conf, ctx);
  273. if (local->ops->assign_vif_chanctx) {
  274. WARN_ON_ONCE(!ctx->driver_present);
  275. ret = local->ops->assign_vif_chanctx(&local->hw,
  276. &sdata->vif,
  277. link_conf,
  278. &ctx->conf);
  279. }
  280. trace_drv_return_int(local, ret);
  281. return ret;
  282. }
  283. void drv_unassign_vif_chanctx(struct ieee80211_local *local,
  284. struct ieee80211_sub_if_data *sdata,
  285. struct ieee80211_bss_conf *link_conf,
  286. struct ieee80211_chanctx *ctx)
  287. {
  288. might_sleep();
  289. lockdep_assert_wiphy(local->hw.wiphy);
  290. if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
  291. local->emulate_chanctx &&
  292. !ieee80211_hw_check(&local->hw, WANT_MONITOR_VIF))
  293. return;
  294. if (!check_sdata_in_driver(sdata))
  295. return;
  296. if (!ieee80211_vif_link_active(&sdata->vif, link_conf->link_id))
  297. return;
  298. trace_drv_unassign_vif_chanctx(local, sdata, link_conf, ctx);
  299. if (local->ops->unassign_vif_chanctx) {
  300. WARN_ON_ONCE(!ctx->driver_present);
  301. local->ops->unassign_vif_chanctx(&local->hw,
  302. &sdata->vif,
  303. link_conf,
  304. &ctx->conf);
  305. }
  306. trace_drv_return_void(local);
  307. }
  308. int drv_switch_vif_chanctx(struct ieee80211_local *local,
  309. struct ieee80211_vif_chanctx_switch *vifs,
  310. int n_vifs, enum ieee80211_chanctx_switch_mode mode)
  311. {
  312. int ret = 0;
  313. int i;
  314. might_sleep();
  315. lockdep_assert_wiphy(local->hw.wiphy);
  316. if (!local->ops->switch_vif_chanctx)
  317. return -EOPNOTSUPP;
  318. for (i = 0; i < n_vifs; i++) {
  319. struct ieee80211_chanctx *new_ctx =
  320. container_of(vifs[i].new_ctx,
  321. struct ieee80211_chanctx,
  322. conf);
  323. struct ieee80211_chanctx *old_ctx =
  324. container_of(vifs[i].old_ctx,
  325. struct ieee80211_chanctx,
  326. conf);
  327. WARN_ON_ONCE(!old_ctx->driver_present);
  328. WARN_ON_ONCE((mode == CHANCTX_SWMODE_SWAP_CONTEXTS &&
  329. new_ctx->driver_present) ||
  330. (mode == CHANCTX_SWMODE_REASSIGN_VIF &&
  331. !new_ctx->driver_present));
  332. }
  333. trace_drv_switch_vif_chanctx(local, vifs, n_vifs, mode);
  334. ret = local->ops->switch_vif_chanctx(&local->hw,
  335. vifs, n_vifs, mode);
  336. trace_drv_return_int(local, ret);
  337. if (!ret && mode == CHANCTX_SWMODE_SWAP_CONTEXTS) {
  338. for (i = 0; i < n_vifs; i++) {
  339. struct ieee80211_chanctx *new_ctx =
  340. container_of(vifs[i].new_ctx,
  341. struct ieee80211_chanctx,
  342. conf);
  343. struct ieee80211_chanctx *old_ctx =
  344. container_of(vifs[i].old_ctx,
  345. struct ieee80211_chanctx,
  346. conf);
  347. new_ctx->driver_present = true;
  348. old_ctx->driver_present = false;
  349. }
  350. }
  351. return ret;
  352. }
  353. int drv_ampdu_action(struct ieee80211_local *local,
  354. struct ieee80211_sub_if_data *sdata,
  355. struct ieee80211_ampdu_params *params)
  356. {
  357. int ret = -EOPNOTSUPP;
  358. might_sleep();
  359. lockdep_assert_wiphy(local->hw.wiphy);
  360. sdata = get_bss_sdata(sdata);
  361. if (!check_sdata_in_driver(sdata))
  362. return -EIO;
  363. trace_drv_ampdu_action(local, sdata, params);
  364. if (local->ops->ampdu_action)
  365. ret = local->ops->ampdu_action(&local->hw, &sdata->vif, params);
  366. trace_drv_return_int(local, ret);
  367. return ret;
  368. }
  369. void drv_link_info_changed(struct ieee80211_local *local,
  370. struct ieee80211_sub_if_data *sdata,
  371. struct ieee80211_bss_conf *info,
  372. int link_id, u64 changed)
  373. {
  374. might_sleep();
  375. lockdep_assert_wiphy(local->hw.wiphy);
  376. if (WARN_ON_ONCE(changed & (BSS_CHANGED_BEACON |
  377. BSS_CHANGED_BEACON_ENABLED) &&
  378. sdata->vif.type != NL80211_IFTYPE_AP &&
  379. sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  380. sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
  381. sdata->vif.type != NL80211_IFTYPE_OCB))
  382. return;
  383. if (WARN_ON_ONCE(sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE ||
  384. sdata->vif.type == NL80211_IFTYPE_NAN ||
  385. (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
  386. !sdata->vif.bss_conf.mu_mimo_owner &&
  387. !(changed & BSS_CHANGED_TXPOWER))))
  388. return;
  389. if (!check_sdata_in_driver(sdata))
  390. return;
  391. if (!ieee80211_vif_link_active(&sdata->vif, link_id))
  392. return;
  393. trace_drv_link_info_changed(local, sdata, info, changed);
  394. if (local->ops->link_info_changed)
  395. local->ops->link_info_changed(&local->hw, &sdata->vif,
  396. info, changed);
  397. else if (local->ops->bss_info_changed)
  398. local->ops->bss_info_changed(&local->hw, &sdata->vif,
  399. info, changed);
  400. trace_drv_return_void(local);
  401. }
  402. int drv_set_key(struct ieee80211_local *local,
  403. enum set_key_cmd cmd,
  404. struct ieee80211_sub_if_data *sdata,
  405. struct ieee80211_sta *sta,
  406. struct ieee80211_key_conf *key)
  407. {
  408. int ret;
  409. might_sleep();
  410. lockdep_assert_wiphy(local->hw.wiphy);
  411. sdata = get_bss_sdata(sdata);
  412. if (!check_sdata_in_driver(sdata))
  413. return -EIO;
  414. if (WARN_ON(key->link_id >= 0 && sdata->vif.active_links &&
  415. !(sdata->vif.active_links & BIT(key->link_id))))
  416. return -ENOLINK;
  417. trace_drv_set_key(local, cmd, sdata, sta, key);
  418. ret = local->ops->set_key(&local->hw, cmd, &sdata->vif, sta, key);
  419. trace_drv_return_int(local, ret);
  420. return ret;
  421. }
  422. int drv_change_vif_links(struct ieee80211_local *local,
  423. struct ieee80211_sub_if_data *sdata,
  424. u16 old_links, u16 new_links,
  425. struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
  426. {
  427. struct ieee80211_link_data *link;
  428. unsigned long links_to_add;
  429. unsigned long links_to_rem;
  430. unsigned int link_id;
  431. int ret = -EOPNOTSUPP;
  432. might_sleep();
  433. lockdep_assert_wiphy(local->hw.wiphy);
  434. if (!check_sdata_in_driver(sdata))
  435. return -EIO;
  436. if (old_links == new_links)
  437. return 0;
  438. links_to_add = ~old_links & new_links;
  439. links_to_rem = old_links & ~new_links;
  440. for_each_set_bit(link_id, &links_to_rem, IEEE80211_MLD_MAX_NUM_LINKS) {
  441. link = rcu_access_pointer(sdata->link[link_id]);
  442. ieee80211_link_debugfs_drv_remove(link);
  443. }
  444. trace_drv_change_vif_links(local, sdata, old_links, new_links);
  445. if (local->ops->change_vif_links)
  446. ret = local->ops->change_vif_links(&local->hw, &sdata->vif,
  447. old_links, new_links, old);
  448. trace_drv_return_int(local, ret);
  449. if (ret)
  450. return ret;
  451. if (!local->in_reconfig && !local->resuming) {
  452. for_each_set_bit(link_id, &links_to_add,
  453. IEEE80211_MLD_MAX_NUM_LINKS) {
  454. link = rcu_access_pointer(sdata->link[link_id]);
  455. ieee80211_link_debugfs_drv_add(link);
  456. }
  457. }
  458. return 0;
  459. }
  460. int drv_change_sta_links(struct ieee80211_local *local,
  461. struct ieee80211_sub_if_data *sdata,
  462. struct ieee80211_sta *sta,
  463. u16 old_links, u16 new_links)
  464. {
  465. struct sta_info *info = container_of(sta, struct sta_info, sta);
  466. struct link_sta_info *link_sta;
  467. unsigned long links_to_add;
  468. unsigned long links_to_rem;
  469. unsigned int link_id;
  470. int ret = -EOPNOTSUPP;
  471. might_sleep();
  472. lockdep_assert_wiphy(local->hw.wiphy);
  473. if (!check_sdata_in_driver(sdata))
  474. return -EIO;
  475. old_links &= sdata->vif.active_links;
  476. new_links &= sdata->vif.active_links;
  477. if (old_links == new_links)
  478. return 0;
  479. links_to_add = ~old_links & new_links;
  480. links_to_rem = old_links & ~new_links;
  481. for_each_set_bit(link_id, &links_to_rem, IEEE80211_MLD_MAX_NUM_LINKS) {
  482. link_sta = rcu_dereference_protected(info->link[link_id],
  483. lockdep_is_held(&local->hw.wiphy->mtx));
  484. ieee80211_link_sta_debugfs_drv_remove(link_sta);
  485. }
  486. trace_drv_change_sta_links(local, sdata, sta, old_links, new_links);
  487. if (local->ops->change_sta_links)
  488. ret = local->ops->change_sta_links(&local->hw, &sdata->vif, sta,
  489. old_links, new_links);
  490. trace_drv_return_int(local, ret);
  491. if (ret)
  492. return ret;
  493. /* during reconfig don't add it to debugfs again */
  494. if (local->in_reconfig || local->resuming)
  495. return 0;
  496. for_each_set_bit(link_id, &links_to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
  497. link_sta = rcu_dereference_protected(info->link[link_id],
  498. lockdep_is_held(&local->hw.wiphy->mtx));
  499. ieee80211_link_sta_debugfs_drv_add(link_sta);
  500. }
  501. return 0;
  502. }