blob: c3f0ddfaa5da507461975d5708f832136f5b29b7 [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"
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +030020#include "txrx.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080021
22static int wil_open(struct net_device *ndev)
23{
24 struct wil6210_priv *wil = ndev_to_wil(ndev);
25
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +030026 wil_dbg_misc(wil, "%s()\n", __func__);
27
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080028 return wil_up(wil);
29}
30
31static int wil_stop(struct net_device *ndev)
32{
33 struct wil6210_priv *wil = ndev_to_wil(ndev);
34
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +030035 wil_dbg_misc(wil, "%s()\n", __func__);
36
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080037 return wil_down(wil);
38}
39
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030040static int wil_change_mtu(struct net_device *ndev, int new_mtu)
41{
42 struct wil6210_priv *wil = ndev_to_wil(ndev);
43
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +030044 if (new_mtu < 68 || new_mtu > (TX_BUF_LEN - ETH_HLEN)) {
45 wil_err(wil, "invalid MTU %d\n", new_mtu);
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030046 return -EINVAL;
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +030047 }
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030048
49 wil_dbg_misc(wil, "change MTU %d -> %d\n", ndev->mtu, new_mtu);
50 ndev->mtu = new_mtu;
51
52 return 0;
53}
54
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080055static const struct net_device_ops wil_netdev_ops = {
56 .ndo_open = wil_open,
57 .ndo_stop = wil_stop,
58 .ndo_start_xmit = wil_start_xmit,
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +020059 .ndo_set_mac_address = eth_mac_addr,
60 .ndo_validate_addr = eth_validate_addr,
Vladimir Kondratievd87bac12014-05-27 14:45:44 +030061 .ndo_change_mtu = wil_change_mtu,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080062};
63
Vladimir Kondratieve0287c42013-05-12 14:43:36 +030064static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
65{
66 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
67 napi_rx);
68 int quota = budget;
69 int done;
70
71 wil_rx_handle(wil, &quota);
72 done = budget - quota;
73
74 if (done <= 1) { /* burst ends - only one packet processed */
75 napi_complete(napi);
76 wil6210_unmask_irq_rx(wil);
77 wil_dbg_txrx(wil, "NAPI RX complete\n");
78 }
79
80 wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
81
82 return done;
83}
84
85static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
86{
87 struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
88 napi_tx);
89 int tx_done = 0;
90 uint i;
91
92 /* always process ALL Tx complete, regardless budget - it is fast */
93 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
94 struct vring *vring = &wil->vring_tx[i];
95
96 if (!vring->va)
97 continue;
98
99 tx_done += wil_tx_complete(wil, i);
100 }
101
102 if (tx_done <= 1) { /* burst ends - only one packet processed */
103 napi_complete(napi);
104 wil6210_unmask_irq_tx(wil);
105 wil_dbg_txrx(wil, "NAPI TX complete\n");
106 }
107
108 wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
109
110 return min(tx_done, budget);
111}
112
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800113void *wil_if_alloc(struct device *dev, void __iomem *csr)
114{
115 struct net_device *ndev;
116 struct wireless_dev *wdev;
117 struct wil6210_priv *wil;
118 struct ieee80211_channel *ch;
119 int rc = 0;
120
121 wdev = wil_cfg80211_init(dev);
122 if (IS_ERR(wdev)) {
123 dev_err(dev, "wil_cfg80211_init failed\n");
124 return wdev;
125 }
126
127 wil = wdev_to_wil(wdev);
128 wil->csr = csr;
129 wil->wdev = wdev;
130
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300131 wil_dbg_misc(wil, "%s()\n", __func__);
132
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800133 rc = wil_priv_init(wil);
134 if (rc) {
135 dev_err(dev, "wil_priv_init failed\n");
136 goto out_wdev;
137 }
138
139 wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
140 /* default monitor channel */
141 ch = wdev->wiphy->bands[IEEE80211_BAND_60GHZ]->channels;
142 cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
143
Tom Gundersenc835a672014-07-14 16:37:24 +0200144 ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, ether_setup);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800145 if (!ndev) {
146 dev_err(dev, "alloc_netdev_mqs failed\n");
147 rc = -ENOMEM;
148 goto out_priv;
149 }
150
151 ndev->netdev_ops = &wil_netdev_ops;
Vladimir Kondratievb6b1b0e2014-09-22 15:31:41 +0300152 wil_set_ethtoolops(ndev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800153 ndev->ieee80211_ptr = wdev;
Vladimir Kondratievc2366582014-03-17 15:34:08 +0200154 ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
Vladimir Kondratievb5998e62014-03-17 15:34:22 +0200155 NETIF_F_SG | NETIF_F_GRO;
Vladimir Kondratievc2366582014-03-17 15:34:08 +0200156 ndev->features |= ndev->hw_features;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800157 SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
158 wdev->netdev = ndev;
159
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300160 netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
161 WIL6210_NAPI_BUDGET);
162 netif_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
163 WIL6210_NAPI_BUDGET);
164
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800165 wil_link_off(wil);
166
167 return wil;
168
169 out_priv:
170 wil_priv_deinit(wil);
171
172 out_wdev:
173 wil_wdev_free(wil);
174
175 return ERR_PTR(rc);
176}
177
178void wil_if_free(struct wil6210_priv *wil)
179{
180 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev8fcfdea2014-08-06 10:31:59 +0300181
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300182 wil_dbg_misc(wil, "%s()\n", __func__);
183
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800184 if (!ndev)
185 return;
186
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800187 wil_priv_deinit(wil);
Vladimir Kondratiev8fcfdea2014-08-06 10:31:59 +0300188
189 wil_to_ndev(wil) = NULL;
190 free_netdev(ndev);
191
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800192 wil_wdev_free(wil);
193}
194
195int wil_if_add(struct wil6210_priv *wil)
196{
197 struct net_device *ndev = wil_to_ndev(wil);
198 int rc;
199
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300200 wil_dbg_misc(wil, "%s()\n", __func__);
201
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800202 rc = register_netdev(ndev);
203 if (rc < 0) {
204 dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
205 return rc;
206 }
207
208 wil_link_off(wil);
209
210 return 0;
211}
212
213void wil_if_remove(struct wil6210_priv *wil)
214{
215 struct net_device *ndev = wil_to_ndev(wil);
216
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300217 wil_dbg_misc(wil, "%s()\n", __func__);
218
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800219 unregister_netdev(ndev);
220}