1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2015-2018 Atomic Rules LLC 3 */ 4 5 #include <unistd.h> 6 7 #include "ark_ethdev_rx.h" 8 #include "ark_global.h" 9 #include "ark_logs.h" 10 #include "ark_mpu.h" 11 #include "ark_udm.h" 12 13 #define ARK_RX_META_SIZE 32 14 #define ARK_RX_META_OFFSET (RTE_PKTMBUF_HEADROOM - ARK_RX_META_SIZE) 15 #define ARK_RX_MAX_NOCHAIN (RTE_MBUF_DEFAULT_DATAROOM) 16 17 /* Forward declarations */ 18 struct ark_rx_queue; 19 struct ark_rx_meta; 20 21 static void dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi); 22 static void ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue); 23 static uint32_t eth_ark_rx_jumbo(struct ark_rx_queue *queue, 24 struct ark_rx_meta *meta, 25 struct rte_mbuf *mbuf0, 26 uint32_t cons_index); 27 static inline int eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue); 28 static int eth_ark_rx_seed_recovery(struct ark_rx_queue *queue, 29 uint32_t *pnb, 30 struct rte_mbuf **mbufs); 31 32 /* ************************************************************************* */ 33 struct ark_rx_queue { 34 /* array of mbufs to populate */ 35 struct rte_mbuf **reserve_q; 36 /* array of physical addresses of the mbuf data pointer */ 37 /* This point is a virtual address */ 38 rte_iova_t *paddress_q; 39 struct rte_mempool *mb_pool; 40 41 struct ark_udm_t *udm; 42 struct ark_mpu_t *mpu; 43 44 uint32_t queue_size; 45 uint32_t queue_mask; 46 47 uint32_t seed_index; /* step 1 set with empty mbuf */ 48 uint32_t cons_index; /* step 3 consumed by driver */ 49 50 /* The queue Id is used to identify the HW Q */ 51 uint16_t phys_qid; 52 53 /* The queue Index is used within the dpdk device structures */ 54 uint16_t queue_index; 55 56 uint32_t last_cons; 57 58 /* separate cache line */ 59 /* second cache line - fields only used in slow path */ 60 RTE_MARKER cacheline1 __rte_cache_min_aligned; 61 62 volatile uint32_t prod_index; /* step 2 filled by FPGA */ 63 } __rte_cache_aligned; 64 65 66 /* ************************************************************************* */ 67 static int 68 eth_ark_rx_hw_setup(struct rte_eth_dev *dev, 69 struct ark_rx_queue *queue, 70 uint16_t rx_queue_id __rte_unused, uint16_t rx_queue_idx) 71 { 72 rte_iova_t queue_base; 73 rte_iova_t phys_addr_q_base; 74 rte_iova_t phys_addr_prod_index; 75 76 queue_base = rte_malloc_virt2iova(queue); 77 phys_addr_prod_index = queue_base + 78 offsetof(struct ark_rx_queue, prod_index); 79 80 phys_addr_q_base = rte_malloc_virt2iova(queue->paddress_q); 81 82 /* Verify HW */ 83 if (ark_mpu_verify(queue->mpu, sizeof(rte_iova_t))) { 84 ARK_PMD_LOG(ERR, "Illegal configuration rx queue\n"); 85 return -1; 86 } 87 88 /* Stop and Reset and configure MPU */ 89 ark_mpu_configure(queue->mpu, phys_addr_q_base, queue->queue_size, 0); 90 91 ark_udm_write_addr(queue->udm, phys_addr_prod_index); 92 93 /* advance the valid pointer, but don't start until the queue starts */ 94 ark_mpu_reset_stats(queue->mpu); 95 96 /* The seed is the producer index for the HW */ 97 ark_mpu_set_producer(queue->mpu, queue->seed_index); 98 dev->data->rx_queue_state[rx_queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED; 99 100 return 0; 101 } 102 103 static inline void 104 eth_ark_rx_update_cons_index(struct ark_rx_queue *queue, uint32_t cons_index) 105 { 106 queue->cons_index = cons_index; 107 eth_ark_rx_seed_mbufs(queue); 108 if (((cons_index - queue->last_cons) >= 64U)) { 109 queue->last_cons = cons_index; 110 ark_mpu_set_producer(queue->mpu, queue->seed_index); 111 } 112 } 113 114 /* ************************************************************************* */ 115 int 116 eth_ark_dev_rx_queue_setup(struct rte_eth_dev *dev, 117 uint16_t queue_idx, 118 uint16_t nb_desc, 119 unsigned int socket_id, 120 const struct rte_eth_rxconf *rx_conf, 121 struct rte_mempool *mb_pool) 122 { 123 static int warning1; /* = 0 */ 124 struct ark_adapter *ark = dev->data->dev_private; 125 126 struct ark_rx_queue *queue; 127 uint32_t i; 128 int status; 129 130 int qidx = queue_idx; 131 132 /* We may already be setup, free memory prior to re-allocation */ 133 if (dev->data->rx_queues[queue_idx] != NULL) { 134 eth_ark_dev_rx_queue_release(dev->data->rx_queues[queue_idx]); 135 dev->data->rx_queues[queue_idx] = NULL; 136 } 137 138 if (rx_conf != NULL && warning1 == 0) { 139 warning1 = 1; 140 ARK_PMD_LOG(NOTICE, 141 "Arkville ignores rte_eth_rxconf argument.\n"); 142 } 143 144 if (RTE_PKTMBUF_HEADROOM < ARK_RX_META_SIZE) { 145 ARK_PMD_LOG(ERR, 146 "Error: DPDK Arkville requires head room > %d bytes (%s)\n", 147 ARK_RX_META_SIZE, __func__); 148 return -1; /* ERROR CODE */ 149 } 150 151 if (!rte_is_power_of_2(nb_desc)) { 152 ARK_PMD_LOG(ERR, 153 "DPDK Arkville configuration queue size must be power of two %u (%s)\n", 154 nb_desc, __func__); 155 return -1; /* ERROR CODE */ 156 } 157 158 /* Allocate queue struct */ 159 queue = rte_zmalloc_socket("Ark_rxqueue", 160 sizeof(struct ark_rx_queue), 161 64, 162 socket_id); 163 if (queue == 0) { 164 ARK_PMD_LOG(ERR, "Failed to allocate memory in %s\n", __func__); 165 return -ENOMEM; 166 } 167 168 /* NOTE zmalloc is used, no need to 0 indexes, etc. */ 169 queue->mb_pool = mb_pool; 170 queue->phys_qid = qidx; 171 queue->queue_index = queue_idx; 172 queue->queue_size = nb_desc; 173 queue->queue_mask = nb_desc - 1; 174 175 queue->reserve_q = 176 rte_zmalloc_socket("Ark_rx_queue mbuf", 177 nb_desc * sizeof(struct rte_mbuf *), 178 64, 179 socket_id); 180 queue->paddress_q = 181 rte_zmalloc_socket("Ark_rx_queue paddr", 182 nb_desc * sizeof(rte_iova_t), 183 64, 184 socket_id); 185 186 if (queue->reserve_q == 0 || queue->paddress_q == 0) { 187 ARK_PMD_LOG(ERR, 188 "Failed to allocate queue memory in %s\n", 189 __func__); 190 rte_free(queue->reserve_q); 191 rte_free(queue->paddress_q); 192 rte_free(queue); 193 return -ENOMEM; 194 } 195 196 dev->data->rx_queues[queue_idx] = queue; 197 queue->udm = RTE_PTR_ADD(ark->udm.v, qidx * ARK_UDM_QOFFSET); 198 queue->mpu = RTE_PTR_ADD(ark->mpurx.v, qidx * ARK_MPU_QOFFSET); 199 200 /* populate mbuf reserve */ 201 status = eth_ark_rx_seed_mbufs(queue); 202 203 if (queue->seed_index != nb_desc) { 204 ARK_PMD_LOG(ERR, "Failed to allocate %u mbufs for RX queue %d\n", 205 nb_desc, qidx); 206 status = -1; 207 } 208 /* MPU Setup */ 209 if (status == 0) 210 status = eth_ark_rx_hw_setup(dev, queue, qidx, queue_idx); 211 212 if (unlikely(status != 0)) { 213 struct rte_mbuf **mbuf; 214 215 ARK_PMD_LOG(ERR, "Failed to initialize RX queue %d %s\n", 216 qidx, 217 __func__); 218 /* Free the mbufs allocated */ 219 for (i = 0, mbuf = queue->reserve_q; 220 i < queue->seed_index; ++i, mbuf++) { 221 rte_pktmbuf_free(*mbuf); 222 } 223 rte_free(queue->reserve_q); 224 rte_free(queue->paddress_q); 225 rte_free(queue); 226 return -1; /* ERROR CODE */ 227 } 228 229 return 0; 230 } 231 232 /* ************************************************************************* */ 233 uint16_t 234 eth_ark_recv_pkts_noop(void *rx_queue __rte_unused, 235 struct rte_mbuf **rx_pkts __rte_unused, 236 uint16_t nb_pkts __rte_unused) 237 { 238 return 0; 239 } 240 241 /* ************************************************************************* */ 242 uint16_t 243 eth_ark_recv_pkts(void *rx_queue, 244 struct rte_mbuf **rx_pkts, 245 uint16_t nb_pkts) 246 { 247 struct ark_rx_queue *queue; 248 register uint32_t cons_index, prod_index; 249 uint16_t nb; 250 struct rte_mbuf *mbuf; 251 struct ark_rx_meta *meta; 252 253 queue = (struct ark_rx_queue *)rx_queue; 254 if (unlikely(queue == 0)) 255 return 0; 256 if (unlikely(nb_pkts == 0)) 257 return 0; 258 prod_index = queue->prod_index; 259 cons_index = queue->cons_index; 260 nb = 0; 261 262 while (prod_index != cons_index) { 263 mbuf = queue->reserve_q[cons_index & queue->queue_mask]; 264 /* prefetch mbuf */ 265 rte_mbuf_prefetch_part1(mbuf); 266 rte_mbuf_prefetch_part2(mbuf); 267 268 /* META DATA embedded in headroom */ 269 meta = RTE_PTR_ADD(mbuf->buf_addr, ARK_RX_META_OFFSET); 270 271 mbuf->port = meta->port; 272 mbuf->pkt_len = meta->pkt_len; 273 mbuf->data_len = meta->pkt_len; 274 mbuf->timestamp = meta->timestamp; 275 mbuf->udata64 = meta->user_data; 276 277 if (ARK_DEBUG_CORE) { /* debug sanity checks */ 278 if ((meta->pkt_len > (1024 * 16)) || 279 (meta->pkt_len == 0)) { 280 ARK_PMD_LOG(DEBUG, "RX: Bad Meta Q: %u" 281 " cons: %" PRIU32 282 " prod: %" PRIU32 283 " seed_index %" PRIU32 284 "\n", 285 queue->phys_qid, 286 cons_index, 287 queue->prod_index, 288 queue->seed_index); 289 290 291 ARK_PMD_LOG(DEBUG, " : UDM" 292 " prod: %" PRIU32 293 " len: %u\n", 294 queue->udm->rt_cfg.prod_idx, 295 meta->pkt_len); 296 ark_mpu_dump(queue->mpu, 297 " ", 298 queue->phys_qid); 299 dump_mbuf_data(mbuf, 0, 256); 300 /* its FUBAR so fix it */ 301 mbuf->pkt_len = 63; 302 meta->pkt_len = 63; 303 } 304 /* seqn is only set under debug */ 305 mbuf->seqn = cons_index; 306 } 307 308 if (unlikely(meta->pkt_len > ARK_RX_MAX_NOCHAIN)) 309 cons_index = eth_ark_rx_jumbo 310 (queue, meta, mbuf, cons_index + 1); 311 else 312 cons_index += 1; 313 314 rx_pkts[nb] = mbuf; 315 nb++; 316 if (nb >= nb_pkts) 317 break; 318 } 319 320 if (unlikely(nb != 0)) 321 /* report next free to FPGA */ 322 eth_ark_rx_update_cons_index(queue, cons_index); 323 324 return nb; 325 } 326 327 /* ************************************************************************* */ 328 static uint32_t 329 eth_ark_rx_jumbo(struct ark_rx_queue *queue, 330 struct ark_rx_meta *meta, 331 struct rte_mbuf *mbuf0, 332 uint32_t cons_index) 333 { 334 struct rte_mbuf *mbuf_prev; 335 struct rte_mbuf *mbuf; 336 337 uint16_t remaining; 338 uint16_t data_len; 339 uint16_t segments; 340 341 /* first buf populated by called */ 342 mbuf_prev = mbuf0; 343 segments = 1; 344 data_len = RTE_MIN(meta->pkt_len, RTE_MBUF_DEFAULT_DATAROOM); 345 remaining = meta->pkt_len - data_len; 346 mbuf0->data_len = data_len; 347 348 /* HW guarantees that the data does not exceed prod_index! */ 349 while (remaining != 0) { 350 data_len = RTE_MIN(remaining, 351 RTE_MBUF_DEFAULT_DATAROOM + 352 RTE_PKTMBUF_HEADROOM); 353 354 remaining -= data_len; 355 segments += 1; 356 357 mbuf = queue->reserve_q[cons_index & queue->queue_mask]; 358 mbuf_prev->next = mbuf; 359 mbuf_prev = mbuf; 360 mbuf->data_len = data_len; 361 mbuf->data_off = 0; 362 if (ARK_DEBUG_CORE) 363 mbuf->seqn = cons_index; /* for debug only */ 364 365 cons_index += 1; 366 } 367 368 mbuf0->nb_segs = segments; 369 return cons_index; 370 } 371 372 /* Drain the internal queue allowing hw to clear out. */ 373 static void 374 eth_ark_rx_queue_drain(struct ark_rx_queue *queue) 375 { 376 register uint32_t cons_index; 377 struct rte_mbuf *mbuf; 378 379 cons_index = queue->cons_index; 380 381 /* NOT performance optimized, since this is a one-shot call */ 382 while ((cons_index ^ queue->prod_index) & queue->queue_mask) { 383 mbuf = queue->reserve_q[cons_index & queue->queue_mask]; 384 rte_pktmbuf_free(mbuf); 385 cons_index++; 386 eth_ark_rx_update_cons_index(queue, cons_index); 387 } 388 } 389 390 uint32_t 391 eth_ark_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id) 392 { 393 struct ark_rx_queue *queue; 394 395 queue = dev->data->rx_queues[queue_id]; 396 return (queue->prod_index - queue->cons_index); /* mod arith */ 397 } 398 399 /* ************************************************************************* */ 400 int 401 eth_ark_rx_start_queue(struct rte_eth_dev *dev, uint16_t queue_id) 402 { 403 struct ark_rx_queue *queue; 404 405 queue = dev->data->rx_queues[queue_id]; 406 if (queue == 0) 407 return -1; 408 409 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED; 410 411 ark_mpu_set_producer(queue->mpu, queue->seed_index); 412 ark_mpu_start(queue->mpu); 413 414 ark_udm_queue_enable(queue->udm, 1); 415 416 return 0; 417 } 418 419 /* ************************************************************************* */ 420 421 /* Queue can be restarted. data remains 422 */ 423 int 424 eth_ark_rx_stop_queue(struct rte_eth_dev *dev, uint16_t queue_id) 425 { 426 struct ark_rx_queue *queue; 427 428 queue = dev->data->rx_queues[queue_id]; 429 if (queue == 0) 430 return -1; 431 432 ark_udm_queue_enable(queue->udm, 0); 433 434 dev->data->rx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED; 435 436 return 0; 437 } 438 439 /* ************************************************************************* */ 440 static inline int 441 eth_ark_rx_seed_mbufs(struct ark_rx_queue *queue) 442 { 443 uint32_t limit = queue->cons_index + queue->queue_size; 444 uint32_t seed_index = queue->seed_index; 445 446 uint32_t count = 0; 447 uint32_t seed_m = queue->seed_index & queue->queue_mask; 448 449 uint32_t nb = limit - seed_index; 450 451 /* Handle wrap around -- remainder is filled on the next call */ 452 if (unlikely(seed_m + nb > queue->queue_size)) 453 nb = queue->queue_size - seed_m; 454 455 struct rte_mbuf **mbufs = &queue->reserve_q[seed_m]; 456 int status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, nb); 457 458 if (unlikely(status != 0)) { 459 /* Try to recover from lack of mbufs in pool */ 460 status = eth_ark_rx_seed_recovery(queue, &nb, mbufs); 461 if (unlikely(status != 0)) { 462 return -1; 463 } 464 } 465 466 if (ARK_DEBUG_CORE) { /* DEBUG */ 467 while (count != nb) { 468 struct rte_mbuf *mbuf_init = 469 queue->reserve_q[seed_m + count]; 470 471 memset(mbuf_init->buf_addr, -1, 512); 472 *((uint32_t *)mbuf_init->buf_addr) = 473 seed_index + count; 474 *(uint16_t *)RTE_PTR_ADD(mbuf_init->buf_addr, 4) = 475 queue->phys_qid; 476 count++; 477 } 478 count = 0; 479 } /* DEBUG */ 480 queue->seed_index += nb; 481 482 /* Duff's device https://en.wikipedia.org/wiki/Duff's_device */ 483 switch (nb % 4) { 484 case 0: 485 while (count != nb) { 486 queue->paddress_q[seed_m++] = 487 (*mbufs++)->buf_iova; 488 count++; 489 /* FALLTHROUGH */ 490 case 3: 491 queue->paddress_q[seed_m++] = 492 (*mbufs++)->buf_iova; 493 count++; 494 /* FALLTHROUGH */ 495 case 2: 496 queue->paddress_q[seed_m++] = 497 (*mbufs++)->buf_iova; 498 count++; 499 /* FALLTHROUGH */ 500 case 1: 501 queue->paddress_q[seed_m++] = 502 (*mbufs++)->buf_iova; 503 count++; 504 /* FALLTHROUGH */ 505 506 } /* while (count != nb) */ 507 } /* switch */ 508 509 return 0; 510 } 511 512 int 513 eth_ark_rx_seed_recovery(struct ark_rx_queue *queue, 514 uint32_t *pnb, 515 struct rte_mbuf **mbufs) 516 { 517 int status = -1; 518 519 /* Ignore small allocation failures */ 520 if (*pnb <= 64) 521 return -1; 522 523 *pnb = 64U; 524 status = rte_pktmbuf_alloc_bulk(queue->mb_pool, mbufs, *pnb); 525 if (status != 0) { 526 ARK_PMD_LOG(NOTICE, 527 "ARK: Could not allocate %u mbufs from pool for RX queue %u;" 528 " %u free buffers remaining in queue\n", 529 *pnb, queue->queue_index, 530 queue->seed_index - queue->cons_index); 531 } 532 return status; 533 } 534 535 void 536 eth_ark_rx_dump_queue(struct rte_eth_dev *dev, uint16_t queue_id, 537 const char *msg) 538 { 539 struct ark_rx_queue *queue; 540 541 queue = dev->data->rx_queues[queue_id]; 542 543 ark_ethdev_rx_dump(msg, queue); 544 } 545 546 /* ************************************************************************* */ 547 /* Call on device closed no user API, queue is stopped */ 548 void 549 eth_ark_dev_rx_queue_release(void *vqueue) 550 { 551 struct ark_rx_queue *queue; 552 uint32_t i; 553 554 queue = (struct ark_rx_queue *)vqueue; 555 if (queue == 0) 556 return; 557 558 ark_udm_queue_enable(queue->udm, 0); 559 /* Stop the MPU since pointer are going away */ 560 ark_mpu_stop(queue->mpu); 561 562 /* Need to clear out mbufs here, dropping packets along the way */ 563 eth_ark_rx_queue_drain(queue); 564 565 for (i = 0; i < queue->queue_size; ++i) 566 rte_pktmbuf_free(queue->reserve_q[i]); 567 568 rte_free(queue->reserve_q); 569 rte_free(queue->paddress_q); 570 rte_free(queue); 571 } 572 573 void 574 eth_rx_queue_stats_get(void *vqueue, struct rte_eth_stats *stats) 575 { 576 struct ark_rx_queue *queue; 577 struct ark_udm_t *udm; 578 579 queue = vqueue; 580 if (queue == 0) 581 return; 582 udm = queue->udm; 583 584 uint64_t ibytes = ark_udm_bytes(udm); 585 uint64_t ipackets = ark_udm_packets(udm); 586 uint64_t idropped = ark_udm_dropped(queue->udm); 587 588 stats->q_ipackets[queue->queue_index] = ipackets; 589 stats->q_ibytes[queue->queue_index] = ibytes; 590 stats->q_errors[queue->queue_index] = idropped; 591 stats->ipackets += ipackets; 592 stats->ibytes += ibytes; 593 stats->imissed += idropped; 594 } 595 596 void 597 eth_rx_queue_stats_reset(void *vqueue) 598 { 599 struct ark_rx_queue *queue; 600 601 queue = vqueue; 602 if (queue == 0) 603 return; 604 605 ark_mpu_reset_stats(queue->mpu); 606 ark_udm_queue_stats_reset(queue->udm); 607 } 608 609 void 610 eth_ark_udm_force_close(struct rte_eth_dev *dev) 611 { 612 struct ark_adapter *ark = dev->data->dev_private; 613 struct ark_rx_queue *queue; 614 uint32_t index; 615 uint16_t i; 616 617 if (!ark_udm_is_flushed(ark->udm.v)) { 618 /* restart the MPUs */ 619 ARK_PMD_LOG(NOTICE, "UDM not flushed -- forcing flush\n"); 620 for (i = 0; i < dev->data->nb_rx_queues; i++) { 621 queue = (struct ark_rx_queue *)dev->data->rx_queues[i]; 622 if (queue == 0) 623 continue; 624 625 ark_mpu_start(queue->mpu); 626 /* Add some buffers */ 627 index = 100000 + queue->seed_index; 628 ark_mpu_set_producer(queue->mpu, index); 629 } 630 /* Wait to allow data to pass */ 631 usleep(100); 632 633 ARK_PMD_LOG(DEBUG, "UDM forced flush attempt, stopped = %d\n", 634 ark_udm_is_flushed(ark->udm.v)); 635 } 636 ark_udm_reset(ark->udm.v); 637 } 638 639 static void 640 ark_ethdev_rx_dump(const char *name, struct ark_rx_queue *queue) 641 { 642 if (queue == NULL) 643 return; 644 ARK_PMD_LOG(DEBUG, "RX QUEUE %d -- %s", queue->phys_qid, name); 645 ARK_PMD_LOG(DEBUG, ARK_SU32 ARK_SU32 ARK_SU32 ARK_SU32 "\n", 646 "queue_size", queue->queue_size, 647 "seed_index", queue->seed_index, 648 "prod_index", queue->prod_index, 649 "cons_index", queue->cons_index); 650 651 ark_mpu_dump(queue->mpu, name, queue->phys_qid); 652 ark_mpu_dump_setup(queue->mpu, queue->phys_qid); 653 ark_udm_dump(queue->udm, name); 654 ark_udm_dump_setup(queue->udm, queue->phys_qid); 655 } 656 657 /* Only used in debug. 658 * This function is a raw memory dump of a portion of an mbuf's memory 659 * region. The usual function, rte_pktmbuf_dump() only shows data 660 * with respect to the data_off field. This function show data 661 * anywhere in the mbuf's buffer. This is useful for examining 662 * data in the headroom or tailroom portion of an mbuf. 663 */ 664 static void 665 dump_mbuf_data(struct rte_mbuf *mbuf, uint16_t lo, uint16_t hi) 666 { 667 uint16_t i, j; 668 669 ARK_PMD_LOG(DEBUG, " MBUF: %p len %d, off: %d, seq: %" PRIU32 "\n", 670 mbuf, mbuf->pkt_len, mbuf->data_off, mbuf->seqn); 671 for (i = lo; i < hi; i += 16) { 672 uint8_t *dp = RTE_PTR_ADD(mbuf->buf_addr, i); 673 674 ARK_PMD_LOG(DEBUG, " %6d: ", i); 675 for (j = 0; j < 16; j++) 676 ARK_PMD_LOG(DEBUG, " %02x", dp[j]); 677 678 ARK_PMD_LOG(DEBUG, "\n"); 679 } 680 } 681