summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/meta/mpnic/mpnic_netdev.c
blob: 7fdf57f70151a49a1ae9d558ec27900f82cff43f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) Meta Platforms, Inc. and affiliates. */

#include <linux/etherdevice.h>
#include <linux/ipv6.h>
#include <linux/netdevice.h>
#include <linux/pci.h>
#include <linux/types.h>
#include <net/netdev_lock.h>

#include "mpnic.h"
#include "mpnic_netdev.h"
#include "mpnic_txrx.h"

static int mpnic_open(struct net_device *netdev)
{
	struct mpnic_net *mpn = netdev_priv(netdev);
	int err;

	err = mpnic_alloc_napi_vectors(mpn);
	if (err)
		return err;

	err = mpnic_alloc_resources(mpn);
	if (err)
		goto err_free_napi_vectors;

	err = mpnic_set_netif_queues(mpn);
	if (err)
		goto err_free_resources;

	mpnic_stats_attach_rings(mpn);

	mpnic_enable(mpn);
	mpnic_fill(mpn);
	mpnic_napi_enable(mpn);

	netif_tx_wake_all_queues(netdev);
	netif_carrier_on(netdev);

	schedule_delayed_work(&mpn->mpd->service_task, HZ);

	return 0;

err_free_resources:
	mpnic_free_resources(mpn);
err_free_napi_vectors:
	mpnic_free_napi_vectors(mpn);
	return err;
}

static int mpnic_stop(struct net_device *netdev)
{
	struct mpnic_net *mpn = netdev_priv(netdev);

	cancel_delayed_work(&mpn->mpd->service_task);
	netif_carrier_off(netdev);

	mpnic_napi_disable(mpn);
	netif_tx_disable(netdev);

	mpnic_disable(mpn);
	mpnic_wait_all_queues_idle(mpn->mpd);
	mpnic_flush(mpn);

	mpnic_stats_fold_rings(mpn);

	mpnic_reset_netif_queues(mpn);
	mpnic_free_resources(mpn);
	mpnic_free_napi_vectors(mpn);

	return 0;
}

static void
mpnic_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
{
	struct mpnic_net *mpn = netdev_priv(dev);
	u64 bytes, packets, dropped, errors;
	struct mpnic_queue_stats *stats;
	struct mpnic_base_stats *base;
	unsigned int start, i;

	rcu_read_lock();

	base = rcu_dereference(mpn->stats.base);

	stats64->tx_bytes = base->tx.bytes;
	stats64->tx_packets = base->tx.packets;
	stats64->tx_dropped = base->tx.dropped;

	stats64->rx_bytes = base->rx.bytes;
	stats64->rx_packets = base->rx.packets;
	stats64->rx_dropped = base->rx.dropped;
	stats64->rx_errors = base->rx.errors;

	for (i = 0; i < base->num_tx_queues; i++) {
		struct mpnic_ring *txr = base->txr[i];
		struct mpnic_q_triad *qt;

		qt = container_of(txr, struct mpnic_q_triad, sub0);

		stats = &qt->cmpl.stats;
		do {
			start = u64_stats_fetch_begin(&stats->syncp);
			bytes = u64_stats_read(&stats->tcq.bytes);
			packets = u64_stats_read(&stats->tcq.packets);
		} while (u64_stats_fetch_retry(&stats->syncp, start));

		stats = &txr->stats;
		do {
			start = u64_stats_fetch_begin(&stats->syncp);
			dropped = u64_stats_read(&stats->twq.dropped);
		} while (u64_stats_fetch_retry(&stats->syncp, start));

		stats64->tx_bytes += bytes;
		stats64->tx_packets += packets;
		stats64->tx_dropped += dropped;
	}

	for (i = 0; i < base->num_rx_queues; i++) {
		struct mpnic_ring *rxr = base->rxr[i];

		stats = &rxr->stats;
		do {
			start = u64_stats_fetch_begin(&stats->syncp);
			bytes = u64_stats_read(&stats->rcq.bytes);
			packets = u64_stats_read(&stats->rcq.packets);
			dropped = u64_stats_read(&stats->rcq.dropped);
			errors = u64_stats_read(&stats->rcq.errors);
		} while (u64_stats_fetch_retry(&stats->syncp, start));

		stats64->rx_bytes += bytes;
		stats64->rx_packets += packets;
		stats64->rx_dropped += dropped;
		stats64->rx_errors += errors;
	}

	rcu_read_unlock();
}

