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