blob: e3b3c8fb4605502a8c7cf80bbf12b1473a69388c [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
Vladimir Shulmanf1871cd2015-01-25 10:52:45 +02002 * Copyright (c) 2012-2015 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#include "wil6210.h"
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +030019#include "txrx.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080020
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 Kondratievbfc2dc72015-03-30 11:28:50 +030027 if (debug_fw) {
28 wil_err(wil, "%s() while in debug_fw mode\n", __func__);
29 return -EINVAL;
30 }
31
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080032 return wil_up(wil);
33}
34
35static int wil_stop(struct net_device *ndev)
36{
37 struct wil6210_priv *wil = ndev_to_wil(ndev);
38
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +030039 wil_dbg_misc(wil, "%s()\n", __func__);
40
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080041 return wil_down(wil);
42}
43
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030044static int wil_change_mtu(struct net_device *ndev, int new_mtu)
45{
46 struct wil6210_priv *wil = ndev_to_wil(ndev);
47
Vladimir Kondratiev9a06bec2014-10-28 16:51:27 +020048 if (new_mtu < 68 || new_mtu > mtu_max) {
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +030049 wil_err(wil, "invalid MTU %d\n", new_mtu);
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030050 return -EINVAL;
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +030051 }
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030052
53 wil_dbg_misc(wil, "change MTU %d -> %d\n", ndev->mtu, new_mtu);
54 ndev->mtu = new_mtu;
55
56 return 0;
57}
58
Vladimir Kondratievdba4b742014-10-01 15:05:25 +030059static int wil_do_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
60{
61 struct wil6210_priv *wil = ndev_to_wil(ndev);
62
63 int ret = wil_ioctl(wil, ifr->ifr_data, cmd);
64
65 wil_dbg_misc(wil, "ioctl(0x%04x) -> %d\n", cmd, ret);
66
67 return ret;
68}
69
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080070static const struct net_device_ops wil_netdev_ops = {
71 .ndo_open = wil_open,
72 .ndo_stop = wil_stop,
73 .ndo_start_xmit = wil_start_xmit,
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +020074 .ndo_set_mac_address = eth_mac_addr,
75 .ndo_validate_addr = eth_validate_addr,
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030076 .ndo_change_mtu = wil_change_mtu,
Vladimir Kondratievdba4b742014-10-01 15:05:25 +030077 .ndo_do_ioctl = wil_do_ioctl,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080078};
79
Vladimir Kondratieve0287c42013-05-12 14:43:36 +030080static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
81{
82 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
83 napi_rx);
84 int quota = budget;
85 int done;
86
87 wil_rx_handle(wil, &quota);
88 done = budget - quota;
89
Vladimir Kondratiev7308a202015-03-08 15:42:01 +020090 if (done < budget) {
Vladimir Kondratieve0287c42013-05-12 14:43:36 +030091 napi_complete(napi);
92 wil6210_unmask_irq_rx(wil);
93 wil_dbg_txrx(wil, "NAPI RX complete\n");
94 }
95
96 wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
97
98 return done;
99}
100
101static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
102{
103 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
104 napi_tx);
105 int tx_done = 0;
106 uint i;
107
108 /* always process ALL Tx complete, regardless budget - it is fast */
109 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
110 struct vring *vring = &wil->vring_tx[i];
111
112 if (!vring->va)
113 continue;
114
115 tx_done += wil_tx_complete(wil, i);
116 }
117
Vladimir Kondratiev7308a202015-03-08 15:42:01 +0200118 if (tx_done < budget) {
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300119 napi_complete(napi);
120 wil6210_unmask_irq_tx(wil);
121 wil_dbg_txrx(wil, "NAPI TX complete\n");
122 }
123
124 wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
125
126 return min(tx_done, budget);
127}
128
Vladimir Shulmanf1871cd2015-01-25 10:52:45 +0200129static void wil_dev_setup(struct net_device *dev)
130{
131 ether_setup(dev);
132 dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
133}
134
Vladimir Kondratiev3e2d8e12015-06-09 14:11:19 +0300135void *wil_if_alloc(struct device *dev)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800136{
137 struct net_device *ndev;
138 struct wireless_dev *wdev;
139 struct wil6210_priv *wil;
140 struct ieee80211_channel *ch;
141 int rc = 0;
142
143 wdev = wil_cfg80211_init(dev);
144 if (IS_ERR(wdev)) {
145 dev_err(dev, "wil_cfg80211_init failed\n");
146 return wdev;
147 }
148
149 wil = wdev_to_wil(wdev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800150 wil->wdev = wdev;
151
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300152 wil_dbg_misc(wil, "%s()\n", __func__);
153
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800154 rc = wil_priv_init(wil);
155 if (rc) {
156 dev_err(dev, "wil_priv_init failed\n");
157 goto out_wdev;
158 }
159
160 wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
161 /* default monitor channel */
162 ch = wdev->wiphy->bands[IEEE80211_BAND_60GHZ]->channels;
163 cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
164
Vladimir Shulmanf1871cd2015-01-25 10:52:45 +0200165 ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800166 if (!ndev) {
167 dev_err(dev, "alloc_netdev_mqs failed\n");
168 rc = -ENOMEM;
169 goto out_priv;
170 }
171
172 ndev->netdev_ops = &wil_netdev_ops;
Vladimir Kondratievb6b1b0e2014-09-22 15:31:41 +0300173 wil_set_ethtoolops(ndev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800174 ndev->ieee80211_ptr = wdev;
Vladimir Kondratievc2366582014-03-17 15:34:08 +0200175 ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +0300176 NETIF_F_SG | NETIF_F_GRO |
Vladimir Shulman05536402015-07-30 13:52:04 +0300177 NETIF_F_TSO | NETIF_F_TSO6 |
178 NETIF_F_RXHASH;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +0300179
Vladimir Kondratievc2366582014-03-17 15:34:08 +0200180 ndev->features |= ndev->hw_features;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800181 SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
182 wdev->netdev = ndev;
183
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300184 netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
185 WIL6210_NAPI_BUDGET);
186 netif_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
187 WIL6210_NAPI_BUDGET);
188
Dedy Lanskyc5e96c92015-01-25 10:52:43 +0200189 netif_tx_stop_all_queues(ndev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800190
191 return wil;
192
193 out_priv:
194 wil_priv_deinit(wil);
195
196 out_wdev:
197 wil_wdev_free(wil);
198
199 return ERR_PTR(rc);
200}
201
202void wil_if_free(struct wil6210_priv *wil)
203{
204 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev8fcfdea2014-08-06 10:31:59 +0300205
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300206 wil_dbg_misc(wil, "%s()\n", __func__);
207
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800208 if (!ndev)
209 return;
210
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800211 wil_priv_deinit(wil);
Vladimir Kondratiev8fcfdea2014-08-06 10:31:59 +0300212
213 wil_to_ndev(wil) = NULL;
214 free_netdev(ndev);
215
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800216 wil_wdev_free(wil);
217}
218
219int wil_if_add(struct wil6210_priv *wil)
220{
221 struct net_device *ndev = wil_to_ndev(wil);
222 int rc;
223
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300224 wil_dbg_misc(wil, "%s()\n", __func__);
225
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800226 rc = register_netdev(ndev);
227 if (rc < 0) {
228 dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
229 return rc;
230 }
231
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800232 return 0;
233}
234
235void wil_if_remove(struct wil6210_priv *wil)
236{
237 struct net_device *ndev = wil_to_ndev(wil);
238
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300239 wil_dbg_misc(wil, "%s()\n", __func__);
240
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800241 unregister_netdev(ndev);
242}