netdev.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. return wil_ioctl(wil, ifr->ifr_data, cmd);
  50. }
  51. static const struct net_device_ops wil_netdev_ops = {
  52. .ndo_open = wil_open,
  53. .ndo_stop = wil_stop,
  54. .ndo_start_xmit = wil_start_xmit,
  55. .ndo_set_mac_address = eth_mac_addr,
  56. .ndo_validate_addr = eth_validate_addr,
  57. .ndo_change_mtu = wil_change_mtu,
  58. .ndo_do_ioctl = wil_do_ioctl,
  59. };
  60. static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
  61. {
  62. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  63. napi_rx);
  64. int quota = budget;
  65. int done;
  66. wil_rx_handle(wil, &quota);
  67. done = budget - quota;
  68. if (done < budget) {
  69. napi_complete(napi);
  70. wil6210_unmask_irq_rx(wil);
  71. wil_dbg_txrx(wil, "NAPI RX complete\n");
  72. }
  73. wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
  74. return done;
  75. }
  76. static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
  77. {
  78. struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
  79. napi_tx);
  80. int tx_done = 0;
  81. uint i;
  82. /* always process ALL Tx complete, regardless budget - it is fast */
  83. for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
  84. struct vring *vring = &wil->vring_tx[i];
  85. struct vring_tx_data *txdata = &wil->vring_tx_data[i];
  86. if (!vring->va || !txdata->enabled)
  87. continue;
  88. tx_done += wil_tx_complete(wil, i);
  89. }
  90. if (tx_done < budget) {
  91. napi_complete(napi);
  92. wil6210_unmask_irq_tx(wil);
  93. wil_dbg_txrx(wil, "NAPI TX complete\n");
  94. }
  95. wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
  96. return min(tx_done, budget);
  97. }
  98. static void wil_dev_setup(struct net_device *dev)
  99. {
  100. ether_setup(dev);
  101. dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
  102. }
  103. void *wil_if_alloc(struct device *dev)
  104. {
  105. struct net_device *ndev;
  106. struct wireless_dev *wdev;
  107. struct wil6210_priv *wil;
  108. struct ieee80211_channel *ch;
  109. int rc = 0;
  110. wdev = wil_cfg80211_init(dev);
  111. if (IS_ERR(wdev)) {
  112. dev_err(dev, "wil_cfg80211_init failed\n");
  113. return wdev;
  114. }
  115. wil = wdev_to_wil(wdev);
  116. wil->wdev = wdev;
  117. wil->radio_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[NL80211_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. 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 wireless_dev *wdev = wil_to_wdev(wil);
  165. struct wiphy *wiphy = wdev->wiphy;
  166. struct net_device *ndev = wil_to_ndev(wil);
  167. int rc;
  168. wil_dbg_misc(wil, "entered");
  169. strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
  170. rc = wiphy_register(wiphy);
  171. if (rc < 0) {
  172. wil_err(wil, "failed to register wiphy, err %d\n", rc);
  173. return rc;
  174. }
  175. netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
  176. WIL6210_NAPI_BUDGET);
  177. netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
  178. WIL6210_NAPI_BUDGET);
  179. netif_tx_stop_all_queues(ndev);
  180. rc = register_netdev(ndev);
  181. if (rc < 0) {
  182. dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
  183. goto out_wiphy;
  184. }
  185. return 0;
  186. out_wiphy:
  187. wiphy_unregister(wdev->wiphy);
  188. return rc;
  189. }
  190. void wil_if_remove(struct wil6210_priv *wil)
  191. {
  192. struct net_device *ndev = wil_to_ndev(wil);
  193. struct wireless_dev *wdev = wil_to_wdev(wil);
  194. wil_dbg_misc(wil, "%s()\n", __func__);
  195. unregister_netdev(ndev);
  196. wiphy_unregister(wdev->wiphy);
  197. }