OSDN Git Service

BACKPORT: cfg80211: remove enum ieee80211_band
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / net / wireless / ath / wil6210 / netdev.c
1 /*
2  * Copyright (c) 2012-2017 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
17 #include <linux/moduleparam.h>
18 #include <linux/etherdevice.h>
19 #include "wil6210.h"
20 #include "txrx.h"
21
22 static bool alt_ifname; /* = false; */
23 module_param(alt_ifname, bool, S_IRUGO);
24 MODULE_PARM_DESC(alt_ifname, " use an alternate interface name wigigN instead of wlanN");
25
26 static int wil_open(struct net_device *ndev)
27 {
28         struct wil6210_priv *wil = ndev_to_wil(ndev);
29
30         wil_dbg_misc(wil, "open\n");
31
32         if (debug_fw ||
33             test_bit(WMI_FW_CAPABILITY_WMI_ONLY, wil->fw_capabilities)) {
34                 wil_err(wil, "while in debug_fw or wmi_only mode\n");
35                 return -EINVAL;
36         }
37
38         return wil_up(wil);
39 }
40
41 static int wil_stop(struct net_device *ndev)
42 {
43         struct wil6210_priv *wil = ndev_to_wil(ndev);
44
45         wil_dbg_misc(wil, "stop\n");
46
47         return wil_down(wil);
48 }
49
50 static int wil_change_mtu(struct net_device *ndev, int new_mtu)
51 {
52         struct wil6210_priv *wil = ndev_to_wil(ndev);
53
54         if (new_mtu < 68 || new_mtu > mtu_max) {
55                 wil_err(wil, "invalid MTU %d\n", new_mtu);
56                 return -EINVAL;
57         }
58
59         wil_dbg_misc(wil, "change MTU %d -> %d\n", ndev->mtu, new_mtu);
60         ndev->mtu = new_mtu;
61
62         return 0;
63 }
64
65 static int wil_do_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
66 {
67         struct wil6210_priv *wil = ndev_to_wil(ndev);
68
69         return wil_ioctl(wil, ifr->ifr_data, cmd);
70 }
71
72 static const struct net_device_ops wil_netdev_ops = {
73         .ndo_open               = wil_open,
74         .ndo_stop               = wil_stop,
75         .ndo_start_xmit         = wil_start_xmit,
76         .ndo_set_mac_address    = eth_mac_addr,
77         .ndo_validate_addr      = eth_validate_addr,
78         .ndo_change_mtu         = wil_change_mtu,
79         .ndo_do_ioctl           = wil_do_ioctl,
80 };
81
82 static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
83 {
84         struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
85                                                 napi_rx);
86         int quota = budget;
87         int done;
88
89         wil_rx_handle(wil, &quota);
90         done = budget - quota;
91
92         if (done < budget) {
93                 napi_complete_done(napi, done);
94                 wil6210_unmask_irq_rx(wil);
95                 wil_dbg_txrx(wil, "NAPI RX complete\n");
96         }
97
98         wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
99
100         return done;
101 }
102
103 static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
104 {
105         struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
106                                                 napi_tx);
107         int tx_done = 0;
108         uint i;
109
110         /* always process ALL Tx complete, regardless budget - it is fast */
111         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
112                 struct vring *vring = &wil->vring_tx[i];
113                 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
114
115                 if (!vring->va || !txdata->enabled)
116                         continue;
117
118                 tx_done += wil_tx_complete(wil, i);
119         }
120
121         if (tx_done < budget) {
122                 napi_complete(napi);
123                 wil6210_unmask_irq_tx(wil);
124                 wil_dbg_txrx(wil, "NAPI TX complete\n");
125         }
126
127         wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
128
129         return min(tx_done, budget);
130 }
131
132 static void wil_dev_setup(struct net_device *dev)
133 {
134         ether_setup(dev);
135         dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
136 }
137
138 void *wil_if_alloc(struct device *dev)
139 {
140         struct net_device *ndev;
141         struct wireless_dev *wdev;
142         struct wil6210_priv *wil;
143         struct ieee80211_channel *ch;
144         int rc = 0;
145         const char *ifname = alt_ifname ? "wigig%d" : "wlan%d";
146
147         wdev = wil_cfg80211_init(dev);
148         if (IS_ERR(wdev)) {
149                 dev_err(dev, "wil_cfg80211_init failed\n");
150                 return wdev;
151         }
152
153         wil = wdev_to_wil(wdev);
154         wil->wdev = wdev;
155         wil->radio_wdev = wdev;
156
157         wil_dbg_misc(wil, "if_alloc\n");
158
159         rc = wil_priv_init(wil);
160         if (rc) {
161                 dev_err(dev, "wil_priv_init failed\n");
162                 goto out_wdev;
163         }
164
165         wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
166         /* default monitor channel */
167         ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels;
168         cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
169
170         ndev = alloc_netdev(0, ifname, NET_NAME_UNKNOWN, wil_dev_setup);
171         if (!ndev) {
172                 dev_err(dev, "alloc_netdev_mqs failed\n");
173                 rc = -ENOMEM;
174                 goto out_priv;
175         }
176
177         ndev->netdev_ops = &wil_netdev_ops;
178         wil_set_ethtoolops(ndev);
179         ndev->ieee80211_ptr = wdev;
180         ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
181                             NETIF_F_SG | NETIF_F_GRO |
182                             NETIF_F_TSO | NETIF_F_TSO6 |
183                             NETIF_F_RXHASH;
184
185         ndev->features |= ndev->hw_features;
186         SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
187         wdev->netdev = ndev;
188
189         return wil;
190
191  out_priv:
192         wil_priv_deinit(wil);
193
194  out_wdev:
195         wil_wdev_free(wil);
196
197         return ERR_PTR(rc);
198 }
199
200 void wil_if_free(struct wil6210_priv *wil)
201 {
202         struct net_device *ndev = wil_to_ndev(wil);
203
204         wil_dbg_misc(wil, "if_free\n");
205
206         if (!ndev)
207                 return;
208
209         wil_priv_deinit(wil);
210
211         wil_to_ndev(wil) = NULL;
212         free_netdev(ndev);
213
214         wil_wdev_free(wil);
215 }
216
217 int wil_if_add(struct wil6210_priv *wil)
218 {
219         struct wireless_dev *wdev = wil_to_wdev(wil);
220         struct wiphy *wiphy = wdev->wiphy;
221         struct net_device *ndev = wil_to_ndev(wil);
222         int rc;
223
224         wil_dbg_misc(wil, "entered");
225
226         strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
227
228         rc = wiphy_register(wiphy);
229         if (rc < 0) {
230                 wil_err(wil, "failed to register wiphy, err %d\n", rc);
231                 return rc;
232         }
233
234         netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
235                        WIL6210_NAPI_BUDGET);
236         netif_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
237                        WIL6210_NAPI_BUDGET);
238
239         wil_update_net_queues_bh(wil, NULL, true);
240
241         rc = register_netdev(ndev);
242         if (rc < 0) {
243                 dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
244                 goto out_wiphy;
245         }
246
247         return 0;
248
249 out_wiphy:
250         wiphy_unregister(wdev->wiphy);
251         return rc;
252 }
253
254 void wil_if_remove(struct wil6210_priv *wil)
255 {
256         struct net_device *ndev = wil_to_ndev(wil);
257         struct wireless_dev *wdev = wil_to_wdev(wil);
258
259         wil_dbg_misc(wil, "if_remove\n");
260
261         unregister_netdev(ndev);
262         wiphy_unregister(wdev->wiphy);
263 }