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