summaryrefslogtreecommitdiff
path: root/drivers/net/netvsc/hn_vf.c
blob: f1be7e22f0723955211ad950346d0d11fa694ea7 (plain)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2018 Microsoft Corp.
 * All rights reserved.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>

#include <rte_ether.h>
#include <rte_ethdev.h>
#include <rte_ethdev_driver.h>
#include <rte_lcore.h>
#include <rte_memory.h>
#include <rte_bus_vmbus.h>
#include <rte_pci.h>
#include <rte_bus_pci.h>
#include <rte_log.h>
#include <rte_string_fns.h>

#include "hn_logs.h"
#include "hn_var.h"
#include "hn_nvs.h"

/* Search for VF with matching MAC address, return port id */
static int hn_vf_match(const struct rte_eth_dev *dev)
{
	const struct rte_ether_addr *mac = dev->data->mac_addrs;
	int i;

	RTE_ETH_FOREACH_DEV(i) {
		const struct rte_eth_dev *vf_dev = &rte_eth_devices[i];
		const struct rte_ether_addr *vf_mac = vf_dev->data->mac_addrs;

		if (vf_dev == dev)
			continue;

		if (is_same_ether_addr(mac, vf_mac))
			return i;
	}
	return -ENOENT;
}


/*
 * Attach new PCI VF device and return the port_id
 */
static int hn_vf_attach(struct hn_data *hv, uint16_t port_id)
{
	struct rte_eth_dev_owner owner = { .id = RTE_ETH_DEV_NO_OWNER };
	int ret;

	if (hn_vf_attached(hv)) {
		PMD_DRV_LOG(ERR, "VF already attached");
		return -EEXIST;
	}

	ret = rte_eth_dev_owner_get(port_id, &owner);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Can not find owner for port %d", port_id);
		return ret;
	}

	if (owner.id != RTE_ETH_DEV_NO_OWNER) {
		PMD_DRV_LOG(ERR, "Port %u already owned by other device %s",
			    port_id, owner.name);
		return -EBUSY;
	}

	ret = rte_eth_dev_owner_set(port_id, &hv->owner);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Can set owner for port %d", port_id);
		return ret;
	}

	PMD_DRV_LOG(DEBUG, "Attach VF device %u", port_id);
	hv->vf_port = port_id;
	rte_smp_wmb();

	return 0;
}

/* Add new VF device to synthetic device */
int hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv)
{
	int port, err;

	port = hn_vf_match(dev);
	if (port < 0) {
		PMD_DRV_LOG(NOTICE, "No matching MAC found");
		return port;
	}

	rte_spinlock_lock(&hv->vf_lock);
	err = hn_vf_attach(hv, port);

	if (err == 0) {
		dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
		hv->vf_intr = (struct rte_intr_handle) {
			.fd = -1,
			.type = RTE_INTR_HANDLE_EXT,
		};
		dev->intr_handle = &hv->vf_intr;
		hn_nvs_set_datapath(hv, NVS_DATAPATH_VF);
	}
	rte_spinlock_unlock(&hv->vf_lock);

	return err;
}

/* Remove new VF device */
static void hn_vf_remove(struct hn_data *hv)
{

	rte_spinlock_lock(&hv->vf_lock);

	if (!hn_vf_attached(hv)) {
		PMD_DRV_LOG(ERR, "VF path not active");
	} else {
		/* Stop incoming packets from arriving on VF */
		hn_nvs_set_datapath(hv, NVS_DATAPATH_SYNTHETIC);

		/* Stop transmission over VF */
		hv->vf_port = HN_INVALID_PORT;
		rte_smp_wmb();

		/* Give back ownership */
		rte_eth_dev_owner_unset(hv->vf_port, hv->owner.id);
	}
	rte_spinlock_unlock(&hv->vf_lock);
}

/* Handle VF association message from host */
void
hn_nvs_handle_vfassoc(struct rte_eth_dev *dev,
		      const struct vmbus_chanpkt_hdr *hdr,
		      const void *data)
{
	struct hn_data *hv = dev->data->dev_private;
	const struct hn_nvs_vf_association *vf_assoc = data;

	if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*vf_assoc))) {
		PMD_DRV_LOG(ERR, "invalid vf association NVS");
		return;
	}

	PMD_DRV_LOG(DEBUG, "VF serial %u %s port %u",
		    vf_assoc->serial,
		    vf_assoc->allocated ? "add to" : "remove from",
		    dev->data->port_id);

	hv->vf_present = vf_assoc->allocated;

	if (dev->state != RTE_ETH_DEV_ATTACHED)
		return;

	if (vf_assoc->allocated)
		hn_vf_add(dev, hv);
	else
		hn_vf_remove(hv);
}

/*
 * Merge the info from the VF and synthetic path.
 * use the default config of the VF
 * and the minimum number of queues and buffer sizes.
 */
static void hn_vf_info_merge(struct rte_eth_dev *vf_dev,
			     struct rte_eth_dev_info *info)
{
	struct rte_eth_dev_info vf_info;

