1 /*
2  * Copyright (c) 2012-2016 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 	if (debug_fw) {
28 		wil_err(wil, "%s() while in debug_fw mode\n", __func__);
29 		return -EINVAL;
30 	}
31 
32 	return wil_up(wil);
33 }
34 
35 static int wil_stop(struct net_device *ndev)
36 {
37 	struct wil6210_priv *wil = ndev_to_wil(ndev);
38 
39 	wil_dbg_misc(wil, "%s()\n", __func__);
40 
41 	return wil_down(wil);
42 }
43 
44 static int wil_do_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
45 {
46 	struct wil6210_priv *wil = ndev_to_wil(ndev);
47 
48 	return wil_ioctl(wil, ifr->ifr_data, cmd);
49 }
50 
51 static const struct net_device_ops wil_netdev_ops = {
52 	.ndo_open		= wil_open,
53 	.ndo_stop		= wil_stop,
54 	.ndo_start_xmit		= wil_start_xmit,
55 	.ndo_set_mac_address	= eth_mac_addr,
56 	.ndo_validate_addr	= eth_validate_addr,
57 	.ndo_do_ioctl		= wil_do_ioctl,
58 };
59 
60 static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
61 {
62 	struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
63 						napi_rx);
64 	int quota = budget;
65 	int done;
66 
67 	wil_rx_handle(wil, &quota);
68 	done = budget - quota;
69 
70 	if (done < budget) {
71 		napi_complete(napi);
72 		wil6210_unmask_irq_rx(wil);
73 		wil_dbg_txrx(wil, "NAPI RX complete\n");
74 	}
75 
76 	wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
77 
78 	return done;
79 }
80 
81 static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
82 {
83 	struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
84 						napi_tx);
85 	int tx_done = 0;
86 	uint i;
87 
88 	/* always process ALL Tx complete, regardless budget - it is fast */
89 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
90 		struct vring *vring = &wil->vring_tx[i];
91 		struct vring_tx_data *txdata = &wil->vring_tx_data[i];
92 
93 		if (!vring->va || !txdata->enabled)
94 			continue;
95 
96 		tx_done += wil_tx_complete(wil, i);
97 	}
98 
99 	if (tx_done < budget) {
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 
110 static void wil_dev_setup(struct net_device *dev)
111 {
112 	ether_setup(dev);
113 	dev->max_mtu = mtu_max;
114 	dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
115 }
116 
117 void *wil_if_alloc(struct device *dev)
118 {
119 	struct net_device *ndev;
120 	struct wireless_dev *wdev;
121 	struct wil6210_priv *wil;
122 	struct ieee80211_channel *ch;
123 	int rc = 0;
124 
125 	wdev = wil_cfg80211_init(dev);
126 	if (IS_ERR(wdev)) {
127 		dev_err(dev, "wil_cfg80211_init failed\n");
128 		return wdev;
129 	}
130 
131 	wil = wdev_to_wil(wdev);
132 	wil->wdev = wdev;
133 	wil->radio_wdev = wdev;
134 
135 	wil_dbg_misc(wil, "%s()\n", __func__);
136 
137 	rc = wil_priv_init(wil);
138 	if (rc) {
139 		dev_err(dev, "wil_priv_init failed\n");
140 		goto out_wdev;
141 	}
142 
143 	wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
144 	/* default monitor channel */
145 	ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels;
146 	cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);
147 
148 	ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup);
149 	if (!ndev) {
150 		dev_err(dev, "alloc_netdev_mqs failed\n");
151 		rc = -ENOMEM;
152 		goto out_priv;
153 	}
154 
155 	ndev->netdev_ops = &wil_netdev_ops;
156 	wil_set_ethtoolops(ndev);
157 	ndev->ieee80211_ptr = wdev;
158 	ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
159 			    NETIF_F_SG | NETIF_F_GRO |
160 			    NETIF_F_TSO | NETIF_F_TSO6 |
161 			    NETIF_F_RXHASH;
162 
163 	ndev->features |= ndev->hw_features;
164 	SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
165 	wdev->netdev = ndev;
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 
178 void wil_if_free(struct wil6210_priv *wil)
179 {
180 	struct net_device *ndev = wil_to_ndev(wil);
181 
182 	wil_dbg_misc(wil, "%s()\n", __func__);
183 
184 	if (!ndev)
185 		return;
186 
187 	wil_priv_deinit(wil);
188 
189 	wil_to_ndev(wil) = NULL;
190 	free_netdev(ndev);
191 
192 	wil_wdev_free(wil);
193 }
194 
195 int wil_if_add(struct wil6210_priv *wil)
196 {
197 	struct wireless_dev *wdev = wil_to_wdev(wil);
198 	struct wiphy *wiphy = wdev->wiphy;
199 	struct net_device *ndev = wil_to_ndev(wil);
200 	int rc;
201 
202 	wil_dbg_misc(wil, "entered");
203 
204 	strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
205 
206 	rc = wiphy_register(wiphy);
207 	if (rc < 0) {
208 		wil_err(wil, "failed to register wiphy, err %d\n", rc);
209 		return rc;
210 	}
211 
212 	netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
213 		       WIL6210_NAPI_BUDGET);
214 	netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
215 			  WIL6210_NAPI_BUDGET);
216 
217 	wil_update_net_queues_bh(wil, NULL, true);
218 
219 	rc = register_netdev(ndev);
220 	if (rc < 0) {
221 		dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
222 		goto out_wiphy;
223 	}
224 
225 	return 0;
226 
227 out_wiphy:
228 	wiphy_unregister(wdev->wiphy);
229 	return rc;
230 }
231 
232 void wil_if_remove(struct wil6210_priv *wil)
233 {
234 	struct net_device *ndev = wil_to_ndev(wil);
235 	struct wireless_dev *wdev = wil_to_wdev(wil);
236 
237 	wil_dbg_misc(wil, "%s()\n", __func__);
238 
239 	unregister_netdev(ndev);
240 	wiphy_unregister(wdev->wiphy);
241 }
242