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