netdev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/etherdevice.h>
  17. #include "wil6210.h"
  18. #include "txrx.h"
  19. static int wil_open(struct net_device *ndev)
  20. {
  21. struct wil6210_priv *wil = ndev_to_wil(ndev);
  22. wil_dbg_misc(wil, "open\n");
  23. if (debug_fw ||
  24. test_bit(WMI_FW_CAPABILITY_WMI_ONLY, wil->fw_capabilities)) {
  25. wil_err(wil, "while in debug_fw or wmi_only mode\n");
  26. return -EINVAL;
  27. }
  28. return wil_up(wil);
  29. }
  30. static int wil_stop(struct net_device *ndev)
  31. {
  32. struct wil6210_priv *wil = ndev_to_wil(ndev);
  33. wil_dbg_misc(wil, "stop\n");
  34. return wil_down(wil);
  35. }
  36. static const struct net_device_ops wil_netdev_ops = {
  37. .ndo_open = wil_open,
  38. .ndo_stop = wil_stop,
  39. .ndo_start_xmit = wil_start_xmit,
  40. .ndo_set_mac_address = eth_mac_addr,
  41. .ndo_validate_addr = eth_validate_addr,
  42. };
  43. static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
  44. {
  45. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  46. napi_rx);
  47. int quota = budget;
  48. int done;
  49. wil_rx_handle(wil, &quota);
  50. done = budget - quota;
  51. if (done < budget) {
  52. napi_complete_done(napi, done);
  53. wil6210_unmask_irq_rx(wil);
  54. wil_dbg_txrx(wil, "NAPI RX complete\n");
  55. }
  56. wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
  57. return done;
  58. }
  59. static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
  60. {
  61. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  62. napi_tx);
  63. int tx_done = 0;
  64. uint i;
  65. /* always process ALL Tx complete, regardless budget - it is fast */
  66. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  67. struct vring *vring = &wil->vring_tx[i];
  68. struct vring_tx_data *txdata = &wil->vring_tx_data[i];
  69. if (!vring->va || !txdata->enabled)
  70. continue;
  71. tx_done += wil_tx_complete(wil, i);
  72. }
  73. if (tx_done < budget) {
  74. napi_complete(napi);
  75. wil6210_unmask_irq_tx(wil);
  76. wil_dbg_txrx(wil, "NAPI TX complete\n");
  77. }
  78. wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
  79. return min(tx_done, budget);
  80. }
  81. static void wil_dev_setup(struct net_device *dev)
  82. {
  83. ether_setup(dev);
  84. dev->max_mtu = mtu_max;
  85. dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
  86. }
  87. void *wil_if_alloc(struct device *dev)
  88. {
  89. struct net_device *ndev;
  90. struct wireless_dev *wdev;
  91. struct wil6210_priv *wil;
  92. struct ieee80211_channel *ch;
  93. int rc = 0;
  94. wdev = wil_cfg80211_init(dev);
  95. if (IS_ERR(wdev)) {
  96. dev_err(dev, "wil_cfg80211_init failed\n");
  97. return wdev;
  98. }
  99. wil = wdev_to_wil(wdev);
  100. wil->wdev = wdev;
  101. wil->radio_wdev = wdev;
  102. wil_dbg_misc(wil, "if_alloc\n");
  103. rc = wil_priv_init(wil);
  104. if (rc) {
  105. dev_err(dev, "wil_priv_init failed\n");
  106. goto out_wdev;
  107. }
  108. wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
  109. /* default monitor channel */
  110. ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels;
  111. cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
  112. ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup);
  113. if (!ndev) {
  114. dev_err(dev, "alloc_netdev_mqs failed\n");
  115. rc = -ENOMEM;
  116. goto out_priv;
  117. }
  118. ndev->netdev_ops = &wil_netdev_ops;
  119. wil_set_ethtoolops(ndev);
  120. ndev->ieee80211_ptr = wdev;
  121. ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
  122. NETIF_F_SG | NETIF_F_GRO |
  123. NETIF_F_TSO | NETIF_F_TSO6 |
  124. NETIF_F_RXHASH;
  125. ndev->features |= ndev->hw_features;
  126. SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
  127. wdev->netdev = ndev;
  128. return wil;
  129. out_priv:
  130. wil_priv_deinit(wil);
  131. out_wdev:
  132. wil_wdev_free(wil);
  133. return ERR_PTR(rc);
  134. }
  135. void wil_if_free(struct wil6210_priv *wil)
  136. {
  137. struct net_device *ndev = wil_to_ndev(wil);
  138. wil_dbg_misc(wil, "if_free\n");
  139. if (!ndev)
  140. return;
  141. wil_priv_deinit(wil);
  142. wil_to_ndev(wil) = NULL;
  143. free_netdev(ndev);
  144. wil_wdev_free(wil);
  145. }
  146. int wil_if_add(struct wil6210_priv *wil)
  147. {
  148. struct wireless_dev *wdev = wil_to_wdev(wil);
  149. struct wiphy *wiphy = wdev->wiphy;
  150. struct net_device *ndev = wil_to_ndev(wil);
  151. int rc;
  152. wil_dbg_misc(wil, "entered");
  153. strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
  154. rc = wiphy_register(wiphy);
  155. if (rc < 0) {
  156. wil_err(wil, "failed to register wiphy, err %d\n", rc);
  157. return rc;
  158. }
  159. netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
  160. WIL6210_NAPI_BUDGET);
  161. netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
  162. WIL6210_NAPI_BUDGET);
  163. wil_update_net_queues_bh(wil, NULL, true);
  164. rc = register_netdev(ndev);
  165. if (rc < 0) {
  166. dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
  167. goto out_wiphy;
  168. }
  169. return 0;
  170. out_wiphy:
  171. wiphy_unregister(wdev->wiphy);
  172. return rc;
  173. }
  174. void wil_if_remove(struct wil6210_priv *wil)
  175. {
  176. struct net_device *ndev = wil_to_ndev(wil);
  177. struct wireless_dev *wdev = wil_to_wdev(wil);
  178. wil_dbg_misc(wil, "if_remove\n");
  179. unregister_netdev(ndev);
  180. wiphy_unregister(wdev->wiphy);
  181. }