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 13 #include <rte_ethdev.h> 14 #include <rte_memcpy.h> 15 #include <rte_string_fns.h> 16 #include <rte_memzone.h> 17 #include <rte_devargs.h> 18 #include <rte_malloc.h> 19 #include <rte_kvargs.h> 20 #include <rte_atomic.h> 21 #include <rte_branch_prediction.h> 22 #include <rte_ether.h> 23 #include <rte_ethdev_driver.h> 24 #include <rte_cycles.h> 25 #include <rte_errno.h> 26 #include <rte_memory.h> 27 #include <rte_eal.h> 28 #include <rte_dev.h> 29 #include <rte_bus_vmbus.h> 30 31 #include "hn_logs.h" 32 #include "hn_var.h" 33 #include "hn_rndis.h" 34 #include "hn_nvs.h" 35 #include "ndis.h" 36 37 #define HN_TX_OFFLOAD_CAPS (DEV_TX_OFFLOAD_IPV4_CKSUM | \ 38 DEV_TX_OFFLOAD_TCP_CKSUM | \ 39 DEV_TX_OFFLOAD_UDP_CKSUM | \ 40 DEV_TX_OFFLOAD_TCP_TSO | \ 41 DEV_TX_OFFLOAD_MULTI_SEGS | \ 42 DEV_TX_OFFLOAD_VLAN_INSERT) 43 44 #define HN_RX_OFFLOAD_CAPS (DEV_RX_OFFLOAD_CHECKSUM | \ 45 DEV_RX_OFFLOAD_VLAN_STRIP) 46 47 int hn_logtype_init; 48 int hn_logtype_driver; 49 50 struct hn_xstats_name_off { 51 char name[RTE_ETH_XSTATS_NAME_SIZE]; 52 unsigned int offset; 53 }; 54 55 static const struct hn_xstats_name_off hn_stat_strings[] = { 56 { "good_packets", offsetof(struct hn_stats, packets) }, 57 { "good_bytes", offsetof(struct hn_stats, bytes) }, 58 { "errors", offsetof(struct hn_stats, errors) }, 59 { "ring full", offsetof(struct hn_stats, ring_full) }, 60 { "multicast_packets", offsetof(struct hn_stats, multicast) }, 61 { "broadcast_packets", offsetof(struct hn_stats, broadcast) }, 62 { "undersize_packets", offsetof(struct hn_stats, size_bins[0]) }, 63 { "size_64_packets", offsetof(struct hn_stats, size_bins[1]) }, 64 { "size_65_127_packets", offsetof(struct hn_stats, size_bins[2]) }, 65 { "size_128_255_packets", offsetof(struct hn_stats, size_bins[3]) }, 66 { "size_256_511_packets", offsetof(struct hn_stats, size_bins[4]) }, 67 { "size_512_1023_packets", offsetof(struct hn_stats, size_bins[5]) }, 68 { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) }, 69 { "size_1519_max_packets", offsetof(struct hn_stats, size_bins[7]) }, 70 }; 71 72 /* The default RSS key. 73 * This value is the same as MLX5 so that flows will be 74 * received on same path for both VF ans synthetic NIC. 75 */ 76 static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = { 77 0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7, 78 0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94, 79 0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1, 80 0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59, 81 0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a, 82 }; 83 84 static struct rte_eth_dev * 85 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size) 86 { 87 struct rte_eth_dev *eth_dev; 88 const char *name; 89 90 if (!dev) 91 return NULL; 92 93 name = dev->device.name; 94 95 if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 96 eth_dev = rte_eth_dev_allocate(name); 97 if (!eth_dev) { 98 PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev"); 99 return NULL; 100 } 101 102 if (private_data_size) { 103 eth_dev->data->dev_private = 104 rte_zmalloc_socket(name, private_data_size, 105 RTE_CACHE_LINE_SIZE, dev->device.numa_node); 106 if (!eth_dev->data->dev_private) { 107 PMD_DRV_LOG(NOTICE, "can not allocate driver data"); 108 rte_eth_dev_release_port(eth_dev); 109 return NULL; 110 } 111 } 112 } else { 113 eth_dev = rte_eth_dev_attach_secondary(name); 114 if (!eth_dev) { 115 PMD_DRV_LOG(NOTICE, "can not attach secondary"); 116 return NULL; 117 } 118 } 119 120 eth_dev->device = &dev->device; 121 122 /* interrupt is simulated */ 123 dev->intr_handle.type = RTE_INTR_HANDLE_EXT; 124 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC; 125 eth_dev->intr_handle = &dev->intr_handle; 126 127 /* allow ethdev to remove on close */ 128 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE; 129 130 return eth_dev; 131 } 132 133 static void 134 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev) 135 { 136 /* mac_addrs must not be freed alone because part of dev_private */ 137 eth_dev->data->mac_addrs = NULL; 138 /* free ether device */ 139 rte_eth_dev_release_port(eth_dev); 140 141 eth_dev->device = NULL; 142 eth_dev->intr_handle = NULL; 143 } 144 145 /* handle "latency=X" from devargs */ 146 static int hn_set_latency(const char *key, const char *value, void *opaque) 147 { 148 struct hn_data *hv = opaque; 149 char *endp = NULL; 150 unsigned long lat; 151 152 errno = 0; 153 lat = strtoul(value, &endp, 0); 154 155 if (*value == '\0' || *endp != '\0') { 156 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value); 157 return -EINVAL; 158 } 159 160 PMD_DRV_LOG(DEBUG, "set latency %lu usec", lat); 161 162 hv->latency = lat * 1000; /* usec to nsec */ 163 return 0; 164 } 165 166 /* Parse device arguments */ 167 static int hn_parse_args(const struct rte_eth_dev *dev) 168 { 169 struct hn_data *hv = dev->data->dev_private; 170 struct rte_devargs *devargs = dev->device->devargs; 171 static const char * const valid_keys[] = { 172 "latency", 173 NULL 174 }; 175 struct rte_kvargs *kvlist; 176 int ret; 177 178 if (!devargs) 179 return 0; 180 181 PMD_INIT_LOG(DEBUG, "device args %s %s", 182 devargs->name, devargs->args); 183 184 kvlist = rte_kvargs_parse(devargs->args, valid_keys); 185 if (!kvlist) { 186 PMD_DRV_LOG(NOTICE, "invalid parameters"); 187 return -EINVAL; 188 } 189 190 ret = rte_kvargs_process(kvlist, "latency", hn_set_latency, hv); 191 if (ret) 192 PMD_DRV_LOG(ERR, "Unable to process latency arg\n"); 193 194 rte_kvargs_free(kvlist); 195 return ret; 196 } 197 198 /* Update link status. 199 * Note: the DPDK definition of "wait_to_complete" 200 * means block this call until link is up. 201 * which is not worth supporting. 202 */ 203 int 204 hn_dev_link_update(struct rte_eth_dev *dev, 205 int wait_to_complete) 206 { 207 struct hn_data *hv = dev->data->dev_private; 208 struct rte_eth_link link, old; 209 int error; 210 211 old = dev->data->dev_link; 212 213 error = hn_rndis_get_linkstatus(hv); 214 if (error) 215 return error; 216 217 hn_rndis_get_linkspeed(hv); 218 219 hn_vf_link_update(dev, wait_to_complete); 220 221 link = (struct rte_eth_link) { 222 .link_duplex = ETH_LINK_FULL_DUPLEX, 223 .link_autoneg = ETH_LINK_SPEED_FIXED, 224 .link_speed = hv->link_speed / 10000, 225 }; 226 227 if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED) 228 link.link_status = ETH_LINK_UP; 229 else 230 link.link_status = ETH_LINK_DOWN; 231 232 if (old.link_status == link.link_status) 233 return 0; 234 235 PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id, 236 (link.link_status == ETH_LINK_UP) ? "up" : "down"); 237 238 return rte_eth_linkstatus_set(dev, &link); 239 } 240 241 static int hn_dev_info_get(struct rte_eth_dev *dev, 242 struct rte_eth_dev_info *dev_info) 243 { 244 struct hn_data *hv = dev->data->dev_private; 245 int rc; 246 247 dev_info->speed_capa = ETH_LINK_SPEED_10G; 248 dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE; 249 dev_info->max_rx_pktlen = HN_MAX_XFER_LEN; 250 dev_info->max_mac_addrs = 1; 251 252 dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ; 253 dev_info->flow_type_rss_offloads = hv->rss_offloads; 254 dev_info->reta_size = ETH_RSS_RETA_SIZE_128; 255 256 dev_info->max_rx_queues = hv->max_queues; 257 dev_info->max_tx_queues = hv->max_queues; 258 259 rc = hn_rndis_get_offload(hv, dev_info); 260 if (rc != 0) 261 return rc; 262 263 rc = hn_vf_info_get(hv, dev_info); 264 if (rc != 0) 265 return rc; 266 267 return 0; 268 } 269 270 static int hn_rss_reta_update(struct rte_eth_dev *dev, 271 struct rte_eth_rss_reta_entry64 *reta_conf, 272 uint16_t reta_size) 273 { 274 struct hn_data *hv = dev->data->dev_private; 275 unsigned int i; 276 int err; 277 278 PMD_INIT_FUNC_TRACE(); 279 280 if (reta_size != NDIS_HASH_INDCNT) { 281 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS"); 282 return -EINVAL; 283 } 284 285 for (i = 0; i < NDIS_HASH_INDCNT; i++) { 286 uint16_t idx = i / RTE_RETA_GROUP_SIZE; 287 uint16_t shift = i % RTE_RETA_GROUP_SIZE; 288 uint64_t mask = (uint64_t)1 << shift; 289 290 if (reta_conf[idx].mask & mask) 291 hv->rss_ind[i] = reta_conf[idx].reta[shift]; 292 } 293 294 err = hn_rndis_conf_rss(hv, 0); 295 if (err) { 296 PMD_DRV_LOG(NOTICE, 297 "reta reconfig failed"); 298 return err; 299 } 300 301 return hn_vf_reta_hash_update(dev, reta_conf, reta_size); 302 } 303 304 static int hn_rss_reta_query(struct rte_eth_dev *dev, 305 struct rte_eth_rss_reta_entry64 *reta_conf, 306 uint16_t reta_size) 307 { 308 struct hn_data *hv = dev->data->dev_private; 309 unsigned int i; 310 311 PMD_INIT_FUNC_TRACE(); 312 313 if (reta_size != NDIS_HASH_INDCNT) { 314 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS"); 315 return -EINVAL; 316 } 317 318 for (i = 0; i < NDIS_HASH_INDCNT; i++) { 319 uint16_t idx = i / RTE_RETA_GROUP_SIZE; 320 uint16_t shift = i % RTE_RETA_GROUP_SIZE; 321 uint64_t mask = (uint64_t)1 << shift; 322 323 if (reta_conf[idx].mask & mask) 324 reta_conf[idx].reta[shift] = hv->rss_ind[i]; 325 } 326 return 0; 327 } 328 329 static void hn_rss_hash_init(struct hn_data *hv, 330 const struct rte_eth_rss_conf *rss_conf) 331 { 332 /* Convert from DPDK RSS hash flags to NDIS hash flags */ 333 hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ; 334 335 if (rss_conf->rss_hf & ETH_RSS_IPV4) 336 hv->rss_hash |= NDIS_HASH_IPV4; 337 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) 338 hv->rss_hash |= NDIS_HASH_TCP_IPV4; 339 if (rss_conf->rss_hf & ETH_RSS_IPV6) 340 hv->rss_hash |= NDIS_HASH_IPV6; 341 if (rss_conf->rss_hf & ETH_RSS_IPV6_EX) 342 hv->rss_hash |= NDIS_HASH_IPV6_EX; 343 if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP) 344 hv->rss_hash |= NDIS_HASH_TCP_IPV6; 345 if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX) 346 hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX; 347 348 memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key, 349 NDIS_HASH_KEYSIZE_TOEPLITZ); 350 } 351 352 static int hn_rss_hash_update(struct rte_eth_dev *dev, 353 struct rte_eth_rss_conf *rss_conf) 354 { 355 struct hn_data *hv = dev->data->dev_private; 356 int err; 357 358 PMD_INIT_FUNC_TRACE(); 359 360 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE); 361 if (err) { 362 PMD_DRV_LOG(NOTICE, 363 "rss disable failed"); 364 return err; 365 } 366 367 hn_rss_hash_init(hv, rss_conf); 368 369 err = hn_rndis_conf_rss(hv, 0); 370 if (err) { 371 PMD_DRV_LOG(NOTICE, 372 "rss reconfig failed (RSS disabled)"); 373 return err; 374 } 375 376 377 return hn_vf_rss_hash_update(dev, rss_conf); 378 } 379 380 static int hn_rss_hash_conf_get(struct rte_eth_dev *dev, 381 struct rte_eth_rss_conf *rss_conf) 382 { 383 struct hn_data *hv = dev->data->dev_private; 384 385 PMD_INIT_FUNC_TRACE(); 386 387 if (hv->ndis_ver < NDIS_VERSION_6_20) { 388 PMD_DRV_LOG(DEBUG, "RSS not supported on this host"); 389 return -EOPNOTSUPP; 390 } 391 392 rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ; 393 if (rss_conf->rss_key) 394 memcpy(rss_conf->rss_key, hv->rss_key, 395 NDIS_HASH_KEYSIZE_TOEPLITZ); 396 397 rss_conf->rss_hf = 0; 398 if (hv->rss_hash & NDIS_HASH_IPV4) 399 rss_conf->rss_hf |= ETH_RSS_IPV4; 400 401 if (hv->rss_hash & NDIS_HASH_TCP_IPV4) 402 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP; 403 404 if (hv->rss_hash & NDIS_HASH_IPV6) 405 rss_conf->rss_hf |= ETH_RSS_IPV6; 406 407 if (hv->rss_hash & NDIS_HASH_IPV6_EX) 408 rss_conf->rss_hf |= ETH_RSS_IPV6_EX; 409 410 if (hv->rss_hash & NDIS_HASH_TCP_IPV6) 411 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP; 412 413 if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX) 414 rss_conf->rss_hf |= ETH_RSS_IPV6_TCP_EX; 415 416 return 0; 417 } 418 419 static int 420 hn_dev_promiscuous_enable(struct rte_eth_dev *dev) 421 { 422 struct hn_data *hv = dev->data->dev_private; 423 424 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS); 425 return hn_vf_promiscuous_enable(dev); 426 } 427 428 static int 429 hn_dev_promiscuous_disable(struct rte_eth_dev *dev) 430 { 431 struct hn_data *hv = dev->data->dev_private; 432 uint32_t filter; 433 434 filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST; 435 if (dev->data->all_multicast) 436 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST; 437 hn_rndis_set_rxfilter(hv, filter); 438 return hn_vf_promiscuous_disable(dev); 439 } 440 441 static int 442 hn_dev_allmulticast_enable(struct rte_eth_dev *dev) 443 { 444 struct hn_data *hv = dev->data->dev_private; 445 446 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED | 447 NDIS_PACKET_TYPE_ALL_MULTICAST | 448 NDIS_PACKET_TYPE_BROADCAST); 449 return hn_vf_allmulticast_enable(dev); 450 } 451 452 static int 453 hn_dev_allmulticast_disable(struct rte_eth_dev *dev) 454 { 455 struct hn_data *hv = dev->data->dev_private; 456 457 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED | 458 NDIS_PACKET_TYPE_BROADCAST); 459 return hn_vf_allmulticast_disable(dev); 460 } 461 462 static int 463 hn_dev_mc_addr_list(struct rte_eth_dev *dev, 464 struct rte_ether_addr *mc_addr_set, 465 uint32_t nb_mc_addr) 466 { 467 /* No filtering on the synthetic path, but can do it on VF */ 468 return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr); 469 } 470 471 /* Setup shared rx/tx queue data */ 472 static int hn_subchan_configure(struct hn_data *hv, 473 uint32_t subchan) 474 { 475 struct vmbus_channel *primary = hn_primary_chan(hv); 476 int err; 477 unsigned int retry = 0; 478 479 PMD_DRV_LOG(DEBUG, 480 "open %u subchannels", subchan); 481 482 /* Send create sub channels command */ 483 err = hn_nvs_alloc_subchans(hv, &subchan); 484 if (err) 485 return err; 486 487 while (subchan > 0) { 488 struct vmbus_channel *new_sc; 489 uint16_t chn_index; 490 491 err = rte_vmbus_subchan_open(primary, &new_sc); 492 if (err == -ENOENT && ++retry < 1000) { 493 /* This can happen if not ready yet */ 494 rte_delay_ms(10); 495 continue; 496 } 497 498 if (err) { 499 PMD_DRV_LOG(ERR, 500 "open subchannel failed: %d", err); 501 return err; 502 } 503 504 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency); 505 506 retry = 0; 507 chn_index = rte_vmbus_sub_channel_index(new_sc); 508 if (chn_index == 0 || chn_index > hv->max_queues) { 509 PMD_DRV_LOG(ERR, 510 "Invalid subchannel offermsg channel %u", 511 chn_index); 512 return -EIO; 513 } 514 515 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index); 516 hv->channels[chn_index] = new_sc; 517 --subchan; 518 } 519 520 return err; 521 } 522 523 static int hn_dev_configure(struct rte_eth_dev *dev) 524 { 525 struct rte_eth_conf *dev_conf = &dev->data->dev_conf; 526 struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf; 527 const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode; 528 const struct rte_eth_txmode *txmode = &dev_conf->txmode; 529 struct hn_data *hv = dev->data->dev_private; 530 uint64_t unsupported; 531 int i, err, subchan; 532 533 PMD_INIT_FUNC_TRACE(); 534 535 dev_conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH; 536 537 unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS; 538 if (unsupported) { 539 PMD_DRV_LOG(NOTICE, 540 "unsupported TX offload: %#" PRIx64, 541 unsupported); 542 return -EINVAL; 543 } 544 545 unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS; 546 if (unsupported) { 547 PMD_DRV_LOG(NOTICE, 548 "unsupported RX offload: %#" PRIx64, 549 rxmode->offloads); 550 return -EINVAL; 551 } 552 553 hv->vlan_strip = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP); 554 555 err = hn_rndis_conf_offload(hv, txmode->offloads, 556 rxmode->offloads); 557 if (err) { 558 PMD_DRV_LOG(NOTICE, 559 "offload configure failed"); 560 return err; 561 } 562 563 hv->num_queues = RTE_MAX(dev->data->nb_rx_queues, 564 dev->data->nb_tx_queues); 565 566 for (i = 0; i < NDIS_HASH_INDCNT; i++) 567 hv->rss_ind[i] = i % hv->num_queues; 568 569 hn_rss_hash_init(hv, rss_conf); 570 571 subchan = hv->num_queues - 1; 572 if (subchan > 0) { 573 err = hn_subchan_configure(hv, subchan); 574 if (err) { 575 PMD_DRV_LOG(NOTICE, 576 "subchannel configuration failed"); 577 return err; 578 } 579 580 err = hn_rndis_conf_rss(hv, 0); 581 if (err) { 582 PMD_DRV_LOG(NOTICE, 583 "initial RSS config failed"); 584 return err; 585 } 586 } 587 588 return hn_vf_configure(dev, dev_conf); 589 } 590 591 static int hn_dev_stats_get(struct rte_eth_dev *dev, 592 struct rte_eth_stats *stats) 593 { 594 unsigned int i; 595 596 hn_vf_stats_get(dev, stats); 597 598 for (i = 0; i < dev->data->nb_tx_queues; i++) { 599 const struct hn_tx_queue *txq = dev->data->tx_queues[i]; 600 601 if (!txq) 602 continue; 603 604 stats->opackets += txq->stats.packets; 605 stats->obytes += txq->stats.bytes; 606 stats->oerrors += txq->stats.errors; 607 608 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 609 stats->q_opackets[i] = txq->stats.packets; 610 stats->q_obytes[i] = txq->stats.bytes; 611 } 612 } 613 614 for (i = 0; i < dev->data->nb_rx_queues; i++) { 615 const struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 616 617 if (!rxq) 618 continue; 619 620 stats->ipackets += rxq->stats.packets; 621 stats->ibytes += rxq->stats.bytes; 622 stats->ierrors += rxq->stats.errors; 623 stats->imissed += rxq->stats.ring_full; 624 625 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 626 stats->q_ipackets[i] = rxq->stats.packets; 627 stats->q_ibytes[i] = rxq->stats.bytes; 628 } 629 } 630 631 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; 632 return 0; 633 } 634 635 static int 636 hn_dev_stats_reset(struct rte_eth_dev *dev) 637 { 638 unsigned int i; 639 640 PMD_INIT_FUNC_TRACE(); 641 642 for (i = 0; i < dev->data->nb_tx_queues; i++) { 643 struct hn_tx_queue *txq = dev->data->tx_queues[i]; 644 645 if (!txq) 646 continue; 647 memset(&txq->stats, 0, sizeof(struct hn_stats)); 648 } 649 650 for (i = 0; i < dev->data->nb_rx_queues; i++) { 651 struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 652 653 if (!rxq) 654 continue; 655 656 memset(&rxq->stats, 0, sizeof(struct hn_stats)); 657 } 658 659 return 0; 660 } 661 662 static int 663 hn_dev_xstats_reset(struct rte_eth_dev *dev) 664 { 665 int ret; 666 667 ret = hn_dev_stats_reset(dev); 668 if (ret != 0) 669 return 0; 670 671 return hn_vf_xstats_reset(dev); 672 } 673 674 static int 675 hn_dev_xstats_count(struct rte_eth_dev *dev) 676 { 677 int ret, count; 678 679 count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings); 680 count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings); 681 682 ret = hn_vf_xstats_get_names(dev, NULL, 0); 683 if (ret < 0) 684 return ret; 685 686 return count + ret; 687 } 688 689 static int 690 hn_dev_xstats_get_names(struct rte_eth_dev *dev, 691 struct rte_eth_xstat_name *xstats_names, 692 unsigned int limit) 693 { 694 unsigned int i, t, count = 0; 695 int ret; 696 697 if (!xstats_names) 698 return hn_dev_xstats_count(dev); 699 700 /* Note: limit checked in rte_eth_xstats_names() */ 701 for (i = 0; i < dev->data->nb_tx_queues; i++) { 702 const struct hn_tx_queue *txq = dev->data->tx_queues[i]; 703 704 if (!txq) 705 continue; 706 707 if (count >= limit) 708 break; 709 710 for (t = 0; t < RTE_DIM(hn_stat_strings); t++) 711 snprintf(xstats_names[count++].name, 712 RTE_ETH_XSTATS_NAME_SIZE, 713 "tx_q%u_%s", i, hn_stat_strings[t].name); 714 } 715 716 for (i = 0; i < dev->data->nb_rx_queues; i++) { 717 const struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 718 719 if (!rxq) 720 continue; 721 722 if (count >= limit) 723 break; 724 725 for (t = 0; t < RTE_DIM(hn_stat_strings); t++) 726 snprintf(xstats_names[count++].name, 727 RTE_ETH_XSTATS_NAME_SIZE, 728 "rx_q%u_%s", i, 729 hn_stat_strings[t].name); 730 } 731 732 ret = hn_vf_xstats_get_names(dev, xstats_names + count, 733 limit - count); 734 if (ret < 0) 735 return ret; 736 737 return count + ret; 738 } 739 740 static int 741 hn_dev_xstats_get(struct rte_eth_dev *dev, 742 struct rte_eth_xstat *xstats, 743 unsigned int n) 744 { 745 unsigned int i, t, count = 0; 746 const unsigned int nstats = hn_dev_xstats_count(dev); 747 const char *stats; 748 int ret; 749 750 PMD_INIT_FUNC_TRACE(); 751 752 if (n < nstats) 753 return nstats; 754 755 for (i = 0; i < dev->data->nb_tx_queues; i++) { 756 const struct hn_tx_queue *txq = dev->data->tx_queues[i]; 757 758 if (!txq) 759 continue; 760 761 stats = (const char *)&txq->stats; 762 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) { 763 xstats[count].id = count; 764 xstats[count].value = *(const uint64_t *) 765 (stats + hn_stat_strings[t].offset); 766 } 767 } 768 769 for (i = 0; i < dev->data->nb_rx_queues; i++) { 770 const struct hn_rx_queue *rxq = dev->data->rx_queues[i]; 771 772 if (!rxq) 773 continue; 774 775 stats = (const char *)&rxq->stats; 776 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) { 777 xstats[count].id = count; 778 xstats[count].value = *(const uint64_t *) 779 (stats + hn_stat_strings[t].offset); 780 } 781 } 782 783 ret = hn_vf_xstats_get(dev, xstats, count, n); 784 if (ret < 0) 785 return ret; 786 787 return count + ret; 788 } 789 790 static int 791 hn_dev_start(struct rte_eth_dev *dev) 792 { 793 struct hn_data *hv = dev->data->dev_private; 794 int error; 795 796 PMD_INIT_FUNC_TRACE(); 797 798 error = hn_rndis_set_rxfilter(hv, 799 NDIS_PACKET_TYPE_BROADCAST | 800 NDIS_PACKET_TYPE_ALL_MULTICAST | 801 NDIS_PACKET_TYPE_DIRECTED); 802 if (error) 803 return error; 804 805 error = hn_vf_start(dev); 806 if (error) 807 hn_rndis_set_rxfilter(hv, 0); 808 809 return error; 810 } 811 812 static void 813 hn_dev_stop(struct rte_eth_dev *dev) 814 { 815 struct hn_data *hv = dev->data->dev_private; 816 817 PMD_INIT_FUNC_TRACE(); 818 819 hn_rndis_set_rxfilter(hv, 0); 820 hn_vf_stop(dev); 821 } 822 823 static void 824 hn_dev_close(struct rte_eth_dev *dev) 825 { 826 PMD_INIT_FUNC_TRACE(); 827 828 hn_vf_close(dev); 829 hn_dev_free_queues(dev); 830 } 831 832 static const struct eth_dev_ops hn_eth_dev_ops = { 833 .dev_configure = hn_dev_configure, 834 .dev_start = hn_dev_start, 835 .dev_stop = hn_dev_stop, 836 .dev_close = hn_dev_close, 837 .dev_infos_get = hn_dev_info_get, 838 .dev_supported_ptypes_get = hn_vf_supported_ptypes, 839 .promiscuous_enable = hn_dev_promiscuous_enable, 840 .promiscuous_disable = hn_dev_promiscuous_disable, 841 .allmulticast_enable = hn_dev_allmulticast_enable, 842 .allmulticast_disable = hn_dev_allmulticast_disable, 843 .set_mc_addr_list = hn_dev_mc_addr_list, 844 .reta_update = hn_rss_reta_update, 845 .reta_query = hn_rss_reta_query, 846 .rss_hash_update = hn_rss_hash_update, 847 .rss_hash_conf_get = hn_rss_hash_conf_get, 848 .tx_queue_setup = hn_dev_tx_queue_setup, 849 .tx_queue_release = hn_dev_tx_queue_release, 850 .tx_done_cleanup = hn_dev_tx_done_cleanup, 851 .rx_queue_setup = hn_dev_rx_queue_setup, 852 .rx_queue_release = hn_dev_rx_queue_release, 853 .link_update = hn_dev_link_update, 854 .stats_get = hn_dev_stats_get, 855 .stats_reset = hn_dev_stats_reset, 856 .xstats_get = hn_dev_xstats_get, 857 .xstats_get_names = hn_dev_xstats_get_names, 858 .xstats_reset = hn_dev_xstats_reset, 859 }; 860 861 /* 862 * Setup connection between PMD and kernel. 863 */ 864 static int 865 hn_attach(struct hn_data *hv, unsigned int mtu) 866 { 867 int error; 868 869 /* Attach NVS */ 870 error = hn_nvs_attach(hv, mtu); 871 if (error) 872 goto failed_nvs; 873 874 /* Attach RNDIS */ 875 error = hn_rndis_attach(hv); 876 if (error) 877 goto failed_rndis; 878 879 /* 880 * NOTE: 881 * Under certain conditions on certain versions of Hyper-V, 882 * the RNDIS rxfilter is _not_ zero on the hypervisor side 883 * after the successful RNDIS initialization. 884 */ 885 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE); 886 return 0; 887 failed_rndis: 888 hn_nvs_detach(hv); 889 failed_nvs: 890 return error; 891 } 892 893 static void 894 hn_detach(struct hn_data *hv) 895 { 896 hn_nvs_detach(hv); 897 hn_rndis_detach(hv); 898 } 899 900 static int 901 eth_hn_dev_init(struct rte_eth_dev *eth_dev) 902 { 903 struct hn_data *hv = eth_dev->data->dev_private; 904 struct rte_device *device = eth_dev->device; 905 struct rte_vmbus_device *vmbus; 906 unsigned int rxr_cnt; 907 int err, max_chan; 908 909 PMD_INIT_FUNC_TRACE(); 910 911 vmbus = container_of(device, struct rte_vmbus_device, device); 912 eth_dev->dev_ops = &hn_eth_dev_ops; 913 eth_dev->tx_pkt_burst = &hn_xmit_pkts; 914 eth_dev->rx_pkt_burst = &hn_recv_pkts; 915 916 /* 917 * for secondary processes, we don't initialize any further as primary 918 * has already done this work. 919 */ 920 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 921 return 0; 922 923 /* Since Hyper-V only supports one MAC address, just use local data */ 924 eth_dev->data->mac_addrs = &hv->mac_addr; 925 926 hv->vmbus = vmbus; 927 hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP]; 928 hv->chim_res = &vmbus->resource[HV_SEND_BUF_MAP]; 929 hv->port_id = eth_dev->data->port_id; 930 hv->latency = HN_CHAN_LATENCY_NS; 931 hv->max_queues = 1; 932 rte_spinlock_init(&hv->vf_lock); 933 hv->vf_port = HN_INVALID_PORT; 934 935 err = hn_parse_args(eth_dev); 936 if (err) 937 return err; 938 939 strlcpy(hv->owner.name, eth_dev->device->name, 940 RTE_ETH_MAX_OWNER_NAME_LEN); 941 err = rte_eth_dev_owner_new(&hv->owner.id); 942 if (err) { 943 PMD_INIT_LOG(ERR, "Can not get owner id"); 944 return err; 945 } 946 947 /* Initialize primary channel input for control operations */ 948 err = rte_vmbus_chan_open(vmbus, &hv->channels[0]); 949 if (err) 950 return err; 951 952 rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency); 953 954 hv->primary = hn_rx_queue_alloc(hv, 0, 955 eth_dev->device->numa_node); 956 957 if (!hv->primary) 958 return -ENOMEM; 959 960 err = hn_attach(hv, RTE_ETHER_MTU); 961 if (err) 962 goto failed; 963 964 err = hn_tx_pool_init(eth_dev); 965 if (err) 966 goto failed; 967 968 err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes); 969 if (err) 970 goto failed; 971 972 /* Multi queue requires later versions of windows server */ 973 if (hv->nvs_ver < NVS_VERSION_5) 974 return 0; 975 976 max_chan = rte_vmbus_max_channels(vmbus); 977 PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan); 978 if (max_chan <= 0) 979 goto failed; 980 981 if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0) 982 rxr_cnt = 1; 983 984 hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan); 985 986 /* If VF was reported but not added, do it now */ 987 if (hv->vf_present && !hn_vf_attached(hv)) { 988 PMD_INIT_LOG(DEBUG, "Adding VF device"); 989 990 err = hn_vf_add(eth_dev, hv); 991 if (err) 992 hv->vf_present = 0; 993 } 994 995 return 0; 996 997 failed: 998 PMD_INIT_LOG(NOTICE, "device init failed"); 999 1000 hn_tx_pool_uninit(eth_dev); 1001 hn_detach(hv); 1002 return err; 1003 } 1004 1005 static int 1006 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev) 1007 { 1008 struct hn_data *hv = eth_dev->data->dev_private; 1009 int ret; 1010 1011 PMD_INIT_FUNC_TRACE(); 1012 1013 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 1014 return 0; 1015 1016 hn_dev_stop(eth_dev); 1017 hn_dev_close(eth_dev); 1018 1019 eth_dev->dev_ops = NULL; 1020 eth_dev->tx_pkt_burst = NULL; 1021 eth_dev->rx_pkt_burst = NULL; 1022 1023 hn_detach(hv); 1024 hn_tx_pool_uninit(eth_dev); 1025 rte_vmbus_chan_close(hv->primary->chan); 1026 rte_free(hv->primary); 1027 ret = rte_eth_dev_owner_delete(hv->owner.id); 1028 if (ret != 0) 1029 return ret; 1030 1031 return 0; 1032 } 1033 1034 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused, 1035 struct rte_vmbus_device *dev) 1036 { 1037 struct rte_eth_dev *eth_dev; 1038 int ret; 1039 1040 PMD_INIT_FUNC_TRACE(); 1041 1042 eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data)); 1043 if (!eth_dev) 1044 return -ENOMEM; 1045 1046 ret = eth_hn_dev_init(eth_dev); 1047 if (ret) 1048 eth_dev_vmbus_release(eth_dev); 1049 else 1050 rte_eth_dev_probing_finish(eth_dev); 1051 1052 return ret; 1053 } 1054 1055 static int eth_hn_remove(struct rte_vmbus_device *dev) 1056 { 1057 struct rte_eth_dev *eth_dev; 1058 int ret; 1059 1060 PMD_INIT_FUNC_TRACE(); 1061 1062 eth_dev = rte_eth_dev_allocated(dev->device.name); 1063 if (!eth_dev) 1064 return -ENODEV; 1065 1066 ret = eth_hn_dev_uninit(eth_dev); 1067 if (ret) 1068 return ret; 1069 1070 eth_dev_vmbus_release(eth_dev); 1071 return 0; 1072 } 1073 1074 /* Network device GUID */ 1075 static const rte_uuid_t hn_net_ids[] = { 1076 /* f8615163-df3e-46c5-913f-f2d2f965ed0e */ 1077 RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL), 1078 { 0 } 1079 }; 1080 1081 static struct rte_vmbus_driver rte_netvsc_pmd = { 1082 .id_table = hn_net_ids, 1083 .probe = eth_hn_probe, 1084 .remove = eth_hn_remove, 1085 }; 1086 1087 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd); 1088 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic"); 1089 1090 RTE_INIT(hn_init_log) 1091 { 1092 hn_logtype_init = rte_log_register("pmd.net.netvsc.init"); 1093 if (hn_logtype_init >= 0) 1094 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE); 1095 hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver"); 1096 if (hn_logtype_driver >= 0) 1097 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE); 1098 } 1099