blob: a995d9d59cb58743fb369c3b6674552cc6c41d91 [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
Vladimir Kondratiev02525a72014-08-06 10:31:51 +03002 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08003 *
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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#include <linux/etherdevice.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080018
19#include "wil6210.h"
20
21static int wil_open(struct net_device *ndev)
22{
23 struct wil6210_priv *wil = ndev_to_wil(ndev);
24
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +030025 wil_dbg_misc(wil, "%s()\n", __func__);
26
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080027 return wil_up(wil);
28}
29
30static int wil_stop(struct net_device *ndev)
31{
32 struct wil6210_priv *wil = ndev_to_wil(ndev);
33
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +030034 wil_dbg_misc(wil, "%s()\n", __func__);
35
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080036 return wil_down(wil);
37}
38
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030039static 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 > IEEE80211_MAX_DATA_LEN_DMG)
44 return -EINVAL;
45
46 wil_dbg_misc(wil, "change MTU %d -> %d\n", ndev->mtu, new_mtu);
47 ndev->mtu = new_mtu;
48
49 return 0;
50}
51
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080052static const struct net_device_ops wil_netdev_ops = {
53 .ndo_open = wil_open,
54 .ndo_stop = wil_stop,
55 .ndo_start_xmit = wil_start_xmit,
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +020056 .ndo_set_mac_address = eth_mac_addr,
57 .ndo_validate_addr = eth_validate_addr,
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030058 .ndo_change_mtu = wil_change_mtu,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080059};
60
Vladimir Kondratieve0287c42013-05-12 14:43:36 +030061static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
62{
63 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
64 napi_rx);
65 int quota = budget;
66 int done;
67
68 wil_rx_handle(wil, &quota);
69 done = budget - quota;
70
71 if (done <= 1) { /* burst ends - only one packet processed */
72 napi_complete(napi);
73 wil6210_unmask_irq_rx(wil);
74 wil_dbg_txrx(wil, "NAPI RX complete\n");
75 }
76
77 wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
78
79 return done;
80}
81
82static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
83{
84 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
85 napi_tx);
86 int tx_done = 0;
87 uint i;
88
89 /* always process ALL Tx complete, regardless budget - it is fast */
90 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
91 struct vring *vring = &wil->vring_tx[i];
92
93 if (!vring->va)
94 continue;
95
96 tx_done += wil_tx_complete(wil, i);
97 }
98
99 if (tx_done <= 1) { /* burst ends - only one packet processed */
100 napi_complete(napi);
101 wil6210_unmask_irq_tx(wil);
102 wil_dbg_txrx(wil, "NAPI TX complete\n");
103 }
104
105 wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
106
107 return min(tx_done, budget);
108}
109
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800110void *wil_if_alloc(struct device *dev, void __iomem *csr)
111{
112 struct net_device *ndev;
113 struct wireless_dev *wdev;
114 struct wil6210_priv *wil;
115 struct ieee80211_channel *ch;
116 int rc = 0;
117
118 wdev = wil_cfg80211_init(dev);
119 if (IS_ERR(wdev)) {
120 dev_err(dev, "wil_cfg80211_init failed\n");
121 return wdev;
122 }
123
124 wil = wdev_to_wil(wdev);
125 wil->csr = csr;
126 wil->wdev = wdev;
127
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300128 wil_dbg_misc(wil, "%s()\n", __func__);
129
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800130 rc = wil_priv_init(wil);
131 if (rc) {
132 dev_err(dev, "wil_priv_init failed\n");
133 goto out_wdev;
134 }
135
136 wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
137 /* default monitor channel */
138 ch = wdev->wiphy->bands[IEEE80211_BAND_60GHZ]->channels;
139 cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
140
Tom Gundersenc835a672014-07-14 16:37:24 +0200141 ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, ether_setup);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800142 if (!ndev) {
143 dev_err(dev, "alloc_netdev_mqs failed\n");
144 rc = -ENOMEM;
145 goto out_priv;
146 }
147
148 ndev->netdev_ops = &wil_netdev_ops;
149 ndev->ieee80211_ptr = wdev;
Vladimir Kondratievc2366582014-03-17 15:34:08 +0200150 ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
Vladimir Kondratievb5998e62014-03-17 15:34:22 +0200151 NETIF_F_SG | NETIF_F_GRO;
Vladimir Kondratievc2366582014-03-17 15:34:08 +0200152 ndev->features |= ndev->hw_features;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800153 SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
154 wdev->netdev = ndev;
155
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300156 netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
157 WIL6210_NAPI_BUDGET);
158 netif_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
159 WIL6210_NAPI_BUDGET);
160
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800161 wil_link_off(wil);
162
163 return wil;
164
165 out_priv:
166 wil_priv_deinit(wil);
167
168 out_wdev:
169 wil_wdev_free(wil);
170
171 return ERR_PTR(rc);
172}
173
174void wil_if_free(struct wil6210_priv *wil)
175{
176 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev8fcfdea2014-08-06 10:31:59 +0300177
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300178 wil_dbg_misc(wil, "%s()\n", __func__);
179
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800180 if (!ndev)
181 return;
182
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800183 wil_priv_deinit(wil);
Vladimir Kondratiev8fcfdea2014-08-06 10:31:59 +0300184
185 wil_to_ndev(wil) = NULL;
186 free_netdev(ndev);
187
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800188 wil_wdev_free(wil);
189}
190
191int wil_if_add(struct wil6210_priv *wil)
192{
193 struct net_device *ndev = wil_to_ndev(wil);
194 int rc;
195
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300196 wil_dbg_misc(wil, "%s()\n", __func__);
197
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800198 rc = register_netdev(ndev);
199 if (rc < 0) {
200 dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
201 return rc;
202 }
203
204 wil_link_off(wil);
205
206 return 0;
207}
208
209void wil_if_remove(struct wil6210_priv *wil)
210{
211 struct net_device *ndev = wil_to_ndev(wil);
212
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300213 wil_dbg_misc(wil, "%s()\n", __func__);
214
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800215 unregister_netdev(ndev);
216}