400598a621f18beca52e0f127ad6ff335f482bc1
[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    1024
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 static void
272 hn_nvs_send_completed(struct rte_eth_dev *dev, uint16_t queue_id,
273                       unsigned long xactid, const struct hn_nvs_rndis_ack *ack)
274 {
275         struct hn_txdesc *txd = (struct hn_txdesc *)xactid;
276         struct hn_tx_queue *txq;
277
278         /* Control packets are sent with xacid == 0 */
279         if (!txd)
280                 return;
281
282         txq = dev->data->tx_queues[queue_id];
283         if (likely(ack->status == NVS_STATUS_OK)) {
284                 PMD_TX_LOG(DEBUG, "port %u:%u complete tx %u packets %u bytes %u",
285                            txq->port_id, txq->queue_id, txd->chim_index,
286                            txd->packets, txd->data_size);
287                 txq->stats.bytes += txd->data_size;
288                 txq->stats.packets += txd->packets;
289         } else {
290                 PMD_TX_LOG(NOTICE, "port %u:%u complete tx %u failed status %u",
291                            txq->port_id, txq->queue_id, txd->chim_index, ack->status);
292                 ++txq->stats.errors;
293         }
294
295         rte_pktmbuf_free(txd->m);
296
297         rte_mempool_put(txq->hv->tx_pool, txd);
298 }
299
300 /* Handle transmit completion events */
301 static void
302 hn_nvs_handle_comp(struct rte_eth_dev *dev, uint16_t queue_id,
303                    const struct vmbus_chanpkt_hdr *pkt,
304                    const void *data)
305 {
306         const struct hn_nvs_hdr *hdr = data;
307
308         switch (hdr->type) {
309         case NVS_TYPE_RNDIS_ACK:
310                 hn_nvs_send_completed(dev, queue_id, pkt->xactid, data);
311                 break;
312
313         default:
314                 PMD_TX_LOG(NOTICE,
315                            "unexpected send completion type %u",
316                            hdr->type);
317         }
318 }
319
320 /* Parse per-packet info (meta data) */
321 static int
322 hn_rndis_rxinfo(const void *info_data, unsigned int info_dlen,
323                 struct hn_rxinfo *info)
324 {
325         const struct rndis_pktinfo *pi = info_data;
326         uint32_t mask = 0;
327
328         while (info_dlen != 0) {
329                 const void *data;
330                 uint32_t dlen;
331
332                 if (unlikely(info_dlen < sizeof(*pi)))
333                         return -EINVAL;
334
335                 if (unlikely(info_dlen < pi->size))
336                         return -EINVAL;
337                 info_dlen -= pi->size;
338
339                 if (unlikely(pi->size & RNDIS_PKTINFO_SIZE_ALIGNMASK))
340                         return -EINVAL;
341                 if (unlikely(pi->size < pi->offset))
342                         return -EINVAL;
343
344                 dlen = pi->size - pi->offset;
345                 data = pi->data;
346
347                 switch (pi->type) {
348                 case NDIS_PKTINFO_TYPE_VLAN:
349                         if (unlikely(dlen < NDIS_VLAN_INFO_SIZE))
350                                 return -EINVAL;
351                         info->vlan_info = *((const uint32_t *)data);
352                         mask |= HN_RXINFO_VLAN;
353                         break;
354
355                 case NDIS_PKTINFO_TYPE_CSUM:
356                         if (unlikely(dlen < NDIS_RXCSUM_INFO_SIZE))
357                                 return -EINVAL;
358                         info->csum_info = *((const uint32_t *)data);
359                         mask |= HN_RXINFO_CSUM;
360                         break;
361
362                 case NDIS_PKTINFO_TYPE_HASHVAL:
363                         if (unlikely(dlen < NDIS_HASH_VALUE_SIZE))
364                                 return -EINVAL;
365                         info->hash_value = *((const uint32_t *)data);
366                         mask |= HN_RXINFO_HASHVAL;
367                         break;
368
369                 case NDIS_PKTINFO_TYPE_HASHINF:
370                         if (unlikely(dlen < NDIS_HASH_INFO_SIZE))
371                                 return -EINVAL;
372                         info->hash_info = *((const uint32_t *)data);
373                         mask |= HN_RXINFO_HASHINF;
374                         break;
375
376                 default:
377                         goto next;
378                 }
379
380                 if (mask == HN_RXINFO_ALL)
381                         break; /* All found; done */
382 next:
383                 pi = (const struct rndis_pktinfo *)
384                     ((const uint8_t *)pi + pi->size);
385         }
386
387         /*
388          * Final fixup.
389          * - If there is no hash value, invalidate the hash info.
390          */
391         if (!(mask & HN_RXINFO_HASHVAL))
392                 info->hash_info = HN_NDIS_HASH_INFO_INVALID;
393         return 0;
394 }
395
396 /*
397  * Ack the consumed RXBUF associated w/ this channel packet,
398  * so that this RXBUF can be recycled by the hypervisor.
399  */
400 static void hn_rx_buf_release(struct hn_rx_bufinfo *rxb)
401 {
402         struct rte_mbuf_ext_shared_info *shinfo = &rxb->shinfo;
403         struct hn_data *hv = rxb->hv;
404
405         if (rte_mbuf_ext_refcnt_update(shinfo, -1) == 0) {
406                 hn_nvs_ack_rxbuf(rxb->chan, rxb->xactid);
407                 --hv->rxbuf_outstanding;
408         }
409 }
410
411 static void hn_rx_buf_free_cb(void *buf __rte_unused, void *opaque)
412 {
413         hn_rx_buf_release(opaque);
414 }
415
416 static struct hn_rx_bufinfo *hn_rx_buf_init(const struct hn_rx_queue *rxq,
417                                             const struct vmbus_chanpkt_rxbuf *pkt)
418 {
419         struct hn_rx_bufinfo *rxb;
420
421         rxb = rxq->hv->rxbuf_info + pkt->hdr.xactid;
422         rxb->chan = rxq->chan;
423         rxb->xactid = pkt->hdr.xactid;
424         rxb->hv = rxq->hv;
425
426         rxb->shinfo.free_cb = hn_rx_buf_free_cb;
427         rxb->shinfo.fcb_opaque = rxb;
428         rte_mbuf_ext_refcnt_set(&rxb->shinfo, 1);
429         return rxb;
430 }
431
432 static void hn_rxpkt(struct hn_rx_queue *rxq, struct hn_rx_bufinfo *rxb,
433                      uint8_t *data, unsigned int headroom, unsigned int dlen,
434                      const struct hn_rxinfo *info)
435 {
436         struct hn_data *hv = rxq->hv;
437         struct rte_mbuf *m;
438
439         m = rte_pktmbuf_alloc(rxq->mb_pool);
440         if (unlikely(!m)) {
441                 struct rte_eth_dev *dev =
442                         &rte_eth_devices[rxq->port_id];
443
444                 dev->data->rx_mbuf_alloc_failed++;
445                 return;
446         }
447
448         /*
449          * For large packets, avoid copy if possible but need to keep
450          * some space available in receive area for later packets.
451          */
452         if (dlen >= HN_RXCOPY_THRESHOLD &&
453             hv->rxbuf_outstanding < hv->rxbuf_section_cnt / 2) {
454                 struct rte_mbuf_ext_shared_info *shinfo;
455                 const void *rxbuf;
456                 rte_iova_t iova;
457
458                 /*
459                  * Build an external mbuf that points to recveive area.
460                  * Use refcount to handle multiple packets in same
461                  * receive buffer section.
462                  */
463                 rxbuf = hv->rxbuf_res->addr;
464                 iova = rte_mem_virt2iova(rxbuf) + RTE_PTR_DIFF(data, rxbuf);
465                 shinfo = &rxb->shinfo;
466
467                 if (rte_mbuf_ext_refcnt_update(shinfo, 1) == 1)
468                         ++hv->rxbuf_outstanding;
469
470                 rte_pktmbuf_attach_extbuf(m, data, iova,
471                                           dlen + headroom, shinfo);
472                 m->data_off = headroom;
473         } else {
474                 /* Mbuf's in pool must be large enough to hold small packets */
475                 if (unlikely(rte_pktmbuf_tailroom(m) < dlen)) {
476                         rte_pktmbuf_free_seg(m);
477                         ++rxq->stats.errors;
478                         return;
479                 }
480                 rte_memcpy(rte_pktmbuf_mtod(m, void *),
481                            data + headroom, dlen);
482         }
483
484         m->port = rxq->port_id;
485         m->pkt_len = dlen;
486         m->data_len = dlen;
487
488         if (info->vlan_info != HN_NDIS_VLAN_INFO_INVALID) {
489                 m->vlan_tci = info->vlan_info;
490                 m->ol_flags |= PKT_RX_VLAN_STRIPPED | PKT_RX_VLAN;
491         }
492
493         if (info->csum_info != HN_NDIS_RXCSUM_INFO_INVALID) {
494                 if (info->csum_info & NDIS_RXCSUM_INFO_IPCS_OK)
495                         m->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
496
497                 if (info->csum_info & (NDIS_RXCSUM_INFO_UDPCS_OK
498                                        | NDIS_RXCSUM_INFO_TCPCS_OK))
499                         m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
500         }
501
502         if (info->hash_info != HN_NDIS_HASH_INFO_INVALID) {
503                 m->ol_flags |= PKT_RX_RSS_HASH;
504                 m->hash.rss = info->hash_value;
505         }
506
507         PMD_RX_LOG(DEBUG, "port %u:%u RX id %" PRIu64 " size %u ol_flags %#" PRIx64,
508                    rxq->port_id, rxq->queue_id, rxb->xactid,
509                    m->pkt_len, m->ol_flags);
510
511         ++rxq->stats.packets;
512         rxq->stats.bytes += m->pkt_len;
513         hn_update_packet_stats(&rxq->stats, m);
514
515         if (unlikely(rte_ring_sp_enqueue(rxq->rx_ring, m) != 0)) {
516                 ++rxq->ring_full;
517                 rte_pktmbuf_free(m);
518         }
519 }
520
521 static void hn_rndis_rx_data(struct hn_rx_queue *rxq,
522                              struct hn_rx_bufinfo *rxb,
523                              void *data, uint32_t dlen)
524 {
525         unsigned int data_off, data_len, pktinfo_off, pktinfo_len;
526         const struct rndis_packet_msg *pkt = data;
527         struct hn_rxinfo info = {
528                 .vlan_info = HN_NDIS_VLAN_INFO_INVALID,
529                 .csum_info = HN_NDIS_RXCSUM_INFO_INVALID,
530                 .hash_info = HN_NDIS_HASH_INFO_INVALID,
531         };
532         int err;
533
534         hn_rndis_dump(pkt);
535
536         if (unlikely(dlen < sizeof(*pkt)))
537                 goto error;
538
539         if (unlikely(dlen < pkt->len))
540                 goto error; /* truncated RNDIS from host */
541
542         if (unlikely(pkt->len < pkt->datalen
543                      + pkt->oobdatalen + pkt->pktinfolen))
544                 goto error;
545
546         if (unlikely(pkt->datalen == 0))
547                 goto error;
548
549         /* Check offsets. */
550         if (unlikely(pkt->dataoffset < RNDIS_PACKET_MSG_OFFSET_MIN))
551                 goto error;
552
553         if (likely(pkt->pktinfooffset > 0) &&
554             unlikely(pkt->pktinfooffset < RNDIS_PACKET_MSG_OFFSET_MIN ||
555                      (pkt->pktinfooffset & RNDIS_PACKET_MSG_OFFSET_ALIGNMASK)))
556                 goto error;
557
558         data_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
559         data_len = pkt->datalen;
560         pktinfo_off = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->pktinfooffset);
561         pktinfo_len = pkt->pktinfolen;
562
563         if (likely(pktinfo_len > 0)) {
564                 err = hn_rndis_rxinfo((const uint8_t *)pkt + pktinfo_off,
565                                       pktinfo_len, &info);
566                 if (err)
567                         goto error;
568         }
569
570         if (unlikely(data_off + data_len > pkt->len))
571                 goto error;
572
573         if (unlikely(data_len < ETHER_HDR_LEN))
574                 goto error;
575
576         hn_rxpkt(rxq, rxb, data, data_off, data_len, &info);
577         return;
578 error:
579         ++rxq->stats.errors;
580 }
581
582 static void
583 hn_rndis_receive(const struct rte_eth_dev *dev, struct hn_rx_queue *rxq,
584                  struct hn_rx_bufinfo *rxb, void *buf, uint32_t len)
585 {
586         const struct rndis_msghdr *hdr = buf;
587
588         switch (hdr->type) {
589         case RNDIS_PACKET_MSG:
590                 if (dev->data->dev_started)
591                         hn_rndis_rx_data(rxq, rxb, buf, len);
592                 break;
593
594         case RNDIS_INDICATE_STATUS_MSG:
595                 hn_rndis_link_status(rxq->hv, buf);
596                 break;
597
598         case RNDIS_INITIALIZE_CMPLT:
599         case RNDIS_QUERY_CMPLT:
600         case RNDIS_SET_CMPLT:
601                 hn_rndis_receive_response(rxq->hv, buf, len);
602                 break;
603
604         default:
605                 PMD_DRV_LOG(NOTICE,
606                             "unexpected RNDIS message (type %#x len %u)",
607                             hdr->type, len);
608                 break;
609         }
610 }
611
612 static void
613 hn_nvs_handle_rxbuf(struct rte_eth_dev *dev,
614                     struct hn_data *hv,
615                     struct hn_rx_queue *rxq,
616                     const struct vmbus_chanpkt_hdr *hdr,
617                     const void *buf)
618 {
619         const struct vmbus_chanpkt_rxbuf *pkt;
620         const struct hn_nvs_hdr *nvs_hdr = buf;
621         uint32_t rxbuf_sz = hv->rxbuf_res->len;
622         char *rxbuf = hv->rxbuf_res->addr;
623         unsigned int i, hlen, count;
624         struct hn_rx_bufinfo *rxb;
625
626         /* At minimum we need type header */
627         if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*nvs_hdr))) {
628                 PMD_RX_LOG(ERR, "invalid receive nvs RNDIS");
629                 return;
630         }
631
632         /* Make sure that this is a RNDIS message. */
633         if (unlikely(nvs_hdr->type != NVS_TYPE_RNDIS)) {
634                 PMD_RX_LOG(ERR, "nvs type %u, not RNDIS",
635                            nvs_hdr->type);
636                 return;
637         }
638
639         hlen = vmbus_chanpkt_getlen(hdr->hlen);
640         if (unlikely(hlen < sizeof(*pkt))) {
641                 PMD_RX_LOG(ERR, "invalid rxbuf chanpkt");
642                 return;
643         }
644
645         pkt = container_of(hdr, const struct vmbus_chanpkt_rxbuf, hdr);
646         if (unlikely(pkt->rxbuf_id != NVS_RXBUF_SIG)) {
647                 PMD_RX_LOG(ERR, "invalid rxbuf_id 0x%08x",
648                            pkt->rxbuf_id);
649                 return;
650         }
651
652         count = pkt->rxbuf_cnt;
653         if (unlikely(hlen < offsetof(struct vmbus_chanpkt_rxbuf,
654                                      rxbuf[count]))) {
655                 PMD_RX_LOG(ERR, "invalid rxbuf_cnt %u", count);
656                 return;
657         }
658
659         if (pkt->hdr.xactid > hv->rxbuf_section_cnt) {
660                 PMD_RX_LOG(ERR, "invalid rxbuf section id %" PRIx64,
661                            pkt->hdr.xactid);
662                 return;
663         }
664
665         /* Setup receive buffer info to allow for callback */
666         rxb = hn_rx_buf_init(rxq, pkt);
667
668         /* Each range represents 1 RNDIS pkt that contains 1 Ethernet frame */
669         for (i = 0; i < count; ++i) {
670                 unsigned int ofs, len;
671
672                 ofs = pkt->rxbuf[i].ofs;
673                 len = pkt->rxbuf[i].len;
674
675                 if (unlikely(ofs + len > rxbuf_sz)) {
676                         PMD_RX_LOG(ERR,
677                                    "%uth RNDIS msg overflow ofs %u, len %u",
678                                    i, ofs, len);
679                         continue;
680                 }
681
682                 if (unlikely(len == 0)) {
683                         PMD_RX_LOG(ERR, "%uth RNDIS msg len %u", i, len);
684                         continue;
685                 }
686
687                 hn_rndis_receive(dev, rxq, rxb,
688                                  rxbuf + ofs, len);
689         }
690
691         /* Send ACK now if external mbuf not used */
692         hn_rx_buf_release(rxb);
693 }
694
695 struct hn_rx_queue *hn_rx_queue_alloc(struct hn_data *hv,
696                                       uint16_t queue_id,
697                                       unsigned int socket_id)
698 {
699         struct hn_rx_queue *rxq;
700
701         rxq = rte_zmalloc_socket("HN_RXQ", sizeof(*rxq),
702                                  RTE_CACHE_LINE_SIZE, socket_id);
703         if (rxq) {
704                 rxq->hv = hv;
705                 rxq->chan = hv->channels[queue_id];
706                 rte_spinlock_init(&rxq->ring_lock);
707                 rxq->port_id = hv->port_id;
708                 rxq->queue_id = queue_id;
709
710                 rxq->event_sz = HN_RXQ_EVENT_DEFAULT;
711                 rxq->event_buf = rte_malloc_socket("RX_EVENTS",
712                                                    rxq->event_sz,
713                                                    RTE_CACHE_LINE_SIZE,
714                                                    socket_id);
715                 if (!rxq->event_buf) {
716                         rte_free(rxq);
717                         rxq = NULL;
718                 }
719         }
720         return rxq;
721 }
722
723 int
724 hn_dev_rx_queue_setup(struct rte_eth_dev *dev,
725                       uint16_t queue_idx, uint16_t nb_desc,
726                       unsigned int socket_id,
727                       const struct rte_eth_rxconf *rx_conf __rte_unused,
728                       struct rte_mempool *mp)
729 {
730         struct hn_data *hv = dev->data->dev_private;
731         uint32_t qmax = hv->rxbuf_section_cnt;
732         char ring_name[RTE_RING_NAMESIZE];
733         struct hn_rx_queue *rxq;
734         unsigned int count;
735         size_t size;
736         int err = -ENOMEM;
737
738         PMD_INIT_FUNC_TRACE();
739
740         if (nb_desc == 0 || nb_desc > qmax)
741                 nb_desc = qmax;
742
743         if (queue_idx == 0) {
744                 rxq = hv->primary;
745         } else {
746                 rxq = hn_rx_queue_alloc(hv, queue_idx, socket_id);
747                 if (!rxq)
748                         return -ENOMEM;
749         }
750
751         rxq->mb_pool = mp;
752
753         count = rte_align32pow2(nb_desc);
754         size = sizeof(struct rte_ring) + count * sizeof(void *);
755         rxq->rx_ring = rte_malloc_socket("RX_RING", size,
756                                          RTE_CACHE_LINE_SIZE,
757                                          socket_id);
758         if (!rxq->rx_ring)
759                 goto fail;
760
761         /*
762          * Staging ring from receive event logic to rx_pkts.
763          * rx_pkts assumes caller is handling multi-thread issue.
764          * event logic has locking.
765          */
766         snprintf(ring_name, sizeof(ring_name),
767                  "hn_rx_%u_%u", dev->data->port_id, queue_idx);
768         err = rte_ring_init(rxq->rx_ring, ring_name,
769                             count, 0);
770         if (err)
771                 goto fail;
772
773         dev->data->rx_queues[queue_idx] = rxq;
774         return 0;
775
776 fail:
777         rte_ring_free(rxq->rx_ring);
778         rte_free(rxq->event_buf);
779         rte_free(rxq);
780         return -ENOMEM;
781 }
782
783 void
784 hn_dev_rx_queue_release(void *arg)
785 {
786         struct hn_rx_queue *rxq = arg;
787
788         PMD_INIT_FUNC_TRACE();
789
790         if (!rxq)
791                 return;
792
793         rte_ring_free(rxq->rx_ring);
794         rxq->rx_ring = NULL;
795         rxq->mb_pool = NULL;
796
797         if (rxq != rxq->hv->primary) {
798                 rte_free(rxq->event_buf);
799                 rte_free(rxq);
800         }
801 }
802
803 static void
804 hn_nvs_handle_notify(const struct vmbus_chanpkt_hdr *pkthdr,
805                      const void *data)
806 {
807         const struct hn_nvs_hdr *hdr = data;
808
809         if (unlikely(vmbus_chanpkt_datalen(pkthdr) < sizeof(*hdr))) {
810                 PMD_DRV_LOG(ERR, "invalid nvs notify");
811                 return;
812         }
813
814         PMD_DRV_LOG(INFO,
815                     "got notify, nvs type %u", hdr->type);
816 }
817
818 /*
819  * Process pending events on the channel.
820  * Called from both Rx queue poll and Tx cleanup
821  */
822 void hn_process_events(struct hn_data *hv, uint16_t queue_id)
823 {
824         struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
825         struct hn_rx_queue *rxq;
826         int ret = 0;
827
828         rxq = queue_id == 0 ? hv->primary : dev->data->rx_queues[queue_id];
829
830         /* If no pending data then nothing to do */
831         if (rte_vmbus_chan_rx_empty(rxq->chan))
832                 return;
833
834         /*
835          * Since channel is shared between Rx and TX queue need to have a lock
836          * since DPDK does not force same CPU to be used for Rx/Tx.
837          */
838         if (unlikely(!rte_spinlock_trylock(&rxq->ring_lock)))
839                 return;
840
841         for (;;) {
842                 const struct vmbus_chanpkt_hdr *pkt;
843                 uint32_t len = rxq->event_sz;
844                 const void *data;
845
846                 ret = rte_vmbus_chan_recv_raw(rxq->chan, rxq->event_buf, &len);
847                 if (ret == -EAGAIN)
848                         break;  /* ring is empty */
849
850                 if (ret == -ENOBUFS) {
851                         /* expanded buffer needed */
852                         len = rte_align32pow2(len);
853                         PMD_DRV_LOG(DEBUG, "expand event buf to %u", len);
854
855                         rxq->event_buf = rte_realloc(rxq->event_buf,
856                                                      len, RTE_CACHE_LINE_SIZE);
857                         if (rxq->event_buf) {
858                                 rxq->event_sz = len;
859                                 continue;
860                         }
861
862                         rte_exit(EXIT_FAILURE, "can not expand event buf!\n");
863                         break;
864                 }
865
866                 if (ret != 0) {
867                         PMD_DRV_LOG(ERR, "vmbus ring buffer error: %d", ret);
868                         break;
869                 }
870
871                 pkt = (const struct vmbus_chanpkt_hdr *)rxq->event_buf;
872                 data = (char *)rxq->event_buf + vmbus_chanpkt_getlen(pkt->hlen);
873
874                 switch (pkt->type) {
875                 case VMBUS_CHANPKT_TYPE_COMP:
876                         hn_nvs_handle_comp(dev, queue_id, pkt, data);
877                         break;
878
879                 case VMBUS_CHANPKT_TYPE_RXBUF:
880                         hn_nvs_handle_rxbuf(dev, hv, rxq, pkt, data);
881                         break;
882
883                 case VMBUS_CHANPKT_TYPE_INBAND:
884                         hn_nvs_handle_notify(pkt, data);
885                         break;
886
887                 default:
888                         PMD_DRV_LOG(ERR, "unknown chan pkt %u", pkt->type);
889                         break;
890                 }
891         }
892         rte_spinlock_unlock(&rxq->ring_lock);
893
894         if (unlikely(ret != -EAGAIN))
895                 PMD_DRV_LOG(ERR, "channel receive failed: %d", ret);
896 }
897
898 static void hn_append_to_chim(struct hn_tx_queue *txq,
899                               struct rndis_packet_msg *pkt,
900                               const struct rte_mbuf *m)
901 {
902         struct hn_txdesc *txd = txq->agg_txd;
903         uint8_t *buf = (uint8_t *)pkt;
904         unsigned int data_offs;
905
906         hn_rndis_dump(pkt);
907
908         data_offs = RNDIS_PACKET_MSG_OFFSET_ABS(pkt->dataoffset);
909         txd->chim_size += pkt->len;
910         txd->data_size += m->pkt_len;
911         ++txd->packets;
912         hn_update_packet_stats(&txq->stats, m);
913
914         for (; m; m = m->next) {
915                 uint16_t len = rte_pktmbuf_data_len(m);
916
917                 rte_memcpy(buf + data_offs,
918                            rte_pktmbuf_mtod(m, const char *), len);
919                 data_offs += len;
920         }
921 }
922
923 /*
924  * Send pending aggregated data in chimney buffer (if any).
925  * Returns error if send was unsuccessful because channel ring buffer
926  * was full.
927  */
928 static int hn_flush_txagg(struct hn_tx_queue *txq, bool *need_sig)
929
930 {
931         struct hn_txdesc *txd = txq->agg_txd;
932         struct hn_nvs_rndis rndis;
933         int ret;
934
935         if (!txd)
936                 return 0;
937
938         rndis = (struct hn_nvs_rndis) {
939                 .type = NVS_TYPE_RNDIS,
940                 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
941                 .chim_idx = txd->chim_index,
942                 .chim_sz = txd->chim_size,
943         };
944
945         PMD_TX_LOG(DEBUG, "port %u:%u tx %u size %u",
946                    txq->port_id, txq->queue_id, txd->chim_index, txd->chim_size);
947
948         ret = hn_nvs_send(txq->chan, VMBUS_CHANPKT_FLAG_RC,
949                           &rndis, sizeof(rndis), (uintptr_t)txd, need_sig);
950
951         if (likely(ret == 0))
952                 hn_reset_txagg(txq);
953         else
954                 PMD_TX_LOG(NOTICE, "port %u:%u send failed: %d",
955                            txq->port_id, txq->queue_id, ret);
956
957         return ret;
958 }
959
960 static struct hn_txdesc *hn_new_txd(struct hn_data *hv,
961                                     struct hn_tx_queue *txq)
962 {
963         struct hn_txdesc *txd;
964
965         if (rte_mempool_get(hv->tx_pool, (void **)&txd)) {
966                 ++txq->stats.nomemory;
967                 PMD_TX_LOG(DEBUG, "tx pool exhausted!");
968                 return NULL;
969         }
970
971         txd->m = NULL;
972         txd->queue_id = txq->queue_id;
973         txd->packets = 0;
974         txd->data_size = 0;
975         txd->chim_size = 0;
976
977         return txd;
978 }
979
980 static void *
981 hn_try_txagg(struct hn_data *hv, struct hn_tx_queue *txq, uint32_t pktsize)
982 {
983         struct hn_txdesc *agg_txd = txq->agg_txd;
984         struct rndis_packet_msg *pkt;
985         void *chim;
986
987         if (agg_txd) {
988                 unsigned int padding, olen;
989
990                 /*
991                  * Update the previous RNDIS packet's total length,
992                  * it can be increased due to the mandatory alignment
993                  * padding for this RNDIS packet.  And update the
994                  * aggregating txdesc's chimney sending buffer size
995                  * accordingly.
996                  *
997                  * Zero-out the padding, as required by the RNDIS spec.
998                  */
999                 pkt = txq->agg_prevpkt;
1000                 olen = pkt->len;
1001                 padding = RTE_ALIGN(olen, txq->agg_align) - olen;
1002                 if (padding > 0) {
1003                         agg_txd->chim_size += padding;
1004                         pkt->len += padding;
1005                         memset((uint8_t *)pkt + olen, 0, padding);
1006                 }
1007
1008                 chim = (uint8_t *)pkt + pkt->len;
1009
1010                 txq->agg_pktleft--;
1011                 txq->agg_szleft -= pktsize;
1012                 if (txq->agg_szleft < HN_PKTSIZE_MIN(txq->agg_align)) {
1013                         /*
1014                          * Probably can't aggregate more packets,
1015                          * flush this aggregating txdesc proactively.
1016                          */
1017                         txq->agg_pktleft = 0;
1018                 }
1019         } else {
1020                 agg_txd = hn_new_txd(hv, txq);
1021                 if (!agg_txd)
1022                         return NULL;
1023
1024                 chim = (uint8_t *)hv->chim_res->addr
1025                         + agg_txd->chim_index * hv->chim_szmax;
1026
1027                 txq->agg_txd = agg_txd;
1028                 txq->agg_pktleft = txq->agg_pktmax - 1;
1029                 txq->agg_szleft = txq->agg_szmax - pktsize;
1030         }
1031         txq->agg_prevpkt = chim;
1032
1033         return chim;
1034 }
1035
1036 static inline void *
1037 hn_rndis_pktinfo_append(struct rndis_packet_msg *pkt,
1038                         uint32_t pi_dlen, uint32_t pi_type)
1039 {
1040         const uint32_t pi_size = RNDIS_PKTINFO_SIZE(pi_dlen);
1041         struct rndis_pktinfo *pi;
1042
1043         /*
1044          * Per-packet-info does not move; it only grows.
1045          *
1046          * NOTE:
1047          * pktinfooffset in this phase counts from the beginning
1048          * of rndis_packet_msg.
1049          */
1050         pi = (struct rndis_pktinfo *)((uint8_t *)pkt + hn_rndis_pktlen(pkt));
1051
1052         pkt->pktinfolen += pi_size;
1053
1054         pi->size = pi_size;
1055         pi->type = pi_type;
1056         pi->offset = RNDIS_PKTINFO_OFFSET;
1057
1058         return pi->data;
1059 }
1060
1061 /* Put RNDIS header and packet info on packet */
1062 static void hn_encap(struct rndis_packet_msg *pkt,
1063                      uint16_t queue_id,
1064                      const struct rte_mbuf *m)
1065 {
1066         unsigned int hlen = m->l2_len + m->l3_len;
1067         uint32_t *pi_data;
1068         uint32_t pkt_hlen;
1069
1070         pkt->type = RNDIS_PACKET_MSG;
1071         pkt->len = m->pkt_len;
1072         pkt->dataoffset = 0;
1073         pkt->datalen = m->pkt_len;
1074         pkt->oobdataoffset = 0;
1075         pkt->oobdatalen = 0;
1076         pkt->oobdataelements = 0;
1077         pkt->pktinfooffset = sizeof(*pkt);
1078         pkt->pktinfolen = 0;
1079         pkt->vchandle = 0;
1080         pkt->reserved = 0;
1081
1082         /*
1083          * Set the hash value for this packet, to the queue_id to cause
1084          * TX done event for this packet on the right channel.
1085          */
1086         pi_data = hn_rndis_pktinfo_append(pkt, NDIS_HASH_VALUE_SIZE,
1087                                           NDIS_PKTINFO_TYPE_HASHVAL);
1088         *pi_data = queue_id;
1089
1090         if (m->ol_flags & PKT_TX_VLAN_PKT) {
1091                 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_VLAN_INFO_SIZE,
1092                                                   NDIS_PKTINFO_TYPE_VLAN);
1093                 *pi_data = m->vlan_tci;
1094         }
1095
1096         if (m->ol_flags & PKT_TX_TCP_SEG) {
1097                 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_LSO2_INFO_SIZE,
1098                                                   NDIS_PKTINFO_TYPE_LSO);
1099
1100                 if (m->ol_flags & PKT_TX_IPV6) {
1101                         *pi_data = NDIS_LSO2_INFO_MAKEIPV6(hlen,
1102                                                            m->tso_segsz);
1103                 } else {
1104                         *pi_data = NDIS_LSO2_INFO_MAKEIPV4(hlen,
1105                                                            m->tso_segsz);
1106                 }
1107         } else if (m->ol_flags &
1108                    (PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM)) {
1109                 pi_data = hn_rndis_pktinfo_append(pkt, NDIS_TXCSUM_INFO_SIZE,
1110                                                   NDIS_PKTINFO_TYPE_CSUM);
1111                 *pi_data = 0;
1112
1113                 if (m->ol_flags & PKT_TX_IPV6)
1114                         *pi_data |= NDIS_TXCSUM_INFO_IPV6;
1115                 if (m->ol_flags & PKT_TX_IPV4) {
1116                         *pi_data |= NDIS_TXCSUM_INFO_IPV4;
1117
1118                         if (m->ol_flags & PKT_TX_IP_CKSUM)
1119                                 *pi_data |= NDIS_TXCSUM_INFO_IPCS;
1120                 }
1121
1122                 if (m->ol_flags & PKT_TX_TCP_CKSUM)
1123                         *pi_data |= NDIS_TXCSUM_INFO_MKTCPCS(hlen);
1124                 else if (m->ol_flags & PKT_TX_UDP_CKSUM)
1125                         *pi_data |= NDIS_TXCSUM_INFO_MKUDPCS(hlen);
1126         }
1127
1128         pkt_hlen = pkt->pktinfooffset + pkt->pktinfolen;
1129         /* Fixup RNDIS packet message total length */
1130         pkt->len += pkt_hlen;
1131
1132         /* Convert RNDIS packet message offsets */
1133         pkt->dataoffset = hn_rndis_pktmsg_offset(pkt_hlen);
1134         pkt->pktinfooffset = hn_rndis_pktmsg_offset(pkt->pktinfooffset);
1135 }
1136
1137 /* How many scatter gather list elements ar needed */
1138 static unsigned int hn_get_slots(const struct rte_mbuf *m)
1139 {
1140         unsigned int slots = 1; /* for RNDIS header */
1141
1142         while (m) {
1143                 unsigned int size = rte_pktmbuf_data_len(m);
1144                 unsigned int offs = rte_mbuf_data_iova(m) & PAGE_MASK;
1145
1146                 slots += (offs + size + PAGE_SIZE - 1) / PAGE_SIZE;
1147                 m = m->next;
1148         }
1149
1150         return slots;
1151 }
1152
1153 /* Build scatter gather list from chained mbuf */
1154 static unsigned int hn_fill_sg(struct vmbus_gpa *sg,
1155                                const struct rte_mbuf *m)
1156 {
1157         unsigned int segs = 0;
1158
1159         while (m) {
1160                 rte_iova_t addr = rte_mbuf_data_iova(m);
1161                 unsigned int page = addr / PAGE_SIZE;
1162                 unsigned int offset = addr & PAGE_MASK;
1163                 unsigned int len = rte_pktmbuf_data_len(m);
1164
1165                 while (len > 0) {
1166                         unsigned int bytes = RTE_MIN(len, PAGE_SIZE - offset);
1167
1168                         sg[segs].page = page;
1169                         sg[segs].ofs = offset;
1170                         sg[segs].len = bytes;
1171                         segs++;
1172
1173                         ++page;
1174                         offset = 0;
1175                         len -= bytes;
1176                 }
1177                 m = m->next;
1178         }
1179
1180         return segs;
1181 }
1182
1183 /* Transmit directly from mbuf */
1184 static int hn_xmit_sg(struct hn_tx_queue *txq,
1185                       const struct hn_txdesc *txd, const struct rte_mbuf *m,
1186                       bool *need_sig)
1187 {
1188         struct vmbus_gpa sg[hn_get_slots(m)];
1189         struct hn_nvs_rndis nvs_rndis = {
1190                 .type = NVS_TYPE_RNDIS,
1191                 .rndis_mtype = NVS_RNDIS_MTYPE_DATA,
1192                 .chim_sz = txd->chim_size,
1193         };
1194         rte_iova_t addr;
1195         unsigned int segs;
1196
1197         /* attach aggregation data if present */
1198         if (txd->chim_size > 0)
1199                 nvs_rndis.chim_idx = txd->chim_index;
1200         else
1201                 nvs_rndis.chim_idx = NVS_CHIM_IDX_INVALID;
1202
1203         hn_rndis_dump(txd->rndis_pkt);
1204
1205         /* pass IOVA of rndis header in first segment */
1206         addr = rte_malloc_virt2iova(txd->rndis_pkt);
1207         if (unlikely(addr == RTE_BAD_IOVA)) {
1208                 PMD_DRV_LOG(ERR, "RNDIS transmit can not get iova");
1209                 return -EINVAL;
1210         }
1211
1212         sg[0].page = addr / PAGE_SIZE;
1213         sg[0].ofs = addr & PAGE_MASK;
1214         sg[0].len = RNDIS_PACKET_MSG_OFFSET_ABS(hn_rndis_pktlen(txd->rndis_pkt));
1215         segs = 1;
1216
1217         hn_update_packet_stats(&txq->stats, m);
1218
1219         segs += hn_fill_sg(sg + 1, m);
1220
1221         PMD_TX_LOG(DEBUG, "port %u:%u tx %u segs %u size %u",
1222                    txq->port_id, txq->queue_id, txd->chim_index,
1223                    segs, nvs_rndis.chim_sz);
1224
1225         return hn_nvs_send_sglist(txq->chan, sg, segs,
1226                                   &nvs_rndis, sizeof(nvs_rndis),
1227                                   (uintptr_t)txd, need_sig);
1228 }
1229
1230 uint16_t
1231 hn_xmit_pkts(void *ptxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1232 {
1233         struct hn_tx_queue *txq = ptxq;
1234         struct hn_data *hv = txq->hv;
1235         bool need_sig = false;
1236         uint16_t nb_tx;
1237         int ret;
1238
1239         if (unlikely(hv->closed))
1240                 return 0;
1241
1242         if (rte_mempool_avail_count(hv->tx_pool) <= txq->free_thresh)
1243                 hn_process_events(hv, txq->queue_id);
1244
1245         for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1246                 struct rte_mbuf *m = tx_pkts[nb_tx];
1247                 uint32_t pkt_size = m->pkt_len + HN_RNDIS_PKT_LEN;
1248                 struct rndis_packet_msg *pkt;
1249
1250                 /* For small packets aggregate them in chimney buffer */
1251                 if (m->pkt_len < HN_TXCOPY_THRESHOLD && pkt_size <= txq->agg_szmax) {
1252                         /* If this packet will not fit, then flush  */
1253                         if (txq->agg_pktleft == 0 ||
1254                             RTE_ALIGN(pkt_size, txq->agg_align) > txq->agg_szleft) {
1255                                 if (hn_flush_txagg(txq, &need_sig))
1256                                         goto fail;
1257                         }
1258
1259                         pkt = hn_try_txagg(hv, txq, pkt_size);
1260                         if (unlikely(!pkt))
1261                                 goto fail;
1262
1263                         hn_encap(pkt, txq->queue_id, m);
1264                         hn_append_to_chim(txq, pkt, m);
1265
1266                         rte_pktmbuf_free(m);
1267
1268                         /* if buffer is full, flush */
1269                         if (txq->agg_pktleft == 0 &&
1270                             hn_flush_txagg(txq, &need_sig))
1271                                 goto fail;
1272                 } else {
1273                         struct hn_txdesc *txd;
1274
1275                         /* can send chimney data and large packet at once */
1276                         txd = txq->agg_txd;
1277                         if (txd) {
1278                                 hn_reset_txagg(txq);
1279                         } else {
1280                                 txd = hn_new_txd(hv, txq);
1281                                 if (unlikely(!txd))
1282                                         goto fail;
1283                         }
1284
1285                         pkt = txd->rndis_pkt;
1286                         txd->m = m;
1287                         txd->data_size += m->pkt_len;
1288                         ++txd->packets;
1289
1290                         hn_encap(pkt, txq->queue_id, m);
1291
1292                         ret = hn_xmit_sg(txq, txd, m, &need_sig);
1293                         if (unlikely(ret != 0)) {
1294                                 PMD_TX_LOG(NOTICE, "sg send failed: %d", ret);
1295                                 ++txq->stats.errors;
1296                                 rte_mempool_put(hv->tx_pool, txd);
1297                                 goto fail;
1298                         }
1299                 }
1300         }
1301
1302         /* If partial buffer left, then try and send it.
1303          * if that fails, then reuse it on next send.
1304          */
1305         hn_flush_txagg(txq, &need_sig);
1306
1307 fail:
1308         if (need_sig)
1309                 rte_vmbus_chan_signal_tx(txq->chan);
1310
1311         return nb_tx;
1312 }
1313
1314 uint16_t
1315 hn_recv_pkts(void *prxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1316 {
1317         struct hn_rx_queue *rxq = prxq;
1318         struct hn_data *hv = rxq->hv;
1319
1320         if (unlikely(hv->closed))
1321                 return 0;
1322
1323         /* Get all outstanding receive completions */
1324         hn_process_events(hv, rxq->queue_id);
1325
1326         /* Get mbufs off staging ring */
1327         return rte_ring_sc_dequeue_burst(rxq->rx_ring, (void **)rx_pkts,
1328                                          nb_pkts, NULL);
1329 }