netdev.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 int wil_do_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
  37. {
  38. struct wil6210_priv *wil = ndev_to_wil(ndev);
  39. return wil_ioctl(wil, ifr->ifr_data, cmd);
  40. }
  41. static const struct net_device_ops wil_netdev_ops = {
  42. .ndo_open = wil_open,
  43. .ndo_stop = wil_stop,
  44. .ndo_start_xmit = wil_start_xmit,
  45. .ndo_set_mac_address = eth_mac_addr,
  46. .ndo_validate_addr = eth_validate_addr,
  47. .ndo_do_ioctl = wil_do_ioctl,
  48. };
  49. static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
  50. {
  51. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  52. napi_rx);
  53. int quota = budget;
  54. int done;
  55. wil_rx_handle(wil, &quota);
  56. done = budget - quota;
  57. if (done < budget) {
  58. napi_complete_done(napi, done);
  59. wil6210_unmask_irq_rx(wil);
  60. wil_dbg_txrx(wil, "NAPI RX complete\n");
  61. }
  62. wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
  63. return done;
  64. }
  65. static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
  66. {
  67. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  68. napi_tx);
  69. int tx_done = 0;
  70. uint i;
  71. /* always process ALL Tx complete, regardless budget - it is fast */
  72. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  73. struct vring *vring = &wil->vring_tx[i];
  74. struct vring_tx_data *txdata = &wil->vring_tx_data[i];
  75. if (!vring->va || !txdata->enabled)
  76. continue;
  77. tx_done += wil_tx_complete(wil, i);
  78. }
  79. if (tx_done < budget) {
  80. napi_complete(napi);
  81. wil6210_unmask_irq_tx(wil);
  82. wil_dbg_txrx(wil, "NAPI TX complete\n");
  83. }
  84. wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
  85. return min(tx_done, budget);
  86. }
  87. static void wil_dev_setup(struct net_device *dev)
  88. {
  89. ether_setup(dev);
  90. dev->max_mtu = mtu_max;
  91. dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
  92. }
  93. void *wil_if_alloc(struct device *dev)
  94. {
  95. struct net_device *ndev;
  96. struct wireless_dev *wdev;
  97. struct wil6210_priv *wil;
  98. struct ieee80211_channel *ch;
  99. int rc = 0;
  100. wdev = wil_cfg80211_init(dev);
  101. if (IS_ERR(wdev)) {
  102. dev_err(dev, "wil_cfg80211_init failed\n");
  103. return wdev;
  104. }
  105. wil = wdev_to_wil(wdev);
  106. wil->wdev = wdev;
  107. wil->radio_wdev = wdev;
  108. wil_dbg_misc(wil, "if_alloc\n");
  109. rc = wil_priv_init(wil);
  110. if (rc) {
  111. dev_err(dev, "wil_priv_init failed\n");
  112. goto out_wdev;
  113. }
  114. wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
  115. /* default monitor channel */
  116. ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels;
  117. cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
  118. ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup);
  119. if (!ndev) {
  120. dev_err(dev, "alloc_netdev_mqs failed\n");
  121. rc = -ENOMEM;
  122. goto out_priv;
  123. }
  124. ndev->netdev_ops = &wil_netdev_ops;
  125. wil_set_ethtoolops(ndev);
  126. ndev->ieee80211_ptr = wdev;
  127. ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
  128. NETIF_F_SG | NETIF_F_GRO |
  129. NETIF_F_TSO | NETIF_F_TSO6 |
  130. NETIF_F_RXHASH;
  131. ndev->features |= ndev->hw_features;
  132. SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
  133. wdev->netdev = ndev;
  134. return wil;
  135. out_priv:
  136. wil_priv_deinit(wil);
  137. out_wdev:
  138. wil_wdev_free(wil);
  139. return ERR_PTR(rc);
  140. }
  141. void wil_if_free(struct wil6210_priv *wil)
  142. {
  143. struct net_device *ndev = wil_to_ndev(wil);
  144. wil_dbg_misc(wil, "if_free\n");
  145. if (!ndev)
  146. return;
  147. wil_priv_deinit(wil);
  148. wil_to_ndev(wil) = NULL;
  149. free_netdev(ndev);
  150. wil_wdev_free(wil);
  151. }
  152. int wil_if_add(struct wil6210_priv *wil)
  153. {
  154. struct wireless_dev *wdev = wil_to_wdev(wil);
  155. struct wiphy *wiphy = wdev->wiphy;
  156. struct net_device *ndev = wil_to_ndev(wil);
  157. int rc;
  158. wil_dbg_misc(wil, "entered");
  159. strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
  160. rc = wiphy_register(wiphy);
  161. if (rc < 0) {
  162. wil_err(wil, "failed to register wiphy, err %d\n", rc);
  163. return rc;
  164. }
  165. netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
  166. WIL6210_NAPI_BUDGET);
  167. netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
  168. WIL6210_NAPI_BUDGET);
  169. wil_update_net_queues_bh(wil, NULL, true);
  170. rc = register_netdev(ndev);
  171. if (rc < 0) {
  172. dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
  173. goto out_wiphy;
  174. }
  175. return 0;
  176. out_wiphy:
  177. wiphy_unregister(wdev->wiphy);
  178. return rc;
  179. }
  180. void wil_if_remove(struct wil6210_priv *wil)
  181. {
  182. struct net_device *ndev = wil_to_ndev(wil);
  183. struct wireless_dev *wdev = wil_to_wdev(wil);
  184. wil_dbg_misc(wil, "if_remove\n");
  185. unregister_netdev(ndev);
  186. wiphy_unregister(wdev->wiphy);
  187. }