New upstream version 18.08
[deb_dpdk.git] / drivers / net / netvsc / hn_ethdev.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
13 #include <rte_ethdev.h>
14 #include <rte_memcpy.h>
15 #include <rte_string_fns.h>
16 #include <rte_memzone.h>
17 #include <rte_malloc.h>
18 #include <rte_atomic.h>
19 #include <rte_branch_prediction.h>
20 #include <rte_ether.h>
21 #include <rte_ethdev_driver.h>
22 #include <rte_cycles.h>
23 #include <rte_errno.h>
24 #include <rte_memory.h>
25 #include <rte_eal.h>
26 #include <rte_dev.h>
27 #include <rte_bus_vmbus.h>
28
29 #include "hn_logs.h"
30 #include "hn_var.h"
31 #include "hn_rndis.h"
32 #include "hn_nvs.h"
33 #include "ndis.h"
34
35 #define HN_TX_OFFLOAD_CAPS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
36                             DEV_TX_OFFLOAD_TCP_CKSUM  | \
37                             DEV_TX_OFFLOAD_UDP_CKSUM  | \
38                             DEV_TX_OFFLOAD_TCP_TSO    | \
39                             DEV_TX_OFFLOAD_MULTI_SEGS | \
40                             DEV_TX_OFFLOAD_VLAN_INSERT)
41
42 #define HN_RX_OFFLOAD_CAPS (DEV_RX_OFFLOAD_CHECKSUM | \
43                             DEV_RX_OFFLOAD_VLAN_STRIP | \
44                             DEV_RX_OFFLOAD_CRC_STRIP)
45
46 int hn_logtype_init;
47 int hn_logtype_driver;
48
49 struct hn_xstats_name_off {
50         char name[RTE_ETH_XSTATS_NAME_SIZE];
51         unsigned int offset;
52 };
53
54 static const struct hn_xstats_name_off hn_stat_strings[] = {
55         { "good_packets",           offsetof(struct hn_stats, packets) },
56         { "good_bytes",             offsetof(struct hn_stats, bytes) },
57         { "errors",                 offsetof(struct hn_stats, errors) },
58         { "allocation_failed",      offsetof(struct hn_stats, nomemory) },
59         { "multicast_packets",      offsetof(struct hn_stats, multicast) },
60         { "broadcast_packets",      offsetof(struct hn_stats, broadcast) },
61         { "undersize_packets",      offsetof(struct hn_stats, size_bins[0]) },
62         { "size_64_packets",        offsetof(struct hn_stats, size_bins[1]) },
63         { "size_65_127_packets",    offsetof(struct hn_stats, size_bins[2]) },
64         { "size_128_255_packets",   offsetof(struct hn_stats, size_bins[3]) },
65         { "size_256_511_packets",   offsetof(struct hn_stats, size_bins[4]) },
66         { "size_512_1023_packets",  offsetof(struct hn_stats, size_bins[5]) },
67         { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
68         { "size_1519_max_packets",  offsetof(struct hn_stats, size_bins[7]) },
69 };
70
71 static struct rte_eth_dev *
72 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
73 {
74         struct rte_eth_dev *eth_dev;
75         const char *name;
76
77         if (!dev)
78                 return NULL;
79
80         name = dev->device.name;
81
82         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
83                 eth_dev = rte_eth_dev_allocate(name);
84                 if (!eth_dev) {
85                         PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
86                         return NULL;
87                 }
88
89                 if (private_data_size) {
90                         eth_dev->data->dev_private =
91                                 rte_zmalloc_socket(name, private_data_size,
92                                                      RTE_CACHE_LINE_SIZE, dev->device.numa_node);
93                         if (!eth_dev->data->dev_private) {
94                                 PMD_DRV_LOG(NOTICE, "can not allocate driver data");
95                                 rte_eth_dev_release_port(eth_dev);
96                                 return NULL;
97                         }
98                 }
99         } else {
100                 eth_dev = rte_eth_dev_attach_secondary(name);
101                 if (!eth_dev) {
102                         PMD_DRV_LOG(NOTICE, "can not attach secondary");
103                         return NULL;
104                 }
105         }
106
107         eth_dev->device = &dev->device;
108         eth_dev->intr_handle = &dev->intr_handle;
109
110         return eth_dev;
111 }
112
113 static void
114 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
115 {
116         /* free ether device */
117         rte_eth_dev_release_port(eth_dev);
118
119         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
120                 rte_free(eth_dev->data->dev_private);
121
122         eth_dev->data->dev_private = NULL;
123
124         /*
125          * Secondary process will check the name to attach.
126          * Clear this field to avoid attaching a released ports.
127          */
128         eth_dev->data->name[0] = '\0';
129
130         eth_dev->device = NULL;
131         eth_dev->intr_handle = NULL;
132 }
133
134 /* Update link status.
135  * Note: the DPDK definition of "wait_to_complete"
136  *   means block this call until link is up.
137  *   which is not worth supporting.
138  */
139 static int
140 hn_dev_link_update(struct rte_eth_dev *dev,
141                    __rte_unused int wait_to_complete)
142 {
143         struct hn_data *hv = dev->data->dev_private;
144         struct rte_eth_link link, old;
145         int error;
146
147         old = dev->data->dev_link;
148
149         error = hn_rndis_get_linkstatus(hv);
150         if (error)
151                 return error;
152
153         hn_rndis_get_linkspeed(hv);
154
155         link = (struct rte_eth_link) {
156                 .link_duplex = ETH_LINK_FULL_DUPLEX,
157                 .link_autoneg = ETH_LINK_SPEED_FIXED,
158                 .link_speed = hv->link_speed / 10000,
159         };
160
161         if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
162                 link.link_status = ETH_LINK_UP;
163         else
164                 link.link_status = ETH_LINK_DOWN;
165
166         if (old.link_status == link.link_status)
167                 return 0;
168
169         PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
170                      (link.link_status == ETH_LINK_UP) ? "up" : "down");
171
172         return rte_eth_linkstatus_set(dev, &link);
173 }
174
175 static void hn_dev_info_get(struct rte_eth_dev *dev,
176                             struct rte_eth_dev_info *dev_info)
177 {
178         struct hn_data *hv = dev->data->dev_private;
179
180         dev_info->speed_capa = ETH_LINK_SPEED_10G;
181         dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
182         dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
183         dev_info->max_mac_addrs  = 1;
184
185         dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
186         dev_info->flow_type_rss_offloads =
187                 ETH_RSS_IPV4 | ETH_RSS_IPV6 | ETH_RSS_TCP | ETH_RSS_UDP;
188
189         dev_info->max_rx_queues = hv->max_queues;
190         dev_info->max_tx_queues = hv->max_queues;
191
192         hn_rndis_get_offload(hv, dev_info);
193 }
194
195 static void
196 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
197 {
198         struct hn_data *hv = dev->data->dev_private;
199
200         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
201 }
202
203 static void
204 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
205 {
206         struct hn_data *hv = dev->data->dev_private;
207         uint32_t filter;
208
209         filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
210         if (dev->data->all_multicast)
211                 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
212         hn_rndis_set_rxfilter(hv, filter);
213 }
214
215 static void
216 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
217 {
218         struct hn_data *hv = dev->data->dev_private;
219
220         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
221                               NDIS_PACKET_TYPE_ALL_MULTICAST |
222                         NDIS_PACKET_TYPE_BROADCAST);
223 }
224
225 static void
226 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
227 {
228         struct hn_data *hv = dev->data->dev_private;
229
230         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
231                              NDIS_PACKET_TYPE_BROADCAST);
232 }
233
234 /* Setup shared rx/tx queue data */
235 static int hn_subchan_configure(struct hn_data *hv,
236                                 uint32_t subchan)
237 {
238         struct vmbus_channel *primary = hn_primary_chan(hv);
239         int err;
240         unsigned int retry = 0;
241
242         PMD_DRV_LOG(DEBUG,
243                     "open %u subchannels", subchan);
244
245         /* Send create sub channels command */
246         err = hn_nvs_alloc_subchans(hv, &subchan);
247         if (err)
248                 return  err;
249
250         while (subchan > 0) {
251                 struct vmbus_channel *new_sc;
252                 uint16_t chn_index;
253
254                 err = rte_vmbus_subchan_open(primary, &new_sc);
255                 if (err == -ENOENT && ++retry < 1000) {
256                         /* This can happen if not ready yet */
257                         rte_delay_ms(10);
258                         continue;
259                 }
260
261                 if (err) {
262                         PMD_DRV_LOG(ERR,
263                                     "open subchannel failed: %d", err);
264                         return err;
265                 }
266
267                 retry = 0;
268                 chn_index = rte_vmbus_sub_channel_index(new_sc);
269                 if (chn_index == 0 || chn_index > hv->max_queues) {
270                         PMD_DRV_LOG(ERR,
271                                     "Invalid subchannel offermsg channel %u",
272                                     chn_index);
273                         return -EIO;
274                 }
275
276                 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
277                 hv->channels[chn_index] = new_sc;
278                 --subchan;
279         }
280
281         return err;
282 }
283
284 static int hn_dev_configure(struct rte_eth_dev *dev)
285 {
286         const struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
287         const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
288         const struct rte_eth_txmode *txmode = &dev_conf->txmode;
289
290         const struct rte_eth_rss_conf *rss_conf =
291                 &dev_conf->rx_adv_conf.rss_conf;
292         struct hn_data *hv = dev->data->dev_private;
293         uint64_t unsupported;
294         int err, subchan;
295
296         PMD_INIT_FUNC_TRACE();
297
298         unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
299         if (unsupported) {
300                 PMD_DRV_LOG(NOTICE,
301                             "unsupported TX offload: %#" PRIx64,
302                             unsupported);
303                 return -EINVAL;
304         }
305
306         unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
307         if (unsupported) {
308                 PMD_DRV_LOG(NOTICE,
309                             "unsupported RX offload: %#" PRIx64,
310                             rxmode->offloads);
311                 return -EINVAL;
312         }
313
314         err = hn_rndis_conf_offload(hv, txmode->offloads,
315                                     rxmode->offloads);
316         if (err) {
317                 PMD_DRV_LOG(NOTICE,
318                             "offload configure failed");
319                 return err;
320         }
321
322         hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
323                                  dev->data->nb_tx_queues);
324         subchan = hv->num_queues - 1;
325         if (subchan > 0) {
326                 err = hn_subchan_configure(hv, subchan);
327                 if (err) {
328                         PMD_DRV_LOG(NOTICE,
329                                     "subchannel configuration failed");
330                         return err;
331                 }
332
333                 err = hn_rndis_conf_rss(hv, rss_conf);
334                 if (err) {
335                         PMD_DRV_LOG(NOTICE,
336                                     "rss configuration failed");
337                         return err;
338                 }
339         }
340
341         return 0;
342 }
343
344 static int hn_dev_stats_get(struct rte_eth_dev *dev,
345                             struct rte_eth_stats *stats)
346 {
347         unsigned int i;
348
349         for (i = 0; i < dev->data->nb_tx_queues; i++) {
350                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
351
352                 if (!txq)
353                         continue;
354
355                 stats->opackets += txq->stats.packets;
356                 stats->obytes += txq->stats.bytes;
357                 stats->oerrors += txq->stats.errors + txq->stats.nomemory;
358
359                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
360                         stats->q_opackets[i] = txq->stats.packets;
361                         stats->q_obytes[i] = txq->stats.bytes;
362                 }
363         }
364
365         for (i = 0; i < dev->data->nb_rx_queues; i++) {
366                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
367
368                 if (!rxq)
369                         continue;
370
371                 stats->ipackets += rxq->stats.packets;
372                 stats->ibytes += rxq->stats.bytes;
373                 stats->ierrors += rxq->stats.errors;
374                 stats->imissed += rxq->ring_full;
375
376                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
377                         stats->q_ipackets[i] = rxq->stats.packets;
378                         stats->q_ibytes[i] = rxq->stats.bytes;
379                 }
380         }
381
382         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
383         return 0;
384 }
385
386 static void
387 hn_dev_stats_reset(struct rte_eth_dev *dev)
388 {
389         unsigned int i;
390
391         PMD_INIT_FUNC_TRACE();
392
393         for (i = 0; i < dev->data->nb_tx_queues; i++) {
394                 struct hn_tx_queue *txq = dev->data->tx_queues[i];
395
396                 if (!txq)
397                         continue;
398                 memset(&txq->stats, 0, sizeof(struct hn_stats));
399         }
400
401         for (i = 0; i < dev->data->nb_rx_queues; i++) {
402                 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
403
404                 if (!rxq)
405                         continue;
406
407                 memset(&rxq->stats, 0, sizeof(struct hn_stats));
408                 rxq->ring_full = 0;
409         }
410 }
411
412 static int
413 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
414                         struct rte_eth_xstat_name *xstats_names,
415                         __rte_unused unsigned int limit)
416 {
417         unsigned int i, t, count = 0;
418
419         PMD_INIT_FUNC_TRACE();
420
421         if (!xstats_names)
422                 return dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
423                         + dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
424
425         /* Note: limit checked in rte_eth_xstats_names() */
426         for (i = 0; i < dev->data->nb_tx_queues; i++) {
427                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
428
429                 if (!txq)
430                         continue;
431
432                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
433                         snprintf(xstats_names[count++].name,
434                                  RTE_ETH_XSTATS_NAME_SIZE,
435                                  "tx_q%u_%s", i, hn_stat_strings[t].name);
436         }
437
438         for (i = 0; i < dev->data->nb_rx_queues; i++)  {
439                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
440
441                 if (!rxq)
442                         continue;
443
444                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
445                         snprintf(xstats_names[count++].name,
446                                  RTE_ETH_XSTATS_NAME_SIZE,
447                                  "rx_q%u_%s", i,
448                                  hn_stat_strings[t].name);
449         }
450
451         return count;
452 }
453
454 static int
455 hn_dev_xstats_get(struct rte_eth_dev *dev,
456                   struct rte_eth_xstat *xstats,
457                   unsigned int n)
458 {
459         unsigned int i, t, count = 0;
460
461         const unsigned int nstats =
462                 dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
463                 + dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
464         const char *stats;
465
466         PMD_INIT_FUNC_TRACE();
467
468         if (n < nstats)
469                 return nstats;
470
471         for (i = 0; i < dev->data->nb_tx_queues; i++) {
472                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
473
474                 if (!txq)
475                         continue;
476
477                 stats = (const char *)&txq->stats;
478                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
479                         xstats[count++].value = *(const uint64_t *)
480                                 (stats + hn_stat_strings[t].offset);
481         }
482
483         for (i = 0; i < dev->data->nb_rx_queues; i++) {
484                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
485
486                 if (!rxq)
487                         continue;
488
489                 stats = (const char *)&rxq->stats;
490                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
491                         xstats[count++].value = *(const uint64_t *)
492                                 (stats + hn_stat_strings[t].offset);
493         }
494
495         return count;
496 }
497
498 static int
499 hn_dev_start(struct rte_eth_dev *dev)
500 {
501         struct hn_data *hv = dev->data->dev_private;
502
503         PMD_INIT_FUNC_TRACE();
504
505         /* check if lsc interrupt feature is enabled */
506         if (dev->data->dev_conf.intr_conf.lsc) {
507                 PMD_DRV_LOG(ERR, "link status not supported yet");
508                 return -ENOTSUP;
509         }
510
511         return hn_rndis_set_rxfilter(hv,
512                                      NDIS_PACKET_TYPE_BROADCAST |
513                                      NDIS_PACKET_TYPE_ALL_MULTICAST |
514                                      NDIS_PACKET_TYPE_DIRECTED);
515 }
516
517 static void
518 hn_dev_stop(struct rte_eth_dev *dev)
519 {
520         struct hn_data *hv = dev->data->dev_private;
521
522         PMD_INIT_FUNC_TRACE();
523
524         hn_rndis_set_rxfilter(hv, 0);
525 }
526
527 static void
528 hn_dev_close(struct rte_eth_dev *dev __rte_unused)
529 {
530         PMD_INIT_LOG(DEBUG, "close");
531 }
532
533 static const struct eth_dev_ops hn_eth_dev_ops = {
534         .dev_configure          = hn_dev_configure,
535         .dev_start              = hn_dev_start,
536         .dev_stop               = hn_dev_stop,
537         .dev_close              = hn_dev_close,
538         .dev_infos_get          = hn_dev_info_get,
539         .txq_info_get           = hn_dev_tx_queue_info,
540         .rxq_info_get           = hn_dev_rx_queue_info,
541         .promiscuous_enable     = hn_dev_promiscuous_enable,
542         .promiscuous_disable    = hn_dev_promiscuous_disable,
543         .allmulticast_enable    = hn_dev_allmulticast_enable,
544         .allmulticast_disable   = hn_dev_allmulticast_disable,
545         .tx_queue_setup         = hn_dev_tx_queue_setup,
546         .tx_queue_release       = hn_dev_tx_queue_release,
547         .rx_queue_setup         = hn_dev_rx_queue_setup,
548         .rx_queue_release       = hn_dev_rx_queue_release,
549         .link_update            = hn_dev_link_update,
550         .stats_get              = hn_dev_stats_get,
551         .xstats_get             = hn_dev_xstats_get,
552         .xstats_get_names       = hn_dev_xstats_get_names,
553         .stats_reset            = hn_dev_stats_reset,
554         .xstats_reset           = hn_dev_stats_reset,
555 };
556
557 /*
558  * Setup connection between PMD and kernel.
559  */
560 static int
561 hn_attach(struct hn_data *hv, unsigned int mtu)
562 {
563         int error;
564
565         /* Attach NVS */
566         error = hn_nvs_attach(hv, mtu);
567         if (error)
568                 goto failed_nvs;
569
570         /* Attach RNDIS */
571         error = hn_rndis_attach(hv);
572         if (error)
573                 goto failed_rndis;
574
575         /*
576          * NOTE:
577          * Under certain conditions on certain versions of Hyper-V,
578          * the RNDIS rxfilter is _not_ zero on the hypervisor side
579          * after the successful RNDIS initialization.
580          */
581         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
582         return 0;
583 failed_rndis:
584         hn_nvs_detach(hv);
585 failed_nvs:
586         return error;
587 }
588
589 static void
590 hn_detach(struct hn_data *hv)
591 {
592         hn_nvs_detach(hv);
593         hn_rndis_detach(hv);
594 }
595
596 static int
597 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
598 {
599         struct hn_data *hv = eth_dev->data->dev_private;
600         struct rte_device *device = eth_dev->device;
601         struct rte_vmbus_device *vmbus;
602         unsigned int rxr_cnt;
603         int err, max_chan;
604
605         PMD_INIT_FUNC_TRACE();
606
607         vmbus = container_of(device, struct rte_vmbus_device, device);
608         eth_dev->dev_ops = &hn_eth_dev_ops;
609         eth_dev->tx_pkt_burst = &hn_xmit_pkts;
610         eth_dev->rx_pkt_burst = &hn_recv_pkts;
611
612         /*
613          * for secondary processes, we don't initialize any further as primary
614          * has already done this work.
615          */
616         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
617                 return 0;
618
619         /* Since Hyper-V only supports one MAC address, just use local data */
620         eth_dev->data->mac_addrs = &hv->mac_addr;
621
622         hv->vmbus = vmbus;
623         hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
624         hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
625         hv->port_id = eth_dev->data->port_id;
626
627         /* Initialize primary channel input for control operations */
628         err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
629         if (err)
630                 return err;
631
632         hv->primary = hn_rx_queue_alloc(hv, 0,
633                                         eth_dev->device->numa_node);
634
635         if (!hv->primary)
636                 return -ENOMEM;
637
638         err = hn_attach(hv, ETHER_MTU);
639         if  (err)
640                 goto failed;
641
642         err = hn_tx_pool_init(eth_dev);
643         if (err)
644                 goto failed;
645
646         err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
647         if (err)
648                 goto failed;
649
650         max_chan = rte_vmbus_max_channels(vmbus);
651         PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
652         if (max_chan <= 0)
653                 goto failed;
654
655         if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
656                 rxr_cnt = 1;
657
658         hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
659
660         return 0;
661
662 failed:
663         PMD_INIT_LOG(NOTICE, "device init failed");
664
665         hn_detach(hv);
666         return err;
667 }
668
669 static int
670 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
671 {
672         struct hn_data *hv = eth_dev->data->dev_private;
673
674         PMD_INIT_FUNC_TRACE();
675
676         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
677                 return 0;
678
679         hn_dev_stop(eth_dev);
680         hn_dev_close(eth_dev);
681
682         eth_dev->dev_ops = NULL;
683         eth_dev->tx_pkt_burst = NULL;
684         eth_dev->rx_pkt_burst = NULL;
685
686         hn_detach(hv);
687         rte_vmbus_chan_close(hv->primary->chan);
688         rte_free(hv->primary);
689
690         eth_dev->data->mac_addrs = NULL;
691
692         return 0;
693 }
694
695 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
696                         struct rte_vmbus_device *dev)
697 {
698         struct rte_eth_dev *eth_dev;
699         int ret;
700
701         PMD_INIT_FUNC_TRACE();
702
703         eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
704         if (!eth_dev)
705                 return -ENOMEM;
706
707         ret = eth_hn_dev_init(eth_dev);
708         if (ret)
709                 eth_dev_vmbus_release(eth_dev);
710         else
711                 rte_eth_dev_probing_finish(eth_dev);
712
713         return ret;
714 }
715
716 static int eth_hn_remove(struct rte_vmbus_device *dev)
717 {
718         struct rte_eth_dev *eth_dev;
719         int ret;
720
721         PMD_INIT_FUNC_TRACE();
722
723         eth_dev = rte_eth_dev_allocated(dev->device.name);
724         if (!eth_dev)
725                 return -ENODEV;
726
727         ret = eth_hn_dev_uninit(eth_dev);
728         if (ret)
729                 return ret;
730
731         eth_dev_vmbus_release(eth_dev);
732         return 0;
733 }
734
735 /* Network device GUID */
736 static const rte_uuid_t hn_net_ids[] = {
737         /*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
738         RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
739         { 0 }
740 };
741
742 static struct rte_vmbus_driver rte_netvsc_pmd = {
743         .id_table = hn_net_ids,
744         .probe = eth_hn_probe,
745         .remove = eth_hn_remove,
746 };
747
748 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
749 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
750
751 RTE_INIT(hn_init_log);
752 static void
753 hn_init_log(void)
754 {
755         hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
756         if (hn_logtype_init >= 0)
757                 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
758         hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
759         if (hn_logtype_driver >= 0)
760                 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
761 }