]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/net/hyperv/netvsc_drv.c
hv_netvsc: Rename tx_send_table to tx_table
[thirdparty/linux.git] / drivers / net / hyperv / netvsc_drv.c
CommitLineData
fceaf24a 1/*
fceaf24a
HJ
2 * 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
adf8d3ff 14 * this program; if not, see <http://www.gnu.org/licenses/>.
fceaf24a
HJ
15 *
16 * Authors:
d0e94d17 17 * Haiyang Zhang <haiyangz@microsoft.com>
fceaf24a 18 * Hank Janssen <hjanssen@microsoft.com>
fceaf24a 19 */
eb335bc4
HJ
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
fceaf24a 22#include <linux/init.h>
9079ce69 23#include <linux/atomic.h>
fceaf24a
HJ
24#include <linux/module.h>
25#include <linux/highmem.h>
26#include <linux/device.h>
fceaf24a 27#include <linux/io.h>
fceaf24a
HJ
28#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
c802db11 33#include <linux/if_vlan.h>
fceaf24a 34#include <linux/in.h>
5a0e3ad6 35#include <linux/slab.h>
27f5aa92 36#include <linux/rtnetlink.h>
0c195567 37#include <linux/netpoll.h>
27f5aa92 38
fceaf24a
HJ
39#include <net/arp.h>
40#include <net/route.h>
41#include <net/sock.h>
42#include <net/pkt_sched.h>
8eb1b3c3
MK
43#include <net/checksum.h>
44#include <net/ip6_checksum.h>
3f335ea2 45
5ca7252a 46#include "hyperv_net.h"
fceaf24a 47
8b532797 48#define RING_SIZE_MIN 64
49#define NETVSC_MIN_TX_SECTIONS 10
50#define NETVSC_DEFAULT_TX 192 /* ~1M */
51#define NETVSC_MIN_RX_SECTIONS 10 /* ~64K */
5023a6db 52#define NETVSC_DEFAULT_RX 10485 /* Max ~16M */
8b532797 53
27a70af3 54#define LINKCHANGE_INT (2 * HZ)
6123c668 55#define VF_TAKEOVER_INT (HZ / 10)
a50af86d 56
99c8da0f 57static int ring_size = 128;
450d7a4b
SH
58module_param(ring_size, int, S_IRUGO);
59MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
fceaf24a 60
3f300ff4
SX
61static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
62 NETIF_MSG_LINK | NETIF_MSG_IFUP |
63 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR |
64 NETIF_MSG_TX_ERR;
65
66static int debug = -1;
67module_param(debug, int, S_IRUGO);
68MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
69
4e9bfefa 70static void netvsc_set_multicast_list(struct net_device *net)
fceaf24a 71{
792df872 72 struct net_device_context *net_device_ctx = netdev_priv(net);
4f19c0d8 73 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
d426b2e3 74
4f19c0d8 75 rndis_filter_update(nvdev);
fceaf24a
HJ
76}
77
fceaf24a
HJ
78static int netvsc_open(struct net_device *net)
79{
53fa1a6f 80 struct net_device_context *ndev_ctx = netdev_priv(net);
0c195567 81 struct net_device *vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
79e8cbe7 82 struct netvsc_device *nvdev = rtnl_dereference(ndev_ctx->nvdev);
891de74d 83 struct rndis_device *rdev;
02fafbc6 84 int ret = 0;
fceaf24a 85
891de74d
HZ
86 netif_carrier_off(net);
87
d515d0ff 88 /* Open up the device */
2f5fa6c8 89 ret = rndis_filter_open(nvdev);
d515d0ff
HZ
90 if (ret != 0) {
91 netdev_err(net, "unable to open device (ret %d).\n", ret);
92 return ret;
fceaf24a
HJ
93 }
94
2de8530b 95 netif_tx_wake_all_queues(net);
d515d0ff 96
891de74d 97 rdev = nvdev->extension;
0c195567 98
99 if (!rdev->link_state)
891de74d
HZ
100 netif_carrier_on(net);
101
0c195567 102 if (vf_netdev) {
103 /* Setting synthetic device up transparently sets
104 * slave as up. If open fails, then slave will be
105 * still be offline (and not used).
106 */
107 ret = dev_open(vf_netdev);
108 if (ret)
109 netdev_warn(net,
110 "unable to open slave: %s: %d\n",
111 vf_netdev->name, ret);
112 }
113 return 0;
fceaf24a
HJ
114}
115
fceaf24a
HJ
116static int netvsc_close(struct net_device *net)
117{
fceaf24a 118 struct net_device_context *net_device_ctx = netdev_priv(net);
0c195567 119 struct net_device *vf_netdev
120 = rtnl_dereference(net_device_ctx->vf_netdev);
545a8e79 121 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
c6f71c41 122 int ret = 0;
40975962 123 u32 aread, i, msec = 10, retry = 0, retry_max = 20;
2de8530b 124 struct vmbus_channel *chn;
fceaf24a 125
0a282538 126 netif_tx_disable(net);
fceaf24a 127
c6f71c41
HZ
128 /* No need to close rndis filter if it is removed already */
129 if (!nvdev)
130 goto out;
131
2f5fa6c8 132 ret = rndis_filter_close(nvdev);
2de8530b 133 if (ret != 0) {
eb335bc4 134 netdev_err(net, "unable to close device (ret %d).\n", ret);
2de8530b
HZ
135 return ret;
136 }
137
138 /* Ensure pending bytes in ring are read */
139 while (true) {
140 aread = 0;
141 for (i = 0; i < nvdev->num_chn; i++) {
b8b835a8 142 chn = nvdev->chan_table[i].channel;
2de8530b
HZ
143 if (!chn)
144 continue;
145
40975962 146 aread = hv_get_bytes_to_read(&chn->inbound);
2de8530b
HZ
147 if (aread)
148 break;
149
40975962 150 aread = hv_get_bytes_to_read(&chn->outbound);
2de8530b
HZ
151 if (aread)
152 break;
153 }
154
155 retry++;
156 if (retry > retry_max || aread == 0)
157 break;
158
159 msleep(msec);
160
161 if (msec < 1000)
162 msec *= 2;
163 }
164
165 if (aread) {
166 netdev_err(net, "Ring buffer not empty after closing rndis\n");
167 ret = -ETIMEDOUT;
168 }
fceaf24a 169
c6f71c41 170out:
0c195567 171 if (vf_netdev)
172 dev_close(vf_netdev);
173
fceaf24a
HJ
174 return ret;
175}
176
8a00251a 177static void *init_ppi_data(struct rndis_message *msg, u32 ppi_size,
89bb42b1 178 int pkt_type)
8a00251a
KS
179{
180 struct rndis_packet *rndis_pkt;
181 struct rndis_per_packet_info *ppi;
182
183 rndis_pkt = &msg->msg.pkt;
184 rndis_pkt->data_offset += ppi_size;
185
186 ppi = (struct rndis_per_packet_info *)((void *)rndis_pkt +
187 rndis_pkt->per_pkt_info_offset + rndis_pkt->per_pkt_info_len);
188
189 ppi->size = ppi_size;
190 ppi->type = pkt_type;
191 ppi->ppi_offset = sizeof(struct rndis_per_packet_info);
192
193 rndis_pkt->per_pkt_info_len += ppi_size;
194
195 return ppi;
196}
197
4823eb2f
HZ
198/* Azure hosts don't support non-TCP port numbers in hashing for fragmented
199 * packets. We can use ethtool to change UDP hash level when necessary.
f72860af 200 */
4823eb2f
HZ
201static inline u32 netvsc_get_hash(
202 struct sk_buff *skb,
203 const struct net_device_context *ndc)
f72860af
HZ
204{
205 struct flow_keys flow;
486e3981 206 u32 hash, pkt_proto = 0;
f72860af
HZ
207 static u32 hashrnd __read_mostly;
208
209 net_get_random_once(&hashrnd, sizeof(hashrnd));
210
211 if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
212 return 0;
213
486e3981
HZ
214 switch (flow.basic.ip_proto) {
215 case IPPROTO_TCP:
216 if (flow.basic.n_proto == htons(ETH_P_IP))
217 pkt_proto = HV_TCP4_L4HASH;
218 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
219 pkt_proto = HV_TCP6_L4HASH;
220
221 break;
222
223 case IPPROTO_UDP:
224 if (flow.basic.n_proto == htons(ETH_P_IP))
225 pkt_proto = HV_UDP4_L4HASH;
226 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
227 pkt_proto = HV_UDP6_L4HASH;
228
229 break;
230 }
231
232 if (pkt_proto & ndc->l4_hash) {
f72860af
HZ
233 return skb_get_hash(skb);
234 } else {
235 if (flow.basic.n_proto == htons(ETH_P_IP))
236 hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
237 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
238 hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
239 else
240 hash = 0;
241
242 skb_set_hash(skb, hash, PKT_HASH_TYPE_L3);
243 }
244
245 return hash;
246}
247
8db91f6a
HZ
248static inline int netvsc_get_tx_queue(struct net_device *ndev,
249 struct sk_buff *skb, int old_idx)
250{
251 const struct net_device_context *ndc = netdev_priv(ndev);
252 struct sock *sk = skb->sk;
253 int q_idx;
254
39e91cfb
HZ
255 q_idx = ndc->tx_table[netvsc_get_hash(skb, ndc) &
256 (VRSS_SEND_TAB_SIZE - 1)];
8db91f6a
HZ
257
258 /* If queue index changed record the new value */
259 if (q_idx != old_idx &&
260 sk && sk_fullsock(sk) && rcu_access_pointer(sk->sk_dst_cache))
261 sk_tx_queue_set(sk, q_idx);
262
263 return q_idx;
264}
265
d8e18ee0 266/*
267 * Select queue for transmit.
268 *
269 * If a valid queue has already been assigned, then use that.
270 * Otherwise compute tx queue based on hash and the send table.
271 *
272 * This is basically similar to default (__netdev_pick_tx) with the added step
273 * of using the host send_table when no other queue has been assigned.
274 *
275 * TODO support XPS - but get_xps_queue not exported
276 */
0c195567 277static u16 netvsc_pick_tx(struct net_device *ndev, struct sk_buff *skb)
5b54dac8 278{
8db91f6a
HZ
279 int q_idx = sk_tx_queue_get(skb->sk);
280
0c195567 281 if (q_idx < 0 || skb->ooo_okay || q_idx >= ndev->real_num_tx_queues) {
8db91f6a
HZ
282 /* If forwarding a packet, we use the recorded queue when
283 * available for better cache locality.
284 */
285 if (skb_rx_queue_recorded(skb))
286 q_idx = skb_get_rx_queue(skb);
287 else
288 q_idx = netvsc_get_tx_queue(ndev, skb, q_idx);
d8e18ee0 289 }
5b54dac8
HZ
290
291 return q_idx;
292}
293
0c195567 294static u16 netvsc_select_queue(struct net_device *ndev, struct sk_buff *skb,
295 void *accel_priv,
296 select_queue_fallback_t fallback)
297{
298 struct net_device_context *ndc = netdev_priv(ndev);
299 struct net_device *vf_netdev;
300 u16 txq;
301
302 rcu_read_lock();
303 vf_netdev = rcu_dereference(ndc->vf_netdev);
304 if (vf_netdev) {
305 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
306 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb->queue_mapping;
307 } else {
308 txq = netvsc_pick_tx(ndev, skb);
309 }
310 rcu_read_unlock();
311
312 while (unlikely(txq >= ndev->real_num_tx_queues))
313 txq -= ndev->real_num_tx_queues;
314
315 return txq;
316}
317
54a7357f 318static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
89bb42b1 319 struct hv_page_buffer *pb)
54a7357f
KS
320{
321 int j = 0;
322
323 /* Deal with compund pages by ignoring unused part
324 * of the page.
325 */
326 page += (offset >> PAGE_SHIFT);
327 offset &= ~PAGE_MASK;
328
329 while (len > 0) {
330 unsigned long bytes;
331
332 bytes = PAGE_SIZE - offset;
333 if (bytes > len)
334 bytes = len;
335 pb[j].pfn = page_to_pfn(page);
336 pb[j].offset = offset;
337 pb[j].len = bytes;
338
339 offset += bytes;
340 len -= bytes;
341
342 if (offset == PAGE_SIZE && len) {
343 page++;
344 offset = 0;
345 j++;
346 }
347 }
348
349 return j + 1;
350}
351
8a00251a 352static u32 init_page_array(void *hdr, u32 len, struct sk_buff *skb,
a9f2e2d6 353 struct hv_netvsc_packet *packet,
02b6de01 354 struct hv_page_buffer *pb)
54a7357f
KS
355{
356 u32 slots_used = 0;
357 char *data = skb->data;
358 int frags = skb_shinfo(skb)->nr_frags;
359 int i;
360
361 /* The packet is laid out thus:
aa0a34be 362 * 1. hdr: RNDIS header and PPI
54a7357f
KS
363 * 2. skb linear data
364 * 3. skb fragment data
365 */
ea5a32c0 366 slots_used += fill_pg_buf(virt_to_page(hdr),
367 offset_in_page(hdr),
368 len, &pb[slots_used]);
54a7357f 369
aa0a34be
HZ
370 packet->rmsg_size = len;
371 packet->rmsg_pgcnt = slots_used;
372
54a7357f
KS
373 slots_used += fill_pg_buf(virt_to_page(data),
374 offset_in_page(data),
375 skb_headlen(skb), &pb[slots_used]);
376
377 for (i = 0; i < frags; i++) {
378 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
379
380 slots_used += fill_pg_buf(skb_frag_page(frag),
381 frag->page_offset,
382 skb_frag_size(frag), &pb[slots_used]);
383 }
8a00251a 384 return slots_used;
54a7357f
KS
385}
386
80d887db 387static int count_skb_frag_slots(struct sk_buff *skb)
388{
389 int i, frags = skb_shinfo(skb)->nr_frags;
390 int pages = 0;
391
392 for (i = 0; i < frags; i++) {
393 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
394 unsigned long size = skb_frag_size(frag);
395 unsigned long offset = frag->page_offset;
396
397 /* Skip unused frames from start of page */
398 offset &= ~PAGE_MASK;
399 pages += PFN_UP(offset + size);
400 }
401 return pages;
402}
403
404static int netvsc_get_slots(struct sk_buff *skb)
54a7357f 405{
80d887db 406 char *data = skb->data;
407 unsigned int offset = offset_in_page(data);
408 unsigned int len = skb_headlen(skb);
409 int slots;
410 int frag_slots;
411
412 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
413 frag_slots = count_skb_frag_slots(skb);
414 return slots + frag_slots;
54a7357f
KS
415}
416
23312a3b 417static u32 net_checksum_info(struct sk_buff *skb)
08cd04bf 418{
23312a3b 419 if (skb->protocol == htons(ETH_P_IP)) {
420 struct iphdr *ip = ip_hdr(skb);
08cd04bf 421
23312a3b 422 if (ip->protocol == IPPROTO_TCP)
423 return TRANSPORT_INFO_IPV4_TCP;
424 else if (ip->protocol == IPPROTO_UDP)
425 return TRANSPORT_INFO_IPV4_UDP;
08cd04bf 426 } else {
23312a3b 427 struct ipv6hdr *ip6 = ipv6_hdr(skb);
428
429 if (ip6->nexthdr == IPPROTO_TCP)
430 return TRANSPORT_INFO_IPV6_TCP;
37b9dfa0 431 else if (ip6->nexthdr == IPPROTO_UDP)
23312a3b 432 return TRANSPORT_INFO_IPV6_UDP;
08cd04bf
KS
433 }
434
23312a3b 435 return TRANSPORT_INFO_NOT_IP;
08cd04bf
KS
436}
437
0c195567 438/* Send skb on the slave VF device. */
439static int netvsc_vf_xmit(struct net_device *net, struct net_device *vf_netdev,
440 struct sk_buff *skb)
441{
442 struct net_device_context *ndev_ctx = netdev_priv(net);
443 unsigned int len = skb->len;
444 int rc;
445
446 skb->dev = vf_netdev;
447 skb->queue_mapping = qdisc_skb_cb(skb)->slave_dev_queue_mapping;
448
449 rc = dev_queue_xmit(skb);
450 if (likely(rc == NET_XMIT_SUCCESS || rc == NET_XMIT_CN)) {
451 struct netvsc_vf_pcpu_stats *pcpu_stats
452 = this_cpu_ptr(ndev_ctx->vf_stats);
453
454 u64_stats_update_begin(&pcpu_stats->syncp);
455 pcpu_stats->tx_packets++;
456 pcpu_stats->tx_bytes += len;
457 u64_stats_update_end(&pcpu_stats->syncp);
458 } else {
459 this_cpu_inc(ndev_ctx->vf_stats->tx_dropped);
460 }
461
462 return rc;
463}
464
02fafbc6 465static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
fceaf24a 466{
fceaf24a 467 struct net_device_context *net_device_ctx = netdev_priv(net);
981a1bd8 468 struct hv_netvsc_packet *packet = NULL;
02fafbc6 469 int ret;
8a00251a
KS
470 unsigned int num_data_pgs;
471 struct rndis_message *rndis_msg;
472 struct rndis_packet *rndis_pkt;
0c195567 473 struct net_device *vf_netdev;
8a00251a 474 u32 rndis_msg_size;
8a00251a 475 struct rndis_per_packet_info *ppi;
307f0995 476 u32 hash;
02b6de01 477 struct hv_page_buffer pb[MAX_PAGE_BUFFER_COUNT];
fceaf24a 478
0c195567 479 /* if VF is present and up then redirect packets
480 * already called with rcu_read_lock_bh
481 */
482 vf_netdev = rcu_dereference_bh(net_device_ctx->vf_netdev);
483 if (vf_netdev && netif_running(vf_netdev) &&
484 !netpoll_tx_running(net))
485 return netvsc_vf_xmit(net, vf_netdev, skb);
486
80d887db 487 /* We will atmost need two pages to describe the rndis
488 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
e88f7e07
VK
489 * of pages in a single packet. If skb is scattered around
490 * more pages we try linearizing it.
54a7357f 491 */
80d887db 492
493 num_data_pgs = netvsc_get_slots(skb) + 2;
494
0ab05141 495 if (unlikely(num_data_pgs > MAX_PAGE_BUFFER_COUNT)) {
4323b47c
SH
496 ++net_device_ctx->eth_stats.tx_scattered;
497
498 if (skb_linearize(skb))
499 goto no_memory;
0ab05141 500
80d887db 501 num_data_pgs = netvsc_get_slots(skb) + 2;
0ab05141 502 if (num_data_pgs > MAX_PAGE_BUFFER_COUNT) {
4323b47c 503 ++net_device_ctx->eth_stats.tx_too_big;
0ab05141
SH
504 goto drop;
505 }
54a7357f 506 }
fceaf24a 507
c0eb4540
KS
508 /*
509 * Place the rndis header in the skb head room and
510 * the skb->cb will be used for hv_netvsc_packet
511 * structure.
512 */
513 ret = skb_cow_head(skb, RNDIS_AND_PPI_SIZE);
4323b47c
SH
514 if (ret)
515 goto no_memory;
516
c0eb4540
KS
517 /* Use the skb control buffer for building up the packet */
518 BUILD_BUG_ON(sizeof(struct hv_netvsc_packet) >
519 FIELD_SIZEOF(struct sk_buff, cb));
520 packet = (struct hv_netvsc_packet *)skb->cb;
fceaf24a 521
5b54dac8
HZ
522 packet->q_idx = skb_get_queue_mapping(skb);
523
4d447c9a 524 packet->total_data_buflen = skb->len;
793e3955 525 packet->total_bytes = skb->len;
526 packet->total_packets = 1;
fceaf24a 527
c0eb4540 528 rndis_msg = (struct rndis_message *)skb->head;
b08cc791 529
24476760 530 memset(rndis_msg, 0, RNDIS_AND_PPI_SIZE);
fceaf24a 531
8a00251a 532 /* Add the rndis header */
8a00251a
KS
533 rndis_msg->ndis_msg_type = RNDIS_MSG_PACKET;
534 rndis_msg->msg_len = packet->total_data_buflen;
535 rndis_pkt = &rndis_msg->msg.pkt;
536 rndis_pkt->data_offset = sizeof(struct rndis_packet);
537 rndis_pkt->data_len = packet->total_data_buflen;
538 rndis_pkt->per_pkt_info_offset = sizeof(struct rndis_packet);
539
540 rndis_msg_size = RNDIS_MESSAGE_SIZE(struct rndis_packet);
541
307f0995
HZ
542 hash = skb_get_hash_raw(skb);
543 if (hash != 0 && net->real_num_tx_queues > 1) {
544 rndis_msg_size += NDIS_HASH_PPI_SIZE;
545 ppi = init_ppi_data(rndis_msg, NDIS_HASH_PPI_SIZE,
546 NBL_HASH_VALUE);
547 *(u32 *)((void *)ppi + ppi->ppi_offset) = hash;
548 }
549
0ab05141 550 if (skb_vlan_tag_present(skb)) {
8a00251a
KS
551 struct ndis_pkt_8021q_info *vlan;
552
553 rndis_msg_size += NDIS_VLAN_PPI_SIZE;
554 ppi = init_ppi_data(rndis_msg, NDIS_VLAN_PPI_SIZE,
00f5024e 555 IEEE_8021Q_INFO);
556
557 vlan = (void *)ppi + ppi->ppi_offset;
760d1e36
KS
558 vlan->vlanid = skb->vlan_tci & VLAN_VID_MASK;
559 vlan->pri = (skb->vlan_tci & VLAN_PRIO_MASK) >>
8a00251a
KS
560 VLAN_PRIO_SHIFT;
561 }
562
23312a3b 563 if (skb_is_gso(skb)) {
0ab05141
SH
564 struct ndis_tcp_lso_info *lso_info;
565
566 rndis_msg_size += NDIS_LSO_PPI_SIZE;
567 ppi = init_ppi_data(rndis_msg, NDIS_LSO_PPI_SIZE,
568 TCP_LARGESEND_PKTINFO);
569
00f5024e 570 lso_info = (void *)ppi + ppi->ppi_offset;
0ab05141
SH
571
572 lso_info->lso_v2_transmit.type = NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE;
23312a3b 573 if (skb->protocol == htons(ETH_P_IP)) {
0ab05141
SH
574 lso_info->lso_v2_transmit.ip_version =
575 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4;
576 ip_hdr(skb)->tot_len = 0;
577 ip_hdr(skb)->check = 0;
578 tcp_hdr(skb)->check =
579 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
580 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
581 } else {
582 lso_info->lso_v2_transmit.ip_version =
583 NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6;
584 ipv6_hdr(skb)->payload_len = 0;
585 tcp_hdr(skb)->check =
586 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
587 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
588 }
23312a3b 589 lso_info->lso_v2_transmit.tcp_header_offset = skb_transport_offset(skb);
0ab05141 590 lso_info->lso_v2_transmit.mss = skb_shinfo(skb)->gso_size;
ad19bc8a 591 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
23312a3b 592 if (net_checksum_info(skb) & net_device_ctx->tx_checksum_mask) {
593 struct ndis_tcp_ip_checksum_info *csum_info;
594
ad19bc8a 595 rndis_msg_size += NDIS_CSUM_PPI_SIZE;
596 ppi = init_ppi_data(rndis_msg, NDIS_CSUM_PPI_SIZE,
597 TCPIP_CHKSUM_PKTINFO);
598
599 csum_info = (struct ndis_tcp_ip_checksum_info *)((void *)ppi +
600 ppi->ppi_offset);
601
23312a3b 602 csum_info->transmit.tcp_header_offset = skb_transport_offset(skb);
603
604 if (skb->protocol == htons(ETH_P_IP)) {
ad19bc8a 605 csum_info->transmit.is_ipv4 = 1;
23312a3b 606
607 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
608 csum_info->transmit.tcp_checksum = 1;
609 else
610 csum_info->transmit.udp_checksum = 1;
611 } else {
ad19bc8a 612 csum_info->transmit.is_ipv6 = 1;
613
23312a3b 614 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
615 csum_info->transmit.tcp_checksum = 1;
616 else
617 csum_info->transmit.udp_checksum = 1;
618 }
ad19bc8a 619 } else {
23312a3b 620 /* Can't do offload of this type of checksum */
ad19bc8a 621 if (skb_checksum_help(skb))
622 goto drop;
623 }
08cd04bf
KS
624 }
625
8a00251a
KS
626 /* Start filling in the page buffers with the rndis hdr */
627 rndis_msg->msg_len += rndis_msg_size;
942396b0 628 packet->total_data_buflen = rndis_msg->msg_len;
8a00251a 629 packet->page_buf_cnt = init_page_array(rndis_msg, rndis_msg_size,
02b6de01 630 skb, packet, pb);
8a00251a 631
76d13b56 632 /* timestamp packet in software */
633 skb_tx_timestamp(skb);
2a926f79 634
02b6de01 635 ret = netvsc_send(net_device_ctx, packet, rndis_msg, pb, skb);
793e3955 636 if (likely(ret == 0))
0ab05141 637 return NETDEV_TX_OK;
4323b47c
SH
638
639 if (ret == -EAGAIN) {
640 ++net_device_ctx->eth_stats.tx_busy;
0ab05141 641 return NETDEV_TX_BUSY;
4323b47c
SH
642 }
643
644 if (ret == -ENOSPC)
645 ++net_device_ctx->eth_stats.tx_no_space;
0ab05141
SH
646
647drop:
648 dev_kfree_skb_any(skb);
649 net->stats.tx_dropped++;
fceaf24a 650
0ab05141 651 return NETDEV_TX_OK;
4323b47c
SH
652
653no_memory:
654 ++net_device_ctx->eth_stats.tx_no_memory;
655 goto drop;
fceaf24a 656}
89bb42b1 657
3e189519 658/*
02fafbc6
GKH
659 * netvsc_linkstatus_callback - Link up/down notification
660 */
90ef117a 661void netvsc_linkstatus_callback(struct hv_device *device_obj,
3a494e71 662 struct rndis_message *resp)
fceaf24a 663{
3a494e71 664 struct rndis_indicate_status *indicate = &resp->msg.indicate_status;
2ddd5e5f 665 struct net_device *net;
c996edcf 666 struct net_device_context *ndev_ctx;
27a70af3
VK
667 struct netvsc_reconfig *event;
668 unsigned long flags;
891de74d 669
7f5d5af0
HZ
670 net = hv_get_drvdata(device_obj);
671
672 if (!net)
673 return;
674
675 ndev_ctx = netdev_priv(net);
676
677 /* Update the physical link speed when changing to another vSwitch */
678 if (indicate->status == RNDIS_STATUS_LINK_SPEED_CHANGE) {
679 u32 speed;
680
89bb42b1 681 speed = *(u32 *)((void *)indicate
682 + indicate->status_buf_offset) / 10000;
7f5d5af0
HZ
683 ndev_ctx->speed = speed;
684 return;
685 }
686
687 /* Handle these link change statuses below */
27a70af3
VK
688 if (indicate->status != RNDIS_STATUS_NETWORK_CHANGE &&
689 indicate->status != RNDIS_STATUS_MEDIA_CONNECT &&
690 indicate->status != RNDIS_STATUS_MEDIA_DISCONNECT)
3a494e71 691 return;
891de74d 692
7f5d5af0 693 if (net->reg_state != NETREG_REGISTERED)
fceaf24a 694 return;
fceaf24a 695
27a70af3
VK
696 event = kzalloc(sizeof(*event), GFP_ATOMIC);
697 if (!event)
698 return;
699 event->event = indicate->status;
700
701 spin_lock_irqsave(&ndev_ctx->lock, flags);
702 list_add_tail(&event->list, &ndev_ctx->reconfig_events);
703 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
704
705 schedule_delayed_work(&ndev_ctx->dwork, 0);
fceaf24a
HJ
706}
707
84bf9cef 708static struct sk_buff *netvsc_alloc_recv_skb(struct net_device *net,
e91e7dd7 709 struct napi_struct *napi,
dc54a08c 710 const struct ndis_tcp_ip_checksum_info *csum_info,
711 const struct ndis_pkt_8021q_info *vlan,
712 void *data, u32 buflen)
fceaf24a 713{
fceaf24a 714 struct sk_buff *skb;
fceaf24a 715
e91e7dd7 716 skb = napi_alloc_skb(napi, buflen);
84bf9cef
KS
717 if (!skb)
718 return skb;
fceaf24a 719
02fafbc6
GKH
720 /*
721 * Copy to skb. This copy is needed here since the memory pointed by
722 * hv_netvsc_packet cannot be deallocated
723 */
59ae1d12 724 skb_put_data(skb, data, buflen);
fceaf24a
HJ
725
726 skb->protocol = eth_type_trans(skb, net);
e52fed71
SH
727
728 /* skb is already created with CHECKSUM_NONE */
729 skb_checksum_none_assert(skb);
730
731 /*
732 * In Linux, the IP checksum is always checked.
733 * Do L4 checksum offload if enabled and present.
734 */
735 if (csum_info && (net->features & NETIF_F_RXCSUM)) {
736 if (csum_info->receive.tcp_checksum_succeeded ||
737 csum_info->receive.udp_checksum_succeeded)
e3d605ed 738 skb->ip_summed = CHECKSUM_UNNECESSARY;
e3d605ed
KS
739 }
740
dc54a08c 741 if (vlan) {
742 u16 vlan_tci = vlan->vlanid | (vlan->pri << VLAN_PRIO_SHIFT);
743
93725cbd 744 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
760d1e36 745 vlan_tci);
dc54a08c 746 }
fceaf24a 747
84bf9cef
KS
748 return skb;
749}
750
751/*
752 * netvsc_recv_callback - Callback when we receive a packet from the
753 * "wire" on the specified device.
754 */
dc54a08c 755int netvsc_recv_callback(struct net_device *net,
756 struct vmbus_channel *channel,
757 void *data, u32 len,
758 const struct ndis_tcp_ip_checksum_info *csum_info,
759 const struct ndis_pkt_8021q_info *vlan)
84bf9cef 760{
3d541ac5 761 struct net_device_context *net_device_ctx = netdev_priv(net);
545a8e79 762 struct netvsc_device *net_device;
742fe54c 763 u16 q_idx = channel->offermsg.offer.sub_channel_index;
545a8e79 764 struct netvsc_channel *nvchan;
84bf9cef 765 struct sk_buff *skb;
84bf9cef 766 struct netvsc_stats *rx_stats;
84bf9cef 767
9cbcc428 768 if (net->reg_state != NETREG_REGISTERED)
84bf9cef
KS
769 return NVSP_STAT_FAIL;
770
0719e72c 771 rcu_read_lock();
545a8e79 772 net_device = rcu_dereference(net_device_ctx->nvdev);
773 if (unlikely(!net_device))
774 goto drop;
775
776 nvchan = &net_device->chan_table[q_idx];
84bf9cef
KS
777
778 /* Allocate a skb - TODO direct I/O to pages? */
e91e7dd7 779 skb = netvsc_alloc_recv_skb(net, &nvchan->napi,
780 csum_info, vlan, data, len);
84bf9cef 781 if (unlikely(!skb)) {
545a8e79 782drop:
84bf9cef 783 ++net->stats.rx_dropped;
0719e72c 784 rcu_read_unlock();
84bf9cef
KS
785 return NVSP_STAT_FAIL;
786 }
5b54dac8 787
0c195567 788 skb_record_rx_queue(skb, q_idx);
9cbcc428
SH
789
790 /*
791 * Even if injecting the packet, record the statistics
792 * on the synthetic device because modifying the VF device
793 * statistics will not work correctly.
794 */
742fe54c 795 rx_stats = &nvchan->rx_stats;
4b02b58b 796 u64_stats_update_begin(&rx_stats->syncp);
7eafd9b4 797 rx_stats->packets++;
dc54a08c 798 rx_stats->bytes += len;
f7ad75b7
SH
799
800 if (skb->pkt_type == PACKET_BROADCAST)
801 ++rx_stats->broadcast;
802 else if (skb->pkt_type == PACKET_MULTICAST)
803 ++rx_stats->multicast;
4b02b58b 804 u64_stats_update_end(&rx_stats->syncp);
9495c282 805
742fe54c 806 napi_gro_receive(&nvchan->napi, skb);
0719e72c 807 rcu_read_unlock();
fceaf24a 808
fceaf24a
HJ
809 return 0;
810}
811
f82f4ad7
SH
812static void netvsc_get_drvinfo(struct net_device *net,
813 struct ethtool_drvinfo *info)
814{
7826d43f 815 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
7826d43f 816 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
f82f4ad7
SH
817}
818
59995370
AS
819static void netvsc_get_channels(struct net_device *net,
820 struct ethtool_channels *channel)
821{
822 struct net_device_context *net_device_ctx = netdev_priv(net);
545a8e79 823 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
59995370
AS
824
825 if (nvdev) {
826 channel->max_combined = nvdev->max_chn;
827 channel->combined_count = nvdev->num_chn;
828 }
829}
830
b5960e6e
AS
831static int netvsc_set_channels(struct net_device *net,
832 struct ethtool_channels *channels)
833{
834 struct net_device_context *net_device_ctx = netdev_priv(net);
835 struct hv_device *dev = net_device_ctx->device_ctx;
545a8e79 836 struct netvsc_device *nvdev = rtnl_dereference(net_device_ctx->nvdev);
7ca45933 837 unsigned int orig, count = channels->combined_count;
838 struct netvsc_device_info device_info;
ea383bf1 839 bool was_opened;
7ca45933 840 int ret = 0;
2b01888d 841
842 /* We do not support separate count for rx, tx, or other */
843 if (count == 0 ||
844 channels->rx_count || channels->tx_count || channels->other_count)
845 return -EINVAL;
846
a0be450e 847 if (!nvdev || nvdev->destroy)
b5960e6e
AS
848 return -ENODEV;
849
2b01888d 850 if (nvdev->nvsp_version < NVSP_PROTOCOL_VERSION_5)
b5960e6e 851 return -EINVAL;
b5960e6e 852
2b01888d 853 if (count > nvdev->max_chn)
b5960e6e
AS
854 return -EINVAL;
855
7ca45933 856 orig = nvdev->num_chn;
ea383bf1 857 was_opened = rndis_filter_opened(nvdev);
858 if (was_opened)
859 rndis_filter_close(nvdev);
b5960e6e 860
7ca45933 861 memset(&device_info, 0, sizeof(device_info));
862 device_info.num_chn = count;
863 device_info.ring_size = ring_size;
8b532797 864 device_info.send_sections = nvdev->send_section_cnt;
0ab09bef 865 device_info.send_section_size = nvdev->send_section_size;
8b532797 866 device_info.recv_sections = nvdev->recv_section_cnt;
0ab09bef 867 device_info.recv_section_size = nvdev->recv_section_size;
8b532797 868
869 rndis_filter_device_remove(dev, nvdev);
7ca45933 870
871 nvdev = rndis_filter_device_add(dev, &device_info);
8195b139 872 if (IS_ERR(nvdev)) {
d6aac1f2 873 ret = PTR_ERR(nvdev);
7ca45933 874 device_info.num_chn = orig;
68d715f6 875 nvdev = rndis_filter_device_add(dev, &device_info);
876
877 if (IS_ERR(nvdev)) {
878 netdev_err(net, "restoring channel setting failed: %ld\n",
879 PTR_ERR(nvdev));
880 return ret;
881 }
7ca45933 882 }
b5960e6e 883
ea383bf1 884 if (was_opened)
885 rndis_filter_open(nvdev);
163891d7 886
1bdcec8a 887 /* We may have missed link change notifications */
1b01994a 888 net_device_ctx->last_reconfig = 0;
1bdcec8a 889 schedule_delayed_work(&net_device_ctx->dwork, 0);
b5960e6e
AS
890
891 return ret;
b5960e6e
AS
892}
893
5e8456fd
PR
894static bool
895netvsc_validate_ethtool_ss_cmd(const struct ethtool_link_ksettings *cmd)
49eb9389 896{
5e8456fd
PR
897 struct ethtool_link_ksettings diff1 = *cmd;
898 struct ethtool_link_ksettings diff2 = {};
49eb9389 899
5e8456fd
PR
900 diff1.base.speed = 0;
901 diff1.base.duplex = 0;
49eb9389 902 /* advertising and cmd are usually set */
5e8456fd
PR
903 ethtool_link_ksettings_zero_link_mode(&diff1, advertising);
904 diff1.base.cmd = 0;
49eb9389 905 /* We set port to PORT_OTHER */
5e8456fd 906 diff2.base.port = PORT_OTHER;
49eb9389 907
908 return !memcmp(&diff1, &diff2, sizeof(diff1));
909}
910
911static void netvsc_init_settings(struct net_device *dev)
912{
913 struct net_device_context *ndc = netdev_priv(dev);
914
486e3981 915 ndc->l4_hash = HV_DEFAULT_L4HASH;
4823eb2f 916
49eb9389 917 ndc->speed = SPEED_UNKNOWN;
f3c9d40e 918 ndc->duplex = DUPLEX_FULL;
49eb9389 919}
920
5e8456fd
PR
921static int netvsc_get_link_ksettings(struct net_device *dev,
922 struct ethtool_link_ksettings *cmd)
49eb9389 923{
924 struct net_device_context *ndc = netdev_priv(dev);
925
5e8456fd
PR
926 cmd->base.speed = ndc->speed;
927 cmd->base.duplex = ndc->duplex;
928 cmd->base.port = PORT_OTHER;
49eb9389 929
930 return 0;
931}
932
5e8456fd
PR
933static int netvsc_set_link_ksettings(struct net_device *dev,
934 const struct ethtool_link_ksettings *cmd)
49eb9389 935{
936 struct net_device_context *ndc = netdev_priv(dev);
937 u32 speed;
938
5e8456fd 939 speed = cmd->base.speed;
49eb9389 940 if (!ethtool_validate_speed(speed) ||
5e8456fd 941 !ethtool_validate_duplex(cmd->base.duplex) ||
49eb9389 942 !netvsc_validate_ethtool_ss_cmd(cmd))
943 return -EINVAL;
944
945 ndc->speed = speed;
5e8456fd 946 ndc->duplex = cmd->base.duplex;
49eb9389 947
948 return 0;
949}
950
4d447c9a
HZ
951static int netvsc_change_mtu(struct net_device *ndev, int mtu)
952{
953 struct net_device_context *ndevctx = netdev_priv(ndev);
0c195567 954 struct net_device *vf_netdev = rtnl_dereference(ndevctx->vf_netdev);
545a8e79 955 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
3d541ac5 956 struct hv_device *hdev = ndevctx->device_ctx;
9749fed5 957 int orig_mtu = ndev->mtu;
4d447c9a 958 struct netvsc_device_info device_info;
ea383bf1 959 bool was_opened;
9749fed5 960 int ret = 0;
4d447c9a 961
a0be450e 962 if (!nvdev || nvdev->destroy)
4d447c9a
HZ
963 return -ENODEV;
964
0c195567 965 /* Change MTU of underlying VF netdev first. */
966 if (vf_netdev) {
967 ret = dev_set_mtu(vf_netdev, mtu);
968 if (ret)
969 return ret;
970 }
971
ea383bf1 972 netif_device_detach(ndev);
973 was_opened = rndis_filter_opened(nvdev);
974 if (was_opened)
975 rndis_filter_close(nvdev);
2de8530b 976
152669bd
DC
977 memset(&device_info, 0, sizeof(device_info));
978 device_info.ring_size = ring_size;
979 device_info.num_chn = nvdev->num_chn;
8b532797 980 device_info.send_sections = nvdev->send_section_cnt;
0ab09bef 981 device_info.send_section_size = nvdev->send_section_size;
8b532797 982 device_info.recv_sections = nvdev->recv_section_cnt;
0ab09bef 983 device_info.recv_section_size = nvdev->recv_section_size;
152669bd 984
2289f0aa 985 rndis_filter_device_remove(hdev, nvdev);
4d447c9a
HZ
986
987 ndev->mtu = mtu;
988
9749fed5 989 nvdev = rndis_filter_device_add(hdev, &device_info);
990 if (IS_ERR(nvdev)) {
991 ret = PTR_ERR(nvdev);
992
993 /* Attempt rollback to original MTU */
994 ndev->mtu = orig_mtu;
68d715f6 995 nvdev = rndis_filter_device_add(hdev, &device_info);
0c195567 996
997 if (vf_netdev)
998 dev_set_mtu(vf_netdev, orig_mtu);
68d715f6 999
1000 if (IS_ERR(nvdev)) {
1001 netdev_err(ndev, "restoring mtu failed: %ld\n",
1002 PTR_ERR(nvdev));
1003 return ret;
1004 }
9749fed5 1005 }
4d447c9a 1006
ea383bf1 1007 if (was_opened)
1008 rndis_filter_open(nvdev);
1009
1010 netif_device_attach(ndev);
163891d7 1011
1bdcec8a
VK
1012 /* We may have missed link change notifications */
1013 schedule_delayed_work(&ndevctx->dwork, 0);
1014
9749fed5 1015 return ret;
4d447c9a
HZ
1016}
1017
0c195567 1018static void netvsc_get_vf_stats(struct net_device *net,
1019 struct netvsc_vf_pcpu_stats *tot)
1020{
1021 struct net_device_context *ndev_ctx = netdev_priv(net);
1022 int i;
1023
1024 memset(tot, 0, sizeof(*tot));
1025
1026 for_each_possible_cpu(i) {
1027 const struct netvsc_vf_pcpu_stats *stats
1028 = per_cpu_ptr(ndev_ctx->vf_stats, i);
1029 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1030 unsigned int start;
1031
1032 do {
1033 start = u64_stats_fetch_begin_irq(&stats->syncp);
1034 rx_packets = stats->rx_packets;
1035 tx_packets = stats->tx_packets;
1036 rx_bytes = stats->rx_bytes;
1037 tx_bytes = stats->tx_bytes;
1038 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1039
1040 tot->rx_packets += rx_packets;
1041 tot->tx_packets += tx_packets;
1042 tot->rx_bytes += rx_bytes;
1043 tot->tx_bytes += tx_bytes;
1044 tot->tx_dropped += stats->tx_dropped;
1045 }
1046}
1047
bc1f4470 1048static void netvsc_get_stats64(struct net_device *net,
1049 struct rtnl_link_stats64 *t)
7eafd9b4 1050{
1051 struct net_device_context *ndev_ctx = netdev_priv(net);
776e726b 1052 struct netvsc_device *nvdev = rcu_dereference_rtnl(ndev_ctx->nvdev);
0c195567 1053 struct netvsc_vf_pcpu_stats vf_tot;
89bb42b1 1054 int i;
6c80f3fc
SX
1055
1056 if (!nvdev)
1057 return;
1058
0c195567 1059 netdev_stats_to_stats64(t, &net->stats);
1060
1061 netvsc_get_vf_stats(net, &vf_tot);
1062 t->rx_packets += vf_tot.rx_packets;
1063 t->tx_packets += vf_tot.tx_packets;
1064 t->rx_bytes += vf_tot.rx_bytes;
1065 t->tx_bytes += vf_tot.tx_bytes;
1066 t->tx_dropped += vf_tot.tx_dropped;
1067
6c80f3fc
SX
1068 for (i = 0; i < nvdev->num_chn; i++) {
1069 const struct netvsc_channel *nvchan = &nvdev->chan_table[i];
1070 const struct netvsc_stats *stats;
1071 u64 packets, bytes, multicast;
7eafd9b4 1072 unsigned int start;
1073
6c80f3fc 1074 stats = &nvchan->tx_stats;
7eafd9b4 1075 do {
6c80f3fc
SX
1076 start = u64_stats_fetch_begin_irq(&stats->syncp);
1077 packets = stats->packets;
1078 bytes = stats->bytes;
1079 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1080
1081 t->tx_bytes += bytes;
1082 t->tx_packets += packets;
7eafd9b4 1083
6c80f3fc 1084 stats = &nvchan->rx_stats;
7eafd9b4 1085 do {
6c80f3fc
SX
1086 start = u64_stats_fetch_begin_irq(&stats->syncp);
1087 packets = stats->packets;
1088 bytes = stats->bytes;
1089 multicast = stats->multicast + stats->broadcast;
1090 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
1091
1092 t->rx_bytes += bytes;
1093 t->rx_packets += packets;
1094 t->multicast += multicast;
7eafd9b4 1095 }
7eafd9b4 1096}
1ce09e89
HZ
1097
1098static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
1099{
867047c4 1100 struct net_device_context *ndc = netdev_priv(ndev);
16ba3266 1101 struct net_device *vf_netdev = rtnl_dereference(ndc->vf_netdev);
867047c4 1102 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
1ce09e89 1103 struct sockaddr *addr = p;
1ce09e89
HZ
1104 int err;
1105
16ba3266 1106 err = eth_prepare_mac_addr_change(ndev, p);
1107 if (err)
1ce09e89
HZ
1108 return err;
1109
867047c4 1110 if (!nvdev)
1111 return -ENODEV;
1112
16ba3266 1113 if (vf_netdev) {
1114 err = dev_set_mac_address(vf_netdev, addr);
1115 if (err)
1116 return err;
1117 }
1118
867047c4 1119 err = rndis_filter_set_device_mac(nvdev, addr->sa_data);
16ba3266 1120 if (!err) {
1121 eth_commit_mac_addr_change(ndev, p);
1122 } else if (vf_netdev) {
1123 /* rollback change on VF */
1124 memcpy(addr->sa_data, ndev->dev_addr, ETH_ALEN);
1125 dev_set_mac_address(vf_netdev, addr);
1ce09e89
HZ
1126 }
1127
1128 return err;
1129}
1130
4323b47c
SH
1131static const struct {
1132 char name[ETH_GSTRING_LEN];
1133 u16 offset;
1134} netvsc_stats[] = {
1135 { "tx_scattered", offsetof(struct netvsc_ethtool_stats, tx_scattered) },
1136 { "tx_no_memory", offsetof(struct netvsc_ethtool_stats, tx_no_memory) },
1137 { "tx_no_space", offsetof(struct netvsc_ethtool_stats, tx_no_space) },
1138 { "tx_too_big", offsetof(struct netvsc_ethtool_stats, tx_too_big) },
1139 { "tx_busy", offsetof(struct netvsc_ethtool_stats, tx_busy) },
cad5c197 1140 { "tx_send_full", offsetof(struct netvsc_ethtool_stats, tx_send_full) },
1141 { "rx_comp_busy", offsetof(struct netvsc_ethtool_stats, rx_comp_busy) },
09af87d1
SX
1142 { "stop_queue", offsetof(struct netvsc_ethtool_stats, stop_queue) },
1143 { "wake_queue", offsetof(struct netvsc_ethtool_stats, wake_queue) },
0c195567 1144}, vf_stats[] = {
1145 { "vf_rx_packets", offsetof(struct netvsc_vf_pcpu_stats, rx_packets) },
1146 { "vf_rx_bytes", offsetof(struct netvsc_vf_pcpu_stats, rx_bytes) },
1147 { "vf_tx_packets", offsetof(struct netvsc_vf_pcpu_stats, tx_packets) },
1148 { "vf_tx_bytes", offsetof(struct netvsc_vf_pcpu_stats, tx_bytes) },
1149 { "vf_tx_dropped", offsetof(struct netvsc_vf_pcpu_stats, tx_dropped) },
4323b47c
SH
1150};
1151
6c80f3fc 1152#define NETVSC_GLOBAL_STATS_LEN ARRAY_SIZE(netvsc_stats)
0c195567 1153#define NETVSC_VF_STATS_LEN ARRAY_SIZE(vf_stats)
6c80f3fc
SX
1154
1155/* 4 statistics per queue (rx/tx packets/bytes) */
1156#define NETVSC_QUEUE_STATS_LEN(dev) ((dev)->num_chn * 4)
1157
4323b47c
SH
1158static int netvsc_get_sset_count(struct net_device *dev, int string_set)
1159{
6c80f3fc 1160 struct net_device_context *ndc = netdev_priv(dev);
fbd4c7e7 1161 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
545a8e79 1162
1163 if (!nvdev)
1164 return -ENODEV;
6c80f3fc 1165
4323b47c
SH
1166 switch (string_set) {
1167 case ETH_SS_STATS:
0c195567 1168 return NETVSC_GLOBAL_STATS_LEN
1169 + NETVSC_VF_STATS_LEN
1170 + NETVSC_QUEUE_STATS_LEN(nvdev);
4323b47c
SH
1171 default:
1172 return -EINVAL;
1173 }
1174}
1175
1176static void netvsc_get_ethtool_stats(struct net_device *dev,
1177 struct ethtool_stats *stats, u64 *data)
1178{
1179 struct net_device_context *ndc = netdev_priv(dev);
867047c4 1180 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
4323b47c 1181 const void *nds = &ndc->eth_stats;
6c80f3fc 1182 const struct netvsc_stats *qstats;
0c195567 1183 struct netvsc_vf_pcpu_stats sum;
6c80f3fc
SX
1184 unsigned int start;
1185 u64 packets, bytes;
1186 int i, j;
4323b47c 1187
545a8e79 1188 if (!nvdev)
1189 return;
1190
6c80f3fc 1191 for (i = 0; i < NETVSC_GLOBAL_STATS_LEN; i++)
4323b47c 1192 data[i] = *(unsigned long *)(nds + netvsc_stats[i].offset);
6c80f3fc 1193
0c195567 1194 netvsc_get_vf_stats(dev, &sum);
1195 for (j = 0; j < NETVSC_VF_STATS_LEN; j++)
1196 data[i++] = *(u64 *)((void *)&sum + vf_stats[j].offset);
1197
6c80f3fc
SX
1198 for (j = 0; j < nvdev->num_chn; j++) {
1199 qstats = &nvdev->chan_table[j].tx_stats;
1200
1201 do {
1202 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1203 packets = qstats->packets;
1204 bytes = qstats->bytes;
1205 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1206 data[i++] = packets;
1207 data[i++] = bytes;
1208
1209 qstats = &nvdev->chan_table[j].rx_stats;
1210 do {
1211 start = u64_stats_fetch_begin_irq(&qstats->syncp);
1212 packets = qstats->packets;
1213 bytes = qstats->bytes;
1214 } while (u64_stats_fetch_retry_irq(&qstats->syncp, start));
1215 data[i++] = packets;
1216 data[i++] = bytes;
1217 }
4323b47c
SH
1218}
1219
1220static void netvsc_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1221{
6c80f3fc 1222 struct net_device_context *ndc = netdev_priv(dev);
867047c4 1223 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
6c80f3fc 1224 u8 *p = data;
4323b47c
SH
1225 int i;
1226
545a8e79 1227 if (!nvdev)
1228 return;
1229
4323b47c
SH
1230 switch (stringset) {
1231 case ETH_SS_STATS:
0c195567 1232 for (i = 0; i < ARRAY_SIZE(netvsc_stats); i++) {
1233 memcpy(p, netvsc_stats[i].name, ETH_GSTRING_LEN);
1234 p += ETH_GSTRING_LEN;
1235 }
1236
1237 for (i = 0; i < ARRAY_SIZE(vf_stats); i++) {
1238 memcpy(p, vf_stats[i].name, ETH_GSTRING_LEN);
1239 p += ETH_GSTRING_LEN;
1240 }
6c80f3fc 1241
6c80f3fc
SX
1242 for (i = 0; i < nvdev->num_chn; i++) {
1243 sprintf(p, "tx_queue_%u_packets", i);
1244 p += ETH_GSTRING_LEN;
1245 sprintf(p, "tx_queue_%u_bytes", i);
1246 p += ETH_GSTRING_LEN;
1247 sprintf(p, "rx_queue_%u_packets", i);
1248 p += ETH_GSTRING_LEN;
1249 sprintf(p, "rx_queue_%u_bytes", i);
1250 p += ETH_GSTRING_LEN;
1251 }
1252
4323b47c
SH
1253 break;
1254 }
1255}
1256
b5a5dc8d 1257static int
4823eb2f
HZ
1258netvsc_get_rss_hash_opts(struct net_device_context *ndc,
1259 struct ethtool_rxnfc *info)
b5a5dc8d 1260{
486e3981
HZ
1261 const u32 l4_flag = RXH_L4_B_0_1 | RXH_L4_B_2_3;
1262
b5a5dc8d 1263 info->data = RXH_IP_SRC | RXH_IP_DST;
1264
1265 switch (info->flow_type) {
1266 case TCP_V4_FLOW:
0518ec4f
HZ
1267 if (ndc->l4_hash & HV_TCP4_L4HASH)
1268 info->data |= l4_flag;
1269
1270 break;
1271
b5a5dc8d 1272 case TCP_V6_FLOW:
0518ec4f
HZ
1273 if (ndc->l4_hash & HV_TCP6_L4HASH)
1274 info->data |= l4_flag;
1275
4823eb2f
HZ
1276 break;
1277
b5a5dc8d 1278 case UDP_V4_FLOW:
486e3981
HZ
1279 if (ndc->l4_hash & HV_UDP4_L4HASH)
1280 info->data |= l4_flag;
4823eb2f
HZ
1281
1282 break;
1283
b5a5dc8d 1284 case UDP_V6_FLOW:
486e3981
HZ
1285 if (ndc->l4_hash & HV_UDP6_L4HASH)
1286 info->data |= l4_flag;
4823eb2f
HZ
1287
1288 break;
1289
b5a5dc8d 1290 case IPV4_FLOW:
1291 case IPV6_FLOW:
1292 break;
1293 default:
1294 info->data = 0;
1295 break;
1296 }
1297
1298 return 0;
1299}
1300
b448f4e8 1301static int
1302netvsc_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1303 u32 *rules)
1304{
1305 struct net_device_context *ndc = netdev_priv(dev);
867047c4 1306 struct netvsc_device *nvdev = rtnl_dereference(ndc->nvdev);
545a8e79 1307
1308 if (!nvdev)
1309 return -ENODEV;
b448f4e8 1310
1311 switch (info->cmd) {
1312 case ETHTOOL_GRXRINGS:
1313 info->data = nvdev->num_chn;
1314 return 0;
b5a5dc8d 1315
1316 case ETHTOOL_GRXFH:
4823eb2f 1317 return netvsc_get_rss_hash_opts(ndc, info);
b448f4e8 1318 }
1319 return -EOPNOTSUPP;
1320}
1321
4823eb2f
HZ
1322static int netvsc_set_rss_hash_opts(struct net_device_context *ndc,
1323 struct ethtool_rxnfc *info)
1324{
1325 if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1326 RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
486e3981 1327 switch (info->flow_type) {
0518ec4f
HZ
1328 case TCP_V4_FLOW:
1329 ndc->l4_hash |= HV_TCP4_L4HASH;
1330 break;
1331
1332 case TCP_V6_FLOW:
1333 ndc->l4_hash |= HV_TCP6_L4HASH;
1334 break;
1335
486e3981
HZ
1336 case UDP_V4_FLOW:
1337 ndc->l4_hash |= HV_UDP4_L4HASH;
1338 break;
1339
1340 case UDP_V6_FLOW:
1341 ndc->l4_hash |= HV_UDP6_L4HASH;
1342 break;
1343
1344 default:
4823eb2f 1345 return -EOPNOTSUPP;
486e3981 1346 }
4823eb2f
HZ
1347
1348 return 0;
1349 }
1350
1351 if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
486e3981 1352 switch (info->flow_type) {
0518ec4f
HZ
1353 case TCP_V4_FLOW:
1354 ndc->l4_hash &= ~HV_TCP4_L4HASH;
1355 break;
1356
1357 case TCP_V6_FLOW:
1358 ndc->l4_hash &= ~HV_TCP6_L4HASH;
1359 break;
1360
486e3981
HZ
1361 case UDP_V4_FLOW:
1362 ndc->l4_hash &= ~HV_UDP4_L4HASH;
1363 break;
1364
1365 case UDP_V6_FLOW:
1366 ndc->l4_hash &= ~HV_UDP6_L4HASH;
1367 break;
1368
1369 default:
4823eb2f 1370 return -EOPNOTSUPP;
486e3981 1371 }
4823eb2f
HZ
1372
1373 return 0;
1374 }
1375
1376 return -EOPNOTSUPP;
1377}
1378
1379static int
1380netvsc_set_rxnfc(struct net_device *ndev, struct ethtool_rxnfc *info)
1381{
1382 struct net_device_context *ndc = netdev_priv(ndev);
1383
1384 if (info->cmd == ETHTOOL_SRXFH)
1385 return netvsc_set_rss_hash_opts(ndc, info);
1386
1387 return -EOPNOTSUPP;
1388}
1389
316158fe 1390#ifdef CONFIG_NET_POLL_CONTROLLER
a5ecd439 1391static void netvsc_poll_controller(struct net_device *dev)
316158fe 1392{
a5ecd439 1393 struct net_device_context *ndc = netdev_priv(dev);
1394 struct netvsc_device *ndev;
1395 int i;
1396
1397 rcu_read_lock();
1398 ndev = rcu_dereference(ndc->nvdev);
1399 if (ndev) {
1400 for (i = 0; i < ndev->num_chn; i++) {
1401 struct netvsc_channel *nvchan = &ndev->chan_table[i];
1402
1403 napi_schedule(&nvchan->napi);
1404 }
1405 }
1406 rcu_read_unlock();
316158fe
RW
1407}
1408#endif
1ce09e89 1409
962f3fee 1410static u32 netvsc_get_rxfh_key_size(struct net_device *dev)
1411{
1412 return NETVSC_HASH_KEYLEN;
1413}
1414
1415static u32 netvsc_rss_indir_size(struct net_device *dev)
1416{
ff4a4419 1417 return ITAB_NUM;
962f3fee 1418}
1419
1420static int netvsc_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
1421 u8 *hfunc)
1422{
1423 struct net_device_context *ndc = netdev_priv(dev);
867047c4 1424 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
eb996edb 1425 struct rndis_device *rndis_dev;
ff4a4419 1426 int i;
962f3fee 1427
545a8e79 1428 if (!ndev)
1429 return -ENODEV;
1430
962f3fee 1431 if (hfunc)
1432 *hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
1433
eb996edb 1434 rndis_dev = ndev->extension;
ff4a4419 1435 if (indir) {
1436 for (i = 0; i < ITAB_NUM; i++)
47371300 1437 indir[i] = rndis_dev->rx_table[i];
ff4a4419 1438 }
1439
962f3fee 1440 if (key)
1441 memcpy(key, rndis_dev->rss_key, NETVSC_HASH_KEYLEN);
1442
1443 return 0;
1444}
1445
1446static int netvsc_set_rxfh(struct net_device *dev, const u32 *indir,
1447 const u8 *key, const u8 hfunc)
1448{
1449 struct net_device_context *ndc = netdev_priv(dev);
545a8e79 1450 struct netvsc_device *ndev = rtnl_dereference(ndc->nvdev);
eb996edb 1451 struct rndis_device *rndis_dev;
ff4a4419 1452 int i;
962f3fee 1453
545a8e79 1454 if (!ndev)
1455 return -ENODEV;
1456
962f3fee 1457 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1458 return -EOPNOTSUPP;
1459
eb996edb 1460 rndis_dev = ndev->extension;
ff4a4419 1461 if (indir) {
1462 for (i = 0; i < ITAB_NUM; i++)
db3cd7af 1463 if (indir[i] >= ndev->num_chn)
ff4a4419 1464 return -EINVAL;
1465
1466 for (i = 0; i < ITAB_NUM; i++)
47371300 1467 rndis_dev->rx_table[i] = indir[i];
ff4a4419 1468 }
1469
1470 if (!key) {
1471 if (!indir)
1472 return 0;
1473
1474 key = rndis_dev->rss_key;
1475 }
962f3fee 1476
715e2ec5 1477 return rndis_filter_set_rss_param(rndis_dev, key);
962f3fee 1478}
1479
8b532797 1480/* Hyper-V RNDIS protocol does not have ring in the HW sense.
1481 * It does have pre-allocated receive area which is divided into sections.
1482 */
1483static void __netvsc_get_ringparam(struct netvsc_device *nvdev,
1484 struct ethtool_ringparam *ring)
1485{
1486 u32 max_buf_size;
1487
1488 ring->rx_pending = nvdev->recv_section_cnt;
1489 ring->tx_pending = nvdev->send_section_cnt;
1490
1491 if (nvdev->nvsp_version <= NVSP_PROTOCOL_VERSION_2)
1492 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE_LEGACY;
1493 else
1494 max_buf_size = NETVSC_RECEIVE_BUFFER_SIZE;
1495
1496 ring->rx_max_pending = max_buf_size / nvdev->recv_section_size;
1497 ring->tx_max_pending = NETVSC_SEND_BUFFER_SIZE
1498 / nvdev->send_section_size;
1499}
1500
1501static void netvsc_get_ringparam(struct net_device *ndev,
1502 struct ethtool_ringparam *ring)
1503{
1504 struct net_device_context *ndevctx = netdev_priv(ndev);
1505 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1506
1507 if (!nvdev)
1508 return;
1509
1510 __netvsc_get_ringparam(nvdev, ring);
1511}
1512
1513static int netvsc_set_ringparam(struct net_device *ndev,
1514 struct ethtool_ringparam *ring)
1515{
1516 struct net_device_context *ndevctx = netdev_priv(ndev);
1517 struct netvsc_device *nvdev = rtnl_dereference(ndevctx->nvdev);
1518 struct hv_device *hdev = ndevctx->device_ctx;
1519 struct netvsc_device_info device_info;
1520 struct ethtool_ringparam orig;
1521 u32 new_tx, new_rx;
1522 bool was_opened;
1523 int ret = 0;
1524
1525 if (!nvdev || nvdev->destroy)
1526 return -ENODEV;
1527
1528 memset(&orig, 0, sizeof(orig));
1529 __netvsc_get_ringparam(nvdev, &orig);
1530
1531 new_tx = clamp_t(u32, ring->tx_pending,
1532 NETVSC_MIN_TX_SECTIONS, orig.tx_max_pending);
1533 new_rx = clamp_t(u32, ring->rx_pending,
1534 NETVSC_MIN_RX_SECTIONS, orig.rx_max_pending);
1535
1536 if (new_tx == orig.tx_pending &&
1537 new_rx == orig.rx_pending)
1538 return 0; /* no change */
1539
1540 memset(&device_info, 0, sizeof(device_info));
1541 device_info.num_chn = nvdev->num_chn;
1542 device_info.ring_size = ring_size;
1543 device_info.send_sections = new_tx;
0ab09bef 1544 device_info.send_section_size = nvdev->send_section_size;
8b532797 1545 device_info.recv_sections = new_rx;
0ab09bef 1546 device_info.recv_section_size = nvdev->recv_section_size;
8b532797 1547
1548 netif_device_detach(ndev);
1549 was_opened = rndis_filter_opened(nvdev);
1550 if (was_opened)
1551 rndis_filter_close(nvdev);
1552
1553 rndis_filter_device_remove(hdev, nvdev);
1554
1555 nvdev = rndis_filter_device_add(hdev, &device_info);
1556 if (IS_ERR(nvdev)) {
1557 ret = PTR_ERR(nvdev);
1558
1559 device_info.send_sections = orig.tx_pending;
1560 device_info.recv_sections = orig.rx_pending;
1561 nvdev = rndis_filter_device_add(hdev, &device_info);
1562 if (IS_ERR(nvdev)) {
1563 netdev_err(ndev, "restoring ringparam failed: %ld\n",
1564 PTR_ERR(nvdev));
1565 return ret;
1566 }
1567 }
1568
1569 if (was_opened)
1570 rndis_filter_open(nvdev);
1571 netif_device_attach(ndev);
1572
1573 /* We may have missed link change notifications */
1574 ndevctx->last_reconfig = 0;
1575 schedule_delayed_work(&ndevctx->dwork, 0);
1576
1577 return ret;
1578}
1579
f82f4ad7
SH
1580static const struct ethtool_ops ethtool_ops = {
1581 .get_drvinfo = netvsc_get_drvinfo,
f82f4ad7 1582 .get_link = ethtool_op_get_link,
4323b47c
SH
1583 .get_ethtool_stats = netvsc_get_ethtool_stats,
1584 .get_sset_count = netvsc_get_sset_count,
1585 .get_strings = netvsc_get_strings,
59995370 1586 .get_channels = netvsc_get_channels,
b5960e6e 1587 .set_channels = netvsc_set_channels,
76d13b56 1588 .get_ts_info = ethtool_op_get_ts_info,
b448f4e8 1589 .get_rxnfc = netvsc_get_rxnfc,
4823eb2f 1590 .set_rxnfc = netvsc_set_rxnfc,
962f3fee 1591 .get_rxfh_key_size = netvsc_get_rxfh_key_size,
1592 .get_rxfh_indir_size = netvsc_rss_indir_size,
1593 .get_rxfh = netvsc_get_rxfh,
1594 .set_rxfh = netvsc_set_rxfh,
5e8456fd
PR
1595 .get_link_ksettings = netvsc_get_link_ksettings,
1596 .set_link_ksettings = netvsc_set_link_ksettings,
8b532797 1597 .get_ringparam = netvsc_get_ringparam,
1598 .set_ringparam = netvsc_set_ringparam,
f82f4ad7
SH
1599};
1600
df2fff28
GKH
1601static const struct net_device_ops device_ops = {
1602 .ndo_open = netvsc_open,
1603 .ndo_stop = netvsc_close,
1604 .ndo_start_xmit = netvsc_start_xmit,
afc4b13d 1605 .ndo_set_rx_mode = netvsc_set_multicast_list,
4d447c9a 1606 .ndo_change_mtu = netvsc_change_mtu,
b681b588 1607 .ndo_validate_addr = eth_validate_addr,
1ce09e89 1608 .ndo_set_mac_address = netvsc_set_mac_addr,
5b54dac8 1609 .ndo_select_queue = netvsc_select_queue,
7eafd9b4 1610 .ndo_get_stats64 = netvsc_get_stats64,
316158fe
RW
1611#ifdef CONFIG_NET_POLL_CONTROLLER
1612 .ndo_poll_controller = netvsc_poll_controller,
1613#endif
df2fff28
GKH
1614};
1615
c996edcf 1616/*
27a70af3
VK
1617 * Handle link status changes. For RNDIS_STATUS_NETWORK_CHANGE emulate link
1618 * down/up sequence. In case of RNDIS_STATUS_MEDIA_CONNECT when carrier is
1619 * present send GARP packet to network peers with netif_notify_peers().
c996edcf 1620 */
891de74d 1621static void netvsc_link_change(struct work_struct *w)
c996edcf 1622{
0a1275ca
VK
1623 struct net_device_context *ndev_ctx =
1624 container_of(w, struct net_device_context, dwork.work);
1625 struct hv_device *device_obj = ndev_ctx->device_ctx;
1626 struct net_device *net = hv_get_drvdata(device_obj);
2ddd5e5f 1627 struct netvsc_device *net_device;
891de74d 1628 struct rndis_device *rdev;
27a70af3
VK
1629 struct netvsc_reconfig *event = NULL;
1630 bool notify = false, reschedule = false;
1631 unsigned long flags, next_reconfig, delay;
c996edcf 1632
9b4e946c 1633 /* if changes are happening, comeback later */
1634 if (!rtnl_trylock()) {
1635 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1636 return;
1637 }
1638
a0be450e 1639 net_device = rtnl_dereference(ndev_ctx->nvdev);
1640 if (!net_device)
1bdcec8a
VK
1641 goto out_unlock;
1642
891de74d 1643 rdev = net_device->extension;
891de74d 1644
27a70af3
VK
1645 next_reconfig = ndev_ctx->last_reconfig + LINKCHANGE_INT;
1646 if (time_is_after_jiffies(next_reconfig)) {
1647 /* link_watch only sends one notification with current state
1648 * per second, avoid doing reconfig more frequently. Handle
1649 * wrap around.
1650 */
1651 delay = next_reconfig - jiffies;
1652 delay = delay < LINKCHANGE_INT ? delay : LINKCHANGE_INT;
1653 schedule_delayed_work(&ndev_ctx->dwork, delay);
1bdcec8a 1654 goto out_unlock;
27a70af3
VK
1655 }
1656 ndev_ctx->last_reconfig = jiffies;
1657
1658 spin_lock_irqsave(&ndev_ctx->lock, flags);
1659 if (!list_empty(&ndev_ctx->reconfig_events)) {
1660 event = list_first_entry(&ndev_ctx->reconfig_events,
1661 struct netvsc_reconfig, list);
1662 list_del(&event->list);
1663 reschedule = !list_empty(&ndev_ctx->reconfig_events);
1664 }
1665 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1666
1667 if (!event)
1bdcec8a 1668 goto out_unlock;
27a70af3
VK
1669
1670 switch (event->event) {
1671 /* Only the following events are possible due to the check in
1672 * netvsc_linkstatus_callback()
1673 */
1674 case RNDIS_STATUS_MEDIA_CONNECT:
1675 if (rdev->link_state) {
1676 rdev->link_state = false;
0c195567 1677 netif_carrier_on(net);
27a70af3
VK
1678 netif_tx_wake_all_queues(net);
1679 } else {
1680 notify = true;
1681 }
1682 kfree(event);
1683 break;
1684 case RNDIS_STATUS_MEDIA_DISCONNECT:
1685 if (!rdev->link_state) {
1686 rdev->link_state = true;
1687 netif_carrier_off(net);
1688 netif_tx_stop_all_queues(net);
1689 }
1690 kfree(event);
1691 break;
1692 case RNDIS_STATUS_NETWORK_CHANGE:
1693 /* Only makes sense if carrier is present */
1694 if (!rdev->link_state) {
1695 rdev->link_state = true;
1696 netif_carrier_off(net);
1697 netif_tx_stop_all_queues(net);
1698 event->event = RNDIS_STATUS_MEDIA_CONNECT;
1699 spin_lock_irqsave(&ndev_ctx->lock, flags);
15cfd407 1700 list_add(&event->list, &ndev_ctx->reconfig_events);
27a70af3
VK
1701 spin_unlock_irqrestore(&ndev_ctx->lock, flags);
1702 reschedule = true;
3a494e71 1703 }
27a70af3 1704 break;
891de74d
HZ
1705 }
1706
1707 rtnl_unlock();
1708
1709 if (notify)
1710 netdev_notify_peers(net);
27a70af3
VK
1711
1712 /* link_watch only sends one notification with current state per
1713 * second, handle next reconfig event in 2 seconds.
1714 */
1715 if (reschedule)
1716 schedule_delayed_work(&ndev_ctx->dwork, LINKCHANGE_INT);
1bdcec8a
VK
1717
1718 return;
1719
1720out_unlock:
1721 rtnl_unlock();
c996edcf
HZ
1722}
1723
e8ff40d4 1724static struct net_device *get_netvsc_bymac(const u8 *mac)
84bf9cef 1725{
e8ff40d4 1726 struct net_device *dev;
84bf9cef 1727
8737caaf 1728 ASSERT_RTNL();
84bf9cef
KS
1729
1730 for_each_netdev(&init_net, dev) {
e8ff40d4
SH
1731 if (dev->netdev_ops != &device_ops)
1732 continue; /* not a netvsc device */
1733
1734 if (ether_addr_equal(mac, dev->perm_addr))
1735 return dev;
1736 }
1737
1738 return NULL;
1739}
1740
f207c10d 1741static struct net_device *get_netvsc_byref(struct net_device *vf_netdev)
e8ff40d4
SH
1742{
1743 struct net_device *dev;
1744
1745 ASSERT_RTNL();
1746
1747 for_each_netdev(&init_net, dev) {
1748 struct net_device_context *net_device_ctx;
1749
1750 if (dev->netdev_ops != &device_ops)
1751 continue; /* not a netvsc device */
1752
1753 net_device_ctx = netdev_priv(dev);
79e8cbe7 1754 if (!rtnl_dereference(net_device_ctx->nvdev))
e8ff40d4
SH
1755 continue; /* device is removed */
1756
f207c10d 1757 if (rtnl_dereference(net_device_ctx->vf_netdev) == vf_netdev)
e8ff40d4 1758 return dev; /* a match */
84bf9cef 1759 }
84bf9cef 1760
e8ff40d4 1761 return NULL;
84bf9cef
KS
1762}
1763
0c195567 1764/* Called when VF is injecting data into network stack.
1765 * Change the associated network device from VF to netvsc.
1766 * note: already called with rcu_read_lock
1767 */
1768static rx_handler_result_t netvsc_vf_handle_frame(struct sk_buff **pskb)
1769{
1770 struct sk_buff *skb = *pskb;
1771 struct net_device *ndev = rcu_dereference(skb->dev->rx_handler_data);
1772 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1773 struct netvsc_vf_pcpu_stats *pcpu_stats
1774 = this_cpu_ptr(ndev_ctx->vf_stats);
1775
1776 skb->dev = ndev;
1777
1778 u64_stats_update_begin(&pcpu_stats->syncp);
1779 pcpu_stats->rx_packets++;
1780 pcpu_stats->rx_bytes += skb->len;
1781 u64_stats_update_end(&pcpu_stats->syncp);
1782
1783 return RX_HANDLER_ANOTHER;
1784}
1785
1786static int netvsc_vf_join(struct net_device *vf_netdev,
1787 struct net_device *ndev)
1788{
1789 struct net_device_context *ndev_ctx = netdev_priv(ndev);
1790 int ret;
1791
1792 ret = netdev_rx_handler_register(vf_netdev,
1793 netvsc_vf_handle_frame, ndev);
1794 if (ret != 0) {
1795 netdev_err(vf_netdev,
1796 "can not register netvsc VF receive handler (err = %d)\n",
1797 ret);
1798 goto rx_handler_failed;
1799 }
1800
42ab19ee 1801 ret = netdev_upper_dev_link(vf_netdev, ndev, NULL);
0c195567 1802 if (ret != 0) {
1803 netdev_err(vf_netdev,
1804 "can not set master device %s (err = %d)\n",
1805 ndev->name, ret);
1806 goto upper_link_failed;
1807 }
1808
1809 /* set slave flag before open to prevent IPv6 addrconf */
1810 vf_netdev->flags |= IFF_SLAVE;
1811
6123c668 1812 schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
1813
1814 call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
0c195567 1815
1816 netdev_info(vf_netdev, "joined to %s\n", ndev->name);
1817 return 0;
1818
1819upper_link_failed:
1820 netdev_rx_handler_unregister(vf_netdev);
1821rx_handler_failed:
1822 return ret;
1823}
1824
1825static void __netvsc_vf_setup(struct net_device *ndev,
1826 struct net_device *vf_netdev)
1827{
1828 int ret;
1829
0c195567 1830 /* Align MTU of VF with master */
1831 ret = dev_set_mtu(vf_netdev, ndev->mtu);
1832 if (ret)
1833 netdev_warn(vf_netdev,
1834 "unable to change mtu to %u\n", ndev->mtu);
1835
1836 if (netif_running(ndev)) {
1837 ret = dev_open(vf_netdev);
1838 if (ret)
1839 netdev_warn(vf_netdev,
1840 "unable to open: %d\n", ret);
1841 }
1842}
1843
1844/* Setup VF as slave of the synthetic device.
1845 * Runs in workqueue to avoid recursion in netlink callbacks.
1846 */
1847static void netvsc_vf_setup(struct work_struct *w)
1848{
1849 struct net_device_context *ndev_ctx
6123c668 1850 = container_of(w, struct net_device_context, vf_takeover.work);
0c195567 1851 struct net_device *ndev = hv_get_drvdata(ndev_ctx->device_ctx);
1852 struct net_device *vf_netdev;
1853
fb84af8a 1854 if (!rtnl_trylock()) {
6123c668 1855 schedule_delayed_work(&ndev_ctx->vf_takeover, 0);
fb84af8a 1856 return;
1857 }
1858
0c195567 1859 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
1860 if (vf_netdev)
1861 __netvsc_vf_setup(ndev, vf_netdev);
1862
1863 rtnl_unlock();
1864}
1865
84bf9cef
KS
1866static int netvsc_register_vf(struct net_device *vf_netdev)
1867{
0a1275ca
VK
1868 struct net_device *ndev;
1869 struct net_device_context *net_device_ctx;
84bf9cef 1870 struct netvsc_device *netvsc_dev;
84bf9cef 1871
e8ff40d4
SH
1872 if (vf_netdev->addr_len != ETH_ALEN)
1873 return NOTIFY_DONE;
1874
84bf9cef
KS
1875 /*
1876 * We will use the MAC address to locate the synthetic interface to
1877 * associate with the VF interface. If we don't find a matching
1878 * synthetic interface, move on.
1879 */
e8ff40d4 1880 ndev = get_netvsc_bymac(vf_netdev->perm_addr);
0a1275ca
VK
1881 if (!ndev)
1882 return NOTIFY_DONE;
1883
1884 net_device_ctx = netdev_priv(ndev);
545a8e79 1885 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
f207c10d 1886 if (!netvsc_dev || rtnl_dereference(net_device_ctx->vf_netdev))
84bf9cef
KS
1887 return NOTIFY_DONE;
1888
0c195567 1889 if (netvsc_vf_join(vf_netdev, ndev) != 0)
1890 return NOTIFY_DONE;
1891
0a1275ca 1892 netdev_info(ndev, "VF registering: %s\n", vf_netdev->name);
0c195567 1893
07d0f000 1894 dev_hold(vf_netdev);
f207c10d 1895 rcu_assign_pointer(net_device_ctx->vf_netdev, vf_netdev);
84bf9cef
KS
1896 return NOTIFY_OK;
1897}
1898
9a0c48df
SH
1899/* VF up/down change detected, schedule to change data path */
1900static int netvsc_vf_changed(struct net_device *vf_netdev)
84bf9cef 1901{
7b83f520 1902 struct net_device_context *net_device_ctx;
84bf9cef 1903 struct netvsc_device *netvsc_dev;
7b83f520 1904 struct net_device *ndev;
9a0c48df 1905 bool vf_is_up = netif_running(vf_netdev);
fb84af8a 1906
7b83f520 1907 ndev = get_netvsc_byref(vf_netdev);
1908 if (!ndev)
1909 return NOTIFY_DONE;
0c195567 1910
7b83f520 1911 net_device_ctx = netdev_priv(ndev);
1912 netvsc_dev = rtnl_dereference(net_device_ctx->nvdev);
0c195567 1913 if (!netvsc_dev)
7b83f520 1914 return NOTIFY_DONE;
84bf9cef 1915
9a0c48df
SH
1916 netvsc_switch_datapath(ndev, vf_is_up);
1917 netdev_info(ndev, "Data path switched %s VF: %s\n",
1918 vf_is_up ? "to" : "from", vf_netdev->name);
84bf9cef
KS
1919
1920 return NOTIFY_OK;
1921}
1922
84bf9cef
KS
1923static int netvsc_unregister_vf(struct net_device *vf_netdev)
1924{
0a1275ca 1925 struct net_device *ndev;
0a1275ca 1926 struct net_device_context *net_device_ctx;
84bf9cef 1927
e8ff40d4 1928 ndev = get_netvsc_byref(vf_netdev);
0a1275ca
VK
1929 if (!ndev)
1930 return NOTIFY_DONE;
1931
1932 net_device_ctx = netdev_priv(ndev);
6123c668 1933 cancel_delayed_work_sync(&net_device_ctx->vf_takeover);
e8ff40d4 1934
0a1275ca 1935 netdev_info(ndev, "VF unregistering: %s\n", vf_netdev->name);
f207c10d 1936
ec158f77 1937 netdev_rx_handler_unregister(vf_netdev);
0c195567 1938 netdev_upper_dev_unlink(vf_netdev, ndev);
f207c10d 1939 RCU_INIT_POINTER(net_device_ctx->vf_netdev, NULL);
07d0f000 1940 dev_put(vf_netdev);
ec158f77 1941
84bf9cef
KS
1942 return NOTIFY_OK;
1943}
1944
84946899
S
1945static int netvsc_probe(struct hv_device *dev,
1946 const struct hv_vmbus_device_id *dev_id)
df2fff28 1947{
df2fff28
GKH
1948 struct net_device *net = NULL;
1949 struct net_device_context *net_device_ctx;
1950 struct netvsc_device_info device_info;
5b54dac8 1951 struct netvsc_device *nvdev;
0c195567 1952 int ret = -ENOMEM;
df2fff28 1953
5b54dac8 1954 net = alloc_etherdev_mq(sizeof(struct net_device_context),
2b01888d 1955 VRSS_CHANNEL_MAX);
df2fff28 1956 if (!net)
0c195567 1957 goto no_net;
df2fff28 1958
1b07da51
HZ
1959 netif_carrier_off(net);
1960
b37879e6
HZ
1961 netvsc_init_settings(net);
1962
df2fff28 1963 net_device_ctx = netdev_priv(net);
9efd21e1 1964 net_device_ctx->device_ctx = dev;
3f300ff4
SX
1965 net_device_ctx->msg_enable = netif_msg_init(debug, default_msg);
1966 if (netif_msg_probe(net_device_ctx))
1967 netdev_dbg(net, "netvsc msg_enable: %d\n",
1968 net_device_ctx->msg_enable);
1969
2ddd5e5f 1970 hv_set_drvdata(dev, net);
f580aec4 1971
891de74d 1972 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
df2fff28 1973
27a70af3
VK
1974 spin_lock_init(&net_device_ctx->lock);
1975 INIT_LIST_HEAD(&net_device_ctx->reconfig_events);
6123c668 1976 INIT_DELAYED_WORK(&net_device_ctx->vf_takeover, netvsc_vf_setup);
0c195567 1977
1978 net_device_ctx->vf_stats
1979 = netdev_alloc_pcpu_stats(struct netvsc_vf_pcpu_stats);
1980 if (!net_device_ctx->vf_stats)
1981 goto no_stats;
27a70af3 1982
df2fff28 1983 net->netdev_ops = &device_ops;
7ad24ea4 1984 net->ethtool_ops = &ethtool_ops;
9efd21e1 1985 SET_NETDEV_DEV(net, &dev->device);
df2fff28 1986
14a03cf8
VK
1987 /* We always need headroom for rndis header */
1988 net->needed_headroom = RNDIS_AND_PPI_SIZE;
1989
6450f8f2
HZ
1990 /* Initialize the number of queues to be 1, we may change it if more
1991 * channels are offered later.
1992 */
1993 netif_set_real_num_tx_queues(net, 1);
1994 netif_set_real_num_rx_queues(net, 1);
1995
692e084e 1996 /* Notify the netvsc driver of the new device */
8ebdcc52 1997 memset(&device_info, 0, sizeof(device_info));
692e084e 1998 device_info.ring_size = ring_size;
3071ada4 1999 device_info.num_chn = VRSS_CHANNEL_DEFAULT;
8b532797 2000 device_info.send_sections = NETVSC_DEFAULT_TX;
0ab09bef 2001 device_info.send_section_size = NETVSC_SEND_SECTION_SIZE;
8b532797 2002 device_info.recv_sections = NETVSC_DEFAULT_RX;
0ab09bef 2003 device_info.recv_section_size = NETVSC_RECV_SECTION_SIZE;
9749fed5 2004
2005 nvdev = rndis_filter_device_add(dev, &device_info);
2006 if (IS_ERR(nvdev)) {
2007 ret = PTR_ERR(nvdev);
692e084e 2008 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
0c195567 2009 goto rndis_failed;
df2fff28 2010 }
0c195567 2011
692e084e
HZ
2012 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
2013
23312a3b 2014 /* hw_features computed in rndis_filter_device_add */
2015 net->features = net->hw_features |
2016 NETIF_F_HIGHDMA | NETIF_F_SG |
2017 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
2018 net->vlan_features = net->features;
2019
9749fed5 2020 netdev_lockdep_set_classes(net);
2021
d0c2c997
JW
2022 /* MTU range: 68 - 1500 or 65521 */
2023 net->min_mtu = NETVSC_MTU_MIN;
2024 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
2025 net->max_mtu = NETVSC_MTU - ETH_HLEN;
2026 else
2027 net->max_mtu = ETH_DATA_LEN;
2028
a68f9614
HZ
2029 ret = register_netdev(net);
2030 if (ret != 0) {
2031 pr_err("Unable to register netdev.\n");
0c195567 2032 goto register_failed;
a68f9614
HZ
2033 }
2034
df2fff28 2035 return ret;
0c195567 2036
2037register_failed:
2038 rndis_filter_device_remove(dev, nvdev);
2039rndis_failed:
2040 free_percpu(net_device_ctx->vf_stats);
2041no_stats:
2042 hv_set_drvdata(dev, NULL);
2043 free_netdev(net);
2044no_net:
2045 return ret;
df2fff28
GKH
2046}
2047
415b023a 2048static int netvsc_remove(struct hv_device *dev)
df2fff28 2049{
122a5f64 2050 struct net_device_context *ndev_ctx;
ec158f77
SH
2051 struct net_device *vf_netdev;
2052 struct net_device *net;
2ddd5e5f 2053
3d541ac5 2054 net = hv_get_drvdata(dev);
df2fff28 2055 if (net == NULL) {
415b023a 2056 dev_err(&dev->device, "No net device to remove\n");
df2fff28
GKH
2057 return 0;
2058 }
2059
122a5f64 2060 ndev_ctx = netdev_priv(net);
3d541ac5 2061
a0be450e 2062 netif_device_detach(net);
f580aec4 2063
122a5f64
HZ
2064 cancel_delayed_work_sync(&ndev_ctx->dwork);
2065
df2fff28
GKH
2066 /*
2067 * Call to the vsc driver to let it know that the device is being
a0be450e 2068 * removed. Also blocks mtu and channel changes.
df2fff28 2069 */
a0be450e 2070 rtnl_lock();
ec158f77
SH
2071 vf_netdev = rtnl_dereference(ndev_ctx->vf_netdev);
2072 if (vf_netdev)
2073 netvsc_unregister_vf(vf_netdev);
2074
8195b139
SH
2075 unregister_netdevice(net);
2076
79e8cbe7 2077 rndis_filter_device_remove(dev,
2078 rtnl_dereference(ndev_ctx->nvdev));
a0be450e 2079 rtnl_unlock();
2080
3d541ac5
VK
2081 hv_set_drvdata(dev, NULL);
2082
0c195567 2083 free_percpu(ndev_ctx->vf_stats);
6c80f3fc 2084 free_netdev(net);
df06bcff 2085 return 0;
df2fff28
GKH
2086}
2087
345c4cc3 2088static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4 2089 /* Network guid */
8f505944 2090 { HV_NIC_GUID, },
c45cf2d4 2091 { },
345c4cc3
S
2092};
2093
2094MODULE_DEVICE_TABLE(vmbus, id_table);
2095
f1542a66 2096/* The one and only one */
fde0ef9b 2097static struct hv_driver netvsc_drv = {
d31b20fc 2098 .name = KBUILD_MODNAME,
345c4cc3 2099 .id_table = id_table,
fde0ef9b
S
2100 .probe = netvsc_probe,
2101 .remove = netvsc_remove,
d4890970 2102};
f1542a66 2103
84bf9cef
KS
2104/*
2105 * On Hyper-V, every VF interface is matched with a corresponding
2106 * synthetic interface. The synthetic interface is presented first
2107 * to the guest. When the corresponding VF instance is registered,
2108 * we will take care of switching the data path.
2109 */
2110static int netvsc_netdev_event(struct notifier_block *this,
2111 unsigned long event, void *ptr)
2112{
2113 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
2114
ee837a13
SH
2115 /* Skip our own events */
2116 if (event_dev->netdev_ops == &device_ops)
2117 return NOTIFY_DONE;
2118
2119 /* Avoid non-Ethernet type devices */
2120 if (event_dev->type != ARPHRD_ETHER)
2121 return NOTIFY_DONE;
2122
0dbff144 2123 /* Avoid Vlan dev with same MAC registering as VF */
d0d7b10b 2124 if (is_vlan_dev(event_dev))
0dbff144
VK
2125 return NOTIFY_DONE;
2126
2127 /* Avoid Bonding master dev with same MAC registering as VF */
ee837a13
SH
2128 if ((event_dev->priv_flags & IFF_BONDING) &&
2129 (event_dev->flags & IFF_MASTER))
cb2911fe
HZ
2130 return NOTIFY_DONE;
2131
84bf9cef
KS
2132 switch (event) {
2133 case NETDEV_REGISTER:
2134 return netvsc_register_vf(event_dev);
2135 case NETDEV_UNREGISTER:
2136 return netvsc_unregister_vf(event_dev);
2137 case NETDEV_UP:
84bf9cef 2138 case NETDEV_DOWN:
9a0c48df 2139 return netvsc_vf_changed(event_dev);
84bf9cef
KS
2140 default:
2141 return NOTIFY_DONE;
2142 }
2143}
2144
2145static struct notifier_block netvsc_netdev_notifier = {
2146 .notifier_call = netvsc_netdev_event,
2147};
2148
a9869c94 2149static void __exit netvsc_drv_exit(void)
fceaf24a 2150{
84bf9cef 2151 unregister_netdevice_notifier(&netvsc_netdev_notifier);
768fa219 2152 vmbus_driver_unregister(&netvsc_drv);
fceaf24a
HJ
2153}
2154
1fde28cf 2155static int __init netvsc_drv_init(void)
df2fff28 2156{
84bf9cef
KS
2157 int ret;
2158
fa85a6c2
HZ
2159 if (ring_size < RING_SIZE_MIN) {
2160 ring_size = RING_SIZE_MIN;
2161 pr_info("Increased ring_size to %d (min allowed)\n",
2162 ring_size);
2163 }
84bf9cef
KS
2164 ret = vmbus_driver_register(&netvsc_drv);
2165
2166 if (ret)
2167 return ret;
2168
2169 register_netdevice_notifier(&netvsc_netdev_notifier);
2170 return 0;
df2fff28
GKH
2171}
2172
26c14cc1 2173MODULE_LICENSE("GPL");
7880fc54 2174MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
fceaf24a 2175
1fde28cf 2176module_init(netvsc_drv_init);
a9869c94 2177module_exit(netvsc_drv_exit);