	rte_eth_dev_info_get(vf_dev->data->port_id, &vf_info);

	info->speed_capa = vf_info.speed_capa;
	info->default_rxportconf = vf_info.default_rxportconf;
	info->default_txportconf = vf_info.default_txportconf;

	info->max_rx_queues = RTE_MIN(vf_info.max_rx_queues,
				      info->max_rx_queues);
	info->rx_offload_capa &= vf_info.rx_offload_capa;
	info->rx_queue_offload_capa &= vf_info.rx_queue_offload_capa;
	info->flow_type_rss_offloads &= vf_info.flow_type_rss_offloads;

	info->max_tx_queues = RTE_MIN(vf_info.max_tx_queues,
				      info->max_tx_queues);
	info->tx_offload_capa &= vf_info.tx_offload_capa;
	info->tx_queue_offload_capa &= vf_info.tx_queue_offload_capa;

	info->min_rx_bufsize = RTE_MAX(vf_info.min_rx_bufsize,
				       info->min_rx_bufsize);
	info->max_rx_pktlen  = RTE_MAX(vf_info.max_rx_pktlen,
				       info->max_rx_pktlen);
}

void hn_vf_info_get(struct hn_data *hv, struct rte_eth_dev_info *info)
{
	struct rte_eth_dev *vf_dev;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		hn_vf_info_merge(vf_dev, info);
	rte_spinlock_unlock(&hv->vf_lock);
}

int hn_vf_link_update(struct rte_eth_dev *dev,
		      int wait_to_complete)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->link_update)
		ret = (*vf_dev->dev_ops->link_update)(vf_dev, wait_to_complete);
	rte_spinlock_unlock(&hv->vf_lock);

	return ret;
}

/* called when VF has link state interrupts enabled */
static int hn_vf_lsc_event(uint16_t port_id __rte_unused,
			   enum rte_eth_event_type event,
			   void *cb_arg, void *out __rte_unused)
{
	struct rte_eth_dev *dev = cb_arg;

	if (event != RTE_ETH_EVENT_INTR_LSC)
		return 0;

	/* if link state has changed pass on */
	if (hn_dev_link_update(dev, 0) == 0)
		return 0; /* no change */

	return _rte_eth_dev_callback_process(dev,
					     RTE_ETH_EVENT_INTR_LSC,
					     NULL);
}

static int _hn_vf_configure(struct rte_eth_dev *dev,
			    uint16_t vf_port,
			    const struct rte_eth_conf *dev_conf)
{
	struct rte_eth_conf vf_conf = *dev_conf;
	struct rte_eth_dev *vf_dev;
	int ret;

	vf_dev = &rte_eth_devices[vf_port];
	if (dev_conf->intr_conf.lsc &&
	    (vf_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
		PMD_DRV_LOG(DEBUG, "enabling LSC for VF %u",
			    vf_port);
		vf_conf.intr_conf.lsc = 1;
	} else {
		PMD_DRV_LOG(DEBUG, "disabling LSC for VF %u",
			    vf_port);
		vf_conf.intr_conf.lsc = 0;
	}

	ret = rte_eth_dev_configure(vf_port,
				    dev->data->nb_rx_queues,
				    dev->data->nb_tx_queues,
				    &vf_conf);
	if (ret) {
		PMD_DRV_LOG(ERR,
			    "VF configuration failed: %d", ret);
	} else if (vf_conf.intr_conf.lsc) {
		ret = rte_eth_dev_callback_register(vf_port,
						    RTE_ETH_DEV_INTR_LSC,
						    hn_vf_lsc_event, dev);
		if (ret)
			PMD_DRV_LOG(ERR,
				    "Failed to register LSC callback for VF %u",
				    vf_port);
	}
	return ret;
}

/*
 * Configure VF if present.
 * Force VF to have same number of queues as synthetic device
 */
int hn_vf_configure(struct rte_eth_dev *dev,
		    const struct rte_eth_conf *dev_conf)
{
	struct hn_data *hv = dev->data->dev_private;
	int ret = 0;

	rte_spinlock_lock(&hv->vf_lock);
	if (hv->vf_port != HN_INVALID_PORT)
		ret = _hn_vf_configure(dev, hv->vf_port, dev_conf);
	rte_spinlock_unlock(&hv->vf_lock);
	return ret;
}

const uint32_t *hn_vf_supported_ptypes(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	const uint32_t *ptypes = NULL;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->dev_supported_ptypes_get)
		ptypes = (*vf_dev->dev_ops->dev_supported_ptypes_get)(vf_dev);
	rte_spinlock_unlock(&hv->vf_lock);

	return ptypes;
}

int hn_vf_start(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_dev_start(vf_dev->data->port_id);
	rte_spinlock_unlock(&hv->vf_lock);
	return ret;
}

void hn_vf_stop(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		rte_eth_dev_stop(vf_dev->data->port_id);
	rte_spinlock_unlock(&hv->vf_lock);
}

