netdev.c 5.5 KB

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