static const struct net_device_ops mpnic_netdev_ops = {
	.ndo_open		= mpnic_open,
	.ndo_stop		= mpnic_stop,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_start_xmit		= mpnic_xmit_frame,
	.ndo_get_stats64	= mpnic_get_stats64,
};

static u64 mpnic_bdq_alloc_failed(struct mpnic_ring *bdq)
{
	struct mpnic_queue_stats *stats = &bdq->stats;
	unsigned int start;
	u64 alloc_failed;

	do {
		start = u64_stats_fetch_begin(&stats->syncp);
		alloc_failed = u64_stats_read(&stats->bdq.alloc_failed);
	} while (u64_stats_fetch_retry(&stats->syncp, start));

	return alloc_failed;
}

static void mpnic_get_queue_stats_rx(struct net_device *dev, int idx,
				     struct netdev_queue_stats_rx *rx)
{
	struct mpnic_net *mpn = netdev_priv(dev);
	struct mpnic_ring *rxr = mpn->rx[idx];
	u64 bytes, packets, alloc_failed;
	struct mpnic_queue_stats *stats;
	struct mpnic_q_triad *qt;
	unsigned int start;

	if (!rxr)
		return;

	stats = &rxr->stats;
	do {
		start = u64_stats_fetch_begin(&stats->syncp);
		bytes = u64_stats_read(&stats->rcq.bytes);
		packets = u64_stats_read(&stats->rcq.packets);
		alloc_failed = u64_stats_read(&stats->rcq.alloc_failed);
	} while (u64_stats_fetch_retry(&stats->syncp, start));

	qt = container_of(rxr, struct mpnic_q_triad, cmpl);
	alloc_failed += mpnic_bdq_alloc_failed(&qt->sub0);
	alloc_failed += mpnic_bdq_alloc_failed(&qt->sub1);

	rx->bytes = bytes;
	rx->packets = packets;
	rx->alloc_fail = alloc_failed;
}

static void mpnic_get_queue_stats_tx(struct net_device *dev, int idx,
				     struct netdev_queue_stats_tx *tx)
{
	struct mpnic_net *mpn = netdev_priv(dev);
	struct mpnic_ring *txr = mpn->tx[idx];
	struct mpnic_queue_stats *stats;
	u64 bytes, packets, stop, wake;
	struct mpnic_q_triad *qt;
	unsigned int start;

	if (!txr)
		return;

	qt = container_of(txr, struct mpnic_q_triad, sub0);

	stats = &qt->cmpl.stats;
	do {
		start = u64_stats_fetch_begin(&stats->syncp);
		bytes = u64_stats_read(&stats->tcq.bytes);
		packets = u64_stats_read(&stats->tcq.packets);
		wake = u64_stats_read(&stats->tcq.wake);
	} while (u64_stats_fetch_retry(&stats->syncp, start));

	stats = &txr->stats;
	do {
		start = u64_stats_fetch_begin(&stats->syncp);
		stop = u64_stats_read(&stats->twq.stop);
	} while (u64_stats_fetch_retry(&stats->syncp, start));

	tx->bytes = bytes;
	tx->packets = packets;
	tx->stop = stop;
	tx->wake = wake;
}

static void mpnic_get_base_stats(struct net_device *dev,
				 struct netdev_queue_stats_rx *rx,
				 struct netdev_queue_stats_tx *tx)
{
	struct mpnic_net *mpn = netdev_priv(dev);
	struct mpnic_base_stats *base;

	base = netdev_lock_dereference(mpn->stats.base, dev);

	tx->bytes = base->tx.bytes;
	tx->packets = base->tx.packets;
	tx->stop = base->tx.stop;
	tx->wake = base->tx.wake;

	rx->bytes = base->rx.bytes;
	rx->packets = base->rx.packets;
	rx->alloc_fail = base->rx.alloc_failed;
}

static const struct netdev_stat_ops mpnic_stat_ops = {
	.get_queue_stats_rx	= mpnic_get_queue_stats_rx,
	.get_queue_stats_tx	= mpnic_get_queue_stats_tx,
	.get_base_stats		= mpnic_get_base_stats,
};