/* If VF is present, then cascade configuration down */
#define VF_ETHDEV_FUNC(dev, func)				\
	{							\
		struct hn_data *hv = (dev)->data->dev_private;	\
		struct rte_eth_dev *vf_dev;			\
		rte_spinlock_lock(&hv->vf_lock);		\
		vf_dev = hn_get_vf_dev(hv);			\
		if (vf_dev)					\
			func(vf_dev->data->port_id);		\
		rte_spinlock_unlock(&hv->vf_lock);		\
	}

void hn_vf_reset(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC(dev, rte_eth_dev_reset);
}

void hn_vf_close(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	uint16_t vf_port;

	rte_spinlock_lock(&hv->vf_lock);
	vf_port = hv->vf_port;
	if (vf_port != HN_INVALID_PORT)
		rte_eth_dev_close(vf_port);

	hv->vf_port = HN_INVALID_PORT;
	rte_spinlock_unlock(&hv->vf_lock);
}

void hn_vf_stats_reset(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC(dev, rte_eth_stats_reset);
}

void hn_vf_allmulticast_enable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC(dev, rte_eth_allmulticast_enable);
}

void hn_vf_allmulticast_disable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC(dev, rte_eth_allmulticast_disable);
}

void hn_vf_promiscuous_enable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC(dev, rte_eth_promiscuous_enable);
}

void hn_vf_promiscuous_disable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC(dev, rte_eth_promiscuous_disable);
}

int hn_vf_mc_addr_list(struct rte_eth_dev *dev,
			struct rte_ether_addr *mc_addr_set,
			uint32_t nb_mc_addr)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_dev_set_mc_addr_list(vf_dev->data->port_id,
						   mc_addr_set, nb_mc_addr);
	rte_spinlock_unlock(&hv->vf_lock);
	return ret;
}

int hn_vf_tx_queue_setup(struct rte_eth_dev *dev,
			 uint16_t queue_idx, uint16_t nb_desc,
			 unsigned int socket_id,
			 const struct rte_eth_txconf *tx_conf)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_tx_queue_setup(vf_dev->data->port_id,
					     queue_idx, nb_desc,
					     socket_id, tx_conf);
	rte_spinlock_unlock(&hv->vf_lock);
	return ret;
}

void hn_vf_tx_queue_release(struct hn_data *hv, uint16_t queue_id)
{
	struct rte_eth_dev *vf_dev;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->tx_queue_release) {
		void *subq = vf_dev->data->tx_queues[queue_id];

		(*vf_dev->dev_ops->tx_queue_release)(subq);
	}

	rte_spinlock_unlock(&hv->vf_lock);
}

int hn_vf_rx_queue_setup(struct rte_eth_dev *dev,
			 uint16_t queue_idx, uint16_t nb_desc,
			 unsigned int socket_id,
			 const struct rte_eth_rxconf *rx_conf,
			 struct rte_mempool *mp)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_rx_queue_setup(vf_dev->data->port_id,
					     queue_idx, nb_desc,
					     socket_id, rx_conf, mp);
	rte_spinlock_unlock(&hv->vf_lock);
	return ret;
}

void hn_vf_rx_queue_release(struct hn_data *hv, uint16_t queue_id)
{
	struct rte_eth_dev *vf_dev;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->rx_queue_release) {
		void *subq = vf_dev->data->rx_queues[queue_id];

		(*vf_dev->dev_ops->rx_queue_release)(subq);
	}
	rte_spinlock_unlock(&hv->vf_lock);
}

int hn_vf_stats_get(struct rte_eth_dev *dev,
		    struct rte_eth_stats *stats)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_stats_get(vf_dev->data->port_id, stats);
	rte_spinlock_unlock(&hv->vf_lock);
	return ret;
}

int hn_vf_xstats_get_names(struct rte_eth_dev *dev,
			   struct rte_eth_xstat_name *names,
			   unsigned int n)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int i, count = 0;
	char tmp[RTE_ETH_XSTATS_NAME_SIZE];

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->xstats_get_names)
		count = vf_dev->dev_ops->xstats_get_names(vf_dev, names, n);
	rte_spinlock_unlock(&hv->vf_lock);

	/* add vf_ prefix to xstat names */
	if (names) {
		for (i = 0; i < count; i++) {
			snprintf(tmp, sizeof(tmp), "vf_%s", names[i].name);
			strlcpy(names[i].name, tmp, sizeof(names[i].name));
		}
	}

	return count;
}

int hn_vf_xstats_get(struct rte_eth_dev *dev,
		     struct rte_eth_xstat *xstats,
		     unsigned int n)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int count = 0;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->xstats_get)
		count = vf_dev->dev_ops->xstats_get(vf_dev, xstats, n);
	rte_spinlock_unlock(&hv->vf_lock);

	return count;
}

void hn_vf_xstats_reset(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;

	rte_spinlock_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->xstats_reset)
		vf_dev->dev_ops->xstats_reset(vf_dev);
	rte_spinlock_unlock(&hv->vf_lock);
}