netdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_tx(struct napi_struct *napi, int budget)
  102. {
  103. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  104. napi_tx);
  105. int tx_done = 0;
  106. uint i;
  107. /* always process ALL Tx complete, regardless budget - it is fast */
  108. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  109. struct vring *vring = &wil->vring_tx[i];
  110. struct vring_tx_data *txdata = &wil->vring_tx_data[i];
  111. struct wil6210_vif *vif;
  112. if (!vring->va || !txdata->enabled ||
  113. txdata->mid >= wil->max_vifs)
  114. continue;
  115. vif = wil->vifs[txdata->mid];
  116. if (unlikely(!vif)) {
  117. wil_dbg_txrx(wil, "Invalid MID %d\n", txdata->mid);
  118. continue;
  119. }
  120. tx_done += wil_tx_complete(vif, i);
  121. }
  122. if (tx_done < budget) {
  123. napi_complete(napi);
  124. wil6210_unmask_irq_tx(wil);
  125. wil_dbg_txrx(wil, "NAPI TX complete\n");
  126. }
  127. wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
  128. return min(tx_done, budget);
  129. }
  130. static void wil_dev_setup(struct net_device *dev)
  131. {
  132. ether_setup(dev);
  133. dev->max_mtu = mtu_max;
  134. dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
  135. }
  136. static void wil_vif_deinit(struct wil6210_vif *vif)
  137. {
  138. del_timer_sync(&vif->scan_timer);
  139. del_timer_sync(&vif->p2p.discovery_timer);
  140. cancel_work_sync(&vif->disconnect_worker);
  141. cancel_work_sync(&vif->p2p.discovery_expired_work);
  142. cancel_work_sync(&vif->p2p.delayed_listen_work);
  143. wil_probe_client_flush(vif);
  144. cancel_work_sync(&vif->probe_client_worker);
  145. }
  146. void wil_vif_free(struct wil6210_vif *vif)
  147. {
  148. struct net_device *ndev = vif_to_ndev(vif);
  149. wil_vif_deinit(vif);
  150. free_netdev(ndev);
  151. }
  152. static void wil_ndev_destructor(struct net_device *ndev)
  153. {
  154. struct wil6210_vif *vif = ndev_to_vif(ndev);
  155. wil_vif_deinit(vif);
  156. }
  157. static void wil_connect_timer_fn(struct timer_list *t)
  158. {
  159. struct wil6210_vif *vif = from_timer(vif, t, connect_timer);
  160. struct wil6210_priv *wil = vif_to_wil(vif);
  161. bool q;
  162. wil_err(wil, "Connect timeout detected, disconnect station\n");
  163. /* reschedule to thread context - disconnect won't
  164. * run from atomic context.
  165. * queue on wmi_wq to prevent race with connect event.
  166. */
  167. q = queue_work(wil->wmi_wq, &vif->disconnect_worker);
  168. wil_dbg_wmi(wil, "queue_work of disconnect_worker -> %d\n", q);
  169. }
  170. static void wil_scan_timer_fn(struct timer_list *t)
  171. {
  172. struct wil6210_vif *vif = from_timer(vif, t, scan_timer);
  173. struct wil6210_priv *wil = vif_to_wil(vif);
  174. clear_bit(wil_status_fwready, wil->status);
  175. wil_err(wil, "Scan timeout detected, start fw error recovery\n");
  176. wil_fw_error_recovery(wil);
  177. }
  178. static void wil_p2p_discovery_timer_fn(struct timer_list *t)
  179. {
  180. struct wil6210_vif *vif = from_timer(vif, t, p2p.discovery_timer);
  181. struct wil6210_priv *wil = vif_to_wil(vif);
  182. wil_dbg_misc(wil, "p2p_discovery_timer_fn\n");
  183. schedule_work(&vif->p2p.discovery_expired_work);
  184. }
  185. static void wil_vif_init(struct wil6210_vif *vif)
  186. {
  187. vif->bcast_vring = -1;
  188. mutex_init(&vif->probe_client_mutex);
  189. timer_setup(&vif->connect_timer, wil_connect_timer_fn, 0);
  190. timer_setup(&vif->scan_timer, wil_scan_timer_fn, 0);
  191. timer_setup(&vif->p2p.discovery_timer, wil_p2p_discovery_timer_fn, 0);
  192. INIT_WORK(&vif->probe_client_worker, wil_probe_client_worker);
  193. INIT_WORK(&vif->disconnect_worker, wil_disconnect_worker);
  194. INIT_WORK(&vif->p2p.delayed_listen_work, wil_p2p_delayed_listen_work);
  195. INIT_LIST_HEAD(&vif->probe_client_pending);
  196. vif->net_queue_stopped = 1;
  197. }
  198. static u8 wil_vif_find_free_mid(struct wil6210_priv *wil)
  199. {
  200. u8 i;
  201. for (i = 0; i < wil->max_vifs; i++) {
  202. if (!wil->vifs[i])
  203. return i;
  204. }
  205. return U8_MAX;
  206. }
  207. struct wil6210_vif *
  208. wil_vif_alloc(struct wil6210_priv *wil, const char *name,
  209. unsigned char name_assign_type, enum nl80211_iftype iftype)
  210. {
  211. struct net_device *ndev;
  212. struct wireless_dev *wdev;
  213. struct wil6210_vif *vif;
  214. u8 mid;
  215. mid = wil_vif_find_free_mid(wil);
  216. if (mid == U8_MAX) {
  217. wil_err(wil, "no available virtual interface\n");
  218. return ERR_PTR(-EINVAL);
  219. }
  220. ndev = alloc_netdev(sizeof(*vif), name, name_assign_type,
  221. wil_dev_setup);
  222. if (!ndev) {
  223. dev_err(wil_to_dev(wil), "alloc_netdev failed\n");
  224. return ERR_PTR(-ENOMEM);
  225. }
  226. if (mid == 0) {
  227. wil->main_ndev = ndev;
  228. } else {
  229. ndev->priv_destructor = wil_ndev_destructor;
  230. ndev->needs_free_netdev = true;
  231. }
  232. vif = ndev_to_vif(ndev);
  233. vif->ndev = ndev;
  234. vif->wil = wil;
  235. vif->mid = mid;
  236. wil_vif_init(vif);
  237. wdev = &vif->wdev;
  238. wdev->wiphy = wil->wiphy;
  239. wdev->iftype = iftype;
  240. ndev->netdev_ops = &wil_netdev_ops;
  241. wil_set_ethtoolops(ndev);
  242. ndev->ieee80211_ptr = wdev;
  243. ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
  244. NETIF_F_SG | NETIF_F_GRO |
  245. NETIF_F_TSO | NETIF_F_TSO6 |
  246. NETIF_F_RXHASH;
  247. ndev->features |= ndev->hw_features;
  248. SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
  249. wdev->netdev = ndev;
  250. return vif;
  251. }
  252. void *wil_if_alloc(struct device *dev)
  253. {
  254. struct wil6210_priv *wil;
  255. struct wil6210_vif *vif;
  256. int rc = 0;
  257. wil = wil_cfg80211_init(dev);
  258. if (IS_ERR(wil)) {
  259. dev_err(dev, "wil_cfg80211_init failed\n");
  260. return wil;
  261. }
  262. rc = wil_priv_init(wil);
  263. if (rc) {
  264. dev_err(dev, "wil_priv_init failed\n");
  265. goto out_cfg;
  266. }
  267. wil_dbg_misc(wil, "if_alloc\n");
  268. vif = wil_vif_alloc(wil, "wlan%d", NET_NAME_UNKNOWN,
  269. NL80211_IFTYPE_STATION);
  270. if (IS_ERR(vif)) {
  271. dev_err(dev, "wil_vif_alloc failed\n");
  272. rc = -ENOMEM;
  273. goto out_priv;
  274. }
  275. wil->radio_wdev = vif_to_wdev(vif);
  276. return wil;
  277. out_priv:
  278. wil_priv_deinit(wil);
  279. out_cfg:
  280. wil_cfg80211_deinit(wil);
  281. return ERR_PTR(rc);
  282. }
  283. void wil_if_free(struct wil6210_priv *wil)
  284. {
  285. struct net_device *ndev = wil->main_ndev;
  286. wil_dbg_misc(wil, "if_free\n");
  287. if (!ndev)
  288. return;
  289. wil_priv_deinit(wil);
  290. wil->main_ndev = NULL;
  291. wil_ndev_destructor(ndev);
  292. free_netdev(ndev);
  293. wil_cfg80211_deinit(wil);
  294. }
  295. int wil_vif_add(struct wil6210_priv *wil, struct wil6210_vif *vif)
  296. {
  297. struct net_device *ndev = vif_to_ndev(vif);
  298. struct wireless_dev *wdev = vif_to_wdev(vif);
  299. bool any_active = wil_has_active_ifaces(wil, true, false);
  300. int rc;
  301. ASSERT_RTNL();
  302. if (wil->vifs[vif->mid]) {
  303. dev_err(&ndev->dev, "VIF with mid %d already in use\n",
  304. vif->mid);
  305. return -EEXIST;
  306. }
  307. if (any_active && vif->mid != 0) {
  308. rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr,
  309. wdev->iftype);
  310. if (rc)
  311. return rc;
  312. }
  313. rc = register_netdevice(ndev);
  314. if (rc < 0) {
  315. dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
  316. if (any_active && vif->mid != 0)
  317. wmi_port_delete(wil, vif->mid);
  318. return rc;
  319. }
  320. wil->vifs[vif->mid] = vif;
  321. return 0;
  322. }
  323. int wil_if_add(struct wil6210_priv *wil)
  324. {
  325. struct wiphy *wiphy = wil->wiphy;
  326. struct net_device *ndev = wil->main_ndev;
  327. struct wil6210_vif *vif = ndev_to_vif(ndev);
  328. int rc;
  329. wil_dbg_misc(wil, "entered");
  330. strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
  331. rc = wiphy_register(wiphy);
  332. if (rc < 0) {
  333. wil_err(wil, "failed to register wiphy, err %d\n", rc);
  334. return rc;
  335. }
  336. init_dummy_netdev(&wil->napi_ndev);
  337. netif_napi_add(&wil->napi_ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
  338. WIL6210_NAPI_BUDGET);
  339. netif_tx_napi_add(&wil->napi_ndev,
  340. &wil->napi_tx, wil6210_netdev_poll_tx,
  341. WIL6210_NAPI_BUDGET);
  342. wil_update_net_queues_bh(wil, vif, NULL, true);
  343. rtnl_lock();
  344. rc = wil_vif_add(wil, vif);
  345. rtnl_unlock();
  346. if (rc < 0)
  347. goto out_wiphy;
  348. return 0;
  349. out_wiphy:
  350. wiphy_unregister(wiphy);
  351. return rc;
  352. }
  353. void wil_vif_remove(struct wil6210_priv *wil, u8 mid)
  354. {
  355. struct wil6210_vif *vif;
  356. struct net_device *ndev;
  357. bool any_active = wil_has_active_ifaces(wil, true, false);
  358. ASSERT_RTNL();
  359. if (mid >= wil->max_vifs) {
  360. wil_err(wil, "invalid MID: %d\n", mid);
  361. return;
  362. }
  363. vif = wil->vifs[mid];
  364. if (!vif) {
  365. wil_err(wil, "MID %d not registered\n", mid);
  366. return;
  367. }
  368. mutex_lock(&wil->mutex);
  369. wil6210_disconnect(vif, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
  370. mutex_unlock(&wil->mutex);
  371. ndev = vif_to_ndev(vif);
  372. /* during unregister_netdevice cfg80211_leave may perform operations
  373. * such as stop AP, disconnect, so we only clear the VIF afterwards
  374. */
  375. unregister_netdevice(ndev);
  376. if (any_active && vif->mid != 0)
  377. wmi_port_delete(wil, vif->mid);
  378. /* make sure no one is accessing the VIF before removing */
  379. mutex_lock(&wil->vif_mutex);
  380. wil->vifs[mid] = NULL;
  381. /* ensure NAPI code will see the NULL VIF */
  382. wmb();
  383. if (test_bit(wil_status_napi_en, wil->status)) {
  384. napi_synchronize(&wil->napi_rx);
  385. napi_synchronize(&wil->napi_tx);
  386. }
  387. mutex_unlock(&wil->vif_mutex);
  388. flush_work(&wil->wmi_event_worker);
  389. del_timer_sync(&vif->connect_timer);
  390. cancel_work_sync(&vif->disconnect_worker);
  391. wil_probe_client_flush(vif);
  392. cancel_work_sync(&vif->probe_client_worker);
  393. /* for VIFs, ndev will be freed by destructor after RTNL is unlocked.
  394. * the main interface will be freed in wil_if_free, we need to keep it
  395. * a bit longer so logging macros will work.
  396. */
  397. }
  398. void wil_if_remove(struct wil6210_priv *wil)
  399. {
  400. struct net_device *ndev = wil->main_ndev;
  401. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  402. wil_dbg_misc(wil, "if_remove\n");
  403. rtnl_lock();
  404. wil_vif_remove(wil, 0);
  405. rtnl_unlock();
  406. netif_napi_del(&wil->napi_tx);
  407. netif_napi_del(&wil->napi_rx);
  408. wiphy_unregister(wdev->wiphy);
  409. }