/**
 * mpnic_netdev_free - Free the netdev associated with mpnic
 * @mpd: Driver specific structure to free netdev from
 **/
void mpnic_netdev_free(struct mpnic_dev *mpd)
{
	free_netdev(mpd->netdev);
	mpd->netdev = NULL;
}

/**
 * mpnic_netdev_alloc - Allocate a netdev and associate it with mpnic
 * @mpd: Driver specific structure to associate the netdev with
 *
 * Return: NULL on failure.
 **/
struct net_device *mpnic_netdev_alloc(struct mpnic_dev *mpd)
{
	struct net_device *netdev;
	struct mpnic_net *mpn;
	unsigned int queues;

	netdev = alloc_etherdev_mq(sizeof(*mpn), MPNIC_MAX_RXQS);
	if (!netdev)
		return NULL;

	SET_NETDEV_DEV(netdev, mpd->dev);
	mpd->netdev = netdev;

	netdev->netdev_ops = &mpnic_netdev_ops;
	netdev->stat_ops = &mpnic_stat_ops;
	netdev->request_ops_lock = true;

	mpn = netdev_priv(netdev);
	mpn->netdev = netdev;
	mpn->mpd = mpd;

	mpn->stats.buf[0].txr = mpn->tx;
	mpn->stats.buf[0].rxr = mpn->rx;
	RCU_INIT_POINTER(mpn->stats.base, &mpn->stats.buf[0]);

	mpn->txq_size = MPNIC_TXQ_SIZE_DEFAULT;
	mpn->hpq_size = MPNIC_HPQ_SIZE_DEFAULT;
	mpn->ppq_size = MPNIC_PPQ_SIZE_DEFAULT;
	mpn->rcq_size = MPNIC_RCQ_SIZE_DEFAULT;

	queues = min(netif_get_num_default_rss_queues(),
		     mpd->num_irqs - MPNIC_NON_NAPI_VECTORS);
	mpn->num_tx_queues = queues;
	mpn->num_rx_queues = queues;
	mpn->num_napi = queues;

	netdev->features |= NETIF_F_SG;
	netdev->hw_features |= netdev->features;
	netdev->vlan_features |= netdev->features;

	netdev->min_mtu = IPV6_MIN_MTU;
	netdev->max_mtu = MPNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;

	netif_carrier_off(netdev);
	netif_tx_stop_all_queues(netdev);

	return netdev;
}

static int mpnic_dsn_to_mac_addr(u64 dsn, u8 *addr)
{
	addr[0] = (dsn >> 56) & 0xFF;
	addr[1] = (dsn >> 48) & 0xFF;
	addr[2] = (dsn >> 40) & 0xFF;
	addr[3] = (dsn >> 16) & 0xFF;
	addr[4] = (dsn >> 8) & 0xFF;
	addr[5] = dsn & 0xFF;

	return is_valid_ether_addr(addr) ? 0 : -EINVAL;
}

/**
 * mpnic_netdev_register - Assign the MAC address and register the netdev
 * @netdev: Netdev to register
 *
 * The permanent address is derived from the PCIe device serial number, the
 * same way the firmware and the BMC derive it. A random address would break
 * provisioning, so refuse to spawn the interface if the serial number does
 * not yield a valid one.
 *
 * Return: non-zero on failure.
 **/
int mpnic_netdev_register(struct net_device *netdev)
{
	struct mpnic_net *mpn = netdev_priv(netdev);
	struct mpnic_dev *mpd = mpn->mpd;
	u8 addr[ETH_ALEN];
	int err;

	err = mpnic_dsn_to_mac_addr(mpd->dsn, addr);
	if (err) {
		dev_err(mpd->dev, "MAC addr %pM invalid\n", addr);
		return err;
	}

	ether_addr_copy(netdev->perm_addr, addr);
	eth_hw_addr_set(netdev, addr);

	err = netif_set_real_num_queues(netdev, mpn->num_tx_queues,
					mpn->num_rx_queues);
	if (err)
		return err;

	/* Abort if MMIO has failed. This has to be the last check before
	 * registration, the register accessors can only detach the device
	 * once it has been registered.
	 */
	if (!mpnic_present(mpd))
		return -EIO;

	return register_netdev(netdev);
}