netdev.c 13 KB

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