netdev.c 5.3 KB

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