1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016 Cavium, Inc 3 */ 4 5 #include <assert.h> 6 #include <stdio.h> 7 #include <stdbool.h> 8 #include <errno.h> 9 #include <stdint.h> 10 #include <string.h> 11 #include <unistd.h> 12 #include <stdarg.h> 13 #include <inttypes.h> 14 #include <netinet/in.h> 15 #include <sys/queue.h> 16 17 #include <rte_alarm.h> 18 #include <rte_branch_prediction.h> 19 #include <rte_byteorder.h> 20 #include <rte_common.h> 21 #include <rte_cycles.h> 22 #include <rte_debug.h> 23 #include <rte_dev.h> 24 #include <rte_eal.h> 25 #include <rte_ether.h> 26 #include <ethdev_driver.h> 27 #include <ethdev_pci.h> 28 #include <rte_interrupts.h> 29 #include <rte_log.h> 30 #include <rte_memory.h> 31 #include <rte_memzone.h> 32 #include <rte_malloc.h> 33 #include <rte_random.h> 34 #include <rte_pci.h> 35 #include <rte_bus_pci.h> 36 #include <rte_tailq.h> 37 #include <rte_devargs.h> 38 #include <rte_kvargs.h> 39 40 #include "base/nicvf_plat.h" 41 42 #include "nicvf_ethdev.h" 43 #include "nicvf_rxtx.h" 44 #include "nicvf_svf.h" 45 #include "nicvf_logs.h" 46 47 static int nicvf_dev_stop(struct rte_eth_dev *dev); 48 static void nicvf_dev_stop_cleanup(struct rte_eth_dev *dev, bool cleanup); 49 static void nicvf_vf_stop(struct rte_eth_dev *dev, struct nicvf *nic, 50 bool cleanup); 51 static int nicvf_vlan_offload_config(struct rte_eth_dev *dev, int mask); 52 static int nicvf_vlan_offload_set(struct rte_eth_dev *dev, int mask); 53 54 RTE_LOG_REGISTER_SUFFIX(nicvf_logtype_mbox, mbox, NOTICE); 55 RTE_LOG_REGISTER_SUFFIX(nicvf_logtype_init, init, NOTICE); 56 RTE_LOG_REGISTER_SUFFIX(nicvf_logtype_driver, driver, NOTICE); 57 58 #define NICVF_QLM_MODE_SGMII 7 59 #define NICVF_QLM_MODE_XFI 12 60 61 enum nicvf_link_speed { 62 NICVF_LINK_SPEED_SGMII, 63 NICVF_LINK_SPEED_XAUI, 64 NICVF_LINK_SPEED_RXAUI, 65 NICVF_LINK_SPEED_10G_R, 66 NICVF_LINK_SPEED_40G_R, 67 NICVF_LINK_SPEED_RESERVE1, 68 NICVF_LINK_SPEED_QSGMII, 69 NICVF_LINK_SPEED_RESERVE2, 70 NICVF_LINK_SPEED_UNKNOWN = 255 71 }; 72 73 static inline uint32_t 74 nicvf_parse_link_speeds(uint32_t link_speeds) 75 { 76 uint32_t link_speed = NICVF_LINK_SPEED_UNKNOWN; 77 78 if (link_speeds & RTE_ETH_LINK_SPEED_40G) 79 link_speed = NICVF_LINK_SPEED_40G_R; 80 81 if (link_speeds & RTE_ETH_LINK_SPEED_10G) { 82 link_speed = NICVF_LINK_SPEED_XAUI; 83 link_speed |= NICVF_LINK_SPEED_RXAUI; 84 link_speed |= NICVF_LINK_SPEED_10G_R; 85 } 86 87 if (link_speeds & RTE_ETH_LINK_SPEED_5G) 88 link_speed = NICVF_LINK_SPEED_QSGMII; 89 90 if (link_speeds & RTE_ETH_LINK_SPEED_1G) 91 link_speed = NICVF_LINK_SPEED_SGMII; 92 93 return link_speed; 94 } 95 96 static inline uint8_t 97 nicvf_parse_eth_link_duplex(uint32_t link_speeds) 98 { 99 if ((link_speeds & RTE_ETH_LINK_SPEED_10M_HD) || 100 (link_speeds & RTE_ETH_LINK_SPEED_100M_HD)) 101 return RTE_ETH_LINK_HALF_DUPLEX; 102 else 103 return RTE_ETH_LINK_FULL_DUPLEX; 104 } 105 106 static int 107 nicvf_apply_link_speed(struct rte_eth_dev *dev) 108 { 109 struct nicvf *nic = nicvf_pmd_priv(dev); 110 struct rte_eth_conf *conf = &dev->data->dev_conf; 111 struct change_link_mode cfg; 112 if (conf->link_speeds == RTE_ETH_LINK_SPEED_AUTONEG) 113 /* TODO: Handle this case */ 114 return 0; 115 116 cfg.speed = nicvf_parse_link_speeds(conf->link_speeds); 117 cfg.autoneg = (conf->link_speeds & RTE_ETH_LINK_SPEED_FIXED) ? 1 : 0; 118 cfg.duplex = nicvf_parse_eth_link_duplex(conf->link_speeds); 119 cfg.qlm_mode = ((conf->link_speeds & RTE_ETH_LINK_SPEED_1G) ? 120 NICVF_QLM_MODE_SGMII : 121 (conf->link_speeds & RTE_ETH_LINK_SPEED_10G) ? 122 NICVF_QLM_MODE_XFI : 0); 123 124 if (cfg.speed != NICVF_LINK_SPEED_UNKNOWN && 125 (cfg.speed != nic->speed || cfg.duplex != nic->duplex)) { 126 nic->speed = cfg.speed; 127 nic->duplex = cfg.duplex; 128 return nicvf_mbox_change_mode(nic, &cfg); 129 } else { 130 return 0; 131 } 132 } 133 134 static void 135 nicvf_link_status_update(struct nicvf *nic, 136 struct rte_eth_link *link) 137 { 138 memset(link, 0, sizeof(*link)); 139 140 link->link_status = nic->link_up ? RTE_ETH_LINK_UP : RTE_ETH_LINK_DOWN; 141 142 if (nic->duplex == NICVF_HALF_DUPLEX) 143 link->link_duplex = RTE_ETH_LINK_HALF_DUPLEX; 144 else if (nic->duplex == NICVF_FULL_DUPLEX) 145 link->link_duplex = RTE_ETH_LINK_FULL_DUPLEX; 146 link->link_speed = nic->speed; 147 link->link_autoneg = RTE_ETH_LINK_AUTONEG; 148 } 149 150 /*Poll for link status change by sending NIC_MBOX_MSG_BGX_LINK_CHANGE msg 151 * periodically to PF. 152 */ 153 static void 154 nicvf_interrupt(void *arg) 155 { 156 struct rte_eth_dev *dev = arg; 157 struct nicvf *nic = nicvf_pmd_priv(dev); 158 struct rte_eth_link link; 159 160 rte_eth_linkstatus_get(dev, &link); 161 162 nicvf_mbox_link_change(nic); 163 if (nic->link_up != link.link_status) { 164 if (dev->data->dev_conf.intr_conf.lsc) { 165 nicvf_link_status_update(nic, &link); 166 rte_eth_linkstatus_set(dev, &link); 167 168 rte_eth_dev_callback_process(dev, 169 RTE_ETH_EVENT_INTR_LSC, 170 NULL); 171 } 172 } 173 174 rte_eal_alarm_set(NICVF_INTR_LINK_POLL_INTERVAL_MS * 1000, 175 nicvf_interrupt, dev); 176 } 177 178 static void 179 nicvf_vf_interrupt(void *arg) 180 { 181 struct nicvf *nic = arg; 182 183 nicvf_reg_poll_interrupts(nic); 184 185 rte_eal_alarm_set(NICVF_INTR_POLL_INTERVAL_MS * 1000, 186 nicvf_vf_interrupt, nic); 187 } 188 189 static int 190 nicvf_periodic_alarm_start(void (fn)(void *), void *arg) 191 { 192 return rte_eal_alarm_set(NICVF_INTR_POLL_INTERVAL_MS * 1000, fn, arg); 193 } 194 195 static int 196 nicvf_periodic_alarm_stop(void (fn)(void *), void *arg) 197 { 198 return rte_eal_alarm_cancel(fn, arg); 199 } 200 201 /* 202 * Return 0 means link status changed, -1 means not changed 203 */ 204 static int 205 nicvf_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete) 206 { 207 #define CHECK_INTERVAL 100 /* 100ms */ 208 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 209 struct rte_eth_link link; 210 struct nicvf *nic = nicvf_pmd_priv(dev); 211 int i; 212 213 PMD_INIT_FUNC_TRACE(); 214 215 if (wait_to_complete) { 216 /* rte_eth_link_get() might need to wait up to 9 seconds */ 217 for (i = 0; i < MAX_CHECK_TIME; i++) { 218 nicvf_link_status_update(nic, &link); 219 if (link.link_status == RTE_ETH_LINK_UP) 220 break; 221 rte_delay_ms(CHECK_INTERVAL); 222 } 223 } else { 224 nicvf_link_status_update(nic, &link); 225 } 226 227 return rte_eth_linkstatus_set(dev, &link); 228 } 229 230 static int 231 nicvf_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu) 232 { 233 struct nicvf *nic = nicvf_pmd_priv(dev); 234 uint32_t buffsz, frame_size = mtu + NIC_HW_L2_OVERHEAD; 235 size_t i; 236 237 PMD_INIT_FUNC_TRACE(); 238 239 buffsz = dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM; 240 241 /* 242 * Refuse mtu that requires the support of scattered packets 243 * when this feature has not been enabled before. 244 */ 245 if (dev->data->dev_started && !dev->data->scattered_rx && 246 (frame_size + 2 * VLAN_TAG_SIZE > buffsz)) 247 return -EINVAL; 248 249 /* check <seg size> * <max_seg> >= max_frame */ 250 if (dev->data->scattered_rx && 251 (frame_size + 2 * VLAN_TAG_SIZE > buffsz * NIC_HW_MAX_SEGS)) 252 return -EINVAL; 253 254 if (nicvf_mbox_update_hw_max_frs(nic, mtu)) 255 return -EINVAL; 256 257 nic->mtu = mtu; 258 259 for (i = 0; i < nic->sqs_count; i++) 260 nic->snicvf[i]->mtu = mtu; 261 262 return 0; 263 } 264 265 static int 266 nicvf_dev_get_regs(struct rte_eth_dev *dev, struct rte_dev_reg_info *regs) 267 { 268 uint64_t *data = regs->data; 269 struct nicvf *nic = nicvf_pmd_priv(dev); 270 271 if (data == NULL) { 272 regs->length = nicvf_reg_get_count(); 273 regs->width = THUNDERX_REG_BYTES; 274 return 0; 275 } 276 277 /* Support only full register dump */ 278 if ((regs->length == 0) || 279 (regs->length == (uint32_t)nicvf_reg_get_count())) { 280 regs->version = nic->vendor_id << 16 | nic->device_id; 281 nicvf_reg_dump(nic, data); 282 return 0; 283 } 284 return -ENOTSUP; 285 } 286 287 static int 288 nicvf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 289 { 290 uint16_t qidx; 291 struct nicvf_hw_rx_qstats rx_qstats; 292 struct nicvf_hw_tx_qstats tx_qstats; 293 struct nicvf_hw_stats port_stats; 294 struct nicvf *nic = nicvf_pmd_priv(dev); 295 uint16_t rx_start, rx_end; 296 uint16_t tx_start, tx_end; 297 size_t i; 298 299 /* RX queue indices for the first VF */ 300 nicvf_rx_range(dev, nic, &rx_start, &rx_end); 301 302 /* Reading per RX ring stats */ 303 for (qidx = rx_start; qidx <= rx_end; qidx++) { 304 if (qidx >= RTE_ETHDEV_QUEUE_STAT_CNTRS) 305 break; 306 307 nicvf_hw_get_rx_qstats(nic, &rx_qstats, qidx); 308 stats->q_ibytes[qidx] = rx_qstats.q_rx_bytes; 309 stats->q_ipackets[qidx] = rx_qstats.q_rx_packets; 310 } 311 312 /* TX queue indices for the first VF */ 313 nicvf_tx_range(dev, nic, &tx_start, &tx_end); 314 315 /* Reading per TX ring stats */ 316 for (qidx = tx_start; qidx <= tx_end; qidx++) { 317 if (qidx >= RTE_ETHDEV_QUEUE_STAT_CNTRS) 318 break; 319 320 nicvf_hw_get_tx_qstats(nic, &tx_qstats, qidx); 321 stats->q_obytes[qidx] = tx_qstats.q_tx_bytes; 322 stats->q_opackets[qidx] = tx_qstats.q_tx_packets; 323 } 324 325 for (i = 0; i < nic->sqs_count; i++) { 326 struct nicvf *snic = nic->snicvf[i]; 327 328 if (snic == NULL) 329 break; 330 331 /* RX queue indices for a secondary VF */ 332 nicvf_rx_range(dev, snic, &rx_start, &rx_end); 333 334 /* Reading per RX ring stats */ 335 for (qidx = rx_start; qidx <= rx_end; qidx++) { 336 if (qidx >= RTE_ETHDEV_QUEUE_STAT_CNTRS) 337 break; 338 339 nicvf_hw_get_rx_qstats(snic, &rx_qstats, 340 qidx % MAX_RCV_QUEUES_PER_QS); 341 stats->q_ibytes[qidx] = rx_qstats.q_rx_bytes; 342 stats->q_ipackets[qidx] = rx_qstats.q_rx_packets; 343 } 344 345 /* TX queue indices for a secondary VF */ 346 nicvf_tx_range(dev, snic, &tx_start, &tx_end); 347 /* Reading per TX ring stats */ 348 for (qidx = tx_start; qidx <= tx_end; qidx++) { 349 if (qidx >= RTE_ETHDEV_QUEUE_STAT_CNTRS) 350 break; 351 352 nicvf_hw_get_tx_qstats(snic, &tx_qstats, 353 qidx % MAX_SND_QUEUES_PER_QS); 354 stats->q_obytes[qidx] = tx_qstats.q_tx_bytes; 355 stats->q_opackets[qidx] = tx_qstats.q_tx_packets; 356 } 357 } 358 359 nicvf_hw_get_stats(nic, &port_stats); 360 stats->ibytes = port_stats.rx_bytes; 361 stats->ipackets = port_stats.rx_ucast_frames; 362 stats->ipackets += port_stats.rx_bcast_frames; 363 stats->ipackets += port_stats.rx_mcast_frames; 364 stats->ierrors = port_stats.rx_l2_errors; 365 stats->imissed = port_stats.rx_drop_red; 366 stats->imissed += port_stats.rx_drop_overrun; 367 stats->imissed += port_stats.rx_drop_bcast; 368 stats->imissed += port_stats.rx_drop_mcast; 369 stats->imissed += port_stats.rx_drop_l3_bcast; 370 stats->imissed += port_stats.rx_drop_l3_mcast; 371 372 stats->obytes = port_stats.tx_bytes_ok; 373 stats->opackets = port_stats.tx_ucast_frames_ok; 374 stats->opackets += port_stats.tx_bcast_frames_ok; 375 stats->opackets += port_stats.tx_mcast_frames_ok; 376 stats->oerrors = port_stats.tx_drops; 377 378 return 0; 379 } 380 381 static const uint32_t * 382 nicvf_dev_supported_ptypes_get(struct rte_eth_dev *dev) 383 { 384 size_t copied; 385 static uint32_t ptypes[32]; 386 struct nicvf *nic = nicvf_pmd_priv(dev); 387 static const uint32_t ptypes_common[] = { 388 RTE_PTYPE_L3_IPV4, 389 RTE_PTYPE_L3_IPV4_EXT, 390 RTE_PTYPE_L3_IPV6, 391 RTE_PTYPE_L3_IPV6_EXT, 392 RTE_PTYPE_L4_TCP, 393 RTE_PTYPE_L4_UDP, 394 RTE_PTYPE_L4_FRAG, 395 }; 396 static const uint32_t ptypes_tunnel[] = { 397 RTE_PTYPE_TUNNEL_GRE, 398 RTE_PTYPE_TUNNEL_GENEVE, 399 RTE_PTYPE_TUNNEL_VXLAN, 400 RTE_PTYPE_TUNNEL_NVGRE, 401 }; 402 static const uint32_t ptypes_end = RTE_PTYPE_UNKNOWN; 403 404 copied = sizeof(ptypes_common); 405 memcpy(ptypes, ptypes_common, copied); 406 if (nicvf_hw_cap(nic) & NICVF_CAP_TUNNEL_PARSING) { 407 memcpy((char *)ptypes + copied, ptypes_tunnel, 408 sizeof(ptypes_tunnel)); 409 copied += sizeof(ptypes_tunnel); 410 } 411 412 memcpy((char *)ptypes + copied, &ptypes_end, sizeof(ptypes_end)); 413 414 /* All Ptypes are supported in all Rx functions. */ 415 return ptypes; 416 } 417 418 static int 419 nicvf_dev_stats_reset(struct rte_eth_dev *dev) 420 { 421 int i; 422 uint16_t rxqs = 0, txqs = 0; 423 struct nicvf *nic = nicvf_pmd_priv(dev); 424 uint16_t rx_start, rx_end; 425 uint16_t tx_start, tx_end; 426 int ret; 427 428 /* Reset all primary nic counters */ 429 nicvf_rx_range(dev, nic, &rx_start, &rx_end); 430 for (i = rx_start; i <= rx_end; i++) 431 rxqs |= (0x3 << (i * 2)); 432 433 nicvf_tx_range(dev, nic, &tx_start, &tx_end); 434 for (i = tx_start; i <= tx_end; i++) 435 txqs |= (0x3 << (i * 2)); 436 437 ret = nicvf_mbox_reset_stat_counters(nic, 0x3FFF, 0x1F, rxqs, txqs); 438 if (ret != 0) 439 return ret; 440 441 /* Reset secondary nic queue counters */ 442 for (i = 0; i < nic->sqs_count; i++) { 443 struct nicvf *snic = nic->snicvf[i]; 444 if (snic == NULL) 445 break; 446 447 nicvf_rx_range(dev, snic, &rx_start, &rx_end); 448 for (i = rx_start; i <= rx_end; i++) 449 rxqs |= (0x3 << ((i % MAX_CMP_QUEUES_PER_QS) * 2)); 450 451 nicvf_tx_range(dev, snic, &tx_start, &tx_end); 452 for (i = tx_start; i <= tx_end; i++) 453 txqs |= (0x3 << ((i % MAX_SND_QUEUES_PER_QS) * 2)); 454 455 ret = nicvf_mbox_reset_stat_counters(snic, 0, 0, rxqs, txqs); 456 if (ret != 0) 457 return ret; 458 } 459 460 return 0; 461 } 462 463 /* Promiscuous mode enabled by default in LMAC to VF 1:1 map configuration */ 464 static int 465 nicvf_dev_promisc_enable(struct rte_eth_dev *dev __rte_unused) 466 { 467 return 0; 468 } 469 470 static inline uint64_t 471 nicvf_rss_ethdev_to_nic(struct nicvf *nic, uint64_t ethdev_rss) 472 { 473 uint64_t nic_rss = 0; 474 475 if (ethdev_rss & RTE_ETH_RSS_IPV4) 476 nic_rss |= RSS_IP_ENA; 477 478 if (ethdev_rss & RTE_ETH_RSS_IPV6) 479 nic_rss |= RSS_IP_ENA; 480 481 if (ethdev_rss & RTE_ETH_RSS_NONFRAG_IPV4_UDP) 482 nic_rss |= (RSS_IP_ENA | RSS_UDP_ENA); 483 484 if (ethdev_rss & RTE_ETH_RSS_NONFRAG_IPV4_TCP) 485 nic_rss |= (RSS_IP_ENA | RSS_TCP_ENA); 486 487 if (ethdev_rss & RTE_ETH_RSS_NONFRAG_IPV6_UDP) 488 nic_rss |= (RSS_IP_ENA | RSS_UDP_ENA); 489 490 if (ethdev_rss & RTE_ETH_RSS_NONFRAG_IPV6_TCP) 491 nic_rss |= (RSS_IP_ENA | RSS_TCP_ENA); 492 493 if (ethdev_rss & RTE_ETH_RSS_PORT) 494 nic_rss |= RSS_L2_EXTENDED_HASH_ENA; 495 496 if (nicvf_hw_cap(nic) & NICVF_CAP_TUNNEL_PARSING) { 497 if (ethdev_rss & RTE_ETH_RSS_VXLAN) 498 nic_rss |= RSS_TUN_VXLAN_ENA; 499 500 if (ethdev_rss & RTE_ETH_RSS_GENEVE) 501 nic_rss |= RSS_TUN_GENEVE_ENA; 502 503 if (ethdev_rss & RTE_ETH_RSS_NVGRE) 504 nic_rss |= RSS_TUN_NVGRE_ENA; 505 } 506 507 return nic_rss; 508 } 509 510 static inline uint64_t 511 nicvf_rss_nic_to_ethdev(struct nicvf *nic, uint64_t nic_rss) 512 { 513 uint64_t ethdev_rss = 0; 514 515 if (nic_rss & RSS_IP_ENA) 516 ethdev_rss |= (RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_IPV6); 517 518 if ((nic_rss & RSS_IP_ENA) && (nic_rss & RSS_TCP_ENA)) 519 ethdev_rss |= (RTE_ETH_RSS_NONFRAG_IPV4_TCP | 520 RTE_ETH_RSS_NONFRAG_IPV6_TCP); 521 522 if ((nic_rss & RSS_IP_ENA) && (nic_rss & RSS_UDP_ENA)) 523 ethdev_rss |= (RTE_ETH_RSS_NONFRAG_IPV4_UDP | 524 RTE_ETH_RSS_NONFRAG_IPV6_UDP); 525 526 if (nic_rss & RSS_L2_EXTENDED_HASH_ENA) 527 ethdev_rss |= RTE_ETH_RSS_PORT; 528 529 if (nicvf_hw_cap(nic) & NICVF_CAP_TUNNEL_PARSING) { 530 if (nic_rss & RSS_TUN_VXLAN_ENA) 531 ethdev_rss |= RTE_ETH_RSS_VXLAN; 532 533 if (nic_rss & RSS_TUN_GENEVE_ENA) 534 ethdev_rss |= RTE_ETH_RSS_GENEVE; 535 536 if (nic_rss & RSS_TUN_NVGRE_ENA) 537 ethdev_rss |= RTE_ETH_RSS_NVGRE; 538 } 539 return ethdev_rss; 540 } 541 542 static int 543 nicvf_dev_reta_query(struct rte_eth_dev *dev, 544 struct rte_eth_rss_reta_entry64 *reta_conf, 545 uint16_t reta_size) 546 { 547 struct nicvf *nic = nicvf_pmd_priv(dev); 548 uint8_t tbl[NIC_MAX_RSS_IDR_TBL_SIZE]; 549 int ret, i, j; 550 551 if (reta_size != NIC_MAX_RSS_IDR_TBL_SIZE) { 552 PMD_DRV_LOG(ERR, 553 "The size of hash lookup table configured " 554 "(%u) doesn't match the number hardware can supported " 555 "(%u)", reta_size, NIC_MAX_RSS_IDR_TBL_SIZE); 556 return -EINVAL; 557 } 558 559 ret = nicvf_rss_reta_query(nic, tbl, NIC_MAX_RSS_IDR_TBL_SIZE); 560 if (ret) 561 return ret; 562 563 /* Copy RETA table */ 564 for (i = 0; i < (NIC_MAX_RSS_IDR_TBL_SIZE / RTE_ETH_RETA_GROUP_SIZE); i++) { 565 for (j = 0; j < RTE_ETH_RETA_GROUP_SIZE; j++) 566 if ((reta_conf[i].mask >> j) & 0x01) 567 reta_conf[i].reta[j] = tbl[j]; 568 } 569 570 return 0; 571 } 572 573 static int 574 nicvf_dev_reta_update(struct rte_eth_dev *dev, 575 struct rte_eth_rss_reta_entry64 *reta_conf, 576 uint16_t reta_size) 577 { 578 struct nicvf *nic = nicvf_pmd_priv(dev); 579 uint8_t tbl[NIC_MAX_RSS_IDR_TBL_SIZE]; 580 int ret, i, j; 581 582 if (reta_size != NIC_MAX_RSS_IDR_TBL_SIZE) { 583 PMD_DRV_LOG(ERR, "The size of hash lookup table configured " 584 "(%u) doesn't match the number hardware can supported " 585 "(%u)", reta_size, NIC_MAX_RSS_IDR_TBL_SIZE); 586 return -EINVAL; 587 } 588 589 ret = nicvf_rss_reta_query(nic, tbl, NIC_MAX_RSS_IDR_TBL_SIZE); 590 if (ret) 591 return ret; 592 593 /* Copy RETA table */ 594 for (i = 0; i < (NIC_MAX_RSS_IDR_TBL_SIZE / RTE_ETH_RETA_GROUP_SIZE); i++) { 595 for (j = 0; j < RTE_ETH_RETA_GROUP_SIZE; j++) 596 if ((reta_conf[i].mask >> j) & 0x01) 597 tbl[j] = reta_conf[i].reta[j]; 598 } 599 600 return nicvf_rss_reta_update(nic, tbl, NIC_MAX_RSS_IDR_TBL_SIZE); 601 } 602 603 static int 604 nicvf_dev_rss_hash_conf_get(struct rte_eth_dev *dev, 605 struct rte_eth_rss_conf *rss_conf) 606 { 607 struct nicvf *nic = nicvf_pmd_priv(dev); 608 609 if (rss_conf->rss_key) 610 nicvf_rss_get_key(nic, rss_conf->rss_key); 611 612 rss_conf->rss_key_len = RSS_HASH_KEY_BYTE_SIZE; 613 rss_conf->rss_hf = nicvf_rss_nic_to_ethdev(nic, nicvf_rss_get_cfg(nic)); 614 return 0; 615 } 616 617 static int 618 nicvf_dev_rss_hash_update(struct rte_eth_dev *dev, 619 struct rte_eth_rss_conf *rss_conf) 620 { 621 struct nicvf *nic = nicvf_pmd_priv(dev); 622 uint64_t nic_rss; 623 624 if (rss_conf->rss_key && 625 rss_conf->rss_key_len != RSS_HASH_KEY_BYTE_SIZE) { 626 PMD_DRV_LOG(ERR, "Hash key size mismatch %u", 627 rss_conf->rss_key_len); 628 return -EINVAL; 629 } 630 631 if (rss_conf->rss_key) 632 nicvf_rss_set_key(nic, rss_conf->rss_key); 633 634 nic_rss = nicvf_rss_ethdev_to_nic(nic, rss_conf->rss_hf); 635 nicvf_rss_set_cfg(nic, nic_rss); 636 return 0; 637 } 638 639 static int 640 nicvf_qset_cq_alloc(struct rte_eth_dev *dev, struct nicvf *nic, 641 struct nicvf_rxq *rxq, uint16_t qidx, uint32_t desc_cnt) 642 { 643 const struct rte_memzone *rz; 644 uint32_t ring_size = CMP_QUEUE_SZ_MAX * sizeof(union cq_entry_t); 645 646 rz = rte_eth_dma_zone_reserve(dev, "cq_ring", 647 nicvf_netdev_qidx(nic, qidx), ring_size, 648 NICVF_CQ_BASE_ALIGN_BYTES, nic->node); 649 if (rz == NULL) { 650 PMD_INIT_LOG(ERR, "Failed to allocate mem for cq hw ring"); 651 return -ENOMEM; 652 } 653 654 memset(rz->addr, 0, ring_size); 655 656 rxq->phys = rz->iova; 657 rxq->desc = rz->addr; 658 rxq->qlen_mask = desc_cnt - 1; 659 660 return 0; 661 } 662 663 static int 664 nicvf_qset_sq_alloc(struct rte_eth_dev *dev, struct nicvf *nic, 665 struct nicvf_txq *sq, uint16_t qidx, uint32_t desc_cnt) 666 { 667 const struct rte_memzone *rz; 668 uint32_t ring_size = SND_QUEUE_SZ_MAX * sizeof(union sq_entry_t); 669 670 rz = rte_eth_dma_zone_reserve(dev, "sq", 671 nicvf_netdev_qidx(nic, qidx), ring_size, 672 NICVF_SQ_BASE_ALIGN_BYTES, nic->node); 673 if (rz == NULL) { 674 PMD_INIT_LOG(ERR, "Failed allocate mem for sq hw ring"); 675 return -ENOMEM; 676 } 677 678 memset(rz->addr, 0, ring_size); 679 680 sq->phys = rz->iova; 681 sq->desc = rz->addr; 682 sq->qlen_mask = desc_cnt - 1; 683 684 return 0; 685 } 686 687 static int 688 nicvf_qset_rbdr_alloc(struct rte_eth_dev *dev, struct nicvf *nic, 689 uint32_t desc_cnt, uint32_t buffsz) 690 { 691 struct nicvf_rbdr *rbdr; 692 const struct rte_memzone *rz; 693 uint32_t ring_size; 694 695 assert(nic->rbdr == NULL); 696 rbdr = rte_zmalloc_socket("rbdr", sizeof(struct nicvf_rbdr), 697 RTE_CACHE_LINE_SIZE, nic->node); 698 if (rbdr == NULL) { 699 PMD_INIT_LOG(ERR, "Failed to allocate mem for rbdr"); 700 return -ENOMEM; 701 } 702 703 ring_size = sizeof(struct rbdr_entry_t) * RBDR_QUEUE_SZ_MAX; 704 rz = rte_eth_dma_zone_reserve(dev, "rbdr", 705 nicvf_netdev_qidx(nic, 0), ring_size, 706 NICVF_RBDR_BASE_ALIGN_BYTES, nic->node); 707 if (rz == NULL) { 708 PMD_INIT_LOG(ERR, "Failed to allocate mem for rbdr desc ring"); 709 rte_free(rbdr); 710 return -ENOMEM; 711 } 712 713 memset(rz->addr, 0, ring_size); 714 715 rbdr->phys = rz->iova; 716 rbdr->tail = 0; 717 rbdr->next_tail = 0; 718 rbdr->desc = rz->addr; 719 rbdr->buffsz = buffsz; 720 rbdr->qlen_mask = desc_cnt - 1; 721 rbdr->rbdr_status = 722 nicvf_qset_base(nic, 0) + NIC_QSET_RBDR_0_1_STATUS0; 723 rbdr->rbdr_door = 724 nicvf_qset_base(nic, 0) + NIC_QSET_RBDR_0_1_DOOR; 725 726 nic->rbdr = rbdr; 727 return 0; 728 } 729 730 static void 731 nicvf_rbdr_release_mbuf(struct rte_eth_dev *dev, struct nicvf *nic, 732 nicvf_iova_addr_t phy) 733 { 734 uint16_t qidx; 735 void *obj; 736 struct nicvf_rxq *rxq; 737 uint16_t rx_start, rx_end; 738 739 /* Get queue ranges for this VF */ 740 nicvf_rx_range(dev, nic, &rx_start, &rx_end); 741 742 for (qidx = rx_start; qidx <= rx_end; qidx++) { 743 rxq = dev->data->rx_queues[qidx]; 744 if (rxq->precharge_cnt) { 745 obj = (void *)nicvf_mbuff_phy2virt(phy, 746 rxq->mbuf_phys_off); 747 rte_mempool_put(rxq->pool, obj); 748 rxq->precharge_cnt--; 749 break; 750 } 751 } 752 } 753 754 static inline void 755 nicvf_rbdr_release_mbufs(struct rte_eth_dev *dev, struct nicvf *nic) 756 { 757 uint32_t qlen_mask, head; 758 struct rbdr_entry_t *entry; 759 struct nicvf_rbdr *rbdr = nic->rbdr; 760 761 qlen_mask = rbdr->qlen_mask; 762 head = rbdr->head; 763 while (head != rbdr->tail) { 764 entry = rbdr->desc + head; 765 nicvf_rbdr_release_mbuf(dev, nic, entry->full_addr); 766 head++; 767 head = head & qlen_mask; 768 } 769 } 770 771 static inline void 772 nicvf_tx_queue_release_mbufs(struct nicvf_txq *txq) 773 { 774 uint32_t head; 775 776 head = txq->head; 777 while (head != txq->tail) { 778 if (txq->txbuffs[head]) { 779 rte_pktmbuf_free_seg(txq->txbuffs[head]); 780 txq->txbuffs[head] = NULL; 781 } 782 head++; 783 head = head & txq->qlen_mask; 784 } 785 } 786 787 static void 788 nicvf_tx_queue_reset(struct nicvf_txq *txq) 789 { 790 uint32_t txq_desc_cnt = txq->qlen_mask + 1; 791 792 memset(txq->desc, 0, sizeof(union sq_entry_t) * txq_desc_cnt); 793 memset(txq->txbuffs, 0, sizeof(struct rte_mbuf *) * txq_desc_cnt); 794 txq->tail = 0; 795 txq->head = 0; 796 txq->xmit_bufs = 0; 797 } 798 799 static inline int 800 nicvf_vf_start_tx_queue(struct rte_eth_dev *dev, struct nicvf *nic, 801 uint16_t qidx) 802 { 803 struct nicvf_txq *txq; 804 int ret; 805 806 assert(qidx < MAX_SND_QUEUES_PER_QS); 807 808 if (dev->data->tx_queue_state[nicvf_netdev_qidx(nic, qidx)] == 809 RTE_ETH_QUEUE_STATE_STARTED) 810 return 0; 811 812 txq = dev->data->tx_queues[nicvf_netdev_qidx(nic, qidx)]; 813 txq->pool = NULL; 814 ret = nicvf_qset_sq_config(nic, qidx, txq); 815 if (ret) { 816 PMD_INIT_LOG(ERR, "Failed to configure sq VF%d %d %d", 817 nic->vf_id, qidx, ret); 818 goto config_sq_error; 819 } 820 821 dev->data->tx_queue_state[nicvf_netdev_qidx(nic, qidx)] = 822 RTE_ETH_QUEUE_STATE_STARTED; 823 return ret; 824 825 config_sq_error: 826 nicvf_qset_sq_reclaim(nic, qidx); 827 return ret; 828 } 829 830 static inline int 831 nicvf_vf_stop_tx_queue(struct rte_eth_dev *dev, struct nicvf *nic, 832 uint16_t qidx) 833 { 834 struct nicvf_txq *txq; 835 int ret; 836 837 assert(qidx < MAX_SND_QUEUES_PER_QS); 838 839 if (dev->data->tx_queue_state[nicvf_netdev_qidx(nic, qidx)] == 840 RTE_ETH_QUEUE_STATE_STOPPED) 841 return 0; 842 843 ret = nicvf_qset_sq_reclaim(nic, qidx); 844 if (ret) 845 PMD_INIT_LOG(ERR, "Failed to reclaim sq VF%d %d %d", 846 nic->vf_id, qidx, ret); 847 848 txq = dev->data->tx_queues[nicvf_netdev_qidx(nic, qidx)]; 849 nicvf_tx_queue_release_mbufs(txq); 850 nicvf_tx_queue_reset(txq); 851 852 dev->data->tx_queue_state[nicvf_netdev_qidx(nic, qidx)] = 853 RTE_ETH_QUEUE_STATE_STOPPED; 854 return ret; 855 } 856 857 static inline int 858 nicvf_configure_cpi(struct rte_eth_dev *dev) 859 { 860 struct nicvf *nic = nicvf_pmd_priv(dev); 861 uint16_t qidx, qcnt; 862 int ret; 863 864 /* Count started rx queues */ 865 for (qidx = qcnt = 0; qidx < dev->data->nb_rx_queues; qidx++) 866 if (dev->data->rx_queue_state[qidx] == 867 RTE_ETH_QUEUE_STATE_STARTED) 868 qcnt++; 869 870 nic->cpi_alg = CPI_ALG_NONE; 871 ret = nicvf_mbox_config_cpi(nic, qcnt); 872 if (ret) 873 PMD_INIT_LOG(ERR, "Failed to configure CPI %d", ret); 874 875 return ret; 876 } 877 878 static inline int 879 nicvf_configure_rss(struct rte_eth_dev *dev) 880 { 881 struct nicvf *nic = nicvf_pmd_priv(dev); 882 uint64_t rsshf; 883 int ret = -EINVAL; 884 885 rsshf = nicvf_rss_ethdev_to_nic(nic, 886 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf); 887 PMD_DRV_LOG(INFO, "mode=%d rx_queues=%d loopback=%d rsshf=0x%" PRIx64, 888 dev->data->dev_conf.rxmode.mq_mode, 889 dev->data->nb_rx_queues, 890 dev->data->dev_conf.lpbk_mode, rsshf); 891 892 if (dev->data->dev_conf.rxmode.mq_mode == RTE_ETH_MQ_RX_NONE) 893 ret = nicvf_rss_term(nic); 894 else if (dev->data->dev_conf.rxmode.mq_mode == RTE_ETH_MQ_RX_RSS) 895 ret = nicvf_rss_config(nic, dev->data->nb_rx_queues, rsshf); 896 if (ret) 897 PMD_INIT_LOG(ERR, "Failed to configure RSS %d", ret); 898 899 return ret; 900 } 901 902 static int 903 nicvf_configure_rss_reta(struct rte_eth_dev *dev) 904 { 905 struct nicvf *nic = nicvf_pmd_priv(dev); 906 unsigned int idx, qmap_size; 907 uint8_t qmap[RTE_MAX_QUEUES_PER_PORT]; 908 uint8_t default_reta[NIC_MAX_RSS_IDR_TBL_SIZE]; 909 910 if (nic->cpi_alg != CPI_ALG_NONE) 911 return -EINVAL; 912 913 /* Prepare queue map */ 914 for (idx = 0, qmap_size = 0; idx < dev->data->nb_rx_queues; idx++) { 915 if (dev->data->rx_queue_state[idx] == 916 RTE_ETH_QUEUE_STATE_STARTED) 917 qmap[qmap_size++] = idx; 918 } 919 920 /* Update default RSS RETA */ 921 for (idx = 0; idx < NIC_MAX_RSS_IDR_TBL_SIZE; idx++) 922 default_reta[idx] = qmap[idx % qmap_size]; 923 924 return nicvf_rss_reta_update(nic, default_reta, 925 NIC_MAX_RSS_IDR_TBL_SIZE); 926 } 927 928 static void 929 nicvf_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid) 930 { 931 struct nicvf_txq *txq = dev->data->tx_queues[qid]; 932 933 PMD_INIT_FUNC_TRACE(); 934 935 if (txq) { 936 if (txq->txbuffs != NULL) { 937 nicvf_tx_queue_release_mbufs(txq); 938 rte_free(txq->txbuffs); 939 txq->txbuffs = NULL; 940 } 941 rte_free(txq); 942 dev->data->tx_queues[qid] = NULL; 943 } 944 } 945 946 static void 947 nicvf_set_tx_function(struct rte_eth_dev *dev) 948 { 949 struct nicvf_txq *txq = NULL; 950 size_t i; 951 bool multiseg = false; 952 953 for (i = 0; i < dev->data->nb_tx_queues; i++) { 954 txq = dev->data->tx_queues[i]; 955 if (txq->offloads & RTE_ETH_TX_OFFLOAD_MULTI_SEGS) { 956 multiseg = true; 957 break; 958 } 959 } 960 961 /* Use a simple Tx queue (no offloads, no multi segs) if possible */ 962 if (multiseg) { 963 PMD_DRV_LOG(DEBUG, "Using multi-segment tx callback"); 964 dev->tx_pkt_burst = nicvf_xmit_pkts_multiseg; 965 } else { 966 PMD_DRV_LOG(DEBUG, "Using single-segment tx callback"); 967 dev->tx_pkt_burst = nicvf_xmit_pkts; 968 } 969 970 if (!txq) 971 return; 972 973 if (txq->pool_free == nicvf_single_pool_free_xmited_buffers) 974 PMD_DRV_LOG(DEBUG, "Using single-mempool tx free method"); 975 else 976 PMD_DRV_LOG(DEBUG, "Using multi-mempool tx free method"); 977 } 978 979 static void 980 nicvf_set_rx_function(struct rte_eth_dev *dev) 981 { 982 struct nicvf *nic = nicvf_pmd_priv(dev); 983 984 const eth_rx_burst_t rx_burst_func[2][2][2] = { 985 /* [NORMAL/SCATTER] [CKSUM/NO_CKSUM] [VLAN_STRIP/NO_VLAN_STRIP] */ 986 [0][0][0] = nicvf_recv_pkts_no_offload, 987 [0][0][1] = nicvf_recv_pkts_vlan_strip, 988 [0][1][0] = nicvf_recv_pkts_cksum, 989 [0][1][1] = nicvf_recv_pkts_cksum_vlan_strip, 990 [1][0][0] = nicvf_recv_pkts_multiseg_no_offload, 991 [1][0][1] = nicvf_recv_pkts_multiseg_vlan_strip, 992 [1][1][0] = nicvf_recv_pkts_multiseg_cksum, 993 [1][1][1] = nicvf_recv_pkts_multiseg_cksum_vlan_strip, 994 }; 995 996 dev->rx_pkt_burst = 997 rx_burst_func[dev->data->scattered_rx] 998 [nic->offload_cksum][nic->vlan_strip]; 999 } 1000 1001 static int 1002 nicvf_dev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx, 1003 uint16_t nb_desc, unsigned int socket_id, 1004 const struct rte_eth_txconf *tx_conf) 1005 { 1006 uint16_t tx_free_thresh; 1007 bool is_single_pool; 1008 struct nicvf_txq *txq; 1009 struct nicvf *nic = nicvf_pmd_priv(dev); 1010 uint64_t offloads; 1011 1012 PMD_INIT_FUNC_TRACE(); 1013 1014 if (qidx >= MAX_SND_QUEUES_PER_QS) 1015 nic = nic->snicvf[qidx / MAX_SND_QUEUES_PER_QS - 1]; 1016 1017 qidx = qidx % MAX_SND_QUEUES_PER_QS; 1018 1019 /* Socket id check */ 1020 if (socket_id != (unsigned int)SOCKET_ID_ANY && socket_id != nic->node) 1021 PMD_DRV_LOG(WARNING, "socket_id expected %d, configured %d", 1022 socket_id, nic->node); 1023 1024 /* Tx deferred start is not supported */ 1025 if (tx_conf->tx_deferred_start) { 1026 PMD_INIT_LOG(ERR, "Tx deferred start not supported"); 1027 return -EINVAL; 1028 } 1029 1030 /* Roundup nb_desc to available qsize and validate max number of desc */ 1031 nb_desc = nicvf_qsize_sq_roundup(nb_desc); 1032 if (nb_desc == 0) { 1033 PMD_INIT_LOG(ERR, "Value of nb_desc beyond available sq qsize"); 1034 return -EINVAL; 1035 } 1036 1037 /* Validate tx_free_thresh */ 1038 tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ? 1039 tx_conf->tx_free_thresh : 1040 NICVF_DEFAULT_TX_FREE_THRESH); 1041 1042 if (tx_free_thresh > (nb_desc) || 1043 tx_free_thresh > NICVF_MAX_TX_FREE_THRESH) { 1044 PMD_INIT_LOG(ERR, 1045 "tx_free_thresh must be less than the number of TX " 1046 "descriptors. (tx_free_thresh=%u port=%d " 1047 "queue=%d)", (unsigned int)tx_free_thresh, 1048 (int)dev->data->port_id, (int)qidx); 1049 return -EINVAL; 1050 } 1051 1052 /* Free memory prior to re-allocation if needed. */ 1053 if (dev->data->tx_queues[nicvf_netdev_qidx(nic, qidx)] != NULL) { 1054 PMD_TX_LOG(DEBUG, "Freeing memory prior to re-allocation %d", 1055 nicvf_netdev_qidx(nic, qidx)); 1056 nicvf_dev_tx_queue_release(dev, nicvf_netdev_qidx(nic, qidx)); 1057 dev->data->tx_queues[nicvf_netdev_qidx(nic, qidx)] = NULL; 1058 } 1059 1060 /* Allocating tx queue data structure */ 1061 txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct nicvf_txq), 1062 RTE_CACHE_LINE_SIZE, nic->node); 1063 if (txq == NULL) { 1064 PMD_INIT_LOG(ERR, "Failed to allocate txq=%d", 1065 nicvf_netdev_qidx(nic, qidx)); 1066 return -ENOMEM; 1067 } 1068 1069 txq->nic = nic; 1070 txq->queue_id = qidx; 1071 txq->tx_free_thresh = tx_free_thresh; 1072 txq->sq_head = nicvf_qset_base(nic, qidx) + NIC_QSET_SQ_0_7_HEAD; 1073 txq->sq_door = nicvf_qset_base(nic, qidx) + NIC_QSET_SQ_0_7_DOOR; 1074 offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads; 1075 txq->offloads = offloads; 1076 1077 is_single_pool = !!(offloads & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE); 1078 1079 /* Choose optimum free threshold value for multipool case */ 1080 if (!is_single_pool) { 1081 txq->tx_free_thresh = (uint16_t) 1082 (tx_conf->tx_free_thresh == NICVF_DEFAULT_TX_FREE_THRESH ? 1083 NICVF_TX_FREE_MPOOL_THRESH : 1084 tx_conf->tx_free_thresh); 1085 txq->pool_free = nicvf_multi_pool_free_xmited_buffers; 1086 } else { 1087 txq->pool_free = nicvf_single_pool_free_xmited_buffers; 1088 } 1089 1090 dev->data->tx_queues[nicvf_netdev_qidx(nic, qidx)] = txq; 1091 1092 /* Allocate software ring */ 1093 txq->txbuffs = rte_zmalloc_socket("txq->txbuffs", 1094 nb_desc * sizeof(struct rte_mbuf *), 1095 RTE_CACHE_LINE_SIZE, nic->node); 1096 1097 if (txq->txbuffs == NULL) { 1098 nicvf_dev_tx_queue_release(dev, nicvf_netdev_qidx(nic, qidx)); 1099 return -ENOMEM; 1100 } 1101 1102 if (nicvf_qset_sq_alloc(dev, nic, txq, qidx, nb_desc)) { 1103 PMD_INIT_LOG(ERR, "Failed to allocate mem for sq %d", qidx); 1104 nicvf_dev_tx_queue_release(dev, nicvf_netdev_qidx(nic, qidx)); 1105 return -ENOMEM; 1106 } 1107 1108 nicvf_tx_queue_reset(txq); 1109 1110 PMD_INIT_LOG(DEBUG, "[%d] txq=%p nb_desc=%d desc=%p" 1111 " phys=0x%" PRIx64 " offloads=0x%" PRIx64, 1112 nicvf_netdev_qidx(nic, qidx), txq, nb_desc, txq->desc, 1113 txq->phys, txq->offloads); 1114 1115 dev->data->tx_queue_state[nicvf_netdev_qidx(nic, qidx)] = 1116 RTE_ETH_QUEUE_STATE_STOPPED; 1117 return 0; 1118 } 1119 1120 static inline void 1121 nicvf_rx_queue_release_mbufs(struct rte_eth_dev *dev, struct nicvf_rxq *rxq) 1122 { 1123 uint32_t rxq_cnt; 1124 uint32_t nb_pkts, released_pkts = 0; 1125 uint32_t refill_cnt = 0; 1126 struct rte_mbuf *rx_pkts[NICVF_MAX_RX_FREE_THRESH]; 1127 1128 if (dev->rx_pkt_burst == NULL) 1129 return; 1130 1131 while ((rxq_cnt = nicvf_dev_rx_queue_count(rxq))) { 1132 nb_pkts = dev->rx_pkt_burst(rxq, rx_pkts, 1133 NICVF_MAX_RX_FREE_THRESH); 1134 PMD_DRV_LOG(INFO, "nb_pkts=%d rxq_cnt=%d", nb_pkts, rxq_cnt); 1135 while (nb_pkts) { 1136 rte_pktmbuf_free_seg(rx_pkts[--nb_pkts]); 1137 released_pkts++; 1138 } 1139 } 1140 1141 1142 refill_cnt += nicvf_dev_rbdr_refill(dev, 1143 nicvf_netdev_qidx(rxq->nic, rxq->queue_id)); 1144 1145 PMD_DRV_LOG(INFO, "free_cnt=%d refill_cnt=%d", 1146 released_pkts, refill_cnt); 1147 } 1148 1149 static void 1150 nicvf_rx_queue_reset(struct nicvf_rxq *rxq) 1151 { 1152 rxq->head = 0; 1153 rxq->available_space = 0; 1154 rxq->recv_buffers = 0; 1155 } 1156 1157 static inline int 1158 nicvf_vf_start_rx_queue(struct rte_eth_dev *dev, struct nicvf *nic, 1159 uint16_t qidx) 1160 { 1161 struct nicvf_rxq *rxq; 1162 int ret; 1163 1164 assert(qidx < MAX_RCV_QUEUES_PER_QS); 1165 1166 if (dev->data->rx_queue_state[nicvf_netdev_qidx(nic, qidx)] == 1167 RTE_ETH_QUEUE_STATE_STARTED) 1168 return 0; 1169 1170 /* Update rbdr pointer to all rxq */ 1171 rxq = dev->data->rx_queues[nicvf_netdev_qidx(nic, qidx)]; 1172 rxq->shared_rbdr = nic->rbdr; 1173 1174 ret = nicvf_qset_rq_config(nic, qidx, rxq); 1175 if (ret) { 1176 PMD_INIT_LOG(ERR, "Failed to configure rq VF%d %d %d", 1177 nic->vf_id, qidx, ret); 1178 goto config_rq_error; 1179 } 1180 ret = nicvf_qset_cq_config(nic, qidx, rxq); 1181 if (ret) { 1182 PMD_INIT_LOG(ERR, "Failed to configure cq VF%d %d %d", 1183 nic->vf_id, qidx, ret); 1184 goto config_cq_error; 1185 } 1186 1187 dev->data->rx_queue_state[nicvf_netdev_qidx(nic, qidx)] = 1188 RTE_ETH_QUEUE_STATE_STARTED; 1189 return 0; 1190 1191 config_cq_error: 1192 nicvf_qset_cq_reclaim(nic, qidx); 1193 config_rq_error: 1194 nicvf_qset_rq_reclaim(nic, qidx); 1195 return ret; 1196 } 1197 1198 static inline int 1199 nicvf_vf_stop_rx_queue(struct rte_eth_dev *dev, struct nicvf *nic, 1200 uint16_t qidx) 1201 { 1202 struct nicvf_rxq *rxq; 1203 int ret, other_error; 1204 1205 if (dev->data->rx_queue_state[nicvf_netdev_qidx(nic, qidx)] == 1206 RTE_ETH_QUEUE_STATE_STOPPED) 1207 return 0; 1208 1209 ret = nicvf_qset_rq_reclaim(nic, qidx); 1210 if (ret) 1211 PMD_INIT_LOG(ERR, "Failed to reclaim rq VF%d %d %d", 1212 nic->vf_id, qidx, ret); 1213 1214 other_error = ret; 1215 rxq = dev->data->rx_queues[nicvf_netdev_qidx(nic, qidx)]; 1216 nicvf_rx_queue_release_mbufs(dev, rxq); 1217 nicvf_rx_queue_reset(rxq); 1218 1219 ret = nicvf_qset_cq_reclaim(nic, qidx); 1220 if (ret) 1221 PMD_INIT_LOG(ERR, "Failed to reclaim cq VF%d %d %d", 1222 nic->vf_id, qidx, ret); 1223 1224 other_error |= ret; 1225 dev->data->rx_queue_state[nicvf_netdev_qidx(nic, qidx)] = 1226 RTE_ETH_QUEUE_STATE_STOPPED; 1227 return other_error; 1228 } 1229 1230 static void 1231 nicvf_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid) 1232 { 1233 PMD_INIT_FUNC_TRACE(); 1234 1235 rte_free(dev->data->rx_queues[qid]); 1236 } 1237 1238 static int 1239 nicvf_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t qidx) 1240 { 1241 struct nicvf *nic = nicvf_pmd_priv(dev); 1242 int ret; 1243 1244 if (qidx >= MAX_RCV_QUEUES_PER_QS) 1245 nic = nic->snicvf[(qidx / MAX_RCV_QUEUES_PER_QS - 1)]; 1246 1247 qidx = qidx % MAX_RCV_QUEUES_PER_QS; 1248 1249 ret = nicvf_vf_start_rx_queue(dev, nic, qidx); 1250 if (ret) 1251 return ret; 1252 1253 ret = nicvf_configure_cpi(dev); 1254 if (ret) 1255 return ret; 1256 1257 return nicvf_configure_rss_reta(dev); 1258 } 1259 1260 static int 1261 nicvf_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t qidx) 1262 { 1263 int ret; 1264 struct nicvf *nic = nicvf_pmd_priv(dev); 1265 1266 if (qidx >= MAX_SND_QUEUES_PER_QS) 1267 nic = nic->snicvf[(qidx / MAX_SND_QUEUES_PER_QS - 1)]; 1268 1269 qidx = qidx % MAX_RCV_QUEUES_PER_QS; 1270 1271 ret = nicvf_vf_stop_rx_queue(dev, nic, qidx); 1272 ret |= nicvf_configure_cpi(dev); 1273 ret |= nicvf_configure_rss_reta(dev); 1274 return ret; 1275 } 1276 1277 static int 1278 nicvf_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t qidx) 1279 { 1280 struct nicvf *nic = nicvf_pmd_priv(dev); 1281 1282 if (qidx >= MAX_SND_QUEUES_PER_QS) 1283 nic = nic->snicvf[(qidx / MAX_SND_QUEUES_PER_QS - 1)]; 1284 1285 qidx = qidx % MAX_SND_QUEUES_PER_QS; 1286 1287 return nicvf_vf_start_tx_queue(dev, nic, qidx); 1288 } 1289 1290 static int 1291 nicvf_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t qidx) 1292 { 1293 struct nicvf *nic = nicvf_pmd_priv(dev); 1294 1295 if (qidx >= MAX_SND_QUEUES_PER_QS) 1296 nic = nic->snicvf[(qidx / MAX_SND_QUEUES_PER_QS - 1)]; 1297 1298 qidx = qidx % MAX_SND_QUEUES_PER_QS; 1299 1300 return nicvf_vf_stop_tx_queue(dev, nic, qidx); 1301 } 1302 1303 static inline void 1304 nicvf_rxq_mbuf_setup(struct nicvf_rxq *rxq) 1305 { 1306 uintptr_t p; 1307 struct rte_mbuf mb_def; 1308 struct nicvf *nic = rxq->nic; 1309 1310 RTE_BUILD_BUG_ON(sizeof(union mbuf_initializer) != 8); 1311 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) % 8 != 0); 1312 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, refcnt) - 1313 offsetof(struct rte_mbuf, data_off) != 2); 1314 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, nb_segs) - 1315 offsetof(struct rte_mbuf, data_off) != 4); 1316 RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, port) - 1317 offsetof(struct rte_mbuf, data_off) != 6); 1318 RTE_BUILD_BUG_ON(offsetof(struct nicvf_rxq, rxq_fastpath_data_end) - 1319 offsetof(struct nicvf_rxq, 1320 rxq_fastpath_data_start) > 128); 1321 mb_def.nb_segs = 1; 1322 mb_def.data_off = RTE_PKTMBUF_HEADROOM + (nic->skip_bytes); 1323 mb_def.port = rxq->port_id; 1324 rte_mbuf_refcnt_set(&mb_def, 1); 1325 1326 /* Prevent compiler reordering: rearm_data covers previous fields */ 1327 rte_compiler_barrier(); 1328 p = (uintptr_t)&mb_def.rearm_data; 1329 rxq->mbuf_initializer.value = *(uint64_t *)p; 1330 } 1331 1332 static int 1333 nicvf_dev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t qidx, 1334 uint16_t nb_desc, unsigned int socket_id, 1335 const struct rte_eth_rxconf *rx_conf, 1336 struct rte_mempool *mp) 1337 { 1338 uint16_t rx_free_thresh; 1339 struct nicvf_rxq *rxq; 1340 struct nicvf *nic = nicvf_pmd_priv(dev); 1341 uint64_t offloads; 1342 uint32_t buffsz; 1343 struct rte_pktmbuf_pool_private *mbp_priv; 1344 1345 PMD_INIT_FUNC_TRACE(); 1346 1347 /* First skip check */ 1348 mbp_priv = rte_mempool_get_priv(mp); 1349 buffsz = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM; 1350 if (buffsz < (uint32_t)(nic->skip_bytes)) { 1351 PMD_INIT_LOG(ERR, "First skip is more than configured buffer size"); 1352 return -EINVAL; 1353 } 1354 1355 if (qidx >= MAX_RCV_QUEUES_PER_QS) 1356 nic = nic->snicvf[qidx / MAX_RCV_QUEUES_PER_QS - 1]; 1357 1358 qidx = qidx % MAX_RCV_QUEUES_PER_QS; 1359 1360 /* Socket id check */ 1361 if (socket_id != (unsigned int)SOCKET_ID_ANY && socket_id != nic->node) 1362 PMD_DRV_LOG(WARNING, "socket_id expected %d, configured %d", 1363 socket_id, nic->node); 1364 1365 /* Mempool memory must be contiguous, so must be one memory segment*/ 1366 if (mp->nb_mem_chunks != 1) { 1367 PMD_INIT_LOG(ERR, "Non-contiguous mempool, add more huge pages"); 1368 return -EINVAL; 1369 } 1370 1371 /* Mempool memory must be physically contiguous */ 1372 if (mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG) { 1373 PMD_INIT_LOG(ERR, "Mempool memory must be physically contiguous"); 1374 return -EINVAL; 1375 } 1376 1377 /* Rx deferred start is not supported */ 1378 if (rx_conf->rx_deferred_start) { 1379 PMD_INIT_LOG(ERR, "Rx deferred start not supported"); 1380 return -EINVAL; 1381 } 1382 1383 /* Roundup nb_desc to available qsize and validate max number of desc */ 1384 nb_desc = nicvf_qsize_cq_roundup(nb_desc); 1385 if (nb_desc == 0) { 1386 PMD_INIT_LOG(ERR, "Value nb_desc beyond available hw cq qsize"); 1387 return -EINVAL; 1388 } 1389 1390 1391 /* Check rx_free_thresh upper bound */ 1392 rx_free_thresh = (uint16_t)((rx_conf->rx_free_thresh) ? 1393 rx_conf->rx_free_thresh : 1394 NICVF_DEFAULT_RX_FREE_THRESH); 1395 if (rx_free_thresh > NICVF_MAX_RX_FREE_THRESH || 1396 rx_free_thresh >= nb_desc * .75) { 1397 PMD_INIT_LOG(ERR, "rx_free_thresh greater than expected %d", 1398 rx_free_thresh); 1399 return -EINVAL; 1400 } 1401 1402 /* Free memory prior to re-allocation if needed */ 1403 if (dev->data->rx_queues[nicvf_netdev_qidx(nic, qidx)] != NULL) { 1404 PMD_RX_LOG(DEBUG, "Freeing memory prior to re-allocation %d", 1405 nicvf_netdev_qidx(nic, qidx)); 1406 nicvf_dev_rx_queue_release(dev, nicvf_netdev_qidx(nic, qidx)); 1407 dev->data->rx_queues[nicvf_netdev_qidx(nic, qidx)] = NULL; 1408 } 1409 1410 /* Allocate rxq memory */ 1411 rxq = rte_zmalloc_socket("ethdev rx queue", sizeof(struct nicvf_rxq), 1412 RTE_CACHE_LINE_SIZE, nic->node); 1413 if (rxq == NULL) { 1414 PMD_INIT_LOG(ERR, "Failed to allocate rxq=%d", 1415 nicvf_netdev_qidx(nic, qidx)); 1416 return -ENOMEM; 1417 } 1418 1419 rxq->nic = nic; 1420 rxq->pool = mp; 1421 rxq->queue_id = qidx; 1422 rxq->port_id = dev->data->port_id; 1423 rxq->rx_free_thresh = rx_free_thresh; 1424 rxq->rx_drop_en = rx_conf->rx_drop_en; 1425 rxq->cq_status = nicvf_qset_base(nic, qidx) + NIC_QSET_CQ_0_7_STATUS; 1426 rxq->cq_door = nicvf_qset_base(nic, qidx) + NIC_QSET_CQ_0_7_DOOR; 1427 rxq->precharge_cnt = 0; 1428 1429 if (nicvf_hw_cap(nic) & NICVF_CAP_CQE_RX2) 1430 rxq->rbptr_offset = NICVF_CQE_RX2_RBPTR_WORD; 1431 else 1432 rxq->rbptr_offset = NICVF_CQE_RBPTR_WORD; 1433 1434 dev->data->rx_queues[nicvf_netdev_qidx(nic, qidx)] = rxq; 1435 1436 nicvf_rxq_mbuf_setup(rxq); 1437 1438 /* Alloc completion queue */ 1439 if (nicvf_qset_cq_alloc(dev, nic, rxq, rxq->queue_id, nb_desc)) { 1440 PMD_INIT_LOG(ERR, "failed to allocate cq %u", rxq->queue_id); 1441 nicvf_dev_rx_queue_release(dev, nicvf_netdev_qidx(nic, qidx)); 1442 return -ENOMEM; 1443 } 1444 1445 nicvf_rx_queue_reset(rxq); 1446 1447 offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads; 1448 PMD_INIT_LOG(DEBUG, "[%d] rxq=%p pool=%s nb_desc=(%d/%d)" 1449 " phy=0x%" PRIx64 " offloads=0x%" PRIx64, 1450 nicvf_netdev_qidx(nic, qidx), rxq, mp->name, nb_desc, 1451 rte_mempool_avail_count(mp), rxq->phys, offloads); 1452 1453 dev->data->rx_queue_state[nicvf_netdev_qidx(nic, qidx)] = 1454 RTE_ETH_QUEUE_STATE_STOPPED; 1455 return 0; 1456 } 1457 1458 static int 1459 nicvf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 1460 { 1461 struct nicvf *nic = nicvf_pmd_priv(dev); 1462 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev); 1463 1464 PMD_INIT_FUNC_TRACE(); 1465 1466 /* Autonegotiation may be disabled */ 1467 dev_info->speed_capa = RTE_ETH_LINK_SPEED_FIXED; 1468 dev_info->speed_capa |= RTE_ETH_LINK_SPEED_10M | RTE_ETH_LINK_SPEED_100M | 1469 RTE_ETH_LINK_SPEED_1G | RTE_ETH_LINK_SPEED_10G; 1470 if (nicvf_hw_version(nic) != PCI_SUB_DEVICE_ID_CN81XX_NICVF) 1471 dev_info->speed_capa |= RTE_ETH_LINK_SPEED_40G; 1472 1473 dev_info->min_rx_bufsize = RTE_ETHER_MIN_MTU; 1474 dev_info->max_rx_pktlen = NIC_HW_MAX_MTU + RTE_ETHER_HDR_LEN; 1475 dev_info->max_rx_queues = 1476 (uint16_t)MAX_RCV_QUEUES_PER_QS * (MAX_SQS_PER_VF + 1); 1477 dev_info->max_tx_queues = 1478 (uint16_t)MAX_SND_QUEUES_PER_QS * (MAX_SQS_PER_VF + 1); 1479 dev_info->max_mac_addrs = 1; 1480 dev_info->max_vfs = pci_dev->max_vfs; 1481 1482 dev_info->rx_offload_capa = NICVF_RX_OFFLOAD_CAPA; 1483 dev_info->tx_offload_capa = NICVF_TX_OFFLOAD_CAPA; 1484 dev_info->rx_queue_offload_capa = NICVF_RX_OFFLOAD_CAPA; 1485 dev_info->tx_queue_offload_capa = NICVF_TX_OFFLOAD_CAPA; 1486 1487 dev_info->reta_size = nic->rss_info.rss_size; 1488 dev_info->hash_key_size = RSS_HASH_KEY_BYTE_SIZE; 1489 dev_info->flow_type_rss_offloads = NICVF_RSS_OFFLOAD_PASS1; 1490 if (nicvf_hw_cap(nic) & NICVF_CAP_TUNNEL_PARSING) 1491 dev_info->flow_type_rss_offloads |= NICVF_RSS_OFFLOAD_TUNNEL; 1492 1493 dev_info->default_rxconf = (struct rte_eth_rxconf) { 1494 .rx_free_thresh = NICVF_DEFAULT_RX_FREE_THRESH, 1495 .rx_drop_en = 0, 1496 }; 1497 1498 dev_info->default_txconf = (struct rte_eth_txconf) { 1499 .tx_free_thresh = NICVF_DEFAULT_TX_FREE_THRESH, 1500 .offloads = RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE | 1501 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM | 1502 RTE_ETH_TX_OFFLOAD_UDP_CKSUM | 1503 RTE_ETH_TX_OFFLOAD_TCP_CKSUM, 1504 }; 1505 1506 return 0; 1507 } 1508 1509 static nicvf_iova_addr_t 1510 rbdr_rte_mempool_get(void *dev, void *opaque) 1511 { 1512 uint16_t qidx; 1513 uintptr_t mbuf; 1514 struct nicvf_rxq *rxq; 1515 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)dev; 1516 struct nicvf *nic = (struct nicvf *)opaque; 1517 uint16_t rx_start, rx_end; 1518 1519 /* Get queue ranges for this VF */ 1520 nicvf_rx_range(eth_dev, nic, &rx_start, &rx_end); 1521 1522 for (qidx = rx_start; qidx <= rx_end; qidx++) { 1523 rxq = eth_dev->data->rx_queues[qidx]; 1524 /* Maintain equal buffer count across all pools */ 1525 if (rxq->precharge_cnt >= rxq->qlen_mask) 1526 continue; 1527 rxq->precharge_cnt++; 1528 mbuf = (uintptr_t)rte_pktmbuf_alloc(rxq->pool); 1529 if (mbuf) 1530 return nicvf_mbuff_virt2phy(mbuf, rxq->mbuf_phys_off); 1531 } 1532 return 0; 1533 } 1534 1535 static int 1536 nicvf_vf_start(struct rte_eth_dev *dev, struct nicvf *nic, uint32_t rbdrsz) 1537 { 1538 int ret; 1539 uint16_t qidx, data_off; 1540 uint32_t total_rxq_desc, nb_rbdr_desc, exp_buffs; 1541 uint64_t mbuf_phys_off = 0; 1542 struct nicvf_rxq *rxq; 1543 struct rte_mbuf *mbuf; 1544 uint16_t rx_start, rx_end; 1545 uint16_t tx_start, tx_end; 1546 int mask; 1547 1548 PMD_INIT_FUNC_TRACE(); 1549 1550 /* Userspace process exited without proper shutdown in last run */ 1551 if (nicvf_qset_rbdr_active(nic, 0)) 1552 nicvf_vf_stop(dev, nic, false); 1553 1554 /* Get queue ranges for this VF */ 1555 nicvf_rx_range(dev, nic, &rx_start, &rx_end); 1556 1557 /* 1558 * Thunderx nicvf PMD can support more than one pool per port only when 1559 * 1) Data payload size is same across all the pools in given port 1560 * AND 1561 * 2) All mbuffs in the pools are from the same hugepage 1562 * AND 1563 * 3) Mbuff metadata size is same across all the pools in given port 1564 * 1565 * This is to support existing application that uses multiple pool/port. 1566 * But, the purpose of using multipool for QoS will not be addressed. 1567 * 1568 */ 1569 1570 /* Validate mempool attributes */ 1571 for (qidx = rx_start; qidx <= rx_end; qidx++) { 1572 rxq = dev->data->rx_queues[qidx]; 1573 rxq->mbuf_phys_off = nicvf_mempool_phy_offset(rxq->pool); 1574 mbuf = rte_pktmbuf_alloc(rxq->pool); 1575 if (mbuf == NULL) { 1576 PMD_INIT_LOG(ERR, "Failed allocate mbuf VF%d qid=%d " 1577 "pool=%s", 1578 nic->vf_id, qidx, rxq->pool->name); 1579 return -ENOMEM; 1580 } 1581 data_off = nicvf_mbuff_meta_length(mbuf); 1582 data_off += RTE_PKTMBUF_HEADROOM; 1583 rte_pktmbuf_free(mbuf); 1584 1585 if (data_off % RTE_CACHE_LINE_SIZE) { 1586 PMD_INIT_LOG(ERR, "%s: unaligned data_off=%d delta=%d", 1587 rxq->pool->name, data_off, 1588 data_off % RTE_CACHE_LINE_SIZE); 1589 return -EINVAL; 1590 } 1591 rxq->mbuf_phys_off -= data_off; 1592 rxq->mbuf_phys_off -= nic->skip_bytes; 1593 1594 if (mbuf_phys_off == 0) 1595 mbuf_phys_off = rxq->mbuf_phys_off; 1596 if (mbuf_phys_off != rxq->mbuf_phys_off) { 1597 PMD_INIT_LOG(ERR, "pool params not same,%s VF%d %" 1598 PRIx64, rxq->pool->name, nic->vf_id, 1599 mbuf_phys_off); 1600 return -EINVAL; 1601 } 1602 } 1603 1604 /* Check the level of buffers in the pool */ 1605 total_rxq_desc = 0; 1606 for (qidx = rx_start; qidx <= rx_end; qidx++) { 1607 rxq = dev->data->rx_queues[qidx]; 1608 /* Count total numbers of rxq descs */ 1609 total_rxq_desc += rxq->qlen_mask + 1; 1610 exp_buffs = RTE_MEMPOOL_CACHE_MAX_SIZE + rxq->rx_free_thresh; 1611 exp_buffs *= dev->data->nb_rx_queues; 1612 if (rte_mempool_avail_count(rxq->pool) < exp_buffs) { 1613 PMD_INIT_LOG(ERR, "Buff shortage in pool=%s (%d/%d)", 1614 rxq->pool->name, 1615 rte_mempool_avail_count(rxq->pool), 1616 exp_buffs); 1617 return -ENOENT; 1618 } 1619 } 1620 1621 /* Check RBDR desc overflow */ 1622 ret = nicvf_qsize_rbdr_roundup(total_rxq_desc); 1623 if (ret == 0) { 1624 PMD_INIT_LOG(ERR, "Reached RBDR desc limit, reduce nr desc " 1625 "VF%d", nic->vf_id); 1626 return -ENOMEM; 1627 } 1628 1629 /* Enable qset */ 1630 ret = nicvf_qset_config(nic); 1631 if (ret) { 1632 PMD_INIT_LOG(ERR, "Failed to enable qset %d VF%d", ret, 1633 nic->vf_id); 1634 return ret; 1635 } 1636 1637 /* Allocate RBDR and RBDR ring desc */ 1638 nb_rbdr_desc = nicvf_qsize_rbdr_roundup(total_rxq_desc); 1639 ret = nicvf_qset_rbdr_alloc(dev, nic, nb_rbdr_desc, rbdrsz); 1640 if (ret) { 1641 PMD_INIT_LOG(ERR, "Failed to allocate memory for rbdr alloc " 1642 "VF%d", nic->vf_id); 1643 goto qset_reclaim; 1644 } 1645 1646 /* Enable and configure RBDR registers */ 1647 ret = nicvf_qset_rbdr_config(nic, 0); 1648 if (ret) { 1649 PMD_INIT_LOG(ERR, "Failed to configure rbdr %d VF%d", ret, 1650 nic->vf_id); 1651 goto qset_rbdr_free; 1652 } 1653 1654 /* Fill rte_mempool buffers in RBDR pool and precharge it */ 1655 ret = nicvf_qset_rbdr_precharge(dev, nic, 0, rbdr_rte_mempool_get, 1656 total_rxq_desc); 1657 if (ret) { 1658 PMD_INIT_LOG(ERR, "Failed to fill rbdr %d VF%d", ret, 1659 nic->vf_id); 1660 goto qset_rbdr_reclaim; 1661 } 1662 1663 PMD_DRV_LOG(INFO, "Filled %d out of %d entries in RBDR VF%d", 1664 nic->rbdr->tail, nb_rbdr_desc, nic->vf_id); 1665 1666 /* Configure VLAN Strip */ 1667 mask = RTE_ETH_VLAN_STRIP_MASK | RTE_ETH_VLAN_FILTER_MASK | 1668 RTE_ETH_VLAN_EXTEND_MASK; 1669 ret = nicvf_vlan_offload_config(dev, mask); 1670 1671 /* Based on the packet type(IPv4 or IPv6), the nicvf HW aligns L3 data 1672 * to the 64bit memory address. 1673 * The alignment creates a hole in mbuf(between the end of headroom and 1674 * packet data start). The new revision of the HW provides an option to 1675 * disable the L3 alignment feature and make mbuf layout looks 1676 * more like other NICs. For better application compatibility, disabling 1677 * l3 alignment feature on the hardware revisions it supports 1678 */ 1679 nicvf_apad_config(nic, false); 1680 1681 /* Get queue ranges for this VF */ 1682 nicvf_tx_range(dev, nic, &tx_start, &tx_end); 1683 1684 /* Configure TX queues */ 1685 for (qidx = tx_start; qidx <= tx_end; qidx++) { 1686 ret = nicvf_vf_start_tx_queue(dev, nic, 1687 qidx % MAX_SND_QUEUES_PER_QS); 1688 if (ret) 1689 goto start_txq_error; 1690 } 1691 1692 /* Configure RX queues */ 1693 for (qidx = rx_start; qidx <= rx_end; qidx++) { 1694 ret = nicvf_vf_start_rx_queue(dev, nic, 1695 qidx % MAX_RCV_QUEUES_PER_QS); 1696 if (ret) 1697 goto start_rxq_error; 1698 } 1699 1700 if (!nic->sqs_mode) { 1701 /* Configure CPI algorithm */ 1702 ret = nicvf_configure_cpi(dev); 1703 if (ret) 1704 goto start_txq_error; 1705 1706 ret = nicvf_mbox_get_rss_size(nic); 1707 if (ret) { 1708 PMD_INIT_LOG(ERR, "Failed to get rss table size"); 1709 goto qset_rss_error; 1710 } 1711 1712 /* Configure RSS */ 1713 ret = nicvf_configure_rss(dev); 1714 if (ret) 1715 goto qset_rss_error; 1716 } 1717 1718 /* Done; Let PF make the BGX's RX and TX switches to ON position */ 1719 nicvf_mbox_cfg_done(nic); 1720 return 0; 1721 1722 qset_rss_error: 1723 nicvf_rss_term(nic); 1724 start_rxq_error: 1725 for (qidx = rx_start; qidx <= rx_end; qidx++) 1726 nicvf_vf_stop_rx_queue(dev, nic, qidx % MAX_RCV_QUEUES_PER_QS); 1727 start_txq_error: 1728 for (qidx = tx_start; qidx <= tx_end; qidx++) 1729 nicvf_vf_stop_tx_queue(dev, nic, qidx % MAX_SND_QUEUES_PER_QS); 1730 qset_rbdr_reclaim: 1731 nicvf_qset_rbdr_reclaim(nic, 0); 1732 nicvf_rbdr_release_mbufs(dev, nic); 1733 qset_rbdr_free: 1734 if (nic->rbdr) { 1735 rte_free(nic->rbdr); 1736 nic->rbdr = NULL; 1737 } 1738 qset_reclaim: 1739 nicvf_qset_reclaim(nic); 1740 return ret; 1741 } 1742 1743 static int 1744 nicvf_dev_start(struct rte_eth_dev *dev) 1745 { 1746 uint16_t qidx; 1747 int ret; 1748 size_t i; 1749 struct nicvf *nic = nicvf_pmd_priv(dev); 1750 struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode; 1751 uint16_t mtu; 1752 uint32_t buffsz = 0, rbdrsz = 0; 1753 struct rte_pktmbuf_pool_private *mbp_priv; 1754 struct nicvf_rxq *rxq; 1755 1756 PMD_INIT_FUNC_TRACE(); 1757 1758 /* This function must be called for a primary device */ 1759 assert_primary(nic); 1760 1761 /* Validate RBDR buff size */ 1762 for (qidx = 0; qidx < dev->data->nb_rx_queues; qidx++) { 1763 rxq = dev->data->rx_queues[qidx]; 1764 mbp_priv = rte_mempool_get_priv(rxq->pool); 1765 buffsz = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM; 1766 if (buffsz % 128) { 1767 PMD_INIT_LOG(ERR, "rxbuf size must be multiply of 128"); 1768 return -EINVAL; 1769 } 1770 if (rbdrsz == 0) 1771 rbdrsz = buffsz; 1772 if (rbdrsz != buffsz) { 1773 PMD_INIT_LOG(ERR, "buffsz not same, qidx=%d (%d/%d)", 1774 qidx, rbdrsz, buffsz); 1775 return -EINVAL; 1776 } 1777 } 1778 1779 /* Configure loopback */ 1780 ret = nicvf_loopback_config(nic, dev->data->dev_conf.lpbk_mode); 1781 if (ret) { 1782 PMD_INIT_LOG(ERR, "Failed to configure loopback %d", ret); 1783 return ret; 1784 } 1785 1786 /* Reset all statistics counters attached to this port */ 1787 ret = nicvf_mbox_reset_stat_counters(nic, 0x3FFF, 0x1F, 0xFFFF, 0xFFFF); 1788 if (ret) { 1789 PMD_INIT_LOG(ERR, "Failed to reset stat counters %d", ret); 1790 return ret; 1791 } 1792 1793 /* Setup scatter mode if needed by jumbo */ 1794 if (dev->data->mtu + (uint32_t)NIC_HW_L2_OVERHEAD + 2 * VLAN_TAG_SIZE > buffsz) 1795 dev->data->scattered_rx = 1; 1796 if ((rx_conf->offloads & RTE_ETH_RX_OFFLOAD_SCATTER) != 0) 1797 dev->data->scattered_rx = 1; 1798 1799 /* Setup MTU */ 1800 mtu = dev->data->mtu; 1801 1802 if (nicvf_dev_set_mtu(dev, mtu)) { 1803 PMD_INIT_LOG(ERR, "Failed to set default mtu size"); 1804 return -EBUSY; 1805 } 1806 1807 /* Apply new link configurations if changed */ 1808 ret = nicvf_apply_link_speed(dev); 1809 if (ret) { 1810 PMD_INIT_LOG(ERR, "Failed to set link configuration\n"); 1811 return ret; 1812 } 1813 1814 ret = nicvf_vf_start(dev, nic, rbdrsz); 1815 if (ret != 0) 1816 return ret; 1817 1818 for (i = 0; i < nic->sqs_count; i++) { 1819 assert(nic->snicvf[i]); 1820 1821 ret = nicvf_vf_start(dev, nic->snicvf[i], rbdrsz); 1822 if (ret != 0) 1823 return ret; 1824 } 1825 1826 /* Configure callbacks based on offloads */ 1827 nicvf_set_tx_function(dev); 1828 nicvf_set_rx_function(dev); 1829 1830 return 0; 1831 } 1832 1833 static void 1834 nicvf_dev_stop_cleanup(struct rte_eth_dev *dev, bool cleanup) 1835 { 1836 size_t i; 1837 int ret; 1838 struct nicvf *nic = nicvf_pmd_priv(dev); 1839 1840 PMD_INIT_FUNC_TRACE(); 1841 dev->data->dev_started = 0; 1842 1843 /* Teardown secondary vf first */ 1844 for (i = 0; i < nic->sqs_count; i++) { 1845 if (!nic->snicvf[i]) 1846 continue; 1847 1848 nicvf_vf_stop(dev, nic->snicvf[i], cleanup); 1849 } 1850 1851 /* Stop the primary VF now */ 1852 nicvf_vf_stop(dev, nic, cleanup); 1853 1854 /* Disable loopback */ 1855 ret = nicvf_loopback_config(nic, 0); 1856 if (ret) 1857 PMD_INIT_LOG(ERR, "Failed to disable loopback %d", ret); 1858 1859 /* Reclaim CPI configuration */ 1860 ret = nicvf_mbox_config_cpi(nic, 0); 1861 if (ret) 1862 PMD_INIT_LOG(ERR, "Failed to reclaim CPI config %d", ret); 1863 } 1864 1865 static int 1866 nicvf_dev_stop(struct rte_eth_dev *dev) 1867 { 1868 PMD_INIT_FUNC_TRACE(); 1869 1870 nicvf_dev_stop_cleanup(dev, false); 1871 1872 return 0; 1873 } 1874 1875 static void 1876 nicvf_vf_stop(struct rte_eth_dev *dev, struct nicvf *nic, bool cleanup) 1877 { 1878 int ret; 1879 uint16_t qidx; 1880 uint16_t tx_start, tx_end; 1881 uint16_t rx_start, rx_end; 1882 1883 PMD_INIT_FUNC_TRACE(); 1884 1885 if (cleanup) { 1886 /* Let PF make the BGX's RX and TX switches to OFF position */ 1887 nicvf_mbox_shutdown(nic); 1888 } 1889 1890 /* Disable VLAN Strip */ 1891 nicvf_vlan_hw_strip(nic, 0); 1892 1893 /* Get queue ranges for this VF */ 1894 nicvf_tx_range(dev, nic, &tx_start, &tx_end); 1895 1896 for (qidx = tx_start; qidx <= tx_end; qidx++) 1897 nicvf_vf_stop_tx_queue(dev, nic, qidx % MAX_SND_QUEUES_PER_QS); 1898 1899 /* Get queue ranges for this VF */ 1900 nicvf_rx_range(dev, nic, &rx_start, &rx_end); 1901 1902 /* Reclaim rq */ 1903 for (qidx = rx_start; qidx <= rx_end; qidx++) 1904 nicvf_vf_stop_rx_queue(dev, nic, qidx % MAX_RCV_QUEUES_PER_QS); 1905 1906 /* Reclaim RBDR */ 1907 ret = nicvf_qset_rbdr_reclaim(nic, 0); 1908 if (ret) 1909 PMD_INIT_LOG(ERR, "Failed to reclaim RBDR %d", ret); 1910 1911 /* Move all charged buffers in RBDR back to pool */ 1912 if (nic->rbdr != NULL) 1913 nicvf_rbdr_release_mbufs(dev, nic); 1914 1915 /* Disable qset */ 1916 ret = nicvf_qset_reclaim(nic); 1917 if (ret) 1918 PMD_INIT_LOG(ERR, "Failed to disable qset %d", ret); 1919 1920 /* Disable all interrupts */ 1921 nicvf_disable_all_interrupts(nic); 1922 1923 /* Free RBDR SW structure */ 1924 if (nic->rbdr) { 1925 rte_free(nic->rbdr); 1926 nic->rbdr = NULL; 1927 } 1928 } 1929 1930 static int 1931 nicvf_dev_close(struct rte_eth_dev *dev) 1932 { 1933 struct nicvf *nic = nicvf_pmd_priv(dev); 1934 1935 PMD_INIT_FUNC_TRACE(); 1936 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 1937 return 0; 1938 1939 nicvf_dev_stop_cleanup(dev, true); 1940 nicvf_periodic_alarm_stop(nicvf_interrupt, dev); 1941 nicvf_periodic_alarm_stop(nicvf_vf_interrupt, nic); 1942 1943 rte_intr_instance_free(nic->intr_handle); 1944 1945 return 0; 1946 } 1947 1948 static int 1949 nicvf_request_sqs(struct nicvf *nic) 1950 { 1951 size_t i; 1952 1953 assert_primary(nic); 1954 assert(nic->sqs_count > 0); 1955 assert(nic->sqs_count <= MAX_SQS_PER_VF); 1956 1957 /* Set no of Rx/Tx queues in each of the SQsets */ 1958 for (i = 0; i < nic->sqs_count; i++) { 1959 if (nicvf_svf_empty()) 1960 rte_panic("Cannot assign sufficient number of " 1961 "secondary queues to primary VF%" PRIu8 "\n", 1962 nic->vf_id); 1963 1964 nic->snicvf[i] = nicvf_svf_pop(); 1965 nic->snicvf[i]->sqs_id = i; 1966 } 1967 1968 return nicvf_mbox_request_sqs(nic); 1969 } 1970 1971 static int 1972 nicvf_dev_configure(struct rte_eth_dev *dev) 1973 { 1974 struct rte_eth_dev_data *data = dev->data; 1975 struct rte_eth_conf *conf = &data->dev_conf; 1976 struct rte_eth_rxmode *rxmode = &conf->rxmode; 1977 struct rte_eth_txmode *txmode = &conf->txmode; 1978 struct nicvf *nic = nicvf_pmd_priv(dev); 1979 uint8_t cqcount; 1980 1981 PMD_INIT_FUNC_TRACE(); 1982 1983 if (rxmode->mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) 1984 rxmode->offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH; 1985 1986 if (!rte_eal_has_hugepages()) { 1987 PMD_INIT_LOG(INFO, "Huge page is not configured"); 1988 return -EINVAL; 1989 } 1990 1991 if (txmode->mq_mode) { 1992 PMD_INIT_LOG(INFO, "Tx mq_mode DCB or VMDq not supported"); 1993 return -EINVAL; 1994 } 1995 1996 if (rxmode->mq_mode != RTE_ETH_MQ_RX_NONE && 1997 rxmode->mq_mode != RTE_ETH_MQ_RX_RSS) { 1998 PMD_INIT_LOG(INFO, "Unsupported rx qmode %d", rxmode->mq_mode); 1999 return -EINVAL; 2000 } 2001 2002 if (rxmode->split_hdr_size) { 2003 PMD_INIT_LOG(INFO, "Rxmode does not support split header"); 2004 return -EINVAL; 2005 } 2006 2007 if (conf->dcb_capability_en) { 2008 PMD_INIT_LOG(INFO, "DCB enable not supported"); 2009 return -EINVAL; 2010 } 2011 2012 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) { 2013 PMD_INIT_LOG(INFO, "Flow director not supported"); 2014 return -EINVAL; 2015 } 2016 2017 assert_primary(nic); 2018 NICVF_STATIC_ASSERT(MAX_RCV_QUEUES_PER_QS == MAX_SND_QUEUES_PER_QS); 2019 cqcount = RTE_MAX(data->nb_tx_queues, data->nb_rx_queues); 2020 if (cqcount > MAX_RCV_QUEUES_PER_QS) { 2021 nic->sqs_count = RTE_ALIGN_CEIL(cqcount, MAX_RCV_QUEUES_PER_QS); 2022 nic->sqs_count = (nic->sqs_count / MAX_RCV_QUEUES_PER_QS) - 1; 2023 } else { 2024 nic->sqs_count = 0; 2025 } 2026 2027 assert(nic->sqs_count <= MAX_SQS_PER_VF); 2028 2029 if (nic->sqs_count > 0) { 2030 if (nicvf_request_sqs(nic)) { 2031 rte_panic("Cannot assign sufficient number of " 2032 "secondary queues to PORT%d VF%" PRIu8 "\n", 2033 dev->data->port_id, nic->vf_id); 2034 } 2035 } 2036 2037 if (rxmode->offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM) 2038 nic->offload_cksum = 1; 2039 2040 PMD_INIT_LOG(DEBUG, "Configured ethdev port%d hwcap=0x%" PRIx64, 2041 dev->data->port_id, nicvf_hw_cap(nic)); 2042 2043 return 0; 2044 } 2045 2046 static int 2047 nicvf_dev_set_link_up(struct rte_eth_dev *dev) 2048 { 2049 struct nicvf *nic = nicvf_pmd_priv(dev); 2050 int rc, i; 2051 2052 rc = nicvf_mbox_set_link_up_down(nic, true); 2053 if (rc) 2054 goto done; 2055 2056 /* Start tx queues */ 2057 for (i = 0; i < dev->data->nb_tx_queues; i++) 2058 nicvf_dev_tx_queue_start(dev, i); 2059 2060 done: 2061 return rc; 2062 } 2063 2064 static int 2065 nicvf_dev_set_link_down(struct rte_eth_dev *dev) 2066 { 2067 struct nicvf *nic = nicvf_pmd_priv(dev); 2068 int i; 2069 2070 /* Stop tx queues */ 2071 for (i = 0; i < dev->data->nb_tx_queues; i++) 2072 nicvf_dev_tx_queue_stop(dev, i); 2073 2074 return nicvf_mbox_set_link_up_down(nic, false); 2075 } 2076 2077 /* Initialize and register driver with DPDK Application */ 2078 static const struct eth_dev_ops nicvf_eth_dev_ops = { 2079 .dev_configure = nicvf_dev_configure, 2080 .dev_start = nicvf_dev_start, 2081 .dev_stop = nicvf_dev_stop, 2082 .link_update = nicvf_dev_link_update, 2083 .dev_close = nicvf_dev_close, 2084 .stats_get = nicvf_dev_stats_get, 2085 .stats_reset = nicvf_dev_stats_reset, 2086 .promiscuous_enable = nicvf_dev_promisc_enable, 2087 .dev_infos_get = nicvf_dev_info_get, 2088 .dev_supported_ptypes_get = nicvf_dev_supported_ptypes_get, 2089 .mtu_set = nicvf_dev_set_mtu, 2090 .vlan_offload_set = nicvf_vlan_offload_set, 2091 .reta_update = nicvf_dev_reta_update, 2092 .reta_query = nicvf_dev_reta_query, 2093 .rss_hash_update = nicvf_dev_rss_hash_update, 2094 .rss_hash_conf_get = nicvf_dev_rss_hash_conf_get, 2095 .rx_queue_start = nicvf_dev_rx_queue_start, 2096 .rx_queue_stop = nicvf_dev_rx_queue_stop, 2097 .tx_queue_start = nicvf_dev_tx_queue_start, 2098 .tx_queue_stop = nicvf_dev_tx_queue_stop, 2099 .rx_queue_setup = nicvf_dev_rx_queue_setup, 2100 .rx_queue_release = nicvf_dev_rx_queue_release, 2101 .tx_queue_setup = nicvf_dev_tx_queue_setup, 2102 .tx_queue_release = nicvf_dev_tx_queue_release, 2103 .dev_set_link_up = nicvf_dev_set_link_up, 2104 .dev_set_link_down = nicvf_dev_set_link_down, 2105 .get_reg = nicvf_dev_get_regs, 2106 }; 2107 2108 static int 2109 nicvf_vlan_offload_config(struct rte_eth_dev *dev, int mask) 2110 { 2111 struct rte_eth_rxmode *rxmode; 2112 struct nicvf *nic = nicvf_pmd_priv(dev); 2113 rxmode = &dev->data->dev_conf.rxmode; 2114 if (mask & RTE_ETH_VLAN_STRIP_MASK) { 2115 if (rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP) 2116 nicvf_vlan_hw_strip(nic, true); 2117 else 2118 nicvf_vlan_hw_strip(nic, false); 2119 } 2120 2121 return 0; 2122 } 2123 2124 static int 2125 nicvf_vlan_offload_set(struct rte_eth_dev *dev, int mask) 2126 { 2127 nicvf_vlan_offload_config(dev, mask); 2128 2129 return 0; 2130 } 2131 2132 static inline int 2133 nicvf_set_first_skip(struct rte_eth_dev *dev) 2134 { 2135 int bytes_to_skip = 0; 2136 int ret = 0; 2137 unsigned int i; 2138 struct rte_kvargs *kvlist; 2139 static const char *const skip[] = { 2140 SKIP_DATA_BYTES, 2141 NULL}; 2142 struct nicvf *nic = nicvf_pmd_priv(dev); 2143 2144 if (!dev->device->devargs) { 2145 nicvf_first_skip_config(nic, 0); 2146 return ret; 2147 } 2148 2149 kvlist = rte_kvargs_parse(dev->device->devargs->args, skip); 2150 if (!kvlist) 2151 return -EINVAL; 2152 2153 if (kvlist->count == 0) 2154 goto exit; 2155 2156 for (i = 0; i != kvlist->count; ++i) { 2157 const struct rte_kvargs_pair *pair = &kvlist->pairs[i]; 2158 2159 if (!strcmp(pair->key, SKIP_DATA_BYTES)) 2160 bytes_to_skip = atoi(pair->value); 2161 } 2162 2163 /*128 bytes amounts to one cache line*/ 2164 if (bytes_to_skip >= 0 && bytes_to_skip < 128) { 2165 if (!(bytes_to_skip % 8)) { 2166 nicvf_first_skip_config(nic, (bytes_to_skip / 8)); 2167 nic->skip_bytes = bytes_to_skip; 2168 goto kvlist_free; 2169 } else { 2170 PMD_INIT_LOG(ERR, "skip_data_bytes should be multiple of 8"); 2171 ret = -EINVAL; 2172 goto exit; 2173 } 2174 } else { 2175 PMD_INIT_LOG(ERR, "skip_data_bytes should be less than 128"); 2176 ret = -EINVAL; 2177 goto exit; 2178 } 2179 exit: 2180 nicvf_first_skip_config(nic, 0); 2181 kvlist_free: 2182 rte_kvargs_free(kvlist); 2183 return ret; 2184 } 2185 static int 2186 nicvf_eth_dev_uninit(struct rte_eth_dev *dev) 2187 { 2188 PMD_INIT_FUNC_TRACE(); 2189 nicvf_dev_close(dev); 2190 return 0; 2191 } 2192 static int 2193 nicvf_eth_dev_init(struct rte_eth_dev *eth_dev) 2194 { 2195 int ret; 2196 struct rte_pci_device *pci_dev; 2197 struct nicvf *nic = nicvf_pmd_priv(eth_dev); 2198 2199 PMD_INIT_FUNC_TRACE(); 2200 2201 eth_dev->dev_ops = &nicvf_eth_dev_ops; 2202 eth_dev->rx_queue_count = nicvf_dev_rx_queue_count; 2203 2204 /* For secondary processes, the primary has done all the work */ 2205 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 2206 if (nic) { 2207 /* Setup callbacks for secondary process */ 2208 nicvf_set_tx_function(eth_dev); 2209 nicvf_set_rx_function(eth_dev); 2210 return 0; 2211 } else { 2212 /* If nic == NULL than it is secondary function 2213 * so ethdev need to be released by caller */ 2214 return ENOTSUP; 2215 } 2216 } 2217 2218 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev); 2219 rte_eth_copy_pci_info(eth_dev, pci_dev); 2220 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS; 2221 2222 nic->device_id = pci_dev->id.device_id; 2223 nic->vendor_id = pci_dev->id.vendor_id; 2224 nic->subsystem_device_id = pci_dev->id.subsystem_device_id; 2225 nic->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id; 2226 2227 PMD_INIT_LOG(DEBUG, "nicvf: device (%x:%x) %u:%u:%u:%u", 2228 pci_dev->id.vendor_id, pci_dev->id.device_id, 2229 pci_dev->addr.domain, pci_dev->addr.bus, 2230 pci_dev->addr.devid, pci_dev->addr.function); 2231 2232 nic->reg_base = (uintptr_t)pci_dev->mem_resource[0].addr; 2233 if (!nic->reg_base) { 2234 PMD_INIT_LOG(ERR, "Failed to map BAR0"); 2235 ret = -ENODEV; 2236 goto fail; 2237 } 2238 2239 /* Allocate interrupt instance */ 2240 nic->intr_handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED); 2241 if (nic->intr_handle == NULL) { 2242 PMD_INIT_LOG(ERR, "Failed to allocate intr handle"); 2243 ret = -ENODEV; 2244 goto fail; 2245 } 2246 2247 nicvf_disable_all_interrupts(nic); 2248 2249 /* To read mbox messages */ 2250 ret = nicvf_periodic_alarm_start(nicvf_vf_interrupt, nic); 2251 if (ret) { 2252 PMD_INIT_LOG(ERR, "Failed to start period alarm"); 2253 goto fail; 2254 } 2255 2256 /* To poll link status change*/ 2257 ret = nicvf_periodic_alarm_start(nicvf_interrupt, eth_dev); 2258 if (ret) { 2259 PMD_INIT_LOG(ERR, "Failed to start period alarm"); 2260 goto fail; 2261 } 2262 2263 ret = nicvf_mbox_check_pf_ready(nic); 2264 if (ret) { 2265 PMD_INIT_LOG(ERR, "Failed to get ready message from PF"); 2266 goto alarm_fail; 2267 } else { 2268 PMD_INIT_LOG(INFO, 2269 "node=%d vf=%d mode=%s sqs=%s loopback_supported=%s", 2270 nic->node, nic->vf_id, 2271 nic->tns_mode == NIC_TNS_MODE ? "tns" : "tns-bypass", 2272 nic->sqs_mode ? "true" : "false", 2273 nic->loopback_supported ? "true" : "false" 2274 ); 2275 } 2276 2277 /* To make sure RX DMAC register is set to default value (0x3) */ 2278 nicvf_mbox_reset_xcast(nic); 2279 2280 ret = nicvf_base_init(nic); 2281 if (ret) { 2282 PMD_INIT_LOG(ERR, "Failed to execute nicvf_base_init"); 2283 goto malloc_fail; 2284 } 2285 2286 if (nic->sqs_mode) { 2287 /* Push nic to stack of secondary vfs */ 2288 nicvf_svf_push(nic); 2289 2290 /* Steal nic pointer from the device for further reuse */ 2291 eth_dev->data->dev_private = NULL; 2292 2293 nicvf_periodic_alarm_stop(nicvf_interrupt, eth_dev); 2294 2295 /* Detach port by returning positive error number */ 2296 return ENOTSUP; 2297 } 2298 2299 eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", 2300 RTE_ETHER_ADDR_LEN, 0); 2301 if (eth_dev->data->mac_addrs == NULL) { 2302 PMD_INIT_LOG(ERR, "Failed to allocate memory for mac addr"); 2303 ret = -ENOMEM; 2304 goto alarm_fail; 2305 } 2306 if (rte_is_zero_ether_addr((struct rte_ether_addr *)nic->mac_addr)) 2307 rte_eth_random_addr(&nic->mac_addr[0]); 2308 2309 rte_ether_addr_copy((struct rte_ether_addr *)nic->mac_addr, 2310 ð_dev->data->mac_addrs[0]); 2311 2312 ret = nicvf_mbox_set_mac_addr(nic, nic->mac_addr); 2313 if (ret) { 2314 PMD_INIT_LOG(ERR, "Failed to set mac addr"); 2315 goto malloc_fail; 2316 } 2317 2318 ret = nicvf_set_first_skip(eth_dev); 2319 if (ret) { 2320 PMD_INIT_LOG(ERR, "Failed to configure first skip"); 2321 goto malloc_fail; 2322 } 2323 PMD_INIT_LOG(INFO, "Port %d (%x:%x) mac=" RTE_ETHER_ADDR_PRT_FMT, 2324 eth_dev->data->port_id, nic->vendor_id, nic->device_id, 2325 nic->mac_addr[0], nic->mac_addr[1], nic->mac_addr[2], 2326 nic->mac_addr[3], nic->mac_addr[4], nic->mac_addr[5]); 2327 2328 return 0; 2329 2330 malloc_fail: 2331 rte_free(eth_dev->data->mac_addrs); 2332 eth_dev->data->mac_addrs = NULL; 2333 alarm_fail: 2334 nicvf_periodic_alarm_stop(nicvf_interrupt, eth_dev); 2335 fail: 2336 return ret; 2337 } 2338 2339 static const struct rte_pci_id pci_id_nicvf_map[] = { 2340 { 2341 .class_id = RTE_CLASS_ANY_ID, 2342 .vendor_id = PCI_VENDOR_ID_CAVIUM, 2343 .device_id = PCI_DEVICE_ID_THUNDERX_CN88XX_PASS1_NICVF, 2344 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM, 2345 .subsystem_device_id = PCI_SUB_DEVICE_ID_CN88XX_PASS1_NICVF, 2346 }, 2347 { 2348 .class_id = RTE_CLASS_ANY_ID, 2349 .vendor_id = PCI_VENDOR_ID_CAVIUM, 2350 .device_id = PCI_DEVICE_ID_THUNDERX_NICVF, 2351 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM, 2352 .subsystem_device_id = PCI_SUB_DEVICE_ID_CN88XX_PASS2_NICVF, 2353 }, 2354 { 2355 .class_id = RTE_CLASS_ANY_ID, 2356 .vendor_id = PCI_VENDOR_ID_CAVIUM, 2357 .device_id = PCI_DEVICE_ID_THUNDERX_NICVF, 2358 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM, 2359 .subsystem_device_id = PCI_SUB_DEVICE_ID_CN81XX_NICVF, 2360 }, 2361 { 2362 .class_id = RTE_CLASS_ANY_ID, 2363 .vendor_id = PCI_VENDOR_ID_CAVIUM, 2364 .device_id = PCI_DEVICE_ID_THUNDERX_NICVF, 2365 .subsystem_vendor_id = PCI_VENDOR_ID_CAVIUM, 2366 .subsystem_device_id = PCI_SUB_DEVICE_ID_CN83XX_NICVF, 2367 }, 2368 { 2369 .vendor_id = 0, 2370 }, 2371 }; 2372 2373 static int nicvf_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 2374 struct rte_pci_device *pci_dev) 2375 { 2376 return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct nicvf), 2377 nicvf_eth_dev_init); 2378 } 2379 2380 static int nicvf_eth_pci_remove(struct rte_pci_device *pci_dev) 2381 { 2382 return rte_eth_dev_pci_generic_remove(pci_dev, nicvf_eth_dev_uninit); 2383 } 2384 2385 static struct rte_pci_driver rte_nicvf_pmd = { 2386 .id_table = pci_id_nicvf_map, 2387 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_KEEP_MAPPED_RES | 2388 RTE_PCI_DRV_INTR_LSC, 2389 .probe = nicvf_eth_pci_probe, 2390 .remove = nicvf_eth_pci_remove, 2391 }; 2392 2393 RTE_PMD_REGISTER_PCI(net_thunderx, rte_nicvf_pmd); 2394 RTE_PMD_REGISTER_PCI_TABLE(net_thunderx, pci_id_nicvf_map); 2395 RTE_PMD_REGISTER_KMOD_DEP(net_thunderx, "* igb_uio | uio_pci_generic | vfio-pci"); 2396 RTE_PMD_REGISTER_PARAM_STRING(net_thunderx, SKIP_DATA_BYTES "=<int>"); 2397