blob: faea0be18924a58a0d8b2c52f93239cfac80542f [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
Hank Janssenfceaf242009-07-13 15:34:54 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Hank Janssenfceaf242009-07-13 15:34:54 -070015 *
16 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000017 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070018 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 */
Hank Jansseneb335bc2011-03-29 13:58:48 -070020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Hank Janssenfceaf242009-07-13 15:34:54 -070022#include <linux/init.h>
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -070023#include <linux/atomic.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070024#include <linux/module.h>
25#include <linux/highmem.h>
26#include <linux/device.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070027#include <linux/io.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070028#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
Haiyang Zhangc802db12013-05-28 06:15:56 +000033#include <linux/if_vlan.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070034#include <linux/in.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
stephen hemminger27f5aa92017-07-24 10:57:29 -070036#include <linux/rtnetlink.h>
stephen hemminger0c195562017-08-01 19:58:53 -070037#include <linux/netpoll.h>
Stephen Hemmingera7f99d02017-12-01 11:01:47 -080038#include <linux/reciprocal_div.h>
stephen hemminger27f5aa92017-07-24 10:57:29 -070039
Hank Janssenfceaf242009-07-13 15:34:54 -070040#include <net/arp.h>
41#include <net/route.h>
42#include <net/sock.h>
43#include <net/pkt_sched.h>
Michael Kelley8eb1b3c2017-05-30 11:36:56 -070044#include <net/checksum.h>
45#include <net/ip6_checksum.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070046
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070047#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070048
stephen hemminger8b532792017-08-09 17:46:11 -070049#define RING_SIZE_MIN 64
stephen hemminger8b532792017-08-09 17:46:11 -070050
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +010051#define LINKCHANGE_INT (2 * HZ)
stephen hemminger6123c662017-08-09 17:46:03 -070052#define VF_TAKEOVER_INT (HZ / 10)
stephen hemmingera50af862016-12-06 13:43:54 -080053
Stephen Hemmingera7f99d02017-12-01 11:01:47 -080054static unsigned int ring_size __ro_after_init = 128;
55module_param(ring_size, uint, S_IRUGO);
Stephen Hemminger450d7a42010-05-04 09:58:53 -070056MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Stephen Hemmingera7f99d02017-12-01 11:01:47 -080057unsigned int netvsc_ring_bytes __ro_after_init;
58struct reciprocal_value netvsc_ring_reciprocal __ro_after_init;
Hank Janssenfceaf242009-07-13 15:34:54 -070059
Simon Xiao3f300ff2015-04-28 01:05:17 -070060static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
61 NETIF_MSG_LINK | NETIF_MSG_IFUP |
62 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
63 NETIF_MSG_TX_ERR;
64
65static int debug = -1;
66module_param(debug, int, S_IRUGO);
67MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
68
Stephen Hemmingerbee9d412018-03-02 13:49:09 -080069static void netvsc_change_rx_flags(struct net_device *net, int change)
Hank Janssenfceaf242009-07-13 15:34:54 -070070{
Stephen Hemmingerbee9d412018-03-02 13:49:09 -080071 struct net_device_context *ndev_ctx = netdev_priv(net);
72 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
73 int inc;
74
75 if (!vf_netdev)
76 return;
77
78 if (change & IFF_PROMISC) {
79 inc = (net->flags & IFF_PROMISC) ? 1 : -1;
80 dev_set_promiscuity(vf_netdev, inc);
81 }
82
83 if (change & IFF_ALLMULTI) {
84 inc = (net->flags & IFF_ALLMULTI) ? 1 : -1;
85 dev_set_allmulti(vf_netdev, inc);
86 }
87}
88
89static void netvsc_set_rx_mode(struct net_device *net)
90{
91 struct net_device_context *ndev_ctx = netdev_priv(net);
Stephen Hemminger35a57b72018-03-07 13:49:11 -080092 struct net_device *vf_netdev;
93 struct netvsc_device *nvdev;
Stephen Hemmingerbee9d412018-03-02 13:49:09 -080094
Stephen Hemminger35a57b72018-03-07 13:49:11 -080095 rcu_read_lock();
96 vf_netdev = rcu_dereference(ndev_ctx->vf_netdev);
Stephen Hemmingerbee9d412018-03-02 13:49:09 -080097 if (vf_netdev) {
98 dev_uc_sync(vf_netdev, net);
99 dev_mc_sync(vf_netdev, net);
100 }
Haiyang Zhangd426b2e2011-11-30 07:19:08 -0800101
Stephen Hemminger35a57b72018-03-07 13:49:11 -0800102 nvdev = rcu_dereference(ndev_ctx->nvdev);
103 if (nvdev)
104 rndis_filter_update(nvdev);
105 rcu_read_unlock();
Hank Janssenfceaf242009-07-13 15:34:54 -0700106}
107
Hank Janssenfceaf242009-07-13 15:34:54 -0700108static int netvsc_open(struct net_device *net)
109{
Haiyang Zhang53fa1a62017-06-21 16:40:47 -0700110 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -0700111 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -0700112 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
Haiyang Zhang891de742014-02-12 16:54:27 -0800113 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700114 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700115
Haiyang Zhang891de742014-02-12 16:54:27 -0800116 netif_carrier_off(net);
117
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700118 /* Open up the device */
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200119 ret = rndis_filter_open(nvdev);
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700120 if (ret != 0) {
121 netdev_err(net, "unable to open device (ret %d).\n", ret);
122 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700123 }
124
Haiyang Zhang891de742014-02-12 16:54:27 -0800125 rdev = nvdev->extension;
Stephen Hemmingerf4950e42018-03-02 13:49:02 -0800126 if (!rdev->link_state) {
Haiyang Zhang891de742014-02-12 16:54:27 -0800127 netif_carrier_on(net);
Stephen Hemmingerf4950e42018-03-02 13:49:02 -0800128 netif_tx_wake_all_queues(net);
129 }
Haiyang Zhang891de742014-02-12 16:54:27 -0800130
stephen hemminger0c195562017-08-01 19:58:53 -0700131 if (vf_netdev) {
132 /* Setting synthetic device up transparently sets
133 * slave as up. If open fails, then slave will be
134 * still be offline (and not used).
135 */
136 ret = dev_open(vf_netdev);
137 if (ret)
138 netdev_warn(net,
139 "unable to open slave: %s: %d\n",
140 vf_netdev->name, ret);
141 }
142 return 0;
Hank Janssenfceaf242009-07-13 15:34:54 -0700143}
144
Hank Janssenfceaf242009-07-13 15:34:54 -0700145static int netvsc_close(struct net_device *net)
146{
Hank Janssenfceaf242009-07-13 15:34:54 -0700147 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger0c195562017-08-01 19:58:53 -0700148 struct net_device *vf_netdev
149 = rtnl_dereference(net_device_ctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700150 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Haiyang Zhangc6f71c42017-08-24 11:50:02 -0700151 int ret = 0;
stephen hemminger40975962017-06-08 16:21:19 -0700152 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700153 struct vmbus_channel *chn;
Hank Janssenfceaf242009-07-13 15:34:54 -0700154
Haiyang Zhang0a282532012-02-02 07:17:59 +0000155 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700156
Haiyang Zhangc6f71c42017-08-24 11:50:02 -0700157 /* No need to close rndis filter if it is removed already */
158 if (!nvdev)
159 goto out;
160
Vitaly Kuznetsov2f5fa6c2016-06-03 17:51:00 +0200161 ret = rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700162 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700163 netdev_err(net, "unable to close device (ret %d).\n", ret);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700164 return ret;
165 }
166
167 /* Ensure pending bytes in ring are read */
168 while (true) {
169 aread = 0;
170 for (i = 0; i < nvdev->num_chn; i++) {
stephen hemmingerb8b835a2017-01-24 13:06:07 -0800171 chn = nvdev->chan_table[i].channel;
Haiyang Zhang2de85302015-07-13 13:09:16 -0700172 if (!chn)
173 continue;
174
stephen hemminger40975962017-06-08 16:21:19 -0700175 aread = hv_get_bytes_to_read(&chn->inbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700176 if (aread)
177 break;
178
stephen hemminger40975962017-06-08 16:21:19 -0700179 aread = hv_get_bytes_to_read(&chn->outbound);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700180 if (aread)
181 break;
182 }
183
184 retry++;
185 if (retry > retry_max || aread == 0)
186 break;
187
188 msleep(msec);
189
190 if (msec < 1000)
191 msec *= 2;
192 }
193
194 if (aread) {
195 netdev_err(net, "Ring buffer not empty after closing rndis\n");
196 ret = -ETIMEDOUT;
197 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700198
Haiyang Zhangc6f71c42017-08-24 11:50:02 -0700199out:
stephen hemminger0c195562017-08-01 19:58:53 -0700200 if (vf_netdev)
201 dev_close(vf_netdev);
202
Hank Janssenfceaf242009-07-13 15:34:54 -0700203 return ret;
204}
205
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800206static inline void *init_ppi_data(struct rndis_message *msg,
207 u32 ppi_size, u32 pkt_type)
KY Srinivasan8a002512014-03-08 19:23:14 -0800208{
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800209 struct rndis_packet *rndis_pkt = &msg->msg.pkt;
KY Srinivasan8a002512014-03-08 19:23:14 -0800210 struct rndis_per_packet_info *ppi;
211
KY Srinivasan8a002512014-03-08 19:23:14 -0800212 rndis_pkt->data_offset += ppi_size;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800213 ppi = (void *)rndis_pkt + rndis_pkt->per_pkt_info_offset
214 + rndis_pkt->per_pkt_info_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800215
216 ppi->size = ppi_size;
217 ppi->type = pkt_type;
218 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
219
220 rndis_pkt->per_pkt_info_len += ppi_size;
221
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800222 return ppi + 1;
KY Srinivasan8a002512014-03-08 19:23:14 -0800223}
224
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700225/* Azure hosts don't support non-TCP port numbers in hashing for fragmented
226 * packets. We can use ethtool to change UDP hash level when necessary.
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700227 */
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700228static inline u32 netvsc_get_hash(
229 struct sk_buff *skb,
230 const struct net_device_context *ndc)
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700231{
232 struct flow_keys flow;
Haiyang Zhang486e3982017-10-06 08:33:57 -0700233 u32 hash, pkt_proto = 0;
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700234 static u32 hashrnd __read_mostly;
235
236 net_get_random_once(&hashrnd, sizeof(hashrnd));
237
238 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
239 return 0;
240
Haiyang Zhang486e3982017-10-06 08:33:57 -0700241 switch (flow.basic.ip_proto) {
242 case IPPROTO_TCP:
243 if (flow.basic.n_proto == htons(ETH_P_IP))
244 pkt_proto = HV_TCP4_L4HASH;
245 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
246 pkt_proto = HV_TCP6_L4HASH;
247
248 break;
249
250 case IPPROTO_UDP:
251 if (flow.basic.n_proto == htons(ETH_P_IP))
252 pkt_proto = HV_UDP4_L4HASH;
253 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
254 pkt_proto = HV_UDP6_L4HASH;
255
256 break;
257 }
258
259 if (pkt_proto & ndc->l4_hash) {
Haiyang Zhangf72860a2017-04-12 11:45:18 -0700260 return skb_get_hash(skb);
261 } else {
262 if (flow.basic.n_proto == htons(ETH_P_IP))
263 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
264 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
265 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
266 else
267 hash = 0;
268
269 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
270 }
271
272 return hash;
273}
274
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700275static inline int netvsc_get_tx_queue(struct net_device *ndev,
276 struct sk_buff *skb, int old_idx)
277{
278 const struct net_device_context *ndc = netdev_priv(ndev);
279 struct sock *sk = skb->sk;
280 int q_idx;
281
Haiyang Zhang39e91cf2017-10-13 12:28:04 -0700282 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
283 (VRSS_SEND_TAB_SIZE - 1)];
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700284
285 /* If queue index changed record the new value */
286 if (q_idx != old_idx &&
287 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
288 sk_tx_queue_set(sk, q_idx);
289
290 return q_idx;
291}
292
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800293/*
294 * Select queue for transmit.
295 *
296 * If a valid queue has already been assigned, then use that.
297 * Otherwise compute tx queue based on hash and the send table.
298 *
299 * This is basically similar to default (__netdev_pick_tx) with the added step
300 * of using the host send_table when no other queue has been assigned.
301 *
302 * TODO support XPS - but get_xps_queue not exported
303 */
stephen hemminger0c195562017-08-01 19:58:53 -0700304static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700305{
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700306 int q_idx = sk_tx_queue_get(skb->sk);
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700307
stephen hemminger0c195562017-08-01 19:58:53 -0700308 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
Haiyang Zhang8db91f62017-04-12 11:35:05 -0700309 /* If forwarding a packet, we use the recorded queue when
310 * available for better cache locality.
311 */
312 if (skb_rx_queue_recorded(skb))
313 q_idx = skb_get_rx_queue(skb);
314 else
315 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
stephen hemmingerd8e18ee2017-01-24 13:06:05 -0800316 }
317
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700318 return q_idx;
319}
320
stephen hemminger0c195562017-08-01 19:58:53 -0700321static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
322 void *accel_priv,
323 select_queue_fallback_t fallback)
324{
325 struct net_device_context *ndc = netdev_priv(ndev);
326 struct net_device *vf_netdev;
327 u16 txq;
328
329 rcu_read_lock();
330 vf_netdev = rcu_dereference(ndc->vf_netdev);
331 if (vf_netdev) {
Stephen Hemmingerb3bf5662018-03-02 13:49:07 -0800332 const struct net_device_ops *vf_ops = vf_netdev->netdev_ops;
333
334 if (vf_ops->ndo_select_queue)
335 txq = vf_ops->ndo_select_queue(vf_netdev, skb,
336 accel_priv, fallback);
337 else
338 txq = fallback(vf_netdev, skb);
339
340 /* Record the queue selected by VF so that it can be
341 * used for common case where VF has more queues than
342 * the synthetic device.
343 */
344 qdisc_skb_cb(skb)->slave_dev_queue_mapping = txq;
stephen hemminger0c195562017-08-01 19:58:53 -0700345 } else {
346 txq = netvsc_pick_tx(ndev, skb);
347 }
348 rcu_read_unlock();
349
350 while (unlikely(txq >= ndev->real_num_tx_queues))
351 txq -= ndev->real_num_tx_queues;
352
353 return txq;
354}
355
KY Srinivasan54a73572014-03-08 19:23:13 -0800356static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
stephen hemminger89bb42b2017-08-09 17:46:08 -0700357 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800358{
359 int j = 0;
360
361 /* Deal with compund pages by ignoring unused part
362 * of the page.
363 */
364 page += (offset >> PAGE_SHIFT);
365 offset &= ~PAGE_MASK;
366
367 while (len > 0) {
368 unsigned long bytes;
369
370 bytes = PAGE_SIZE - offset;
371 if (bytes > len)
372 bytes = len;
373 pb[j].pfn = page_to_pfn(page);
374 pb[j].offset = offset;
375 pb[j].len = bytes;
376
377 offset += bytes;
378 len -= bytes;
379
380 if (offset == PAGE_SIZE && len) {
381 page++;
382 offset = 0;
383 j++;
384 }
385 }
386
387 return j + 1;
388}
389
KY Srinivasan8a002512014-03-08 19:23:14 -0800390static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
KY Srinivasana9f2e2d2015-12-01 16:43:13 -0800391 struct hv_netvsc_packet *packet,
stephen hemminger02b6de02017-07-28 08:59:44 -0700392 struct hv_page_buffer *pb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800393{
394 u32 slots_used = 0;
395 char *data = skb->data;
396 int frags = skb_shinfo(skb)->nr_frags;
397 int i;
398
399 /* The packet is laid out thus:
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700400 * 1. hdr: RNDIS header and PPI
KY Srinivasan54a73572014-03-08 19:23:13 -0800401 * 2. skb linear data
402 * 3. skb fragment data
403 */
stephen hemmingerea5a32c2017-08-09 17:46:10 -0700404 slots_used += fill_pg_buf(virt_to_page(hdr),
405 offset_in_page(hdr),
406 len, &pb[slots_used]);
KY Srinivasan54a73572014-03-08 19:23:13 -0800407
Haiyang Zhangaa0a34b2015-04-13 16:34:35 -0700408 packet->rmsg_size = len;
409 packet->rmsg_pgcnt = slots_used;
410
KY Srinivasan54a73572014-03-08 19:23:13 -0800411 slots_used += fill_pg_buf(virt_to_page(data),
412 offset_in_page(data),
413 skb_headlen(skb), &pb[slots_used]);
414
415 for (i = 0; i < frags; i++) {
416 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
417
418 slots_used += fill_pg_buf(skb_frag_page(frag),
419 frag->page_offset,
420 skb_frag_size(frag), &pb[slots_used]);
421 }
KY Srinivasan8a002512014-03-08 19:23:14 -0800422 return slots_used;
KY Srinivasan54a73572014-03-08 19:23:13 -0800423}
424
stephen hemminger80d887d2017-07-24 21:03:19 -0700425static int count_skb_frag_slots(struct sk_buff *skb)
KY Srinivasan54a73572014-03-08 19:23:13 -0800426{
stephen hemminger80d887d2017-07-24 21:03:19 -0700427 int i, frags = skb_shinfo(skb)->nr_frags;
428 int pages = 0;
429
430 for (i = 0; i < frags; i++) {
431 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
432 unsigned long size = skb_frag_size(frag);
433 unsigned long offset = frag->page_offset;
434
435 /* Skip unused frames from start of page */
436 offset &= ~PAGE_MASK;
437 pages += PFN_UP(offset + size);
438 }
439 return pages;
440}
441
442static int netvsc_get_slots(struct sk_buff *skb)
443{
444 char *data = skb->data;
445 unsigned int offset = offset_in_page(data);
446 unsigned int len = skb_headlen(skb);
447 int slots;
448 int frag_slots;
449
450 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
451 frag_slots = count_skb_frag_slots(skb);
452 return slots + frag_slots;
KY Srinivasan54a73572014-03-08 19:23:13 -0800453}
454
stephen hemminger23312a32017-01-24 13:05:59 -0800455static u32 net_checksum_info(struct sk_buff *skb)
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800456{
stephen hemminger23312a32017-01-24 13:05:59 -0800457 if (skb->protocol == htons(ETH_P_IP)) {
458 struct iphdr *ip = ip_hdr(skb);
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800459
stephen hemminger23312a32017-01-24 13:05:59 -0800460 if (ip->protocol == IPPROTO_TCP)
461 return TRANSPORT_INFO_IPV4_TCP;
462 else if (ip->protocol == IPPROTO_UDP)
463 return TRANSPORT_INFO_IPV4_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800464 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800465 struct ipv6hdr *ip6 = ipv6_hdr(skb);
466
467 if (ip6->nexthdr == IPPROTO_TCP)
468 return TRANSPORT_INFO_IPV6_TCP;
Mohammed Gamal37b9dfa2017-07-24 10:57:26 -0700469 else if (ip6->nexthdr == IPPROTO_UDP)
stephen hemminger23312a32017-01-24 13:05:59 -0800470 return TRANSPORT_INFO_IPV6_UDP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800471 }
472
stephen hemminger23312a32017-01-24 13:05:59 -0800473 return TRANSPORT_INFO_NOT_IP;
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800474}
475
stephen hemminger0c195562017-08-01 19:58:53 -0700476/* Send skb on the slave VF device. */
477static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
478 struct sk_buff *skb)
479{
480 struct net_device_context *ndev_ctx = netdev_priv(net);
481 unsigned int len = skb->len;
482 int rc;
483
484 skb->dev = vf_netdev;
485 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
486
487 rc = dev_queue_xmit(skb);
488 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
489 struct netvsc_vf_pcpu_stats *pcpu_stats
490 = this_cpu_ptr(ndev_ctx->vf_stats);
491
492 u64_stats_update_begin(&pcpu_stats->syncp);
493 pcpu_stats->tx_packets++;
494 pcpu_stats->tx_bytes += len;
495 u64_stats_update_end(&pcpu_stats->syncp);
496 } else {
497 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
498 }
499
500 return rc;
501}
502
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700503static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700504{
Hank Janssenfceaf242009-07-13 15:34:54 -0700505 struct net_device_context *net_device_ctx = netdev_priv(net);
Vitaly Kuznetsov981a1bd2015-04-08 17:54:05 +0200506 struct hv_netvsc_packet *packet = NULL;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700507 int ret;
KY Srinivasan8a002512014-03-08 19:23:14 -0800508 unsigned int num_data_pgs;
509 struct rndis_message *rndis_msg;
stephen hemminger0c195562017-08-01 19:58:53 -0700510 struct net_device *vf_netdev;
KY Srinivasan8a002512014-03-08 19:23:14 -0800511 u32 rndis_msg_size;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700512 u32 hash;
stephen hemminger02b6de02017-07-28 08:59:44 -0700513 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
Hank Janssenfceaf242009-07-13 15:34:54 -0700514
stephen hemminger0c195562017-08-01 19:58:53 -0700515 /* if VF is present and up then redirect packets
516 * already called with rcu_read_lock_bh
517 */
518 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
519 if (vf_netdev && netif_running(vf_netdev) &&
520 !netpoll_tx_running(net))
521 return netvsc_vf_xmit(net, vf_netdev, skb);
522
stephen hemminger80d887d2017-07-24 21:03:19 -0700523 /* We will atmost need two pages to describe the rndis
524 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
Vitaly Kuznetsove88f7e02015-04-08 17:54:06 +0200525 * of pages in a single packet. If skb is scattered around
526 * more pages we try linearizing it.
KY Srinivasan54a73572014-03-08 19:23:13 -0800527 */
stephen hemminger80d887d2017-07-24 21:03:19 -0700528
529 num_data_pgs = netvsc_get_slots(skb) + 2;
530
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700531 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700532 ++net_device_ctx->eth_stats.tx_scattered;
533
534 if (skb_linearize(skb))
535 goto no_memory;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700536
stephen hemminger80d887d2017-07-24 21:03:19 -0700537 num_data_pgs = netvsc_get_slots(skb) + 2;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700538 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
Stephen Hemminger4323b472016-08-23 12:17:57 -0700539 ++net_device_ctx->eth_stats.tx_too_big;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700540 goto drop;
541 }
KY Srinivasan54a73572014-03-08 19:23:13 -0800542 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700543
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800544 /*
545 * Place the rndis header in the skb head room and
546 * the skb->cb will be used for hv_netvsc_packet
547 * structure.
548 */
549 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
Stephen Hemminger4323b472016-08-23 12:17:57 -0700550 if (ret)
551 goto no_memory;
552
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800553 /* Use the skb control buffer for building up the packet */
554 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
555 FIELD_SIZEOF(struct sk_buff, cb));
556 packet = (struct hv_netvsc_packet *)skb->cb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700557
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700558 packet->q_idx = skb_get_queue_mapping(skb);
559
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800560 packet->total_data_buflen = skb->len;
stephen hemminger793e3952017-01-24 13:06:12 -0800561 packet->total_bytes = skb->len;
562 packet->total_packets = 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700563
KY Srinivasanc0eb4542015-12-01 16:43:10 -0800564 rndis_msg = (struct rndis_message *)skb->head;
KY Srinivasanb08cc792015-03-29 21:08:42 -0700565
KY Srinivasan8a002512014-03-08 19:23:14 -0800566 /* Add the rndis header */
KY Srinivasan8a002512014-03-08 19:23:14 -0800567 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
568 rndis_msg->msg_len = packet->total_data_buflen;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800569
570 rndis_msg->msg.pkt = (struct rndis_packet) {
571 .data_offset = sizeof(struct rndis_packet),
572 .data_len = packet->total_data_buflen,
573 .per_pkt_info_offset = sizeof(struct rndis_packet),
574 };
KY Srinivasan8a002512014-03-08 19:23:14 -0800575
576 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
577
Haiyang Zhang307f0992014-05-21 12:55:39 -0700578 hash = skb_get_hash_raw(skb);
579 if (hash != 0 && net->real_num_tx_queues > 1) {
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800580 u32 *hash_info;
581
Haiyang Zhang307f0992014-05-21 12:55:39 -0700582 rndis_msg_size += NDIS_HASH_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800583 hash_info = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
584 NBL_HASH_VALUE);
585 *hash_info = hash;
Haiyang Zhang307f0992014-05-21 12:55:39 -0700586 }
587
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700588 if (skb_vlan_tag_present(skb)) {
KY Srinivasan8a002512014-03-08 19:23:14 -0800589 struct ndis_pkt_8021q_info *vlan;
590
591 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800592 vlan = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
593 IEEE_8021Q_INFO);
stephen hemminger00f50242017-08-09 17:46:09 -0700594
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800595 vlan->value = 0;
KY Srinivasan760d1e32015-12-01 16:43:19 -0800596 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
597 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
KY Srinivasan8a002512014-03-08 19:23:14 -0800598 VLAN_PRIO_SHIFT;
599 }
600
stephen hemminger23312a32017-01-24 13:05:59 -0800601 if (skb_is_gso(skb)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700602 struct ndis_tcp_lso_info *lso_info;
603
604 rndis_msg_size += NDIS_LSO_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800605 lso_info = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
606 TCP_LARGESEND_PKTINFO);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700607
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800608 lso_info->value = 0;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700609 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
stephen hemminger23312a32017-01-24 13:05:59 -0800610 if (skb->protocol == htons(ETH_P_IP)) {
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700611 lso_info->lso_v2_transmit.ip_version =
612 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
613 ip_hdr(skb)->tot_len = 0;
614 ip_hdr(skb)->check = 0;
615 tcp_hdr(skb)->check =
616 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
617 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
618 } else {
619 lso_info->lso_v2_transmit.ip_version =
620 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
621 ipv6_hdr(skb)->payload_len = 0;
622 tcp_hdr(skb)->check =
623 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
624 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
625 }
stephen hemminger23312a32017-01-24 13:05:59 -0800626 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700627 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
stephen hemmingerad19bc82016-10-11 14:03:07 -0700628 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
stephen hemminger23312a32017-01-24 13:05:59 -0800629 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
630 struct ndis_tcp_ip_checksum_info *csum_info;
631
stephen hemmingerad19bc82016-10-11 14:03:07 -0700632 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800633 csum_info = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
634 TCPIP_CHKSUM_PKTINFO);
stephen hemmingerad19bc82016-10-11 14:03:07 -0700635
Stephen Hemmingerf5a22552017-12-01 11:01:48 -0800636 csum_info->value = 0;
stephen hemminger23312a32017-01-24 13:05:59 -0800637 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
638
639 if (skb->protocol == htons(ETH_P_IP)) {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700640 csum_info->transmit.is_ipv4 = 1;
stephen hemminger23312a32017-01-24 13:05:59 -0800641
642 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
643 csum_info->transmit.tcp_checksum = 1;
644 else
645 csum_info->transmit.udp_checksum = 1;
646 } else {
stephen hemmingerad19bc82016-10-11 14:03:07 -0700647 csum_info->transmit.is_ipv6 = 1;
648
stephen hemminger23312a32017-01-24 13:05:59 -0800649 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
650 csum_info->transmit.tcp_checksum = 1;
651 else
652 csum_info->transmit.udp_checksum = 1;
653 }
stephen hemmingerad19bc82016-10-11 14:03:07 -0700654 } else {
stephen hemminger23312a32017-01-24 13:05:59 -0800655 /* Can't do offload of this type of checksum */
stephen hemmingerad19bc82016-10-11 14:03:07 -0700656 if (skb_checksum_help(skb))
657 goto drop;
658 }
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700659 }
KY Srinivasan08cd04b2014-03-08 19:23:17 -0800660
KY Srinivasan8a002512014-03-08 19:23:14 -0800661 /* Start filling in the page buffers with the rndis hdr */
662 rndis_msg->msg_len += rndis_msg_size;
Haiyang Zhang942396b2014-10-22 13:47:18 -0700663 packet->total_data_buflen = rndis_msg->msg_len;
KY Srinivasan8a002512014-03-08 19:23:14 -0800664 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
stephen hemminger02b6de02017-07-28 08:59:44 -0700665 skb, packet, pb);
KY Srinivasan8a002512014-03-08 19:23:14 -0800666
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -0800667 /* timestamp packet in software */
668 skb_tx_timestamp(skb);
stephen hemminger2a926f72017-07-19 11:53:17 -0700669
Stephen Hemmingercfd8afd2017-12-12 16:48:40 -0800670 ret = netvsc_send(net, packet, rndis_msg, pb, skb);
stephen hemminger793e3952017-01-24 13:06:12 -0800671 if (likely(ret == 0))
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700672 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700673
674 if (ret == -EAGAIN) {
675 ++net_device_ctx->eth_stats.tx_busy;
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700676 return NETDEV_TX_BUSY;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700677 }
678
679 if (ret == -ENOSPC)
680 ++net_device_ctx->eth_stats.tx_no_space;
Hank Janssenfceaf242009-07-13 15:34:54 -0700681
Stephen Hemminger0ab05142016-08-23 12:17:52 -0700682drop:
683 dev_kfree_skb_any(skb);
684 net->stats.tx_dropped++;
685
686 return NETDEV_TX_OK;
Stephen Hemminger4323b472016-08-23 12:17:57 -0700687
688no_memory:
689 ++net_device_ctx->eth_stats.tx_no_memory;
690 goto drop;
Hank Janssenfceaf242009-07-13 15:34:54 -0700691}
stephen hemminger89bb42b2017-08-09 17:46:08 -0700692
Hank Janssen3e189512010-03-04 22:11:00 +0000693/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700694 * netvsc_linkstatus_callback - Link up/down notification
695 */
Stephen Hemminger79cf1ba2017-12-12 16:48:37 -0800696void netvsc_linkstatus_callback(struct net_device *net,
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700697 struct rndis_message *resp)
Hank Janssenfceaf242009-07-13 15:34:54 -0700698{
Haiyang Zhang3a494e72014-06-19 18:34:36 -0700699 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
Stephen Hemminger79cf1ba2017-12-12 16:48:37 -0800700 struct net_device_context *ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100701 struct netvsc_reconfig *event;
702 unsigned long flags;
703
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700704 /* Update the physical link speed when changing to another vSwitch */
705 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
706 u32 speed;
707
stephen hemminger89bb42b2017-08-09 17:46:08 -0700708 speed = *(u32 *)((void *)indicate
709 + indicate->status_buf_offset) / 10000;
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700710 ndev_ctx->speed = speed;
711 return;
712 }
713
714 /* Handle these link change statuses below */
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100715 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
716 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
717 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
718 return;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700719
Haiyang Zhang7f5d5af2016-08-04 10:42:15 -0700720 if (net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700721 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700722
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +0100723 event = kzalloc(sizeof(*event), GFP_ATOMIC);
724 if (!event)
725 return;
726 event->event = indicate->status;
727
728 spin_lock_irqsave(&ndev_ctx->lock, flags);
729 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
730 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
731
732 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700733}
734
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700735static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800736 struct napi_struct *napi,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800737 const struct ndis_tcp_ip_checksum_info *csum_info,
738 const struct ndis_pkt_8021q_info *vlan,
739 void *data, u32 buflen)
Hank Janssenfceaf242009-07-13 15:34:54 -0700740{
Hank Janssenfceaf242009-07-13 15:34:54 -0700741 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -0700742
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800743 skb = napi_alloc_skb(napi, buflen);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700744 if (!skb)
745 return skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700746
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700747 /*
748 * Copy to skb. This copy is needed here since the memory pointed by
749 * hv_netvsc_packet cannot be deallocated
750 */
Johannes Berg59ae1d12017-06-16 14:29:20 +0200751 skb_put_data(skb, data, buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700752
753 skb->protocol = eth_type_trans(skb, net);
Stephen Hemmingere52fed72016-10-23 21:32:47 -0700754
755 /* skb is already created with CHECKSUM_NONE */
756 skb_checksum_none_assert(skb);
757
758 /*
759 * In Linux, the IP checksum is always checked.
760 * Do L4 checksum offload if enabled and present.
761 */
762 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
763 if (csum_info->receive.tcp_checksum_succeeded ||
764 csum_info->receive.udp_checksum_succeeded)
KY Srinivasane3d605e2014-03-08 19:23:16 -0800765 skb->ip_summed = CHECKSUM_UNNECESSARY;
KY Srinivasane3d605e2014-03-08 19:23:16 -0800766 }
767
stephen hemmingerdc54a082017-01-24 13:06:08 -0800768 if (vlan) {
769 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
770
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700771 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
KY Srinivasan760d1e32015-12-01 16:43:19 -0800772 vlan_tci);
stephen hemmingerdc54a082017-01-24 13:06:08 -0800773 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700774
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700775 return skb;
776}
777
778/*
779 * netvsc_recv_callback - Callback when we receive a packet from the
780 * "wire" on the specified device.
781 */
stephen hemmingerdc54a082017-01-24 13:06:08 -0800782int netvsc_recv_callback(struct net_device *net,
Stephen Hemminger345ac082017-12-12 16:48:38 -0800783 struct netvsc_device *net_device,
stephen hemmingerdc54a082017-01-24 13:06:08 -0800784 struct vmbus_channel *channel,
785 void *data, u32 len,
786 const struct ndis_tcp_ip_checksum_info *csum_info,
787 const struct ndis_pkt_8021q_info *vlan)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700788{
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200789 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger742fe542017-02-27 10:26:50 -0800790 u16 q_idx = channel->offermsg.offer.sub_channel_index;
Stephen Hemminger345ac082017-12-12 16:48:38 -0800791 struct netvsc_channel *nvchan = &net_device->chan_table[q_idx];
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700792 struct sk_buff *skb;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700793 struct netvsc_stats *rx_stats;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700794
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700795 if (net->reg_state != NETREG_REGISTERED)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700796 return NVSP_STAT_FAIL;
797
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700798 /* Allocate a skb - TODO direct I/O to pages? */
stephen hemmingere91e7dd2017-02-27 10:26:51 -0800799 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
800 csum_info, vlan, data, len);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700801 if (unlikely(!skb)) {
Stephen Hemmingerf61a9d62017-12-12 16:48:36 -0800802 ++net_device_ctx->eth_stats.rx_no_memory;
stephen hemminger0719e722017-01-11 09:16:32 -0800803 rcu_read_unlock();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -0700804 return NVSP_STAT_FAIL;
805 }
Haiyang Zhang5b54dac2014-04-21 10:20:28 -0700806
stephen hemminger0c195562017-08-01 19:58:53 -0700807 skb_record_rx_queue(skb, q_idx);
Stephen Hemminger9cbcc422016-09-22 16:56:34 -0700808
809 /*
810 * Even if injecting the packet, record the statistics
811 * on the synthetic device because modifying the VF device
812 * statistics will not work correctly.
813 */
stephen hemminger742fe542017-02-27 10:26:50 -0800814 rx_stats = &nvchan->rx_stats;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700815 u64_stats_update_begin(&rx_stats->syncp);
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -0700816 rx_stats->packets++;
stephen hemmingerdc54a082017-01-24 13:06:08 -0800817 rx_stats->bytes += len;
Stephen Hemmingerf7ad75b2016-09-22 16:56:35 -0700818
819 if (skb->pkt_type == PACKET_BROADCAST)
820 ++rx_stats->broadcast;
821 else if (skb->pkt_type == PACKET_MULTICAST)
822 ++rx_stats->multicast;
sixiao@microsoft.com4b02b582015-05-15 02:33:03 -0700823 u64_stats_update_end(&rx_stats->syncp);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800824
stephen hemminger742fe542017-02-27 10:26:50 -0800825 napi_gro_receive(&nvchan->napi, skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700826 return 0;
827}
828
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700829static void netvsc_get_drvinfo(struct net_device *net,
830 struct ethtool_drvinfo *info)
831{
Jiri Pirko7826d432013-01-06 00:44:26 +0000832 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000833 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700834}
835
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800836static void netvsc_get_channels(struct net_device *net,
837 struct ethtool_channels *channel)
838{
839 struct net_device_context *net_device_ctx = netdev_priv(net);
stephen hemminger545a8e72017-03-22 14:51:00 -0700840 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -0800841
842 if (nvdev) {
843 channel->max_combined = nvdev->max_chn;
844 channel->combined_count = nvdev->num_chn;
845 }
846}
847
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700848static int netvsc_set_channels(struct net_device *net,
849 struct ethtool_channels *channels)
850{
851 struct net_device_context *net_device_ctx = netdev_priv(net);
852 struct hv_device *dev = net_device_ctx->device_ctx;
stephen hemminger545a8e72017-03-22 14:51:00 -0700853 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700854 unsigned int orig, count = channels->combined_count;
855 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700856 bool was_opened;
stephen hemminger7ca45932017-07-24 10:57:28 -0700857 int ret = 0;
stephen hemminger2b018882017-01-24 13:06:03 -0800858
859 /* We do not support separate count for rx, tx, or other */
860 if (count == 0 ||
861 channels->rx_count || channels->tx_count || channels->other_count)
862 return -EINVAL;
863
stephen hemmingera0be4502017-03-22 14:51:01 -0700864 if (!nvdev || nvdev->destroy)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700865 return -ENODEV;
866
stephen hemminger2b018882017-01-24 13:06:03 -0800867 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700868 return -EINVAL;
869
stephen hemminger2b018882017-01-24 13:06:03 -0800870 if (count > nvdev->max_chn)
871 return -EINVAL;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700872
stephen hemminger7ca45932017-07-24 10:57:28 -0700873 orig = nvdev->num_chn;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700874 was_opened = rndis_filter_opened(nvdev);
875 if (was_opened)
876 rndis_filter_close(nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700877
stephen hemminger7ca45932017-07-24 10:57:28 -0700878 memset(&device_info, 0, sizeof(device_info));
879 device_info.num_chn = count;
stephen hemminger8b532792017-08-09 17:46:11 -0700880 device_info.send_sections = nvdev->send_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700881 device_info.send_section_size = nvdev->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700882 device_info.recv_sections = nvdev->recv_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700883 device_info.recv_section_size = nvdev->recv_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700884
885 rndis_filter_device_remove(dev, nvdev);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700886
stephen hemminger7ca45932017-07-24 10:57:28 -0700887 nvdev = rndis_filter_device_add(dev, &device_info);
Stephen Hemminger8195b132017-09-06 13:53:05 -0700888 if (IS_ERR(nvdev)) {
stephen hemmingerd6aac1f2017-07-28 08:59:41 -0700889 ret = PTR_ERR(nvdev);
stephen hemminger7ca45932017-07-24 10:57:28 -0700890 device_info.num_chn = orig;
stephen hemminger68d715f2017-08-09 17:46:06 -0700891 nvdev = rndis_filter_device_add(dev, &device_info);
892
893 if (IS_ERR(nvdev)) {
894 netdev_err(net, "restoring channel setting failed: %ld\n",
895 PTR_ERR(nvdev));
896 return ret;
897 }
stephen hemminger7ca45932017-07-24 10:57:28 -0700898 }
899
stephen hemmingerea383bf2017-07-19 11:53:15 -0700900 if (was_opened)
901 rndis_filter_open(nvdev);
stephen hemminger163891d2017-03-22 14:50:58 -0700902
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200903 /* We may have missed link change notifications */
stephen hemminger1b019942017-07-19 11:53:12 -0700904 net_device_ctx->last_reconfig = 0;
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +0200905 schedule_delayed_work(&net_device_ctx->dwork, 0);
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700906
907 return ret;
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -0700908}
909
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100910static bool
911netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800912{
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100913 struct ethtool_link_ksettings diff1 = *cmd;
914 struct ethtool_link_ksettings diff2 = {};
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800915
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100916 diff1.base.speed = 0;
917 diff1.base.duplex = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800918 /* advertising and cmd are usually set */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100919 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
920 diff1.base.cmd = 0;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800921 /* We set port to PORT_OTHER */
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100922 diff2.base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800923
924 return !memcmp(&diff1, &diff2, sizeof(diff1));
925}
926
927static void netvsc_init_settings(struct net_device *dev)
928{
929 struct net_device_context *ndc = netdev_priv(dev);
930
Haiyang Zhang486e3982017-10-06 08:33:57 -0700931 ndc->l4_hash = HV_DEFAULT_L4HASH;
Haiyang Zhang4823eb22017-08-21 19:22:39 -0700932
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800933 ndc->speed = SPEED_UNKNOWN;
Simon Xiaof3c9d40e2017-04-14 14:42:58 -0700934 ndc->duplex = DUPLEX_FULL;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800935}
936
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100937static int netvsc_get_link_ksettings(struct net_device *dev,
938 struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800939{
940 struct net_device_context *ndc = netdev_priv(dev);
941
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100942 cmd->base.speed = ndc->speed;
943 cmd->base.duplex = ndc->duplex;
944 cmd->base.port = PORT_OTHER;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800945
946 return 0;
947}
948
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100949static int netvsc_set_link_ksettings(struct net_device *dev,
950 const struct ethtool_link_ksettings *cmd)
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800951{
952 struct net_device_context *ndc = netdev_priv(dev);
953 u32 speed;
954
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100955 speed = cmd->base.speed;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800956 if (!ethtool_validate_speed(speed) ||
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100957 !ethtool_validate_duplex(cmd->base.duplex) ||
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800958 !netvsc_validate_ethtool_ss_cmd(cmd))
959 return -EINVAL;
960
961 ndc->speed = speed;
Philippe Reynes5e8456f2017-03-08 23:41:04 +0100962 ndc->duplex = cmd->base.duplex;
sixiao@microsoft.com49eb9382016-02-25 15:24:08 -0800963
964 return 0;
965}
966
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800967static int netvsc_change_mtu(struct net_device *ndev, int mtu)
968{
969 struct net_device_context *ndevctx = netdev_priv(ndev);
stephen hemminger0c195562017-08-01 19:58:53 -0700970 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
stephen hemminger545a8e72017-03-22 14:51:00 -0700971 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +0200972 struct hv_device *hdev = ndevctx->device_ctx;
stephen hemminger9749fed2017-07-19 11:53:16 -0700973 int orig_mtu = ndev->mtu;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800974 struct netvsc_device_info device_info;
stephen hemmingerea383bf2017-07-19 11:53:15 -0700975 bool was_opened;
stephen hemminger9749fed2017-07-19 11:53:16 -0700976 int ret = 0;
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800977
stephen hemmingera0be4502017-03-22 14:51:01 -0700978 if (!nvdev || nvdev->destroy)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800979 return -ENODEV;
980
stephen hemminger0c195562017-08-01 19:58:53 -0700981 /* Change MTU of underlying VF netdev first. */
982 if (vf_netdev) {
983 ret = dev_set_mtu(vf_netdev, mtu);
984 if (ret)
985 return ret;
986 }
987
stephen hemmingerea383bf2017-07-19 11:53:15 -0700988 netif_device_detach(ndev);
989 was_opened = rndis_filter_opened(nvdev);
990 if (was_opened)
991 rndis_filter_close(nvdev);
Haiyang Zhang2de85302015-07-13 13:09:16 -0700992
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -0700993 memset(&device_info, 0, sizeof(device_info));
stephen hemminger2b018882017-01-24 13:06:03 -0800994 device_info.num_chn = nvdev->num_chn;
stephen hemminger8b532792017-08-09 17:46:11 -0700995 device_info.send_sections = nvdev->send_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700996 device_info.send_section_size = nvdev->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -0700997 device_info.recv_sections = nvdev->recv_section_cnt;
Alex Ng0ab09be2017-09-20 11:17:35 -0700998 device_info.recv_section_size = nvdev->recv_section_size;
Dexuan Cui152669b2017-03-02 13:00:53 +0000999
Dexuan Cui152669b2017-03-02 13:00:53 +00001000 rndis_filter_device_remove(hdev, nvdev);
1001
Dexuan Cui152669b2017-03-02 13:00:53 +00001002 ndev->mtu = mtu;
1003
stephen hemminger9749fed2017-07-19 11:53:16 -07001004 nvdev = rndis_filter_device_add(hdev, &device_info);
1005 if (IS_ERR(nvdev)) {
1006 ret = PTR_ERR(nvdev);
1007
1008 /* Attempt rollback to original MTU */
1009 ndev->mtu = orig_mtu;
stephen hemminger68d715f2017-08-09 17:46:06 -07001010 nvdev = rndis_filter_device_add(hdev, &device_info);
stephen hemminger0c195562017-08-01 19:58:53 -07001011
1012 if (vf_netdev)
1013 dev_set_mtu(vf_netdev, orig_mtu);
stephen hemminger68d715f2017-08-09 17:46:06 -07001014
1015 if (IS_ERR(nvdev)) {
1016 netdev_err(ndev, "restoring mtu failed: %ld\n",
1017 PTR_ERR(nvdev));
1018 return ret;
1019 }
stephen hemminger9749fed2017-07-19 11:53:16 -07001020 }
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001021
stephen hemmingerea383bf2017-07-19 11:53:15 -07001022 if (was_opened)
1023 rndis_filter_open(nvdev);
1024
1025 netif_device_attach(ndev);
stephen hemminger163891d2017-03-22 14:50:58 -07001026
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001027 /* We may have missed link change notifications */
1028 schedule_delayed_work(&ndevctx->dwork, 0);
1029
stephen hemminger9749fed2017-07-19 11:53:16 -07001030 return ret;
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001031}
1032
stephen hemminger0c195562017-08-01 19:58:53 -07001033static void netvsc_get_vf_stats(struct net_device *net,
1034 struct netvsc_vf_pcpu_stats *tot)
1035{
1036 struct net_device_context *ndev_ctx = netdev_priv(net);
1037 int i;
1038
1039 memset(tot, 0, sizeof(*tot));
1040
1041 for_each_possible_cpu(i) {
1042 const struct netvsc_vf_pcpu_stats *stats
1043 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1044 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1045 unsigned int start;
1046
1047 do {
1048 start = u64_stats_fetch_begin_irq(&stats->syncp);
1049 rx_packets = stats->rx_packets;
1050 tx_packets = stats->tx_packets;
1051 rx_bytes = stats->rx_bytes;
1052 tx_bytes = stats->tx_bytes;
1053 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1054
1055 tot->rx_packets += rx_packets;
1056 tot->tx_packets += tx_packets;
1057 tot->rx_bytes += rx_bytes;
1058 tot->tx_bytes += tx_bytes;
1059 tot->tx_dropped += stats->tx_dropped;
1060 }
1061}
1062
stephen hemmingerbc1f4472017-01-06 19:12:52 -08001063static void netvsc_get_stats64(struct net_device *net,
1064 struct rtnl_link_stats64 *t)
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001065{
1066 struct net_device_context *ndev_ctx = netdev_priv(net);
stephen hemminger776e7262017-04-14 14:42:57 -07001067 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001068 struct netvsc_vf_pcpu_stats vf_tot;
stephen hemminger89bb42b2017-08-09 17:46:08 -07001069 int i;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001070
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001071 if (!nvdev)
1072 return;
1073
stephen hemminger0c195562017-08-01 19:58:53 -07001074 netdev_stats_to_stats64(t, &net->stats);
1075
1076 netvsc_get_vf_stats(net, &vf_tot);
1077 t->rx_packets += vf_tot.rx_packets;
1078 t->tx_packets += vf_tot.tx_packets;
1079 t->rx_bytes += vf_tot.rx_bytes;
1080 t->tx_bytes += vf_tot.tx_bytes;
1081 t->tx_dropped += vf_tot.tx_dropped;
1082
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001083 for (i = 0; i < nvdev->num_chn; i++) {
1084 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1085 const struct netvsc_stats *stats;
1086 u64 packets, bytes, multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001087 unsigned int start;
1088
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001089 stats = &nvchan->tx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001090 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001091 start = u64_stats_fetch_begin_irq(&stats->syncp);
1092 packets = stats->packets;
1093 bytes = stats->bytes;
1094 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001095
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001096 t->tx_bytes += bytes;
1097 t->tx_packets += packets;
1098
1099 stats = &nvchan->rx_stats;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001100 do {
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001101 start = u64_stats_fetch_begin_irq(&stats->syncp);
1102 packets = stats->packets;
1103 bytes = stats->bytes;
1104 multicast = stats->multicast + stats->broadcast;
1105 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001106
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001107 t->rx_bytes += bytes;
1108 t->rx_packets += packets;
1109 t->multicast += multicast;
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001110 }
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001111}
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001112
1113static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1114{
stephen hemminger867047c2017-07-28 08:59:42 -07001115 struct net_device_context *ndc = netdev_priv(ndev);
stephen hemminger16ba3262017-08-09 17:46:05 -07001116 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
stephen hemminger867047c2017-07-28 08:59:42 -07001117 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001118 struct sockaddr *addr = p;
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001119 int err;
1120
stephen hemminger16ba3262017-08-09 17:46:05 -07001121 err = eth_prepare_mac_addr_change(ndev, p);
1122 if (err)
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001123 return err;
1124
stephen hemminger867047c2017-07-28 08:59:42 -07001125 if (!nvdev)
1126 return -ENODEV;
1127
stephen hemminger16ba3262017-08-09 17:46:05 -07001128 if (vf_netdev) {
1129 err = dev_set_mac_address(vf_netdev, addr);
1130 if (err)
1131 return err;
1132 }
1133
stephen hemminger867047c2017-07-28 08:59:42 -07001134 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
stephen hemminger16ba3262017-08-09 17:46:05 -07001135 if (!err) {
1136 eth_commit_mac_addr_change(ndev, p);
1137 } else if (vf_netdev) {
1138 /* rollback change on VF */
1139 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1140 dev_set_mac_address(vf_netdev, addr);
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001141 }
1142
1143 return err;
1144}
1145
Stephen Hemminger4323b472016-08-23 12:17:57 -07001146static const struct {
1147 char name[ETH_GSTRING_LEN];
1148 u16 offset;
1149} netvsc_stats[] = {
1150 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
Stephen Hemmingerf61a9d62017-12-12 16:48:36 -08001151 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
Stephen Hemminger4323b472016-08-23 12:17:57 -07001152 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1153 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1154 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
stephen hemmingercad5c192017-08-09 17:46:12 -07001155 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1156 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
Stephen Hemmingerf61a9d62017-12-12 16:48:36 -08001157 { "rx_no_memory", offsetof(struct netvsc_ethtool_stats, rx_no_memory) },
Simon Xiao09af87d2017-09-29 11:39:46 -07001158 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1159 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
stephen hemminger0c195562017-08-01 19:58:53 -07001160}, vf_stats[] = {
1161 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1162 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1163 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1164 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1165 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
Stephen Hemminger4323b472016-08-23 12:17:57 -07001166};
1167
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001168#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
stephen hemminger0c195562017-08-01 19:58:53 -07001169#define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001170
1171/* 4 statistics per queue (rx/tx packets/bytes) */
1172#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1173
Stephen Hemminger4323b472016-08-23 12:17:57 -07001174static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1175{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001176 struct net_device_context *ndc = netdev_priv(dev);
stephen hemmingerfbd4c7e2017-06-07 15:53:47 -07001177 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001178
1179 if (!nvdev)
1180 return -ENODEV;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001181
Stephen Hemminger4323b472016-08-23 12:17:57 -07001182 switch (string_set) {
1183 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001184 return NETVSC_GLOBAL_STATS_LEN
1185 + NETVSC_VF_STATS_LEN
1186 + NETVSC_QUEUE_STATS_LEN(nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001187 default:
1188 return -EINVAL;
1189 }
1190}
1191
1192static void netvsc_get_ethtool_stats(struct net_device *dev,
1193 struct ethtool_stats *stats, u64 *data)
1194{
1195 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001196 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Stephen Hemminger4323b472016-08-23 12:17:57 -07001197 const void *nds = &ndc->eth_stats;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001198 const struct netvsc_stats *qstats;
stephen hemminger0c195562017-08-01 19:58:53 -07001199 struct netvsc_vf_pcpu_stats sum;
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001200 unsigned int start;
1201 u64 packets, bytes;
1202 int i, j;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001203
stephen hemminger545a8e72017-03-22 14:51:00 -07001204 if (!nvdev)
1205 return;
1206
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001207 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
Stephen Hemminger4323b472016-08-23 12:17:57 -07001208 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001209
stephen hemminger0c195562017-08-01 19:58:53 -07001210 netvsc_get_vf_stats(dev, &sum);
1211 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1212 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1213
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001214 for (j = 0; j < nvdev->num_chn; j++) {
1215 qstats = &nvdev->chan_table[j].tx_stats;
1216
1217 do {
1218 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1219 packets = qstats->packets;
1220 bytes = qstats->bytes;
1221 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1222 data[i++] = packets;
1223 data[i++] = bytes;
1224
1225 qstats = &nvdev->chan_table[j].rx_stats;
1226 do {
1227 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1228 packets = qstats->packets;
1229 bytes = qstats->bytes;
1230 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1231 data[i++] = packets;
1232 data[i++] = bytes;
1233 }
Stephen Hemminger4323b472016-08-23 12:17:57 -07001234}
1235
1236static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1237{
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001238 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001239 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001240 u8 *p = data;
Stephen Hemminger4323b472016-08-23 12:17:57 -07001241 int i;
1242
stephen hemminger545a8e72017-03-22 14:51:00 -07001243 if (!nvdev)
1244 return;
1245
Stephen Hemminger4323b472016-08-23 12:17:57 -07001246 switch (stringset) {
1247 case ETH_SS_STATS:
stephen hemminger0c195562017-08-01 19:58:53 -07001248 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1249 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1250 p += ETH_GSTRING_LEN;
1251 }
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001252
stephen hemminger0c195562017-08-01 19:58:53 -07001253 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1254 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1255 p += ETH_GSTRING_LEN;
1256 }
1257
Simon Xiao6c80f3f2017-01-24 13:06:13 -08001258 for (i = 0; i < nvdev->num_chn; i++) {
1259 sprintf(p, "tx_queue_%u_packets", i);
1260 p += ETH_GSTRING_LEN;
1261 sprintf(p, "tx_queue_%u_bytes", i);
1262 p += ETH_GSTRING_LEN;
1263 sprintf(p, "rx_queue_%u_packets", i);
1264 p += ETH_GSTRING_LEN;
1265 sprintf(p, "rx_queue_%u_bytes", i);
1266 p += ETH_GSTRING_LEN;
1267 }
1268
Stephen Hemminger4323b472016-08-23 12:17:57 -07001269 break;
1270 }
1271}
1272
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001273static int
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001274netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1275 struct ethtool_rxnfc *info)
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001276{
Haiyang Zhang486e3982017-10-06 08:33:57 -07001277 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1278
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001279 info->data = RXH_IP_SRC | RXH_IP_DST;
1280
1281 switch (info->flow_type) {
1282 case TCP_V4_FLOW:
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001283 if (ndc->l4_hash & HV_TCP4_L4HASH)
1284 info->data |= l4_flag;
1285
1286 break;
1287
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001288 case TCP_V6_FLOW:
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001289 if (ndc->l4_hash & HV_TCP6_L4HASH)
1290 info->data |= l4_flag;
1291
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001292 break;
1293
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001294 case UDP_V4_FLOW:
Haiyang Zhang486e3982017-10-06 08:33:57 -07001295 if (ndc->l4_hash & HV_UDP4_L4HASH)
1296 info->data |= l4_flag;
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001297
1298 break;
1299
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001300 case UDP_V6_FLOW:
Haiyang Zhang486e3982017-10-06 08:33:57 -07001301 if (ndc->l4_hash & HV_UDP6_L4HASH)
1302 info->data |= l4_flag;
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001303
1304 break;
1305
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001306 case IPV4_FLOW:
1307 case IPV6_FLOW:
1308 break;
1309 default:
1310 info->data = 0;
1311 break;
1312 }
1313
1314 return 0;
1315}
1316
1317static int
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001318netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1319 u32 *rules)
1320{
1321 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001322 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001323
1324 if (!nvdev)
1325 return -ENODEV;
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001326
1327 switch (info->cmd) {
1328 case ETHTOOL_GRXRINGS:
1329 info->data = nvdev->num_chn;
1330 return 0;
stephen hemmingerb5a5dc82017-01-24 13:06:01 -08001331
1332 case ETHTOOL_GRXFH:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001333 return netvsc_get_rss_hash_opts(ndc, info);
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001334 }
1335 return -EOPNOTSUPP;
1336}
1337
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001338static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1339 struct ethtool_rxnfc *info)
1340{
1341 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1342 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
Haiyang Zhang486e3982017-10-06 08:33:57 -07001343 switch (info->flow_type) {
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001344 case TCP_V4_FLOW:
1345 ndc->l4_hash |= HV_TCP4_L4HASH;
1346 break;
1347
1348 case TCP_V6_FLOW:
1349 ndc->l4_hash |= HV_TCP6_L4HASH;
1350 break;
1351
Haiyang Zhang486e3982017-10-06 08:33:57 -07001352 case UDP_V4_FLOW:
1353 ndc->l4_hash |= HV_UDP4_L4HASH;
1354 break;
1355
1356 case UDP_V6_FLOW:
1357 ndc->l4_hash |= HV_UDP6_L4HASH;
1358 break;
1359
1360 default:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001361 return -EOPNOTSUPP;
Haiyang Zhang486e3982017-10-06 08:33:57 -07001362 }
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001363
1364 return 0;
1365 }
1366
1367 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
Haiyang Zhang486e3982017-10-06 08:33:57 -07001368 switch (info->flow_type) {
Haiyang Zhang0518ec4f2017-10-06 08:33:58 -07001369 case TCP_V4_FLOW:
1370 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1371 break;
1372
1373 case TCP_V6_FLOW:
1374 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1375 break;
1376
Haiyang Zhang486e3982017-10-06 08:33:57 -07001377 case UDP_V4_FLOW:
1378 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1379 break;
1380
1381 case UDP_V6_FLOW:
1382 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1383 break;
1384
1385 default:
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001386 return -EOPNOTSUPP;
Haiyang Zhang486e3982017-10-06 08:33:57 -07001387 }
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001388
1389 return 0;
1390 }
1391
1392 return -EOPNOTSUPP;
1393}
1394
1395static int
1396netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1397{
1398 struct net_device_context *ndc = netdev_priv(ndev);
1399
1400 if (info->cmd == ETHTOOL_SRXFH)
1401 return netvsc_set_rss_hash_opts(ndc, info);
1402
1403 return -EOPNOTSUPP;
1404}
1405
Richard Weinberger316158f2014-07-09 16:23:59 +02001406#ifdef CONFIG_NET_POLL_CONTROLLER
stephen hemmingera5ecd432017-06-07 15:53:48 -07001407static void netvsc_poll_controller(struct net_device *dev)
Richard Weinberger316158f2014-07-09 16:23:59 +02001408{
stephen hemmingera5ecd432017-06-07 15:53:48 -07001409 struct net_device_context *ndc = netdev_priv(dev);
1410 struct netvsc_device *ndev;
1411 int i;
1412
1413 rcu_read_lock();
1414 ndev = rcu_dereference(ndc->nvdev);
1415 if (ndev) {
1416 for (i = 0; i < ndev->num_chn; i++) {
1417 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1418
1419 napi_schedule(&nvchan->napi);
1420 }
1421 }
1422 rcu_read_unlock();
Richard Weinberger316158f2014-07-09 16:23:59 +02001423}
1424#endif
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001425
stephen hemminger962f3fe2017-01-24 13:06:02 -08001426static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1427{
1428 return NETVSC_HASH_KEYLEN;
1429}
1430
1431static u32 netvsc_rss_indir_size(struct net_device *dev)
1432{
stephen hemmingerff4a4412017-01-24 13:06:04 -08001433 return ITAB_NUM;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001434}
1435
1436static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1437 u8 *hfunc)
1438{
1439 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger867047c2017-07-28 08:59:42 -07001440 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001441 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001442 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001443
stephen hemminger545a8e72017-03-22 14:51:00 -07001444 if (!ndev)
1445 return -ENODEV;
1446
stephen hemminger962f3fe2017-01-24 13:06:02 -08001447 if (hfunc)
1448 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1449
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001450 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001451 if (indir) {
1452 for (i = 0; i < ITAB_NUM; i++)
Haiyang Zhang473713002017-10-13 12:28:03 -07001453 indir[i] = rndis_dev->rx_table[i];
stephen hemmingerff4a4412017-01-24 13:06:04 -08001454 }
1455
stephen hemminger962f3fe2017-01-24 13:06:02 -08001456 if (key)
1457 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1458
1459 return 0;
1460}
1461
1462static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1463 const u8 *key, const u8 hfunc)
1464{
1465 struct net_device_context *ndc = netdev_priv(dev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001466 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001467 struct rndis_device *rndis_dev;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001468 int i;
stephen hemminger962f3fe2017-01-24 13:06:02 -08001469
stephen hemminger545a8e72017-03-22 14:51:00 -07001470 if (!ndev)
1471 return -ENODEV;
1472
stephen hemminger962f3fe2017-01-24 13:06:02 -08001473 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1474 return -EOPNOTSUPP;
1475
Colin Ian Kingeb996ed2017-03-25 14:26:39 +00001476 rndis_dev = ndev->extension;
stephen hemmingerff4a4412017-01-24 13:06:04 -08001477 if (indir) {
1478 for (i = 0; i < ITAB_NUM; i++)
Haiyang Zhangdb3cd7a2017-09-01 14:30:07 -07001479 if (indir[i] >= ndev->num_chn)
stephen hemmingerff4a4412017-01-24 13:06:04 -08001480 return -EINVAL;
1481
1482 for (i = 0; i < ITAB_NUM; i++)
Haiyang Zhang473713002017-10-13 12:28:03 -07001483 rndis_dev->rx_table[i] = indir[i];
stephen hemmingerff4a4412017-01-24 13:06:04 -08001484 }
1485
1486 if (!key) {
1487 if (!indir)
1488 return 0;
1489
1490 key = rndis_dev->rss_key;
1491 }
stephen hemminger962f3fe2017-01-24 13:06:02 -08001492
Haiyang Zhang715e2ec2017-09-01 14:30:04 -07001493 return rndis_filter_set_rss_param(rndis_dev, key);
stephen hemminger962f3fe2017-01-24 13:06:02 -08001494}
1495
stephen hemminger8b532792017-08-09 17:46:11 -07001496/* Hyper-V RNDIS protocol does not have ring in the HW sense.
1497 * It does have pre-allocated receive area which is divided into sections.
1498 */
1499static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1500 struct ethtool_ringparam *ring)
1501{
1502 u32 max_buf_size;
1503
1504 ring->rx_pending = nvdev->recv_section_cnt;
1505 ring->tx_pending = nvdev->send_section_cnt;
1506
1507 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1508 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1509 else
1510 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1511
1512 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1513 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1514 / nvdev->send_section_size;
1515}
1516
1517static void netvsc_get_ringparam(struct net_device *ndev,
1518 struct ethtool_ringparam *ring)
1519{
1520 struct net_device_context *ndevctx = netdev_priv(ndev);
1521 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1522
1523 if (!nvdev)
1524 return;
1525
1526 __netvsc_get_ringparam(nvdev, ring);
1527}
1528
1529static int netvsc_set_ringparam(struct net_device *ndev,
1530 struct ethtool_ringparam *ring)
1531{
1532 struct net_device_context *ndevctx = netdev_priv(ndev);
1533 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1534 struct hv_device *hdev = ndevctx->device_ctx;
1535 struct netvsc_device_info device_info;
1536 struct ethtool_ringparam orig;
1537 u32 new_tx, new_rx;
1538 bool was_opened;
1539 int ret = 0;
1540
1541 if (!nvdev || nvdev->destroy)
1542 return -ENODEV;
1543
1544 memset(&orig, 0, sizeof(orig));
1545 __netvsc_get_ringparam(nvdev, &orig);
1546
1547 new_tx = clamp_t(u32, ring->tx_pending,
1548 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1549 new_rx = clamp_t(u32, ring->rx_pending,
1550 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1551
1552 if (new_tx == orig.tx_pending &&
1553 new_rx == orig.rx_pending)
1554 return 0; /* no change */
1555
1556 memset(&device_info, 0, sizeof(device_info));
1557 device_info.num_chn = nvdev->num_chn;
stephen hemminger8b532792017-08-09 17:46:11 -07001558 device_info.send_sections = new_tx;
Alex Ng0ab09be2017-09-20 11:17:35 -07001559 device_info.send_section_size = nvdev->send_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -07001560 device_info.recv_sections = new_rx;
Alex Ng0ab09be2017-09-20 11:17:35 -07001561 device_info.recv_section_size = nvdev->recv_section_size;
stephen hemminger8b532792017-08-09 17:46:11 -07001562
1563 netif_device_detach(ndev);
1564 was_opened = rndis_filter_opened(nvdev);
1565 if (was_opened)
1566 rndis_filter_close(nvdev);
1567
1568 rndis_filter_device_remove(hdev, nvdev);
1569
1570 nvdev = rndis_filter_device_add(hdev, &device_info);
1571 if (IS_ERR(nvdev)) {
1572 ret = PTR_ERR(nvdev);
1573
1574 device_info.send_sections = orig.tx_pending;
1575 device_info.recv_sections = orig.rx_pending;
1576 nvdev = rndis_filter_device_add(hdev, &device_info);
1577 if (IS_ERR(nvdev)) {
1578 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1579 PTR_ERR(nvdev));
1580 return ret;
1581 }
1582 }
1583
1584 if (was_opened)
1585 rndis_filter_open(nvdev);
1586 netif_device_attach(ndev);
1587
1588 /* We may have missed link change notifications */
1589 ndevctx->last_reconfig = 0;
1590 schedule_delayed_work(&ndevctx->dwork, 0);
1591
1592 return ret;
1593}
1594
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001595static const struct ethtool_ops ethtool_ops = {
1596 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001597 .get_link = ethtool_op_get_link,
Stephen Hemminger4323b472016-08-23 12:17:57 -07001598 .get_ethtool_stats = netvsc_get_ethtool_stats,
1599 .get_sset_count = netvsc_get_sset_count,
1600 .get_strings = netvsc_get_strings,
Andrew Schwartzmeyer59995372015-02-26 16:27:14 -08001601 .get_channels = netvsc_get_channels,
Andrew Schwartzmeyerb5960e62015-08-11 17:14:32 -07001602 .set_channels = netvsc_set_channels,
sixiao@microsoft.com76d13b52016-02-17 16:43:59 -08001603 .get_ts_info = ethtool_op_get_ts_info,
stephen hemmingerb448f4e2017-01-24 13:06:00 -08001604 .get_rxnfc = netvsc_get_rxnfc,
Haiyang Zhang4823eb22017-08-21 19:22:39 -07001605 .set_rxnfc = netvsc_set_rxnfc,
stephen hemminger962f3fe2017-01-24 13:06:02 -08001606 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1607 .get_rxfh_indir_size = netvsc_rss_indir_size,
1608 .get_rxfh = netvsc_get_rxfh,
1609 .set_rxfh = netvsc_set_rxfh,
Philippe Reynes5e8456f2017-03-08 23:41:04 +01001610 .get_link_ksettings = netvsc_get_link_ksettings,
1611 .set_link_ksettings = netvsc_set_link_ksettings,
stephen hemminger8b532792017-08-09 17:46:11 -07001612 .get_ringparam = netvsc_get_ringparam,
1613 .set_ringparam = netvsc_set_ringparam,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -07001614};
1615
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001616static const struct net_device_ops device_ops = {
1617 .ndo_open = netvsc_open,
1618 .ndo_stop = netvsc_close,
1619 .ndo_start_xmit = netvsc_start_xmit,
Stephen Hemmingerbee9d412018-03-02 13:49:09 -08001620 .ndo_change_rx_flags = netvsc_change_rx_flags,
1621 .ndo_set_rx_mode = netvsc_set_rx_mode,
Haiyang Zhang4d447c92011-12-15 13:45:17 -08001622 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +00001623 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +00001624 .ndo_set_mac_address = netvsc_set_mac_addr,
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001625 .ndo_select_queue = netvsc_select_queue,
sixiao@microsoft.com7eafd9b2015-05-14 01:00:25 -07001626 .ndo_get_stats64 = netvsc_get_stats64,
Richard Weinberger316158f2014-07-09 16:23:59 +02001627#ifdef CONFIG_NET_POLL_CONTROLLER
1628 .ndo_poll_controller = netvsc_poll_controller,
1629#endif
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001630};
1631
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001632/*
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001633 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1634 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1635 * present send GARP packet to network peers with netif_notify_peers().
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001636 */
Haiyang Zhang891de742014-02-12 16:54:27 -08001637static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001638{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001639 struct net_device_context *ndev_ctx =
1640 container_of(w, struct net_device_context, dwork.work);
1641 struct hv_device *device_obj = ndev_ctx->device_ctx;
1642 struct net_device *net = hv_get_drvdata(device_obj);
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001643 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -08001644 struct rndis_device *rdev;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001645 struct netvsc_reconfig *event = NULL;
1646 bool notify = false, reschedule = false;
1647 unsigned long flags, next_reconfig, delay;
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001648
stephen hemminger9b4e9462017-08-24 16:49:16 -07001649 /* if changes are happening, comeback later */
1650 if (!rtnl_trylock()) {
1651 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1652 return;
1653 }
1654
stephen hemmingera0be4502017-03-22 14:51:01 -07001655 net_device = rtnl_dereference(ndev_ctx->nvdev);
1656 if (!net_device)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001657 goto out_unlock;
1658
Haiyang Zhang891de742014-02-12 16:54:27 -08001659 rdev = net_device->extension;
Haiyang Zhang891de742014-02-12 16:54:27 -08001660
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001661 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1662 if (time_is_after_jiffies(next_reconfig)) {
1663 /* link_watch only sends one notification with current state
1664 * per second, avoid doing reconfig more frequently. Handle
1665 * wrap around.
1666 */
1667 delay = next_reconfig - jiffies;
1668 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1669 schedule_delayed_work(&ndev_ctx->dwork, delay);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001670 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001671 }
1672 ndev_ctx->last_reconfig = jiffies;
1673
1674 spin_lock_irqsave(&ndev_ctx->lock, flags);
1675 if (!list_empty(&ndev_ctx->reconfig_events)) {
1676 event = list_first_entry(&ndev_ctx->reconfig_events,
1677 struct netvsc_reconfig, list);
1678 list_del(&event->list);
1679 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1680 }
1681 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1682
1683 if (!event)
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001684 goto out_unlock;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001685
1686 switch (event->event) {
1687 /* Only the following events are possible due to the check in
1688 * netvsc_linkstatus_callback()
1689 */
1690 case RNDIS_STATUS_MEDIA_CONNECT:
1691 if (rdev->link_state) {
1692 rdev->link_state = false;
stephen hemminger0c195562017-08-01 19:58:53 -07001693 netif_carrier_on(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001694 netif_tx_wake_all_queues(net);
1695 } else {
1696 notify = true;
Haiyang Zhang3a494e72014-06-19 18:34:36 -07001697 }
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001698 kfree(event);
1699 break;
1700 case RNDIS_STATUS_MEDIA_DISCONNECT:
1701 if (!rdev->link_state) {
1702 rdev->link_state = true;
1703 netif_carrier_off(net);
1704 netif_tx_stop_all_queues(net);
1705 }
1706 kfree(event);
1707 break;
1708 case RNDIS_STATUS_NETWORK_CHANGE:
1709 /* Only makes sense if carrier is present */
1710 if (!rdev->link_state) {
1711 rdev->link_state = true;
1712 netif_carrier_off(net);
1713 netif_tx_stop_all_queues(net);
1714 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1715 spin_lock_irqsave(&ndev_ctx->lock, flags);
Haiyang Zhang15cfd402016-04-21 16:13:01 -07001716 list_add(&event->list, &ndev_ctx->reconfig_events);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001717 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1718 reschedule = true;
1719 }
1720 break;
Haiyang Zhang891de742014-02-12 16:54:27 -08001721 }
1722
1723 rtnl_unlock();
1724
1725 if (notify)
1726 netdev_notify_peers(net);
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001727
1728 /* link_watch only sends one notification with current state per
1729 * second, handle next reconfig event in 2 seconds.
1730 */
1731 if (reschedule)
1732 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
Vitaly Kuznetsov1bdcec82016-05-13 13:55:21 +02001733
1734 return;
1735
1736out_unlock:
1737 rtnl_unlock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -07001738}
1739
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001740static struct net_device *get_netvsc_bymac(const u8 *mac)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001741{
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001742 struct net_device *dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001743
Stephen Hemminger8737caa2016-08-23 12:17:44 -07001744 ASSERT_RTNL();
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001745
1746 for_each_netdev(&init_net, dev) {
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001747 if (dev->netdev_ops != &device_ops)
1748 continue; /* not a netvsc device */
1749
1750 if (ether_addr_equal(mac, dev->perm_addr))
1751 return dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001752 }
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001753
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001754 return NULL;
1755}
1756
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001757static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001758{
1759 struct net_device *dev;
1760
1761 ASSERT_RTNL();
1762
1763 for_each_netdev(&init_net, dev) {
1764 struct net_device_context *net_device_ctx;
1765
1766 if (dev->netdev_ops != &device_ops)
1767 continue; /* not a netvsc device */
1768
1769 net_device_ctx = netdev_priv(dev);
stephen hemminger79e8cbe2017-07-19 11:53:13 -07001770 if (!rtnl_dereference(net_device_ctx->nvdev))
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001771 continue; /* device is removed */
1772
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001773 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001774 return dev; /* a match */
1775 }
1776
1777 return NULL;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001778}
1779
stephen hemminger0c195562017-08-01 19:58:53 -07001780/* Called when VF is injecting data into network stack.
1781 * Change the associated network device from VF to netvsc.
1782 * note: already called with rcu_read_lock
1783 */
1784static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1785{
1786 struct sk_buff *skb = *pskb;
1787 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1788 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1789 struct netvsc_vf_pcpu_stats *pcpu_stats
1790 = this_cpu_ptr(ndev_ctx->vf_stats);
1791
1792 skb->dev = ndev;
1793
1794 u64_stats_update_begin(&pcpu_stats->syncp);
1795 pcpu_stats->rx_packets++;
1796 pcpu_stats->rx_bytes += skb->len;
1797 u64_stats_update_end(&pcpu_stats->syncp);
1798
1799 return RX_HANDLER_ANOTHER;
1800}
1801
1802static int netvsc_vf_join(struct net_device *vf_netdev,
1803 struct net_device *ndev)
1804{
1805 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1806 int ret;
1807
1808 ret = netdev_rx_handler_register(vf_netdev,
1809 netvsc_vf_handle_frame, ndev);
1810 if (ret != 0) {
1811 netdev_err(vf_netdev,
1812 "can not register netvsc VF receive handler (err = %d)\n",
1813 ret);
1814 goto rx_handler_failed;
1815 }
1816
David Ahern42ab19e2017-10-04 17:48:47 -07001817 ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
stephen hemminger0c195562017-08-01 19:58:53 -07001818 if (ret != 0) {
1819 netdev_err(vf_netdev,
1820 "can not set master device %s (err = %d)\n",
1821 ndev->name, ret);
1822 goto upper_link_failed;
1823 }
1824
1825 /* set slave flag before open to prevent IPv6 addrconf */
1826 vf_netdev->flags |= IFF_SLAVE;
1827
stephen hemminger6123c662017-08-09 17:46:03 -07001828 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1829
1830 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001831
1832 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1833 return 0;
1834
1835upper_link_failed:
1836 netdev_rx_handler_unregister(vf_netdev);
1837rx_handler_failed:
1838 return ret;
1839}
1840
1841static void __netvsc_vf_setup(struct net_device *ndev,
1842 struct net_device *vf_netdev)
1843{
1844 int ret;
1845
stephen hemminger0c195562017-08-01 19:58:53 -07001846 /* Align MTU of VF with master */
1847 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1848 if (ret)
1849 netdev_warn(vf_netdev,
1850 "unable to change mtu to %u\n", ndev->mtu);
1851
Stephen Hemmingerbee9d412018-03-02 13:49:09 -08001852 /* set multicast etc flags on VF */
1853 dev_change_flags(vf_netdev, ndev->flags | IFF_SLAVE);
Stephen Hemmingerb0dee792018-03-07 13:49:12 -08001854
1855 /* sync address list from ndev to VF */
1856 netif_addr_lock_bh(ndev);
Stephen Hemmingerbee9d412018-03-02 13:49:09 -08001857 dev_uc_sync(vf_netdev, ndev);
1858 dev_mc_sync(vf_netdev, ndev);
Stephen Hemmingerb0dee792018-03-07 13:49:12 -08001859 netif_addr_unlock_bh(ndev);
Stephen Hemmingerbee9d412018-03-02 13:49:09 -08001860
stephen hemminger0c195562017-08-01 19:58:53 -07001861 if (netif_running(ndev)) {
1862 ret = dev_open(vf_netdev);
1863 if (ret)
1864 netdev_warn(vf_netdev,
1865 "unable to open: %d\n", ret);
1866 }
1867}
1868
1869/* Setup VF as slave of the synthetic device.
1870 * Runs in workqueue to avoid recursion in netlink callbacks.
1871 */
1872static void netvsc_vf_setup(struct work_struct *w)
1873{
1874 struct net_device_context *ndev_ctx
stephen hemminger6123c662017-08-09 17:46:03 -07001875 = container_of(w, struct net_device_context, vf_takeover.work);
stephen hemminger0c195562017-08-01 19:58:53 -07001876 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1877 struct net_device *vf_netdev;
1878
stephen hemmingerfb84af82017-08-04 12:14:00 -07001879 if (!rtnl_trylock()) {
stephen hemminger6123c662017-08-09 17:46:03 -07001880 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
stephen hemmingerfb84af82017-08-04 12:14:00 -07001881 return;
1882 }
1883
stephen hemminger0c195562017-08-01 19:58:53 -07001884 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1885 if (vf_netdev)
1886 __netvsc_vf_setup(ndev, vf_netdev);
1887
1888 rtnl_unlock();
1889}
1890
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001891static int netvsc_register_vf(struct net_device *vf_netdev)
1892{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001893 struct net_device *ndev;
1894 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001895 struct netvsc_device *netvsc_dev;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001896
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001897 if (vf_netdev->addr_len != ETH_ALEN)
1898 return NOTIFY_DONE;
1899
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001900 /*
1901 * We will use the MAC address to locate the synthetic interface to
1902 * associate with the VF interface. If we don't find a matching
1903 * synthetic interface, move on.
1904 */
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001905 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001906 if (!ndev)
1907 return NOTIFY_DONE;
1908
1909 net_device_ctx = netdev_priv(ndev);
stephen hemminger545a8e72017-03-22 14:51:00 -07001910 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001911 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001912 return NOTIFY_DONE;
1913
stephen hemminger0c195562017-08-01 19:58:53 -07001914 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1915 return NOTIFY_DONE;
1916
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001917 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
stephen hemminger0c195562017-08-01 19:58:53 -07001918
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001919 dev_hold(vf_netdev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001920 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001921 return NOTIFY_OK;
1922}
1923
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07001924/* VF up/down change detected, schedule to change data path */
1925static int netvsc_vf_changed(struct net_device *vf_netdev)
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001926{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001927 struct net_device_context *net_device_ctx;
stephen hemminger7b83f522017-08-07 11:30:00 -07001928 struct netvsc_device *netvsc_dev;
stephen hemminger0c195562017-08-01 19:58:53 -07001929 struct net_device *ndev;
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07001930 bool vf_is_up = netif_running(vf_netdev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001931
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001932 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001933 if (!ndev)
1934 return NOTIFY_DONE;
1935
1936 net_device_ctx = netdev_priv(ndev);
stephen hemminger7b83f522017-08-07 11:30:00 -07001937 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
1938 if (!netvsc_dev)
1939 return NOTIFY_DONE;
1940
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07001941 netvsc_switch_datapath(ndev, vf_is_up);
1942 netdev_info(ndev, "Data path switched %s VF: %s\n",
1943 vf_is_up ? "to" : "from", vf_netdev->name);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001944
1945 return NOTIFY_OK;
1946}
1947
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001948static int netvsc_unregister_vf(struct net_device *vf_netdev)
1949{
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001950 struct net_device *ndev;
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001951 struct net_device_context *net_device_ctx;
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001952
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001953 ndev = get_netvsc_byref(vf_netdev);
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001954 if (!ndev)
1955 return NOTIFY_DONE;
1956
1957 net_device_ctx = netdev_priv(ndev);
stephen hemminger6123c662017-08-09 17:46:03 -07001958 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
Stephen Hemmingere8ff40d2016-09-22 16:56:32 -07001959
Vitaly Kuznetsov0a1275c2016-05-13 13:55:23 +02001960 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001961
Stephen Hemmingerec158f72017-08-31 16:16:13 -07001962 netdev_rx_handler_unregister(vf_netdev);
stephen hemminger0c195562017-08-01 19:58:53 -07001963 netdev_upper_dev_unlink(vf_netdev, ndev);
Stephen Hemmingerf207c102016-09-22 16:56:33 -07001964 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
Stephen Hemminger07d0f002016-09-22 16:56:30 -07001965 dev_put(vf_netdev);
Stephen Hemmingerec158f72017-08-31 16:16:13 -07001966
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07001967 return NOTIFY_OK;
1968}
1969
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001970static int netvsc_probe(struct hv_device *dev,
1971 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001972{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001973 struct net_device *net = NULL;
1974 struct net_device_context *net_device_ctx;
1975 struct netvsc_device_info device_info;
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001976 struct netvsc_device *nvdev;
stephen hemminger0c195562017-08-01 19:58:53 -07001977 int ret = -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001978
Haiyang Zhang5b54dac2014-04-21 10:20:28 -07001979 net = alloc_etherdev_mq(sizeof(struct net_device_context),
stephen hemminger2b018882017-01-24 13:06:03 -08001980 VRSS_CHANNEL_MAX);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001981 if (!net)
stephen hemminger0c195562017-08-01 19:58:53 -07001982 goto no_net;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001983
Haiyang Zhang1b07da52014-03-04 14:11:06 -08001984 netif_carrier_off(net);
1985
Haiyang Zhangb37879e2016-08-04 10:42:14 -07001986 netvsc_init_settings(net);
1987
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001988 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07001989 net_device_ctx->device_ctx = dev;
Simon Xiao3f300ff2015-04-28 01:05:17 -07001990 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1991 if (netif_msg_probe(net_device_ctx))
1992 netdev_dbg(net, "netvsc msg_enable: %d\n",
1993 net_device_ctx->msg_enable);
1994
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07001995 hv_set_drvdata(dev, net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02001996
Haiyang Zhang891de742014-02-12 16:54:27 -08001997 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07001998
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01001999 spin_lock_init(&net_device_ctx->lock);
2000 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
stephen hemminger6123c662017-08-09 17:46:03 -07002001 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
stephen hemminger0c195562017-08-01 19:58:53 -07002002
2003 net_device_ctx->vf_stats
2004 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
2005 if (!net_device_ctx->vf_stats)
2006 goto no_stats;
Vitaly Kuznetsov27a70af2015-11-27 11:39:55 +01002007
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002008 net->netdev_ops = &device_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002009 net->ethtool_ops = &ethtool_ops;
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -07002010 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002011
Vitaly Kuznetsov14a03cf2016-02-05 17:29:08 +01002012 /* We always need headroom for rndis header */
2013 net->needed_headroom = RNDIS_AND_PPI_SIZE;
2014
Haiyang Zhang6450f8f2017-09-22 15:31:38 -07002015 /* Initialize the number of queues to be 1, we may change it if more
2016 * channels are offered later.
2017 */
2018 netif_set_real_num_tx_queues(net, 1);
2019 netif_set_real_num_rx_queues(net, 1);
2020
Haiyang Zhang692e0842011-09-01 12:19:43 -07002021 /* Notify the netvsc driver of the new device */
Andrew Schwartzmeyer8ebdcc52015-08-11 17:14:31 -07002022 memset(&device_info, 0, sizeof(device_info));
stephen hemminger3071ada2017-03-22 14:50:59 -07002023 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
stephen hemminger8b532792017-08-09 17:46:11 -07002024 device_info.send_sections = NETVSC_DEFAULT_TX;
Alex Ng0ab09be2017-09-20 11:17:35 -07002025 device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
stephen hemminger8b532792017-08-09 17:46:11 -07002026 device_info.recv_sections = NETVSC_DEFAULT_RX;
Alex Ng0ab09be2017-09-20 11:17:35 -07002027 device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
stephen hemminger9749fed2017-07-19 11:53:16 -07002028
2029 nvdev = rndis_filter_device_add(dev, &device_info);
2030 if (IS_ERR(nvdev)) {
2031 ret = PTR_ERR(nvdev);
Haiyang Zhang692e0842011-09-01 12:19:43 -07002032 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
stephen hemminger0c195562017-08-01 19:58:53 -07002033 goto rndis_failed;
Haiyang Zhang692e0842011-09-01 12:19:43 -07002034 }
stephen hemminger0c195562017-08-01 19:58:53 -07002035
Haiyang Zhang692e0842011-09-01 12:19:43 -07002036 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
2037
Vitaly Kuznetsovaefd80e2017-11-15 15:12:55 +01002038 /* hw_features computed in rndis_netdev_set_hwcaps() */
stephen hemminger23312a32017-01-24 13:05:59 -08002039 net->features = net->hw_features |
2040 NETIF_F_HIGHDMA | NETIF_F_SG |
2041 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2042 net->vlan_features = net->features;
2043
stephen hemminger9749fed2017-07-19 11:53:16 -07002044 netdev_lockdep_set_classes(net);
2045
Jarod Wilsond0c2c992016-10-20 13:55:21 -04002046 /* MTU range: 68 - 1500 or 65521 */
2047 net->min_mtu = NETVSC_MTU_MIN;
2048 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2049 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2050 else
2051 net->max_mtu = ETH_DATA_LEN;
2052
Haiyang Zhanga68f9612013-12-20 16:52:31 -08002053 ret = register_netdev(net);
2054 if (ret != 0) {
2055 pr_err("Unable to register netdev.\n");
stephen hemminger0c195562017-08-01 19:58:53 -07002056 goto register_failed;
Haiyang Zhanga68f9612013-12-20 16:52:31 -08002057 }
2058
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002059 return ret;
stephen hemminger0c195562017-08-01 19:58:53 -07002060
2061register_failed:
2062 rndis_filter_device_remove(dev, nvdev);
2063rndis_failed:
2064 free_percpu(net_device_ctx->vf_stats);
2065no_stats:
2066 hv_set_drvdata(dev, NULL);
2067 free_netdev(net);
2068no_net:
2069 return ret;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002070}
2071
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07002072static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002073{
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002074 struct net_device_context *ndev_ctx;
Stephen Hemmingerec158f72017-08-31 16:16:13 -07002075 struct net_device *vf_netdev;
2076 struct net_device *net;
K. Y. Srinivasan2ddd5e5f2011-09-13 10:59:49 -07002077
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002078 net = hv_get_drvdata(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002079 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -07002080 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002081 return 0;
2082 }
2083
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002084 ndev_ctx = netdev_priv(net);
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002085
stephen hemmingera0be4502017-03-22 14:51:01 -07002086 netif_device_detach(net);
Vitaly Kuznetsovf580aec2016-05-13 13:55:20 +02002087
Haiyang Zhang122a5f62011-05-27 06:21:55 -07002088 cancel_delayed_work_sync(&ndev_ctx->dwork);
2089
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002090 /*
2091 * Call to the vsc driver to let it know that the device is being
stephen hemmingera0be4502017-03-22 14:51:01 -07002092 * removed. Also blocks mtu and channel changes.
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002093 */
stephen hemmingera0be4502017-03-22 14:51:01 -07002094 rtnl_lock();
Stephen Hemmingerec158f72017-08-31 16:16:13 -07002095 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2096 if (vf_netdev)
2097 netvsc_unregister_vf(vf_netdev);
2098
Stephen Hemminger8195b132017-09-06 13:53:05 -07002099 unregister_netdevice(net);
2100
stephen hemminger79e8cbe2017-07-19 11:53:13 -07002101 rndis_filter_device_remove(dev,
2102 rtnl_dereference(ndev_ctx->nvdev));
stephen hemmingera0be4502017-03-22 14:51:01 -07002103 rtnl_unlock();
2104
Vitaly Kuznetsov3d541ac2016-05-13 13:55:22 +02002105 hv_set_drvdata(dev, NULL);
2106
stephen hemminger0c195562017-08-01 19:58:53 -07002107 free_percpu(ndev_ctx->vf_stats);
Simon Xiao6c80f3f2017-01-24 13:06:13 -08002108 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -07002109 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002110}
2111
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002112static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07002113 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -08002114 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07002115 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002116};
2117
2118MODULE_DEVICE_TABLE(vmbus, id_table);
2119
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07002120/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07002121static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +00002122 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -07002123 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -07002124 .probe = netvsc_probe,
2125 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -07002126};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -07002127
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002128/*
2129 * On Hyper-V, every VF interface is matched with a corresponding
2130 * synthetic interface. The synthetic interface is presented first
2131 * to the guest. When the corresponding VF instance is registered,
2132 * we will take care of switching the data path.
2133 */
2134static int netvsc_netdev_event(struct notifier_block *this,
2135 unsigned long event, void *ptr)
2136{
2137 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2138
Stephen Hemmingeree837a12016-09-22 16:56:31 -07002139 /* Skip our own events */
2140 if (event_dev->netdev_ops == &device_ops)
2141 return NOTIFY_DONE;
2142
2143 /* Avoid non-Ethernet type devices */
2144 if (event_dev->type != ARPHRD_ETHER)
2145 return NOTIFY_DONE;
2146
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02002147 /* Avoid Vlan dev with same MAC registering as VF */
Parav Panditd0d7b102017-02-04 11:00:49 -06002148 if (is_vlan_dev(event_dev))
Vitaly Kuznetsov0dbff142016-08-15 17:48:43 +02002149 return NOTIFY_DONE;
2150
2151 /* Avoid Bonding master dev with same MAC registering as VF */
Stephen Hemmingeree837a12016-09-22 16:56:31 -07002152 if ((event_dev->priv_flags & IFF_BONDING) &&
2153 (event_dev->flags & IFF_MASTER))
Haiyang Zhangcb2911f2016-06-02 12:02:04 -07002154 return NOTIFY_DONE;
2155
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002156 switch (event) {
2157 case NETDEV_REGISTER:
2158 return netvsc_register_vf(event_dev);
2159 case NETDEV_UNREGISTER:
2160 return netvsc_unregister_vf(event_dev);
2161 case NETDEV_UP:
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002162 case NETDEV_DOWN:
Stephen Hemminger9a0c48d2017-08-31 16:16:12 -07002163 return netvsc_vf_changed(event_dev);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002164 default:
2165 return NOTIFY_DONE;
2166 }
2167}
2168
2169static struct notifier_block netvsc_netdev_notifier = {
2170 .notifier_call = netvsc_netdev_event,
2171};
2172
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07002173static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -07002174{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002175 unregister_netdevice_notifier(&netvsc_netdev_notifier);
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07002176 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -07002177}
2178
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07002179static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002180{
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002181 int ret;
2182
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00002183 if (ring_size < RING_SIZE_MIN) {
2184 ring_size = RING_SIZE_MIN;
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08002185 pr_info("Increased ring_size to %u (min allowed)\n",
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +00002186 ring_size);
2187 }
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08002188 netvsc_ring_bytes = ring_size * PAGE_SIZE;
2189 netvsc_ring_reciprocal = reciprocal_value(netvsc_ring_bytes);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002190
Stephen Hemmingera7f99d02017-12-01 11:01:47 -08002191 ret = vmbus_driver_register(&netvsc_drv);
KY Srinivasan84bf9ce2016-04-14 16:31:54 -07002192 if (ret)
2193 return ret;
2194
2195 register_netdevice_notifier(&netvsc_netdev_notifier);
2196 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -07002197}
2198
Hank Janssen26c14cc2010-02-11 23:02:42 +00002199MODULE_LICENSE("GPL");
Stephen Hemminger7880fc52010-05-04 09:58:52 -07002200MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -07002201
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -07002202module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -07002203module_exit(netvsc_drv_exit);