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 : : #define HN_TX_OFFLOAD_CAPS (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | \
45 : : RTE_ETH_TX_OFFLOAD_TCP_CKSUM | \
46 : : RTE_ETH_TX_OFFLOAD_UDP_CKSUM | \
47 : : RTE_ETH_TX_OFFLOAD_TCP_TSO | \
48 : : RTE_ETH_TX_OFFLOAD_MULTI_SEGS | \
49 : : RTE_ETH_TX_OFFLOAD_VLAN_INSERT)
50 : :
51 : : #define HN_RX_OFFLOAD_CAPS (RTE_ETH_RX_OFFLOAD_CHECKSUM | \
52 : : RTE_ETH_RX_OFFLOAD_VLAN_STRIP | \
53 : : RTE_ETH_RX_OFFLOAD_RSS_HASH)
54 : :
55 : : #define NETVSC_ARG_LATENCY "latency"
56 : : #define NETVSC_ARG_RXBREAK "rx_copybreak"
57 : : #define NETVSC_ARG_TXBREAK "tx_copybreak"
58 : : #define NETVSC_ARG_RX_EXTMBUF_ENABLE "rx_extmbuf_enable"
59 : :
60 : : /* The max number of retry when hot adding a VF device */
61 : : #define NETVSC_MAX_HOTADD_RETRY 10
62 : :
63 : : struct hn_xstats_name_off {
64 : : char name[RTE_ETH_XSTATS_NAME_SIZE];
65 : : unsigned int offset;
66 : : };
67 : :
68 : : static const struct hn_xstats_name_off hn_stat_strings[] = {
69 : : { "good_packets", offsetof(struct hn_stats, packets) },
70 : : { "good_bytes", offsetof(struct hn_stats, bytes) },
71 : : { "errors", offsetof(struct hn_stats, errors) },
72 : : { "ring full", offsetof(struct hn_stats, ring_full) },
73 : : { "channel full", offsetof(struct hn_stats, channel_full) },
74 : : { "multicast_packets", offsetof(struct hn_stats, multicast) },
75 : : { "broadcast_packets", offsetof(struct hn_stats, broadcast) },
76 : : { "undersize_packets", offsetof(struct hn_stats, size_bins[0]) },
77 : : { "size_64_packets", offsetof(struct hn_stats, size_bins[1]) },
78 : : { "size_65_127_packets", offsetof(struct hn_stats, size_bins[2]) },
79 : : { "size_128_255_packets", offsetof(struct hn_stats, size_bins[3]) },
80 : : { "size_256_511_packets", offsetof(struct hn_stats, size_bins[4]) },
81 : : { "size_512_1023_packets", offsetof(struct hn_stats, size_bins[5]) },
82 : : { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
83 : : { "size_1519_max_packets", offsetof(struct hn_stats, size_bins[7]) },
84 : : };
85 : :
86 : : /* The default RSS key.
87 : : * This value is the same as MLX5 so that flows will be
88 : : * received on same path for both VF and synthetic NIC.
89 : : */
90 : : static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = {
91 : : 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7,
92 : : 0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94,
93 : : 0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1,
94 : : 0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59,
95 : : 0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a,
96 : : };
97 : :
98 : : static struct rte_eth_dev *
99 : 0 : eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
100 : : {
101 : : struct rte_eth_dev *eth_dev;
102 : : const char *name;
103 : :
104 [ # # ]: 0 : if (!dev)
105 : : return NULL;
106 : :
107 : 0 : name = dev->device.name;
108 : :
109 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
110 : 0 : eth_dev = rte_eth_dev_allocate(name);
111 [ # # ]: 0 : if (!eth_dev) {
112 : 0 : PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
113 : 0 : return NULL;
114 : : }
115 : :
116 [ # # ]: 0 : if (private_data_size) {
117 : 0 : eth_dev->data->dev_private =
118 : 0 : rte_zmalloc_socket(name, private_data_size,
119 : : RTE_CACHE_LINE_SIZE, dev->device.numa_node);
120 [ # # ]: 0 : if (!eth_dev->data->dev_private) {
121 : 0 : PMD_DRV_LOG(NOTICE, "can not allocate driver data");
122 : 0 : rte_eth_dev_release_port(eth_dev);
123 : 0 : return NULL;
124 : : }
125 : : }
126 : : } else {
127 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
128 [ # # ]: 0 : if (!eth_dev) {
129 : 0 : PMD_DRV_LOG(NOTICE, "can not attach secondary");
130 : 0 : return NULL;
131 : : }
132 : : }
133 : :
134 : 0 : eth_dev->device = &dev->device;
135 : :
136 : : /* interrupt is simulated */
137 : 0 : rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_EXT);
138 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
139 : 0 : eth_dev->intr_handle = dev->intr_handle;
140 : :
141 : 0 : return eth_dev;
142 : : }
143 : :
144 : : static void
145 : : eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
146 : : {
147 : : /* free ether device */
148 : 0 : rte_eth_dev_release_port(eth_dev);
149 : :
150 : 0 : eth_dev->device = NULL;
151 : 0 : eth_dev->intr_handle = NULL;
152 : : }
153 : :
154 : 0 : static int hn_set_parameter(const char *key, const char *value, void *opaque)
155 : : {
156 : : struct hn_data *hv = opaque;
157 : 0 : char *endp = NULL;
158 : : unsigned long v;
159 : :
160 : 0 : v = strtoul(value, &endp, 0);
161 [ # # # # ]: 0 : if (*value == '\0' || *endp != '\0') {
162 : 0 : PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
163 : 0 : return -EINVAL;
164 : : }
165 : :
166 [ # # ]: 0 : if (!strcmp(key, NETVSC_ARG_LATENCY)) {
167 : : /* usec to nsec */
168 : 0 : hv->latency = v * 1000;
169 : 0 : PMD_DRV_LOG(DEBUG, "set latency %u usec", hv->latency);
170 [ # # ]: 0 : } else if (!strcmp(key, NETVSC_ARG_RXBREAK)) {
171 : 0 : hv->rx_copybreak = v;
172 : 0 : PMD_DRV_LOG(DEBUG, "rx copy break set to %u",
173 : : hv->rx_copybreak);
174 [ # # ]: 0 : } else if (!strcmp(key, NETVSC_ARG_TXBREAK)) {
175 : 0 : hv->tx_copybreak = v;
176 : 0 : PMD_DRV_LOG(DEBUG, "tx copy break set to %u",
177 : : hv->tx_copybreak);
178 [ # # ]: 0 : } else if (!strcmp(key, NETVSC_ARG_RX_EXTMBUF_ENABLE)) {
179 : 0 : hv->rx_extmbuf_enable = v;
180 : 0 : PMD_DRV_LOG(DEBUG, "rx extmbuf enable set to %u",
181 : : hv->rx_extmbuf_enable);
182 : : }
183 : :
184 : : return 0;
185 : : }
186 : :
187 : : /* Parse device arguments */
188 : 0 : static int hn_parse_args(const struct rte_eth_dev *dev)
189 : : {
190 : 0 : struct hn_data *hv = dev->data->dev_private;
191 : 0 : struct rte_devargs *devargs = dev->device->devargs;
192 : : static const char * const valid_keys[] = {
193 : : NETVSC_ARG_LATENCY,
194 : : NETVSC_ARG_RXBREAK,
195 : : NETVSC_ARG_TXBREAK,
196 : : NETVSC_ARG_RX_EXTMBUF_ENABLE,
197 : : NULL
198 : : };
199 : : struct rte_kvargs *kvlist;
200 : : int ret;
201 : :
202 [ # # ]: 0 : if (!devargs)
203 : : return 0;
204 : :
205 : 0 : PMD_INIT_LOG(DEBUG, "device args %s %s",
206 : : devargs->name, devargs->args);
207 : :
208 : 0 : kvlist = rte_kvargs_parse(devargs->args, valid_keys);
209 [ # # ]: 0 : if (!kvlist) {
210 : 0 : PMD_DRV_LOG(ERR, "invalid parameters");
211 : 0 : return -EINVAL;
212 : : }
213 : :
214 : 0 : ret = rte_kvargs_process(kvlist, NULL, hn_set_parameter, hv);
215 : 0 : rte_kvargs_free(kvlist);
216 : :
217 : 0 : return ret;
218 : : }
219 : :
220 : : /* Update link status.
221 : : * Note: the DPDK definition of "wait_to_complete"
222 : : * means block this call until link is up.
223 : : * which is not worth supporting.
224 : : */
225 : : int
226 : 0 : hn_dev_link_update(struct rte_eth_dev *dev,
227 : : int wait_to_complete __rte_unused)
228 : : {
229 : 0 : struct hn_data *hv = dev->data->dev_private;
230 : : struct rte_eth_link link, old;
231 : : int error;
232 : :
233 : 0 : old = dev->data->dev_link;
234 : :
235 : 0 : error = hn_rndis_get_linkstatus(hv);
236 [ # # ]: 0 : if (error)
237 : : return error;
238 : :
239 : 0 : hn_rndis_get_linkspeed(hv);
240 : :
241 : 0 : link = (struct rte_eth_link) {
242 : : .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
243 : : .link_autoneg = RTE_ETH_LINK_SPEED_FIXED,
244 : 0 : .link_speed = hv->link_speed / 10000,
245 : : };
246 : :
247 [ # # ]: 0 : if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
248 : 0 : link.link_status = RTE_ETH_LINK_UP;
249 : : else
250 : : link.link_status = RTE_ETH_LINK_DOWN;
251 : :
252 [ # # ]: 0 : if (old.link_status == link.link_status)
253 : : return 0;
254 : :
255 [ # # ]: 0 : PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
256 : : (link.link_status == RTE_ETH_LINK_UP) ? "up" : "down");
257 : :
258 : : return rte_eth_linkstatus_set(dev, &link);
259 : : }
260 : :
261 : 0 : static int hn_dev_info_get(struct rte_eth_dev *dev,
262 : : struct rte_eth_dev_info *dev_info)
263 : : {
264 : 0 : struct hn_data *hv = dev->data->dev_private;
265 : : int rc;
266 : :
267 : 0 : dev_info->speed_capa = RTE_ETH_LINK_SPEED_10G;
268 : 0 : dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
269 : 0 : dev_info->max_rx_pktlen = HN_MAX_XFER_LEN;
270 : 0 : dev_info->max_mac_addrs = 1;
271 : :
272 : 0 : dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
273 : 0 : dev_info->flow_type_rss_offloads = hv->rss_offloads;
274 : 0 : dev_info->reta_size = RTE_ETH_RSS_RETA_SIZE_128;
275 : :
276 : 0 : dev_info->max_rx_queues = hv->max_queues;
277 : 0 : dev_info->max_tx_queues = hv->max_queues;
278 : :
279 : 0 : dev_info->tx_desc_lim.nb_min = 1;
280 : 0 : dev_info->tx_desc_lim.nb_max = 4096;
281 : :
282 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
283 : : return 0;
284 : :
285 : : /* fills in rx and tx offload capability */
286 : 0 : rc = hn_rndis_get_offload(hv, dev_info);
287 [ # # ]: 0 : if (rc != 0)
288 : : return rc;
289 : :
290 : : /* merges the offload and queues of vf */
291 : 0 : return hn_vf_info_get(hv, dev_info);
292 : : }
293 : :
294 : 0 : static int hn_rss_reta_update(struct rte_eth_dev *dev,
295 : : struct rte_eth_rss_reta_entry64 *reta_conf,
296 : : uint16_t reta_size)
297 : : {
298 : 0 : struct hn_data *hv = dev->data->dev_private;
299 : : unsigned int i;
300 : : int err;
301 : :
302 : 0 : PMD_INIT_FUNC_TRACE();
303 : :
304 [ # # ]: 0 : if (reta_size != NDIS_HASH_INDCNT) {
305 : 0 : PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
306 : 0 : return -EINVAL;
307 : : }
308 : :
309 [ # # ]: 0 : for (i = 0; i < NDIS_HASH_INDCNT; i++) {
310 : 0 : uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
311 : 0 : uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
312 : 0 : uint64_t mask = (uint64_t)1 << shift;
313 : :
314 [ # # ]: 0 : if (reta_conf[idx].mask & mask)
315 : 0 : hv->rss_ind[i] = reta_conf[idx].reta[shift];
316 : :
317 : : /*
318 : : * Ensure we don't allow config that directs traffic to an Rx
319 : : * queue that we aren't going to poll
320 : : */
321 [ # # ]: 0 : if (hv->rss_ind[i] >= dev->data->nb_rx_queues) {
322 : 0 : PMD_DRV_LOG(ERR, "RSS distributing traffic to invalid Rx queue");
323 : 0 : return -EINVAL;
324 : : }
325 : : }
326 : :
327 : 0 : err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
328 [ # # ]: 0 : if (err) {
329 : 0 : PMD_DRV_LOG(NOTICE,
330 : : "rss disable failed");
331 : 0 : return err;
332 : : }
333 : :
334 : 0 : err = hn_rndis_conf_rss(hv, 0);
335 [ # # ]: 0 : if (err) {
336 : 0 : PMD_DRV_LOG(NOTICE,
337 : : "reta reconfig failed");
338 : 0 : return err;
339 : : }
340 : :
341 : 0 : return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
342 : : }
343 : :
344 : 0 : static int hn_rss_reta_query(struct rte_eth_dev *dev,
345 : : struct rte_eth_rss_reta_entry64 *reta_conf,
346 : : uint16_t reta_size)
347 : : {
348 : 0 : struct hn_data *hv = dev->data->dev_private;
349 : : unsigned int i;
350 : :
351 : 0 : PMD_INIT_FUNC_TRACE();
352 : :
353 [ # # ]: 0 : if (reta_size != NDIS_HASH_INDCNT) {
354 : 0 : PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
355 : 0 : return -EINVAL;
356 : : }
357 : :
358 [ # # ]: 0 : for (i = 0; i < NDIS_HASH_INDCNT; i++) {
359 : 0 : uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
360 : 0 : uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
361 : 0 : uint64_t mask = (uint64_t)1 << shift;
362 : :
363 [ # # ]: 0 : if (reta_conf[idx].mask & mask)
364 : 0 : reta_conf[idx].reta[shift] = hv->rss_ind[i];
365 : : }
366 : : return 0;
367 : : }
368 : :
369 : 0 : static void hn_rss_hash_init(struct hn_data *hv,
370 : : const struct rte_eth_rss_conf *rss_conf)
371 : : {
372 : : /* Convert from DPDK RSS hash flags to NDIS hash flags */
373 : 0 : hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
374 : :
375 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV4)
376 : 0 : hv->rss_hash |= NDIS_HASH_IPV4;
377 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
378 : 0 : hv->rss_hash |= NDIS_HASH_TCP_IPV4;
379 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6)
380 : 0 : hv->rss_hash |= NDIS_HASH_IPV6;
381 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_EX)
382 : 0 : hv->rss_hash |= NDIS_HASH_IPV6_EX;
383 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_TCP)
384 : 0 : hv->rss_hash |= NDIS_HASH_TCP_IPV6;
385 [ # # ]: 0 : if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_TCP_EX)
386 : 0 : hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
387 : :
388 [ # # ]: 0 : memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
389 : : NDIS_HASH_KEYSIZE_TOEPLITZ);
390 : 0 : }
391 : :
392 : 0 : static int hn_rss_hash_update(struct rte_eth_dev *dev,
393 : : struct rte_eth_rss_conf *rss_conf)
394 : : {
395 : 0 : struct hn_data *hv = dev->data->dev_private;
396 : : int err;
397 : :
398 : 0 : PMD_INIT_FUNC_TRACE();
399 : :
400 : 0 : err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
401 [ # # ]: 0 : if (err) {
402 : 0 : PMD_DRV_LOG(NOTICE,
403 : : "rss disable failed");
404 : 0 : return err;
405 : : }
406 : :
407 : 0 : hn_rss_hash_init(hv, rss_conf);
408 : :
409 [ # # ]: 0 : if (rss_conf->rss_hf != 0) {
410 : 0 : err = hn_rndis_conf_rss(hv, 0);
411 [ # # ]: 0 : if (err) {
412 : 0 : PMD_DRV_LOG(NOTICE,
413 : : "rss reconfig failed (RSS disabled)");
414 : 0 : return err;
415 : : }
416 : : }
417 : :
418 : 0 : return hn_vf_rss_hash_update(dev, rss_conf);
419 : : }
420 : :
421 : 0 : static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
422 : : struct rte_eth_rss_conf *rss_conf)
423 : : {
424 : 0 : struct hn_data *hv = dev->data->dev_private;
425 : :
426 : 0 : PMD_INIT_FUNC_TRACE();
427 : :
428 [ # # ]: 0 : if (hv->ndis_ver < NDIS_VERSION_6_20) {
429 : 0 : PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
430 : 0 : return -EOPNOTSUPP;
431 : : }
432 : :
433 : 0 : rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ;
434 [ # # ]: 0 : if (rss_conf->rss_key)
435 : 0 : memcpy(rss_conf->rss_key, hv->rss_key,
436 : : NDIS_HASH_KEYSIZE_TOEPLITZ);
437 : :
438 : 0 : rss_conf->rss_hf = 0;
439 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_IPV4)
440 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV4;
441 : :
442 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
443 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_TCP;
444 : :
445 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_IPV6)
446 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV6;
447 : :
448 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_IPV6_EX)
449 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_EX;
450 : :
451 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
452 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_TCP;
453 : :
454 [ # # ]: 0 : if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
455 : 0 : rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_TCP_EX;
456 : :
457 : : return 0;
458 : : }
459 : :
460 : : static int
461 : 0 : hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
462 : : {
463 : 0 : struct hn_data *hv = dev->data->dev_private;
464 : :
465 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
466 : 0 : return hn_vf_promiscuous_enable(dev);
467 : : }
468 : :
469 : : static int
470 : 0 : hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
471 : : {
472 : 0 : struct hn_data *hv = dev->data->dev_private;
473 : : uint32_t filter;
474 : :
475 : : filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
476 [ # # ]: 0 : if (dev->data->all_multicast)
477 : : filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
478 : 0 : hn_rndis_set_rxfilter(hv, filter);
479 : 0 : return hn_vf_promiscuous_disable(dev);
480 : : }
481 : :
482 : : static int
483 : 0 : hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
484 : : {
485 : 0 : struct hn_data *hv = dev->data->dev_private;
486 : :
487 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
488 : : NDIS_PACKET_TYPE_ALL_MULTICAST |
489 : : NDIS_PACKET_TYPE_BROADCAST);
490 : 0 : return hn_vf_allmulticast_enable(dev);
491 : : }
492 : :
493 : : static int
494 : 0 : hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
495 : : {
496 : 0 : struct hn_data *hv = dev->data->dev_private;
497 : :
498 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
499 : : NDIS_PACKET_TYPE_BROADCAST);
500 : 0 : return hn_vf_allmulticast_disable(dev);
501 : : }
502 : :
503 : : static int
504 : 0 : hn_dev_mc_addr_list(struct rte_eth_dev *dev,
505 : : struct rte_ether_addr *mc_addr_set,
506 : : uint32_t nb_mc_addr)
507 : : {
508 : : /* No filtering on the synthetic path, but can do it on VF */
509 : 0 : return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
510 : : }
511 : :
512 : : /* Setup shared rx/tx queue data */
513 : 0 : static int hn_subchan_configure(struct hn_data *hv,
514 : : uint32_t subchan)
515 : : {
516 : : struct vmbus_channel *primary = hn_primary_chan(hv);
517 : : int err;
518 : : unsigned int retry = 0;
519 : :
520 : 0 : PMD_DRV_LOG(DEBUG,
521 : : "open %u subchannels", subchan);
522 : :
523 : : /* Send create sub channels command */
524 : 0 : err = hn_nvs_alloc_subchans(hv, &subchan);
525 [ # # ]: 0 : if (err)
526 : : return err;
527 : :
528 [ # # ]: 0 : while (subchan > 0) {
529 : : struct vmbus_channel *new_sc;
530 : : uint16_t chn_index;
531 : :
532 : 0 : err = rte_vmbus_subchan_open(primary, &new_sc);
533 [ # # # # ]: 0 : if (err == -ENOENT && ++retry < 1000) {
534 : : /* This can happen if not ready yet */
535 : : rte_delay_ms(10);
536 : 0 : continue;
537 : : }
538 : :
539 [ # # ]: 0 : if (err) {
540 : 0 : PMD_DRV_LOG(ERR,
541 : : "open subchannel failed: %d", err);
542 : 0 : return err;
543 : : }
544 : :
545 : 0 : rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
546 : :
547 : : retry = 0;
548 : 0 : chn_index = rte_vmbus_sub_channel_index(new_sc);
549 [ # # # # ]: 0 : if (chn_index == 0 || chn_index > hv->max_queues) {
550 : 0 : PMD_DRV_LOG(ERR,
551 : : "Invalid subchannel offermsg channel %u",
552 : : chn_index);
553 : 0 : return -EIO;
554 : : }
555 : :
556 : 0 : PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
557 : 0 : hv->channels[chn_index] = new_sc;
558 : 0 : --subchan;
559 : : }
560 : :
561 : : return err;
562 : : }
563 : :
564 : 0 : static void netvsc_hotplug_retry(void *args)
565 : : {
566 : : int ret;
567 : : struct hv_hotadd_context *hot_ctx = args;
568 : 0 : struct hn_data *hv = hot_ctx->hv;
569 : 0 : struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
570 : : struct rte_devargs *d = &hot_ctx->da;
571 : : char buf[256];
572 : :
573 : : DIR *di = NULL;
574 : : struct dirent *dir;
575 : : struct ifreq req;
576 : : struct rte_ether_addr eth_addr;
577 : : int s;
578 : :
579 : 0 : PMD_DRV_LOG(DEBUG, "%s: retry count %d",
580 : : __func__, hot_ctx->eal_hot_plug_retry);
581 : :
582 [ # # ]: 0 : if (hot_ctx->eal_hot_plug_retry++ > NETVSC_MAX_HOTADD_RETRY) {
583 : 0 : PMD_DRV_LOG(NOTICE, "Failed to parse PCI device retry=%d",
584 : : hot_ctx->eal_hot_plug_retry);
585 : 0 : goto free_hotadd_ctx;
586 : : }
587 : :
588 : 0 : snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/net", d->name);
589 : 0 : di = opendir(buf);
590 [ # # ]: 0 : if (!di) {
591 : 0 : PMD_DRV_LOG(DEBUG, "%s: can't open directory %s, "
592 : : "retrying in 1 second", __func__, buf);
593 : : /* The device is still being initialized, retry after 1 second */
594 : 0 : rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hot_ctx);
595 : 0 : return;
596 : : }
597 : :
598 [ # # ]: 0 : while ((dir = readdir(di))) {
599 : : /* Skip . and .. directories */
600 [ # # # # ]: 0 : if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
601 : 0 : continue;
602 : :
603 : : /* trying to get mac address if this is a network device*/
604 : 0 : s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
605 [ # # ]: 0 : if (s == -1) {
606 : 0 : PMD_DRV_LOG(ERR, "Failed to create socket errno %d",
607 : : errno);
608 : 0 : break;
609 : : }
610 : : strlcpy(req.ifr_name, dir->d_name, sizeof(req.ifr_name));
611 : 0 : ret = ioctl(s, SIOCGIFHWADDR, &req);
612 : 0 : close(s);
613 [ # # ]: 0 : if (ret == -1) {
614 : 0 : PMD_DRV_LOG(ERR,
615 : : "Failed to send SIOCGIFHWADDR for device %s",
616 : : dir->d_name);
617 : 0 : break;
618 : : }
619 [ # # ]: 0 : if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER)
620 : 0 : continue;
621 : :
622 : : memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
623 : : RTE_DIM(eth_addr.addr_bytes));
624 : :
625 [ # # ]: 0 : if (rte_is_same_ether_addr(ð_addr, dev->data->mac_addrs)) {
626 : 0 : PMD_DRV_LOG(NOTICE,
627 : : "Found matching MAC address, adding device %s network name %s",
628 : : d->name, dir->d_name);
629 : :
630 : : /* If this device has been hot removed from this
631 : : * parent device, restore its args.
632 : : */
633 : 0 : ret = rte_eal_hotplug_add(d->bus->name, d->name,
634 [ # # ]: 0 : hv->vf_devargs ?
635 : : hv->vf_devargs : "");
636 [ # # ]: 0 : if (ret) {
637 : 0 : PMD_DRV_LOG(ERR,
638 : : "Failed to add PCI device %s",
639 : : d->name);
640 : : }
641 : :
642 : : break;
643 : : }
644 : : }
645 : :
646 : 0 : free_hotadd_ctx:
647 : : if (di)
648 : 0 : closedir(di);
649 : :
650 : 0 : rte_spinlock_lock(&hv->hotadd_lock);
651 [ # # ]: 0 : LIST_REMOVE(hot_ctx, list);
652 : : rte_spinlock_unlock(&hv->hotadd_lock);
653 : :
654 : 0 : rte_free(hot_ctx);
655 : : }
656 : :
657 : : static void
658 : 0 : netvsc_hotadd_callback(const char *device_name, enum rte_dev_event_type type,
659 : : void *arg)
660 : : {
661 : : struct hn_data *hv = arg;
662 : : struct hv_hotadd_context *hot_ctx;
663 : : struct rte_devargs *d;
664 : : int ret;
665 : :
666 : 0 : PMD_DRV_LOG(INFO, "Device notification type=%d device_name=%s",
667 : : type, device_name);
668 : :
669 [ # # ]: 0 : switch (type) {
670 : 0 : case RTE_DEV_EVENT_ADD:
671 : : /* if we already has a VF, don't check on hot add */
672 [ # # ]: 0 : if (hv->vf_ctx.vf_state > vf_removed)
673 : : break;
674 : :
675 : 0 : hot_ctx = rte_zmalloc("NETVSC-HOTADD", sizeof(*hot_ctx),
676 : 0 : rte_mem_page_size());
677 : :
678 [ # # ]: 0 : if (!hot_ctx) {
679 : 0 : PMD_DRV_LOG(ERR, "Failed to allocate hotadd context");
680 : 0 : return;
681 : : }
682 : :
683 : 0 : hot_ctx->hv = hv;
684 : 0 : d = &hot_ctx->da;
685 : :
686 : 0 : ret = rte_devargs_parse(d, device_name);
687 [ # # ]: 0 : if (ret) {
688 : 0 : PMD_DRV_LOG(ERR,
689 : : "devargs parsing failed ret=%d", ret);
690 : 0 : goto free_ctx;
691 : : }
692 : :
693 [ # # ]: 0 : if (!strcmp(d->bus->name, "pci")) {
694 : : /* Start the process of figuring out if this
695 : : * PCI device is a VF device
696 : : */
697 : 0 : rte_spinlock_lock(&hv->hotadd_lock);
698 [ # # ]: 0 : LIST_INSERT_HEAD(&hv->hotadd_list, hot_ctx, list);
699 : : rte_spinlock_unlock(&hv->hotadd_lock);
700 : 0 : rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hot_ctx);
701 : 0 : return;
702 : : }
703 : :
704 : : /* We will switch to VF on RDNIS configure message
705 : : * sent from VSP
706 : : */
707 : 0 : free_ctx:
708 : 0 : rte_free(hot_ctx);
709 : 0 : break;
710 : :
711 : : default:
712 : : break;
713 : : }
714 : : }
715 : :
716 : 0 : static int hn_dev_configure(struct rte_eth_dev *dev)
717 : : {
718 : 0 : struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
719 : 0 : struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
720 : : const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
721 : : const struct rte_eth_txmode *txmode = &dev_conf->txmode;
722 : 0 : struct hn_data *hv = dev->data->dev_private;
723 : : uint64_t unsupported;
724 : : int i, err, subchan;
725 : :
726 : 0 : PMD_INIT_FUNC_TRACE();
727 : :
728 [ # # ]: 0 : if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG)
729 : 0 : dev_conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
730 : :
731 : 0 : unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
732 [ # # ]: 0 : if (unsupported) {
733 : 0 : PMD_DRV_LOG(NOTICE,
734 : : "unsupported TX offload: %#" PRIx64,
735 : : unsupported);
736 : 0 : return -EINVAL;
737 : : }
738 : :
739 : 0 : unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
740 [ # # ]: 0 : if (unsupported) {
741 : 0 : PMD_DRV_LOG(NOTICE,
742 : : "unsupported RX offload: %#" PRIx64,
743 : : rxmode->offloads);
744 : 0 : return -EINVAL;
745 : : }
746 : :
747 : 0 : hv->vlan_strip = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
748 : :
749 : 0 : err = hn_rndis_conf_offload(hv, txmode->offloads,
750 : : rxmode->offloads);
751 [ # # ]: 0 : if (err) {
752 : 0 : PMD_DRV_LOG(NOTICE,
753 : : "offload configure failed");
754 : 0 : return err;
755 : : }
756 : :
757 : 0 : hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
758 : : dev->data->nb_tx_queues);
759 : :
760 [ # # ]: 0 : for (i = 0; i < NDIS_HASH_INDCNT; i++)
761 : 0 : hv->rss_ind[i] = i % dev->data->nb_rx_queues;
762 : :
763 : 0 : hn_rss_hash_init(hv, rss_conf);
764 : :
765 : 0 : subchan = hv->num_queues - 1;
766 [ # # ]: 0 : if (subchan > 0) {
767 : 0 : err = hn_subchan_configure(hv, subchan);
768 [ # # ]: 0 : if (err) {
769 : 0 : PMD_DRV_LOG(NOTICE,
770 : : "subchannel configuration failed");
771 : 0 : return err;
772 : : }
773 : :
774 : 0 : err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
775 [ # # ]: 0 : if (err) {
776 : 0 : PMD_DRV_LOG(NOTICE,
777 : : "rss disable failed");
778 : 0 : return err;
779 : : }
780 : :
781 [ # # ]: 0 : if (rss_conf->rss_hf != 0) {
782 : 0 : err = hn_rndis_conf_rss(hv, 0);
783 [ # # ]: 0 : if (err) {
784 : 0 : PMD_DRV_LOG(NOTICE,
785 : : "initial RSS config failed");
786 : 0 : return err;
787 : : }
788 : : }
789 : : }
790 : :
791 : 0 : return hn_vf_configure_locked(dev, dev_conf);
792 : : }
793 : :
794 : 0 : static int hn_dev_stats_get(struct rte_eth_dev *dev,
795 : : struct rte_eth_stats *stats)
796 : : {
797 : : unsigned int i;
798 : :
799 : 0 : hn_vf_stats_get(dev, stats);
800 : :
801 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
802 : 0 : const struct hn_tx_queue *txq = dev->data->tx_queues[i];
803 : :
804 [ # # ]: 0 : if (!txq)
805 : 0 : continue;
806 : :
807 : 0 : stats->opackets += txq->stats.packets;
808 : 0 : stats->obytes += txq->stats.bytes;
809 : 0 : stats->oerrors += txq->stats.errors;
810 : :
811 [ # # ]: 0 : if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
812 : 0 : stats->q_opackets[i] = txq->stats.packets;
813 : 0 : stats->q_obytes[i] = txq->stats.bytes;
814 : : }
815 : : }
816 : :
817 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
818 : 0 : const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
819 : :
820 [ # # ]: 0 : if (!rxq)
821 : 0 : continue;
822 : :
823 : 0 : stats->ipackets += rxq->stats.packets;
824 : 0 : stats->ibytes += rxq->stats.bytes;
825 : 0 : stats->ierrors += rxq->stats.errors;
826 : 0 : stats->imissed += rxq->stats.ring_full;
827 : :
828 [ # # ]: 0 : if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
829 : 0 : stats->q_ipackets[i] = rxq->stats.packets;
830 : 0 : stats->q_ibytes[i] = rxq->stats.bytes;
831 : : }
832 : : }
833 : :
834 : 0 : stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
835 : 0 : return 0;
836 : : }
837 : :
838 : : static int
839 : 0 : hn_dev_stats_reset(struct rte_eth_dev *dev)
840 : : {
841 : : unsigned int i;
842 : :
843 : 0 : PMD_INIT_FUNC_TRACE();
844 : :
845 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
846 : 0 : struct hn_tx_queue *txq = dev->data->tx_queues[i];
847 : :
848 [ # # ]: 0 : if (!txq)
849 : 0 : continue;
850 : 0 : memset(&txq->stats, 0, sizeof(struct hn_stats));
851 : : }
852 : :
853 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
854 : 0 : struct hn_rx_queue *rxq = dev->data->rx_queues[i];
855 : :
856 [ # # ]: 0 : if (!rxq)
857 : 0 : continue;
858 : :
859 : 0 : memset(&rxq->stats, 0, sizeof(struct hn_stats));
860 : : }
861 : :
862 : 0 : return 0;
863 : : }
864 : :
865 : : static int
866 : 0 : hn_dev_xstats_reset(struct rte_eth_dev *dev)
867 : : {
868 : : int ret;
869 : :
870 : 0 : ret = hn_dev_stats_reset(dev);
871 [ # # ]: 0 : if (ret != 0)
872 : : return 0;
873 : :
874 : 0 : return hn_vf_xstats_reset(dev);
875 : : }
876 : :
877 : : static int
878 : 0 : hn_dev_xstats_count(struct rte_eth_dev *dev)
879 : : {
880 : : int ret, count;
881 : :
882 : 0 : count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
883 : 0 : count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
884 : :
885 : 0 : ret = hn_vf_xstats_get_names(dev, NULL, 0);
886 [ # # ]: 0 : if (ret < 0)
887 : : return ret;
888 : :
889 : 0 : return count + ret;
890 : : }
891 : :
892 : : static int
893 : 0 : hn_dev_xstats_get_names(struct rte_eth_dev *dev,
894 : : struct rte_eth_xstat_name *xstats_names,
895 : : unsigned int limit)
896 : : {
897 : : unsigned int i, t, count = 0;
898 : : int ret;
899 : :
900 [ # # ]: 0 : if (!xstats_names)
901 : 0 : return hn_dev_xstats_count(dev);
902 : :
903 : : /* Note: limit checked in rte_eth_xstats_names() */
904 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
905 : 0 : const struct hn_tx_queue *txq = dev->data->tx_queues[i];
906 : :
907 [ # # ]: 0 : if (!txq)
908 : 0 : continue;
909 : :
910 [ # # ]: 0 : if (count >= limit)
911 : : break;
912 : :
913 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
914 : 0 : snprintf(xstats_names[count++].name,
915 : : RTE_ETH_XSTATS_NAME_SIZE,
916 : 0 : "tx_q%u_%s", i, hn_stat_strings[t].name);
917 : : }
918 : :
919 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
920 : 0 : const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
921 : :
922 [ # # ]: 0 : if (!rxq)
923 : 0 : continue;
924 : :
925 [ # # ]: 0 : if (count >= limit)
926 : : break;
927 : :
928 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
929 : 0 : snprintf(xstats_names[count++].name,
930 : : RTE_ETH_XSTATS_NAME_SIZE,
931 : : "rx_q%u_%s", i,
932 : 0 : hn_stat_strings[t].name);
933 : : }
934 : :
935 : 0 : ret = hn_vf_xstats_get_names(dev, xstats_names + count,
936 : : limit - count);
937 [ # # ]: 0 : if (ret < 0)
938 : : return ret;
939 : :
940 : 0 : return count + ret;
941 : : }
942 : :
943 : : static int
944 : 0 : hn_dev_xstats_get(struct rte_eth_dev *dev,
945 : : struct rte_eth_xstat *xstats,
946 : : unsigned int n)
947 : : {
948 : : unsigned int i, t, count = 0;
949 : 0 : const unsigned int nstats = hn_dev_xstats_count(dev);
950 : : const char *stats;
951 : : int ret;
952 : :
953 : 0 : PMD_INIT_FUNC_TRACE();
954 : :
955 [ # # ]: 0 : if (n < nstats)
956 : : return nstats;
957 : :
958 [ # # ]: 0 : for (i = 0; i < dev->data->nb_tx_queues; i++) {
959 : 0 : const struct hn_tx_queue *txq = dev->data->tx_queues[i];
960 : :
961 [ # # ]: 0 : if (!txq)
962 : 0 : continue;
963 : :
964 : 0 : stats = (const char *)&txq->stats;
965 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
966 : 0 : xstats[count].id = count;
967 : 0 : xstats[count].value = *(const uint64_t *)
968 : 0 : (stats + hn_stat_strings[t].offset);
969 : : }
970 : : }
971 : :
972 [ # # ]: 0 : for (i = 0; i < dev->data->nb_rx_queues; i++) {
973 : 0 : const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
974 : :
975 [ # # ]: 0 : if (!rxq)
976 : 0 : continue;
977 : :
978 : 0 : stats = (const char *)&rxq->stats;
979 [ # # ]: 0 : for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
980 : 0 : xstats[count].id = count;
981 : 0 : xstats[count].value = *(const uint64_t *)
982 : 0 : (stats + hn_stat_strings[t].offset);
983 : : }
984 : : }
985 : :
986 : 0 : ret = hn_vf_xstats_get(dev, xstats, count, n);
987 [ # # ]: 0 : if (ret < 0)
988 : : return ret;
989 : :
990 : 0 : return count + ret;
991 : : }
992 : :
993 : : static int
994 : 0 : hn_dev_start(struct rte_eth_dev *dev)
995 : : {
996 : 0 : struct hn_data *hv = dev->data->dev_private;
997 : : int i, error;
998 : :
999 : 0 : PMD_INIT_FUNC_TRACE();
1000 : :
1001 : : /* Register to monitor hot plug events */
1002 : 0 : error = rte_dev_event_callback_register(NULL, netvsc_hotadd_callback,
1003 : : hv);
1004 [ # # ]: 0 : if (error) {
1005 : 0 : PMD_DRV_LOG(ERR, "failed to register device event callback");
1006 : 0 : return error;
1007 : : }
1008 : :
1009 : 0 : error = hn_rndis_set_rxfilter(hv,
1010 : : NDIS_PACKET_TYPE_BROADCAST |
1011 : : NDIS_PACKET_TYPE_ALL_MULTICAST |
1012 : : NDIS_PACKET_TYPE_DIRECTED);
1013 [ # # ]: 0 : if (error)
1014 : : return error;
1015 : :
1016 : 0 : error = hn_vf_start(dev);
1017 [ # # ]: 0 : if (error)
1018 : 0 : hn_rndis_set_rxfilter(hv, 0);
1019 : :
1020 : : /* Initialize Link state */
1021 [ # # ]: 0 : if (error == 0)
1022 : 0 : hn_dev_link_update(dev, 0);
1023 : :
1024 [ # # ]: 0 : for (i = 0; i < hv->num_queues; i++) {
1025 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
1026 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
1027 : : }
1028 : :
1029 : : return error;
1030 : : }
1031 : :
1032 : : static int
1033 : 0 : hn_dev_stop(struct rte_eth_dev *dev)
1034 : : {
1035 : 0 : struct hn_data *hv = dev->data->dev_private;
1036 : : int i, ret;
1037 : :
1038 : 0 : PMD_INIT_FUNC_TRACE();
1039 : 0 : dev->data->dev_started = 0;
1040 : :
1041 : 0 : rte_dev_event_callback_unregister(NULL, netvsc_hotadd_callback, hv);
1042 : 0 : hn_rndis_set_rxfilter(hv, 0);
1043 : 0 : ret = hn_vf_stop(dev);
1044 : :
1045 [ # # ]: 0 : for (i = 0; i < hv->num_queues; i++) {
1046 : 0 : dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1047 : 0 : dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1048 : : }
1049 : :
1050 : 0 : return ret;
1051 : : }
1052 : :
1053 : : static int
1054 : 0 : hn_dev_close(struct rte_eth_dev *dev)
1055 : : {
1056 : : int ret;
1057 : 0 : struct hn_data *hv = dev->data->dev_private;
1058 : : struct hv_hotadd_context *hot_ctx;
1059 : :
1060 : 0 : PMD_INIT_FUNC_TRACE();
1061 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1062 : : return 0;
1063 : :
1064 : 0 : rte_spinlock_lock(&hv->hotadd_lock);
1065 [ # # ]: 0 : while (!LIST_EMPTY(&hv->hotadd_list)) {
1066 : : hot_ctx = LIST_FIRST(&hv->hotadd_list);
1067 : 0 : rte_eal_alarm_cancel(netvsc_hotplug_retry, hot_ctx);
1068 [ # # ]: 0 : LIST_REMOVE(hot_ctx, list);
1069 : 0 : rte_free(hot_ctx);
1070 : : }
1071 : : rte_spinlock_unlock(&hv->hotadd_lock);
1072 : :
1073 : 0 : ret = hn_vf_close(dev);
1074 : 0 : hn_dev_free_queues(dev);
1075 : :
1076 : 0 : return ret;
1077 : : }
1078 : :
1079 : : /*
1080 : : * Setup connection between PMD and kernel.
1081 : : */
1082 : : static int
1083 : 0 : hn_attach(struct hn_data *hv, unsigned int mtu)
1084 : : {
1085 : : int error;
1086 : :
1087 : : /* Attach NVS */
1088 : 0 : error = hn_nvs_attach(hv, mtu);
1089 [ # # ]: 0 : if (error)
1090 : 0 : goto failed_nvs;
1091 : :
1092 : : /* Attach RNDIS */
1093 : 0 : error = hn_rndis_attach(hv);
1094 [ # # ]: 0 : if (error)
1095 : 0 : goto failed_rndis;
1096 : :
1097 : : /*
1098 : : * NOTE:
1099 : : * Under certain conditions on certain versions of Hyper-V,
1100 : : * the RNDIS rxfilter is _not_ zero on the hypervisor side
1101 : : * after the successful RNDIS initialization.
1102 : : */
1103 : 0 : hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
1104 : 0 : return 0;
1105 : : failed_rndis:
1106 : 0 : hn_nvs_detach(hv);
1107 : : failed_nvs:
1108 : : return error;
1109 : : }
1110 : :
1111 : : static void
1112 : : hn_detach(struct hn_data *hv)
1113 : : {
1114 : 0 : hn_nvs_detach(hv);
1115 : 0 : hn_rndis_detach(hv);
1116 : : }
1117 : :
1118 : : /*
1119 : : * Connects EXISTING rx/tx queues to NEW vmbus channel(s), and
1120 : : * re-initializes NDIS and RNDIS, including re-sending initial
1121 : : * NDIS/RNDIS configuration. To be used after the underlying vmbus
1122 : : * has been un- and re-mapped, e.g. as must happen when the device
1123 : : * MTU is changed.
1124 : : */
1125 : : static int
1126 : 0 : hn_reinit(struct rte_eth_dev *dev, uint16_t mtu)
1127 : : {
1128 : 0 : struct hn_data *hv = dev->data->dev_private;
1129 : 0 : struct hn_rx_queue **rxqs = (struct hn_rx_queue **)dev->data->rx_queues;
1130 : 0 : struct hn_tx_queue **txqs = (struct hn_tx_queue **)dev->data->tx_queues;
1131 : : int i, ret = 0;
1132 : :
1133 : : /* Point primary queues at new primary channel */
1134 [ # # ]: 0 : if (rxqs[0]) {
1135 : 0 : rxqs[0]->chan = hv->channels[0];
1136 : 0 : txqs[0]->chan = hv->channels[0];
1137 : : }
1138 : :
1139 : 0 : ret = hn_attach(hv, mtu);
1140 [ # # ]: 0 : if (ret)
1141 : : return ret;
1142 : :
1143 : : /* Create vmbus subchannels, additional RNDIS configuration */
1144 : 0 : ret = hn_dev_configure(dev);
1145 [ # # ]: 0 : if (ret)
1146 : : return ret;
1147 : :
1148 : : /* Point any additional queues at new subchannels */
1149 [ # # ]: 0 : if (rxqs[0]) {
1150 [ # # ]: 0 : for (i = 1; i < dev->data->nb_rx_queues; i++)
1151 : 0 : rxqs[i]->chan = hv->channels[i];
1152 [ # # ]: 0 : for (i = 1; i < dev->data->nb_tx_queues; i++)
1153 : 0 : txqs[i]->chan = hv->channels[i];
1154 : : }
1155 : :
1156 : : return ret;
1157 : : }
1158 : :
1159 : : static int
1160 : 0 : hn_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1161 : : {
1162 : 0 : struct hn_data *hv = dev->data->dev_private;
1163 : 0 : unsigned int orig_mtu = dev->data->mtu;
1164 : : uint32_t rndis_mtu;
1165 : : int ret = 0;
1166 : : int i;
1167 : :
1168 [ # # ]: 0 : if (dev->data->dev_started) {
1169 : 0 : PMD_DRV_LOG(ERR, "Device must be stopped before changing MTU");
1170 : 0 : return -EBUSY;
1171 : : }
1172 : :
1173 : : /* Change MTU of underlying VF dev first, if it exists */
1174 : 0 : ret = hn_vf_mtu_set(dev, mtu);
1175 [ # # ]: 0 : if (ret)
1176 : : return ret;
1177 : :
1178 : : /* Release channel resources */
1179 : : hn_detach(hv);
1180 : :
1181 : : /* Close any secondary vmbus channels */
1182 [ # # ]: 0 : for (i = 1; i < hv->num_queues; i++)
1183 : 0 : rte_vmbus_chan_close(hv->channels[i]);
1184 : :
1185 : : /* Close primary vmbus channel */
1186 : 0 : rte_free(hv->channels[0]);
1187 : :
1188 : : /* Unmap and re-map vmbus device */
1189 : 0 : rte_vmbus_unmap_device(hv->vmbus);
1190 : 0 : ret = rte_vmbus_map_device(hv->vmbus);
1191 [ # # ]: 0 : if (ret) {
1192 : : /* This is a catastrophic error - the device is unusable */
1193 : 0 : PMD_DRV_LOG(ERR, "Could not re-map vmbus device!");
1194 : 0 : return ret;
1195 : : }
1196 : :
1197 : : /* Update pointers to re-mapped UIO resources */
1198 : 0 : hv->rxbuf_res = hv->vmbus->resource[HV_RECV_BUF_MAP];
1199 : 0 : hv->chim_res = hv->vmbus->resource[HV_SEND_BUF_MAP];
1200 : :
1201 : : /* Re-open the primary vmbus channel */
1202 : 0 : ret = rte_vmbus_chan_open(hv->vmbus, &hv->channels[0]);
1203 [ # # ]: 0 : if (ret) {
1204 : : /* This is a catastrophic error - the device is unusable */
1205 : 0 : PMD_DRV_LOG(ERR, "Could not re-open vmbus channel!");
1206 : 0 : return ret;
1207 : : }
1208 : :
1209 : 0 : rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
1210 : :
1211 : 0 : ret = hn_reinit(dev, mtu);
1212 [ # # ]: 0 : if (!ret)
1213 : 0 : goto out;
1214 : :
1215 : : /* In case of error, attempt to restore original MTU */
1216 : 0 : ret = hn_reinit(dev, orig_mtu);
1217 [ # # ]: 0 : if (ret)
1218 : 0 : PMD_DRV_LOG(ERR, "Restoring original MTU failed for netvsc");
1219 : :
1220 : 0 : ret = hn_vf_mtu_set(dev, orig_mtu);
1221 [ # # ]: 0 : if (ret)
1222 : 0 : PMD_DRV_LOG(ERR, "Restoring original MTU failed for VF");
1223 : :
1224 : 0 : out:
1225 [ # # ]: 0 : if (hn_rndis_get_mtu(hv, &rndis_mtu)) {
1226 : 0 : PMD_DRV_LOG(ERR, "Could not get MTU via RNDIS");
1227 : : } else {
1228 : 0 : dev->data->mtu = (uint16_t)rndis_mtu;
1229 : 0 : PMD_DRV_LOG(DEBUG, "RNDIS MTU is %u", dev->data->mtu);
1230 : : }
1231 : :
1232 : : return ret;
1233 : : }
1234 : :
1235 : : static const struct eth_dev_ops hn_eth_dev_ops = {
1236 : : .dev_configure = hn_dev_configure,
1237 : : .dev_start = hn_dev_start,
1238 : : .dev_stop = hn_dev_stop,
1239 : : .dev_close = hn_dev_close,
1240 : : .dev_infos_get = hn_dev_info_get,
1241 : : .txq_info_get = hn_dev_tx_queue_info,
1242 : : .rxq_info_get = hn_dev_rx_queue_info,
1243 : : .dev_supported_ptypes_get = hn_vf_supported_ptypes,
1244 : : .promiscuous_enable = hn_dev_promiscuous_enable,
1245 : : .promiscuous_disable = hn_dev_promiscuous_disable,
1246 : : .allmulticast_enable = hn_dev_allmulticast_enable,
1247 : : .allmulticast_disable = hn_dev_allmulticast_disable,
1248 : : .set_mc_addr_list = hn_dev_mc_addr_list,
1249 : : .mtu_set = hn_dev_mtu_set,
1250 : : .reta_update = hn_rss_reta_update,
1251 : : .reta_query = hn_rss_reta_query,
1252 : : .rss_hash_update = hn_rss_hash_update,
1253 : : .rss_hash_conf_get = hn_rss_hash_conf_get,
1254 : : .tx_queue_setup = hn_dev_tx_queue_setup,
1255 : : .tx_queue_release = hn_dev_tx_queue_release,
1256 : : .tx_done_cleanup = hn_dev_tx_done_cleanup,
1257 : : .rx_queue_setup = hn_dev_rx_queue_setup,
1258 : : .rx_queue_release = hn_dev_rx_queue_release,
1259 : : .link_update = hn_dev_link_update,
1260 : : .stats_get = hn_dev_stats_get,
1261 : : .stats_reset = hn_dev_stats_reset,
1262 : : .xstats_get = hn_dev_xstats_get,
1263 : : .xstats_get_names = hn_dev_xstats_get_names,
1264 : : .xstats_reset = hn_dev_xstats_reset,
1265 : : };
1266 : :
1267 : : static int
1268 : 0 : eth_hn_dev_init(struct rte_eth_dev *eth_dev)
1269 : : {
1270 : 0 : struct hn_data *hv = eth_dev->data->dev_private;
1271 : 0 : struct rte_device *device = eth_dev->device;
1272 : : struct rte_vmbus_device *vmbus;
1273 : : uint32_t mtu;
1274 : : unsigned int rxr_cnt;
1275 : : int err, max_chan;
1276 : :
1277 : 0 : PMD_INIT_FUNC_TRACE();
1278 : :
1279 : : rte_spinlock_init(&hv->hotadd_lock);
1280 : 0 : LIST_INIT(&hv->hotadd_list);
1281 : :
1282 : 0 : vmbus = container_of(device, struct rte_vmbus_device, device);
1283 : 0 : eth_dev->dev_ops = &hn_eth_dev_ops;
1284 : 0 : eth_dev->rx_queue_count = hn_dev_rx_queue_count;
1285 : 0 : eth_dev->rx_descriptor_status = hn_dev_rx_queue_status;
1286 : 0 : eth_dev->tx_descriptor_status = hn_dev_tx_descriptor_status;
1287 : 0 : eth_dev->tx_pkt_burst = &hn_xmit_pkts;
1288 : 0 : eth_dev->rx_pkt_burst = &hn_recv_pkts;
1289 : :
1290 : : /*
1291 : : * for secondary processes, we don't initialize any further as primary
1292 : : * has already done this work.
1293 : : */
1294 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1295 : : return 0;
1296 : :
1297 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1298 : :
1299 : : /* Since Hyper-V only supports one MAC address */
1300 : 0 : eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS,
1301 : : sizeof(struct rte_ether_addr), 0);
1302 [ # # ]: 0 : if (eth_dev->data->mac_addrs == NULL) {
1303 : 0 : PMD_INIT_LOG(ERR,
1304 : : "Failed to allocate memory store MAC addresses");
1305 : 0 : return -ENOMEM;
1306 : : }
1307 : :
1308 : 0 : hv->vmbus = vmbus;
1309 : 0 : hv->rxbuf_res = vmbus->resource[HV_RECV_BUF_MAP];
1310 : 0 : hv->chim_res = vmbus->resource[HV_SEND_BUF_MAP];
1311 : 0 : hv->port_id = eth_dev->data->port_id;
1312 : 0 : hv->latency = HN_CHAN_LATENCY_NS;
1313 : 0 : hv->rx_copybreak = HN_RXCOPY_THRESHOLD;
1314 : 0 : hv->tx_copybreak = HN_TXCOPY_THRESHOLD;
1315 : 0 : hv->rx_extmbuf_enable = HN_RX_EXTMBUF_ENABLE;
1316 : 0 : hv->max_queues = 1;
1317 : :
1318 : : rte_rwlock_init(&hv->vf_lock);
1319 : 0 : hv->vf_ctx.vf_vsc_switched = false;
1320 : 0 : hv->vf_ctx.vf_vsp_reported = false;
1321 : 0 : hv->vf_ctx.vf_attached = false;
1322 : 0 : hv->vf_ctx.vf_state = vf_unknown;
1323 : :
1324 : 0 : err = hn_parse_args(eth_dev);
1325 [ # # ]: 0 : if (err)
1326 : : return err;
1327 : :
1328 : 0 : strlcpy(hv->owner.name, eth_dev->device->name,
1329 : : RTE_ETH_MAX_OWNER_NAME_LEN);
1330 : 0 : err = rte_eth_dev_owner_new(&hv->owner.id);
1331 [ # # ]: 0 : if (err) {
1332 : 0 : PMD_INIT_LOG(ERR, "Can not get owner id");
1333 : 0 : return err;
1334 : : }
1335 : :
1336 : : /* Initialize primary channel input for control operations */
1337 : 0 : err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
1338 [ # # ]: 0 : if (err)
1339 : : return err;
1340 : :
1341 : 0 : rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
1342 : :
1343 : 0 : hv->primary = hn_rx_queue_alloc(hv, 0,
1344 : 0 : eth_dev->device->numa_node);
1345 : :
1346 [ # # ]: 0 : if (!hv->primary)
1347 : : return -ENOMEM;
1348 : :
1349 : 0 : err = hn_attach(hv, RTE_ETHER_MTU);
1350 [ # # ]: 0 : if (err)
1351 : 0 : goto failed;
1352 : :
1353 : 0 : err = hn_chim_init(eth_dev);
1354 [ # # ]: 0 : if (err)
1355 : 0 : goto failed;
1356 : :
1357 : 0 : err = hn_rndis_get_mtu(hv, &mtu);
1358 [ # # ]: 0 : if (err)
1359 : 0 : goto failed;
1360 : 0 : eth_dev->data->mtu = (uint16_t)mtu;
1361 : 0 : PMD_INIT_LOG(DEBUG, "RNDIS MTU is %u", eth_dev->data->mtu);
1362 : :
1363 : 0 : err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes);
1364 [ # # ]: 0 : if (err)
1365 : 0 : goto failed;
1366 : :
1367 : : /* Multi queue requires later versions of windows server */
1368 [ # # ]: 0 : if (hv->nvs_ver < NVS_VERSION_5)
1369 : : return 0;
1370 : :
1371 : 0 : max_chan = rte_vmbus_max_channels(vmbus);
1372 : 0 : PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
1373 [ # # ]: 0 : if (max_chan <= 0)
1374 : 0 : goto failed;
1375 : :
1376 [ # # ]: 0 : if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
1377 : 0 : rxr_cnt = 1;
1378 : :
1379 : 0 : hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
1380 : :
1381 : : /* If VF was reported but not added, do it now */
1382 [ # # # # ]: 0 : if (hv->vf_ctx.vf_vsp_reported && !hv->vf_ctx.vf_vsc_switched) {
1383 : 0 : PMD_INIT_LOG(DEBUG, "Adding VF device");
1384 : :
1385 : 0 : err = hn_vf_add(eth_dev, hv);
1386 : : }
1387 : :
1388 : : return 0;
1389 : :
1390 : 0 : failed:
1391 : 0 : PMD_INIT_LOG(NOTICE, "device init failed");
1392 : :
1393 : 0 : hn_chim_uninit(eth_dev);
1394 : : hn_detach(hv);
1395 : 0 : return err;
1396 : : }
1397 : :
1398 : : static int
1399 : 0 : eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1400 : : {
1401 : 0 : struct hn_data *hv = eth_dev->data->dev_private;
1402 : : int ret, ret_stop;
1403 : :
1404 : 0 : PMD_INIT_FUNC_TRACE();
1405 : :
1406 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1407 : : return 0;
1408 : :
1409 : 0 : ret_stop = hn_dev_stop(eth_dev);
1410 : 0 : hn_dev_close(eth_dev);
1411 : :
1412 : 0 : free(hv->vf_devargs);
1413 : 0 : hv->vf_devargs = NULL;
1414 : :
1415 : : hn_detach(hv);
1416 : 0 : hn_chim_uninit(eth_dev);
1417 : 0 : rte_vmbus_chan_close(hv->channels[0]);
1418 : 0 : rte_free(hv->primary);
1419 : 0 : ret = rte_eth_dev_owner_delete(hv->owner.id);
1420 [ # # ]: 0 : if (ret != 0)
1421 : 0 : return ret;
1422 : :
1423 : : return ret_stop;
1424 : : }
1425 : :
1426 : 0 : static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
1427 : : struct rte_vmbus_device *dev)
1428 : : {
1429 : : struct rte_eth_dev *eth_dev;
1430 : : int ret;
1431 : :
1432 : 0 : PMD_INIT_FUNC_TRACE();
1433 : :
1434 : 0 : ret = rte_dev_event_monitor_start();
1435 [ # # ]: 0 : if (ret) {
1436 : 0 : PMD_DRV_LOG(ERR, "Failed to start device event monitoring");
1437 : 0 : return ret;
1438 : : }
1439 : :
1440 : 0 : eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
1441 [ # # ]: 0 : if (!eth_dev)
1442 : : return -ENOMEM;
1443 : :
1444 : 0 : ret = eth_hn_dev_init(eth_dev);
1445 [ # # ]: 0 : if (ret) {
1446 : : eth_dev_vmbus_release(eth_dev);
1447 : 0 : rte_dev_event_monitor_stop();
1448 : : } else {
1449 : 0 : rte_eth_dev_probing_finish(eth_dev);
1450 : : }
1451 : :
1452 : : return ret;
1453 : : }
1454 : :
1455 : 0 : static int eth_hn_remove(struct rte_vmbus_device *dev)
1456 : : {
1457 : : struct rte_eth_dev *eth_dev;
1458 : : int ret;
1459 : :
1460 : 0 : PMD_INIT_FUNC_TRACE();
1461 : :
1462 : 0 : eth_dev = rte_eth_dev_allocated(dev->device.name);
1463 [ # # ]: 0 : if (!eth_dev)
1464 : : return 0; /* port already released */
1465 : :
1466 : 0 : ret = eth_hn_dev_uninit(eth_dev);
1467 [ # # ]: 0 : if (ret)
1468 : : return ret;
1469 : :
1470 : : eth_dev_vmbus_release(eth_dev);
1471 : 0 : rte_dev_event_monitor_stop();
1472 : 0 : return 0;
1473 : : }
1474 : :
1475 : : /* Network device GUID */
1476 : : static const rte_uuid_t hn_net_ids[] = {
1477 : : /* f8615163-df3e-46c5-913f-f2d2f965ed0e */
1478 : : RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
1479 : : { 0 }
1480 : : };
1481 : :
1482 : : static struct rte_vmbus_driver rte_netvsc_pmd = {
1483 : : .id_table = hn_net_ids,
1484 : : .probe = eth_hn_probe,
1485 : : .remove = eth_hn_remove,
1486 : : };
1487 : :
1488 : 252 : RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
1489 : : RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
1490 [ - + ]: 252 : RTE_LOG_REGISTER_SUFFIX(hn_logtype_init, init, NOTICE);
1491 [ - + ]: 252 : RTE_LOG_REGISTER_SUFFIX(hn_logtype_driver, driver, NOTICE);
1492 : : RTE_PMD_REGISTER_PARAM_STRING(net_netvsc,
1493 : : NETVSC_ARG_LATENCY "=<uint32> "
1494 : : NETVSC_ARG_RXBREAK "=<uint32> "
1495 : : NETVSC_ARG_TXBREAK "=<uint32> "
1496 : : NETVSC_ARG_RX_EXTMBUF_ENABLE "=<0|1>");
|