1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation.
9 #include <rte_common.h>
10 #include <rte_lcore.h>
11 #include <rte_cycles.h>
16 #include <rte_bus_pci.h>
17 #include <rte_memzone.h>
18 #include <rte_memcpy.h>
19 #include <rte_rawdev.h>
20 #include <rte_rawdev_pmd.h>
22 #include "ntb_hw_intel.h"
23 #include "rte_pmd_ntb.h"
26 static const struct rte_pci_id pci_id_ntb_map[] = {
27 { RTE_PCI_DEVICE(NTB_INTEL_VENDOR_ID, NTB_INTEL_DEV_ID_B2B_SKX) },
28 { .vendor_id = 0, /* sentinel */ },
31 /* Align with enum ntb_xstats_idx */
32 static struct rte_rawdev_xstats_name ntb_xstats_names[] = {
40 #define NTB_XSTATS_NUM RTE_DIM(ntb_xstats_names)
43 ntb_link_cleanup(struct rte_rawdev *dev)
45 struct ntb_hw *hw = dev->dev_private;
48 if (hw->ntb_ops->spad_write == NULL ||
49 hw->ntb_ops->mw_set_trans == NULL) {
50 NTB_LOG(ERR, "Not supported to clean up link.");
54 /* Clean spad registers. */
55 for (i = 0; i < hw->spad_cnt; i++) {
56 status = (*hw->ntb_ops->spad_write)(dev, i, 0, 0);
58 NTB_LOG(ERR, "Failed to clean local spad.");
61 /* Clear mw so that peer cannot access local memory.*/
62 for (i = 0; i < hw->used_mw_num; i++) {
63 status = (*hw->ntb_ops->mw_set_trans)(dev, i, 0, 0);
65 NTB_LOG(ERR, "Failed to clean mw.");
70 ntb_handshake_work(const struct rte_rawdev *dev)
72 struct ntb_hw *hw = dev->dev_private;
76 if (hw->ntb_ops->spad_write == NULL ||
77 hw->ntb_ops->mw_set_trans == NULL) {
78 NTB_LOG(ERR, "Scratchpad/MW setting is not supported.");
82 /* Tell peer the mw info of local side. */
83 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_NUM_MWS, 1, hw->mw_cnt);
86 for (i = 0; i < hw->mw_cnt; i++) {
87 NTB_LOG(INFO, "Local %u mw size: 0x%"PRIx64"", i,
89 val = hw->mw_size[i] >> 32;
90 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_SZ_H + 2 * i,
95 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_SZ_L + 2 * i,
101 /* Tell peer about the queue info and map memory to the peer. */
102 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_Q_SZ, 1, hw->queue_size);
105 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_NUM_QPS, 1,
109 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_USED_MWS, 1,
113 for (i = 0; i < hw->used_mw_num; i++) {
114 val = (uint64_t)(size_t)(hw->mz[i]->addr) >> 32;
115 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_BA_H + 2 * i,
119 val = (uint64_t)(size_t)(hw->mz[i]->addr);
120 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_BA_L + 2 * i,
126 for (i = 0; i < hw->used_mw_num; i++) {
127 ret = (*hw->ntb_ops->mw_set_trans)(dev, i, hw->mz[i]->iova,
133 /* Ring doorbell 0 to tell peer the device is ready. */
134 ret = (*hw->ntb_ops->peer_db_set)(dev, 0);
142 ntb_dev_intr_handler(void *param)
144 struct rte_rawdev *dev = (struct rte_rawdev *)param;
145 struct ntb_hw *hw = dev->dev_private;
146 uint32_t val_h, val_l;
147 uint64_t peer_mw_size;
148 uint64_t db_bits = 0;
152 if (hw->ntb_ops->db_read == NULL ||
153 hw->ntb_ops->db_clear == NULL ||
154 hw->ntb_ops->peer_db_set == NULL) {
155 NTB_LOG(ERR, "Doorbell is not supported.");
159 db_bits = (*hw->ntb_ops->db_read)(dev);
161 NTB_LOG(ERR, "No doorbells");
163 /* Doorbell 0 is for peer device ready. */
165 NTB_LOG(INFO, "DB0: Peer device is up.");
166 /* Clear received doorbell. */
167 (*hw->ntb_ops->db_clear)(dev, 1);
170 * Peer dev is already up. All mw settings are already done.
176 if (hw->ntb_ops->spad_read == NULL) {
177 NTB_LOG(ERR, "Scratchpad read is not supported.");
181 /* Check if mw setting on the peer is the same as local. */
182 peer_mw_cnt = (*hw->ntb_ops->spad_read)(dev, SPAD_NUM_MWS, 0);
183 if (peer_mw_cnt != hw->mw_cnt) {
184 NTB_LOG(ERR, "Both mw cnt must be the same.");
188 for (i = 0; i < hw->mw_cnt; i++) {
189 val_h = (*hw->ntb_ops->spad_read)
190 (dev, SPAD_MW0_SZ_H + 2 * i, 0);
191 val_l = (*hw->ntb_ops->spad_read)
192 (dev, SPAD_MW0_SZ_L + 2 * i, 0);
193 peer_mw_size = ((uint64_t)val_h << 32) | val_l;
194 NTB_LOG(DEBUG, "Peer %u mw size: 0x%"PRIx64"", i,
196 if (peer_mw_size != hw->mw_size[i]) {
197 NTB_LOG(ERR, "Mw config must be the same.");
205 * Handshake with peer. Spad_write & mw_set_trans only works
206 * when both devices are up. So write spad again when db is
207 * received. And set db again for the later device who may miss
210 if (ntb_handshake_work(dev) < 0) {
211 NTB_LOG(ERR, "Handshake work failed.");
215 /* To get the link info. */
216 if (hw->ntb_ops->get_link_status == NULL) {
217 NTB_LOG(ERR, "Not supported to get link status.");
220 (*hw->ntb_ops->get_link_status)(dev);
221 NTB_LOG(INFO, "Link is up. Link speed: %u. Link width: %u",
222 hw->link_speed, hw->link_width);
226 if (db_bits & (1 << 1)) {
227 NTB_LOG(INFO, "DB1: Peer device is down.");
228 /* Clear received doorbell. */
229 (*hw->ntb_ops->db_clear)(dev, 2);
231 /* Peer device will be down, So clean local side too. */
232 ntb_link_cleanup(dev);
235 /* Response peer's dev_stop request. */
236 (*hw->ntb_ops->peer_db_set)(dev, 2);
240 if (db_bits & (1 << 2)) {
241 NTB_LOG(INFO, "DB2: Peer device agrees dev to be down.");
242 /* Clear received doorbell. */
243 (*hw->ntb_ops->db_clear)(dev, (1 << 2));
250 ntb_queue_conf_get(struct rte_rawdev *dev,
252 rte_rawdev_obj_t queue_conf)
254 struct ntb_queue_conf *q_conf = queue_conf;
255 struct ntb_hw *hw = dev->dev_private;
257 q_conf->tx_free_thresh = hw->tx_queues[queue_id]->tx_free_thresh;
258 q_conf->nb_desc = hw->rx_queues[queue_id]->nb_rx_desc;
259 q_conf->rx_mp = hw->rx_queues[queue_id]->mpool;
263 ntb_rxq_release_mbufs(struct ntb_rx_queue *q)
267 if (!q || !q->sw_ring) {
268 NTB_LOG(ERR, "Pointer to rxq or sw_ring is NULL");
272 for (i = 0; i < q->nb_rx_desc; i++) {
273 if (q->sw_ring[i].mbuf) {
274 rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
275 q->sw_ring[i].mbuf = NULL;
281 ntb_rxq_release(struct ntb_rx_queue *rxq)
284 NTB_LOG(ERR, "Pointer to rxq is NULL");
288 ntb_rxq_release_mbufs(rxq);
290 rte_free(rxq->sw_ring);
295 ntb_rxq_setup(struct rte_rawdev *dev,
297 rte_rawdev_obj_t queue_conf)
299 struct ntb_queue_conf *rxq_conf = queue_conf;
300 struct ntb_hw *hw = dev->dev_private;
301 struct ntb_rx_queue *rxq;
303 /* Allocate the rx queue data structure */
304 rxq = rte_zmalloc_socket("ntb rx queue",
305 sizeof(struct ntb_rx_queue),
309 NTB_LOG(ERR, "Failed to allocate memory for "
310 "rx queue data structure.");
314 if (rxq_conf->rx_mp == NULL) {
315 NTB_LOG(ERR, "Invalid null mempool pointer.");
318 rxq->nb_rx_desc = rxq_conf->nb_desc;
319 rxq->mpool = rxq_conf->rx_mp;
320 rxq->port_id = dev->dev_id;
321 rxq->queue_id = qp_id;
324 /* Allocate the software ring. */
326 rte_zmalloc_socket("ntb rx sw ring",
327 sizeof(struct ntb_rx_entry) *
332 ntb_rxq_release(rxq);
334 NTB_LOG(ERR, "Failed to allocate memory for SW ring");
338 hw->rx_queues[qp_id] = rxq;
344 ntb_txq_release_mbufs(struct ntb_tx_queue *q)
348 if (!q || !q->sw_ring) {
349 NTB_LOG(ERR, "Pointer to txq or sw_ring is NULL");
353 for (i = 0; i < q->nb_tx_desc; i++) {
354 if (q->sw_ring[i].mbuf) {
355 rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
356 q->sw_ring[i].mbuf = NULL;
362 ntb_txq_release(struct ntb_tx_queue *txq)
365 NTB_LOG(ERR, "Pointer to txq is NULL");
369 ntb_txq_release_mbufs(txq);
371 rte_free(txq->sw_ring);
376 ntb_txq_setup(struct rte_rawdev *dev,
378 rte_rawdev_obj_t queue_conf)
380 struct ntb_queue_conf *txq_conf = queue_conf;
381 struct ntb_hw *hw = dev->dev_private;
382 struct ntb_tx_queue *txq;
385 /* Allocate the TX queue data structure. */
386 txq = rte_zmalloc_socket("ntb tx queue",
387 sizeof(struct ntb_tx_queue),
391 NTB_LOG(ERR, "Failed to allocate memory for "
392 "tx queue structure");
396 txq->nb_tx_desc = txq_conf->nb_desc;
397 txq->port_id = dev->dev_id;
398 txq->queue_id = qp_id;
401 /* Allocate software ring */
403 rte_zmalloc_socket("ntb tx sw ring",
404 sizeof(struct ntb_tx_entry) *
409 ntb_txq_release(txq);
411 NTB_LOG(ERR, "Failed to allocate memory for SW TX ring");
415 prev = txq->nb_tx_desc - 1;
416 for (i = 0; i < txq->nb_tx_desc; i++) {
417 txq->sw_ring[i].mbuf = NULL;
418 txq->sw_ring[i].last_id = i;
419 txq->sw_ring[prev].next_id = i;
423 txq->tx_free_thresh = txq_conf->tx_free_thresh ?
424 txq_conf->tx_free_thresh :
425 NTB_DFLT_TX_FREE_THRESH;
426 if (txq->tx_free_thresh >= txq->nb_tx_desc - 3) {
427 NTB_LOG(ERR, "tx_free_thresh must be less than nb_desc - 3. "
428 "(tx_free_thresh=%u qp_id=%u)", txq->tx_free_thresh,
433 hw->tx_queues[qp_id] = txq;
440 ntb_queue_setup(struct rte_rawdev *dev,
442 rte_rawdev_obj_t queue_conf)
444 struct ntb_hw *hw = dev->dev_private;
447 if (queue_id >= hw->queue_pairs)
450 ret = ntb_txq_setup(dev, queue_id, queue_conf);
454 ret = ntb_rxq_setup(dev, queue_id, queue_conf);
460 ntb_queue_release(struct rte_rawdev *dev, uint16_t queue_id)
462 struct ntb_hw *hw = dev->dev_private;
464 if (queue_id >= hw->queue_pairs)
467 ntb_txq_release(hw->tx_queues[queue_id]);
468 hw->tx_queues[queue_id] = NULL;
469 ntb_rxq_release(hw->rx_queues[queue_id]);
470 hw->rx_queues[queue_id] = NULL;
476 ntb_queue_count(struct rte_rawdev *dev)
478 struct ntb_hw *hw = dev->dev_private;
479 return hw->queue_pairs;
483 ntb_queue_init(struct rte_rawdev *dev, uint16_t qp_id)
485 struct ntb_hw *hw = dev->dev_private;
486 struct ntb_rx_queue *rxq = hw->rx_queues[qp_id];
487 struct ntb_tx_queue *txq = hw->tx_queues[qp_id];
488 volatile struct ntb_header *local_hdr;
489 struct ntb_header *remote_hdr;
490 uint16_t q_size = hw->queue_size;
495 if (hw->ntb_ops->get_peer_mw_addr == NULL) {
496 NTB_LOG(ERR, "Getting peer mw addr is not supported.");
500 /* Put queue info into the start of shared memory. */
501 hdr_offset = hw->hdr_size_per_queue * qp_id;
502 local_hdr = (volatile struct ntb_header *)
503 ((size_t)hw->mz[0]->addr + hdr_offset);
504 bar_addr = (*hw->ntb_ops->get_peer_mw_addr)(dev, 0);
505 if (bar_addr == NULL)
507 remote_hdr = (struct ntb_header *)
508 ((size_t)bar_addr + hdr_offset);
511 rxq->rx_desc_ring = (struct ntb_desc *)
512 (&remote_hdr->desc_ring);
513 rxq->rx_used_ring = (volatile struct ntb_used *)
514 (&local_hdr->desc_ring[q_size]);
515 rxq->avail_cnt = &remote_hdr->avail_cnt;
516 rxq->used_cnt = &local_hdr->used_cnt;
518 for (i = 0; i < rxq->nb_rx_desc - 1; i++) {
519 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mpool);
520 if (unlikely(!mbuf)) {
521 NTB_LOG(ERR, "Failed to allocate mbuf for RX");
524 mbuf->port = dev->dev_id;
526 rxq->sw_ring[i].mbuf = mbuf;
528 rxq->rx_desc_ring[i].addr = rte_pktmbuf_mtod(mbuf, size_t);
529 rxq->rx_desc_ring[i].len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
532 *rxq->avail_cnt = rxq->nb_rx_desc - 1;
533 rxq->last_avail = rxq->nb_rx_desc - 1;
537 txq->tx_desc_ring = (volatile struct ntb_desc *)
538 (&local_hdr->desc_ring);
539 txq->tx_used_ring = (struct ntb_used *)
540 (&remote_hdr->desc_ring[q_size]);
541 txq->avail_cnt = &local_hdr->avail_cnt;
542 txq->used_cnt = &remote_hdr->used_cnt;
548 txq->nb_tx_free = txq->nb_tx_desc - 1;
550 /* Set per queue stats. */
551 for (i = 0; i < NTB_XSTATS_NUM; i++) {
552 hw->ntb_xstats[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
553 hw->ntb_xstats_off[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
560 ntb_enqueue_cleanup(struct ntb_tx_queue *txq)
562 struct ntb_tx_entry *sw_ring = txq->sw_ring;
563 uint16_t tx_free = txq->last_avail;
564 uint16_t nb_to_clean, i;
566 /* avail_cnt + 1 represents where to rx next in the peer. */
567 nb_to_clean = (*txq->avail_cnt - txq->last_avail + 1 +
568 txq->nb_tx_desc) & (txq->nb_tx_desc - 1);
569 nb_to_clean = RTE_MIN(nb_to_clean, txq->tx_free_thresh);
570 for (i = 0; i < nb_to_clean; i++) {
571 if (sw_ring[tx_free].mbuf)
572 rte_pktmbuf_free_seg(sw_ring[tx_free].mbuf);
573 tx_free = (tx_free + 1) & (txq->nb_tx_desc - 1);
576 txq->nb_tx_free += nb_to_clean;
577 txq->last_avail = tx_free;
581 ntb_enqueue_bufs(struct rte_rawdev *dev,
582 struct rte_rawdev_buf **buffers,
584 rte_rawdev_obj_t context)
586 struct ntb_hw *hw = dev->dev_private;
587 struct ntb_tx_queue *txq = hw->tx_queues[(size_t)context];
588 struct ntb_tx_entry *sw_ring = txq->sw_ring;
589 struct rte_mbuf *txm;
590 struct ntb_used tx_used[NTB_MAX_DESC_SIZE];
591 volatile struct ntb_desc *tx_item;
592 uint16_t tx_last, nb_segs, off, last_used, avail_cnt;
593 uint16_t nb_mbufs = 0;
599 if (unlikely(hw->ntb_ops->ioremap == NULL)) {
600 NTB_LOG(ERR, "Ioremap not supported.");
604 if (unlikely(dev->started == 0 || hw->peer_dev_up == 0)) {
605 NTB_LOG(DEBUG, "Link is not up.");
609 if (txq->nb_tx_free < txq->tx_free_thresh)
610 ntb_enqueue_cleanup(txq);
612 off = NTB_XSTATS_NUM * ((size_t)context + 1);
613 last_used = txq->last_used;
614 avail_cnt = *txq->avail_cnt;/* Where to alloc next. */
615 for (nb_tx = 0; nb_tx < count; nb_tx++) {
616 txm = (struct rte_mbuf *)(buffers[nb_tx]->buf_addr);
617 if (txm == NULL || txq->nb_tx_free < txm->nb_segs)
620 tx_last = (txq->last_used + txm->nb_segs - 1) &
621 (txq->nb_tx_desc - 1);
622 nb_segs = txm->nb_segs;
623 for (i = 0; i < nb_segs; i++) {
624 /* Not enough ring space for tx. */
625 if (txq->last_used == avail_cnt)
627 sw_ring[txq->last_used].mbuf = txm;
628 tx_item = txq->tx_desc_ring + txq->last_used;
631 (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
634 if (txm->data_len > tx_item->len) {
635 NTB_LOG(ERR, "Data length exceeds buf length."
636 " Only %u data would be transmitted.",
638 txm->data_len = tx_item->len;
641 /* translate remote virtual addr to bar virtual addr */
642 buf_addr = (*hw->ntb_ops->ioremap)(dev, tx_item->addr);
643 if (buf_addr == NULL) {
644 (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
645 NTB_LOG(ERR, "Null remap addr.");
648 rte_memcpy(buf_addr, rte_pktmbuf_mtod(txm, void *),
651 tx_used[nb_mbufs].len = txm->data_len;
652 tx_used[nb_mbufs++].flags = (txq->last_used ==
657 bytes += txm->data_len;
661 sw_ring[txq->last_used].next_id = (txq->last_used + 1) &
662 (txq->nb_tx_desc - 1);
663 sw_ring[txq->last_used].last_id = tx_last;
664 txq->last_used = (txq->last_used + 1) &
665 (txq->nb_tx_desc - 1);
667 txq->nb_tx_free -= nb_segs;
673 if (nb_mbufs > txq->nb_tx_desc - last_used) {
674 nb1 = txq->nb_tx_desc - last_used;
675 nb2 = nb_mbufs - txq->nb_tx_desc + last_used;
680 rte_memcpy(txq->tx_used_ring + last_used, tx_used,
681 sizeof(struct ntb_used) * nb1);
682 rte_memcpy(txq->tx_used_ring, tx_used + nb1,
683 sizeof(struct ntb_used) * nb2);
685 *txq->used_cnt = txq->last_used;
687 /* update queue stats */
688 hw->ntb_xstats[NTB_TX_BYTES_ID + off] += bytes;
689 hw->ntb_xstats[NTB_TX_PKTS_ID + off] += nb_tx;
696 ntb_dequeue_bufs(struct rte_rawdev *dev,
697 struct rte_rawdev_buf **buffers,
699 rte_rawdev_obj_t context)
701 struct ntb_hw *hw = dev->dev_private;
702 struct ntb_rx_queue *rxq = hw->rx_queues[(size_t)context];
703 struct ntb_rx_entry *sw_ring = rxq->sw_ring;
704 struct ntb_desc rx_desc[NTB_MAX_DESC_SIZE];
705 struct rte_mbuf *first, *rxm_t;
706 struct rte_mbuf *prev = NULL;
707 volatile struct ntb_used *rx_item;
708 uint16_t nb_mbufs = 0;
711 uint16_t off, last_avail, used_cnt, used_nb;
714 if (unlikely(dev->started == 0 || hw->peer_dev_up == 0)) {
715 NTB_LOG(DEBUG, "Link is not up");
719 used_cnt = *rxq->used_cnt;
721 if (rxq->last_used == used_cnt)
724 last_avail = rxq->last_avail;
725 used_nb = (used_cnt - rxq->last_used) & (rxq->nb_rx_desc - 1);
726 count = RTE_MIN(count, used_nb);
727 for (nb_rx = 0; nb_rx < count; nb_rx++) {
730 rx_item = rxq->rx_used_ring + rxq->last_used;
731 rxm_t = sw_ring[rxq->last_used].mbuf;
732 rxm_t->data_len = rx_item->len;
733 rxm_t->data_off = RTE_PKTMBUF_HEADROOM;
734 rxm_t->port = rxq->port_id;
740 buffers[nb_rx]->buf_addr = rxm_t;
747 first->pkt_len += prev->data_len;
748 rxq->last_used = (rxq->last_used + 1) &
749 (rxq->nb_rx_desc - 1);
752 rxm_t = rte_mbuf_raw_alloc(rxq->mpool);
753 if (unlikely(rxm_t == NULL)) {
754 NTB_LOG(ERR, "recv alloc mbuf failed.");
757 rxm_t->port = rxq->port_id;
758 sw_ring[rxq->last_avail].mbuf = rxm_t;
762 rx_desc[nb_mbufs].addr =
763 rte_pktmbuf_mtod(rxm_t, size_t);
764 rx_desc[nb_mbufs++].len = rxm_t->buf_len -
765 RTE_PKTMBUF_HEADROOM;
766 rxq->last_avail = (rxq->last_avail + 1) &
767 (rxq->nb_rx_desc - 1);
769 if (rx_item->flags & NTB_FLAG_EOP)
773 bytes += first->pkt_len;
779 if (nb_mbufs > rxq->nb_rx_desc - last_avail) {
780 nb1 = rxq->nb_rx_desc - last_avail;
781 nb2 = nb_mbufs - rxq->nb_rx_desc + last_avail;
786 rte_memcpy(rxq->rx_desc_ring + last_avail, rx_desc,
787 sizeof(struct ntb_desc) * nb1);
788 rte_memcpy(rxq->rx_desc_ring, rx_desc + nb1,
789 sizeof(struct ntb_desc) * nb2);
791 *rxq->avail_cnt = rxq->last_avail;
793 /* update queue stats */
794 off = NTB_XSTATS_NUM * ((size_t)context + 1);
795 hw->ntb_xstats[NTB_RX_BYTES_ID + off] += bytes;
796 hw->ntb_xstats[NTB_RX_PKTS_ID + off] += nb_rx;
797 hw->ntb_xstats[NTB_RX_MISS_ID + off] += (count - nb_rx);
804 ntb_dev_info_get(struct rte_rawdev *dev, rte_rawdev_obj_t dev_info,
805 size_t dev_info_size)
807 struct ntb_hw *hw = dev->dev_private;
808 struct ntb_dev_info *info = dev_info;
810 if (dev_info_size != sizeof(*info)) {
811 NTB_LOG(ERR, "Invalid size parameter to %s", __func__);
815 info->mw_cnt = hw->mw_cnt;
816 info->mw_size = hw->mw_size;
819 * Intel hardware requires that mapped memory base address should be
820 * aligned with EMBARSZ and needs continuous memzone.
822 info->mw_size_align = (uint8_t)(hw->pci_dev->id.vendor_id ==
823 NTB_INTEL_VENDOR_ID);
825 if (!hw->queue_size || !hw->queue_pairs) {
826 NTB_LOG(ERR, "No queue size and queue num assigned.");
830 hw->hdr_size_per_queue = RTE_ALIGN(sizeof(struct ntb_header) +
831 hw->queue_size * sizeof(struct ntb_desc) +
832 hw->queue_size * sizeof(struct ntb_used),
833 RTE_CACHE_LINE_SIZE);
834 info->ntb_hdr_size = hw->hdr_size_per_queue * hw->queue_pairs;
838 ntb_dev_configure(const struct rte_rawdev *dev, rte_rawdev_obj_t config)
840 struct ntb_dev_config *conf = config;
841 struct ntb_hw *hw = dev->dev_private;
845 hw->queue_pairs = conf->num_queues;
846 hw->queue_size = conf->queue_size;
847 hw->used_mw_num = conf->mz_num;
848 hw->mz = conf->mz_list;
849 hw->rx_queues = rte_zmalloc("ntb_rx_queues",
850 sizeof(struct ntb_rx_queue *) * hw->queue_pairs, 0);
851 hw->tx_queues = rte_zmalloc("ntb_tx_queues",
852 sizeof(struct ntb_tx_queue *) * hw->queue_pairs, 0);
853 /* First total stats, then per queue stats. */
854 xstats_num = (hw->queue_pairs + 1) * NTB_XSTATS_NUM;
855 hw->ntb_xstats = rte_zmalloc("ntb_xstats", xstats_num *
856 sizeof(uint64_t), 0);
857 hw->ntb_xstats_off = rte_zmalloc("ntb_xstats_off", xstats_num *
858 sizeof(uint64_t), 0);
860 /* Start handshake with the peer. */
861 ret = ntb_handshake_work(dev);
863 rte_free(hw->rx_queues);
864 rte_free(hw->tx_queues);
865 hw->rx_queues = NULL;
866 hw->tx_queues = NULL;
874 ntb_dev_start(struct rte_rawdev *dev)
876 struct ntb_hw *hw = dev->dev_private;
877 uint32_t peer_base_l, peer_val;
878 uint64_t peer_base_h;
882 if (!hw->link_status || !hw->peer_dev_up)
885 /* Set total stats. */
886 for (i = 0; i < NTB_XSTATS_NUM; i++) {
887 hw->ntb_xstats[i] = 0;
888 hw->ntb_xstats_off[i] = 0;
891 for (i = 0; i < hw->queue_pairs; i++) {
892 ret = ntb_queue_init(dev, i);
894 NTB_LOG(ERR, "Failed to init queue.");
899 hw->peer_mw_base = rte_zmalloc("ntb_peer_mw_base", hw->mw_cnt *
900 sizeof(uint64_t), 0);
902 if (hw->ntb_ops->spad_read == NULL) {
907 peer_val = (*hw->ntb_ops->spad_read)(dev, SPAD_Q_SZ, 0);
908 if (peer_val != hw->queue_size) {
909 NTB_LOG(ERR, "Inconsistent queue size! (local: %u peer: %u)",
910 hw->queue_size, peer_val);
915 peer_val = (*hw->ntb_ops->spad_read)(dev, SPAD_NUM_QPS, 0);
916 if (peer_val != hw->queue_pairs) {
917 NTB_LOG(ERR, "Inconsistent number of queues! (local: %u peer:"
918 " %u)", hw->queue_pairs, peer_val);
923 hw->peer_used_mws = (*hw->ntb_ops->spad_read)(dev, SPAD_USED_MWS, 0);
925 for (i = 0; i < hw->peer_used_mws; i++) {
926 peer_base_h = (*hw->ntb_ops->spad_read)(dev,
927 SPAD_MW0_BA_H + 2 * i, 0);
928 peer_base_l = (*hw->ntb_ops->spad_read)(dev,
929 SPAD_MW0_BA_L + 2 * i, 0);
930 hw->peer_mw_base[i] = (peer_base_h << 32) + peer_base_l;
938 rte_free(hw->peer_mw_base);
940 for (i = 0; i < hw->queue_pairs; i++) {
941 ntb_rxq_release_mbufs(hw->rx_queues[i]);
942 ntb_txq_release_mbufs(hw->tx_queues[i]);
949 ntb_dev_stop(struct rte_rawdev *dev)
951 struct ntb_hw *hw = dev->dev_private;
955 if (!hw->peer_dev_up)
958 ntb_link_cleanup(dev);
960 /* Notify the peer that device will be down. */
961 if (hw->ntb_ops->peer_db_set == NULL) {
962 NTB_LOG(ERR, "Peer doorbell setting is not supported.");
965 status = (*hw->ntb_ops->peer_db_set)(dev, 1);
967 NTB_LOG(ERR, "Failed to tell peer device is down.");
972 * Set time out as 1s in case that the peer is stopped accidently
973 * without any notification.
977 /* Wait for cleanup work down before db mask clear. */
978 while (hw->peer_dev_up && time_out) {
984 /* Clear doorbells mask. */
985 if (hw->ntb_ops->db_set_mask == NULL) {
986 NTB_LOG(ERR, "Doorbell mask setting is not supported.");
989 status = (*hw->ntb_ops->db_set_mask)(dev,
990 (((uint64_t)1 << hw->db_cnt) - 1));
992 NTB_LOG(ERR, "Failed to clear doorbells.");
994 for (i = 0; i < hw->queue_pairs; i++) {
995 ntb_rxq_release_mbufs(hw->rx_queues[i]);
996 ntb_txq_release_mbufs(hw->tx_queues[i]);
1003 ntb_dev_close(struct rte_rawdev *dev)
1005 struct ntb_hw *hw = dev->dev_private;
1006 struct rte_intr_handle *intr_handle;
1013 for (i = 0; i < hw->queue_pairs; i++)
1014 ntb_queue_release(dev, i);
1015 hw->queue_pairs = 0;
1017 intr_handle = &hw->pci_dev->intr_handle;
1018 /* Clean datapath event and vec mapping */
1019 rte_intr_efd_disable(intr_handle);
1020 if (intr_handle->intr_vec) {
1021 rte_free(intr_handle->intr_vec);
1022 intr_handle->intr_vec = NULL;
1024 /* Disable uio intr before callback unregister */
1025 rte_intr_disable(intr_handle);
1027 /* Unregister callback func to eal lib */
1028 rte_intr_callback_unregister(intr_handle,
1029 ntb_dev_intr_handler, dev);
1035 ntb_dev_reset(struct rte_rawdev *rawdev __rte_unused)
1041 ntb_attr_set(struct rte_rawdev *dev, const char *attr_name,
1042 uint64_t attr_value)
1047 if (dev == NULL || attr_name == NULL) {
1048 NTB_LOG(ERR, "Invalid arguments for setting attributes");
1052 hw = dev->dev_private;
1054 if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
1055 if (hw->ntb_ops->spad_write == NULL)
1057 index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
1058 (*hw->ntb_ops->spad_write)(dev, hw->spad_user_list[index],
1060 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
1061 attr_name, attr_value);
1065 if (!strncmp(attr_name, NTB_QUEUE_SZ_NAME, NTB_ATTR_NAME_LEN)) {
1066 hw->queue_size = attr_value;
1067 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
1068 attr_name, attr_value);
1072 if (!strncmp(attr_name, NTB_QUEUE_NUM_NAME, NTB_ATTR_NAME_LEN)) {
1073 hw->queue_pairs = attr_value;
1074 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
1075 attr_name, attr_value);
1079 /* Attribute not found. */
1080 NTB_LOG(ERR, "Attribute not found.");
1085 ntb_attr_get(struct rte_rawdev *dev, const char *attr_name,
1086 uint64_t *attr_value)
1091 if (dev == NULL || attr_name == NULL || attr_value == NULL) {
1092 NTB_LOG(ERR, "Invalid arguments for getting attributes");
1096 hw = dev->dev_private;
1098 if (!strncmp(attr_name, NTB_TOPO_NAME, NTB_ATTR_NAME_LEN)) {
1099 *attr_value = hw->topo;
1100 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1101 attr_name, *attr_value);
1105 if (!strncmp(attr_name, NTB_LINK_STATUS_NAME, NTB_ATTR_NAME_LEN)) {
1106 /* hw->link_status only indicates hw link status. */
1107 *attr_value = hw->link_status && hw->peer_dev_up;
1108 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1109 attr_name, *attr_value);
1113 if (!strncmp(attr_name, NTB_SPEED_NAME, NTB_ATTR_NAME_LEN)) {
1114 *attr_value = hw->link_speed;
1115 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1116 attr_name, *attr_value);
1120 if (!strncmp(attr_name, NTB_WIDTH_NAME, NTB_ATTR_NAME_LEN)) {
1121 *attr_value = hw->link_width;
1122 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1123 attr_name, *attr_value);
1127 if (!strncmp(attr_name, NTB_MW_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1128 *attr_value = hw->mw_cnt;
1129 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1130 attr_name, *attr_value);
1134 if (!strncmp(attr_name, NTB_DB_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1135 *attr_value = hw->db_cnt;
1136 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1137 attr_name, *attr_value);
1141 if (!strncmp(attr_name, NTB_SPAD_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1142 *attr_value = hw->spad_cnt;
1143 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1144 attr_name, *attr_value);
1148 if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
1149 if (hw->ntb_ops->spad_read == NULL)
1151 index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
1152 *attr_value = (*hw->ntb_ops->spad_read)(dev,
1153 hw->spad_user_list[index], 0);
1154 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1155 attr_name, *attr_value);
1159 /* Attribute not found. */
1160 NTB_LOG(ERR, "Attribute not found.");
1164 static inline uint64_t
1165 ntb_stats_update(uint64_t offset, uint64_t stat)
1168 return (stat - offset);
1170 return (uint64_t)(((uint64_t)-1) - offset + stat + 1);
1174 ntb_xstats_get(const struct rte_rawdev *dev,
1175 const unsigned int ids[],
1179 struct ntb_hw *hw = dev->dev_private;
1180 uint32_t i, j, off, xstats_num;
1182 /* Calculate total stats of all queues. */
1183 for (i = 0; i < NTB_XSTATS_NUM; i++) {
1184 hw->ntb_xstats[i] = 0;
1185 for (j = 0; j < hw->queue_pairs; j++) {
1186 off = NTB_XSTATS_NUM * (j + 1) + i;
1187 hw->ntb_xstats[i] +=
1188 ntb_stats_update(hw->ntb_xstats_off[off],
1189 hw->ntb_xstats[off]);
1193 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1194 for (i = 0; i < n && ids[i] < xstats_num; i++) {
1195 if (ids[i] < NTB_XSTATS_NUM)
1196 values[i] = hw->ntb_xstats[ids[i]];
1199 ntb_stats_update(hw->ntb_xstats_off[ids[i]],
1200 hw->ntb_xstats[ids[i]]);
1207 ntb_xstats_get_names(const struct rte_rawdev *dev,
1208 struct rte_rawdev_xstats_name *xstats_names,
1211 struct ntb_hw *hw = dev->dev_private;
1212 uint32_t xstats_num, i, j, off;
1214 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1215 if (xstats_names == NULL || size < xstats_num)
1218 /* Total stats names */
1219 memcpy(xstats_names, ntb_xstats_names, sizeof(ntb_xstats_names));
1221 /* Queue stats names */
1222 for (i = 0; i < hw->queue_pairs; i++) {
1223 for (j = 0; j < NTB_XSTATS_NUM; j++) {
1224 off = j + (i + 1) * NTB_XSTATS_NUM;
1225 snprintf(xstats_names[off].name,
1226 sizeof(xstats_names[0].name),
1227 "%s_q%u", ntb_xstats_names[j].name, i);
1235 ntb_xstats_get_by_name(const struct rte_rawdev *dev,
1236 const char *name, unsigned int *id)
1238 struct rte_rawdev_xstats_name *xstats_names;
1239 struct ntb_hw *hw = dev->dev_private;
1240 uint32_t xstats_num, i, j, off;
1245 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1246 xstats_names = rte_zmalloc("ntb_stats_name",
1247 sizeof(struct rte_rawdev_xstats_name) *
1249 ntb_xstats_get_names(dev, xstats_names, xstats_num);
1251 /* Calculate total stats of all queues. */
1252 for (i = 0; i < NTB_XSTATS_NUM; i++) {
1253 for (j = 0; j < hw->queue_pairs; j++) {
1254 off = NTB_XSTATS_NUM * (j + 1) + i;
1255 hw->ntb_xstats[i] +=
1256 ntb_stats_update(hw->ntb_xstats_off[off],
1257 hw->ntb_xstats[off]);
1261 for (i = 0; i < xstats_num; i++) {
1262 if (!strncmp(name, xstats_names[i].name,
1263 RTE_RAW_DEV_XSTATS_NAME_SIZE)) {
1265 rte_free(xstats_names);
1266 if (i < NTB_XSTATS_NUM)
1267 return hw->ntb_xstats[i];
1269 return ntb_stats_update(hw->ntb_xstats_off[i],
1274 NTB_LOG(ERR, "Cannot find the xstats name.");
1280 ntb_xstats_reset(struct rte_rawdev *dev,
1281 const uint32_t ids[],
1284 struct ntb_hw *hw = dev->dev_private;
1285 uint32_t i, j, off, xstats_num;
1287 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1288 for (i = 0; i < nb_ids && ids[i] < xstats_num; i++) {
1289 if (ids[i] < NTB_XSTATS_NUM) {
1290 for (j = 0; j < hw->queue_pairs; j++) {
1291 off = NTB_XSTATS_NUM * (j + 1) + ids[i];
1292 hw->ntb_xstats_off[off] = hw->ntb_xstats[off];
1295 hw->ntb_xstats_off[ids[i]] = hw->ntb_xstats[ids[i]];
1302 static const struct rte_rawdev_ops ntb_ops = {
1303 .dev_info_get = ntb_dev_info_get,
1304 .dev_configure = ntb_dev_configure,
1305 .dev_start = ntb_dev_start,
1306 .dev_stop = ntb_dev_stop,
1307 .dev_close = ntb_dev_close,
1308 .dev_reset = ntb_dev_reset,
1310 .queue_def_conf = ntb_queue_conf_get,
1311 .queue_setup = ntb_queue_setup,
1312 .queue_release = ntb_queue_release,
1313 .queue_count = ntb_queue_count,
1315 .enqueue_bufs = ntb_enqueue_bufs,
1316 .dequeue_bufs = ntb_dequeue_bufs,
1318 .attr_get = ntb_attr_get,
1319 .attr_set = ntb_attr_set,
1321 .xstats_get = ntb_xstats_get,
1322 .xstats_get_names = ntb_xstats_get_names,
1323 .xstats_get_by_name = ntb_xstats_get_by_name,
1324 .xstats_reset = ntb_xstats_reset,
1328 ntb_init_hw(struct rte_rawdev *dev, struct rte_pci_device *pci_dev)
1330 struct ntb_hw *hw = dev->dev_private;
1331 struct rte_intr_handle *intr_handle;
1334 hw->pci_dev = pci_dev;
1335 hw->peer_dev_up = 0;
1336 hw->link_status = NTB_LINK_DOWN;
1337 hw->link_speed = NTB_SPEED_NONE;
1338 hw->link_width = NTB_WIDTH_NONE;
1340 switch (pci_dev->id.device_id) {
1341 case NTB_INTEL_DEV_ID_B2B_SKX:
1342 hw->ntb_ops = &intel_ntb_ops;
1345 NTB_LOG(ERR, "Not supported device.");
1349 if (hw->ntb_ops->ntb_dev_init == NULL)
1351 ret = (*hw->ntb_ops->ntb_dev_init)(dev);
1353 NTB_LOG(ERR, "Unable to init ntb dev.");
1357 if (hw->ntb_ops->set_link == NULL)
1359 ret = (*hw->ntb_ops->set_link)(dev, 1);
1363 /* Init doorbell. */
1364 hw->db_valid_mask = RTE_LEN2MASK(hw->db_cnt, uint64_t);
1366 intr_handle = &pci_dev->intr_handle;
1367 /* Register callback func to eal lib */
1368 rte_intr_callback_register(intr_handle,
1369 ntb_dev_intr_handler, dev);
1371 ret = rte_intr_efd_enable(intr_handle, hw->db_cnt);
1375 /* To clarify, the interrupt for each doorbell is already mapped
1376 * by default for intel gen3. They are mapped to msix vec 1-32,
1377 * and hardware intr is mapped to 0. Map all to 0 for uio.
1379 if (!rte_intr_cap_multiple(intr_handle)) {
1380 for (i = 0; i < hw->db_cnt; i++) {
1381 if (hw->ntb_ops->vector_bind == NULL)
1383 ret = (*hw->ntb_ops->vector_bind)(dev, i, 0);
1389 if (hw->ntb_ops->db_set_mask == NULL ||
1390 hw->ntb_ops->peer_db_set == NULL) {
1391 NTB_LOG(ERR, "Doorbell is not supported.");
1395 ret = (*hw->ntb_ops->db_set_mask)(dev, hw->db_mask);
1397 NTB_LOG(ERR, "Unable to enable intr for all dbs.");
1401 /* enable uio intr after callback register */
1402 rte_intr_enable(intr_handle);
1408 ntb_create(struct rte_pci_device *pci_dev, int socket_id)
1410 char name[RTE_RAWDEV_NAME_MAX_LEN];
1411 struct rte_rawdev *rawdev = NULL;
1414 if (pci_dev == NULL) {
1415 NTB_LOG(ERR, "Invalid pci_dev.");
1419 memset(name, 0, sizeof(name));
1420 snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
1421 pci_dev->addr.bus, pci_dev->addr.devid,
1422 pci_dev->addr.function);
1424 NTB_LOG(INFO, "Init %s on NUMA node %d", name, socket_id);
1426 /* Allocate device structure. */
1427 rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct ntb_hw),
1429 if (rawdev == NULL) {
1430 NTB_LOG(ERR, "Unable to allocate rawdev.");
1434 rawdev->dev_ops = &ntb_ops;
1435 rawdev->device = &pci_dev->device;
1436 rawdev->driver_name = pci_dev->driver->driver.name;
1438 ret = ntb_init_hw(rawdev, pci_dev);
1440 NTB_LOG(ERR, "Unable to init ntb hw.");
1448 rte_rawdev_pmd_release(rawdev);
1454 ntb_destroy(struct rte_pci_device *pci_dev)
1456 char name[RTE_RAWDEV_NAME_MAX_LEN];
1457 struct rte_rawdev *rawdev;
1460 if (pci_dev == NULL) {
1461 NTB_LOG(ERR, "Invalid pci_dev.");
1466 memset(name, 0, sizeof(name));
1467 snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
1468 pci_dev->addr.bus, pci_dev->addr.devid,
1469 pci_dev->addr.function);
1471 NTB_LOG(INFO, "Closing %s on NUMA node %d", name, rte_socket_id());
1473 rawdev = rte_rawdev_pmd_get_named_dev(name);
1474 if (rawdev == NULL) {
1475 NTB_LOG(ERR, "Invalid device name (%s)", name);
1480 ret = rte_rawdev_pmd_release(rawdev);
1482 NTB_LOG(ERR, "Failed to destroy ntb rawdev.");
1488 ntb_probe(struct rte_pci_driver *pci_drv __rte_unused,
1489 struct rte_pci_device *pci_dev)
1491 return ntb_create(pci_dev, rte_socket_id());
1495 ntb_remove(struct rte_pci_device *pci_dev)
1497 return ntb_destroy(pci_dev);
1501 static struct rte_pci_driver rte_ntb_pmd = {
1502 .id_table = pci_id_ntb_map,
1503 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_WC_ACTIVATE,
1505 .remove = ntb_remove,
1508 RTE_PMD_REGISTER_PCI(raw_ntb, rte_ntb_pmd);
1509 RTE_PMD_REGISTER_PCI_TABLE(raw_ntb, pci_id_ntb_map);
1510 RTE_PMD_REGISTER_KMOD_DEP(raw_ntb, "* igb_uio | uio_pci_generic | vfio-pci");
1511 RTE_LOG_REGISTER(ntb_logtype, pmd.raw.ntb, INFO);