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 { RTE_PCI_DEVICE(NTB_INTEL_VENDOR_ID, NTB_INTEL_DEV_ID_B2B_ICX) },
29 { .vendor_id = 0, /* sentinel */ },
32 /* Align with enum ntb_xstats_idx */
33 static struct rte_rawdev_xstats_name ntb_xstats_names[] = {
41 #define NTB_XSTATS_NUM RTE_DIM(ntb_xstats_names)
44 ntb_link_cleanup(struct rte_rawdev *dev)
46 struct ntb_hw *hw = dev->dev_private;
49 if (hw->ntb_ops->spad_write == NULL ||
50 hw->ntb_ops->mw_set_trans == NULL) {
51 NTB_LOG(ERR, "Not supported to clean up link.");
55 /* Clean spad registers. */
56 for (i = 0; i < hw->spad_cnt; i++) {
57 status = (*hw->ntb_ops->spad_write)(dev, i, 0, 0);
59 NTB_LOG(ERR, "Failed to clean local spad.");
62 /* Clear mw so that peer cannot access local memory.*/
63 for (i = 0; i < hw->used_mw_num; i++) {
64 status = (*hw->ntb_ops->mw_set_trans)(dev, i, 0, 0);
66 NTB_LOG(ERR, "Failed to clean mw.");
71 ntb_handshake_work(const struct rte_rawdev *dev)
73 struct ntb_hw *hw = dev->dev_private;
77 if (hw->ntb_ops->spad_write == NULL ||
78 hw->ntb_ops->mw_set_trans == NULL) {
79 NTB_LOG(ERR, "Scratchpad/MW setting is not supported.");
83 /* Tell peer the mw info of local side. */
84 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_NUM_MWS, 1, hw->mw_cnt);
87 for (i = 0; i < hw->mw_cnt; i++) {
88 NTB_LOG(INFO, "Local %u mw size: 0x%"PRIx64"", i,
90 val = hw->mw_size[i] >> 32;
91 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_SZ_H + 2 * i,
96 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_SZ_L + 2 * i,
102 /* Tell peer about the queue info and map memory to the peer. */
103 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_Q_SZ, 1, hw->queue_size);
106 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_NUM_QPS, 1,
110 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_USED_MWS, 1,
114 for (i = 0; i < hw->used_mw_num; i++) {
115 val = (uint64_t)(size_t)(hw->mz[i]->addr) >> 32;
116 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_BA_H + 2 * i,
120 val = (uint64_t)(size_t)(hw->mz[i]->addr);
121 ret = (*hw->ntb_ops->spad_write)(dev, SPAD_MW0_BA_L + 2 * i,
127 for (i = 0; i < hw->used_mw_num; i++) {
128 ret = (*hw->ntb_ops->mw_set_trans)(dev, i, hw->mz[i]->iova,
134 /* Ring doorbell 0 to tell peer the device is ready. */
135 ret = (*hw->ntb_ops->peer_db_set)(dev, 0);
143 ntb_dev_intr_handler(void *param)
145 struct rte_rawdev *dev = (struct rte_rawdev *)param;
146 struct ntb_hw *hw = dev->dev_private;
147 uint32_t val_h, val_l;
148 uint64_t peer_mw_size;
149 uint64_t db_bits = 0;
153 if (hw->ntb_ops->db_read == NULL ||
154 hw->ntb_ops->db_clear == NULL ||
155 hw->ntb_ops->peer_db_set == NULL) {
156 NTB_LOG(ERR, "Doorbell is not supported.");
160 db_bits = (*hw->ntb_ops->db_read)(dev);
162 NTB_LOG(ERR, "No doorbells");
164 /* Doorbell 0 is for peer device ready. */
166 NTB_LOG(INFO, "DB0: Peer device is up.");
167 /* Clear received doorbell. */
168 (*hw->ntb_ops->db_clear)(dev, 1);
171 * Peer dev is already up. All mw settings are already done.
177 if (hw->ntb_ops->spad_read == NULL) {
178 NTB_LOG(ERR, "Scratchpad read is not supported.");
182 /* Check if mw setting on the peer is the same as local. */
183 peer_mw_cnt = (*hw->ntb_ops->spad_read)(dev, SPAD_NUM_MWS, 0);
184 if (peer_mw_cnt != hw->mw_cnt) {
185 NTB_LOG(ERR, "Both mw cnt must be the same.");
189 for (i = 0; i < hw->mw_cnt; i++) {
190 val_h = (*hw->ntb_ops->spad_read)
191 (dev, SPAD_MW0_SZ_H + 2 * i, 0);
192 val_l = (*hw->ntb_ops->spad_read)
193 (dev, SPAD_MW0_SZ_L + 2 * i, 0);
194 peer_mw_size = ((uint64_t)val_h << 32) | val_l;
195 NTB_LOG(DEBUG, "Peer %u mw size: 0x%"PRIx64"", i,
197 if (peer_mw_size != hw->mw_size[i]) {
198 NTB_LOG(ERR, "Mw config must be the same.");
206 * Handshake with peer. Spad_write & mw_set_trans only works
207 * when both devices are up. So write spad again when db is
208 * received. And set db again for the later device who may miss
211 if (ntb_handshake_work(dev) < 0) {
212 NTB_LOG(ERR, "Handshake work failed.");
216 /* To get the link info. */
217 if (hw->ntb_ops->get_link_status == NULL) {
218 NTB_LOG(ERR, "Not supported to get link status.");
221 (*hw->ntb_ops->get_link_status)(dev);
222 NTB_LOG(INFO, "Link is up. Link speed: %u. Link width: %u",
223 hw->link_speed, hw->link_width);
227 if (db_bits & (1 << 1)) {
228 NTB_LOG(INFO, "DB1: Peer device is down.");
229 /* Clear received doorbell. */
230 (*hw->ntb_ops->db_clear)(dev, 2);
232 /* Peer device will be down, So clean local side too. */
233 ntb_link_cleanup(dev);
236 /* Response peer's dev_stop request. */
237 (*hw->ntb_ops->peer_db_set)(dev, 2);
241 if (db_bits & (1 << 2)) {
242 NTB_LOG(INFO, "DB2: Peer device agrees dev to be down.");
243 /* Clear received doorbell. */
244 (*hw->ntb_ops->db_clear)(dev, (1 << 2));
249 /* Clear other received doorbells. */
250 (*hw->ntb_ops->db_clear)(dev, db_bits);
254 ntb_queue_conf_get(struct rte_rawdev *dev,
256 rte_rawdev_obj_t queue_conf,
259 struct ntb_queue_conf *q_conf = queue_conf;
260 struct ntb_hw *hw = dev->dev_private;
262 if (conf_size != sizeof(*q_conf))
265 q_conf->tx_free_thresh = hw->tx_queues[queue_id]->tx_free_thresh;
266 q_conf->nb_desc = hw->rx_queues[queue_id]->nb_rx_desc;
267 q_conf->rx_mp = hw->rx_queues[queue_id]->mpool;
273 ntb_rxq_release_mbufs(struct ntb_rx_queue *q)
277 if (!q || !q->sw_ring) {
278 NTB_LOG(ERR, "Pointer to rxq or sw_ring is NULL");
282 for (i = 0; i < q->nb_rx_desc; i++) {
283 if (q->sw_ring[i].mbuf) {
284 rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
285 q->sw_ring[i].mbuf = NULL;
291 ntb_rxq_release(struct ntb_rx_queue *rxq)
294 NTB_LOG(ERR, "Pointer to rxq is NULL");
298 ntb_rxq_release_mbufs(rxq);
300 rte_free(rxq->sw_ring);
305 ntb_rxq_setup(struct rte_rawdev *dev,
307 rte_rawdev_obj_t queue_conf,
310 struct ntb_queue_conf *rxq_conf = queue_conf;
311 struct ntb_hw *hw = dev->dev_private;
312 struct ntb_rx_queue *rxq;
314 if (conf_size != sizeof(*rxq_conf))
317 /* Allocate the rx queue data structure */
318 rxq = rte_zmalloc_socket("ntb rx queue",
319 sizeof(struct ntb_rx_queue),
323 NTB_LOG(ERR, "Failed to allocate memory for "
324 "rx queue data structure.");
328 if (rxq_conf->rx_mp == NULL) {
329 NTB_LOG(ERR, "Invalid null mempool pointer.");
332 rxq->nb_rx_desc = rxq_conf->nb_desc;
333 rxq->mpool = rxq_conf->rx_mp;
334 rxq->port_id = dev->dev_id;
335 rxq->queue_id = qp_id;
338 /* Allocate the software ring. */
340 rte_zmalloc_socket("ntb rx sw ring",
341 sizeof(struct ntb_rx_entry) *
346 ntb_rxq_release(rxq);
348 NTB_LOG(ERR, "Failed to allocate memory for SW ring");
352 hw->rx_queues[qp_id] = rxq;
358 ntb_txq_release_mbufs(struct ntb_tx_queue *q)
362 if (!q || !q->sw_ring) {
363 NTB_LOG(ERR, "Pointer to txq or sw_ring is NULL");
367 for (i = 0; i < q->nb_tx_desc; i++) {
368 if (q->sw_ring[i].mbuf) {
369 rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
370 q->sw_ring[i].mbuf = NULL;
376 ntb_txq_release(struct ntb_tx_queue *txq)
379 NTB_LOG(ERR, "Pointer to txq is NULL");
383 ntb_txq_release_mbufs(txq);
385 rte_free(txq->sw_ring);
390 ntb_txq_setup(struct rte_rawdev *dev,
392 rte_rawdev_obj_t queue_conf,
395 struct ntb_queue_conf *txq_conf = queue_conf;
396 struct ntb_hw *hw = dev->dev_private;
397 struct ntb_tx_queue *txq;
400 if (conf_size != sizeof(*txq_conf))
403 /* Allocate the TX queue data structure. */
404 txq = rte_zmalloc_socket("ntb tx queue",
405 sizeof(struct ntb_tx_queue),
409 NTB_LOG(ERR, "Failed to allocate memory for "
410 "tx queue structure");
414 txq->nb_tx_desc = txq_conf->nb_desc;
415 txq->port_id = dev->dev_id;
416 txq->queue_id = qp_id;
419 /* Allocate software ring */
421 rte_zmalloc_socket("ntb tx sw ring",
422 sizeof(struct ntb_tx_entry) *
427 ntb_txq_release(txq);
429 NTB_LOG(ERR, "Failed to allocate memory for SW TX ring");
433 prev = txq->nb_tx_desc - 1;
434 for (i = 0; i < txq->nb_tx_desc; i++) {
435 txq->sw_ring[i].mbuf = NULL;
436 txq->sw_ring[i].last_id = i;
437 txq->sw_ring[prev].next_id = i;
441 txq->tx_free_thresh = txq_conf->tx_free_thresh ?
442 txq_conf->tx_free_thresh :
443 NTB_DFLT_TX_FREE_THRESH;
444 if (txq->tx_free_thresh >= txq->nb_tx_desc - 3) {
445 NTB_LOG(ERR, "tx_free_thresh must be less than nb_desc - 3. "
446 "(tx_free_thresh=%u qp_id=%u)", txq->tx_free_thresh,
451 hw->tx_queues[qp_id] = txq;
458 ntb_queue_setup(struct rte_rawdev *dev,
460 rte_rawdev_obj_t queue_conf,
463 struct ntb_hw *hw = dev->dev_private;
466 if (queue_id >= hw->queue_pairs)
469 ret = ntb_txq_setup(dev, queue_id, queue_conf, conf_size);
473 ret = ntb_rxq_setup(dev, queue_id, queue_conf, conf_size);
479 ntb_queue_release(struct rte_rawdev *dev, uint16_t queue_id)
481 struct ntb_hw *hw = dev->dev_private;
483 if (queue_id >= hw->queue_pairs)
486 ntb_txq_release(hw->tx_queues[queue_id]);
487 hw->tx_queues[queue_id] = NULL;
488 ntb_rxq_release(hw->rx_queues[queue_id]);
489 hw->rx_queues[queue_id] = NULL;
495 ntb_queue_count(struct rte_rawdev *dev)
497 struct ntb_hw *hw = dev->dev_private;
498 return hw->queue_pairs;
502 ntb_queue_init(struct rte_rawdev *dev, uint16_t qp_id)
504 struct ntb_hw *hw = dev->dev_private;
505 struct ntb_rx_queue *rxq = hw->rx_queues[qp_id];
506 struct ntb_tx_queue *txq = hw->tx_queues[qp_id];
507 volatile struct ntb_header *local_hdr;
508 struct ntb_header *remote_hdr;
509 uint16_t q_size = hw->queue_size;
514 if (hw->ntb_ops->get_peer_mw_addr == NULL) {
515 NTB_LOG(ERR, "Getting peer mw addr is not supported.");
519 /* Put queue info into the start of shared memory. */
520 hdr_offset = hw->hdr_size_per_queue * qp_id;
521 local_hdr = (volatile struct ntb_header *)
522 ((size_t)hw->mz[0]->addr + hdr_offset);
523 bar_addr = (*hw->ntb_ops->get_peer_mw_addr)(dev, 0);
524 if (bar_addr == NULL)
526 remote_hdr = (struct ntb_header *)
527 ((size_t)bar_addr + hdr_offset);
530 rxq->rx_desc_ring = (struct ntb_desc *)
531 (&remote_hdr->desc_ring);
532 rxq->rx_used_ring = (volatile struct ntb_used *)
533 (&local_hdr->desc_ring[q_size]);
534 rxq->avail_cnt = &remote_hdr->avail_cnt;
535 rxq->used_cnt = &local_hdr->used_cnt;
537 for (i = 0; i < rxq->nb_rx_desc - 1; i++) {
538 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mpool);
539 if (unlikely(!mbuf)) {
540 NTB_LOG(ERR, "Failed to allocate mbuf for RX");
543 mbuf->port = dev->dev_id;
545 rxq->sw_ring[i].mbuf = mbuf;
547 rxq->rx_desc_ring[i].addr = rte_pktmbuf_mtod(mbuf, size_t);
548 rxq->rx_desc_ring[i].len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
551 *rxq->avail_cnt = rxq->nb_rx_desc - 1;
552 rxq->last_avail = rxq->nb_rx_desc - 1;
556 txq->tx_desc_ring = (volatile struct ntb_desc *)
557 (&local_hdr->desc_ring);
558 txq->tx_used_ring = (struct ntb_used *)
559 (&remote_hdr->desc_ring[q_size]);
560 txq->avail_cnt = &local_hdr->avail_cnt;
561 txq->used_cnt = &remote_hdr->used_cnt;
567 txq->nb_tx_free = txq->nb_tx_desc - 1;
569 /* Set per queue stats. */
570 for (i = 0; i < NTB_XSTATS_NUM; i++) {
571 hw->ntb_xstats[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
572 hw->ntb_xstats_off[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
579 ntb_enqueue_cleanup(struct ntb_tx_queue *txq)
581 struct ntb_tx_entry *sw_ring = txq->sw_ring;
582 uint16_t tx_free = txq->last_avail;
583 uint16_t nb_to_clean, i;
585 /* avail_cnt + 1 represents where to rx next in the peer. */
586 nb_to_clean = (*txq->avail_cnt - txq->last_avail + 1 +
587 txq->nb_tx_desc) & (txq->nb_tx_desc - 1);
588 nb_to_clean = RTE_MIN(nb_to_clean, txq->tx_free_thresh);
589 for (i = 0; i < nb_to_clean; i++) {
590 if (sw_ring[tx_free].mbuf)
591 rte_pktmbuf_free_seg(sw_ring[tx_free].mbuf);
592 tx_free = (tx_free + 1) & (txq->nb_tx_desc - 1);
595 txq->nb_tx_free += nb_to_clean;
596 txq->last_avail = tx_free;
600 ntb_enqueue_bufs(struct rte_rawdev *dev,
601 struct rte_rawdev_buf **buffers,
603 rte_rawdev_obj_t context)
605 struct ntb_hw *hw = dev->dev_private;
606 struct ntb_tx_queue *txq = hw->tx_queues[(size_t)context];
607 struct ntb_tx_entry *sw_ring = txq->sw_ring;
608 struct rte_mbuf *txm;
609 struct ntb_used tx_used[NTB_MAX_DESC_SIZE];
610 volatile struct ntb_desc *tx_item;
611 uint16_t tx_last, nb_segs, off, last_used, avail_cnt;
612 uint16_t nb_mbufs = 0;
618 if (unlikely(hw->ntb_ops->ioremap == NULL)) {
619 NTB_LOG(ERR, "Ioremap not supported.");
623 if (unlikely(dev->started == 0 || hw->peer_dev_up == 0)) {
624 NTB_LOG(DEBUG, "Link is not up.");
628 if (txq->nb_tx_free < txq->tx_free_thresh)
629 ntb_enqueue_cleanup(txq);
631 off = NTB_XSTATS_NUM * ((size_t)context + 1);
632 last_used = txq->last_used;
633 avail_cnt = *txq->avail_cnt;/* Where to alloc next. */
634 for (nb_tx = 0; nb_tx < count; nb_tx++) {
635 txm = (struct rte_mbuf *)(buffers[nb_tx]->buf_addr);
636 if (txm == NULL || txq->nb_tx_free < txm->nb_segs)
639 tx_last = (txq->last_used + txm->nb_segs - 1) &
640 (txq->nb_tx_desc - 1);
641 nb_segs = txm->nb_segs;
642 for (i = 0; i < nb_segs; i++) {
643 /* Not enough ring space for tx. */
644 if (txq->last_used == avail_cnt)
646 sw_ring[txq->last_used].mbuf = txm;
647 tx_item = txq->tx_desc_ring + txq->last_used;
650 (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
653 if (txm->data_len > tx_item->len) {
654 NTB_LOG(ERR, "Data length exceeds buf length."
655 " Only %u data would be transmitted.",
657 txm->data_len = tx_item->len;
660 /* translate remote virtual addr to bar virtual addr */
661 buf_addr = (*hw->ntb_ops->ioremap)(dev, tx_item->addr);
662 if (buf_addr == NULL) {
663 (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
664 NTB_LOG(ERR, "Null remap addr.");
667 rte_memcpy(buf_addr, rte_pktmbuf_mtod(txm, void *),
670 tx_used[nb_mbufs].len = txm->data_len;
671 tx_used[nb_mbufs++].flags = (txq->last_used ==
676 bytes += txm->data_len;
680 sw_ring[txq->last_used].next_id = (txq->last_used + 1) &
681 (txq->nb_tx_desc - 1);
682 sw_ring[txq->last_used].last_id = tx_last;
683 txq->last_used = (txq->last_used + 1) &
684 (txq->nb_tx_desc - 1);
686 txq->nb_tx_free -= nb_segs;
692 if (nb_mbufs > txq->nb_tx_desc - last_used) {
693 nb1 = txq->nb_tx_desc - last_used;
694 nb2 = nb_mbufs - txq->nb_tx_desc + last_used;
699 rte_memcpy(txq->tx_used_ring + last_used, tx_used,
700 sizeof(struct ntb_used) * nb1);
701 rte_memcpy(txq->tx_used_ring, tx_used + nb1,
702 sizeof(struct ntb_used) * nb2);
704 *txq->used_cnt = txq->last_used;
706 /* update queue stats */
707 hw->ntb_xstats[NTB_TX_BYTES_ID + off] += bytes;
708 hw->ntb_xstats[NTB_TX_PKTS_ID + off] += nb_tx;
715 ntb_dequeue_bufs(struct rte_rawdev *dev,
716 struct rte_rawdev_buf **buffers,
718 rte_rawdev_obj_t context)
720 struct ntb_hw *hw = dev->dev_private;
721 struct ntb_rx_queue *rxq = hw->rx_queues[(size_t)context];
722 struct ntb_rx_entry *sw_ring = rxq->sw_ring;
723 struct ntb_desc rx_desc[NTB_MAX_DESC_SIZE];
724 struct rte_mbuf *first, *rxm_t;
725 struct rte_mbuf *prev = NULL;
726 volatile struct ntb_used *rx_item;
727 uint16_t nb_mbufs = 0;
730 uint16_t off, last_avail, used_cnt, used_nb;
733 if (unlikely(dev->started == 0 || hw->peer_dev_up == 0)) {
734 NTB_LOG(DEBUG, "Link is not up");
738 used_cnt = *rxq->used_cnt;
740 if (rxq->last_used == used_cnt)
743 last_avail = rxq->last_avail;
744 used_nb = (used_cnt - rxq->last_used) & (rxq->nb_rx_desc - 1);
745 count = RTE_MIN(count, used_nb);
746 for (nb_rx = 0; nb_rx < count; nb_rx++) {
749 rx_item = rxq->rx_used_ring + rxq->last_used;
750 rxm_t = sw_ring[rxq->last_used].mbuf;
751 rxm_t->data_len = rx_item->len;
752 rxm_t->data_off = RTE_PKTMBUF_HEADROOM;
753 rxm_t->port = rxq->port_id;
759 buffers[nb_rx]->buf_addr = rxm_t;
766 first->pkt_len += prev->data_len;
767 rxq->last_used = (rxq->last_used + 1) &
768 (rxq->nb_rx_desc - 1);
771 rxm_t = rte_mbuf_raw_alloc(rxq->mpool);
772 if (unlikely(rxm_t == NULL)) {
773 NTB_LOG(ERR, "recv alloc mbuf failed.");
776 rxm_t->port = rxq->port_id;
777 sw_ring[rxq->last_avail].mbuf = rxm_t;
781 rx_desc[nb_mbufs].addr =
782 rte_pktmbuf_mtod(rxm_t, size_t);
783 rx_desc[nb_mbufs++].len = rxm_t->buf_len -
784 RTE_PKTMBUF_HEADROOM;
785 rxq->last_avail = (rxq->last_avail + 1) &
786 (rxq->nb_rx_desc - 1);
788 if (rx_item->flags & NTB_FLAG_EOP)
792 bytes += first->pkt_len;
798 if (nb_mbufs > rxq->nb_rx_desc - last_avail) {
799 nb1 = rxq->nb_rx_desc - last_avail;
800 nb2 = nb_mbufs - rxq->nb_rx_desc + last_avail;
805 rte_memcpy(rxq->rx_desc_ring + last_avail, rx_desc,
806 sizeof(struct ntb_desc) * nb1);
807 rte_memcpy(rxq->rx_desc_ring, rx_desc + nb1,
808 sizeof(struct ntb_desc) * nb2);
810 *rxq->avail_cnt = rxq->last_avail;
812 /* update queue stats */
813 off = NTB_XSTATS_NUM * ((size_t)context + 1);
814 hw->ntb_xstats[NTB_RX_BYTES_ID + off] += bytes;
815 hw->ntb_xstats[NTB_RX_PKTS_ID + off] += nb_rx;
816 hw->ntb_xstats[NTB_RX_MISS_ID + off] += (count - nb_rx);
823 ntb_dev_info_get(struct rte_rawdev *dev, rte_rawdev_obj_t dev_info,
824 size_t dev_info_size)
826 struct ntb_hw *hw = dev->dev_private;
827 struct ntb_dev_info *info = dev_info;
829 if (dev_info_size != sizeof(*info)) {
830 NTB_LOG(ERR, "Invalid size parameter to %s", __func__);
834 info->mw_cnt = hw->mw_cnt;
835 info->mw_size = hw->mw_size;
838 * Intel hardware requires that mapped memory base address should be
839 * aligned with EMBARSZ and needs continuous memzone.
841 info->mw_size_align = (uint8_t)(hw->pci_dev->id.vendor_id ==
842 NTB_INTEL_VENDOR_ID);
844 if (!hw->queue_size || !hw->queue_pairs) {
845 NTB_LOG(ERR, "No queue size and queue num assigned.");
849 hw->hdr_size_per_queue = RTE_ALIGN(sizeof(struct ntb_header) +
850 hw->queue_size * sizeof(struct ntb_desc) +
851 hw->queue_size * sizeof(struct ntb_used),
852 RTE_CACHE_LINE_SIZE);
853 info->ntb_hdr_size = hw->hdr_size_per_queue * hw->queue_pairs;
859 ntb_dev_configure(const struct rte_rawdev *dev, rte_rawdev_obj_t config,
862 struct ntb_dev_config *conf = config;
863 struct ntb_hw *hw = dev->dev_private;
867 if (conf == NULL || config_size != sizeof(*conf))
870 hw->queue_pairs = conf->num_queues;
871 hw->queue_size = conf->queue_size;
872 hw->used_mw_num = conf->mz_num;
873 hw->mz = conf->mz_list;
874 hw->rx_queues = rte_zmalloc("ntb_rx_queues",
875 sizeof(struct ntb_rx_queue *) * hw->queue_pairs, 0);
876 hw->tx_queues = rte_zmalloc("ntb_tx_queues",
877 sizeof(struct ntb_tx_queue *) * hw->queue_pairs, 0);
878 /* First total stats, then per queue stats. */
879 xstats_num = (hw->queue_pairs + 1) * NTB_XSTATS_NUM;
880 hw->ntb_xstats = rte_zmalloc("ntb_xstats", xstats_num *
881 sizeof(uint64_t), 0);
882 hw->ntb_xstats_off = rte_zmalloc("ntb_xstats_off", xstats_num *
883 sizeof(uint64_t), 0);
885 /* Start handshake with the peer. */
886 ret = ntb_handshake_work(dev);
888 rte_free(hw->rx_queues);
889 rte_free(hw->tx_queues);
890 hw->rx_queues = NULL;
891 hw->tx_queues = NULL;
899 ntb_dev_start(struct rte_rawdev *dev)
901 struct ntb_hw *hw = dev->dev_private;
902 uint32_t peer_base_l, peer_val;
903 uint64_t peer_base_h;
907 if (!hw->link_status || !hw->peer_dev_up)
910 /* Set total stats. */
911 for (i = 0; i < NTB_XSTATS_NUM; i++) {
912 hw->ntb_xstats[i] = 0;
913 hw->ntb_xstats_off[i] = 0;
916 for (i = 0; i < hw->queue_pairs; i++) {
917 ret = ntb_queue_init(dev, i);
919 NTB_LOG(ERR, "Failed to init queue.");
924 hw->peer_mw_base = rte_zmalloc("ntb_peer_mw_base", hw->mw_cnt *
925 sizeof(uint64_t), 0);
926 if (hw->peer_mw_base == NULL) {
927 NTB_LOG(ERR, "Cannot allocate memory for peer mw base.");
932 if (hw->ntb_ops->spad_read == NULL) {
937 peer_val = (*hw->ntb_ops->spad_read)(dev, SPAD_Q_SZ, 0);
938 if (peer_val != hw->queue_size) {
939 NTB_LOG(ERR, "Inconsistent queue size! (local: %u peer: %u)",
940 hw->queue_size, peer_val);
945 peer_val = (*hw->ntb_ops->spad_read)(dev, SPAD_NUM_QPS, 0);
946 if (peer_val != hw->queue_pairs) {
947 NTB_LOG(ERR, "Inconsistent number of queues! (local: %u peer:"
948 " %u)", hw->queue_pairs, peer_val);
953 hw->peer_used_mws = (*hw->ntb_ops->spad_read)(dev, SPAD_USED_MWS, 0);
955 for (i = 0; i < hw->peer_used_mws; i++) {
956 peer_base_h = (*hw->ntb_ops->spad_read)(dev,
957 SPAD_MW0_BA_H + 2 * i, 0);
958 peer_base_l = (*hw->ntb_ops->spad_read)(dev,
959 SPAD_MW0_BA_L + 2 * i, 0);
960 hw->peer_mw_base[i] = (peer_base_h << 32) + peer_base_l;
968 rte_free(hw->peer_mw_base);
970 for (i = 0; i < hw->queue_pairs; i++) {
971 ntb_rxq_release_mbufs(hw->rx_queues[i]);
972 ntb_txq_release_mbufs(hw->tx_queues[i]);
979 ntb_dev_stop(struct rte_rawdev *dev)
981 struct ntb_hw *hw = dev->dev_private;
985 if (!hw->peer_dev_up)
988 ntb_link_cleanup(dev);
990 /* Notify the peer that device will be down. */
991 if (hw->ntb_ops->peer_db_set == NULL) {
992 NTB_LOG(ERR, "Peer doorbell setting is not supported.");
995 status = (*hw->ntb_ops->peer_db_set)(dev, 1);
997 NTB_LOG(ERR, "Failed to tell peer device is down.");
1002 * Set time out as 1s in case that the peer is stopped accidently
1003 * without any notification.
1007 /* Wait for cleanup work down before db mask clear. */
1008 while (hw->peer_dev_up && time_out) {
1014 /* Clear doorbells mask. */
1015 if (hw->ntb_ops->db_set_mask == NULL) {
1016 NTB_LOG(ERR, "Doorbell mask setting is not supported.");
1019 status = (*hw->ntb_ops->db_set_mask)(dev,
1020 (((uint64_t)1 << hw->db_cnt) - 1));
1022 NTB_LOG(ERR, "Failed to clear doorbells.");
1024 for (i = 0; i < hw->queue_pairs; i++) {
1025 ntb_rxq_release_mbufs(hw->rx_queues[i]);
1026 ntb_txq_release_mbufs(hw->tx_queues[i]);
1033 ntb_dev_close(struct rte_rawdev *dev)
1035 struct ntb_hw *hw = dev->dev_private;
1036 struct rte_intr_handle *intr_handle;
1043 for (i = 0; i < hw->queue_pairs; i++)
1044 ntb_queue_release(dev, i);
1045 hw->queue_pairs = 0;
1047 intr_handle = &hw->pci_dev->intr_handle;
1048 /* Clean datapath event and vec mapping */
1049 rte_intr_efd_disable(intr_handle);
1050 if (intr_handle->intr_vec) {
1051 rte_free(intr_handle->intr_vec);
1052 intr_handle->intr_vec = NULL;
1054 /* Disable uio intr before callback unregister */
1055 rte_intr_disable(intr_handle);
1057 /* Unregister callback func to eal lib */
1058 rte_intr_callback_unregister(intr_handle,
1059 ntb_dev_intr_handler, dev);
1065 ntb_dev_reset(struct rte_rawdev *rawdev __rte_unused)
1071 ntb_attr_set(struct rte_rawdev *dev, const char *attr_name,
1072 uint64_t attr_value)
1077 if (dev == NULL || attr_name == NULL) {
1078 NTB_LOG(ERR, "Invalid arguments for setting attributes");
1082 hw = dev->dev_private;
1084 if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
1085 if (hw->ntb_ops->spad_write == NULL)
1087 index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
1088 if (index < 0 || index >= NTB_SPAD_USER_MAX_NUM) {
1089 NTB_LOG(ERR, "Invalid attribute (%s)", attr_name);
1092 (*hw->ntb_ops->spad_write)(dev, hw->spad_user_list[index],
1094 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
1095 attr_name, attr_value);
1099 if (!strncmp(attr_name, NTB_QUEUE_SZ_NAME, NTB_ATTR_NAME_LEN)) {
1100 hw->queue_size = attr_value;
1101 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
1102 attr_name, attr_value);
1106 if (!strncmp(attr_name, NTB_QUEUE_NUM_NAME, NTB_ATTR_NAME_LEN)) {
1107 hw->queue_pairs = attr_value;
1108 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
1109 attr_name, attr_value);
1113 /* Attribute not found. */
1114 NTB_LOG(ERR, "Attribute not found.");
1119 ntb_attr_get(struct rte_rawdev *dev, const char *attr_name,
1120 uint64_t *attr_value)
1125 if (dev == NULL || attr_name == NULL || attr_value == NULL) {
1126 NTB_LOG(ERR, "Invalid arguments for getting attributes");
1130 hw = dev->dev_private;
1132 if (!strncmp(attr_name, NTB_TOPO_NAME, NTB_ATTR_NAME_LEN)) {
1133 *attr_value = hw->topo;
1134 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1135 attr_name, *attr_value);
1139 if (!strncmp(attr_name, NTB_LINK_STATUS_NAME, NTB_ATTR_NAME_LEN)) {
1140 /* hw->link_status only indicates hw link status. */
1141 *attr_value = hw->link_status && hw->peer_dev_up;
1142 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1143 attr_name, *attr_value);
1147 if (!strncmp(attr_name, NTB_SPEED_NAME, NTB_ATTR_NAME_LEN)) {
1148 *attr_value = hw->link_speed;
1149 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1150 attr_name, *attr_value);
1154 if (!strncmp(attr_name, NTB_WIDTH_NAME, NTB_ATTR_NAME_LEN)) {
1155 *attr_value = hw->link_width;
1156 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1157 attr_name, *attr_value);
1161 if (!strncmp(attr_name, NTB_MW_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1162 *attr_value = hw->mw_cnt;
1163 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1164 attr_name, *attr_value);
1168 if (!strncmp(attr_name, NTB_DB_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1169 *attr_value = hw->db_cnt;
1170 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1171 attr_name, *attr_value);
1175 if (!strncmp(attr_name, NTB_SPAD_CNT_NAME, NTB_ATTR_NAME_LEN)) {
1176 *attr_value = hw->spad_cnt;
1177 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1178 attr_name, *attr_value);
1182 if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
1183 if (hw->ntb_ops->spad_read == NULL)
1185 index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
1186 if (index < 0 || index >= NTB_SPAD_USER_MAX_NUM) {
1187 NTB_LOG(ERR, "Attribute (%s) out of range", attr_name);
1190 *attr_value = (*hw->ntb_ops->spad_read)(dev,
1191 hw->spad_user_list[index], 0);
1192 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
1193 attr_name, *attr_value);
1197 /* Attribute not found. */
1198 NTB_LOG(ERR, "Attribute not found.");
1202 static inline uint64_t
1203 ntb_stats_update(uint64_t offset, uint64_t stat)
1206 return (stat - offset);
1208 return (uint64_t)(((uint64_t)-1) - offset + stat + 1);
1212 ntb_xstats_get(const struct rte_rawdev *dev,
1213 const unsigned int ids[],
1217 struct ntb_hw *hw = dev->dev_private;
1218 uint32_t i, j, off, xstats_num;
1220 /* Calculate total stats of all queues. */
1221 for (i = 0; i < NTB_XSTATS_NUM; i++) {
1222 hw->ntb_xstats[i] = 0;
1223 for (j = 0; j < hw->queue_pairs; j++) {
1224 off = NTB_XSTATS_NUM * (j + 1) + i;
1225 hw->ntb_xstats[i] +=
1226 ntb_stats_update(hw->ntb_xstats_off[off],
1227 hw->ntb_xstats[off]);
1231 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1232 for (i = 0; i < n && ids[i] < xstats_num; i++) {
1233 if (ids[i] < NTB_XSTATS_NUM)
1234 values[i] = hw->ntb_xstats[ids[i]];
1237 ntb_stats_update(hw->ntb_xstats_off[ids[i]],
1238 hw->ntb_xstats[ids[i]]);
1245 ntb_xstats_get_names(const struct rte_rawdev *dev,
1246 struct rte_rawdev_xstats_name *xstats_names,
1249 struct ntb_hw *hw = dev->dev_private;
1250 uint32_t xstats_num, i, j, off;
1252 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1253 if (xstats_names == NULL || size < xstats_num)
1256 /* Total stats names */
1257 memcpy(xstats_names, ntb_xstats_names, sizeof(ntb_xstats_names));
1259 /* Queue stats names */
1260 for (i = 0; i < hw->queue_pairs; i++) {
1261 for (j = 0; j < NTB_XSTATS_NUM; j++) {
1262 off = j + (i + 1) * NTB_XSTATS_NUM;
1263 snprintf(xstats_names[off].name,
1264 sizeof(xstats_names[0].name),
1265 "%s_q%u", ntb_xstats_names[j].name, i);
1273 ntb_xstats_get_by_name(const struct rte_rawdev *dev,
1274 const char *name, unsigned int *id)
1276 struct rte_rawdev_xstats_name *xstats_names;
1277 struct ntb_hw *hw = dev->dev_private;
1278 uint32_t xstats_num, i, j, off;
1283 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1284 xstats_names = rte_zmalloc("ntb_stats_name",
1285 sizeof(struct rte_rawdev_xstats_name) *
1287 ntb_xstats_get_names(dev, xstats_names, xstats_num);
1289 /* Calculate total stats of all queues. */
1290 for (i = 0; i < NTB_XSTATS_NUM; i++) {
1291 for (j = 0; j < hw->queue_pairs; j++) {
1292 off = NTB_XSTATS_NUM * (j + 1) + i;
1293 hw->ntb_xstats[i] +=
1294 ntb_stats_update(hw->ntb_xstats_off[off],
1295 hw->ntb_xstats[off]);
1299 for (i = 0; i < xstats_num; i++) {
1300 if (!strncmp(name, xstats_names[i].name,
1301 RTE_RAW_DEV_XSTATS_NAME_SIZE)) {
1303 rte_free(xstats_names);
1304 if (i < NTB_XSTATS_NUM)
1305 return hw->ntb_xstats[i];
1307 return ntb_stats_update(hw->ntb_xstats_off[i],
1312 NTB_LOG(ERR, "Cannot find the xstats name.");
1318 ntb_xstats_reset(struct rte_rawdev *dev,
1319 const uint32_t ids[],
1322 struct ntb_hw *hw = dev->dev_private;
1323 uint32_t i, j, off, xstats_num;
1325 xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
1326 for (i = 0; i < nb_ids && ids[i] < xstats_num; i++) {
1327 if (ids[i] < NTB_XSTATS_NUM) {
1328 for (j = 0; j < hw->queue_pairs; j++) {
1329 off = NTB_XSTATS_NUM * (j + 1) + ids[i];
1330 hw->ntb_xstats_off[off] = hw->ntb_xstats[off];
1333 hw->ntb_xstats_off[ids[i]] = hw->ntb_xstats[ids[i]];
1340 static const struct rte_rawdev_ops ntb_ops = {
1341 .dev_info_get = ntb_dev_info_get,
1342 .dev_configure = ntb_dev_configure,
1343 .dev_start = ntb_dev_start,
1344 .dev_stop = ntb_dev_stop,
1345 .dev_close = ntb_dev_close,
1346 .dev_reset = ntb_dev_reset,
1348 .queue_def_conf = ntb_queue_conf_get,
1349 .queue_setup = ntb_queue_setup,
1350 .queue_release = ntb_queue_release,
1351 .queue_count = ntb_queue_count,
1353 .enqueue_bufs = ntb_enqueue_bufs,
1354 .dequeue_bufs = ntb_dequeue_bufs,
1356 .attr_get = ntb_attr_get,
1357 .attr_set = ntb_attr_set,
1359 .xstats_get = ntb_xstats_get,
1360 .xstats_get_names = ntb_xstats_get_names,
1361 .xstats_get_by_name = ntb_xstats_get_by_name,
1362 .xstats_reset = ntb_xstats_reset,
1366 ntb_init_hw(struct rte_rawdev *dev, struct rte_pci_device *pci_dev)
1368 struct ntb_hw *hw = dev->dev_private;
1369 struct rte_intr_handle *intr_handle;
1372 hw->pci_dev = pci_dev;
1373 hw->peer_dev_up = 0;
1374 hw->link_status = NTB_LINK_DOWN;
1375 hw->link_speed = NTB_SPEED_NONE;
1376 hw->link_width = NTB_WIDTH_NONE;
1378 switch (pci_dev->id.device_id) {
1379 case NTB_INTEL_DEV_ID_B2B_SKX:
1380 case NTB_INTEL_DEV_ID_B2B_ICX:
1381 hw->ntb_ops = &intel_ntb_ops;
1384 NTB_LOG(ERR, "Not supported device.");
1388 if (hw->ntb_ops->ntb_dev_init == NULL)
1390 ret = (*hw->ntb_ops->ntb_dev_init)(dev);
1392 NTB_LOG(ERR, "Unable to init ntb dev.");
1396 if (hw->ntb_ops->set_link == NULL)
1398 ret = (*hw->ntb_ops->set_link)(dev, 1);
1402 /* Init doorbell. */
1403 hw->db_valid_mask = RTE_LEN2MASK(hw->db_cnt, uint64_t);
1405 intr_handle = &pci_dev->intr_handle;
1406 /* Register callback func to eal lib */
1407 rte_intr_callback_register(intr_handle,
1408 ntb_dev_intr_handler, dev);
1410 ret = rte_intr_efd_enable(intr_handle, hw->db_cnt);
1414 /* To clarify, the interrupt for each doorbell is already mapped
1415 * by default for intel gen3. They are mapped to msix vec 1-32,
1416 * and hardware intr is mapped to 0. Map all to 0 for uio.
1418 if (!rte_intr_cap_multiple(intr_handle)) {
1419 for (i = 0; i < hw->db_cnt; i++) {
1420 if (hw->ntb_ops->vector_bind == NULL)
1422 ret = (*hw->ntb_ops->vector_bind)(dev, i, 0);
1428 if (hw->ntb_ops->db_set_mask == NULL ||
1429 hw->ntb_ops->peer_db_set == NULL) {
1430 NTB_LOG(ERR, "Doorbell is not supported.");
1434 ret = (*hw->ntb_ops->db_set_mask)(dev, hw->db_mask);
1436 NTB_LOG(ERR, "Unable to enable intr for all dbs.");
1440 /* enable uio intr after callback register */
1441 rte_intr_enable(intr_handle);
1447 ntb_create(struct rte_pci_device *pci_dev, int socket_id)
1449 char name[RTE_RAWDEV_NAME_MAX_LEN];
1450 struct rte_rawdev *rawdev = NULL;
1453 if (pci_dev == NULL) {
1454 NTB_LOG(ERR, "Invalid pci_dev.");
1458 memset(name, 0, sizeof(name));
1459 snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
1460 pci_dev->addr.bus, pci_dev->addr.devid,
1461 pci_dev->addr.function);
1463 NTB_LOG(INFO, "Init %s on NUMA node %d", name, socket_id);
1465 /* Allocate device structure. */
1466 rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct ntb_hw),
1468 if (rawdev == NULL) {
1469 NTB_LOG(ERR, "Unable to allocate rawdev.");
1473 rawdev->dev_ops = &ntb_ops;
1474 rawdev->device = &pci_dev->device;
1475 rawdev->driver_name = pci_dev->driver->driver.name;
1477 ret = ntb_init_hw(rawdev, pci_dev);
1479 NTB_LOG(ERR, "Unable to init ntb hw.");
1487 rte_rawdev_pmd_release(rawdev);
1493 ntb_destroy(struct rte_pci_device *pci_dev)
1495 char name[RTE_RAWDEV_NAME_MAX_LEN];
1496 struct rte_rawdev *rawdev;
1499 if (pci_dev == NULL) {
1500 NTB_LOG(ERR, "Invalid pci_dev.");
1505 memset(name, 0, sizeof(name));
1506 snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
1507 pci_dev->addr.bus, pci_dev->addr.devid,
1508 pci_dev->addr.function);
1510 NTB_LOG(INFO, "Closing %s on NUMA node %d", name, rte_socket_id());
1512 rawdev = rte_rawdev_pmd_get_named_dev(name);
1513 if (rawdev == NULL) {
1514 NTB_LOG(ERR, "Invalid device name (%s)", name);
1519 ret = rte_rawdev_pmd_release(rawdev);
1521 NTB_LOG(ERR, "Failed to destroy ntb rawdev.");
1527 ntb_probe(struct rte_pci_driver *pci_drv __rte_unused,
1528 struct rte_pci_device *pci_dev)
1530 return ntb_create(pci_dev, rte_socket_id());
1534 ntb_remove(struct rte_pci_device *pci_dev)
1536 return ntb_destroy(pci_dev);
1540 static struct rte_pci_driver rte_ntb_pmd = {
1541 .id_table = pci_id_ntb_map,
1542 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_WC_ACTIVATE,
1544 .remove = ntb_remove,
1547 RTE_PMD_REGISTER_PCI(raw_ntb, rte_ntb_pmd);
1548 RTE_PMD_REGISTER_PCI_TABLE(raw_ntb, pci_id_ntb_map);
1549 RTE_PMD_REGISTER_KMOD_DEP(raw_ntb, "* igb_uio | uio_pci_generic | vfio-pci");
1550 RTE_LOG_REGISTER_DEFAULT(ntb_logtype, INFO);