Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2018 Microsoft Corporation
3 : : * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
4 : : * All rights reserved.
5 : : */
6 : :
7 : : #include <stdint.h>
8 : : #include <string.h>
9 : : #include <stdio.h>
10 : : #include <errno.h>
11 : : #include <unistd.h>
12 : : #include <dirent.h>
13 : : #include <net/if.h>
14 : : #include <net/if_arp.h>
15 : : #include <netinet/in.h>
16 : : #include <sys/ioctl.h>
17 : :
18 : : #include <rte_ethdev.h>
19 : : #include <rte_memcpy.h>
20 : : #include <rte_string_fns.h>
21 : : #include <rte_memzone.h>
22 : : #include <rte_devargs.h>
23 : : #include <rte_malloc.h>
24 : : #include <rte_kvargs.h>
25 : : #include <rte_atomic.h>
26 : : #include <rte_branch_prediction.h>
27 : : #include <rte_ether.h>
28 : : #include <ethdev_driver.h>
29 : : #include <rte_cycles.h>
30 : : #include <rte_errno.h>
31 : : #include <rte_memory.h>
32 : : #include <rte_eal.h>
33 : : #include <dev_driver.h>
34 : : #include <bus_driver.h>
35 : : #include <bus_vmbus_driver.h>
36 : : #include <rte_alarm.h>
37 : :
38 : : #include "hn_logs.h"
39 : : #include "hn_var.h"
40 : : #include "hn_rndis.h"
41 : : #include "hn_nvs.h"
42 : : #include "ndis.h"
43 : :
44 : : #ifndef LIST_FOREACH_SAFE
45 : : #define LIST_FOREACH_SAFE(var, head, field, tvar) \
46 : : for ((var) = LIST_FIRST((head)); \
47 : : (var) && ((tvar) = LIST_NEXT((var), field), 1); \
48 : : (var) = (tvar))
49 : : #endif
50 : :
51 : : /* Spinlock for netvsc_shared_data */
52 : : static rte_spinlock_t netvsc_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
53 : :
54 : : static struct netvsc_shared_data {
55 : : RTE_ATOMIC(uint32_t) secondary_cnt;
56 : : } *netvsc_shared_data;
57 : :
58 : : static const struct rte_memzone *netvsc_shared_mz;
59 : : #define MZ_NETVSC_SHARED_DATA "netvsc_shared_data"
60 : :
61 : : static struct netvsc_local_data {
62 : : bool init_done;
63 : : unsigned int primary_cnt;
64 : : unsigned int secondary_cnt;
65 : : } netvsc_local_data;
66 : :
67 : : #define NETVSC_MP_NAME "net_netvsc_mp"
68 : : #define NETVSC_MP_REQ_TIMEOUT_SEC 5
69 : :
70 : : struct netvsc_mp_param {
71 : : enum netvsc_mp_req_type type;
72 : : int vf_port;
73 : : int result;
74 : : };
75 : :
76 : : #define HN_TX_OFFLOAD_CAPS (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | \
77 : : RTE_ETH_TX_OFFLOAD_TCP_CKSUM | \
78 : : RTE_ETH_TX_OFFLOAD_UDP_CKSUM | \
79 : : RTE_ETH_TX_OFFLOAD_TCP_TSO | \
80 : : RTE_ETH_TX_OFFLOAD_MULTI_SEGS | \
81 : : RTE_ETH_TX_OFFLOAD_VLAN_INSERT)
82 : :
83 : : #define HN_RX_OFFLOAD_CAPS (RTE_ETH_RX_OFFLOAD_CHECKSUM | \
84 : : RTE_ETH_RX_OFFLOAD_VLAN_STRIP | \
85 : : RTE_ETH_RX_OFFLOAD_RSS_HASH)
86 : :
87 : : #define NETVSC_ARG_LATENCY "latency"
88 : : #define NETVSC_ARG_RXBREAK "rx_copybreak"
89 : : #define NETVSC_ARG_TXBREAK "tx_copybreak"
90 : : #define NETVSC_ARG_RX_EXTMBUF_ENABLE "rx_extmbuf_enable"
91 : :
92 : : /* Retry interval for hot-add VF device (microseconds) */
93 : : #define NETVSC_HOTADD_RETRY_INTERVAL 1000000
94 : :
95 : : /* Max retries when net/ directory exists but no matching MAC found.
96 : : * On multi-NIC PCI devices, a second VF may register later.
97 : : * 120 retries = ~2 minutes.
98 : : */
99 : : #define NETVSC_MAX_MAC_RETRY 120
100 : :
101 : : struct hn_xstats_name_off {
102 : : char name[RTE_ETH_XSTATS_NAME_SIZE];
103 : : unsigned int offset;
104 : : };
105 : :
106 : : static const struct hn_xstats_name_off hn_stat_strings[] = {
107 : : { "good_packets", offsetof(struct hn_stats, packets) },
108 : : { "good_bytes", offsetof(struct hn_stats, bytes) },
109 : : { "errors", offsetof(struct hn_stats, errors) },
110 : : { "ring full", offsetof(struct hn_stats, ring_full) },
111 : : { "channel full", offsetof(struct hn_stats, channel_full) },
112 : : { "multicast_packets", offsetof(struct hn_stats, multicast) },
113 : : { "broadcast_packets", offsetof(struct hn_stats, broadcast) },
114 : : { "undersize_packets", offsetof(struct hn_stats, size_bins[0]) },
115 : : { "size_64_packets", offsetof(struct hn_stats, size_bins[1]) },
116 : : { "size_65_127_packets", offsetof(struct hn_stats, size_bins[2]) },
117 : : { "size_128_255_packets", offsetof(struct hn_stats, size_bins[3]) },
118 : : { "size_256_511_packets", offsetof(struct hn_stats, size_bins[4]) },
119 : : { "size_512_1023_packets", offsetof(struct hn_stats, size_bins[5]) },
120 : : { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
121 : : { "size_1519_max_packets", offsetof(struct hn_stats, size_bins[7]) },
122 : : };
123 : :
124 : : /* The default RSS key.
125 : : * This value is the same as MLX5 so that flows will be
126 : : * received on same path for both VF and synthetic NIC.
127 : : */
128 : : static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = {
129 : : 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7,
130 : : 0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94,
131 : : 0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1,
132 : : 0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59,
133 : : 0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a,
134 : : };
135 : :
136 : : static rte_spinlock_t netvsc_lock = RTE_SPINLOCK_INITIALIZER;
137 : : struct da_cache {
138 : : LIST_ENTRY(da_cache) list;
139 : : char name[RTE_DEV_NAME_MAX_LEN];
140 : : char drv_str[];
141 : : };
142 : :
143 : : static LIST_HEAD(da_cache_list, da_cache) da_cache_list;
144 : : static unsigned int da_cache_usage;
145 : :
146 : : static struct rte_eth_dev *
147 : 0 : eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
148 : : {
149 : : struct rte_eth_dev *eth_dev;
150 : : const char *name;
151 : :
152 [ # # ]: 0 : if (!dev)
153 : : return NULL;
154 : :
155 : 0 : name = dev->device.name;
156 : :
157 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
158 : 0 : eth_dev = rte_eth_dev_allocate(name);
159 [ # # ]: 0 : if (!eth_dev) {
160 : 0 : PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
161 : 0 : return NULL;
162 : : }
163 : :
164 [ # # ]: 0 : if (private_data_size) {
165 : 0 : eth_dev->data->dev_private =
166 : 0 : rte_zmalloc_socket(name, private_data_size,
167 : : RTE_CACHE_LINE_SIZE, dev->device.numa_node);
168 [ # # ]: 0 : if (!eth_dev->data->dev_private) {
169 : 0 : PMD_DRV_LOG(NOTICE, "can not allocate driver data");
170 : 0 : rte_eth_dev_release_port(eth_dev);
171 : 0 : return NULL;
172 : : }
173 : : }
174 : : } else {
175 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
176 [ # # ]: 0 : if (!eth_dev) {
177 : 0 : PMD_DRV_LOG(NOTICE, "can not attach secondary");
178 : 0 : return NULL;
179 : : }
180 : : }
181 : :
182 : 0 : eth_dev->device = &dev->device;
183 : :
184 : : /* interrupt is simulated */
185 : 0 : rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_EXT);
186 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
187 : 0 : eth_dev->intr_handle = dev->intr_handle;
188 : :
189 : 0 : return eth_dev;
190 : : }
191 : :
192 : : static void
193 : : eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
194 : : {
195 : : /* free ether device */
196 : 0 : rte_eth_dev_release_port(eth_dev);
197 : :
198 : 0 : eth_dev->device = NULL;
199 : 0 : eth_dev->intr_handle = NULL;
200 : 0 : }
201 : :
202 : 0 : static int hn_set_parameter(const char *key, const char *value, void *opaque)
203 : : {
204 : : struct hn_data *hv = opaque;
205 : 0 : char *endp = NULL;
206 : : unsigned long v;
207 : :
208 : 0 : v = strtoul(value, &endp, 0);
209 [ # # # # ]: 0 : if (*value == '\0' || *endp != '\0') {
210 : 0 : PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
211 : 0 : return -EINVAL;
212 : : }
213 : :
214 [ # # ]: 0 : if (!strcmp(key, NETVSC_ARG_LATENCY)) {
215 : : /* usec to nsec */
216 : 0 : hv->latency = v * 1000;
217 : 0 : PMD_DRV_LOG(DEBUG, "set latency %u usec", hv->latency);
218 [ # # ]: 0 : } else if (!strcmp(key, NETVSC_ARG_RXBREAK)) {
219 : 0 : hv->rx_copybreak = v;
220 : 0 : PMD_DRV_LOG(DEBUG, "rx copy break set to %u",
221 : : hv->rx_copybreak);
222 [ # # ]: 0 : } else if (!strcmp(key, NETVSC_ARG_TXBREAK)) {
223 : 0 : hv->tx_copybreak = v;
224 : 0 : PMD_DRV_LOG(DEBUG, "tx copy break set to %u",
225 : : hv->tx_copybreak);
226 [ # # ]: 0 : } else if (!strcmp(key, NETVSC_ARG_RX_EXTMBUF_ENABLE)) {
227 : 0 : hv->rx_extmbuf_enable = v;
228 : 0 : PMD_DRV_LOG(DEBUG, "rx extmbuf enable set to %u",
229 : : hv->rx_extmbuf_enable);
230 : : }
231 : :
232 : : return 0;
233 : : }
234 : :
235 : : /* Parse device arguments */
236 : 0 : static int hn_parse_args(const struct rte_eth_dev *dev)
237 : : {
238 : 0 : struct hn_data *hv = dev->data->dev_private;
239 : 0 : struct rte_devargs *devargs = dev->device->devargs;
240 : : static const char * const valid_keys[] = {
241 : : NETVSC_ARG_LATENCY,
242 : : NETVSC_ARG_RXBREAK,
243 : : NETVSC_ARG_TXBREAK,
244 : : NETVSC_ARG_RX_EXTMBUF_ENABLE,
245 : : NETVSC_ARG_NUMA_AWARE,
246 : : NULL
247 : : };
248 : : struct rte_kvargs *kvlist;
249 : : int ret;
250 : :
251 [ # # ]: 0 : if (!devargs)
252 : : return 0;
253 : :
254 : 0 : PMD_INIT_LOG(DEBUG, "device args %s %s",
255 : : devargs->name, devargs->args);
256 : :
257 : 0 : kvlist = rte_kvargs_parse(devargs->args, valid_keys);
258 [ # # ]: 0 : if (!kvlist) {
259 : 0 : PMD_DRV_LOG(ERR, "invalid parameters");
260 : 0 : return -EINVAL;
261 : : }
262 : :
263 : 0 : ret = rte_kvargs_process(kvlist, NULL, hn_set_parameter, hv);
264 : 0 : rte_kvargs_free(kvlist);
265 : :
266 : 0 : return ret;
267 : : }
268 : :
269 : : /* Update link status.
270 : : * Note: the DPDK definition of "wait_to_complete"
271 : : * means block this call until link is up.
272 : : * which is not worth supporting.
273 : : */
274 : : int
275 : 0 : hn_dev_link_update(struct rte_eth_dev *dev,
276 : : int wait_to_complete __rte_unused)
277 : : {
278 : 0 : struct hn_data *hv = dev->data->dev_private;
279 : : struct rte_eth_link link, old;
280 : : int error;
281 : :
282 : 0 : old = dev->data->dev_link;
283 : :
284 : 0 : error = hn_rndis_get_linkstatus(hv);
285 [ # # ]: 0 : if (error)
286 : : return error;
287 : :
288 : 0 : hn_rndis_get_linkspeed(hv);
289 : :
290 : 0 : link = (struct rte_eth_link) {
291 : : .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
292 : : .link_autoneg = RTE_ETH_LINK_SPEED_FIXED,
293 : 0 : .link_speed = hv->link_speed / 10000,
294 : : };
295 : :
296 [ # # ]: 0 : if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
297 : 0 : link.link_status = RTE_ETH_LINK_UP;
298 : : else
299 : : link.link_status = RTE_ETH_LINK_DOWN;
300 : :
301 [ # # ]: 0 : if (old.link_status != link.link_status)
302 [ # # ]: 0 : PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
303 : : (link.link_status == RTE_ETH_LINK_UP) ? "up" : "down");
304 : :
305 : : return rte_eth_linkstatus_set(dev, &link);
306 : : }
307 : :
308 : 0 : static int hn_dev_info_get(struct rte_eth_dev *dev,
309 : : struct rte_eth_dev_info *dev_info)
310 : : {
311 : 0 : struct hn_data *hv = dev->data->dev_private;
312 : : int rc;
313 : :
314 : 0 : dev_info->speed_capa = RTE_ETH_LINK_SPEED_10G;
315 : 0 : dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
316 : 0 : dev_info->max_rx_pktlen = HN_MAX_XFER_LEN;
317 : 0 : dev_info->max_mac_addrs = 1;
318 : :
319 : 0 : dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
320 : 0 : dev_info->flow_type_rss_offloads = hv->rss_offloads;
321 : 0 : dev_info->reta_size = RTE_ETH_RSS_RETA_SIZE_128;
322 : :
323 : 0 : dev_info->max_rx_queues = hv->max_queues;
324 : 0 : dev_info->max_tx_queues = hv->max_queues;
325 : :
326 : 0 : dev_info->tx_desc_lim.nb_min = 1;
327 : 0 : dev_info->tx_desc_lim.nb_max = 4096;
328 : :
329 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
330 : : return 0;
331 : :
332 : : /* fills in rx and tx offload capability */
333 : 0 : rc = hn_rndis_get_offload(hv, dev_info);
334 [ # # ]: 0 : if (rc != 0)
335 : : return rc;
336 : :
337 : : /* merges the offload and queues of vf */
338 : 0 : return hn_vf_info_get(hv, dev_info);
339 : : }
340 : :
341 : 0 : static int hn_rss_reta_update(struct rte_eth_dev *dev,
342 : : struct rte_eth_rss_reta_entry64 *reta_conf,
343 : : uint16_t reta_size)
344 : : {
345 : 0 : struct hn_data *hv = dev->data->dev_private;
346 : : unsigned int i;
347 : : int err;
348 : :
349 : 0 : PMD_INIT_FUNC_TRACE();
350 : :
351 [ # # ]: 0 : if (reta_size != NDIS_HASH_INDCNT) {
352 : 0 : PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
353 : 0 : return -EINVAL;
354 : : }
355 : :
356 [ # # ]: 0 : for (i = 0; i < NDIS_HASH_INDCNT; i++) {
357 : 0 : uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
358 : 0 : uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
359 : 0 : uint64_t mask = (uint64_t)1 << shift;
360 : :
361 [ # # ]: 0 : if (reta_conf[idx].mask & mask)
362 : 0 : hv->rss_ind[i] = reta_conf[idx].reta[shift];
363 : :
364 : : /*
365 : : * Ensure we don't allow config that directs traffic to an Rx
366 : : * queue that we aren't going to poll
367 : : */
368 [ # # ]: 0 : if (hv->rss_ind[i] >= dev->data->nb_rx_queues) {
369 : 0 : PMD_DRV_LOG(ERR, "RSS distributing traffic to invalid Rx queue");
370 : 0 : return -EINVAL;
371 : : }
372 : : }
373 : :
374 : 0 : err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
375 [ # # ]: 0 : if (err) {
376 : 0 : PMD_DRV_LOG(NOTICE,
377 : : "rss disable failed");
378 : 0 : return err;
379 : : }
380 : :
381 : 0 : err = hn_rndis_conf_rss(hv, 0);
382 [ # # ]: 0 : if (err) {
383 : 0 : PMD_DRV_LOG(NOTICE,
384 : : "reta reconfig failed");
385 : 0 : return err;
386 : : }
387 : :
388 : 0 : return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
389 : : }
390 : :
391 : 0 : static int hn_rss_reta_query(struct rte_eth_dev *dev,
392 : : struct rte_eth_rss_reta_entry64 *reta_conf,
393 : : uint16_t reta_size)
394 : : {
395 : 0 : struct hn_data *hv = dev->data->dev_private;
396 : : unsigned int i;
397 : :
398 : 0 : PMD_INIT_FUNC_TRACE();
399 : :
400 [ # # ]: 0 : if (reta_size != NDIS_HASH_INDCNT) {
401 : 0 : PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
402 : 0 : return -EINVAL;
403 : : }
404 : :
405 [ # # ]: 0 : for (i = 0; i < NDIS_HASH_INDCNT; i++) {
406 : 0 : uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
407 : 0 : uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
408 : 0 : uint64_t mask = (uint64_t)1 << shift;
409 : :
410 [ # # ]: 0 : if (reta_conf[idx].mask & mask)
411 : 0 : reta_conf[idx].reta[shift] = hv->rss_ind[i];
412 : : }
413 : : return 0;
414 : : }
415 : :
416 : 0 : static void hn_rss_hash_init(struct hn_data *hv,
417 : : const struct rte_eth_rss_conf *rss_conf)
418 : : {
419 : : /* Convert from DPDK RSS hash flags to NDIS hash flags */
420 : 0 : hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
421 : :
422 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV4)
423 : 0 : hv->rss_hash |= NDIS_HASH_IPV4;
424 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
425 : 0 : hv->rss_hash |= NDIS_HASH_TCP_IPV4;
426 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6)
427 : 0 : hv->rss_hash |= NDIS_HASH_IPV6;
428 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_EX)
429 : 0 : hv->rss_hash |= NDIS_HASH_IPV6_EX;
430 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_TCP)
431 : 0 : hv->rss_hash |= NDIS_HASH_TCP_IPV6;
432 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_TCP_EX)
433 : 0 : hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
434 : :
435 [ # # ]: 0 : memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
436 : : NDIS_HASH_KEYSIZE_TOEPLITZ);
437 : 0 : }
438 : :
439 : 0 : static int hn_rss_hash_update(struct rte_eth_dev *dev,
440 : : struct rte_eth_rss_conf *rss_conf)
441 : : {
442 : 0 : struct hn_data *hv = dev->data->dev_private;
443 : : int err;
444 : :
445 : 0 : PMD_INIT_FUNC_TRACE();
446 : :
447 : 0 : err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
448 [ # # ]: 0 : if (err) {
449 : 0 : PMD_DRV_LOG(NOTICE,
450 : : "rss disable failed");
451 : 0 : return err;
452 : : }
453 : :
454 : 0 : hn_rss_hash_init(hv, rss_conf);
455 : :
456 [ # # ]: 0 : if (rss_conf->rss_hf != 0) {
457 : 0 : err = hn_rndis_conf_rss(hv, 0);
458 [ # # ]: 0 : if (err) {
459 : 0 : PMD_DRV_LOG(NOTICE,
460 : : "rss reconfig failed (RSS disabled)");
461 : 0 : return err;
462 : : }
463 : : }
464 : :
465 : 0 : return hn_vf_rss_hash_update(dev, rss_conf);
466 : : }
467 : :
468 : 0 : static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
469 : : struct rte_eth_rss_conf *rss_conf)
470 : : {
471 : 0 : struct hn_data *hv = dev->data->dev_private;
472 : :
473 : 0 : PMD_INIT_FUNC_TRACE();
474 : :
475 [ # # ]: 0 : if (hv->ndis_ver < NDIS_VERSION_6_20) {
476 : 0 : PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
477 : 0 : return -EOPNOTSUPP;
478 : : }
479 : :
480 : 0 : rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ;
481 [ # # ]: 0 : if (rss_conf->rss_key)
482 : 0 : memcpy(rss_conf->rss_key, hv->rss_key,
483 : : NDIS_HASH_KEYSIZE_TOEPLITZ);
484 : :
485 : 0 : rss_conf->rss_hf = 0;
486 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_IPV4)
487 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV4;
488 : :
489 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
490 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_TCP;
491 : :
492 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_IPV6)
493 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV6;
494 : :
495 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_IPV6_EX)
496 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_EX;
497 : :
498 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
499 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_TCP;
500 : :
501 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
502 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_TCP_EX;
503 : :
504 : : return 0;
505 : : }
506 : :
507 : : static int
508 : 0 : hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
509 : : {
510 : 0 : struct hn_data *hv = dev->data->dev_private;
511 : :
512 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
513 : 0 : return hn_vf_promiscuous_enable(dev);
514 : : }
515 : :
516 : : static int
517 : 0 : hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
518 : : {
519 : 0 : struct hn_data *hv = dev->data->dev_private;
520 : : uint32_t filter;
521 : :
522 : : filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
523 [ # # ]: 0 : if (dev->data->all_multicast)
524 : : filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
525 : 0 : hn_rndis_set_rxfilter(hv, filter);
526 : 0 : return hn_vf_promiscuous_disable(dev);
527 : : }
528 : :
529 : : static int
530 : 0 : hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
531 : : {
532 : 0 : struct hn_data *hv = dev->data->dev_private;
533 : :
534 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
535 : : NDIS_PACKET_TYPE_ALL_MULTICAST |
536 : : NDIS_PACKET_TYPE_BROADCAST);
537 : 0 : return hn_vf_allmulticast_enable(dev);
538 : : }
539 : :
540 : : static int
541 : 0 : hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
542 : : {
543 : 0 : struct hn_data *hv = dev->data->dev_private;
544 : :
545 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
546 : : NDIS_PACKET_TYPE_BROADCAST);
547 : 0 : return hn_vf_allmulticast_disable(dev);
548 : : }
549 : :
550 : : static int
551 : 0 : hn_dev_mc_addr_list(struct rte_eth_dev *dev,
552 : : struct rte_ether_addr *mc_addr_set,
553 : : uint32_t nb_mc_addr)
554 : : {
555 : : /* No filtering on the synthetic path, but can do it on VF */
556 : 0 : return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
557 : : }
558 : :
559 : : /* Setup shared rx/tx queue data */
560 : 0 : static int hn_subchan_configure(struct hn_data *hv,
561 : : uint32_t subchan)
562 : : {
563 : : struct vmbus_channel *primary = hn_primary_chan(hv);
564 : : int err;
565 : : unsigned int retry = 0;
566 : :
567 : 0 : PMD_DRV_LOG(DEBUG,
568 : : "open %u subchannels", subchan);
569 : :
570 : : /* Send create sub channels command */
571 : 0 : err = hn_nvs_alloc_subchans(hv, &subchan);
572 [ # # ]: 0 : if (err)
573 : : return err;
574 : :
575 [ # # ]: 0 : while (subchan > 0) {
576 : : struct vmbus_channel *new_sc;
577 : : uint16_t chn_index;
578 : :
579 : 0 : err = rte_vmbus_subchan_open(primary, &new_sc);
580 [ # # # # ]: 0 : if (err == -ENOENT && ++retry < 1000) {
581 : : /* This can happen if not ready yet */
582 : : rte_delay_ms(10);
583 : 0 : continue;
584 : : }
585 : :
586 [ # # ]: 0 : if (err) {
587 : 0 : PMD_DRV_LOG(ERR,
588 : : "open subchannel failed: %d", err);
589 : 0 : return err;
590 : : }
591 : :
592 : 0 : rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
593 : :
594 : : retry = 0;
595 : 0 : chn_index = rte_vmbus_sub_channel_index(new_sc);
596 [ # # # # ]: 0 : if (chn_index == 0 || chn_index > hv->max_queues) {
597 : 0 : PMD_DRV_LOG(ERR,
598 : : "Invalid subchannel offermsg channel %u",
599 : : chn_index);
600 : 0 : return -EIO;
601 : : }
602 : :
603 : 0 : PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
604 : 0 : hv->channels[chn_index] = new_sc;
605 : 0 : --subchan;
606 : : }
607 : :
608 : : return err;
609 : : }
610 : :
611 : 0 : static void netvsc_hotplug_retry(void *args)
612 : : {
613 : : int ret;
614 : : struct hv_hotadd_context *hot_ctx = args;
615 : 0 : struct hn_data *hv = hot_ctx->hv;
616 : 0 : struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
617 : 0 : struct rte_devargs *d = &hot_ctx->da;
618 : : char buf[256];
619 : :
620 : : DIR *di = NULL;
621 : : struct dirent *dir;
622 : : struct ifreq req;
623 : : struct rte_ether_addr eth_addr;
624 : : int s;
625 : :
626 : 0 : PMD_DRV_LOG(DEBUG, "%s: retry count %d",
627 : : __func__, hot_ctx->eal_hot_plug_retry);
628 : :
629 : 0 : hot_ctx->eal_hot_plug_retry++;
630 : :
631 : : /* Check if PCI device still exists — if it disappeared, give up.
632 : : * Otherwise keep retrying indefinitely until the net directory
633 : : * appears. This is safe because:
634 : : * - MANA driver probe can take >100s after PCI rescan
635 : : * - The retry uses rte_eal_alarm callbacks serialized on the
636 : : * EAL interrupt thread, preventing races with device close
637 : : * - Device close cancels pending alarms and frees the context
638 : : * - If the PCI device is removed, the access() check below
639 : : * detects the missing sysfs path and stops immediately
640 : : */
641 : 0 : snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s", d->name);
642 [ # # ]: 0 : if (access(buf, F_OK) != 0) {
643 : 0 : PMD_DRV_LOG(NOTICE,
644 : : "PCI device %s no longer exists, giving up after %d retries",
645 : : d->name, hot_ctx->eal_hot_plug_retry);
646 : 0 : goto free_hotadd_ctx;
647 : : }
648 : :
649 : : snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/net", d->name);
650 : 0 : di = opendir(buf);
651 [ # # ]: 0 : if (!di) {
652 [ # # ]: 0 : if (hot_ctx->eal_hot_plug_retry % 30 == 0)
653 : 0 : PMD_DRV_LOG(NOTICE,
654 : : "%s: waiting for %s (retry %d, %ds elapsed)",
655 : : __func__, buf, hot_ctx->eal_hot_plug_retry,
656 : : hot_ctx->eal_hot_plug_retry);
657 : : else
658 : 0 : PMD_DRV_LOG(DEBUG, "%s: can't open directory %s, "
659 : : "retrying in 1 second", __func__, buf);
660 : 0 : rte_eal_alarm_set(NETVSC_HOTADD_RETRY_INTERVAL,
661 : : netvsc_hotplug_retry, hot_ctx);
662 : 0 : return;
663 : : }
664 : :
665 [ # # ]: 0 : while ((dir = readdir(di))) {
666 : : /* Skip . and .. directories */
667 [ # # # # ]: 0 : if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
668 : 0 : continue;
669 : :
670 : 0 : PMD_DRV_LOG(DEBUG,
671 : : "%s: checking interface %s in %s (retry %d)",
672 : : __func__, dir->d_name, buf,
673 : : hot_ctx->eal_hot_plug_retry);
674 : :
675 : : /* trying to get mac address if this is a network device*/
676 : 0 : s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
677 [ # # ]: 0 : if (s == -1) {
678 : 0 : PMD_DRV_LOG(ERR, "Failed to create socket errno %d",
679 : : errno);
680 : 0 : break;
681 : : }
682 : : strlcpy(req.ifr_name, dir->d_name, sizeof(req.ifr_name));
683 : 0 : ret = ioctl(s, SIOCGIFHWADDR, &req);
684 : 0 : close(s);
685 [ # # ]: 0 : if (ret == -1) {
686 : : /* Interface may be renamed by udev (e.g. eth1 → ens1).
687 : : * Retry from the top — the PCI device check above
688 : : * ensures we stop if the device disappears.
689 : : */
690 : 0 : PMD_DRV_LOG(DEBUG,
691 : : "Failed to send SIOCGIFHWADDR for device %s, "
692 : : "interface may be renaming, retrying",
693 : : dir->d_name);
694 : 0 : closedir(di);
695 : 0 : rte_eal_alarm_set(NETVSC_HOTADD_RETRY_INTERVAL,
696 : : netvsc_hotplug_retry, hot_ctx);
697 : 0 : return;
698 : : }
699 [ # # ]: 0 : if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
700 : 0 : PMD_DRV_LOG(DEBUG,
701 : : "%s: device %s sa_family=%d not ARPHRD_ETHER, skipping",
702 : : __func__, dir->d_name, req.ifr_hwaddr.sa_family);
703 : 0 : continue;
704 : : }
705 : :
706 : : memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
707 : : RTE_DIM(eth_addr.addr_bytes));
708 : :
709 [ # # ]: 0 : if (rte_is_same_ether_addr(ð_addr, dev->data->mac_addrs)) {
710 : : struct da_cache *cache;
711 : : char *drv_str = NULL;
712 : :
713 : : rte_spinlock_lock(&netvsc_lock);
714 : :
715 [ # # ]: 0 : LIST_FOREACH(cache, &da_cache_list, list) {
716 [ # # ]: 0 : if (strcmp(cache->name, d->name) == 0)
717 : : break;
718 : : }
719 : :
720 [ # # ]: 0 : if (cache)
721 : 0 : drv_str = strdup(cache->drv_str);
722 : :
723 : : rte_spinlock_unlock(&netvsc_lock);
724 : :
725 [ # # ]: 0 : PMD_DRV_LOG(NOTICE,
726 : : "Found matching MAC address, adding device %s network name %s args %s",
727 : : d->name, dir->d_name, drv_str ? drv_str : "");
728 : :
729 : : /* If this device has been hot removed from this
730 : : * parent device, restore its args.
731 : : */
732 : 0 : ret = rte_eal_hotplug_add(d->bus->name, d->name, drv_str ? drv_str : "");
733 : 0 : free(drv_str);
734 : :
735 [ # # ]: 0 : if (ret == -ENODEV) {
736 : : /* IB device not ready yet (mana_ib not probed).
737 : : * Restart the full retry from the PCI device
738 : : * check so we re-verify the device exists and
739 : : * get fresh interface names after any renames.
740 : : * This retries indefinitely — the PCI sysfs
741 : : * check at the top of this function ensures
742 : : * we stop if the device disappears.
743 : : */
744 : 0 : PMD_DRV_LOG(NOTICE,
745 : : "IB device not ready for %s, "
746 : : "restarting probe in 1 second",
747 : : d->name);
748 : 0 : closedir(di);
749 : 0 : rte_eal_alarm_set(NETVSC_HOTADD_RETRY_INTERVAL,
750 : : netvsc_hotplug_retry,
751 : : hot_ctx);
752 : 0 : return;
753 : : }
754 : :
755 [ # # ]: 0 : if (ret && ret != -EEXIST)
756 : 0 : PMD_DRV_LOG(ERR,
757 : : "Failed to add PCI device %s (ret=%d)",
758 : : d->name, ret);
759 : :
760 : 0 : ret = hn_vf_add(dev, hv);
761 [ # # ]: 0 : if (ret)
762 : 0 : PMD_DRV_LOG(ERR, "Failed to add VF: %d", ret);
763 : : break;
764 : : }
765 : :
766 : 0 : PMD_DRV_LOG(DEBUG,
767 : : "%s: MAC mismatch for %s: got "
768 : : RTE_ETHER_ADDR_PRT_FMT
769 : : " expected " RTE_ETHER_ADDR_PRT_FMT,
770 : : __func__, dir->d_name,
771 : : RTE_ETHER_ADDR_BYTES(ð_addr),
772 : : RTE_ETHER_ADDR_BYTES(dev->data->mac_addrs));
773 : : }
774 : :
775 : : /* If we opened the net directory but didn't find a matching MAC,
776 : : * the VF interface may not have appeared yet (e.g. on a multi-NIC
777 : : * PCI device, the second VF registers later). Retry.
778 : : */
779 : : if (di != NULL) {
780 : 0 : closedir(di);
781 : : di = NULL;
782 [ # # ]: 0 : if (dir == NULL) {
783 : : /* readdir returned NULL — loop ended without match */
784 : 0 : hot_ctx->mac_retry++;
785 [ # # ]: 0 : if (hot_ctx->mac_retry < NETVSC_MAX_MAC_RETRY) {
786 : 0 : PMD_DRV_LOG(DEBUG,
787 : : "%s: no matching MAC found in %s, "
788 : : "retrying in 1 second (mac_retry %d/%d)",
789 : : __func__, buf,
790 : : hot_ctx->mac_retry,
791 : : NETVSC_MAX_MAC_RETRY);
792 : 0 : rte_eal_alarm_set(NETVSC_HOTADD_RETRY_INTERVAL,
793 : : netvsc_hotplug_retry,
794 : : hot_ctx);
795 : 0 : return;
796 : : }
797 : 0 : PMD_DRV_LOG(NOTICE,
798 : : "%s: no matching MAC found after %d retries, giving up",
799 : : __func__, hot_ctx->mac_retry);
800 : : }
801 : : }
802 : :
803 : 0 : free_hotadd_ctx:
804 : : if (di != NULL)
805 : : closedir(di);
806 : :
807 : 0 : PMD_DRV_LOG(DEBUG, "%s: retry loop exiting for device %s (retry %d)",
808 : : __func__, d->name, hot_ctx->eal_hot_plug_retry);
809 : :
810 : 0 : rte_spinlock_lock(&hv->hotadd_lock);
811 [ # # ]: 0 : LIST_REMOVE(hot_ctx, list);
812 : : rte_spinlock_unlock(&hv->hotadd_lock);
813 : :
814 : 0 : rte_devargs_reset(d);
815 : 0 : free(hot_ctx);
816 : : }
817 : :
818 : : static void
819 : 0 : netvsc_hotadd_callback(const char *device_name, enum rte_dev_event_type type,
820 : : void *arg)
821 : : {
822 : : struct hn_data *hv = arg;
823 : : struct hv_hotadd_context *hot_ctx;
824 : : struct rte_devargs *d;
825 : : int ret;
826 : :
827 : 0 : PMD_DRV_LOG(INFO, "Device notification type=%d device_name=%s",
828 : : type, device_name);
829 : :
830 [ # # ]: 0 : switch (type) {
831 : 0 : case RTE_DEV_EVENT_ADD:
832 : : /* if we already has a VF, don't check on hot add */
833 [ # # ]: 0 : if (hv->vf_ctx.vf_state > vf_removed)
834 : : break;
835 : :
836 : 0 : hot_ctx = calloc(1, sizeof(*hot_ctx));
837 : :
838 [ # # ]: 0 : if (!hot_ctx) {
839 : 0 : PMD_DRV_LOG(ERR, "Failed to allocate hotadd context");
840 : 0 : return;
841 : : }
842 : :
843 : 0 : hot_ctx->hv = hv;
844 : 0 : d = &hot_ctx->da;
845 : :
846 : 0 : ret = rte_devargs_parse(d, device_name);
847 [ # # ]: 0 : if (ret) {
848 : 0 : PMD_DRV_LOG(ERR,
849 : : "devargs parsing failed ret=%d", ret);
850 : 0 : goto free_ctx;
851 : : }
852 : :
853 [ # # ]: 0 : if (!strcmp(d->bus->name, "pci")) {
854 : : /* Start the process of figuring out if this
855 : : * PCI device is a VF device
856 : : */
857 : 0 : rte_spinlock_lock(&hv->hotadd_lock);
858 [ # # ]: 0 : LIST_INSERT_HEAD(&hv->hotadd_list, hot_ctx, list);
859 : : rte_spinlock_unlock(&hv->hotadd_lock);
860 : 0 : rte_eal_alarm_set(NETVSC_HOTADD_RETRY_INTERVAL,
861 : : netvsc_hotplug_retry, hot_ctx);
862 : 0 : return;
863 : : }
864 : :
865 : : /* We will switch to VF on RDNIS configure message
866 : : * sent from VSP
867 : : */
868 : 0 : free_ctx:
869 : 0 : free(hot_ctx);
870 : 0 : break;
871 : :
872 : : default:
873 : : break;
874 : : }
875 : : }
876 : :
877 : : static void hn_detach(struct hn_data *hv);
878 : : static int hn_attach(struct hn_data *hv, unsigned int mtu);
879 : :
880 : 0 : static int hn_dev_configure(struct rte_eth_dev *dev)
881 : : {
882 : 0 : struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
883 : 0 : struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
884 : : const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
885 : : const struct rte_eth_txmode *txmode = &dev_conf->txmode;
886 : 0 : struct hn_data *hv = dev->data->dev_private;
887 : : uint64_t unsupported;
888 : : int i, err, subchan;
889 : : uint32_t old_subchans = 0;
890 : : bool device_unmapped = false;
891 : :
892 : 0 : PMD_INIT_FUNC_TRACE();
893 : :
894 [ # # ]: 0 : if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG)
895 : 0 : dev_conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
896 : :
897 : 0 : unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
898 [ # # ]: 0 : if (unsupported) {
899 : 0 : PMD_DRV_LOG(NOTICE,
900 : : "unsupported TX offload: %#" PRIx64,
901 : : unsupported);
902 : 0 : return -EINVAL;
903 : : }
904 : :
905 : 0 : unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
906 [ # # ]: 0 : if (unsupported) {
907 : 0 : PMD_DRV_LOG(NOTICE,
908 : : "unsupported RX offload: %#" PRIx64,
909 : : rxmode->offloads);
910 : 0 : return -EINVAL;
911 : : }
912 : :
913 : 0 : hv->vlan_strip = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
914 : :
915 : : /* If queue count unchanged, skip subchannel teardown/reinit */
916 : 0 : if (RTE_MAX(dev->data->nb_rx_queues,
917 [ # # ]: 0 : dev->data->nb_tx_queues) == hv->num_queues)
918 : 0 : goto skip_reinit;
919 : :
920 : 0 : hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
921 : : dev->data->nb_tx_queues);
922 : :
923 : : /* Close all existing subchannels */
924 [ # # ]: 0 : for (i = 1; i < HN_MAX_CHANNELS; i++) {
925 [ # # ]: 0 : if (hv->channels[i] != NULL) {
926 : 0 : rte_vmbus_chan_close(hv->channels[i]);
927 : 0 : hv->channels[i] = NULL;
928 : 0 : old_subchans++;
929 : : }
930 : : }
931 : :
932 : : /*
933 : : * If subchannels existed, do a full NVS/RNDIS teardown
934 : : * and vmbus re-init to ensure a clean NVS session.
935 : : * Cannot re-send NVS subchannel request on the same
936 : : * session without invalidating the data path.
937 : : */
938 [ # # ]: 0 : if (old_subchans > 0) {
939 : 0 : PMD_DRV_LOG(NOTICE,
940 : : "reinit NVS (had %u subchannels)",
941 : : old_subchans);
942 : :
943 : 0 : hn_chim_uninit(dev);
944 : 0 : rte_free(hv->primary->rxbuf_info);
945 : 0 : hv->primary->rxbuf_info = NULL;
946 : : hn_detach(hv);
947 : :
948 : 0 : rte_vmbus_chan_close(hv->channels[0]);
949 : 0 : rte_free(hv->channels[0]);
950 : 0 : hv->channels[0] = NULL;
951 : :
952 : 0 : rte_vmbus_unmap_device(hv->vmbus);
953 : : device_unmapped = true;
954 : 0 : err = rte_vmbus_map_device(hv->vmbus);
955 [ # # ]: 0 : if (err) {
956 : 0 : PMD_DRV_LOG(ERR,
957 : : "Could not re-map vmbus device!");
958 : 0 : goto reinit_failed;
959 : : }
960 : : device_unmapped = false;
961 : :
962 : 0 : hv->rxbuf_res = hv->vmbus->resource[HV_RECV_BUF_MAP];
963 : 0 : hv->chim_res = hv->vmbus->resource[HV_SEND_BUF_MAP];
964 : :
965 : 0 : err = rte_vmbus_chan_open(hv->vmbus, &hv->channels[0]);
966 [ # # ]: 0 : if (err) {
967 : 0 : PMD_DRV_LOG(ERR,
968 : : "Could not re-open vmbus channel!");
969 : 0 : goto reinit_failed;
970 : : }
971 : :
972 : 0 : hv->primary->chan = hv->channels[0];
973 : :
974 : 0 : rte_vmbus_set_latency(hv->vmbus, hv->channels[0],
975 : : hv->latency);
976 : :
977 : 0 : err = hn_attach(hv, dev->data->mtu);
978 [ # # ]: 0 : if (err) {
979 : 0 : rte_vmbus_chan_close(hv->channels[0]);
980 : 0 : rte_free(hv->channels[0]);
981 : 0 : hv->channels[0] = NULL;
982 : 0 : PMD_DRV_LOG(ERR,
983 : : "NVS reinit failed: %d", err);
984 : 0 : goto reinit_failed;
985 : : }
986 : :
987 : 0 : err = hn_chim_init(dev);
988 [ # # ]: 0 : if (err) {
989 : : hn_detach(hv);
990 : 0 : rte_vmbus_chan_close(hv->channels[0]);
991 : 0 : rte_free(hv->channels[0]);
992 : 0 : hv->channels[0] = NULL;
993 : 0 : PMD_DRV_LOG(ERR,
994 : : "chim reinit failed: %d", err);
995 : 0 : goto reinit_failed;
996 : : }
997 : : }
998 : :
999 [ # # ]: 0 : for (i = 0; i < NDIS_HASH_INDCNT; i++)
1000 : 0 : hv->rss_ind[i] = i % dev->data->nb_rx_queues;
1001 : :
1002 : 0 : hn_rss_hash_init(hv, rss_conf);
1003 : :
1004 : 0 : subchan = hv->num_queues - 1;
1005 : :
1006 : : /* Allocate fresh subchannels and configure RSS */
1007 [ # # ]: 0 : if (subchan > 0) {
1008 : 0 : err = hn_subchan_configure(hv, subchan);
1009 [ # # ]: 0 : if (err) {
1010 : 0 : PMD_DRV_LOG(NOTICE,
1011 : : "subchannel configuration failed");
1012 : 0 : goto subchan_cleanup;
1013 : : }
1014 : :
1015 : 0 : err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
1016 [ # # ]: 0 : if (err) {
1017 : 0 : PMD_DRV_LOG(NOTICE,
1018 : : "rss disable failed");
1019 : 0 : goto subchan_cleanup;
1020 : : }
1021 : :
1022 [ # # ]: 0 : if (rss_conf->rss_hf != 0) {
1023 : 0 : err = hn_rndis_conf_rss(hv, 0);
1024 [ # # ]: 0 : if (err) {
1025 : 0 : PMD_DRV_LOG(NOTICE,
1026 : : "initial RSS config failed");
1027 : 0 : goto subchan_cleanup;
1028 : : }
1029 : : }
1030 : : }
1031 : :
1032 : 0 : skip_reinit:
1033 : : /* Apply offload config after reinit so it targets the final RNDIS session */
1034 : 0 : err = hn_rndis_conf_offload(hv, txmode->offloads,
1035 : 0 : rxmode->offloads);
1036 [ # # ]: 0 : if (err) {
1037 : 0 : PMD_DRV_LOG(NOTICE,
1038 : : "offload configure failed");
1039 : 0 : return err;
1040 : : }
1041 : :
1042 : 0 : return hn_vf_configure_locked(dev, dev_conf);
1043 : :
1044 : 0 : subchan_cleanup:
1045 [ # # ]: 0 : for (i = 1; i < HN_MAX_CHANNELS; i++) {
1046 [ # # ]: 0 : if (hv->channels[i] != NULL) {
1047 : 0 : rte_vmbus_chan_close(hv->channels[i]);
1048 : 0 : hv->channels[i] = NULL;
1049 : : }
1050 : : }
1051 : 0 : hv->num_queues = 1;
1052 [ # # ]: 0 : for (i = 0; i < NDIS_HASH_INDCNT; i++)
1053 : 0 : hv->rss_ind[i] = 0;
1054 : :
1055 : : /* Apply offload config so device is usable on primary queue */
1056 : 0 : hn_rndis_conf_offload(hv, txmode->offloads, rxmode->offloads);
1057 : 0 : return err;
1058 : :
1059 : 0 : reinit_failed:
1060 : : /*
1061 : : * Device is in a broken state after failed reinit.
1062 : : * Try to re-establish minimal connectivity.
1063 : : */
1064 : 0 : PMD_DRV_LOG(ERR,
1065 : : "reinit failed (err %d), attempting recovery", err);
1066 [ # # ]: 0 : if (hv->channels[0] == NULL) {
1067 [ # # ]: 0 : if (device_unmapped) {
1068 [ # # ]: 0 : if (rte_vmbus_map_device(hv->vmbus)) {
1069 : 0 : hv->num_queues = 0;
1070 : 0 : PMD_DRV_LOG(ERR,
1071 : : "recovery failed, could not re-map device");
1072 : 0 : return err;
1073 : : }
1074 : 0 : hv->rxbuf_res = hv->vmbus->resource[HV_RECV_BUF_MAP];
1075 : 0 : hv->chim_res = hv->vmbus->resource[HV_SEND_BUF_MAP];
1076 : : }
1077 [ # # ]: 0 : if (rte_vmbus_chan_open(hv->vmbus, &hv->channels[0]) == 0) {
1078 [ # # ]: 0 : if (hn_attach(hv, dev->data->mtu) == 0) {
1079 : 0 : hv->primary->chan = hv->channels[0];
1080 [ # # ]: 0 : if (hn_chim_init(dev) != 0)
1081 : 0 : PMD_DRV_LOG(WARNING,
1082 : : "chim reinit failed during recovery");
1083 : 0 : hv->num_queues = 1;
1084 : 0 : PMD_DRV_LOG(NOTICE,
1085 : : "recovery successful on primary channel");
1086 : : } else {
1087 : 0 : rte_vmbus_chan_close(hv->channels[0]);
1088 : 0 : rte_free(hv->channels[0]);
1089 : 0 : hv->channels[0] = NULL;
1090 : 0 : hv->num_queues = 0;
1091 : 0 : PMD_DRV_LOG(ERR,
1092 : : "recovery failed, device unusable");
1093 : : }
1094 : : } else {
1095 : 0 : hv->num_queues = 0;
1096 : 0 : PMD_DRV_LOG(ERR,
1097 : : "recovery failed, device unusable");
1098 : : }
1099 : : } else {
1100 : 0 : hv->num_queues = 1;
1101 : : }
1102 : : return err;
1103 : : }
1104 : :
1105 : 0 : static int hn_dev_stats_get(struct rte_eth_dev *dev,
1106 : : struct rte_eth_stats *stats,
1107 : : struct eth_queue_stats *qstats)
1108 : : {
1109 : : unsigned int i;
1110 : :
1111 : 0 : hn_vf_stats_get(dev, stats, qstats);
1112 : :
1113 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
1114 : 0 : const struct hn_tx_queue *txq = dev->data->tx_queues[i];
1115 : :
1116 [ # # ]: 0 : if (!txq)
1117 : 0 : continue;
1118 : :
1119 : 0 : stats->opackets += txq->stats.packets;
1120 : 0 : stats->obytes += txq->stats.bytes;
1121 : 0 : stats->oerrors += txq->stats.errors;
1122 : :
1123 [ # # ]: 0 : if (qstats != NULL) {
1124 : 0 : qstats[i].q_opackets += txq->stats.packets;
1125 : 0 : qstats[i].q_obytes += txq->stats.bytes;
1126 : : }
1127 : : }
1128 : :
1129 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
1130 : 0 : const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
1131 : :
1132 [ # # ]: 0 : if (!rxq)
1133 : 0 : continue;
1134 : :
1135 : 0 : stats->ipackets += rxq->stats.packets;
1136 : 0 : stats->ibytes += rxq->stats.bytes;
1137 : 0 : stats->ierrors += rxq->stats.errors;
1138 : 0 : stats->imissed += rxq->stats.ring_full;
1139 : :
1140 [ # # ]: 0 : if (qstats != NULL) {
1141 : 0 : qstats[i].q_ipackets += rxq->stats.packets;
1142 : 0 : qstats[i].q_ibytes += rxq->stats.bytes;
1143 : : }
1144 : : }
1145 : :
1146 : 0 : stats->rx_nombuf += dev->data->rx_mbuf_alloc_failed;
1147 : 0 : return 0;
1148 : : }
1149 : :
1150 : : static int
1151 : 0 : hn_dev_stats_reset(struct rte_eth_dev *dev)
1152 : : {
1153 : : unsigned int i;
1154 : :
1155 : 0 : PMD_INIT_FUNC_TRACE();
1156 : :
1157 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
1158 : 0 : struct hn_tx_queue *txq = dev->data->tx_queues[i];
1159 : :
1160 [ # # ]: 0 : if (!txq)
1161 : 0 : continue;
1162 : 0 : memset(&txq->stats, 0, sizeof(struct hn_stats));
1163 : : }
1164 : :
1165 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
1166 : 0 : struct hn_rx_queue *rxq = dev->data->rx_queues[i];
1167 : :
1168 [ # # ]: 0 : if (!rxq)
1169 : 0 : continue;
1170 : :
1171 : 0 : memset(&rxq->stats, 0, sizeof(struct hn_stats));
1172 : : }
1173 : :
1174 : 0 : return 0;
1175 : : }
1176 : :
1177 : : static int
1178 : 0 : hn_dev_xstats_reset(struct rte_eth_dev *dev)
1179 : : {
1180 : : int ret;
1181 : :
1182 : 0 : ret = hn_dev_stats_reset(dev);
1183 [ # # ]: 0 : if (ret != 0)
1184 : : return 0;
1185 : :
1186 : 0 : return hn_vf_xstats_reset(dev);
1187 : : }
1188 : :
1189 : : static int
1190 : 0 : hn_dev_xstats_count(struct rte_eth_dev *dev)
1191 : : {
1192 : : int ret, count;
1193 : :
1194 : 0 : count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
1195 : 0 : count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
1196 : :
1197 : 0 : ret = hn_vf_xstats_get_names(dev, NULL, 0);
1198 [ # # ]: 0 : if (ret < 0)
1199 : : return ret;
1200 : :
1201 : 0 : return count + ret;
1202 : : }
1203 : :
1204 : : static int
1205 : 0 : hn_dev_xstats_get_names(struct rte_eth_dev *dev,
1206 : : struct rte_eth_xstat_name *xstats_names,
1207 : : unsigned int limit)
1208 : : {
1209 : : unsigned int i, t, count = 0;
1210 : : int ret;
1211 : :
1212 [ # # ]: 0 : if (!xstats_names)
1213 : 0 : return hn_dev_xstats_count(dev);
1214 : :
1215 : : /* Note: limit checked in rte_eth_xstats_names() */
1216 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
1217 : 0 : const struct hn_tx_queue *txq = dev->data->tx_queues[i];
1218 : :
1219 [ # # ]: 0 : if (!txq)
1220 : 0 : continue;
1221 : :
1222 [ # # ]: 0 : if (count >= limit)
1223 : : break;
1224 : :
1225 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
1226 : 0 : snprintf(xstats_names[count++].name,
1227 : : RTE_ETH_XSTATS_NAME_SIZE,
1228 : 0 : "tx_q%u_%s", i, hn_stat_strings[t].name);
1229 : : }
1230 : :
1231 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
1232 : 0 : const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
1233 : :
1234 [ # # ]: 0 : if (!rxq)
1235 : 0 : continue;
1236 : :
1237 [ # # ]: 0 : if (count >= limit)
1238 : : break;
1239 : :
1240 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
1241 : 0 : snprintf(xstats_names[count++].name,
1242 : : RTE_ETH_XSTATS_NAME_SIZE,
1243 : : "rx_q%u_%s", i,
1244 : 0 : hn_stat_strings[t].name);
1245 : : }
1246 : :
1247 : 0 : ret = hn_vf_xstats_get_names(dev, xstats_names + count,
1248 : : limit - count);
1249 [ # # ]: 0 : if (ret < 0)
1250 : : return ret;
1251 : :
1252 : 0 : return count + ret;
1253 : : }
1254 : :
1255 : : static int
1256 : 0 : hn_dev_xstats_get(struct rte_eth_dev *dev,
1257 : : struct rte_eth_xstat *xstats,
1258 : : unsigned int n)
1259 : : {
1260 : : unsigned int i, t, count = 0;
1261 : 0 : const unsigned int nstats = hn_dev_xstats_count(dev);
1262 : : const char *stats;
1263 : : int ret;
1264 : :
1265 : 0 : PMD_INIT_FUNC_TRACE();
1266 : :
1267 [ # # ]: 0 : if (n < nstats)
1268 : : return nstats;
1269 : :
1270 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
1271 : 0 : const struct hn_tx_queue *txq = dev->data->tx_queues[i];
1272 : :
1273 [ # # ]: 0 : if (!txq)
1274 : 0 : continue;
1275 : :
1276 : 0 : stats = (const char *)&txq->stats;
1277 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
1278 : 0 : xstats[count].id = count;
1279 : 0 : xstats[count].value = *(const uint64_t *)
1280 : 0 : (stats + hn_stat_strings[t].offset);
1281 : : }
1282 : : }
1283 : :
1284 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
1285 : 0 : const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
1286 : :
1287 [ # # ]: 0 : if (!rxq)
1288 : 0 : continue;
1289 : :
1290 : 0 : stats = (const char *)&rxq->stats;
1291 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
1292 : 0 : xstats[count].id = count;
1293 : 0 : xstats[count].value = *(const uint64_t *)
1294 : 0 : (stats + hn_stat_strings[t].offset);
1295 : : }
1296 : : }
1297 : :
1298 : 0 : ret = hn_vf_xstats_get(dev, xstats, count, n);
1299 [ # # ]: 0 : if (ret < 0)
1300 : : return ret;
1301 : :
1302 : 0 : return count + ret;
1303 : : }
1304 : :
1305 : : static int
1306 : 0 : hn_dev_start(struct rte_eth_dev *dev)
1307 : : {
1308 : 0 : struct hn_data *hv = dev->data->dev_private;
1309 : : int i, error;
1310 : :
1311 : 0 : PMD_INIT_FUNC_TRACE();
1312 : :
1313 : : /* Register to monitor hot plug events */
1314 : 0 : error = rte_dev_event_callback_register(NULL, netvsc_hotadd_callback,
1315 : : hv);
1316 [ # # ]: 0 : if (error) {
1317 : 0 : PMD_DRV_LOG(ERR, "failed to register device event callback");
1318 : 0 : return error;
1319 : : }
1320 : :
1321 : 0 : error = hn_rndis_set_rxfilter(hv,
1322 : : NDIS_PACKET_TYPE_BROADCAST |
1323 : : NDIS_PACKET_TYPE_ALL_MULTICAST |
1324 : : NDIS_PACKET_TYPE_DIRECTED);
1325 [ # # ]: 0 : if (error) {
1326 : 0 : rte_dev_event_callback_unregister(NULL,
1327 : : netvsc_hotadd_callback, hv);
1328 : 0 : return error;
1329 : : }
1330 : :
1331 : 0 : error = hn_vf_start(dev);
1332 [ # # ]: 0 : if (error) {
1333 : 0 : hn_rndis_set_rxfilter(hv, 0);
1334 : 0 : rte_dev_event_callback_unregister(NULL,
1335 : : netvsc_hotadd_callback, hv);
1336 : 0 : return error;
1337 : : }
1338 : :
1339 : : /* Initialize Link state */
1340 : 0 : hn_dev_link_update(dev, 0);
1341 : :
1342 [ # # ]: 0 : for (i = 0; i < hv->num_queues; i++) {
1343 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
1344 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
1345 : : }
1346 : :
1347 : : return error;
1348 : : }
1349 : :
1350 : : static int
1351 : 0 : hn_dev_stop(struct rte_eth_dev *dev)
1352 : : {
1353 : 0 : struct hn_data *hv = dev->data->dev_private;
1354 : : int i, ret;
1355 : : unsigned int retry;
1356 : :
1357 : 0 : PMD_INIT_FUNC_TRACE();
1358 : 0 : dev->data->dev_started = 0;
1359 : :
1360 : 0 : rte_dev_event_callback_unregister(NULL, netvsc_hotadd_callback, hv);
1361 : 0 : hn_rndis_set_rxfilter(hv, 0);
1362 : 0 : ret = hn_vf_stop(dev);
1363 : :
1364 : : /*
1365 : : * Drain pending TX completions to prevent stale completions
1366 : : * from corrupting queue state after port reconfiguration.
1367 : : */
1368 [ # # ]: 0 : for (retry = 0; retry < 100; retry++) {
1369 : : uint32_t pending = 0;
1370 : :
1371 [ # # ]: 0 : for (i = 0; i < hv->num_queues; i++) {
1372 : 0 : struct hn_tx_queue *txq = dev->data->tx_queues[i];
1373 : :
1374 [ # # ]: 0 : if (txq == NULL)
1375 : 0 : continue;
1376 : 0 : hn_process_events(hv, i, 0);
1377 : 0 : pending += rte_mempool_in_use_count(txq->txdesc_pool);
1378 : : }
1379 [ # # ]: 0 : if (pending == 0)
1380 : : break;
1381 : : rte_delay_ms(10);
1382 : : }
1383 [ # # ]: 0 : if (retry >= 100)
1384 : 0 : PMD_DRV_LOG(WARNING,
1385 : : "Failed to drain all TX completions");
1386 : :
1387 [ # # ]: 0 : for (i = 0; i < hv->num_queues; i++) {
1388 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1389 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1390 : : }
1391 : :
1392 : 0 : return ret;
1393 : : }
1394 : :
1395 : : static int
1396 : 0 : hn_dev_close(struct rte_eth_dev *dev)
1397 : : {
1398 : : int ret;
1399 : 0 : struct hn_data *hv = dev->data->dev_private;
1400 : : struct hv_hotadd_context *hot_ctx;
1401 : :
1402 : 0 : PMD_INIT_FUNC_TRACE();
1403 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1404 : : return 0;
1405 : :
1406 : 0 : rte_spinlock_lock(&hv->hotadd_lock);
1407 [ # # ]: 0 : while (!LIST_EMPTY(&hv->hotadd_list)) {
1408 : : hot_ctx = LIST_FIRST(&hv->hotadd_list);
1409 : 0 : rte_eal_alarm_cancel(netvsc_hotplug_retry, hot_ctx);
1410 [ # # ]: 0 : LIST_REMOVE(hot_ctx, list);
1411 : 0 : rte_devargs_reset(&hot_ctx->da);
1412 : 0 : free(hot_ctx);
1413 : : }
1414 : : rte_spinlock_unlock(&hv->hotadd_lock);
1415 : :
1416 : 0 : ret = hn_vf_close(dev);
1417 : 0 : hn_dev_free_queues(dev);
1418 : :
1419 : 0 : return ret;
1420 : : }
1421 : :
1422 : : /*
1423 : : * Setup connection between PMD and kernel.
1424 : : */
1425 : : static int
1426 : 0 : hn_attach(struct hn_data *hv, unsigned int mtu)
1427 : : {
1428 : : int error;
1429 : :
1430 : : /* Attach NVS */
1431 : 0 : error = hn_nvs_attach(hv, mtu);
1432 [ # # ]: 0 : if (error)
1433 : 0 : goto failed_nvs;
1434 : :
1435 : : /* Attach RNDIS */
1436 : 0 : error = hn_rndis_attach(hv);
1437 [ # # ]: 0 : if (error)
1438 : 0 : goto failed_rndis;
1439 : :
1440 : : /*
1441 : : * NOTE:
1442 : : * Under certain conditions on certain versions of Hyper-V,
1443 : : * the RNDIS rxfilter is _not_ zero on the hypervisor side
1444 : : * after the successful RNDIS initialization.
1445 : : */
1446 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
1447 : 0 : return 0;
1448 : : failed_rndis:
1449 : 0 : hn_nvs_detach(hv);
1450 : : failed_nvs:
1451 : : return error;
1452 : : }
1453 : :
1454 : : static void
1455 : : hn_detach(struct hn_data *hv)
1456 : : {
1457 : 0 : hn_nvs_detach(hv);
1458 : 0 : hn_rndis_detach(hv);
1459 : : }
1460 : :
1461 : : /*
1462 : : * Connects EXISTING rx/tx queues to NEW vmbus channel(s), and
1463 : : * re-initializes NDIS and RNDIS, including re-sending initial
1464 : : * NDIS/RNDIS configuration. To be used after the underlying vmbus
1465 : : * has been un- and re-mapped, e.g. as must happen when the device
1466 : : * MTU is changed.
1467 : : */
1468 : : static int
1469 : 0 : hn_reinit(struct rte_eth_dev *dev, uint16_t mtu)
1470 : : {
1471 : 0 : struct hn_data *hv = dev->data->dev_private;
1472 : 0 : struct hn_rx_queue **rxqs = (struct hn_rx_queue **)dev->data->rx_queues;
1473 : 0 : struct hn_tx_queue **txqs = (struct hn_tx_queue **)dev->data->tx_queues;
1474 : : int i, ret = 0;
1475 : :
1476 : : /* Point primary queues at new primary channel */
1477 [ # # ]: 0 : if (rxqs[0]) {
1478 : 0 : rxqs[0]->chan = hv->channels[0];
1479 : 0 : txqs[0]->chan = hv->channels[0];
1480 : : }
1481 : :
1482 : 0 : ret = hn_attach(hv, mtu);
1483 [ # # ]: 0 : if (ret)
1484 : : return ret;
1485 : :
1486 : : /* Create vmbus subchannels, additional RNDIS configuration */
1487 : 0 : ret = hn_dev_configure(dev);
1488 [ # # ]: 0 : if (ret)
1489 : : return ret;
1490 : :
1491 : : /* Point any additional queues at new subchannels */
1492 [ # # ]: 0 : if (rxqs[0]) {
1493 [ # # ]: 0 : for (i = 1; i < dev->data->nb_rx_queues; i++)
1494 : 0 : rxqs[i]->chan = hv->channels[i];
1495 [ # # ]: 0 : for (i = 1; i < dev->data->nb_tx_queues; i++)
1496 : 0 : txqs[i]->chan = hv->channels[i];
1497 : : }
1498 : :
1499 : : return ret;
1500 : : }
1501 : :
1502 : : static int
1503 : 0 : hn_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1504 : : {
1505 : 0 : struct hn_data *hv = dev->data->dev_private;
1506 : 0 : unsigned int orig_mtu = dev->data->mtu;
1507 : : uint32_t rndis_mtu;
1508 : : int ret = 0;
1509 : : int i;
1510 : :
1511 [ # # ]: 0 : if (dev->data->dev_started) {
1512 : 0 : PMD_DRV_LOG(ERR, "Device must be stopped before changing MTU");
1513 : 0 : return -EBUSY;
1514 : : }
1515 : :
1516 : : /* Change MTU of underlying VF dev first, if it exists */
1517 : 0 : ret = hn_vf_mtu_set(dev, mtu);
1518 [ # # ]: 0 : if (ret)
1519 : : return ret;
1520 : :
1521 : : /* Free chimney bitmap and rxbuf_info before NVS detach */
1522 : 0 : hn_chim_uninit(dev);
1523 : 0 : rte_free(hv->primary->rxbuf_info);
1524 : 0 : hv->primary->rxbuf_info = NULL;
1525 : :
1526 : : /* Release channel resources */
1527 : : hn_detach(hv);
1528 : :
1529 : : /* Close any secondary vmbus channels */
1530 [ # # ]: 0 : for (i = 1; i < hv->num_queues; i++) {
1531 : 0 : rte_vmbus_chan_close(hv->channels[i]);
1532 : 0 : hv->channels[i] = NULL;
1533 : : }
1534 : 0 : hv->num_queues = 1;
1535 : :
1536 : : /* Close primary vmbus channel */
1537 : 0 : rte_vmbus_chan_close(hv->channels[0]);
1538 : 0 : rte_free(hv->channels[0]);
1539 : :
1540 : : /* Unmap and re-map vmbus device */
1541 : 0 : rte_vmbus_unmap_device(hv->vmbus);
1542 : 0 : ret = rte_vmbus_map_device(hv->vmbus);
1543 [ # # ]: 0 : if (ret) {
1544 : : /* This is a catastrophic error - the device is unusable */
1545 : 0 : PMD_DRV_LOG(ERR, "Could not re-map vmbus device!");
1546 : 0 : return ret;
1547 : : }
1548 : :
1549 : : /* Update pointers to re-mapped UIO resources */
1550 : 0 : hv->rxbuf_res = hv->vmbus->resource[HV_RECV_BUF_MAP];
1551 : 0 : hv->chim_res = hv->vmbus->resource[HV_SEND_BUF_MAP];
1552 : :
1553 : : /* Re-open the primary vmbus channel */
1554 : 0 : ret = rte_vmbus_chan_open(hv->vmbus, &hv->channels[0]);
1555 [ # # ]: 0 : if (ret) {
1556 : : /* This is a catastrophic error - the device is unusable */
1557 : 0 : PMD_DRV_LOG(ERR, "Could not re-open vmbus channel!");
1558 : 0 : return ret;
1559 : : }
1560 : :
1561 : 0 : hv->primary->chan = hv->channels[0];
1562 : 0 : rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
1563 : :
1564 : 0 : ret = hn_reinit(dev, mtu);
1565 [ # # ]: 0 : if (!ret) {
1566 : 0 : hn_chim_init(dev);
1567 : 0 : goto out;
1568 : : }
1569 : :
1570 : : /* In case of error, attempt to restore original MTU */
1571 : 0 : ret = hn_reinit(dev, orig_mtu);
1572 [ # # ]: 0 : if (ret)
1573 : 0 : PMD_DRV_LOG(ERR, "Restoring original MTU failed for netvsc");
1574 : : else
1575 : 0 : hn_chim_init(dev);
1576 : :
1577 : 0 : ret = hn_vf_mtu_set(dev, orig_mtu);
1578 [ # # ]: 0 : if (ret)
1579 : 0 : PMD_DRV_LOG(ERR, "Restoring original MTU failed for VF");
1580 : :
1581 : 0 : out:
1582 [ # # ]: 0 : if (hn_rndis_get_mtu(hv, &rndis_mtu)) {
1583 : 0 : PMD_DRV_LOG(ERR, "Could not get MTU via RNDIS");
1584 : : } else {
1585 : 0 : dev->data->mtu = (uint16_t)rndis_mtu;
1586 : 0 : PMD_DRV_LOG(DEBUG, "RNDIS MTU is %u", dev->data->mtu);
1587 : : }
1588 : :
1589 : : return ret;
1590 : : }
1591 : :
1592 : : static const struct eth_dev_ops hn_eth_dev_ops = {
1593 : : .dev_configure = hn_dev_configure,
1594 : : .dev_start = hn_dev_start,
1595 : : .dev_stop = hn_dev_stop,
1596 : : .dev_close = hn_dev_close,
1597 : : .dev_infos_get = hn_dev_info_get,
1598 : : .txq_info_get = hn_dev_tx_queue_info,
1599 : : .rxq_info_get = hn_dev_rx_queue_info,
1600 : : .dev_supported_ptypes_get = hn_vf_supported_ptypes,
1601 : : .promiscuous_enable = hn_dev_promiscuous_enable,
1602 : : .promiscuous_disable = hn_dev_promiscuous_disable,
1603 : : .allmulticast_enable = hn_dev_allmulticast_enable,
1604 : : .allmulticast_disable = hn_dev_allmulticast_disable,
1605 : : .set_mc_addr_list = hn_dev_mc_addr_list,
1606 : : .mtu_set = hn_dev_mtu_set,
1607 : : .reta_update = hn_rss_reta_update,
1608 : : .reta_query = hn_rss_reta_query,
1609 : : .rss_hash_update = hn_rss_hash_update,
1610 : : .rss_hash_conf_get = hn_rss_hash_conf_get,
1611 : : .tx_queue_setup = hn_dev_tx_queue_setup,
1612 : : .tx_queue_release = hn_dev_tx_queue_release,
1613 : : .tx_done_cleanup = hn_dev_tx_done_cleanup,
1614 : : .rx_queue_setup = hn_dev_rx_queue_setup,
1615 : : .rx_queue_release = hn_dev_rx_queue_release,
1616 : : .link_update = hn_dev_link_update,
1617 : : .stats_get = hn_dev_stats_get,
1618 : : .stats_reset = hn_dev_stats_reset,
1619 : : .xstats_get = hn_dev_xstats_get,
1620 : : .xstats_get_names = hn_dev_xstats_get_names,
1621 : : .xstats_reset = hn_dev_xstats_reset,
1622 : : };
1623 : :
1624 : : static int
1625 : 0 : eth_hn_dev_init(struct rte_eth_dev *eth_dev)
1626 : : {
1627 : 0 : struct hn_data *hv = eth_dev->data->dev_private;
1628 : : struct rte_vmbus_device *vmbus;
1629 : : uint32_t mtu;
1630 : : unsigned int rxr_cnt;
1631 : : int err, max_chan;
1632 : :
1633 : 0 : PMD_INIT_FUNC_TRACE();
1634 : :
1635 : : rte_spinlock_init(&hv->hotadd_lock);
1636 : 0 : LIST_INIT(&hv->hotadd_list);
1637 : :
1638 : 0 : vmbus = RTE_CLASS_TO_BUS_DEVICE(eth_dev, *vmbus);
1639 : 0 : eth_dev->dev_ops = &hn_eth_dev_ops;
1640 : 0 : eth_dev->rx_queue_count = hn_dev_rx_queue_count;
1641 : 0 : eth_dev->rx_descriptor_status = hn_dev_rx_queue_status;
1642 : 0 : eth_dev->tx_descriptor_status = hn_dev_tx_descriptor_status;
1643 : 0 : eth_dev->tx_pkt_burst = &hn_xmit_pkts;
1644 : 0 : eth_dev->rx_pkt_burst = &hn_recv_pkts;
1645 : :
1646 : : /*
1647 : : * for secondary processes, we don't initialize any further as primary
1648 : : * has already done this work.
1649 : : */
1650 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1651 : : return 0;
1652 : :
1653 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1654 : :
1655 : : /* Since Hyper-V only supports one MAC address */
1656 : 0 : eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS,
1657 : : sizeof(struct rte_ether_addr), 0);
1658 [ # # ]: 0 : if (eth_dev->data->mac_addrs == NULL) {
1659 : 0 : PMD_INIT_LOG(ERR,
1660 : : "Failed to allocate memory store MAC addresses");
1661 : 0 : return -ENOMEM;
1662 : : }
1663 : :
1664 : 0 : hv->vmbus = vmbus;
1665 : 0 : hv->rxbuf_res = vmbus->resource[HV_RECV_BUF_MAP];
1666 : 0 : hv->chim_res = vmbus->resource[HV_SEND_BUF_MAP];
1667 : 0 : hv->port_id = eth_dev->data->port_id;
1668 : 0 : hv->latency = HN_CHAN_LATENCY_NS;
1669 : 0 : hv->rx_copybreak = HN_RXCOPY_THRESHOLD;
1670 : 0 : hv->tx_copybreak = HN_TXCOPY_THRESHOLD;
1671 : 0 : hv->rx_extmbuf_enable = HN_RX_EXTMBUF_ENABLE;
1672 : 0 : hv->max_queues = 1;
1673 : :
1674 : : rte_rwlock_init(&hv->vf_lock);
1675 : 0 : hv->vf_ctx.vf_vsc_switched = false;
1676 : 0 : hv->vf_ctx.vf_vsp_reported = false;
1677 : 0 : hv->vf_ctx.vf_attached = false;
1678 : 0 : hv->vf_ctx.vf_state = vf_unknown;
1679 : :
1680 : 0 : err = hn_parse_args(eth_dev);
1681 [ # # ]: 0 : if (err)
1682 : : return err;
1683 : :
1684 : 0 : strlcpy(hv->owner.name, eth_dev->device->name,
1685 : : RTE_ETH_MAX_OWNER_NAME_LEN);
1686 : 0 : err = rte_eth_dev_owner_new(&hv->owner.id);
1687 [ # # ]: 0 : if (err) {
1688 : 0 : PMD_INIT_LOG(ERR, "Can not get owner id");
1689 : 0 : return err;
1690 : : }
1691 : :
1692 : : /* Initialize primary channel input for control operations */
1693 : 0 : err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
1694 [ # # ]: 0 : if (err)
1695 : : return err;
1696 : :
1697 : 0 : rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
1698 : :
1699 : 0 : hv->primary = hn_rx_queue_alloc(hv, 0,
1700 : 0 : eth_dev->device->numa_node);
1701 : :
1702 [ # # ]: 0 : if (!hv->primary) {
1703 : : err = -ENOMEM;
1704 : 0 : goto failed;
1705 : : }
1706 : :
1707 : 0 : err = hn_attach(hv, RTE_ETHER_MTU);
1708 [ # # ]: 0 : if (err)
1709 : 0 : goto failed;
1710 : :
1711 : 0 : err = hn_chim_init(eth_dev);
1712 [ # # ]: 0 : if (err)
1713 : 0 : goto failed;
1714 : :
1715 : 0 : err = hn_rndis_get_mtu(hv, &mtu);
1716 [ # # ]: 0 : if (err)
1717 : 0 : goto failed;
1718 : 0 : eth_dev->data->mtu = (uint16_t)mtu;
1719 : 0 : PMD_INIT_LOG(DEBUG, "RNDIS MTU is %u", eth_dev->data->mtu);
1720 : :
1721 : 0 : err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes);
1722 [ # # ]: 0 : if (err)
1723 : 0 : goto failed;
1724 : :
1725 : : /* Multi queue requires later versions of windows server */
1726 [ # # ]: 0 : if (hv->nvs_ver < NVS_VERSION_5)
1727 : : return 0;
1728 : :
1729 : 0 : max_chan = rte_vmbus_max_channels(vmbus);
1730 : 0 : PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
1731 [ # # ]: 0 : if (max_chan <= 0) {
1732 [ # # ]: 0 : err = max_chan ? max_chan : -ENODEV;
1733 : 0 : goto failed;
1734 : : }
1735 : :
1736 [ # # ]: 0 : if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
1737 : 0 : rxr_cnt = 1;
1738 : :
1739 : 0 : hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
1740 : :
1741 : : /* If VF was reported but not added, do it now */
1742 : 0 : rte_rwlock_write_lock(&hv->vf_lock);
1743 [ # # # # ]: 0 : if (hv->vf_ctx.vf_vsp_reported && !hv->vf_ctx.vf_vsc_switched) {
1744 : 0 : PMD_INIT_LOG(DEBUG, "Adding VF device");
1745 : 0 : err = hn_vf_add_unlocked(eth_dev, hv);
1746 : : }
1747 : : rte_rwlock_write_unlock(&hv->vf_lock);
1748 : :
1749 : 0 : return 0;
1750 : :
1751 : 0 : failed:
1752 : 0 : PMD_INIT_LOG(NOTICE, "device init failed");
1753 : :
1754 : 0 : hn_chim_uninit(eth_dev);
1755 : : hn_detach(hv);
1756 : 0 : rte_free(hv->primary);
1757 : 0 : rte_vmbus_chan_close(hv->channels[0]);
1758 : 0 : return err;
1759 : : }
1760 : :
1761 : : static int
1762 : 0 : eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1763 : : {
1764 : 0 : struct hn_data *hv = eth_dev->data->dev_private;
1765 : : int ret, ret_stop;
1766 : : int i;
1767 : :
1768 : 0 : PMD_INIT_FUNC_TRACE();
1769 : :
1770 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1771 : : return 0;
1772 : :
1773 : 0 : ret_stop = hn_dev_stop(eth_dev);
1774 : 0 : hn_dev_close(eth_dev);
1775 : :
1776 : : hn_detach(hv);
1777 : 0 : hn_chim_uninit(eth_dev);
1778 : :
1779 : : /* Close any subchannels before closing the primary channel */
1780 [ # # ]: 0 : for (i = 1; i < HN_MAX_CHANNELS; i++) {
1781 [ # # ]: 0 : if (hv->channels[i] != NULL) {
1782 : 0 : rte_vmbus_chan_close(hv->channels[i]);
1783 : 0 : hv->channels[i] = NULL;
1784 : : }
1785 : : }
1786 : :
1787 : 0 : rte_vmbus_chan_close(hv->channels[0]);
1788 : 0 : ret = rte_eth_dev_owner_delete(hv->owner.id);
1789 [ # # ]: 0 : if (ret != 0)
1790 : 0 : return ret;
1791 : :
1792 : : return ret_stop;
1793 : : }
1794 : :
1795 : 0 : static int populate_cache_list(void)
1796 : : {
1797 : : int ret = 0;
1798 : : struct rte_devargs *da;
1799 : :
1800 : : rte_spinlock_lock(&netvsc_lock);
1801 : 0 : da_cache_usage++;
1802 [ # # ]: 0 : if (da_cache_usage > 1) {
1803 : : ret = 0;
1804 : 0 : goto out;
1805 : : }
1806 : :
1807 : 0 : LIST_INIT(&da_cache_list);
1808 [ # # ]: 0 : RTE_EAL_DEVARGS_FOREACH("pci", da) {
1809 : : struct da_cache *cache;
1810 : :
1811 : 0 : cache = calloc(1, sizeof(*cache) + strlen(da->drv_str) + 1);
1812 [ # # ]: 0 : if (!cache) {
1813 : : ret = -ENOMEM;
1814 : 0 : goto out;
1815 : : }
1816 : :
1817 [ # # ]: 0 : strlcpy(cache->name, da->name, sizeof(cache->name));
1818 : 0 : strlcpy(cache->drv_str, da->drv_str, strlen(da->drv_str) + 1);
1819 [ # # ]: 0 : LIST_INSERT_HEAD(&da_cache_list, cache, list);
1820 : : }
1821 : 0 : out:
1822 : : rte_spinlock_unlock(&netvsc_lock);
1823 : 0 : return ret;
1824 : : }
1825 : :
1826 : 0 : static void remove_cache_list(void)
1827 : : {
1828 : : struct da_cache *cache, *tmp;
1829 : :
1830 : : rte_spinlock_lock(&netvsc_lock);
1831 : 0 : da_cache_usage--;
1832 [ # # ]: 0 : if (da_cache_usage)
1833 : 0 : goto out;
1834 : :
1835 [ # # ]: 0 : LIST_FOREACH_SAFE(cache, &da_cache_list, list, tmp) {
1836 [ # # ]: 0 : LIST_REMOVE(cache, list);
1837 : 0 : free(cache);
1838 : : }
1839 : 0 : out:
1840 : : rte_spinlock_unlock(&netvsc_lock);
1841 : 0 : }
1842 : :
1843 : : static int
1844 : 0 : netvsc_mp_primary_handle(const struct rte_mp_msg *mp_msg __rte_unused,
1845 : : const void *peer __rte_unused)
1846 : : {
1847 : : /* Stub function required for multi-process message handling registration */
1848 : 0 : return 0;
1849 : : }
1850 : :
1851 : : static void
1852 : : mp_init_msg(struct rte_mp_msg *msg, enum netvsc_mp_req_type type, int vf_port)
1853 : : {
1854 : : struct netvsc_mp_param *param;
1855 : :
1856 : : strlcpy(msg->name, NETVSC_MP_NAME, sizeof(msg->name));
1857 : 0 : msg->len_param = sizeof(*param);
1858 : :
1859 : : param = (struct netvsc_mp_param *)msg->param;
1860 : 0 : param->type = type;
1861 : 0 : param->vf_port = vf_port;
1862 : : }
1863 : :
1864 : 0 : static int netvsc_secondary_handle_device_remove(int vf_port)
1865 : : {
1866 [ # # ]: 0 : if (!rte_eth_dev_is_valid_port(vf_port)) {
1867 : : /* VF not probed in this secondary — nothing to release */
1868 : 0 : PMD_DRV_LOG(DEBUG, "VF port %u not present in secondary, skipping",
1869 : : vf_port);
1870 : 0 : return 0;
1871 : : }
1872 : :
1873 : 0 : PMD_DRV_LOG(DEBUG, "Secondary releasing VF port %d", vf_port);
1874 : 0 : return rte_eth_dev_release_port(&rte_eth_devices[vf_port]);
1875 : : }
1876 : :
1877 : : static int
1878 : 0 : netvsc_mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
1879 : : {
1880 : 0 : struct rte_mp_msg mp_res = { 0 };
1881 : : struct netvsc_mp_param *res = (struct netvsc_mp_param *)mp_res.param;
1882 : : const struct netvsc_mp_param *param =
1883 : : (const struct netvsc_mp_param *)mp_msg->param;
1884 : : int ret = 0;
1885 : :
1886 [ # # ]: 0 : mp_init_msg(&mp_res, param->type, param->vf_port);
1887 : :
1888 [ # # ]: 0 : switch (param->type) {
1889 : 0 : case NETVSC_MP_REQ_VF_REMOVE:
1890 : 0 : res->result = netvsc_secondary_handle_device_remove(param->vf_port);
1891 : 0 : ret = rte_mp_reply(&mp_res, peer);
1892 : 0 : break;
1893 : :
1894 : 0 : default:
1895 : 0 : PMD_DRV_LOG(ERR, "Unknown primary MP type %u", param->type);
1896 : : ret = -EINVAL;
1897 : : }
1898 : :
1899 : 0 : return ret;
1900 : : }
1901 : :
1902 : 0 : static int netvsc_mp_init_primary(void)
1903 : : {
1904 : : int ret;
1905 : 0 : ret = rte_mp_action_register(NETVSC_MP_NAME, netvsc_mp_primary_handle);
1906 [ # # # # ]: 0 : if (ret && rte_errno != ENOTSUP) {
1907 : 0 : PMD_DRV_LOG(ERR, "Failed to register primary handler %d %d",
1908 : : ret, rte_errno);
1909 : 0 : return -1;
1910 : : }
1911 : :
1912 : : return 0;
1913 : : }
1914 : :
1915 : : static void netvsc_mp_uninit_primary(void)
1916 : : {
1917 : 0 : rte_mp_action_unregister(NETVSC_MP_NAME);
1918 : : }
1919 : :
1920 : : static int netvsc_mp_init_secondary(void)
1921 : : {
1922 : 0 : return rte_mp_action_register(NETVSC_MP_NAME, netvsc_mp_secondary_handle);
1923 : : }
1924 : :
1925 : : static void netvsc_mp_uninit_secondary(void)
1926 : : {
1927 : 0 : rte_mp_action_unregister(NETVSC_MP_NAME);
1928 : 0 : }
1929 : :
1930 : 0 : int netvsc_mp_req_vf(struct hn_data *hv, enum netvsc_mp_req_type type,
1931 : : int vf_port)
1932 : : {
1933 : 0 : struct rte_mp_msg mp_req = { 0 };
1934 : : struct rte_mp_msg *mp_res;
1935 : 0 : struct rte_mp_reply mp_rep = { 0 };
1936 : : struct netvsc_mp_param *res;
1937 : 0 : struct timespec ts = {.tv_sec = NETVSC_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
1938 : : int i, ret;
1939 : :
1940 : : /* if secondary count is 0, return */
1941 [ # # ]: 0 : if (rte_atomic_load_explicit(&netvsc_shared_data->secondary_cnt,
1942 : : rte_memory_order_acquire) == 0)
1943 : : return 0;
1944 : :
1945 : : mp_init_msg(&mp_req, type, vf_port);
1946 : :
1947 : 0 : ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
1948 [ # # ]: 0 : if (ret) {
1949 [ # # ]: 0 : if (rte_errno != ENOTSUP)
1950 : 0 : PMD_DRV_LOG(ERR, "port %u failed to request VF remove",
1951 : : hv->port_id);
1952 : : else
1953 : : ret = 0;
1954 : 0 : goto exit;
1955 : : }
1956 : :
1957 [ # # ]: 0 : if (mp_rep.nb_sent != mp_rep.nb_received) {
1958 : 0 : PMD_DRV_LOG(ERR, "port %u not all secondaries responded type %d",
1959 : : hv->port_id, type);
1960 : : ret = -1;
1961 : 0 : goto exit;
1962 : : }
1963 [ # # ]: 0 : for (i = 0; i < mp_rep.nb_received; i++) {
1964 : 0 : mp_res = &mp_rep.msgs[i];
1965 : : res = (struct netvsc_mp_param *)mp_res->param;
1966 [ # # ]: 0 : if (res->result) {
1967 : 0 : PMD_DRV_LOG(ERR, "port %u request failed on secondary %d",
1968 : : hv->port_id, i);
1969 : : ret = -1;
1970 : 0 : goto exit;
1971 : : }
1972 : : }
1973 : :
1974 : 0 : exit:
1975 : 0 : free(mp_rep.msgs);
1976 : 0 : return ret;
1977 : : }
1978 : :
1979 : 0 : static int netvsc_init_once(void)
1980 : : {
1981 : : int ret = 0;
1982 : : const struct rte_memzone *secondary_mz;
1983 : :
1984 [ # # ]: 0 : if (netvsc_local_data.init_done)
1985 : : return 0;
1986 : :
1987 [ # # # ]: 0 : switch (rte_eal_process_type()) {
1988 : 0 : case RTE_PROC_PRIMARY:
1989 : 0 : netvsc_shared_mz = rte_memzone_reserve(MZ_NETVSC_SHARED_DATA,
1990 : : sizeof(*netvsc_shared_data), SOCKET_ID_ANY, 0);
1991 [ # # ]: 0 : if (!netvsc_shared_mz) {
1992 : 0 : PMD_DRV_LOG(ERR, "Cannot allocate netvsc shared data");
1993 : 0 : return -rte_errno;
1994 : : }
1995 : 0 : netvsc_shared_data = netvsc_shared_mz->addr;
1996 : 0 : rte_atomic_store_explicit(&netvsc_shared_data->secondary_cnt,
1997 : : 0, rte_memory_order_release);
1998 : :
1999 : 0 : ret = netvsc_mp_init_primary();
2000 [ # # ]: 0 : if (ret) {
2001 : 0 : rte_memzone_free(netvsc_shared_mz);
2002 : 0 : netvsc_shared_mz = NULL;
2003 : 0 : netvsc_shared_data = NULL;
2004 : 0 : break;
2005 : : }
2006 : :
2007 : 0 : PMD_DRV_LOG(DEBUG, "MP INIT PRIMARY");
2008 : 0 : netvsc_local_data.init_done = true;
2009 : 0 : break;
2010 : :
2011 : 0 : case RTE_PROC_SECONDARY:
2012 : 0 : secondary_mz = rte_memzone_lookup(MZ_NETVSC_SHARED_DATA);
2013 [ # # ]: 0 : if (!secondary_mz) {
2014 : 0 : PMD_DRV_LOG(ERR, "Cannot attach netvsc shared data");
2015 : 0 : return -rte_errno;
2016 : : }
2017 : 0 : netvsc_shared_data = secondary_mz->addr;
2018 : : ret = netvsc_mp_init_secondary();
2019 [ # # ]: 0 : if (ret) {
2020 : 0 : netvsc_shared_data = NULL;
2021 : 0 : break;
2022 : : }
2023 : :
2024 : 0 : PMD_DRV_LOG(DEBUG, "MP INIT SECONDARY");
2025 : 0 : netvsc_local_data.init_done = true;
2026 : 0 : break;
2027 : :
2028 : : default:
2029 : : /* Impossible */
2030 : : ret = -EPROTO;
2031 : : break;
2032 : : }
2033 : :
2034 : : return ret;
2035 : : }
2036 : :
2037 : 0 : static void netvsc_uninit_once(void)
2038 : : {
2039 [ # # ]: 0 : if (netvsc_local_data.primary_cnt ||
2040 [ # # ]: 0 : netvsc_local_data.secondary_cnt)
2041 : : return;
2042 : :
2043 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2044 : : netvsc_mp_uninit_primary();
2045 : 0 : rte_memzone_free(netvsc_shared_mz);
2046 : 0 : netvsc_shared_mz = NULL;
2047 : 0 : netvsc_shared_data = NULL;
2048 : : } else {
2049 : : netvsc_mp_uninit_secondary();
2050 : : }
2051 : 0 : netvsc_local_data.init_done = false;
2052 : : }
2053 : :
2054 : 0 : static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
2055 : : struct rte_vmbus_device *dev)
2056 : : {
2057 : : struct rte_eth_dev *eth_dev;
2058 : : struct hn_nvs_process_priv *process_priv;
2059 : : int ret = 0;
2060 : :
2061 : 0 : PMD_INIT_FUNC_TRACE();
2062 : :
2063 : 0 : ret = populate_cache_list();
2064 [ # # ]: 0 : if (ret)
2065 : : return ret;
2066 : :
2067 : : rte_spinlock_lock(&netvsc_shared_data_lock);
2068 : 0 : ret = netvsc_init_once();
2069 [ # # ]: 0 : if (!ret) {
2070 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2071 : 0 : netvsc_local_data.primary_cnt++;
2072 : : } else {
2073 : 0 : rte_atomic_fetch_add_explicit(&netvsc_shared_data->secondary_cnt,
2074 : : 1, rte_memory_order_release);
2075 : 0 : netvsc_local_data.secondary_cnt++;
2076 : : }
2077 : : }
2078 : : rte_spinlock_unlock(&netvsc_shared_data_lock);
2079 [ # # ]: 0 : if (ret)
2080 : 0 : goto fail;
2081 : :
2082 : 0 : ret = rte_dev_event_monitor_start();
2083 [ # # ]: 0 : if (ret) {
2084 : 0 : PMD_DRV_LOG(ERR, "Failed to start device event monitoring");
2085 : 0 : goto init_once_failed;
2086 : : }
2087 : :
2088 : 0 : eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
2089 [ # # ]: 0 : if (!eth_dev) {
2090 : : ret = -ENOMEM;
2091 : 0 : goto vmbus_alloc_failed;
2092 : : }
2093 : :
2094 : 0 : process_priv = rte_zmalloc_socket("netvsc_proc_priv",
2095 : : sizeof(struct hn_nvs_process_priv),
2096 : : RTE_CACHE_LINE_SIZE,
2097 : : dev->device.numa_node);
2098 [ # # ]: 0 : if (!process_priv) {
2099 : : ret = -ENOMEM;
2100 : 0 : goto priv_alloc_failed;
2101 : : }
2102 : :
2103 : 0 : process_priv->vmbus_dev = dev;
2104 : 0 : eth_dev->process_private = process_priv;
2105 : :
2106 : 0 : ret = eth_hn_dev_init(eth_dev);
2107 [ # # ]: 0 : if (ret)
2108 : 0 : goto dev_init_failed;
2109 : :
2110 : 0 : rte_eth_dev_probing_finish(eth_dev);
2111 : :
2112 : 0 : return ret;
2113 : :
2114 : : dev_init_failed:
2115 : 0 : rte_free(process_priv);
2116 : :
2117 : 0 : priv_alloc_failed:
2118 : : eth_dev_vmbus_release(eth_dev);
2119 : :
2120 : 0 : vmbus_alloc_failed:
2121 : 0 : rte_dev_event_monitor_stop();
2122 : :
2123 : 0 : init_once_failed:
2124 : : rte_spinlock_lock(&netvsc_shared_data_lock);
2125 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2126 : 0 : netvsc_local_data.primary_cnt--;
2127 : : } else {
2128 : 0 : rte_atomic_fetch_sub_explicit(&netvsc_shared_data->secondary_cnt,
2129 : : 1, rte_memory_order_release);
2130 : 0 : netvsc_local_data.secondary_cnt--;
2131 : : }
2132 : 0 : netvsc_uninit_once();
2133 : : rte_spinlock_unlock(&netvsc_shared_data_lock);
2134 : :
2135 : 0 : fail:
2136 : 0 : remove_cache_list();
2137 : 0 : return ret;
2138 : : }
2139 : :
2140 : 0 : static int eth_hn_remove(struct rte_vmbus_device *dev)
2141 : : {
2142 : : struct rte_eth_dev *eth_dev;
2143 : : struct hn_nvs_process_priv *process_priv;
2144 : : int ret;
2145 : :
2146 : 0 : PMD_INIT_FUNC_TRACE();
2147 : :
2148 : 0 : eth_dev = rte_eth_dev_allocated(dev->device.name);
2149 [ # # ]: 0 : if (!eth_dev) {
2150 : : ret = 0; /* port already released */
2151 : 0 : goto uninit;
2152 : : }
2153 : :
2154 : 0 : ret = eth_hn_dev_uninit(eth_dev);
2155 [ # # ]: 0 : if (ret)
2156 : 0 : goto uninit;
2157 : :
2158 : 0 : process_priv = eth_dev->process_private;
2159 : 0 : rte_free(process_priv);
2160 : :
2161 : : eth_dev_vmbus_release(eth_dev);
2162 : 0 : rte_dev_event_monitor_stop();
2163 : :
2164 : 0 : remove_cache_list();
2165 : :
2166 : 0 : uninit:
2167 : : rte_spinlock_lock(&netvsc_shared_data_lock);
2168 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2169 : 0 : netvsc_local_data.primary_cnt--;
2170 : : } else {
2171 : 0 : rte_atomic_fetch_sub_explicit(&netvsc_shared_data->secondary_cnt,
2172 : : 1, rte_memory_order_release);
2173 : 0 : netvsc_local_data.secondary_cnt--;
2174 : : }
2175 : 0 : netvsc_uninit_once();
2176 : : rte_spinlock_unlock(&netvsc_shared_data_lock);
2177 : :
2178 : 0 : return ret;
2179 : : }
2180 : :
2181 : : /* Network device GUID */
2182 : : static const rte_uuid_t hn_net_ids[] = {
2183 : : /* f8615163-df3e-46c5-913f-f2d2f965ed0e */
2184 : : RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
2185 : : { 0 }
2186 : : };
2187 : :
2188 : : static struct rte_vmbus_driver rte_netvsc_pmd = {
2189 : : .id_table = hn_net_ids,
2190 : : .probe = eth_hn_probe,
2191 : : .remove = eth_hn_remove,
2192 : : };
2193 : :
2194 : 306 : RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
2195 : : RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
2196 [ - + ]: 306 : RTE_LOG_REGISTER_SUFFIX(hn_logtype_init, init, NOTICE);
2197 [ - + ]: 306 : RTE_LOG_REGISTER_SUFFIX(hn_logtype_driver, driver, NOTICE);
2198 : : RTE_PMD_REGISTER_PARAM_STRING(net_netvsc,
2199 : : NETVSC_ARG_LATENCY "=<uint32> "
2200 : : NETVSC_ARG_RXBREAK "=<uint32> "
2201 : : NETVSC_ARG_TXBREAK "=<uint32> "
2202 : : NETVSC_ARG_RX_EXTMBUF_ENABLE "=<0|1>");
|