bus/vmbus: avoid signalling host on read
[dpdk.git] / drivers / net / netvsc / hn_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2018 Microsoft Corporation
3  * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
4  * All rights reserved.
5  */
6
7 #include <stdint.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include <strings.h>
13
14 #include <rte_ethdev.h>
15 #include <rte_memcpy.h>
16 #include <rte_string_fns.h>
17 #include <rte_memzone.h>
18 #include <rte_malloc.h>
19 #include <rte_atomic.h>
20 #include <rte_branch_prediction.h>
21 #include <rte_ether.h>
22 #include <rte_common.h>
23 #include <rte_errno.h>
24 #include <rte_memory.h>
25 #include <rte_eal.h>
26 #include <rte_dev.h>
27 #include <rte_bus_vmbus.h>
28 #include <rte_spinlock.h>
29
30 #include "hn_logs.h"
31 #include "hn_var.h"
32 #include "hn_rndis.h"
33 #include "hn_nvs.h"
34 #include "ndis.h"
35
36 #define HN_NVS_SEND_MSG_SIZE \
37         (sizeof(struct vmbus_chanpkt_hdr) + sizeof(struct hn_nvs_rndis))
38
39 #define HN_TXD_CACHE_SIZE       32 /* per cpu tx_descriptor pool cache */
40 #define HN_TXCOPY_THRESHOLD     512
41
42 #define HN_RXCOPY_THRESHOLD     256
43 #define HN_RXQ_EVENT_DEFAULT    2048
44
45 struct hn_rxinfo {
46         uint32_t        vlan_info;
47         uint32_t        csum_info;
48         uint32_t        hash_info;
49         uint32_t        hash_value;
50 };
51
52 #define HN_RXINFO_VLAN                  0x0001
53 #define HN_RXINFO_CSUM                  0x0002
54 #define HN_RXINFO_HASHINF               0x0004
55 #define HN_RXINFO_HASHVAL               0x0008
56 #define HN_RXINFO_ALL                   \
57         (HN_RXINFO_VLAN |               \
58          HN_RXINFO_CSUM |               \
59          HN_RXINFO_HASHINF |            \
60          HN_RXINFO_HASHVAL)
61
62 #define HN_NDIS_VLAN_INFO_INVALID       0xffffffff
63 #define HN_NDIS_RXCSUM_INFO_INVALID     0
64 #define HN_NDIS_HASH_INFO_INVALID       0
65
66 /*
67  * Per-transmit book keeping.
68  * A slot in transmit ring (chim_index) is reserved for each transmit.
69  *
70  * There are two types of transmit:
71  *   - buffered transmit where chimney buffer is used and RNDIS header
72  *     is in the buffer. mbuf == NULL for this case.
73  *
74  *   - direct transmit where RNDIS header is in the in  rndis_pkt
75  *     mbuf is freed after transmit.
76  *
77  * Descriptors come from per-port pool which is used
78  * to limit number of outstanding requests per device.
79  */
80 struct hn_txdesc {
81         struct rte_mbuf *m;
82
83         uint16_t        queue_id;
84         uint16_t        chim_index;
85         uint32_t        chim_size;
86         uint32_t        data_size;
87         uint32_t        packets;
88
89         struct rndis_packet_msg *rndis_pkt;
90 };
91
92 #define HN_RNDIS_PKT_LEN                                \
93         (sizeof(struct rndis_packet_msg) +              \
94          RNDIS_PKTINFO_SIZE(NDIS_HASH_VALUE_SIZE) +     \
95          RNDIS_PKTINFO_SIZE(NDIS_VLAN_INFO_SIZE) +      \
96          RNDIS_PKTINFO_SIZE(NDIS_LSO2_INFO_SIZE) +      \
97          RNDIS_PKTINFO_SIZE(NDIS_TXCSUM_INFO_SIZE))
98
99 /* Minimum space required for a packet */
100 #define HN_PKTSIZE_MIN(align) \
101         RTE_ALIGN(ETHER_MIN_LEN + HN_RNDIS_PKT_LEN, align)
102
103 #define DEFAULT_TX_FREE_THRESH 32U
104
105 static void
106 hn_update_packet_stats(struct hn_stats *stats, const struct rte_mbuf *m)
107 {
108         uint32_t s = m->pkt_len;
109         const struct ether_addr *ea;
110
111         if (s == 64) {
112                 stats->size_bins[1]++;
113         } else if (s > 64 && s < 1024) {
114                 uint32_t bin;
115
116                 /* count zeros, and offset into correct bin */
117                 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
118                 stats->size_bins[bin]++;
119         } else {
120                 if (s < 64)
121                         stats->size_bins[0]++;
122                 else if (s < 1519)
123                         stats->size_bins[6]++;
124                 else if (s >= 1519)
125                         stats->size_bins[7]++;
126         }
127
128         ea = rte_pktmbuf_mtod(m, const struct ether_addr *);
129         if (is_multicast_ether_addr(ea)) {
130                 if (is_broadcast_ether_addr(ea))
131                         stats->broadcast++;
132                 else
133                         stats->multicast++;
134         }
135 }
136
137 static inline unsigned int hn_rndis_pktlen(const struct rndis_packet_msg *pkt)
138 {
139         return pkt->pktinfooffset + pkt->pktinfolen;
140 }
141
142 static inline uint32_t
143 hn_rndis_pktmsg_offset(uint32_t ofs)
144 {
145         return ofs - offsetof(struct rndis_packet_msg, dataoffset);
146 }
147
148 static void hn_txd_init(struct rte_mempool *mp __rte_unused,
149                         void *opaque, void *obj, unsigned int idx)
150 {
151         struct hn_txdesc *txd = obj;
152         struct rte_eth_dev *dev = opaque;
153         struct rndis_packet_msg *pkt;
154
155         memset(txd, 0, sizeof(*txd));
156         txd->chim_index = idx;
157
158         pkt = rte_malloc_socket("RNDIS_TX", HN_RNDIS_PKT_LEN,
159                                 rte_align32pow2(HN_RNDIS_PKT_LEN),
160                                 dev->device->numa_node);
161         if (!pkt)
162                 rte_exit(EXIT_FAILURE, "can not allocate RNDIS header");
163
164         txd->rndis_pkt = pkt;
165 }
166
167 /*
168  * Unlike Linux and FreeBSD, this driver uses a mempool
169  * to limit outstanding transmits and reserve buffers
170  */
171 int
172 hn_tx_pool_init(struct rte_eth_dev *dev)
173 {
174         struct hn_data *hv = dev->data->dev_private;
175         char name[RTE_MEMPOOL_NAMESIZE];
176         struct rte_mempool *mp;
177
178         snprintf(name, sizeof(name),
179                  "hn_txd_%u", dev->data->port_id);
180
181         PMD_INIT_LOG(DEBUG, "create a TX send pool %s n=%u size=%zu socket=%d",
182                      name, hv->chim_cnt, sizeof(struct hn_txdesc),
183                      dev->device->numa_node);
184
185         mp = rte_mempool_create(name, hv->chim_cnt, sizeof(struct hn_txdesc),
186                                 HN_TXD_CACHE_SIZE, 0,
187                                 NULL, NULL,
188                                 hn_txd_init, dev,
189                                 dev->device->numa_node, 0);
190         if (!mp) {
191                 PMD_DRV_LOG(ERR,
192                             "mempool %s create failed: %d", name, rte_errno);
193                 return -rte_errno;
194         }
195
196         hv->tx_pool = mp;
197         return 0;
198 }
199
200 static void hn_reset_txagg(struct hn_tx_queue *txq)
201 {
202         txq->agg_szleft = txq->agg_szmax;
203         txq->agg_pktleft = txq->agg_pktmax;
204         txq->agg_txd = NULL;
205         txq->agg_prevpkt = NULL;
206 }
207
208 int
209 hn_dev_tx_queue_setup(struct rte_eth_dev *dev,
210                       uint16_t queue_idx, uint16_t nb_desc __rte_unused,
211                       unsigned int socket_id,
212                       const struct rte_eth_txconf *tx_conf)
213
214 {
215         struct hn_data *hv = dev->data->dev_private;
216         struct hn_tx_queue *txq;
217         uint32_t tx_free_thresh;
218
219         PMD_INIT_FUNC_TRACE();
220
221         txq = rte_zmalloc_socket("HN_TXQ", sizeof(*txq), RTE_CACHE_LINE_SIZE,
222                                  socket_id);
223         if (!txq)
224                 return -ENOMEM;
225
226         txq->hv = hv;
227         txq->chan = hv->channels[queue_idx];
228         txq->port_id = dev->data->port_id;
229         txq->queue_id = queue_idx;
230
231         tx_free_thresh = tx_conf->tx_free_thresh;
232         if (tx_free_thresh == 0)
233                 tx_free_thresh = RTE_MIN(hv->chim_cnt / 4,
234                                          DEFAULT_TX_FREE_THRESH);
235
236         if (tx_free_thresh >= hv->chim_cnt - 3)
237                 tx_free_thresh = hv->chim_cnt - 3;
238
239         txq->free_thresh = tx_free_thresh;
240
241         txq->agg_szmax  = RTE_MIN(hv->chim_szmax, hv->rndis_agg_size);
242         txq->agg_pktmax = hv->rndis_agg_pkts;
243         txq->agg_align  = hv->rndis_agg_align;
244
245         hn_reset_txagg(txq);
246
247         dev->data->tx_queues[queue_idx] = txq;
248
249         return 0;
250 }
251
252 void
253 hn_dev_tx_queue_release(void *arg)
254 {
255         struct hn_tx_queue *txq = arg;
256         struct hn_txdesc *txd;
257
258         PMD_INIT_FUNC_TRACE();
259
260         if (!txq)
261                 return;
262
263         /* If any pending data is still present just drop it */
264         txd = txq->agg_txd;
265         if (txd)
266                 rte_mempool_put(txq->hv->tx_pool, txd);
267
268         rte_free(txq);
269 }
270
271 void
272 hn_dev_tx_queue_info(struct rte_eth_dev *dev, uint16_t queue_idx,
273                      struct rte_eth_txq_info *qinfo)
274 {
275         struct hn_data *hv = dev->data->dev_private;
276         struct hn_tx_queue *txq = dev->data->rx_queues[queue_idx];
277
278         qinfo->conf.tx_free_thresh = txq->free_thresh;
279         qinfo->nb_desc = hv->tx_pool->size;
280 }
281
282 static void
283 hn_nvs_send_completed(struct rte_eth_dev *dev, uint16_t queue_id,
284                       unsigned long xactid, const struct hn_nvs_rndis_ack *ack)
285 {
286         struct hn_txdesc *txd = (struct hn_txdesc *)xactid;
287         struct hn_tx_queue *txq;
288
289         /* Control packets are sent with xacid == 0 */
290         if (!txd)
291                 return;
292
293         txq = dev->data->tx_queues[queue_id];
294         if (likely(ack->status == NVS_STATUS_OK)) {
295                 PMD_TX_LOG(DEBUG, "port %u:%u complete tx %u packets %u bytes %u",
296                            txq->port_id, txq->queue_id, txd->chim_index,
297                            txd->packets, txd->data_size);
298                 txq->stats.bytes += txd->data_size;
299                 txq->stats.packets += txd->packets;
300         } else {
301                 PMD_TX_LOG(NOTICE, "port %u:%u complete tx %u failed status %u",
302                            txq->port_id, txq->queue_id, txd->chim_index, ack->status);
303                 ++txq->stats.errors;
304         }
305
306         rte_pktmbuf_free(txd->m);
307
308         rte_mempool_put(txq->hv->tx_pool, txd);
309 }
310
311 /* Handle transmit completion events */
312 static void
313 hn_nvs_handle_comp(struct rte_eth_dev *dev, uint16_t queue_id,
314                    const struct vmbus_chanpkt_hdr *pkt,
315                    const void *data)
316 {
317         const struct hn_nvs_hdr *hdr = data;
318
319         switch (hdr->type) {
320         case NVS_TYPE_RNDIS_ACK:
321                 hn_nvs_send_completed(dev, queue_id, pkt->xactid, data);
322                 break;
323
324         default:
325                 PMD_TX_LOG(NOTICE,
326                            "unexpected send completion type %u",
327                            hdr->type);
328         }
329 }
330
331 /* Parse per-packet info (meta data) */
332 static int
333 hn_rndis_rxinfo(const void *info_data, unsigned int info_dlen,
334                 struct hn_rxinfo *info)
335 {
336         const struct rndis_pktinfo *pi = info_data;
337         uint32_t mask = 0;
338
339         while (info_dlen != 0) {
340                 const void *data;
341                 uint32_t dlen;
342
343                 if (unlikely(info_dlen < sizeof(*pi)))
344                         return -EINVAL;
345
346                 if (unlikely(info_dlen < pi->size))
347                         return -EINVAL;
348                 info_dlen -= pi->size;
349
350                 if (unlikely(pi->size & RNDIS_PKTINFO_SIZE_ALIGNMASK))
351                         return -EINVAL;
352                 if (unlikely(pi->size < pi->offset))
353                         return -EINVAL;
354
355                 dlen = pi->size - pi->offset;
356                 data = pi->data;
357
358                 switch (pi->type) {
359                 case NDIS_PKTINFO_TYPE_VLAN:
360                         if (unlikely(dlen < NDIS_VLAN_INFO_SIZE))
361                                 return -EINVAL;
362                         info->vlan_info = *((const uint32_t *)data);
363                         mask |= HN_RXINFO_VLAN;
364                         break;
365
366                 case NDIS_PKTINFO_TYPE_CSUM:
367                         if (unlikely(dlen < NDIS_RXCSUM_INFO_SIZE))
368                                 return -EINVAL;
369                         info->csum_info = *((const uint32_t *)data);
370                         mask |= HN_RXINFO_CSUM;
371                         break;
372
373                 case NDIS_PKTINFO_TYPE_HASHVAL:
374                         if (unlikely(dlen < NDIS_HASH_VALUE_SIZE))
375                                 return -EINVAL;
376                         info->hash_value = *((const uint32_t *)data);
377                         mask |= HN_RXINFO_HASHVAL;
378                         break;
379
380                 case NDIS_PKTINFO_TYPE_HASHINF:
381                         if (unlikely(dlen < NDIS_HASH_INFO_SIZE))
382                                 return -EINVAL;
383                         info->hash_info = *((const uint32_t *)data);
384                         mask |= HN_RXINFO_HASHINF;
385                         break;
386
387                 default:
388                         goto next;
389                 }
390
391                 if (mask == HN_RXINFO_ALL)
392                         break; /* All found; done */
393 next:
394                 pi = (const struct rndis_pktinfo *)
395                     ((const uint8_t *)pi + pi->size);
396         }
397
398         /*
399          * Final fixup.
400          * - If there is no hash value, invalidate the hash info.
401          */
402         if (!(mask & HN_RXINFO_HASHVAL))
403                 info->hash_info = HN_NDIS_HASH_INFO_INVALID;
404         return 0;
405 }
406
407 /*
408  * Ack the consumed RXBUF associated w/ this channel packet,
409  * so that this RXBUF can be recycled by the hypervisor.
410  */
411 static void hn_rx_buf_release(struct hn_rx_bufinfo *rxb)
412 {
413         struct rte_mbuf_ext_shared_info *shinfo = &rxb->shinfo;
414         struct hn_data *hv = rxb->hv;
415
416         if (rte_mbuf_ext_refcnt_update(shinfo, -1) == 0) {
417                 hn_nvs_ack_rxbuf(rxb->chan, rxb->xactid);
418                 --hv->rxbuf_outstanding;
419         }
420 }
421
422 static void hn_rx_buf_free_cb(void *buf __rte_unused, void *opaque)
423 {
424         hn_rx_buf_release(opaque);
425 }
426
427 static struct hn_rx_bufinfo *hn_rx_buf_init(const struct hn_rx_queue *rxq,
428                                             const struct vmbus_chanpkt_rxbuf *pkt)
429 {
430         struct hn_rx_bufinfo *rxb;
431
432         rxb = rxq->hv->rxbuf_info + pkt->hdr.xactid;
433         rxb->chan = rxq->chan;
434         rxb->xactid = pkt->hdr.xactid;
435         rxb->hv = rxq->hv;
436
437         rxb->shinfo.free_cb = hn_rx_buf_free_cb;
438         rxb->shinfo.fcb_opaque = rxb;
439         rte_mbuf_ext_refcnt_set(&rxb->shinfo, 1);
440         return rxb;
441 }
442
443 static void hn_rxpkt(struct hn_rx_queue *rxq, struct hn_rx_bufinfo *rxb,
444                      uint8_t *data, unsigned int headroom, unsigned int dlen,
445                      const struct hn_rxinfo *info)
446 {
447         struct hn_data *hv = rxq->hv;
448         struct rte_mbuf *m;
449
450         m = rte_pktmbuf_alloc(rxq->mb_pool);
451         if (unlikely(!m)) {
452                 struct rte_eth_dev *dev =
453                         &rte_eth_devices[rxq->port_id];
454
455                 dev->data->rx_mbuf_alloc_failed++;
456                 return;
457         }
458
459         /*
460          * For large packets, avoid copy if possible but need to keep
461          * some space available in receive area for later packets.
462          */
463         if (dlen >= HN_RXCOPY_THRESHOLD &&
464             hv->rxbuf_outstanding < hv->rxbuf_section_cnt / 2) {
465                 struct rte_mbuf_ext_shared_info *shinfo;
466                 const void *rxbuf;
467                 rte_iova_t iova;
468
469                 /*
470                  * Build an external mbuf that points to recveive area.
471                  * Use refcount to handle multiple packets in same
472                  * receive buffer section.
473                  */
474                 rxbuf = hv->rxbuf_res->addr;
475                 iova = rte_mem_virt2iova(rxbuf) + RTE_PTR_DIFF(data, rxbuf);
476                 shinfo = &rxb->shinfo;
477
478                 if (rte_mbuf_ext_refcnt_update(shinfo, 1) == 1)
479                         ++hv->rxbuf_outstanding;
480
481                 rte_pktmbuf_attach_extbuf(m, data, iova,
482                                           dlen + headroom, shinfo);
483                 m->data_off = headroom;
484         } else {
485                 /* Mbuf's in pool must be large enough to hold small packets */
486                 if (unlikely(rte_pktmbuf_tailroom(m) < dlen)) {
487                         rte_pktmbuf_free_seg(m);
488                         ++rxq->stats.errors;
489                         return;
490                 }
491                 rte_memcpy(rte_pktmbuf_mtod(m, void *),
492                            data + headroom, dlen);
493         }
494
495         m->port = rxq->port_id;
496         m->pkt_len = dlen;
497         m->data_len = dlen;
498
499         if (info->vlan_info != HN_NDIS_VLAN_INFO_INVALID) {
500                 m->vlan_tci = info->vlan_info;
501                 m->ol_flags |= PKT_RX_VLAN_STRIPPED | PKT_RX_VLAN;
502         }
503
504         if (info->csum_info != HN_NDIS_RXCSUM_INFO_INVALID) {
505                 if (info->csum_info & NDIS_RXCSUM_INFO_IPCS_OK)
506                         m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
507
508                 if (info->csum_info & (NDIS_RXCSUM_INFO_UDPCS_OK
509                                        | NDIS_RXCSUM_INFO_TCPCS_OK))
510                         m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
511         }
512
513         if (info->hash_info != HN_NDIS_HASH_INFO_INVALID) {
514                 m->ol_flags |= PKT_RX_RSS_HASH;
515                 m->hash.rss = info->hash_value;
516         }
517
518         PMD_RX_LOG(DEBUG, "port %u:%u RX id %" PRIu64 " size %u ol_flags %#" PRIx64,
519                    rxq->port_id, rxq->queue_id, rxb->xactid,
520                    m->pkt_len, m->ol_flags);
521
522         ++rxq->stats.packets;
523         rxq->stats.bytes += m->pkt_len;
524         hn_update_packet_stats(&rxq->stats, m);
525
526         if (unlikely(rte_ring_sp_enqueue(rxq->rx_ring, m) != 0)) {
527                 ++rxq->ring_full;
528                 rte_pktmbuf_free(m);
529         }
530 }
531
532 static void hn_rndis_rx_data(struct hn_rx_queue *rxq,
533                              struct hn_rx_bufinfo *rxb,
534                              void *data, uint32_t dlen)
535 {
536         unsigned int data_off, data_len, pktinfo_off, pktinfo_len;
537         const struct rndis_packet_msg *pkt = data;
538         struct hn_rxinfo info = {
539                 .vlan_info = HN_NDIS_VLAN_INFO_INVALID,
540                 .csum_info = HN_NDIS_RXCSUM_INFO_INVALID,
541                 .hash_info = HN_NDIS_HASH_INFO_INVALID,
542         };
543         int err;
544
545         hn_rndis_dump(pkt);
546
547         if (unlikely(dlen < sizeof(*pkt)))
548                 goto error;
549
550         if (unlikely(dlen < pkt->len))
551                 goto error; /* truncated RNDIS from host */
552
553         if (unlikely(pkt->len < pkt->datalen
554                      + pkt->oobdatalen + pkt->pktinfolen))
555                 goto error;
556
557         if (unlikely(pkt->datalen == 0))
558                 goto error;
559
560         /* Check offsets. */
561         if (unlikely(pkt->dataoffset < RNDIS_PACKET_MSG_OFFSET_MIN))
562                 goto error;
563
564         if (likely(pkt->pktinfooffset > 0) &&
565             unlikely(pkt->pktinfooffset < RNDIS_PACKET_MSG_OFFSET_MIN ||
566                      (pkt->pktinfooffset & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK)))
567                 goto error;
568
569         data_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
570         data_len = pkt->datalen;
571         pktinfo_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->pktinfooffset);
572         pktinfo_len = pkt->pktinfolen;
573
574         if (likely(pktinfo_len > 0)) {
575                 err = hn_rndis_rxinfo((const uint8_t *)pkt + pktinfo_off,
576                                       pktinfo_len, &info);
577                 if (err)
578                         goto error;
579         }
580
581         if (unlikely(data_off + data_len > pkt->len))
582                 goto error;
583
584         if (unlikely(data_len < ETHER_HDR_LEN))
585                 goto error;
586
587         hn_rxpkt(rxq, rxb, data, data_off, data_len, &info);
588         return;
589 error:
590         ++rxq->stats.errors;
591 }
592
593 static void
594 hn_rndis_receive(const struct rte_eth_dev *dev, struct hn_rx_queue *rxq,
595                  struct hn_rx_bufinfo *rxb, void *buf, uint32_t len)
596 {
597         const struct rndis_msghdr *hdr = buf;
598
599         switch (hdr->type) {
600         case RNDIS_PACKET_MSG:
601                 if (dev->data->dev_started)
602                         hn_rndis_rx_data(rxq, rxb, buf, len);
603                 break;
604
605         case RNDIS_INDICATE_STATUS_MSG:
606                 hn_rndis_link_status(rxq->hv, buf);
607                 break;
608
609         case RNDIS_INITIALIZE_CMPLT:
610         case RNDIS_QUERY_CMPLT:
611         case RNDIS_SET_CMPLT:
612                 hn_rndis_receive_response(rxq->hv, buf, len);
613                 break;
614
615         default:
616                 PMD_DRV_LOG(NOTICE,
617                             "unexpected RNDIS message (type %#x len %u)",
618                             hdr->type, len);
619                 break;
620         }
621 }
622
623 static void
624 hn_nvs_handle_rxbuf(struct rte_eth_dev *dev,
625                     struct hn_data *hv,
626                     struct hn_rx_queue *rxq,
627                     const struct vmbus_chanpkt_hdr *hdr,
628                     const void *buf)
629 {
630         const struct vmbus_chanpkt_rxbuf *pkt;
631         const struct hn_nvs_hdr *nvs_hdr = buf;
632         uint32_t rxbuf_sz = hv->rxbuf_res->len;
633         char *rxbuf = hv->rxbuf_res->addr;
634         unsigned int i, hlen, count;
635         struct hn_rx_bufinfo *rxb;
636
637         /* At minimum we need type header */
638         if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*nvs_hdr))) {
639                 PMD_RX_LOG(ERR, "invalid receive nvs RNDIS");
640                 return;
641         }
642
643         /* Make sure that this is a RNDIS message. */
644         if (unlikely(nvs_hdr->type != NVS_TYPE_RNDIS)) {
645                 PMD_RX_LOG(ERR, "nvs type %u, not RNDIS",
646                            nvs_hdr->type);
647                 return;
648         }
649
650         hlen = vmbus_chanpkt_getlen(hdr->hlen);
651         if (unlikely(hlen < sizeof(*pkt))) {
652                 PMD_RX_LOG(ERR, "invalid rxbuf chanpkt");
653                 return;
654         }
655
656         pkt = container_of(hdr, const struct vmbus_chanpkt_rxbuf, hdr);
657         if (unlikely(pkt->rxbuf_id != NVS_RXBUF_SIG)) {
658                 PMD_RX_LOG(ERR, "invalid rxbuf_id 0x%08x",
659                            pkt->rxbuf_id);
660                 return;
661         }
662
663         count = pkt->rxbuf_cnt;
664         if (unlikely(hlen < offsetof(struct vmbus_chanpkt_rxbuf,
665                                      rxbuf[count]))) {
666                 PMD_RX_LOG(ERR, "invalid rxbuf_cnt %u", count);
667                 return;
668         }
669
670         if (pkt->hdr.xactid > hv->rxbuf_section_cnt) {
671                 PMD_RX_LOG(ERR, "invalid rxbuf section id %" PRIx64,
672                            pkt->hdr.xactid);
673                 return;
674         }
675
676         /* Setup receive buffer info to allow for callback */
677         rxb = hn_rx_buf_init(rxq, pkt);
678
679         /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
680         for (i = 0; i < count; ++i) {
681                 unsigned int ofs, len;
682
683                 ofs = pkt->rxbuf[i].ofs;
684                 len = pkt->rxbuf[i].len;
685
686                 if (unlikely(ofs + len > rxbuf_sz)) {
687                         PMD_RX_LOG(ERR,
688                                    "%uth RNDIS msg overflow ofs %u, len %u",
689                                    i, ofs, len);
690                         continue;
691                 }
692
693                 if (unlikely(len == 0)) {
694                         PMD_RX_LOG(ERR, "%uth RNDIS msg len %u", i, len);
695                         continue;
696                 }
697
698                 hn_rndis_receive(dev, rxq, rxb,
699                                  rxbuf + ofs, len);
700         }
701
702         /* Send ACK now if external mbuf not used */
703         hn_rx_buf_release(rxb);
704 }
705
706 struct hn_rx_queue *hn_rx_queue_alloc(struct hn_data *hv,
707                                       uint16_t queue_id,
708                                       unsigned int socket_id)
709 {
710         struct hn_rx_queue *rxq;
711
712         rxq = rte_zmalloc_socket("HN_RXQ",
713                                  sizeof(*rxq) + HN_RXQ_EVENT_DEFAULT,
714                                  RTE_CACHE_LINE_SIZE, socket_id);
715         if (rxq) {
716                 rxq->hv = hv;
717                 rxq->chan = hv->channels[queue_id];
718                 rte_spinlock_init(&rxq->ring_lock);
719                 rxq->port_id = hv->port_id;
720                 rxq->queue_id = queue_id;
721         }
722         return rxq;
723 }
724
725 int
726 hn_dev_rx_queue_setup(struct rte_eth_dev *dev,
727                       uint16_t queue_idx, uint16_t nb_desc,
728                       unsigned int socket_id,
729                       const struct rte_eth_rxconf *rx_conf __rte_unused,
730                       struct rte_mempool *mp)
731 {
732         struct hn_data *hv = dev->data->dev_private;
733         char ring_name[RTE_RING_NAMESIZE];
734         struct hn_rx_queue *rxq;
735         unsigned int count;
736
737         PMD_INIT_FUNC_TRACE();
738
739         if (queue_idx == 0) {
740                 rxq = hv->primary;
741         } else {
742                 rxq = hn_rx_queue_alloc(hv, queue_idx, socket_id);
743                 if (!rxq)
744                         return -ENOMEM;
745         }
746
747         rxq->mb_pool = mp;
748         count = rte_mempool_avail_count(mp) / dev->data->nb_rx_queues;
749         if (nb_desc == 0 || nb_desc > count)
750                 nb_desc = count;
751
752         /*
753          * Staging ring from receive event logic to rx_pkts.
754          * rx_pkts assumes caller is handling multi-thread issue.
755          * event logic has locking.
756          */
757         snprintf(ring_name, sizeof(ring_name),
758                  "hn_rx_%u_%u", dev->data->port_id, queue_idx);
759         rxq->rx_ring = rte_ring_create(ring_name,
760                                        rte_align32pow2(nb_desc),
761                                        socket_id, 0);
762         if (!rxq->rx_ring)
763                 goto fail;
764
765         dev->data->rx_queues[queue_idx] = rxq;
766         return 0;
767
768 fail:
769         rte_ring_free(rxq->rx_ring);
770         rte_free(rxq->event_buf);
771         rte_free(rxq);
772         return -ENOMEM;
773 }
774
775 void
776 hn_dev_rx_queue_release(void *arg)
777 {
778         struct hn_rx_queue *rxq = arg;
779
780         PMD_INIT_FUNC_TRACE();
781
782         if (!rxq)
783                 return;
784
785         rte_ring_free(rxq->rx_ring);
786         rxq->rx_ring = NULL;
787         rxq->mb_pool = NULL;
788
789         if (rxq != rxq->hv->primary) {
790                 rte_free(rxq->event_buf);
791                 rte_free(rxq);
792         }
793 }
794
795 void
796 hn_dev_rx_queue_info(struct rte_eth_dev *dev, uint16_t queue_idx,
797                      struct rte_eth_rxq_info *qinfo)
798 {
799         struct hn_rx_queue *rxq = dev->data->rx_queues[queue_idx];
800
801         qinfo->mp = rxq->mb_pool;
802         qinfo->scattered_rx = 1;
803         qinfo->nb_desc = rte_ring_get_capacity(rxq->rx_ring);
804 }
805
806 static void
807 hn_nvs_handle_notify(const struct vmbus_chanpkt_hdr *pkthdr,
808                      const void *data)
809 {
810         const struct hn_nvs_hdr *hdr = data;
811
812         if (unlikely(vmbus_chanpkt_datalen(pkthdr) < sizeof(*hdr))) {
813                 PMD_DRV_LOG(ERR, "invalid nvs notify");
814                 return;
815         }
816
817         PMD_DRV_LOG(INFO,
818                     "got notify, nvs type %u", hdr->type);
819 }
820
821 /*
822  * Process pending events on the channel.
823  * Called from both Rx queue poll and Tx cleanup
824  */
825 void hn_process_events(struct hn_data *hv, uint16_t queue_id)
826 {
827         struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
828         struct hn_rx_queue *rxq;
829         uint32_t bytes_read = 0;
830         int ret = 0;
831
832         rxq = queue_id == 0 ? hv->primary : dev->data->rx_queues[queue_id];
833
834         /* If no pending data then nothing to do */
835         if (rte_vmbus_chan_rx_empty(rxq->chan))
836                 return;
837
838         /*
839          * Since channel is shared between Rx and TX queue need to have a lock
840          * since DPDK does not force same CPU to be used for Rx/Tx.
841          */
842         if (unlikely(!rte_spinlock_trylock(&rxq->ring_lock)))
843                 return;
844
845         for (;;) {
846                 const struct vmbus_chanpkt_hdr *pkt;
847                 uint32_t len = HN_RXQ_EVENT_DEFAULT;
848                 const void *data;
849
850                 ret = rte_vmbus_chan_recv_raw(rxq->chan, rxq->event_buf, &len);
851                 if (ret == -EAGAIN)
852                         break;  /* ring is empty */
853
854                 else if (ret == -ENOBUFS)
855                         rte_exit(EXIT_FAILURE, "event buffer not big enough (%u < %u)",
856                                  HN_RXQ_EVENT_DEFAULT, len);
857                 else if (ret <= 0)
858                         rte_exit(EXIT_FAILURE,
859                                  "vmbus ring buffer error: %d", ret);
860
861                 bytes_read += ret;
862                 pkt = (const struct vmbus_chanpkt_hdr *)rxq->event_buf;
863                 data = (char *)rxq->event_buf + vmbus_chanpkt_getlen(pkt->hlen);
864
865                 switch (pkt->type) {
866                 case VMBUS_CHANPKT_TYPE_COMP:
867                         hn_nvs_handle_comp(dev, queue_id, pkt, data);
868                         break;
869
870                 case VMBUS_CHANPKT_TYPE_RXBUF:
871                         hn_nvs_handle_rxbuf(dev, hv, rxq, pkt, data);
872                         break;
873
874                 case VMBUS_CHANPKT_TYPE_INBAND:
875                         hn_nvs_handle_notify(pkt, data);
876                         break;
877
878                 default:
879                         PMD_DRV_LOG(ERR, "unknown chan pkt %u", pkt->type);
880                         break;
881                 }
882
883                 if (rxq->rx_ring && rte_ring_full(rxq->rx_ring))
884                         break;
885         }
886
887         if (bytes_read > 0)
888                 rte_vmbus_chan_signal_read(rxq->chan, bytes_read);
889
890         rte_spinlock_unlock(&rxq->ring_lock);
891 }
892
893 static void hn_append_to_chim(struct hn_tx_queue *txq,
894                               struct rndis_packet_msg *pkt,
895                               const struct rte_mbuf *m)
896 {
897         struct hn_txdesc *txd = txq->agg_txd;
898         uint8_t *buf = (uint8_t *)pkt;
899         unsigned int data_offs;
900
901         hn_rndis_dump(pkt);
902
903         data_offs = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
904         txd->chim_size += pkt->len;
905         txd->data_size += m->pkt_len;
906         ++txd->packets;
907         hn_update_packet_stats(&txq->stats, m);
908
909         for (; m; m = m->next) {
910                 uint16_t len = rte_pktmbuf_data_len(m);
911
912                 rte_memcpy(buf + data_offs,
913                            rte_pktmbuf_mtod(m, const char *), len);
914                 data_offs += len;
915         }
916 }
917
918 /*
919  * Send pending aggregated data in chimney buffer (if any).
920  * Returns error if send was unsuccessful because channel ring buffer
921  * was full.
922  */
923 static int hn_flush_txagg(struct hn_tx_queue *txq, bool *need_sig)
924
925 {
926         struct hn_txdesc *txd = txq->agg_txd;
927         struct hn_nvs_rndis rndis;
928         int ret;
929
930         if (!txd)
931                 return 0;
932
933         rndis = (struct hn_nvs_rndis) {
934                 .type = NVS_TYPE_RNDIS,
935                 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
936                 .chim_idx = txd->chim_index,
937                 .chim_sz = txd->chim_size,
938         };
939
940         PMD_TX_LOG(DEBUG, "port %u:%u tx %u size %u",
941                    txq->port_id, txq->queue_id, txd->chim_index, txd->chim_size);
942
943         ret = hn_nvs_send(txq->chan, VMBUS_CHANPKT_FLAG_RC,
944                           &rndis, sizeof(rndis), (uintptr_t)txd, need_sig);
945
946         if (likely(ret == 0))
947                 hn_reset_txagg(txq);
948         else
949                 PMD_TX_LOG(NOTICE, "port %u:%u send failed: %d",
950                            txq->port_id, txq->queue_id, ret);
951
952         return ret;
953 }
954
955 static struct hn_txdesc *hn_new_txd(struct hn_data *hv,
956                                     struct hn_tx_queue *txq)
957 {
958         struct hn_txdesc *txd;
959
960         if (rte_mempool_get(hv->tx_pool, (void **)&txd)) {
961                 ++txq->stats.nomemory;
962                 PMD_TX_LOG(DEBUG, "tx pool exhausted!");
963                 return NULL;
964         }
965
966         txd->m = NULL;
967         txd->queue_id = txq->queue_id;
968         txd->packets = 0;
969         txd->data_size = 0;
970         txd->chim_size = 0;
971
972         return txd;
973 }
974
975 static void *
976 hn_try_txagg(struct hn_data *hv, struct hn_tx_queue *txq, uint32_t pktsize)
977 {
978         struct hn_txdesc *agg_txd = txq->agg_txd;
979         struct rndis_packet_msg *pkt;
980         void *chim;
981
982         if (agg_txd) {
983                 unsigned int padding, olen;
984
985                 /*
986                  * Update the previous RNDIS packet's total length,
987                  * it can be increased due to the mandatory alignment
988                  * padding for this RNDIS packet.  And update the
989                  * aggregating txdesc's chimney sending buffer size
990                  * accordingly.
991                  *
992                  * Zero-out the padding, as required by the RNDIS spec.
993                  */
994                 pkt = txq->agg_prevpkt;
995                 olen = pkt->len;
996                 padding = RTE_ALIGN(olen, txq->agg_align) - olen;
997                 if (padding > 0) {
998                         agg_txd->chim_size += padding;
999                         pkt->len += padding;
1000                         memset((uint8_t *)pkt + olen, 0, padding);
1001                 }
1002
1003                 chim = (uint8_t *)pkt + pkt->len;
1004
1005                 txq->agg_pktleft--;
1006                 txq->agg_szleft -= pktsize;
1007                 if (txq->agg_szleft < HN_PKTSIZE_MIN(txq->agg_align)) {
1008                         /*
1009                          * Probably can't aggregate more packets,
1010                          * flush this aggregating txdesc proactively.
1011                          */
1012                         txq->agg_pktleft = 0;
1013                 }
1014         } else {
1015                 agg_txd = hn_new_txd(hv, txq);
1016                 if (!agg_txd)
1017                         return NULL;
1018
1019                 chim = (uint8_t *)hv->chim_res->addr
1020                         + agg_txd->chim_index * hv->chim_szmax;
1021
1022                 txq->agg_txd = agg_txd;
1023                 txq->agg_pktleft = txq->agg_pktmax - 1;
1024                 txq->agg_szleft = txq->agg_szmax - pktsize;
1025         }
1026         txq->agg_prevpkt = chim;
1027
1028         return chim;
1029 }
1030
1031 static inline void *
1032 hn_rndis_pktinfo_append(struct rndis_packet_msg *pkt,
1033                         uint32_t pi_dlen, uint32_t pi_type)
1034 {
1035         const uint32_t pi_size = RNDIS_PKTINFO_SIZE(pi_dlen);
1036         struct rndis_pktinfo *pi;
1037
1038         /*
1039          * Per-packet-info does not move; it only grows.
1040          *
1041          * NOTE:
1042          * pktinfooffset in this phase counts from the beginning
1043          * of rndis_packet_msg.
1044          */
1045         pi = (struct rndis_pktinfo *)((uint8_t *)pkt + hn_rndis_pktlen(pkt));
1046
1047         pkt->pktinfolen += pi_size;
1048
1049         pi->size = pi_size;
1050         pi->type = pi_type;
1051         pi->offset = RNDIS_PKTINFO_OFFSET;
1052
1053         return pi->data;
1054 }
1055
1056 /* Put RNDIS header and packet info on packet */
1057 static void hn_encap(struct rndis_packet_msg *pkt,
1058                      uint16_t queue_id,
1059                      const struct rte_mbuf *m)
1060 {
1061         unsigned int hlen = m->l2_len + m->l3_len;
1062         uint32_t *pi_data;
1063         uint32_t pkt_hlen;
1064
1065         pkt->type = RNDIS_PACKET_MSG;
1066         pkt->len = m->pkt_len;
1067         pkt->dataoffset = 0;
1068         pkt->datalen = m->pkt_len;
1069         pkt->oobdataoffset = 0;
1070         pkt->oobdatalen = 0;
1071         pkt->oobdataelements = 0;
1072         pkt->pktinfooffset = sizeof(*pkt);
1073         pkt->pktinfolen = 0;
1074         pkt->vchandle = 0;
1075         pkt->reserved = 0;
1076
1077         /*
1078          * Set the hash value for this packet, to the queue_id to cause
1079          * TX done event for this packet on the right channel.
1080          */
1081         pi_data = hn_rndis_pktinfo_append(pkt, NDIS_HASH_VALUE_SIZE,
1082                                           NDIS_PKTINFO_TYPE_HASHVAL);
1083         *pi_data = queue_id;
1084
1085         if (m->ol_flags & PKT_TX_VLAN_PKT) {
1086                 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_VLAN_INFO_SIZE,
1087                                                   NDIS_PKTINFO_TYPE_VLAN);
1088                 *pi_data = m->vlan_tci;
1089         }
1090
1091         if (m->ol_flags & PKT_TX_TCP_SEG) {
1092                 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_LSO2_INFO_SIZE,
1093                                                   NDIS_PKTINFO_TYPE_LSO);
1094
1095                 if (m->ol_flags & PKT_TX_IPV6) {
1096                         *pi_data = NDIS_LSO2_INFO_MAKEIPV6(hlen,
1097                                                            m->tso_segsz);
1098                 } else {
1099                         *pi_data = NDIS_LSO2_INFO_MAKEIPV4(hlen,
1100                                                            m->tso_segsz);
1101                 }
1102         } else if (m->ol_flags &
1103                    (PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM)) {
1104                 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_TXCSUM_INFO_SIZE,
1105                                                   NDIS_PKTINFO_TYPE_CSUM);
1106                 *pi_data = 0;
1107
1108                 if (m->ol_flags & PKT_TX_IPV6)
1109                         *pi_data |= NDIS_TXCSUM_INFO_IPV6;
1110                 if (m->ol_flags & PKT_TX_IPV4) {
1111                         *pi_data |= NDIS_TXCSUM_INFO_IPV4;
1112
1113                         if (m->ol_flags & PKT_TX_IP_CKSUM)
1114                                 *pi_data |= NDIS_TXCSUM_INFO_IPCS;
1115                 }
1116
1117                 if (m->ol_flags & PKT_TX_TCP_CKSUM)
1118                         *pi_data |= NDIS_TXCSUM_INFO_MKTCPCS(hlen);
1119                 else if (m->ol_flags & PKT_TX_UDP_CKSUM)
1120                         *pi_data |= NDIS_TXCSUM_INFO_MKUDPCS(hlen);
1121         }
1122
1123         pkt_hlen = pkt->pktinfooffset + pkt->pktinfolen;
1124         /* Fixup RNDIS packet message total length */
1125         pkt->len += pkt_hlen;
1126
1127         /* Convert RNDIS packet message offsets */
1128         pkt->dataoffset = hn_rndis_pktmsg_offset(pkt_hlen);
1129         pkt->pktinfooffset = hn_rndis_pktmsg_offset(pkt->pktinfooffset);
1130 }
1131
1132 /* How many scatter gather list elements ar needed */
1133 static unsigned int hn_get_slots(const struct rte_mbuf *m)
1134 {
1135         unsigned int slots = 1; /* for RNDIS header */
1136
1137         while (m) {
1138                 unsigned int size = rte_pktmbuf_data_len(m);
1139                 unsigned int offs = rte_mbuf_data_iova(m) & PAGE_MASK;
1140
1141                 slots += (offs + size + PAGE_SIZE - 1) / PAGE_SIZE;
1142                 m = m->next;
1143         }
1144
1145         return slots;
1146 }
1147
1148 /* Build scatter gather list from chained mbuf */
1149 static unsigned int hn_fill_sg(struct vmbus_gpa *sg,
1150                                const struct rte_mbuf *m)
1151 {
1152         unsigned int segs = 0;
1153
1154         while (m) {
1155                 rte_iova_t addr = rte_mbuf_data_iova(m);
1156                 unsigned int page = addr / PAGE_SIZE;
1157                 unsigned int offset = addr & PAGE_MASK;
1158                 unsigned int len = rte_pktmbuf_data_len(m);
1159
1160                 while (len > 0) {
1161                         unsigned int bytes = RTE_MIN(len, PAGE_SIZE - offset);
1162
1163                         sg[segs].page = page;
1164                         sg[segs].ofs = offset;
1165                         sg[segs].len = bytes;
1166                         segs++;
1167
1168                         ++page;
1169                         offset = 0;
1170                         len -= bytes;
1171                 }
1172                 m = m->next;
1173         }
1174
1175         return segs;
1176 }
1177
1178 /* Transmit directly from mbuf */
1179 static int hn_xmit_sg(struct hn_tx_queue *txq,
1180                       const struct hn_txdesc *txd, const struct rte_mbuf *m,
1181                       bool *need_sig)
1182 {
1183         struct vmbus_gpa sg[hn_get_slots(m)];
1184         struct hn_nvs_rndis nvs_rndis = {
1185                 .type = NVS_TYPE_RNDIS,
1186                 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
1187                 .chim_sz = txd->chim_size,
1188         };
1189         rte_iova_t addr;
1190         unsigned int segs;
1191
1192         /* attach aggregation data if present */
1193         if (txd->chim_size > 0)
1194                 nvs_rndis.chim_idx = txd->chim_index;
1195         else
1196                 nvs_rndis.chim_idx = NVS_CHIM_IDX_INVALID;
1197
1198         hn_rndis_dump(txd->rndis_pkt);
1199
1200         /* pass IOVA of rndis header in first segment */
1201         addr = rte_malloc_virt2iova(txd->rndis_pkt);
1202         if (unlikely(addr == RTE_BAD_IOVA)) {
1203                 PMD_DRV_LOG(ERR, "RNDIS transmit can not get iova");
1204                 return -EINVAL;
1205         }
1206
1207         sg[0].page = addr / PAGE_SIZE;
1208         sg[0].ofs = addr & PAGE_MASK;
1209         sg[0].len = RNDIS_PACKET_MSG_OFFSET_ABS(hn_rndis_pktlen(txd->rndis_pkt));
1210         segs = 1;
1211
1212         hn_update_packet_stats(&txq->stats, m);
1213
1214         segs += hn_fill_sg(sg + 1, m);
1215
1216         PMD_TX_LOG(DEBUG, "port %u:%u tx %u segs %u size %u",
1217                    txq->port_id, txq->queue_id, txd->chim_index,
1218                    segs, nvs_rndis.chim_sz);
1219
1220         return hn_nvs_send_sglist(txq->chan, sg, segs,
1221                                   &nvs_rndis, sizeof(nvs_rndis),
1222                                   (uintptr_t)txd, need_sig);
1223 }
1224
1225 uint16_t
1226 hn_xmit_pkts(void *ptxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1227 {
1228         struct hn_tx_queue *txq = ptxq;
1229         struct hn_data *hv = txq->hv;
1230         bool need_sig = false;
1231         uint16_t nb_tx;
1232         int ret;
1233
1234         if (unlikely(hv->closed))
1235                 return 0;
1236
1237         if (rte_mempool_avail_count(hv->tx_pool) <= txq->free_thresh)
1238                 hn_process_events(hv, txq->queue_id);
1239
1240         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1241                 struct rte_mbuf *m = tx_pkts[nb_tx];
1242                 uint32_t pkt_size = m->pkt_len + HN_RNDIS_PKT_LEN;
1243                 struct rndis_packet_msg *pkt;
1244
1245                 /* For small packets aggregate them in chimney buffer */
1246                 if (m->pkt_len < HN_TXCOPY_THRESHOLD && pkt_size <= txq->agg_szmax) {
1247                         /* If this packet will not fit, then flush  */
1248                         if (txq->agg_pktleft == 0 ||
1249                             RTE_ALIGN(pkt_size, txq->agg_align) > txq->agg_szleft) {
1250                                 if (hn_flush_txagg(txq, &need_sig))
1251                                         goto fail;
1252                         }
1253
1254                         pkt = hn_try_txagg(hv, txq, pkt_size);
1255                         if (unlikely(!pkt))
1256                                 break;
1257
1258                         hn_encap(pkt, txq->queue_id, m);
1259                         hn_append_to_chim(txq, pkt, m);
1260
1261                         rte_pktmbuf_free(m);
1262
1263                         /* if buffer is full, flush */
1264                         if (txq->agg_pktleft == 0 &&
1265                             hn_flush_txagg(txq, &need_sig))
1266                                 goto fail;
1267                 } else {
1268                         struct hn_txdesc *txd;
1269
1270                         /* can send chimney data and large packet at once */
1271                         txd = txq->agg_txd;
1272                         if (txd) {
1273                                 hn_reset_txagg(txq);
1274                         } else {
1275                                 txd = hn_new_txd(hv, txq);
1276                                 if (unlikely(!txd))
1277                                         break;
1278                         }
1279
1280                         pkt = txd->rndis_pkt;
1281                         txd->m = m;
1282                         txd->data_size += m->pkt_len;
1283                         ++txd->packets;
1284
1285                         hn_encap(pkt, txq->queue_id, m);
1286
1287                         ret = hn_xmit_sg(txq, txd, m, &need_sig);
1288                         if (unlikely(ret != 0)) {
1289                                 PMD_TX_LOG(NOTICE, "sg send failed: %d", ret);
1290                                 ++txq->stats.errors;
1291                                 rte_mempool_put(hv->tx_pool, txd);
1292                                 goto fail;
1293                         }
1294                 }
1295         }
1296
1297         /* If partial buffer left, then try and send it.
1298          * if that fails, then reuse it on next send.
1299          */
1300         hn_flush_txagg(txq, &need_sig);
1301
1302 fail:
1303         if (need_sig)
1304                 rte_vmbus_chan_signal_tx(txq->chan);
1305
1306         return nb_tx;
1307 }
1308
1309 uint16_t
1310 hn_recv_pkts(void *prxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1311 {
1312         struct hn_rx_queue *rxq = prxq;
1313         struct hn_data *hv = rxq->hv;
1314
1315         if (unlikely(hv->closed))
1316                 return 0;
1317
1318         /* If ring is empty then process more */
1319         if (rte_ring_count(rxq->rx_ring) < nb_pkts)
1320                 hn_process_events(hv, rxq->queue_id);
1321
1322         /* Get mbufs off staging ring */
1323         return rte_ring_sc_dequeue_burst(rxq->rx_ring, (void **)rx_pkts,
1324                                          nb_pkts, NULL);
1325 }