netdev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2012-2015 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. if (!vring->va)
  88. continue;
  89. tx_done += wil_tx_complete(wil, i);
  90. }
  91. if (tx_done < budget) {
  92. napi_complete(napi);
  93. wil6210_unmask_irq_tx(wil);
  94. wil_dbg_txrx(wil, "NAPI TX complete\n");
  95. }
  96. wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
  97. return min(tx_done, budget);
  98. }
  99. static void wil_dev_setup(struct net_device *dev)
  100. {
  101. ether_setup(dev);
  102. dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
  103. }
  104. void *wil_if_alloc(struct device *dev)
  105. {
  106. struct net_device *ndev;
  107. struct wireless_dev *wdev;
  108. struct wil6210_priv *wil;
  109. struct ieee80211_channel *ch;
  110. int rc = 0;
  111. wdev = wil_cfg80211_init(dev);
  112. if (IS_ERR(wdev)) {
  113. dev_err(dev, "wil_cfg80211_init failed\n");
  114. return wdev;
  115. }
  116. wil = wdev_to_wil(wdev);
  117. wil->wdev = wdev;
  118. wil_dbg_misc(wil, "%s()\n", __func__);
  119. rc = wil_priv_init(wil);
  120. if (rc) {
  121. dev_err(dev, "wil_priv_init failed\n");
  122. goto out_wdev;
  123. }
  124. wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
  125. /* default monitor channel */
  126. ch = wdev->wiphy->bands[IEEE80211_BAND_60GHZ]->channels;
  127. cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
  128. ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup);
  129. if (!ndev) {
  130. dev_err(dev, "alloc_netdev_mqs failed\n");
  131. rc = -ENOMEM;
  132. goto out_priv;
  133. }
  134. ndev->netdev_ops = &wil_netdev_ops;
  135. wil_set_ethtoolops(ndev);
  136. ndev->ieee80211_ptr = wdev;
  137. ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
  138. NETIF_F_SG | NETIF_F_GRO |
  139. NETIF_F_TSO | NETIF_F_TSO6 |
  140. NETIF_F_RXHASH;
  141. ndev->features |= ndev->hw_features;
  142. SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
  143. wdev->netdev = ndev;
  144. netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
  145. WIL6210_NAPI_BUDGET);
  146. netif_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
  147. WIL6210_NAPI_BUDGET);
  148. netif_tx_stop_all_queues(ndev);
  149. return wil;
  150. out_priv:
  151. wil_priv_deinit(wil);
  152. out_wdev:
  153. wil_wdev_free(wil);
  154. return ERR_PTR(rc);
  155. }
  156. void wil_if_free(struct wil6210_priv *wil)
  157. {
  158. struct net_device *ndev = wil_to_ndev(wil);
  159. wil_dbg_misc(wil, "%s()\n", __func__);
  160. if (!ndev)
  161. return;
  162. wil_priv_deinit(wil);
  163. wil_to_ndev(wil) = NULL;
  164. free_netdev(ndev);
  165. wil_wdev_free(wil);
  166. }
  167. int wil_if_add(struct wil6210_priv *wil)
  168. {
  169. struct net_device *ndev = wil_to_ndev(wil);
  170. int rc;
  171. wil_dbg_misc(wil, "%s()\n", __func__);
  172. rc = register_netdev(ndev);
  173. if (rc < 0) {
  174. dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
  175. return rc;
  176. }
  177. return 0;
  178. }
  179. void wil_if_remove(struct wil6210_priv *wil)
  180. {
  181. struct net_device *ndev = wil_to_ndev(wil);
  182. wil_dbg_misc(wil, "%s()\n", __func__);
  183. unregister_netdev(ndev);
  184. }