netdev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
  4. * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/etherdevice.h>
  7. #include <linux/rtnetlink.h>
  8. #include "wil6210.h"
  9. #include "txrx.h"
  10. bool wil_has_other_active_ifaces(struct wil6210_priv *wil,
  11. struct net_device *ndev, bool up, bool ok)
  12. {
  13. int i;
  14. struct wil6210_vif *vif;
  15. struct net_device *ndev_i;
  16. for (i = 0; i < GET_MAX_VIFS(wil); i++) {
  17. vif = wil->vifs[i];
  18. if (vif) {
  19. ndev_i = vif_to_ndev(vif);
  20. if (ndev_i != ndev)
  21. if ((up && (ndev_i->flags & IFF_UP)) ||
  22. (ok && netif_carrier_ok(ndev_i)))
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. bool wil_has_active_ifaces(struct wil6210_priv *wil, bool up, bool ok)
  29. {
  30. /* use NULL ndev argument to check all interfaces */
  31. return wil_has_other_active_ifaces(wil, NULL, up, ok);
  32. }
  33. static int wil_open(struct net_device *ndev)
  34. {
  35. struct wil6210_priv *wil = ndev_to_wil(ndev);
  36. int rc = 0;
  37. wil_dbg_misc(wil, "open\n");
  38. if (debug_fw ||
  39. test_bit(WMI_FW_CAPABILITY_WMI_ONLY, wil->fw_capabilities)) {
  40. wil_err(wil, "while in debug_fw or wmi_only mode\n");
  41. return -EINVAL;
  42. }
  43. if (!wil_has_other_active_ifaces(wil, ndev, true, false)) {
  44. wil_dbg_misc(wil, "open, first iface\n");
  45. rc = wil_pm_runtime_get(wil);
  46. if (rc < 0)
  47. return rc;
  48. rc = wil_up(wil);
  49. if (rc)
  50. wil_pm_runtime_put(wil);
  51. }
  52. return rc;
  53. }
  54. static int wil_stop(struct net_device *ndev)
  55. {
  56. struct wil6210_priv *wil = ndev_to_wil(ndev);
  57. int rc = 0;
  58. wil_dbg_misc(wil, "stop\n");
  59. if (!wil_has_other_active_ifaces(wil, ndev, true, false)) {
  60. wil_dbg_misc(wil, "stop, last iface\n");
  61. rc = wil_down(wil);
  62. if (!rc)
  63. wil_pm_runtime_put(wil);
  64. }
  65. return rc;
  66. }
  67. static const struct net_device_ops wil_netdev_ops = {
  68. .ndo_open = wil_open,
  69. .ndo_stop = wil_stop,
  70. .ndo_start_xmit = wil_start_xmit,
  71. .ndo_set_mac_address = eth_mac_addr,
  72. .ndo_validate_addr = eth_validate_addr,
  73. };
  74. static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
  75. {
  76. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  77. napi_rx);
  78. int quota = budget;
  79. int done;
  80. wil_rx_handle(wil, &quota);
  81. done = budget - quota;
  82. if (done < budget) {
  83. napi_complete_done(napi, done);
  84. wil6210_unmask_irq_rx(wil);
  85. wil_dbg_txrx(wil, "NAPI RX complete\n");
  86. }
  87. wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
  88. return done;
  89. }
  90. static int wil6210_netdev_poll_rx_edma(struct napi_struct *napi, int budget)
  91. {
  92. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  93. napi_rx);
  94. int quota = budget;
  95. int done;
  96. wil_rx_handle_edma(wil, &quota);
  97. done = budget - quota;
  98. if (done < budget) {
  99. napi_complete_done(napi, done);
  100. wil6210_unmask_irq_rx_edma(wil);
  101. wil_dbg_txrx(wil, "NAPI RX complete\n");
  102. }
  103. wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
  104. return done;
  105. }
  106. static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
  107. {
  108. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  109. napi_tx);
  110. int tx_done = 0;
  111. uint i;
  112. /* always process ALL Tx complete, regardless budget - it is fast */
  113. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  114. struct wil_ring *ring = &wil->ring_tx[i];
  115. struct wil_ring_tx_data *txdata = &wil->ring_tx_data[i];
  116. struct wil6210_vif *vif;
  117. if (!ring->va || !txdata->enabled ||
  118. txdata->mid >= GET_MAX_VIFS(wil))
  119. continue;
  120. vif = wil->vifs[txdata->mid];
  121. if (unlikely(!vif)) {
  122. wil_dbg_txrx(wil, "Invalid MID %d\n", txdata->mid);
  123. continue;
  124. }
  125. tx_done += wil_tx_complete(vif, i);
  126. }
  127. if (tx_done < budget) {
  128. napi_complete(napi);
  129. wil6210_unmask_irq_tx(wil);
  130. wil_dbg_txrx(wil, "NAPI TX complete\n");
  131. }
  132. wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
  133. return min(tx_done, budget);
  134. }
  135. static int wil6210_netdev_poll_tx_edma(struct napi_struct *napi, int budget)
  136. {
  137. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  138. napi_tx);
  139. int tx_done;
  140. /* There is only one status TX ring */
  141. struct wil_status_ring *sring = &wil->srings[wil->tx_sring_idx];
  142. if (!sring->va)
  143. return 0;
  144. tx_done = wil_tx_sring_handler(wil, sring);
  145. if (tx_done < budget) {
  146. napi_complete(napi);
  147. wil6210_unmask_irq_tx_edma(wil);
  148. wil_dbg_txrx(wil, "NAPI TX complete\n");
  149. }
  150. wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
  151. return min(tx_done, budget);
  152. }
  153. static void wil_dev_setup(struct net_device *dev)
  154. {
  155. ether_setup(dev);
  156. dev->max_mtu = mtu_max;
  157. dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
  158. }
  159. static void wil_vif_deinit(struct wil6210_vif *vif)
  160. {
  161. del_timer_sync(&vif->scan_timer);
  162. del_timer_sync(&vif->p2p.discovery_timer);
  163. cancel_work_sync(&vif->disconnect_worker);
  164. cancel_work_sync(&vif->p2p.discovery_expired_work);
  165. cancel_work_sync(&vif->p2p.delayed_listen_work);
  166. wil_probe_client_flush(vif);
  167. cancel_work_sync(&vif->probe_client_worker);
  168. cancel_work_sync(&vif->enable_tx_key_worker);
  169. }
  170. void wil_vif_free(struct wil6210_vif *vif)
  171. {
  172. struct net_device *ndev = vif_to_ndev(vif);
  173. wil_vif_deinit(vif);
  174. free_netdev(ndev);
  175. }
  176. static void wil_ndev_destructor(struct net_device *ndev)
  177. {
  178. struct wil6210_vif *vif = ndev_to_vif(ndev);
  179. wil_vif_deinit(vif);
  180. }
  181. static void wil_connect_timer_fn(struct timer_list *t)
  182. {
  183. struct wil6210_vif *vif = from_timer(vif, t, connect_timer);
  184. struct wil6210_priv *wil = vif_to_wil(vif);
  185. bool q;
  186. wil_err(wil, "Connect timeout detected, disconnect station\n");
  187. /* reschedule to thread context - disconnect won't
  188. * run from atomic context.
  189. * queue on wmi_wq to prevent race with connect event.
  190. */
  191. q = queue_work(wil->wmi_wq, &vif->disconnect_worker);
  192. wil_dbg_wmi(wil, "queue_work of disconnect_worker -> %d\n", q);
  193. }
  194. static void wil_scan_timer_fn(struct timer_list *t)
  195. {
  196. struct wil6210_vif *vif = from_timer(vif, t, scan_timer);
  197. struct wil6210_priv *wil = vif_to_wil(vif);
  198. clear_bit(wil_status_fwready, wil->status);
  199. wil_err(wil, "Scan timeout detected, start fw error recovery\n");
  200. wil_fw_error_recovery(wil);
  201. }
  202. static void wil_p2p_discovery_timer_fn(struct timer_list *t)
  203. {
  204. struct wil6210_vif *vif = from_timer(vif, t, p2p.discovery_timer);
  205. struct wil6210_priv *wil = vif_to_wil(vif);
  206. wil_dbg_misc(wil, "p2p_discovery_timer_fn\n");
  207. schedule_work(&vif->p2p.discovery_expired_work);
  208. }
  209. static void wil_vif_init(struct wil6210_vif *vif)
  210. {
  211. vif->bcast_ring = -1;
  212. mutex_init(&vif->probe_client_mutex);
  213. timer_setup(&vif->connect_timer, wil_connect_timer_fn, 0);
  214. timer_setup(&vif->scan_timer, wil_scan_timer_fn, 0);
  215. timer_setup(&vif->p2p.discovery_timer, wil_p2p_discovery_timer_fn, 0);
  216. INIT_WORK(&vif->probe_client_worker, wil_probe_client_worker);
  217. INIT_WORK(&vif->disconnect_worker, wil_disconnect_worker);
  218. INIT_WORK(&vif->p2p.discovery_expired_work, wil_p2p_listen_expired);
  219. INIT_WORK(&vif->p2p.delayed_listen_work, wil_p2p_delayed_listen_work);
  220. INIT_WORK(&vif->enable_tx_key_worker, wil_enable_tx_key_worker);
  221. INIT_LIST_HEAD(&vif->probe_client_pending);
  222. vif->net_queue_stopped = 1;
  223. }
  224. static u8 wil_vif_find_free_mid(struct wil6210_priv *wil)
  225. {
  226. u8 i;
  227. for (i = 0; i < GET_MAX_VIFS(wil); i++) {
  228. if (!wil->vifs[i])
  229. return i;
  230. }
  231. return U8_MAX;
  232. }
  233. struct wil6210_vif *
  234. wil_vif_alloc(struct wil6210_priv *wil, const char *name,
  235. unsigned char name_assign_type, enum nl80211_iftype iftype)
  236. {
  237. struct net_device *ndev;
  238. struct wireless_dev *wdev;
  239. struct wil6210_vif *vif;
  240. u8 mid;
  241. mid = wil_vif_find_free_mid(wil);
  242. if (mid == U8_MAX) {
  243. wil_err(wil, "no available virtual interface\n");
  244. return ERR_PTR(-EINVAL);
  245. }
  246. ndev = alloc_netdev(sizeof(*vif), name, name_assign_type,
  247. wil_dev_setup);
  248. if (!ndev) {
  249. dev_err(wil_to_dev(wil), "alloc_netdev failed\n");
  250. return ERR_PTR(-ENOMEM);
  251. }
  252. if (mid == 0) {
  253. wil->main_ndev = ndev;
  254. } else {
  255. ndev->priv_destructor = wil_ndev_destructor;
  256. ndev->needs_free_netdev = true;
  257. }
  258. vif = ndev_to_vif(ndev);
  259. vif->ndev = ndev;
  260. vif->wil = wil;
  261. vif->mid = mid;
  262. wil_vif_init(vif);
  263. wdev = &vif->wdev;
  264. wdev->wiphy = wil->wiphy;
  265. wdev->iftype = iftype;
  266. ndev->netdev_ops = &wil_netdev_ops;
  267. wil_set_ethtoolops(ndev);
  268. ndev->ieee80211_ptr = wdev;
  269. ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
  270. NETIF_F_SG | NETIF_F_GRO |
  271. NETIF_F_TSO | NETIF_F_TSO6;
  272. ndev->features |= ndev->hw_features;
  273. SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
  274. wdev->netdev = ndev;
  275. return vif;
  276. }
  277. void *wil_if_alloc(struct device *dev)
  278. {
  279. struct wil6210_priv *wil;
  280. struct wil6210_vif *vif;
  281. int rc = 0;
  282. wil = wil_cfg80211_init(dev);
  283. if (IS_ERR(wil)) {
  284. dev_err(dev, "wil_cfg80211_init failed\n");
  285. return wil;
  286. }
  287. rc = wil_priv_init(wil);
  288. if (rc) {
  289. dev_err(dev, "wil_priv_init failed\n");
  290. goto out_cfg;
  291. }
  292. wil_dbg_misc(wil, "if_alloc\n");
  293. vif = wil_vif_alloc(wil, "wlan%d", NET_NAME_UNKNOWN,
  294. NL80211_IFTYPE_STATION);
  295. if (IS_ERR(vif)) {
  296. dev_err(dev, "wil_vif_alloc failed\n");
  297. rc = -ENOMEM;
  298. goto out_priv;
  299. }
  300. wil->radio_wdev = vif_to_wdev(vif);
  301. return wil;
  302. out_priv:
  303. wil_priv_deinit(wil);
  304. out_cfg:
  305. wil_cfg80211_deinit(wil);
  306. return ERR_PTR(rc);
  307. }
  308. void wil_if_free(struct wil6210_priv *wil)
  309. {
  310. struct net_device *ndev = wil->main_ndev;
  311. wil_dbg_misc(wil, "if_free\n");
  312. if (!ndev)
  313. return;
  314. wil_priv_deinit(wil);
  315. wil->main_ndev = NULL;
  316. wil_ndev_destructor(ndev);
  317. free_netdev(ndev);
  318. wil_cfg80211_deinit(wil);
  319. }
  320. int wil_vif_add(struct wil6210_priv *wil, struct wil6210_vif *vif)
  321. {
  322. struct net_device *ndev = vif_to_ndev(vif);
  323. struct wireless_dev *wdev = vif_to_wdev(vif);
  324. bool any_active = wil_has_active_ifaces(wil, true, false);
  325. int rc;
  326. ASSERT_RTNL();
  327. if (wil->vifs[vif->mid]) {
  328. dev_err(&ndev->dev, "VIF with mid %d already in use\n",
  329. vif->mid);
  330. return -EEXIST;
  331. }
  332. if (any_active && vif->mid != 0) {
  333. rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr,
  334. wdev->iftype);
  335. if (rc)
  336. return rc;
  337. }
  338. rc = cfg80211_register_netdevice(ndev);
  339. if (rc < 0) {
  340. dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
  341. if (any_active && vif->mid != 0)
  342. wmi_port_delete(wil, vif->mid);
  343. return rc;
  344. }
  345. wil->vifs[vif->mid] = vif;
  346. return 0;
  347. }
  348. int wil_if_add(struct wil6210_priv *wil)
  349. {
  350. struct wiphy *wiphy = wil->wiphy;
  351. struct net_device *ndev = wil->main_ndev;
  352. struct wil6210_vif *vif = ndev_to_vif(ndev);
  353. int rc;
  354. wil_dbg_misc(wil, "entered");
  355. strscpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
  356. rc = wiphy_register(wiphy);
  357. if (rc < 0) {
  358. wil_err(wil, "failed to register wiphy, err %d\n", rc);
  359. return rc;
  360. }
  361. init_dummy_netdev(&wil->napi_ndev);
  362. if (wil->use_enhanced_dma_hw) {
  363. netif_napi_add(&wil->napi_ndev, &wil->napi_rx,
  364. wil6210_netdev_poll_rx_edma);
  365. netif_napi_add_tx(&wil->napi_ndev,
  366. &wil->napi_tx, wil6210_netdev_poll_tx_edma);
  367. } else {
  368. netif_napi_add(&wil->napi_ndev, &wil->napi_rx,
  369. wil6210_netdev_poll_rx);
  370. netif_napi_add_tx(&wil->napi_ndev,
  371. &wil->napi_tx, wil6210_netdev_poll_tx);
  372. }
  373. wil_update_net_queues_bh(wil, vif, NULL, true);
  374. rtnl_lock();
  375. wiphy_lock(wiphy);
  376. rc = wil_vif_add(wil, vif);
  377. wiphy_unlock(wiphy);
  378. rtnl_unlock();
  379. if (rc < 0)
  380. goto out_wiphy;
  381. return 0;
  382. out_wiphy:
  383. wiphy_unregister(wiphy);
  384. return rc;
  385. }
  386. void wil_vif_remove(struct wil6210_priv *wil, u8 mid)
  387. {
  388. struct wil6210_vif *vif;
  389. struct net_device *ndev;
  390. bool any_active = wil_has_active_ifaces(wil, true, false);
  391. ASSERT_RTNL();
  392. if (mid >= GET_MAX_VIFS(wil)) {
  393. wil_err(wil, "invalid MID: %d\n", mid);
  394. return;
  395. }
  396. vif = wil->vifs[mid];
  397. if (!vif) {
  398. wil_err(wil, "MID %d not registered\n", mid);
  399. return;
  400. }
  401. mutex_lock(&wil->mutex);
  402. wil6210_disconnect(vif, NULL, WLAN_REASON_DEAUTH_LEAVING);
  403. mutex_unlock(&wil->mutex);
  404. ndev = vif_to_ndev(vif);
  405. /* during unregister_netdevice cfg80211_leave may perform operations
  406. * such as stop AP, disconnect, so we only clear the VIF afterwards
  407. */
  408. cfg80211_unregister_netdevice(ndev);
  409. if (any_active && vif->mid != 0)
  410. wmi_port_delete(wil, vif->mid);
  411. /* make sure no one is accessing the VIF before removing */
  412. mutex_lock(&wil->vif_mutex);
  413. wil->vifs[mid] = NULL;
  414. /* ensure NAPI code will see the NULL VIF */
  415. wmb();
  416. if (test_bit(wil_status_napi_en, wil->status)) {
  417. napi_synchronize(&wil->napi_rx);
  418. napi_synchronize(&wil->napi_tx);
  419. }
  420. mutex_unlock(&wil->vif_mutex);
  421. flush_work(&wil->wmi_event_worker);
  422. del_timer_sync(&vif->connect_timer);
  423. cancel_work_sync(&vif->disconnect_worker);
  424. wil_probe_client_flush(vif);
  425. cancel_work_sync(&vif->probe_client_worker);
  426. cancel_work_sync(&vif->enable_tx_key_worker);
  427. /* for VIFs, ndev will be freed by destructor after RTNL is unlocked.
  428. * the main interface will be freed in wil_if_free, we need to keep it
  429. * a bit longer so logging macros will work.
  430. */
  431. }
  432. void wil_if_remove(struct wil6210_priv *wil)
  433. {
  434. struct net_device *ndev = wil->main_ndev;
  435. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  436. struct wiphy *wiphy = wdev->wiphy;
  437. wil_dbg_misc(wil, "if_remove\n");
  438. rtnl_lock();
  439. wiphy_lock(wiphy);
  440. wil_vif_remove(wil, 0);
  441. wiphy_unlock(wiphy);
  442. rtnl_unlock();
  443. netif_napi_del(&wil->napi_tx);
  444. netif_napi_del(&wil->napi_rx);
  445. wiphy_unregister(wiphy);
  446. }