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