1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Microsoft Corporation
3 * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
15 #include <rte_ethdev.h>
16 #include <rte_memcpy.h>
17 #include <rte_string_fns.h>
18 #include <rte_memzone.h>
19 #include <rte_malloc.h>
20 #include <rte_atomic.h>
21 #include <rte_bitmap.h>
22 #include <rte_branch_prediction.h>
23 #include <rte_ether.h>
24 #include <rte_common.h>
25 #include <rte_errno.h>
26 #include <rte_memory.h>
30 #include <rte_bus_vmbus.h>
31 #include <rte_spinlock.h>
39 #define HN_NVS_SEND_MSG_SIZE \
40 (sizeof(struct vmbus_chanpkt_hdr) + sizeof(struct hn_nvs_rndis))
42 #define HN_TXD_CACHE_SIZE 32 /* per cpu tx_descriptor pool cache */
43 #define HN_TXCOPY_THRESHOLD 512
45 #define HN_RXCOPY_THRESHOLD 256
46 #define HN_RXQ_EVENT_DEFAULT 2048
55 #define HN_RXINFO_VLAN 0x0001
56 #define HN_RXINFO_CSUM 0x0002
57 #define HN_RXINFO_HASHINF 0x0004
58 #define HN_RXINFO_HASHVAL 0x0008
59 #define HN_RXINFO_ALL \
65 #define HN_NDIS_VLAN_INFO_INVALID 0xffffffff
66 #define HN_NDIS_RXCSUM_INFO_INVALID 0
67 #define HN_NDIS_HASH_INFO_INVALID 0
70 * Per-transmit book keeping.
71 * A slot in transmit ring (chim_index) is reserved for each transmit.
73 * There are two types of transmit:
74 * - buffered transmit where chimney buffer is used and RNDIS header
75 * is in the buffer. mbuf == NULL for this case.
77 * - direct transmit where RNDIS header is in the in rndis_pkt
78 * mbuf is freed after transmit.
80 * Descriptors come from per-port pool which is used
81 * to limit number of outstanding requests per device.
92 struct rndis_packet_msg *rndis_pkt;
95 #define HN_RNDIS_PKT_LEN \
96 (sizeof(struct rndis_packet_msg) + \
97 RNDIS_PKTINFO_SIZE(NDIS_HASH_VALUE_SIZE) + \
98 RNDIS_PKTINFO_SIZE(NDIS_VLAN_INFO_SIZE) + \
99 RNDIS_PKTINFO_SIZE(NDIS_LSO2_INFO_SIZE) + \
100 RNDIS_PKTINFO_SIZE(NDIS_TXCSUM_INFO_SIZE))
102 #define HN_RNDIS_PKT_ALIGNED RTE_ALIGN(HN_RNDIS_PKT_LEN, RTE_CACHE_LINE_SIZE)
104 /* Minimum space required for a packet */
105 #define HN_PKTSIZE_MIN(align) \
106 RTE_ALIGN(RTE_ETHER_MIN_LEN + HN_RNDIS_PKT_LEN, align)
108 #define DEFAULT_TX_FREE_THRESH 32
111 hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)
113 uint32_t s = m->pkt_len;
114 const struct rte_ether_addr *ea;
117 stats->size_bins[1]++;
118 } else if (s > 64 && s < 1024) {
121 /* count zeros, and offset into correct bin */
122 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
123 stats->size_bins[bin]++;
126 stats->size_bins[0]++;
128 stats->size_bins[6]++;
130 stats->size_bins[7]++;
133 ea = rte_pktmbuf_mtod(m, const struct rte_ether_addr *);
134 if (rte_is_multicast_ether_addr(ea)) {
135 if (rte_is_broadcast_ether_addr(ea))
142 static inline unsigned int hn_rndis_pktlen(const struct rndis_packet_msg *pkt)
144 return pkt->pktinfooffset + pkt->pktinfolen;
147 static inline uint32_t
148 hn_rndis_pktmsg_offset(uint32_t ofs)
150 return ofs - offsetof(struct rndis_packet_msg, dataoffset);
153 static void hn_txd_init(struct rte_mempool *mp __rte_unused,
154 void *opaque, void *obj, unsigned int idx)
156 struct hn_tx_queue *txq = opaque;
157 struct hn_txdesc *txd = obj;
159 memset(txd, 0, sizeof(*txd));
161 txd->queue_id = txq->queue_id;
162 txd->chim_index = NVS_CHIM_IDX_INVALID;
163 txd->rndis_pkt = (struct rndis_packet_msg *)(char *)txq->tx_rndis
164 + idx * HN_RNDIS_PKT_ALIGNED;
168 hn_chim_init(struct rte_eth_dev *dev)
170 struct hn_data *hv = dev->data->dev_private;
171 uint32_t i, chim_bmp_size;
173 rte_spinlock_init(&hv->chim_lock);
174 chim_bmp_size = rte_bitmap_get_memory_footprint(hv->chim_cnt);
175 hv->chim_bmem = rte_zmalloc("hn_chim_bitmap", chim_bmp_size,
176 RTE_CACHE_LINE_SIZE);
177 if (hv->chim_bmem == NULL) {
178 PMD_INIT_LOG(ERR, "failed to allocate bitmap size %u",
183 hv->chim_bmap = rte_bitmap_init(hv->chim_cnt,
184 hv->chim_bmem, chim_bmp_size);
185 if (hv->chim_bmap == NULL) {
186 PMD_INIT_LOG(ERR, "failed to init chim bitmap");
190 for (i = 0; i < hv->chim_cnt; i++)
191 rte_bitmap_set(hv->chim_bmap, i);
197 hn_chim_uninit(struct rte_eth_dev *dev)
199 struct hn_data *hv = dev->data->dev_private;
201 rte_bitmap_free(hv->chim_bmap);
202 rte_free(hv->chim_bmem);
203 hv->chim_bmem = NULL;
206 static uint32_t hn_chim_alloc(struct hn_data *hv)
208 uint32_t index = NVS_CHIM_IDX_INVALID;
211 rte_spinlock_lock(&hv->chim_lock);
212 if (rte_bitmap_scan(hv->chim_bmap, &index, &slab))
213 rte_bitmap_clear(hv->chim_bmap, index);
214 rte_spinlock_unlock(&hv->chim_lock);
219 static void hn_chim_free(struct hn_data *hv, uint32_t chim_idx)
221 if (chim_idx >= hv->chim_cnt) {
222 PMD_DRV_LOG(ERR, "Invalid chimney index %u", chim_idx);
224 rte_spinlock_lock(&hv->chim_lock);
225 rte_bitmap_set(hv->chim_bmap, chim_idx);
226 rte_spinlock_unlock(&hv->chim_lock);
230 static void hn_reset_txagg(struct hn_tx_queue *txq)
232 txq->agg_szleft = txq->agg_szmax;
233 txq->agg_pktleft = txq->agg_pktmax;
235 txq->agg_prevpkt = NULL;
239 hn_dev_tx_queue_setup(struct rte_eth_dev *dev,
240 uint16_t queue_idx, uint16_t nb_desc,
241 unsigned int socket_id,
242 const struct rte_eth_txconf *tx_conf)
245 struct hn_data *hv = dev->data->dev_private;
246 struct hn_tx_queue *txq;
247 char name[RTE_MEMPOOL_NAMESIZE];
248 uint32_t tx_free_thresh;
251 PMD_INIT_FUNC_TRACE();
253 txq = rte_zmalloc_socket("HN_TXQ", sizeof(*txq), RTE_CACHE_LINE_SIZE,
259 txq->chan = hv->channels[queue_idx];
260 txq->port_id = dev->data->port_id;
261 txq->queue_id = queue_idx;
263 tx_free_thresh = tx_conf->tx_free_thresh;
264 if (tx_free_thresh == 0)
265 tx_free_thresh = RTE_MIN(nb_desc / 4,
266 DEFAULT_TX_FREE_THRESH);
268 if (tx_free_thresh + 3 >= nb_desc) {
270 "tx_free_thresh must be less than the number of TX entries minus 3(%u)."
271 " (tx_free_thresh=%u port=%u queue=%u)\n",
273 tx_free_thresh, dev->data->port_id, queue_idx);
277 txq->free_thresh = tx_free_thresh;
279 snprintf(name, sizeof(name),
280 "hn_txd_%u_%u", dev->data->port_id, queue_idx);
282 PMD_INIT_LOG(DEBUG, "TX descriptor pool %s n=%u size=%zu",
283 name, nb_desc, sizeof(struct hn_txdesc));
285 txq->tx_rndis = rte_calloc("hn_txq_rndis", nb_desc,
286 HN_RNDIS_PKT_ALIGNED, RTE_CACHE_LINE_SIZE);
287 if (txq->tx_rndis == NULL)
290 txq->txdesc_pool = rte_mempool_create(name, nb_desc,
291 sizeof(struct hn_txdesc),
294 dev->device->numa_node, 0);
295 if (txq->txdesc_pool == NULL) {
297 "mempool %s create failed: %d", name, rte_errno);
301 txq->agg_szmax = RTE_MIN(hv->chim_szmax, hv->rndis_agg_size);
302 txq->agg_pktmax = hv->rndis_agg_pkts;
303 txq->agg_align = hv->rndis_agg_align;
307 err = hn_vf_tx_queue_setup(dev, queue_idx, nb_desc,
310 dev->data->tx_queues[queue_idx] = txq;
315 if (txq->txdesc_pool)
316 rte_mempool_free(txq->txdesc_pool);
317 rte_free(txq->tx_rndis);
323 hn_dev_tx_queue_info(struct rte_eth_dev *dev, uint16_t queue_id,
324 struct rte_eth_txq_info *qinfo)
326 struct hn_tx_queue *txq = dev->data->tx_queues[queue_id];
328 qinfo->nb_desc = txq->txdesc_pool->size;
329 qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
332 static struct hn_txdesc *hn_txd_get(struct hn_tx_queue *txq)
334 struct hn_txdesc *txd;
336 if (rte_mempool_get(txq->txdesc_pool, (void **)&txd)) {
337 ++txq->stats.ring_full;
338 PMD_TX_LOG(DEBUG, "tx pool exhausted!");
350 static void hn_txd_put(struct hn_tx_queue *txq, struct hn_txdesc *txd)
352 rte_mempool_put(txq->txdesc_pool, txd);
356 hn_dev_tx_queue_release(void *arg)
358 struct hn_tx_queue *txq = arg;
360 PMD_INIT_FUNC_TRACE();
365 if (txq->txdesc_pool)
366 rte_mempool_free(txq->txdesc_pool);
368 rte_free(txq->tx_rndis);
373 hn_nvs_send_completed(struct rte_eth_dev *dev, uint16_t queue_id,
374 unsigned long xactid, const struct hn_nvs_rndis_ack *ack)
376 struct hn_data *hv = dev->data->dev_private;
377 struct hn_txdesc *txd = (struct hn_txdesc *)xactid;
378 struct hn_tx_queue *txq;
380 /* Control packets are sent with xacid == 0 */
384 txq = dev->data->tx_queues[queue_id];
385 if (likely(ack->status == NVS_STATUS_OK)) {
386 PMD_TX_LOG(DEBUG, "port %u:%u complete tx %u packets %u bytes %u",
387 txq->port_id, txq->queue_id, txd->chim_index,
388 txd->packets, txd->data_size);
389 txq->stats.bytes += txd->data_size;
390 txq->stats.packets += txd->packets;
392 PMD_TX_LOG(NOTICE, "port %u:%u complete tx %u failed status %u",
393 txq->port_id, txq->queue_id, txd->chim_index, ack->status);
397 if (txd->chim_index != NVS_CHIM_IDX_INVALID)
398 hn_chim_free(hv, txd->chim_index);
400 rte_pktmbuf_free(txd->m);
401 hn_txd_put(txq, txd);
404 /* Handle transmit completion events */
406 hn_nvs_handle_comp(struct rte_eth_dev *dev, uint16_t queue_id,
407 const struct vmbus_chanpkt_hdr *pkt,
410 const struct hn_nvs_hdr *hdr = data;
413 case NVS_TYPE_RNDIS_ACK:
414 hn_nvs_send_completed(dev, queue_id, pkt->xactid, data);
419 "unexpected send completion type %u",
424 /* Parse per-packet info (meta data) */
426 hn_rndis_rxinfo(const void *info_data, unsigned int info_dlen,
427 struct hn_rxinfo *info)
429 const struct rndis_pktinfo *pi = info_data;
432 while (info_dlen != 0) {
436 if (unlikely(info_dlen < sizeof(*pi)))
439 if (unlikely(info_dlen < pi->size))
441 info_dlen -= pi->size;
443 if (unlikely(pi->size & RNDIS_PKTINFO_SIZE_ALIGNMASK))
445 if (unlikely(pi->size < pi->offset))
448 dlen = pi->size - pi->offset;
452 case NDIS_PKTINFO_TYPE_VLAN:
453 if (unlikely(dlen < NDIS_VLAN_INFO_SIZE))
455 info->vlan_info = *((const uint32_t *)data);
456 mask |= HN_RXINFO_VLAN;
459 case NDIS_PKTINFO_TYPE_CSUM:
460 if (unlikely(dlen < NDIS_RXCSUM_INFO_SIZE))
462 info->csum_info = *((const uint32_t *)data);
463 mask |= HN_RXINFO_CSUM;
466 case NDIS_PKTINFO_TYPE_HASHVAL:
467 if (unlikely(dlen < NDIS_HASH_VALUE_SIZE))
469 info->hash_value = *((const uint32_t *)data);
470 mask |= HN_RXINFO_HASHVAL;
473 case NDIS_PKTINFO_TYPE_HASHINF:
474 if (unlikely(dlen < NDIS_HASH_INFO_SIZE))
476 info->hash_info = *((const uint32_t *)data);
477 mask |= HN_RXINFO_HASHINF;
484 if (mask == HN_RXINFO_ALL)
485 break; /* All found; done */
487 pi = (const struct rndis_pktinfo *)
488 ((const uint8_t *)pi + pi->size);
493 * - If there is no hash value, invalidate the hash info.
495 if (!(mask & HN_RXINFO_HASHVAL))
496 info->hash_info = HN_NDIS_HASH_INFO_INVALID;
501 * Ack the consumed RXBUF associated w/ this channel packet,
502 * so that this RXBUF can be recycled by the hypervisor.
504 static void hn_rx_buf_release(struct hn_rx_bufinfo *rxb)
506 struct rte_mbuf_ext_shared_info *shinfo = &rxb->shinfo;
507 struct hn_data *hv = rxb->hv;
509 if (rte_mbuf_ext_refcnt_update(shinfo, -1) == 0) {
510 hn_nvs_ack_rxbuf(rxb->chan, rxb->xactid);
511 --hv->rxbuf_outstanding;
515 static void hn_rx_buf_free_cb(void *buf __rte_unused, void *opaque)
517 hn_rx_buf_release(opaque);
520 static struct hn_rx_bufinfo *hn_rx_buf_init(const struct hn_rx_queue *rxq,
521 const struct vmbus_chanpkt_rxbuf *pkt)
523 struct hn_rx_bufinfo *rxb;
525 rxb = rxq->hv->rxbuf_info + pkt->hdr.xactid;
526 rxb->chan = rxq->chan;
527 rxb->xactid = pkt->hdr.xactid;
530 rxb->shinfo.free_cb = hn_rx_buf_free_cb;
531 rxb->shinfo.fcb_opaque = rxb;
532 rte_mbuf_ext_refcnt_set(&rxb->shinfo, 1);
536 static void hn_rxpkt(struct hn_rx_queue *rxq, struct hn_rx_bufinfo *rxb,
537 uint8_t *data, unsigned int headroom, unsigned int dlen,
538 const struct hn_rxinfo *info)
540 struct hn_data *hv = rxq->hv;
543 m = rte_pktmbuf_alloc(rxq->mb_pool);
545 struct rte_eth_dev *dev =
546 &rte_eth_devices[rxq->port_id];
548 dev->data->rx_mbuf_alloc_failed++;
553 * For large packets, avoid copy if possible but need to keep
554 * some space available in receive area for later packets.
556 if (dlen >= HN_RXCOPY_THRESHOLD &&
557 hv->rxbuf_outstanding < hv->rxbuf_section_cnt / 2) {
558 struct rte_mbuf_ext_shared_info *shinfo;
563 * Build an external mbuf that points to recveive area.
564 * Use refcount to handle multiple packets in same
565 * receive buffer section.
567 rxbuf = hv->rxbuf_res->addr;
568 iova = rte_mem_virt2iova(rxbuf) + RTE_PTR_DIFF(data, rxbuf);
569 shinfo = &rxb->shinfo;
571 if (rte_mbuf_ext_refcnt_update(shinfo, 1) == 1)
572 ++hv->rxbuf_outstanding;
574 rte_pktmbuf_attach_extbuf(m, data, iova,
575 dlen + headroom, shinfo);
576 m->data_off = headroom;
578 /* Mbuf's in pool must be large enough to hold small packets */
579 if (unlikely(rte_pktmbuf_tailroom(m) < dlen)) {
580 rte_pktmbuf_free_seg(m);
584 rte_memcpy(rte_pktmbuf_mtod(m, void *),
585 data + headroom, dlen);
588 m->port = rxq->port_id;
591 m->packet_type = rte_net_get_ptype(m, NULL,
596 if (info->vlan_info != HN_NDIS_VLAN_INFO_INVALID) {
597 m->vlan_tci = info->vlan_info;
598 m->ol_flags |= PKT_RX_VLAN_STRIPPED | PKT_RX_VLAN;
600 /* NDIS always strips tag, put it back if necessary */
601 if (!hv->vlan_strip && rte_vlan_insert(&m)) {
602 PMD_DRV_LOG(DEBUG, "vlan insert failed");
609 if (info->csum_info != HN_NDIS_RXCSUM_INFO_INVALID) {
610 if (info->csum_info & NDIS_RXCSUM_INFO_IPCS_OK)
611 m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
613 if (info->csum_info & (NDIS_RXCSUM_INFO_UDPCS_OK
614 | NDIS_RXCSUM_INFO_TCPCS_OK))
615 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
616 else if (info->csum_info & (NDIS_RXCSUM_INFO_TCPCS_FAILED
617 | NDIS_RXCSUM_INFO_UDPCS_FAILED))
618 m->ol_flags |= PKT_RX_L4_CKSUM_BAD;
621 if (info->hash_info != HN_NDIS_HASH_INFO_INVALID) {
622 m->ol_flags |= PKT_RX_RSS_HASH;
623 m->hash.rss = info->hash_value;
627 "port %u:%u RX id %"PRIu64" size %u type %#x ol_flags %#"PRIx64,
628 rxq->port_id, rxq->queue_id, rxb->xactid,
629 m->pkt_len, m->packet_type, m->ol_flags);
631 ++rxq->stats.packets;
632 rxq->stats.bytes += m->pkt_len;
633 hn_update_packet_stats(&rxq->stats, m);
635 if (unlikely(rte_ring_sp_enqueue(rxq->rx_ring, m) != 0)) {
636 ++rxq->stats.ring_full;
641 static void hn_rndis_rx_data(struct hn_rx_queue *rxq,
642 struct hn_rx_bufinfo *rxb,
643 void *data, uint32_t dlen)
645 unsigned int data_off, data_len, pktinfo_off, pktinfo_len;
646 const struct rndis_packet_msg *pkt = data;
647 struct hn_rxinfo info = {
648 .vlan_info = HN_NDIS_VLAN_INFO_INVALID,
649 .csum_info = HN_NDIS_RXCSUM_INFO_INVALID,
650 .hash_info = HN_NDIS_HASH_INFO_INVALID,
656 if (unlikely(dlen < sizeof(*pkt)))
659 if (unlikely(dlen < pkt->len))
660 goto error; /* truncated RNDIS from host */
662 if (unlikely(pkt->len < pkt->datalen
663 + pkt->oobdatalen + pkt->pktinfolen))
666 if (unlikely(pkt->datalen == 0))
670 if (unlikely(pkt->dataoffset < RNDIS_PACKET_MSG_OFFSET_MIN))
673 if (likely(pkt->pktinfooffset > 0) &&
674 unlikely(pkt->pktinfooffset < RNDIS_PACKET_MSG_OFFSET_MIN ||
675 (pkt->pktinfooffset & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK)))
678 data_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
679 data_len = pkt->datalen;
680 pktinfo_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->pktinfooffset);
681 pktinfo_len = pkt->pktinfolen;
683 if (likely(pktinfo_len > 0)) {
684 err = hn_rndis_rxinfo((const uint8_t *)pkt + pktinfo_off,
690 if (unlikely(data_off + data_len > pkt->len))
693 if (unlikely(data_len < RTE_ETHER_HDR_LEN))
696 hn_rxpkt(rxq, rxb, data, data_off, data_len, &info);
703 hn_rndis_receive(struct rte_eth_dev *dev, struct hn_rx_queue *rxq,
704 struct hn_rx_bufinfo *rxb, void *buf, uint32_t len)
706 const struct rndis_msghdr *hdr = buf;
709 case RNDIS_PACKET_MSG:
710 if (dev->data->dev_started)
711 hn_rndis_rx_data(rxq, rxb, buf, len);
714 case RNDIS_INDICATE_STATUS_MSG:
715 hn_rndis_link_status(dev, buf);
718 case RNDIS_INITIALIZE_CMPLT:
719 case RNDIS_QUERY_CMPLT:
720 case RNDIS_SET_CMPLT:
721 hn_rndis_receive_response(rxq->hv, buf, len);
726 "unexpected RNDIS message (type %#x len %u)",
733 hn_nvs_handle_rxbuf(struct rte_eth_dev *dev,
735 struct hn_rx_queue *rxq,
736 const struct vmbus_chanpkt_hdr *hdr,
739 const struct vmbus_chanpkt_rxbuf *pkt;
740 const struct hn_nvs_hdr *nvs_hdr = buf;
741 uint32_t rxbuf_sz = hv->rxbuf_res->len;
742 char *rxbuf = hv->rxbuf_res->addr;
743 unsigned int i, hlen, count;
744 struct hn_rx_bufinfo *rxb;
746 /* At minimum we need type header */
747 if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*nvs_hdr))) {
748 PMD_RX_LOG(ERR, "invalid receive nvs RNDIS");
752 /* Make sure that this is a RNDIS message. */
753 if (unlikely(nvs_hdr->type != NVS_TYPE_RNDIS)) {
754 PMD_RX_LOG(ERR, "nvs type %u, not RNDIS",
759 hlen = vmbus_chanpkt_getlen(hdr->hlen);
760 if (unlikely(hlen < sizeof(*pkt))) {
761 PMD_RX_LOG(ERR, "invalid rxbuf chanpkt");
765 pkt = container_of(hdr, const struct vmbus_chanpkt_rxbuf, hdr);
766 if (unlikely(pkt->rxbuf_id != NVS_RXBUF_SIG)) {
767 PMD_RX_LOG(ERR, "invalid rxbuf_id 0x%08x",
772 count = pkt->rxbuf_cnt;
773 if (unlikely(hlen < offsetof(struct vmbus_chanpkt_rxbuf,
775 PMD_RX_LOG(ERR, "invalid rxbuf_cnt %u", count);
779 if (pkt->hdr.xactid > hv->rxbuf_section_cnt) {
780 PMD_RX_LOG(ERR, "invalid rxbuf section id %" PRIx64,
785 /* Setup receive buffer info to allow for callback */
786 rxb = hn_rx_buf_init(rxq, pkt);
788 /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
789 for (i = 0; i < count; ++i) {
790 unsigned int ofs, len;
792 ofs = pkt->rxbuf[i].ofs;
793 len = pkt->rxbuf[i].len;
795 if (unlikely(ofs + len > rxbuf_sz)) {
797 "%uth RNDIS msg overflow ofs %u, len %u",
802 if (unlikely(len == 0)) {
803 PMD_RX_LOG(ERR, "%uth RNDIS msg len %u", i, len);
807 hn_rndis_receive(dev, rxq, rxb,
811 /* Send ACK now if external mbuf not used */
812 hn_rx_buf_release(rxb);
816 * Called when NVS inband events are received.
817 * Send up a two part message with port_id and the NVS message
818 * to the pipe to the netvsc-vf-event control thread.
820 static void hn_nvs_handle_notify(struct rte_eth_dev *dev,
821 const struct vmbus_chanpkt_hdr *pkt,
824 const struct hn_nvs_hdr *hdr = data;
827 case NVS_TYPE_TXTBL_NOTE:
828 /* Transmit indirection table has locking problems
829 * in DPDK and therefore not implemented
831 PMD_DRV_LOG(DEBUG, "host notify of transmit indirection table");
834 case NVS_TYPE_VFASSOC_NOTE:
835 hn_nvs_handle_vfassoc(dev, pkt, data);
840 "got notify, nvs type %u", hdr->type);
844 struct hn_rx_queue *hn_rx_queue_alloc(struct hn_data *hv,
846 unsigned int socket_id)
848 struct hn_rx_queue *rxq;
850 rxq = rte_zmalloc_socket("HN_RXQ", sizeof(*rxq),
851 RTE_CACHE_LINE_SIZE, socket_id);
856 rxq->chan = hv->channels[queue_id];
857 rte_spinlock_init(&rxq->ring_lock);
858 rxq->port_id = hv->port_id;
859 rxq->queue_id = queue_id;
860 rxq->event_sz = HN_RXQ_EVENT_DEFAULT;
861 rxq->event_buf = rte_malloc_socket("HN_EVENTS", HN_RXQ_EVENT_DEFAULT,
862 RTE_CACHE_LINE_SIZE, socket_id);
863 if (!rxq->event_buf) {
872 hn_dev_rx_queue_info(struct rte_eth_dev *dev, uint16_t queue_id,
873 struct rte_eth_rxq_info *qinfo)
875 struct hn_rx_queue *rxq = dev->data->rx_queues[queue_id];
877 qinfo->mp = rxq->mb_pool;
878 qinfo->nb_desc = rxq->rx_ring->size;
879 qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
883 hn_dev_rx_queue_setup(struct rte_eth_dev *dev,
884 uint16_t queue_idx, uint16_t nb_desc,
885 unsigned int socket_id,
886 const struct rte_eth_rxconf *rx_conf,
887 struct rte_mempool *mp)
889 struct hn_data *hv = dev->data->dev_private;
890 char ring_name[RTE_RING_NAMESIZE];
891 struct hn_rx_queue *rxq;
895 PMD_INIT_FUNC_TRACE();
897 if (queue_idx == 0) {
900 rxq = hn_rx_queue_alloc(hv, queue_idx, socket_id);
906 count = rte_mempool_avail_count(mp) / dev->data->nb_rx_queues;
907 if (nb_desc == 0 || nb_desc > count)
911 * Staging ring from receive event logic to rx_pkts.
912 * rx_pkts assumes caller is handling multi-thread issue.
913 * event logic has locking.
915 snprintf(ring_name, sizeof(ring_name),
916 "hn_rx_%u_%u", dev->data->port_id, queue_idx);
917 rxq->rx_ring = rte_ring_create(ring_name,
918 rte_align32pow2(nb_desc),
923 error = hn_vf_rx_queue_setup(dev, queue_idx, nb_desc,
924 socket_id, rx_conf, mp);
928 dev->data->rx_queues[queue_idx] = rxq;
932 rte_ring_free(rxq->rx_ring);
933 rte_free(rxq->event_buf);
939 hn_rx_queue_free(struct hn_rx_queue *rxq, bool keep_primary)
945 rte_ring_free(rxq->rx_ring);
949 hn_vf_rx_queue_release(rxq->hv, rxq->queue_id);
951 /* Keep primary queue to allow for control operations */
952 if (keep_primary && rxq == rxq->hv->primary)
955 rte_free(rxq->event_buf);
960 hn_dev_rx_queue_release(void *arg)
962 struct hn_rx_queue *rxq = arg;
964 PMD_INIT_FUNC_TRACE();
966 hn_rx_queue_free(rxq, true);
970 hn_dev_tx_done_cleanup(void *arg, uint32_t free_cnt)
972 struct hn_tx_queue *txq = arg;
974 return hn_process_events(txq->hv, txq->queue_id, free_cnt);
978 * Process pending events on the channel.
979 * Called from both Rx queue poll and Tx cleanup
981 uint32_t hn_process_events(struct hn_data *hv, uint16_t queue_id,
984 struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
985 struct hn_rx_queue *rxq;
986 uint32_t bytes_read = 0;
987 uint32_t tx_done = 0;
990 rxq = queue_id == 0 ? hv->primary : dev->data->rx_queues[queue_id];
993 * Since channel is shared between Rx and TX queue need to have a lock
994 * since DPDK does not force same CPU to be used for Rx/Tx.
996 if (unlikely(!rte_spinlock_trylock(&rxq->ring_lock)))
1000 const struct vmbus_chanpkt_hdr *pkt;
1001 uint32_t len = rxq->event_sz;
1005 ret = rte_vmbus_chan_recv_raw(rxq->chan, rxq->event_buf, &len);
1007 break; /* ring is empty */
1009 if (unlikely(ret == -ENOBUFS)) {
1010 /* event buffer not large enough to read ring */
1013 "event buffer expansion (need %u)", len);
1014 rxq->event_sz = len + len / 4;
1015 rxq->event_buf = rte_realloc(rxq->event_buf, rxq->event_sz,
1016 RTE_CACHE_LINE_SIZE);
1019 /* out of memory, no more events now */
1024 if (unlikely(ret <= 0)) {
1025 /* This indicates a failure to communicate (or worse) */
1026 rte_exit(EXIT_FAILURE,
1027 "vmbus ring buffer error: %d", ret);
1031 pkt = (const struct vmbus_chanpkt_hdr *)rxq->event_buf;
1032 data = (char *)rxq->event_buf + vmbus_chanpkt_getlen(pkt->hlen);
1034 switch (pkt->type) {
1035 case VMBUS_CHANPKT_TYPE_COMP:
1037 hn_nvs_handle_comp(dev, queue_id, pkt, data);
1040 case VMBUS_CHANPKT_TYPE_RXBUF:
1041 hn_nvs_handle_rxbuf(dev, hv, rxq, pkt, data);
1044 case VMBUS_CHANPKT_TYPE_INBAND:
1045 hn_nvs_handle_notify(dev, pkt, data);
1049 PMD_DRV_LOG(ERR, "unknown chan pkt %u", pkt->type);
1053 if (tx_limit && tx_done >= tx_limit)
1058 rte_vmbus_chan_signal_read(rxq->chan, bytes_read);
1060 rte_spinlock_unlock(&rxq->ring_lock);
1065 static void hn_append_to_chim(struct hn_tx_queue *txq,
1066 struct rndis_packet_msg *pkt,
1067 const struct rte_mbuf *m)
1069 struct hn_txdesc *txd = txq->agg_txd;
1070 uint8_t *buf = (uint8_t *)pkt;
1071 unsigned int data_offs;
1075 data_offs = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
1076 txd->chim_size += pkt->len;
1077 txd->data_size += m->pkt_len;
1079 hn_update_packet_stats(&txq->stats, m);
1081 for (; m; m = m->next) {
1082 uint16_t len = rte_pktmbuf_data_len(m);
1084 rte_memcpy(buf + data_offs,
1085 rte_pktmbuf_mtod(m, const char *), len);
1091 * Send pending aggregated data in chimney buffer (if any).
1092 * Returns error if send was unsuccessful because channel ring buffer
1095 static int hn_flush_txagg(struct hn_tx_queue *txq, bool *need_sig)
1098 struct hn_txdesc *txd = txq->agg_txd;
1099 struct hn_nvs_rndis rndis;
1105 rndis = (struct hn_nvs_rndis) {
1106 .type = NVS_TYPE_RNDIS,
1107 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
1108 .chim_idx = txd->chim_index,
1109 .chim_sz = txd->chim_size,
1112 PMD_TX_LOG(DEBUG, "port %u:%u tx %u size %u",
1113 txq->port_id, txq->queue_id, txd->chim_index, txd->chim_size);
1115 ret = hn_nvs_send(txq->chan, VMBUS_CHANPKT_FLAG_RC,
1116 &rndis, sizeof(rndis), (uintptr_t)txd, need_sig);
1118 if (likely(ret == 0))
1119 hn_reset_txagg(txq);
1121 PMD_TX_LOG(NOTICE, "port %u:%u send failed: %d",
1122 txq->port_id, txq->queue_id, ret);
1128 * Try and find a place in a send chimney buffer to put
1129 * the small packet. If space is available, this routine
1130 * returns a pointer of where to place the data.
1131 * If no space, caller should try direct transmit.
1134 hn_try_txagg(struct hn_data *hv, struct hn_tx_queue *txq,
1135 struct hn_txdesc *txd, uint32_t pktsize)
1137 struct hn_txdesc *agg_txd = txq->agg_txd;
1138 struct rndis_packet_msg *pkt;
1142 unsigned int padding, olen;
1145 * Update the previous RNDIS packet's total length,
1146 * it can be increased due to the mandatory alignment
1147 * padding for this RNDIS packet. And update the
1148 * aggregating txdesc's chimney sending buffer size
1151 * Zero-out the padding, as required by the RNDIS spec.
1153 pkt = txq->agg_prevpkt;
1155 padding = RTE_ALIGN(olen, txq->agg_align) - olen;
1157 agg_txd->chim_size += padding;
1158 pkt->len += padding;
1159 memset((uint8_t *)pkt + olen, 0, padding);
1162 chim = (uint8_t *)pkt + pkt->len;
1163 txq->agg_prevpkt = chim;
1165 txq->agg_szleft -= pktsize;
1166 if (txq->agg_szleft < HN_PKTSIZE_MIN(txq->agg_align)) {
1168 * Probably can't aggregate more packets,
1169 * flush this aggregating txdesc proactively.
1171 txq->agg_pktleft = 0;
1174 hn_txd_put(txq, txd);
1178 txd->chim_index = hn_chim_alloc(hv);
1179 if (txd->chim_index == NVS_CHIM_IDX_INVALID)
1182 chim = (uint8_t *)hv->chim_res->addr
1183 + txd->chim_index * hv->chim_szmax;
1186 txq->agg_pktleft = txq->agg_pktmax - 1;
1187 txq->agg_szleft = txq->agg_szmax - pktsize;
1188 txq->agg_prevpkt = chim;
1193 static inline void *
1194 hn_rndis_pktinfo_append(struct rndis_packet_msg *pkt,
1195 uint32_t pi_dlen, uint32_t pi_type)
1197 const uint32_t pi_size = RNDIS_PKTINFO_SIZE(pi_dlen);
1198 struct rndis_pktinfo *pi;
1201 * Per-packet-info does not move; it only grows.
1204 * pktinfooffset in this phase counts from the beginning
1205 * of rndis_packet_msg.
1207 pi = (struct rndis_pktinfo *)((uint8_t *)pkt + hn_rndis_pktlen(pkt));
1209 pkt->pktinfolen += pi_size;
1213 pi->offset = RNDIS_PKTINFO_OFFSET;
1218 /* Put RNDIS header and packet info on packet */
1219 static void hn_encap(struct rndis_packet_msg *pkt,
1221 const struct rte_mbuf *m)
1223 unsigned int hlen = m->l2_len + m->l3_len;
1227 pkt->type = RNDIS_PACKET_MSG;
1228 pkt->len = m->pkt_len;
1229 pkt->dataoffset = 0;
1230 pkt->datalen = m->pkt_len;
1231 pkt->oobdataoffset = 0;
1232 pkt->oobdatalen = 0;
1233 pkt->oobdataelements = 0;
1234 pkt->pktinfooffset = sizeof(*pkt);
1235 pkt->pktinfolen = 0;
1240 * Set the hash value for this packet, to the queue_id to cause
1241 * TX done event for this packet on the right channel.
1243 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_HASH_VALUE_SIZE,
1244 NDIS_PKTINFO_TYPE_HASHVAL);
1245 *pi_data = queue_id;
1247 if (m->ol_flags & PKT_TX_VLAN_PKT) {
1248 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_VLAN_INFO_SIZE,
1249 NDIS_PKTINFO_TYPE_VLAN);
1250 *pi_data = m->vlan_tci;
1253 if (m->ol_flags & PKT_TX_TCP_SEG) {
1254 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_LSO2_INFO_SIZE,
1255 NDIS_PKTINFO_TYPE_LSO);
1257 if (m->ol_flags & PKT_TX_IPV6) {
1258 *pi_data = NDIS_LSO2_INFO_MAKEIPV6(hlen,
1261 *pi_data = NDIS_LSO2_INFO_MAKEIPV4(hlen,
1264 } else if (m->ol_flags &
1265 (PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM)) {
1266 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_TXCSUM_INFO_SIZE,
1267 NDIS_PKTINFO_TYPE_CSUM);
1270 if (m->ol_flags & PKT_TX_IPV6)
1271 *pi_data |= NDIS_TXCSUM_INFO_IPV6;
1272 if (m->ol_flags & PKT_TX_IPV4) {
1273 *pi_data |= NDIS_TXCSUM_INFO_IPV4;
1275 if (m->ol_flags & PKT_TX_IP_CKSUM)
1276 *pi_data |= NDIS_TXCSUM_INFO_IPCS;
1279 if (m->ol_flags & PKT_TX_TCP_CKSUM)
1280 *pi_data |= NDIS_TXCSUM_INFO_MKTCPCS(hlen);
1281 else if (m->ol_flags & PKT_TX_UDP_CKSUM)
1282 *pi_data |= NDIS_TXCSUM_INFO_MKUDPCS(hlen);
1285 pkt_hlen = pkt->pktinfooffset + pkt->pktinfolen;
1286 /* Fixup RNDIS packet message total length */
1287 pkt->len += pkt_hlen;
1289 /* Convert RNDIS packet message offsets */
1290 pkt->dataoffset = hn_rndis_pktmsg_offset(pkt_hlen);
1291 pkt->pktinfooffset = hn_rndis_pktmsg_offset(pkt->pktinfooffset);
1294 /* How many scatter gather list elements ar needed */
1295 static unsigned int hn_get_slots(const struct rte_mbuf *m)
1297 unsigned int slots = 1; /* for RNDIS header */
1300 unsigned int size = rte_pktmbuf_data_len(m);
1301 unsigned int offs = rte_mbuf_data_iova(m) & PAGE_MASK;
1303 slots += (offs + size + PAGE_SIZE - 1) / PAGE_SIZE;
1310 /* Build scatter gather list from chained mbuf */
1311 static unsigned int hn_fill_sg(struct vmbus_gpa *sg,
1312 const struct rte_mbuf *m)
1314 unsigned int segs = 0;
1317 rte_iova_t addr = rte_mbuf_data_iova(m);
1318 unsigned int page = addr / PAGE_SIZE;
1319 unsigned int offset = addr & PAGE_MASK;
1320 unsigned int len = rte_pktmbuf_data_len(m);
1323 unsigned int bytes = RTE_MIN(len, PAGE_SIZE - offset);
1325 sg[segs].page = page;
1326 sg[segs].ofs = offset;
1327 sg[segs].len = bytes;
1340 /* Transmit directly from mbuf */
1341 static int hn_xmit_sg(struct hn_tx_queue *txq,
1342 const struct hn_txdesc *txd, const struct rte_mbuf *m,
1345 struct vmbus_gpa sg[hn_get_slots(m)];
1346 struct hn_nvs_rndis nvs_rndis = {
1347 .type = NVS_TYPE_RNDIS,
1348 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
1349 .chim_sz = txd->chim_size,
1354 /* attach aggregation data if present */
1355 if (txd->chim_size > 0)
1356 nvs_rndis.chim_idx = txd->chim_index;
1358 nvs_rndis.chim_idx = NVS_CHIM_IDX_INVALID;
1360 hn_rndis_dump(txd->rndis_pkt);
1362 /* pass IOVA of rndis header in first segment */
1363 addr = rte_malloc_virt2iova(txd->rndis_pkt);
1364 if (unlikely(addr == RTE_BAD_IOVA)) {
1365 PMD_DRV_LOG(ERR, "RNDIS transmit can not get iova");
1369 sg[0].page = addr / PAGE_SIZE;
1370 sg[0].ofs = addr & PAGE_MASK;
1371 sg[0].len = RNDIS_PACKET_MSG_OFFSET_ABS(hn_rndis_pktlen(txd->rndis_pkt));
1374 hn_update_packet_stats(&txq->stats, m);
1376 segs += hn_fill_sg(sg + 1, m);
1378 PMD_TX_LOG(DEBUG, "port %u:%u tx %u segs %u size %u",
1379 txq->port_id, txq->queue_id, txd->chim_index,
1380 segs, nvs_rndis.chim_sz);
1382 return hn_nvs_send_sglist(txq->chan, sg, segs,
1383 &nvs_rndis, sizeof(nvs_rndis),
1384 (uintptr_t)txd, need_sig);
1388 hn_xmit_pkts(void *ptxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1390 struct hn_tx_queue *txq = ptxq;
1391 uint16_t queue_id = txq->queue_id;
1392 struct hn_data *hv = txq->hv;
1393 struct rte_eth_dev *vf_dev;
1394 bool need_sig = false;
1395 uint16_t nb_tx, tx_thresh;
1398 if (unlikely(hv->closed))
1402 * Always check for events on the primary channel
1403 * because that is where hotplug notifications occur.
1405 tx_thresh = RTE_MAX(txq->free_thresh, nb_pkts);
1406 if (txq->queue_id == 0 ||
1407 rte_mempool_avail_count(txq->txdesc_pool) < tx_thresh)
1408 hn_process_events(hv, txq->queue_id, 0);
1410 /* Transmit over VF if present and up */
1411 rte_rwlock_read_lock(&hv->vf_lock);
1412 vf_dev = hn_get_vf_dev(hv);
1413 if (vf_dev && vf_dev->data->dev_started) {
1414 void *sub_q = vf_dev->data->tx_queues[queue_id];
1416 nb_tx = (*vf_dev->tx_pkt_burst)(sub_q, tx_pkts, nb_pkts);
1417 rte_rwlock_read_unlock(&hv->vf_lock);
1420 rte_rwlock_read_unlock(&hv->vf_lock);
1422 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1423 struct rte_mbuf *m = tx_pkts[nb_tx];
1424 uint32_t pkt_size = m->pkt_len + HN_RNDIS_PKT_LEN;
1425 struct rndis_packet_msg *pkt;
1426 struct hn_txdesc *txd;
1428 txd = hn_txd_get(txq);
1432 /* For small packets aggregate them in chimney buffer */
1433 if (m->pkt_len < HN_TXCOPY_THRESHOLD && pkt_size <= txq->agg_szmax) {
1434 /* If this packet will not fit, then flush */
1435 if (txq->agg_pktleft == 0 ||
1436 RTE_ALIGN(pkt_size, txq->agg_align) > txq->agg_szleft) {
1437 if (hn_flush_txagg(txq, &need_sig))
1442 pkt = hn_try_txagg(hv, txq, txd, pkt_size);
1446 hn_encap(pkt, queue_id, m);
1447 hn_append_to_chim(txq, pkt, m);
1449 rte_pktmbuf_free(m);
1451 /* if buffer is full, flush */
1452 if (txq->agg_pktleft == 0 &&
1453 hn_flush_txagg(txq, &need_sig))
1456 /* Send any outstanding packets in buffer */
1457 if (txq->agg_txd && hn_flush_txagg(txq, &need_sig))
1460 pkt = txd->rndis_pkt;
1462 txd->data_size = m->pkt_len;
1465 hn_encap(pkt, queue_id, m);
1467 ret = hn_xmit_sg(txq, txd, m, &need_sig);
1468 if (unlikely(ret != 0)) {
1469 PMD_TX_LOG(NOTICE, "sg send failed: %d", ret);
1470 ++txq->stats.errors;
1471 hn_txd_put(txq, txd);
1477 /* If partial buffer left, then try and send it.
1478 * if that fails, then reuse it on next send.
1480 hn_flush_txagg(txq, &need_sig);
1484 rte_vmbus_chan_signal_tx(txq->chan);
1490 hn_recv_vf(uint16_t vf_port, const struct hn_rx_queue *rxq,
1491 struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1495 if (unlikely(nb_pkts == 0))
1498 n = rte_eth_rx_burst(vf_port, rxq->queue_id, rx_pkts, nb_pkts);
1500 /* relabel the received mbufs */
1501 for (i = 0; i < n; i++)
1502 rx_pkts[i]->port = rxq->port_id;
1508 hn_recv_pkts(void *prxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1510 struct hn_rx_queue *rxq = prxq;
1511 struct hn_data *hv = rxq->hv;
1512 struct rte_eth_dev *vf_dev;
1515 if (unlikely(hv->closed))
1518 /* Check for new completions (and hotplug) */
1519 if (likely(rte_ring_count(rxq->rx_ring) < nb_pkts))
1520 hn_process_events(hv, rxq->queue_id, 0);
1522 /* Always check the vmbus path for multicast and new flows */
1523 nb_rcv = rte_ring_sc_dequeue_burst(rxq->rx_ring,
1524 (void **)rx_pkts, nb_pkts, NULL);
1526 /* If VF is available, check that as well */
1527 rte_rwlock_read_lock(&hv->vf_lock);
1528 vf_dev = hn_get_vf_dev(hv);
1529 if (vf_dev && vf_dev->data->dev_started)
1530 nb_rcv += hn_recv_vf(vf_dev->data->port_id, rxq,
1531 rx_pkts + nb_rcv, nb_pkts - nb_rcv);
1533 rte_rwlock_read_unlock(&hv->vf_lock);
1538 hn_dev_free_queues(struct rte_eth_dev *dev)
1542 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1543 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
1545 hn_rx_queue_free(rxq, false);
1546 dev->data->rx_queues[i] = NULL;
1548 dev->data->nb_rx_queues = 0;
1550 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1551 hn_dev_tx_queue_release(dev->data->tx_queues[i]);
1552 dev->data->tx_queues[i] = NULL;
1554 dev->data->nb_tx_queues = 0;