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