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