netdev.c 5.5 KB

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