net/netvsc: support VF device hot add/remove
[dpdk.git] / drivers / net / netvsc / hn_var.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2009-2018 Microsoft Corp.
3  * Copyright (c) 2016 Brocade Communications Systems, Inc.
4  * Copyright (c) 2012 NetApp Inc.
5  * Copyright (c) 2012 Citrix Inc.
6  * All rights reserved.
7  */
8
9 /*
10  * Tunable ethdev params
11  */
12 #define HN_MIN_RX_BUF_SIZE      1024
13 #define HN_MAX_XFER_LEN         2048
14 #define HN_MAX_MAC_ADDRS        1
15 #define HN_MAX_CHANNELS         64
16
17 /* Claimed to be 12232B */
18 #define HN_MTU_MAX              (9 * 1024)
19
20 /* Retry interval */
21 #define HN_CHAN_INTERVAL_US     100
22
23 /* Host monitor interval */
24 #define HN_CHAN_LATENCY_NS      50000
25
26 #define HN_TXCOPY_THRESHOLD     512
27 #define HN_RXCOPY_THRESHOLD     256
28
29 #define HN_RX_EXTMBUF_ENABLE    0
30
31 /* Buffers need to be aligned */
32 #ifndef PAGE_SIZE
33 #define PAGE_SIZE 4096
34 #endif
35
36 #ifndef PAGE_MASK
37 #define PAGE_MASK (PAGE_SIZE - 1)
38 #endif
39
40 struct hn_data;
41 struct hn_txdesc;
42
43 struct hn_stats {
44         uint64_t        packets;
45         uint64_t        bytes;
46         uint64_t        errors;
47         uint64_t        ring_full;
48         uint64_t        channel_full;
49         uint64_t        multicast;
50         uint64_t        broadcast;
51         /* Size bins in array as RFC 2819, undersized [0], 64 [1], etc */
52         uint64_t        size_bins[8];
53 };
54
55 struct hn_tx_queue {
56         struct hn_data  *hv;
57         struct vmbus_channel *chan;
58         uint16_t        port_id;
59         uint16_t        queue_id;
60         uint32_t        free_thresh;
61         struct rte_mempool *txdesc_pool;
62         const struct rte_memzone *tx_rndis_mz;
63         void            *tx_rndis;
64         rte_iova_t      tx_rndis_iova;
65
66         /* Applied packet transmission aggregation limits. */
67         uint32_t        agg_szmax;
68         uint32_t        agg_pktmax;
69         uint32_t        agg_align;
70
71         /* Packet transmission aggregation states */
72         struct hn_txdesc *agg_txd;
73         uint32_t        agg_pktleft;
74         uint32_t        agg_szleft;
75         struct rndis_packet_msg *agg_prevpkt;
76
77         struct hn_stats stats;
78 };
79
80 struct hn_rx_queue {
81         struct hn_data  *hv;
82         struct vmbus_channel *chan;
83         struct rte_mempool *mb_pool;
84         struct rte_ring *rx_ring;
85
86         rte_spinlock_t ring_lock;
87         uint32_t event_sz;
88         uint16_t port_id;
89         uint16_t queue_id;
90         struct hn_stats stats;
91
92         void *event_buf;
93         struct hn_rx_bufinfo *rxbuf_info;
94         rte_atomic32_t  rxbuf_outstanding;
95 };
96
97
98 /* multi-packet data from host */
99 struct hn_rx_bufinfo {
100         struct vmbus_channel *chan;
101         struct hn_rx_queue *rxq;
102         uint64_t        xactid;
103         struct rte_mbuf_ext_shared_info shinfo;
104 } __rte_cache_aligned;
105
106 #define HN_INVALID_PORT UINT16_MAX
107
108 enum vf_device_state {
109         vf_unknown = 0,
110         vf_removed,
111         vf_configured,
112         vf_started,
113         vf_stopped,
114 };
115
116 struct hn_vf_ctx {
117         uint16_t        vf_port;
118
119         /* We have taken ownership of this VF port from DPDK */
120         bool            vf_attached;
121
122         /* VSC has requested to switch data path to VF */
123         bool            vf_vsc_switched;
124
125         /* VSP has reported the VF is present for this NIC */
126         bool            vf_vsp_reported;
127
128         enum vf_device_state    vf_state;
129 };
130
131 struct hn_data {
132         struct rte_vmbus_device *vmbus;
133         struct hn_rx_queue *primary;
134         rte_rwlock_t    vf_lock;
135         uint16_t        port_id;
136
137         struct hn_vf_ctx        vf_ctx;
138
139         uint8_t         closed;
140         uint8_t         vlan_strip;
141
142         uint32_t        link_status;
143         uint32_t        link_speed;
144
145         struct rte_mem_resource *rxbuf_res;     /* UIO resource for Rx */
146         uint32_t        rxbuf_section_cnt;      /* # of Rx sections */
147         uint32_t        rx_copybreak;
148         uint32_t        rx_extmbuf_enable;
149         uint16_t        max_queues;             /* Max available queues */
150         uint16_t        num_queues;
151         uint64_t        rss_offloads;
152
153         rte_spinlock_t  chim_lock;
154         struct rte_mem_resource *chim_res;      /* UIO resource for Tx */
155         struct rte_bitmap *chim_bmap;           /* Send buffer map */
156         void            *chim_bmem;
157         uint32_t        tx_copybreak;
158         uint32_t        chim_szmax;             /* Max size per buffer */
159         uint32_t        chim_cnt;               /* Max packets per buffer */
160
161         uint32_t        latency;
162         uint32_t        nvs_ver;
163         uint32_t        ndis_ver;
164         uint32_t        rndis_agg_size;
165         uint32_t        rndis_agg_pkts;
166         uint32_t        rndis_agg_align;
167
168         volatile uint32_t  rndis_pending;
169         rte_atomic32_t  rndis_req_id;
170         uint8_t         rndis_resp[256];
171
172         uint32_t        rss_hash;
173         uint8_t         rss_key[40];
174         uint16_t        rss_ind[128];
175
176         struct rte_eth_dev_owner owner;
177
178         struct vmbus_channel *channels[HN_MAX_CHANNELS];
179
180         struct rte_devargs devargs;
181         int             eal_hot_plug_retry;
182 };
183
184 static inline struct vmbus_channel *
185 hn_primary_chan(const struct hn_data *hv)
186 {
187         return hv->channels[0];
188 }
189
190 uint32_t hn_process_events(struct hn_data *hv, uint16_t queue_id,
191                        uint32_t tx_limit);
192
193 uint16_t hn_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
194                       uint16_t nb_pkts);
195 uint16_t hn_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
196                       uint16_t nb_pkts);
197
198 int     hn_chim_init(struct rte_eth_dev *dev);
199 void    hn_chim_uninit(struct rte_eth_dev *dev);
200 int     hn_dev_link_update(struct rte_eth_dev *dev, int wait);
201 int     hn_dev_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
202                               uint16_t nb_desc, unsigned int socket_id,
203                               const struct rte_eth_txconf *tx_conf);
204 void    hn_dev_tx_queue_release(void *arg);
205 void    hn_dev_tx_queue_info(struct rte_eth_dev *dev, uint16_t queue_idx,
206                              struct rte_eth_txq_info *qinfo);
207 int     hn_dev_tx_done_cleanup(void *arg, uint32_t free_cnt);
208 int     hn_dev_tx_descriptor_status(void *arg, uint16_t offset);
209
210 struct hn_rx_queue *hn_rx_queue_alloc(struct hn_data *hv,
211                                       uint16_t queue_id,
212                                       unsigned int socket_id);
213 int     hn_dev_rx_queue_setup(struct rte_eth_dev *dev,
214                               uint16_t queue_idx, uint16_t nb_desc,
215                               unsigned int socket_id,
216                               const struct rte_eth_rxconf *rx_conf,
217                               struct rte_mempool *mp);
218 void    hn_dev_rx_queue_info(struct rte_eth_dev *dev, uint16_t queue_id,
219                              struct rte_eth_rxq_info *qinfo);
220 void    hn_dev_rx_queue_release(void *arg);
221 uint32_t hn_dev_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_id);
222 int     hn_dev_rx_queue_status(void *rxq, uint16_t offset);
223 void    hn_dev_free_queues(struct rte_eth_dev *dev);
224
225 /*
226  * Get VF device for existing netvsc device
227  * Assumes vf_lock is held.
228  */
229 static inline struct rte_eth_dev *
230 hn_get_vf_dev(const struct hn_data *hv)
231 {
232         if (hv->vf_ctx.vf_attached)
233                 return &rte_eth_devices[hv->vf_ctx.vf_port];
234         else
235                 return NULL;
236 }
237
238 int     hn_vf_info_get(struct hn_data *hv,
239                        struct rte_eth_dev_info *info);
240 int     hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv);
241 int     hn_vf_configure_locked(struct rte_eth_dev *dev,
242                                const struct rte_eth_conf *dev_conf);
243 const uint32_t *hn_vf_supported_ptypes(struct rte_eth_dev *dev);
244 int     hn_vf_start(struct rte_eth_dev *dev);
245 void    hn_vf_reset(struct rte_eth_dev *dev);
246 int     hn_vf_close(struct rte_eth_dev *dev);
247 int     hn_vf_stop(struct rte_eth_dev *dev);
248
249 int     hn_vf_allmulticast_enable(struct rte_eth_dev *dev);
250 int     hn_vf_allmulticast_disable(struct rte_eth_dev *dev);
251 int     hn_vf_promiscuous_enable(struct rte_eth_dev *dev);
252 int     hn_vf_promiscuous_disable(struct rte_eth_dev *dev);
253 int     hn_vf_mc_addr_list(struct rte_eth_dev *dev,
254                            struct rte_ether_addr *mc_addr_set,
255                            uint32_t nb_mc_addr);
256
257 int     hn_vf_tx_queue_setup(struct rte_eth_dev *dev,
258                              uint16_t queue_idx, uint16_t nb_desc,
259                              unsigned int socket_id,
260                              const struct rte_eth_txconf *tx_conf);
261 void    hn_vf_tx_queue_release(struct hn_data *hv, uint16_t queue_id);
262 int     hn_vf_tx_queue_status(struct hn_data *hv, uint16_t queue_id, uint16_t offset);
263
264 int     hn_vf_rx_queue_setup(struct rte_eth_dev *dev,
265                              uint16_t queue_idx, uint16_t nb_desc,
266                              unsigned int socket_id,
267                              const struct rte_eth_rxconf *rx_conf,
268                              struct rte_mempool *mp);
269 void    hn_vf_rx_queue_release(struct hn_data *hv, uint16_t queue_id);
270
271 int     hn_vf_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
272 int     hn_vf_stats_reset(struct rte_eth_dev *dev);
273 int     hn_vf_xstats_get_names(struct rte_eth_dev *dev,
274                                struct rte_eth_xstat_name *xstats_names,
275                                unsigned int size);
276 int     hn_vf_xstats_get(struct rte_eth_dev *dev,
277                          struct rte_eth_xstat *xstats,
278                          unsigned int offset, unsigned int n);
279 int     hn_vf_xstats_reset(struct rte_eth_dev *dev);
280 int     hn_vf_rss_hash_update(struct rte_eth_dev *dev,
281                               struct rte_eth_rss_conf *rss_conf);
282 int     hn_vf_reta_hash_update(struct rte_eth_dev *dev,
283                                struct rte_eth_rss_reta_entry64 *reta_conf,
284                                uint16_t reta_size);
285 int     hn_eth_rmv_event_callback(uint16_t port_id,
286                                   enum rte_eth_event_type event __rte_unused,
287                                   void *cb_arg, void *out __rte_unused);