netdev.c 5.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2012-2016 Qualcomm Atheros, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/etherdevice.h>
#include "wil6210.h"
19
#include "txrx.h"
20 21 22 23 24

static int wil_open(struct net_device *ndev)
{
	struct wil6210_priv *wil = ndev_to_wil(ndev);

25 26
	wil_dbg_misc(wil, "%s()\n", __func__);

V
Vladimir Kondratiev 已提交
27 28 29 30 31
	if (debug_fw) {
		wil_err(wil, "%s() while in debug_fw mode\n", __func__);
		return -EINVAL;
	}

32 33 34 35 36 37 38
	return wil_up(wil);
}

static int wil_stop(struct net_device *ndev)
{
	struct wil6210_priv *wil = ndev_to_wil(ndev);

39 40
	wil_dbg_misc(wil, "%s()\n", __func__);

41 42 43
	return wil_down(wil);
}

44 45 46 47
static int wil_do_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
{
	struct wil6210_priv *wil = ndev_to_wil(ndev);

L
Lior David 已提交
48
	return wil_ioctl(wil, ifr->ifr_data, cmd);
49 50
}

51 52 53 54
static const struct net_device_ops wil_netdev_ops = {
	.ndo_open		= wil_open,
	.ndo_stop		= wil_stop,
	.ndo_start_xmit		= wil_start_xmit,
55 56
	.ndo_set_mac_address	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
57
	.ndo_do_ioctl		= wil_do_ioctl,
58 59
};

V
Vladimir Kondratiev 已提交
60 61 62 63 64 65 66 67 68 69
static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
{
	struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
						napi_rx);
	int quota = budget;
	int done;

	wil_rx_handle(wil, &quota);
	done = budget - quota;

70
	if (done < budget) {
V
Vladimir Kondratiev 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		napi_complete(napi);
		wil6210_unmask_irq_rx(wil);
		wil_dbg_txrx(wil, "NAPI RX complete\n");
	}

	wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);

	return done;
}

static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
{
	struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
						napi_tx);
	int tx_done = 0;
	uint i;

	/* always process ALL Tx complete, regardless budget - it is fast */
	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
		struct vring *vring = &wil->vring_tx[i];
91
		struct vring_tx_data *txdata = &wil->vring_tx_data[i];
V
Vladimir Kondratiev 已提交
92

93
		if (!vring->va || !txdata->enabled)
V
Vladimir Kondratiev 已提交
94 95 96 97 98
			continue;

		tx_done += wil_tx_complete(wil, i);
	}

99
	if (tx_done < budget) {
V
Vladimir Kondratiev 已提交
100 101 102 103 104 105 106 107 108 109
		napi_complete(napi);
		wil6210_unmask_irq_tx(wil);
		wil_dbg_txrx(wil, "NAPI TX complete\n");
	}

	wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);

	return min(tx_done, budget);
}

110 111 112
static void wil_dev_setup(struct net_device *dev)
{
	ether_setup(dev);
113
	dev->max_mtu = mtu_max;
114 115 116
	dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
}

117
void *wil_if_alloc(struct device *dev)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
	struct net_device *ndev;
	struct wireless_dev *wdev;
	struct wil6210_priv *wil;
	struct ieee80211_channel *ch;
	int rc = 0;

	wdev = wil_cfg80211_init(dev);
	if (IS_ERR(wdev)) {
		dev_err(dev, "wil_cfg80211_init failed\n");
		return wdev;
	}

	wil = wdev_to_wil(wdev);
	wil->wdev = wdev;
133
	wil->radio_wdev = wdev;
134

135 136
	wil_dbg_misc(wil, "%s()\n", __func__);

137 138 139 140 141 142 143 144
	rc = wil_priv_init(wil);
	if (rc) {
		dev_err(dev, "wil_priv_init failed\n");
		goto out_wdev;
	}

	wdev->iftype = NL80211_IFTYPE_STATION; /* TODO */
	/* default monitor channel */
145
	ch = wdev->wiphy->bands[NL80211_BAND_60GHZ]->channels;
146 147
	cfg80211_chandef_create(&wdev->preset_chandef, ch, NL80211_CHAN_NO_HT);

148
	ndev = alloc_netdev(0, "wlan%d", NET_NAME_UNKNOWN, wil_dev_setup);
149 150 151 152 153 154 155
	if (!ndev) {
		dev_err(dev, "alloc_netdev_mqs failed\n");
		rc = -ENOMEM;
		goto out_priv;
	}

	ndev->netdev_ops = &wil_netdev_ops;
V
Vladimir Kondratiev 已提交
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 161
			    NETIF_F_TSO | NETIF_F_TSO6 |
			    NETIF_F_RXHASH;
162

163
	ndev->features |= ndev->hw_features;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
	wdev->netdev = ndev;

	return wil;

 out_priv:
	wil_priv_deinit(wil);

 out_wdev:
	wil_wdev_free(wil);

	return ERR_PTR(rc);
}

void wil_if_free(struct wil6210_priv *wil)
{
	struct net_device *ndev = wil_to_ndev(wil);
181

182 183
	wil_dbg_misc(wil, "%s()\n", __func__);

184 185 186 187
	if (!ndev)
		return;

	wil_priv_deinit(wil);
188 189 190 191

	wil_to_ndev(wil) = NULL;
	free_netdev(ndev);

192 193 194 195 196
	wil_wdev_free(wil);
}

int wil_if_add(struct wil6210_priv *wil)
{
197 198
	struct wireless_dev *wdev = wil_to_wdev(wil);
	struct wiphy *wiphy = wdev->wiphy;
199 200 201
	struct net_device *ndev = wil_to_ndev(wil);
	int rc;

202 203
	wil_dbg_misc(wil, "entered");

204 205
	strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));

206 207 208 209 210 211 212 213 214 215 216
	rc = wiphy_register(wiphy);
	if (rc < 0) {
		wil_err(wil, "failed to register wiphy, err %d\n", rc);
		return rc;
	}

	netif_napi_add(ndev, &wil->napi_rx, wil6210_netdev_poll_rx,
		       WIL6210_NAPI_BUDGET);
	netif_tx_napi_add(ndev, &wil->napi_tx, wil6210_netdev_poll_tx,
			  WIL6210_NAPI_BUDGET);

D
Dedy Lansky 已提交
217
	wil_update_net_queues_bh(wil, NULL, true);
218

219 220 221
	rc = register_netdev(ndev);
	if (rc < 0) {
		dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
222
		goto out_wiphy;
223 224 225
	}

	return 0;
226 227 228 229

out_wiphy:
	wiphy_unregister(wdev->wiphy);
	return rc;
230 231 232 233 234
}

void wil_if_remove(struct wil6210_priv *wil)
{
	struct net_device *ndev = wil_to_ndev(wil);
235
	struct wireless_dev *wdev = wil_to_wdev(wil);
236

237 238
	wil_dbg_misc(wil, "%s()\n", __func__);

239
	unregister_netdev(ndev);
240
	wiphy_unregister(wdev->wiphy);
241
}