Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / wireless / ath / wil6210 / netdev.c
blob7ba4e0af8f5725dd6cb7436e603baef4865dcf25
1 /*
2 * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
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.
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.
17 #include <linux/etherdevice.h>
18 #include "wil6210.h"
19 #include "txrx.h"
21 static int wil_open(struct net_device *ndev)
23 struct wil6210_priv *wil = ndev_to_wil(ndev);
24 int rc;
26 wil_dbg_misc(wil, "open\n");
28 if (debug_fw ||
29 test_bit(WMI_FW_CAPABILITY_WMI_ONLY, wil->fw_capabilities)) {
30 wil_err(wil, "while in debug_fw or wmi_only mode\n");
31 return -EINVAL;
34 rc = wil_pm_runtime_get(wil);
35 if (rc < 0)
36 return rc;
38 rc = wil_up(wil);
39 if (rc)
40 wil_pm_runtime_put(wil);
42 return rc;
45 static int wil_stop(struct net_device *ndev)
47 struct wil6210_priv *wil = ndev_to_wil(ndev);
48 int rc;
50 wil_dbg_misc(wil, "stop\n");
52 rc = wil_down(wil);
53 if (!rc)
54 wil_pm_runtime_put(wil);
56 return rc;
59 static const struct net_device_ops wil_netdev_ops = {
60 .ndo_open = wil_open,
61 .ndo_stop = wil_stop,
62 .ndo_start_xmit = wil_start_xmit,
63 .ndo_set_mac_address = eth_mac_addr,
64 .ndo_validate_addr = eth_validate_addr,
67 static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
69 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
70 napi_rx);
71 int quota = budget;
72 int done;
74 wil_rx_handle(wil, &quota);
75 done = budget - quota;
77 if (done < budget) {
78 napi_complete_done(napi, done);
79 wil6210_unmask_irq_rx(wil);
80 wil_dbg_txrx(wil, "NAPI RX complete\n");
83 wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
85 return done;
88 static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
90 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
91 napi_tx);
92 int tx_done = 0;
93 uint i;
95 /* always process ALL Tx complete, regardless budget - it is fast */
96 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
97 struct vring *vring = &wil->vring_tx[i];
98 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
100 if (!vring->va || !txdata->enabled)
101 continue;
103 tx_done += wil_tx_complete(wil, i);
106 if (tx_done < budget) {
107 napi_complete(napi);
108 wil6210_unmask_irq_tx(wil);
109 wil_dbg_txrx(wil, "NAPI TX complete\n");
112 wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
114 return min(tx_done, budget);
117 static void wil_dev_setup(struct net_device *dev)
119 ether_setup(dev);
120 dev->max_mtu = mtu_max;
121 dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
124 void *wil_if_alloc(struct device *dev)
126 struct net_device *ndev;
127 struct wireless_dev *wdev;
128 struct wil6210_priv *wil;
129 struct ieee80211_channel *ch;
130 int rc = 0;
132 wdev = wil_cfg80211_init(dev);
133 if (IS_ERR(wdev)) {
134 dev_err(dev, "wil_cfg80211_init failed\n");
135 return wdev;
138 wil = wdev_to_wil(wdev);
139 wil->wdev = wdev;
140 wil->radio_wdev = wdev;
142 wil_dbg_misc(wil, "if_alloc\n");
144 rc = wil_priv_init(wil);
145 if (rc) {
146 dev_err(dev, "wil_priv_init failed\n");
147 goto out_wdev;
150 wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
151 /* default monitor channel */
152 ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels;
153 cfg80211_chandef_create(&wil->monitor_chandef, ch, NL80211_CHAN_NO_HT);
155 ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup);
156 if (!ndev) {
157 dev_err(dev, "alloc_netdev_mqs failed\n");
158 rc = -ENOMEM;
159 goto out_priv;
162 ndev->netdev_ops = &wil_netdev_ops;
163 wil_set_ethtoolops(ndev);
164 ndev->ieee80211_ptr = wdev;
165 ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
166 NETIF_F_SG | NETIF_F_GRO |
167 NETIF_F_TSO | NETIF_F_TSO6 |
168 NETIF_F_RXHASH;
170 ndev->features |= ndev->hw_features;
171 SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
172 wdev->netdev = ndev;
174 return wil;
176 out_priv:
177 wil_priv_deinit(wil);
179 out_wdev:
180 wil_wdev_free(wil);
182 return ERR_PTR(rc);
185 void wil_if_free(struct wil6210_priv *wil)
187 struct net_device *ndev = wil_to_ndev(wil);
189 wil_dbg_misc(wil, "if_free\n");
191 if (!ndev)
192 return;
194 wil_priv_deinit(wil);
196 wil_to_ndev(wil) = NULL;
197 free_netdev(ndev);
199 wil_wdev_free(wil);
202 int wil_if_add(struct wil6210_priv *wil)
204 struct wireless_dev *wdev = wil_to_wdev(wil);
205 struct wiphy *wiphy = wdev->wiphy;
206 struct net_device *ndev = wil_to_ndev(wil);
207 int rc;
209 wil_dbg_misc(wil, "entered");
211 strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
213 rc = wiphy_register(wiphy);
214 if (rc < 0) {
215 wil_err(wil, "failed to register wiphy, err %d\n", rc);
216 return rc;
219 netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
220 WIL6210_NAPI_BUDGET);
221 netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
222 WIL6210_NAPI_BUDGET);
224 wil_update_net_queues_bh(wil, NULL, true);
226 rc = register_netdev(ndev);
227 if (rc < 0) {
228 dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
229 goto out_wiphy;
232 return 0;
234 out_wiphy:
235 wiphy_unregister(wdev->wiphy);
236 return rc;
239 void wil_if_remove(struct wil6210_priv *wil)
241 struct net_device *ndev = wil_to_ndev(wil);
242 struct wireless_dev *wdev = wil_to_wdev(wil);
244 wil_dbg_misc(wil, "if_remove\n");
246 unregister_netdev(ndev);
247 wiphy_unregister(wdev->wiphy);